[ARM] Use frag's thumb_mode information when available
[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[16];
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   name      = input_line_pointer;
2872   delim     = get_symbol_end ();
2873   end_name  = input_line_pointer;
2874   *end_name = delim;
2875
2876   if (*input_line_pointer != ',')
2877     {
2878       *end_name = 0;
2879       as_bad (_("expected comma after name \"%s\""), name);
2880       *end_name = delim;
2881       ignore_rest_of_line ();
2882       return;
2883     }
2884
2885   input_line_pointer++;
2886   *end_name = 0;
2887
2888   if (name[0] == '.' && name[1] == '\0')
2889     {
2890       /* XXX - this should not happen to .thumb_set.  */
2891       abort ();
2892     }
2893
2894   if ((symbolP = symbol_find (name)) == NULL
2895       && (symbolP = md_undefined_symbol (name)) == NULL)
2896     {
2897 #ifndef NO_LISTING
2898       /* When doing symbol listings, play games with dummy fragments living
2899          outside the normal fragment chain to record the file and line info
2900          for this symbol.  */
2901       if (listing & LISTING_SYMBOLS)
2902         {
2903           extern struct list_info_struct * listing_tail;
2904           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2905
2906           memset (dummy_frag, 0, sizeof (fragS));
2907           dummy_frag->fr_type = rs_fill;
2908           dummy_frag->line = listing_tail;
2909           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2910           dummy_frag->fr_symbol = symbolP;
2911         }
2912       else
2913 #endif
2914         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2915
2916 #ifdef OBJ_COFF
2917       /* "set" symbols are local unless otherwise specified.  */
2918       SF_SET_LOCAL (symbolP);
2919 #endif /* OBJ_COFF  */
2920     }                           /* Make a new symbol.  */
2921
2922   symbol_table_insert (symbolP);
2923
2924   * end_name = delim;
2925
2926   if (equiv
2927       && S_IS_DEFINED (symbolP)
2928       && S_GET_SEGMENT (symbolP) != reg_section)
2929     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2930
2931   pseudo_set (symbolP);
2932
2933   demand_empty_rest_of_line ();
2934
2935   /* XXX Now we come to the Thumb specific bit of code.  */
2936
2937   THUMB_SET_FUNC (symbolP, 1);
2938   ARM_SET_THUMB (symbolP, 1);
2939 #if defined OBJ_ELF || defined OBJ_COFF
2940   ARM_SET_INTERWORK (symbolP, support_interwork);
2941 #endif
2942 }
2943
2944 /* Directives: Mode selection.  */
2945
2946 /* .syntax [unified|divided] - choose the new unified syntax
2947    (same for Arm and Thumb encoding, modulo slight differences in what
2948    can be represented) or the old divergent syntax for each mode.  */
2949 static void
2950 s_syntax (int unused ATTRIBUTE_UNUSED)
2951 {
2952   char *name, delim;
2953
2954   name = input_line_pointer;
2955   delim = get_symbol_end ();
2956
2957   if (!strcasecmp (name, "unified"))
2958     unified_syntax = TRUE;
2959   else if (!strcasecmp (name, "divided"))
2960     unified_syntax = FALSE;
2961   else
2962     {
2963       as_bad (_("unrecognized syntax mode \"%s\""), name);
2964       return;
2965     }
2966   *input_line_pointer = delim;
2967   demand_empty_rest_of_line ();
2968 }
2969
2970 /* Directives: sectioning and alignment.  */
2971
2972 /* Same as s_align_ptwo but align 0 => align 2.  */
2973
2974 static void
2975 s_align (int unused ATTRIBUTE_UNUSED)
2976 {
2977   int temp;
2978   bfd_boolean fill_p;
2979   long temp_fill;
2980   long max_alignment = 15;
2981
2982   temp = get_absolute_expression ();
2983   if (temp > max_alignment)
2984     as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
2985   else if (temp < 0)
2986     {
2987       as_bad (_("alignment negative. 0 assumed."));
2988       temp = 0;
2989     }
2990
2991   if (*input_line_pointer == ',')
2992     {
2993       input_line_pointer++;
2994       temp_fill = get_absolute_expression ();
2995       fill_p = TRUE;
2996     }
2997   else
2998     {
2999       fill_p = FALSE;
3000       temp_fill = 0;
3001     }
3002
3003   if (!temp)
3004     temp = 2;
3005
3006   /* Only make a frag if we HAVE to.  */
3007   if (temp && !need_pass_2)
3008     {
3009       if (!fill_p && subseg_text_p (now_seg))
3010         frag_align_code (temp, 0);
3011       else
3012         frag_align (temp, (int) temp_fill, 0);
3013     }
3014   demand_empty_rest_of_line ();
3015
3016   record_alignment (now_seg, temp);
3017 }
3018
3019 static void
3020 s_bss (int ignore ATTRIBUTE_UNUSED)
3021 {
3022   /* We don't support putting frags in the BSS segment, we fake it by
3023      marking in_bss, then looking at s_skip for clues.  */
3024   subseg_set (bss_section, 0);
3025   demand_empty_rest_of_line ();
3026
3027 #ifdef md_elf_section_change_hook
3028   md_elf_section_change_hook ();
3029 #endif
3030 }
3031
3032 static void
3033 s_even (int ignore ATTRIBUTE_UNUSED)
3034 {
3035   /* Never make frag if expect extra pass.  */
3036   if (!need_pass_2)
3037     frag_align (1, 0, 0);
3038
3039   record_alignment (now_seg, 1);
3040
3041   demand_empty_rest_of_line ();
3042 }
3043
3044 /* Directives: CodeComposer Studio.  */
3045
3046 /*  .ref  (for CodeComposer Studio syntax only).  */
3047 static void
3048 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3049 {
3050   if (codecomposer_syntax)
3051     ignore_rest_of_line ();
3052   else
3053     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3054 }
3055
3056 /*  If name is not NULL, then it is used for marking the beginning of a
3057     function, wherease if it is NULL then it means the function end.  */
3058 static void
3059 asmfunc_debug (const char * name)
3060 {
3061   static const char * last_name = NULL;
3062
3063   if (name != NULL)
3064     {
3065       gas_assert (last_name == NULL);
3066       last_name = name;
3067
3068       if (debug_type == DEBUG_STABS)
3069          stabs_generate_asm_func (name, name);
3070     }
3071   else
3072     {
3073       gas_assert (last_name != NULL);
3074
3075       if (debug_type == DEBUG_STABS)
3076         stabs_generate_asm_endfunc (last_name, last_name);
3077
3078       last_name = NULL;
3079     }
3080 }
3081
3082 static void
3083 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3084 {
3085   if (codecomposer_syntax)
3086     {
3087       switch (asmfunc_state)
3088         {
3089         case OUTSIDE_ASMFUNC:
3090           asmfunc_state = WAITING_ASMFUNC_NAME;
3091           break;
3092
3093         case WAITING_ASMFUNC_NAME:
3094           as_bad (_(".asmfunc repeated."));
3095           break;
3096
3097         case WAITING_ENDASMFUNC:
3098           as_bad (_(".asmfunc without function."));
3099           break;
3100         }
3101       demand_empty_rest_of_line ();
3102     }
3103   else
3104     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3105 }
3106
3107 static void
3108 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3109 {
3110   if (codecomposer_syntax)
3111     {
3112       switch (asmfunc_state)
3113         {
3114         case OUTSIDE_ASMFUNC:
3115           as_bad (_(".endasmfunc without a .asmfunc."));
3116           break;
3117
3118         case WAITING_ASMFUNC_NAME:
3119           as_bad (_(".endasmfunc without function."));
3120           break;
3121
3122         case WAITING_ENDASMFUNC:
3123           asmfunc_state = OUTSIDE_ASMFUNC;
3124           asmfunc_debug (NULL);
3125           break;
3126         }
3127       demand_empty_rest_of_line ();
3128     }
3129   else
3130     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3131 }
3132
3133 static void
3134 s_ccs_def (int name)
3135 {
3136   if (codecomposer_syntax)
3137     s_globl (name);
3138   else
3139     as_bad (_(".def pseudo-op only available with -mccs flag."));
3140 }
3141
3142 /* Directives: Literal pools.  */
3143
3144 static literal_pool *
3145 find_literal_pool (void)
3146 {
3147   literal_pool * pool;
3148
3149   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3150     {
3151       if (pool->section == now_seg
3152           && pool->sub_section == now_subseg)
3153         break;
3154     }
3155
3156   return pool;
3157 }
3158
3159 static literal_pool *
3160 find_or_make_literal_pool (void)
3161 {
3162   /* Next literal pool ID number.  */
3163   static unsigned int latest_pool_num = 1;
3164   literal_pool *      pool;
3165
3166   pool = find_literal_pool ();
3167
3168   if (pool == NULL)
3169     {
3170       /* Create a new pool.  */
3171       pool = (literal_pool *) xmalloc (sizeof (* pool));
3172       if (! pool)
3173         return NULL;
3174
3175       pool->next_free_entry = 0;
3176       pool->section         = now_seg;
3177       pool->sub_section     = now_subseg;
3178       pool->next            = list_of_pools;
3179       pool->symbol          = NULL;
3180       pool->alignment       = 2;
3181
3182       /* Add it to the list.  */
3183       list_of_pools = pool;
3184     }
3185
3186   /* New pools, and emptied pools, will have a NULL symbol.  */
3187   if (pool->symbol == NULL)
3188     {
3189       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3190                                     (valueT) 0, &zero_address_frag);
3191       pool->id = latest_pool_num ++;
3192     }
3193
3194   /* Done.  */
3195   return pool;
3196 }
3197
3198 /* Add the literal in the global 'inst'
3199    structure to the relevant literal pool.  */
3200
3201 static int
3202 add_to_lit_pool (unsigned int nbytes)
3203 {
3204 #define PADDING_SLOT 0x1
3205 #define LIT_ENTRY_SIZE_MASK 0xFF
3206   literal_pool * pool;
3207   unsigned int entry, pool_size = 0;
3208   bfd_boolean padding_slot_p = FALSE;
3209   unsigned imm1 = 0;
3210   unsigned imm2 = 0;
3211
3212   if (nbytes == 8)
3213     {
3214       imm1 = inst.operands[1].imm;
3215       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3216                : inst.reloc.exp.X_unsigned ? 0
3217                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3218       if (target_big_endian)
3219         {
3220           imm1 = imm2;
3221           imm2 = inst.operands[1].imm;
3222         }
3223     }
3224
3225   pool = find_or_make_literal_pool ();
3226
3227   /* Check if this literal value is already in the pool.  */
3228   for (entry = 0; entry < pool->next_free_entry; entry ++)
3229     {
3230       if (nbytes == 4)
3231         {
3232           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3233               && (inst.reloc.exp.X_op == O_constant)
3234               && (pool->literals[entry].X_add_number
3235                   == inst.reloc.exp.X_add_number)
3236               && (pool->literals[entry].X_md == nbytes)
3237               && (pool->literals[entry].X_unsigned
3238                   == inst.reloc.exp.X_unsigned))
3239             break;
3240
3241           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3242               && (inst.reloc.exp.X_op == O_symbol)
3243               && (pool->literals[entry].X_add_number
3244                   == inst.reloc.exp.X_add_number)
3245               && (pool->literals[entry].X_add_symbol
3246                   == inst.reloc.exp.X_add_symbol)
3247               && (pool->literals[entry].X_op_symbol
3248                   == inst.reloc.exp.X_op_symbol)
3249               && (pool->literals[entry].X_md == nbytes))
3250             break;
3251         }
3252       else if ((nbytes == 8)
3253                && !(pool_size & 0x7)
3254                && ((entry + 1) != pool->next_free_entry)
3255                && (pool->literals[entry].X_op == O_constant)
3256                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3257                && (pool->literals[entry].X_unsigned
3258                    == inst.reloc.exp.X_unsigned)
3259                && (pool->literals[entry + 1].X_op == O_constant)
3260                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3261                && (pool->literals[entry + 1].X_unsigned
3262                    == inst.reloc.exp.X_unsigned))
3263         break;
3264
3265       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3266       if (padding_slot_p && (nbytes == 4))
3267         break;
3268
3269       pool_size += 4;
3270     }
3271
3272   /* Do we need to create a new entry?  */
3273   if (entry == pool->next_free_entry)
3274     {
3275       if (entry >= MAX_LITERAL_POOL_SIZE)
3276         {
3277           inst.error = _("literal pool overflow");
3278           return FAIL;
3279         }
3280
3281       if (nbytes == 8)
3282         {
3283           /* For 8-byte entries, we align to an 8-byte boundary,
3284              and split it into two 4-byte entries, because on 32-bit
3285              host, 8-byte constants are treated as big num, thus
3286              saved in "generic_bignum" which will be overwritten
3287              by later assignments.
3288
3289              We also need to make sure there is enough space for
3290              the split.
3291
3292              We also check to make sure the literal operand is a
3293              constant number.  */
3294           if (!(inst.reloc.exp.X_op == O_constant
3295                 || inst.reloc.exp.X_op == O_big))
3296             {
3297               inst.error = _("invalid type for literal pool");
3298               return FAIL;
3299             }
3300           else if (pool_size & 0x7)
3301             {
3302               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3303                 {
3304                   inst.error = _("literal pool overflow");
3305                   return FAIL;
3306                 }
3307
3308               pool->literals[entry] = inst.reloc.exp;
3309               pool->literals[entry].X_add_number = 0;
3310               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3311               pool->next_free_entry += 1;
3312               pool_size += 4;
3313             }
3314           else if ((entry + 1) >= MAX_LITERAL_POOL_SIZE)
3315             {
3316               inst.error = _("literal pool overflow");
3317               return FAIL;
3318             }
3319
3320           pool->literals[entry] = inst.reloc.exp;
3321           pool->literals[entry].X_op = O_constant;
3322           pool->literals[entry].X_add_number = imm1;
3323           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3324           pool->literals[entry++].X_md = 4;
3325           pool->literals[entry] = inst.reloc.exp;
3326           pool->literals[entry].X_op = O_constant;
3327           pool->literals[entry].X_add_number = imm2;
3328           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3329           pool->literals[entry].X_md = 4;
3330           pool->alignment = 3;
3331           pool->next_free_entry += 1;
3332         }
3333       else
3334         {
3335           pool->literals[entry] = inst.reloc.exp;
3336           pool->literals[entry].X_md = 4;
3337         }
3338
3339 #ifdef OBJ_ELF
3340       /* PR ld/12974: Record the location of the first source line to reference
3341          this entry in the literal pool.  If it turns out during linking that the
3342          symbol does not exist we will be able to give an accurate line number for
3343          the (first use of the) missing reference.  */
3344       if (debug_type == DEBUG_DWARF2)
3345         dwarf2_where (pool->locs + entry);
3346 #endif
3347       pool->next_free_entry += 1;
3348     }
3349   else if (padding_slot_p)
3350     {
3351       pool->literals[entry] = inst.reloc.exp;
3352       pool->literals[entry].X_md = nbytes;
3353     }
3354
3355   inst.reloc.exp.X_op         = O_symbol;
3356   inst.reloc.exp.X_add_number = pool_size;
3357   inst.reloc.exp.X_add_symbol = pool->symbol;
3358
3359   return SUCCESS;
3360 }
3361
3362 bfd_boolean
3363 tc_start_label_without_colon (char unused1 ATTRIBUTE_UNUSED, const char * rest)
3364 {
3365   bfd_boolean ret = TRUE;
3366
3367   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3368     {
3369       const char *label = rest;
3370
3371       while (!is_end_of_line[(int) label[-1]])
3372         --label;
3373
3374       if (*label == '.')
3375         {
3376           as_bad (_("Invalid label '%s'"), label);
3377           ret = FALSE;
3378         }
3379
3380       asmfunc_debug (label);
3381
3382       asmfunc_state = WAITING_ENDASMFUNC;
3383     }
3384
3385   return ret;
3386 }
3387
3388 /* Can't use symbol_new here, so have to create a symbol and then at
3389    a later date assign it a value. Thats what these functions do.  */
3390
3391 static void
3392 symbol_locate (symbolS *    symbolP,
3393                const char * name,       /* It is copied, the caller can modify.  */
3394                segT         segment,    /* Segment identifier (SEG_<something>).  */
3395                valueT       valu,       /* Symbol value.  */
3396                fragS *      frag)       /* Associated fragment.  */
3397 {
3398   size_t name_length;
3399   char * preserved_copy_of_name;
3400
3401   name_length = strlen (name) + 1;   /* +1 for \0.  */
3402   obstack_grow (&notes, name, name_length);
3403   preserved_copy_of_name = (char *) obstack_finish (&notes);
3404
3405 #ifdef tc_canonicalize_symbol_name
3406   preserved_copy_of_name =
3407     tc_canonicalize_symbol_name (preserved_copy_of_name);
3408 #endif
3409
3410   S_SET_NAME (symbolP, preserved_copy_of_name);
3411
3412   S_SET_SEGMENT (symbolP, segment);
3413   S_SET_VALUE (symbolP, valu);
3414   symbol_clear_list_pointers (symbolP);
3415
3416   symbol_set_frag (symbolP, frag);
3417
3418   /* Link to end of symbol chain.  */
3419   {
3420     extern int symbol_table_frozen;
3421
3422     if (symbol_table_frozen)
3423       abort ();
3424   }
3425
3426   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3427
3428   obj_symbol_new_hook (symbolP);
3429
3430 #ifdef tc_symbol_new_hook
3431   tc_symbol_new_hook (symbolP);
3432 #endif
3433
3434 #ifdef DEBUG_SYMS
3435   verify_symbol_chain (symbol_rootP, symbol_lastP);
3436 #endif /* DEBUG_SYMS  */
3437 }
3438
3439 static void
3440 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3441 {
3442   unsigned int entry;
3443   literal_pool * pool;
3444   char sym_name[20];
3445
3446   pool = find_literal_pool ();
3447   if (pool == NULL
3448       || pool->symbol == NULL
3449       || pool->next_free_entry == 0)
3450     return;
3451
3452   /* Align pool as you have word accesses.
3453      Only make a frag if we have to.  */
3454   if (!need_pass_2)
3455     frag_align (pool->alignment, 0, 0);
3456
3457   record_alignment (now_seg, 2);
3458
3459 #ifdef OBJ_ELF
3460   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3461   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3462 #endif
3463   sprintf (sym_name, "$$lit_\002%x", pool->id);
3464
3465   symbol_locate (pool->symbol, sym_name, now_seg,
3466                  (valueT) frag_now_fix (), frag_now);
3467   symbol_table_insert (pool->symbol);
3468
3469   ARM_SET_THUMB (pool->symbol, thumb_mode);
3470
3471 #if defined OBJ_COFF || defined OBJ_ELF
3472   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3473 #endif
3474
3475   for (entry = 0; entry < pool->next_free_entry; entry ++)
3476     {
3477 #ifdef OBJ_ELF
3478       if (debug_type == DEBUG_DWARF2)
3479         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3480 #endif
3481       /* First output the expression in the instruction to the pool.  */
3482       emit_expr (&(pool->literals[entry]),
3483                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3484     }
3485
3486   /* Mark the pool as empty.  */
3487   pool->next_free_entry = 0;
3488   pool->symbol = NULL;
3489 }
3490
3491 #ifdef OBJ_ELF
3492 /* Forward declarations for functions below, in the MD interface
3493    section.  */
3494 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3495 static valueT create_unwind_entry (int);
3496 static void start_unwind_section (const segT, int);
3497 static void add_unwind_opcode (valueT, int);
3498 static void flush_pending_unwind (void);
3499
3500 /* Directives: Data.  */
3501
3502 static void
3503 s_arm_elf_cons (int nbytes)
3504 {
3505   expressionS exp;
3506
3507 #ifdef md_flush_pending_output
3508   md_flush_pending_output ();
3509 #endif
3510
3511   if (is_it_end_of_statement ())
3512     {
3513       demand_empty_rest_of_line ();
3514       return;
3515     }
3516
3517 #ifdef md_cons_align
3518   md_cons_align (nbytes);
3519 #endif
3520
3521   mapping_state (MAP_DATA);
3522   do
3523     {
3524       int reloc;
3525       char *base = input_line_pointer;
3526
3527       expression (& exp);
3528
3529       if (exp.X_op != O_symbol)
3530         emit_expr (&exp, (unsigned int) nbytes);
3531       else
3532         {
3533           char *before_reloc = input_line_pointer;
3534           reloc = parse_reloc (&input_line_pointer);
3535           if (reloc == -1)
3536             {
3537               as_bad (_("unrecognized relocation suffix"));
3538               ignore_rest_of_line ();
3539               return;
3540             }
3541           else if (reloc == BFD_RELOC_UNUSED)
3542             emit_expr (&exp, (unsigned int) nbytes);
3543           else
3544             {
3545               reloc_howto_type *howto = (reloc_howto_type *)
3546                   bfd_reloc_type_lookup (stdoutput,
3547                                          (bfd_reloc_code_real_type) reloc);
3548               int size = bfd_get_reloc_size (howto);
3549
3550               if (reloc == BFD_RELOC_ARM_PLT32)
3551                 {
3552                   as_bad (_("(plt) is only valid on branch targets"));
3553                   reloc = BFD_RELOC_UNUSED;
3554                   size = 0;
3555                 }
3556
3557               if (size > nbytes)
3558                 as_bad (_("%s relocations do not fit in %d bytes"),
3559                         howto->name, nbytes);
3560               else
3561                 {
3562                   /* We've parsed an expression stopping at O_symbol.
3563                      But there may be more expression left now that we
3564                      have parsed the relocation marker.  Parse it again.
3565                      XXX Surely there is a cleaner way to do this.  */
3566                   char *p = input_line_pointer;
3567                   int offset;
3568                   char *save_buf = (char *) alloca (input_line_pointer - base);
3569                   memcpy (save_buf, base, input_line_pointer - base);
3570                   memmove (base + (input_line_pointer - before_reloc),
3571                            base, before_reloc - base);
3572
3573                   input_line_pointer = base + (input_line_pointer-before_reloc);
3574                   expression (&exp);
3575                   memcpy (base, save_buf, p - base);
3576
3577                   offset = nbytes - size;
3578                   p = frag_more (nbytes);
3579                   memset (p, 0, nbytes);
3580                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3581                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3582                 }
3583             }
3584         }
3585     }
3586   while (*input_line_pointer++ == ',');
3587
3588   /* Put terminator back into stream.  */
3589   input_line_pointer --;
3590   demand_empty_rest_of_line ();
3591 }
3592
3593 /* Emit an expression containing a 32-bit thumb instruction.
3594    Implementation based on put_thumb32_insn.  */
3595
3596 static void
3597 emit_thumb32_expr (expressionS * exp)
3598 {
3599   expressionS exp_high = *exp;
3600
3601   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3602   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3603   exp->X_add_number &= 0xffff;
3604   emit_expr (exp, (unsigned int) THUMB_SIZE);
3605 }
3606
3607 /*  Guess the instruction size based on the opcode.  */
3608
3609 static int
3610 thumb_insn_size (int opcode)
3611 {
3612   if ((unsigned int) opcode < 0xe800u)
3613     return 2;
3614   else if ((unsigned int) opcode >= 0xe8000000u)
3615     return 4;
3616   else
3617     return 0;
3618 }
3619
3620 static bfd_boolean
3621 emit_insn (expressionS *exp, int nbytes)
3622 {
3623   int size = 0;
3624
3625   if (exp->X_op == O_constant)
3626     {
3627       size = nbytes;
3628
3629       if (size == 0)
3630         size = thumb_insn_size (exp->X_add_number);
3631
3632       if (size != 0)
3633         {
3634           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3635             {
3636               as_bad (_(".inst.n operand too big. "\
3637                         "Use .inst.w instead"));
3638               size = 0;
3639             }
3640           else
3641             {
3642               if (now_it.state == AUTOMATIC_IT_BLOCK)
3643                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3644               else
3645                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3646
3647               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3648                 emit_thumb32_expr (exp);
3649               else
3650                 emit_expr (exp, (unsigned int) size);
3651
3652               it_fsm_post_encode ();
3653             }
3654         }
3655       else
3656         as_bad (_("cannot determine Thumb instruction size. "   \
3657                   "Use .inst.n/.inst.w instead"));
3658     }
3659   else
3660     as_bad (_("constant expression required"));
3661
3662   return (size != 0);
3663 }
3664
3665 /* Like s_arm_elf_cons but do not use md_cons_align and
3666    set the mapping state to MAP_ARM/MAP_THUMB.  */
3667
3668 static void
3669 s_arm_elf_inst (int nbytes)
3670 {
3671   if (is_it_end_of_statement ())
3672     {
3673       demand_empty_rest_of_line ();
3674       return;
3675     }
3676
3677   /* Calling mapping_state () here will not change ARM/THUMB,
3678      but will ensure not to be in DATA state.  */
3679
3680   if (thumb_mode)
3681     mapping_state (MAP_THUMB);
3682   else
3683     {
3684       if (nbytes != 0)
3685         {
3686           as_bad (_("width suffixes are invalid in ARM mode"));
3687           ignore_rest_of_line ();
3688           return;
3689         }
3690
3691       nbytes = 4;
3692
3693       mapping_state (MAP_ARM);
3694     }
3695
3696   do
3697     {
3698       expressionS exp;
3699
3700       expression (& exp);
3701
3702       if (! emit_insn (& exp, nbytes))
3703         {
3704           ignore_rest_of_line ();
3705           return;
3706         }
3707     }
3708   while (*input_line_pointer++ == ',');
3709
3710   /* Put terminator back into stream.  */
3711   input_line_pointer --;
3712   demand_empty_rest_of_line ();
3713 }
3714
3715 /* Parse a .rel31 directive.  */
3716
3717 static void
3718 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3719 {
3720   expressionS exp;
3721   char *p;
3722   valueT highbit;
3723
3724   highbit = 0;
3725   if (*input_line_pointer == '1')
3726     highbit = 0x80000000;
3727   else if (*input_line_pointer != '0')
3728     as_bad (_("expected 0 or 1"));
3729
3730   input_line_pointer++;
3731   if (*input_line_pointer != ',')
3732     as_bad (_("missing comma"));
3733   input_line_pointer++;
3734
3735 #ifdef md_flush_pending_output
3736   md_flush_pending_output ();
3737 #endif
3738
3739 #ifdef md_cons_align
3740   md_cons_align (4);
3741 #endif
3742
3743   mapping_state (MAP_DATA);
3744
3745   expression (&exp);
3746
3747   p = frag_more (4);
3748   md_number_to_chars (p, highbit, 4);
3749   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3750                BFD_RELOC_ARM_PREL31);
3751
3752   demand_empty_rest_of_line ();
3753 }
3754
3755 /* Directives: AEABI stack-unwind tables.  */
3756
3757 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3758
3759 static void
3760 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3761 {
3762   demand_empty_rest_of_line ();
3763   if (unwind.proc_start)
3764     {
3765       as_bad (_("duplicate .fnstart directive"));
3766       return;
3767     }
3768
3769   /* Mark the start of the function.  */
3770   unwind.proc_start = expr_build_dot ();
3771
3772   /* Reset the rest of the unwind info.  */
3773   unwind.opcode_count = 0;
3774   unwind.table_entry = NULL;
3775   unwind.personality_routine = NULL;
3776   unwind.personality_index = -1;
3777   unwind.frame_size = 0;
3778   unwind.fp_offset = 0;
3779   unwind.fp_reg = REG_SP;
3780   unwind.fp_used = 0;
3781   unwind.sp_restored = 0;
3782 }
3783
3784
3785 /* Parse a handlerdata directive.  Creates the exception handling table entry
3786    for the function.  */
3787
3788 static void
3789 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3790 {
3791   demand_empty_rest_of_line ();
3792   if (!unwind.proc_start)
3793     as_bad (MISSING_FNSTART);
3794
3795   if (unwind.table_entry)
3796     as_bad (_("duplicate .handlerdata directive"));
3797
3798   create_unwind_entry (1);
3799 }
3800
3801 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3802
3803 static void
3804 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3805 {
3806   long where;
3807   char *ptr;
3808   valueT val;
3809   unsigned int marked_pr_dependency;
3810
3811   demand_empty_rest_of_line ();
3812
3813   if (!unwind.proc_start)
3814     {
3815       as_bad (_(".fnend directive without .fnstart"));
3816       return;
3817     }
3818
3819   /* Add eh table entry.  */
3820   if (unwind.table_entry == NULL)
3821     val = create_unwind_entry (0);
3822   else
3823     val = 0;
3824
3825   /* Add index table entry.  This is two words.  */
3826   start_unwind_section (unwind.saved_seg, 1);
3827   frag_align (2, 0, 0);
3828   record_alignment (now_seg, 2);
3829
3830   ptr = frag_more (8);
3831   memset (ptr, 0, 8);
3832   where = frag_now_fix () - 8;
3833
3834   /* Self relative offset of the function start.  */
3835   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3836            BFD_RELOC_ARM_PREL31);
3837
3838   /* Indicate dependency on EHABI-defined personality routines to the
3839      linker, if it hasn't been done already.  */
3840   marked_pr_dependency
3841     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3842   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3843       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3844     {
3845       static const char *const name[] =
3846         {
3847           "__aeabi_unwind_cpp_pr0",
3848           "__aeabi_unwind_cpp_pr1",
3849           "__aeabi_unwind_cpp_pr2"
3850         };
3851       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3852       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3853       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3854         |= 1 << unwind.personality_index;
3855     }
3856
3857   if (val)
3858     /* Inline exception table entry.  */
3859     md_number_to_chars (ptr + 4, val, 4);
3860   else
3861     /* Self relative offset of the table entry.  */
3862     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3863              BFD_RELOC_ARM_PREL31);
3864
3865   /* Restore the original section.  */
3866   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3867
3868   unwind.proc_start = NULL;
3869 }
3870
3871
3872 /* Parse an unwind_cantunwind directive.  */
3873
3874 static void
3875 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3876 {
3877   demand_empty_rest_of_line ();
3878   if (!unwind.proc_start)
3879     as_bad (MISSING_FNSTART);
3880
3881   if (unwind.personality_routine || unwind.personality_index != -1)
3882     as_bad (_("personality routine specified for cantunwind frame"));
3883
3884   unwind.personality_index = -2;
3885 }
3886
3887
3888 /* Parse a personalityindex directive.  */
3889
3890 static void
3891 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3892 {
3893   expressionS exp;
3894
3895   if (!unwind.proc_start)
3896     as_bad (MISSING_FNSTART);
3897
3898   if (unwind.personality_routine || unwind.personality_index != -1)
3899     as_bad (_("duplicate .personalityindex directive"));
3900
3901   expression (&exp);
3902
3903   if (exp.X_op != O_constant
3904       || exp.X_add_number < 0 || exp.X_add_number > 15)
3905     {
3906       as_bad (_("bad personality routine number"));
3907       ignore_rest_of_line ();
3908       return;
3909     }
3910
3911   unwind.personality_index = exp.X_add_number;
3912
3913   demand_empty_rest_of_line ();
3914 }
3915
3916
3917 /* Parse a personality directive.  */
3918
3919 static void
3920 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3921 {
3922   char *name, *p, c;
3923
3924   if (!unwind.proc_start)
3925     as_bad (MISSING_FNSTART);
3926
3927   if (unwind.personality_routine || unwind.personality_index != -1)
3928     as_bad (_("duplicate .personality directive"));
3929
3930   name = input_line_pointer;
3931   c = get_symbol_end ();
3932   p = input_line_pointer;
3933   unwind.personality_routine = symbol_find_or_make (name);
3934   *p = c;
3935   demand_empty_rest_of_line ();
3936 }
3937
3938
3939 /* Parse a directive saving core registers.  */
3940
3941 static void
3942 s_arm_unwind_save_core (void)
3943 {
3944   valueT op;
3945   long range;
3946   int n;
3947
3948   range = parse_reg_list (&input_line_pointer);
3949   if (range == FAIL)
3950     {
3951       as_bad (_("expected register list"));
3952       ignore_rest_of_line ();
3953       return;
3954     }
3955
3956   demand_empty_rest_of_line ();
3957
3958   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3959      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3960      ip because it is clobbered by calls.  */
3961   if (unwind.sp_restored && unwind.fp_reg == 12
3962       && (range & 0x3000) == 0x1000)
3963     {
3964       unwind.opcode_count--;
3965       unwind.sp_restored = 0;
3966       range = (range | 0x2000) & ~0x1000;
3967       unwind.pending_offset = 0;
3968     }
3969
3970   /* Pop r4-r15.  */
3971   if (range & 0xfff0)
3972     {
3973       /* See if we can use the short opcodes.  These pop a block of up to 8
3974          registers starting with r4, plus maybe r14.  */
3975       for (n = 0; n < 8; n++)
3976         {
3977           /* Break at the first non-saved register.      */
3978           if ((range & (1 << (n + 4))) == 0)
3979             break;
3980         }
3981       /* See if there are any other bits set.  */
3982       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
3983         {
3984           /* Use the long form.  */
3985           op = 0x8000 | ((range >> 4) & 0xfff);
3986           add_unwind_opcode (op, 2);
3987         }
3988       else
3989         {
3990           /* Use the short form.  */
3991           if (range & 0x4000)
3992             op = 0xa8; /* Pop r14.      */
3993           else
3994             op = 0xa0; /* Do not pop r14.  */
3995           op |= (n - 1);
3996           add_unwind_opcode (op, 1);
3997         }
3998     }
3999
4000   /* Pop r0-r3.  */
4001   if (range & 0xf)
4002     {
4003       op = 0xb100 | (range & 0xf);
4004       add_unwind_opcode (op, 2);
4005     }
4006
4007   /* Record the number of bytes pushed.  */
4008   for (n = 0; n < 16; n++)
4009     {
4010       if (range & (1 << n))
4011         unwind.frame_size += 4;
4012     }
4013 }
4014
4015
4016 /* Parse a directive saving FPA registers.  */
4017
4018 static void
4019 s_arm_unwind_save_fpa (int reg)
4020 {
4021   expressionS exp;
4022   int num_regs;
4023   valueT op;
4024
4025   /* Get Number of registers to transfer.  */
4026   if (skip_past_comma (&input_line_pointer) != FAIL)
4027     expression (&exp);
4028   else
4029     exp.X_op = O_illegal;
4030
4031   if (exp.X_op != O_constant)
4032     {
4033       as_bad (_("expected , <constant>"));
4034       ignore_rest_of_line ();
4035       return;
4036     }
4037
4038   num_regs = exp.X_add_number;
4039
4040   if (num_regs < 1 || num_regs > 4)
4041     {
4042       as_bad (_("number of registers must be in the range [1:4]"));
4043       ignore_rest_of_line ();
4044       return;
4045     }
4046
4047   demand_empty_rest_of_line ();
4048
4049   if (reg == 4)
4050     {
4051       /* Short form.  */
4052       op = 0xb4 | (num_regs - 1);
4053       add_unwind_opcode (op, 1);
4054     }
4055   else
4056     {
4057       /* Long form.  */
4058       op = 0xc800 | (reg << 4) | (num_regs - 1);
4059       add_unwind_opcode (op, 2);
4060     }
4061   unwind.frame_size += num_regs * 12;
4062 }
4063
4064
4065 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4066
4067 static void
4068 s_arm_unwind_save_vfp_armv6 (void)
4069 {
4070   int count;
4071   unsigned int start;
4072   valueT op;
4073   int num_vfpv3_regs = 0;
4074   int num_regs_below_16;
4075
4076   count = parse_vfp_reg_list (&input_line_pointer, &start, 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   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4087      than FSTMX/FLDMX-style ones).  */
4088
4089   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4090   if (start >= 16)
4091     num_vfpv3_regs = count;
4092   else if (start + count > 16)
4093     num_vfpv3_regs = start + count - 16;
4094
4095   if (num_vfpv3_regs > 0)
4096     {
4097       int start_offset = start > 16 ? start - 16 : 0;
4098       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4099       add_unwind_opcode (op, 2);
4100     }
4101
4102   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4103   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4104   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4105   if (num_regs_below_16 > 0)
4106     {
4107       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4108       add_unwind_opcode (op, 2);
4109     }
4110
4111   unwind.frame_size += count * 8;
4112 }
4113
4114
4115 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4116
4117 static void
4118 s_arm_unwind_save_vfp (void)
4119 {
4120   int count;
4121   unsigned int reg;
4122   valueT op;
4123
4124   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
4125   if (count == FAIL)
4126     {
4127       as_bad (_("expected register list"));
4128       ignore_rest_of_line ();
4129       return;
4130     }
4131
4132   demand_empty_rest_of_line ();
4133
4134   if (reg == 8)
4135     {
4136       /* Short form.  */
4137       op = 0xb8 | (count - 1);
4138       add_unwind_opcode (op, 1);
4139     }
4140   else
4141     {
4142       /* Long form.  */
4143       op = 0xb300 | (reg << 4) | (count - 1);
4144       add_unwind_opcode (op, 2);
4145     }
4146   unwind.frame_size += count * 8 + 4;
4147 }
4148
4149
4150 /* Parse a directive saving iWMMXt data registers.  */
4151
4152 static void
4153 s_arm_unwind_save_mmxwr (void)
4154 {
4155   int reg;
4156   int hi_reg;
4157   int i;
4158   unsigned mask = 0;
4159   valueT op;
4160
4161   if (*input_line_pointer == '{')
4162     input_line_pointer++;
4163
4164   do
4165     {
4166       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4167
4168       if (reg == FAIL)
4169         {
4170           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4171           goto error;
4172         }
4173
4174       if (mask >> reg)
4175         as_tsktsk (_("register list not in ascending order"));
4176       mask |= 1 << reg;
4177
4178       if (*input_line_pointer == '-')
4179         {
4180           input_line_pointer++;
4181           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4182           if (hi_reg == FAIL)
4183             {
4184               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4185               goto error;
4186             }
4187           else if (reg >= hi_reg)
4188             {
4189               as_bad (_("bad register range"));
4190               goto error;
4191             }
4192           for (; reg < hi_reg; reg++)
4193             mask |= 1 << reg;
4194         }
4195     }
4196   while (skip_past_comma (&input_line_pointer) != FAIL);
4197
4198   skip_past_char (&input_line_pointer, '}');
4199
4200   demand_empty_rest_of_line ();
4201
4202   /* Generate any deferred opcodes because we're going to be looking at
4203      the list.  */
4204   flush_pending_unwind ();
4205
4206   for (i = 0; i < 16; i++)
4207     {
4208       if (mask & (1 << i))
4209         unwind.frame_size += 8;
4210     }
4211
4212   /* Attempt to combine with a previous opcode.  We do this because gcc
4213      likes to output separate unwind directives for a single block of
4214      registers.  */
4215   if (unwind.opcode_count > 0)
4216     {
4217       i = unwind.opcodes[unwind.opcode_count - 1];
4218       if ((i & 0xf8) == 0xc0)
4219         {
4220           i &= 7;
4221           /* Only merge if the blocks are contiguous.  */
4222           if (i < 6)
4223             {
4224               if ((mask & 0xfe00) == (1 << 9))
4225                 {
4226                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4227                   unwind.opcode_count--;
4228                 }
4229             }
4230           else if (i == 6 && unwind.opcode_count >= 2)
4231             {
4232               i = unwind.opcodes[unwind.opcode_count - 2];
4233               reg = i >> 4;
4234               i &= 0xf;
4235
4236               op = 0xffff << (reg - 1);
4237               if (reg > 0
4238                   && ((mask & op) == (1u << (reg - 1))))
4239                 {
4240                   op = (1 << (reg + i + 1)) - 1;
4241                   op &= ~((1 << reg) - 1);
4242                   mask |= op;
4243                   unwind.opcode_count -= 2;
4244                 }
4245             }
4246         }
4247     }
4248
4249   hi_reg = 15;
4250   /* We want to generate opcodes in the order the registers have been
4251      saved, ie. descending order.  */
4252   for (reg = 15; reg >= -1; reg--)
4253     {
4254       /* Save registers in blocks.  */
4255       if (reg < 0
4256           || !(mask & (1 << reg)))
4257         {
4258           /* We found an unsaved reg.  Generate opcodes to save the
4259              preceding block.   */
4260           if (reg != hi_reg)
4261             {
4262               if (reg == 9)
4263                 {
4264                   /* Short form.  */
4265                   op = 0xc0 | (hi_reg - 10);
4266                   add_unwind_opcode (op, 1);
4267                 }
4268               else
4269                 {
4270                   /* Long form.  */
4271                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4272                   add_unwind_opcode (op, 2);
4273                 }
4274             }
4275           hi_reg = reg - 1;
4276         }
4277     }
4278
4279   return;
4280 error:
4281   ignore_rest_of_line ();
4282 }
4283
4284 static void
4285 s_arm_unwind_save_mmxwcg (void)
4286 {
4287   int reg;
4288   int hi_reg;
4289   unsigned mask = 0;
4290   valueT op;
4291
4292   if (*input_line_pointer == '{')
4293     input_line_pointer++;
4294
4295   skip_whitespace (input_line_pointer);
4296
4297   do
4298     {
4299       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4300
4301       if (reg == FAIL)
4302         {
4303           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4304           goto error;
4305         }
4306
4307       reg -= 8;
4308       if (mask >> reg)
4309         as_tsktsk (_("register list not in ascending order"));
4310       mask |= 1 << reg;
4311
4312       if (*input_line_pointer == '-')
4313         {
4314           input_line_pointer++;
4315           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4316           if (hi_reg == FAIL)
4317             {
4318               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4319               goto error;
4320             }
4321           else if (reg >= hi_reg)
4322             {
4323               as_bad (_("bad register range"));
4324               goto error;
4325             }
4326           for (; reg < hi_reg; reg++)
4327             mask |= 1 << reg;
4328         }
4329     }
4330   while (skip_past_comma (&input_line_pointer) != FAIL);
4331
4332   skip_past_char (&input_line_pointer, '}');
4333
4334   demand_empty_rest_of_line ();
4335
4336   /* Generate any deferred opcodes because we're going to be looking at
4337      the list.  */
4338   flush_pending_unwind ();
4339
4340   for (reg = 0; reg < 16; reg++)
4341     {
4342       if (mask & (1 << reg))
4343         unwind.frame_size += 4;
4344     }
4345   op = 0xc700 | mask;
4346   add_unwind_opcode (op, 2);
4347   return;
4348 error:
4349   ignore_rest_of_line ();
4350 }
4351
4352
4353 /* Parse an unwind_save directive.
4354    If the argument is non-zero, this is a .vsave directive.  */
4355
4356 static void
4357 s_arm_unwind_save (int arch_v6)
4358 {
4359   char *peek;
4360   struct reg_entry *reg;
4361   bfd_boolean had_brace = FALSE;
4362
4363   if (!unwind.proc_start)
4364     as_bad (MISSING_FNSTART);
4365
4366   /* Figure out what sort of save we have.  */
4367   peek = input_line_pointer;
4368
4369   if (*peek == '{')
4370     {
4371       had_brace = TRUE;
4372       peek++;
4373     }
4374
4375   reg = arm_reg_parse_multi (&peek);
4376
4377   if (!reg)
4378     {
4379       as_bad (_("register expected"));
4380       ignore_rest_of_line ();
4381       return;
4382     }
4383
4384   switch (reg->type)
4385     {
4386     case REG_TYPE_FN:
4387       if (had_brace)
4388         {
4389           as_bad (_("FPA .unwind_save does not take a register list"));
4390           ignore_rest_of_line ();
4391           return;
4392         }
4393       input_line_pointer = peek;
4394       s_arm_unwind_save_fpa (reg->number);
4395       return;
4396
4397     case REG_TYPE_RN:
4398       s_arm_unwind_save_core ();
4399       return;
4400
4401     case REG_TYPE_VFD:
4402       if (arch_v6)
4403         s_arm_unwind_save_vfp_armv6 ();
4404       else
4405         s_arm_unwind_save_vfp ();
4406       return;
4407
4408     case REG_TYPE_MMXWR:
4409       s_arm_unwind_save_mmxwr ();
4410       return;
4411
4412     case REG_TYPE_MMXWCG:
4413       s_arm_unwind_save_mmxwcg ();
4414       return;
4415
4416     default:
4417       as_bad (_(".unwind_save does not support this kind of register"));
4418       ignore_rest_of_line ();
4419     }
4420 }
4421
4422
4423 /* Parse an unwind_movsp directive.  */
4424
4425 static void
4426 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4427 {
4428   int reg;
4429   valueT op;
4430   int offset;
4431
4432   if (!unwind.proc_start)
4433     as_bad (MISSING_FNSTART);
4434
4435   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4436   if (reg == FAIL)
4437     {
4438       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4439       ignore_rest_of_line ();
4440       return;
4441     }
4442
4443   /* Optional constant.  */
4444   if (skip_past_comma (&input_line_pointer) != FAIL)
4445     {
4446       if (immediate_for_directive (&offset) == FAIL)
4447         return;
4448     }
4449   else
4450     offset = 0;
4451
4452   demand_empty_rest_of_line ();
4453
4454   if (reg == REG_SP || reg == REG_PC)
4455     {
4456       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4457       return;
4458     }
4459
4460   if (unwind.fp_reg != REG_SP)
4461     as_bad (_("unexpected .unwind_movsp directive"));
4462
4463   /* Generate opcode to restore the value.  */
4464   op = 0x90 | reg;
4465   add_unwind_opcode (op, 1);
4466
4467   /* Record the information for later.  */
4468   unwind.fp_reg = reg;
4469   unwind.fp_offset = unwind.frame_size - offset;
4470   unwind.sp_restored = 1;
4471 }
4472
4473 /* Parse an unwind_pad directive.  */
4474
4475 static void
4476 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4477 {
4478   int offset;
4479
4480   if (!unwind.proc_start)
4481     as_bad (MISSING_FNSTART);
4482
4483   if (immediate_for_directive (&offset) == FAIL)
4484     return;
4485
4486   if (offset & 3)
4487     {
4488       as_bad (_("stack increment must be multiple of 4"));
4489       ignore_rest_of_line ();
4490       return;
4491     }
4492
4493   /* Don't generate any opcodes, just record the details for later.  */
4494   unwind.frame_size += offset;
4495   unwind.pending_offset += offset;
4496
4497   demand_empty_rest_of_line ();
4498 }
4499
4500 /* Parse an unwind_setfp directive.  */
4501
4502 static void
4503 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4504 {
4505   int sp_reg;
4506   int fp_reg;
4507   int offset;
4508
4509   if (!unwind.proc_start)
4510     as_bad (MISSING_FNSTART);
4511
4512   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4513   if (skip_past_comma (&input_line_pointer) == FAIL)
4514     sp_reg = FAIL;
4515   else
4516     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4517
4518   if (fp_reg == FAIL || sp_reg == FAIL)
4519     {
4520       as_bad (_("expected <reg>, <reg>"));
4521       ignore_rest_of_line ();
4522       return;
4523     }
4524
4525   /* Optional constant.  */
4526   if (skip_past_comma (&input_line_pointer) != FAIL)
4527     {
4528       if (immediate_for_directive (&offset) == FAIL)
4529         return;
4530     }
4531   else
4532     offset = 0;
4533
4534   demand_empty_rest_of_line ();
4535
4536   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4537     {
4538       as_bad (_("register must be either sp or set by a previous"
4539                 "unwind_movsp directive"));
4540       return;
4541     }
4542
4543   /* Don't generate any opcodes, just record the information for later.  */
4544   unwind.fp_reg = fp_reg;
4545   unwind.fp_used = 1;
4546   if (sp_reg == REG_SP)
4547     unwind.fp_offset = unwind.frame_size - offset;
4548   else
4549     unwind.fp_offset -= offset;
4550 }
4551
4552 /* Parse an unwind_raw directive.  */
4553
4554 static void
4555 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4556 {
4557   expressionS exp;
4558   /* This is an arbitrary limit.         */
4559   unsigned char op[16];
4560   int count;
4561
4562   if (!unwind.proc_start)
4563     as_bad (MISSING_FNSTART);
4564
4565   expression (&exp);
4566   if (exp.X_op == O_constant
4567       && skip_past_comma (&input_line_pointer) != FAIL)
4568     {
4569       unwind.frame_size += exp.X_add_number;
4570       expression (&exp);
4571     }
4572   else
4573     exp.X_op = O_illegal;
4574
4575   if (exp.X_op != O_constant)
4576     {
4577       as_bad (_("expected <offset>, <opcode>"));
4578       ignore_rest_of_line ();
4579       return;
4580     }
4581
4582   count = 0;
4583
4584   /* Parse the opcode.  */
4585   for (;;)
4586     {
4587       if (count >= 16)
4588         {
4589           as_bad (_("unwind opcode too long"));
4590           ignore_rest_of_line ();
4591         }
4592       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4593         {
4594           as_bad (_("invalid unwind opcode"));
4595           ignore_rest_of_line ();
4596           return;
4597         }
4598       op[count++] = exp.X_add_number;
4599
4600       /* Parse the next byte.  */
4601       if (skip_past_comma (&input_line_pointer) == FAIL)
4602         break;
4603
4604       expression (&exp);
4605     }
4606
4607   /* Add the opcode bytes in reverse order.  */
4608   while (count--)
4609     add_unwind_opcode (op[count], 1);
4610
4611   demand_empty_rest_of_line ();
4612 }
4613
4614
4615 /* Parse a .eabi_attribute directive.  */
4616
4617 static void
4618 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4619 {
4620   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4621
4622   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4623     attributes_set_explicitly[tag] = 1;
4624 }
4625
4626 /* Emit a tls fix for the symbol.  */
4627
4628 static void
4629 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4630 {
4631   char *p;
4632   expressionS exp;
4633 #ifdef md_flush_pending_output
4634   md_flush_pending_output ();
4635 #endif
4636
4637 #ifdef md_cons_align
4638   md_cons_align (4);
4639 #endif
4640
4641   /* Since we're just labelling the code, there's no need to define a
4642      mapping symbol.  */
4643   expression (&exp);
4644   p = obstack_next_free (&frchain_now->frch_obstack);
4645   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4646                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4647                : BFD_RELOC_ARM_TLS_DESCSEQ);
4648 }
4649 #endif /* OBJ_ELF */
4650
4651 static void s_arm_arch (int);
4652 static void s_arm_object_arch (int);
4653 static void s_arm_cpu (int);
4654 static void s_arm_fpu (int);
4655 static void s_arm_arch_extension (int);
4656
4657 #ifdef TE_PE
4658
4659 static void
4660 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4661 {
4662   expressionS exp;
4663
4664   do
4665     {
4666       expression (&exp);
4667       if (exp.X_op == O_symbol)
4668         exp.X_op = O_secrel;
4669
4670       emit_expr (&exp, 4);
4671     }
4672   while (*input_line_pointer++ == ',');
4673
4674   input_line_pointer--;
4675   demand_empty_rest_of_line ();
4676 }
4677 #endif /* TE_PE */
4678
4679 /* This table describes all the machine specific pseudo-ops the assembler
4680    has to support.  The fields are:
4681      pseudo-op name without dot
4682      function to call to execute this pseudo-op
4683      Integer arg to pass to the function.  */
4684
4685 const pseudo_typeS md_pseudo_table[] =
4686 {
4687   /* Never called because '.req' does not start a line.  */
4688   { "req",         s_req,         0 },
4689   /* Following two are likewise never called.  */
4690   { "dn",          s_dn,          0 },
4691   { "qn",          s_qn,          0 },
4692   { "unreq",       s_unreq,       0 },
4693   { "bss",         s_bss,         0 },
4694   { "align",       s_align,       0 },
4695   { "arm",         s_arm,         0 },
4696   { "thumb",       s_thumb,       0 },
4697   { "code",        s_code,        0 },
4698   { "force_thumb", s_force_thumb, 0 },
4699   { "thumb_func",  s_thumb_func,  0 },
4700   { "thumb_set",   s_thumb_set,   0 },
4701   { "even",        s_even,        0 },
4702   { "ltorg",       s_ltorg,       0 },
4703   { "pool",        s_ltorg,       0 },
4704   { "syntax",      s_syntax,      0 },
4705   { "cpu",         s_arm_cpu,     0 },
4706   { "arch",        s_arm_arch,    0 },
4707   { "object_arch", s_arm_object_arch,   0 },
4708   { "fpu",         s_arm_fpu,     0 },
4709   { "arch_extension", s_arm_arch_extension, 0 },
4710 #ifdef OBJ_ELF
4711   { "word",             s_arm_elf_cons, 4 },
4712   { "long",             s_arm_elf_cons, 4 },
4713   { "inst.n",           s_arm_elf_inst, 2 },
4714   { "inst.w",           s_arm_elf_inst, 4 },
4715   { "inst",             s_arm_elf_inst, 0 },
4716   { "rel31",            s_arm_rel31,      0 },
4717   { "fnstart",          s_arm_unwind_fnstart,   0 },
4718   { "fnend",            s_arm_unwind_fnend,     0 },
4719   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4720   { "personality",      s_arm_unwind_personality, 0 },
4721   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4722   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4723   { "save",             s_arm_unwind_save,      0 },
4724   { "vsave",            s_arm_unwind_save,      1 },
4725   { "movsp",            s_arm_unwind_movsp,     0 },
4726   { "pad",              s_arm_unwind_pad,       0 },
4727   { "setfp",            s_arm_unwind_setfp,     0 },
4728   { "unwind_raw",       s_arm_unwind_raw,       0 },
4729   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4730   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4731 #else
4732   { "word",        cons, 4},
4733
4734   /* These are used for dwarf.  */
4735   {"2byte", cons, 2},
4736   {"4byte", cons, 4},
4737   {"8byte", cons, 8},
4738   /* These are used for dwarf2.  */
4739   { "file", (void (*) (int)) dwarf2_directive_file, 0 },
4740   { "loc",  dwarf2_directive_loc,  0 },
4741   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4742 #endif
4743   { "extend",      float_cons, 'x' },
4744   { "ldouble",     float_cons, 'x' },
4745   { "packed",      float_cons, 'p' },
4746 #ifdef TE_PE
4747   {"secrel32", pe_directive_secrel, 0},
4748 #endif
4749
4750   /* These are for compatibility with CodeComposer Studio.  */
4751   {"ref",          s_ccs_ref,        0},
4752   {"def",          s_ccs_def,        0},
4753   {"asmfunc",      s_ccs_asmfunc,    0},
4754   {"endasmfunc",   s_ccs_endasmfunc, 0},
4755
4756   { 0, 0, 0 }
4757 };
4758 \f
4759 /* Parser functions used exclusively in instruction operands.  */
4760
4761 /* Generic immediate-value read function for use in insn parsing.
4762    STR points to the beginning of the immediate (the leading #);
4763    VAL receives the value; if the value is outside [MIN, MAX]
4764    issue an error.  PREFIX_OPT is true if the immediate prefix is
4765    optional.  */
4766
4767 static int
4768 parse_immediate (char **str, int *val, int min, int max,
4769                  bfd_boolean prefix_opt)
4770 {
4771   expressionS exp;
4772   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4773   if (exp.X_op != O_constant)
4774     {
4775       inst.error = _("constant expression required");
4776       return FAIL;
4777     }
4778
4779   if (exp.X_add_number < min || exp.X_add_number > max)
4780     {
4781       inst.error = _("immediate value out of range");
4782       return FAIL;
4783     }
4784
4785   *val = exp.X_add_number;
4786   return SUCCESS;
4787 }
4788
4789 /* Less-generic immediate-value read function with the possibility of loading a
4790    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4791    instructions. Puts the result directly in inst.operands[i].  */
4792
4793 static int
4794 parse_big_immediate (char **str, int i, expressionS *in_exp,
4795                      bfd_boolean allow_symbol_p)
4796 {
4797   expressionS exp;
4798   expressionS *exp_p = in_exp ? in_exp : &exp;
4799   char *ptr = *str;
4800
4801   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
4802
4803   if (exp_p->X_op == O_constant)
4804     {
4805       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
4806       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4807          O_constant.  We have to be careful not to break compilation for
4808          32-bit X_add_number, though.  */
4809       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4810         {
4811           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
4812           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
4813                                   & 0xffffffff);
4814           inst.operands[i].regisimm = 1;
4815         }
4816     }
4817   else if (exp_p->X_op == O_big
4818            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
4819     {
4820       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4821
4822       /* Bignums have their least significant bits in
4823          generic_bignum[0]. Make sure we put 32 bits in imm and
4824          32 bits in reg,  in a (hopefully) portable way.  */
4825       gas_assert (parts != 0);
4826
4827       /* Make sure that the number is not too big.
4828          PR 11972: Bignums can now be sign-extended to the
4829          size of a .octa so check that the out of range bits
4830          are all zero or all one.  */
4831       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
4832         {
4833           LITTLENUM_TYPE m = -1;
4834
4835           if (generic_bignum[parts * 2] != 0
4836               && generic_bignum[parts * 2] != m)
4837             return FAIL;
4838
4839           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
4840             if (generic_bignum[j] != generic_bignum[j-1])
4841               return FAIL;
4842         }
4843
4844       inst.operands[i].imm = 0;
4845       for (j = 0; j < parts; j++, idx++)
4846         inst.operands[i].imm |= generic_bignum[idx]
4847                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4848       inst.operands[i].reg = 0;
4849       for (j = 0; j < parts; j++, idx++)
4850         inst.operands[i].reg |= generic_bignum[idx]
4851                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4852       inst.operands[i].regisimm = 1;
4853     }
4854   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
4855     return FAIL;
4856
4857   *str = ptr;
4858
4859   return SUCCESS;
4860 }
4861
4862 /* Returns the pseudo-register number of an FPA immediate constant,
4863    or FAIL if there isn't a valid constant here.  */
4864
4865 static int
4866 parse_fpa_immediate (char ** str)
4867 {
4868   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4869   char *         save_in;
4870   expressionS    exp;
4871   int            i;
4872   int            j;
4873
4874   /* First try and match exact strings, this is to guarantee
4875      that some formats will work even for cross assembly.  */
4876
4877   for (i = 0; fp_const[i]; i++)
4878     {
4879       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4880         {
4881           char *start = *str;
4882
4883           *str += strlen (fp_const[i]);
4884           if (is_end_of_line[(unsigned char) **str])
4885             return i + 8;
4886           *str = start;
4887         }
4888     }
4889
4890   /* Just because we didn't get a match doesn't mean that the constant
4891      isn't valid, just that it is in a format that we don't
4892      automatically recognize.  Try parsing it with the standard
4893      expression routines.  */
4894
4895   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4896
4897   /* Look for a raw floating point number.  */
4898   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4899       && is_end_of_line[(unsigned char) *save_in])
4900     {
4901       for (i = 0; i < NUM_FLOAT_VALS; i++)
4902         {
4903           for (j = 0; j < MAX_LITTLENUMS; j++)
4904             {
4905               if (words[j] != fp_values[i][j])
4906                 break;
4907             }
4908
4909           if (j == MAX_LITTLENUMS)
4910             {
4911               *str = save_in;
4912               return i + 8;
4913             }
4914         }
4915     }
4916
4917   /* Try and parse a more complex expression, this will probably fail
4918      unless the code uses a floating point prefix (eg "0f").  */
4919   save_in = input_line_pointer;
4920   input_line_pointer = *str;
4921   if (expression (&exp) == absolute_section
4922       && exp.X_op == O_big
4923       && exp.X_add_number < 0)
4924     {
4925       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4926          Ditto for 15.  */
4927       if (gen_to_words (words, 5, (long) 15) == 0)
4928         {
4929           for (i = 0; i < NUM_FLOAT_VALS; i++)
4930             {
4931               for (j = 0; j < MAX_LITTLENUMS; j++)
4932                 {
4933                   if (words[j] != fp_values[i][j])
4934                     break;
4935                 }
4936
4937               if (j == MAX_LITTLENUMS)
4938                 {
4939                   *str = input_line_pointer;
4940                   input_line_pointer = save_in;
4941                   return i + 8;
4942                 }
4943             }
4944         }
4945     }
4946
4947   *str = input_line_pointer;
4948   input_line_pointer = save_in;
4949   inst.error = _("invalid FPA immediate expression");
4950   return FAIL;
4951 }
4952
4953 /* Returns 1 if a number has "quarter-precision" float format
4954    0baBbbbbbc defgh000 00000000 00000000.  */
4955
4956 static int
4957 is_quarter_float (unsigned imm)
4958 {
4959   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4960   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4961 }
4962
4963
4964 /* Detect the presence of a floating point or integer zero constant,
4965    i.e. #0.0 or #0.  */
4966
4967 static bfd_boolean
4968 parse_ifimm_zero (char **in)
4969 {
4970   int error_code;
4971
4972   if (!is_immediate_prefix (**in))
4973     return FALSE;
4974
4975   ++*in;
4976
4977   /* Accept #0x0 as a synonym for #0.  */
4978   if (strncmp (*in, "0x", 2) == 0)
4979     {
4980       int val;
4981       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
4982         return FALSE;
4983       return TRUE;
4984     }
4985
4986   error_code = atof_generic (in, ".", EXP_CHARS,
4987                              &generic_floating_point_number);
4988
4989   if (!error_code
4990       && generic_floating_point_number.sign == '+'
4991       && (generic_floating_point_number.low
4992           > generic_floating_point_number.leader))
4993     return TRUE;
4994
4995   return FALSE;
4996 }
4997
4998 /* Parse an 8-bit "quarter-precision" floating point number of the form:
4999    0baBbbbbbc defgh000 00000000 00000000.
5000    The zero and minus-zero cases need special handling, since they can't be
5001    encoded in the "quarter-precision" float format, but can nonetheless be
5002    loaded as integer constants.  */
5003
5004 static unsigned
5005 parse_qfloat_immediate (char **ccp, int *immed)
5006 {
5007   char *str = *ccp;
5008   char *fpnum;
5009   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5010   int found_fpchar = 0;
5011
5012   skip_past_char (&str, '#');
5013
5014   /* We must not accidentally parse an integer as a floating-point number. Make
5015      sure that the value we parse is not an integer by checking for special
5016      characters '.' or 'e'.
5017      FIXME: This is a horrible hack, but doing better is tricky because type
5018      information isn't in a very usable state at parse time.  */
5019   fpnum = str;
5020   skip_whitespace (fpnum);
5021
5022   if (strncmp (fpnum, "0x", 2) == 0)
5023     return FAIL;
5024   else
5025     {
5026       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
5027         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
5028           {
5029             found_fpchar = 1;
5030             break;
5031           }
5032
5033       if (!found_fpchar)
5034         return FAIL;
5035     }
5036
5037   if ((str = atof_ieee (str, 's', words)) != NULL)
5038     {
5039       unsigned fpword = 0;
5040       int i;
5041
5042       /* Our FP word must be 32 bits (single-precision FP).  */
5043       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
5044         {
5045           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5046           fpword |= words[i];
5047         }
5048
5049       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5050         *immed = fpword;
5051       else
5052         return FAIL;
5053
5054       *ccp = str;
5055
5056       return SUCCESS;
5057     }
5058
5059   return FAIL;
5060 }
5061
5062 /* Shift operands.  */
5063 enum shift_kind
5064 {
5065   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
5066 };
5067
5068 struct asm_shift_name
5069 {
5070   const char      *name;
5071   enum shift_kind  kind;
5072 };
5073
5074 /* Third argument to parse_shift.  */
5075 enum parse_shift_mode
5076 {
5077   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5078   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5079   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5080   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5081   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5082 };
5083
5084 /* Parse a <shift> specifier on an ARM data processing instruction.
5085    This has three forms:
5086
5087      (LSL|LSR|ASL|ASR|ROR) Rs
5088      (LSL|LSR|ASL|ASR|ROR) #imm
5089      RRX
5090
5091    Note that ASL is assimilated to LSL in the instruction encoding, and
5092    RRX to ROR #0 (which cannot be written as such).  */
5093
5094 static int
5095 parse_shift (char **str, int i, enum parse_shift_mode mode)
5096 {
5097   const struct asm_shift_name *shift_name;
5098   enum shift_kind shift;
5099   char *s = *str;
5100   char *p = s;
5101   int reg;
5102
5103   for (p = *str; ISALPHA (*p); p++)
5104     ;
5105
5106   if (p == *str)
5107     {
5108       inst.error = _("shift expression expected");
5109       return FAIL;
5110     }
5111
5112   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5113                                                             p - *str);
5114
5115   if (shift_name == NULL)
5116     {
5117       inst.error = _("shift expression expected");
5118       return FAIL;
5119     }
5120
5121   shift = shift_name->kind;
5122
5123   switch (mode)
5124     {
5125     case NO_SHIFT_RESTRICT:
5126     case SHIFT_IMMEDIATE:   break;
5127
5128     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5129       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5130         {
5131           inst.error = _("'LSL' or 'ASR' required");
5132           return FAIL;
5133         }
5134       break;
5135
5136     case SHIFT_LSL_IMMEDIATE:
5137       if (shift != SHIFT_LSL)
5138         {
5139           inst.error = _("'LSL' required");
5140           return FAIL;
5141         }
5142       break;
5143
5144     case SHIFT_ASR_IMMEDIATE:
5145       if (shift != SHIFT_ASR)
5146         {
5147           inst.error = _("'ASR' required");
5148           return FAIL;
5149         }
5150       break;
5151
5152     default: abort ();
5153     }
5154
5155   if (shift != SHIFT_RRX)
5156     {
5157       /* Whitespace can appear here if the next thing is a bare digit.  */
5158       skip_whitespace (p);
5159
5160       if (mode == NO_SHIFT_RESTRICT
5161           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5162         {
5163           inst.operands[i].imm = reg;
5164           inst.operands[i].immisreg = 1;
5165         }
5166       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5167         return FAIL;
5168     }
5169   inst.operands[i].shift_kind = shift;
5170   inst.operands[i].shifted = 1;
5171   *str = p;
5172   return SUCCESS;
5173 }
5174
5175 /* Parse a <shifter_operand> for an ARM data processing instruction:
5176
5177       #<immediate>
5178       #<immediate>, <rotate>
5179       <Rm>
5180       <Rm>, <shift>
5181
5182    where <shift> is defined by parse_shift above, and <rotate> is a
5183    multiple of 2 between 0 and 30.  Validation of immediate operands
5184    is deferred to md_apply_fix.  */
5185
5186 static int
5187 parse_shifter_operand (char **str, int i)
5188 {
5189   int value;
5190   expressionS exp;
5191
5192   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5193     {
5194       inst.operands[i].reg = value;
5195       inst.operands[i].isreg = 1;
5196
5197       /* parse_shift will override this if appropriate */
5198       inst.reloc.exp.X_op = O_constant;
5199       inst.reloc.exp.X_add_number = 0;
5200
5201       if (skip_past_comma (str) == FAIL)
5202         return SUCCESS;
5203
5204       /* Shift operation on register.  */
5205       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5206     }
5207
5208   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
5209     return FAIL;
5210
5211   if (skip_past_comma (str) == SUCCESS)
5212     {
5213       /* #x, y -- ie explicit rotation by Y.  */
5214       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5215         return FAIL;
5216
5217       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
5218         {
5219           inst.error = _("constant expression expected");
5220           return FAIL;
5221         }
5222
5223       value = exp.X_add_number;
5224       if (value < 0 || value > 30 || value % 2 != 0)
5225         {
5226           inst.error = _("invalid rotation");
5227           return FAIL;
5228         }
5229       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
5230         {
5231           inst.error = _("invalid constant");
5232           return FAIL;
5233         }
5234
5235       /* Encode as specified.  */
5236       inst.operands[i].imm = inst.reloc.exp.X_add_number | value << 7;
5237       return SUCCESS;
5238     }
5239
5240   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5241   inst.reloc.pc_rel = 0;
5242   return SUCCESS;
5243 }
5244
5245 /* Group relocation information.  Each entry in the table contains the
5246    textual name of the relocation as may appear in assembler source
5247    and must end with a colon.
5248    Along with this textual name are the relocation codes to be used if
5249    the corresponding instruction is an ALU instruction (ADD or SUB only),
5250    an LDR, an LDRS, or an LDC.  */
5251
5252 struct group_reloc_table_entry
5253 {
5254   const char *name;
5255   int alu_code;
5256   int ldr_code;
5257   int ldrs_code;
5258   int ldc_code;
5259 };
5260
5261 typedef enum
5262 {
5263   /* Varieties of non-ALU group relocation.  */
5264
5265   GROUP_LDR,
5266   GROUP_LDRS,
5267   GROUP_LDC
5268 } group_reloc_type;
5269
5270 static struct group_reloc_table_entry group_reloc_table[] =
5271   { /* Program counter relative: */
5272     { "pc_g0_nc",
5273       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5274       0,                                /* LDR */
5275       0,                                /* LDRS */
5276       0 },                              /* LDC */
5277     { "pc_g0",
5278       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5279       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5280       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5281       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5282     { "pc_g1_nc",
5283       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5284       0,                                /* LDR */
5285       0,                                /* LDRS */
5286       0 },                              /* LDC */
5287     { "pc_g1",
5288       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5289       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5290       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5291       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5292     { "pc_g2",
5293       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5294       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5295       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5296       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5297     /* Section base relative */
5298     { "sb_g0_nc",
5299       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5300       0,                                /* LDR */
5301       0,                                /* LDRS */
5302       0 },                              /* LDC */
5303     { "sb_g0",
5304       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5305       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5306       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5307       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5308     { "sb_g1_nc",
5309       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5310       0,                                /* LDR */
5311       0,                                /* LDRS */
5312       0 },                              /* LDC */
5313     { "sb_g1",
5314       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5315       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5316       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5317       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5318     { "sb_g2",
5319       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5320       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5321       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5322       BFD_RELOC_ARM_LDC_SB_G2 } };      /* LDC */
5323
5324 /* Given the address of a pointer pointing to the textual name of a group
5325    relocation as may appear in assembler source, attempt to find its details
5326    in group_reloc_table.  The pointer will be updated to the character after
5327    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5328    otherwise.  On success, *entry will be updated to point at the relevant
5329    group_reloc_table entry. */
5330
5331 static int
5332 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5333 {
5334   unsigned int i;
5335   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5336     {
5337       int length = strlen (group_reloc_table[i].name);
5338
5339       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5340           && (*str)[length] == ':')
5341         {
5342           *out = &group_reloc_table[i];
5343           *str += (length + 1);
5344           return SUCCESS;
5345         }
5346     }
5347
5348   return FAIL;
5349 }
5350
5351 /* Parse a <shifter_operand> for an ARM data processing instruction
5352    (as for parse_shifter_operand) where group relocations are allowed:
5353
5354       #<immediate>
5355       #<immediate>, <rotate>
5356       #:<group_reloc>:<expression>
5357       <Rm>
5358       <Rm>, <shift>
5359
5360    where <group_reloc> is one of the strings defined in group_reloc_table.
5361    The hashes are optional.
5362
5363    Everything else is as for parse_shifter_operand.  */
5364
5365 static parse_operand_result
5366 parse_shifter_operand_group_reloc (char **str, int i)
5367 {
5368   /* Determine if we have the sequence of characters #: or just :
5369      coming next.  If we do, then we check for a group relocation.
5370      If we don't, punt the whole lot to parse_shifter_operand.  */
5371
5372   if (((*str)[0] == '#' && (*str)[1] == ':')
5373       || (*str)[0] == ':')
5374     {
5375       struct group_reloc_table_entry *entry;
5376
5377       if ((*str)[0] == '#')
5378         (*str) += 2;
5379       else
5380         (*str)++;
5381
5382       /* Try to parse a group relocation.  Anything else is an error.  */
5383       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5384         {
5385           inst.error = _("unknown group relocation");
5386           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5387         }
5388
5389       /* We now have the group relocation table entry corresponding to
5390          the name in the assembler source.  Next, we parse the expression.  */
5391       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5392         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5393
5394       /* Record the relocation type (always the ALU variant here).  */
5395       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5396       gas_assert (inst.reloc.type != 0);
5397
5398       return PARSE_OPERAND_SUCCESS;
5399     }
5400   else
5401     return parse_shifter_operand (str, i) == SUCCESS
5402            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5403
5404   /* Never reached.  */
5405 }
5406
5407 /* Parse a Neon alignment expression.  Information is written to
5408    inst.operands[i].  We assume the initial ':' has been skipped.
5409
5410    align        .imm = align << 8, .immisalign=1, .preind=0  */
5411 static parse_operand_result
5412 parse_neon_alignment (char **str, int i)
5413 {
5414   char *p = *str;
5415   expressionS exp;
5416
5417   my_get_expression (&exp, &p, GE_NO_PREFIX);
5418
5419   if (exp.X_op != O_constant)
5420     {
5421       inst.error = _("alignment must be constant");
5422       return PARSE_OPERAND_FAIL;
5423     }
5424
5425   inst.operands[i].imm = exp.X_add_number << 8;
5426   inst.operands[i].immisalign = 1;
5427   /* Alignments are not pre-indexes.  */
5428   inst.operands[i].preind = 0;
5429
5430   *str = p;
5431   return PARSE_OPERAND_SUCCESS;
5432 }
5433
5434 /* Parse all forms of an ARM address expression.  Information is written
5435    to inst.operands[i] and/or inst.reloc.
5436
5437    Preindexed addressing (.preind=1):
5438
5439    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5440    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5441    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5442                        .shift_kind=shift .reloc.exp=shift_imm
5443
5444    These three may have a trailing ! which causes .writeback to be set also.
5445
5446    Postindexed addressing (.postind=1, .writeback=1):
5447
5448    [Rn], #offset       .reg=Rn .reloc.exp=offset
5449    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5450    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5451                        .shift_kind=shift .reloc.exp=shift_imm
5452
5453    Unindexed addressing (.preind=0, .postind=0):
5454
5455    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5456
5457    Other:
5458
5459    [Rn]{!}             shorthand for [Rn,#0]{!}
5460    =immediate          .isreg=0 .reloc.exp=immediate
5461    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5462
5463   It is the caller's responsibility to check for addressing modes not
5464   supported by the instruction, and to set inst.reloc.type.  */
5465
5466 static parse_operand_result
5467 parse_address_main (char **str, int i, int group_relocations,
5468                     group_reloc_type group_type)
5469 {
5470   char *p = *str;
5471   int reg;
5472
5473   if (skip_past_char (&p, '[') == FAIL)
5474     {
5475       if (skip_past_char (&p, '=') == FAIL)
5476         {
5477           /* Bare address - translate to PC-relative offset.  */
5478           inst.reloc.pc_rel = 1;
5479           inst.operands[i].reg = REG_PC;
5480           inst.operands[i].isreg = 1;
5481           inst.operands[i].preind = 1;
5482
5483           if (my_get_expression (&inst.reloc.exp, &p, GE_OPT_PREFIX_BIG))
5484             return PARSE_OPERAND_FAIL;
5485         }
5486       else if (parse_big_immediate (&p, i, &inst.reloc.exp,
5487                                     /*allow_symbol_p=*/TRUE))
5488         return PARSE_OPERAND_FAIL;
5489
5490       *str = p;
5491       return PARSE_OPERAND_SUCCESS;
5492     }
5493
5494   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5495   skip_whitespace (p);
5496
5497   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5498     {
5499       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5500       return PARSE_OPERAND_FAIL;
5501     }
5502   inst.operands[i].reg = reg;
5503   inst.operands[i].isreg = 1;
5504
5505   if (skip_past_comma (&p) == SUCCESS)
5506     {
5507       inst.operands[i].preind = 1;
5508
5509       if (*p == '+') p++;
5510       else if (*p == '-') p++, inst.operands[i].negative = 1;
5511
5512       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5513         {
5514           inst.operands[i].imm = reg;
5515           inst.operands[i].immisreg = 1;
5516
5517           if (skip_past_comma (&p) == SUCCESS)
5518             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5519               return PARSE_OPERAND_FAIL;
5520         }
5521       else if (skip_past_char (&p, ':') == SUCCESS)
5522         {
5523           /* FIXME: '@' should be used here, but it's filtered out by generic
5524              code before we get to see it here. This may be subject to
5525              change.  */
5526           parse_operand_result result = parse_neon_alignment (&p, i);
5527
5528           if (result != PARSE_OPERAND_SUCCESS)
5529             return result;
5530         }
5531       else
5532         {
5533           if (inst.operands[i].negative)
5534             {
5535               inst.operands[i].negative = 0;
5536               p--;
5537             }
5538
5539           if (group_relocations
5540               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5541             {
5542               struct group_reloc_table_entry *entry;
5543
5544               /* Skip over the #: or : sequence.  */
5545               if (*p == '#')
5546                 p += 2;
5547               else
5548                 p++;
5549
5550               /* Try to parse a group relocation.  Anything else is an
5551                  error.  */
5552               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5553                 {
5554                   inst.error = _("unknown group relocation");
5555                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5556                 }
5557
5558               /* We now have the group relocation table entry corresponding to
5559                  the name in the assembler source.  Next, we parse the
5560                  expression.  */
5561               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5562                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5563
5564               /* Record the relocation type.  */
5565               switch (group_type)
5566                 {
5567                   case GROUP_LDR:
5568                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5569                     break;
5570
5571                   case GROUP_LDRS:
5572                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5573                     break;
5574
5575                   case GROUP_LDC:
5576                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5577                     break;
5578
5579                   default:
5580                     gas_assert (0);
5581                 }
5582
5583               if (inst.reloc.type == 0)
5584                 {
5585                   inst.error = _("this group relocation is not allowed on this instruction");
5586                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5587                 }
5588             }
5589           else
5590             {
5591               char *q = p;
5592               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5593                 return PARSE_OPERAND_FAIL;
5594               /* If the offset is 0, find out if it's a +0 or -0.  */
5595               if (inst.reloc.exp.X_op == O_constant
5596                   && inst.reloc.exp.X_add_number == 0)
5597                 {
5598                   skip_whitespace (q);
5599                   if (*q == '#')
5600                     {
5601                       q++;
5602                       skip_whitespace (q);
5603                     }
5604                   if (*q == '-')
5605                     inst.operands[i].negative = 1;
5606                 }
5607             }
5608         }
5609     }
5610   else if (skip_past_char (&p, ':') == SUCCESS)
5611     {
5612       /* FIXME: '@' should be used here, but it's filtered out by generic code
5613          before we get to see it here. This may be subject to change.  */
5614       parse_operand_result result = parse_neon_alignment (&p, i);
5615
5616       if (result != PARSE_OPERAND_SUCCESS)
5617         return result;
5618     }
5619
5620   if (skip_past_char (&p, ']') == FAIL)
5621     {
5622       inst.error = _("']' expected");
5623       return PARSE_OPERAND_FAIL;
5624     }
5625
5626   if (skip_past_char (&p, '!') == SUCCESS)
5627     inst.operands[i].writeback = 1;
5628
5629   else if (skip_past_comma (&p) == SUCCESS)
5630     {
5631       if (skip_past_char (&p, '{') == SUCCESS)
5632         {
5633           /* [Rn], {expr} - unindexed, with option */
5634           if (parse_immediate (&p, &inst.operands[i].imm,
5635                                0, 255, TRUE) == FAIL)
5636             return PARSE_OPERAND_FAIL;
5637
5638           if (skip_past_char (&p, '}') == FAIL)
5639             {
5640               inst.error = _("'}' expected at end of 'option' field");
5641               return PARSE_OPERAND_FAIL;
5642             }
5643           if (inst.operands[i].preind)
5644             {
5645               inst.error = _("cannot combine index with option");
5646               return PARSE_OPERAND_FAIL;
5647             }
5648           *str = p;
5649           return PARSE_OPERAND_SUCCESS;
5650         }
5651       else
5652         {
5653           inst.operands[i].postind = 1;
5654           inst.operands[i].writeback = 1;
5655
5656           if (inst.operands[i].preind)
5657             {
5658               inst.error = _("cannot combine pre- and post-indexing");
5659               return PARSE_OPERAND_FAIL;
5660             }
5661
5662           if (*p == '+') p++;
5663           else if (*p == '-') p++, inst.operands[i].negative = 1;
5664
5665           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5666             {
5667               /* We might be using the immediate for alignment already. If we
5668                  are, OR the register number into the low-order bits.  */
5669               if (inst.operands[i].immisalign)
5670                 inst.operands[i].imm |= reg;
5671               else
5672                 inst.operands[i].imm = reg;
5673               inst.operands[i].immisreg = 1;
5674
5675               if (skip_past_comma (&p) == SUCCESS)
5676                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5677                   return PARSE_OPERAND_FAIL;
5678             }
5679           else
5680             {
5681               char *q = p;
5682               if (inst.operands[i].negative)
5683                 {
5684                   inst.operands[i].negative = 0;
5685                   p--;
5686                 }
5687               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5688                 return PARSE_OPERAND_FAIL;
5689               /* If the offset is 0, find out if it's a +0 or -0.  */
5690               if (inst.reloc.exp.X_op == O_constant
5691                   && inst.reloc.exp.X_add_number == 0)
5692                 {
5693                   skip_whitespace (q);
5694                   if (*q == '#')
5695                     {
5696                       q++;
5697                       skip_whitespace (q);
5698                     }
5699                   if (*q == '-')
5700                     inst.operands[i].negative = 1;
5701                 }
5702             }
5703         }
5704     }
5705
5706   /* If at this point neither .preind nor .postind is set, we have a
5707      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5708   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5709     {
5710       inst.operands[i].preind = 1;
5711       inst.reloc.exp.X_op = O_constant;
5712       inst.reloc.exp.X_add_number = 0;
5713     }
5714   *str = p;
5715   return PARSE_OPERAND_SUCCESS;
5716 }
5717
5718 static int
5719 parse_address (char **str, int i)
5720 {
5721   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5722          ? SUCCESS : FAIL;
5723 }
5724
5725 static parse_operand_result
5726 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5727 {
5728   return parse_address_main (str, i, 1, type);
5729 }
5730
5731 /* Parse an operand for a MOVW or MOVT instruction.  */
5732 static int
5733 parse_half (char **str)
5734 {
5735   char * p;
5736
5737   p = *str;
5738   skip_past_char (&p, '#');
5739   if (strncasecmp (p, ":lower16:", 9) == 0)
5740     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5741   else if (strncasecmp (p, ":upper16:", 9) == 0)
5742     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5743
5744   if (inst.reloc.type != BFD_RELOC_UNUSED)
5745     {
5746       p += 9;
5747       skip_whitespace (p);
5748     }
5749
5750   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5751     return FAIL;
5752
5753   if (inst.reloc.type == BFD_RELOC_UNUSED)
5754     {
5755       if (inst.reloc.exp.X_op != O_constant)
5756         {
5757           inst.error = _("constant expression expected");
5758           return FAIL;
5759         }
5760       if (inst.reloc.exp.X_add_number < 0
5761           || inst.reloc.exp.X_add_number > 0xffff)
5762         {
5763           inst.error = _("immediate value out of range");
5764           return FAIL;
5765         }
5766     }
5767   *str = p;
5768   return SUCCESS;
5769 }
5770
5771 /* Miscellaneous. */
5772
5773 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5774    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5775 static int
5776 parse_psr (char **str, bfd_boolean lhs)
5777 {
5778   char *p;
5779   unsigned long psr_field;
5780   const struct asm_psr *psr;
5781   char *start;
5782   bfd_boolean is_apsr = FALSE;
5783   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5784
5785   /* PR gas/12698:  If the user has specified -march=all then m_profile will
5786      be TRUE, but we want to ignore it in this case as we are building for any
5787      CPU type, including non-m variants.  */
5788   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
5789     m_profile = FALSE;
5790
5791   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5792      feature for ease of use and backwards compatibility.  */
5793   p = *str;
5794   if (strncasecmp (p, "SPSR", 4) == 0)
5795     {
5796       if (m_profile)
5797         goto unsupported_psr;
5798
5799       psr_field = SPSR_BIT;
5800     }
5801   else if (strncasecmp (p, "CPSR", 4) == 0)
5802     {
5803       if (m_profile)
5804         goto unsupported_psr;
5805
5806       psr_field = 0;
5807     }
5808   else if (strncasecmp (p, "APSR", 4) == 0)
5809     {
5810       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5811          and ARMv7-R architecture CPUs.  */
5812       is_apsr = TRUE;
5813       psr_field = 0;
5814     }
5815   else if (m_profile)
5816     {
5817       start = p;
5818       do
5819         p++;
5820       while (ISALNUM (*p) || *p == '_');
5821
5822       if (strncasecmp (start, "iapsr", 5) == 0
5823           || strncasecmp (start, "eapsr", 5) == 0
5824           || strncasecmp (start, "xpsr", 4) == 0
5825           || strncasecmp (start, "psr", 3) == 0)
5826         p = start + strcspn (start, "rR") + 1;
5827
5828       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5829                                                   p - start);
5830
5831       if (!psr)
5832         return FAIL;
5833
5834       /* If APSR is being written, a bitfield may be specified.  Note that
5835          APSR itself is handled above.  */
5836       if (psr->field <= 3)
5837         {
5838           psr_field = psr->field;
5839           is_apsr = TRUE;
5840           goto check_suffix;
5841         }
5842
5843       *str = p;
5844       /* M-profile MSR instructions have the mask field set to "10", except
5845          *PSR variants which modify APSR, which may use a different mask (and
5846          have been handled already).  Do that by setting the PSR_f field
5847          here.  */
5848       return psr->field | (lhs ? PSR_f : 0);
5849     }
5850   else
5851     goto unsupported_psr;
5852
5853   p += 4;
5854 check_suffix:
5855   if (*p == '_')
5856     {
5857       /* A suffix follows.  */
5858       p++;
5859       start = p;
5860
5861       do
5862         p++;
5863       while (ISALNUM (*p) || *p == '_');
5864
5865       if (is_apsr)
5866         {
5867           /* APSR uses a notation for bits, rather than fields.  */
5868           unsigned int nzcvq_bits = 0;
5869           unsigned int g_bit = 0;
5870           char *bit;
5871
5872           for (bit = start; bit != p; bit++)
5873             {
5874               switch (TOLOWER (*bit))
5875                 {
5876                 case 'n':
5877                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5878                   break;
5879
5880                 case 'z':
5881                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5882                   break;
5883
5884                 case 'c':
5885                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5886                   break;
5887
5888                 case 'v':
5889                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5890                   break;
5891
5892                 case 'q':
5893                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5894                   break;
5895
5896                 case 'g':
5897                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5898                   break;
5899
5900                 default:
5901                   inst.error = _("unexpected bit specified after APSR");
5902                   return FAIL;
5903                 }
5904             }
5905
5906           if (nzcvq_bits == 0x1f)
5907             psr_field |= PSR_f;
5908
5909           if (g_bit == 0x1)
5910             {
5911               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5912                 {
5913                   inst.error = _("selected processor does not "
5914                                  "support DSP extension");
5915                   return FAIL;
5916                 }
5917
5918               psr_field |= PSR_s;
5919             }
5920
5921           if ((nzcvq_bits & 0x20) != 0
5922               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5923               || (g_bit & 0x2) != 0)
5924             {
5925               inst.error = _("bad bitmask specified after APSR");
5926               return FAIL;
5927             }
5928         }
5929       else
5930         {
5931           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
5932                                                       p - start);
5933           if (!psr)
5934             goto error;
5935
5936           psr_field |= psr->field;
5937         }
5938     }
5939   else
5940     {
5941       if (ISALNUM (*p))
5942         goto error;    /* Garbage after "[CS]PSR".  */
5943
5944       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
5945          is deprecated, but allow it anyway.  */
5946       if (is_apsr && lhs)
5947         {
5948           psr_field |= PSR_f;
5949           as_tsktsk (_("writing to APSR without specifying a bitmask is "
5950                        "deprecated"));
5951         }
5952       else if (!m_profile)
5953         /* These bits are never right for M-profile devices: don't set them
5954            (only code paths which read/write APSR reach here).  */
5955         psr_field |= (PSR_c | PSR_f);
5956     }
5957   *str = p;
5958   return psr_field;
5959
5960  unsupported_psr:
5961   inst.error = _("selected processor does not support requested special "
5962                  "purpose register");
5963   return FAIL;
5964
5965  error:
5966   inst.error = _("flag for {c}psr instruction expected");
5967   return FAIL;
5968 }
5969
5970 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
5971    value suitable for splatting into the AIF field of the instruction.  */
5972
5973 static int
5974 parse_cps_flags (char **str)
5975 {
5976   int val = 0;
5977   int saw_a_flag = 0;
5978   char *s = *str;
5979
5980   for (;;)
5981     switch (*s++)
5982       {
5983       case '\0': case ',':
5984         goto done;
5985
5986       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
5987       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
5988       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
5989
5990       default:
5991         inst.error = _("unrecognized CPS flag");
5992         return FAIL;
5993       }
5994
5995  done:
5996   if (saw_a_flag == 0)
5997     {
5998       inst.error = _("missing CPS flags");
5999       return FAIL;
6000     }
6001
6002   *str = s - 1;
6003   return val;
6004 }
6005
6006 /* Parse an endian specifier ("BE" or "LE", case insensitive);
6007    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
6008
6009 static int
6010 parse_endian_specifier (char **str)
6011 {
6012   int little_endian;
6013   char *s = *str;
6014
6015   if (strncasecmp (s, "BE", 2))
6016     little_endian = 0;
6017   else if (strncasecmp (s, "LE", 2))
6018     little_endian = 1;
6019   else
6020     {
6021       inst.error = _("valid endian specifiers are be or le");
6022       return FAIL;
6023     }
6024
6025   if (ISALNUM (s[2]) || s[2] == '_')
6026     {
6027       inst.error = _("valid endian specifiers are be or le");
6028       return FAIL;
6029     }
6030
6031   *str = s + 2;
6032   return little_endian;
6033 }
6034
6035 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6036    value suitable for poking into the rotate field of an sxt or sxta
6037    instruction, or FAIL on error.  */
6038
6039 static int
6040 parse_ror (char **str)
6041 {
6042   int rot;
6043   char *s = *str;
6044
6045   if (strncasecmp (s, "ROR", 3) == 0)
6046     s += 3;
6047   else
6048     {
6049       inst.error = _("missing rotation field after comma");
6050       return FAIL;
6051     }
6052
6053   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6054     return FAIL;
6055
6056   switch (rot)
6057     {
6058     case  0: *str = s; return 0x0;
6059     case  8: *str = s; return 0x1;
6060     case 16: *str = s; return 0x2;
6061     case 24: *str = s; return 0x3;
6062
6063     default:
6064       inst.error = _("rotation can only be 0, 8, 16, or 24");
6065       return FAIL;
6066     }
6067 }
6068
6069 /* Parse a conditional code (from conds[] below).  The value returned is in the
6070    range 0 .. 14, or FAIL.  */
6071 static int
6072 parse_cond (char **str)
6073 {
6074   char *q;
6075   const struct asm_cond *c;
6076   int n;
6077   /* Condition codes are always 2 characters, so matching up to
6078      3 characters is sufficient.  */
6079   char cond[3];
6080
6081   q = *str;
6082   n = 0;
6083   while (ISALPHA (*q) && n < 3)
6084     {
6085       cond[n] = TOLOWER (*q);
6086       q++;
6087       n++;
6088     }
6089
6090   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6091   if (!c)
6092     {
6093       inst.error = _("condition required");
6094       return FAIL;
6095     }
6096
6097   *str = q;
6098   return c->value;
6099 }
6100
6101 /* If the given feature available in the selected CPU, mark it as used.
6102    Returns TRUE iff feature is available.  */
6103 static bfd_boolean
6104 mark_feature_used (const arm_feature_set *feature)
6105 {
6106   /* Ensure the option is valid on the current architecture.  */
6107   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
6108     return FALSE;
6109
6110   /* Add the appropriate architecture feature for the barrier option used.
6111      */
6112   if (thumb_mode)
6113     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
6114   else
6115     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
6116
6117   return TRUE;
6118 }
6119
6120 /* Parse an option for a barrier instruction.  Returns the encoding for the
6121    option, or FAIL.  */
6122 static int
6123 parse_barrier (char **str)
6124 {
6125   char *p, *q;
6126   const struct asm_barrier_opt *o;
6127
6128   p = q = *str;
6129   while (ISALPHA (*q))
6130     q++;
6131
6132   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6133                                                     q - p);
6134   if (!o)
6135     return FAIL;
6136
6137   if (!mark_feature_used (&o->arch))
6138     return FAIL;
6139
6140   *str = q;
6141   return o->value;
6142 }
6143
6144 /* Parse the operands of a table branch instruction.  Similar to a memory
6145    operand.  */
6146 static int
6147 parse_tb (char **str)
6148 {
6149   char * p = *str;
6150   int reg;
6151
6152   if (skip_past_char (&p, '[') == FAIL)
6153     {
6154       inst.error = _("'[' expected");
6155       return FAIL;
6156     }
6157
6158   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6159     {
6160       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6161       return FAIL;
6162     }
6163   inst.operands[0].reg = reg;
6164
6165   if (skip_past_comma (&p) == FAIL)
6166     {
6167       inst.error = _("',' expected");
6168       return FAIL;
6169     }
6170
6171   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6172     {
6173       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6174       return FAIL;
6175     }
6176   inst.operands[0].imm = reg;
6177
6178   if (skip_past_comma (&p) == SUCCESS)
6179     {
6180       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6181         return FAIL;
6182       if (inst.reloc.exp.X_add_number != 1)
6183         {
6184           inst.error = _("invalid shift");
6185           return FAIL;
6186         }
6187       inst.operands[0].shifted = 1;
6188     }
6189
6190   if (skip_past_char (&p, ']') == FAIL)
6191     {
6192       inst.error = _("']' expected");
6193       return FAIL;
6194     }
6195   *str = p;
6196   return SUCCESS;
6197 }
6198
6199 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6200    information on the types the operands can take and how they are encoded.
6201    Up to four operands may be read; this function handles setting the
6202    ".present" field for each read operand itself.
6203    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6204    else returns FAIL.  */
6205
6206 static int
6207 parse_neon_mov (char **str, int *which_operand)
6208 {
6209   int i = *which_operand, val;
6210   enum arm_reg_type rtype;
6211   char *ptr = *str;
6212   struct neon_type_el optype;
6213
6214   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6215     {
6216       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6217       inst.operands[i].reg = val;
6218       inst.operands[i].isscalar = 1;
6219       inst.operands[i].vectype = optype;
6220       inst.operands[i++].present = 1;
6221
6222       if (skip_past_comma (&ptr) == FAIL)
6223         goto wanted_comma;
6224
6225       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6226         goto wanted_arm;
6227
6228       inst.operands[i].reg = val;
6229       inst.operands[i].isreg = 1;
6230       inst.operands[i].present = 1;
6231     }
6232   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6233            != FAIL)
6234     {
6235       /* Cases 0, 1, 2, 3, 5 (D only).  */
6236       if (skip_past_comma (&ptr) == FAIL)
6237         goto wanted_comma;
6238
6239       inst.operands[i].reg = val;
6240       inst.operands[i].isreg = 1;
6241       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6242       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6243       inst.operands[i].isvec = 1;
6244       inst.operands[i].vectype = optype;
6245       inst.operands[i++].present = 1;
6246
6247       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6248         {
6249           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6250              Case 13: VMOV <Sd>, <Rm>  */
6251           inst.operands[i].reg = val;
6252           inst.operands[i].isreg = 1;
6253           inst.operands[i].present = 1;
6254
6255           if (rtype == REG_TYPE_NQ)
6256             {
6257               first_error (_("can't use Neon quad register here"));
6258               return FAIL;
6259             }
6260           else if (rtype != REG_TYPE_VFS)
6261             {
6262               i++;
6263               if (skip_past_comma (&ptr) == FAIL)
6264                 goto wanted_comma;
6265               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6266                 goto wanted_arm;
6267               inst.operands[i].reg = val;
6268               inst.operands[i].isreg = 1;
6269               inst.operands[i].present = 1;
6270             }
6271         }
6272       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6273                                            &optype)) != FAIL)
6274         {
6275           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6276              Case 1: VMOV<c><q> <Dd>, <Dm>
6277              Case 8: VMOV.F32 <Sd>, <Sm>
6278              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6279
6280           inst.operands[i].reg = val;
6281           inst.operands[i].isreg = 1;
6282           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6283           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6284           inst.operands[i].isvec = 1;
6285           inst.operands[i].vectype = optype;
6286           inst.operands[i].present = 1;
6287
6288           if (skip_past_comma (&ptr) == SUCCESS)
6289             {
6290               /* Case 15.  */
6291               i++;
6292
6293               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6294                 goto wanted_arm;
6295
6296               inst.operands[i].reg = val;
6297               inst.operands[i].isreg = 1;
6298               inst.operands[i++].present = 1;
6299
6300               if (skip_past_comma (&ptr) == FAIL)
6301                 goto wanted_comma;
6302
6303               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6304                 goto wanted_arm;
6305
6306               inst.operands[i].reg = val;
6307               inst.operands[i].isreg = 1;
6308               inst.operands[i].present = 1;
6309             }
6310         }
6311       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6312           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6313              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6314              Case 10: VMOV.F32 <Sd>, #<imm>
6315              Case 11: VMOV.F64 <Dd>, #<imm>  */
6316         inst.operands[i].immisfloat = 1;
6317       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6318                == SUCCESS)
6319           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6320              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6321         ;
6322       else
6323         {
6324           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6325           return FAIL;
6326         }
6327     }
6328   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6329     {
6330       /* Cases 6, 7.  */
6331       inst.operands[i].reg = val;
6332       inst.operands[i].isreg = 1;
6333       inst.operands[i++].present = 1;
6334
6335       if (skip_past_comma (&ptr) == FAIL)
6336         goto wanted_comma;
6337
6338       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6339         {
6340           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6341           inst.operands[i].reg = val;
6342           inst.operands[i].isscalar = 1;
6343           inst.operands[i].present = 1;
6344           inst.operands[i].vectype = optype;
6345         }
6346       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6347         {
6348           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6349           inst.operands[i].reg = val;
6350           inst.operands[i].isreg = 1;
6351           inst.operands[i++].present = 1;
6352
6353           if (skip_past_comma (&ptr) == FAIL)
6354             goto wanted_comma;
6355
6356           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6357               == FAIL)
6358             {
6359               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
6360               return FAIL;
6361             }
6362
6363           inst.operands[i].reg = val;
6364           inst.operands[i].isreg = 1;
6365           inst.operands[i].isvec = 1;
6366           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6367           inst.operands[i].vectype = optype;
6368           inst.operands[i].present = 1;
6369
6370           if (rtype == REG_TYPE_VFS)
6371             {
6372               /* Case 14.  */
6373               i++;
6374               if (skip_past_comma (&ptr) == FAIL)
6375                 goto wanted_comma;
6376               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6377                                               &optype)) == FAIL)
6378                 {
6379                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6380                   return FAIL;
6381                 }
6382               inst.operands[i].reg = val;
6383               inst.operands[i].isreg = 1;
6384               inst.operands[i].isvec = 1;
6385               inst.operands[i].issingle = 1;
6386               inst.operands[i].vectype = optype;
6387               inst.operands[i].present = 1;
6388             }
6389         }
6390       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6391                != FAIL)
6392         {
6393           /* Case 13.  */
6394           inst.operands[i].reg = val;
6395           inst.operands[i].isreg = 1;
6396           inst.operands[i].isvec = 1;
6397           inst.operands[i].issingle = 1;
6398           inst.operands[i].vectype = optype;
6399           inst.operands[i].present = 1;
6400         }
6401     }
6402   else
6403     {
6404       first_error (_("parse error"));
6405       return FAIL;
6406     }
6407
6408   /* Successfully parsed the operands. Update args.  */
6409   *which_operand = i;
6410   *str = ptr;
6411   return SUCCESS;
6412
6413  wanted_comma:
6414   first_error (_("expected comma"));
6415   return FAIL;
6416
6417  wanted_arm:
6418   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6419   return FAIL;
6420 }
6421
6422 /* Use this macro when the operand constraints are different
6423    for ARM and THUMB (e.g. ldrd).  */
6424 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6425         ((arm_operand) | ((thumb_operand) << 16))
6426
6427 /* Matcher codes for parse_operands.  */
6428 enum operand_parse_code
6429 {
6430   OP_stop,      /* end of line */
6431
6432   OP_RR,        /* ARM register */
6433   OP_RRnpc,     /* ARM register, not r15 */
6434   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6435   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6436   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6437                    optional trailing ! */
6438   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6439   OP_RCP,       /* Coprocessor number */
6440   OP_RCN,       /* Coprocessor register */
6441   OP_RF,        /* FPA register */
6442   OP_RVS,       /* VFP single precision register */
6443   OP_RVD,       /* VFP double precision register (0..15) */
6444   OP_RND,       /* Neon double precision register (0..31) */
6445   OP_RNQ,       /* Neon quad precision register */
6446   OP_RVSD,      /* VFP single or double precision register */
6447   OP_RNDQ,      /* Neon double or quad precision register */
6448   OP_RNSDQ,     /* Neon single, double or quad precision register */
6449   OP_RNSC,      /* Neon scalar D[X] */
6450   OP_RVC,       /* VFP control register */
6451   OP_RMF,       /* Maverick F register */
6452   OP_RMD,       /* Maverick D register */
6453   OP_RMFX,      /* Maverick FX register */
6454   OP_RMDX,      /* Maverick DX register */
6455   OP_RMAX,      /* Maverick AX register */
6456   OP_RMDS,      /* Maverick DSPSC register */
6457   OP_RIWR,      /* iWMMXt wR register */
6458   OP_RIWC,      /* iWMMXt wC register */
6459   OP_RIWG,      /* iWMMXt wCG register */
6460   OP_RXA,       /* XScale accumulator register */
6461
6462   OP_REGLST,    /* ARM register list */
6463   OP_VRSLST,    /* VFP single-precision register list */
6464   OP_VRDLST,    /* VFP double-precision register list */
6465   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6466   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6467   OP_NSTRLST,   /* Neon element/structure list */
6468
6469   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6470   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6471   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
6472   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6473   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6474   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6475   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6476   OP_VMOV,      /* Neon VMOV operands.  */
6477   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6478   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6479   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6480
6481   OP_I0,        /* immediate zero */
6482   OP_I7,        /* immediate value 0 .. 7 */
6483   OP_I15,       /*                 0 .. 15 */
6484   OP_I16,       /*                 1 .. 16 */
6485   OP_I16z,      /*                 0 .. 16 */
6486   OP_I31,       /*                 0 .. 31 */
6487   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6488   OP_I32,       /*                 1 .. 32 */
6489   OP_I32z,      /*                 0 .. 32 */
6490   OP_I63,       /*                 0 .. 63 */
6491   OP_I63s,      /*               -64 .. 63 */
6492   OP_I64,       /*                 1 .. 64 */
6493   OP_I64z,      /*                 0 .. 64 */
6494   OP_I255,      /*                 0 .. 255 */
6495
6496   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6497   OP_I7b,       /*                             0 .. 7 */
6498   OP_I15b,      /*                             0 .. 15 */
6499   OP_I31b,      /*                             0 .. 31 */
6500
6501   OP_SH,        /* shifter operand */
6502   OP_SHG,       /* shifter operand with possible group relocation */
6503   OP_ADDR,      /* Memory address expression (any mode) */
6504   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6505   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6506   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6507   OP_EXP,       /* arbitrary expression */
6508   OP_EXPi,      /* same, with optional immediate prefix */
6509   OP_EXPr,      /* same, with optional relocation suffix */
6510   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6511
6512   OP_CPSF,      /* CPS flags */
6513   OP_ENDI,      /* Endianness specifier */
6514   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6515   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6516   OP_COND,      /* conditional code */
6517   OP_TB,        /* Table branch.  */
6518
6519   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6520
6521   OP_RRnpc_I0,  /* ARM register or literal 0 */
6522   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
6523   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6524   OP_RF_IF,     /* FPA register or immediate */
6525   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6526   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6527
6528   /* Optional operands.  */
6529   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6530   OP_oI31b,      /*                             0 .. 31 */
6531   OP_oI32b,      /*                             1 .. 32 */
6532   OP_oI32z,      /*                             0 .. 32 */
6533   OP_oIffffb,    /*                             0 .. 65535 */
6534   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6535
6536   OP_oRR,        /* ARM register */
6537   OP_oRRnpc,     /* ARM register, not the PC */
6538   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6539   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6540   OP_oRND,       /* Optional Neon double precision register */
6541   OP_oRNQ,       /* Optional Neon quad precision register */
6542   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6543   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6544   OP_oSHll,      /* LSL immediate */
6545   OP_oSHar,      /* ASR immediate */
6546   OP_oSHllar,    /* LSL or ASR immediate */
6547   OP_oROR,       /* ROR 0/8/16/24 */
6548   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6549
6550   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6551   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6552   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6553   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6554
6555   OP_FIRST_OPTIONAL = OP_oI7b
6556 };
6557
6558 /* Generic instruction operand parser.  This does no encoding and no
6559    semantic validation; it merely squirrels values away in the inst
6560    structure.  Returns SUCCESS or FAIL depending on whether the
6561    specified grammar matched.  */
6562 static int
6563 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6564 {
6565   unsigned const int *upat = pattern;
6566   char *backtrack_pos = 0;
6567   const char *backtrack_error = 0;
6568   int i, val = 0, backtrack_index = 0;
6569   enum arm_reg_type rtype;
6570   parse_operand_result result;
6571   unsigned int op_parse_code;
6572
6573 #define po_char_or_fail(chr)                    \
6574   do                                            \
6575     {                                           \
6576       if (skip_past_char (&str, chr) == FAIL)   \
6577         goto bad_args;                          \
6578     }                                           \
6579   while (0)
6580
6581 #define po_reg_or_fail(regtype)                                 \
6582   do                                                            \
6583     {                                                           \
6584       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6585                                  & inst.operands[i].vectype);   \
6586       if (val == FAIL)                                          \
6587         {                                                       \
6588           first_error (_(reg_expected_msgs[regtype]));          \
6589           goto failure;                                         \
6590         }                                                       \
6591       inst.operands[i].reg = val;                               \
6592       inst.operands[i].isreg = 1;                               \
6593       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6594       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6595       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6596                              || rtype == REG_TYPE_VFD           \
6597                              || rtype == REG_TYPE_NQ);          \
6598     }                                                           \
6599   while (0)
6600
6601 #define po_reg_or_goto(regtype, label)                          \
6602   do                                                            \
6603     {                                                           \
6604       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6605                                  & inst.operands[i].vectype);   \
6606       if (val == FAIL)                                          \
6607         goto label;                                             \
6608                                                                 \
6609       inst.operands[i].reg = val;                               \
6610       inst.operands[i].isreg = 1;                               \
6611       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6612       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6613       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6614                              || rtype == REG_TYPE_VFD           \
6615                              || rtype == REG_TYPE_NQ);          \
6616     }                                                           \
6617   while (0)
6618
6619 #define po_imm_or_fail(min, max, popt)                          \
6620   do                                                            \
6621     {                                                           \
6622       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6623         goto failure;                                           \
6624       inst.operands[i].imm = val;                               \
6625     }                                                           \
6626   while (0)
6627
6628 #define po_scalar_or_goto(elsz, label)                                  \
6629   do                                                                    \
6630     {                                                                   \
6631       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6632       if (val == FAIL)                                                  \
6633         goto label;                                                     \
6634       inst.operands[i].reg = val;                                       \
6635       inst.operands[i].isscalar = 1;                                    \
6636     }                                                                   \
6637   while (0)
6638
6639 #define po_misc_or_fail(expr)                   \
6640   do                                            \
6641     {                                           \
6642       if (expr)                                 \
6643         goto failure;                           \
6644     }                                           \
6645   while (0)
6646
6647 #define po_misc_or_fail_no_backtrack(expr)              \
6648   do                                                    \
6649     {                                                   \
6650       result = expr;                                    \
6651       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6652         backtrack_pos = 0;                              \
6653       if (result != PARSE_OPERAND_SUCCESS)              \
6654         goto failure;                                   \
6655     }                                                   \
6656   while (0)
6657
6658 #define po_barrier_or_imm(str)                             \
6659   do                                                       \
6660     {                                                      \
6661       val = parse_barrier (&str);                          \
6662       if (val == FAIL && ! ISALPHA (*str))                 \
6663         goto immediate;                                    \
6664       if (val == FAIL                                      \
6665           /* ISB can only take SY as an option.  */        \
6666           || ((inst.instruction & 0xf0) == 0x60            \
6667                && val != 0xf))                             \
6668         {                                                  \
6669            inst.error = _("invalid barrier type");         \
6670            backtrack_pos = 0;                              \
6671            goto failure;                                   \
6672         }                                                  \
6673     }                                                      \
6674   while (0)
6675
6676   skip_whitespace (str);
6677
6678   for (i = 0; upat[i] != OP_stop; i++)
6679     {
6680       op_parse_code = upat[i];
6681       if (op_parse_code >= 1<<16)
6682         op_parse_code = thumb ? (op_parse_code >> 16)
6683                                 : (op_parse_code & ((1<<16)-1));
6684
6685       if (op_parse_code >= OP_FIRST_OPTIONAL)
6686         {
6687           /* Remember where we are in case we need to backtrack.  */
6688           gas_assert (!backtrack_pos);
6689           backtrack_pos = str;
6690           backtrack_error = inst.error;
6691           backtrack_index = i;
6692         }
6693
6694       if (i > 0 && (i > 1 || inst.operands[0].present))
6695         po_char_or_fail (',');
6696
6697       switch (op_parse_code)
6698         {
6699           /* Registers */
6700         case OP_oRRnpc:
6701         case OP_oRRnpcsp:
6702         case OP_RRnpc:
6703         case OP_RRnpcsp:
6704         case OP_oRR:
6705         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6706         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6707         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6708         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6709         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6710         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6711         case OP_oRND:
6712         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6713         case OP_RVC:
6714           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6715           break;
6716           /* Also accept generic coprocessor regs for unknown registers.  */
6717           coproc_reg:
6718           po_reg_or_fail (REG_TYPE_CN);
6719           break;
6720         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6721         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6722         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6723         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6724         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6725         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6726         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6727         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6728         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6729         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6730         case OP_oRNQ:
6731         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6732         case OP_oRNDQ:
6733         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6734         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6735         case OP_oRNSDQ:
6736         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6737
6738         /* Neon scalar. Using an element size of 8 means that some invalid
6739            scalars are accepted here, so deal with those in later code.  */
6740         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6741
6742         case OP_RNDQ_I0:
6743           {
6744             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6745             break;
6746             try_imm0:
6747             po_imm_or_fail (0, 0, TRUE);
6748           }
6749           break;
6750
6751         case OP_RVSD_I0:
6752           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6753           break;
6754
6755         case OP_RSVD_FI0:
6756           {
6757             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
6758             break;
6759             try_ifimm0:
6760             if (parse_ifimm_zero (&str))
6761               inst.operands[i].imm = 0;
6762             else
6763             {
6764               inst.error
6765                 = _("only floating point zero is allowed as immediate value");
6766               goto failure;
6767             }
6768           }
6769           break;
6770
6771         case OP_RR_RNSC:
6772           {
6773             po_scalar_or_goto (8, try_rr);
6774             break;
6775             try_rr:
6776             po_reg_or_fail (REG_TYPE_RN);
6777           }
6778           break;
6779
6780         case OP_RNSDQ_RNSC:
6781           {
6782             po_scalar_or_goto (8, try_nsdq);
6783             break;
6784             try_nsdq:
6785             po_reg_or_fail (REG_TYPE_NSDQ);
6786           }
6787           break;
6788
6789         case OP_RNDQ_RNSC:
6790           {
6791             po_scalar_or_goto (8, try_ndq);
6792             break;
6793             try_ndq:
6794             po_reg_or_fail (REG_TYPE_NDQ);
6795           }
6796           break;
6797
6798         case OP_RND_RNSC:
6799           {
6800             po_scalar_or_goto (8, try_vfd);
6801             break;
6802             try_vfd:
6803             po_reg_or_fail (REG_TYPE_VFD);
6804           }
6805           break;
6806
6807         case OP_VMOV:
6808           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6809              not careful then bad things might happen.  */
6810           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6811           break;
6812
6813         case OP_RNDQ_Ibig:
6814           {
6815             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6816             break;
6817             try_immbig:
6818             /* There's a possibility of getting a 64-bit immediate here, so
6819                we need special handling.  */
6820             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
6821                 == FAIL)
6822               {
6823                 inst.error = _("immediate value is out of range");
6824                 goto failure;
6825               }
6826           }
6827           break;
6828
6829         case OP_RNDQ_I63b:
6830           {
6831             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6832             break;
6833             try_shimm:
6834             po_imm_or_fail (0, 63, TRUE);
6835           }
6836           break;
6837
6838         case OP_RRnpcb:
6839           po_char_or_fail ('[');
6840           po_reg_or_fail  (REG_TYPE_RN);
6841           po_char_or_fail (']');
6842           break;
6843
6844         case OP_RRnpctw:
6845         case OP_RRw:
6846         case OP_oRRw:
6847           po_reg_or_fail (REG_TYPE_RN);
6848           if (skip_past_char (&str, '!') == SUCCESS)
6849             inst.operands[i].writeback = 1;
6850           break;
6851
6852           /* Immediates */
6853         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6854         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6855         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6856         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6857         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6858         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6859         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6860         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6861         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6862         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6863         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6864         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6865
6866         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6867         case OP_oI7b:
6868         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6869         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6870         case OP_oI31b:
6871         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6872         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6873         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
6874         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6875
6876           /* Immediate variants */
6877         case OP_oI255c:
6878           po_char_or_fail ('{');
6879           po_imm_or_fail (0, 255, TRUE);
6880           po_char_or_fail ('}');
6881           break;
6882
6883         case OP_I31w:
6884           /* The expression parser chokes on a trailing !, so we have
6885              to find it first and zap it.  */
6886           {
6887             char *s = str;
6888             while (*s && *s != ',')
6889               s++;
6890             if (s[-1] == '!')
6891               {
6892                 s[-1] = '\0';
6893                 inst.operands[i].writeback = 1;
6894               }
6895             po_imm_or_fail (0, 31, TRUE);
6896             if (str == s - 1)
6897               str = s;
6898           }
6899           break;
6900
6901           /* Expressions */
6902         case OP_EXPi:   EXPi:
6903           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6904                                               GE_OPT_PREFIX));
6905           break;
6906
6907         case OP_EXP:
6908           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6909                                               GE_NO_PREFIX));
6910           break;
6911
6912         case OP_EXPr:   EXPr:
6913           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6914                                               GE_NO_PREFIX));
6915           if (inst.reloc.exp.X_op == O_symbol)
6916             {
6917               val = parse_reloc (&str);
6918               if (val == -1)
6919                 {
6920                   inst.error = _("unrecognized relocation suffix");
6921                   goto failure;
6922                 }
6923               else if (val != BFD_RELOC_UNUSED)
6924                 {
6925                   inst.operands[i].imm = val;
6926                   inst.operands[i].hasreloc = 1;
6927                 }
6928             }
6929           break;
6930
6931           /* Operand for MOVW or MOVT.  */
6932         case OP_HALF:
6933           po_misc_or_fail (parse_half (&str));
6934           break;
6935
6936           /* Register or expression.  */
6937         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
6938         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
6939
6940           /* Register or immediate.  */
6941         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
6942         I0:               po_imm_or_fail (0, 0, FALSE);       break;
6943
6944         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
6945         IF:
6946           if (!is_immediate_prefix (*str))
6947             goto bad_args;
6948           str++;
6949           val = parse_fpa_immediate (&str);
6950           if (val == FAIL)
6951             goto failure;
6952           /* FPA immediates are encoded as registers 8-15.
6953              parse_fpa_immediate has already applied the offset.  */
6954           inst.operands[i].reg = val;
6955           inst.operands[i].isreg = 1;
6956           break;
6957
6958         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
6959         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
6960
6961           /* Two kinds of register.  */
6962         case OP_RIWR_RIWC:
6963           {
6964             struct reg_entry *rege = arm_reg_parse_multi (&str);
6965             if (!rege
6966                 || (rege->type != REG_TYPE_MMXWR
6967                     && rege->type != REG_TYPE_MMXWC
6968                     && rege->type != REG_TYPE_MMXWCG))
6969               {
6970                 inst.error = _("iWMMXt data or control register expected");
6971                 goto failure;
6972               }
6973             inst.operands[i].reg = rege->number;
6974             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
6975           }
6976           break;
6977
6978         case OP_RIWC_RIWG:
6979           {
6980             struct reg_entry *rege = arm_reg_parse_multi (&str);
6981             if (!rege
6982                 || (rege->type != REG_TYPE_MMXWC
6983                     && rege->type != REG_TYPE_MMXWCG))
6984               {
6985                 inst.error = _("iWMMXt control register expected");
6986                 goto failure;
6987               }
6988             inst.operands[i].reg = rege->number;
6989             inst.operands[i].isreg = 1;
6990           }
6991           break;
6992
6993           /* Misc */
6994         case OP_CPSF:    val = parse_cps_flags (&str);          break;
6995         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
6996         case OP_oROR:    val = parse_ror (&str);                break;
6997         case OP_COND:    val = parse_cond (&str);               break;
6998         case OP_oBARRIER_I15:
6999           po_barrier_or_imm (str); break;
7000           immediate:
7001           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7002             goto failure;
7003           break;
7004
7005         case OP_wPSR:
7006         case OP_rPSR:
7007           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7008           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7009             {
7010               inst.error = _("Banked registers are not available with this "
7011                              "architecture.");
7012               goto failure;
7013             }
7014           break;
7015           try_psr:
7016           val = parse_psr (&str, op_parse_code == OP_wPSR);
7017           break;
7018
7019         case OP_APSR_RR:
7020           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7021           break;
7022           try_apsr:
7023           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7024              instruction).  */
7025           if (strncasecmp (str, "APSR_", 5) == 0)
7026             {
7027               unsigned found = 0;
7028               str += 5;
7029               while (found < 15)
7030                 switch (*str++)
7031                   {
7032                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7033                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7034                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7035                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7036                   default: found = 16;
7037                   }
7038               if (found != 15)
7039                 goto failure;
7040               inst.operands[i].isvec = 1;
7041               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7042               inst.operands[i].reg = REG_PC;
7043             }
7044           else
7045             goto failure;
7046           break;
7047
7048         case OP_TB:
7049           po_misc_or_fail (parse_tb (&str));
7050           break;
7051
7052           /* Register lists.  */
7053         case OP_REGLST:
7054           val = parse_reg_list (&str);
7055           if (*str == '^')
7056             {
7057               inst.operands[i].writeback = 1;
7058               str++;
7059             }
7060           break;
7061
7062         case OP_VRSLST:
7063           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
7064           break;
7065
7066         case OP_VRDLST:
7067           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
7068           break;
7069
7070         case OP_VRSDLST:
7071           /* Allow Q registers too.  */
7072           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7073                                     REGLIST_NEON_D);
7074           if (val == FAIL)
7075             {
7076               inst.error = NULL;
7077               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7078                                         REGLIST_VFP_S);
7079               inst.operands[i].issingle = 1;
7080             }
7081           break;
7082
7083         case OP_NRDLST:
7084           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7085                                     REGLIST_NEON_D);
7086           break;
7087
7088         case OP_NSTRLST:
7089           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7090                                            &inst.operands[i].vectype);
7091           break;
7092
7093           /* Addressing modes */
7094         case OP_ADDR:
7095           po_misc_or_fail (parse_address (&str, i));
7096           break;
7097
7098         case OP_ADDRGLDR:
7099           po_misc_or_fail_no_backtrack (
7100             parse_address_group_reloc (&str, i, GROUP_LDR));
7101           break;
7102
7103         case OP_ADDRGLDRS:
7104           po_misc_or_fail_no_backtrack (
7105             parse_address_group_reloc (&str, i, GROUP_LDRS));
7106           break;
7107
7108         case OP_ADDRGLDC:
7109           po_misc_or_fail_no_backtrack (
7110             parse_address_group_reloc (&str, i, GROUP_LDC));
7111           break;
7112
7113         case OP_SH:
7114           po_misc_or_fail (parse_shifter_operand (&str, i));
7115           break;
7116
7117         case OP_SHG:
7118           po_misc_or_fail_no_backtrack (
7119             parse_shifter_operand_group_reloc (&str, i));
7120           break;
7121
7122         case OP_oSHll:
7123           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7124           break;
7125
7126         case OP_oSHar:
7127           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7128           break;
7129
7130         case OP_oSHllar:
7131           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7132           break;
7133
7134         default:
7135           as_fatal (_("unhandled operand code %d"), op_parse_code);
7136         }
7137
7138       /* Various value-based sanity checks and shared operations.  We
7139          do not signal immediate failures for the register constraints;
7140          this allows a syntax error to take precedence.  */
7141       switch (op_parse_code)
7142         {
7143         case OP_oRRnpc:
7144         case OP_RRnpc:
7145         case OP_RRnpcb:
7146         case OP_RRw:
7147         case OP_oRRw:
7148         case OP_RRnpc_I0:
7149           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7150             inst.error = BAD_PC;
7151           break;
7152
7153         case OP_oRRnpcsp:
7154         case OP_RRnpcsp:
7155           if (inst.operands[i].isreg)
7156             {
7157               if (inst.operands[i].reg == REG_PC)
7158                 inst.error = BAD_PC;
7159               else if (inst.operands[i].reg == REG_SP)
7160                 inst.error = BAD_SP;
7161             }
7162           break;
7163
7164         case OP_RRnpctw:
7165           if (inst.operands[i].isreg
7166               && inst.operands[i].reg == REG_PC
7167               && (inst.operands[i].writeback || thumb))
7168             inst.error = BAD_PC;
7169           break;
7170
7171         case OP_CPSF:
7172         case OP_ENDI:
7173         case OP_oROR:
7174         case OP_wPSR:
7175         case OP_rPSR:
7176         case OP_COND:
7177         case OP_oBARRIER_I15:
7178         case OP_REGLST:
7179         case OP_VRSLST:
7180         case OP_VRDLST:
7181         case OP_VRSDLST:
7182         case OP_NRDLST:
7183         case OP_NSTRLST:
7184           if (val == FAIL)
7185             goto failure;
7186           inst.operands[i].imm = val;
7187           break;
7188
7189         default:
7190           break;
7191         }
7192
7193       /* If we get here, this operand was successfully parsed.  */
7194       inst.operands[i].present = 1;
7195       continue;
7196
7197     bad_args:
7198       inst.error = BAD_ARGS;
7199
7200     failure:
7201       if (!backtrack_pos)
7202         {
7203           /* The parse routine should already have set inst.error, but set a
7204              default here just in case.  */
7205           if (!inst.error)
7206             inst.error = _("syntax error");
7207           return FAIL;
7208         }
7209
7210       /* Do not backtrack over a trailing optional argument that
7211          absorbed some text.  We will only fail again, with the
7212          'garbage following instruction' error message, which is
7213          probably less helpful than the current one.  */
7214       if (backtrack_index == i && backtrack_pos != str
7215           && upat[i+1] == OP_stop)
7216         {
7217           if (!inst.error)
7218             inst.error = _("syntax error");
7219           return FAIL;
7220         }
7221
7222       /* Try again, skipping the optional argument at backtrack_pos.  */
7223       str = backtrack_pos;
7224       inst.error = backtrack_error;
7225       inst.operands[backtrack_index].present = 0;
7226       i = backtrack_index;
7227       backtrack_pos = 0;
7228     }
7229
7230   /* Check that we have parsed all the arguments.  */
7231   if (*str != '\0' && !inst.error)
7232     inst.error = _("garbage following instruction");
7233
7234   return inst.error ? FAIL : SUCCESS;
7235 }
7236
7237 #undef po_char_or_fail
7238 #undef po_reg_or_fail
7239 #undef po_reg_or_goto
7240 #undef po_imm_or_fail
7241 #undef po_scalar_or_fail
7242 #undef po_barrier_or_imm
7243
7244 /* Shorthand macro for instruction encoding functions issuing errors.  */
7245 #define constraint(expr, err)                   \
7246   do                                            \
7247     {                                           \
7248       if (expr)                                 \
7249         {                                       \
7250           inst.error = err;                     \
7251           return;                               \
7252         }                                       \
7253     }                                           \
7254   while (0)
7255
7256 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
7257    instructions are unpredictable if these registers are used.  This
7258    is the BadReg predicate in ARM's Thumb-2 documentation.  */
7259 #define reject_bad_reg(reg)                             \
7260   do                                                    \
7261    if (reg == REG_SP || reg == REG_PC)                  \
7262      {                                                  \
7263        inst.error = (reg == REG_SP) ? BAD_SP : BAD_PC;  \
7264        return;                                          \
7265      }                                                  \
7266   while (0)
7267
7268 /* If REG is R13 (the stack pointer), warn that its use is
7269    deprecated.  */
7270 #define warn_deprecated_sp(reg)                 \
7271   do                                            \
7272     if (warn_on_deprecated && reg == REG_SP)    \
7273        as_tsktsk (_("use of r13 is deprecated"));       \
7274   while (0)
7275
7276 /* Functions for operand encoding.  ARM, then Thumb.  */
7277
7278 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
7279
7280 /* If VAL can be encoded in the immediate field of an ARM instruction,
7281    return the encoded form.  Otherwise, return FAIL.  */
7282
7283 static unsigned int
7284 encode_arm_immediate (unsigned int val)
7285 {
7286   unsigned int a, i;
7287
7288   for (i = 0; i < 32; i += 2)
7289     if ((a = rotate_left (val, i)) <= 0xff)
7290       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
7291
7292   return FAIL;
7293 }
7294
7295 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
7296    return the encoded form.  Otherwise, return FAIL.  */
7297 static unsigned int
7298 encode_thumb32_immediate (unsigned int val)
7299 {
7300   unsigned int a, i;
7301
7302   if (val <= 0xff)
7303     return val;
7304
7305   for (i = 1; i <= 24; i++)
7306     {
7307       a = val >> i;
7308       if ((val & ~(0xff << i)) == 0)
7309         return ((val >> i) & 0x7f) | ((32 - i) << 7);
7310     }
7311
7312   a = val & 0xff;
7313   if (val == ((a << 16) | a))
7314     return 0x100 | a;
7315   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
7316     return 0x300 | a;
7317
7318   a = val & 0xff00;
7319   if (val == ((a << 16) | a))
7320     return 0x200 | (a >> 8);
7321
7322   return FAIL;
7323 }
7324 /* Encode a VFP SP or DP register number into inst.instruction.  */
7325
7326 static void
7327 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
7328 {
7329   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
7330       && reg > 15)
7331     {
7332       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
7333         {
7334           if (thumb_mode)
7335             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
7336                                     fpu_vfp_ext_d32);
7337           else
7338             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
7339                                     fpu_vfp_ext_d32);
7340         }
7341       else
7342         {
7343           first_error (_("D register out of range for selected VFP version"));
7344           return;
7345         }
7346     }
7347
7348   switch (pos)
7349     {
7350     case VFP_REG_Sd:
7351       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
7352       break;
7353
7354     case VFP_REG_Sn:
7355       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
7356       break;
7357
7358     case VFP_REG_Sm:
7359       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
7360       break;
7361
7362     case VFP_REG_Dd:
7363       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
7364       break;
7365
7366     case VFP_REG_Dn:
7367       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
7368       break;
7369
7370     case VFP_REG_Dm:
7371       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
7372       break;
7373
7374     default:
7375       abort ();
7376     }
7377 }
7378
7379 /* Encode a <shift> in an ARM-format instruction.  The immediate,
7380    if any, is handled by md_apply_fix.   */
7381 static void
7382 encode_arm_shift (int i)
7383 {
7384   if (inst.operands[i].shift_kind == SHIFT_RRX)
7385     inst.instruction |= SHIFT_ROR << 5;
7386   else
7387     {
7388       inst.instruction |= inst.operands[i].shift_kind << 5;
7389       if (inst.operands[i].immisreg)
7390         {
7391           inst.instruction |= SHIFT_BY_REG;
7392           inst.instruction |= inst.operands[i].imm << 8;
7393         }
7394       else
7395         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7396     }
7397 }
7398
7399 static void
7400 encode_arm_shifter_operand (int i)
7401 {
7402   if (inst.operands[i].isreg)
7403     {
7404       inst.instruction |= inst.operands[i].reg;
7405       encode_arm_shift (i);
7406     }
7407   else
7408     {
7409       inst.instruction |= INST_IMMEDIATE;
7410       if (inst.reloc.type != BFD_RELOC_ARM_IMMEDIATE)
7411         inst.instruction |= inst.operands[i].imm;
7412     }
7413 }
7414
7415 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7416 static void
7417 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7418 {
7419   /* PR 14260:
7420      Generate an error if the operand is not a register.  */
7421   constraint (!inst.operands[i].isreg,
7422               _("Instruction does not support =N addresses"));
7423
7424   inst.instruction |= inst.operands[i].reg << 16;
7425
7426   if (inst.operands[i].preind)
7427     {
7428       if (is_t)
7429         {
7430           inst.error = _("instruction does not accept preindexed addressing");
7431           return;
7432         }
7433       inst.instruction |= PRE_INDEX;
7434       if (inst.operands[i].writeback)
7435         inst.instruction |= WRITE_BACK;
7436
7437     }
7438   else if (inst.operands[i].postind)
7439     {
7440       gas_assert (inst.operands[i].writeback);
7441       if (is_t)
7442         inst.instruction |= WRITE_BACK;
7443     }
7444   else /* unindexed - only for coprocessor */
7445     {
7446       inst.error = _("instruction does not accept unindexed addressing");
7447       return;
7448     }
7449
7450   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7451       && (((inst.instruction & 0x000f0000) >> 16)
7452           == ((inst.instruction & 0x0000f000) >> 12)))
7453     as_warn ((inst.instruction & LOAD_BIT)
7454              ? _("destination register same as write-back base")
7455              : _("source register same as write-back base"));
7456 }
7457
7458 /* inst.operands[i] was set up by parse_address.  Encode it into an
7459    ARM-format mode 2 load or store instruction.  If is_t is true,
7460    reject forms that cannot be used with a T instruction (i.e. not
7461    post-indexed).  */
7462 static void
7463 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7464 {
7465   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7466
7467   encode_arm_addr_mode_common (i, is_t);
7468
7469   if (inst.operands[i].immisreg)
7470     {
7471       constraint ((inst.operands[i].imm == REG_PC
7472                    || (is_pc && inst.operands[i].writeback)),
7473                   BAD_PC_ADDRESSING);
7474       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7475       inst.instruction |= inst.operands[i].imm;
7476       if (!inst.operands[i].negative)
7477         inst.instruction |= INDEX_UP;
7478       if (inst.operands[i].shifted)
7479         {
7480           if (inst.operands[i].shift_kind == SHIFT_RRX)
7481             inst.instruction |= SHIFT_ROR << 5;
7482           else
7483             {
7484               inst.instruction |= inst.operands[i].shift_kind << 5;
7485               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7486             }
7487         }
7488     }
7489   else /* immediate offset in inst.reloc */
7490     {
7491       if (is_pc && !inst.reloc.pc_rel)
7492         {
7493           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7494
7495           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7496              cannot use PC in addressing.
7497              PC cannot be used in writeback addressing, either.  */
7498           constraint ((is_t || inst.operands[i].writeback),
7499                       BAD_PC_ADDRESSING);
7500
7501           /* Use of PC in str is deprecated for ARMv7.  */
7502           if (warn_on_deprecated
7503               && !is_load
7504               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7505             as_tsktsk (_("use of PC in this instruction is deprecated"));
7506         }
7507
7508       if (inst.reloc.type == BFD_RELOC_UNUSED)
7509         {
7510           /* Prefer + for zero encoded value.  */
7511           if (!inst.operands[i].negative)
7512             inst.instruction |= INDEX_UP;
7513           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7514         }
7515     }
7516 }
7517
7518 /* inst.operands[i] was set up by parse_address.  Encode it into an
7519    ARM-format mode 3 load or store instruction.  Reject forms that
7520    cannot be used with such instructions.  If is_t is true, reject
7521    forms that cannot be used with a T instruction (i.e. not
7522    post-indexed).  */
7523 static void
7524 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7525 {
7526   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7527     {
7528       inst.error = _("instruction does not accept scaled register index");
7529       return;
7530     }
7531
7532   encode_arm_addr_mode_common (i, is_t);
7533
7534   if (inst.operands[i].immisreg)
7535     {
7536       constraint ((inst.operands[i].imm == REG_PC
7537                    || (is_t && inst.operands[i].reg == REG_PC)),
7538                   BAD_PC_ADDRESSING);
7539       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
7540                   BAD_PC_WRITEBACK);
7541       inst.instruction |= inst.operands[i].imm;
7542       if (!inst.operands[i].negative)
7543         inst.instruction |= INDEX_UP;
7544     }
7545   else /* immediate offset in inst.reloc */
7546     {
7547       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7548                    && inst.operands[i].writeback),
7549                   BAD_PC_WRITEBACK);
7550       inst.instruction |= HWOFFSET_IMM;
7551       if (inst.reloc.type == BFD_RELOC_UNUSED)
7552         {
7553           /* Prefer + for zero encoded value.  */
7554           if (!inst.operands[i].negative)
7555             inst.instruction |= INDEX_UP;
7556
7557           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7558         }
7559     }
7560 }
7561
7562 /* Write immediate bits [7:0] to the following locations:
7563
7564   |28/24|23     19|18 16|15                    4|3     0|
7565   |  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|
7566
7567   This function is used by VMOV/VMVN/VORR/VBIC.  */
7568
7569 static void
7570 neon_write_immbits (unsigned immbits)
7571 {
7572   inst.instruction |= immbits & 0xf;
7573   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
7574   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
7575 }
7576
7577 /* Invert low-order SIZE bits of XHI:XLO.  */
7578
7579 static void
7580 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
7581 {
7582   unsigned immlo = xlo ? *xlo : 0;
7583   unsigned immhi = xhi ? *xhi : 0;
7584
7585   switch (size)
7586     {
7587     case 8:
7588       immlo = (~immlo) & 0xff;
7589       break;
7590
7591     case 16:
7592       immlo = (~immlo) & 0xffff;
7593       break;
7594
7595     case 64:
7596       immhi = (~immhi) & 0xffffffff;
7597       /* fall through.  */
7598
7599     case 32:
7600       immlo = (~immlo) & 0xffffffff;
7601       break;
7602
7603     default:
7604       abort ();
7605     }
7606
7607   if (xlo)
7608     *xlo = immlo;
7609
7610   if (xhi)
7611     *xhi = immhi;
7612 }
7613
7614 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
7615    A, B, C, D.  */
7616
7617 static int
7618 neon_bits_same_in_bytes (unsigned imm)
7619 {
7620   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
7621          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
7622          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
7623          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
7624 }
7625
7626 /* For immediate of above form, return 0bABCD.  */
7627
7628 static unsigned
7629 neon_squash_bits (unsigned imm)
7630 {
7631   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
7632          | ((imm & 0x01000000) >> 21);
7633 }
7634
7635 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
7636
7637 static unsigned
7638 neon_qfloat_bits (unsigned imm)
7639 {
7640   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
7641 }
7642
7643 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
7644    the instruction. *OP is passed as the initial value of the op field, and
7645    may be set to a different value depending on the constant (i.e.
7646    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
7647    MVN).  If the immediate looks like a repeated pattern then also
7648    try smaller element sizes.  */
7649
7650 static int
7651 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
7652                          unsigned *immbits, int *op, int size,
7653                          enum neon_el_type type)
7654 {
7655   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
7656      float.  */
7657   if (type == NT_float && !float_p)
7658     return FAIL;
7659
7660   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
7661     {
7662       if (size != 32 || *op == 1)
7663         return FAIL;
7664       *immbits = neon_qfloat_bits (immlo);
7665       return 0xf;
7666     }
7667
7668   if (size == 64)
7669     {
7670       if (neon_bits_same_in_bytes (immhi)
7671           && neon_bits_same_in_bytes (immlo))
7672         {
7673           if (*op == 1)
7674             return FAIL;
7675           *immbits = (neon_squash_bits (immhi) << 4)
7676                      | neon_squash_bits (immlo);
7677           *op = 1;
7678           return 0xe;
7679         }
7680
7681       if (immhi != immlo)
7682         return FAIL;
7683     }
7684
7685   if (size >= 32)
7686     {
7687       if (immlo == (immlo & 0x000000ff))
7688         {
7689           *immbits = immlo;
7690           return 0x0;
7691         }
7692       else if (immlo == (immlo & 0x0000ff00))
7693         {
7694           *immbits = immlo >> 8;
7695           return 0x2;
7696         }
7697       else if (immlo == (immlo & 0x00ff0000))
7698         {
7699           *immbits = immlo >> 16;
7700           return 0x4;
7701         }
7702       else if (immlo == (immlo & 0xff000000))
7703         {
7704           *immbits = immlo >> 24;
7705           return 0x6;
7706         }
7707       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
7708         {
7709           *immbits = (immlo >> 8) & 0xff;
7710           return 0xc;
7711         }
7712       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
7713         {
7714           *immbits = (immlo >> 16) & 0xff;
7715           return 0xd;
7716         }
7717
7718       if ((immlo & 0xffff) != (immlo >> 16))
7719         return FAIL;
7720       immlo &= 0xffff;
7721     }
7722
7723   if (size >= 16)
7724     {
7725       if (immlo == (immlo & 0x000000ff))
7726         {
7727           *immbits = immlo;
7728           return 0x8;
7729         }
7730       else if (immlo == (immlo & 0x0000ff00))
7731         {
7732           *immbits = immlo >> 8;
7733           return 0xa;
7734         }
7735
7736       if ((immlo & 0xff) != (immlo >> 8))
7737         return FAIL;
7738       immlo &= 0xff;
7739     }
7740
7741   if (immlo == (immlo & 0x000000ff))
7742     {
7743       /* Don't allow MVN with 8-bit immediate.  */
7744       if (*op == 1)
7745         return FAIL;
7746       *immbits = immlo;
7747       return 0xe;
7748     }
7749
7750   return FAIL;
7751 }
7752
7753 enum lit_type
7754 {
7755   CONST_THUMB,
7756   CONST_ARM,
7757   CONST_VEC
7758 };
7759
7760 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7761    Determine whether it can be performed with a move instruction; if
7762    it can, convert inst.instruction to that move instruction and
7763    return TRUE; if it can't, convert inst.instruction to a literal-pool
7764    load and return FALSE.  If this is not a valid thing to do in the
7765    current context, set inst.error and return TRUE.
7766
7767    inst.operands[i] describes the destination register.  */
7768
7769 static bfd_boolean
7770 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
7771 {
7772   unsigned long tbit;
7773   bfd_boolean thumb_p = (t == CONST_THUMB);
7774   bfd_boolean arm_p   = (t == CONST_ARM);
7775   bfd_boolean vec64_p = (t == CONST_VEC) && !inst.operands[i].issingle;
7776
7777   if (thumb_p)
7778     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7779   else
7780     tbit = LOAD_BIT;
7781
7782   if ((inst.instruction & tbit) == 0)
7783     {
7784       inst.error = _("invalid pseudo operation");
7785       return TRUE;
7786     }
7787   if (inst.reloc.exp.X_op != O_constant
7788       && inst.reloc.exp.X_op != O_symbol
7789       && inst.reloc.exp.X_op != O_big)
7790     {
7791       inst.error = _("constant expression expected");
7792       return TRUE;
7793     }
7794   if ((inst.reloc.exp.X_op == O_constant
7795        || inst.reloc.exp.X_op == O_big)
7796       && !inst.operands[i].issingle)
7797     {
7798       if (thumb_p && inst.reloc.exp.X_op == O_constant)
7799         {
7800           if (!unified_syntax && (inst.reloc.exp.X_add_number & ~0xFF) == 0)
7801             {
7802               /* This can be done with a mov(1) instruction.  */
7803               inst.instruction  = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
7804               inst.instruction |= inst.reloc.exp.X_add_number;
7805               return TRUE;
7806             }
7807         }
7808       else if (arm_p && inst.reloc.exp.X_op == O_constant)
7809         {
7810           int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
7811           if (value != FAIL)
7812             {
7813               /* This can be done with a mov instruction.  */
7814               inst.instruction &= LITERAL_MASK;
7815               inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
7816               inst.instruction |= value & 0xfff;
7817               return TRUE;
7818             }
7819
7820           value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
7821           if (value != FAIL)
7822             {
7823               /* This can be done with a mvn instruction.  */
7824               inst.instruction &= LITERAL_MASK;
7825               inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
7826               inst.instruction |= value & 0xfff;
7827               return TRUE;
7828             }
7829         }
7830       else if (vec64_p)
7831         {
7832           int op = 0;
7833           unsigned immbits = 0;
7834           unsigned immlo = inst.operands[1].imm;
7835           unsigned immhi = inst.operands[1].regisimm
7836                            ? inst.operands[1].reg
7837                            : inst.reloc.exp.X_unsigned
7838                              ? 0
7839                              : ((bfd_int64_t)((int) immlo)) >> 32;
7840           int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
7841                                                &op, 64, NT_invtype);
7842
7843           if (cmode == FAIL)
7844             {
7845               neon_invert_size (&immlo, &immhi, 64);
7846               op = !op;
7847               cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
7848                                                &op, 64, NT_invtype);
7849             }
7850           if (cmode != FAIL)
7851             {
7852               inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
7853                                   | (1 << 23)
7854                                   | (cmode << 8)
7855                                   | (op << 5)
7856                                   | (1 << 4);
7857               /* Fill other bits in vmov encoding for both thumb and arm.  */
7858               if (thumb_mode)
7859                 inst.instruction |= (0x7 << 29) | (0xF << 24);
7860               else
7861                 inst.instruction |= (0xF << 28) | (0x1 << 25);
7862               neon_write_immbits (immbits);
7863               return TRUE;
7864             }
7865         }
7866     }
7867
7868   if (add_to_lit_pool ((!inst.operands[i].isvec
7869                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
7870     return TRUE;
7871
7872   inst.operands[1].reg = REG_PC;
7873   inst.operands[1].isreg = 1;
7874   inst.operands[1].preind = 1;
7875   inst.reloc.pc_rel = 1;
7876   inst.reloc.type = (thumb_p
7877                      ? BFD_RELOC_ARM_THUMB_OFFSET
7878                      : (mode_3
7879                         ? BFD_RELOC_ARM_HWLITERAL
7880                         : BFD_RELOC_ARM_LITERAL));
7881   return FALSE;
7882 }
7883
7884 /* inst.operands[i] was set up by parse_address.  Encode it into an
7885    ARM-format instruction.  Reject all forms which cannot be encoded
7886    into a coprocessor load/store instruction.  If wb_ok is false,
7887    reject use of writeback; if unind_ok is false, reject use of
7888    unindexed addressing.  If reloc_override is not 0, use it instead
7889    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
7890    (in which case it is preserved).  */
7891
7892 static int
7893 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
7894 {
7895   if (!inst.operands[i].isreg)
7896     {
7897       /* PR 18256 */
7898       if (! inst.operands[0].isvec)
7899         {
7900           inst.error = _("invalid co-processor operand");
7901           return FAIL;
7902         }
7903       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
7904         return SUCCESS;
7905     }
7906
7907   inst.instruction |= inst.operands[i].reg << 16;
7908
7909   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
7910
7911   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
7912     {
7913       gas_assert (!inst.operands[i].writeback);
7914       if (!unind_ok)
7915         {
7916           inst.error = _("instruction does not support unindexed addressing");
7917           return FAIL;
7918         }
7919       inst.instruction |= inst.operands[i].imm;
7920       inst.instruction |= INDEX_UP;
7921       return SUCCESS;
7922     }
7923
7924   if (inst.operands[i].preind)
7925     inst.instruction |= PRE_INDEX;
7926
7927   if (inst.operands[i].writeback)
7928     {
7929       if (inst.operands[i].reg == REG_PC)
7930         {
7931           inst.error = _("pc may not be used with write-back");
7932           return FAIL;
7933         }
7934       if (!wb_ok)
7935         {
7936           inst.error = _("instruction does not support writeback");
7937           return FAIL;
7938         }
7939       inst.instruction |= WRITE_BACK;
7940     }
7941
7942   if (reloc_override)
7943     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
7944   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
7945             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
7946            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
7947     {
7948       if (thumb_mode)
7949         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
7950       else
7951         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
7952     }
7953
7954   /* Prefer + for zero encoded value.  */
7955   if (!inst.operands[i].negative)
7956     inst.instruction |= INDEX_UP;
7957
7958   return SUCCESS;
7959 }
7960
7961 /* Functions for instruction encoding, sorted by sub-architecture.
7962    First some generics; their names are taken from the conventional
7963    bit positions for register arguments in ARM format instructions.  */
7964
7965 static void
7966 do_noargs (void)
7967 {
7968 }
7969
7970 static void
7971 do_rd (void)
7972 {
7973   inst.instruction |= inst.operands[0].reg << 12;
7974 }
7975
7976 static void
7977 do_rd_rm (void)
7978 {
7979   inst.instruction |= inst.operands[0].reg << 12;
7980   inst.instruction |= inst.operands[1].reg;
7981 }
7982
7983 static void
7984 do_rm_rn (void)
7985 {
7986   inst.instruction |= inst.operands[0].reg;
7987   inst.instruction |= inst.operands[1].reg << 16;
7988 }
7989
7990 static void
7991 do_rd_rn (void)
7992 {
7993   inst.instruction |= inst.operands[0].reg << 12;
7994   inst.instruction |= inst.operands[1].reg << 16;
7995 }
7996
7997 static void
7998 do_rn_rd (void)
7999 {
8000   inst.instruction |= inst.operands[0].reg << 16;
8001   inst.instruction |= inst.operands[1].reg << 12;
8002 }
8003
8004 static bfd_boolean
8005 check_obsolete (const arm_feature_set *feature, const char *msg)
8006 {
8007   if (ARM_CPU_IS_ANY (cpu_variant))
8008     {
8009       as_tsktsk ("%s", msg);
8010       return TRUE;
8011     }
8012   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
8013     {
8014       as_bad ("%s", msg);
8015       return TRUE;
8016     }
8017
8018   return FALSE;
8019 }
8020
8021 static void
8022 do_rd_rm_rn (void)
8023 {
8024   unsigned Rn = inst.operands[2].reg;
8025   /* Enforce restrictions on SWP instruction.  */
8026   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
8027     {
8028       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
8029                   _("Rn must not overlap other operands"));
8030
8031       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
8032        */
8033       if (!check_obsolete (&arm_ext_v8,
8034                            _("swp{b} use is obsoleted for ARMv8 and later"))
8035           && warn_on_deprecated
8036           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
8037         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
8038     }
8039
8040   inst.instruction |= inst.operands[0].reg << 12;
8041   inst.instruction |= inst.operands[1].reg;
8042   inst.instruction |= Rn << 16;
8043 }
8044
8045 static void
8046 do_rd_rn_rm (void)
8047 {
8048   inst.instruction |= inst.operands[0].reg << 12;
8049   inst.instruction |= inst.operands[1].reg << 16;
8050   inst.instruction |= inst.operands[2].reg;
8051 }
8052
8053 static void
8054 do_rm_rd_rn (void)
8055 {
8056   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
8057   constraint (((inst.reloc.exp.X_op != O_constant
8058                 && inst.reloc.exp.X_op != O_illegal)
8059                || inst.reloc.exp.X_add_number != 0),
8060               BAD_ADDR_MODE);
8061   inst.instruction |= inst.operands[0].reg;
8062   inst.instruction |= inst.operands[1].reg << 12;
8063   inst.instruction |= inst.operands[2].reg << 16;
8064 }
8065
8066 static void
8067 do_imm0 (void)
8068 {
8069   inst.instruction |= inst.operands[0].imm;
8070 }
8071
8072 static void
8073 do_rd_cpaddr (void)
8074 {
8075   inst.instruction |= inst.operands[0].reg << 12;
8076   encode_arm_cp_address (1, TRUE, TRUE, 0);
8077 }
8078
8079 /* ARM instructions, in alphabetical order by function name (except
8080    that wrapper functions appear immediately after the function they
8081    wrap).  */
8082
8083 /* This is a pseudo-op of the form "adr rd, label" to be converted
8084    into a relative address of the form "add rd, pc, #label-.-8".  */
8085
8086 static void
8087 do_adr (void)
8088 {
8089   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8090
8091   /* Frag hacking will turn this into a sub instruction if the offset turns
8092      out to be negative.  */
8093   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8094   inst.reloc.pc_rel = 1;
8095   inst.reloc.exp.X_add_number -= 8;
8096 }
8097
8098 /* This is a pseudo-op of the form "adrl rd, label" to be converted
8099    into a relative address of the form:
8100    add rd, pc, #low(label-.-8)"
8101    add rd, rd, #high(label-.-8)"  */
8102
8103 static void
8104 do_adrl (void)
8105 {
8106   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8107
8108   /* Frag hacking will turn this into a sub instruction if the offset turns
8109      out to be negative.  */
8110   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
8111   inst.reloc.pc_rel            = 1;
8112   inst.size                    = INSN_SIZE * 2;
8113   inst.reloc.exp.X_add_number -= 8;
8114 }
8115
8116 static void
8117 do_arit (void)
8118 {
8119   if (!inst.operands[1].present)
8120     inst.operands[1].reg = inst.operands[0].reg;
8121   inst.instruction |= inst.operands[0].reg << 12;
8122   inst.instruction |= inst.operands[1].reg << 16;
8123   encode_arm_shifter_operand (2);
8124 }
8125
8126 static void
8127 do_barrier (void)
8128 {
8129   if (inst.operands[0].present)
8130     inst.instruction |= inst.operands[0].imm;
8131   else
8132     inst.instruction |= 0xf;
8133 }
8134
8135 static void
8136 do_bfc (void)
8137 {
8138   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
8139   constraint (msb > 32, _("bit-field extends past end of register"));
8140   /* The instruction encoding stores the LSB and MSB,
8141      not the LSB and width.  */
8142   inst.instruction |= inst.operands[0].reg << 12;
8143   inst.instruction |= inst.operands[1].imm << 7;
8144   inst.instruction |= (msb - 1) << 16;
8145 }
8146
8147 static void
8148 do_bfi (void)
8149 {
8150   unsigned int msb;
8151
8152   /* #0 in second position is alternative syntax for bfc, which is
8153      the same instruction but with REG_PC in the Rm field.  */
8154   if (!inst.operands[1].isreg)
8155     inst.operands[1].reg = REG_PC;
8156
8157   msb = inst.operands[2].imm + inst.operands[3].imm;
8158   constraint (msb > 32, _("bit-field extends past end of register"));
8159   /* The instruction encoding stores the LSB and MSB,
8160      not the LSB and width.  */
8161   inst.instruction |= inst.operands[0].reg << 12;
8162   inst.instruction |= inst.operands[1].reg;
8163   inst.instruction |= inst.operands[2].imm << 7;
8164   inst.instruction |= (msb - 1) << 16;
8165 }
8166
8167 static void
8168 do_bfx (void)
8169 {
8170   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
8171               _("bit-field extends past end of register"));
8172   inst.instruction |= inst.operands[0].reg << 12;
8173   inst.instruction |= inst.operands[1].reg;
8174   inst.instruction |= inst.operands[2].imm << 7;
8175   inst.instruction |= (inst.operands[3].imm - 1) << 16;
8176 }
8177
8178 /* ARM V5 breakpoint instruction (argument parse)
8179      BKPT <16 bit unsigned immediate>
8180      Instruction is not conditional.
8181         The bit pattern given in insns[] has the COND_ALWAYS condition,
8182         and it is an error if the caller tried to override that.  */
8183
8184 static void
8185 do_bkpt (void)
8186 {
8187   /* Top 12 of 16 bits to bits 19:8.  */
8188   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
8189
8190   /* Bottom 4 of 16 bits to bits 3:0.  */
8191   inst.instruction |= inst.operands[0].imm & 0xf;
8192 }
8193
8194 static void
8195 encode_branch (int default_reloc)
8196 {
8197   if (inst.operands[0].hasreloc)
8198     {
8199       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
8200                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
8201                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
8202       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
8203         ? BFD_RELOC_ARM_PLT32
8204         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
8205     }
8206   else
8207     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
8208   inst.reloc.pc_rel = 1;
8209 }
8210
8211 static void
8212 do_branch (void)
8213 {
8214 #ifdef OBJ_ELF
8215   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8216     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8217   else
8218 #endif
8219     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8220 }
8221
8222 static void
8223 do_bl (void)
8224 {
8225 #ifdef OBJ_ELF
8226   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8227     {
8228       if (inst.cond == COND_ALWAYS)
8229         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
8230       else
8231         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8232     }
8233   else
8234 #endif
8235     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8236 }
8237
8238 /* ARM V5 branch-link-exchange instruction (argument parse)
8239      BLX <target_addr>          ie BLX(1)
8240      BLX{<condition>} <Rm>      ie BLX(2)
8241    Unfortunately, there are two different opcodes for this mnemonic.
8242    So, the insns[].value is not used, and the code here zaps values
8243         into inst.instruction.
8244    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
8245
8246 static void
8247 do_blx (void)
8248 {
8249   if (inst.operands[0].isreg)
8250     {
8251       /* Arg is a register; the opcode provided by insns[] is correct.
8252          It is not illegal to do "blx pc", just useless.  */
8253       if (inst.operands[0].reg == REG_PC)
8254         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
8255
8256       inst.instruction |= inst.operands[0].reg;
8257     }
8258   else
8259     {
8260       /* Arg is an address; this instruction cannot be executed
8261          conditionally, and the opcode must be adjusted.
8262          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
8263          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
8264       constraint (inst.cond != COND_ALWAYS, BAD_COND);
8265       inst.instruction = 0xfa000000;
8266       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
8267     }
8268 }
8269
8270 static void
8271 do_bx (void)
8272 {
8273   bfd_boolean want_reloc;
8274
8275   if (inst.operands[0].reg == REG_PC)
8276     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
8277
8278   inst.instruction |= inst.operands[0].reg;
8279   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
8280      it is for ARMv4t or earlier.  */
8281   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
8282   if (object_arch && !ARM_CPU_HAS_FEATURE (*object_arch, arm_ext_v5))
8283       want_reloc = TRUE;
8284
8285 #ifdef OBJ_ELF
8286   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
8287 #endif
8288     want_reloc = FALSE;
8289
8290   if (want_reloc)
8291     inst.reloc.type = BFD_RELOC_ARM_V4BX;
8292 }
8293
8294
8295 /* ARM v5TEJ.  Jump to Jazelle code.  */
8296
8297 static void
8298 do_bxj (void)
8299 {
8300   if (inst.operands[0].reg == REG_PC)
8301     as_tsktsk (_("use of r15 in bxj is not really useful"));
8302
8303   inst.instruction |= inst.operands[0].reg;
8304 }
8305
8306 /* Co-processor data operation:
8307       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
8308       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
8309 static void
8310 do_cdp (void)
8311 {
8312   inst.instruction |= inst.operands[0].reg << 8;
8313   inst.instruction |= inst.operands[1].imm << 20;
8314   inst.instruction |= inst.operands[2].reg << 12;
8315   inst.instruction |= inst.operands[3].reg << 16;
8316   inst.instruction |= inst.operands[4].reg;
8317   inst.instruction |= inst.operands[5].imm << 5;
8318 }
8319
8320 static void
8321 do_cmp (void)
8322 {
8323   inst.instruction |= inst.operands[0].reg << 16;
8324   encode_arm_shifter_operand (1);
8325 }
8326
8327 /* Transfer between coprocessor and ARM registers.
8328    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
8329    MRC2
8330    MCR{cond}
8331    MCR2
8332
8333    No special properties.  */
8334
8335 struct deprecated_coproc_regs_s
8336 {
8337   unsigned cp;
8338   int opc1;
8339   unsigned crn;
8340   unsigned crm;
8341   int opc2;
8342   arm_feature_set deprecated;
8343   arm_feature_set obsoleted;
8344   const char *dep_msg;
8345   const char *obs_msg;
8346 };
8347
8348 #define DEPR_ACCESS_V8 \
8349   N_("This coprocessor register access is deprecated in ARMv8")
8350
8351 /* Table of all deprecated coprocessor registers.  */
8352 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
8353 {
8354     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
8355      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8356      DEPR_ACCESS_V8, NULL},
8357     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
8358      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8359      DEPR_ACCESS_V8, NULL},
8360     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
8361      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8362      DEPR_ACCESS_V8, NULL},
8363     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
8364      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8365      DEPR_ACCESS_V8, NULL},
8366     {14, 6, 0,  0, 0,                                   /* TEECR.  */
8367      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8368      DEPR_ACCESS_V8, NULL},
8369 };
8370
8371 #undef DEPR_ACCESS_V8
8372
8373 static const size_t deprecated_coproc_reg_count =
8374   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
8375
8376 static void
8377 do_co_reg (void)
8378 {
8379   unsigned Rd;
8380   size_t i;
8381
8382   Rd = inst.operands[2].reg;
8383   if (thumb_mode)
8384     {
8385       if (inst.instruction == 0xee000010
8386           || inst.instruction == 0xfe000010)
8387         /* MCR, MCR2  */
8388         reject_bad_reg (Rd);
8389       else
8390         /* MRC, MRC2  */
8391         constraint (Rd == REG_SP, BAD_SP);
8392     }
8393   else
8394     {
8395       /* MCR */
8396       if (inst.instruction == 0xe000010)
8397         constraint (Rd == REG_PC, BAD_PC);
8398     }
8399
8400     for (i = 0; i < deprecated_coproc_reg_count; ++i)
8401       {
8402         const struct deprecated_coproc_regs_s *r =
8403           deprecated_coproc_regs + i;
8404
8405         if (inst.operands[0].reg == r->cp
8406             && inst.operands[1].imm == r->opc1
8407             && inst.operands[3].reg == r->crn
8408             && inst.operands[4].reg == r->crm
8409             && inst.operands[5].imm == r->opc2)
8410           {
8411             if (! ARM_CPU_IS_ANY (cpu_variant)
8412                 && warn_on_deprecated
8413                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
8414               as_tsktsk ("%s", r->dep_msg);
8415           }
8416       }
8417
8418   inst.instruction |= inst.operands[0].reg << 8;
8419   inst.instruction |= inst.operands[1].imm << 21;
8420   inst.instruction |= Rd << 12;
8421   inst.instruction |= inst.operands[3].reg << 16;
8422   inst.instruction |= inst.operands[4].reg;
8423   inst.instruction |= inst.operands[5].imm << 5;
8424 }
8425
8426 /* Transfer between coprocessor register and pair of ARM registers.
8427    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
8428    MCRR2
8429    MRRC{cond}
8430    MRRC2
8431
8432    Two XScale instructions are special cases of these:
8433
8434      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
8435      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
8436
8437    Result unpredictable if Rd or Rn is R15.  */
8438
8439 static void
8440 do_co_reg2c (void)
8441 {
8442   unsigned Rd, Rn;
8443
8444   Rd = inst.operands[2].reg;
8445   Rn = inst.operands[3].reg;
8446
8447   if (thumb_mode)
8448     {
8449       reject_bad_reg (Rd);
8450       reject_bad_reg (Rn);
8451     }
8452   else
8453     {
8454       constraint (Rd == REG_PC, BAD_PC);
8455       constraint (Rn == REG_PC, BAD_PC);
8456     }
8457
8458   inst.instruction |= inst.operands[0].reg << 8;
8459   inst.instruction |= inst.operands[1].imm << 4;
8460   inst.instruction |= Rd << 12;
8461   inst.instruction |= Rn << 16;
8462   inst.instruction |= inst.operands[4].reg;
8463 }
8464
8465 static void
8466 do_cpsi (void)
8467 {
8468   inst.instruction |= inst.operands[0].imm << 6;
8469   if (inst.operands[1].present)
8470     {
8471       inst.instruction |= CPSI_MMOD;
8472       inst.instruction |= inst.operands[1].imm;
8473     }
8474 }
8475
8476 static void
8477 do_dbg (void)
8478 {
8479   inst.instruction |= inst.operands[0].imm;
8480 }
8481
8482 static void
8483 do_div (void)
8484 {
8485   unsigned Rd, Rn, Rm;
8486
8487   Rd = inst.operands[0].reg;
8488   Rn = (inst.operands[1].present
8489         ? inst.operands[1].reg : Rd);
8490   Rm = inst.operands[2].reg;
8491
8492   constraint ((Rd == REG_PC), BAD_PC);
8493   constraint ((Rn == REG_PC), BAD_PC);
8494   constraint ((Rm == REG_PC), BAD_PC);
8495
8496   inst.instruction |= Rd << 16;
8497   inst.instruction |= Rn << 0;
8498   inst.instruction |= Rm << 8;
8499 }
8500
8501 static void
8502 do_it (void)
8503 {
8504   /* There is no IT instruction in ARM mode.  We
8505      process it to do the validation as if in
8506      thumb mode, just in case the code gets
8507      assembled for thumb using the unified syntax.  */
8508
8509   inst.size = 0;
8510   if (unified_syntax)
8511     {
8512       set_it_insn_type (IT_INSN);
8513       now_it.mask = (inst.instruction & 0xf) | 0x10;
8514       now_it.cc = inst.operands[0].imm;
8515     }
8516 }
8517
8518 /* If there is only one register in the register list,
8519    then return its register number.  Otherwise return -1.  */
8520 static int
8521 only_one_reg_in_list (int range)
8522 {
8523   int i = ffs (range) - 1;
8524   return (i > 15 || range != (1 << i)) ? -1 : i;
8525 }
8526
8527 static void
8528 encode_ldmstm(int from_push_pop_mnem)
8529 {
8530   int base_reg = inst.operands[0].reg;
8531   int range = inst.operands[1].imm;
8532   int one_reg;
8533
8534   inst.instruction |= base_reg << 16;
8535   inst.instruction |= range;
8536
8537   if (inst.operands[1].writeback)
8538     inst.instruction |= LDM_TYPE_2_OR_3;
8539
8540   if (inst.operands[0].writeback)
8541     {
8542       inst.instruction |= WRITE_BACK;
8543       /* Check for unpredictable uses of writeback.  */
8544       if (inst.instruction & LOAD_BIT)
8545         {
8546           /* Not allowed in LDM type 2.  */
8547           if ((inst.instruction & LDM_TYPE_2_OR_3)
8548               && ((range & (1 << REG_PC)) == 0))
8549             as_warn (_("writeback of base register is UNPREDICTABLE"));
8550           /* Only allowed if base reg not in list for other types.  */
8551           else if (range & (1 << base_reg))
8552             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
8553         }
8554       else /* STM.  */
8555         {
8556           /* Not allowed for type 2.  */
8557           if (inst.instruction & LDM_TYPE_2_OR_3)
8558             as_warn (_("writeback of base register is UNPREDICTABLE"));
8559           /* Only allowed if base reg not in list, or first in list.  */
8560           else if ((range & (1 << base_reg))
8561                    && (range & ((1 << base_reg) - 1)))
8562             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
8563         }
8564     }
8565
8566   /* If PUSH/POP has only one register, then use the A2 encoding.  */
8567   one_reg = only_one_reg_in_list (range);
8568   if (from_push_pop_mnem && one_reg >= 0)
8569     {
8570       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
8571
8572       inst.instruction &= A_COND_MASK;
8573       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
8574       inst.instruction |= one_reg << 12;
8575     }
8576 }
8577
8578 static void
8579 do_ldmstm (void)
8580 {
8581   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
8582 }
8583
8584 /* ARMv5TE load-consecutive (argument parse)
8585    Mode is like LDRH.
8586
8587      LDRccD R, mode
8588      STRccD R, mode.  */
8589
8590 static void
8591 do_ldrd (void)
8592 {
8593   constraint (inst.operands[0].reg % 2 != 0,
8594               _("first transfer register must be even"));
8595   constraint (inst.operands[1].present
8596               && inst.operands[1].reg != inst.operands[0].reg + 1,
8597               _("can only transfer two consecutive registers"));
8598   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8599   constraint (!inst.operands[2].isreg, _("'[' expected"));
8600
8601   if (!inst.operands[1].present)
8602     inst.operands[1].reg = inst.operands[0].reg + 1;
8603
8604   /* encode_arm_addr_mode_3 will diagnose overlap between the base
8605      register and the first register written; we have to diagnose
8606      overlap between the base and the second register written here.  */
8607
8608   if (inst.operands[2].reg == inst.operands[1].reg
8609       && (inst.operands[2].writeback || inst.operands[2].postind))
8610     as_warn (_("base register written back, and overlaps "
8611                "second transfer register"));
8612
8613   if (!(inst.instruction & V4_STR_BIT))
8614     {
8615       /* For an index-register load, the index register must not overlap the
8616         destination (even if not write-back).  */
8617       if (inst.operands[2].immisreg
8618               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
8619               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
8620         as_warn (_("index register overlaps transfer register"));
8621     }
8622   inst.instruction |= inst.operands[0].reg << 12;
8623   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
8624 }
8625
8626 static void
8627 do_ldrex (void)
8628 {
8629   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
8630               || inst.operands[1].postind || inst.operands[1].writeback
8631               || inst.operands[1].immisreg || inst.operands[1].shifted
8632               || inst.operands[1].negative
8633               /* This can arise if the programmer has written
8634                    strex rN, rM, foo
8635                  or if they have mistakenly used a register name as the last
8636                  operand,  eg:
8637                    strex rN, rM, rX
8638                  It is very difficult to distinguish between these two cases
8639                  because "rX" might actually be a label. ie the register
8640                  name has been occluded by a symbol of the same name. So we
8641                  just generate a general 'bad addressing mode' type error
8642                  message and leave it up to the programmer to discover the
8643                  true cause and fix their mistake.  */
8644               || (inst.operands[1].reg == REG_PC),
8645               BAD_ADDR_MODE);
8646
8647   constraint (inst.reloc.exp.X_op != O_constant
8648               || inst.reloc.exp.X_add_number != 0,
8649               _("offset must be zero in ARM encoding"));
8650
8651   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
8652
8653   inst.instruction |= inst.operands[0].reg << 12;
8654   inst.instruction |= inst.operands[1].reg << 16;
8655   inst.reloc.type = BFD_RELOC_UNUSED;
8656 }
8657
8658 static void
8659 do_ldrexd (void)
8660 {
8661   constraint (inst.operands[0].reg % 2 != 0,
8662               _("even register required"));
8663   constraint (inst.operands[1].present
8664               && inst.operands[1].reg != inst.operands[0].reg + 1,
8665               _("can only load two consecutive registers"));
8666   /* If op 1 were present and equal to PC, this function wouldn't
8667      have been called in the first place.  */
8668   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8669
8670   inst.instruction |= inst.operands[0].reg << 12;
8671   inst.instruction |= inst.operands[2].reg << 16;
8672 }
8673
8674 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
8675    which is not a multiple of four is UNPREDICTABLE.  */
8676 static void
8677 check_ldr_r15_aligned (void)
8678 {
8679   constraint (!(inst.operands[1].immisreg)
8680               && (inst.operands[0].reg == REG_PC
8681               && inst.operands[1].reg == REG_PC
8682               && (inst.reloc.exp.X_add_number & 0x3)),
8683               _("ldr to register 15 must be 4-byte alligned"));
8684 }
8685
8686 static void
8687 do_ldst (void)
8688 {
8689   inst.instruction |= inst.operands[0].reg << 12;
8690   if (!inst.operands[1].isreg)
8691     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
8692       return;
8693   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
8694   check_ldr_r15_aligned ();
8695 }
8696
8697 static void
8698 do_ldstt (void)
8699 {
8700   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8701      reject [Rn,...].  */
8702   if (inst.operands[1].preind)
8703     {
8704       constraint (inst.reloc.exp.X_op != O_constant
8705                   || inst.reloc.exp.X_add_number != 0,
8706                   _("this instruction requires a post-indexed address"));
8707
8708       inst.operands[1].preind = 0;
8709       inst.operands[1].postind = 1;
8710       inst.operands[1].writeback = 1;
8711     }
8712   inst.instruction |= inst.operands[0].reg << 12;
8713   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
8714 }
8715
8716 /* Halfword and signed-byte load/store operations.  */
8717
8718 static void
8719 do_ldstv4 (void)
8720 {
8721   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8722   inst.instruction |= inst.operands[0].reg << 12;
8723   if (!inst.operands[1].isreg)
8724     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
8725       return;
8726   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
8727 }
8728
8729 static void
8730 do_ldsttv4 (void)
8731 {
8732   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8733      reject [Rn,...].  */
8734   if (inst.operands[1].preind)
8735     {
8736       constraint (inst.reloc.exp.X_op != O_constant
8737                   || inst.reloc.exp.X_add_number != 0,
8738                   _("this instruction requires a post-indexed address"));
8739
8740       inst.operands[1].preind = 0;
8741       inst.operands[1].postind = 1;
8742       inst.operands[1].writeback = 1;
8743     }
8744   inst.instruction |= inst.operands[0].reg << 12;
8745   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
8746 }
8747
8748 /* Co-processor register load/store.
8749    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
8750 static void
8751 do_lstc (void)
8752 {
8753   inst.instruction |= inst.operands[0].reg << 8;
8754   inst.instruction |= inst.operands[1].reg << 12;
8755   encode_arm_cp_address (2, TRUE, TRUE, 0);
8756 }
8757
8758 static void
8759 do_mlas (void)
8760 {
8761   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
8762   if (inst.operands[0].reg == inst.operands[1].reg
8763       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
8764       && !(inst.instruction & 0x00400000))
8765     as_tsktsk (_("Rd and Rm should be different in mla"));
8766
8767   inst.instruction |= inst.operands[0].reg << 16;
8768   inst.instruction |= inst.operands[1].reg;
8769   inst.instruction |= inst.operands[2].reg << 8;
8770   inst.instruction |= inst.operands[3].reg << 12;
8771 }
8772
8773 static void
8774 do_mov (void)
8775 {
8776   inst.instruction |= inst.operands[0].reg << 12;
8777   encode_arm_shifter_operand (1);
8778 }
8779
8780 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
8781 static void
8782 do_mov16 (void)
8783 {
8784   bfd_vma imm;
8785   bfd_boolean top;
8786
8787   top = (inst.instruction & 0x00400000) != 0;
8788   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
8789               _(":lower16: not allowed this instruction"));
8790   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
8791               _(":upper16: not allowed instruction"));
8792   inst.instruction |= inst.operands[0].reg << 12;
8793   if (inst.reloc.type == BFD_RELOC_UNUSED)
8794     {
8795       imm = inst.reloc.exp.X_add_number;
8796       /* The value is in two pieces: 0:11, 16:19.  */
8797       inst.instruction |= (imm & 0x00000fff);
8798       inst.instruction |= (imm & 0x0000f000) << 4;
8799     }
8800 }
8801
8802 static void do_vfp_nsyn_opcode (const char *);
8803
8804 static int
8805 do_vfp_nsyn_mrs (void)
8806 {
8807   if (inst.operands[0].isvec)
8808     {
8809       if (inst.operands[1].reg != 1)
8810         first_error (_("operand 1 must be FPSCR"));
8811       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
8812       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
8813       do_vfp_nsyn_opcode ("fmstat");
8814     }
8815   else if (inst.operands[1].isvec)
8816     do_vfp_nsyn_opcode ("fmrx");
8817   else
8818     return FAIL;
8819
8820   return SUCCESS;
8821 }
8822
8823 static int
8824 do_vfp_nsyn_msr (void)
8825 {
8826   if (inst.operands[0].isvec)
8827     do_vfp_nsyn_opcode ("fmxr");
8828   else
8829     return FAIL;
8830
8831   return SUCCESS;
8832 }
8833
8834 static void
8835 do_vmrs (void)
8836 {
8837   unsigned Rt = inst.operands[0].reg;
8838
8839   if (thumb_mode && Rt == REG_SP)
8840     {
8841       inst.error = BAD_SP;
8842       return;
8843     }
8844
8845   /* APSR_ sets isvec. All other refs to PC are illegal.  */
8846   if (!inst.operands[0].isvec && Rt == REG_PC)
8847     {
8848       inst.error = BAD_PC;
8849       return;
8850     }
8851
8852   /* If we get through parsing the register name, we just insert the number
8853      generated into the instruction without further validation.  */
8854   inst.instruction |= (inst.operands[1].reg << 16);
8855   inst.instruction |= (Rt << 12);
8856 }
8857
8858 static void
8859 do_vmsr (void)
8860 {
8861   unsigned Rt = inst.operands[1].reg;
8862
8863   if (thumb_mode)
8864     reject_bad_reg (Rt);
8865   else if (Rt == REG_PC)
8866     {
8867       inst.error = BAD_PC;
8868       return;
8869     }
8870
8871   /* If we get through parsing the register name, we just insert the number
8872      generated into the instruction without further validation.  */
8873   inst.instruction |= (inst.operands[0].reg << 16);
8874   inst.instruction |= (Rt << 12);
8875 }
8876
8877 static void
8878 do_mrs (void)
8879 {
8880   unsigned br;
8881
8882   if (do_vfp_nsyn_mrs () == SUCCESS)
8883     return;
8884
8885   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8886   inst.instruction |= inst.operands[0].reg << 12;
8887
8888   if (inst.operands[1].isreg)
8889     {
8890       br = inst.operands[1].reg;
8891       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf000))
8892         as_bad (_("bad register for mrs"));
8893     }
8894   else
8895     {
8896       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
8897       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
8898                   != (PSR_c|PSR_f),
8899                   _("'APSR', 'CPSR' or 'SPSR' expected"));
8900       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
8901     }
8902
8903   inst.instruction |= br;
8904 }
8905
8906 /* Two possible forms:
8907       "{C|S}PSR_<field>, Rm",
8908       "{C|S}PSR_f, #expression".  */
8909
8910 static void
8911 do_msr (void)
8912 {
8913   if (do_vfp_nsyn_msr () == SUCCESS)
8914     return;
8915
8916   inst.instruction |= inst.operands[0].imm;
8917   if (inst.operands[1].isreg)
8918     inst.instruction |= inst.operands[1].reg;
8919   else
8920     {
8921       inst.instruction |= INST_IMMEDIATE;
8922       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8923       inst.reloc.pc_rel = 0;
8924     }
8925 }
8926
8927 static void
8928 do_mul (void)
8929 {
8930   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
8931
8932   if (!inst.operands[2].present)
8933     inst.operands[2].reg = inst.operands[0].reg;
8934   inst.instruction |= inst.operands[0].reg << 16;
8935   inst.instruction |= inst.operands[1].reg;
8936   inst.instruction |= inst.operands[2].reg << 8;
8937
8938   if (inst.operands[0].reg == inst.operands[1].reg
8939       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8940     as_tsktsk (_("Rd and Rm should be different in mul"));
8941 }
8942
8943 /* Long Multiply Parser
8944    UMULL RdLo, RdHi, Rm, Rs
8945    SMULL RdLo, RdHi, Rm, Rs
8946    UMLAL RdLo, RdHi, Rm, Rs
8947    SMLAL RdLo, RdHi, Rm, Rs.  */
8948
8949 static void
8950 do_mull (void)
8951 {
8952   inst.instruction |= inst.operands[0].reg << 12;
8953   inst.instruction |= inst.operands[1].reg << 16;
8954   inst.instruction |= inst.operands[2].reg;
8955   inst.instruction |= inst.operands[3].reg << 8;
8956
8957   /* rdhi and rdlo must be different.  */
8958   if (inst.operands[0].reg == inst.operands[1].reg)
8959     as_tsktsk (_("rdhi and rdlo must be different"));
8960
8961   /* rdhi, rdlo and rm must all be different before armv6.  */
8962   if ((inst.operands[0].reg == inst.operands[2].reg
8963       || inst.operands[1].reg == inst.operands[2].reg)
8964       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8965     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
8966 }
8967
8968 static void
8969 do_nop (void)
8970 {
8971   if (inst.operands[0].present
8972       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
8973     {
8974       /* Architectural NOP hints are CPSR sets with no bits selected.  */
8975       inst.instruction &= 0xf0000000;
8976       inst.instruction |= 0x0320f000;
8977       if (inst.operands[0].present)
8978         inst.instruction |= inst.operands[0].imm;
8979     }
8980 }
8981
8982 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
8983    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
8984    Condition defaults to COND_ALWAYS.
8985    Error if Rd, Rn or Rm are R15.  */
8986
8987 static void
8988 do_pkhbt (void)
8989 {
8990   inst.instruction |= inst.operands[0].reg << 12;
8991   inst.instruction |= inst.operands[1].reg << 16;
8992   inst.instruction |= inst.operands[2].reg;
8993   if (inst.operands[3].present)
8994     encode_arm_shift (3);
8995 }
8996
8997 /* ARM V6 PKHTB (Argument Parse).  */
8998
8999 static void
9000 do_pkhtb (void)
9001 {
9002   if (!inst.operands[3].present)
9003     {
9004       /* If the shift specifier is omitted, turn the instruction
9005          into pkhbt rd, rm, rn. */
9006       inst.instruction &= 0xfff00010;
9007       inst.instruction |= inst.operands[0].reg << 12;
9008       inst.instruction |= inst.operands[1].reg;
9009       inst.instruction |= inst.operands[2].reg << 16;
9010     }
9011   else
9012     {
9013       inst.instruction |= inst.operands[0].reg << 12;
9014       inst.instruction |= inst.operands[1].reg << 16;
9015       inst.instruction |= inst.operands[2].reg;
9016       encode_arm_shift (3);
9017     }
9018 }
9019
9020 /* ARMv5TE: Preload-Cache
9021    MP Extensions: Preload for write
9022
9023     PLD(W) <addr_mode>
9024
9025   Syntactically, like LDR with B=1, W=0, L=1.  */
9026
9027 static void
9028 do_pld (void)
9029 {
9030   constraint (!inst.operands[0].isreg,
9031               _("'[' expected after PLD mnemonic"));
9032   constraint (inst.operands[0].postind,
9033               _("post-indexed expression used in preload instruction"));
9034   constraint (inst.operands[0].writeback,
9035               _("writeback used in preload instruction"));
9036   constraint (!inst.operands[0].preind,
9037               _("unindexed addressing used in preload instruction"));
9038   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9039 }
9040
9041 /* ARMv7: PLI <addr_mode>  */
9042 static void
9043 do_pli (void)
9044 {
9045   constraint (!inst.operands[0].isreg,
9046               _("'[' expected after PLI mnemonic"));
9047   constraint (inst.operands[0].postind,
9048               _("post-indexed expression used in preload instruction"));
9049   constraint (inst.operands[0].writeback,
9050               _("writeback used in preload instruction"));
9051   constraint (!inst.operands[0].preind,
9052               _("unindexed addressing used in preload instruction"));
9053   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9054   inst.instruction &= ~PRE_INDEX;
9055 }
9056
9057 static void
9058 do_push_pop (void)
9059 {
9060   constraint (inst.operands[0].writeback,
9061               _("push/pop do not support {reglist}^"));
9062   inst.operands[1] = inst.operands[0];
9063   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
9064   inst.operands[0].isreg = 1;
9065   inst.operands[0].writeback = 1;
9066   inst.operands[0].reg = REG_SP;
9067   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
9068 }
9069
9070 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
9071    word at the specified address and the following word
9072    respectively.
9073    Unconditionally executed.
9074    Error if Rn is R15.  */
9075
9076 static void
9077 do_rfe (void)
9078 {
9079   inst.instruction |= inst.operands[0].reg << 16;
9080   if (inst.operands[0].writeback)
9081     inst.instruction |= WRITE_BACK;
9082 }
9083
9084 /* ARM V6 ssat (argument parse).  */
9085
9086 static void
9087 do_ssat (void)
9088 {
9089   inst.instruction |= inst.operands[0].reg << 12;
9090   inst.instruction |= (inst.operands[1].imm - 1) << 16;
9091   inst.instruction |= inst.operands[2].reg;
9092
9093   if (inst.operands[3].present)
9094     encode_arm_shift (3);
9095 }
9096
9097 /* ARM V6 usat (argument parse).  */
9098
9099 static void
9100 do_usat (void)
9101 {
9102   inst.instruction |= inst.operands[0].reg << 12;
9103   inst.instruction |= inst.operands[1].imm << 16;
9104   inst.instruction |= inst.operands[2].reg;
9105
9106   if (inst.operands[3].present)
9107     encode_arm_shift (3);
9108 }
9109
9110 /* ARM V6 ssat16 (argument parse).  */
9111
9112 static void
9113 do_ssat16 (void)
9114 {
9115   inst.instruction |= inst.operands[0].reg << 12;
9116   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
9117   inst.instruction |= inst.operands[2].reg;
9118 }
9119
9120 static void
9121 do_usat16 (void)
9122 {
9123   inst.instruction |= inst.operands[0].reg << 12;
9124   inst.instruction |= inst.operands[1].imm << 16;
9125   inst.instruction |= inst.operands[2].reg;
9126 }
9127
9128 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
9129    preserving the other bits.
9130
9131    setend <endian_specifier>, where <endian_specifier> is either
9132    BE or LE.  */
9133
9134 static void
9135 do_setend (void)
9136 {
9137   if (warn_on_deprecated
9138       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9139       as_tsktsk (_("setend use is deprecated for ARMv8"));
9140
9141   if (inst.operands[0].imm)
9142     inst.instruction |= 0x200;
9143 }
9144
9145 static void
9146 do_shift (void)
9147 {
9148   unsigned int Rm = (inst.operands[1].present
9149                      ? inst.operands[1].reg
9150                      : inst.operands[0].reg);
9151
9152   inst.instruction |= inst.operands[0].reg << 12;
9153   inst.instruction |= Rm;
9154   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
9155     {
9156       inst.instruction |= inst.operands[2].reg << 8;
9157       inst.instruction |= SHIFT_BY_REG;
9158       /* PR 12854: Error on extraneous shifts.  */
9159       constraint (inst.operands[2].shifted,
9160                   _("extraneous shift as part of operand to shift insn"));
9161     }
9162   else
9163     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
9164 }
9165
9166 static void
9167 do_smc (void)
9168 {
9169   inst.reloc.type = BFD_RELOC_ARM_SMC;
9170   inst.reloc.pc_rel = 0;
9171 }
9172
9173 static void
9174 do_hvc (void)
9175 {
9176   inst.reloc.type = BFD_RELOC_ARM_HVC;
9177   inst.reloc.pc_rel = 0;
9178 }
9179
9180 static void
9181 do_swi (void)
9182 {
9183   inst.reloc.type = BFD_RELOC_ARM_SWI;
9184   inst.reloc.pc_rel = 0;
9185 }
9186
9187 static void
9188 do_setpan (void)
9189 {
9190   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9191               _("selected processor does not support SETPAN instruction"));
9192
9193   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
9194 }
9195
9196 static void
9197 do_t_setpan (void)
9198 {
9199   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9200               _("selected processor does not support SETPAN instruction"));
9201
9202   inst.instruction |= (inst.operands[0].imm << 3);
9203 }
9204
9205 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
9206    SMLAxy{cond} Rd,Rm,Rs,Rn
9207    SMLAWy{cond} Rd,Rm,Rs,Rn
9208    Error if any register is R15.  */
9209
9210 static void
9211 do_smla (void)
9212 {
9213   inst.instruction |= inst.operands[0].reg << 16;
9214   inst.instruction |= inst.operands[1].reg;
9215   inst.instruction |= inst.operands[2].reg << 8;
9216   inst.instruction |= inst.operands[3].reg << 12;
9217 }
9218
9219 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
9220    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
9221    Error if any register is R15.
9222    Warning if Rdlo == Rdhi.  */
9223
9224 static void
9225 do_smlal (void)
9226 {
9227   inst.instruction |= inst.operands[0].reg << 12;
9228   inst.instruction |= inst.operands[1].reg << 16;
9229   inst.instruction |= inst.operands[2].reg;
9230   inst.instruction |= inst.operands[3].reg << 8;
9231
9232   if (inst.operands[0].reg == inst.operands[1].reg)
9233     as_tsktsk (_("rdhi and rdlo must be different"));
9234 }
9235
9236 /* ARM V5E (El Segundo) signed-multiply (argument parse)
9237    SMULxy{cond} Rd,Rm,Rs
9238    Error if any register is R15.  */
9239
9240 static void
9241 do_smul (void)
9242 {
9243   inst.instruction |= inst.operands[0].reg << 16;
9244   inst.instruction |= inst.operands[1].reg;
9245   inst.instruction |= inst.operands[2].reg << 8;
9246 }
9247
9248 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
9249    the same for both ARM and Thumb-2.  */
9250
9251 static void
9252 do_srs (void)
9253 {
9254   int reg;
9255
9256   if (inst.operands[0].present)
9257     {
9258       reg = inst.operands[0].reg;
9259       constraint (reg != REG_SP, _("SRS base register must be r13"));
9260     }
9261   else
9262     reg = REG_SP;
9263
9264   inst.instruction |= reg << 16;
9265   inst.instruction |= inst.operands[1].imm;
9266   if (inst.operands[0].writeback || inst.operands[1].writeback)
9267     inst.instruction |= WRITE_BACK;
9268 }
9269
9270 /* ARM V6 strex (argument parse).  */
9271
9272 static void
9273 do_strex (void)
9274 {
9275   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9276               || inst.operands[2].postind || inst.operands[2].writeback
9277               || inst.operands[2].immisreg || inst.operands[2].shifted
9278               || inst.operands[2].negative
9279               /* See comment in do_ldrex().  */
9280               || (inst.operands[2].reg == REG_PC),
9281               BAD_ADDR_MODE);
9282
9283   constraint (inst.operands[0].reg == inst.operands[1].reg
9284               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9285
9286   constraint (inst.reloc.exp.X_op != O_constant
9287               || inst.reloc.exp.X_add_number != 0,
9288               _("offset must be zero in ARM encoding"));
9289
9290   inst.instruction |= inst.operands[0].reg << 12;
9291   inst.instruction |= inst.operands[1].reg;
9292   inst.instruction |= inst.operands[2].reg << 16;
9293   inst.reloc.type = BFD_RELOC_UNUSED;
9294 }
9295
9296 static void
9297 do_t_strexbh (void)
9298 {
9299   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9300               || inst.operands[2].postind || inst.operands[2].writeback
9301               || inst.operands[2].immisreg || inst.operands[2].shifted
9302               || inst.operands[2].negative,
9303               BAD_ADDR_MODE);
9304
9305   constraint (inst.operands[0].reg == inst.operands[1].reg
9306               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9307
9308   do_rm_rd_rn ();
9309 }
9310
9311 static void
9312 do_strexd (void)
9313 {
9314   constraint (inst.operands[1].reg % 2 != 0,
9315               _("even register required"));
9316   constraint (inst.operands[2].present
9317               && inst.operands[2].reg != inst.operands[1].reg + 1,
9318               _("can only store two consecutive registers"));
9319   /* If op 2 were present and equal to PC, this function wouldn't
9320      have been called in the first place.  */
9321   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
9322
9323   constraint (inst.operands[0].reg == inst.operands[1].reg
9324               || inst.operands[0].reg == inst.operands[1].reg + 1
9325               || inst.operands[0].reg == inst.operands[3].reg,
9326               BAD_OVERLAP);
9327
9328   inst.instruction |= inst.operands[0].reg << 12;
9329   inst.instruction |= inst.operands[1].reg;
9330   inst.instruction |= inst.operands[3].reg << 16;
9331 }
9332
9333 /* ARM V8 STRL.  */
9334 static void
9335 do_stlex (void)
9336 {
9337   constraint (inst.operands[0].reg == inst.operands[1].reg
9338               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9339
9340   do_rd_rm_rn ();
9341 }
9342
9343 static void
9344 do_t_stlex (void)
9345 {
9346   constraint (inst.operands[0].reg == inst.operands[1].reg
9347               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9348
9349   do_rm_rd_rn ();
9350 }
9351
9352 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
9353    extends it to 32-bits, and adds the result to a value in another
9354    register.  You can specify a rotation by 0, 8, 16, or 24 bits
9355    before extracting the 16-bit value.
9356    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
9357    Condition defaults to COND_ALWAYS.
9358    Error if any register uses R15.  */
9359
9360 static void
9361 do_sxtah (void)
9362 {
9363   inst.instruction |= inst.operands[0].reg << 12;
9364   inst.instruction |= inst.operands[1].reg << 16;
9365   inst.instruction |= inst.operands[2].reg;
9366   inst.instruction |= inst.operands[3].imm << 10;
9367 }
9368
9369 /* ARM V6 SXTH.
9370
9371    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
9372    Condition defaults to COND_ALWAYS.
9373    Error if any register uses R15.  */
9374
9375 static void
9376 do_sxth (void)
9377 {
9378   inst.instruction |= inst.operands[0].reg << 12;
9379   inst.instruction |= inst.operands[1].reg;
9380   inst.instruction |= inst.operands[2].imm << 10;
9381 }
9382 \f
9383 /* VFP instructions.  In a logical order: SP variant first, monad
9384    before dyad, arithmetic then move then load/store.  */
9385
9386 static void
9387 do_vfp_sp_monadic (void)
9388 {
9389   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9390   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9391 }
9392
9393 static void
9394 do_vfp_sp_dyadic (void)
9395 {
9396   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9397   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9398   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9399 }
9400
9401 static void
9402 do_vfp_sp_compare_z (void)
9403 {
9404   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9405 }
9406
9407 static void
9408 do_vfp_dp_sp_cvt (void)
9409 {
9410   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9411   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9412 }
9413
9414 static void
9415 do_vfp_sp_dp_cvt (void)
9416 {
9417   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9418   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9419 }
9420
9421 static void
9422 do_vfp_reg_from_sp (void)
9423 {
9424   inst.instruction |= inst.operands[0].reg << 12;
9425   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9426 }
9427
9428 static void
9429 do_vfp_reg2_from_sp2 (void)
9430 {
9431   constraint (inst.operands[2].imm != 2,
9432               _("only two consecutive VFP SP registers allowed here"));
9433   inst.instruction |= inst.operands[0].reg << 12;
9434   inst.instruction |= inst.operands[1].reg << 16;
9435   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9436 }
9437
9438 static void
9439 do_vfp_sp_from_reg (void)
9440 {
9441   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
9442   inst.instruction |= inst.operands[1].reg << 12;
9443 }
9444
9445 static void
9446 do_vfp_sp2_from_reg2 (void)
9447 {
9448   constraint (inst.operands[0].imm != 2,
9449               _("only two consecutive VFP SP registers allowed here"));
9450   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
9451   inst.instruction |= inst.operands[1].reg << 12;
9452   inst.instruction |= inst.operands[2].reg << 16;
9453 }
9454
9455 static void
9456 do_vfp_sp_ldst (void)
9457 {
9458   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9459   encode_arm_cp_address (1, FALSE, TRUE, 0);
9460 }
9461
9462 static void
9463 do_vfp_dp_ldst (void)
9464 {
9465   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9466   encode_arm_cp_address (1, FALSE, TRUE, 0);
9467 }
9468
9469
9470 static void
9471 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
9472 {
9473   if (inst.operands[0].writeback)
9474     inst.instruction |= WRITE_BACK;
9475   else
9476     constraint (ldstm_type != VFP_LDSTMIA,
9477                 _("this addressing mode requires base-register writeback"));
9478   inst.instruction |= inst.operands[0].reg << 16;
9479   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
9480   inst.instruction |= inst.operands[1].imm;
9481 }
9482
9483 static void
9484 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
9485 {
9486   int count;
9487
9488   if (inst.operands[0].writeback)
9489     inst.instruction |= WRITE_BACK;
9490   else
9491     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
9492                 _("this addressing mode requires base-register writeback"));
9493
9494   inst.instruction |= inst.operands[0].reg << 16;
9495   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9496
9497   count = inst.operands[1].imm << 1;
9498   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
9499     count += 1;
9500
9501   inst.instruction |= count;
9502 }
9503
9504 static void
9505 do_vfp_sp_ldstmia (void)
9506 {
9507   vfp_sp_ldstm (VFP_LDSTMIA);
9508 }
9509
9510 static void
9511 do_vfp_sp_ldstmdb (void)
9512 {
9513   vfp_sp_ldstm (VFP_LDSTMDB);
9514 }
9515
9516 static void
9517 do_vfp_dp_ldstmia (void)
9518 {
9519   vfp_dp_ldstm (VFP_LDSTMIA);
9520 }
9521
9522 static void
9523 do_vfp_dp_ldstmdb (void)
9524 {
9525   vfp_dp_ldstm (VFP_LDSTMDB);
9526 }
9527
9528 static void
9529 do_vfp_xp_ldstmia (void)
9530 {
9531   vfp_dp_ldstm (VFP_LDSTMIAX);
9532 }
9533
9534 static void
9535 do_vfp_xp_ldstmdb (void)
9536 {
9537   vfp_dp_ldstm (VFP_LDSTMDBX);
9538 }
9539
9540 static void
9541 do_vfp_dp_rd_rm (void)
9542 {
9543   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9544   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9545 }
9546
9547 static void
9548 do_vfp_dp_rn_rd (void)
9549 {
9550   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
9551   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9552 }
9553
9554 static void
9555 do_vfp_dp_rd_rn (void)
9556 {
9557   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9558   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9559 }
9560
9561 static void
9562 do_vfp_dp_rd_rn_rm (void)
9563 {
9564   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9565   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9566   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
9567 }
9568
9569 static void
9570 do_vfp_dp_rd (void)
9571 {
9572   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9573 }
9574
9575 static void
9576 do_vfp_dp_rm_rd_rn (void)
9577 {
9578   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
9579   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9580   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
9581 }
9582
9583 /* VFPv3 instructions.  */
9584 static void
9585 do_vfp_sp_const (void)
9586 {
9587   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9588   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9589   inst.instruction |= (inst.operands[1].imm & 0x0f);
9590 }
9591
9592 static void
9593 do_vfp_dp_const (void)
9594 {
9595   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9596   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9597   inst.instruction |= (inst.operands[1].imm & 0x0f);
9598 }
9599
9600 static void
9601 vfp_conv (int srcsize)
9602 {
9603   int immbits = srcsize - inst.operands[1].imm;
9604
9605   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
9606     {
9607       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
9608          i.e. immbits must be in range 0 - 16.  */
9609       inst.error = _("immediate value out of range, expected range [0, 16]");
9610       return;
9611     }
9612   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
9613     {
9614       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
9615          i.e. immbits must be in range 0 - 31.  */
9616       inst.error = _("immediate value out of range, expected range [1, 32]");
9617       return;
9618     }
9619
9620   inst.instruction |= (immbits & 1) << 5;
9621   inst.instruction |= (immbits >> 1);
9622 }
9623
9624 static void
9625 do_vfp_sp_conv_16 (void)
9626 {
9627   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9628   vfp_conv (16);
9629 }
9630
9631 static void
9632 do_vfp_dp_conv_16 (void)
9633 {
9634   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9635   vfp_conv (16);
9636 }
9637
9638 static void
9639 do_vfp_sp_conv_32 (void)
9640 {
9641   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9642   vfp_conv (32);
9643 }
9644
9645 static void
9646 do_vfp_dp_conv_32 (void)
9647 {
9648   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9649   vfp_conv (32);
9650 }
9651 \f
9652 /* FPA instructions.  Also in a logical order.  */
9653
9654 static void
9655 do_fpa_cmp (void)
9656 {
9657   inst.instruction |= inst.operands[0].reg << 16;
9658   inst.instruction |= inst.operands[1].reg;
9659 }
9660
9661 static void
9662 do_fpa_ldmstm (void)
9663 {
9664   inst.instruction |= inst.operands[0].reg << 12;
9665   switch (inst.operands[1].imm)
9666     {
9667     case 1: inst.instruction |= CP_T_X;          break;
9668     case 2: inst.instruction |= CP_T_Y;          break;
9669     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
9670     case 4:                                      break;
9671     default: abort ();
9672     }
9673
9674   if (inst.instruction & (PRE_INDEX | INDEX_UP))
9675     {
9676       /* The instruction specified "ea" or "fd", so we can only accept
9677          [Rn]{!}.  The instruction does not really support stacking or
9678          unstacking, so we have to emulate these by setting appropriate
9679          bits and offsets.  */
9680       constraint (inst.reloc.exp.X_op != O_constant
9681                   || inst.reloc.exp.X_add_number != 0,
9682                   _("this instruction does not support indexing"));
9683
9684       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
9685         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
9686
9687       if (!(inst.instruction & INDEX_UP))
9688         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
9689
9690       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
9691         {
9692           inst.operands[2].preind = 0;
9693           inst.operands[2].postind = 1;
9694         }
9695     }
9696
9697   encode_arm_cp_address (2, TRUE, TRUE, 0);
9698 }
9699 \f
9700 /* iWMMXt instructions: strictly in alphabetical order.  */
9701
9702 static void
9703 do_iwmmxt_tandorc (void)
9704 {
9705   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
9706 }
9707
9708 static void
9709 do_iwmmxt_textrc (void)
9710 {
9711   inst.instruction |= inst.operands[0].reg << 12;
9712   inst.instruction |= inst.operands[1].imm;
9713 }
9714
9715 static void
9716 do_iwmmxt_textrm (void)
9717 {
9718   inst.instruction |= inst.operands[0].reg << 12;
9719   inst.instruction |= inst.operands[1].reg << 16;
9720   inst.instruction |= inst.operands[2].imm;
9721 }
9722
9723 static void
9724 do_iwmmxt_tinsr (void)
9725 {
9726   inst.instruction |= inst.operands[0].reg << 16;
9727   inst.instruction |= inst.operands[1].reg << 12;
9728   inst.instruction |= inst.operands[2].imm;
9729 }
9730
9731 static void
9732 do_iwmmxt_tmia (void)
9733 {
9734   inst.instruction |= inst.operands[0].reg << 5;
9735   inst.instruction |= inst.operands[1].reg;
9736   inst.instruction |= inst.operands[2].reg << 12;
9737 }
9738
9739 static void
9740 do_iwmmxt_waligni (void)
9741 {
9742   inst.instruction |= inst.operands[0].reg << 12;
9743   inst.instruction |= inst.operands[1].reg << 16;
9744   inst.instruction |= inst.operands[2].reg;
9745   inst.instruction |= inst.operands[3].imm << 20;
9746 }
9747
9748 static void
9749 do_iwmmxt_wmerge (void)
9750 {
9751   inst.instruction |= inst.operands[0].reg << 12;
9752   inst.instruction |= inst.operands[1].reg << 16;
9753   inst.instruction |= inst.operands[2].reg;
9754   inst.instruction |= inst.operands[3].imm << 21;
9755 }
9756
9757 static void
9758 do_iwmmxt_wmov (void)
9759 {
9760   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
9761   inst.instruction |= inst.operands[0].reg << 12;
9762   inst.instruction |= inst.operands[1].reg << 16;
9763   inst.instruction |= inst.operands[1].reg;
9764 }
9765
9766 static void
9767 do_iwmmxt_wldstbh (void)
9768 {
9769   int reloc;
9770   inst.instruction |= inst.operands[0].reg << 12;
9771   if (thumb_mode)
9772     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
9773   else
9774     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
9775   encode_arm_cp_address (1, TRUE, FALSE, reloc);
9776 }
9777
9778 static void
9779 do_iwmmxt_wldstw (void)
9780 {
9781   /* RIWR_RIWC clears .isreg for a control register.  */
9782   if (!inst.operands[0].isreg)
9783     {
9784       constraint (inst.cond != COND_ALWAYS, BAD_COND);
9785       inst.instruction |= 0xf0000000;
9786     }
9787
9788   inst.instruction |= inst.operands[0].reg << 12;
9789   encode_arm_cp_address (1, TRUE, TRUE, 0);
9790 }
9791
9792 static void
9793 do_iwmmxt_wldstd (void)
9794 {
9795   inst.instruction |= inst.operands[0].reg << 12;
9796   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
9797       && inst.operands[1].immisreg)
9798     {
9799       inst.instruction &= ~0x1a000ff;
9800       inst.instruction |= (0xf << 28);
9801       if (inst.operands[1].preind)
9802         inst.instruction |= PRE_INDEX;
9803       if (!inst.operands[1].negative)
9804         inst.instruction |= INDEX_UP;
9805       if (inst.operands[1].writeback)
9806         inst.instruction |= WRITE_BACK;
9807       inst.instruction |= inst.operands[1].reg << 16;
9808       inst.instruction |= inst.reloc.exp.X_add_number << 4;
9809       inst.instruction |= inst.operands[1].imm;
9810     }
9811   else
9812     encode_arm_cp_address (1, TRUE, FALSE, 0);
9813 }
9814
9815 static void
9816 do_iwmmxt_wshufh (void)
9817 {
9818   inst.instruction |= inst.operands[0].reg << 12;
9819   inst.instruction |= inst.operands[1].reg << 16;
9820   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
9821   inst.instruction |= (inst.operands[2].imm & 0x0f);
9822 }
9823
9824 static void
9825 do_iwmmxt_wzero (void)
9826 {
9827   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
9828   inst.instruction |= inst.operands[0].reg;
9829   inst.instruction |= inst.operands[0].reg << 12;
9830   inst.instruction |= inst.operands[0].reg << 16;
9831 }
9832
9833 static void
9834 do_iwmmxt_wrwrwr_or_imm5 (void)
9835 {
9836   if (inst.operands[2].isreg)
9837     do_rd_rn_rm ();
9838   else {
9839     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
9840                 _("immediate operand requires iWMMXt2"));
9841     do_rd_rn ();
9842     if (inst.operands[2].imm == 0)
9843       {
9844         switch ((inst.instruction >> 20) & 0xf)
9845           {
9846           case 4:
9847           case 5:
9848           case 6:
9849           case 7:
9850             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
9851             inst.operands[2].imm = 16;
9852             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
9853             break;
9854           case 8:
9855           case 9:
9856           case 10:
9857           case 11:
9858             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
9859             inst.operands[2].imm = 32;
9860             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
9861             break;
9862           case 12:
9863           case 13:
9864           case 14:
9865           case 15:
9866             {
9867               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
9868               unsigned long wrn;
9869               wrn = (inst.instruction >> 16) & 0xf;
9870               inst.instruction &= 0xff0fff0f;
9871               inst.instruction |= wrn;
9872               /* Bail out here; the instruction is now assembled.  */
9873               return;
9874             }
9875           }
9876       }
9877     /* Map 32 -> 0, etc.  */
9878     inst.operands[2].imm &= 0x1f;
9879     inst.instruction |= (0xf << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
9880   }
9881 }
9882 \f
9883 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
9884    operations first, then control, shift, and load/store.  */
9885
9886 /* Insns like "foo X,Y,Z".  */
9887
9888 static void
9889 do_mav_triple (void)
9890 {
9891   inst.instruction |= inst.operands[0].reg << 16;
9892   inst.instruction |= inst.operands[1].reg;
9893   inst.instruction |= inst.operands[2].reg << 12;
9894 }
9895
9896 /* Insns like "foo W,X,Y,Z".
9897     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
9898
9899 static void
9900 do_mav_quad (void)
9901 {
9902   inst.instruction |= inst.operands[0].reg << 5;
9903   inst.instruction |= inst.operands[1].reg << 12;
9904   inst.instruction |= inst.operands[2].reg << 16;
9905   inst.instruction |= inst.operands[3].reg;
9906 }
9907
9908 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
9909 static void
9910 do_mav_dspsc (void)
9911 {
9912   inst.instruction |= inst.operands[1].reg << 12;
9913 }
9914
9915 /* Maverick shift immediate instructions.
9916    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
9917    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
9918
9919 static void
9920 do_mav_shift (void)
9921 {
9922   int imm = inst.operands[2].imm;
9923
9924   inst.instruction |= inst.operands[0].reg << 12;
9925   inst.instruction |= inst.operands[1].reg << 16;
9926
9927   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
9928      Bits 5-7 of the insn should have bits 4-6 of the immediate.
9929      Bit 4 should be 0.  */
9930   imm = (imm & 0xf) | ((imm & 0x70) << 1);
9931
9932   inst.instruction |= imm;
9933 }
9934 \f
9935 /* XScale instructions.  Also sorted arithmetic before move.  */
9936
9937 /* Xscale multiply-accumulate (argument parse)
9938      MIAcc   acc0,Rm,Rs
9939      MIAPHcc acc0,Rm,Rs
9940      MIAxycc acc0,Rm,Rs.  */
9941
9942 static void
9943 do_xsc_mia (void)
9944 {
9945   inst.instruction |= inst.operands[1].reg;
9946   inst.instruction |= inst.operands[2].reg << 12;
9947 }
9948
9949 /* Xscale move-accumulator-register (argument parse)
9950
9951      MARcc   acc0,RdLo,RdHi.  */
9952
9953 static void
9954 do_xsc_mar (void)
9955 {
9956   inst.instruction |= inst.operands[1].reg << 12;
9957   inst.instruction |= inst.operands[2].reg << 16;
9958 }
9959
9960 /* Xscale move-register-accumulator (argument parse)
9961
9962      MRAcc   RdLo,RdHi,acc0.  */
9963
9964 static void
9965 do_xsc_mra (void)
9966 {
9967   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
9968   inst.instruction |= inst.operands[0].reg << 12;
9969   inst.instruction |= inst.operands[1].reg << 16;
9970 }
9971 \f
9972 /* Encoding functions relevant only to Thumb.  */
9973
9974 /* inst.operands[i] is a shifted-register operand; encode
9975    it into inst.instruction in the format used by Thumb32.  */
9976
9977 static void
9978 encode_thumb32_shifted_operand (int i)
9979 {
9980   unsigned int value = inst.reloc.exp.X_add_number;
9981   unsigned int shift = inst.operands[i].shift_kind;
9982
9983   constraint (inst.operands[i].immisreg,
9984               _("shift by register not allowed in thumb mode"));
9985   inst.instruction |= inst.operands[i].reg;
9986   if (shift == SHIFT_RRX)
9987     inst.instruction |= SHIFT_ROR << 4;
9988   else
9989     {
9990       constraint (inst.reloc.exp.X_op != O_constant,
9991                   _("expression too complex"));
9992
9993       constraint (value > 32
9994                   || (value == 32 && (shift == SHIFT_LSL
9995                                       || shift == SHIFT_ROR)),
9996                   _("shift expression is too large"));
9997
9998       if (value == 0)
9999         shift = SHIFT_LSL;
10000       else if (value == 32)
10001         value = 0;
10002
10003       inst.instruction |= shift << 4;
10004       inst.instruction |= (value & 0x1c) << 10;
10005       inst.instruction |= (value & 0x03) << 6;
10006     }
10007 }
10008
10009
10010 /* inst.operands[i] was set up by parse_address.  Encode it into a
10011    Thumb32 format load or store instruction.  Reject forms that cannot
10012    be used with such instructions.  If is_t is true, reject forms that
10013    cannot be used with a T instruction; if is_d is true, reject forms
10014    that cannot be used with a D instruction.  If it is a store insn,
10015    reject PC in Rn.  */
10016
10017 static void
10018 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
10019 {
10020   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
10021
10022   constraint (!inst.operands[i].isreg,
10023               _("Instruction does not support =N addresses"));
10024
10025   inst.instruction |= inst.operands[i].reg << 16;
10026   if (inst.operands[i].immisreg)
10027     {
10028       constraint (is_pc, BAD_PC_ADDRESSING);
10029       constraint (is_t || is_d, _("cannot use register index with this instruction"));
10030       constraint (inst.operands[i].negative,
10031                   _("Thumb does not support negative register indexing"));
10032       constraint (inst.operands[i].postind,
10033                   _("Thumb does not support register post-indexing"));
10034       constraint (inst.operands[i].writeback,
10035                   _("Thumb does not support register indexing with writeback"));
10036       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
10037                   _("Thumb supports only LSL in shifted register indexing"));
10038
10039       inst.instruction |= inst.operands[i].imm;
10040       if (inst.operands[i].shifted)
10041         {
10042           constraint (inst.reloc.exp.X_op != O_constant,
10043                       _("expression too complex"));
10044           constraint (inst.reloc.exp.X_add_number < 0
10045                       || inst.reloc.exp.X_add_number > 3,
10046                       _("shift out of range"));
10047           inst.instruction |= inst.reloc.exp.X_add_number << 4;
10048         }
10049       inst.reloc.type = BFD_RELOC_UNUSED;
10050     }
10051   else if (inst.operands[i].preind)
10052     {
10053       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
10054       constraint (is_t && inst.operands[i].writeback,
10055                   _("cannot use writeback with this instruction"));
10056       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
10057                   BAD_PC_ADDRESSING);
10058
10059       if (is_d)
10060         {
10061           inst.instruction |= 0x01000000;
10062           if (inst.operands[i].writeback)
10063             inst.instruction |= 0x00200000;
10064         }
10065       else
10066         {
10067           inst.instruction |= 0x00000c00;
10068           if (inst.operands[i].writeback)
10069             inst.instruction |= 0x00000100;
10070         }
10071       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10072     }
10073   else if (inst.operands[i].postind)
10074     {
10075       gas_assert (inst.operands[i].writeback);
10076       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
10077       constraint (is_t, _("cannot use post-indexing with this instruction"));
10078
10079       if (is_d)
10080         inst.instruction |= 0x00200000;
10081       else
10082         inst.instruction |= 0x00000900;
10083       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10084     }
10085   else /* unindexed - only for coprocessor */
10086     inst.error = _("instruction does not accept unindexed addressing");
10087 }
10088
10089 /* Table of Thumb instructions which exist in both 16- and 32-bit
10090    encodings (the latter only in post-V6T2 cores).  The index is the
10091    value used in the insns table below.  When there is more than one
10092    possible 16-bit encoding for the instruction, this table always
10093    holds variant (1).
10094    Also contains several pseudo-instructions used during relaxation.  */
10095 #define T16_32_TAB                              \
10096   X(_adc,   4140, eb400000),                    \
10097   X(_adcs,  4140, eb500000),                    \
10098   X(_add,   1c00, eb000000),                    \
10099   X(_adds,  1c00, eb100000),                    \
10100   X(_addi,  0000, f1000000),                    \
10101   X(_addis, 0000, f1100000),                    \
10102   X(_add_pc,000f, f20f0000),                    \
10103   X(_add_sp,000d, f10d0000),                    \
10104   X(_adr,   000f, f20f0000),                    \
10105   X(_and,   4000, ea000000),                    \
10106   X(_ands,  4000, ea100000),                    \
10107   X(_asr,   1000, fa40f000),                    \
10108   X(_asrs,  1000, fa50f000),                    \
10109   X(_b,     e000, f000b000),                    \
10110   X(_bcond, d000, f0008000),                    \
10111   X(_bic,   4380, ea200000),                    \
10112   X(_bics,  4380, ea300000),                    \
10113   X(_cmn,   42c0, eb100f00),                    \
10114   X(_cmp,   2800, ebb00f00),                    \
10115   X(_cpsie, b660, f3af8400),                    \
10116   X(_cpsid, b670, f3af8600),                    \
10117   X(_cpy,   4600, ea4f0000),                    \
10118   X(_dec_sp,80dd, f1ad0d00),                    \
10119   X(_eor,   4040, ea800000),                    \
10120   X(_eors,  4040, ea900000),                    \
10121   X(_inc_sp,00dd, f10d0d00),                    \
10122   X(_ldmia, c800, e8900000),                    \
10123   X(_ldr,   6800, f8500000),                    \
10124   X(_ldrb,  7800, f8100000),                    \
10125   X(_ldrh,  8800, f8300000),                    \
10126   X(_ldrsb, 5600, f9100000),                    \
10127   X(_ldrsh, 5e00, f9300000),                    \
10128   X(_ldr_pc,4800, f85f0000),                    \
10129   X(_ldr_pc2,4800, f85f0000),                   \
10130   X(_ldr_sp,9800, f85d0000),                    \
10131   X(_lsl,   0000, fa00f000),                    \
10132   X(_lsls,  0000, fa10f000),                    \
10133   X(_lsr,   0800, fa20f000),                    \
10134   X(_lsrs,  0800, fa30f000),                    \
10135   X(_mov,   2000, ea4f0000),                    \
10136   X(_movs,  2000, ea5f0000),                    \
10137   X(_mul,   4340, fb00f000),                     \
10138   X(_muls,  4340, ffffffff), /* no 32b muls */  \
10139   X(_mvn,   43c0, ea6f0000),                    \
10140   X(_mvns,  43c0, ea7f0000),                    \
10141   X(_neg,   4240, f1c00000), /* rsb #0 */       \
10142   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
10143   X(_orr,   4300, ea400000),                    \
10144   X(_orrs,  4300, ea500000),                    \
10145   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
10146   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
10147   X(_rev,   ba00, fa90f080),                    \
10148   X(_rev16, ba40, fa90f090),                    \
10149   X(_revsh, bac0, fa90f0b0),                    \
10150   X(_ror,   41c0, fa60f000),                    \
10151   X(_rors,  41c0, fa70f000),                    \
10152   X(_sbc,   4180, eb600000),                    \
10153   X(_sbcs,  4180, eb700000),                    \
10154   X(_stmia, c000, e8800000),                    \
10155   X(_str,   6000, f8400000),                    \
10156   X(_strb,  7000, f8000000),                    \
10157   X(_strh,  8000, f8200000),                    \
10158   X(_str_sp,9000, f84d0000),                    \
10159   X(_sub,   1e00, eba00000),                    \
10160   X(_subs,  1e00, ebb00000),                    \
10161   X(_subi,  8000, f1a00000),                    \
10162   X(_subis, 8000, f1b00000),                    \
10163   X(_sxtb,  b240, fa4ff080),                    \
10164   X(_sxth,  b200, fa0ff080),                    \
10165   X(_tst,   4200, ea100f00),                    \
10166   X(_uxtb,  b2c0, fa5ff080),                    \
10167   X(_uxth,  b280, fa1ff080),                    \
10168   X(_nop,   bf00, f3af8000),                    \
10169   X(_yield, bf10, f3af8001),                    \
10170   X(_wfe,   bf20, f3af8002),                    \
10171   X(_wfi,   bf30, f3af8003),                    \
10172   X(_sev,   bf40, f3af8004),                    \
10173   X(_sevl,  bf50, f3af8005),                    \
10174   X(_udf,   de00, f7f0a000)
10175
10176 /* To catch errors in encoding functions, the codes are all offset by
10177    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
10178    as 16-bit instructions.  */
10179 #define X(a,b,c) T_MNEM##a
10180 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
10181 #undef X
10182
10183 #define X(a,b,c) 0x##b
10184 static const unsigned short thumb_op16[] = { T16_32_TAB };
10185 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
10186 #undef X
10187
10188 #define X(a,b,c) 0x##c
10189 static const unsigned int thumb_op32[] = { T16_32_TAB };
10190 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
10191 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
10192 #undef X
10193 #undef T16_32_TAB
10194
10195 /* Thumb instruction encoders, in alphabetical order.  */
10196
10197 /* ADDW or SUBW.  */
10198
10199 static void
10200 do_t_add_sub_w (void)
10201 {
10202   int Rd, Rn;
10203
10204   Rd = inst.operands[0].reg;
10205   Rn = inst.operands[1].reg;
10206
10207   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
10208      is the SP-{plus,minus}-immediate form of the instruction.  */
10209   if (Rn == REG_SP)
10210     constraint (Rd == REG_PC, BAD_PC);
10211   else
10212     reject_bad_reg (Rd);
10213
10214   inst.instruction |= (Rn << 16) | (Rd << 8);
10215   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10216 }
10217
10218 /* Parse an add or subtract instruction.  We get here with inst.instruction
10219    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
10220
10221 static void
10222 do_t_add_sub (void)
10223 {
10224   int Rd, Rs, Rn;
10225
10226   Rd = inst.operands[0].reg;
10227   Rs = (inst.operands[1].present
10228         ? inst.operands[1].reg    /* Rd, Rs, foo */
10229         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10230
10231   if (Rd == REG_PC)
10232     set_it_insn_type_last ();
10233
10234   if (unified_syntax)
10235     {
10236       bfd_boolean flags;
10237       bfd_boolean narrow;
10238       int opcode;
10239
10240       flags = (inst.instruction == T_MNEM_adds
10241                || inst.instruction == T_MNEM_subs);
10242       if (flags)
10243         narrow = !in_it_block ();
10244       else
10245         narrow = in_it_block ();
10246       if (!inst.operands[2].isreg)
10247         {
10248           int add;
10249
10250           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10251
10252           add = (inst.instruction == T_MNEM_add
10253                  || inst.instruction == T_MNEM_adds);
10254           opcode = 0;
10255           if (inst.size_req != 4)
10256             {
10257               /* Attempt to use a narrow opcode, with relaxation if
10258                  appropriate.  */
10259               if (Rd == REG_SP && Rs == REG_SP && !flags)
10260                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
10261               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
10262                 opcode = T_MNEM_add_sp;
10263               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
10264                 opcode = T_MNEM_add_pc;
10265               else if (Rd <= 7 && Rs <= 7 && narrow)
10266                 {
10267                   if (flags)
10268                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
10269                   else
10270                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
10271                 }
10272               if (opcode)
10273                 {
10274                   inst.instruction = THUMB_OP16(opcode);
10275                   inst.instruction |= (Rd << 4) | Rs;
10276                   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10277                   if (inst.size_req != 2)
10278                     inst.relax = opcode;
10279                 }
10280               else
10281                 constraint (inst.size_req == 2, BAD_HIREG);
10282             }
10283           if (inst.size_req == 4
10284               || (inst.size_req != 2 && !opcode))
10285             {
10286               if (Rd == REG_PC)
10287                 {
10288                   constraint (add, BAD_PC);
10289                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
10290                              _("only SUBS PC, LR, #const allowed"));
10291                   constraint (inst.reloc.exp.X_op != O_constant,
10292                               _("expression too complex"));
10293                   constraint (inst.reloc.exp.X_add_number < 0
10294                               || inst.reloc.exp.X_add_number > 0xff,
10295                              _("immediate value out of range"));
10296                   inst.instruction = T2_SUBS_PC_LR
10297                                      | inst.reloc.exp.X_add_number;
10298                   inst.reloc.type = BFD_RELOC_UNUSED;
10299                   return;
10300                 }
10301               else if (Rs == REG_PC)
10302                 {
10303                   /* Always use addw/subw.  */
10304                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
10305                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10306                 }
10307               else
10308                 {
10309                   inst.instruction = THUMB_OP32 (inst.instruction);
10310                   inst.instruction = (inst.instruction & 0xe1ffffff)
10311                                      | 0x10000000;
10312                   if (flags)
10313                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10314                   else
10315                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
10316                 }
10317               inst.instruction |= Rd << 8;
10318               inst.instruction |= Rs << 16;
10319             }
10320         }
10321       else
10322         {
10323           unsigned int value = inst.reloc.exp.X_add_number;
10324           unsigned int shift = inst.operands[2].shift_kind;
10325
10326           Rn = inst.operands[2].reg;
10327           /* See if we can do this with a 16-bit instruction.  */
10328           if (!inst.operands[2].shifted && inst.size_req != 4)
10329             {
10330               if (Rd > 7 || Rs > 7 || Rn > 7)
10331                 narrow = FALSE;
10332
10333               if (narrow)
10334                 {
10335                   inst.instruction = ((inst.instruction == T_MNEM_adds
10336                                        || inst.instruction == T_MNEM_add)
10337                                       ? T_OPCODE_ADD_R3
10338                                       : T_OPCODE_SUB_R3);
10339                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10340                   return;
10341                 }
10342
10343               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
10344                 {
10345                   /* Thumb-1 cores (except v6-M) require at least one high
10346                      register in a narrow non flag setting add.  */
10347                   if (Rd > 7 || Rn > 7
10348                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
10349                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
10350                     {
10351                       if (Rd == Rn)
10352                         {
10353                           Rn = Rs;
10354                           Rs = Rd;
10355                         }
10356                       inst.instruction = T_OPCODE_ADD_HI;
10357                       inst.instruction |= (Rd & 8) << 4;
10358                       inst.instruction |= (Rd & 7);
10359                       inst.instruction |= Rn << 3;
10360                       return;
10361                     }
10362                 }
10363             }
10364
10365           constraint (Rd == REG_PC, BAD_PC);
10366           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10367           constraint (Rs == REG_PC, BAD_PC);
10368           reject_bad_reg (Rn);
10369
10370           /* If we get here, it can't be done in 16 bits.  */
10371           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
10372                       _("shift must be constant"));
10373           inst.instruction = THUMB_OP32 (inst.instruction);
10374           inst.instruction |= Rd << 8;
10375           inst.instruction |= Rs << 16;
10376           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
10377                       _("shift value over 3 not allowed in thumb mode"));
10378           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
10379                       _("only LSL shift allowed in thumb mode"));
10380           encode_thumb32_shifted_operand (2);
10381         }
10382     }
10383   else
10384     {
10385       constraint (inst.instruction == T_MNEM_adds
10386                   || inst.instruction == T_MNEM_subs,
10387                   BAD_THUMB32);
10388
10389       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
10390         {
10391           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
10392                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
10393                       BAD_HIREG);
10394
10395           inst.instruction = (inst.instruction == T_MNEM_add
10396                               ? 0x0000 : 0x8000);
10397           inst.instruction |= (Rd << 4) | Rs;
10398           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10399           return;
10400         }
10401
10402       Rn = inst.operands[2].reg;
10403       constraint (inst.operands[2].shifted, _("unshifted register required"));
10404
10405       /* We now have Rd, Rs, and Rn set to registers.  */
10406       if (Rd > 7 || Rs > 7 || Rn > 7)
10407         {
10408           /* Can't do this for SUB.      */
10409           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
10410           inst.instruction = T_OPCODE_ADD_HI;
10411           inst.instruction |= (Rd & 8) << 4;
10412           inst.instruction |= (Rd & 7);
10413           if (Rs == Rd)
10414             inst.instruction |= Rn << 3;
10415           else if (Rn == Rd)
10416             inst.instruction |= Rs << 3;
10417           else
10418             constraint (1, _("dest must overlap one source register"));
10419         }
10420       else
10421         {
10422           inst.instruction = (inst.instruction == T_MNEM_add
10423                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
10424           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10425         }
10426     }
10427 }
10428
10429 static void
10430 do_t_adr (void)
10431 {
10432   unsigned Rd;
10433
10434   Rd = inst.operands[0].reg;
10435   reject_bad_reg (Rd);
10436
10437   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
10438     {
10439       /* Defer to section relaxation.  */
10440       inst.relax = inst.instruction;
10441       inst.instruction = THUMB_OP16 (inst.instruction);
10442       inst.instruction |= Rd << 4;
10443     }
10444   else if (unified_syntax && inst.size_req != 2)
10445     {
10446       /* Generate a 32-bit opcode.  */
10447       inst.instruction = THUMB_OP32 (inst.instruction);
10448       inst.instruction |= Rd << 8;
10449       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
10450       inst.reloc.pc_rel = 1;
10451     }
10452   else
10453     {
10454       /* Generate a 16-bit opcode.  */
10455       inst.instruction = THUMB_OP16 (inst.instruction);
10456       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10457       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
10458       inst.reloc.pc_rel = 1;
10459
10460       inst.instruction |= Rd << 4;
10461     }
10462 }
10463
10464 /* Arithmetic instructions for which there is just one 16-bit
10465    instruction encoding, and it allows only two low registers.
10466    For maximal compatibility with ARM syntax, we allow three register
10467    operands even when Thumb-32 instructions are not available, as long
10468    as the first two are identical.  For instance, both "sbc r0,r1" and
10469    "sbc r0,r0,r1" are allowed.  */
10470 static void
10471 do_t_arit3 (void)
10472 {
10473   int Rd, Rs, Rn;
10474
10475   Rd = inst.operands[0].reg;
10476   Rs = (inst.operands[1].present
10477         ? inst.operands[1].reg    /* Rd, Rs, foo */
10478         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10479   Rn = inst.operands[2].reg;
10480
10481   reject_bad_reg (Rd);
10482   reject_bad_reg (Rs);
10483   if (inst.operands[2].isreg)
10484     reject_bad_reg (Rn);
10485
10486   if (unified_syntax)
10487     {
10488       if (!inst.operands[2].isreg)
10489         {
10490           /* For an immediate, we always generate a 32-bit opcode;
10491              section relaxation will shrink it later if possible.  */
10492           inst.instruction = THUMB_OP32 (inst.instruction);
10493           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10494           inst.instruction |= Rd << 8;
10495           inst.instruction |= Rs << 16;
10496           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10497         }
10498       else
10499         {
10500           bfd_boolean narrow;
10501
10502           /* See if we can do this with a 16-bit instruction.  */
10503           if (THUMB_SETS_FLAGS (inst.instruction))
10504             narrow = !in_it_block ();
10505           else
10506             narrow = in_it_block ();
10507
10508           if (Rd > 7 || Rn > 7 || Rs > 7)
10509             narrow = FALSE;
10510           if (inst.operands[2].shifted)
10511             narrow = FALSE;
10512           if (inst.size_req == 4)
10513             narrow = FALSE;
10514
10515           if (narrow
10516               && Rd == Rs)
10517             {
10518               inst.instruction = THUMB_OP16 (inst.instruction);
10519               inst.instruction |= Rd;
10520               inst.instruction |= Rn << 3;
10521               return;
10522             }
10523
10524           /* If we get here, it can't be done in 16 bits.  */
10525           constraint (inst.operands[2].shifted
10526                       && inst.operands[2].immisreg,
10527                       _("shift must be constant"));
10528           inst.instruction = THUMB_OP32 (inst.instruction);
10529           inst.instruction |= Rd << 8;
10530           inst.instruction |= Rs << 16;
10531           encode_thumb32_shifted_operand (2);
10532         }
10533     }
10534   else
10535     {
10536       /* On its face this is a lie - the instruction does set the
10537          flags.  However, the only supported mnemonic in this mode
10538          says it doesn't.  */
10539       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10540
10541       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10542                   _("unshifted register required"));
10543       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10544       constraint (Rd != Rs,
10545                   _("dest and source1 must be the same register"));
10546
10547       inst.instruction = THUMB_OP16 (inst.instruction);
10548       inst.instruction |= Rd;
10549       inst.instruction |= Rn << 3;
10550     }
10551 }
10552
10553 /* Similarly, but for instructions where the arithmetic operation is
10554    commutative, so we can allow either of them to be different from
10555    the destination operand in a 16-bit instruction.  For instance, all
10556    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
10557    accepted.  */
10558 static void
10559 do_t_arit3c (void)
10560 {
10561   int Rd, Rs, Rn;
10562
10563   Rd = inst.operands[0].reg;
10564   Rs = (inst.operands[1].present
10565         ? inst.operands[1].reg    /* Rd, Rs, foo */
10566         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10567   Rn = inst.operands[2].reg;
10568
10569   reject_bad_reg (Rd);
10570   reject_bad_reg (Rs);
10571   if (inst.operands[2].isreg)
10572     reject_bad_reg (Rn);
10573
10574   if (unified_syntax)
10575     {
10576       if (!inst.operands[2].isreg)
10577         {
10578           /* For an immediate, we always generate a 32-bit opcode;
10579              section relaxation will shrink it later if possible.  */
10580           inst.instruction = THUMB_OP32 (inst.instruction);
10581           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10582           inst.instruction |= Rd << 8;
10583           inst.instruction |= Rs << 16;
10584           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10585         }
10586       else
10587         {
10588           bfd_boolean narrow;
10589
10590           /* See if we can do this with a 16-bit instruction.  */
10591           if (THUMB_SETS_FLAGS (inst.instruction))
10592             narrow = !in_it_block ();
10593           else
10594             narrow = in_it_block ();
10595
10596           if (Rd > 7 || Rn > 7 || Rs > 7)
10597             narrow = FALSE;
10598           if (inst.operands[2].shifted)
10599             narrow = FALSE;
10600           if (inst.size_req == 4)
10601             narrow = FALSE;
10602
10603           if (narrow)
10604             {
10605               if (Rd == Rs)
10606                 {
10607                   inst.instruction = THUMB_OP16 (inst.instruction);
10608                   inst.instruction |= Rd;
10609                   inst.instruction |= Rn << 3;
10610                   return;
10611                 }
10612               if (Rd == Rn)
10613                 {
10614                   inst.instruction = THUMB_OP16 (inst.instruction);
10615                   inst.instruction |= Rd;
10616                   inst.instruction |= Rs << 3;
10617                   return;
10618                 }
10619             }
10620
10621           /* If we get here, it can't be done in 16 bits.  */
10622           constraint (inst.operands[2].shifted
10623                       && inst.operands[2].immisreg,
10624                       _("shift must be constant"));
10625           inst.instruction = THUMB_OP32 (inst.instruction);
10626           inst.instruction |= Rd << 8;
10627           inst.instruction |= Rs << 16;
10628           encode_thumb32_shifted_operand (2);
10629         }
10630     }
10631   else
10632     {
10633       /* On its face this is a lie - the instruction does set the
10634          flags.  However, the only supported mnemonic in this mode
10635          says it doesn't.  */
10636       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10637
10638       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10639                   _("unshifted register required"));
10640       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10641
10642       inst.instruction = THUMB_OP16 (inst.instruction);
10643       inst.instruction |= Rd;
10644
10645       if (Rd == Rs)
10646         inst.instruction |= Rn << 3;
10647       else if (Rd == Rn)
10648         inst.instruction |= Rs << 3;
10649       else
10650         constraint (1, _("dest must overlap one source register"));
10651     }
10652 }
10653
10654 static void
10655 do_t_bfc (void)
10656 {
10657   unsigned Rd;
10658   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
10659   constraint (msb > 32, _("bit-field extends past end of register"));
10660   /* The instruction encoding stores the LSB and MSB,
10661      not the LSB and width.  */
10662   Rd = inst.operands[0].reg;
10663   reject_bad_reg (Rd);
10664   inst.instruction |= Rd << 8;
10665   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
10666   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
10667   inst.instruction |= msb - 1;
10668 }
10669
10670 static void
10671 do_t_bfi (void)
10672 {
10673   int Rd, Rn;
10674   unsigned int msb;
10675
10676   Rd = inst.operands[0].reg;
10677   reject_bad_reg (Rd);
10678
10679   /* #0 in second position is alternative syntax for bfc, which is
10680      the same instruction but with REG_PC in the Rm field.  */
10681   if (!inst.operands[1].isreg)
10682     Rn = REG_PC;
10683   else
10684     {
10685       Rn = inst.operands[1].reg;
10686       reject_bad_reg (Rn);
10687     }
10688
10689   msb = inst.operands[2].imm + inst.operands[3].imm;
10690   constraint (msb > 32, _("bit-field extends past end of register"));
10691   /* The instruction encoding stores the LSB and MSB,
10692      not the LSB and width.  */
10693   inst.instruction |= Rd << 8;
10694   inst.instruction |= Rn << 16;
10695   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10696   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10697   inst.instruction |= msb - 1;
10698 }
10699
10700 static void
10701 do_t_bfx (void)
10702 {
10703   unsigned Rd, Rn;
10704
10705   Rd = inst.operands[0].reg;
10706   Rn = inst.operands[1].reg;
10707
10708   reject_bad_reg (Rd);
10709   reject_bad_reg (Rn);
10710
10711   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
10712               _("bit-field extends past end of register"));
10713   inst.instruction |= Rd << 8;
10714   inst.instruction |= Rn << 16;
10715   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10716   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10717   inst.instruction |= inst.operands[3].imm - 1;
10718 }
10719
10720 /* ARM V5 Thumb BLX (argument parse)
10721         BLX <target_addr>       which is BLX(1)
10722         BLX <Rm>                which is BLX(2)
10723    Unfortunately, there are two different opcodes for this mnemonic.
10724    So, the insns[].value is not used, and the code here zaps values
10725         into inst.instruction.
10726
10727    ??? How to take advantage of the additional two bits of displacement
10728    available in Thumb32 mode?  Need new relocation?  */
10729
10730 static void
10731 do_t_blx (void)
10732 {
10733   set_it_insn_type_last ();
10734
10735   if (inst.operands[0].isreg)
10736     {
10737       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
10738       /* We have a register, so this is BLX(2).  */
10739       inst.instruction |= inst.operands[0].reg << 3;
10740     }
10741   else
10742     {
10743       /* No register.  This must be BLX(1).  */
10744       inst.instruction = 0xf000e800;
10745       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
10746     }
10747 }
10748
10749 static void
10750 do_t_branch (void)
10751 {
10752   int opcode;
10753   int cond;
10754   int reloc;
10755
10756   cond = inst.cond;
10757   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
10758
10759   if (in_it_block ())
10760     {
10761       /* Conditional branches inside IT blocks are encoded as unconditional
10762          branches.  */
10763       cond = COND_ALWAYS;
10764     }
10765   else
10766     cond = inst.cond;
10767
10768   if (cond != COND_ALWAYS)
10769     opcode = T_MNEM_bcond;
10770   else
10771     opcode = inst.instruction;
10772
10773   if (unified_syntax
10774       && (inst.size_req == 4
10775           || (inst.size_req != 2
10776               && (inst.operands[0].hasreloc
10777                   || inst.reloc.exp.X_op == O_constant))))
10778     {
10779       inst.instruction = THUMB_OP32(opcode);
10780       if (cond == COND_ALWAYS)
10781         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
10782       else
10783         {
10784           gas_assert (cond != 0xF);
10785           inst.instruction |= cond << 22;
10786           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
10787         }
10788     }
10789   else
10790     {
10791       inst.instruction = THUMB_OP16(opcode);
10792       if (cond == COND_ALWAYS)
10793         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
10794       else
10795         {
10796           inst.instruction |= cond << 8;
10797           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
10798         }
10799       /* Allow section relaxation.  */
10800       if (unified_syntax && inst.size_req != 2)
10801         inst.relax = opcode;
10802     }
10803   inst.reloc.type = reloc;
10804   inst.reloc.pc_rel = 1;
10805 }
10806
10807 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
10808    between the two is the maximum immediate allowed - which is passed in
10809    RANGE.  */
10810 static void
10811 do_t_bkpt_hlt1 (int range)
10812 {
10813   constraint (inst.cond != COND_ALWAYS,
10814               _("instruction is always unconditional"));
10815   if (inst.operands[0].present)
10816     {
10817       constraint (inst.operands[0].imm > range,
10818                   _("immediate value out of range"));
10819       inst.instruction |= inst.operands[0].imm;
10820     }
10821
10822   set_it_insn_type (NEUTRAL_IT_INSN);
10823 }
10824
10825 static void
10826 do_t_hlt (void)
10827 {
10828   do_t_bkpt_hlt1 (63);
10829 }
10830
10831 static void
10832 do_t_bkpt (void)
10833 {
10834   do_t_bkpt_hlt1 (255);
10835 }
10836
10837 static void
10838 do_t_branch23 (void)
10839 {
10840   set_it_insn_type_last ();
10841   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
10842
10843   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
10844      this file.  We used to simply ignore the PLT reloc type here --
10845      the branch encoding is now needed to deal with TLSCALL relocs.
10846      So if we see a PLT reloc now, put it back to how it used to be to
10847      keep the preexisting behaviour.  */
10848   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
10849     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
10850
10851 #if defined(OBJ_COFF)
10852   /* If the destination of the branch is a defined symbol which does not have
10853      the THUMB_FUNC attribute, then we must be calling a function which has
10854      the (interfacearm) attribute.  We look for the Thumb entry point to that
10855      function and change the branch to refer to that function instead.  */
10856   if (   inst.reloc.exp.X_op == O_symbol
10857       && inst.reloc.exp.X_add_symbol != NULL
10858       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
10859       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
10860     inst.reloc.exp.X_add_symbol =
10861       find_real_start (inst.reloc.exp.X_add_symbol);
10862 #endif
10863 }
10864
10865 static void
10866 do_t_bx (void)
10867 {
10868   set_it_insn_type_last ();
10869   inst.instruction |= inst.operands[0].reg << 3;
10870   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
10871      should cause the alignment to be checked once it is known.  This is
10872      because BX PC only works if the instruction is word aligned.  */
10873 }
10874
10875 static void
10876 do_t_bxj (void)
10877 {
10878   int Rm;
10879
10880   set_it_insn_type_last ();
10881   Rm = inst.operands[0].reg;
10882   reject_bad_reg (Rm);
10883   inst.instruction |= Rm << 16;
10884 }
10885
10886 static void
10887 do_t_clz (void)
10888 {
10889   unsigned Rd;
10890   unsigned Rm;
10891
10892   Rd = inst.operands[0].reg;
10893   Rm = inst.operands[1].reg;
10894
10895   reject_bad_reg (Rd);
10896   reject_bad_reg (Rm);
10897
10898   inst.instruction |= Rd << 8;
10899   inst.instruction |= Rm << 16;
10900   inst.instruction |= Rm;
10901 }
10902
10903 static void
10904 do_t_cps (void)
10905 {
10906   set_it_insn_type (OUTSIDE_IT_INSN);
10907   inst.instruction |= inst.operands[0].imm;
10908 }
10909
10910 static void
10911 do_t_cpsi (void)
10912 {
10913   set_it_insn_type (OUTSIDE_IT_INSN);
10914   if (unified_syntax
10915       && (inst.operands[1].present || inst.size_req == 4)
10916       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
10917     {
10918       unsigned int imod = (inst.instruction & 0x0030) >> 4;
10919       inst.instruction = 0xf3af8000;
10920       inst.instruction |= imod << 9;
10921       inst.instruction |= inst.operands[0].imm << 5;
10922       if (inst.operands[1].present)
10923         inst.instruction |= 0x100 | inst.operands[1].imm;
10924     }
10925   else
10926     {
10927       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
10928                   && (inst.operands[0].imm & 4),
10929                   _("selected processor does not support 'A' form "
10930                     "of this instruction"));
10931       constraint (inst.operands[1].present || inst.size_req == 4,
10932                   _("Thumb does not support the 2-argument "
10933                     "form of this instruction"));
10934       inst.instruction |= inst.operands[0].imm;
10935     }
10936 }
10937
10938 /* THUMB CPY instruction (argument parse).  */
10939
10940 static void
10941 do_t_cpy (void)
10942 {
10943   if (inst.size_req == 4)
10944     {
10945       inst.instruction = THUMB_OP32 (T_MNEM_mov);
10946       inst.instruction |= inst.operands[0].reg << 8;
10947       inst.instruction |= inst.operands[1].reg;
10948     }
10949   else
10950     {
10951       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
10952       inst.instruction |= (inst.operands[0].reg & 0x7);
10953       inst.instruction |= inst.operands[1].reg << 3;
10954     }
10955 }
10956
10957 static void
10958 do_t_cbz (void)
10959 {
10960   set_it_insn_type (OUTSIDE_IT_INSN);
10961   constraint (inst.operands[0].reg > 7, BAD_HIREG);
10962   inst.instruction |= inst.operands[0].reg;
10963   inst.reloc.pc_rel = 1;
10964   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
10965 }
10966
10967 static void
10968 do_t_dbg (void)
10969 {
10970   inst.instruction |= inst.operands[0].imm;
10971 }
10972
10973 static void
10974 do_t_div (void)
10975 {
10976   unsigned Rd, Rn, Rm;
10977
10978   Rd = inst.operands[0].reg;
10979   Rn = (inst.operands[1].present
10980         ? inst.operands[1].reg : Rd);
10981   Rm = inst.operands[2].reg;
10982
10983   reject_bad_reg (Rd);
10984   reject_bad_reg (Rn);
10985   reject_bad_reg (Rm);
10986
10987   inst.instruction |= Rd << 8;
10988   inst.instruction |= Rn << 16;
10989   inst.instruction |= Rm;
10990 }
10991
10992 static void
10993 do_t_hint (void)
10994 {
10995   if (unified_syntax && inst.size_req == 4)
10996     inst.instruction = THUMB_OP32 (inst.instruction);
10997   else
10998     inst.instruction = THUMB_OP16 (inst.instruction);
10999 }
11000
11001 static void
11002 do_t_it (void)
11003 {
11004   unsigned int cond = inst.operands[0].imm;
11005
11006   set_it_insn_type (IT_INSN);
11007   now_it.mask = (inst.instruction & 0xf) | 0x10;
11008   now_it.cc = cond;
11009   now_it.warn_deprecated = FALSE;
11010
11011   /* If the condition is a negative condition, invert the mask.  */
11012   if ((cond & 0x1) == 0x0)
11013     {
11014       unsigned int mask = inst.instruction & 0x000f;
11015
11016       if ((mask & 0x7) == 0)
11017         {
11018           /* No conversion needed.  */
11019           now_it.block_length = 1;
11020         }
11021       else if ((mask & 0x3) == 0)
11022         {
11023           mask ^= 0x8;
11024           now_it.block_length = 2;
11025         }
11026       else if ((mask & 0x1) == 0)
11027         {
11028           mask ^= 0xC;
11029           now_it.block_length = 3;
11030         }
11031       else
11032         {
11033           mask ^= 0xE;
11034           now_it.block_length = 4;
11035         }
11036
11037       inst.instruction &= 0xfff0;
11038       inst.instruction |= mask;
11039     }
11040
11041   inst.instruction |= cond << 4;
11042 }
11043
11044 /* Helper function used for both push/pop and ldm/stm.  */
11045 static void
11046 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
11047 {
11048   bfd_boolean load;
11049
11050   load = (inst.instruction & (1 << 20)) != 0;
11051
11052   if (mask & (1 << 13))
11053     inst.error =  _("SP not allowed in register list");
11054
11055   if ((mask & (1 << base)) != 0
11056       && writeback)
11057     inst.error = _("having the base register in the register list when "
11058                    "using write back is UNPREDICTABLE");
11059
11060   if (load)
11061     {
11062       if (mask & (1 << 15))
11063         {
11064           if (mask & (1 << 14))
11065             inst.error = _("LR and PC should not both be in register list");
11066           else
11067             set_it_insn_type_last ();
11068         }
11069     }
11070   else
11071     {
11072       if (mask & (1 << 15))
11073         inst.error = _("PC not allowed in register list");
11074     }
11075
11076   if ((mask & (mask - 1)) == 0)
11077     {
11078       /* Single register transfers implemented as str/ldr.  */
11079       if (writeback)
11080         {
11081           if (inst.instruction & (1 << 23))
11082             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
11083           else
11084             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
11085         }
11086       else
11087         {
11088           if (inst.instruction & (1 << 23))
11089             inst.instruction = 0x00800000; /* ia -> [base] */
11090           else
11091             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
11092         }
11093
11094       inst.instruction |= 0xf8400000;
11095       if (load)
11096         inst.instruction |= 0x00100000;
11097
11098       mask = ffs (mask) - 1;
11099       mask <<= 12;
11100     }
11101   else if (writeback)
11102     inst.instruction |= WRITE_BACK;
11103
11104   inst.instruction |= mask;
11105   inst.instruction |= base << 16;
11106 }
11107
11108 static void
11109 do_t_ldmstm (void)
11110 {
11111   /* This really doesn't seem worth it.  */
11112   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11113               _("expression too complex"));
11114   constraint (inst.operands[1].writeback,
11115               _("Thumb load/store multiple does not support {reglist}^"));
11116
11117   if (unified_syntax)
11118     {
11119       bfd_boolean narrow;
11120       unsigned mask;
11121
11122       narrow = FALSE;
11123       /* See if we can use a 16-bit instruction.  */
11124       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
11125           && inst.size_req != 4
11126           && !(inst.operands[1].imm & ~0xff))
11127         {
11128           mask = 1 << inst.operands[0].reg;
11129
11130           if (inst.operands[0].reg <= 7)
11131             {
11132               if (inst.instruction == T_MNEM_stmia
11133                   ? inst.operands[0].writeback
11134                   : (inst.operands[0].writeback
11135                      == !(inst.operands[1].imm & mask)))
11136                 {
11137                   if (inst.instruction == T_MNEM_stmia
11138                       && (inst.operands[1].imm & mask)
11139                       && (inst.operands[1].imm & (mask - 1)))
11140                     as_warn (_("value stored for r%d is UNKNOWN"),
11141                              inst.operands[0].reg);
11142
11143                   inst.instruction = THUMB_OP16 (inst.instruction);
11144                   inst.instruction |= inst.operands[0].reg << 8;
11145                   inst.instruction |= inst.operands[1].imm;
11146                   narrow = TRUE;
11147                 }
11148               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11149                 {
11150                   /* This means 1 register in reg list one of 3 situations:
11151                      1. Instruction is stmia, but without writeback.
11152                      2. lmdia without writeback, but with Rn not in
11153                         reglist.
11154                      3. ldmia with writeback, but with Rn in reglist.
11155                      Case 3 is UNPREDICTABLE behaviour, so we handle
11156                      case 1 and 2 which can be converted into a 16-bit
11157                      str or ldr. The SP cases are handled below.  */
11158                   unsigned long opcode;
11159                   /* First, record an error for Case 3.  */
11160                   if (inst.operands[1].imm & mask
11161                       && inst.operands[0].writeback)
11162                     inst.error =
11163                         _("having the base register in the register list when "
11164                           "using write back is UNPREDICTABLE");
11165
11166                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
11167                                                              : T_MNEM_ldr);
11168                   inst.instruction = THUMB_OP16 (opcode);
11169                   inst.instruction |= inst.operands[0].reg << 3;
11170                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
11171                   narrow = TRUE;
11172                 }
11173             }
11174           else if (inst.operands[0] .reg == REG_SP)
11175             {
11176               if (inst.operands[0].writeback)
11177                 {
11178                   inst.instruction =
11179                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11180                                     ? T_MNEM_push : T_MNEM_pop);
11181                   inst.instruction |= inst.operands[1].imm;
11182                   narrow = TRUE;
11183                 }
11184               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11185                 {
11186                   inst.instruction =
11187                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11188                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
11189                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
11190                   narrow = TRUE;
11191                 }
11192             }
11193         }
11194
11195       if (!narrow)
11196         {
11197           if (inst.instruction < 0xffff)
11198             inst.instruction = THUMB_OP32 (inst.instruction);
11199
11200           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
11201                                 inst.operands[0].writeback);
11202         }
11203     }
11204   else
11205     {
11206       constraint (inst.operands[0].reg > 7
11207                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
11208       constraint (inst.instruction != T_MNEM_ldmia
11209                   && inst.instruction != T_MNEM_stmia,
11210                   _("Thumb-2 instruction only valid in unified syntax"));
11211       if (inst.instruction == T_MNEM_stmia)
11212         {
11213           if (!inst.operands[0].writeback)
11214             as_warn (_("this instruction will write back the base register"));
11215           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
11216               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
11217             as_warn (_("value stored for r%d is UNKNOWN"),
11218                      inst.operands[0].reg);
11219         }
11220       else
11221         {
11222           if (!inst.operands[0].writeback
11223               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
11224             as_warn (_("this instruction will write back the base register"));
11225           else if (inst.operands[0].writeback
11226                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
11227             as_warn (_("this instruction will not write back the base register"));
11228         }
11229
11230       inst.instruction = THUMB_OP16 (inst.instruction);
11231       inst.instruction |= inst.operands[0].reg << 8;
11232       inst.instruction |= inst.operands[1].imm;
11233     }
11234 }
11235
11236 static void
11237 do_t_ldrex (void)
11238 {
11239   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
11240               || inst.operands[1].postind || inst.operands[1].writeback
11241               || inst.operands[1].immisreg || inst.operands[1].shifted
11242               || inst.operands[1].negative,
11243               BAD_ADDR_MODE);
11244
11245   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
11246
11247   inst.instruction |= inst.operands[0].reg << 12;
11248   inst.instruction |= inst.operands[1].reg << 16;
11249   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
11250 }
11251
11252 static void
11253 do_t_ldrexd (void)
11254 {
11255   if (!inst.operands[1].present)
11256     {
11257       constraint (inst.operands[0].reg == REG_LR,
11258                   _("r14 not allowed as first register "
11259                     "when second register is omitted"));
11260       inst.operands[1].reg = inst.operands[0].reg + 1;
11261     }
11262   constraint (inst.operands[0].reg == inst.operands[1].reg,
11263               BAD_OVERLAP);
11264
11265   inst.instruction |= inst.operands[0].reg << 12;
11266   inst.instruction |= inst.operands[1].reg << 8;
11267   inst.instruction |= inst.operands[2].reg << 16;
11268 }
11269
11270 static void
11271 do_t_ldst (void)
11272 {
11273   unsigned long opcode;
11274   int Rn;
11275
11276   if (inst.operands[0].isreg
11277       && !inst.operands[0].preind
11278       && inst.operands[0].reg == REG_PC)
11279     set_it_insn_type_last ();
11280
11281   opcode = inst.instruction;
11282   if (unified_syntax)
11283     {
11284       if (!inst.operands[1].isreg)
11285         {
11286           if (opcode <= 0xffff)
11287             inst.instruction = THUMB_OP32 (opcode);
11288           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11289             return;
11290         }
11291       if (inst.operands[1].isreg
11292           && !inst.operands[1].writeback
11293           && !inst.operands[1].shifted && !inst.operands[1].postind
11294           && !inst.operands[1].negative && inst.operands[0].reg <= 7
11295           && opcode <= 0xffff
11296           && inst.size_req != 4)
11297         {
11298           /* Insn may have a 16-bit form.  */
11299           Rn = inst.operands[1].reg;
11300           if (inst.operands[1].immisreg)
11301             {
11302               inst.instruction = THUMB_OP16 (opcode);
11303               /* [Rn, Rik] */
11304               if (Rn <= 7 && inst.operands[1].imm <= 7)
11305                 goto op16;
11306               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
11307                 reject_bad_reg (inst.operands[1].imm);
11308             }
11309           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
11310                     && opcode != T_MNEM_ldrsb)
11311                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
11312                    || (Rn == REG_SP && opcode == T_MNEM_str))
11313             {
11314               /* [Rn, #const] */
11315               if (Rn > 7)
11316                 {
11317                   if (Rn == REG_PC)
11318                     {
11319                       if (inst.reloc.pc_rel)
11320                         opcode = T_MNEM_ldr_pc2;
11321                       else
11322                         opcode = T_MNEM_ldr_pc;
11323                     }
11324                   else
11325                     {
11326                       if (opcode == T_MNEM_ldr)
11327                         opcode = T_MNEM_ldr_sp;
11328                       else
11329                         opcode = T_MNEM_str_sp;
11330                     }
11331                   inst.instruction = inst.operands[0].reg << 8;
11332                 }
11333               else
11334                 {
11335                   inst.instruction = inst.operands[0].reg;
11336                   inst.instruction |= inst.operands[1].reg << 3;
11337                 }
11338               inst.instruction |= THUMB_OP16 (opcode);
11339               if (inst.size_req == 2)
11340                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11341               else
11342                 inst.relax = opcode;
11343               return;
11344             }
11345         }
11346       /* Definitely a 32-bit variant.  */
11347
11348       /* Warning for Erratum 752419.  */
11349       if (opcode == T_MNEM_ldr
11350           && inst.operands[0].reg == REG_SP
11351           && inst.operands[1].writeback == 1
11352           && !inst.operands[1].immisreg)
11353         {
11354           if (no_cpu_selected ()
11355               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
11356                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
11357                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
11358             as_warn (_("This instruction may be unpredictable "
11359                        "if executed on M-profile cores "
11360                        "with interrupts enabled."));
11361         }
11362
11363       /* Do some validations regarding addressing modes.  */
11364       if (inst.operands[1].immisreg)
11365         reject_bad_reg (inst.operands[1].imm);
11366
11367       constraint (inst.operands[1].writeback == 1
11368                   && inst.operands[0].reg == inst.operands[1].reg,
11369                   BAD_OVERLAP);
11370
11371       inst.instruction = THUMB_OP32 (opcode);
11372       inst.instruction |= inst.operands[0].reg << 12;
11373       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
11374       check_ldr_r15_aligned ();
11375       return;
11376     }
11377
11378   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11379
11380   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
11381     {
11382       /* Only [Rn,Rm] is acceptable.  */
11383       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
11384       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
11385                   || inst.operands[1].postind || inst.operands[1].shifted
11386                   || inst.operands[1].negative,
11387                   _("Thumb does not support this addressing mode"));
11388       inst.instruction = THUMB_OP16 (inst.instruction);
11389       goto op16;
11390     }
11391
11392   inst.instruction = THUMB_OP16 (inst.instruction);
11393   if (!inst.operands[1].isreg)
11394     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11395       return;
11396
11397   constraint (!inst.operands[1].preind
11398               || inst.operands[1].shifted
11399               || inst.operands[1].writeback,
11400               _("Thumb does not support this addressing mode"));
11401   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
11402     {
11403       constraint (inst.instruction & 0x0600,
11404                   _("byte or halfword not valid for base register"));
11405       constraint (inst.operands[1].reg == REG_PC
11406                   && !(inst.instruction & THUMB_LOAD_BIT),
11407                   _("r15 based store not allowed"));
11408       constraint (inst.operands[1].immisreg,
11409                   _("invalid base register for register offset"));
11410
11411       if (inst.operands[1].reg == REG_PC)
11412         inst.instruction = T_OPCODE_LDR_PC;
11413       else if (inst.instruction & THUMB_LOAD_BIT)
11414         inst.instruction = T_OPCODE_LDR_SP;
11415       else
11416         inst.instruction = T_OPCODE_STR_SP;
11417
11418       inst.instruction |= inst.operands[0].reg << 8;
11419       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11420       return;
11421     }
11422
11423   constraint (inst.operands[1].reg > 7, BAD_HIREG);
11424   if (!inst.operands[1].immisreg)
11425     {
11426       /* Immediate offset.  */
11427       inst.instruction |= inst.operands[0].reg;
11428       inst.instruction |= inst.operands[1].reg << 3;
11429       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11430       return;
11431     }
11432
11433   /* Register offset.  */
11434   constraint (inst.operands[1].imm > 7, BAD_HIREG);
11435   constraint (inst.operands[1].negative,
11436               _("Thumb does not support this addressing mode"));
11437
11438  op16:
11439   switch (inst.instruction)
11440     {
11441     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
11442     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
11443     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
11444     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
11445     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
11446     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
11447     case 0x5600 /* ldrsb */:
11448     case 0x5e00 /* ldrsh */: break;
11449     default: abort ();
11450     }
11451
11452   inst.instruction |= inst.operands[0].reg;
11453   inst.instruction |= inst.operands[1].reg << 3;
11454   inst.instruction |= inst.operands[1].imm << 6;
11455 }
11456
11457 static void
11458 do_t_ldstd (void)
11459 {
11460   if (!inst.operands[1].present)
11461     {
11462       inst.operands[1].reg = inst.operands[0].reg + 1;
11463       constraint (inst.operands[0].reg == REG_LR,
11464                   _("r14 not allowed here"));
11465       constraint (inst.operands[0].reg == REG_R12,
11466                   _("r12 not allowed here"));
11467     }
11468
11469   if (inst.operands[2].writeback
11470       && (inst.operands[0].reg == inst.operands[2].reg
11471       || inst.operands[1].reg == inst.operands[2].reg))
11472     as_warn (_("base register written back, and overlaps "
11473                "one of transfer registers"));
11474
11475   inst.instruction |= inst.operands[0].reg << 12;
11476   inst.instruction |= inst.operands[1].reg << 8;
11477   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
11478 }
11479
11480 static void
11481 do_t_ldstt (void)
11482 {
11483   inst.instruction |= inst.operands[0].reg << 12;
11484   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
11485 }
11486
11487 static void
11488 do_t_mla (void)
11489 {
11490   unsigned Rd, Rn, Rm, Ra;
11491
11492   Rd = inst.operands[0].reg;
11493   Rn = inst.operands[1].reg;
11494   Rm = inst.operands[2].reg;
11495   Ra = inst.operands[3].reg;
11496
11497   reject_bad_reg (Rd);
11498   reject_bad_reg (Rn);
11499   reject_bad_reg (Rm);
11500   reject_bad_reg (Ra);
11501
11502   inst.instruction |= Rd << 8;
11503   inst.instruction |= Rn << 16;
11504   inst.instruction |= Rm;
11505   inst.instruction |= Ra << 12;
11506 }
11507
11508 static void
11509 do_t_mlal (void)
11510 {
11511   unsigned RdLo, RdHi, Rn, Rm;
11512
11513   RdLo = inst.operands[0].reg;
11514   RdHi = inst.operands[1].reg;
11515   Rn = inst.operands[2].reg;
11516   Rm = inst.operands[3].reg;
11517
11518   reject_bad_reg (RdLo);
11519   reject_bad_reg (RdHi);
11520   reject_bad_reg (Rn);
11521   reject_bad_reg (Rm);
11522
11523   inst.instruction |= RdLo << 12;
11524   inst.instruction |= RdHi << 8;
11525   inst.instruction |= Rn << 16;
11526   inst.instruction |= Rm;
11527 }
11528
11529 static void
11530 do_t_mov_cmp (void)
11531 {
11532   unsigned Rn, Rm;
11533
11534   Rn = inst.operands[0].reg;
11535   Rm = inst.operands[1].reg;
11536
11537   if (Rn == REG_PC)
11538     set_it_insn_type_last ();
11539
11540   if (unified_syntax)
11541     {
11542       int r0off = (inst.instruction == T_MNEM_mov
11543                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
11544       unsigned long opcode;
11545       bfd_boolean narrow;
11546       bfd_boolean low_regs;
11547
11548       low_regs = (Rn <= 7 && Rm <= 7);
11549       opcode = inst.instruction;
11550       if (in_it_block ())
11551         narrow = opcode != T_MNEM_movs;
11552       else
11553         narrow = opcode != T_MNEM_movs || low_regs;
11554       if (inst.size_req == 4
11555           || inst.operands[1].shifted)
11556         narrow = FALSE;
11557
11558       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
11559       if (opcode == T_MNEM_movs && inst.operands[1].isreg
11560           && !inst.operands[1].shifted
11561           && Rn == REG_PC
11562           && Rm == REG_LR)
11563         {
11564           inst.instruction = T2_SUBS_PC_LR;
11565           return;
11566         }
11567
11568       if (opcode == T_MNEM_cmp)
11569         {
11570           constraint (Rn == REG_PC, BAD_PC);
11571           if (narrow)
11572             {
11573               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
11574                  but valid.  */
11575               warn_deprecated_sp (Rm);
11576               /* R15 was documented as a valid choice for Rm in ARMv6,
11577                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
11578                  tools reject R15, so we do too.  */
11579               constraint (Rm == REG_PC, BAD_PC);
11580             }
11581           else
11582             reject_bad_reg (Rm);
11583         }
11584       else if (opcode == T_MNEM_mov
11585                || opcode == T_MNEM_movs)
11586         {
11587           if (inst.operands[1].isreg)
11588             {
11589               if (opcode == T_MNEM_movs)
11590                 {
11591                   reject_bad_reg (Rn);
11592                   reject_bad_reg (Rm);
11593                 }
11594               else if (narrow)
11595                 {
11596                   /* This is mov.n.  */
11597                   if ((Rn == REG_SP || Rn == REG_PC)
11598                       && (Rm == REG_SP || Rm == REG_PC))
11599                     {
11600                       as_tsktsk (_("Use of r%u as a source register is "
11601                                  "deprecated when r%u is the destination "
11602                                  "register."), Rm, Rn);
11603                     }
11604                 }
11605               else
11606                 {
11607                   /* This is mov.w.  */
11608                   constraint (Rn == REG_PC, BAD_PC);
11609                   constraint (Rm == REG_PC, BAD_PC);
11610                   constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
11611                 }
11612             }
11613           else
11614             reject_bad_reg (Rn);
11615         }
11616
11617       if (!inst.operands[1].isreg)
11618         {
11619           /* Immediate operand.  */
11620           if (!in_it_block () && opcode == T_MNEM_mov)
11621             narrow = 0;
11622           if (low_regs && narrow)
11623             {
11624               inst.instruction = THUMB_OP16 (opcode);
11625               inst.instruction |= Rn << 8;
11626               if (inst.size_req == 2)
11627                 inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
11628               else
11629                 inst.relax = opcode;
11630             }
11631           else
11632             {
11633               inst.instruction = THUMB_OP32 (inst.instruction);
11634               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11635               inst.instruction |= Rn << r0off;
11636               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11637             }
11638         }
11639       else if (inst.operands[1].shifted && inst.operands[1].immisreg
11640                && (inst.instruction == T_MNEM_mov
11641                    || inst.instruction == T_MNEM_movs))
11642         {
11643           /* Register shifts are encoded as separate shift instructions.  */
11644           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
11645
11646           if (in_it_block ())
11647             narrow = !flags;
11648           else
11649             narrow = flags;
11650
11651           if (inst.size_req == 4)
11652             narrow = FALSE;
11653
11654           if (!low_regs || inst.operands[1].imm > 7)
11655             narrow = FALSE;
11656
11657           if (Rn != Rm)
11658             narrow = FALSE;
11659
11660           switch (inst.operands[1].shift_kind)
11661             {
11662             case SHIFT_LSL:
11663               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
11664               break;
11665             case SHIFT_ASR:
11666               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
11667               break;
11668             case SHIFT_LSR:
11669               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
11670               break;
11671             case SHIFT_ROR:
11672               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
11673               break;
11674             default:
11675               abort ();
11676             }
11677
11678           inst.instruction = opcode;
11679           if (narrow)
11680             {
11681               inst.instruction |= Rn;
11682               inst.instruction |= inst.operands[1].imm << 3;
11683             }
11684           else
11685             {
11686               if (flags)
11687                 inst.instruction |= CONDS_BIT;
11688
11689               inst.instruction |= Rn << 8;
11690               inst.instruction |= Rm << 16;
11691               inst.instruction |= inst.operands[1].imm;
11692             }
11693         }
11694       else if (!narrow)
11695         {
11696           /* Some mov with immediate shift have narrow variants.
11697              Register shifts are handled above.  */
11698           if (low_regs && inst.operands[1].shifted
11699               && (inst.instruction == T_MNEM_mov
11700                   || inst.instruction == T_MNEM_movs))
11701             {
11702               if (in_it_block ())
11703                 narrow = (inst.instruction == T_MNEM_mov);
11704               else
11705                 narrow = (inst.instruction == T_MNEM_movs);
11706             }
11707
11708           if (narrow)
11709             {
11710               switch (inst.operands[1].shift_kind)
11711                 {
11712                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
11713                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
11714                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
11715                 default: narrow = FALSE; break;
11716                 }
11717             }
11718
11719           if (narrow)
11720             {
11721               inst.instruction |= Rn;
11722               inst.instruction |= Rm << 3;
11723               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11724             }
11725           else
11726             {
11727               inst.instruction = THUMB_OP32 (inst.instruction);
11728               inst.instruction |= Rn << r0off;
11729               encode_thumb32_shifted_operand (1);
11730             }
11731         }
11732       else
11733         switch (inst.instruction)
11734           {
11735           case T_MNEM_mov:
11736             /* In v4t or v5t a move of two lowregs produces unpredictable
11737                results. Don't allow this.  */
11738             if (low_regs)
11739               {
11740                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
11741                             "MOV Rd, Rs with two low registers is not "
11742                             "permitted on this architecture");
11743                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
11744                                         arm_ext_v6);
11745               }
11746
11747             inst.instruction = T_OPCODE_MOV_HR;
11748             inst.instruction |= (Rn & 0x8) << 4;
11749             inst.instruction |= (Rn & 0x7);
11750             inst.instruction |= Rm << 3;
11751             break;
11752
11753           case T_MNEM_movs:
11754             /* We know we have low registers at this point.
11755                Generate LSLS Rd, Rs, #0.  */
11756             inst.instruction = T_OPCODE_LSL_I;
11757             inst.instruction |= Rn;
11758             inst.instruction |= Rm << 3;
11759             break;
11760
11761           case T_MNEM_cmp:
11762             if (low_regs)
11763               {
11764                 inst.instruction = T_OPCODE_CMP_LR;
11765                 inst.instruction |= Rn;
11766                 inst.instruction |= Rm << 3;
11767               }
11768             else
11769               {
11770                 inst.instruction = T_OPCODE_CMP_HR;
11771                 inst.instruction |= (Rn & 0x8) << 4;
11772                 inst.instruction |= (Rn & 0x7);
11773                 inst.instruction |= Rm << 3;
11774               }
11775             break;
11776           }
11777       return;
11778     }
11779
11780   inst.instruction = THUMB_OP16 (inst.instruction);
11781
11782   /* PR 10443: Do not silently ignore shifted operands.  */
11783   constraint (inst.operands[1].shifted,
11784               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
11785
11786   if (inst.operands[1].isreg)
11787     {
11788       if (Rn < 8 && Rm < 8)
11789         {
11790           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
11791              since a MOV instruction produces unpredictable results.  */
11792           if (inst.instruction == T_OPCODE_MOV_I8)
11793             inst.instruction = T_OPCODE_ADD_I3;
11794           else
11795             inst.instruction = T_OPCODE_CMP_LR;
11796
11797           inst.instruction |= Rn;
11798           inst.instruction |= Rm << 3;
11799         }
11800       else
11801         {
11802           if (inst.instruction == T_OPCODE_MOV_I8)
11803             inst.instruction = T_OPCODE_MOV_HR;
11804           else
11805             inst.instruction = T_OPCODE_CMP_HR;
11806           do_t_cpy ();
11807         }
11808     }
11809   else
11810     {
11811       constraint (Rn > 7,
11812                   _("only lo regs allowed with immediate"));
11813       inst.instruction |= Rn << 8;
11814       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
11815     }
11816 }
11817
11818 static void
11819 do_t_mov16 (void)
11820 {
11821   unsigned Rd;
11822   bfd_vma imm;
11823   bfd_boolean top;
11824
11825   top = (inst.instruction & 0x00800000) != 0;
11826   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
11827     {
11828       constraint (top, _(":lower16: not allowed this instruction"));
11829       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
11830     }
11831   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
11832     {
11833       constraint (!top, _(":upper16: not allowed this instruction"));
11834       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
11835     }
11836
11837   Rd = inst.operands[0].reg;
11838   reject_bad_reg (Rd);
11839
11840   inst.instruction |= Rd << 8;
11841   if (inst.reloc.type == BFD_RELOC_UNUSED)
11842     {
11843       imm = inst.reloc.exp.X_add_number;
11844       inst.instruction |= (imm & 0xf000) << 4;
11845       inst.instruction |= (imm & 0x0800) << 15;
11846       inst.instruction |= (imm & 0x0700) << 4;
11847       inst.instruction |= (imm & 0x00ff);
11848     }
11849 }
11850
11851 static void
11852 do_t_mvn_tst (void)
11853 {
11854   unsigned Rn, Rm;
11855
11856   Rn = inst.operands[0].reg;
11857   Rm = inst.operands[1].reg;
11858
11859   if (inst.instruction == T_MNEM_cmp
11860       || inst.instruction == T_MNEM_cmn)
11861     constraint (Rn == REG_PC, BAD_PC);
11862   else
11863     reject_bad_reg (Rn);
11864   reject_bad_reg (Rm);
11865
11866   if (unified_syntax)
11867     {
11868       int r0off = (inst.instruction == T_MNEM_mvn
11869                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
11870       bfd_boolean narrow;
11871
11872       if (inst.size_req == 4
11873           || inst.instruction > 0xffff
11874           || inst.operands[1].shifted
11875           || Rn > 7 || Rm > 7)
11876         narrow = FALSE;
11877       else if (inst.instruction == T_MNEM_cmn
11878                || inst.instruction == T_MNEM_tst)
11879         narrow = TRUE;
11880       else if (THUMB_SETS_FLAGS (inst.instruction))
11881         narrow = !in_it_block ();
11882       else
11883         narrow = in_it_block ();
11884
11885       if (!inst.operands[1].isreg)
11886         {
11887           /* For an immediate, we always generate a 32-bit opcode;
11888              section relaxation will shrink it later if possible.  */
11889           if (inst.instruction < 0xffff)
11890             inst.instruction = THUMB_OP32 (inst.instruction);
11891           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11892           inst.instruction |= Rn << r0off;
11893           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11894         }
11895       else
11896         {
11897           /* See if we can do this with a 16-bit instruction.  */
11898           if (narrow)
11899             {
11900               inst.instruction = THUMB_OP16 (inst.instruction);
11901               inst.instruction |= Rn;
11902               inst.instruction |= Rm << 3;
11903             }
11904           else
11905             {
11906               constraint (inst.operands[1].shifted
11907                           && inst.operands[1].immisreg,
11908                           _("shift must be constant"));
11909               if (inst.instruction < 0xffff)
11910                 inst.instruction = THUMB_OP32 (inst.instruction);
11911               inst.instruction |= Rn << r0off;
11912               encode_thumb32_shifted_operand (1);
11913             }
11914         }
11915     }
11916   else
11917     {
11918       constraint (inst.instruction > 0xffff
11919                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
11920       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
11921                   _("unshifted register required"));
11922       constraint (Rn > 7 || Rm > 7,
11923                   BAD_HIREG);
11924
11925       inst.instruction = THUMB_OP16 (inst.instruction);
11926       inst.instruction |= Rn;
11927       inst.instruction |= Rm << 3;
11928     }
11929 }
11930
11931 static void
11932 do_t_mrs (void)
11933 {
11934   unsigned Rd;
11935
11936   if (do_vfp_nsyn_mrs () == SUCCESS)
11937     return;
11938
11939   Rd = inst.operands[0].reg;
11940   reject_bad_reg (Rd);
11941   inst.instruction |= Rd << 8;
11942
11943   if (inst.operands[1].isreg)
11944     {
11945       unsigned br = inst.operands[1].reg;
11946       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
11947         as_bad (_("bad register for mrs"));
11948
11949       inst.instruction |= br & (0xf << 16);
11950       inst.instruction |= (br & 0x300) >> 4;
11951       inst.instruction |= (br & SPSR_BIT) >> 2;
11952     }
11953   else
11954     {
11955       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
11956
11957       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
11958         {
11959           /* PR gas/12698:  The constraint is only applied for m_profile.
11960              If the user has specified -march=all, we want to ignore it as
11961              we are building for any CPU type, including non-m variants.  */
11962           bfd_boolean m_profile =
11963             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
11964           constraint ((flags != 0) && m_profile, _("selected processor does "
11965                                                    "not support requested special purpose register"));
11966         }
11967       else
11968         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
11969            devices).  */
11970         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
11971                     _("'APSR', 'CPSR' or 'SPSR' expected"));
11972
11973       inst.instruction |= (flags & SPSR_BIT) >> 2;
11974       inst.instruction |= inst.operands[1].imm & 0xff;
11975       inst.instruction |= 0xf0000;
11976     }
11977 }
11978
11979 static void
11980 do_t_msr (void)
11981 {
11982   int flags;
11983   unsigned Rn;
11984
11985   if (do_vfp_nsyn_msr () == SUCCESS)
11986     return;
11987
11988   constraint (!inst.operands[1].isreg,
11989               _("Thumb encoding does not support an immediate here"));
11990
11991   if (inst.operands[0].isreg)
11992     flags = (int)(inst.operands[0].reg);
11993   else
11994     flags = inst.operands[0].imm;
11995
11996   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
11997     {
11998       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
11999
12000       /* PR gas/12698:  The constraint is only applied for m_profile.
12001          If the user has specified -march=all, we want to ignore it as
12002          we are building for any CPU type, including non-m variants.  */
12003       bfd_boolean m_profile =
12004         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12005       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12006            && (bits & ~(PSR_s | PSR_f)) != 0)
12007           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12008               && bits != PSR_f)) && m_profile,
12009           _("selected processor does not support requested special "
12010             "purpose register"));
12011     }
12012   else
12013      constraint ((flags & 0xff) != 0, _("selected processor does not support "
12014                  "requested special purpose register"));
12015
12016   Rn = inst.operands[1].reg;
12017   reject_bad_reg (Rn);
12018
12019   inst.instruction |= (flags & SPSR_BIT) >> 2;
12020   inst.instruction |= (flags & 0xf0000) >> 8;
12021   inst.instruction |= (flags & 0x300) >> 4;
12022   inst.instruction |= (flags & 0xff);
12023   inst.instruction |= Rn << 16;
12024 }
12025
12026 static void
12027 do_t_mul (void)
12028 {
12029   bfd_boolean narrow;
12030   unsigned Rd, Rn, Rm;
12031
12032   if (!inst.operands[2].present)
12033     inst.operands[2].reg = inst.operands[0].reg;
12034
12035   Rd = inst.operands[0].reg;
12036   Rn = inst.operands[1].reg;
12037   Rm = inst.operands[2].reg;
12038
12039   if (unified_syntax)
12040     {
12041       if (inst.size_req == 4
12042           || (Rd != Rn
12043               && Rd != Rm)
12044           || Rn > 7
12045           || Rm > 7)
12046         narrow = FALSE;
12047       else if (inst.instruction == T_MNEM_muls)
12048         narrow = !in_it_block ();
12049       else
12050         narrow = in_it_block ();
12051     }
12052   else
12053     {
12054       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
12055       constraint (Rn > 7 || Rm > 7,
12056                   BAD_HIREG);
12057       narrow = TRUE;
12058     }
12059
12060   if (narrow)
12061     {
12062       /* 16-bit MULS/Conditional MUL.  */
12063       inst.instruction = THUMB_OP16 (inst.instruction);
12064       inst.instruction |= Rd;
12065
12066       if (Rd == Rn)
12067         inst.instruction |= Rm << 3;
12068       else if (Rd == Rm)
12069         inst.instruction |= Rn << 3;
12070       else
12071         constraint (1, _("dest must overlap one source register"));
12072     }
12073   else
12074     {
12075       constraint (inst.instruction != T_MNEM_mul,
12076                   _("Thumb-2 MUL must not set flags"));
12077       /* 32-bit MUL.  */
12078       inst.instruction = THUMB_OP32 (inst.instruction);
12079       inst.instruction |= Rd << 8;
12080       inst.instruction |= Rn << 16;
12081       inst.instruction |= Rm << 0;
12082
12083       reject_bad_reg (Rd);
12084       reject_bad_reg (Rn);
12085       reject_bad_reg (Rm);
12086     }
12087 }
12088
12089 static void
12090 do_t_mull (void)
12091 {
12092   unsigned RdLo, RdHi, Rn, Rm;
12093
12094   RdLo = inst.operands[0].reg;
12095   RdHi = inst.operands[1].reg;
12096   Rn = inst.operands[2].reg;
12097   Rm = inst.operands[3].reg;
12098
12099   reject_bad_reg (RdLo);
12100   reject_bad_reg (RdHi);
12101   reject_bad_reg (Rn);
12102   reject_bad_reg (Rm);
12103
12104   inst.instruction |= RdLo << 12;
12105   inst.instruction |= RdHi << 8;
12106   inst.instruction |= Rn << 16;
12107   inst.instruction |= Rm;
12108
12109  if (RdLo == RdHi)
12110     as_tsktsk (_("rdhi and rdlo must be different"));
12111 }
12112
12113 static void
12114 do_t_nop (void)
12115 {
12116   set_it_insn_type (NEUTRAL_IT_INSN);
12117
12118   if (unified_syntax)
12119     {
12120       if (inst.size_req == 4 || inst.operands[0].imm > 15)
12121         {
12122           inst.instruction = THUMB_OP32 (inst.instruction);
12123           inst.instruction |= inst.operands[0].imm;
12124         }
12125       else
12126         {
12127           /* PR9722: Check for Thumb2 availability before
12128              generating a thumb2 nop instruction.  */
12129           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
12130             {
12131               inst.instruction = THUMB_OP16 (inst.instruction);
12132               inst.instruction |= inst.operands[0].imm << 4;
12133             }
12134           else
12135             inst.instruction = 0x46c0;
12136         }
12137     }
12138   else
12139     {
12140       constraint (inst.operands[0].present,
12141                   _("Thumb does not support NOP with hints"));
12142       inst.instruction = 0x46c0;
12143     }
12144 }
12145
12146 static void
12147 do_t_neg (void)
12148 {
12149   if (unified_syntax)
12150     {
12151       bfd_boolean narrow;
12152
12153       if (THUMB_SETS_FLAGS (inst.instruction))
12154         narrow = !in_it_block ();
12155       else
12156         narrow = in_it_block ();
12157       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12158         narrow = FALSE;
12159       if (inst.size_req == 4)
12160         narrow = FALSE;
12161
12162       if (!narrow)
12163         {
12164           inst.instruction = THUMB_OP32 (inst.instruction);
12165           inst.instruction |= inst.operands[0].reg << 8;
12166           inst.instruction |= inst.operands[1].reg << 16;
12167         }
12168       else
12169         {
12170           inst.instruction = THUMB_OP16 (inst.instruction);
12171           inst.instruction |= inst.operands[0].reg;
12172           inst.instruction |= inst.operands[1].reg << 3;
12173         }
12174     }
12175   else
12176     {
12177       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
12178                   BAD_HIREG);
12179       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12180
12181       inst.instruction = THUMB_OP16 (inst.instruction);
12182       inst.instruction |= inst.operands[0].reg;
12183       inst.instruction |= inst.operands[1].reg << 3;
12184     }
12185 }
12186
12187 static void
12188 do_t_orn (void)
12189 {
12190   unsigned Rd, Rn;
12191
12192   Rd = inst.operands[0].reg;
12193   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
12194
12195   reject_bad_reg (Rd);
12196   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
12197   reject_bad_reg (Rn);
12198
12199   inst.instruction |= Rd << 8;
12200   inst.instruction |= Rn << 16;
12201
12202   if (!inst.operands[2].isreg)
12203     {
12204       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12205       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12206     }
12207   else
12208     {
12209       unsigned Rm;
12210
12211       Rm = inst.operands[2].reg;
12212       reject_bad_reg (Rm);
12213
12214       constraint (inst.operands[2].shifted
12215                   && inst.operands[2].immisreg,
12216                   _("shift must be constant"));
12217       encode_thumb32_shifted_operand (2);
12218     }
12219 }
12220
12221 static void
12222 do_t_pkhbt (void)
12223 {
12224   unsigned Rd, Rn, Rm;
12225
12226   Rd = inst.operands[0].reg;
12227   Rn = inst.operands[1].reg;
12228   Rm = inst.operands[2].reg;
12229
12230   reject_bad_reg (Rd);
12231   reject_bad_reg (Rn);
12232   reject_bad_reg (Rm);
12233
12234   inst.instruction |= Rd << 8;
12235   inst.instruction |= Rn << 16;
12236   inst.instruction |= Rm;
12237   if (inst.operands[3].present)
12238     {
12239       unsigned int val = inst.reloc.exp.X_add_number;
12240       constraint (inst.reloc.exp.X_op != O_constant,
12241                   _("expression too complex"));
12242       inst.instruction |= (val & 0x1c) << 10;
12243       inst.instruction |= (val & 0x03) << 6;
12244     }
12245 }
12246
12247 static void
12248 do_t_pkhtb (void)
12249 {
12250   if (!inst.operands[3].present)
12251     {
12252       unsigned Rtmp;
12253
12254       inst.instruction &= ~0x00000020;
12255
12256       /* PR 10168.  Swap the Rm and Rn registers.  */
12257       Rtmp = inst.operands[1].reg;
12258       inst.operands[1].reg = inst.operands[2].reg;
12259       inst.operands[2].reg = Rtmp;
12260     }
12261   do_t_pkhbt ();
12262 }
12263
12264 static void
12265 do_t_pld (void)
12266 {
12267   if (inst.operands[0].immisreg)
12268     reject_bad_reg (inst.operands[0].imm);
12269
12270   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
12271 }
12272
12273 static void
12274 do_t_push_pop (void)
12275 {
12276   unsigned mask;
12277
12278   constraint (inst.operands[0].writeback,
12279               _("push/pop do not support {reglist}^"));
12280   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
12281               _("expression too complex"));
12282
12283   mask = inst.operands[0].imm;
12284   if (inst.size_req != 4 && (mask & ~0xff) == 0)
12285     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
12286   else if (inst.size_req != 4
12287            && (mask & ~0xff) == (1 << (inst.instruction == T_MNEM_push
12288                                        ? REG_LR : REG_PC)))
12289     {
12290       inst.instruction = THUMB_OP16 (inst.instruction);
12291       inst.instruction |= THUMB_PP_PC_LR;
12292       inst.instruction |= mask & 0xff;
12293     }
12294   else if (unified_syntax)
12295     {
12296       inst.instruction = THUMB_OP32 (inst.instruction);
12297       encode_thumb2_ldmstm (13, mask, TRUE);
12298     }
12299   else
12300     {
12301       inst.error = _("invalid register list to push/pop instruction");
12302       return;
12303     }
12304 }
12305
12306 static void
12307 do_t_rbit (void)
12308 {
12309   unsigned Rd, Rm;
12310
12311   Rd = inst.operands[0].reg;
12312   Rm = inst.operands[1].reg;
12313
12314   reject_bad_reg (Rd);
12315   reject_bad_reg (Rm);
12316
12317   inst.instruction |= Rd << 8;
12318   inst.instruction |= Rm << 16;
12319   inst.instruction |= Rm;
12320 }
12321
12322 static void
12323 do_t_rev (void)
12324 {
12325   unsigned Rd, Rm;
12326
12327   Rd = inst.operands[0].reg;
12328   Rm = inst.operands[1].reg;
12329
12330   reject_bad_reg (Rd);
12331   reject_bad_reg (Rm);
12332
12333   if (Rd <= 7 && Rm <= 7
12334       && inst.size_req != 4)
12335     {
12336       inst.instruction = THUMB_OP16 (inst.instruction);
12337       inst.instruction |= Rd;
12338       inst.instruction |= Rm << 3;
12339     }
12340   else if (unified_syntax)
12341     {
12342       inst.instruction = THUMB_OP32 (inst.instruction);
12343       inst.instruction |= Rd << 8;
12344       inst.instruction |= Rm << 16;
12345       inst.instruction |= Rm;
12346     }
12347   else
12348     inst.error = BAD_HIREG;
12349 }
12350
12351 static void
12352 do_t_rrx (void)
12353 {
12354   unsigned Rd, Rm;
12355
12356   Rd = inst.operands[0].reg;
12357   Rm = inst.operands[1].reg;
12358
12359   reject_bad_reg (Rd);
12360   reject_bad_reg (Rm);
12361
12362   inst.instruction |= Rd << 8;
12363   inst.instruction |= Rm;
12364 }
12365
12366 static void
12367 do_t_rsb (void)
12368 {
12369   unsigned Rd, Rs;
12370
12371   Rd = inst.operands[0].reg;
12372   Rs = (inst.operands[1].present
12373         ? inst.operands[1].reg    /* Rd, Rs, foo */
12374         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
12375
12376   reject_bad_reg (Rd);
12377   reject_bad_reg (Rs);
12378   if (inst.operands[2].isreg)
12379     reject_bad_reg (inst.operands[2].reg);
12380
12381   inst.instruction |= Rd << 8;
12382   inst.instruction |= Rs << 16;
12383   if (!inst.operands[2].isreg)
12384     {
12385       bfd_boolean narrow;
12386
12387       if ((inst.instruction & 0x00100000) != 0)
12388         narrow = !in_it_block ();
12389       else
12390         narrow = in_it_block ();
12391
12392       if (Rd > 7 || Rs > 7)
12393         narrow = FALSE;
12394
12395       if (inst.size_req == 4 || !unified_syntax)
12396         narrow = FALSE;
12397
12398       if (inst.reloc.exp.X_op != O_constant
12399           || inst.reloc.exp.X_add_number != 0)
12400         narrow = FALSE;
12401
12402       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
12403          relaxation, but it doesn't seem worth the hassle.  */
12404       if (narrow)
12405         {
12406           inst.reloc.type = BFD_RELOC_UNUSED;
12407           inst.instruction = THUMB_OP16 (T_MNEM_negs);
12408           inst.instruction |= Rs << 3;
12409           inst.instruction |= Rd;
12410         }
12411       else
12412         {
12413           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12414           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12415         }
12416     }
12417   else
12418     encode_thumb32_shifted_operand (2);
12419 }
12420
12421 static void
12422 do_t_setend (void)
12423 {
12424   if (warn_on_deprecated
12425       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12426       as_tsktsk (_("setend use is deprecated for ARMv8"));
12427
12428   set_it_insn_type (OUTSIDE_IT_INSN);
12429   if (inst.operands[0].imm)
12430     inst.instruction |= 0x8;
12431 }
12432
12433 static void
12434 do_t_shift (void)
12435 {
12436   if (!inst.operands[1].present)
12437     inst.operands[1].reg = inst.operands[0].reg;
12438
12439   if (unified_syntax)
12440     {
12441       bfd_boolean narrow;
12442       int shift_kind;
12443
12444       switch (inst.instruction)
12445         {
12446         case T_MNEM_asr:
12447         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
12448         case T_MNEM_lsl:
12449         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
12450         case T_MNEM_lsr:
12451         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
12452         case T_MNEM_ror:
12453         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
12454         default: abort ();
12455         }
12456
12457       if (THUMB_SETS_FLAGS (inst.instruction))
12458         narrow = !in_it_block ();
12459       else
12460         narrow = in_it_block ();
12461       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12462         narrow = FALSE;
12463       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
12464         narrow = FALSE;
12465       if (inst.operands[2].isreg
12466           && (inst.operands[1].reg != inst.operands[0].reg
12467               || inst.operands[2].reg > 7))
12468         narrow = FALSE;
12469       if (inst.size_req == 4)
12470         narrow = FALSE;
12471
12472       reject_bad_reg (inst.operands[0].reg);
12473       reject_bad_reg (inst.operands[1].reg);
12474
12475       if (!narrow)
12476         {
12477           if (inst.operands[2].isreg)
12478             {
12479               reject_bad_reg (inst.operands[2].reg);
12480               inst.instruction = THUMB_OP32 (inst.instruction);
12481               inst.instruction |= inst.operands[0].reg << 8;
12482               inst.instruction |= inst.operands[1].reg << 16;
12483               inst.instruction |= inst.operands[2].reg;
12484
12485               /* PR 12854: Error on extraneous shifts.  */
12486               constraint (inst.operands[2].shifted,
12487                           _("extraneous shift as part of operand to shift insn"));
12488             }
12489           else
12490             {
12491               inst.operands[1].shifted = 1;
12492               inst.operands[1].shift_kind = shift_kind;
12493               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
12494                                              ? T_MNEM_movs : T_MNEM_mov);
12495               inst.instruction |= inst.operands[0].reg << 8;
12496               encode_thumb32_shifted_operand (1);
12497               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
12498               inst.reloc.type = BFD_RELOC_UNUSED;
12499             }
12500         }
12501       else
12502         {
12503           if (inst.operands[2].isreg)
12504             {
12505               switch (shift_kind)
12506                 {
12507                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
12508                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
12509                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
12510                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
12511                 default: abort ();
12512                 }
12513
12514               inst.instruction |= inst.operands[0].reg;
12515               inst.instruction |= inst.operands[2].reg << 3;
12516
12517               /* PR 12854: Error on extraneous shifts.  */
12518               constraint (inst.operands[2].shifted,
12519                           _("extraneous shift as part of operand to shift insn"));
12520             }
12521           else
12522             {
12523               switch (shift_kind)
12524                 {
12525                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12526                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12527                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12528                 default: abort ();
12529                 }
12530               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12531               inst.instruction |= inst.operands[0].reg;
12532               inst.instruction |= inst.operands[1].reg << 3;
12533             }
12534         }
12535     }
12536   else
12537     {
12538       constraint (inst.operands[0].reg > 7
12539                   || inst.operands[1].reg > 7, BAD_HIREG);
12540       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12541
12542       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
12543         {
12544           constraint (inst.operands[2].reg > 7, BAD_HIREG);
12545           constraint (inst.operands[0].reg != inst.operands[1].reg,
12546                       _("source1 and dest must be same register"));
12547
12548           switch (inst.instruction)
12549             {
12550             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
12551             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
12552             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
12553             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
12554             default: abort ();
12555             }
12556
12557           inst.instruction |= inst.operands[0].reg;
12558           inst.instruction |= inst.operands[2].reg << 3;
12559
12560           /* PR 12854: Error on extraneous shifts.  */
12561           constraint (inst.operands[2].shifted,
12562                       _("extraneous shift as part of operand to shift insn"));
12563         }
12564       else
12565         {
12566           switch (inst.instruction)
12567             {
12568             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
12569             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
12570             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
12571             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
12572             default: abort ();
12573             }
12574           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12575           inst.instruction |= inst.operands[0].reg;
12576           inst.instruction |= inst.operands[1].reg << 3;
12577         }
12578     }
12579 }
12580
12581 static void
12582 do_t_simd (void)
12583 {
12584   unsigned Rd, Rn, Rm;
12585
12586   Rd = inst.operands[0].reg;
12587   Rn = inst.operands[1].reg;
12588   Rm = inst.operands[2].reg;
12589
12590   reject_bad_reg (Rd);
12591   reject_bad_reg (Rn);
12592   reject_bad_reg (Rm);
12593
12594   inst.instruction |= Rd << 8;
12595   inst.instruction |= Rn << 16;
12596   inst.instruction |= Rm;
12597 }
12598
12599 static void
12600 do_t_simd2 (void)
12601 {
12602   unsigned Rd, Rn, Rm;
12603
12604   Rd = inst.operands[0].reg;
12605   Rm = inst.operands[1].reg;
12606   Rn = inst.operands[2].reg;
12607
12608   reject_bad_reg (Rd);
12609   reject_bad_reg (Rn);
12610   reject_bad_reg (Rm);
12611
12612   inst.instruction |= Rd << 8;
12613   inst.instruction |= Rn << 16;
12614   inst.instruction |= Rm;
12615 }
12616
12617 static void
12618 do_t_smc (void)
12619 {
12620   unsigned int value = inst.reloc.exp.X_add_number;
12621   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
12622               _("SMC is not permitted on this architecture"));
12623   constraint (inst.reloc.exp.X_op != O_constant,
12624               _("expression too complex"));
12625   inst.reloc.type = BFD_RELOC_UNUSED;
12626   inst.instruction |= (value & 0xf000) >> 12;
12627   inst.instruction |= (value & 0x0ff0);
12628   inst.instruction |= (value & 0x000f) << 16;
12629   /* PR gas/15623: SMC instructions must be last in an IT block.  */
12630   set_it_insn_type_last ();
12631 }
12632
12633 static void
12634 do_t_hvc (void)
12635 {
12636   unsigned int value = inst.reloc.exp.X_add_number;
12637
12638   inst.reloc.type = BFD_RELOC_UNUSED;
12639   inst.instruction |= (value & 0x0fff);
12640   inst.instruction |= (value & 0xf000) << 4;
12641 }
12642
12643 static void
12644 do_t_ssat_usat (int bias)
12645 {
12646   unsigned Rd, Rn;
12647
12648   Rd = inst.operands[0].reg;
12649   Rn = inst.operands[2].reg;
12650
12651   reject_bad_reg (Rd);
12652   reject_bad_reg (Rn);
12653
12654   inst.instruction |= Rd << 8;
12655   inst.instruction |= inst.operands[1].imm - bias;
12656   inst.instruction |= Rn << 16;
12657
12658   if (inst.operands[3].present)
12659     {
12660       offsetT shift_amount = inst.reloc.exp.X_add_number;
12661
12662       inst.reloc.type = BFD_RELOC_UNUSED;
12663
12664       constraint (inst.reloc.exp.X_op != O_constant,
12665                   _("expression too complex"));
12666
12667       if (shift_amount != 0)
12668         {
12669           constraint (shift_amount > 31,
12670                       _("shift expression is too large"));
12671
12672           if (inst.operands[3].shift_kind == SHIFT_ASR)
12673             inst.instruction |= 0x00200000;  /* sh bit.  */
12674
12675           inst.instruction |= (shift_amount & 0x1c) << 10;
12676           inst.instruction |= (shift_amount & 0x03) << 6;
12677         }
12678     }
12679 }
12680
12681 static void
12682 do_t_ssat (void)
12683 {
12684   do_t_ssat_usat (1);
12685 }
12686
12687 static void
12688 do_t_ssat16 (void)
12689 {
12690   unsigned Rd, Rn;
12691
12692   Rd = inst.operands[0].reg;
12693   Rn = inst.operands[2].reg;
12694
12695   reject_bad_reg (Rd);
12696   reject_bad_reg (Rn);
12697
12698   inst.instruction |= Rd << 8;
12699   inst.instruction |= inst.operands[1].imm - 1;
12700   inst.instruction |= Rn << 16;
12701 }
12702
12703 static void
12704 do_t_strex (void)
12705 {
12706   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
12707               || inst.operands[2].postind || inst.operands[2].writeback
12708               || inst.operands[2].immisreg || inst.operands[2].shifted
12709               || inst.operands[2].negative,
12710               BAD_ADDR_MODE);
12711
12712   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
12713
12714   inst.instruction |= inst.operands[0].reg << 8;
12715   inst.instruction |= inst.operands[1].reg << 12;
12716   inst.instruction |= inst.operands[2].reg << 16;
12717   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
12718 }
12719
12720 static void
12721 do_t_strexd (void)
12722 {
12723   if (!inst.operands[2].present)
12724     inst.operands[2].reg = inst.operands[1].reg + 1;
12725
12726   constraint (inst.operands[0].reg == inst.operands[1].reg
12727               || inst.operands[0].reg == inst.operands[2].reg
12728               || inst.operands[0].reg == inst.operands[3].reg,
12729               BAD_OVERLAP);
12730
12731   inst.instruction |= inst.operands[0].reg;
12732   inst.instruction |= inst.operands[1].reg << 12;
12733   inst.instruction |= inst.operands[2].reg << 8;
12734   inst.instruction |= inst.operands[3].reg << 16;
12735 }
12736
12737 static void
12738 do_t_sxtah (void)
12739 {
12740   unsigned Rd, Rn, Rm;
12741
12742   Rd = inst.operands[0].reg;
12743   Rn = inst.operands[1].reg;
12744   Rm = inst.operands[2].reg;
12745
12746   reject_bad_reg (Rd);
12747   reject_bad_reg (Rn);
12748   reject_bad_reg (Rm);
12749
12750   inst.instruction |= Rd << 8;
12751   inst.instruction |= Rn << 16;
12752   inst.instruction |= Rm;
12753   inst.instruction |= inst.operands[3].imm << 4;
12754 }
12755
12756 static void
12757 do_t_sxth (void)
12758 {
12759   unsigned Rd, Rm;
12760
12761   Rd = inst.operands[0].reg;
12762   Rm = inst.operands[1].reg;
12763
12764   reject_bad_reg (Rd);
12765   reject_bad_reg (Rm);
12766
12767   if (inst.instruction <= 0xffff
12768       && inst.size_req != 4
12769       && Rd <= 7 && Rm <= 7
12770       && (!inst.operands[2].present || inst.operands[2].imm == 0))
12771     {
12772       inst.instruction = THUMB_OP16 (inst.instruction);
12773       inst.instruction |= Rd;
12774       inst.instruction |= Rm << 3;
12775     }
12776   else if (unified_syntax)
12777     {
12778       if (inst.instruction <= 0xffff)
12779         inst.instruction = THUMB_OP32 (inst.instruction);
12780       inst.instruction |= Rd << 8;
12781       inst.instruction |= Rm;
12782       inst.instruction |= inst.operands[2].imm << 4;
12783     }
12784   else
12785     {
12786       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
12787                   _("Thumb encoding does not support rotation"));
12788       constraint (1, BAD_HIREG);
12789     }
12790 }
12791
12792 static void
12793 do_t_swi (void)
12794 {
12795   /* We have to do the following check manually as ARM_EXT_OS only applies
12796      to ARM_EXT_V6M.  */
12797   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6m))
12798     {
12799       if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_os)
12800           /* This only applies to the v6m howver, not later architectures.  */
12801           && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7))
12802         as_bad (_("SVC is not permitted on this architecture"));
12803       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, arm_ext_os);
12804     }
12805
12806   inst.reloc.type = BFD_RELOC_ARM_SWI;
12807 }
12808
12809 static void
12810 do_t_tb (void)
12811 {
12812   unsigned Rn, Rm;
12813   int half;
12814
12815   half = (inst.instruction & 0x10) != 0;
12816   set_it_insn_type_last ();
12817   constraint (inst.operands[0].immisreg,
12818               _("instruction requires register index"));
12819
12820   Rn = inst.operands[0].reg;
12821   Rm = inst.operands[0].imm;
12822
12823   constraint (Rn == REG_SP, BAD_SP);
12824   reject_bad_reg (Rm);
12825
12826   constraint (!half && inst.operands[0].shifted,
12827               _("instruction does not allow shifted index"));
12828   inst.instruction |= (Rn << 16) | Rm;
12829 }
12830
12831 static void
12832 do_t_udf (void)
12833 {
12834   if (!inst.operands[0].present)
12835     inst.operands[0].imm = 0;
12836
12837   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
12838     {
12839       constraint (inst.size_req == 2,
12840                   _("immediate value out of range"));
12841       inst.instruction = THUMB_OP32 (inst.instruction);
12842       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
12843       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
12844     }
12845   else
12846     {
12847       inst.instruction = THUMB_OP16 (inst.instruction);
12848       inst.instruction |= inst.operands[0].imm;
12849     }
12850
12851   set_it_insn_type (NEUTRAL_IT_INSN);
12852 }
12853
12854
12855 static void
12856 do_t_usat (void)
12857 {
12858   do_t_ssat_usat (0);
12859 }
12860
12861 static void
12862 do_t_usat16 (void)
12863 {
12864   unsigned Rd, Rn;
12865
12866   Rd = inst.operands[0].reg;
12867   Rn = inst.operands[2].reg;
12868
12869   reject_bad_reg (Rd);
12870   reject_bad_reg (Rn);
12871
12872   inst.instruction |= Rd << 8;
12873   inst.instruction |= inst.operands[1].imm;
12874   inst.instruction |= Rn << 16;
12875 }
12876
12877 /* Neon instruction encoder helpers.  */
12878
12879 /* Encodings for the different types for various Neon opcodes.  */
12880
12881 /* An "invalid" code for the following tables.  */
12882 #define N_INV -1u
12883
12884 struct neon_tab_entry
12885 {
12886   unsigned integer;
12887   unsigned float_or_poly;
12888   unsigned scalar_or_imm;
12889 };
12890
12891 /* Map overloaded Neon opcodes to their respective encodings.  */
12892 #define NEON_ENC_TAB                                    \
12893   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
12894   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
12895   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
12896   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
12897   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
12898   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
12899   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
12900   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
12901   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
12902   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
12903   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
12904   /* Register variants of the following two instructions are encoded as
12905      vcge / vcgt with the operands reversed.  */        \
12906   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
12907   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
12908   X(vfma,       N_INV, 0x0000c10, N_INV),               \
12909   X(vfms,       N_INV, 0x0200c10, N_INV),               \
12910   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
12911   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
12912   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
12913   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
12914   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
12915   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
12916   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
12917   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
12918   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
12919   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
12920   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
12921   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
12922   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
12923   X(vshl,       0x0000400, N_INV,     0x0800510),       \
12924   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
12925   X(vand,       0x0000110, N_INV,     0x0800030),       \
12926   X(vbic,       0x0100110, N_INV,     0x0800030),       \
12927   X(veor,       0x1000110, N_INV,     N_INV),           \
12928   X(vorn,       0x0300110, N_INV,     0x0800010),       \
12929   X(vorr,       0x0200110, N_INV,     0x0800010),       \
12930   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
12931   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
12932   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
12933   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
12934   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
12935   X(vst1,       0x0000000, 0x0800000, N_INV),           \
12936   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
12937   X(vst2,       0x0000100, 0x0800100, N_INV),           \
12938   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
12939   X(vst3,       0x0000200, 0x0800200, N_INV),           \
12940   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
12941   X(vst4,       0x0000300, 0x0800300, N_INV),           \
12942   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
12943   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
12944   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
12945   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
12946   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
12947   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
12948   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
12949   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
12950   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
12951   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
12952   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
12953   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
12954   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
12955   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
12956   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
12957   X(vselge,     0xe200a00, N_INV,     N_INV),           \
12958   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
12959   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
12960   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
12961   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
12962   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
12963   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
12964   X(aes,        0x3b00300, N_INV,     N_INV),           \
12965   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
12966   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
12967   X(sha2op,     0x3ba0380, N_INV,     N_INV)
12968
12969 enum neon_opc
12970 {
12971 #define X(OPC,I,F,S) N_MNEM_##OPC
12972 NEON_ENC_TAB
12973 #undef X
12974 };
12975
12976 static const struct neon_tab_entry neon_enc_tab[] =
12977 {
12978 #define X(OPC,I,F,S) { (I), (F), (S) }
12979 NEON_ENC_TAB
12980 #undef X
12981 };
12982
12983 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
12984 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
12985 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
12986 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12987 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12988 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12989 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12990 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
12991 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12992 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12993 #define NEON_ENC_SINGLE_(X) \
12994   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
12995 #define NEON_ENC_DOUBLE_(X) \
12996   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
12997 #define NEON_ENC_FPV8_(X) \
12998   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
12999
13000 #define NEON_ENCODE(type, inst)                                 \
13001   do                                                            \
13002     {                                                           \
13003       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
13004       inst.is_neon = 1;                                         \
13005     }                                                           \
13006   while (0)
13007
13008 #define check_neon_suffixes                                             \
13009   do                                                                    \
13010     {                                                                   \
13011       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
13012         {                                                               \
13013           as_bad (_("invalid neon suffix for non neon instruction"));   \
13014           return;                                                       \
13015         }                                                               \
13016     }                                                                   \
13017   while (0)
13018
13019 /* Define shapes for instruction operands. The following mnemonic characters
13020    are used in this table:
13021
13022      F - VFP S<n> register
13023      D - Neon D<n> register
13024      Q - Neon Q<n> register
13025      I - Immediate
13026      S - Scalar
13027      R - ARM register
13028      L - D<n> register list
13029
13030    This table is used to generate various data:
13031      - enumerations of the form NS_DDR to be used as arguments to
13032        neon_select_shape.
13033      - a table classifying shapes into single, double, quad, mixed.
13034      - a table used to drive neon_select_shape.  */
13035
13036 #define NEON_SHAPE_DEF                  \
13037   X(3, (D, D, D), DOUBLE),              \
13038   X(3, (Q, Q, Q), QUAD),                \
13039   X(3, (D, D, I), DOUBLE),              \
13040   X(3, (Q, Q, I), QUAD),                \
13041   X(3, (D, D, S), DOUBLE),              \
13042   X(3, (Q, Q, S), QUAD),                \
13043   X(2, (D, D), DOUBLE),                 \
13044   X(2, (Q, Q), QUAD),                   \
13045   X(2, (D, S), DOUBLE),                 \
13046   X(2, (Q, S), QUAD),                   \
13047   X(2, (D, R), DOUBLE),                 \
13048   X(2, (Q, R), QUAD),                   \
13049   X(2, (D, I), DOUBLE),                 \
13050   X(2, (Q, I), QUAD),                   \
13051   X(3, (D, L, D), DOUBLE),              \
13052   X(2, (D, Q), MIXED),                  \
13053   X(2, (Q, D), MIXED),                  \
13054   X(3, (D, Q, I), MIXED),               \
13055   X(3, (Q, D, I), MIXED),               \
13056   X(3, (Q, D, D), MIXED),               \
13057   X(3, (D, Q, Q), MIXED),               \
13058   X(3, (Q, Q, D), MIXED),               \
13059   X(3, (Q, D, S), MIXED),               \
13060   X(3, (D, Q, S), MIXED),               \
13061   X(4, (D, D, D, I), DOUBLE),           \
13062   X(4, (Q, Q, Q, I), QUAD),             \
13063   X(2, (F, F), SINGLE),                 \
13064   X(3, (F, F, F), SINGLE),              \
13065   X(2, (F, I), SINGLE),                 \
13066   X(2, (F, D), MIXED),                  \
13067   X(2, (D, F), MIXED),                  \
13068   X(3, (F, F, I), MIXED),               \
13069   X(4, (R, R, F, F), SINGLE),           \
13070   X(4, (F, F, R, R), SINGLE),           \
13071   X(3, (D, R, R), DOUBLE),              \
13072   X(3, (R, R, D), DOUBLE),              \
13073   X(2, (S, R), SINGLE),                 \
13074   X(2, (R, S), SINGLE),                 \
13075   X(2, (F, R), SINGLE),                 \
13076   X(2, (R, F), SINGLE)
13077
13078 #define S2(A,B)         NS_##A##B
13079 #define S3(A,B,C)       NS_##A##B##C
13080 #define S4(A,B,C,D)     NS_##A##B##C##D
13081
13082 #define X(N, L, C) S##N L
13083
13084 enum neon_shape
13085 {
13086   NEON_SHAPE_DEF,
13087   NS_NULL
13088 };
13089
13090 #undef X
13091 #undef S2
13092 #undef S3
13093 #undef S4
13094
13095 enum neon_shape_class
13096 {
13097   SC_SINGLE,
13098   SC_DOUBLE,
13099   SC_QUAD,
13100   SC_MIXED
13101 };
13102
13103 #define X(N, L, C) SC_##C
13104
13105 static enum neon_shape_class neon_shape_class[] =
13106 {
13107   NEON_SHAPE_DEF
13108 };
13109
13110 #undef X
13111
13112 enum neon_shape_el
13113 {
13114   SE_F,
13115   SE_D,
13116   SE_Q,
13117   SE_I,
13118   SE_S,
13119   SE_R,
13120   SE_L
13121 };
13122
13123 /* Register widths of above.  */
13124 static unsigned neon_shape_el_size[] =
13125 {
13126   32,
13127   64,
13128   128,
13129   0,
13130   32,
13131   32,
13132   0
13133 };
13134
13135 struct neon_shape_info
13136 {
13137   unsigned els;
13138   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
13139 };
13140
13141 #define S2(A,B)         { SE_##A, SE_##B }
13142 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
13143 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
13144
13145 #define X(N, L, C) { N, S##N L }
13146
13147 static struct neon_shape_info neon_shape_tab[] =
13148 {
13149   NEON_SHAPE_DEF
13150 };
13151
13152 #undef X
13153 #undef S2
13154 #undef S3
13155 #undef S4
13156
13157 /* Bit masks used in type checking given instructions.
13158   'N_EQK' means the type must be the same as (or based on in some way) the key
13159    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
13160    set, various other bits can be set as well in order to modify the meaning of
13161    the type constraint.  */
13162
13163 enum neon_type_mask
13164 {
13165   N_S8   = 0x0000001,
13166   N_S16  = 0x0000002,
13167   N_S32  = 0x0000004,
13168   N_S64  = 0x0000008,
13169   N_U8   = 0x0000010,
13170   N_U16  = 0x0000020,
13171   N_U32  = 0x0000040,
13172   N_U64  = 0x0000080,
13173   N_I8   = 0x0000100,
13174   N_I16  = 0x0000200,
13175   N_I32  = 0x0000400,
13176   N_I64  = 0x0000800,
13177   N_8    = 0x0001000,
13178   N_16   = 0x0002000,
13179   N_32   = 0x0004000,
13180   N_64   = 0x0008000,
13181   N_P8   = 0x0010000,
13182   N_P16  = 0x0020000,
13183   N_F16  = 0x0040000,
13184   N_F32  = 0x0080000,
13185   N_F64  = 0x0100000,
13186   N_P64  = 0x0200000,
13187   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
13188   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
13189   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
13190   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
13191   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
13192   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
13193   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
13194   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
13195   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
13196   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
13197   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
13198   N_UTYP = 0,
13199   N_MAX_NONSPECIAL = N_P64
13200 };
13201
13202 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
13203
13204 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
13205 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
13206 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
13207 #define N_SUF_32   (N_SU_32 | N_F32)
13208 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
13209 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F32)
13210
13211 /* Pass this as the first type argument to neon_check_type to ignore types
13212    altogether.  */
13213 #define N_IGNORE_TYPE (N_KEY | N_EQK)
13214
13215 /* Select a "shape" for the current instruction (describing register types or
13216    sizes) from a list of alternatives. Return NS_NULL if the current instruction
13217    doesn't fit. For non-polymorphic shapes, checking is usually done as a
13218    function of operand parsing, so this function doesn't need to be called.
13219    Shapes should be listed in order of decreasing length.  */
13220
13221 static enum neon_shape
13222 neon_select_shape (enum neon_shape shape, ...)
13223 {
13224   va_list ap;
13225   enum neon_shape first_shape = shape;
13226
13227   /* Fix missing optional operands. FIXME: we don't know at this point how
13228      many arguments we should have, so this makes the assumption that we have
13229      > 1. This is true of all current Neon opcodes, I think, but may not be
13230      true in the future.  */
13231   if (!inst.operands[1].present)
13232     inst.operands[1] = inst.operands[0];
13233
13234   va_start (ap, shape);
13235
13236   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
13237     {
13238       unsigned j;
13239       int matches = 1;
13240
13241       for (j = 0; j < neon_shape_tab[shape].els; j++)
13242         {
13243           if (!inst.operands[j].present)
13244             {
13245               matches = 0;
13246               break;
13247             }
13248
13249           switch (neon_shape_tab[shape].el[j])
13250             {
13251             case SE_F:
13252               if (!(inst.operands[j].isreg
13253                     && inst.operands[j].isvec
13254                     && inst.operands[j].issingle
13255                     && !inst.operands[j].isquad))
13256                 matches = 0;
13257               break;
13258
13259             case SE_D:
13260               if (!(inst.operands[j].isreg
13261                     && inst.operands[j].isvec
13262                     && !inst.operands[j].isquad
13263                     && !inst.operands[j].issingle))
13264                 matches = 0;
13265               break;
13266
13267             case SE_R:
13268               if (!(inst.operands[j].isreg
13269                     && !inst.operands[j].isvec))
13270                 matches = 0;
13271               break;
13272
13273             case SE_Q:
13274               if (!(inst.operands[j].isreg
13275                     && inst.operands[j].isvec
13276                     && inst.operands[j].isquad
13277                     && !inst.operands[j].issingle))
13278                 matches = 0;
13279               break;
13280
13281             case SE_I:
13282               if (!(!inst.operands[j].isreg
13283                     && !inst.operands[j].isscalar))
13284                 matches = 0;
13285               break;
13286
13287             case SE_S:
13288               if (!(!inst.operands[j].isreg
13289                     && inst.operands[j].isscalar))
13290                 matches = 0;
13291               break;
13292
13293             case SE_L:
13294               break;
13295             }
13296           if (!matches)
13297             break;
13298         }
13299       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
13300         /* We've matched all the entries in the shape table, and we don't
13301            have any left over operands which have not been matched.  */
13302         break;
13303     }
13304
13305   va_end (ap);
13306
13307   if (shape == NS_NULL && first_shape != NS_NULL)
13308     first_error (_("invalid instruction shape"));
13309
13310   return shape;
13311 }
13312
13313 /* True if SHAPE is predominantly a quadword operation (most of the time, this
13314    means the Q bit should be set).  */
13315
13316 static int
13317 neon_quad (enum neon_shape shape)
13318 {
13319   return neon_shape_class[shape] == SC_QUAD;
13320 }
13321
13322 static void
13323 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
13324                        unsigned *g_size)
13325 {
13326   /* Allow modification to be made to types which are constrained to be
13327      based on the key element, based on bits set alongside N_EQK.  */
13328   if ((typebits & N_EQK) != 0)
13329     {
13330       if ((typebits & N_HLF) != 0)
13331         *g_size /= 2;
13332       else if ((typebits & N_DBL) != 0)
13333         *g_size *= 2;
13334       if ((typebits & N_SGN) != 0)
13335         *g_type = NT_signed;
13336       else if ((typebits & N_UNS) != 0)
13337         *g_type = NT_unsigned;
13338       else if ((typebits & N_INT) != 0)
13339         *g_type = NT_integer;
13340       else if ((typebits & N_FLT) != 0)
13341         *g_type = NT_float;
13342       else if ((typebits & N_SIZ) != 0)
13343         *g_type = NT_untyped;
13344     }
13345 }
13346
13347 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
13348    operand type, i.e. the single type specified in a Neon instruction when it
13349    is the only one given.  */
13350
13351 static struct neon_type_el
13352 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
13353 {
13354   struct neon_type_el dest = *key;
13355
13356   gas_assert ((thisarg & N_EQK) != 0);
13357
13358   neon_modify_type_size (thisarg, &dest.type, &dest.size);
13359
13360   return dest;
13361 }
13362
13363 /* Convert Neon type and size into compact bitmask representation.  */
13364
13365 static enum neon_type_mask
13366 type_chk_of_el_type (enum neon_el_type type, unsigned size)
13367 {
13368   switch (type)
13369     {
13370     case NT_untyped:
13371       switch (size)
13372         {
13373         case 8:  return N_8;
13374         case 16: return N_16;
13375         case 32: return N_32;
13376         case 64: return N_64;
13377         default: ;
13378         }
13379       break;
13380
13381     case NT_integer:
13382       switch (size)
13383         {
13384         case 8:  return N_I8;
13385         case 16: return N_I16;
13386         case 32: return N_I32;
13387         case 64: return N_I64;
13388         default: ;
13389         }
13390       break;
13391
13392     case NT_float:
13393       switch (size)
13394         {
13395         case 16: return N_F16;
13396         case 32: return N_F32;
13397         case 64: return N_F64;
13398         default: ;
13399         }
13400       break;
13401
13402     case NT_poly:
13403       switch (size)
13404         {
13405         case 8:  return N_P8;
13406         case 16: return N_P16;
13407         case 64: return N_P64;
13408         default: ;
13409         }
13410       break;
13411
13412     case NT_signed:
13413       switch (size)
13414         {
13415         case 8:  return N_S8;
13416         case 16: return N_S16;
13417         case 32: return N_S32;
13418         case 64: return N_S64;
13419         default: ;
13420         }
13421       break;
13422
13423     case NT_unsigned:
13424       switch (size)
13425         {
13426         case 8:  return N_U8;
13427         case 16: return N_U16;
13428         case 32: return N_U32;
13429         case 64: return N_U64;
13430         default: ;
13431         }
13432       break;
13433
13434     default: ;
13435     }
13436
13437   return N_UTYP;
13438 }
13439
13440 /* Convert compact Neon bitmask type representation to a type and size. Only
13441    handles the case where a single bit is set in the mask.  */
13442
13443 static int
13444 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
13445                      enum neon_type_mask mask)
13446 {
13447   if ((mask & N_EQK) != 0)
13448     return FAIL;
13449
13450   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
13451     *size = 8;
13452   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
13453     *size = 16;
13454   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
13455     *size = 32;
13456   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
13457     *size = 64;
13458   else
13459     return FAIL;
13460
13461   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
13462     *type = NT_signed;
13463   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
13464     *type = NT_unsigned;
13465   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
13466     *type = NT_integer;
13467   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
13468     *type = NT_untyped;
13469   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
13470     *type = NT_poly;
13471   else if ((mask & (N_F16 | N_F32 | N_F64)) != 0)
13472     *type = NT_float;
13473   else
13474     return FAIL;
13475
13476   return SUCCESS;
13477 }
13478
13479 /* Modify a bitmask of allowed types. This is only needed for type
13480    relaxation.  */
13481
13482 static unsigned
13483 modify_types_allowed (unsigned allowed, unsigned mods)
13484 {
13485   unsigned size;
13486   enum neon_el_type type;
13487   unsigned destmask;
13488   int i;
13489
13490   destmask = 0;
13491
13492   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
13493     {
13494       if (el_type_of_type_chk (&type, &size,
13495                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
13496         {
13497           neon_modify_type_size (mods, &type, &size);
13498           destmask |= type_chk_of_el_type (type, size);
13499         }
13500     }
13501
13502   return destmask;
13503 }
13504
13505 /* Check type and return type classification.
13506    The manual states (paraphrase): If one datatype is given, it indicates the
13507    type given in:
13508     - the second operand, if there is one
13509     - the operand, if there is no second operand
13510     - the result, if there are no operands.
13511    This isn't quite good enough though, so we use a concept of a "key" datatype
13512    which is set on a per-instruction basis, which is the one which matters when
13513    only one data type is written.
13514    Note: this function has side-effects (e.g. filling in missing operands). All
13515    Neon instructions should call it before performing bit encoding.  */
13516
13517 static struct neon_type_el
13518 neon_check_type (unsigned els, enum neon_shape ns, ...)
13519 {
13520   va_list ap;
13521   unsigned i, pass, key_el = 0;
13522   unsigned types[NEON_MAX_TYPE_ELS];
13523   enum neon_el_type k_type = NT_invtype;
13524   unsigned k_size = -1u;
13525   struct neon_type_el badtype = {NT_invtype, -1};
13526   unsigned key_allowed = 0;
13527
13528   /* Optional registers in Neon instructions are always (not) in operand 1.
13529      Fill in the missing operand here, if it was omitted.  */
13530   if (els > 1 && !inst.operands[1].present)
13531     inst.operands[1] = inst.operands[0];
13532
13533   /* Suck up all the varargs.  */
13534   va_start (ap, ns);
13535   for (i = 0; i < els; i++)
13536     {
13537       unsigned thisarg = va_arg (ap, unsigned);
13538       if (thisarg == N_IGNORE_TYPE)
13539         {
13540           va_end (ap);
13541           return badtype;
13542         }
13543       types[i] = thisarg;
13544       if ((thisarg & N_KEY) != 0)
13545         key_el = i;
13546     }
13547   va_end (ap);
13548
13549   if (inst.vectype.elems > 0)
13550     for (i = 0; i < els; i++)
13551       if (inst.operands[i].vectype.type != NT_invtype)
13552         {
13553           first_error (_("types specified in both the mnemonic and operands"));
13554           return badtype;
13555         }
13556
13557   /* Duplicate inst.vectype elements here as necessary.
13558      FIXME: No idea if this is exactly the same as the ARM assembler,
13559      particularly when an insn takes one register and one non-register
13560      operand. */
13561   if (inst.vectype.elems == 1 && els > 1)
13562     {
13563       unsigned j;
13564       inst.vectype.elems = els;
13565       inst.vectype.el[key_el] = inst.vectype.el[0];
13566       for (j = 0; j < els; j++)
13567         if (j != key_el)
13568           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
13569                                                   types[j]);
13570     }
13571   else if (inst.vectype.elems == 0 && els > 0)
13572     {
13573       unsigned j;
13574       /* No types were given after the mnemonic, so look for types specified
13575          after each operand. We allow some flexibility here; as long as the
13576          "key" operand has a type, we can infer the others.  */
13577       for (j = 0; j < els; j++)
13578         if (inst.operands[j].vectype.type != NT_invtype)
13579           inst.vectype.el[j] = inst.operands[j].vectype;
13580
13581       if (inst.operands[key_el].vectype.type != NT_invtype)
13582         {
13583           for (j = 0; j < els; j++)
13584             if (inst.operands[j].vectype.type == NT_invtype)
13585               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
13586                                                       types[j]);
13587         }
13588       else
13589         {
13590           first_error (_("operand types can't be inferred"));
13591           return badtype;
13592         }
13593     }
13594   else if (inst.vectype.elems != els)
13595     {
13596       first_error (_("type specifier has the wrong number of parts"));
13597       return badtype;
13598     }
13599
13600   for (pass = 0; pass < 2; pass++)
13601     {
13602       for (i = 0; i < els; i++)
13603         {
13604           unsigned thisarg = types[i];
13605           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
13606             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
13607           enum neon_el_type g_type = inst.vectype.el[i].type;
13608           unsigned g_size = inst.vectype.el[i].size;
13609
13610           /* Decay more-specific signed & unsigned types to sign-insensitive
13611              integer types if sign-specific variants are unavailable.  */
13612           if ((g_type == NT_signed || g_type == NT_unsigned)
13613               && (types_allowed & N_SU_ALL) == 0)
13614             g_type = NT_integer;
13615
13616           /* If only untyped args are allowed, decay any more specific types to
13617              them. Some instructions only care about signs for some element
13618              sizes, so handle that properly.  */
13619           if (((types_allowed & N_UNT) == 0)
13620               && ((g_size == 8 && (types_allowed & N_8) != 0)
13621                   || (g_size == 16 && (types_allowed & N_16) != 0)
13622                   || (g_size == 32 && (types_allowed & N_32) != 0)
13623                   || (g_size == 64 && (types_allowed & N_64) != 0)))
13624             g_type = NT_untyped;
13625
13626           if (pass == 0)
13627             {
13628               if ((thisarg & N_KEY) != 0)
13629                 {
13630                   k_type = g_type;
13631                   k_size = g_size;
13632                   key_allowed = thisarg & ~N_KEY;
13633                 }
13634             }
13635           else
13636             {
13637               if ((thisarg & N_VFP) != 0)
13638                 {
13639                   enum neon_shape_el regshape;
13640                   unsigned regwidth, match;
13641
13642                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
13643                   if (ns == NS_NULL)
13644                     {
13645                       first_error (_("invalid instruction shape"));
13646                       return badtype;
13647                     }
13648                   regshape = neon_shape_tab[ns].el[i];
13649                   regwidth = neon_shape_el_size[regshape];
13650
13651                   /* In VFP mode, operands must match register widths. If we
13652                      have a key operand, use its width, else use the width of
13653                      the current operand.  */
13654                   if (k_size != -1u)
13655                     match = k_size;
13656                   else
13657                     match = g_size;
13658
13659                   if (regwidth != match)
13660                     {
13661                       first_error (_("operand size must match register width"));
13662                       return badtype;
13663                     }
13664                 }
13665
13666               if ((thisarg & N_EQK) == 0)
13667                 {
13668                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
13669
13670                   if ((given_type & types_allowed) == 0)
13671                     {
13672                       first_error (_("bad type in Neon instruction"));
13673                       return badtype;
13674                     }
13675                 }
13676               else
13677                 {
13678                   enum neon_el_type mod_k_type = k_type;
13679                   unsigned mod_k_size = k_size;
13680                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
13681                   if (g_type != mod_k_type || g_size != mod_k_size)
13682                     {
13683                       first_error (_("inconsistent types in Neon instruction"));
13684                       return badtype;
13685                     }
13686                 }
13687             }
13688         }
13689     }
13690
13691   return inst.vectype.el[key_el];
13692 }
13693
13694 /* Neon-style VFP instruction forwarding.  */
13695
13696 /* Thumb VFP instructions have 0xE in the condition field.  */
13697
13698 static void
13699 do_vfp_cond_or_thumb (void)
13700 {
13701   inst.is_neon = 1;
13702
13703   if (thumb_mode)
13704     inst.instruction |= 0xe0000000;
13705   else
13706     inst.instruction |= inst.cond << 28;
13707 }
13708
13709 /* Look up and encode a simple mnemonic, for use as a helper function for the
13710    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
13711    etc.  It is assumed that operand parsing has already been done, and that the
13712    operands are in the form expected by the given opcode (this isn't necessarily
13713    the same as the form in which they were parsed, hence some massaging must
13714    take place before this function is called).
13715    Checks current arch version against that in the looked-up opcode.  */
13716
13717 static void
13718 do_vfp_nsyn_opcode (const char *opname)
13719 {
13720   const struct asm_opcode *opcode;
13721
13722   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
13723
13724   if (!opcode)
13725     abort ();
13726
13727   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
13728                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
13729               _(BAD_FPU));
13730
13731   inst.is_neon = 1;
13732
13733   if (thumb_mode)
13734     {
13735       inst.instruction = opcode->tvalue;
13736       opcode->tencode ();
13737     }
13738   else
13739     {
13740       inst.instruction = (inst.cond << 28) | opcode->avalue;
13741       opcode->aencode ();
13742     }
13743 }
13744
13745 static void
13746 do_vfp_nsyn_add_sub (enum neon_shape rs)
13747 {
13748   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
13749
13750   if (rs == NS_FFF)
13751     {
13752       if (is_add)
13753         do_vfp_nsyn_opcode ("fadds");
13754       else
13755         do_vfp_nsyn_opcode ("fsubs");
13756     }
13757   else
13758     {
13759       if (is_add)
13760         do_vfp_nsyn_opcode ("faddd");
13761       else
13762         do_vfp_nsyn_opcode ("fsubd");
13763     }
13764 }
13765
13766 /* Check operand types to see if this is a VFP instruction, and if so call
13767    PFN ().  */
13768
13769 static int
13770 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
13771 {
13772   enum neon_shape rs;
13773   struct neon_type_el et;
13774
13775   switch (args)
13776     {
13777     case 2:
13778       rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
13779       et = neon_check_type (2, rs,
13780         N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13781       break;
13782
13783     case 3:
13784       rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
13785       et = neon_check_type (3, rs,
13786         N_EQK | N_VFP, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13787       break;
13788
13789     default:
13790       abort ();
13791     }
13792
13793   if (et.type != NT_invtype)
13794     {
13795       pfn (rs);
13796       return SUCCESS;
13797     }
13798
13799   inst.error = NULL;
13800   return FAIL;
13801 }
13802
13803 static void
13804 do_vfp_nsyn_mla_mls (enum neon_shape rs)
13805 {
13806   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
13807
13808   if (rs == NS_FFF)
13809     {
13810       if (is_mla)
13811         do_vfp_nsyn_opcode ("fmacs");
13812       else
13813         do_vfp_nsyn_opcode ("fnmacs");
13814     }
13815   else
13816     {
13817       if (is_mla)
13818         do_vfp_nsyn_opcode ("fmacd");
13819       else
13820         do_vfp_nsyn_opcode ("fnmacd");
13821     }
13822 }
13823
13824 static void
13825 do_vfp_nsyn_fma_fms (enum neon_shape rs)
13826 {
13827   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
13828
13829   if (rs == NS_FFF)
13830     {
13831       if (is_fma)
13832         do_vfp_nsyn_opcode ("ffmas");
13833       else
13834         do_vfp_nsyn_opcode ("ffnmas");
13835     }
13836   else
13837     {
13838       if (is_fma)
13839         do_vfp_nsyn_opcode ("ffmad");
13840       else
13841         do_vfp_nsyn_opcode ("ffnmad");
13842     }
13843 }
13844
13845 static void
13846 do_vfp_nsyn_mul (enum neon_shape rs)
13847 {
13848   if (rs == NS_FFF)
13849     do_vfp_nsyn_opcode ("fmuls");
13850   else
13851     do_vfp_nsyn_opcode ("fmuld");
13852 }
13853
13854 static void
13855 do_vfp_nsyn_abs_neg (enum neon_shape rs)
13856 {
13857   int is_neg = (inst.instruction & 0x80) != 0;
13858   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_VFP | N_KEY);
13859
13860   if (rs == NS_FF)
13861     {
13862       if (is_neg)
13863         do_vfp_nsyn_opcode ("fnegs");
13864       else
13865         do_vfp_nsyn_opcode ("fabss");
13866     }
13867   else
13868     {
13869       if (is_neg)
13870         do_vfp_nsyn_opcode ("fnegd");
13871       else
13872         do_vfp_nsyn_opcode ("fabsd");
13873     }
13874 }
13875
13876 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
13877    insns belong to Neon, and are handled elsewhere.  */
13878
13879 static void
13880 do_vfp_nsyn_ldm_stm (int is_dbmode)
13881 {
13882   int is_ldm = (inst.instruction & (1 << 20)) != 0;
13883   if (is_ldm)
13884     {
13885       if (is_dbmode)
13886         do_vfp_nsyn_opcode ("fldmdbs");
13887       else
13888         do_vfp_nsyn_opcode ("fldmias");
13889     }
13890   else
13891     {
13892       if (is_dbmode)
13893         do_vfp_nsyn_opcode ("fstmdbs");
13894       else
13895         do_vfp_nsyn_opcode ("fstmias");
13896     }
13897 }
13898
13899 static void
13900 do_vfp_nsyn_sqrt (void)
13901 {
13902   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
13903   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13904
13905   if (rs == NS_FF)
13906     do_vfp_nsyn_opcode ("fsqrts");
13907   else
13908     do_vfp_nsyn_opcode ("fsqrtd");
13909 }
13910
13911 static void
13912 do_vfp_nsyn_div (void)
13913 {
13914   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
13915   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
13916     N_F32 | N_F64 | N_KEY | N_VFP);
13917
13918   if (rs == NS_FFF)
13919     do_vfp_nsyn_opcode ("fdivs");
13920   else
13921     do_vfp_nsyn_opcode ("fdivd");
13922 }
13923
13924 static void
13925 do_vfp_nsyn_nmul (void)
13926 {
13927   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
13928   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
13929     N_F32 | N_F64 | N_KEY | N_VFP);
13930
13931   if (rs == NS_FFF)
13932     {
13933       NEON_ENCODE (SINGLE, inst);
13934       do_vfp_sp_dyadic ();
13935     }
13936   else
13937     {
13938       NEON_ENCODE (DOUBLE, inst);
13939       do_vfp_dp_rd_rn_rm ();
13940     }
13941   do_vfp_cond_or_thumb ();
13942 }
13943
13944 static void
13945 do_vfp_nsyn_cmp (void)
13946 {
13947   if (inst.operands[1].isreg)
13948     {
13949       enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
13950       neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13951
13952       if (rs == NS_FF)
13953         {
13954           NEON_ENCODE (SINGLE, inst);
13955           do_vfp_sp_monadic ();
13956         }
13957       else
13958         {
13959           NEON_ENCODE (DOUBLE, inst);
13960           do_vfp_dp_rd_rm ();
13961         }
13962     }
13963   else
13964     {
13965       enum neon_shape rs = neon_select_shape (NS_FI, NS_DI, NS_NULL);
13966       neon_check_type (2, rs, N_F32 | N_F64 | N_KEY | N_VFP, N_EQK);
13967
13968       switch (inst.instruction & 0x0fffffff)
13969         {
13970         case N_MNEM_vcmp:
13971           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
13972           break;
13973         case N_MNEM_vcmpe:
13974           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
13975           break;
13976         default:
13977           abort ();
13978         }
13979
13980       if (rs == NS_FI)
13981         {
13982           NEON_ENCODE (SINGLE, inst);
13983           do_vfp_sp_compare_z ();
13984         }
13985       else
13986         {
13987           NEON_ENCODE (DOUBLE, inst);
13988           do_vfp_dp_rd ();
13989         }
13990     }
13991   do_vfp_cond_or_thumb ();
13992 }
13993
13994 static void
13995 nsyn_insert_sp (void)
13996 {
13997   inst.operands[1] = inst.operands[0];
13998   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
13999   inst.operands[0].reg = REG_SP;
14000   inst.operands[0].isreg = 1;
14001   inst.operands[0].writeback = 1;
14002   inst.operands[0].present = 1;
14003 }
14004
14005 static void
14006 do_vfp_nsyn_push (void)
14007 {
14008   nsyn_insert_sp ();
14009   if (inst.operands[1].issingle)
14010     do_vfp_nsyn_opcode ("fstmdbs");
14011   else
14012     do_vfp_nsyn_opcode ("fstmdbd");
14013 }
14014
14015 static void
14016 do_vfp_nsyn_pop (void)
14017 {
14018   nsyn_insert_sp ();
14019   if (inst.operands[1].issingle)
14020     do_vfp_nsyn_opcode ("fldmias");
14021   else
14022     do_vfp_nsyn_opcode ("fldmiad");
14023 }
14024
14025 /* Fix up Neon data-processing instructions, ORing in the correct bits for
14026    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
14027
14028 static void
14029 neon_dp_fixup (struct arm_it* insn)
14030 {
14031   unsigned int i = insn->instruction;
14032   insn->is_neon = 1;
14033
14034   if (thumb_mode)
14035     {
14036       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
14037       if (i & (1 << 24))
14038         i |= 1 << 28;
14039
14040       i &= ~(1 << 24);
14041
14042       i |= 0xef000000;
14043     }
14044   else
14045     i |= 0xf2000000;
14046
14047   insn->instruction = i;
14048 }
14049
14050 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
14051    (0, 1, 2, 3).  */
14052
14053 static unsigned
14054 neon_logbits (unsigned x)
14055 {
14056   return ffs (x) - 4;
14057 }
14058
14059 #define LOW4(R) ((R) & 0xf)
14060 #define HI1(R) (((R) >> 4) & 1)
14061
14062 /* Encode insns with bit pattern:
14063
14064   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
14065   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
14066
14067   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
14068   different meaning for some instruction.  */
14069
14070 static void
14071 neon_three_same (int isquad, int ubit, int size)
14072 {
14073   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14074   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14075   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14076   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14077   inst.instruction |= LOW4 (inst.operands[2].reg);
14078   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14079   inst.instruction |= (isquad != 0) << 6;
14080   inst.instruction |= (ubit != 0) << 24;
14081   if (size != -1)
14082     inst.instruction |= neon_logbits (size) << 20;
14083
14084   neon_dp_fixup (&inst);
14085 }
14086
14087 /* Encode instructions of the form:
14088
14089   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
14090   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
14091
14092   Don't write size if SIZE == -1.  */
14093
14094 static void
14095 neon_two_same (int qbit, int ubit, int size)
14096 {
14097   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14098   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14099   inst.instruction |= LOW4 (inst.operands[1].reg);
14100   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14101   inst.instruction |= (qbit != 0) << 6;
14102   inst.instruction |= (ubit != 0) << 24;
14103
14104   if (size != -1)
14105     inst.instruction |= neon_logbits (size) << 18;
14106
14107   neon_dp_fixup (&inst);
14108 }
14109
14110 /* Neon instruction encoders, in approximate order of appearance.  */
14111
14112 static void
14113 do_neon_dyadic_i_su (void)
14114 {
14115   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14116   struct neon_type_el et = neon_check_type (3, rs,
14117     N_EQK, N_EQK, N_SU_32 | N_KEY);
14118   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14119 }
14120
14121 static void
14122 do_neon_dyadic_i64_su (void)
14123 {
14124   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14125   struct neon_type_el et = neon_check_type (3, rs,
14126     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14127   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14128 }
14129
14130 static void
14131 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
14132                 unsigned immbits)
14133 {
14134   unsigned size = et.size >> 3;
14135   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14136   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14137   inst.instruction |= LOW4 (inst.operands[1].reg);
14138   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14139   inst.instruction |= (isquad != 0) << 6;
14140   inst.instruction |= immbits << 16;
14141   inst.instruction |= (size >> 3) << 7;
14142   inst.instruction |= (size & 0x7) << 19;
14143   if (write_ubit)
14144     inst.instruction |= (uval != 0) << 24;
14145
14146   neon_dp_fixup (&inst);
14147 }
14148
14149 static void
14150 do_neon_shl_imm (void)
14151 {
14152   if (!inst.operands[2].isreg)
14153     {
14154       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14155       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
14156       int imm = inst.operands[2].imm;
14157
14158       constraint (imm < 0 || (unsigned)imm >= et.size,
14159                   _("immediate out of range for shift"));
14160       NEON_ENCODE (IMMED, inst);
14161       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14162     }
14163   else
14164     {
14165       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14166       struct neon_type_el et = neon_check_type (3, rs,
14167         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14168       unsigned int tmp;
14169
14170       /* VSHL/VQSHL 3-register variants have syntax such as:
14171            vshl.xx Dd, Dm, Dn
14172          whereas other 3-register operations encoded by neon_three_same have
14173          syntax like:
14174            vadd.xx Dd, Dn, Dm
14175          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
14176          here.  */
14177       tmp = inst.operands[2].reg;
14178       inst.operands[2].reg = inst.operands[1].reg;
14179       inst.operands[1].reg = tmp;
14180       NEON_ENCODE (INTEGER, inst);
14181       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14182     }
14183 }
14184
14185 static void
14186 do_neon_qshl_imm (void)
14187 {
14188   if (!inst.operands[2].isreg)
14189     {
14190       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14191       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
14192       int imm = inst.operands[2].imm;
14193
14194       constraint (imm < 0 || (unsigned)imm >= et.size,
14195                   _("immediate out of range for shift"));
14196       NEON_ENCODE (IMMED, inst);
14197       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
14198     }
14199   else
14200     {
14201       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14202       struct neon_type_el et = neon_check_type (3, rs,
14203         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14204       unsigned int tmp;
14205
14206       /* See note in do_neon_shl_imm.  */
14207       tmp = inst.operands[2].reg;
14208       inst.operands[2].reg = inst.operands[1].reg;
14209       inst.operands[1].reg = tmp;
14210       NEON_ENCODE (INTEGER, inst);
14211       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14212     }
14213 }
14214
14215 static void
14216 do_neon_rshl (void)
14217 {
14218   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14219   struct neon_type_el et = neon_check_type (3, rs,
14220     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14221   unsigned int tmp;
14222
14223   tmp = inst.operands[2].reg;
14224   inst.operands[2].reg = inst.operands[1].reg;
14225   inst.operands[1].reg = tmp;
14226   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14227 }
14228
14229 static int
14230 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
14231 {
14232   /* Handle .I8 pseudo-instructions.  */
14233   if (size == 8)
14234     {
14235       /* Unfortunately, this will make everything apart from zero out-of-range.
14236          FIXME is this the intended semantics? There doesn't seem much point in
14237          accepting .I8 if so.  */
14238       immediate |= immediate << 8;
14239       size = 16;
14240     }
14241
14242   if (size >= 32)
14243     {
14244       if (immediate == (immediate & 0x000000ff))
14245         {
14246           *immbits = immediate;
14247           return 0x1;
14248         }
14249       else if (immediate == (immediate & 0x0000ff00))
14250         {
14251           *immbits = immediate >> 8;
14252           return 0x3;
14253         }
14254       else if (immediate == (immediate & 0x00ff0000))
14255         {
14256           *immbits = immediate >> 16;
14257           return 0x5;
14258         }
14259       else if (immediate == (immediate & 0xff000000))
14260         {
14261           *immbits = immediate >> 24;
14262           return 0x7;
14263         }
14264       if ((immediate & 0xffff) != (immediate >> 16))
14265         goto bad_immediate;
14266       immediate &= 0xffff;
14267     }
14268
14269   if (immediate == (immediate & 0x000000ff))
14270     {
14271       *immbits = immediate;
14272       return 0x9;
14273     }
14274   else if (immediate == (immediate & 0x0000ff00))
14275     {
14276       *immbits = immediate >> 8;
14277       return 0xb;
14278     }
14279
14280   bad_immediate:
14281   first_error (_("immediate value out of range"));
14282   return FAIL;
14283 }
14284
14285 static void
14286 do_neon_logic (void)
14287 {
14288   if (inst.operands[2].present && inst.operands[2].isreg)
14289     {
14290       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14291       neon_check_type (3, rs, N_IGNORE_TYPE);
14292       /* U bit and size field were set as part of the bitmask.  */
14293       NEON_ENCODE (INTEGER, inst);
14294       neon_three_same (neon_quad (rs), 0, -1);
14295     }
14296   else
14297     {
14298       const int three_ops_form = (inst.operands[2].present
14299                                   && !inst.operands[2].isreg);
14300       const int immoperand = (three_ops_form ? 2 : 1);
14301       enum neon_shape rs = (three_ops_form
14302                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
14303                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
14304       struct neon_type_el et = neon_check_type (2, rs,
14305         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14306       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
14307       unsigned immbits;
14308       int cmode;
14309
14310       if (et.type == NT_invtype)
14311         return;
14312
14313       if (three_ops_form)
14314         constraint (inst.operands[0].reg != inst.operands[1].reg,
14315                     _("first and second operands shall be the same register"));
14316
14317       NEON_ENCODE (IMMED, inst);
14318
14319       immbits = inst.operands[immoperand].imm;
14320       if (et.size == 64)
14321         {
14322           /* .i64 is a pseudo-op, so the immediate must be a repeating
14323              pattern.  */
14324           if (immbits != (inst.operands[immoperand].regisimm ?
14325                           inst.operands[immoperand].reg : 0))
14326             {
14327               /* Set immbits to an invalid constant.  */
14328               immbits = 0xdeadbeef;
14329             }
14330         }
14331
14332       switch (opcode)
14333         {
14334         case N_MNEM_vbic:
14335           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14336           break;
14337
14338         case N_MNEM_vorr:
14339           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14340           break;
14341
14342         case N_MNEM_vand:
14343           /* Pseudo-instruction for VBIC.  */
14344           neon_invert_size (&immbits, 0, et.size);
14345           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14346           break;
14347
14348         case N_MNEM_vorn:
14349           /* Pseudo-instruction for VORR.  */
14350           neon_invert_size (&immbits, 0, et.size);
14351           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14352           break;
14353
14354         default:
14355           abort ();
14356         }
14357
14358       if (cmode == FAIL)
14359         return;
14360
14361       inst.instruction |= neon_quad (rs) << 6;
14362       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14363       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14364       inst.instruction |= cmode << 8;
14365       neon_write_immbits (immbits);
14366
14367       neon_dp_fixup (&inst);
14368     }
14369 }
14370
14371 static void
14372 do_neon_bitfield (void)
14373 {
14374   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14375   neon_check_type (3, rs, N_IGNORE_TYPE);
14376   neon_three_same (neon_quad (rs), 0, -1);
14377 }
14378
14379 static void
14380 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
14381                   unsigned destbits)
14382 {
14383   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14384   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
14385                                             types | N_KEY);
14386   if (et.type == NT_float)
14387     {
14388       NEON_ENCODE (FLOAT, inst);
14389       neon_three_same (neon_quad (rs), 0, -1);
14390     }
14391   else
14392     {
14393       NEON_ENCODE (INTEGER, inst);
14394       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
14395     }
14396 }
14397
14398 static void
14399 do_neon_dyadic_if_su (void)
14400 {
14401   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14402 }
14403
14404 static void
14405 do_neon_dyadic_if_su_d (void)
14406 {
14407   /* This version only allow D registers, but that constraint is enforced during
14408      operand parsing so we don't need to do anything extra here.  */
14409   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14410 }
14411
14412 static void
14413 do_neon_dyadic_if_i_d (void)
14414 {
14415   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14416      affected if we specify unsigned args.  */
14417   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14418 }
14419
14420 enum vfp_or_neon_is_neon_bits
14421 {
14422   NEON_CHECK_CC = 1,
14423   NEON_CHECK_ARCH = 2,
14424   NEON_CHECK_ARCH8 = 4
14425 };
14426
14427 /* Call this function if an instruction which may have belonged to the VFP or
14428    Neon instruction sets, but turned out to be a Neon instruction (due to the
14429    operand types involved, etc.). We have to check and/or fix-up a couple of
14430    things:
14431
14432      - Make sure the user hasn't attempted to make a Neon instruction
14433        conditional.
14434      - Alter the value in the condition code field if necessary.
14435      - Make sure that the arch supports Neon instructions.
14436
14437    Which of these operations take place depends on bits from enum
14438    vfp_or_neon_is_neon_bits.
14439
14440    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
14441    current instruction's condition is COND_ALWAYS, the condition field is
14442    changed to inst.uncond_value. This is necessary because instructions shared
14443    between VFP and Neon may be conditional for the VFP variants only, and the
14444    unconditional Neon version must have, e.g., 0xF in the condition field.  */
14445
14446 static int
14447 vfp_or_neon_is_neon (unsigned check)
14448 {
14449   /* Conditions are always legal in Thumb mode (IT blocks).  */
14450   if (!thumb_mode && (check & NEON_CHECK_CC))
14451     {
14452       if (inst.cond != COND_ALWAYS)
14453         {
14454           first_error (_(BAD_COND));
14455           return FAIL;
14456         }
14457       if (inst.uncond_value != -1)
14458         inst.instruction |= inst.uncond_value << 28;
14459     }
14460
14461   if ((check & NEON_CHECK_ARCH)
14462       && !mark_feature_used (&fpu_neon_ext_v1))
14463     {
14464       first_error (_(BAD_FPU));
14465       return FAIL;
14466     }
14467
14468   if ((check & NEON_CHECK_ARCH8)
14469       && !mark_feature_used (&fpu_neon_ext_armv8))
14470     {
14471       first_error (_(BAD_FPU));
14472       return FAIL;
14473     }
14474
14475   return SUCCESS;
14476 }
14477
14478 static void
14479 do_neon_addsub_if_i (void)
14480 {
14481   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
14482     return;
14483
14484   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14485     return;
14486
14487   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14488      affected if we specify unsigned args.  */
14489   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
14490 }
14491
14492 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
14493    result to be:
14494      V<op> A,B     (A is operand 0, B is operand 2)
14495    to mean:
14496      V<op> A,B,A
14497    not:
14498      V<op> A,B,B
14499    so handle that case specially.  */
14500
14501 static void
14502 neon_exchange_operands (void)
14503 {
14504   void *scratch = alloca (sizeof (inst.operands[0]));
14505   if (inst.operands[1].present)
14506     {
14507       /* Swap operands[1] and operands[2].  */
14508       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
14509       inst.operands[1] = inst.operands[2];
14510       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
14511     }
14512   else
14513     {
14514       inst.operands[1] = inst.operands[2];
14515       inst.operands[2] = inst.operands[0];
14516     }
14517 }
14518
14519 static void
14520 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
14521 {
14522   if (inst.operands[2].isreg)
14523     {
14524       if (invert)
14525         neon_exchange_operands ();
14526       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
14527     }
14528   else
14529     {
14530       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14531       struct neon_type_el et = neon_check_type (2, rs,
14532         N_EQK | N_SIZ, immtypes | N_KEY);
14533
14534       NEON_ENCODE (IMMED, inst);
14535       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14536       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14537       inst.instruction |= LOW4 (inst.operands[1].reg);
14538       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14539       inst.instruction |= neon_quad (rs) << 6;
14540       inst.instruction |= (et.type == NT_float) << 10;
14541       inst.instruction |= neon_logbits (et.size) << 18;
14542
14543       neon_dp_fixup (&inst);
14544     }
14545 }
14546
14547 static void
14548 do_neon_cmp (void)
14549 {
14550   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, FALSE);
14551 }
14552
14553 static void
14554 do_neon_cmp_inv (void)
14555 {
14556   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, TRUE);
14557 }
14558
14559 static void
14560 do_neon_ceq (void)
14561 {
14562   neon_compare (N_IF_32, N_IF_32, FALSE);
14563 }
14564
14565 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
14566    scalars, which are encoded in 5 bits, M : Rm.
14567    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
14568    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
14569    index in M.  */
14570
14571 static unsigned
14572 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
14573 {
14574   unsigned regno = NEON_SCALAR_REG (scalar);
14575   unsigned elno = NEON_SCALAR_INDEX (scalar);
14576
14577   switch (elsize)
14578     {
14579     case 16:
14580       if (regno > 7 || elno > 3)
14581         goto bad_scalar;
14582       return regno | (elno << 3);
14583
14584     case 32:
14585       if (regno > 15 || elno > 1)
14586         goto bad_scalar;
14587       return regno | (elno << 4);
14588
14589     default:
14590     bad_scalar:
14591       first_error (_("scalar out of range for multiply instruction"));
14592     }
14593
14594   return 0;
14595 }
14596
14597 /* Encode multiply / multiply-accumulate scalar instructions.  */
14598
14599 static void
14600 neon_mul_mac (struct neon_type_el et, int ubit)
14601 {
14602   unsigned scalar;
14603
14604   /* Give a more helpful error message if we have an invalid type.  */
14605   if (et.type == NT_invtype)
14606     return;
14607
14608   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
14609   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14610   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14611   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14612   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14613   inst.instruction |= LOW4 (scalar);
14614   inst.instruction |= HI1 (scalar) << 5;
14615   inst.instruction |= (et.type == NT_float) << 8;
14616   inst.instruction |= neon_logbits (et.size) << 20;
14617   inst.instruction |= (ubit != 0) << 24;
14618
14619   neon_dp_fixup (&inst);
14620 }
14621
14622 static void
14623 do_neon_mac_maybe_scalar (void)
14624 {
14625   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
14626     return;
14627
14628   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14629     return;
14630
14631   if (inst.operands[2].isscalar)
14632     {
14633       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
14634       struct neon_type_el et = neon_check_type (3, rs,
14635         N_EQK, N_EQK, N_I16 | N_I32 | N_F32 | N_KEY);
14636       NEON_ENCODE (SCALAR, inst);
14637       neon_mul_mac (et, neon_quad (rs));
14638     }
14639   else
14640     {
14641       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
14642          affected if we specify unsigned args.  */
14643       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14644     }
14645 }
14646
14647 static void
14648 do_neon_fmac (void)
14649 {
14650   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
14651     return;
14652
14653   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14654     return;
14655
14656   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14657 }
14658
14659 static void
14660 do_neon_tst (void)
14661 {
14662   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14663   struct neon_type_el et = neon_check_type (3, rs,
14664     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
14665   neon_three_same (neon_quad (rs), 0, et.size);
14666 }
14667
14668 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
14669    same types as the MAC equivalents. The polynomial type for this instruction
14670    is encoded the same as the integer type.  */
14671
14672 static void
14673 do_neon_mul (void)
14674 {
14675   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
14676     return;
14677
14678   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14679     return;
14680
14681   if (inst.operands[2].isscalar)
14682     do_neon_mac_maybe_scalar ();
14683   else
14684     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F32 | N_P8, 0);
14685 }
14686
14687 static void
14688 do_neon_qdmulh (void)
14689 {
14690   if (inst.operands[2].isscalar)
14691     {
14692       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
14693       struct neon_type_el et = neon_check_type (3, rs,
14694         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
14695       NEON_ENCODE (SCALAR, inst);
14696       neon_mul_mac (et, neon_quad (rs));
14697     }
14698   else
14699     {
14700       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14701       struct neon_type_el et = neon_check_type (3, rs,
14702         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
14703       NEON_ENCODE (INTEGER, inst);
14704       /* The U bit (rounding) comes from bit mask.  */
14705       neon_three_same (neon_quad (rs), 0, et.size);
14706     }
14707 }
14708
14709 static void
14710 do_neon_fcmp_absolute (void)
14711 {
14712   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14713   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
14714   /* Size field comes from bit mask.  */
14715   neon_three_same (neon_quad (rs), 1, -1);
14716 }
14717
14718 static void
14719 do_neon_fcmp_absolute_inv (void)
14720 {
14721   neon_exchange_operands ();
14722   do_neon_fcmp_absolute ();
14723 }
14724
14725 static void
14726 do_neon_step (void)
14727 {
14728   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14729   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
14730   neon_three_same (neon_quad (rs), 0, -1);
14731 }
14732
14733 static void
14734 do_neon_abs_neg (void)
14735 {
14736   enum neon_shape rs;
14737   struct neon_type_el et;
14738
14739   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
14740     return;
14741
14742   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14743     return;
14744
14745   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14746   et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_F32 | N_KEY);
14747
14748   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14749   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14750   inst.instruction |= LOW4 (inst.operands[1].reg);
14751   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14752   inst.instruction |= neon_quad (rs) << 6;
14753   inst.instruction |= (et.type == NT_float) << 10;
14754   inst.instruction |= neon_logbits (et.size) << 18;
14755
14756   neon_dp_fixup (&inst);
14757 }
14758
14759 static void
14760 do_neon_sli (void)
14761 {
14762   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14763   struct neon_type_el et = neon_check_type (2, rs,
14764     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14765   int imm = inst.operands[2].imm;
14766   constraint (imm < 0 || (unsigned)imm >= et.size,
14767               _("immediate out of range for insert"));
14768   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14769 }
14770
14771 static void
14772 do_neon_sri (void)
14773 {
14774   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14775   struct neon_type_el et = neon_check_type (2, rs,
14776     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14777   int imm = inst.operands[2].imm;
14778   constraint (imm < 1 || (unsigned)imm > et.size,
14779               _("immediate out of range for insert"));
14780   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
14781 }
14782
14783 static void
14784 do_neon_qshlu_imm (void)
14785 {
14786   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14787   struct neon_type_el et = neon_check_type (2, rs,
14788     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
14789   int imm = inst.operands[2].imm;
14790   constraint (imm < 0 || (unsigned)imm >= et.size,
14791               _("immediate out of range for shift"));
14792   /* Only encodes the 'U present' variant of the instruction.
14793      In this case, signed types have OP (bit 8) set to 0.
14794      Unsigned types have OP set to 1.  */
14795   inst.instruction |= (et.type == NT_unsigned) << 8;
14796   /* The rest of the bits are the same as other immediate shifts.  */
14797   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14798 }
14799
14800 static void
14801 do_neon_qmovn (void)
14802 {
14803   struct neon_type_el et = neon_check_type (2, NS_DQ,
14804     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14805   /* Saturating move where operands can be signed or unsigned, and the
14806      destination has the same signedness.  */
14807   NEON_ENCODE (INTEGER, inst);
14808   if (et.type == NT_unsigned)
14809     inst.instruction |= 0xc0;
14810   else
14811     inst.instruction |= 0x80;
14812   neon_two_same (0, 1, et.size / 2);
14813 }
14814
14815 static void
14816 do_neon_qmovun (void)
14817 {
14818   struct neon_type_el et = neon_check_type (2, NS_DQ,
14819     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
14820   /* Saturating move with unsigned results. Operands must be signed.  */
14821   NEON_ENCODE (INTEGER, inst);
14822   neon_two_same (0, 1, et.size / 2);
14823 }
14824
14825 static void
14826 do_neon_rshift_sat_narrow (void)
14827 {
14828   /* FIXME: Types for narrowing. If operands are signed, results can be signed
14829      or unsigned. If operands are unsigned, results must also be unsigned.  */
14830   struct neon_type_el et = neon_check_type (2, NS_DQI,
14831     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14832   int imm = inst.operands[2].imm;
14833   /* This gets the bounds check, size encoding and immediate bits calculation
14834      right.  */
14835   et.size /= 2;
14836
14837   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
14838      VQMOVN.I<size> <Dd>, <Qm>.  */
14839   if (imm == 0)
14840     {
14841       inst.operands[2].present = 0;
14842       inst.instruction = N_MNEM_vqmovn;
14843       do_neon_qmovn ();
14844       return;
14845     }
14846
14847   constraint (imm < 1 || (unsigned)imm > et.size,
14848               _("immediate out of range"));
14849   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
14850 }
14851
14852 static void
14853 do_neon_rshift_sat_narrow_u (void)
14854 {
14855   /* FIXME: Types for narrowing. If operands are signed, results can be signed
14856      or unsigned. If operands are unsigned, results must also be unsigned.  */
14857   struct neon_type_el et = neon_check_type (2, NS_DQI,
14858     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
14859   int imm = inst.operands[2].imm;
14860   /* This gets the bounds check, size encoding and immediate bits calculation
14861      right.  */
14862   et.size /= 2;
14863
14864   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
14865      VQMOVUN.I<size> <Dd>, <Qm>.  */
14866   if (imm == 0)
14867     {
14868       inst.operands[2].present = 0;
14869       inst.instruction = N_MNEM_vqmovun;
14870       do_neon_qmovun ();
14871       return;
14872     }
14873
14874   constraint (imm < 1 || (unsigned)imm > et.size,
14875               _("immediate out of range"));
14876   /* FIXME: The manual is kind of unclear about what value U should have in
14877      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
14878      must be 1.  */
14879   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
14880 }
14881
14882 static void
14883 do_neon_movn (void)
14884 {
14885   struct neon_type_el et = neon_check_type (2, NS_DQ,
14886     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14887   NEON_ENCODE (INTEGER, inst);
14888   neon_two_same (0, 1, et.size / 2);
14889 }
14890
14891 static void
14892 do_neon_rshift_narrow (void)
14893 {
14894   struct neon_type_el et = neon_check_type (2, NS_DQI,
14895     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14896   int imm = inst.operands[2].imm;
14897   /* This gets the bounds check, size encoding and immediate bits calculation
14898      right.  */
14899   et.size /= 2;
14900
14901   /* If immediate is zero then we are a pseudo-instruction for
14902      VMOVN.I<size> <Dd>, <Qm>  */
14903   if (imm == 0)
14904     {
14905       inst.operands[2].present = 0;
14906       inst.instruction = N_MNEM_vmovn;
14907       do_neon_movn ();
14908       return;
14909     }
14910
14911   constraint (imm < 1 || (unsigned)imm > et.size,
14912               _("immediate out of range for narrowing operation"));
14913   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
14914 }
14915
14916 static void
14917 do_neon_shll (void)
14918 {
14919   /* FIXME: Type checking when lengthening.  */
14920   struct neon_type_el et = neon_check_type (2, NS_QDI,
14921     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
14922   unsigned imm = inst.operands[2].imm;
14923
14924   if (imm == et.size)
14925     {
14926       /* Maximum shift variant.  */
14927       NEON_ENCODE (INTEGER, inst);
14928       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14929       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14930       inst.instruction |= LOW4 (inst.operands[1].reg);
14931       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14932       inst.instruction |= neon_logbits (et.size) << 18;
14933
14934       neon_dp_fixup (&inst);
14935     }
14936   else
14937     {
14938       /* A more-specific type check for non-max versions.  */
14939       et = neon_check_type (2, NS_QDI,
14940         N_EQK | N_DBL, N_SU_32 | N_KEY);
14941       NEON_ENCODE (IMMED, inst);
14942       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
14943     }
14944 }
14945
14946 /* Check the various types for the VCVT instruction, and return which version
14947    the current instruction is.  */
14948
14949 #define CVT_FLAVOUR_VAR                                                       \
14950   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
14951   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
14952   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
14953   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
14954   /* Half-precision conversions.  */                                          \
14955   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
14956   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
14957   /* VFP instructions.  */                                                    \
14958   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
14959   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
14960   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
14961   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
14962   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
14963   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
14964   /* VFP instructions with bitshift.  */                                      \
14965   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
14966   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
14967   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
14968   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
14969   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
14970   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
14971   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
14972   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
14973
14974 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
14975   neon_cvt_flavour_##C,
14976
14977 /* The different types of conversions we can do.  */
14978 enum neon_cvt_flavour
14979 {
14980   CVT_FLAVOUR_VAR
14981   neon_cvt_flavour_invalid,
14982   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
14983 };
14984
14985 #undef CVT_VAR
14986
14987 static enum neon_cvt_flavour
14988 get_neon_cvt_flavour (enum neon_shape rs)
14989 {
14990 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
14991   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
14992   if (et.type != NT_invtype)                            \
14993     {                                                   \
14994       inst.error = NULL;                                \
14995       return (neon_cvt_flavour_##C);                    \
14996     }
14997
14998   struct neon_type_el et;
14999   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
15000                         || rs == NS_FF) ? N_VFP : 0;
15001   /* The instruction versions which take an immediate take one register
15002      argument, which is extended to the width of the full register. Thus the
15003      "source" and "destination" registers must have the same width.  Hack that
15004      here by making the size equal to the key (wider, in this case) operand.  */
15005   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
15006
15007   CVT_FLAVOUR_VAR;
15008
15009   return neon_cvt_flavour_invalid;
15010 #undef CVT_VAR
15011 }
15012
15013 enum neon_cvt_mode
15014 {
15015   neon_cvt_mode_a,
15016   neon_cvt_mode_n,
15017   neon_cvt_mode_p,
15018   neon_cvt_mode_m,
15019   neon_cvt_mode_z,
15020   neon_cvt_mode_x,
15021   neon_cvt_mode_r
15022 };
15023
15024 /* Neon-syntax VFP conversions.  */
15025
15026 static void
15027 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
15028 {
15029   const char *opname = 0;
15030
15031   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI)
15032     {
15033       /* Conversions with immediate bitshift.  */
15034       const char *enc[] =
15035         {
15036 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
15037           CVT_FLAVOUR_VAR
15038           NULL
15039 #undef CVT_VAR
15040         };
15041
15042       if (flavour < (int) ARRAY_SIZE (enc))
15043         {
15044           opname = enc[flavour];
15045           constraint (inst.operands[0].reg != inst.operands[1].reg,
15046                       _("operands 0 and 1 must be the same register"));
15047           inst.operands[1] = inst.operands[2];
15048           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
15049         }
15050     }
15051   else
15052     {
15053       /* Conversions without bitshift.  */
15054       const char *enc[] =
15055         {
15056 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
15057           CVT_FLAVOUR_VAR
15058           NULL
15059 #undef CVT_VAR
15060         };
15061
15062       if (flavour < (int) ARRAY_SIZE (enc))
15063         opname = enc[flavour];
15064     }
15065
15066   if (opname)
15067     do_vfp_nsyn_opcode (opname);
15068 }
15069
15070 static void
15071 do_vfp_nsyn_cvtz (void)
15072 {
15073   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_NULL);
15074   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15075   const char *enc[] =
15076     {
15077 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
15078       CVT_FLAVOUR_VAR
15079       NULL
15080 #undef CVT_VAR
15081     };
15082
15083   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
15084     do_vfp_nsyn_opcode (enc[flavour]);
15085 }
15086
15087 static void
15088 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
15089                       enum neon_cvt_mode mode)
15090 {
15091   int sz, op;
15092   int rm;
15093
15094   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
15095      D register operands.  */
15096   if (flavour == neon_cvt_flavour_s32_f64
15097       || flavour == neon_cvt_flavour_u32_f64)
15098     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15099                 _(BAD_FPU));
15100
15101   set_it_insn_type (OUTSIDE_IT_INSN);
15102
15103   switch (flavour)
15104     {
15105     case neon_cvt_flavour_s32_f64:
15106       sz = 1;
15107       op = 1;
15108       break;
15109     case neon_cvt_flavour_s32_f32:
15110       sz = 0;
15111       op = 1;
15112       break;
15113     case neon_cvt_flavour_u32_f64:
15114       sz = 1;
15115       op = 0;
15116       break;
15117     case neon_cvt_flavour_u32_f32:
15118       sz = 0;
15119       op = 0;
15120       break;
15121     default:
15122       first_error (_("invalid instruction shape"));
15123       return;
15124     }
15125
15126   switch (mode)
15127     {
15128     case neon_cvt_mode_a: rm = 0; break;
15129     case neon_cvt_mode_n: rm = 1; break;
15130     case neon_cvt_mode_p: rm = 2; break;
15131     case neon_cvt_mode_m: rm = 3; break;
15132     default: first_error (_("invalid rounding mode")); return;
15133     }
15134
15135   NEON_ENCODE (FPV8, inst);
15136   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
15137   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
15138   inst.instruction |= sz << 8;
15139   inst.instruction |= op << 7;
15140   inst.instruction |= rm << 16;
15141   inst.instruction |= 0xf0000000;
15142   inst.is_neon = TRUE;
15143 }
15144
15145 static void
15146 do_neon_cvt_1 (enum neon_cvt_mode mode)
15147 {
15148   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
15149     NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ, NS_NULL);
15150   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15151
15152   /* PR11109: Handle round-to-zero for VCVT conversions.  */
15153   if (mode == neon_cvt_mode_z
15154       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
15155       && (flavour == neon_cvt_flavour_s32_f32
15156           || flavour == neon_cvt_flavour_u32_f32
15157           || flavour == neon_cvt_flavour_s32_f64
15158           || flavour == neon_cvt_flavour_u32_f64)
15159       && (rs == NS_FD || rs == NS_FF))
15160     {
15161       do_vfp_nsyn_cvtz ();
15162       return;
15163     }
15164
15165   /* VFP rather than Neon conversions.  */
15166   if (flavour >= neon_cvt_flavour_first_fp)
15167     {
15168       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15169         do_vfp_nsyn_cvt (rs, flavour);
15170       else
15171         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15172
15173       return;
15174     }
15175
15176   switch (rs)
15177     {
15178     case NS_DDI:
15179     case NS_QQI:
15180       {
15181         unsigned immbits;
15182         unsigned enctab[] = { 0x0000100, 0x1000100, 0x0, 0x1000000 };
15183
15184         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15185           return;
15186
15187         /* Fixed-point conversion with #0 immediate is encoded as an
15188            integer conversion.  */
15189         if (inst.operands[2].present && inst.operands[2].imm == 0)
15190           goto int_encode;
15191        immbits = 32 - inst.operands[2].imm;
15192         NEON_ENCODE (IMMED, inst);
15193         if (flavour != neon_cvt_flavour_invalid)
15194           inst.instruction |= enctab[flavour];
15195         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15196         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15197         inst.instruction |= LOW4 (inst.operands[1].reg);
15198         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15199         inst.instruction |= neon_quad (rs) << 6;
15200         inst.instruction |= 1 << 21;
15201         inst.instruction |= immbits << 16;
15202
15203         neon_dp_fixup (&inst);
15204       }
15205       break;
15206
15207     case NS_DD:
15208     case NS_QQ:
15209       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
15210         {
15211           NEON_ENCODE (FLOAT, inst);
15212           set_it_insn_type (OUTSIDE_IT_INSN);
15213
15214           if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
15215             return;
15216
15217           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15218           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15219           inst.instruction |= LOW4 (inst.operands[1].reg);
15220           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15221           inst.instruction |= neon_quad (rs) << 6;
15222           inst.instruction |= (flavour == neon_cvt_flavour_u32_f32) << 7;
15223           inst.instruction |= mode << 8;
15224           if (thumb_mode)
15225             inst.instruction |= 0xfc000000;
15226           else
15227             inst.instruction |= 0xf0000000;
15228         }
15229       else
15230         {
15231     int_encode:
15232           {
15233             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080 };
15234
15235             NEON_ENCODE (INTEGER, inst);
15236
15237             if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15238               return;
15239
15240             if (flavour != neon_cvt_flavour_invalid)
15241               inst.instruction |= enctab[flavour];
15242
15243             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15244             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15245             inst.instruction |= LOW4 (inst.operands[1].reg);
15246             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15247             inst.instruction |= neon_quad (rs) << 6;
15248             inst.instruction |= 2 << 18;
15249
15250             neon_dp_fixup (&inst);
15251           }
15252         }
15253       break;
15254
15255     /* Half-precision conversions for Advanced SIMD -- neon.  */
15256     case NS_QD:
15257     case NS_DQ:
15258
15259       if ((rs == NS_DQ)
15260           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
15261           {
15262             as_bad (_("operand size must match register width"));
15263             break;
15264           }
15265
15266       if ((rs == NS_QD)
15267           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
15268           {
15269             as_bad (_("operand size must match register width"));
15270             break;
15271           }
15272
15273       if (rs == NS_DQ)
15274         inst.instruction = 0x3b60600;
15275       else
15276         inst.instruction = 0x3b60700;
15277
15278       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15279       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15280       inst.instruction |= LOW4 (inst.operands[1].reg);
15281       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15282       neon_dp_fixup (&inst);
15283       break;
15284
15285     default:
15286       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
15287       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15288         do_vfp_nsyn_cvt (rs, flavour);
15289       else
15290         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15291     }
15292 }
15293
15294 static void
15295 do_neon_cvtr (void)
15296 {
15297   do_neon_cvt_1 (neon_cvt_mode_x);
15298 }
15299
15300 static void
15301 do_neon_cvt (void)
15302 {
15303   do_neon_cvt_1 (neon_cvt_mode_z);
15304 }
15305
15306 static void
15307 do_neon_cvta (void)
15308 {
15309   do_neon_cvt_1 (neon_cvt_mode_a);
15310 }
15311
15312 static void
15313 do_neon_cvtn (void)
15314 {
15315   do_neon_cvt_1 (neon_cvt_mode_n);
15316 }
15317
15318 static void
15319 do_neon_cvtp (void)
15320 {
15321   do_neon_cvt_1 (neon_cvt_mode_p);
15322 }
15323
15324 static void
15325 do_neon_cvtm (void)
15326 {
15327   do_neon_cvt_1 (neon_cvt_mode_m);
15328 }
15329
15330 static void
15331 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
15332 {
15333   if (is_double)
15334     mark_feature_used (&fpu_vfp_ext_armv8);
15335
15336   encode_arm_vfp_reg (inst.operands[0].reg,
15337                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
15338   encode_arm_vfp_reg (inst.operands[1].reg,
15339                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
15340   inst.instruction |= to ? 0x10000 : 0;
15341   inst.instruction |= t ? 0x80 : 0;
15342   inst.instruction |= is_double ? 0x100 : 0;
15343   do_vfp_cond_or_thumb ();
15344 }
15345
15346 static void
15347 do_neon_cvttb_1 (bfd_boolean t)
15348 {
15349   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_DF, NS_NULL);
15350
15351   if (rs == NS_NULL)
15352     return;
15353   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
15354     {
15355       inst.error = NULL;
15356       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
15357     }
15358   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
15359     {
15360       inst.error = NULL;
15361       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
15362     }
15363   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
15364     {
15365       /* The VCVTB and VCVTT instructions with D-register operands
15366          don't work for SP only targets.  */
15367       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15368                   _(BAD_FPU));
15369
15370       inst.error = NULL;
15371       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
15372     }
15373   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
15374     {
15375       /* The VCVTB and VCVTT instructions with D-register operands
15376          don't work for SP only targets.  */
15377       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15378                   _(BAD_FPU));
15379
15380       inst.error = NULL;
15381       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
15382     }
15383   else
15384     return;
15385 }
15386
15387 static void
15388 do_neon_cvtb (void)
15389 {
15390   do_neon_cvttb_1 (FALSE);
15391 }
15392
15393
15394 static void
15395 do_neon_cvtt (void)
15396 {
15397   do_neon_cvttb_1 (TRUE);
15398 }
15399
15400 static void
15401 neon_move_immediate (void)
15402 {
15403   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
15404   struct neon_type_el et = neon_check_type (2, rs,
15405     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
15406   unsigned immlo, immhi = 0, immbits;
15407   int op, cmode, float_p;
15408
15409   constraint (et.type == NT_invtype,
15410               _("operand size must be specified for immediate VMOV"));
15411
15412   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
15413   op = (inst.instruction & (1 << 5)) != 0;
15414
15415   immlo = inst.operands[1].imm;
15416   if (inst.operands[1].regisimm)
15417     immhi = inst.operands[1].reg;
15418
15419   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
15420               _("immediate has bits set outside the operand size"));
15421
15422   float_p = inst.operands[1].immisfloat;
15423
15424   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
15425                                         et.size, et.type)) == FAIL)
15426     {
15427       /* Invert relevant bits only.  */
15428       neon_invert_size (&immlo, &immhi, et.size);
15429       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
15430          with one or the other; those cases are caught by
15431          neon_cmode_for_move_imm.  */
15432       op = !op;
15433       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
15434                                             &op, et.size, et.type)) == FAIL)
15435         {
15436           first_error (_("immediate out of range"));
15437           return;
15438         }
15439     }
15440
15441   inst.instruction &= ~(1 << 5);
15442   inst.instruction |= op << 5;
15443
15444   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15445   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15446   inst.instruction |= neon_quad (rs) << 6;
15447   inst.instruction |= cmode << 8;
15448
15449   neon_write_immbits (immbits);
15450 }
15451
15452 static void
15453 do_neon_mvn (void)
15454 {
15455   if (inst.operands[1].isreg)
15456     {
15457       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15458
15459       NEON_ENCODE (INTEGER, inst);
15460       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15461       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15462       inst.instruction |= LOW4 (inst.operands[1].reg);
15463       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15464       inst.instruction |= neon_quad (rs) << 6;
15465     }
15466   else
15467     {
15468       NEON_ENCODE (IMMED, inst);
15469       neon_move_immediate ();
15470     }
15471
15472   neon_dp_fixup (&inst);
15473 }
15474
15475 /* Encode instructions of form:
15476
15477   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
15478   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
15479
15480 static void
15481 neon_mixed_length (struct neon_type_el et, unsigned size)
15482 {
15483   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15484   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15485   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15486   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15487   inst.instruction |= LOW4 (inst.operands[2].reg);
15488   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15489   inst.instruction |= (et.type == NT_unsigned) << 24;
15490   inst.instruction |= neon_logbits (size) << 20;
15491
15492   neon_dp_fixup (&inst);
15493 }
15494
15495 static void
15496 do_neon_dyadic_long (void)
15497 {
15498   /* FIXME: Type checking for lengthening op.  */
15499   struct neon_type_el et = neon_check_type (3, NS_QDD,
15500     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
15501   neon_mixed_length (et, et.size);
15502 }
15503
15504 static void
15505 do_neon_abal (void)
15506 {
15507   struct neon_type_el et = neon_check_type (3, NS_QDD,
15508     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
15509   neon_mixed_length (et, et.size);
15510 }
15511
15512 static void
15513 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
15514 {
15515   if (inst.operands[2].isscalar)
15516     {
15517       struct neon_type_el et = neon_check_type (3, NS_QDS,
15518         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
15519       NEON_ENCODE (SCALAR, inst);
15520       neon_mul_mac (et, et.type == NT_unsigned);
15521     }
15522   else
15523     {
15524       struct neon_type_el et = neon_check_type (3, NS_QDD,
15525         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
15526       NEON_ENCODE (INTEGER, inst);
15527       neon_mixed_length (et, et.size);
15528     }
15529 }
15530
15531 static void
15532 do_neon_mac_maybe_scalar_long (void)
15533 {
15534   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
15535 }
15536
15537 static void
15538 do_neon_dyadic_wide (void)
15539 {
15540   struct neon_type_el et = neon_check_type (3, NS_QQD,
15541     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
15542   neon_mixed_length (et, et.size);
15543 }
15544
15545 static void
15546 do_neon_dyadic_narrow (void)
15547 {
15548   struct neon_type_el et = neon_check_type (3, NS_QDD,
15549     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
15550   /* Operand sign is unimportant, and the U bit is part of the opcode,
15551      so force the operand type to integer.  */
15552   et.type = NT_integer;
15553   neon_mixed_length (et, et.size / 2);
15554 }
15555
15556 static void
15557 do_neon_mul_sat_scalar_long (void)
15558 {
15559   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
15560 }
15561
15562 static void
15563 do_neon_vmull (void)
15564 {
15565   if (inst.operands[2].isscalar)
15566     do_neon_mac_maybe_scalar_long ();
15567   else
15568     {
15569       struct neon_type_el et = neon_check_type (3, NS_QDD,
15570         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
15571
15572       if (et.type == NT_poly)
15573         NEON_ENCODE (POLY, inst);
15574       else
15575         NEON_ENCODE (INTEGER, inst);
15576
15577       /* For polynomial encoding the U bit must be zero, and the size must
15578          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
15579          obviously, as 0b10).  */
15580       if (et.size == 64)
15581         {
15582           /* Check we're on the correct architecture.  */
15583           if (!mark_feature_used (&fpu_crypto_ext_armv8))
15584             inst.error =
15585               _("Instruction form not available on this architecture.");
15586
15587           et.size = 32;
15588         }
15589
15590       neon_mixed_length (et, et.size);
15591     }
15592 }
15593
15594 static void
15595 do_neon_ext (void)
15596 {
15597   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
15598   struct neon_type_el et = neon_check_type (3, rs,
15599     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15600   unsigned imm = (inst.operands[3].imm * et.size) / 8;
15601
15602   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
15603               _("shift out of range"));
15604   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15605   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15606   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15607   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15608   inst.instruction |= LOW4 (inst.operands[2].reg);
15609   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15610   inst.instruction |= neon_quad (rs) << 6;
15611   inst.instruction |= imm << 8;
15612
15613   neon_dp_fixup (&inst);
15614 }
15615
15616 static void
15617 do_neon_rev (void)
15618 {
15619   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15620   struct neon_type_el et = neon_check_type (2, rs,
15621     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15622   unsigned op = (inst.instruction >> 7) & 3;
15623   /* N (width of reversed regions) is encoded as part of the bitmask. We
15624      extract it here to check the elements to be reversed are smaller.
15625      Otherwise we'd get a reserved instruction.  */
15626   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
15627   gas_assert (elsize != 0);
15628   constraint (et.size >= elsize,
15629               _("elements must be smaller than reversal region"));
15630   neon_two_same (neon_quad (rs), 1, et.size);
15631 }
15632
15633 static void
15634 do_neon_dup (void)
15635 {
15636   if (inst.operands[1].isscalar)
15637     {
15638       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
15639       struct neon_type_el et = neon_check_type (2, rs,
15640         N_EQK, N_8 | N_16 | N_32 | N_KEY);
15641       unsigned sizebits = et.size >> 3;
15642       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
15643       int logsize = neon_logbits (et.size);
15644       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
15645
15646       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
15647         return;
15648
15649       NEON_ENCODE (SCALAR, inst);
15650       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15651       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15652       inst.instruction |= LOW4 (dm);
15653       inst.instruction |= HI1 (dm) << 5;
15654       inst.instruction |= neon_quad (rs) << 6;
15655       inst.instruction |= x << 17;
15656       inst.instruction |= sizebits << 16;
15657
15658       neon_dp_fixup (&inst);
15659     }
15660   else
15661     {
15662       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
15663       struct neon_type_el et = neon_check_type (2, rs,
15664         N_8 | N_16 | N_32 | N_KEY, N_EQK);
15665       /* Duplicate ARM register to lanes of vector.  */
15666       NEON_ENCODE (ARMREG, inst);
15667       switch (et.size)
15668         {
15669         case 8:  inst.instruction |= 0x400000; break;
15670         case 16: inst.instruction |= 0x000020; break;
15671         case 32: inst.instruction |= 0x000000; break;
15672         default: break;
15673         }
15674       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
15675       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
15676       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
15677       inst.instruction |= neon_quad (rs) << 21;
15678       /* The encoding for this instruction is identical for the ARM and Thumb
15679          variants, except for the condition field.  */
15680       do_vfp_cond_or_thumb ();
15681     }
15682 }
15683
15684 /* VMOV has particularly many variations. It can be one of:
15685      0. VMOV<c><q> <Qd>, <Qm>
15686      1. VMOV<c><q> <Dd>, <Dm>
15687    (Register operations, which are VORR with Rm = Rn.)
15688      2. VMOV<c><q>.<dt> <Qd>, #<imm>
15689      3. VMOV<c><q>.<dt> <Dd>, #<imm>
15690    (Immediate loads.)
15691      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
15692    (ARM register to scalar.)
15693      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
15694    (Two ARM registers to vector.)
15695      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
15696    (Scalar to ARM register.)
15697      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
15698    (Vector to two ARM registers.)
15699      8. VMOV.F32 <Sd>, <Sm>
15700      9. VMOV.F64 <Dd>, <Dm>
15701    (VFP register moves.)
15702     10. VMOV.F32 <Sd>, #imm
15703     11. VMOV.F64 <Dd>, #imm
15704    (VFP float immediate load.)
15705     12. VMOV <Rd>, <Sm>
15706    (VFP single to ARM reg.)
15707     13. VMOV <Sd>, <Rm>
15708    (ARM reg to VFP single.)
15709     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
15710    (Two ARM regs to two VFP singles.)
15711     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
15712    (Two VFP singles to two ARM regs.)
15713
15714    These cases can be disambiguated using neon_select_shape, except cases 1/9
15715    and 3/11 which depend on the operand type too.
15716
15717    All the encoded bits are hardcoded by this function.
15718
15719    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
15720    Cases 5, 7 may be used with VFPv2 and above.
15721
15722    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
15723    can specify a type where it doesn't make sense to, and is ignored).  */
15724
15725 static void
15726 do_neon_mov (void)
15727 {
15728   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
15729     NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR, NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
15730     NS_NULL);
15731   struct neon_type_el et;
15732   const char *ldconst = 0;
15733
15734   switch (rs)
15735     {
15736     case NS_DD:  /* case 1/9.  */
15737       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
15738       /* It is not an error here if no type is given.  */
15739       inst.error = NULL;
15740       if (et.type == NT_float && et.size == 64)
15741         {
15742           do_vfp_nsyn_opcode ("fcpyd");
15743           break;
15744         }
15745       /* fall through.  */
15746
15747     case NS_QQ:  /* case 0/1.  */
15748       {
15749         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15750           return;
15751         /* The architecture manual I have doesn't explicitly state which
15752            value the U bit should have for register->register moves, but
15753            the equivalent VORR instruction has U = 0, so do that.  */
15754         inst.instruction = 0x0200110;
15755         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15756         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15757         inst.instruction |= LOW4 (inst.operands[1].reg);
15758         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15759         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15760         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15761         inst.instruction |= neon_quad (rs) << 6;
15762
15763         neon_dp_fixup (&inst);
15764       }
15765       break;
15766
15767     case NS_DI:  /* case 3/11.  */
15768       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
15769       inst.error = NULL;
15770       if (et.type == NT_float && et.size == 64)
15771         {
15772           /* case 11 (fconstd).  */
15773           ldconst = "fconstd";
15774           goto encode_fconstd;
15775         }
15776       /* fall through.  */
15777
15778     case NS_QI:  /* case 2/3.  */
15779       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15780         return;
15781       inst.instruction = 0x0800010;
15782       neon_move_immediate ();
15783       neon_dp_fixup (&inst);
15784       break;
15785
15786     case NS_SR:  /* case 4.  */
15787       {
15788         unsigned bcdebits = 0;
15789         int logsize;
15790         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
15791         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
15792
15793         /* .<size> is optional here, defaulting to .32. */
15794         if (inst.vectype.elems == 0
15795             && inst.operands[0].vectype.type == NT_invtype
15796             && inst.operands[1].vectype.type == NT_invtype)
15797           {
15798             inst.vectype.el[0].type = NT_untyped;
15799             inst.vectype.el[0].size = 32;
15800             inst.vectype.elems = 1;
15801           }
15802
15803         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
15804         logsize = neon_logbits (et.size);
15805
15806         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
15807                     _(BAD_FPU));
15808         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
15809                     && et.size != 32, _(BAD_FPU));
15810         constraint (et.type == NT_invtype, _("bad type for scalar"));
15811         constraint (x >= 64 / et.size, _("scalar index out of range"));
15812
15813         switch (et.size)
15814           {
15815           case 8:  bcdebits = 0x8; break;
15816           case 16: bcdebits = 0x1; break;
15817           case 32: bcdebits = 0x0; break;
15818           default: ;
15819           }
15820
15821         bcdebits |= x << logsize;
15822
15823         inst.instruction = 0xe000b10;
15824         do_vfp_cond_or_thumb ();
15825         inst.instruction |= LOW4 (dn) << 16;
15826         inst.instruction |= HI1 (dn) << 7;
15827         inst.instruction |= inst.operands[1].reg << 12;
15828         inst.instruction |= (bcdebits & 3) << 5;
15829         inst.instruction |= (bcdebits >> 2) << 21;
15830       }
15831       break;
15832
15833     case NS_DRR:  /* case 5 (fmdrr).  */
15834       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
15835                   _(BAD_FPU));
15836
15837       inst.instruction = 0xc400b10;
15838       do_vfp_cond_or_thumb ();
15839       inst.instruction |= LOW4 (inst.operands[0].reg);
15840       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
15841       inst.instruction |= inst.operands[1].reg << 12;
15842       inst.instruction |= inst.operands[2].reg << 16;
15843       break;
15844
15845     case NS_RS:  /* case 6.  */
15846       {
15847         unsigned logsize;
15848         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
15849         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
15850         unsigned abcdebits = 0;
15851
15852         /* .<dt> is optional here, defaulting to .32. */
15853         if (inst.vectype.elems == 0
15854             && inst.operands[0].vectype.type == NT_invtype
15855             && inst.operands[1].vectype.type == NT_invtype)
15856           {
15857             inst.vectype.el[0].type = NT_untyped;
15858             inst.vectype.el[0].size = 32;
15859             inst.vectype.elems = 1;
15860           }
15861
15862         et = neon_check_type (2, NS_NULL,
15863                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
15864         logsize = neon_logbits (et.size);
15865
15866         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
15867                     _(BAD_FPU));
15868         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
15869                     && et.size != 32, _(BAD_FPU));
15870         constraint (et.type == NT_invtype, _("bad type for scalar"));
15871         constraint (x >= 64 / et.size, _("scalar index out of range"));
15872
15873         switch (et.size)
15874           {
15875           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
15876           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
15877           case 32: abcdebits = 0x00; break;
15878           default: ;
15879           }
15880
15881         abcdebits |= x << logsize;
15882         inst.instruction = 0xe100b10;
15883         do_vfp_cond_or_thumb ();
15884         inst.instruction |= LOW4 (dn) << 16;
15885         inst.instruction |= HI1 (dn) << 7;
15886         inst.instruction |= inst.operands[0].reg << 12;
15887         inst.instruction |= (abcdebits & 3) << 5;
15888         inst.instruction |= (abcdebits >> 2) << 21;
15889       }
15890       break;
15891
15892     case NS_RRD:  /* case 7 (fmrrd).  */
15893       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
15894                   _(BAD_FPU));
15895
15896       inst.instruction = 0xc500b10;
15897       do_vfp_cond_or_thumb ();
15898       inst.instruction |= inst.operands[0].reg << 12;
15899       inst.instruction |= inst.operands[1].reg << 16;
15900       inst.instruction |= LOW4 (inst.operands[2].reg);
15901       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15902       break;
15903
15904     case NS_FF:  /* case 8 (fcpys).  */
15905       do_vfp_nsyn_opcode ("fcpys");
15906       break;
15907
15908     case NS_FI:  /* case 10 (fconsts).  */
15909       ldconst = "fconsts";
15910       encode_fconstd:
15911       if (is_quarter_float (inst.operands[1].imm))
15912         {
15913           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
15914           do_vfp_nsyn_opcode (ldconst);
15915         }
15916       else
15917         first_error (_("immediate out of range"));
15918       break;
15919
15920     case NS_RF:  /* case 12 (fmrs).  */
15921       do_vfp_nsyn_opcode ("fmrs");
15922       break;
15923
15924     case NS_FR:  /* case 13 (fmsr).  */
15925       do_vfp_nsyn_opcode ("fmsr");
15926       break;
15927
15928     /* The encoders for the fmrrs and fmsrr instructions expect three operands
15929        (one of which is a list), but we have parsed four.  Do some fiddling to
15930        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
15931        expect.  */
15932     case NS_RRFF:  /* case 14 (fmrrs).  */
15933       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
15934                   _("VFP registers must be adjacent"));
15935       inst.operands[2].imm = 2;
15936       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
15937       do_vfp_nsyn_opcode ("fmrrs");
15938       break;
15939
15940     case NS_FFRR:  /* case 15 (fmsrr).  */
15941       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
15942                   _("VFP registers must be adjacent"));
15943       inst.operands[1] = inst.operands[2];
15944       inst.operands[2] = inst.operands[3];
15945       inst.operands[0].imm = 2;
15946       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
15947       do_vfp_nsyn_opcode ("fmsrr");
15948       break;
15949
15950     case NS_NULL:
15951       /* neon_select_shape has determined that the instruction
15952          shape is wrong and has already set the error message.  */
15953       break;
15954
15955     default:
15956       abort ();
15957     }
15958 }
15959
15960 static void
15961 do_neon_rshift_round_imm (void)
15962 {
15963   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15964   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
15965   int imm = inst.operands[2].imm;
15966
15967   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
15968   if (imm == 0)
15969     {
15970       inst.operands[2].present = 0;
15971       do_neon_mov ();
15972       return;
15973     }
15974
15975   constraint (imm < 1 || (unsigned)imm > et.size,
15976               _("immediate out of range for shift"));
15977   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
15978                   et.size - imm);
15979 }
15980
15981 static void
15982 do_neon_movl (void)
15983 {
15984   struct neon_type_el et = neon_check_type (2, NS_QD,
15985     N_EQK | N_DBL, N_SU_32 | N_KEY);
15986   unsigned sizebits = et.size >> 3;
15987   inst.instruction |= sizebits << 19;
15988   neon_two_same (0, et.type == NT_unsigned, -1);
15989 }
15990
15991 static void
15992 do_neon_trn (void)
15993 {
15994   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15995   struct neon_type_el et = neon_check_type (2, rs,
15996     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15997   NEON_ENCODE (INTEGER, inst);
15998   neon_two_same (neon_quad (rs), 1, et.size);
15999 }
16000
16001 static void
16002 do_neon_zip_uzp (void)
16003 {
16004   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16005   struct neon_type_el et = neon_check_type (2, rs,
16006     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16007   if (rs == NS_DD && et.size == 32)
16008     {
16009       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
16010       inst.instruction = N_MNEM_vtrn;
16011       do_neon_trn ();
16012       return;
16013     }
16014   neon_two_same (neon_quad (rs), 1, et.size);
16015 }
16016
16017 static void
16018 do_neon_sat_abs_neg (void)
16019 {
16020   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16021   struct neon_type_el et = neon_check_type (2, rs,
16022     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16023   neon_two_same (neon_quad (rs), 1, et.size);
16024 }
16025
16026 static void
16027 do_neon_pair_long (void)
16028 {
16029   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16030   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
16031   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
16032   inst.instruction |= (et.type == NT_unsigned) << 7;
16033   neon_two_same (neon_quad (rs), 1, et.size);
16034 }
16035
16036 static void
16037 do_neon_recip_est (void)
16038 {
16039   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16040   struct neon_type_el et = neon_check_type (2, rs,
16041     N_EQK | N_FLT, N_F32 | N_U32 | N_KEY);
16042   inst.instruction |= (et.type == NT_float) << 8;
16043   neon_two_same (neon_quad (rs), 1, et.size);
16044 }
16045
16046 static void
16047 do_neon_cls (void)
16048 {
16049   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16050   struct neon_type_el et = neon_check_type (2, rs,
16051     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16052   neon_two_same (neon_quad (rs), 1, et.size);
16053 }
16054
16055 static void
16056 do_neon_clz (void)
16057 {
16058   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16059   struct neon_type_el et = neon_check_type (2, rs,
16060     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
16061   neon_two_same (neon_quad (rs), 1, et.size);
16062 }
16063
16064 static void
16065 do_neon_cnt (void)
16066 {
16067   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16068   struct neon_type_el et = neon_check_type (2, rs,
16069     N_EQK | N_INT, N_8 | N_KEY);
16070   neon_two_same (neon_quad (rs), 1, et.size);
16071 }
16072
16073 static void
16074 do_neon_swp (void)
16075 {
16076   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16077   neon_two_same (neon_quad (rs), 1, -1);
16078 }
16079
16080 static void
16081 do_neon_tbl_tbx (void)
16082 {
16083   unsigned listlenbits;
16084   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
16085
16086   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
16087     {
16088       first_error (_("bad list length for table lookup"));
16089       return;
16090     }
16091
16092   listlenbits = inst.operands[1].imm - 1;
16093   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16094   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16095   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16096   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16097   inst.instruction |= LOW4 (inst.operands[2].reg);
16098   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16099   inst.instruction |= listlenbits << 8;
16100
16101   neon_dp_fixup (&inst);
16102 }
16103
16104 static void
16105 do_neon_ldm_stm (void)
16106 {
16107   /* P, U and L bits are part of bitmask.  */
16108   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
16109   unsigned offsetbits = inst.operands[1].imm * 2;
16110
16111   if (inst.operands[1].issingle)
16112     {
16113       do_vfp_nsyn_ldm_stm (is_dbmode);
16114       return;
16115     }
16116
16117   constraint (is_dbmode && !inst.operands[0].writeback,
16118               _("writeback (!) must be used for VLDMDB and VSTMDB"));
16119
16120   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16121               _("register list must contain at least 1 and at most 16 "
16122                 "registers"));
16123
16124   inst.instruction |= inst.operands[0].reg << 16;
16125   inst.instruction |= inst.operands[0].writeback << 21;
16126   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16127   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
16128
16129   inst.instruction |= offsetbits;
16130
16131   do_vfp_cond_or_thumb ();
16132 }
16133
16134 static void
16135 do_neon_ldr_str (void)
16136 {
16137   int is_ldr = (inst.instruction & (1 << 20)) != 0;
16138
16139   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
16140      And is UNPREDICTABLE in thumb mode.  */
16141   if (!is_ldr
16142       && inst.operands[1].reg == REG_PC
16143       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
16144     {
16145       if (thumb_mode)
16146         inst.error = _("Use of PC here is UNPREDICTABLE");
16147       else if (warn_on_deprecated)
16148         as_tsktsk (_("Use of PC here is deprecated"));
16149     }
16150
16151   if (inst.operands[0].issingle)
16152     {
16153       if (is_ldr)
16154         do_vfp_nsyn_opcode ("flds");
16155       else
16156         do_vfp_nsyn_opcode ("fsts");
16157     }
16158   else
16159     {
16160       if (is_ldr)
16161         do_vfp_nsyn_opcode ("fldd");
16162       else
16163         do_vfp_nsyn_opcode ("fstd");
16164     }
16165 }
16166
16167 /* "interleave" version also handles non-interleaving register VLD1/VST1
16168    instructions.  */
16169
16170 static void
16171 do_neon_ld_st_interleave (void)
16172 {
16173   struct neon_type_el et = neon_check_type (1, NS_NULL,
16174                                             N_8 | N_16 | N_32 | N_64);
16175   unsigned alignbits = 0;
16176   unsigned idx;
16177   /* The bits in this table go:
16178      0: register stride of one (0) or two (1)
16179      1,2: register list length, minus one (1, 2, 3, 4).
16180      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
16181      We use -1 for invalid entries.  */
16182   const int typetable[] =
16183     {
16184       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
16185        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
16186        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
16187        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
16188     };
16189   int typebits;
16190
16191   if (et.type == NT_invtype)
16192     return;
16193
16194   if (inst.operands[1].immisalign)
16195     switch (inst.operands[1].imm >> 8)
16196       {
16197       case 64: alignbits = 1; break;
16198       case 128:
16199         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
16200             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
16201           goto bad_alignment;
16202         alignbits = 2;
16203         break;
16204       case 256:
16205         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
16206           goto bad_alignment;
16207         alignbits = 3;
16208         break;
16209       default:
16210       bad_alignment:
16211         first_error (_("bad alignment"));
16212         return;
16213       }
16214
16215   inst.instruction |= alignbits << 4;
16216   inst.instruction |= neon_logbits (et.size) << 6;
16217
16218   /* Bits [4:6] of the immediate in a list specifier encode register stride
16219      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
16220      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
16221      up the right value for "type" in a table based on this value and the given
16222      list style, then stick it back.  */
16223   idx = ((inst.operands[0].imm >> 4) & 7)
16224         | (((inst.instruction >> 8) & 3) << 3);
16225
16226   typebits = typetable[idx];
16227
16228   constraint (typebits == -1, _("bad list type for instruction"));
16229   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
16230               _("bad element type for instruction"));
16231
16232   inst.instruction &= ~0xf00;
16233   inst.instruction |= typebits << 8;
16234 }
16235
16236 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
16237    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
16238    otherwise. The variable arguments are a list of pairs of legal (size, align)
16239    values, terminated with -1.  */
16240
16241 static int
16242 neon_alignment_bit (int size, int align, int *do_align, ...)
16243 {
16244   va_list ap;
16245   int result = FAIL, thissize, thisalign;
16246
16247   if (!inst.operands[1].immisalign)
16248     {
16249       *do_align = 0;
16250       return SUCCESS;
16251     }
16252
16253   va_start (ap, do_align);
16254
16255   do
16256     {
16257       thissize = va_arg (ap, int);
16258       if (thissize == -1)
16259         break;
16260       thisalign = va_arg (ap, int);
16261
16262       if (size == thissize && align == thisalign)
16263         result = SUCCESS;
16264     }
16265   while (result != SUCCESS);
16266
16267   va_end (ap);
16268
16269   if (result == SUCCESS)
16270     *do_align = 1;
16271   else
16272     first_error (_("unsupported alignment for instruction"));
16273
16274   return result;
16275 }
16276
16277 static void
16278 do_neon_ld_st_lane (void)
16279 {
16280   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
16281   int align_good, do_align = 0;
16282   int logsize = neon_logbits (et.size);
16283   int align = inst.operands[1].imm >> 8;
16284   int n = (inst.instruction >> 8) & 3;
16285   int max_el = 64 / et.size;
16286
16287   if (et.type == NT_invtype)
16288     return;
16289
16290   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
16291               _("bad list length"));
16292   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
16293               _("scalar index out of range"));
16294   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
16295               && et.size == 8,
16296               _("stride of 2 unavailable when element size is 8"));
16297
16298   switch (n)
16299     {
16300     case 0:  /* VLD1 / VST1.  */
16301       align_good = neon_alignment_bit (et.size, align, &do_align, 16, 16,
16302                                        32, 32, -1);
16303       if (align_good == FAIL)
16304         return;
16305       if (do_align)
16306         {
16307           unsigned alignbits = 0;
16308           switch (et.size)
16309             {
16310             case 16: alignbits = 0x1; break;
16311             case 32: alignbits = 0x3; break;
16312             default: ;
16313             }
16314           inst.instruction |= alignbits << 4;
16315         }
16316       break;
16317
16318     case 1:  /* VLD2 / VST2.  */
16319       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 16, 16, 32,
16320                                        32, 64, -1);
16321       if (align_good == FAIL)
16322         return;
16323       if (do_align)
16324         inst.instruction |= 1 << 4;
16325       break;
16326
16327     case 2:  /* VLD3 / VST3.  */
16328       constraint (inst.operands[1].immisalign,
16329                   _("can't use alignment with this instruction"));
16330       break;
16331
16332     case 3:  /* VLD4 / VST4.  */
16333       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
16334                                        16, 64, 32, 64, 32, 128, -1);
16335       if (align_good == FAIL)
16336         return;
16337       if (do_align)
16338         {
16339           unsigned alignbits = 0;
16340           switch (et.size)
16341             {
16342             case 8:  alignbits = 0x1; break;
16343             case 16: alignbits = 0x1; break;
16344             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
16345             default: ;
16346             }
16347           inst.instruction |= alignbits << 4;
16348         }
16349       break;
16350
16351     default: ;
16352     }
16353
16354   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
16355   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16356     inst.instruction |= 1 << (4 + logsize);
16357
16358   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
16359   inst.instruction |= logsize << 10;
16360 }
16361
16362 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
16363
16364 static void
16365 do_neon_ld_dup (void)
16366 {
16367   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
16368   int align_good, do_align = 0;
16369
16370   if (et.type == NT_invtype)
16371     return;
16372
16373   switch ((inst.instruction >> 8) & 3)
16374     {
16375     case 0:  /* VLD1.  */
16376       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
16377       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
16378                                        &do_align, 16, 16, 32, 32, -1);
16379       if (align_good == FAIL)
16380         return;
16381       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
16382         {
16383         case 1: break;
16384         case 2: inst.instruction |= 1 << 5; break;
16385         default: first_error (_("bad list length")); return;
16386         }
16387       inst.instruction |= neon_logbits (et.size) << 6;
16388       break;
16389
16390     case 1:  /* VLD2.  */
16391       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
16392                                        &do_align, 8, 16, 16, 32, 32, 64, -1);
16393       if (align_good == FAIL)
16394         return;
16395       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
16396                   _("bad list length"));
16397       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16398         inst.instruction |= 1 << 5;
16399       inst.instruction |= neon_logbits (et.size) << 6;
16400       break;
16401
16402     case 2:  /* VLD3.  */
16403       constraint (inst.operands[1].immisalign,
16404                   _("can't use alignment with this instruction"));
16405       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
16406                   _("bad list length"));
16407       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16408         inst.instruction |= 1 << 5;
16409       inst.instruction |= neon_logbits (et.size) << 6;
16410       break;
16411
16412     case 3:  /* VLD4.  */
16413       {
16414         int align = inst.operands[1].imm >> 8;
16415         align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
16416                                          16, 64, 32, 64, 32, 128, -1);
16417         if (align_good == FAIL)
16418           return;
16419         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
16420                     _("bad list length"));
16421         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16422           inst.instruction |= 1 << 5;
16423         if (et.size == 32 && align == 128)
16424           inst.instruction |= 0x3 << 6;
16425         else
16426           inst.instruction |= neon_logbits (et.size) << 6;
16427       }
16428       break;
16429
16430     default: ;
16431     }
16432
16433   inst.instruction |= do_align << 4;
16434 }
16435
16436 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
16437    apart from bits [11:4].  */
16438
16439 static void
16440 do_neon_ldx_stx (void)
16441 {
16442   if (inst.operands[1].isreg)
16443     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
16444
16445   switch (NEON_LANE (inst.operands[0].imm))
16446     {
16447     case NEON_INTERLEAVE_LANES:
16448       NEON_ENCODE (INTERLV, inst);
16449       do_neon_ld_st_interleave ();
16450       break;
16451
16452     case NEON_ALL_LANES:
16453       NEON_ENCODE (DUP, inst);
16454       if (inst.instruction == N_INV)
16455         {
16456           first_error ("only loads support such operands");
16457           break;
16458         }
16459       do_neon_ld_dup ();
16460       break;
16461
16462     default:
16463       NEON_ENCODE (LANE, inst);
16464       do_neon_ld_st_lane ();
16465     }
16466
16467   /* L bit comes from bit mask.  */
16468   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16469   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16470   inst.instruction |= inst.operands[1].reg << 16;
16471
16472   if (inst.operands[1].postind)
16473     {
16474       int postreg = inst.operands[1].imm & 0xf;
16475       constraint (!inst.operands[1].immisreg,
16476                   _("post-index must be a register"));
16477       constraint (postreg == 0xd || postreg == 0xf,
16478                   _("bad register for post-index"));
16479       inst.instruction |= postreg;
16480     }
16481   else
16482     {
16483       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
16484       constraint (inst.reloc.exp.X_op != O_constant
16485                   || inst.reloc.exp.X_add_number != 0,
16486                   BAD_ADDR_MODE);
16487
16488       if (inst.operands[1].writeback)
16489         {
16490           inst.instruction |= 0xd;
16491         }
16492       else
16493         inst.instruction |= 0xf;
16494     }
16495
16496   if (thumb_mode)
16497     inst.instruction |= 0xf9000000;
16498   else
16499     inst.instruction |= 0xf4000000;
16500 }
16501
16502 /* FP v8.  */
16503 static void
16504 do_vfp_nsyn_fpv8 (enum neon_shape rs)
16505 {
16506   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
16507      D register operands.  */
16508   if (neon_shape_class[rs] == SC_DOUBLE)
16509     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16510                 _(BAD_FPU));
16511
16512   NEON_ENCODE (FPV8, inst);
16513
16514   if (rs == NS_FFF)
16515     do_vfp_sp_dyadic ();
16516   else
16517     do_vfp_dp_rd_rn_rm ();
16518
16519   if (rs == NS_DDD)
16520     inst.instruction |= 0x100;
16521
16522   inst.instruction |= 0xf0000000;
16523 }
16524
16525 static void
16526 do_vsel (void)
16527 {
16528   set_it_insn_type (OUTSIDE_IT_INSN);
16529
16530   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
16531     first_error (_("invalid instruction shape"));
16532 }
16533
16534 static void
16535 do_vmaxnm (void)
16536 {
16537   set_it_insn_type (OUTSIDE_IT_INSN);
16538
16539   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
16540     return;
16541
16542   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
16543     return;
16544
16545   neon_dyadic_misc (NT_untyped, N_F32, 0);
16546 }
16547
16548 static void
16549 do_vrint_1 (enum neon_cvt_mode mode)
16550 {
16551   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_QQ, NS_NULL);
16552   struct neon_type_el et;
16553
16554   if (rs == NS_NULL)
16555     return;
16556
16557   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
16558      D register operands.  */
16559   if (neon_shape_class[rs] == SC_DOUBLE)
16560     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16561                 _(BAD_FPU));
16562
16563   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
16564   if (et.type != NT_invtype)
16565     {
16566       /* VFP encodings.  */
16567       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
16568           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
16569         set_it_insn_type (OUTSIDE_IT_INSN);
16570
16571       NEON_ENCODE (FPV8, inst);
16572       if (rs == NS_FF)
16573         do_vfp_sp_monadic ();
16574       else
16575         do_vfp_dp_rd_rm ();
16576
16577       switch (mode)
16578         {
16579         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
16580         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
16581         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
16582         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
16583         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
16584         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
16585         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
16586         default: abort ();
16587         }
16588
16589       inst.instruction |= (rs == NS_DD) << 8;
16590       do_vfp_cond_or_thumb ();
16591     }
16592   else
16593     {
16594       /* Neon encodings (or something broken...).  */
16595       inst.error = NULL;
16596       et = neon_check_type (2, rs, N_EQK, N_F32 | N_KEY);
16597
16598       if (et.type == NT_invtype)
16599         return;
16600
16601       set_it_insn_type (OUTSIDE_IT_INSN);
16602       NEON_ENCODE (FLOAT, inst);
16603
16604       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
16605         return;
16606
16607       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16608       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16609       inst.instruction |= LOW4 (inst.operands[1].reg);
16610       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16611       inst.instruction |= neon_quad (rs) << 6;
16612       switch (mode)
16613         {
16614         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
16615         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
16616         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
16617         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
16618         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
16619         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
16620         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
16621         default: abort ();
16622         }
16623
16624       if (thumb_mode)
16625         inst.instruction |= 0xfc000000;
16626       else
16627         inst.instruction |= 0xf0000000;
16628     }
16629 }
16630
16631 static void
16632 do_vrintx (void)
16633 {
16634   do_vrint_1 (neon_cvt_mode_x);
16635 }
16636
16637 static void
16638 do_vrintz (void)
16639 {
16640   do_vrint_1 (neon_cvt_mode_z);
16641 }
16642
16643 static void
16644 do_vrintr (void)
16645 {
16646   do_vrint_1 (neon_cvt_mode_r);
16647 }
16648
16649 static void
16650 do_vrinta (void)
16651 {
16652   do_vrint_1 (neon_cvt_mode_a);
16653 }
16654
16655 static void
16656 do_vrintn (void)
16657 {
16658   do_vrint_1 (neon_cvt_mode_n);
16659 }
16660
16661 static void
16662 do_vrintp (void)
16663 {
16664   do_vrint_1 (neon_cvt_mode_p);
16665 }
16666
16667 static void
16668 do_vrintm (void)
16669 {
16670   do_vrint_1 (neon_cvt_mode_m);
16671 }
16672
16673 /* Crypto v1 instructions.  */
16674 static void
16675 do_crypto_2op_1 (unsigned elttype, int op)
16676 {
16677   set_it_insn_type (OUTSIDE_IT_INSN);
16678
16679   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
16680       == NT_invtype)
16681     return;
16682
16683   inst.error = NULL;
16684
16685   NEON_ENCODE (INTEGER, inst);
16686   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16687   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16688   inst.instruction |= LOW4 (inst.operands[1].reg);
16689   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16690   if (op != -1)
16691     inst.instruction |= op << 6;
16692
16693   if (thumb_mode)
16694     inst.instruction |= 0xfc000000;
16695   else
16696     inst.instruction |= 0xf0000000;
16697 }
16698
16699 static void
16700 do_crypto_3op_1 (int u, int op)
16701 {
16702   set_it_insn_type (OUTSIDE_IT_INSN);
16703
16704   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
16705                        N_32 | N_UNT | N_KEY).type == NT_invtype)
16706     return;
16707
16708   inst.error = NULL;
16709
16710   NEON_ENCODE (INTEGER, inst);
16711   neon_three_same (1, u, 8 << op);
16712 }
16713
16714 static void
16715 do_aese (void)
16716 {
16717   do_crypto_2op_1 (N_8, 0);
16718 }
16719
16720 static void
16721 do_aesd (void)
16722 {
16723   do_crypto_2op_1 (N_8, 1);
16724 }
16725
16726 static void
16727 do_aesmc (void)
16728 {
16729   do_crypto_2op_1 (N_8, 2);
16730 }
16731
16732 static void
16733 do_aesimc (void)
16734 {
16735   do_crypto_2op_1 (N_8, 3);
16736 }
16737
16738 static void
16739 do_sha1c (void)
16740 {
16741   do_crypto_3op_1 (0, 0);
16742 }
16743
16744 static void
16745 do_sha1p (void)
16746 {
16747   do_crypto_3op_1 (0, 1);
16748 }
16749
16750 static void
16751 do_sha1m (void)
16752 {
16753   do_crypto_3op_1 (0, 2);
16754 }
16755
16756 static void
16757 do_sha1su0 (void)
16758 {
16759   do_crypto_3op_1 (0, 3);
16760 }
16761
16762 static void
16763 do_sha256h (void)
16764 {
16765   do_crypto_3op_1 (1, 0);
16766 }
16767
16768 static void
16769 do_sha256h2 (void)
16770 {
16771   do_crypto_3op_1 (1, 1);
16772 }
16773
16774 static void
16775 do_sha256su1 (void)
16776 {
16777   do_crypto_3op_1 (1, 2);
16778 }
16779
16780 static void
16781 do_sha1h (void)
16782 {
16783   do_crypto_2op_1 (N_32, -1);
16784 }
16785
16786 static void
16787 do_sha1su1 (void)
16788 {
16789   do_crypto_2op_1 (N_32, 0);
16790 }
16791
16792 static void
16793 do_sha256su0 (void)
16794 {
16795   do_crypto_2op_1 (N_32, 1);
16796 }
16797
16798 static void
16799 do_crc32_1 (unsigned int poly, unsigned int sz)
16800 {
16801   unsigned int Rd = inst.operands[0].reg;
16802   unsigned int Rn = inst.operands[1].reg;
16803   unsigned int Rm = inst.operands[2].reg;
16804
16805   set_it_insn_type (OUTSIDE_IT_INSN);
16806   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
16807   inst.instruction |= LOW4 (Rn) << 16;
16808   inst.instruction |= LOW4 (Rm);
16809   inst.instruction |= sz << (thumb_mode ? 4 : 21);
16810   inst.instruction |= poly << (thumb_mode ? 20 : 9);
16811
16812   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
16813     as_warn (UNPRED_REG ("r15"));
16814   if (thumb_mode && (Rd == REG_SP || Rn == REG_SP || Rm == REG_SP))
16815     as_warn (UNPRED_REG ("r13"));
16816 }
16817
16818 static void
16819 do_crc32b (void)
16820 {
16821   do_crc32_1 (0, 0);
16822 }
16823
16824 static void
16825 do_crc32h (void)
16826 {
16827   do_crc32_1 (0, 1);
16828 }
16829
16830 static void
16831 do_crc32w (void)
16832 {
16833   do_crc32_1 (0, 2);
16834 }
16835
16836 static void
16837 do_crc32cb (void)
16838 {
16839   do_crc32_1 (1, 0);
16840 }
16841
16842 static void
16843 do_crc32ch (void)
16844 {
16845   do_crc32_1 (1, 1);
16846 }
16847
16848 static void
16849 do_crc32cw (void)
16850 {
16851   do_crc32_1 (1, 2);
16852 }
16853
16854 \f
16855 /* Overall per-instruction processing.  */
16856
16857 /* We need to be able to fix up arbitrary expressions in some statements.
16858    This is so that we can handle symbols that are an arbitrary distance from
16859    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
16860    which returns part of an address in a form which will be valid for
16861    a data instruction.  We do this by pushing the expression into a symbol
16862    in the expr_section, and creating a fix for that.  */
16863
16864 static void
16865 fix_new_arm (fragS *       frag,
16866              int           where,
16867              short int     size,
16868              expressionS * exp,
16869              int           pc_rel,
16870              int           reloc)
16871 {
16872   fixS *           new_fix;
16873
16874   switch (exp->X_op)
16875     {
16876     case O_constant:
16877       if (pc_rel)
16878         {
16879           /* Create an absolute valued symbol, so we have something to
16880              refer to in the object file.  Unfortunately for us, gas's
16881              generic expression parsing will already have folded out
16882              any use of .set foo/.type foo %function that may have
16883              been used to set type information of the target location,
16884              that's being specified symbolically.  We have to presume
16885              the user knows what they are doing.  */
16886           char name[16 + 8];
16887           symbolS *symbol;
16888
16889           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
16890
16891           symbol = symbol_find_or_make (name);
16892           S_SET_SEGMENT (symbol, absolute_section);
16893           symbol_set_frag (symbol, &zero_address_frag);
16894           S_SET_VALUE (symbol, exp->X_add_number);
16895           exp->X_op = O_symbol;
16896           exp->X_add_symbol = symbol;
16897           exp->X_add_number = 0;
16898         }
16899       /* FALLTHROUGH */
16900     case O_symbol:
16901     case O_add:
16902     case O_subtract:
16903       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
16904                              (enum bfd_reloc_code_real) reloc);
16905       break;
16906
16907     default:
16908       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
16909                                   pc_rel, (enum bfd_reloc_code_real) reloc);
16910       break;
16911     }
16912
16913   /* Mark whether the fix is to a THUMB instruction, or an ARM
16914      instruction.  */
16915   new_fix->tc_fix_data = thumb_mode;
16916 }
16917
16918 /* Create a frg for an instruction requiring relaxation.  */
16919 static void
16920 output_relax_insn (void)
16921 {
16922   char * to;
16923   symbolS *sym;
16924   int offset;
16925
16926   /* The size of the instruction is unknown, so tie the debug info to the
16927      start of the instruction.  */
16928   dwarf2_emit_insn (0);
16929
16930   switch (inst.reloc.exp.X_op)
16931     {
16932     case O_symbol:
16933       sym = inst.reloc.exp.X_add_symbol;
16934       offset = inst.reloc.exp.X_add_number;
16935       break;
16936     case O_constant:
16937       sym = NULL;
16938       offset = inst.reloc.exp.X_add_number;
16939       break;
16940     default:
16941       sym = make_expr_symbol (&inst.reloc.exp);
16942       offset = 0;
16943       break;
16944   }
16945   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
16946                  inst.relax, sym, offset, NULL/*offset, opcode*/);
16947   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
16948 }
16949
16950 /* Write a 32-bit thumb instruction to buf.  */
16951 static void
16952 put_thumb32_insn (char * buf, unsigned long insn)
16953 {
16954   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
16955   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
16956 }
16957
16958 static void
16959 output_inst (const char * str)
16960 {
16961   char * to = NULL;
16962
16963   if (inst.error)
16964     {
16965       as_bad ("%s -- `%s'", inst.error, str);
16966       return;
16967     }
16968   if (inst.relax)
16969     {
16970       output_relax_insn ();
16971       return;
16972     }
16973   if (inst.size == 0)
16974     return;
16975
16976   to = frag_more (inst.size);
16977   /* PR 9814: Record the thumb mode into the current frag so that we know
16978      what type of NOP padding to use, if necessary.  We override any previous
16979      setting so that if the mode has changed then the NOPS that we use will
16980      match the encoding of the last instruction in the frag.  */
16981   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
16982
16983   if (thumb_mode && (inst.size > THUMB_SIZE))
16984     {
16985       gas_assert (inst.size == (2 * THUMB_SIZE));
16986       put_thumb32_insn (to, inst.instruction);
16987     }
16988   else if (inst.size > INSN_SIZE)
16989     {
16990       gas_assert (inst.size == (2 * INSN_SIZE));
16991       md_number_to_chars (to, inst.instruction, INSN_SIZE);
16992       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
16993     }
16994   else
16995     md_number_to_chars (to, inst.instruction, inst.size);
16996
16997   if (inst.reloc.type != BFD_RELOC_UNUSED)
16998     fix_new_arm (frag_now, to - frag_now->fr_literal,
16999                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
17000                  inst.reloc.type);
17001
17002   dwarf2_emit_insn (inst.size);
17003 }
17004
17005 static char *
17006 output_it_inst (int cond, int mask, char * to)
17007 {
17008   unsigned long instruction = 0xbf00;
17009
17010   mask &= 0xf;
17011   instruction |= mask;
17012   instruction |= cond << 4;
17013
17014   if (to == NULL)
17015     {
17016       to = frag_more (2);
17017 #ifdef OBJ_ELF
17018       dwarf2_emit_insn (2);
17019 #endif
17020     }
17021
17022   md_number_to_chars (to, instruction, 2);
17023
17024   return to;
17025 }
17026
17027 /* Tag values used in struct asm_opcode's tag field.  */
17028 enum opcode_tag
17029 {
17030   OT_unconditional,     /* Instruction cannot be conditionalized.
17031                            The ARM condition field is still 0xE.  */
17032   OT_unconditionalF,    /* Instruction cannot be conditionalized
17033                            and carries 0xF in its ARM condition field.  */
17034   OT_csuffix,           /* Instruction takes a conditional suffix.  */
17035   OT_csuffixF,          /* Some forms of the instruction take a conditional
17036                            suffix, others place 0xF where the condition field
17037                            would be.  */
17038   OT_cinfix3,           /* Instruction takes a conditional infix,
17039                            beginning at character index 3.  (In
17040                            unified mode, it becomes a suffix.)  */
17041   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
17042                             tsts, cmps, cmns, and teqs. */
17043   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
17044                            character index 3, even in unified mode.  Used for
17045                            legacy instructions where suffix and infix forms
17046                            may be ambiguous.  */
17047   OT_csuf_or_in3,       /* Instruction takes either a conditional
17048                            suffix or an infix at character index 3.  */
17049   OT_odd_infix_unc,     /* This is the unconditional variant of an
17050                            instruction that takes a conditional infix
17051                            at an unusual position.  In unified mode,
17052                            this variant will accept a suffix.  */
17053   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
17054                            are the conditional variants of instructions that
17055                            take conditional infixes in unusual positions.
17056                            The infix appears at character index
17057                            (tag - OT_odd_infix_0).  These are not accepted
17058                            in unified mode.  */
17059 };
17060
17061 /* Subroutine of md_assemble, responsible for looking up the primary
17062    opcode from the mnemonic the user wrote.  STR points to the
17063    beginning of the mnemonic.
17064
17065    This is not simply a hash table lookup, because of conditional
17066    variants.  Most instructions have conditional variants, which are
17067    expressed with a _conditional affix_ to the mnemonic.  If we were
17068    to encode each conditional variant as a literal string in the opcode
17069    table, it would have approximately 20,000 entries.
17070
17071    Most mnemonics take this affix as a suffix, and in unified syntax,
17072    'most' is upgraded to 'all'.  However, in the divided syntax, some
17073    instructions take the affix as an infix, notably the s-variants of
17074    the arithmetic instructions.  Of those instructions, all but six
17075    have the infix appear after the third character of the mnemonic.
17076
17077    Accordingly, the algorithm for looking up primary opcodes given
17078    an identifier is:
17079
17080    1. Look up the identifier in the opcode table.
17081       If we find a match, go to step U.
17082
17083    2. Look up the last two characters of the identifier in the
17084       conditions table.  If we find a match, look up the first N-2
17085       characters of the identifier in the opcode table.  If we
17086       find a match, go to step CE.
17087
17088    3. Look up the fourth and fifth characters of the identifier in
17089       the conditions table.  If we find a match, extract those
17090       characters from the identifier, and look up the remaining
17091       characters in the opcode table.  If we find a match, go
17092       to step CM.
17093
17094    4. Fail.
17095
17096    U. Examine the tag field of the opcode structure, in case this is
17097       one of the six instructions with its conditional infix in an
17098       unusual place.  If it is, the tag tells us where to find the
17099       infix; look it up in the conditions table and set inst.cond
17100       accordingly.  Otherwise, this is an unconditional instruction.
17101       Again set inst.cond accordingly.  Return the opcode structure.
17102
17103   CE. Examine the tag field to make sure this is an instruction that
17104       should receive a conditional suffix.  If it is not, fail.
17105       Otherwise, set inst.cond from the suffix we already looked up,
17106       and return the opcode structure.
17107
17108   CM. Examine the tag field to make sure this is an instruction that
17109       should receive a conditional infix after the third character.
17110       If it is not, fail.  Otherwise, undo the edits to the current
17111       line of input and proceed as for case CE.  */
17112
17113 static const struct asm_opcode *
17114 opcode_lookup (char **str)
17115 {
17116   char *end, *base;
17117   char *affix;
17118   const struct asm_opcode *opcode;
17119   const struct asm_cond *cond;
17120   char save[2];
17121
17122   /* Scan up to the end of the mnemonic, which must end in white space,
17123      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
17124   for (base = end = *str; *end != '\0'; end++)
17125     if (*end == ' ' || *end == '.')
17126       break;
17127
17128   if (end == base)
17129     return NULL;
17130
17131   /* Handle a possible width suffix and/or Neon type suffix.  */
17132   if (end[0] == '.')
17133     {
17134       int offset = 2;
17135
17136       /* The .w and .n suffixes are only valid if the unified syntax is in
17137          use.  */
17138       if (unified_syntax && end[1] == 'w')
17139         inst.size_req = 4;
17140       else if (unified_syntax && end[1] == 'n')
17141         inst.size_req = 2;
17142       else
17143         offset = 0;
17144
17145       inst.vectype.elems = 0;
17146
17147       *str = end + offset;
17148
17149       if (end[offset] == '.')
17150         {
17151           /* See if we have a Neon type suffix (possible in either unified or
17152              non-unified ARM syntax mode).  */
17153           if (parse_neon_type (&inst.vectype, str) == FAIL)
17154             return NULL;
17155         }
17156       else if (end[offset] != '\0' && end[offset] != ' ')
17157         return NULL;
17158     }
17159   else
17160     *str = end;
17161
17162   /* Look for unaffixed or special-case affixed mnemonic.  */
17163   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17164                                                     end - base);
17165   if (opcode)
17166     {
17167       /* step U */
17168       if (opcode->tag < OT_odd_infix_0)
17169         {
17170           inst.cond = COND_ALWAYS;
17171           return opcode;
17172         }
17173
17174       if (warn_on_deprecated && unified_syntax)
17175         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
17176       affix = base + (opcode->tag - OT_odd_infix_0);
17177       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17178       gas_assert (cond);
17179
17180       inst.cond = cond->value;
17181       return opcode;
17182     }
17183
17184   /* Cannot have a conditional suffix on a mnemonic of less than two
17185      characters.  */
17186   if (end - base < 3)
17187     return NULL;
17188
17189   /* Look for suffixed mnemonic.  */
17190   affix = end - 2;
17191   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17192   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17193                                                     affix - base);
17194   if (opcode && cond)
17195     {
17196       /* step CE */
17197       switch (opcode->tag)
17198         {
17199         case OT_cinfix3_legacy:
17200           /* Ignore conditional suffixes matched on infix only mnemonics.  */
17201           break;
17202
17203         case OT_cinfix3:
17204         case OT_cinfix3_deprecated:
17205         case OT_odd_infix_unc:
17206           if (!unified_syntax)
17207             return 0;
17208           /* else fall through */
17209
17210         case OT_csuffix:
17211         case OT_csuffixF:
17212         case OT_csuf_or_in3:
17213           inst.cond = cond->value;
17214           return opcode;
17215
17216         case OT_unconditional:
17217         case OT_unconditionalF:
17218           if (thumb_mode)
17219             inst.cond = cond->value;
17220           else
17221             {
17222               /* Delayed diagnostic.  */
17223               inst.error = BAD_COND;
17224               inst.cond = COND_ALWAYS;
17225             }
17226           return opcode;
17227
17228         default:
17229           return NULL;
17230         }
17231     }
17232
17233   /* Cannot have a usual-position infix on a mnemonic of less than
17234      six characters (five would be a suffix).  */
17235   if (end - base < 6)
17236     return NULL;
17237
17238   /* Look for infixed mnemonic in the usual position.  */
17239   affix = base + 3;
17240   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17241   if (!cond)
17242     return NULL;
17243
17244   memcpy (save, affix, 2);
17245   memmove (affix, affix + 2, (end - affix) - 2);
17246   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17247                                                     (end - base) - 2);
17248   memmove (affix + 2, affix, (end - affix) - 2);
17249   memcpy (affix, save, 2);
17250
17251   if (opcode
17252       && (opcode->tag == OT_cinfix3
17253           || opcode->tag == OT_cinfix3_deprecated
17254           || opcode->tag == OT_csuf_or_in3
17255           || opcode->tag == OT_cinfix3_legacy))
17256     {
17257       /* Step CM.  */
17258       if (warn_on_deprecated && unified_syntax
17259           && (opcode->tag == OT_cinfix3
17260               || opcode->tag == OT_cinfix3_deprecated))
17261         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
17262
17263       inst.cond = cond->value;
17264       return opcode;
17265     }
17266
17267   return NULL;
17268 }
17269
17270 /* This function generates an initial IT instruction, leaving its block
17271    virtually open for the new instructions. Eventually,
17272    the mask will be updated by now_it_add_mask () each time
17273    a new instruction needs to be included in the IT block.
17274    Finally, the block is closed with close_automatic_it_block ().
17275    The block closure can be requested either from md_assemble (),
17276    a tencode (), or due to a label hook.  */
17277
17278 static void
17279 new_automatic_it_block (int cond)
17280 {
17281   now_it.state = AUTOMATIC_IT_BLOCK;
17282   now_it.mask = 0x18;
17283   now_it.cc = cond;
17284   now_it.block_length = 1;
17285   mapping_state (MAP_THUMB);
17286   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
17287   now_it.warn_deprecated = FALSE;
17288   now_it.insn_cond = TRUE;
17289 }
17290
17291 /* Close an automatic IT block.
17292    See comments in new_automatic_it_block ().  */
17293
17294 static void
17295 close_automatic_it_block (void)
17296 {
17297   now_it.mask = 0x10;
17298   now_it.block_length = 0;
17299 }
17300
17301 /* Update the mask of the current automatically-generated IT
17302    instruction. See comments in new_automatic_it_block ().  */
17303
17304 static void
17305 now_it_add_mask (int cond)
17306 {
17307 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
17308 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
17309                                               | ((bitvalue) << (nbit)))
17310   const int resulting_bit = (cond & 1);
17311
17312   now_it.mask &= 0xf;
17313   now_it.mask = SET_BIT_VALUE (now_it.mask,
17314                                    resulting_bit,
17315                                   (5 - now_it.block_length));
17316   now_it.mask = SET_BIT_VALUE (now_it.mask,
17317                                    1,
17318                                    ((5 - now_it.block_length) - 1) );
17319   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
17320
17321 #undef CLEAR_BIT
17322 #undef SET_BIT_VALUE
17323 }
17324
17325 /* The IT blocks handling machinery is accessed through the these functions:
17326      it_fsm_pre_encode ()               from md_assemble ()
17327      set_it_insn_type ()                optional, from the tencode functions
17328      set_it_insn_type_last ()           ditto
17329      in_it_block ()                     ditto
17330      it_fsm_post_encode ()              from md_assemble ()
17331      force_automatic_it_block_close ()  from label habdling functions
17332
17333    Rationale:
17334      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
17335         initializing the IT insn type with a generic initial value depending
17336         on the inst.condition.
17337      2) During the tencode function, two things may happen:
17338         a) The tencode function overrides the IT insn type by
17339            calling either set_it_insn_type (type) or set_it_insn_type_last ().
17340         b) The tencode function queries the IT block state by
17341            calling in_it_block () (i.e. to determine narrow/not narrow mode).
17342
17343         Both set_it_insn_type and in_it_block run the internal FSM state
17344         handling function (handle_it_state), because: a) setting the IT insn
17345         type may incur in an invalid state (exiting the function),
17346         and b) querying the state requires the FSM to be updated.
17347         Specifically we want to avoid creating an IT block for conditional
17348         branches, so it_fsm_pre_encode is actually a guess and we can't
17349         determine whether an IT block is required until the tencode () routine
17350         has decided what type of instruction this actually it.
17351         Because of this, if set_it_insn_type and in_it_block have to be used,
17352         set_it_insn_type has to be called first.
17353
17354         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
17355         determines the insn IT type depending on the inst.cond code.
17356         When a tencode () routine encodes an instruction that can be
17357         either outside an IT block, or, in the case of being inside, has to be
17358         the last one, set_it_insn_type_last () will determine the proper
17359         IT instruction type based on the inst.cond code. Otherwise,
17360         set_it_insn_type can be called for overriding that logic or
17361         for covering other cases.
17362
17363         Calling handle_it_state () may not transition the IT block state to
17364         OUTSIDE_IT_BLOCK immediatelly, since the (current) state could be
17365         still queried. Instead, if the FSM determines that the state should
17366         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
17367         after the tencode () function: that's what it_fsm_post_encode () does.
17368
17369         Since in_it_block () calls the state handling function to get an
17370         updated state, an error may occur (due to invalid insns combination).
17371         In that case, inst.error is set.
17372         Therefore, inst.error has to be checked after the execution of
17373         the tencode () routine.
17374
17375      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
17376         any pending state change (if any) that didn't take place in
17377         handle_it_state () as explained above.  */
17378
17379 static void
17380 it_fsm_pre_encode (void)
17381 {
17382   if (inst.cond != COND_ALWAYS)
17383     inst.it_insn_type = INSIDE_IT_INSN;
17384   else
17385     inst.it_insn_type = OUTSIDE_IT_INSN;
17386
17387   now_it.state_handled = 0;
17388 }
17389
17390 /* IT state FSM handling function.  */
17391
17392 static int
17393 handle_it_state (void)
17394 {
17395   now_it.state_handled = 1;
17396   now_it.insn_cond = FALSE;
17397
17398   switch (now_it.state)
17399     {
17400     case OUTSIDE_IT_BLOCK:
17401       switch (inst.it_insn_type)
17402         {
17403         case OUTSIDE_IT_INSN:
17404           break;
17405
17406         case INSIDE_IT_INSN:
17407         case INSIDE_IT_LAST_INSN:
17408           if (thumb_mode == 0)
17409             {
17410               if (unified_syntax
17411                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
17412                 as_tsktsk (_("Warning: conditional outside an IT block"\
17413                              " for Thumb."));
17414             }
17415           else
17416             {
17417               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
17418                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2))
17419                 {
17420                   /* Automatically generate the IT instruction.  */
17421                   new_automatic_it_block (inst.cond);
17422                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
17423                     close_automatic_it_block ();
17424                 }
17425               else
17426                 {
17427                   inst.error = BAD_OUT_IT;
17428                   return FAIL;
17429                 }
17430             }
17431           break;
17432
17433         case IF_INSIDE_IT_LAST_INSN:
17434         case NEUTRAL_IT_INSN:
17435           break;
17436
17437         case IT_INSN:
17438           now_it.state = MANUAL_IT_BLOCK;
17439           now_it.block_length = 0;
17440           break;
17441         }
17442       break;
17443
17444     case AUTOMATIC_IT_BLOCK:
17445       /* Three things may happen now:
17446          a) We should increment current it block size;
17447          b) We should close current it block (closing insn or 4 insns);
17448          c) We should close current it block and start a new one (due
17449          to incompatible conditions or
17450          4 insns-length block reached).  */
17451
17452       switch (inst.it_insn_type)
17453         {
17454         case OUTSIDE_IT_INSN:
17455           /* The closure of the block shall happen immediatelly,
17456              so any in_it_block () call reports the block as closed.  */
17457           force_automatic_it_block_close ();
17458           break;
17459
17460         case INSIDE_IT_INSN:
17461         case INSIDE_IT_LAST_INSN:
17462         case IF_INSIDE_IT_LAST_INSN:
17463           now_it.block_length++;
17464
17465           if (now_it.block_length > 4
17466               || !now_it_compatible (inst.cond))
17467             {
17468               force_automatic_it_block_close ();
17469               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
17470                 new_automatic_it_block (inst.cond);
17471             }
17472           else
17473             {
17474               now_it.insn_cond = TRUE;
17475               now_it_add_mask (inst.cond);
17476             }
17477
17478           if (now_it.state == AUTOMATIC_IT_BLOCK
17479               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
17480                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
17481             close_automatic_it_block ();
17482           break;
17483
17484         case NEUTRAL_IT_INSN:
17485           now_it.block_length++;
17486           now_it.insn_cond = TRUE;
17487
17488           if (now_it.block_length > 4)
17489             force_automatic_it_block_close ();
17490           else
17491             now_it_add_mask (now_it.cc & 1);
17492           break;
17493
17494         case IT_INSN:
17495           close_automatic_it_block ();
17496           now_it.state = MANUAL_IT_BLOCK;
17497           break;
17498         }
17499       break;
17500
17501     case MANUAL_IT_BLOCK:
17502       {
17503         /* Check conditional suffixes.  */
17504         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
17505         int is_last;
17506         now_it.mask <<= 1;
17507         now_it.mask &= 0x1f;
17508         is_last = (now_it.mask == 0x10);
17509         now_it.insn_cond = TRUE;
17510
17511         switch (inst.it_insn_type)
17512           {
17513           case OUTSIDE_IT_INSN:
17514             inst.error = BAD_NOT_IT;
17515             return FAIL;
17516
17517           case INSIDE_IT_INSN:
17518             if (cond != inst.cond)
17519               {
17520                 inst.error = BAD_IT_COND;
17521                 return FAIL;
17522               }
17523             break;
17524
17525           case INSIDE_IT_LAST_INSN:
17526           case IF_INSIDE_IT_LAST_INSN:
17527             if (cond != inst.cond)
17528               {
17529                 inst.error = BAD_IT_COND;
17530                 return FAIL;
17531               }
17532             if (!is_last)
17533               {
17534                 inst.error = BAD_BRANCH;
17535                 return FAIL;
17536               }
17537             break;
17538
17539           case NEUTRAL_IT_INSN:
17540             /* The BKPT instruction is unconditional even in an IT block.  */
17541             break;
17542
17543           case IT_INSN:
17544             inst.error = BAD_IT_IT;
17545             return FAIL;
17546           }
17547       }
17548       break;
17549     }
17550
17551   return SUCCESS;
17552 }
17553
17554 struct depr_insn_mask
17555 {
17556   unsigned long pattern;
17557   unsigned long mask;
17558   const char* description;
17559 };
17560
17561 /* List of 16-bit instruction patterns deprecated in an IT block in
17562    ARMv8.  */
17563 static const struct depr_insn_mask depr_it_insns[] = {
17564   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
17565   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
17566   { 0xa000, 0xb800, N_("ADR") },
17567   { 0x4800, 0xf800, N_("Literal loads") },
17568   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
17569   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
17570   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
17571      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
17572   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
17573   { 0, 0, NULL }
17574 };
17575
17576 static void
17577 it_fsm_post_encode (void)
17578 {
17579   int is_last;
17580
17581   if (!now_it.state_handled)
17582     handle_it_state ();
17583
17584   if (now_it.insn_cond
17585       && !now_it.warn_deprecated
17586       && warn_on_deprecated
17587       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
17588     {
17589       if (inst.instruction >= 0x10000)
17590         {
17591           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
17592                      "deprecated in ARMv8"));
17593           now_it.warn_deprecated = TRUE;
17594         }
17595       else
17596         {
17597           const struct depr_insn_mask *p = depr_it_insns;
17598
17599           while (p->mask != 0)
17600             {
17601               if ((inst.instruction & p->mask) == p->pattern)
17602                 {
17603                   as_tsktsk (_("IT blocks containing 16-bit Thumb instructions "
17604                              "of the following class are deprecated in ARMv8: "
17605                              "%s"), p->description);
17606                   now_it.warn_deprecated = TRUE;
17607                   break;
17608                 }
17609
17610               ++p;
17611             }
17612         }
17613
17614       if (now_it.block_length > 1)
17615         {
17616           as_tsktsk (_("IT blocks containing more than one conditional "
17617                      "instruction are deprecated in ARMv8"));
17618           now_it.warn_deprecated = TRUE;
17619         }
17620     }
17621
17622   is_last = (now_it.mask == 0x10);
17623   if (is_last)
17624     {
17625       now_it.state = OUTSIDE_IT_BLOCK;
17626       now_it.mask = 0;
17627     }
17628 }
17629
17630 static void
17631 force_automatic_it_block_close (void)
17632 {
17633   if (now_it.state == AUTOMATIC_IT_BLOCK)
17634     {
17635       close_automatic_it_block ();
17636       now_it.state = OUTSIDE_IT_BLOCK;
17637       now_it.mask = 0;
17638     }
17639 }
17640
17641 static int
17642 in_it_block (void)
17643 {
17644   if (!now_it.state_handled)
17645     handle_it_state ();
17646
17647   return now_it.state != OUTSIDE_IT_BLOCK;
17648 }
17649
17650 void
17651 md_assemble (char *str)
17652 {
17653   char *p = str;
17654   const struct asm_opcode * opcode;
17655
17656   /* Align the previous label if needed.  */
17657   if (last_label_seen != NULL)
17658     {
17659       symbol_set_frag (last_label_seen, frag_now);
17660       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
17661       S_SET_SEGMENT (last_label_seen, now_seg);
17662     }
17663
17664   memset (&inst, '\0', sizeof (inst));
17665   inst.reloc.type = BFD_RELOC_UNUSED;
17666
17667   opcode = opcode_lookup (&p);
17668   if (!opcode)
17669     {
17670       /* It wasn't an instruction, but it might be a register alias of
17671          the form alias .req reg, or a Neon .dn/.qn directive.  */
17672       if (! create_register_alias (str, p)
17673           && ! create_neon_reg_alias (str, p))
17674         as_bad (_("bad instruction `%s'"), str);
17675
17676       return;
17677     }
17678
17679   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
17680     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
17681
17682   /* The value which unconditional instructions should have in place of the
17683      condition field.  */
17684   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
17685
17686   if (thumb_mode)
17687     {
17688       arm_feature_set variant;
17689
17690       variant = cpu_variant;
17691       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
17692       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
17693         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
17694       /* Check that this instruction is supported for this CPU.  */
17695       if (!opcode->tvariant
17696           || (thumb_mode == 1
17697               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
17698         {
17699           as_bad (_("selected processor does not support Thumb mode `%s'"), str);
17700           return;
17701         }
17702       if (inst.cond != COND_ALWAYS && !unified_syntax
17703           && opcode->tencode != do_t_branch)
17704         {
17705           as_bad (_("Thumb does not support conditional execution"));
17706           return;
17707         }
17708
17709       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2))
17710         {
17711           if (opcode->tencode != do_t_blx && opcode->tencode != do_t_branch23
17712               && !(ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_msr)
17713                    || ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_barrier)))
17714             {
17715               /* Two things are addressed here.
17716                  1) Implicit require narrow instructions on Thumb-1.
17717                     This avoids relaxation accidentally introducing Thumb-2
17718                      instructions.
17719                  2) Reject wide instructions in non Thumb-2 cores.  */
17720               if (inst.size_req == 0)
17721                 inst.size_req = 2;
17722               else if (inst.size_req == 4)
17723                 {
17724                   as_bad (_("selected processor does not support Thumb-2 mode `%s'"), str);
17725                   return;
17726                 }
17727             }
17728         }
17729
17730       inst.instruction = opcode->tvalue;
17731
17732       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
17733         {
17734           /* Prepare the it_insn_type for those encodings that don't set
17735              it.  */
17736           it_fsm_pre_encode ();
17737
17738           opcode->tencode ();
17739
17740           it_fsm_post_encode ();
17741         }
17742
17743       if (!(inst.error || inst.relax))
17744         {
17745           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
17746           inst.size = (inst.instruction > 0xffff ? 4 : 2);
17747           if (inst.size_req && inst.size_req != inst.size)
17748             {
17749               as_bad (_("cannot honor width suffix -- `%s'"), str);
17750               return;
17751             }
17752         }
17753
17754       /* Something has gone badly wrong if we try to relax a fixed size
17755          instruction.  */
17756       gas_assert (inst.size_req == 0 || !inst.relax);
17757
17758       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
17759                               *opcode->tvariant);
17760       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
17761          set those bits when Thumb-2 32-bit instructions are seen.  ie.
17762          anything other than bl/blx and v6-M instructions.
17763          The impact of relaxable instructions will be considered later after we
17764          finish all relaxation.  */
17765       if ((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
17766           && !(ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
17767                || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier)))
17768         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
17769                                 arm_ext_v6t2);
17770
17771       check_neon_suffixes;
17772
17773       if (!inst.error)
17774         {
17775           mapping_state (MAP_THUMB);
17776         }
17777     }
17778   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
17779     {
17780       bfd_boolean is_bx;
17781
17782       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
17783       is_bx = (opcode->aencode == do_bx);
17784
17785       /* Check that this instruction is supported for this CPU.  */
17786       if (!(is_bx && fix_v4bx)
17787           && !(opcode->avariant &&
17788                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
17789         {
17790           as_bad (_("selected processor does not support ARM mode `%s'"), str);
17791           return;
17792         }
17793       if (inst.size_req)
17794         {
17795           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
17796           return;
17797         }
17798
17799       inst.instruction = opcode->avalue;
17800       if (opcode->tag == OT_unconditionalF)
17801         inst.instruction |= 0xF << 28;
17802       else
17803         inst.instruction |= inst.cond << 28;
17804       inst.size = INSN_SIZE;
17805       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
17806         {
17807           it_fsm_pre_encode ();
17808           opcode->aencode ();
17809           it_fsm_post_encode ();
17810         }
17811       /* Arm mode bx is marked as both v4T and v5 because it's still required
17812          on a hypothetical non-thumb v5 core.  */
17813       if (is_bx)
17814         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
17815       else
17816         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
17817                                 *opcode->avariant);
17818
17819       check_neon_suffixes;
17820
17821       if (!inst.error)
17822         {
17823           mapping_state (MAP_ARM);
17824         }
17825     }
17826   else
17827     {
17828       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
17829                 "-- `%s'"), str);
17830       return;
17831     }
17832   output_inst (str);
17833 }
17834
17835 static void
17836 check_it_blocks_finished (void)
17837 {
17838 #ifdef OBJ_ELF
17839   asection *sect;
17840
17841   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
17842     if (seg_info (sect)->tc_segment_info_data.current_it.state
17843         == MANUAL_IT_BLOCK)
17844       {
17845         as_warn (_("section '%s' finished with an open IT block."),
17846                  sect->name);
17847       }
17848 #else
17849   if (now_it.state == MANUAL_IT_BLOCK)
17850     as_warn (_("file finished with an open IT block."));
17851 #endif
17852 }
17853
17854 /* Various frobbings of labels and their addresses.  */
17855
17856 void
17857 arm_start_line_hook (void)
17858 {
17859   last_label_seen = NULL;
17860 }
17861
17862 void
17863 arm_frob_label (symbolS * sym)
17864 {
17865   last_label_seen = sym;
17866
17867   ARM_SET_THUMB (sym, thumb_mode);
17868
17869 #if defined OBJ_COFF || defined OBJ_ELF
17870   ARM_SET_INTERWORK (sym, support_interwork);
17871 #endif
17872
17873   force_automatic_it_block_close ();
17874
17875   /* Note - do not allow local symbols (.Lxxx) to be labelled
17876      as Thumb functions.  This is because these labels, whilst
17877      they exist inside Thumb code, are not the entry points for
17878      possible ARM->Thumb calls.  Also, these labels can be used
17879      as part of a computed goto or switch statement.  eg gcc
17880      can generate code that looks like this:
17881
17882                 ldr  r2, [pc, .Laaa]
17883                 lsl  r3, r3, #2
17884                 ldr  r2, [r3, r2]
17885                 mov  pc, r2
17886
17887        .Lbbb:  .word .Lxxx
17888        .Lccc:  .word .Lyyy
17889        ..etc...
17890        .Laaa:   .word Lbbb
17891
17892      The first instruction loads the address of the jump table.
17893      The second instruction converts a table index into a byte offset.
17894      The third instruction gets the jump address out of the table.
17895      The fourth instruction performs the jump.
17896
17897      If the address stored at .Laaa is that of a symbol which has the
17898      Thumb_Func bit set, then the linker will arrange for this address
17899      to have the bottom bit set, which in turn would mean that the
17900      address computation performed by the third instruction would end
17901      up with the bottom bit set.  Since the ARM is capable of unaligned
17902      word loads, the instruction would then load the incorrect address
17903      out of the jump table, and chaos would ensue.  */
17904   if (label_is_thumb_function_name
17905       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
17906       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
17907     {
17908       /* When the address of a Thumb function is taken the bottom
17909          bit of that address should be set.  This will allow
17910          interworking between Arm and Thumb functions to work
17911          correctly.  */
17912
17913       THUMB_SET_FUNC (sym, 1);
17914
17915       label_is_thumb_function_name = FALSE;
17916     }
17917
17918   dwarf2_emit_label (sym);
17919 }
17920
17921 bfd_boolean
17922 arm_data_in_code (void)
17923 {
17924   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
17925     {
17926       *input_line_pointer = '/';
17927       input_line_pointer += 5;
17928       *input_line_pointer = 0;
17929       return TRUE;
17930     }
17931
17932   return FALSE;
17933 }
17934
17935 char *
17936 arm_canonicalize_symbol_name (char * name)
17937 {
17938   int len;
17939
17940   if (thumb_mode && (len = strlen (name)) > 5
17941       && streq (name + len - 5, "/data"))
17942     *(name + len - 5) = 0;
17943
17944   return name;
17945 }
17946 \f
17947 /* Table of all register names defined by default.  The user can
17948    define additional names with .req.  Note that all register names
17949    should appear in both upper and lowercase variants.  Some registers
17950    also have mixed-case names.  */
17951
17952 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
17953 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
17954 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
17955 #define REGSET(p,t) \
17956   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
17957   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
17958   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
17959   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
17960 #define REGSETH(p,t) \
17961   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
17962   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
17963   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
17964   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
17965 #define REGSET2(p,t) \
17966   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
17967   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
17968   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
17969   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
17970 #define SPLRBANK(base,bank,t) \
17971   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
17972   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
17973   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
17974   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
17975   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
17976   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
17977
17978 static const struct reg_entry reg_names[] =
17979 {
17980   /* ARM integer registers.  */
17981   REGSET(r, RN), REGSET(R, RN),
17982
17983   /* ATPCS synonyms.  */
17984   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
17985   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
17986   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
17987
17988   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
17989   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
17990   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
17991
17992   /* Well-known aliases.  */
17993   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
17994   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
17995
17996   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
17997   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
17998
17999   /* Coprocessor numbers.  */
18000   REGSET(p, CP), REGSET(P, CP),
18001
18002   /* Coprocessor register numbers.  The "cr" variants are for backward
18003      compatibility.  */
18004   REGSET(c,  CN), REGSET(C, CN),
18005   REGSET(cr, CN), REGSET(CR, CN),
18006
18007   /* ARM banked registers.  */
18008   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
18009   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
18010   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
18011   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
18012   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
18013   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
18014   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
18015
18016   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
18017   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
18018   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
18019   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
18020   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
18021   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
18022   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
18023   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
18024
18025   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
18026   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
18027   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
18028   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
18029   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
18030   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
18031   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
18032   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
18033   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
18034
18035   /* FPA registers.  */
18036   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
18037   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
18038
18039   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
18040   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
18041
18042   /* VFP SP registers.  */
18043   REGSET(s,VFS),  REGSET(S,VFS),
18044   REGSETH(s,VFS), REGSETH(S,VFS),
18045
18046   /* VFP DP Registers.  */
18047   REGSET(d,VFD),  REGSET(D,VFD),
18048   /* Extra Neon DP registers.  */
18049   REGSETH(d,VFD), REGSETH(D,VFD),
18050
18051   /* Neon QP registers.  */
18052   REGSET2(q,NQ),  REGSET2(Q,NQ),
18053
18054   /* VFP control registers.  */
18055   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
18056   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
18057   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
18058   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
18059   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
18060   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
18061
18062   /* Maverick DSP coprocessor registers.  */
18063   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
18064   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
18065
18066   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
18067   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
18068   REGDEF(dspsc,0,DSPSC),
18069
18070   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
18071   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
18072   REGDEF(DSPSC,0,DSPSC),
18073
18074   /* iWMMXt data registers - p0, c0-15.  */
18075   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
18076
18077   /* iWMMXt control registers - p1, c0-3.  */
18078   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
18079   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
18080   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
18081   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
18082
18083   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
18084   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
18085   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
18086   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
18087   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
18088
18089   /* XScale accumulator registers.  */
18090   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
18091 };
18092 #undef REGDEF
18093 #undef REGNUM
18094 #undef REGSET
18095
18096 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
18097    within psr_required_here.  */
18098 static const struct asm_psr psrs[] =
18099 {
18100   /* Backward compatibility notation.  Note that "all" is no longer
18101      truly all possible PSR bits.  */
18102   {"all",  PSR_c | PSR_f},
18103   {"flg",  PSR_f},
18104   {"ctl",  PSR_c},
18105
18106   /* Individual flags.  */
18107   {"f",    PSR_f},
18108   {"c",    PSR_c},
18109   {"x",    PSR_x},
18110   {"s",    PSR_s},
18111
18112   /* Combinations of flags.  */
18113   {"fs",   PSR_f | PSR_s},
18114   {"fx",   PSR_f | PSR_x},
18115   {"fc",   PSR_f | PSR_c},
18116   {"sf",   PSR_s | PSR_f},
18117   {"sx",   PSR_s | PSR_x},
18118   {"sc",   PSR_s | PSR_c},
18119   {"xf",   PSR_x | PSR_f},
18120   {"xs",   PSR_x | PSR_s},
18121   {"xc",   PSR_x | PSR_c},
18122   {"cf",   PSR_c | PSR_f},
18123   {"cs",   PSR_c | PSR_s},
18124   {"cx",   PSR_c | PSR_x},
18125   {"fsx",  PSR_f | PSR_s | PSR_x},
18126   {"fsc",  PSR_f | PSR_s | PSR_c},
18127   {"fxs",  PSR_f | PSR_x | PSR_s},
18128   {"fxc",  PSR_f | PSR_x | PSR_c},
18129   {"fcs",  PSR_f | PSR_c | PSR_s},
18130   {"fcx",  PSR_f | PSR_c | PSR_x},
18131   {"sfx",  PSR_s | PSR_f | PSR_x},
18132   {"sfc",  PSR_s | PSR_f | PSR_c},
18133   {"sxf",  PSR_s | PSR_x | PSR_f},
18134   {"sxc",  PSR_s | PSR_x | PSR_c},
18135   {"scf",  PSR_s | PSR_c | PSR_f},
18136   {"scx",  PSR_s | PSR_c | PSR_x},
18137   {"xfs",  PSR_x | PSR_f | PSR_s},
18138   {"xfc",  PSR_x | PSR_f | PSR_c},
18139   {"xsf",  PSR_x | PSR_s | PSR_f},
18140   {"xsc",  PSR_x | PSR_s | PSR_c},
18141   {"xcf",  PSR_x | PSR_c | PSR_f},
18142   {"xcs",  PSR_x | PSR_c | PSR_s},
18143   {"cfs",  PSR_c | PSR_f | PSR_s},
18144   {"cfx",  PSR_c | PSR_f | PSR_x},
18145   {"csf",  PSR_c | PSR_s | PSR_f},
18146   {"csx",  PSR_c | PSR_s | PSR_x},
18147   {"cxf",  PSR_c | PSR_x | PSR_f},
18148   {"cxs",  PSR_c | PSR_x | PSR_s},
18149   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
18150   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
18151   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
18152   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
18153   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
18154   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
18155   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
18156   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
18157   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
18158   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
18159   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
18160   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
18161   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
18162   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
18163   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
18164   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
18165   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
18166   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
18167   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
18168   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
18169   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
18170   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
18171   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
18172   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
18173 };
18174
18175 /* Table of V7M psr names.  */
18176 static const struct asm_psr v7m_psrs[] =
18177 {
18178   {"apsr",        0 }, {"APSR",         0 },
18179   {"iapsr",       1 }, {"IAPSR",        1 },
18180   {"eapsr",       2 }, {"EAPSR",        2 },
18181   {"psr",         3 }, {"PSR",          3 },
18182   {"xpsr",        3 }, {"XPSR",         3 }, {"xPSR",     3 },
18183   {"ipsr",        5 }, {"IPSR",         5 },
18184   {"epsr",        6 }, {"EPSR",         6 },
18185   {"iepsr",       7 }, {"IEPSR",        7 },
18186   {"msp",         8 }, {"MSP",          8 },
18187   {"psp",         9 }, {"PSP",          9 },
18188   {"primask",     16}, {"PRIMASK",      16},
18189   {"basepri",     17}, {"BASEPRI",      17},
18190   {"basepri_max", 18}, {"BASEPRI_MAX",  18},
18191   {"basepri_max", 18}, {"BASEPRI_MASK", 18}, /* Typo, preserved for backwards compatibility.  */
18192   {"faultmask",   19}, {"FAULTMASK",    19},
18193   {"control",     20}, {"CONTROL",      20}
18194 };
18195
18196 /* Table of all shift-in-operand names.  */
18197 static const struct asm_shift_name shift_names [] =
18198 {
18199   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
18200   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
18201   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
18202   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
18203   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
18204   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
18205 };
18206
18207 /* Table of all explicit relocation names.  */
18208 #ifdef OBJ_ELF
18209 static struct reloc_entry reloc_names[] =
18210 {
18211   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
18212   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
18213   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
18214   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
18215   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
18216   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
18217   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
18218   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
18219   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
18220   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
18221   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
18222   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
18223   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
18224         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
18225   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
18226         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
18227   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
18228         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ}
18229 };
18230 #endif
18231
18232 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
18233 static const struct asm_cond conds[] =
18234 {
18235   {"eq", 0x0},
18236   {"ne", 0x1},
18237   {"cs", 0x2}, {"hs", 0x2},
18238   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
18239   {"mi", 0x4},
18240   {"pl", 0x5},
18241   {"vs", 0x6},
18242   {"vc", 0x7},
18243   {"hi", 0x8},
18244   {"ls", 0x9},
18245   {"ge", 0xa},
18246   {"lt", 0xb},
18247   {"gt", 0xc},
18248   {"le", 0xd},
18249   {"al", 0xe}
18250 };
18251
18252 #define UL_BARRIER(L,U,CODE,FEAT) \
18253   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
18254   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
18255
18256 static struct asm_barrier_opt barrier_opt_names[] =
18257 {
18258   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
18259   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
18260   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
18261   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
18262   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
18263   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
18264   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
18265   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
18266   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
18267   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
18268   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
18269   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
18270   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
18271   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
18272   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
18273   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
18274 };
18275
18276 #undef UL_BARRIER
18277
18278 /* Table of ARM-format instructions.    */
18279
18280 /* Macros for gluing together operand strings.  N.B. In all cases
18281    other than OPS0, the trailing OP_stop comes from default
18282    zero-initialization of the unspecified elements of the array.  */
18283 #define OPS0()            { OP_stop, }
18284 #define OPS1(a)           { OP_##a, }
18285 #define OPS2(a,b)         { OP_##a,OP_##b, }
18286 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
18287 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
18288 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
18289 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
18290
18291 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
18292    This is useful when mixing operands for ARM and THUMB, i.e. using the
18293    MIX_ARM_THUMB_OPERANDS macro.
18294    In order to use these macros, prefix the number of operands with _
18295    e.g. _3.  */
18296 #define OPS_1(a)           { a, }
18297 #define OPS_2(a,b)         { a,b, }
18298 #define OPS_3(a,b,c)       { a,b,c, }
18299 #define OPS_4(a,b,c,d)     { a,b,c,d, }
18300 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
18301 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
18302
18303 /* These macros abstract out the exact format of the mnemonic table and
18304    save some repeated characters.  */
18305
18306 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
18307 #define TxCE(mnem, op, top, nops, ops, ae, te) \
18308   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
18309     THUMB_VARIANT, do_##ae, do_##te }
18310
18311 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
18312    a T_MNEM_xyz enumerator.  */
18313 #define TCE(mnem, aop, top, nops, ops, ae, te) \
18314       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
18315 #define tCE(mnem, aop, top, nops, ops, ae, te) \
18316       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
18317
18318 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
18319    infix after the third character.  */
18320 #define TxC3(mnem, op, top, nops, ops, ae, te) \
18321   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
18322     THUMB_VARIANT, do_##ae, do_##te }
18323 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
18324   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
18325     THUMB_VARIANT, do_##ae, do_##te }
18326 #define TC3(mnem, aop, top, nops, ops, ae, te) \
18327       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
18328 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
18329       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
18330 #define tC3(mnem, aop, top, nops, ops, ae, te) \
18331       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
18332 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
18333       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
18334
18335 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
18336    field is still 0xE.  Many of the Thumb variants can be executed
18337    conditionally, so this is checked separately.  */
18338 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
18339   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
18340     THUMB_VARIANT, do_##ae, do_##te }
18341
18342 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
18343    Used by mnemonics that have very minimal differences in the encoding for
18344    ARM and Thumb variants and can be handled in a common function.  */
18345 #define TUEc(mnem, op, top, nops, ops, en) \
18346   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
18347     THUMB_VARIANT, do_##en, do_##en }
18348
18349 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
18350    condition code field.  */
18351 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
18352   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
18353     THUMB_VARIANT, do_##ae, do_##te }
18354
18355 /* ARM-only variants of all the above.  */
18356 #define CE(mnem,  op, nops, ops, ae)    \
18357   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18358
18359 #define C3(mnem, op, nops, ops, ae)     \
18360   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18361
18362 /* Legacy mnemonics that always have conditional infix after the third
18363    character.  */
18364 #define CL(mnem, op, nops, ops, ae)     \
18365   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
18366     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18367
18368 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
18369 #define cCE(mnem,  op, nops, ops, ae)   \
18370   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
18371
18372 /* Legacy coprocessor instructions where conditional infix and conditional
18373    suffix are ambiguous.  For consistency this includes all FPA instructions,
18374    not just the potentially ambiguous ones.  */
18375 #define cCL(mnem, op, nops, ops, ae)    \
18376   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
18377     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
18378
18379 /* Coprocessor, takes either a suffix or a position-3 infix
18380    (for an FPA corner case). */
18381 #define C3E(mnem, op, nops, ops, ae) \
18382   { mnem, OPS##nops ops, OT_csuf_or_in3, \
18383     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
18384
18385 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
18386   { m1 #m2 m3, OPS##nops ops, \
18387     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
18388     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18389
18390 #define CM(m1, m2, op, nops, ops, ae)   \
18391   xCM_ (m1,   , m2, op, nops, ops, ae), \
18392   xCM_ (m1, eq, m2, op, nops, ops, ae), \
18393   xCM_ (m1, ne, m2, op, nops, ops, ae), \
18394   xCM_ (m1, cs, m2, op, nops, ops, ae), \
18395   xCM_ (m1, hs, m2, op, nops, ops, ae), \
18396   xCM_ (m1, cc, m2, op, nops, ops, ae), \
18397   xCM_ (m1, ul, m2, op, nops, ops, ae), \
18398   xCM_ (m1, lo, m2, op, nops, ops, ae), \
18399   xCM_ (m1, mi, m2, op, nops, ops, ae), \
18400   xCM_ (m1, pl, m2, op, nops, ops, ae), \
18401   xCM_ (m1, vs, m2, op, nops, ops, ae), \
18402   xCM_ (m1, vc, m2, op, nops, ops, ae), \
18403   xCM_ (m1, hi, m2, op, nops, ops, ae), \
18404   xCM_ (m1, ls, m2, op, nops, ops, ae), \
18405   xCM_ (m1, ge, m2, op, nops, ops, ae), \
18406   xCM_ (m1, lt, m2, op, nops, ops, ae), \
18407   xCM_ (m1, gt, m2, op, nops, ops, ae), \
18408   xCM_ (m1, le, m2, op, nops, ops, ae), \
18409   xCM_ (m1, al, m2, op, nops, ops, ae)
18410
18411 #define UE(mnem, op, nops, ops, ae)     \
18412   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
18413
18414 #define UF(mnem, op, nops, ops, ae)     \
18415   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
18416
18417 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
18418    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
18419    use the same encoding function for each.  */
18420 #define NUF(mnem, op, nops, ops, enc)                                   \
18421   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
18422     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
18423
18424 /* Neon data processing, version which indirects through neon_enc_tab for
18425    the various overloaded versions of opcodes.  */
18426 #define nUF(mnem, op, nops, ops, enc)                                   \
18427   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
18428     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
18429
18430 /* Neon insn with conditional suffix for the ARM version, non-overloaded
18431    version.  */
18432 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
18433   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
18434     THUMB_VARIANT, do_##enc, do_##enc }
18435
18436 #define NCE(mnem, op, nops, ops, enc)                                   \
18437    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
18438
18439 #define NCEF(mnem, op, nops, ops, enc)                                  \
18440     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
18441
18442 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
18443 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
18444   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
18445     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
18446
18447 #define nCE(mnem, op, nops, ops, enc)                                   \
18448    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
18449
18450 #define nCEF(mnem, op, nops, ops, enc)                                  \
18451     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
18452
18453 #define do_0 0
18454
18455 static const struct asm_opcode insns[] =
18456 {
18457 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
18458 #define THUMB_VARIANT  & arm_ext_v4t
18459  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
18460  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
18461  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
18462  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
18463  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
18464  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
18465  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
18466  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
18467  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
18468  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
18469  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
18470  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
18471  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
18472  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
18473  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
18474  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
18475
18476  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
18477     for setting PSR flag bits.  They are obsolete in V6 and do not
18478     have Thumb equivalents. */
18479  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
18480  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
18481   CL("tstp",    110f000,           2, (RR, SH),      cmp),
18482  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
18483  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
18484   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
18485  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
18486  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
18487   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
18488
18489  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
18490  tC3("movs",    1b00000, _movs,    2, (RR, SH),      mov,  t_mov_cmp),
18491  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
18492  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
18493
18494  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
18495  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
18496  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
18497                                                                 OP_RRnpc),
18498                                         OP_ADDRGLDR),ldst, t_ldst),
18499  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
18500
18501  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18502  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18503  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18504  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18505  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18506  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18507
18508  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
18509  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
18510  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
18511  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
18512
18513   /* Pseudo ops.  */
18514  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
18515   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
18516  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
18517  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
18518
18519   /* Thumb-compatibility pseudo ops.  */
18520  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
18521  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
18522  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
18523  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
18524  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
18525  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
18526  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
18527  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
18528  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
18529  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
18530  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
18531  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
18532
18533  /* These may simplify to neg.  */
18534  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
18535  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
18536
18537 #undef  THUMB_VARIANT
18538 #define THUMB_VARIANT  & arm_ext_v6
18539
18540  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
18541
18542  /* V1 instructions with no Thumb analogue prior to V6T2.  */
18543 #undef  THUMB_VARIANT
18544 #define THUMB_VARIANT  & arm_ext_v6t2
18545
18546  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
18547  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
18548   CL("teqp",    130f000,           2, (RR, SH),      cmp),
18549
18550  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
18551  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
18552  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
18553  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
18554
18555  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18556  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18557
18558  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18559  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18560
18561  /* V1 instructions with no Thumb analogue at all.  */
18562   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
18563   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
18564
18565   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
18566   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
18567   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
18568   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
18569   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
18570   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
18571   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
18572   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
18573
18574 #undef  ARM_VARIANT
18575 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
18576 #undef  THUMB_VARIANT
18577 #define THUMB_VARIANT  & arm_ext_v4t
18578
18579  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
18580  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
18581
18582 #undef  THUMB_VARIANT
18583 #define THUMB_VARIANT  & arm_ext_v6t2
18584
18585  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
18586   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
18587
18588   /* Generic coprocessor instructions.  */
18589  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
18590  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18591  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18592  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18593  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18594  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
18595  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
18596
18597 #undef  ARM_VARIANT
18598 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
18599
18600   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
18601   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
18602
18603 #undef  ARM_VARIANT
18604 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
18605 #undef  THUMB_VARIANT
18606 #define THUMB_VARIANT  & arm_ext_msr
18607
18608  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
18609  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
18610
18611 #undef  ARM_VARIANT
18612 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
18613 #undef  THUMB_VARIANT
18614 #define THUMB_VARIANT  & arm_ext_v6t2
18615
18616  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18617   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18618  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18619   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18620  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18621   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18622  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18623   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18624
18625 #undef  ARM_VARIANT
18626 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
18627 #undef  THUMB_VARIANT
18628 #define THUMB_VARIANT  & arm_ext_v4t
18629
18630  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18631  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18632  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18633  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18634  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18635  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18636
18637 #undef  ARM_VARIANT
18638 #define ARM_VARIANT  & arm_ext_v4t_5
18639
18640   /* ARM Architecture 4T.  */
18641   /* Note: bx (and blx) are required on V5, even if the processor does
18642      not support Thumb.  */
18643  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
18644
18645 #undef  ARM_VARIANT
18646 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
18647 #undef  THUMB_VARIANT
18648 #define THUMB_VARIANT  & arm_ext_v5t
18649
18650   /* Note: blx has 2 variants; the .value coded here is for
18651      BLX(2).  Only this variant has conditional execution.  */
18652  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
18653  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
18654
18655 #undef  THUMB_VARIANT
18656 #define THUMB_VARIANT  & arm_ext_v6t2
18657
18658  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
18659  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18660  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
18661  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18662  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
18663  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
18664  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
18665  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
18666
18667 #undef  ARM_VARIANT
18668 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
18669 #undef  THUMB_VARIANT
18670 #define THUMB_VARIANT  & arm_ext_v5exp
18671
18672  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18673  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18674  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18675  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18676
18677  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18678  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18679
18680  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18681  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18682  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18683  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18684
18685  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18686  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18687  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18688  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18689
18690  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18691  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18692
18693  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18694  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18695  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18696  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18697
18698 #undef  ARM_VARIANT
18699 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
18700 #undef  THUMB_VARIANT
18701 #define THUMB_VARIANT  & arm_ext_v6t2
18702
18703  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
18704  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
18705      ldrd, t_ldstd),
18706  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
18707                                        ADDRGLDRS), ldrd, t_ldstd),
18708
18709  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18710  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18711
18712 #undef  ARM_VARIANT
18713 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
18714
18715  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
18716
18717 #undef  ARM_VARIANT
18718 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
18719 #undef  THUMB_VARIANT
18720 #define THUMB_VARIANT  & arm_ext_v6
18721
18722  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
18723  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
18724  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
18725  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
18726  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
18727  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18728  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18729  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18730  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18731  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
18732
18733 #undef  THUMB_VARIANT
18734 #define THUMB_VARIANT  & arm_ext_v6t2
18735
18736  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
18737  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
18738                                       strex,  t_strex),
18739  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18740  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18741
18742  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
18743  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
18744
18745 /*  ARM V6 not included in V7M.  */
18746 #undef  THUMB_VARIANT
18747 #define THUMB_VARIANT  & arm_ext_v6_notm
18748  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
18749  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
18750   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
18751   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
18752  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
18753  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
18754   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
18755  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
18756   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
18757  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
18758  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
18759  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
18760   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
18761   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
18762   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
18763   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
18764  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
18765  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
18766
18767 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
18768 #undef  THUMB_VARIANT
18769 #define THUMB_VARIANT  & arm_ext_v6_dsp
18770  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
18771  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
18772  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
18773  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18774  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18775  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18776  /* Old name for QASX.  */
18777  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18778  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18779  /* Old name for QSAX.  */
18780  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18781  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18782  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18783  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18784  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18785  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18786  /* Old name for SASX.  */
18787  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18788  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18789  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18790  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18791  /* Old name for SHASX.  */
18792  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18793  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18794  /* Old name for SHSAX.  */
18795  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18796  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18797  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18798  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18799  /* Old name for SSAX.  */
18800  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18801  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18802  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18803  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18804  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18805  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18806  /* Old name for UASX.  */
18807  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18808  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18809  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18810  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18811  /* Old name for UHASX.  */
18812  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18813  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18814  /* Old name for UHSAX.  */
18815  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18816  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18817  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18818  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18819  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18820  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18821  /* Old name for UQASX.  */
18822  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18823  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18824  /* Old name for UQSAX.  */
18825  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18826  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18827  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18828  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18829  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18830  /* Old name for USAX.  */
18831  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18832  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18833  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18834  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18835  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18836  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
18837  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18838  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18839  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18840  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
18841  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18842  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18843  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18844  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18845  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18846  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18847  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18848  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18849  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18850  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18851  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18852  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18853  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18854  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18855  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18856  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18857  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18858  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18859  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18860  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
18861  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
18862  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
18863  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
18864  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
18865
18866 #undef  ARM_VARIANT
18867 #define ARM_VARIANT   & arm_ext_v6k
18868 #undef  THUMB_VARIANT
18869 #define THUMB_VARIANT & arm_ext_v6k
18870
18871  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
18872  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
18873  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
18874  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
18875
18876 #undef  THUMB_VARIANT
18877 #define THUMB_VARIANT  & arm_ext_v6_notm
18878  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
18879                                       ldrexd, t_ldrexd),
18880  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
18881                                        RRnpcb), strexd, t_strexd),
18882
18883 #undef  THUMB_VARIANT
18884 #define THUMB_VARIANT  & arm_ext_v6t2
18885  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
18886      rd_rn,  rd_rn),
18887  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
18888      rd_rn,  rd_rn),
18889  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
18890      strex, t_strexbh),
18891  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
18892      strex, t_strexbh),
18893  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
18894
18895 #undef  ARM_VARIANT
18896 #define ARM_VARIANT    & arm_ext_sec
18897 #undef  THUMB_VARIANT
18898 #define THUMB_VARIANT  & arm_ext_sec
18899
18900  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
18901
18902 #undef  ARM_VARIANT
18903 #define ARM_VARIANT    & arm_ext_virt
18904 #undef  THUMB_VARIANT
18905 #define THUMB_VARIANT    & arm_ext_virt
18906
18907  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
18908  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
18909
18910 #undef  ARM_VARIANT
18911 #define ARM_VARIANT    & arm_ext_pan
18912 #undef  THUMB_VARIANT
18913 #define THUMB_VARIANT  & arm_ext_pan
18914
18915  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
18916
18917 #undef  ARM_VARIANT
18918 #define ARM_VARIANT    & arm_ext_v6t2
18919 #undef  THUMB_VARIANT
18920 #define THUMB_VARIANT  & arm_ext_v6t2
18921
18922  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
18923  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
18924  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
18925  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
18926
18927  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
18928  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
18929  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
18930  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
18931
18932  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
18933  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
18934  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
18935  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
18936
18937  /* Thumb-only instructions.  */
18938 #undef  ARM_VARIANT
18939 #define ARM_VARIANT NULL
18940   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
18941   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
18942
18943  /* ARM does not really have an IT instruction, so always allow it.
18944     The opcode is copied from Thumb in order to allow warnings in
18945     -mimplicit-it=[never | arm] modes.  */
18946 #undef  ARM_VARIANT
18947 #define ARM_VARIANT  & arm_ext_v1
18948
18949  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
18950  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
18951  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
18952  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
18953  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
18954  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
18955  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
18956  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
18957  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
18958  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
18959  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
18960  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
18961  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
18962  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
18963  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
18964  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
18965  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
18966  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
18967
18968  /* Thumb2 only instructions.  */
18969 #undef  ARM_VARIANT
18970 #define ARM_VARIANT  NULL
18971
18972  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
18973  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
18974  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
18975  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
18976  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
18977  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
18978
18979  /* Hardware division instructions.  */
18980 #undef  ARM_VARIANT
18981 #define ARM_VARIANT    & arm_ext_adiv
18982 #undef  THUMB_VARIANT
18983 #define THUMB_VARIANT  & arm_ext_div
18984
18985  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
18986  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
18987
18988  /* ARM V6M/V7 instructions.  */
18989 #undef  ARM_VARIANT
18990 #define ARM_VARIANT    & arm_ext_barrier
18991 #undef  THUMB_VARIANT
18992 #define THUMB_VARIANT  & arm_ext_barrier
18993
18994  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
18995  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
18996  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
18997
18998  /* ARM V7 instructions.  */
18999 #undef  ARM_VARIANT
19000 #define ARM_VARIANT    & arm_ext_v7
19001 #undef  THUMB_VARIANT
19002 #define THUMB_VARIANT  & arm_ext_v7
19003
19004  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
19005  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
19006
19007 #undef  ARM_VARIANT
19008 #define ARM_VARIANT    & arm_ext_mp
19009 #undef  THUMB_VARIANT
19010 #define THUMB_VARIANT  & arm_ext_mp
19011
19012  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
19013
19014  /* AArchv8 instructions.  */
19015 #undef  ARM_VARIANT
19016 #define ARM_VARIANT   & arm_ext_v8
19017 #undef  THUMB_VARIANT
19018 #define THUMB_VARIANT & arm_ext_v8
19019
19020  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
19021  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
19022  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19023  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
19024                                                         ldrexd, t_ldrexd),
19025  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
19026  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19027  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
19028                                                         stlex,  t_stlex),
19029  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
19030                                                         strexd, t_strexd),
19031  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
19032                                                         stlex, t_stlex),
19033  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
19034                                                         stlex, t_stlex),
19035  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19036  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19037  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19038  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19039  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19040  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19041
19042  /* ARMv8 T32 only.  */
19043 #undef  ARM_VARIANT
19044 #define ARM_VARIANT  NULL
19045  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
19046  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
19047  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
19048
19049   /* FP for ARMv8.  */
19050 #undef  ARM_VARIANT
19051 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
19052 #undef  THUMB_VARIANT
19053 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
19054
19055   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
19056   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
19057   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
19058   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
19059   nUF(vmaxnm, _vmaxnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
19060   nUF(vminnm, _vminnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
19061   nUF(vcvta,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvta),
19062   nUF(vcvtn,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtn),
19063   nUF(vcvtp,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtp),
19064   nUF(vcvtm,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtm),
19065   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
19066   nCE(vrintz, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintz),
19067   nCE(vrintx, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintx),
19068   nUF(vrinta, _vrinta, 2, (RNSDQ, oRNSDQ),              vrinta),
19069   nUF(vrintn, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintn),
19070   nUF(vrintp, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintp),
19071   nUF(vrintm, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintm),
19072
19073   /* Crypto v1 extensions.  */
19074 #undef  ARM_VARIANT
19075 #define ARM_VARIANT & fpu_crypto_ext_armv8
19076 #undef  THUMB_VARIANT
19077 #define THUMB_VARIANT & fpu_crypto_ext_armv8
19078
19079   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
19080   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
19081   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
19082   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
19083   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
19084   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
19085   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
19086   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
19087   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
19088   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
19089   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
19090   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
19091   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
19092   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
19093
19094 #undef  ARM_VARIANT
19095 #define ARM_VARIANT   & crc_ext_armv8
19096 #undef  THUMB_VARIANT
19097 #define THUMB_VARIANT & crc_ext_armv8
19098   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
19099   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
19100   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
19101   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
19102   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
19103   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
19104
19105 #undef  ARM_VARIANT
19106 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
19107 #undef  THUMB_VARIANT
19108 #define THUMB_VARIANT NULL
19109
19110  cCE("wfs",     e200110, 1, (RR),            rd),
19111  cCE("rfs",     e300110, 1, (RR),            rd),
19112  cCE("wfc",     e400110, 1, (RR),            rd),
19113  cCE("rfc",     e500110, 1, (RR),            rd),
19114
19115  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19116  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19117  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19118  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19119
19120  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19121  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19122  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19123  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19124
19125  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
19126  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
19127  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
19128  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
19129  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
19130  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
19131  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
19132  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
19133  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
19134  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
19135  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
19136  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
19137
19138  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
19139  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
19140  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
19141  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
19142  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
19143  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
19144  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
19145  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
19146  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
19147  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
19148  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
19149  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
19150
19151  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
19152  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
19153  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
19154  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
19155  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
19156  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
19157  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
19158  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
19159  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
19160  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
19161  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
19162  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
19163
19164  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
19165  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
19166  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
19167  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
19168  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
19169  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
19170  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
19171  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
19172  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
19173  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
19174  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
19175  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
19176
19177  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
19178  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
19179  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
19180  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
19181  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
19182  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
19183  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
19184  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
19185  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
19186  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
19187  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
19188  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
19189
19190  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
19191  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
19192  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
19193  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
19194  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
19195  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
19196  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
19197  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
19198  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
19199  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
19200  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
19201  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
19202
19203  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
19204  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
19205  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
19206  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
19207  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
19208  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
19209  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
19210  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
19211  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
19212  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
19213  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
19214  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
19215
19216  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
19217  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
19218  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
19219  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
19220  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
19221  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
19222  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
19223  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
19224  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
19225  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
19226  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
19227  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
19228
19229  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
19230  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
19231  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
19232  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
19233  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
19234  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
19235  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
19236  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
19237  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
19238  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
19239  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
19240  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
19241
19242  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
19243  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
19244  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
19245  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
19246  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
19247  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
19248  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
19249  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
19250  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
19251  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
19252  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
19253  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
19254
19255  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
19256  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
19257  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
19258  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
19259  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
19260  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
19261  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
19262  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
19263  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
19264  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
19265  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
19266  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
19267
19268  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
19269  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
19270  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
19271  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
19272  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
19273  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
19274  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
19275  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
19276  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
19277  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
19278  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
19279  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
19280
19281  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
19282  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
19283  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
19284  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
19285  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
19286  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
19287  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
19288  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
19289  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
19290  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
19291  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
19292  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
19293
19294  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
19295  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
19296  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
19297  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
19298  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
19299  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
19300  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
19301  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
19302  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
19303  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
19304  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
19305  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
19306
19307  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
19308  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
19309  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
19310  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
19311  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
19312  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
19313  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
19314  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
19315  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
19316  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
19317  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
19318  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
19319
19320  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
19321  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
19322  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
19323  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
19324  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
19325  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
19326  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
19327  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
19328  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
19329  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
19330  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
19331  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
19332
19333  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
19334  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
19335  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
19336  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
19337  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
19338  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19339  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19340  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19341  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
19342  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
19343  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
19344  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
19345
19346  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
19347  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
19348  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
19349  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
19350  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
19351  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19352  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19353  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19354  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
19355  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
19356  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
19357  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
19358
19359  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
19360  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
19361  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
19362  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
19363  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
19364  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19365  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19366  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19367  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
19368  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
19369  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
19370  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
19371
19372  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
19373  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
19374  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
19375  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
19376  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
19377  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19378  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19379  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19380  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
19381  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
19382  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
19383  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
19384
19385  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
19386  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
19387  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
19388  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
19389  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
19390  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19391  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19392  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19393  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
19394  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
19395  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
19396  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
19397
19398  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
19399  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
19400  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
19401  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
19402  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
19403  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19404  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19405  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19406  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
19407  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
19408  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
19409  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
19410
19411  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
19412  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
19413  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
19414  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
19415  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
19416  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19417  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19418  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19419  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
19420  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
19421  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
19422  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
19423
19424  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
19425  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
19426  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
19427  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
19428  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
19429  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19430  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19431  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19432  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
19433  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
19434  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
19435  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
19436
19437  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
19438  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
19439  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
19440  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
19441  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
19442  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19443  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19444  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19445  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
19446  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
19447  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
19448  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
19449
19450  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
19451  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
19452  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
19453  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
19454  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
19455  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19456  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19457  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19458  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
19459  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
19460  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
19461  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
19462
19463  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
19464  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
19465  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
19466  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
19467  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
19468  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19469  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19470  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19471  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
19472  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
19473  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
19474  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
19475
19476  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
19477  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
19478  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
19479  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
19480  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
19481  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19482  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19483  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19484  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
19485  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
19486  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
19487  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
19488
19489  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
19490  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
19491  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
19492  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
19493  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
19494  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19495  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19496  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19497  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
19498  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
19499  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
19500  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
19501
19502  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
19503  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
19504  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
19505  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
19506
19507  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
19508  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
19509  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
19510  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
19511  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
19512  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
19513  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
19514  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
19515  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
19516  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
19517  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
19518  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
19519
19520   /* The implementation of the FIX instruction is broken on some
19521      assemblers, in that it accepts a precision specifier as well as a
19522      rounding specifier, despite the fact that this is meaningless.
19523      To be more compatible, we accept it as well, though of course it
19524      does not set any bits.  */
19525  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
19526  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
19527  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
19528  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
19529  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
19530  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
19531  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
19532  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
19533  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
19534  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
19535  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
19536  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
19537  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
19538
19539   /* Instructions that were new with the real FPA, call them V2.  */
19540 #undef  ARM_VARIANT
19541 #define ARM_VARIANT  & fpu_fpa_ext_v2
19542
19543  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19544  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19545  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19546  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19547  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19548  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19549
19550 #undef  ARM_VARIANT
19551 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
19552
19553   /* Moves and type conversions.  */
19554  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
19555  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
19556  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
19557  cCE("fmstat",  ef1fa10, 0, (),               noargs),
19558  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
19559  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
19560  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19561  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
19562  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
19563  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19564  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
19565  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19566  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
19567  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
19568
19569   /* Memory operations.  */
19570  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
19571  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
19572  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19573  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19574  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19575  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19576  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19577  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19578  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19579  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19580  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19581  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19582  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19583  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19584  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19585  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19586  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19587  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19588
19589   /* Monadic operations.  */
19590  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19591  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
19592  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19593
19594   /* Dyadic operations.  */
19595  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19596  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19597  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19598  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19599  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19600  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19601  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19602  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19603  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19604
19605   /* Comparisons.  */
19606  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
19607  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
19608  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19609  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
19610
19611  /* Double precision load/store are still present on single precision
19612     implementations.  */
19613  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
19614  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
19615  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19616  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19617  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19618  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19619  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19620  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19621  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19622  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19623
19624 #undef  ARM_VARIANT
19625 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
19626
19627   /* Moves and type conversions.  */
19628  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
19629  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
19630  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19631  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
19632  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
19633  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
19634  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
19635  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
19636  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
19637  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19638  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19639  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19640  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19641
19642   /* Monadic operations.  */
19643  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
19644  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
19645  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
19646
19647   /* Dyadic operations.  */
19648  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19649  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19650  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19651  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19652  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19653  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19654  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19655  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19656  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19657
19658   /* Comparisons.  */
19659  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
19660  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
19661  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
19662  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
19663
19664 #undef  ARM_VARIANT
19665 #define ARM_VARIANT  & fpu_vfp_ext_v2
19666
19667  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
19668  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
19669  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
19670  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
19671
19672 /* Instructions which may belong to either the Neon or VFP instruction sets.
19673    Individual encoder functions perform additional architecture checks.  */
19674 #undef  ARM_VARIANT
19675 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
19676 #undef  THUMB_VARIANT
19677 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
19678
19679   /* These mnemonics are unique to VFP.  */
19680  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
19681  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
19682  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19683  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19684  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19685  nCE(vcmp,      _vcmp,    2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
19686  nCE(vcmpe,     _vcmpe,   2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
19687  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
19688  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
19689  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
19690
19691   /* Mnemonics shared by Neon and VFP.  */
19692  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
19693  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
19694  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
19695
19696  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
19697  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
19698
19699  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
19700  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
19701
19702  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19703  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19704  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19705  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19706  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19707  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19708  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
19709  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
19710
19711  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
19712  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
19713  NCEF(vcvtb,    eb20a40, 2, (RVSD, RVSD), neon_cvtb),
19714  NCEF(vcvtt,    eb20a40, 2, (RVSD, RVSD), neon_cvtt),
19715
19716
19717   /* NOTE: All VMOV encoding is special-cased!  */
19718  NCE(vmov,      0,       1, (VMOV), neon_mov),
19719  NCE(vmovq,     0,       1, (VMOV), neon_mov),
19720
19721 #undef  THUMB_VARIANT
19722 #define THUMB_VARIANT  & fpu_neon_ext_v1
19723 #undef  ARM_VARIANT
19724 #define ARM_VARIANT    & fpu_neon_ext_v1
19725
19726   /* Data processing with three registers of the same length.  */
19727   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
19728  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
19729  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
19730  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
19731  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
19732  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
19733  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
19734  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
19735  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
19736   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
19737  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
19738  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
19739  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
19740  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
19741  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
19742  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
19743  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
19744  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
19745   /* If not immediate, fall back to neon_dyadic_i64_su.
19746      shl_imm should accept I8 I16 I32 I64,
19747      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
19748  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
19749  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
19750  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
19751  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
19752   /* Logic ops, types optional & ignored.  */
19753  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19754  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19755  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19756  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19757  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19758  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19759  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19760  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19761  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
19762  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
19763   /* Bitfield ops, untyped.  */
19764  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
19765  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
19766  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
19767  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
19768  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
19769  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
19770   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F32.  */
19771  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
19772  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
19773  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
19774  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
19775  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
19776  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
19777   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
19778      back to neon_dyadic_if_su.  */
19779  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
19780  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
19781  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
19782  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
19783  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
19784  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
19785  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
19786  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
19787   /* Comparison. Type I8 I16 I32 F32.  */
19788  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
19789  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
19790   /* As above, D registers only.  */
19791  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
19792  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
19793   /* Int and float variants, signedness unimportant.  */
19794  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
19795  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
19796  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
19797   /* Add/sub take types I8 I16 I32 I64 F32.  */
19798  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
19799  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
19800   /* vtst takes sizes 8, 16, 32.  */
19801  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
19802  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
19803   /* VMUL takes I8 I16 I32 F32 P8.  */
19804  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
19805   /* VQD{R}MULH takes S16 S32.  */
19806  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19807  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19808  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19809  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19810  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
19811  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
19812  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
19813  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
19814  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
19815  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
19816  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
19817  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
19818  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
19819  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
19820  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
19821  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
19822  /* ARM v8.1 extension.  */
19823  nUF(vqrdmlah,  _vqrdmlah, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19824  nUF(vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19825  nUF(vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19826  nUF(vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19827
19828   /* Two address, int/float. Types S8 S16 S32 F32.  */
19829  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
19830  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
19831
19832   /* Data processing with two registers and a shift amount.  */
19833   /* Right shifts, and variants with rounding.
19834      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
19835  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
19836  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
19837  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
19838  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
19839  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
19840  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
19841  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
19842  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
19843   /* Shift and insert. Sizes accepted 8 16 32 64.  */
19844  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
19845  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
19846  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
19847  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
19848   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
19849  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
19850  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
19851   /* Right shift immediate, saturating & narrowing, with rounding variants.
19852      Types accepted S16 S32 S64 U16 U32 U64.  */
19853  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
19854  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
19855   /* As above, unsigned. Types accepted S16 S32 S64.  */
19856  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
19857  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
19858   /* Right shift narrowing. Types accepted I16 I32 I64.  */
19859  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
19860  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
19861   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
19862  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
19863   /* CVT with optional immediate for fixed-point variant.  */
19864  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
19865
19866  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
19867  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
19868
19869   /* Data processing, three registers of different lengths.  */
19870   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
19871  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
19872  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
19873  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
19874  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
19875   /* If not scalar, fall back to neon_dyadic_long.
19876      Vector types as above, scalar types S16 S32 U16 U32.  */
19877  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
19878  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
19879   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
19880  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
19881  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
19882   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
19883  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
19884  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
19885  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
19886  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
19887   /* Saturating doubling multiplies. Types S16 S32.  */
19888  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
19889  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
19890  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
19891   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
19892      S16 S32 U16 U32.  */
19893  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
19894
19895   /* Extract. Size 8.  */
19896  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
19897  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
19898
19899   /* Two registers, miscellaneous.  */
19900   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
19901  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
19902  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
19903  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
19904  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
19905  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
19906  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
19907   /* Vector replicate. Sizes 8 16 32.  */
19908  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
19909  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
19910   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
19911  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
19912   /* VMOVN. Types I16 I32 I64.  */
19913  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
19914   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
19915  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
19916   /* VQMOVUN. Types S16 S32 S64.  */
19917  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
19918   /* VZIP / VUZP. Sizes 8 16 32.  */
19919  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
19920  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
19921  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
19922  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
19923   /* VQABS / VQNEG. Types S8 S16 S32.  */
19924  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
19925  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
19926  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
19927  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
19928   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
19929  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
19930  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
19931  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
19932  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
19933   /* Reciprocal estimates. Types U32 F32.  */
19934  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
19935  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
19936  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
19937  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
19938   /* VCLS. Types S8 S16 S32.  */
19939  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
19940  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
19941   /* VCLZ. Types I8 I16 I32.  */
19942  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
19943  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
19944   /* VCNT. Size 8.  */
19945  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
19946  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
19947   /* Two address, untyped.  */
19948  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
19949  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
19950   /* VTRN. Sizes 8 16 32.  */
19951  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
19952  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
19953
19954   /* Table lookup. Size 8.  */
19955  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
19956  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
19957
19958 #undef  THUMB_VARIANT
19959 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
19960 #undef  ARM_VARIANT
19961 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
19962
19963   /* Neon element/structure load/store.  */
19964  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19965  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19966  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19967  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19968  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19969  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19970  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19971  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19972
19973 #undef  THUMB_VARIANT
19974 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
19975 #undef  ARM_VARIANT
19976 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
19977  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
19978  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
19979  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
19980  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
19981  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
19982  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
19983  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
19984  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
19985  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
19986
19987 #undef  THUMB_VARIANT
19988 #define THUMB_VARIANT  & fpu_vfp_ext_v3
19989 #undef  ARM_VARIANT
19990 #define ARM_VARIANT    & fpu_vfp_ext_v3
19991
19992  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
19993  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
19994  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
19995  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
19996  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
19997  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
19998  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
19999  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20000  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20001
20002 #undef  ARM_VARIANT
20003 #define ARM_VARIANT    & fpu_vfp_ext_fma
20004 #undef  THUMB_VARIANT
20005 #define THUMB_VARIANT  & fpu_vfp_ext_fma
20006  /* Mnemonics shared by Neon and VFP.  These are included in the
20007     VFP FMA variant; NEON and VFP FMA always includes the NEON
20008     FMA instructions.  */
20009  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
20010  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
20011  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
20012     the v form should always be used.  */
20013  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20014  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20015  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20016  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20017  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20018  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20019
20020 #undef THUMB_VARIANT
20021 #undef  ARM_VARIANT
20022 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
20023
20024  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20025  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20026  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20027  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20028  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20029  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20030  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
20031  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
20032
20033 #undef  ARM_VARIANT
20034 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
20035
20036  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
20037  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
20038  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
20039  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
20040  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
20041  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
20042  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
20043  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
20044  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
20045  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20046  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20047  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20048  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20049  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20050  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20051  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20052  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20053  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20054  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
20055  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
20056  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20057  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20058  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20059  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20060  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20061  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20062  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
20063  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
20064  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
20065  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
20066  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
20067  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
20068  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
20069  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
20070  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
20071  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
20072  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
20073  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20074  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20075  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20076  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20077  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20078  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20079  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20080  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20081  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20082  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
20083  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20084  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20085  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20086  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20087  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20088  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20089  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20090  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20091  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20092  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20093  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20094  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20095  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20096  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20097  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20098  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20099  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20100  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20101  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20102  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20103  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20104  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
20105  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
20106  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20107  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20108  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20109  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20110  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20111  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20112  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20113  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20114  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20115  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20116  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20117  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20118  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20119  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20120  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20121  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20122  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20123  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20124  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
20125  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20126  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20127  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20128  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20129  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20130  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20131  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20132  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20133  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20134  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20135  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20136  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20137  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20138  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20139  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20140  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20141  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20142  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20143  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20144  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20145  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20146  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
20147  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20148  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20149  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20150  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20151  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20152  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20153  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20154  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20155  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20156  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20157  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20158  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20159  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20160  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20161  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20162  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20163  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20164  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20165  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20166  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20167  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
20168  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
20169  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20170  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20171  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20172  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20173  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20174  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20175  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20176  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20177  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20178  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
20179  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
20180  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
20181  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
20182  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
20183  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
20184  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20185  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20186  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20187  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
20188  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
20189  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
20190  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
20191  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
20192  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
20193  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20194  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20195  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20196  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20197  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
20198
20199 #undef  ARM_VARIANT
20200 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
20201
20202  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
20203  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
20204  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
20205  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
20206  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
20207  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
20208  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20209  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20210  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20211  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20212  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20213  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20214  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20215  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20216  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20217  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20218  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20219  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20220  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20221  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20222  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
20223  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20224  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20225  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20226  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20227  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20228  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20229  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20230  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20231  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20232  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20233  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20234  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20235  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20236  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20237  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20238  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20239  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20240  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20241  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20242  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20243  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20244  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20245  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20246  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20247  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20248  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20249  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20250  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20251  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20252  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20253  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20254  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20255  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20256  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20257  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20258  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20259
20260 #undef  ARM_VARIANT
20261 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
20262
20263  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
20264  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
20265  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
20266  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
20267  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
20268  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
20269  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
20270  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
20271  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
20272  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
20273  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
20274  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
20275  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
20276  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
20277  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
20278  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
20279  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
20280  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
20281  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
20282  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
20283  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
20284  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
20285  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
20286  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
20287  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
20288  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
20289  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
20290  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
20291  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
20292  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
20293  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
20294  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
20295  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
20296  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
20297  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
20298  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
20299  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
20300  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
20301  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
20302  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
20303  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
20304  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
20305  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
20306  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
20307  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
20308  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
20309  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
20310  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
20311  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
20312  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
20313  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
20314  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
20315  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
20316  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
20317  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
20318  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
20319  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
20320  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
20321  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
20322  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
20323  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
20324  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
20325  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
20326  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
20327  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20328  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
20329  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20330  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
20331  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20332  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
20333  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20334  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20335  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
20336  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
20337  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
20338  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
20339 };
20340 #undef ARM_VARIANT
20341 #undef THUMB_VARIANT
20342 #undef TCE
20343 #undef TUE
20344 #undef TUF
20345 #undef TCC
20346 #undef cCE
20347 #undef cCL
20348 #undef C3E
20349 #undef CE
20350 #undef CM
20351 #undef UE
20352 #undef UF
20353 #undef UT
20354 #undef NUF
20355 #undef nUF
20356 #undef NCE
20357 #undef nCE
20358 #undef OPS0
20359 #undef OPS1
20360 #undef OPS2
20361 #undef OPS3
20362 #undef OPS4
20363 #undef OPS5
20364 #undef OPS6
20365 #undef do_0
20366 \f
20367 /* MD interface: bits in the object file.  */
20368
20369 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
20370    for use in the a.out file, and stores them in the array pointed to by buf.
20371    This knows about the endian-ness of the target machine and does
20372    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
20373    2 (short) and 4 (long)  Floating numbers are put out as a series of
20374    LITTLENUMS (shorts, here at least).  */
20375
20376 void
20377 md_number_to_chars (char * buf, valueT val, int n)
20378 {
20379   if (target_big_endian)
20380     number_to_chars_bigendian (buf, val, n);
20381   else
20382     number_to_chars_littleendian (buf, val, n);
20383 }
20384
20385 static valueT
20386 md_chars_to_number (char * buf, int n)
20387 {
20388   valueT result = 0;
20389   unsigned char * where = (unsigned char *) buf;
20390
20391   if (target_big_endian)
20392     {
20393       while (n--)
20394         {
20395           result <<= 8;
20396           result |= (*where++ & 255);
20397         }
20398     }
20399   else
20400     {
20401       while (n--)
20402         {
20403           result <<= 8;
20404           result |= (where[n] & 255);
20405         }
20406     }
20407
20408   return result;
20409 }
20410
20411 /* MD interface: Sections.  */
20412
20413 /* Calculate the maximum variable size (i.e., excluding fr_fix)
20414    that an rs_machine_dependent frag may reach.  */
20415
20416 unsigned int
20417 arm_frag_max_var (fragS *fragp)
20418 {
20419   /* We only use rs_machine_dependent for variable-size Thumb instructions,
20420      which are either THUMB_SIZE (2) or INSN_SIZE (4).
20421
20422      Note that we generate relaxable instructions even for cases that don't
20423      really need it, like an immediate that's a trivial constant.  So we're
20424      overestimating the instruction size for some of those cases.  Rather
20425      than putting more intelligence here, it would probably be better to
20426      avoid generating a relaxation frag in the first place when it can be
20427      determined up front that a short instruction will suffice.  */
20428
20429   gas_assert (fragp->fr_type == rs_machine_dependent);
20430   return INSN_SIZE;
20431 }
20432
20433 /* Estimate the size of a frag before relaxing.  Assume everything fits in
20434    2 bytes.  */
20435
20436 int
20437 md_estimate_size_before_relax (fragS * fragp,
20438                                segT    segtype ATTRIBUTE_UNUSED)
20439 {
20440   fragp->fr_var = 2;
20441   return 2;
20442 }
20443
20444 /* Convert a machine dependent frag.  */
20445
20446 void
20447 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
20448 {
20449   unsigned long insn;
20450   unsigned long old_op;
20451   char *buf;
20452   expressionS exp;
20453   fixS *fixp;
20454   int reloc_type;
20455   int pc_rel;
20456   int opcode;
20457
20458   buf = fragp->fr_literal + fragp->fr_fix;
20459
20460   old_op = bfd_get_16(abfd, buf);
20461   if (fragp->fr_symbol)
20462     {
20463       exp.X_op = O_symbol;
20464       exp.X_add_symbol = fragp->fr_symbol;
20465     }
20466   else
20467     {
20468       exp.X_op = O_constant;
20469     }
20470   exp.X_add_number = fragp->fr_offset;
20471   opcode = fragp->fr_subtype;
20472   switch (opcode)
20473     {
20474     case T_MNEM_ldr_pc:
20475     case T_MNEM_ldr_pc2:
20476     case T_MNEM_ldr_sp:
20477     case T_MNEM_str_sp:
20478     case T_MNEM_ldr:
20479     case T_MNEM_ldrb:
20480     case T_MNEM_ldrh:
20481     case T_MNEM_str:
20482     case T_MNEM_strb:
20483     case T_MNEM_strh:
20484       if (fragp->fr_var == 4)
20485         {
20486           insn = THUMB_OP32 (opcode);
20487           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
20488             {
20489               insn |= (old_op & 0x700) << 4;
20490             }
20491           else
20492             {
20493               insn |= (old_op & 7) << 12;
20494               insn |= (old_op & 0x38) << 13;
20495             }
20496           insn |= 0x00000c00;
20497           put_thumb32_insn (buf, insn);
20498           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
20499         }
20500       else
20501         {
20502           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
20503         }
20504       pc_rel = (opcode == T_MNEM_ldr_pc2);
20505       break;
20506     case T_MNEM_adr:
20507       if (fragp->fr_var == 4)
20508         {
20509           insn = THUMB_OP32 (opcode);
20510           insn |= (old_op & 0xf0) << 4;
20511           put_thumb32_insn (buf, insn);
20512           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
20513         }
20514       else
20515         {
20516           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
20517           exp.X_add_number -= 4;
20518         }
20519       pc_rel = 1;
20520       break;
20521     case T_MNEM_mov:
20522     case T_MNEM_movs:
20523     case T_MNEM_cmp:
20524     case T_MNEM_cmn:
20525       if (fragp->fr_var == 4)
20526         {
20527           int r0off = (opcode == T_MNEM_mov
20528                        || opcode == T_MNEM_movs) ? 0 : 8;
20529           insn = THUMB_OP32 (opcode);
20530           insn = (insn & 0xe1ffffff) | 0x10000000;
20531           insn |= (old_op & 0x700) << r0off;
20532           put_thumb32_insn (buf, insn);
20533           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
20534         }
20535       else
20536         {
20537           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
20538         }
20539       pc_rel = 0;
20540       break;
20541     case T_MNEM_b:
20542       if (fragp->fr_var == 4)
20543         {
20544           insn = THUMB_OP32(opcode);
20545           put_thumb32_insn (buf, insn);
20546           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
20547         }
20548       else
20549         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
20550       pc_rel = 1;
20551       break;
20552     case T_MNEM_bcond:
20553       if (fragp->fr_var == 4)
20554         {
20555           insn = THUMB_OP32(opcode);
20556           insn |= (old_op & 0xf00) << 14;
20557           put_thumb32_insn (buf, insn);
20558           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
20559         }
20560       else
20561         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
20562       pc_rel = 1;
20563       break;
20564     case T_MNEM_add_sp:
20565     case T_MNEM_add_pc:
20566     case T_MNEM_inc_sp:
20567     case T_MNEM_dec_sp:
20568       if (fragp->fr_var == 4)
20569         {
20570           /* ??? Choose between add and addw.  */
20571           insn = THUMB_OP32 (opcode);
20572           insn |= (old_op & 0xf0) << 4;
20573           put_thumb32_insn (buf, insn);
20574           if (opcode == T_MNEM_add_pc)
20575             reloc_type = BFD_RELOC_ARM_T32_IMM12;
20576           else
20577             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
20578         }
20579       else
20580         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
20581       pc_rel = 0;
20582       break;
20583
20584     case T_MNEM_addi:
20585     case T_MNEM_addis:
20586     case T_MNEM_subi:
20587     case T_MNEM_subis:
20588       if (fragp->fr_var == 4)
20589         {
20590           insn = THUMB_OP32 (opcode);
20591           insn |= (old_op & 0xf0) << 4;
20592           insn |= (old_op & 0xf) << 16;
20593           put_thumb32_insn (buf, insn);
20594           if (insn & (1 << 20))
20595             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
20596           else
20597             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
20598         }
20599       else
20600         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
20601       pc_rel = 0;
20602       break;
20603     default:
20604       abort ();
20605     }
20606   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
20607                       (enum bfd_reloc_code_real) reloc_type);
20608   fixp->fx_file = fragp->fr_file;
20609   fixp->fx_line = fragp->fr_line;
20610   fragp->fr_fix += fragp->fr_var;
20611
20612   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
20613   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
20614       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
20615     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
20616 }
20617
20618 /* Return the size of a relaxable immediate operand instruction.
20619    SHIFT and SIZE specify the form of the allowable immediate.  */
20620 static int
20621 relax_immediate (fragS *fragp, int size, int shift)
20622 {
20623   offsetT offset;
20624   offsetT mask;
20625   offsetT low;
20626
20627   /* ??? Should be able to do better than this.  */
20628   if (fragp->fr_symbol)
20629     return 4;
20630
20631   low = (1 << shift) - 1;
20632   mask = (1 << (shift + size)) - (1 << shift);
20633   offset = fragp->fr_offset;
20634   /* Force misaligned offsets to 32-bit variant.  */
20635   if (offset & low)
20636     return 4;
20637   if (offset & ~mask)
20638     return 4;
20639   return 2;
20640 }
20641
20642 /* Get the address of a symbol during relaxation.  */
20643 static addressT
20644 relaxed_symbol_addr (fragS *fragp, long stretch)
20645 {
20646   fragS *sym_frag;
20647   addressT addr;
20648   symbolS *sym;
20649
20650   sym = fragp->fr_symbol;
20651   sym_frag = symbol_get_frag (sym);
20652   know (S_GET_SEGMENT (sym) != absolute_section
20653         || sym_frag == &zero_address_frag);
20654   addr = S_GET_VALUE (sym) + fragp->fr_offset;
20655
20656   /* If frag has yet to be reached on this pass, assume it will
20657      move by STRETCH just as we did.  If this is not so, it will
20658      be because some frag between grows, and that will force
20659      another pass.  */
20660
20661   if (stretch != 0
20662       && sym_frag->relax_marker != fragp->relax_marker)
20663     {
20664       fragS *f;
20665
20666       /* Adjust stretch for any alignment frag.  Note that if have
20667          been expanding the earlier code, the symbol may be
20668          defined in what appears to be an earlier frag.  FIXME:
20669          This doesn't handle the fr_subtype field, which specifies
20670          a maximum number of bytes to skip when doing an
20671          alignment.  */
20672       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
20673         {
20674           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
20675             {
20676               if (stretch < 0)
20677                 stretch = - ((- stretch)
20678                              & ~ ((1 << (int) f->fr_offset) - 1));
20679               else
20680                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
20681               if (stretch == 0)
20682                 break;
20683             }
20684         }
20685       if (f != NULL)
20686         addr += stretch;
20687     }
20688
20689   return addr;
20690 }
20691
20692 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
20693    load.  */
20694 static int
20695 relax_adr (fragS *fragp, asection *sec, long stretch)
20696 {
20697   addressT addr;
20698   offsetT val;
20699
20700   /* Assume worst case for symbols not known to be in the same section.  */
20701   if (fragp->fr_symbol == NULL
20702       || !S_IS_DEFINED (fragp->fr_symbol)
20703       || sec != S_GET_SEGMENT (fragp->fr_symbol)
20704       || S_IS_WEAK (fragp->fr_symbol))
20705     return 4;
20706
20707   val = relaxed_symbol_addr (fragp, stretch);
20708   addr = fragp->fr_address + fragp->fr_fix;
20709   addr = (addr + 4) & ~3;
20710   /* Force misaligned targets to 32-bit variant.  */
20711   if (val & 3)
20712     return 4;
20713   val -= addr;
20714   if (val < 0 || val > 1020)
20715     return 4;
20716   return 2;
20717 }
20718
20719 /* Return the size of a relaxable add/sub immediate instruction.  */
20720 static int
20721 relax_addsub (fragS *fragp, asection *sec)
20722 {
20723   char *buf;
20724   int op;
20725
20726   buf = fragp->fr_literal + fragp->fr_fix;
20727   op = bfd_get_16(sec->owner, buf);
20728   if ((op & 0xf) == ((op >> 4) & 0xf))
20729     return relax_immediate (fragp, 8, 0);
20730   else
20731     return relax_immediate (fragp, 3, 0);
20732 }
20733
20734 /* Return TRUE iff the definition of symbol S could be pre-empted
20735    (overridden) at link or load time.  */
20736 static bfd_boolean
20737 symbol_preemptible (symbolS *s)
20738 {
20739   /* Weak symbols can always be pre-empted.  */
20740   if (S_IS_WEAK (s))
20741     return TRUE;
20742
20743   /* Non-global symbols cannot be pre-empted. */
20744   if (! S_IS_EXTERNAL (s))
20745     return FALSE;
20746
20747 #ifdef OBJ_ELF
20748   /* In ELF, a global symbol can be marked protected, or private.  In that
20749      case it can't be pre-empted (other definitions in the same link unit
20750      would violate the ODR).  */
20751   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
20752     return FALSE;
20753 #endif
20754
20755   /* Other global symbols might be pre-empted.  */
20756   return TRUE;
20757 }
20758
20759 /* Return the size of a relaxable branch instruction.  BITS is the
20760    size of the offset field in the narrow instruction.  */
20761
20762 static int
20763 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
20764 {
20765   addressT addr;
20766   offsetT val;
20767   offsetT limit;
20768
20769   /* Assume worst case for symbols not known to be in the same section.  */
20770   if (!S_IS_DEFINED (fragp->fr_symbol)
20771       || sec != S_GET_SEGMENT (fragp->fr_symbol)
20772       || S_IS_WEAK (fragp->fr_symbol))
20773     return 4;
20774
20775 #ifdef OBJ_ELF
20776   /* A branch to a function in ARM state will require interworking.  */
20777   if (S_IS_DEFINED (fragp->fr_symbol)
20778       && ARM_IS_FUNC (fragp->fr_symbol))
20779       return 4;
20780 #endif
20781
20782   if (symbol_preemptible (fragp->fr_symbol))
20783     return 4;
20784
20785   val = relaxed_symbol_addr (fragp, stretch);
20786   addr = fragp->fr_address + fragp->fr_fix + 4;
20787   val -= addr;
20788
20789   /* Offset is a signed value *2 */
20790   limit = 1 << bits;
20791   if (val >= limit || val < -limit)
20792     return 4;
20793   return 2;
20794 }
20795
20796
20797 /* Relax a machine dependent frag.  This returns the amount by which
20798    the current size of the frag should change.  */
20799
20800 int
20801 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
20802 {
20803   int oldsize;
20804   int newsize;
20805
20806   oldsize = fragp->fr_var;
20807   switch (fragp->fr_subtype)
20808     {
20809     case T_MNEM_ldr_pc2:
20810       newsize = relax_adr (fragp, sec, stretch);
20811       break;
20812     case T_MNEM_ldr_pc:
20813     case T_MNEM_ldr_sp:
20814     case T_MNEM_str_sp:
20815       newsize = relax_immediate (fragp, 8, 2);
20816       break;
20817     case T_MNEM_ldr:
20818     case T_MNEM_str:
20819       newsize = relax_immediate (fragp, 5, 2);
20820       break;
20821     case T_MNEM_ldrh:
20822     case T_MNEM_strh:
20823       newsize = relax_immediate (fragp, 5, 1);
20824       break;
20825     case T_MNEM_ldrb:
20826     case T_MNEM_strb:
20827       newsize = relax_immediate (fragp, 5, 0);
20828       break;
20829     case T_MNEM_adr:
20830       newsize = relax_adr (fragp, sec, stretch);
20831       break;
20832     case T_MNEM_mov:
20833     case T_MNEM_movs:
20834     case T_MNEM_cmp:
20835     case T_MNEM_cmn:
20836       newsize = relax_immediate (fragp, 8, 0);
20837       break;
20838     case T_MNEM_b:
20839       newsize = relax_branch (fragp, sec, 11, stretch);
20840       break;
20841     case T_MNEM_bcond:
20842       newsize = relax_branch (fragp, sec, 8, stretch);
20843       break;
20844     case T_MNEM_add_sp:
20845     case T_MNEM_add_pc:
20846       newsize = relax_immediate (fragp, 8, 2);
20847       break;
20848     case T_MNEM_inc_sp:
20849     case T_MNEM_dec_sp:
20850       newsize = relax_immediate (fragp, 7, 2);
20851       break;
20852     case T_MNEM_addi:
20853     case T_MNEM_addis:
20854     case T_MNEM_subi:
20855     case T_MNEM_subis:
20856       newsize = relax_addsub (fragp, sec);
20857       break;
20858     default:
20859       abort ();
20860     }
20861
20862   fragp->fr_var = newsize;
20863   /* Freeze wide instructions that are at or before the same location as
20864      in the previous pass.  This avoids infinite loops.
20865      Don't freeze them unconditionally because targets may be artificially
20866      misaligned by the expansion of preceding frags.  */
20867   if (stretch <= 0 && newsize > 2)
20868     {
20869       md_convert_frag (sec->owner, sec, fragp);
20870       frag_wane (fragp);
20871     }
20872
20873   return newsize - oldsize;
20874 }
20875
20876 /* Round up a section size to the appropriate boundary.  */
20877
20878 valueT
20879 md_section_align (segT   segment ATTRIBUTE_UNUSED,
20880                   valueT size)
20881 {
20882 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
20883   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
20884     {
20885       /* For a.out, force the section size to be aligned.  If we don't do
20886          this, BFD will align it for us, but it will not write out the
20887          final bytes of the section.  This may be a bug in BFD, but it is
20888          easier to fix it here since that is how the other a.out targets
20889          work.  */
20890       int align;
20891
20892       align = bfd_get_section_alignment (stdoutput, segment);
20893       size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
20894     }
20895 #endif
20896
20897   return size;
20898 }
20899
20900 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
20901    of an rs_align_code fragment.  */
20902
20903 void
20904 arm_handle_align (fragS * fragP)
20905 {
20906   static char const arm_noop[2][2][4] =
20907     {
20908       {  /* ARMv1 */
20909         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
20910         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
20911       },
20912       {  /* ARMv6k */
20913         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
20914         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
20915       },
20916     };
20917   static char const thumb_noop[2][2][2] =
20918     {
20919       {  /* Thumb-1 */
20920         {0xc0, 0x46},  /* LE */
20921         {0x46, 0xc0},  /* BE */
20922       },
20923       {  /* Thumb-2 */
20924         {0x00, 0xbf},  /* LE */
20925         {0xbf, 0x00}   /* BE */
20926       }
20927     };
20928   static char const wide_thumb_noop[2][4] =
20929     {  /* Wide Thumb-2 */
20930       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
20931       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
20932     };
20933
20934   unsigned bytes, fix, noop_size;
20935   char * p;
20936   const char * noop;
20937   const char *narrow_noop = NULL;
20938 #ifdef OBJ_ELF
20939   enum mstate state;
20940 #endif
20941
20942   if (fragP->fr_type != rs_align_code)
20943     return;
20944
20945   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
20946   p = fragP->fr_literal + fragP->fr_fix;
20947   fix = 0;
20948
20949   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
20950     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
20951
20952   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
20953
20954   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
20955     {
20956       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
20957                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
20958         {
20959           narrow_noop = thumb_noop[1][target_big_endian];
20960           noop = wide_thumb_noop[target_big_endian];
20961         }
20962       else
20963         noop = thumb_noop[0][target_big_endian];
20964       noop_size = 2;
20965 #ifdef OBJ_ELF
20966       state = MAP_THUMB;
20967 #endif
20968     }
20969   else
20970     {
20971       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
20972                                            ? selected_cpu : arm_arch_none,
20973                                            arm_ext_v6k) != 0]
20974                      [target_big_endian];
20975       noop_size = 4;
20976 #ifdef OBJ_ELF
20977       state = MAP_ARM;
20978 #endif
20979     }
20980
20981   fragP->fr_var = noop_size;
20982
20983   if (bytes & (noop_size - 1))
20984     {
20985       fix = bytes & (noop_size - 1);
20986 #ifdef OBJ_ELF
20987       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
20988 #endif
20989       memset (p, 0, fix);
20990       p += fix;
20991       bytes -= fix;
20992     }
20993
20994   if (narrow_noop)
20995     {
20996       if (bytes & noop_size)
20997         {
20998           /* Insert a narrow noop.  */
20999           memcpy (p, narrow_noop, noop_size);
21000           p += noop_size;
21001           bytes -= noop_size;
21002           fix += noop_size;
21003         }
21004
21005       /* Use wide noops for the remainder */
21006       noop_size = 4;
21007     }
21008
21009   while (bytes >= noop_size)
21010     {
21011       memcpy (p, noop, noop_size);
21012       p += noop_size;
21013       bytes -= noop_size;
21014       fix += noop_size;
21015     }
21016
21017   fragP->fr_fix += fix;
21018 }
21019
21020 /* Called from md_do_align.  Used to create an alignment
21021    frag in a code section.  */
21022
21023 void
21024 arm_frag_align_code (int n, int max)
21025 {
21026   char * p;
21027
21028   /* We assume that there will never be a requirement
21029      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
21030   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
21031     {
21032       char err_msg[128];
21033
21034       sprintf (err_msg,
21035         _("alignments greater than %d bytes not supported in .text sections."),
21036         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
21037       as_fatal ("%s", err_msg);
21038     }
21039
21040   p = frag_var (rs_align_code,
21041                 MAX_MEM_FOR_RS_ALIGN_CODE,
21042                 1,
21043                 (relax_substateT) max,
21044                 (symbolS *) NULL,
21045                 (offsetT) n,
21046                 (char *) NULL);
21047   *p = 0;
21048 }
21049
21050 /* Perform target specific initialisation of a frag.
21051    Note - despite the name this initialisation is not done when the frag
21052    is created, but only when its type is assigned.  A frag can be created
21053    and used a long time before its type is set, so beware of assuming that
21054    this initialisationis performed first.  */
21055
21056 #ifndef OBJ_ELF
21057 void
21058 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
21059 {
21060   /* Record whether this frag is in an ARM or a THUMB area.  */
21061   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21062 }
21063
21064 #else /* OBJ_ELF is defined.  */
21065 void
21066 arm_init_frag (fragS * fragP, int max_chars)
21067 {
21068   int frag_thumb_mode;
21069
21070   /* If the current ARM vs THUMB mode has not already
21071      been recorded into this frag then do so now.  */
21072   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
21073     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21074
21075   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
21076
21077   /* Record a mapping symbol for alignment frags.  We will delete this
21078      later if the alignment ends up empty.  */
21079   switch (fragP->fr_type)
21080     {
21081     case rs_align:
21082     case rs_align_test:
21083     case rs_fill:
21084       mapping_state_2 (MAP_DATA, max_chars);
21085       break;
21086     case rs_align_code:
21087       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
21088       break;
21089     default:
21090       break;
21091     }
21092 }
21093
21094 /* When we change sections we need to issue a new mapping symbol.  */
21095
21096 void
21097 arm_elf_change_section (void)
21098 {
21099   /* Link an unlinked unwind index table section to the .text section.  */
21100   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
21101       && elf_linked_to_section (now_seg) == NULL)
21102     elf_linked_to_section (now_seg) = text_section;
21103 }
21104
21105 int
21106 arm_elf_section_type (const char * str, size_t len)
21107 {
21108   if (len == 5 && strncmp (str, "exidx", 5) == 0)
21109     return SHT_ARM_EXIDX;
21110
21111   return -1;
21112 }
21113 \f
21114 /* Code to deal with unwinding tables.  */
21115
21116 static void add_unwind_adjustsp (offsetT);
21117
21118 /* Generate any deferred unwind frame offset.  */
21119
21120 static void
21121 flush_pending_unwind (void)
21122 {
21123   offsetT offset;
21124
21125   offset = unwind.pending_offset;
21126   unwind.pending_offset = 0;
21127   if (offset != 0)
21128     add_unwind_adjustsp (offset);
21129 }
21130
21131 /* Add an opcode to this list for this function.  Two-byte opcodes should
21132    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
21133    order.  */
21134
21135 static void
21136 add_unwind_opcode (valueT op, int length)
21137 {
21138   /* Add any deferred stack adjustment.  */
21139   if (unwind.pending_offset)
21140     flush_pending_unwind ();
21141
21142   unwind.sp_restored = 0;
21143
21144   if (unwind.opcode_count + length > unwind.opcode_alloc)
21145     {
21146       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
21147       if (unwind.opcodes)
21148         unwind.opcodes = (unsigned char *) xrealloc (unwind.opcodes,
21149                                                      unwind.opcode_alloc);
21150       else
21151         unwind.opcodes = (unsigned char *) xmalloc (unwind.opcode_alloc);
21152     }
21153   while (length > 0)
21154     {
21155       length--;
21156       unwind.opcodes[unwind.opcode_count] = op & 0xff;
21157       op >>= 8;
21158       unwind.opcode_count++;
21159     }
21160 }
21161
21162 /* Add unwind opcodes to adjust the stack pointer.  */
21163
21164 static void
21165 add_unwind_adjustsp (offsetT offset)
21166 {
21167   valueT op;
21168
21169   if (offset > 0x200)
21170     {
21171       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
21172       char bytes[5];
21173       int n;
21174       valueT o;
21175
21176       /* Long form: 0xb2, uleb128.  */
21177       /* This might not fit in a word so add the individual bytes,
21178          remembering the list is built in reverse order.  */
21179       o = (valueT) ((offset - 0x204) >> 2);
21180       if (o == 0)
21181         add_unwind_opcode (0, 1);
21182
21183       /* Calculate the uleb128 encoding of the offset.  */
21184       n = 0;
21185       while (o)
21186         {
21187           bytes[n] = o & 0x7f;
21188           o >>= 7;
21189           if (o)
21190             bytes[n] |= 0x80;
21191           n++;
21192         }
21193       /* Add the insn.  */
21194       for (; n; n--)
21195         add_unwind_opcode (bytes[n - 1], 1);
21196       add_unwind_opcode (0xb2, 1);
21197     }
21198   else if (offset > 0x100)
21199     {
21200       /* Two short opcodes.  */
21201       add_unwind_opcode (0x3f, 1);
21202       op = (offset - 0x104) >> 2;
21203       add_unwind_opcode (op, 1);
21204     }
21205   else if (offset > 0)
21206     {
21207       /* Short opcode.  */
21208       op = (offset - 4) >> 2;
21209       add_unwind_opcode (op, 1);
21210     }
21211   else if (offset < 0)
21212     {
21213       offset = -offset;
21214       while (offset > 0x100)
21215         {
21216           add_unwind_opcode (0x7f, 1);
21217           offset -= 0x100;
21218         }
21219       op = ((offset - 4) >> 2) | 0x40;
21220       add_unwind_opcode (op, 1);
21221     }
21222 }
21223
21224 /* Finish the list of unwind opcodes for this function.  */
21225 static void
21226 finish_unwind_opcodes (void)
21227 {
21228   valueT op;
21229
21230   if (unwind.fp_used)
21231     {
21232       /* Adjust sp as necessary.  */
21233       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
21234       flush_pending_unwind ();
21235
21236       /* After restoring sp from the frame pointer.  */
21237       op = 0x90 | unwind.fp_reg;
21238       add_unwind_opcode (op, 1);
21239     }
21240   else
21241     flush_pending_unwind ();
21242 }
21243
21244
21245 /* Start an exception table entry.  If idx is nonzero this is an index table
21246    entry.  */
21247
21248 static void
21249 start_unwind_section (const segT text_seg, int idx)
21250 {
21251   const char * text_name;
21252   const char * prefix;
21253   const char * prefix_once;
21254   const char * group_name;
21255   size_t prefix_len;
21256   size_t text_len;
21257   char * sec_name;
21258   size_t sec_name_len;
21259   int type;
21260   int flags;
21261   int linkonce;
21262
21263   if (idx)
21264     {
21265       prefix = ELF_STRING_ARM_unwind;
21266       prefix_once = ELF_STRING_ARM_unwind_once;
21267       type = SHT_ARM_EXIDX;
21268     }
21269   else
21270     {
21271       prefix = ELF_STRING_ARM_unwind_info;
21272       prefix_once = ELF_STRING_ARM_unwind_info_once;
21273       type = SHT_PROGBITS;
21274     }
21275
21276   text_name = segment_name (text_seg);
21277   if (streq (text_name, ".text"))
21278     text_name = "";
21279
21280   if (strncmp (text_name, ".gnu.linkonce.t.",
21281                strlen (".gnu.linkonce.t.")) == 0)
21282     {
21283       prefix = prefix_once;
21284       text_name += strlen (".gnu.linkonce.t.");
21285     }
21286
21287   prefix_len = strlen (prefix);
21288   text_len = strlen (text_name);
21289   sec_name_len = prefix_len + text_len;
21290   sec_name = (char *) xmalloc (sec_name_len + 1);
21291   memcpy (sec_name, prefix, prefix_len);
21292   memcpy (sec_name + prefix_len, text_name, text_len);
21293   sec_name[prefix_len + text_len] = '\0';
21294
21295   flags = SHF_ALLOC;
21296   linkonce = 0;
21297   group_name = 0;
21298
21299   /* Handle COMDAT group.  */
21300   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
21301     {
21302       group_name = elf_group_name (text_seg);
21303       if (group_name == NULL)
21304         {
21305           as_bad (_("Group section `%s' has no group signature"),
21306                   segment_name (text_seg));
21307           ignore_rest_of_line ();
21308           return;
21309         }
21310       flags |= SHF_GROUP;
21311       linkonce = 1;
21312     }
21313
21314   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
21315
21316   /* Set the section link for index tables.  */
21317   if (idx)
21318     elf_linked_to_section (now_seg) = text_seg;
21319 }
21320
21321
21322 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
21323    personality routine data.  Returns zero, or the index table value for
21324    an inline entry.  */
21325
21326 static valueT
21327 create_unwind_entry (int have_data)
21328 {
21329   int size;
21330   addressT where;
21331   char *ptr;
21332   /* The current word of data.  */
21333   valueT data;
21334   /* The number of bytes left in this word.  */
21335   int n;
21336
21337   finish_unwind_opcodes ();
21338
21339   /* Remember the current text section.  */
21340   unwind.saved_seg = now_seg;
21341   unwind.saved_subseg = now_subseg;
21342
21343   start_unwind_section (now_seg, 0);
21344
21345   if (unwind.personality_routine == NULL)
21346     {
21347       if (unwind.personality_index == -2)
21348         {
21349           if (have_data)
21350             as_bad (_("handlerdata in cantunwind frame"));
21351           return 1; /* EXIDX_CANTUNWIND.  */
21352         }
21353
21354       /* Use a default personality routine if none is specified.  */
21355       if (unwind.personality_index == -1)
21356         {
21357           if (unwind.opcode_count > 3)
21358             unwind.personality_index = 1;
21359           else
21360             unwind.personality_index = 0;
21361         }
21362
21363       /* Space for the personality routine entry.  */
21364       if (unwind.personality_index == 0)
21365         {
21366           if (unwind.opcode_count > 3)
21367             as_bad (_("too many unwind opcodes for personality routine 0"));
21368
21369           if (!have_data)
21370             {
21371               /* All the data is inline in the index table.  */
21372               data = 0x80;
21373               n = 3;
21374               while (unwind.opcode_count > 0)
21375                 {
21376                   unwind.opcode_count--;
21377                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
21378                   n--;
21379                 }
21380
21381               /* Pad with "finish" opcodes.  */
21382               while (n--)
21383                 data = (data << 8) | 0xb0;
21384
21385               return data;
21386             }
21387           size = 0;
21388         }
21389       else
21390         /* We get two opcodes "free" in the first word.  */
21391         size = unwind.opcode_count - 2;
21392     }
21393   else
21394     {
21395       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
21396       if (unwind.personality_index != -1)
21397         {
21398           as_bad (_("attempt to recreate an unwind entry"));
21399           return 1;
21400         }
21401
21402       /* An extra byte is required for the opcode count.        */
21403       size = unwind.opcode_count + 1;
21404     }
21405
21406   size = (size + 3) >> 2;
21407   if (size > 0xff)
21408     as_bad (_("too many unwind opcodes"));
21409
21410   frag_align (2, 0, 0);
21411   record_alignment (now_seg, 2);
21412   unwind.table_entry = expr_build_dot ();
21413
21414   /* Allocate the table entry.  */
21415   ptr = frag_more ((size << 2) + 4);
21416   /* PR 13449: Zero the table entries in case some of them are not used.  */
21417   memset (ptr, 0, (size << 2) + 4);
21418   where = frag_now_fix () - ((size << 2) + 4);
21419
21420   switch (unwind.personality_index)
21421     {
21422     case -1:
21423       /* ??? Should this be a PLT generating relocation?  */
21424       /* Custom personality routine.  */
21425       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
21426                BFD_RELOC_ARM_PREL31);
21427
21428       where += 4;
21429       ptr += 4;
21430
21431       /* Set the first byte to the number of additional words.  */
21432       data = size > 0 ? size - 1 : 0;
21433       n = 3;
21434       break;
21435
21436     /* ABI defined personality routines.  */
21437     case 0:
21438       /* Three opcodes bytes are packed into the first word.  */
21439       data = 0x80;
21440       n = 3;
21441       break;
21442
21443     case 1:
21444     case 2:
21445       /* The size and first two opcode bytes go in the first word.  */
21446       data = ((0x80 + unwind.personality_index) << 8) | size;
21447       n = 2;
21448       break;
21449
21450     default:
21451       /* Should never happen.  */
21452       abort ();
21453     }
21454
21455   /* Pack the opcodes into words (MSB first), reversing the list at the same
21456      time.  */
21457   while (unwind.opcode_count > 0)
21458     {
21459       if (n == 0)
21460         {
21461           md_number_to_chars (ptr, data, 4);
21462           ptr += 4;
21463           n = 4;
21464           data = 0;
21465         }
21466       unwind.opcode_count--;
21467       n--;
21468       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
21469     }
21470
21471   /* Finish off the last word.  */
21472   if (n < 4)
21473     {
21474       /* Pad with "finish" opcodes.  */
21475       while (n--)
21476         data = (data << 8) | 0xb0;
21477
21478       md_number_to_chars (ptr, data, 4);
21479     }
21480
21481   if (!have_data)
21482     {
21483       /* Add an empty descriptor if there is no user-specified data.   */
21484       ptr = frag_more (4);
21485       md_number_to_chars (ptr, 0, 4);
21486     }
21487
21488   return 0;
21489 }
21490
21491
21492 /* Initialize the DWARF-2 unwind information for this procedure.  */
21493
21494 void
21495 tc_arm_frame_initial_instructions (void)
21496 {
21497   cfi_add_CFA_def_cfa (REG_SP, 0);
21498 }
21499 #endif /* OBJ_ELF */
21500
21501 /* Convert REGNAME to a DWARF-2 register number.  */
21502
21503 int
21504 tc_arm_regname_to_dw2regnum (char *regname)
21505 {
21506   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
21507   if (reg != FAIL)
21508     return reg;
21509
21510   /* PR 16694: Allow VFP registers as well.  */
21511   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
21512   if (reg != FAIL)
21513     return 64 + reg;
21514
21515   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
21516   if (reg != FAIL)
21517     return reg + 256;
21518
21519   return -1;
21520 }
21521
21522 #ifdef TE_PE
21523 void
21524 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
21525 {
21526   expressionS exp;
21527
21528   exp.X_op = O_secrel;
21529   exp.X_add_symbol = symbol;
21530   exp.X_add_number = 0;
21531   emit_expr (&exp, size);
21532 }
21533 #endif
21534
21535 /* MD interface: Symbol and relocation handling.  */
21536
21537 /* Return the address within the segment that a PC-relative fixup is
21538    relative to.  For ARM, PC-relative fixups applied to instructions
21539    are generally relative to the location of the fixup plus 8 bytes.
21540    Thumb branches are offset by 4, and Thumb loads relative to PC
21541    require special handling.  */
21542
21543 long
21544 md_pcrel_from_section (fixS * fixP, segT seg)
21545 {
21546   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
21547
21548   /* If this is pc-relative and we are going to emit a relocation
21549      then we just want to put out any pipeline compensation that the linker
21550      will need.  Otherwise we want to use the calculated base.
21551      For WinCE we skip the bias for externals as well, since this
21552      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
21553   if (fixP->fx_pcrel
21554       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
21555           || (arm_force_relocation (fixP)
21556 #ifdef TE_WINCE
21557               && !S_IS_EXTERNAL (fixP->fx_addsy)
21558 #endif
21559               )))
21560     base = 0;
21561
21562
21563   switch (fixP->fx_r_type)
21564     {
21565       /* PC relative addressing on the Thumb is slightly odd as the
21566          bottom two bits of the PC are forced to zero for the
21567          calculation.  This happens *after* application of the
21568          pipeline offset.  However, Thumb adrl already adjusts for
21569          this, so we need not do it again.  */
21570     case BFD_RELOC_ARM_THUMB_ADD:
21571       return base & ~3;
21572
21573     case BFD_RELOC_ARM_THUMB_OFFSET:
21574     case BFD_RELOC_ARM_T32_OFFSET_IMM:
21575     case BFD_RELOC_ARM_T32_ADD_PC12:
21576     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
21577       return (base + 4) & ~3;
21578
21579       /* Thumb branches are simply offset by +4.  */
21580     case BFD_RELOC_THUMB_PCREL_BRANCH7:
21581     case BFD_RELOC_THUMB_PCREL_BRANCH9:
21582     case BFD_RELOC_THUMB_PCREL_BRANCH12:
21583     case BFD_RELOC_THUMB_PCREL_BRANCH20:
21584     case BFD_RELOC_THUMB_PCREL_BRANCH25:
21585       return base + 4;
21586
21587     case BFD_RELOC_THUMB_PCREL_BRANCH23:
21588       if (fixP->fx_addsy
21589           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21590           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21591           && ARM_IS_FUNC (fixP->fx_addsy)
21592           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21593         base = fixP->fx_where + fixP->fx_frag->fr_address;
21594        return base + 4;
21595
21596       /* BLX is like branches above, but forces the low two bits of PC to
21597          zero.  */
21598     case BFD_RELOC_THUMB_PCREL_BLX:
21599       if (fixP->fx_addsy
21600           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21601           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21602           && THUMB_IS_FUNC (fixP->fx_addsy)
21603           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21604         base = fixP->fx_where + fixP->fx_frag->fr_address;
21605       return (base + 4) & ~3;
21606
21607       /* ARM mode branches are offset by +8.  However, the Windows CE
21608          loader expects the relocation not to take this into account.  */
21609     case BFD_RELOC_ARM_PCREL_BLX:
21610       if (fixP->fx_addsy
21611           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21612           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21613           && ARM_IS_FUNC (fixP->fx_addsy)
21614           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21615         base = fixP->fx_where + fixP->fx_frag->fr_address;
21616       return base + 8;
21617
21618     case BFD_RELOC_ARM_PCREL_CALL:
21619       if (fixP->fx_addsy
21620           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21621           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21622           && THUMB_IS_FUNC (fixP->fx_addsy)
21623           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21624         base = fixP->fx_where + fixP->fx_frag->fr_address;
21625       return base + 8;
21626
21627     case BFD_RELOC_ARM_PCREL_BRANCH:
21628     case BFD_RELOC_ARM_PCREL_JUMP:
21629     case BFD_RELOC_ARM_PLT32:
21630 #ifdef TE_WINCE
21631       /* When handling fixups immediately, because we have already
21632          discovered the value of a symbol, or the address of the frag involved
21633          we must account for the offset by +8, as the OS loader will never see the reloc.
21634          see fixup_segment() in write.c
21635          The S_IS_EXTERNAL test handles the case of global symbols.
21636          Those need the calculated base, not just the pipe compensation the linker will need.  */
21637       if (fixP->fx_pcrel
21638           && fixP->fx_addsy != NULL
21639           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21640           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
21641         return base + 8;
21642       return base;
21643 #else
21644       return base + 8;
21645 #endif
21646
21647
21648       /* ARM mode loads relative to PC are also offset by +8.  Unlike
21649          branches, the Windows CE loader *does* expect the relocation
21650          to take this into account.  */
21651     case BFD_RELOC_ARM_OFFSET_IMM:
21652     case BFD_RELOC_ARM_OFFSET_IMM8:
21653     case BFD_RELOC_ARM_HWLITERAL:
21654     case BFD_RELOC_ARM_LITERAL:
21655     case BFD_RELOC_ARM_CP_OFF_IMM:
21656       return base + 8;
21657
21658
21659       /* Other PC-relative relocations are un-offset.  */
21660     default:
21661       return base;
21662     }
21663 }
21664
21665 static bfd_boolean flag_warn_syms = TRUE;
21666
21667 bfd_boolean
21668 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
21669 {
21670   /* PR 18347 - Warn if the user attempts to create a symbol with the same
21671      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
21672      does mean that the resulting code might be very confusing to the reader.
21673      Also this warning can be triggered if the user omits an operand before
21674      an immediate address, eg:
21675
21676        LDR =foo
21677
21678      GAS treats this as an assignment of the value of the symbol foo to a
21679      symbol LDR, and so (without this code) it will not issue any kind of
21680      warning or error message.
21681
21682      Note - ARM instructions are case-insensitive but the strings in the hash
21683      table are all stored in lower case, so we must first ensure that name is
21684      lower case too.  */
21685   if (flag_warn_syms && arm_ops_hsh)
21686     {
21687       char * nbuf = strdup (name);
21688       char * p;
21689
21690       for (p = nbuf; *p; p++)
21691         *p = TOLOWER (*p);
21692       if (hash_find (arm_ops_hsh, nbuf) != NULL)
21693         {
21694           static struct hash_control * already_warned = NULL;
21695
21696           if (already_warned == NULL)
21697             already_warned = hash_new ();
21698           /* Only warn about the symbol once.  To keep the code
21699              simple we let hash_insert do the lookup for us.  */
21700           if (hash_insert (already_warned, name, NULL) == NULL)
21701             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
21702         }
21703       else
21704         free (nbuf);
21705     }
21706   
21707   return FALSE;
21708 }
21709
21710 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
21711    Otherwise we have no need to default values of symbols.  */
21712
21713 symbolS *
21714 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
21715 {
21716 #ifdef OBJ_ELF
21717   if (name[0] == '_' && name[1] == 'G'
21718       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
21719     {
21720       if (!GOT_symbol)
21721         {
21722           if (symbol_find (name))
21723             as_bad (_("GOT already in the symbol table"));
21724
21725           GOT_symbol = symbol_new (name, undefined_section,
21726                                    (valueT) 0, & zero_address_frag);
21727         }
21728
21729       return GOT_symbol;
21730     }
21731 #endif
21732
21733   return NULL;
21734 }
21735
21736 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
21737    computed as two separate immediate values, added together.  We
21738    already know that this value cannot be computed by just one ARM
21739    instruction.  */
21740
21741 static unsigned int
21742 validate_immediate_twopart (unsigned int   val,
21743                             unsigned int * highpart)
21744 {
21745   unsigned int a;
21746   unsigned int i;
21747
21748   for (i = 0; i < 32; i += 2)
21749     if (((a = rotate_left (val, i)) & 0xff) != 0)
21750       {
21751         if (a & 0xff00)
21752           {
21753             if (a & ~ 0xffff)
21754               continue;
21755             * highpart = (a  >> 8) | ((i + 24) << 7);
21756           }
21757         else if (a & 0xff0000)
21758           {
21759             if (a & 0xff000000)
21760               continue;
21761             * highpart = (a >> 16) | ((i + 16) << 7);
21762           }
21763         else
21764           {
21765             gas_assert (a & 0xff000000);
21766             * highpart = (a >> 24) | ((i + 8) << 7);
21767           }
21768
21769         return (a & 0xff) | (i << 7);
21770       }
21771
21772   return FAIL;
21773 }
21774
21775 static int
21776 validate_offset_imm (unsigned int val, int hwse)
21777 {
21778   if ((hwse && val > 255) || val > 4095)
21779     return FAIL;
21780   return val;
21781 }
21782
21783 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
21784    negative immediate constant by altering the instruction.  A bit of
21785    a hack really.
21786         MOV <-> MVN
21787         AND <-> BIC
21788         ADC <-> SBC
21789         by inverting the second operand, and
21790         ADD <-> SUB
21791         CMP <-> CMN
21792         by negating the second operand.  */
21793
21794 static int
21795 negate_data_op (unsigned long * instruction,
21796                 unsigned long   value)
21797 {
21798   int op, new_inst;
21799   unsigned long negated, inverted;
21800
21801   negated = encode_arm_immediate (-value);
21802   inverted = encode_arm_immediate (~value);
21803
21804   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
21805   switch (op)
21806     {
21807       /* First negates.  */
21808     case OPCODE_SUB:             /* ADD <-> SUB  */
21809       new_inst = OPCODE_ADD;
21810       value = negated;
21811       break;
21812
21813     case OPCODE_ADD:
21814       new_inst = OPCODE_SUB;
21815       value = negated;
21816       break;
21817
21818     case OPCODE_CMP:             /* CMP <-> CMN  */
21819       new_inst = OPCODE_CMN;
21820       value = negated;
21821       break;
21822
21823     case OPCODE_CMN:
21824       new_inst = OPCODE_CMP;
21825       value = negated;
21826       break;
21827
21828       /* Now Inverted ops.  */
21829     case OPCODE_MOV:             /* MOV <-> MVN  */
21830       new_inst = OPCODE_MVN;
21831       value = inverted;
21832       break;
21833
21834     case OPCODE_MVN:
21835       new_inst = OPCODE_MOV;
21836       value = inverted;
21837       break;
21838
21839     case OPCODE_AND:             /* AND <-> BIC  */
21840       new_inst = OPCODE_BIC;
21841       value = inverted;
21842       break;
21843
21844     case OPCODE_BIC:
21845       new_inst = OPCODE_AND;
21846       value = inverted;
21847       break;
21848
21849     case OPCODE_ADC:              /* ADC <-> SBC  */
21850       new_inst = OPCODE_SBC;
21851       value = inverted;
21852       break;
21853
21854     case OPCODE_SBC:
21855       new_inst = OPCODE_ADC;
21856       value = inverted;
21857       break;
21858
21859       /* We cannot do anything.  */
21860     default:
21861       return FAIL;
21862     }
21863
21864   if (value == (unsigned) FAIL)
21865     return FAIL;
21866
21867   *instruction &= OPCODE_MASK;
21868   *instruction |= new_inst << DATA_OP_SHIFT;
21869   return value;
21870 }
21871
21872 /* Like negate_data_op, but for Thumb-2.   */
21873
21874 static unsigned int
21875 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
21876 {
21877   int op, new_inst;
21878   int rd;
21879   unsigned int negated, inverted;
21880
21881   negated = encode_thumb32_immediate (-value);
21882   inverted = encode_thumb32_immediate (~value);
21883
21884   rd = (*instruction >> 8) & 0xf;
21885   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
21886   switch (op)
21887     {
21888       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
21889     case T2_OPCODE_SUB:
21890       new_inst = T2_OPCODE_ADD;
21891       value = negated;
21892       break;
21893
21894     case T2_OPCODE_ADD:
21895       new_inst = T2_OPCODE_SUB;
21896       value = negated;
21897       break;
21898
21899       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
21900     case T2_OPCODE_ORR:
21901       new_inst = T2_OPCODE_ORN;
21902       value = inverted;
21903       break;
21904
21905     case T2_OPCODE_ORN:
21906       new_inst = T2_OPCODE_ORR;
21907       value = inverted;
21908       break;
21909
21910       /* AND <-> BIC.  TST has no inverted equivalent.  */
21911     case T2_OPCODE_AND:
21912       new_inst = T2_OPCODE_BIC;
21913       if (rd == 15)
21914         value = FAIL;
21915       else
21916         value = inverted;
21917       break;
21918
21919     case T2_OPCODE_BIC:
21920       new_inst = T2_OPCODE_AND;
21921       value = inverted;
21922       break;
21923
21924       /* ADC <-> SBC  */
21925     case T2_OPCODE_ADC:
21926       new_inst = T2_OPCODE_SBC;
21927       value = inverted;
21928       break;
21929
21930     case T2_OPCODE_SBC:
21931       new_inst = T2_OPCODE_ADC;
21932       value = inverted;
21933       break;
21934
21935       /* We cannot do anything.  */
21936     default:
21937       return FAIL;
21938     }
21939
21940   if (value == (unsigned int)FAIL)
21941     return FAIL;
21942
21943   *instruction &= T2_OPCODE_MASK;
21944   *instruction |= new_inst << T2_DATA_OP_SHIFT;
21945   return value;
21946 }
21947
21948 /* Read a 32-bit thumb instruction from buf.  */
21949 static unsigned long
21950 get_thumb32_insn (char * buf)
21951 {
21952   unsigned long insn;
21953   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
21954   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
21955
21956   return insn;
21957 }
21958
21959
21960 /* We usually want to set the low bit on the address of thumb function
21961    symbols.  In particular .word foo - . should have the low bit set.
21962    Generic code tries to fold the difference of two symbols to
21963    a constant.  Prevent this and force a relocation when the first symbols
21964    is a thumb function.  */
21965
21966 bfd_boolean
21967 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
21968 {
21969   if (op == O_subtract
21970       && l->X_op == O_symbol
21971       && r->X_op == O_symbol
21972       && THUMB_IS_FUNC (l->X_add_symbol))
21973     {
21974       l->X_op = O_subtract;
21975       l->X_op_symbol = r->X_add_symbol;
21976       l->X_add_number -= r->X_add_number;
21977       return TRUE;
21978     }
21979
21980   /* Process as normal.  */
21981   return FALSE;
21982 }
21983
21984 /* Encode Thumb2 unconditional branches and calls. The encoding
21985    for the 2 are identical for the immediate values.  */
21986
21987 static void
21988 encode_thumb2_b_bl_offset (char * buf, offsetT value)
21989 {
21990 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
21991   offsetT newval;
21992   offsetT newval2;
21993   addressT S, I1, I2, lo, hi;
21994
21995   S = (value >> 24) & 0x01;
21996   I1 = (value >> 23) & 0x01;
21997   I2 = (value >> 22) & 0x01;
21998   hi = (value >> 12) & 0x3ff;
21999   lo = (value >> 1) & 0x7ff;
22000   newval   = md_chars_to_number (buf, THUMB_SIZE);
22001   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22002   newval  |= (S << 10) | hi;
22003   newval2 &=  ~T2I1I2MASK;
22004   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
22005   md_number_to_chars (buf, newval, THUMB_SIZE);
22006   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
22007 }
22008
22009 void
22010 md_apply_fix (fixS *    fixP,
22011                valueT * valP,
22012                segT     seg)
22013 {
22014   offsetT        value = * valP;
22015   offsetT        newval;
22016   unsigned int   newimm;
22017   unsigned long  temp;
22018   int            sign;
22019   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
22020
22021   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
22022
22023   /* Note whether this will delete the relocation.  */
22024
22025   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
22026     fixP->fx_done = 1;
22027
22028   /* On a 64-bit host, silently truncate 'value' to 32 bits for
22029      consistency with the behaviour on 32-bit hosts.  Remember value
22030      for emit_reloc.  */
22031   value &= 0xffffffff;
22032   value ^= 0x80000000;
22033   value -= 0x80000000;
22034
22035   *valP = value;
22036   fixP->fx_addnumber = value;
22037
22038   /* Same treatment for fixP->fx_offset.  */
22039   fixP->fx_offset &= 0xffffffff;
22040   fixP->fx_offset ^= 0x80000000;
22041   fixP->fx_offset -= 0x80000000;
22042
22043   switch (fixP->fx_r_type)
22044     {
22045     case BFD_RELOC_NONE:
22046       /* This will need to go in the object file.  */
22047       fixP->fx_done = 0;
22048       break;
22049
22050     case BFD_RELOC_ARM_IMMEDIATE:
22051       /* We claim that this fixup has been processed here,
22052          even if in fact we generate an error because we do
22053          not have a reloc for it, so tc_gen_reloc will reject it.  */
22054       fixP->fx_done = 1;
22055
22056       if (fixP->fx_addsy)
22057         {
22058           const char *msg = 0;
22059
22060           if (! S_IS_DEFINED (fixP->fx_addsy))
22061             msg = _("undefined symbol %s used as an immediate value");
22062           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
22063             msg = _("symbol %s is in a different section");
22064           else if (S_IS_WEAK (fixP->fx_addsy))
22065             msg = _("symbol %s is weak and may be overridden later");
22066
22067           if (msg)
22068             {
22069               as_bad_where (fixP->fx_file, fixP->fx_line,
22070                             msg, S_GET_NAME (fixP->fx_addsy));
22071               break;
22072             }
22073         }
22074
22075       temp = md_chars_to_number (buf, INSN_SIZE);
22076
22077       /* If the offset is negative, we should use encoding A2 for ADR.  */
22078       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
22079         newimm = negate_data_op (&temp, value);
22080       else
22081         {
22082           newimm = encode_arm_immediate (value);
22083
22084           /* If the instruction will fail, see if we can fix things up by
22085              changing the opcode.  */
22086           if (newimm == (unsigned int) FAIL)
22087             newimm = negate_data_op (&temp, value);
22088         }
22089
22090       if (newimm == (unsigned int) FAIL)
22091         {
22092           as_bad_where (fixP->fx_file, fixP->fx_line,
22093                         _("invalid constant (%lx) after fixup"),
22094                         (unsigned long) value);
22095           break;
22096         }
22097
22098       newimm |= (temp & 0xfffff000);
22099       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
22100       break;
22101
22102     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
22103       {
22104         unsigned int highpart = 0;
22105         unsigned int newinsn  = 0xe1a00000; /* nop.  */
22106
22107         if (fixP->fx_addsy)
22108           {
22109             const char *msg = 0;
22110
22111             if (! S_IS_DEFINED (fixP->fx_addsy))
22112               msg = _("undefined symbol %s used as an immediate value");
22113             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
22114               msg = _("symbol %s is in a different section");
22115             else if (S_IS_WEAK (fixP->fx_addsy))
22116               msg = _("symbol %s is weak and may be overridden later");
22117
22118             if (msg)
22119               {
22120                 as_bad_where (fixP->fx_file, fixP->fx_line,
22121                               msg, S_GET_NAME (fixP->fx_addsy));
22122                 break;
22123               }
22124           }
22125
22126         newimm = encode_arm_immediate (value);
22127         temp = md_chars_to_number (buf, INSN_SIZE);
22128
22129         /* If the instruction will fail, see if we can fix things up by
22130            changing the opcode.  */
22131         if (newimm == (unsigned int) FAIL
22132             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
22133           {
22134             /* No ?  OK - try using two ADD instructions to generate
22135                the value.  */
22136             newimm = validate_immediate_twopart (value, & highpart);
22137
22138             /* Yes - then make sure that the second instruction is
22139                also an add.  */
22140             if (newimm != (unsigned int) FAIL)
22141               newinsn = temp;
22142             /* Still No ?  Try using a negated value.  */
22143             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
22144               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
22145             /* Otherwise - give up.  */
22146             else
22147               {
22148                 as_bad_where (fixP->fx_file, fixP->fx_line,
22149                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
22150                               (long) value);
22151                 break;
22152               }
22153
22154             /* Replace the first operand in the 2nd instruction (which
22155                is the PC) with the destination register.  We have
22156                already added in the PC in the first instruction and we
22157                do not want to do it again.  */
22158             newinsn &= ~ 0xf0000;
22159             newinsn |= ((newinsn & 0x0f000) << 4);
22160           }
22161
22162         newimm |= (temp & 0xfffff000);
22163         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
22164
22165         highpart |= (newinsn & 0xfffff000);
22166         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
22167       }
22168       break;
22169
22170     case BFD_RELOC_ARM_OFFSET_IMM:
22171       if (!fixP->fx_done && seg->use_rela_p)
22172         value = 0;
22173
22174     case BFD_RELOC_ARM_LITERAL:
22175       sign = value > 0;
22176
22177       if (value < 0)
22178         value = - value;
22179
22180       if (validate_offset_imm (value, 0) == FAIL)
22181         {
22182           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
22183             as_bad_where (fixP->fx_file, fixP->fx_line,
22184                           _("invalid literal constant: pool needs to be closer"));
22185           else
22186             as_bad_where (fixP->fx_file, fixP->fx_line,
22187                           _("bad immediate value for offset (%ld)"),
22188                           (long) value);
22189           break;
22190         }
22191
22192       newval = md_chars_to_number (buf, INSN_SIZE);
22193       if (value == 0)
22194         newval &= 0xfffff000;
22195       else
22196         {
22197           newval &= 0xff7ff000;
22198           newval |= value | (sign ? INDEX_UP : 0);
22199         }
22200       md_number_to_chars (buf, newval, INSN_SIZE);
22201       break;
22202
22203     case BFD_RELOC_ARM_OFFSET_IMM8:
22204     case BFD_RELOC_ARM_HWLITERAL:
22205       sign = value > 0;
22206
22207       if (value < 0)
22208         value = - value;
22209
22210       if (validate_offset_imm (value, 1) == FAIL)
22211         {
22212           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
22213             as_bad_where (fixP->fx_file, fixP->fx_line,
22214                           _("invalid literal constant: pool needs to be closer"));
22215           else
22216             as_bad_where (fixP->fx_file, fixP->fx_line,
22217                           _("bad immediate value for 8-bit offset (%ld)"),
22218                           (long) value);
22219           break;
22220         }
22221
22222       newval = md_chars_to_number (buf, INSN_SIZE);
22223       if (value == 0)
22224         newval &= 0xfffff0f0;
22225       else
22226         {
22227           newval &= 0xff7ff0f0;
22228           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
22229         }
22230       md_number_to_chars (buf, newval, INSN_SIZE);
22231       break;
22232
22233     case BFD_RELOC_ARM_T32_OFFSET_U8:
22234       if (value < 0 || value > 1020 || value % 4 != 0)
22235         as_bad_where (fixP->fx_file, fixP->fx_line,
22236                       _("bad immediate value for offset (%ld)"), (long) value);
22237       value /= 4;
22238
22239       newval = md_chars_to_number (buf+2, THUMB_SIZE);
22240       newval |= value;
22241       md_number_to_chars (buf+2, newval, THUMB_SIZE);
22242       break;
22243
22244     case BFD_RELOC_ARM_T32_OFFSET_IMM:
22245       /* This is a complicated relocation used for all varieties of Thumb32
22246          load/store instruction with immediate offset:
22247
22248          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
22249                                                    *4, optional writeback(W)
22250                                                    (doubleword load/store)
22251
22252          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
22253          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
22254          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
22255          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
22256          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
22257
22258          Uppercase letters indicate bits that are already encoded at
22259          this point.  Lowercase letters are our problem.  For the
22260          second block of instructions, the secondary opcode nybble
22261          (bits 8..11) is present, and bit 23 is zero, even if this is
22262          a PC-relative operation.  */
22263       newval = md_chars_to_number (buf, THUMB_SIZE);
22264       newval <<= 16;
22265       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
22266
22267       if ((newval & 0xf0000000) == 0xe0000000)
22268         {
22269           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
22270           if (value >= 0)
22271             newval |= (1 << 23);
22272           else
22273             value = -value;
22274           if (value % 4 != 0)
22275             {
22276               as_bad_where (fixP->fx_file, fixP->fx_line,
22277                             _("offset not a multiple of 4"));
22278               break;
22279             }
22280           value /= 4;
22281           if (value > 0xff)
22282             {
22283               as_bad_where (fixP->fx_file, fixP->fx_line,
22284                             _("offset out of range"));
22285               break;
22286             }
22287           newval &= ~0xff;
22288         }
22289       else if ((newval & 0x000f0000) == 0x000f0000)
22290         {
22291           /* PC-relative, 12-bit offset.  */
22292           if (value >= 0)
22293             newval |= (1 << 23);
22294           else
22295             value = -value;
22296           if (value > 0xfff)
22297             {
22298               as_bad_where (fixP->fx_file, fixP->fx_line,
22299                             _("offset out of range"));
22300               break;
22301             }
22302           newval &= ~0xfff;
22303         }
22304       else if ((newval & 0x00000100) == 0x00000100)
22305         {
22306           /* Writeback: 8-bit, +/- offset.  */
22307           if (value >= 0)
22308             newval |= (1 << 9);
22309           else
22310             value = -value;
22311           if (value > 0xff)
22312             {
22313               as_bad_where (fixP->fx_file, fixP->fx_line,
22314                             _("offset out of range"));
22315               break;
22316             }
22317           newval &= ~0xff;
22318         }
22319       else if ((newval & 0x00000f00) == 0x00000e00)
22320         {
22321           /* T-instruction: positive 8-bit offset.  */
22322           if (value < 0 || value > 0xff)
22323             {
22324               as_bad_where (fixP->fx_file, fixP->fx_line,
22325                             _("offset out of range"));
22326               break;
22327             }
22328           newval &= ~0xff;
22329           newval |= value;
22330         }
22331       else
22332         {
22333           /* Positive 12-bit or negative 8-bit offset.  */
22334           int limit;
22335           if (value >= 0)
22336             {
22337               newval |= (1 << 23);
22338               limit = 0xfff;
22339             }
22340           else
22341             {
22342               value = -value;
22343               limit = 0xff;
22344             }
22345           if (value > limit)
22346             {
22347               as_bad_where (fixP->fx_file, fixP->fx_line,
22348                             _("offset out of range"));
22349               break;
22350             }
22351           newval &= ~limit;
22352         }
22353
22354       newval |= value;
22355       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
22356       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
22357       break;
22358
22359     case BFD_RELOC_ARM_SHIFT_IMM:
22360       newval = md_chars_to_number (buf, INSN_SIZE);
22361       if (((unsigned long) value) > 32
22362           || (value == 32
22363               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
22364         {
22365           as_bad_where (fixP->fx_file, fixP->fx_line,
22366                         _("shift expression is too large"));
22367           break;
22368         }
22369
22370       if (value == 0)
22371         /* Shifts of zero must be done as lsl.  */
22372         newval &= ~0x60;
22373       else if (value == 32)
22374         value = 0;
22375       newval &= 0xfffff07f;
22376       newval |= (value & 0x1f) << 7;
22377       md_number_to_chars (buf, newval, INSN_SIZE);
22378       break;
22379
22380     case BFD_RELOC_ARM_T32_IMMEDIATE:
22381     case BFD_RELOC_ARM_T32_ADD_IMM:
22382     case BFD_RELOC_ARM_T32_IMM12:
22383     case BFD_RELOC_ARM_T32_ADD_PC12:
22384       /* We claim that this fixup has been processed here,
22385          even if in fact we generate an error because we do
22386          not have a reloc for it, so tc_gen_reloc will reject it.  */
22387       fixP->fx_done = 1;
22388
22389       if (fixP->fx_addsy
22390           && ! S_IS_DEFINED (fixP->fx_addsy))
22391         {
22392           as_bad_where (fixP->fx_file, fixP->fx_line,
22393                         _("undefined symbol %s used as an immediate value"),
22394                         S_GET_NAME (fixP->fx_addsy));
22395           break;
22396         }
22397
22398       newval = md_chars_to_number (buf, THUMB_SIZE);
22399       newval <<= 16;
22400       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
22401
22402       newimm = FAIL;
22403       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
22404           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
22405         {
22406           newimm = encode_thumb32_immediate (value);
22407           if (newimm == (unsigned int) FAIL)
22408             newimm = thumb32_negate_data_op (&newval, value);
22409         }
22410       if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE
22411           && newimm == (unsigned int) FAIL)
22412         {
22413           /* Turn add/sum into addw/subw.  */
22414           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
22415             newval = (newval & 0xfeffffff) | 0x02000000;
22416           /* No flat 12-bit imm encoding for addsw/subsw.  */
22417           if ((newval & 0x00100000) == 0)
22418             {
22419               /* 12 bit immediate for addw/subw.  */
22420               if (value < 0)
22421                 {
22422                   value = -value;
22423                   newval ^= 0x00a00000;
22424                 }
22425               if (value > 0xfff)
22426                 newimm = (unsigned int) FAIL;
22427               else
22428                 newimm = value;
22429             }
22430         }
22431
22432       if (newimm == (unsigned int)FAIL)
22433         {
22434           as_bad_where (fixP->fx_file, fixP->fx_line,
22435                         _("invalid constant (%lx) after fixup"),
22436                         (unsigned long) value);
22437           break;
22438         }
22439
22440       newval |= (newimm & 0x800) << 15;
22441       newval |= (newimm & 0x700) << 4;
22442       newval |= (newimm & 0x0ff);
22443
22444       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
22445       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
22446       break;
22447
22448     case BFD_RELOC_ARM_SMC:
22449       if (((unsigned long) value) > 0xffff)
22450         as_bad_where (fixP->fx_file, fixP->fx_line,
22451                       _("invalid smc expression"));
22452       newval = md_chars_to_number (buf, INSN_SIZE);
22453       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
22454       md_number_to_chars (buf, newval, INSN_SIZE);
22455       break;
22456
22457     case BFD_RELOC_ARM_HVC:
22458       if (((unsigned long) value) > 0xffff)
22459         as_bad_where (fixP->fx_file, fixP->fx_line,
22460                       _("invalid hvc expression"));
22461       newval = md_chars_to_number (buf, INSN_SIZE);
22462       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
22463       md_number_to_chars (buf, newval, INSN_SIZE);
22464       break;
22465
22466     case BFD_RELOC_ARM_SWI:
22467       if (fixP->tc_fix_data != 0)
22468         {
22469           if (((unsigned long) value) > 0xff)
22470             as_bad_where (fixP->fx_file, fixP->fx_line,
22471                           _("invalid swi expression"));
22472           newval = md_chars_to_number (buf, THUMB_SIZE);
22473           newval |= value;
22474           md_number_to_chars (buf, newval, THUMB_SIZE);
22475         }
22476       else
22477         {
22478           if (((unsigned long) value) > 0x00ffffff)
22479             as_bad_where (fixP->fx_file, fixP->fx_line,
22480                           _("invalid swi expression"));
22481           newval = md_chars_to_number (buf, INSN_SIZE);
22482           newval |= value;
22483           md_number_to_chars (buf, newval, INSN_SIZE);
22484         }
22485       break;
22486
22487     case BFD_RELOC_ARM_MULTI:
22488       if (((unsigned long) value) > 0xffff)
22489         as_bad_where (fixP->fx_file, fixP->fx_line,
22490                       _("invalid expression in load/store multiple"));
22491       newval = value | md_chars_to_number (buf, INSN_SIZE);
22492       md_number_to_chars (buf, newval, INSN_SIZE);
22493       break;
22494
22495 #ifdef OBJ_ELF
22496     case BFD_RELOC_ARM_PCREL_CALL:
22497
22498       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
22499           && fixP->fx_addsy
22500           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22501           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22502           && THUMB_IS_FUNC (fixP->fx_addsy))
22503         /* Flip the bl to blx. This is a simple flip
22504            bit here because we generate PCREL_CALL for
22505            unconditional bls.  */
22506         {
22507           newval = md_chars_to_number (buf, INSN_SIZE);
22508           newval = newval | 0x10000000;
22509           md_number_to_chars (buf, newval, INSN_SIZE);
22510           temp = 1;
22511           fixP->fx_done = 1;
22512         }
22513       else
22514         temp = 3;
22515       goto arm_branch_common;
22516
22517     case BFD_RELOC_ARM_PCREL_JUMP:
22518       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
22519           && fixP->fx_addsy
22520           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22521           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22522           && THUMB_IS_FUNC (fixP->fx_addsy))
22523         {
22524           /* This would map to a bl<cond>, b<cond>,
22525              b<always> to a Thumb function. We
22526              need to force a relocation for this particular
22527              case.  */
22528           newval = md_chars_to_number (buf, INSN_SIZE);
22529           fixP->fx_done = 0;
22530         }
22531
22532     case BFD_RELOC_ARM_PLT32:
22533 #endif
22534     case BFD_RELOC_ARM_PCREL_BRANCH:
22535       temp = 3;
22536       goto arm_branch_common;
22537
22538     case BFD_RELOC_ARM_PCREL_BLX:
22539
22540       temp = 1;
22541       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
22542           && fixP->fx_addsy
22543           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22544           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22545           && ARM_IS_FUNC (fixP->fx_addsy))
22546         {
22547           /* Flip the blx to a bl and warn.  */
22548           const char *name = S_GET_NAME (fixP->fx_addsy);
22549           newval = 0xeb000000;
22550           as_warn_where (fixP->fx_file, fixP->fx_line,
22551                          _("blx to '%s' an ARM ISA state function changed to bl"),
22552                           name);
22553           md_number_to_chars (buf, newval, INSN_SIZE);
22554           temp = 3;
22555           fixP->fx_done = 1;
22556         }
22557
22558 #ifdef OBJ_ELF
22559        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
22560          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
22561 #endif
22562
22563     arm_branch_common:
22564       /* We are going to store value (shifted right by two) in the
22565          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
22566          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
22567          also be be clear.  */
22568       if (value & temp)
22569         as_bad_where (fixP->fx_file, fixP->fx_line,
22570                       _("misaligned branch destination"));
22571       if ((value & (offsetT)0xfe000000) != (offsetT)0
22572           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
22573         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22574
22575       if (fixP->fx_done || !seg->use_rela_p)
22576         {
22577           newval = md_chars_to_number (buf, INSN_SIZE);
22578           newval |= (value >> 2) & 0x00ffffff;
22579           /* Set the H bit on BLX instructions.  */
22580           if (temp == 1)
22581             {
22582               if (value & 2)
22583                 newval |= 0x01000000;
22584               else
22585                 newval &= ~0x01000000;
22586             }
22587           md_number_to_chars (buf, newval, INSN_SIZE);
22588         }
22589       break;
22590
22591     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
22592       /* CBZ can only branch forward.  */
22593
22594       /* Attempts to use CBZ to branch to the next instruction
22595          (which, strictly speaking, are prohibited) will be turned into
22596          no-ops.
22597
22598          FIXME: It may be better to remove the instruction completely and
22599          perform relaxation.  */
22600       if (value == -2)
22601         {
22602           newval = md_chars_to_number (buf, THUMB_SIZE);
22603           newval = 0xbf00; /* NOP encoding T1 */
22604           md_number_to_chars (buf, newval, THUMB_SIZE);
22605         }
22606       else
22607         {
22608           if (value & ~0x7e)
22609             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22610
22611           if (fixP->fx_done || !seg->use_rela_p)
22612             {
22613               newval = md_chars_to_number (buf, THUMB_SIZE);
22614               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
22615               md_number_to_chars (buf, newval, THUMB_SIZE);
22616             }
22617         }
22618       break;
22619
22620     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
22621       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
22622         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22623
22624       if (fixP->fx_done || !seg->use_rela_p)
22625         {
22626           newval = md_chars_to_number (buf, THUMB_SIZE);
22627           newval |= (value & 0x1ff) >> 1;
22628           md_number_to_chars (buf, newval, THUMB_SIZE);
22629         }
22630       break;
22631
22632     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
22633       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
22634         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22635
22636       if (fixP->fx_done || !seg->use_rela_p)
22637         {
22638           newval = md_chars_to_number (buf, THUMB_SIZE);
22639           newval |= (value & 0xfff) >> 1;
22640           md_number_to_chars (buf, newval, THUMB_SIZE);
22641         }
22642       break;
22643
22644     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22645       if (fixP->fx_addsy
22646           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22647           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22648           && ARM_IS_FUNC (fixP->fx_addsy)
22649           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22650         {
22651           /* Force a relocation for a branch 20 bits wide.  */
22652           fixP->fx_done = 0;
22653         }
22654       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
22655         as_bad_where (fixP->fx_file, fixP->fx_line,
22656                       _("conditional branch out of range"));
22657
22658       if (fixP->fx_done || !seg->use_rela_p)
22659         {
22660           offsetT newval2;
22661           addressT S, J1, J2, lo, hi;
22662
22663           S  = (value & 0x00100000) >> 20;
22664           J2 = (value & 0x00080000) >> 19;
22665           J1 = (value & 0x00040000) >> 18;
22666           hi = (value & 0x0003f000) >> 12;
22667           lo = (value & 0x00000ffe) >> 1;
22668
22669           newval   = md_chars_to_number (buf, THUMB_SIZE);
22670           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22671           newval  |= (S << 10) | hi;
22672           newval2 |= (J1 << 13) | (J2 << 11) | lo;
22673           md_number_to_chars (buf, newval, THUMB_SIZE);
22674           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
22675         }
22676       break;
22677
22678     case BFD_RELOC_THUMB_PCREL_BLX:
22679       /* If there is a blx from a thumb state function to
22680          another thumb function flip this to a bl and warn
22681          about it.  */
22682
22683       if (fixP->fx_addsy
22684           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22685           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22686           && THUMB_IS_FUNC (fixP->fx_addsy))
22687         {
22688           const char *name = S_GET_NAME (fixP->fx_addsy);
22689           as_warn_where (fixP->fx_file, fixP->fx_line,
22690                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
22691                          name);
22692           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22693           newval = newval | 0x1000;
22694           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
22695           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
22696           fixP->fx_done = 1;
22697         }
22698
22699
22700       goto thumb_bl_common;
22701
22702     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22703       /* A bl from Thumb state ISA to an internal ARM state function
22704          is converted to a blx.  */
22705       if (fixP->fx_addsy
22706           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22707           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22708           && ARM_IS_FUNC (fixP->fx_addsy)
22709           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22710         {
22711           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22712           newval = newval & ~0x1000;
22713           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
22714           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
22715           fixP->fx_done = 1;
22716         }
22717
22718     thumb_bl_common:
22719
22720       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
22721         /* For a BLX instruction, make sure that the relocation is rounded up
22722            to a word boundary.  This follows the semantics of the instruction
22723            which specifies that bit 1 of the target address will come from bit
22724            1 of the base address.  */
22725         value = (value + 3) & ~ 3;
22726
22727 #ifdef OBJ_ELF
22728        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
22729            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
22730          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
22731 #endif
22732
22733       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
22734         {
22735           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2)))
22736             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22737           else if ((value & ~0x1ffffff)
22738                    && ((value & ~0x1ffffff) != ~0x1ffffff))
22739             as_bad_where (fixP->fx_file, fixP->fx_line,
22740                           _("Thumb2 branch out of range"));
22741         }
22742
22743       if (fixP->fx_done || !seg->use_rela_p)
22744         encode_thumb2_b_bl_offset (buf, value);
22745
22746       break;
22747
22748     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22749       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
22750         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22751
22752       if (fixP->fx_done || !seg->use_rela_p)
22753           encode_thumb2_b_bl_offset (buf, value);
22754
22755       break;
22756
22757     case BFD_RELOC_8:
22758       if (fixP->fx_done || !seg->use_rela_p)
22759         *buf = value;
22760       break;
22761
22762     case BFD_RELOC_16:
22763       if (fixP->fx_done || !seg->use_rela_p)
22764         md_number_to_chars (buf, value, 2);
22765       break;
22766
22767 #ifdef OBJ_ELF
22768     case BFD_RELOC_ARM_TLS_CALL:
22769     case BFD_RELOC_ARM_THM_TLS_CALL:
22770     case BFD_RELOC_ARM_TLS_DESCSEQ:
22771     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
22772     case BFD_RELOC_ARM_TLS_GOTDESC:
22773     case BFD_RELOC_ARM_TLS_GD32:
22774     case BFD_RELOC_ARM_TLS_LE32:
22775     case BFD_RELOC_ARM_TLS_IE32:
22776     case BFD_RELOC_ARM_TLS_LDM32:
22777     case BFD_RELOC_ARM_TLS_LDO32:
22778       S_SET_THREAD_LOCAL (fixP->fx_addsy);
22779       break;
22780
22781     case BFD_RELOC_ARM_GOT32:
22782     case BFD_RELOC_ARM_GOTOFF:
22783       break;
22784
22785     case BFD_RELOC_ARM_GOT_PREL:
22786       if (fixP->fx_done || !seg->use_rela_p)
22787         md_number_to_chars (buf, value, 4);
22788       break;
22789
22790     case BFD_RELOC_ARM_TARGET2:
22791       /* TARGET2 is not partial-inplace, so we need to write the
22792          addend here for REL targets, because it won't be written out
22793          during reloc processing later.  */
22794       if (fixP->fx_done || !seg->use_rela_p)
22795         md_number_to_chars (buf, fixP->fx_offset, 4);
22796       break;
22797 #endif
22798
22799     case BFD_RELOC_RVA:
22800     case BFD_RELOC_32:
22801     case BFD_RELOC_ARM_TARGET1:
22802     case BFD_RELOC_ARM_ROSEGREL32:
22803     case BFD_RELOC_ARM_SBREL32:
22804     case BFD_RELOC_32_PCREL:
22805 #ifdef TE_PE
22806     case BFD_RELOC_32_SECREL:
22807 #endif
22808       if (fixP->fx_done || !seg->use_rela_p)
22809 #ifdef TE_WINCE
22810         /* For WinCE we only do this for pcrel fixups.  */
22811         if (fixP->fx_done || fixP->fx_pcrel)
22812 #endif
22813           md_number_to_chars (buf, value, 4);
22814       break;
22815
22816 #ifdef OBJ_ELF
22817     case BFD_RELOC_ARM_PREL31:
22818       if (fixP->fx_done || !seg->use_rela_p)
22819         {
22820           newval = md_chars_to_number (buf, 4) & 0x80000000;
22821           if ((value ^ (value >> 1)) & 0x40000000)
22822             {
22823               as_bad_where (fixP->fx_file, fixP->fx_line,
22824                             _("rel31 relocation overflow"));
22825             }
22826           newval |= value & 0x7fffffff;
22827           md_number_to_chars (buf, newval, 4);
22828         }
22829       break;
22830 #endif
22831
22832     case BFD_RELOC_ARM_CP_OFF_IMM:
22833     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
22834       if (value < -1023 || value > 1023 || (value & 3))
22835         as_bad_where (fixP->fx_file, fixP->fx_line,
22836                       _("co-processor offset out of range"));
22837     cp_off_common:
22838       sign = value > 0;
22839       if (value < 0)
22840         value = -value;
22841       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
22842           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
22843         newval = md_chars_to_number (buf, INSN_SIZE);
22844       else
22845         newval = get_thumb32_insn (buf);
22846       if (value == 0)
22847         newval &= 0xffffff00;
22848       else
22849         {
22850           newval &= 0xff7fff00;
22851           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
22852         }
22853       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
22854           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
22855         md_number_to_chars (buf, newval, INSN_SIZE);
22856       else
22857         put_thumb32_insn (buf, newval);
22858       break;
22859
22860     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
22861     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
22862       if (value < -255 || value > 255)
22863         as_bad_where (fixP->fx_file, fixP->fx_line,
22864                       _("co-processor offset out of range"));
22865       value *= 4;
22866       goto cp_off_common;
22867
22868     case BFD_RELOC_ARM_THUMB_OFFSET:
22869       newval = md_chars_to_number (buf, THUMB_SIZE);
22870       /* Exactly what ranges, and where the offset is inserted depends
22871          on the type of instruction, we can establish this from the
22872          top 4 bits.  */
22873       switch (newval >> 12)
22874         {
22875         case 4: /* PC load.  */
22876           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
22877              forced to zero for these loads; md_pcrel_from has already
22878              compensated for this.  */
22879           if (value & 3)
22880             as_bad_where (fixP->fx_file, fixP->fx_line,
22881                           _("invalid offset, target not word aligned (0x%08lX)"),
22882                           (((unsigned long) fixP->fx_frag->fr_address
22883                             + (unsigned long) fixP->fx_where) & ~3)
22884                           + (unsigned long) value);
22885
22886           if (value & ~0x3fc)
22887             as_bad_where (fixP->fx_file, fixP->fx_line,
22888                           _("invalid offset, value too big (0x%08lX)"),
22889                           (long) value);
22890
22891           newval |= value >> 2;
22892           break;
22893
22894         case 9: /* SP load/store.  */
22895           if (value & ~0x3fc)
22896             as_bad_where (fixP->fx_file, fixP->fx_line,
22897                           _("invalid offset, value too big (0x%08lX)"),
22898                           (long) value);
22899           newval |= value >> 2;
22900           break;
22901
22902         case 6: /* Word load/store.  */
22903           if (value & ~0x7c)
22904             as_bad_where (fixP->fx_file, fixP->fx_line,
22905                           _("invalid offset, value too big (0x%08lX)"),
22906                           (long) value);
22907           newval |= value << 4; /* 6 - 2.  */
22908           break;
22909
22910         case 7: /* Byte load/store.  */
22911           if (value & ~0x1f)
22912             as_bad_where (fixP->fx_file, fixP->fx_line,
22913                           _("invalid offset, value too big (0x%08lX)"),
22914                           (long) value);
22915           newval |= value << 6;
22916           break;
22917
22918         case 8: /* Halfword load/store.  */
22919           if (value & ~0x3e)
22920             as_bad_where (fixP->fx_file, fixP->fx_line,
22921                           _("invalid offset, value too big (0x%08lX)"),
22922                           (long) value);
22923           newval |= value << 5; /* 6 - 1.  */
22924           break;
22925
22926         default:
22927           as_bad_where (fixP->fx_file, fixP->fx_line,
22928                         "Unable to process relocation for thumb opcode: %lx",
22929                         (unsigned long) newval);
22930           break;
22931         }
22932       md_number_to_chars (buf, newval, THUMB_SIZE);
22933       break;
22934
22935     case BFD_RELOC_ARM_THUMB_ADD:
22936       /* This is a complicated relocation, since we use it for all of
22937          the following immediate relocations:
22938
22939             3bit ADD/SUB
22940             8bit ADD/SUB
22941             9bit ADD/SUB SP word-aligned
22942            10bit ADD PC/SP word-aligned
22943
22944          The type of instruction being processed is encoded in the
22945          instruction field:
22946
22947            0x8000  SUB
22948            0x00F0  Rd
22949            0x000F  Rs
22950       */
22951       newval = md_chars_to_number (buf, THUMB_SIZE);
22952       {
22953         int rd = (newval >> 4) & 0xf;
22954         int rs = newval & 0xf;
22955         int subtract = !!(newval & 0x8000);
22956
22957         /* Check for HI regs, only very restricted cases allowed:
22958            Adjusting SP, and using PC or SP to get an address.  */
22959         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
22960             || (rs > 7 && rs != REG_SP && rs != REG_PC))
22961           as_bad_where (fixP->fx_file, fixP->fx_line,
22962                         _("invalid Hi register with immediate"));
22963
22964         /* If value is negative, choose the opposite instruction.  */
22965         if (value < 0)
22966           {
22967             value = -value;
22968             subtract = !subtract;
22969             if (value < 0)
22970               as_bad_where (fixP->fx_file, fixP->fx_line,
22971                             _("immediate value out of range"));
22972           }
22973
22974         if (rd == REG_SP)
22975           {
22976             if (value & ~0x1fc)
22977               as_bad_where (fixP->fx_file, fixP->fx_line,
22978                             _("invalid immediate for stack address calculation"));
22979             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
22980             newval |= value >> 2;
22981           }
22982         else if (rs == REG_PC || rs == REG_SP)
22983           {
22984             if (subtract || value & ~0x3fc)
22985               as_bad_where (fixP->fx_file, fixP->fx_line,
22986                             _("invalid immediate for address calculation (value = 0x%08lX)"),
22987                             (unsigned long) value);
22988             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
22989             newval |= rd << 8;
22990             newval |= value >> 2;
22991           }
22992         else if (rs == rd)
22993           {
22994             if (value & ~0xff)
22995               as_bad_where (fixP->fx_file, fixP->fx_line,
22996                             _("immediate value out of range"));
22997             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
22998             newval |= (rd << 8) | value;
22999           }
23000         else
23001           {
23002             if (value & ~0x7)
23003               as_bad_where (fixP->fx_file, fixP->fx_line,
23004                             _("immediate value out of range"));
23005             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
23006             newval |= rd | (rs << 3) | (value << 6);
23007           }
23008       }
23009       md_number_to_chars (buf, newval, THUMB_SIZE);
23010       break;
23011
23012     case BFD_RELOC_ARM_THUMB_IMM:
23013       newval = md_chars_to_number (buf, THUMB_SIZE);
23014       if (value < 0 || value > 255)
23015         as_bad_where (fixP->fx_file, fixP->fx_line,
23016                       _("invalid immediate: %ld is out of range"),
23017                       (long) value);
23018       newval |= value;
23019       md_number_to_chars (buf, newval, THUMB_SIZE);
23020       break;
23021
23022     case BFD_RELOC_ARM_THUMB_SHIFT:
23023       /* 5bit shift value (0..32).  LSL cannot take 32.  */
23024       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
23025       temp = newval & 0xf800;
23026       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
23027         as_bad_where (fixP->fx_file, fixP->fx_line,
23028                       _("invalid shift value: %ld"), (long) value);
23029       /* Shifts of zero must be encoded as LSL.  */
23030       if (value == 0)
23031         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
23032       /* Shifts of 32 are encoded as zero.  */
23033       else if (value == 32)
23034         value = 0;
23035       newval |= value << 6;
23036       md_number_to_chars (buf, newval, THUMB_SIZE);
23037       break;
23038
23039     case BFD_RELOC_VTABLE_INHERIT:
23040     case BFD_RELOC_VTABLE_ENTRY:
23041       fixP->fx_done = 0;
23042       return;
23043
23044     case BFD_RELOC_ARM_MOVW:
23045     case BFD_RELOC_ARM_MOVT:
23046     case BFD_RELOC_ARM_THUMB_MOVW:
23047     case BFD_RELOC_ARM_THUMB_MOVT:
23048       if (fixP->fx_done || !seg->use_rela_p)
23049         {
23050           /* REL format relocations are limited to a 16-bit addend.  */
23051           if (!fixP->fx_done)
23052             {
23053               if (value < -0x8000 || value > 0x7fff)
23054                   as_bad_where (fixP->fx_file, fixP->fx_line,
23055                                 _("offset out of range"));
23056             }
23057           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
23058                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
23059             {
23060               value >>= 16;
23061             }
23062
23063           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
23064               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
23065             {
23066               newval = get_thumb32_insn (buf);
23067               newval &= 0xfbf08f00;
23068               newval |= (value & 0xf000) << 4;
23069               newval |= (value & 0x0800) << 15;
23070               newval |= (value & 0x0700) << 4;
23071               newval |= (value & 0x00ff);
23072               put_thumb32_insn (buf, newval);
23073             }
23074           else
23075             {
23076               newval = md_chars_to_number (buf, 4);
23077               newval &= 0xfff0f000;
23078               newval |= value & 0x0fff;
23079               newval |= (value & 0xf000) << 4;
23080               md_number_to_chars (buf, newval, 4);
23081             }
23082         }
23083       return;
23084
23085    case BFD_RELOC_ARM_ALU_PC_G0_NC:
23086    case BFD_RELOC_ARM_ALU_PC_G0:
23087    case BFD_RELOC_ARM_ALU_PC_G1_NC:
23088    case BFD_RELOC_ARM_ALU_PC_G1:
23089    case BFD_RELOC_ARM_ALU_PC_G2:
23090    case BFD_RELOC_ARM_ALU_SB_G0_NC:
23091    case BFD_RELOC_ARM_ALU_SB_G0:
23092    case BFD_RELOC_ARM_ALU_SB_G1_NC:
23093    case BFD_RELOC_ARM_ALU_SB_G1:
23094    case BFD_RELOC_ARM_ALU_SB_G2:
23095      gas_assert (!fixP->fx_done);
23096      if (!seg->use_rela_p)
23097        {
23098          bfd_vma insn;
23099          bfd_vma encoded_addend;
23100          bfd_vma addend_abs = abs (value);
23101
23102          /* Check that the absolute value of the addend can be
23103             expressed as an 8-bit constant plus a rotation.  */
23104          encoded_addend = encode_arm_immediate (addend_abs);
23105          if (encoded_addend == (unsigned int) FAIL)
23106            as_bad_where (fixP->fx_file, fixP->fx_line,
23107                          _("the offset 0x%08lX is not representable"),
23108                          (unsigned long) addend_abs);
23109
23110          /* Extract the instruction.  */
23111          insn = md_chars_to_number (buf, INSN_SIZE);
23112
23113          /* If the addend is positive, use an ADD instruction.
23114             Otherwise use a SUB.  Take care not to destroy the S bit.  */
23115          insn &= 0xff1fffff;
23116          if (value < 0)
23117            insn |= 1 << 22;
23118          else
23119            insn |= 1 << 23;
23120
23121          /* Place the encoded addend into the first 12 bits of the
23122             instruction.  */
23123          insn &= 0xfffff000;
23124          insn |= encoded_addend;
23125
23126          /* Update the instruction.  */
23127          md_number_to_chars (buf, insn, INSN_SIZE);
23128        }
23129      break;
23130
23131     case BFD_RELOC_ARM_LDR_PC_G0:
23132     case BFD_RELOC_ARM_LDR_PC_G1:
23133     case BFD_RELOC_ARM_LDR_PC_G2:
23134     case BFD_RELOC_ARM_LDR_SB_G0:
23135     case BFD_RELOC_ARM_LDR_SB_G1:
23136     case BFD_RELOC_ARM_LDR_SB_G2:
23137       gas_assert (!fixP->fx_done);
23138       if (!seg->use_rela_p)
23139         {
23140           bfd_vma insn;
23141           bfd_vma addend_abs = abs (value);
23142
23143           /* Check that the absolute value of the addend can be
23144              encoded in 12 bits.  */
23145           if (addend_abs >= 0x1000)
23146             as_bad_where (fixP->fx_file, fixP->fx_line,
23147                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
23148                           (unsigned long) addend_abs);
23149
23150           /* Extract the instruction.  */
23151           insn = md_chars_to_number (buf, INSN_SIZE);
23152
23153           /* If the addend is negative, clear bit 23 of the instruction.
23154              Otherwise set it.  */
23155           if (value < 0)
23156             insn &= ~(1 << 23);
23157           else
23158             insn |= 1 << 23;
23159
23160           /* Place the absolute value of the addend into the first 12 bits
23161              of the instruction.  */
23162           insn &= 0xfffff000;
23163           insn |= addend_abs;
23164
23165           /* Update the instruction.  */
23166           md_number_to_chars (buf, insn, INSN_SIZE);
23167         }
23168       break;
23169
23170     case BFD_RELOC_ARM_LDRS_PC_G0:
23171     case BFD_RELOC_ARM_LDRS_PC_G1:
23172     case BFD_RELOC_ARM_LDRS_PC_G2:
23173     case BFD_RELOC_ARM_LDRS_SB_G0:
23174     case BFD_RELOC_ARM_LDRS_SB_G1:
23175     case BFD_RELOC_ARM_LDRS_SB_G2:
23176       gas_assert (!fixP->fx_done);
23177       if (!seg->use_rela_p)
23178         {
23179           bfd_vma insn;
23180           bfd_vma addend_abs = abs (value);
23181
23182           /* Check that the absolute value of the addend can be
23183              encoded in 8 bits.  */
23184           if (addend_abs >= 0x100)
23185             as_bad_where (fixP->fx_file, fixP->fx_line,
23186                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
23187                           (unsigned long) addend_abs);
23188
23189           /* Extract the instruction.  */
23190           insn = md_chars_to_number (buf, INSN_SIZE);
23191
23192           /* If the addend is negative, clear bit 23 of the instruction.
23193              Otherwise set it.  */
23194           if (value < 0)
23195             insn &= ~(1 << 23);
23196           else
23197             insn |= 1 << 23;
23198
23199           /* Place the first four bits of the absolute value of the addend
23200              into the first 4 bits of the instruction, and the remaining
23201              four into bits 8 .. 11.  */
23202           insn &= 0xfffff0f0;
23203           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
23204
23205           /* Update the instruction.  */
23206           md_number_to_chars (buf, insn, INSN_SIZE);
23207         }
23208       break;
23209
23210     case BFD_RELOC_ARM_LDC_PC_G0:
23211     case BFD_RELOC_ARM_LDC_PC_G1:
23212     case BFD_RELOC_ARM_LDC_PC_G2:
23213     case BFD_RELOC_ARM_LDC_SB_G0:
23214     case BFD_RELOC_ARM_LDC_SB_G1:
23215     case BFD_RELOC_ARM_LDC_SB_G2:
23216       gas_assert (!fixP->fx_done);
23217       if (!seg->use_rela_p)
23218         {
23219           bfd_vma insn;
23220           bfd_vma addend_abs = abs (value);
23221
23222           /* Check that the absolute value of the addend is a multiple of
23223              four and, when divided by four, fits in 8 bits.  */
23224           if (addend_abs & 0x3)
23225             as_bad_where (fixP->fx_file, fixP->fx_line,
23226                           _("bad offset 0x%08lX (must be word-aligned)"),
23227                           (unsigned long) addend_abs);
23228
23229           if ((addend_abs >> 2) > 0xff)
23230             as_bad_where (fixP->fx_file, fixP->fx_line,
23231                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
23232                           (unsigned long) addend_abs);
23233
23234           /* Extract the instruction.  */
23235           insn = md_chars_to_number (buf, INSN_SIZE);
23236
23237           /* If the addend is negative, clear bit 23 of the instruction.
23238              Otherwise set it.  */
23239           if (value < 0)
23240             insn &= ~(1 << 23);
23241           else
23242             insn |= 1 << 23;
23243
23244           /* Place the addend (divided by four) into the first eight
23245              bits of the instruction.  */
23246           insn &= 0xfffffff0;
23247           insn |= addend_abs >> 2;
23248
23249           /* Update the instruction.  */
23250           md_number_to_chars (buf, insn, INSN_SIZE);
23251         }
23252       break;
23253
23254     case BFD_RELOC_ARM_V4BX:
23255       /* This will need to go in the object file.  */
23256       fixP->fx_done = 0;
23257       break;
23258
23259     case BFD_RELOC_UNUSED:
23260     default:
23261       as_bad_where (fixP->fx_file, fixP->fx_line,
23262                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
23263     }
23264 }
23265
23266 /* Translate internal representation of relocation info to BFD target
23267    format.  */
23268
23269 arelent *
23270 tc_gen_reloc (asection *section, fixS *fixp)
23271 {
23272   arelent * reloc;
23273   bfd_reloc_code_real_type code;
23274
23275   reloc = (arelent *) xmalloc (sizeof (arelent));
23276
23277   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
23278   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
23279   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
23280
23281   if (fixp->fx_pcrel)
23282     {
23283       if (section->use_rela_p)
23284         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
23285       else
23286         fixp->fx_offset = reloc->address;
23287     }
23288   reloc->addend = fixp->fx_offset;
23289
23290   switch (fixp->fx_r_type)
23291     {
23292     case BFD_RELOC_8:
23293       if (fixp->fx_pcrel)
23294         {
23295           code = BFD_RELOC_8_PCREL;
23296           break;
23297         }
23298
23299     case BFD_RELOC_16:
23300       if (fixp->fx_pcrel)
23301         {
23302           code = BFD_RELOC_16_PCREL;
23303           break;
23304         }
23305
23306     case BFD_RELOC_32:
23307       if (fixp->fx_pcrel)
23308         {
23309           code = BFD_RELOC_32_PCREL;
23310           break;
23311         }
23312
23313     case BFD_RELOC_ARM_MOVW:
23314       if (fixp->fx_pcrel)
23315         {
23316           code = BFD_RELOC_ARM_MOVW_PCREL;
23317           break;
23318         }
23319
23320     case BFD_RELOC_ARM_MOVT:
23321       if (fixp->fx_pcrel)
23322         {
23323           code = BFD_RELOC_ARM_MOVT_PCREL;
23324           break;
23325         }
23326
23327     case BFD_RELOC_ARM_THUMB_MOVW:
23328       if (fixp->fx_pcrel)
23329         {
23330           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
23331           break;
23332         }
23333
23334     case BFD_RELOC_ARM_THUMB_MOVT:
23335       if (fixp->fx_pcrel)
23336         {
23337           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
23338           break;
23339         }
23340
23341     case BFD_RELOC_NONE:
23342     case BFD_RELOC_ARM_PCREL_BRANCH:
23343     case BFD_RELOC_ARM_PCREL_BLX:
23344     case BFD_RELOC_RVA:
23345     case BFD_RELOC_THUMB_PCREL_BRANCH7:
23346     case BFD_RELOC_THUMB_PCREL_BRANCH9:
23347     case BFD_RELOC_THUMB_PCREL_BRANCH12:
23348     case BFD_RELOC_THUMB_PCREL_BRANCH20:
23349     case BFD_RELOC_THUMB_PCREL_BRANCH23:
23350     case BFD_RELOC_THUMB_PCREL_BRANCH25:
23351     case BFD_RELOC_VTABLE_ENTRY:
23352     case BFD_RELOC_VTABLE_INHERIT:
23353 #ifdef TE_PE
23354     case BFD_RELOC_32_SECREL:
23355 #endif
23356       code = fixp->fx_r_type;
23357       break;
23358
23359     case BFD_RELOC_THUMB_PCREL_BLX:
23360 #ifdef OBJ_ELF
23361       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
23362         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
23363       else
23364 #endif
23365         code = BFD_RELOC_THUMB_PCREL_BLX;
23366       break;
23367
23368     case BFD_RELOC_ARM_LITERAL:
23369     case BFD_RELOC_ARM_HWLITERAL:
23370       /* If this is called then the a literal has
23371          been referenced across a section boundary.  */
23372       as_bad_where (fixp->fx_file, fixp->fx_line,
23373                     _("literal referenced across section boundary"));
23374       return NULL;
23375
23376 #ifdef OBJ_ELF
23377     case BFD_RELOC_ARM_TLS_CALL:
23378     case BFD_RELOC_ARM_THM_TLS_CALL:
23379     case BFD_RELOC_ARM_TLS_DESCSEQ:
23380     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
23381     case BFD_RELOC_ARM_GOT32:
23382     case BFD_RELOC_ARM_GOTOFF:
23383     case BFD_RELOC_ARM_GOT_PREL:
23384     case BFD_RELOC_ARM_PLT32:
23385     case BFD_RELOC_ARM_TARGET1:
23386     case BFD_RELOC_ARM_ROSEGREL32:
23387     case BFD_RELOC_ARM_SBREL32:
23388     case BFD_RELOC_ARM_PREL31:
23389     case BFD_RELOC_ARM_TARGET2:
23390     case BFD_RELOC_ARM_TLS_LE32:
23391     case BFD_RELOC_ARM_TLS_LDO32:
23392     case BFD_RELOC_ARM_PCREL_CALL:
23393     case BFD_RELOC_ARM_PCREL_JUMP:
23394     case BFD_RELOC_ARM_ALU_PC_G0_NC:
23395     case BFD_RELOC_ARM_ALU_PC_G0:
23396     case BFD_RELOC_ARM_ALU_PC_G1_NC:
23397     case BFD_RELOC_ARM_ALU_PC_G1:
23398     case BFD_RELOC_ARM_ALU_PC_G2:
23399     case BFD_RELOC_ARM_LDR_PC_G0:
23400     case BFD_RELOC_ARM_LDR_PC_G1:
23401     case BFD_RELOC_ARM_LDR_PC_G2:
23402     case BFD_RELOC_ARM_LDRS_PC_G0:
23403     case BFD_RELOC_ARM_LDRS_PC_G1:
23404     case BFD_RELOC_ARM_LDRS_PC_G2:
23405     case BFD_RELOC_ARM_LDC_PC_G0:
23406     case BFD_RELOC_ARM_LDC_PC_G1:
23407     case BFD_RELOC_ARM_LDC_PC_G2:
23408     case BFD_RELOC_ARM_ALU_SB_G0_NC:
23409     case BFD_RELOC_ARM_ALU_SB_G0:
23410     case BFD_RELOC_ARM_ALU_SB_G1_NC:
23411     case BFD_RELOC_ARM_ALU_SB_G1:
23412     case BFD_RELOC_ARM_ALU_SB_G2:
23413     case BFD_RELOC_ARM_LDR_SB_G0:
23414     case BFD_RELOC_ARM_LDR_SB_G1:
23415     case BFD_RELOC_ARM_LDR_SB_G2:
23416     case BFD_RELOC_ARM_LDRS_SB_G0:
23417     case BFD_RELOC_ARM_LDRS_SB_G1:
23418     case BFD_RELOC_ARM_LDRS_SB_G2:
23419     case BFD_RELOC_ARM_LDC_SB_G0:
23420     case BFD_RELOC_ARM_LDC_SB_G1:
23421     case BFD_RELOC_ARM_LDC_SB_G2:
23422     case BFD_RELOC_ARM_V4BX:
23423       code = fixp->fx_r_type;
23424       break;
23425
23426     case BFD_RELOC_ARM_TLS_GOTDESC:
23427     case BFD_RELOC_ARM_TLS_GD32:
23428     case BFD_RELOC_ARM_TLS_IE32:
23429     case BFD_RELOC_ARM_TLS_LDM32:
23430       /* BFD will include the symbol's address in the addend.
23431          But we don't want that, so subtract it out again here.  */
23432       if (!S_IS_COMMON (fixp->fx_addsy))
23433         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
23434       code = fixp->fx_r_type;
23435       break;
23436 #endif
23437
23438     case BFD_RELOC_ARM_IMMEDIATE:
23439       as_bad_where (fixp->fx_file, fixp->fx_line,
23440                     _("internal relocation (type: IMMEDIATE) not fixed up"));
23441       return NULL;
23442
23443     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
23444       as_bad_where (fixp->fx_file, fixp->fx_line,
23445                     _("ADRL used for a symbol not defined in the same file"));
23446       return NULL;
23447
23448     case BFD_RELOC_ARM_OFFSET_IMM:
23449       if (section->use_rela_p)
23450         {
23451           code = fixp->fx_r_type;
23452           break;
23453         }
23454
23455       if (fixp->fx_addsy != NULL
23456           && !S_IS_DEFINED (fixp->fx_addsy)
23457           && S_IS_LOCAL (fixp->fx_addsy))
23458         {
23459           as_bad_where (fixp->fx_file, fixp->fx_line,
23460                         _("undefined local label `%s'"),
23461                         S_GET_NAME (fixp->fx_addsy));
23462           return NULL;
23463         }
23464
23465       as_bad_where (fixp->fx_file, fixp->fx_line,
23466                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
23467       return NULL;
23468
23469     default:
23470       {
23471         char * type;
23472
23473         switch (fixp->fx_r_type)
23474           {
23475           case BFD_RELOC_NONE:             type = "NONE";         break;
23476           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
23477           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
23478           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
23479           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
23480           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
23481           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
23482           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
23483           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
23484           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
23485           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
23486           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
23487           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
23488           default:                         type = _("<unknown>"); break;
23489           }
23490         as_bad_where (fixp->fx_file, fixp->fx_line,
23491                       _("cannot represent %s relocation in this object file format"),
23492                       type);
23493         return NULL;
23494       }
23495     }
23496
23497 #ifdef OBJ_ELF
23498   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
23499       && GOT_symbol
23500       && fixp->fx_addsy == GOT_symbol)
23501     {
23502       code = BFD_RELOC_ARM_GOTPC;
23503       reloc->addend = fixp->fx_offset = reloc->address;
23504     }
23505 #endif
23506
23507   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
23508
23509   if (reloc->howto == NULL)
23510     {
23511       as_bad_where (fixp->fx_file, fixp->fx_line,
23512                     _("cannot represent %s relocation in this object file format"),
23513                     bfd_get_reloc_code_name (code));
23514       return NULL;
23515     }
23516
23517   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
23518      vtable entry to be used in the relocation's section offset.  */
23519   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
23520     reloc->address = fixp->fx_offset;
23521
23522   return reloc;
23523 }
23524
23525 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
23526
23527 void
23528 cons_fix_new_arm (fragS *       frag,
23529                   int           where,
23530                   int           size,
23531                   expressionS * exp,
23532                   bfd_reloc_code_real_type reloc)
23533 {
23534   int pcrel = 0;
23535
23536   /* Pick a reloc.
23537      FIXME: @@ Should look at CPU word size.  */
23538   switch (size)
23539     {
23540     case 1:
23541       reloc = BFD_RELOC_8;
23542       break;
23543     case 2:
23544       reloc = BFD_RELOC_16;
23545       break;
23546     case 4:
23547     default:
23548       reloc = BFD_RELOC_32;
23549       break;
23550     case 8:
23551       reloc = BFD_RELOC_64;
23552       break;
23553     }
23554
23555 #ifdef TE_PE
23556   if (exp->X_op == O_secrel)
23557   {
23558     exp->X_op = O_symbol;
23559     reloc = BFD_RELOC_32_SECREL;
23560   }
23561 #endif
23562
23563   fix_new_exp (frag, where, size, exp, pcrel, reloc);
23564 }
23565
23566 #if defined (OBJ_COFF)
23567 void
23568 arm_validate_fix (fixS * fixP)
23569 {
23570   /* If the destination of the branch is a defined symbol which does not have
23571      the THUMB_FUNC attribute, then we must be calling a function which has
23572      the (interfacearm) attribute.  We look for the Thumb entry point to that
23573      function and change the branch to refer to that function instead.  */
23574   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
23575       && fixP->fx_addsy != NULL
23576       && S_IS_DEFINED (fixP->fx_addsy)
23577       && ! THUMB_IS_FUNC (fixP->fx_addsy))
23578     {
23579       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
23580     }
23581 }
23582 #endif
23583
23584
23585 int
23586 arm_force_relocation (struct fix * fixp)
23587 {
23588 #if defined (OBJ_COFF) && defined (TE_PE)
23589   if (fixp->fx_r_type == BFD_RELOC_RVA)
23590     return 1;
23591 #endif
23592
23593   /* In case we have a call or a branch to a function in ARM ISA mode from
23594      a thumb function or vice-versa force the relocation. These relocations
23595      are cleared off for some cores that might have blx and simple transformations
23596      are possible.  */
23597
23598 #ifdef OBJ_ELF
23599   switch (fixp->fx_r_type)
23600     {
23601     case BFD_RELOC_ARM_PCREL_JUMP:
23602     case BFD_RELOC_ARM_PCREL_CALL:
23603     case BFD_RELOC_THUMB_PCREL_BLX:
23604       if (THUMB_IS_FUNC (fixp->fx_addsy))
23605         return 1;
23606       break;
23607
23608     case BFD_RELOC_ARM_PCREL_BLX:
23609     case BFD_RELOC_THUMB_PCREL_BRANCH25:
23610     case BFD_RELOC_THUMB_PCREL_BRANCH20:
23611     case BFD_RELOC_THUMB_PCREL_BRANCH23:
23612       if (ARM_IS_FUNC (fixp->fx_addsy))
23613         return 1;
23614       break;
23615
23616     default:
23617       break;
23618     }
23619 #endif
23620
23621   /* Resolve these relocations even if the symbol is extern or weak.
23622      Technically this is probably wrong due to symbol preemption.
23623      In practice these relocations do not have enough range to be useful
23624      at dynamic link time, and some code (e.g. in the Linux kernel)
23625      expects these references to be resolved.  */
23626   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
23627       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
23628       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
23629       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
23630       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
23631       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
23632       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
23633       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
23634       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
23635       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
23636       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
23637       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
23638       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
23639       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
23640     return 0;
23641
23642   /* Always leave these relocations for the linker.  */
23643   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
23644        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
23645       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
23646     return 1;
23647
23648   /* Always generate relocations against function symbols.  */
23649   if (fixp->fx_r_type == BFD_RELOC_32
23650       && fixp->fx_addsy
23651       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
23652     return 1;
23653
23654   return generic_force_reloc (fixp);
23655 }
23656
23657 #if defined (OBJ_ELF) || defined (OBJ_COFF)
23658 /* Relocations against function names must be left unadjusted,
23659    so that the linker can use this information to generate interworking
23660    stubs.  The MIPS version of this function
23661    also prevents relocations that are mips-16 specific, but I do not
23662    know why it does this.
23663
23664    FIXME:
23665    There is one other problem that ought to be addressed here, but
23666    which currently is not:  Taking the address of a label (rather
23667    than a function) and then later jumping to that address.  Such
23668    addresses also ought to have their bottom bit set (assuming that
23669    they reside in Thumb code), but at the moment they will not.  */
23670
23671 bfd_boolean
23672 arm_fix_adjustable (fixS * fixP)
23673 {
23674   if (fixP->fx_addsy == NULL)
23675     return 1;
23676
23677   /* Preserve relocations against symbols with function type.  */
23678   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
23679     return FALSE;
23680
23681   if (THUMB_IS_FUNC (fixP->fx_addsy)
23682       && fixP->fx_subsy == NULL)
23683     return FALSE;
23684
23685   /* We need the symbol name for the VTABLE entries.  */
23686   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
23687       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
23688     return FALSE;
23689
23690   /* Don't allow symbols to be discarded on GOT related relocs.  */
23691   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
23692       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
23693       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
23694       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
23695       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
23696       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
23697       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
23698       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
23699       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
23700       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
23701       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
23702       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
23703       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
23704       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
23705     return FALSE;
23706
23707   /* Similarly for group relocations.  */
23708   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
23709        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
23710       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
23711     return FALSE;
23712
23713   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
23714   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
23715       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
23716       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
23717       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
23718       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
23719       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
23720       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
23721       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
23722     return FALSE;
23723
23724   return TRUE;
23725 }
23726 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
23727
23728 #ifdef OBJ_ELF
23729
23730 const char *
23731 elf32_arm_target_format (void)
23732 {
23733 #ifdef TE_SYMBIAN
23734   return (target_big_endian
23735           ? "elf32-bigarm-symbian"
23736           : "elf32-littlearm-symbian");
23737 #elif defined (TE_VXWORKS)
23738   return (target_big_endian
23739           ? "elf32-bigarm-vxworks"
23740           : "elf32-littlearm-vxworks");
23741 #elif defined (TE_NACL)
23742   return (target_big_endian
23743           ? "elf32-bigarm-nacl"
23744           : "elf32-littlearm-nacl");
23745 #else
23746   if (target_big_endian)
23747     return "elf32-bigarm";
23748   else
23749     return "elf32-littlearm";
23750 #endif
23751 }
23752
23753 void
23754 armelf_frob_symbol (symbolS * symp,
23755                     int *     puntp)
23756 {
23757   elf_frob_symbol (symp, puntp);
23758 }
23759 #endif
23760
23761 /* MD interface: Finalization.  */
23762
23763 void
23764 arm_cleanup (void)
23765 {
23766   literal_pool * pool;
23767
23768   /* Ensure that all the IT blocks are properly closed.  */
23769   check_it_blocks_finished ();
23770
23771   for (pool = list_of_pools; pool; pool = pool->next)
23772     {
23773       /* Put it at the end of the relevant section.  */
23774       subseg_set (pool->section, pool->sub_section);
23775 #ifdef OBJ_ELF
23776       arm_elf_change_section ();
23777 #endif
23778       s_ltorg (0);
23779     }
23780 }
23781
23782 #ifdef OBJ_ELF
23783 /* Remove any excess mapping symbols generated for alignment frags in
23784    SEC.  We may have created a mapping symbol before a zero byte
23785    alignment; remove it if there's a mapping symbol after the
23786    alignment.  */
23787 static void
23788 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
23789                        void *dummy ATTRIBUTE_UNUSED)
23790 {
23791   segment_info_type *seginfo = seg_info (sec);
23792   fragS *fragp;
23793
23794   if (seginfo == NULL || seginfo->frchainP == NULL)
23795     return;
23796
23797   for (fragp = seginfo->frchainP->frch_root;
23798        fragp != NULL;
23799        fragp = fragp->fr_next)
23800     {
23801       symbolS *sym = fragp->tc_frag_data.last_map;
23802       fragS *next = fragp->fr_next;
23803
23804       /* Variable-sized frags have been converted to fixed size by
23805          this point.  But if this was variable-sized to start with,
23806          there will be a fixed-size frag after it.  So don't handle
23807          next == NULL.  */
23808       if (sym == NULL || next == NULL)
23809         continue;
23810
23811       if (S_GET_VALUE (sym) < next->fr_address)
23812         /* Not at the end of this frag.  */
23813         continue;
23814       know (S_GET_VALUE (sym) == next->fr_address);
23815
23816       do
23817         {
23818           if (next->tc_frag_data.first_map != NULL)
23819             {
23820               /* Next frag starts with a mapping symbol.  Discard this
23821                  one.  */
23822               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
23823               break;
23824             }
23825
23826           if (next->fr_next == NULL)
23827             {
23828               /* This mapping symbol is at the end of the section.  Discard
23829                  it.  */
23830               know (next->fr_fix == 0 && next->fr_var == 0);
23831               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
23832               break;
23833             }
23834
23835           /* As long as we have empty frags without any mapping symbols,
23836              keep looking.  */
23837           /* If the next frag is non-empty and does not start with a
23838              mapping symbol, then this mapping symbol is required.  */
23839           if (next->fr_address != next->fr_next->fr_address)
23840             break;
23841
23842           next = next->fr_next;
23843         }
23844       while (next != NULL);
23845     }
23846 }
23847 #endif
23848
23849 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
23850    ARM ones.  */
23851
23852 void
23853 arm_adjust_symtab (void)
23854 {
23855 #ifdef OBJ_COFF
23856   symbolS * sym;
23857
23858   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
23859     {
23860       if (ARM_IS_THUMB (sym))
23861         {
23862           if (THUMB_IS_FUNC (sym))
23863             {
23864               /* Mark the symbol as a Thumb function.  */
23865               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
23866                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
23867                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
23868
23869               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
23870                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
23871               else
23872                 as_bad (_("%s: unexpected function type: %d"),
23873                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
23874             }
23875           else switch (S_GET_STORAGE_CLASS (sym))
23876             {
23877             case C_EXT:
23878               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
23879               break;
23880             case C_STAT:
23881               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
23882               break;
23883             case C_LABEL:
23884               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
23885               break;
23886             default:
23887               /* Do nothing.  */
23888               break;
23889             }
23890         }
23891
23892       if (ARM_IS_INTERWORK (sym))
23893         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
23894     }
23895 #endif
23896 #ifdef OBJ_ELF
23897   symbolS * sym;
23898   char      bind;
23899
23900   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
23901     {
23902       if (ARM_IS_THUMB (sym))
23903         {
23904           elf_symbol_type * elf_sym;
23905
23906           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
23907           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
23908
23909           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
23910                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
23911             {
23912               /* If it's a .thumb_func, declare it as so,
23913                  otherwise tag label as .code 16.  */
23914               if (THUMB_IS_FUNC (sym))
23915                 elf_sym->internal_elf_sym.st_target_internal
23916                   = ST_BRANCH_TO_THUMB;
23917               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
23918                 elf_sym->internal_elf_sym.st_info =
23919                   ELF_ST_INFO (bind, STT_ARM_16BIT);
23920             }
23921         }
23922     }
23923
23924   /* Remove any overlapping mapping symbols generated by alignment frags.  */
23925   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
23926   /* Now do generic ELF adjustments.  */
23927   elf_adjust_symtab ();
23928 #endif
23929 }
23930
23931 /* MD interface: Initialization.  */
23932
23933 static void
23934 set_constant_flonums (void)
23935 {
23936   int i;
23937
23938   for (i = 0; i < NUM_FLOAT_VALS; i++)
23939     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
23940       abort ();
23941 }
23942
23943 /* Auto-select Thumb mode if it's the only available instruction set for the
23944    given architecture.  */
23945
23946 static void
23947 autoselect_thumb_from_cpu_variant (void)
23948 {
23949   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
23950     opcode_select (16);
23951 }
23952
23953 void
23954 md_begin (void)
23955 {
23956   unsigned mach;
23957   unsigned int i;
23958
23959   if (   (arm_ops_hsh = hash_new ()) == NULL
23960       || (arm_cond_hsh = hash_new ()) == NULL
23961       || (arm_shift_hsh = hash_new ()) == NULL
23962       || (arm_psr_hsh = hash_new ()) == NULL
23963       || (arm_v7m_psr_hsh = hash_new ()) == NULL
23964       || (arm_reg_hsh = hash_new ()) == NULL
23965       || (arm_reloc_hsh = hash_new ()) == NULL
23966       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
23967     as_fatal (_("virtual memory exhausted"));
23968
23969   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
23970     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
23971   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
23972     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
23973   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
23974     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
23975   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
23976     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
23977   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
23978     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
23979                  (void *) (v7m_psrs + i));
23980   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
23981     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
23982   for (i = 0;
23983        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
23984        i++)
23985     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
23986                  (void *) (barrier_opt_names + i));
23987 #ifdef OBJ_ELF
23988   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
23989     {
23990       struct reloc_entry * entry = reloc_names + i;
23991
23992       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
23993         /* This makes encode_branch() use the EABI versions of this relocation.  */
23994         entry->reloc = BFD_RELOC_UNUSED;
23995
23996       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
23997     }
23998 #endif
23999
24000   set_constant_flonums ();
24001
24002   /* Set the cpu variant based on the command-line options.  We prefer
24003      -mcpu= over -march= if both are set (as for GCC); and we prefer
24004      -mfpu= over any other way of setting the floating point unit.
24005      Use of legacy options with new options are faulted.  */
24006   if (legacy_cpu)
24007     {
24008       if (mcpu_cpu_opt || march_cpu_opt)
24009         as_bad (_("use of old and new-style options to set CPU type"));
24010
24011       mcpu_cpu_opt = legacy_cpu;
24012     }
24013   else if (!mcpu_cpu_opt)
24014     mcpu_cpu_opt = march_cpu_opt;
24015
24016   if (legacy_fpu)
24017     {
24018       if (mfpu_opt)
24019         as_bad (_("use of old and new-style options to set FPU type"));
24020
24021       mfpu_opt = legacy_fpu;
24022     }
24023   else if (!mfpu_opt)
24024     {
24025 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
24026         || defined (TE_NetBSD) || defined (TE_VXWORKS))
24027       /* Some environments specify a default FPU.  If they don't, infer it
24028          from the processor.  */
24029       if (mcpu_fpu_opt)
24030         mfpu_opt = mcpu_fpu_opt;
24031       else
24032         mfpu_opt = march_fpu_opt;
24033 #else
24034       mfpu_opt = &fpu_default;
24035 #endif
24036     }
24037
24038   if (!mfpu_opt)
24039     {
24040       if (mcpu_cpu_opt != NULL)
24041         mfpu_opt = &fpu_default;
24042       else if (mcpu_fpu_opt != NULL && ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
24043         mfpu_opt = &fpu_arch_vfp_v2;
24044       else
24045         mfpu_opt = &fpu_arch_fpa;
24046     }
24047
24048 #ifdef CPU_DEFAULT
24049   if (!mcpu_cpu_opt)
24050     {
24051       mcpu_cpu_opt = &cpu_default;
24052       selected_cpu = cpu_default;
24053     }
24054   else if (no_cpu_selected ())
24055     selected_cpu = cpu_default;
24056 #else
24057   if (mcpu_cpu_opt)
24058     selected_cpu = *mcpu_cpu_opt;
24059   else
24060     mcpu_cpu_opt = &arm_arch_any;
24061 #endif
24062
24063   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
24064
24065   autoselect_thumb_from_cpu_variant ();
24066
24067   arm_arch_used = thumb_arch_used = arm_arch_none;
24068
24069 #if defined OBJ_COFF || defined OBJ_ELF
24070   {
24071     unsigned int flags = 0;
24072
24073 #if defined OBJ_ELF
24074     flags = meabi_flags;
24075
24076     switch (meabi_flags)
24077       {
24078       case EF_ARM_EABI_UNKNOWN:
24079 #endif
24080         /* Set the flags in the private structure.  */
24081         if (uses_apcs_26)      flags |= F_APCS26;
24082         if (support_interwork) flags |= F_INTERWORK;
24083         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
24084         if (pic_code)          flags |= F_PIC;
24085         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
24086           flags |= F_SOFT_FLOAT;
24087
24088         switch (mfloat_abi_opt)
24089           {
24090           case ARM_FLOAT_ABI_SOFT:
24091           case ARM_FLOAT_ABI_SOFTFP:
24092             flags |= F_SOFT_FLOAT;
24093             break;
24094
24095           case ARM_FLOAT_ABI_HARD:
24096             if (flags & F_SOFT_FLOAT)
24097               as_bad (_("hard-float conflicts with specified fpu"));
24098             break;
24099           }
24100
24101         /* Using pure-endian doubles (even if soft-float).      */
24102         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
24103           flags |= F_VFP_FLOAT;
24104
24105 #if defined OBJ_ELF
24106         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
24107             flags |= EF_ARM_MAVERICK_FLOAT;
24108         break;
24109
24110       case EF_ARM_EABI_VER4:
24111       case EF_ARM_EABI_VER5:
24112         /* No additional flags to set.  */
24113         break;
24114
24115       default:
24116         abort ();
24117       }
24118 #endif
24119     bfd_set_private_flags (stdoutput, flags);
24120
24121     /* We have run out flags in the COFF header to encode the
24122        status of ATPCS support, so instead we create a dummy,
24123        empty, debug section called .arm.atpcs.  */
24124     if (atpcs)
24125       {
24126         asection * sec;
24127
24128         sec = bfd_make_section (stdoutput, ".arm.atpcs");
24129
24130         if (sec != NULL)
24131           {
24132             bfd_set_section_flags
24133               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
24134             bfd_set_section_size (stdoutput, sec, 0);
24135             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
24136           }
24137       }
24138   }
24139 #endif
24140
24141   /* Record the CPU type as well.  */
24142   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
24143     mach = bfd_mach_arm_iWMMXt2;
24144   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
24145     mach = bfd_mach_arm_iWMMXt;
24146   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
24147     mach = bfd_mach_arm_XScale;
24148   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
24149     mach = bfd_mach_arm_ep9312;
24150   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
24151     mach = bfd_mach_arm_5TE;
24152   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
24153     {
24154       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
24155         mach = bfd_mach_arm_5T;
24156       else
24157         mach = bfd_mach_arm_5;
24158     }
24159   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
24160     {
24161       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
24162         mach = bfd_mach_arm_4T;
24163       else
24164         mach = bfd_mach_arm_4;
24165     }
24166   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
24167     mach = bfd_mach_arm_3M;
24168   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
24169     mach = bfd_mach_arm_3;
24170   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
24171     mach = bfd_mach_arm_2a;
24172   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
24173     mach = bfd_mach_arm_2;
24174   else
24175     mach = bfd_mach_arm_unknown;
24176
24177   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
24178 }
24179
24180 /* Command line processing.  */
24181
24182 /* md_parse_option
24183       Invocation line includes a switch not recognized by the base assembler.
24184       See if it's a processor-specific option.
24185
24186       This routine is somewhat complicated by the need for backwards
24187       compatibility (since older releases of gcc can't be changed).
24188       The new options try to make the interface as compatible as
24189       possible with GCC.
24190
24191       New options (supported) are:
24192
24193               -mcpu=<cpu name>           Assemble for selected processor
24194               -march=<architecture name> Assemble for selected architecture
24195               -mfpu=<fpu architecture>   Assemble for selected FPU.
24196               -EB/-mbig-endian           Big-endian
24197               -EL/-mlittle-endian        Little-endian
24198               -k                         Generate PIC code
24199               -mthumb                    Start in Thumb mode
24200               -mthumb-interwork          Code supports ARM/Thumb interworking
24201
24202               -m[no-]warn-deprecated     Warn about deprecated features
24203               -m[no-]warn-syms           Warn when symbols match instructions
24204
24205       For now we will also provide support for:
24206
24207               -mapcs-32                  32-bit Program counter
24208               -mapcs-26                  26-bit Program counter
24209               -macps-float               Floats passed in FP registers
24210               -mapcs-reentrant           Reentrant code
24211               -matpcs
24212       (sometime these will probably be replaced with -mapcs=<list of options>
24213       and -matpcs=<list of options>)
24214
24215       The remaining options are only supported for back-wards compatibility.
24216       Cpu variants, the arm part is optional:
24217               -m[arm]1                Currently not supported.
24218               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
24219               -m[arm]3                Arm 3 processor
24220               -m[arm]6[xx],           Arm 6 processors
24221               -m[arm]7[xx][t][[d]m]   Arm 7 processors
24222               -m[arm]8[10]            Arm 8 processors
24223               -m[arm]9[20][tdmi]      Arm 9 processors
24224               -mstrongarm[110[0]]     StrongARM processors
24225               -mxscale                XScale processors
24226               -m[arm]v[2345[t[e]]]    Arm architectures
24227               -mall                   All (except the ARM1)
24228       FP variants:
24229               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
24230               -mfpe-old               (No float load/store multiples)
24231               -mvfpxd                 VFP Single precision
24232               -mvfp                   All VFP
24233               -mno-fpu                Disable all floating point instructions
24234
24235       The following CPU names are recognized:
24236               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
24237               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
24238               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
24239               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
24240               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
24241               arm10t arm10e, arm1020t, arm1020e, arm10200e,
24242               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
24243
24244       */
24245
24246 const char * md_shortopts = "m:k";
24247
24248 #ifdef ARM_BI_ENDIAN
24249 #define OPTION_EB (OPTION_MD_BASE + 0)
24250 #define OPTION_EL (OPTION_MD_BASE + 1)
24251 #else
24252 #if TARGET_BYTES_BIG_ENDIAN
24253 #define OPTION_EB (OPTION_MD_BASE + 0)
24254 #else
24255 #define OPTION_EL (OPTION_MD_BASE + 1)
24256 #endif
24257 #endif
24258 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
24259
24260 struct option md_longopts[] =
24261 {
24262 #ifdef OPTION_EB
24263   {"EB", no_argument, NULL, OPTION_EB},
24264 #endif
24265 #ifdef OPTION_EL
24266   {"EL", no_argument, NULL, OPTION_EL},
24267 #endif
24268   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
24269   {NULL, no_argument, NULL, 0}
24270 };
24271
24272
24273 size_t md_longopts_size = sizeof (md_longopts);
24274
24275 struct arm_option_table
24276 {
24277   char *option;         /* Option name to match.  */
24278   char *help;           /* Help information.  */
24279   int  *var;            /* Variable to change.  */
24280   int   value;          /* What to change it to.  */
24281   char *deprecated;     /* If non-null, print this message.  */
24282 };
24283
24284 struct arm_option_table arm_opts[] =
24285 {
24286   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
24287   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
24288   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
24289    &support_interwork, 1, NULL},
24290   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
24291   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
24292   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
24293    1, NULL},
24294   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
24295   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
24296   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
24297   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
24298    NULL},
24299
24300   /* These are recognized by the assembler, but have no affect on code.  */
24301   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
24302   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
24303
24304   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
24305   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
24306    &warn_on_deprecated, 0, NULL},
24307   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
24308   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
24309   {NULL, NULL, NULL, 0, NULL}
24310 };
24311
24312 struct arm_legacy_option_table
24313 {
24314   char *option;                         /* Option name to match.  */
24315   const arm_feature_set **var;          /* Variable to change.  */
24316   const arm_feature_set value;          /* What to change it to.  */
24317   char *deprecated;                     /* If non-null, print this message.  */
24318 };
24319
24320 const struct arm_legacy_option_table arm_legacy_opts[] =
24321 {
24322   /* DON'T add any new processors to this list -- we want the whole list
24323      to go away...  Add them to the processors table instead.  */
24324   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
24325   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
24326   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
24327   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
24328   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
24329   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
24330   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
24331   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
24332   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
24333   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
24334   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
24335   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
24336   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
24337   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
24338   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
24339   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
24340   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
24341   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
24342   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
24343   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
24344   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
24345   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
24346   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
24347   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
24348   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
24349   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
24350   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
24351   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
24352   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
24353   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
24354   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
24355   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
24356   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
24357   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
24358   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
24359   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
24360   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
24361   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
24362   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
24363   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
24364   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
24365   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
24366   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
24367   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
24368   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
24369   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
24370   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
24371   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
24372   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
24373   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
24374   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
24375   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
24376   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
24377   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
24378   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
24379   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
24380   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
24381   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
24382   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
24383   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
24384   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
24385   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
24386   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
24387   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
24388   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
24389   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
24390   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
24391   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
24392   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
24393   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
24394    N_("use -mcpu=strongarm110")},
24395   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
24396    N_("use -mcpu=strongarm1100")},
24397   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
24398    N_("use -mcpu=strongarm1110")},
24399   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
24400   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
24401   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
24402
24403   /* Architecture variants -- don't add any more to this list either.  */
24404   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
24405   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
24406   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
24407   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
24408   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
24409   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
24410   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
24411   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
24412   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
24413   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
24414   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
24415   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
24416   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
24417   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
24418   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
24419   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
24420   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
24421   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
24422
24423   /* Floating point variants -- don't add any more to this list either.  */
24424   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
24425   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
24426   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
24427   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
24428    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
24429
24430   {NULL, NULL, ARM_ARCH_NONE, NULL}
24431 };
24432
24433 struct arm_cpu_option_table
24434 {
24435   char *name;
24436   size_t name_len;
24437   const arm_feature_set value;
24438   /* For some CPUs we assume an FPU unless the user explicitly sets
24439      -mfpu=...  */
24440   const arm_feature_set default_fpu;
24441   /* The canonical name of the CPU, or NULL to use NAME converted to upper
24442      case.  */
24443   const char *canonical_name;
24444 };
24445
24446 /* This list should, at a minimum, contain all the cpu names
24447    recognized by GCC.  */
24448 #define ARM_CPU_OPT(N, V, DF, CN) { N, sizeof (N) - 1, V, DF, CN }
24449 static const struct arm_cpu_option_table arm_cpus[] =
24450 {
24451   ARM_CPU_OPT ("all",           ARM_ANY,         FPU_ARCH_FPA,    NULL),
24452   ARM_CPU_OPT ("arm1",          ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL),
24453   ARM_CPU_OPT ("arm2",          ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL),
24454   ARM_CPU_OPT ("arm250",        ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
24455   ARM_CPU_OPT ("arm3",          ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
24456   ARM_CPU_OPT ("arm6",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24457   ARM_CPU_OPT ("arm60",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24458   ARM_CPU_OPT ("arm600",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24459   ARM_CPU_OPT ("arm610",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24460   ARM_CPU_OPT ("arm620",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24461   ARM_CPU_OPT ("arm7",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24462   ARM_CPU_OPT ("arm7m",         ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
24463   ARM_CPU_OPT ("arm7d",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24464   ARM_CPU_OPT ("arm7dm",        ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
24465   ARM_CPU_OPT ("arm7di",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24466   ARM_CPU_OPT ("arm7dmi",       ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
24467   ARM_CPU_OPT ("arm70",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24468   ARM_CPU_OPT ("arm700",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24469   ARM_CPU_OPT ("arm700i",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24470   ARM_CPU_OPT ("arm710",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24471   ARM_CPU_OPT ("arm710t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24472   ARM_CPU_OPT ("arm720",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24473   ARM_CPU_OPT ("arm720t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24474   ARM_CPU_OPT ("arm740t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24475   ARM_CPU_OPT ("arm710c",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24476   ARM_CPU_OPT ("arm7100",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24477   ARM_CPU_OPT ("arm7500",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24478   ARM_CPU_OPT ("arm7500fe",     ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24479   ARM_CPU_OPT ("arm7t",         ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24480   ARM_CPU_OPT ("arm7tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24481   ARM_CPU_OPT ("arm7tdmi-s",    ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24482   ARM_CPU_OPT ("arm8",          ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24483   ARM_CPU_OPT ("arm810",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24484   ARM_CPU_OPT ("strongarm",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24485   ARM_CPU_OPT ("strongarm1",    ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24486   ARM_CPU_OPT ("strongarm110",  ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24487   ARM_CPU_OPT ("strongarm1100", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24488   ARM_CPU_OPT ("strongarm1110", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24489   ARM_CPU_OPT ("arm9",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24490   ARM_CPU_OPT ("arm920",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"),
24491   ARM_CPU_OPT ("arm920t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24492   ARM_CPU_OPT ("arm922t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24493   ARM_CPU_OPT ("arm940t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24494   ARM_CPU_OPT ("arm9tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24495   ARM_CPU_OPT ("fa526",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24496   ARM_CPU_OPT ("fa626",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24497   /* For V5 or later processors we default to using VFP; but the user
24498      should really set the FPU type explicitly.  */
24499   ARM_CPU_OPT ("arm9e-r0",      ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
24500   ARM_CPU_OPT ("arm9e",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24501   ARM_CPU_OPT ("arm926ej",      ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
24502   ARM_CPU_OPT ("arm926ejs",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
24503   ARM_CPU_OPT ("arm926ej-s",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
24504   ARM_CPU_OPT ("arm946e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
24505   ARM_CPU_OPT ("arm946e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"),
24506   ARM_CPU_OPT ("arm946e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24507   ARM_CPU_OPT ("arm966e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
24508   ARM_CPU_OPT ("arm966e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"),
24509   ARM_CPU_OPT ("arm966e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24510   ARM_CPU_OPT ("arm968e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24511   ARM_CPU_OPT ("arm10t",        ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
24512   ARM_CPU_OPT ("arm10tdmi",     ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
24513   ARM_CPU_OPT ("arm10e",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24514   ARM_CPU_OPT ("arm1020",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"),
24515   ARM_CPU_OPT ("arm1020t",      ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
24516   ARM_CPU_OPT ("arm1020e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24517   ARM_CPU_OPT ("arm1022e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24518   ARM_CPU_OPT ("arm1026ejs",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2,
24519                                                                  "ARM1026EJ-S"),
24520   ARM_CPU_OPT ("arm1026ej-s",   ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
24521   ARM_CPU_OPT ("fa606te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24522   ARM_CPU_OPT ("fa616te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24523   ARM_CPU_OPT ("fa626te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24524   ARM_CPU_OPT ("fmp626",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24525   ARM_CPU_OPT ("fa726te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24526   ARM_CPU_OPT ("arm1136js",     ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"),
24527   ARM_CPU_OPT ("arm1136j-s",    ARM_ARCH_V6,     FPU_NONE,        NULL),
24528   ARM_CPU_OPT ("arm1136jfs",    ARM_ARCH_V6,     FPU_ARCH_VFP_V2,
24529                                                                  "ARM1136JF-S"),
24530   ARM_CPU_OPT ("arm1136jf-s",   ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL),
24531   ARM_CPU_OPT ("mpcore",        ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, "MPCore"),
24532   ARM_CPU_OPT ("mpcorenovfp",   ARM_ARCH_V6K,    FPU_NONE,        "MPCore"),
24533   ARM_CPU_OPT ("arm1156t2-s",   ARM_ARCH_V6T2,   FPU_NONE,        NULL),
24534   ARM_CPU_OPT ("arm1156t2f-s",  ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL),
24535   ARM_CPU_OPT ("arm1176jz-s",   ARM_ARCH_V6ZK,   FPU_NONE,        NULL),
24536   ARM_CPU_OPT ("arm1176jzf-s",  ARM_ARCH_V6ZK,   FPU_ARCH_VFP_V2, NULL),
24537   ARM_CPU_OPT ("cortex-a5",     ARM_ARCH_V7A_MP_SEC,
24538                                                  FPU_NONE,        "Cortex-A5"),
24539   ARM_CPU_OPT ("cortex-a7",     ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
24540                                                                   "Cortex-A7"),
24541   ARM_CPU_OPT ("cortex-a8",     ARM_ARCH_V7A_SEC,
24542                                                  ARM_FEATURE_COPROC (FPU_VFP_V3
24543                                                         | FPU_NEON_EXT_V1),
24544                                                                   "Cortex-A8"),
24545   ARM_CPU_OPT ("cortex-a9",     ARM_ARCH_V7A_MP_SEC,
24546                                                  ARM_FEATURE_COPROC (FPU_VFP_V3
24547                                                         | FPU_NEON_EXT_V1),
24548                                                                   "Cortex-A9"),
24549   ARM_CPU_OPT ("cortex-a12",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
24550                                                                   "Cortex-A12"),
24551   ARM_CPU_OPT ("cortex-a15",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
24552                                                                   "Cortex-A15"),
24553   ARM_CPU_OPT ("cortex-a17",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
24554                                                                   "Cortex-A17"),
24555   ARM_CPU_OPT ("cortex-a53",    ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24556                                                                   "Cortex-A53"),
24557   ARM_CPU_OPT ("cortex-a57",    ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24558                                                                   "Cortex-A57"),
24559   ARM_CPU_OPT ("cortex-a72",    ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24560                                                                   "Cortex-A72"),
24561   ARM_CPU_OPT ("cortex-r4",     ARM_ARCH_V7R,    FPU_NONE,        "Cortex-R4"),
24562   ARM_CPU_OPT ("cortex-r4f",    ARM_ARCH_V7R,    FPU_ARCH_VFP_V3D16,
24563                                                                   "Cortex-R4F"),
24564   ARM_CPU_OPT ("cortex-r5",     ARM_ARCH_V7R_IDIV,
24565                                                  FPU_NONE,        "Cortex-R5"),
24566   ARM_CPU_OPT ("cortex-r7",     ARM_ARCH_V7R_IDIV,
24567                                                  FPU_ARCH_VFP_V3D16,
24568                                                                   "Cortex-R7"),
24569   ARM_CPU_OPT ("cortex-m7",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M7"),
24570   ARM_CPU_OPT ("cortex-m4",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M4"),
24571   ARM_CPU_OPT ("cortex-m3",     ARM_ARCH_V7M,    FPU_NONE,        "Cortex-M3"),
24572   ARM_CPU_OPT ("cortex-m1",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M1"),
24573   ARM_CPU_OPT ("cortex-m0",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0"),
24574   ARM_CPU_OPT ("cortex-m0plus", ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0+"),
24575   ARM_CPU_OPT ("exynos-m1",     ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24576                                                                   "Samsung " \
24577                                                                   "Exynos M1"),
24578   /* ??? XSCALE is really an architecture.  */
24579   ARM_CPU_OPT ("xscale",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
24580   /* ??? iwmmxt is not a processor.  */
24581   ARM_CPU_OPT ("iwmmxt",        ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL),
24582   ARM_CPU_OPT ("iwmmxt2",       ARM_ARCH_IWMMXT2,FPU_ARCH_VFP_V2, NULL),
24583   ARM_CPU_OPT ("i80200",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
24584   /* Maverick */
24585   ARM_CPU_OPT ("ep9312",        ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
24586                                                  FPU_ARCH_MAVERICK, "ARM920T"),
24587   /* Marvell processors.  */
24588   ARM_CPU_OPT ("marvell-pj4",   ARM_FEATURE_CORE_LOW (ARM_AEXT_V7A | ARM_EXT_MP
24589                                                       | ARM_EXT_SEC),
24590                                                 FPU_ARCH_VFP_V3D16, NULL),
24591   ARM_CPU_OPT ("marvell-whitney", ARM_FEATURE_CORE_LOW (ARM_AEXT_V7A | ARM_EXT_MP
24592                                                         | ARM_EXT_SEC),
24593                                                FPU_ARCH_NEON_VFP_V4, NULL),
24594   /* APM X-Gene family.  */
24595   ARM_CPU_OPT ("xgene1",        ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24596                                                                   "APM X-Gene 1"),
24597   ARM_CPU_OPT ("xgene2",        ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24598                                                                   "APM X-Gene 2"),
24599
24600   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
24601 };
24602 #undef ARM_CPU_OPT
24603
24604 struct arm_arch_option_table
24605 {
24606   char *name;
24607   size_t name_len;
24608   const arm_feature_set value;
24609   const arm_feature_set default_fpu;
24610 };
24611
24612 /* This list should, at a minimum, contain all the architecture names
24613    recognized by GCC.  */
24614 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF }
24615 static const struct arm_arch_option_table arm_archs[] =
24616 {
24617   ARM_ARCH_OPT ("all",          ARM_ANY,         FPU_ARCH_FPA),
24618   ARM_ARCH_OPT ("armv1",        ARM_ARCH_V1,     FPU_ARCH_FPA),
24619   ARM_ARCH_OPT ("armv2",        ARM_ARCH_V2,     FPU_ARCH_FPA),
24620   ARM_ARCH_OPT ("armv2a",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
24621   ARM_ARCH_OPT ("armv2s",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
24622   ARM_ARCH_OPT ("armv3",        ARM_ARCH_V3,     FPU_ARCH_FPA),
24623   ARM_ARCH_OPT ("armv3m",       ARM_ARCH_V3M,    FPU_ARCH_FPA),
24624   ARM_ARCH_OPT ("armv4",        ARM_ARCH_V4,     FPU_ARCH_FPA),
24625   ARM_ARCH_OPT ("armv4xm",      ARM_ARCH_V4xM,   FPU_ARCH_FPA),
24626   ARM_ARCH_OPT ("armv4t",       ARM_ARCH_V4T,    FPU_ARCH_FPA),
24627   ARM_ARCH_OPT ("armv4txm",     ARM_ARCH_V4TxM,  FPU_ARCH_FPA),
24628   ARM_ARCH_OPT ("armv5",        ARM_ARCH_V5,     FPU_ARCH_VFP),
24629   ARM_ARCH_OPT ("armv5t",       ARM_ARCH_V5T,    FPU_ARCH_VFP),
24630   ARM_ARCH_OPT ("armv5txm",     ARM_ARCH_V5TxM,  FPU_ARCH_VFP),
24631   ARM_ARCH_OPT ("armv5te",      ARM_ARCH_V5TE,   FPU_ARCH_VFP),
24632   ARM_ARCH_OPT ("armv5texp",    ARM_ARCH_V5TExP, FPU_ARCH_VFP),
24633   ARM_ARCH_OPT ("armv5tej",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP),
24634   ARM_ARCH_OPT ("armv6",        ARM_ARCH_V6,     FPU_ARCH_VFP),
24635   ARM_ARCH_OPT ("armv6j",       ARM_ARCH_V6,     FPU_ARCH_VFP),
24636   ARM_ARCH_OPT ("armv6k",       ARM_ARCH_V6K,    FPU_ARCH_VFP),
24637   ARM_ARCH_OPT ("armv6z",       ARM_ARCH_V6Z,    FPU_ARCH_VFP),
24638   ARM_ARCH_OPT ("armv6zk",      ARM_ARCH_V6ZK,   FPU_ARCH_VFP),
24639   ARM_ARCH_OPT ("armv6t2",      ARM_ARCH_V6T2,   FPU_ARCH_VFP),
24640   ARM_ARCH_OPT ("armv6kt2",     ARM_ARCH_V6KT2,  FPU_ARCH_VFP),
24641   ARM_ARCH_OPT ("armv6zt2",     ARM_ARCH_V6ZT2,  FPU_ARCH_VFP),
24642   ARM_ARCH_OPT ("armv6zkt2",    ARM_ARCH_V6ZKT2, FPU_ARCH_VFP),
24643   ARM_ARCH_OPT ("armv6-m",      ARM_ARCH_V6M,    FPU_ARCH_VFP),
24644   ARM_ARCH_OPT ("armv6s-m",     ARM_ARCH_V6SM,   FPU_ARCH_VFP),
24645   ARM_ARCH_OPT ("armv7",        ARM_ARCH_V7,     FPU_ARCH_VFP),
24646   /* The official spelling of the ARMv7 profile variants is the dashed form.
24647      Accept the non-dashed form for compatibility with old toolchains.  */
24648   ARM_ARCH_OPT ("armv7a",       ARM_ARCH_V7A,    FPU_ARCH_VFP),
24649   ARM_ARCH_OPT ("armv7ve",      ARM_ARCH_V7VE,   FPU_ARCH_VFP),
24650   ARM_ARCH_OPT ("armv7r",       ARM_ARCH_V7R,    FPU_ARCH_VFP),
24651   ARM_ARCH_OPT ("armv7m",       ARM_ARCH_V7M,    FPU_ARCH_VFP),
24652   ARM_ARCH_OPT ("armv7-a",      ARM_ARCH_V7A,    FPU_ARCH_VFP),
24653   ARM_ARCH_OPT ("armv7-r",      ARM_ARCH_V7R,    FPU_ARCH_VFP),
24654   ARM_ARCH_OPT ("armv7-m",      ARM_ARCH_V7M,    FPU_ARCH_VFP),
24655   ARM_ARCH_OPT ("armv7e-m",     ARM_ARCH_V7EM,   FPU_ARCH_VFP),
24656   ARM_ARCH_OPT ("armv8-a",      ARM_ARCH_V8A,    FPU_ARCH_VFP),
24657   ARM_ARCH_OPT ("armv8.1-a",    ARM_ARCH_V8_1A,  FPU_ARCH_VFP),
24658   ARM_ARCH_OPT ("xscale",       ARM_ARCH_XSCALE, FPU_ARCH_VFP),
24659   ARM_ARCH_OPT ("iwmmxt",       ARM_ARCH_IWMMXT, FPU_ARCH_VFP),
24660   ARM_ARCH_OPT ("iwmmxt2",      ARM_ARCH_IWMMXT2,FPU_ARCH_VFP),
24661   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
24662 };
24663 #undef ARM_ARCH_OPT
24664
24665 /* ISA extensions in the co-processor and main instruction set space.  */
24666 struct arm_option_extension_value_table
24667 {
24668   char *name;
24669   size_t name_len;
24670   const arm_feature_set merge_value;
24671   const arm_feature_set clear_value;
24672   const arm_feature_set allowed_archs;
24673 };
24674
24675 /* The following table must be in alphabetical order with a NULL last entry.
24676    */
24677 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, AA }
24678 static const struct arm_option_extension_value_table arm_extensions[] =
24679 {
24680   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
24681                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24682   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24683                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
24684                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24685   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
24686                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24687   ARM_EXT_OPT ("idiv",  ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
24688                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
24689                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A | ARM_EXT_V7R)),
24690   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
24691                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ANY),
24692   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
24693                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ANY),
24694   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
24695                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ANY),
24696   ARM_EXT_OPT ("mp",    ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
24697                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
24698                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A | ARM_EXT_V7R)),
24699   ARM_EXT_OPT ("simd",   FPU_ARCH_NEON_VFP_ARMV8,
24700                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
24701                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24702   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
24703                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
24704                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
24705   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
24706                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
24707                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24708   ARM_EXT_OPT ("sec",   ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
24709                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
24710                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6K | ARM_EXT_V7A)),
24711   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
24712                                      | ARM_EXT_DIV),
24713                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
24714                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
24715   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8,
24716                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
24717                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24718   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
24719                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ANY),
24720   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, ARM_ARCH_NONE }
24721 };
24722 #undef ARM_EXT_OPT
24723
24724 /* ISA floating-point and Advanced SIMD extensions.  */
24725 struct arm_option_fpu_value_table
24726 {
24727   char *name;
24728   const arm_feature_set value;
24729 };
24730
24731 /* This list should, at a minimum, contain all the fpu names
24732    recognized by GCC.  */
24733 static const struct arm_option_fpu_value_table arm_fpus[] =
24734 {
24735   {"softfpa",           FPU_NONE},
24736   {"fpe",               FPU_ARCH_FPE},
24737   {"fpe2",              FPU_ARCH_FPE},
24738   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
24739   {"fpa",               FPU_ARCH_FPA},
24740   {"fpa10",             FPU_ARCH_FPA},
24741   {"fpa11",             FPU_ARCH_FPA},
24742   {"arm7500fe",         FPU_ARCH_FPA},
24743   {"softvfp",           FPU_ARCH_VFP},
24744   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
24745   {"vfp",               FPU_ARCH_VFP_V2},
24746   {"vfp9",              FPU_ARCH_VFP_V2},
24747   {"vfp3",              FPU_ARCH_VFP_V3}, /* For backwards compatbility.  */
24748   {"vfp10",             FPU_ARCH_VFP_V2},
24749   {"vfp10-r0",          FPU_ARCH_VFP_V1},
24750   {"vfpxd",             FPU_ARCH_VFP_V1xD},
24751   {"vfpv2",             FPU_ARCH_VFP_V2},
24752   {"vfpv3",             FPU_ARCH_VFP_V3},
24753   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
24754   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
24755   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
24756   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
24757   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
24758   {"arm1020t",          FPU_ARCH_VFP_V1},
24759   {"arm1020e",          FPU_ARCH_VFP_V2},
24760   {"arm1136jfs",        FPU_ARCH_VFP_V2},
24761   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
24762   {"maverick",          FPU_ARCH_MAVERICK},
24763   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
24764   {"neon-fp16",         FPU_ARCH_NEON_FP16},
24765   {"vfpv4",             FPU_ARCH_VFP_V4},
24766   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
24767   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
24768   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
24769   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
24770   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
24771   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
24772   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
24773   {"crypto-neon-fp-armv8",
24774                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
24775   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
24776   {NULL,                ARM_ARCH_NONE}
24777 };
24778
24779 struct arm_option_value_table
24780 {
24781   char *name;
24782   long value;
24783 };
24784
24785 static const struct arm_option_value_table arm_float_abis[] =
24786 {
24787   {"hard",      ARM_FLOAT_ABI_HARD},
24788   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
24789   {"soft",      ARM_FLOAT_ABI_SOFT},
24790   {NULL,        0}
24791 };
24792
24793 #ifdef OBJ_ELF
24794 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
24795 static const struct arm_option_value_table arm_eabis[] =
24796 {
24797   {"gnu",       EF_ARM_EABI_UNKNOWN},
24798   {"4",         EF_ARM_EABI_VER4},
24799   {"5",         EF_ARM_EABI_VER5},
24800   {NULL,        0}
24801 };
24802 #endif
24803
24804 struct arm_long_option_table
24805 {
24806   char * option;                /* Substring to match.  */
24807   char * help;                  /* Help information.  */
24808   int (* func) (char * subopt); /* Function to decode sub-option.  */
24809   char * deprecated;            /* If non-null, print this message.  */
24810 };
24811
24812 static bfd_boolean
24813 arm_parse_extension (char *str, const arm_feature_set **opt_p)
24814 {
24815   arm_feature_set *ext_set = (arm_feature_set *)
24816       xmalloc (sizeof (arm_feature_set));
24817
24818   /* We insist on extensions being specified in alphabetical order, and with
24819      extensions being added before being removed.  We achieve this by having
24820      the global ARM_EXTENSIONS table in alphabetical order, and using the
24821      ADDING_VALUE variable to indicate whether we are adding an extension (1)
24822      or removing it (0) and only allowing it to change in the order
24823      -1 -> 1 -> 0.  */
24824   const struct arm_option_extension_value_table * opt = NULL;
24825   int adding_value = -1;
24826
24827   /* Copy the feature set, so that we can modify it.  */
24828   *ext_set = **opt_p;
24829   *opt_p = ext_set;
24830
24831   while (str != NULL && *str != 0)
24832     {
24833       char *ext;
24834       size_t len;
24835
24836       if (*str != '+')
24837         {
24838           as_bad (_("invalid architectural extension"));
24839           return FALSE;
24840         }
24841
24842       str++;
24843       ext = strchr (str, '+');
24844
24845       if (ext != NULL)
24846         len = ext - str;
24847       else
24848         len = strlen (str);
24849
24850       if (len >= 2 && strncmp (str, "no", 2) == 0)
24851         {
24852           if (adding_value != 0)
24853             {
24854               adding_value = 0;
24855               opt = arm_extensions;
24856             }
24857
24858           len -= 2;
24859           str += 2;
24860         }
24861       else if (len > 0)
24862         {
24863           if (adding_value == -1)
24864             {
24865               adding_value = 1;
24866               opt = arm_extensions;
24867             }
24868           else if (adding_value != 1)
24869             {
24870               as_bad (_("must specify extensions to add before specifying "
24871                         "those to remove"));
24872               return FALSE;
24873             }
24874         }
24875
24876       if (len == 0)
24877         {
24878           as_bad (_("missing architectural extension"));
24879           return FALSE;
24880         }
24881
24882       gas_assert (adding_value != -1);
24883       gas_assert (opt != NULL);
24884
24885       /* Scan over the options table trying to find an exact match. */
24886       for (; opt->name != NULL; opt++)
24887         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
24888           {
24889             /* Check we can apply the extension to this architecture.  */
24890             if (!ARM_CPU_HAS_FEATURE (*ext_set, opt->allowed_archs))
24891               {
24892                 as_bad (_("extension does not apply to the base architecture"));
24893                 return FALSE;
24894               }
24895
24896             /* Add or remove the extension.  */
24897             if (adding_value)
24898               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
24899             else
24900               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
24901
24902             break;
24903           }
24904
24905       if (opt->name == NULL)
24906         {
24907           /* Did we fail to find an extension because it wasn't specified in
24908              alphabetical order, or because it does not exist?  */
24909
24910           for (opt = arm_extensions; opt->name != NULL; opt++)
24911             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
24912               break;
24913
24914           if (opt->name == NULL)
24915             as_bad (_("unknown architectural extension `%s'"), str);
24916           else
24917             as_bad (_("architectural extensions must be specified in "
24918                       "alphabetical order"));
24919
24920           return FALSE;
24921         }
24922       else
24923         {
24924           /* We should skip the extension we've just matched the next time
24925              round.  */
24926           opt++;
24927         }
24928
24929       str = ext;
24930     };
24931
24932   return TRUE;
24933 }
24934
24935 static bfd_boolean
24936 arm_parse_cpu (char *str)
24937 {
24938   const struct arm_cpu_option_table *opt;
24939   char *ext = strchr (str, '+');
24940   size_t len;
24941
24942   if (ext != NULL)
24943     len = ext - str;
24944   else
24945     len = strlen (str);
24946
24947   if (len == 0)
24948     {
24949       as_bad (_("missing cpu name `%s'"), str);
24950       return FALSE;
24951     }
24952
24953   for (opt = arm_cpus; opt->name != NULL; opt++)
24954     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
24955       {
24956         mcpu_cpu_opt = &opt->value;
24957         mcpu_fpu_opt = &opt->default_fpu;
24958         if (opt->canonical_name)
24959           strcpy (selected_cpu_name, opt->canonical_name);
24960         else
24961           {
24962             size_t i;
24963
24964             for (i = 0; i < len; i++)
24965               selected_cpu_name[i] = TOUPPER (opt->name[i]);
24966             selected_cpu_name[i] = 0;
24967           }
24968
24969         if (ext != NULL)
24970           return arm_parse_extension (ext, &mcpu_cpu_opt);
24971
24972         return TRUE;
24973       }
24974
24975   as_bad (_("unknown cpu `%s'"), str);
24976   return FALSE;
24977 }
24978
24979 static bfd_boolean
24980 arm_parse_arch (char *str)
24981 {
24982   const struct arm_arch_option_table *opt;
24983   char *ext = strchr (str, '+');
24984   size_t len;
24985
24986   if (ext != NULL)
24987     len = ext - str;
24988   else
24989     len = strlen (str);
24990
24991   if (len == 0)
24992     {
24993       as_bad (_("missing architecture name `%s'"), str);
24994       return FALSE;
24995     }
24996
24997   for (opt = arm_archs; opt->name != NULL; opt++)
24998     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
24999       {
25000         march_cpu_opt = &opt->value;
25001         march_fpu_opt = &opt->default_fpu;
25002         strcpy (selected_cpu_name, opt->name);
25003
25004         if (ext != NULL)
25005           return arm_parse_extension (ext, &march_cpu_opt);
25006
25007         return TRUE;
25008       }
25009
25010   as_bad (_("unknown architecture `%s'\n"), str);
25011   return FALSE;
25012 }
25013
25014 static bfd_boolean
25015 arm_parse_fpu (char * str)
25016 {
25017   const struct arm_option_fpu_value_table * opt;
25018
25019   for (opt = arm_fpus; opt->name != NULL; opt++)
25020     if (streq (opt->name, str))
25021       {
25022         mfpu_opt = &opt->value;
25023         return TRUE;
25024       }
25025
25026   as_bad (_("unknown floating point format `%s'\n"), str);
25027   return FALSE;
25028 }
25029
25030 static bfd_boolean
25031 arm_parse_float_abi (char * str)
25032 {
25033   const struct arm_option_value_table * opt;
25034
25035   for (opt = arm_float_abis; opt->name != NULL; opt++)
25036     if (streq (opt->name, str))
25037       {
25038         mfloat_abi_opt = opt->value;
25039         return TRUE;
25040       }
25041
25042   as_bad (_("unknown floating point abi `%s'\n"), str);
25043   return FALSE;
25044 }
25045
25046 #ifdef OBJ_ELF
25047 static bfd_boolean
25048 arm_parse_eabi (char * str)
25049 {
25050   const struct arm_option_value_table *opt;
25051
25052   for (opt = arm_eabis; opt->name != NULL; opt++)
25053     if (streq (opt->name, str))
25054       {
25055         meabi_flags = opt->value;
25056         return TRUE;
25057       }
25058   as_bad (_("unknown EABI `%s'\n"), str);
25059   return FALSE;
25060 }
25061 #endif
25062
25063 static bfd_boolean
25064 arm_parse_it_mode (char * str)
25065 {
25066   bfd_boolean ret = TRUE;
25067
25068   if (streq ("arm", str))
25069     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
25070   else if (streq ("thumb", str))
25071     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
25072   else if (streq ("always", str))
25073     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
25074   else if (streq ("never", str))
25075     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
25076   else
25077     {
25078       as_bad (_("unknown implicit IT mode `%s', should be "\
25079                 "arm, thumb, always, or never."), str);
25080       ret = FALSE;
25081     }
25082
25083   return ret;
25084 }
25085
25086 static bfd_boolean
25087 arm_ccs_mode (char * unused ATTRIBUTE_UNUSED)
25088 {
25089   codecomposer_syntax = TRUE;
25090   arm_comment_chars[0] = ';';
25091   arm_line_separator_chars[0] = 0;
25092   return TRUE;
25093 }
25094
25095 struct arm_long_option_table arm_long_opts[] =
25096 {
25097   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
25098    arm_parse_cpu, NULL},
25099   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
25100    arm_parse_arch, NULL},
25101   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
25102    arm_parse_fpu, NULL},
25103   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
25104    arm_parse_float_abi, NULL},
25105 #ifdef OBJ_ELF
25106   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
25107    arm_parse_eabi, NULL},
25108 #endif
25109   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
25110    arm_parse_it_mode, NULL},
25111   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
25112    arm_ccs_mode, NULL},
25113   {NULL, NULL, 0, NULL}
25114 };
25115
25116 int
25117 md_parse_option (int c, char * arg)
25118 {
25119   struct arm_option_table *opt;
25120   const struct arm_legacy_option_table *fopt;
25121   struct arm_long_option_table *lopt;
25122
25123   switch (c)
25124     {
25125 #ifdef OPTION_EB
25126     case OPTION_EB:
25127       target_big_endian = 1;
25128       break;
25129 #endif
25130
25131 #ifdef OPTION_EL
25132     case OPTION_EL:
25133       target_big_endian = 0;
25134       break;
25135 #endif
25136
25137     case OPTION_FIX_V4BX:
25138       fix_v4bx = TRUE;
25139       break;
25140
25141     case 'a':
25142       /* Listing option.  Just ignore these, we don't support additional
25143          ones.  */
25144       return 0;
25145
25146     default:
25147       for (opt = arm_opts; opt->option != NULL; opt++)
25148         {
25149           if (c == opt->option[0]
25150               && ((arg == NULL && opt->option[1] == 0)
25151                   || streq (arg, opt->option + 1)))
25152             {
25153               /* If the option is deprecated, tell the user.  */
25154               if (warn_on_deprecated && opt->deprecated != NULL)
25155                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
25156                            arg ? arg : "", _(opt->deprecated));
25157
25158               if (opt->var != NULL)
25159                 *opt->var = opt->value;
25160
25161               return 1;
25162             }
25163         }
25164
25165       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
25166         {
25167           if (c == fopt->option[0]
25168               && ((arg == NULL && fopt->option[1] == 0)
25169                   || streq (arg, fopt->option + 1)))
25170             {
25171               /* If the option is deprecated, tell the user.  */
25172               if (warn_on_deprecated && fopt->deprecated != NULL)
25173                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
25174                            arg ? arg : "", _(fopt->deprecated));
25175
25176               if (fopt->var != NULL)
25177                 *fopt->var = &fopt->value;
25178
25179               return 1;
25180             }
25181         }
25182
25183       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
25184         {
25185           /* These options are expected to have an argument.  */
25186           if (c == lopt->option[0]
25187               && arg != NULL
25188               && strncmp (arg, lopt->option + 1,
25189                           strlen (lopt->option + 1)) == 0)
25190             {
25191               /* If the option is deprecated, tell the user.  */
25192               if (warn_on_deprecated && lopt->deprecated != NULL)
25193                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
25194                            _(lopt->deprecated));
25195
25196               /* Call the sup-option parser.  */
25197               return lopt->func (arg + strlen (lopt->option) - 1);
25198             }
25199         }
25200
25201       return 0;
25202     }
25203
25204   return 1;
25205 }
25206
25207 void
25208 md_show_usage (FILE * fp)
25209 {
25210   struct arm_option_table *opt;
25211   struct arm_long_option_table *lopt;
25212
25213   fprintf (fp, _(" ARM-specific assembler options:\n"));
25214
25215   for (opt = arm_opts; opt->option != NULL; opt++)
25216     if (opt->help != NULL)
25217       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
25218
25219   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
25220     if (lopt->help != NULL)
25221       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
25222
25223 #ifdef OPTION_EB
25224   fprintf (fp, _("\
25225   -EB                     assemble code for a big-endian cpu\n"));
25226 #endif
25227
25228 #ifdef OPTION_EL
25229   fprintf (fp, _("\
25230   -EL                     assemble code for a little-endian cpu\n"));
25231 #endif
25232
25233   fprintf (fp, _("\
25234   --fix-v4bx              Allow BX in ARMv4 code\n"));
25235 }
25236
25237
25238 #ifdef OBJ_ELF
25239 typedef struct
25240 {
25241   int val;
25242   arm_feature_set flags;
25243 } cpu_arch_ver_table;
25244
25245 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
25246    least features first.  */
25247 static const cpu_arch_ver_table cpu_arch_ver[] =
25248 {
25249     {1, ARM_ARCH_V4},
25250     {2, ARM_ARCH_V4T},
25251     {3, ARM_ARCH_V5},
25252     {3, ARM_ARCH_V5T},
25253     {4, ARM_ARCH_V5TE},
25254     {5, ARM_ARCH_V5TEJ},
25255     {6, ARM_ARCH_V6},
25256     {9, ARM_ARCH_V6K},
25257     {7, ARM_ARCH_V6Z},
25258     {11, ARM_ARCH_V6M},
25259     {12, ARM_ARCH_V6SM},
25260     {8, ARM_ARCH_V6T2},
25261     {10, ARM_ARCH_V7VE},
25262     {10, ARM_ARCH_V7R},
25263     {10, ARM_ARCH_V7M},
25264     {14, ARM_ARCH_V8A},
25265     {0, ARM_ARCH_NONE}
25266 };
25267
25268 /* Set an attribute if it has not already been set by the user.  */
25269 static void
25270 aeabi_set_attribute_int (int tag, int value)
25271 {
25272   if (tag < 1
25273       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
25274       || !attributes_set_explicitly[tag])
25275     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
25276 }
25277
25278 static void
25279 aeabi_set_attribute_string (int tag, const char *value)
25280 {
25281   if (tag < 1
25282       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
25283       || !attributes_set_explicitly[tag])
25284     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
25285 }
25286
25287 /* Set the public EABI object attributes.  */
25288 void
25289 aeabi_set_public_attributes (void)
25290 {
25291   int arch;
25292   char profile;
25293   int virt_sec = 0;
25294   int fp16_optional = 0;
25295   arm_feature_set flags;
25296   arm_feature_set tmp;
25297   const cpu_arch_ver_table *p;
25298
25299   /* Choose the architecture based on the capabilities of the requested cpu
25300      (if any) and/or the instructions actually used.  */
25301   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
25302   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
25303   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
25304
25305   if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
25306     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
25307
25308   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
25309     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
25310
25311   selected_cpu = flags;
25312
25313   /* Allow the user to override the reported architecture.  */
25314   if (object_arch)
25315     {
25316       ARM_CLEAR_FEATURE (flags, flags, arm_arch_any);
25317       ARM_MERGE_FEATURE_SETS (flags, flags, *object_arch);
25318     }
25319
25320   /* We need to make sure that the attributes do not identify us as v6S-M
25321      when the only v6S-M feature in use is the Operating System Extensions.  */
25322   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_os))
25323       if (!ARM_CPU_HAS_FEATURE (flags, arm_arch_v6m_only))
25324         ARM_CLEAR_FEATURE (flags, flags, arm_ext_os);
25325
25326   tmp = flags;
25327   arch = 0;
25328   for (p = cpu_arch_ver; p->val; p++)
25329     {
25330       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
25331         {
25332           arch = p->val;
25333           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
25334         }
25335     }
25336
25337   /* The table lookup above finds the last architecture to contribute
25338      a new feature.  Unfortunately, Tag13 is a subset of the union of
25339      v6T2 and v7-M, so it is never seen as contributing a new feature.
25340      We can not search for the last entry which is entirely used,
25341      because if no CPU is specified we build up only those flags
25342      actually used.  Perhaps we should separate out the specified
25343      and implicit cases.  Avoid taking this path for -march=all by
25344      checking for contradictory v7-A / v7-M features.  */
25345   if (arch == 10
25346       && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
25347       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m)
25348       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v6_dsp))
25349     arch = 13;
25350
25351   /* Tag_CPU_name.  */
25352   if (selected_cpu_name[0])
25353     {
25354       char *q;
25355
25356       q = selected_cpu_name;
25357       if (strncmp (q, "armv", 4) == 0)
25358         {
25359           int i;
25360
25361           q += 4;
25362           for (i = 0; q[i]; i++)
25363             q[i] = TOUPPER (q[i]);
25364         }
25365       aeabi_set_attribute_string (Tag_CPU_name, q);
25366     }
25367
25368   /* Tag_CPU_arch.  */
25369   aeabi_set_attribute_int (Tag_CPU_arch, arch);
25370
25371   /* Tag_CPU_arch_profile.  */
25372   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a))
25373     profile = 'A';
25374   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
25375     profile = 'R';
25376   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_m))
25377     profile = 'M';
25378   else
25379     profile = '\0';
25380
25381   if (profile != '\0')
25382     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
25383
25384   /* Tag_ARM_ISA_use.  */
25385   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
25386       || arch == 0)
25387     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
25388
25389   /* Tag_THUMB_ISA_use.  */
25390   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
25391       || arch == 0)
25392     aeabi_set_attribute_int (Tag_THUMB_ISA_use,
25393         ARM_CPU_HAS_FEATURE (flags, arm_arch_t2) ? 2 : 1);
25394
25395   /* Tag_VFP_arch.  */
25396   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
25397     aeabi_set_attribute_int (Tag_VFP_arch,
25398                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
25399                              ? 7 : 8);
25400   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
25401     aeabi_set_attribute_int (Tag_VFP_arch,
25402                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
25403                              ? 5 : 6);
25404   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
25405     {
25406       fp16_optional = 1;
25407       aeabi_set_attribute_int (Tag_VFP_arch, 3);
25408     }
25409   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
25410     {
25411       aeabi_set_attribute_int (Tag_VFP_arch, 4);
25412       fp16_optional = 1;
25413     }
25414   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
25415     aeabi_set_attribute_int (Tag_VFP_arch, 2);
25416   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
25417            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
25418     aeabi_set_attribute_int (Tag_VFP_arch, 1);
25419
25420   /* Tag_ABI_HardFP_use.  */
25421   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
25422       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
25423     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
25424
25425   /* Tag_WMMX_arch.  */
25426   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
25427     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
25428   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
25429     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
25430
25431   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
25432   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
25433     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
25434   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
25435     {
25436       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
25437         {
25438           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
25439         }
25440       else
25441         {
25442           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
25443           fp16_optional = 1;
25444         }
25445     }
25446
25447   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
25448   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
25449     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
25450
25451   /* Tag_DIV_use.
25452
25453      We set Tag_DIV_use to two when integer divide instructions have been used
25454      in ARM state, or when Thumb integer divide instructions have been used,
25455      but we have no architecture profile set, nor have we any ARM instructions.
25456
25457      For ARMv8 we set the tag to 0 as integer divide is implied by the base
25458      architecture.
25459
25460      For new architectures we will have to check these tests.  */
25461   gas_assert (arch <= TAG_CPU_ARCH_V8);
25462   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8))
25463     aeabi_set_attribute_int (Tag_DIV_use, 0);
25464   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
25465            || (profile == '\0'
25466                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
25467                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
25468     aeabi_set_attribute_int (Tag_DIV_use, 2);
25469
25470   /* Tag_MP_extension_use.  */
25471   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
25472     aeabi_set_attribute_int (Tag_MPextension_use, 1);
25473
25474   /* Tag Virtualization_use.  */
25475   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
25476     virt_sec |= 1;
25477   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
25478     virt_sec |= 2;
25479   if (virt_sec != 0)
25480     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
25481 }
25482
25483 /* Add the default contents for the .ARM.attributes section.  */
25484 void
25485 arm_md_end (void)
25486 {
25487   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
25488     return;
25489
25490   aeabi_set_public_attributes ();
25491 }
25492 #endif /* OBJ_ELF */
25493
25494
25495 /* Parse a .cpu directive.  */
25496
25497 static void
25498 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
25499 {
25500   const struct arm_cpu_option_table *opt;
25501   char *name;
25502   char saved_char;
25503
25504   name = input_line_pointer;
25505   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25506     input_line_pointer++;
25507   saved_char = *input_line_pointer;
25508   *input_line_pointer = 0;
25509
25510   /* Skip the first "all" entry.  */
25511   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
25512     if (streq (opt->name, name))
25513       {
25514         mcpu_cpu_opt = &opt->value;
25515         selected_cpu = opt->value;
25516         if (opt->canonical_name)
25517           strcpy (selected_cpu_name, opt->canonical_name);
25518         else
25519           {
25520             int i;
25521             for (i = 0; opt->name[i]; i++)
25522               selected_cpu_name[i] = TOUPPER (opt->name[i]);
25523
25524             selected_cpu_name[i] = 0;
25525           }
25526         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25527         *input_line_pointer = saved_char;
25528         demand_empty_rest_of_line ();
25529         return;
25530       }
25531   as_bad (_("unknown cpu `%s'"), name);
25532   *input_line_pointer = saved_char;
25533   ignore_rest_of_line ();
25534 }
25535
25536
25537 /* Parse a .arch directive.  */
25538
25539 static void
25540 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
25541 {
25542   const struct arm_arch_option_table *opt;
25543   char saved_char;
25544   char *name;
25545
25546   name = input_line_pointer;
25547   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25548     input_line_pointer++;
25549   saved_char = *input_line_pointer;
25550   *input_line_pointer = 0;
25551
25552   /* Skip the first "all" entry.  */
25553   for (opt = arm_archs + 1; opt->name != NULL; opt++)
25554     if (streq (opt->name, name))
25555       {
25556         mcpu_cpu_opt = &opt->value;
25557         selected_cpu = opt->value;
25558         strcpy (selected_cpu_name, opt->name);
25559         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25560         *input_line_pointer = saved_char;
25561         demand_empty_rest_of_line ();
25562         return;
25563       }
25564
25565   as_bad (_("unknown architecture `%s'\n"), name);
25566   *input_line_pointer = saved_char;
25567   ignore_rest_of_line ();
25568 }
25569
25570
25571 /* Parse a .object_arch directive.  */
25572
25573 static void
25574 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
25575 {
25576   const struct arm_arch_option_table *opt;
25577   char saved_char;
25578   char *name;
25579
25580   name = input_line_pointer;
25581   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25582     input_line_pointer++;
25583   saved_char = *input_line_pointer;
25584   *input_line_pointer = 0;
25585
25586   /* Skip the first "all" entry.  */
25587   for (opt = arm_archs + 1; opt->name != NULL; opt++)
25588     if (streq (opt->name, name))
25589       {
25590         object_arch = &opt->value;
25591         *input_line_pointer = saved_char;
25592         demand_empty_rest_of_line ();
25593         return;
25594       }
25595
25596   as_bad (_("unknown architecture `%s'\n"), name);
25597   *input_line_pointer = saved_char;
25598   ignore_rest_of_line ();
25599 }
25600
25601 /* Parse a .arch_extension directive.  */
25602
25603 static void
25604 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
25605 {
25606   const struct arm_option_extension_value_table *opt;
25607   char saved_char;
25608   char *name;
25609   int adding_value = 1;
25610
25611   name = input_line_pointer;
25612   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25613     input_line_pointer++;
25614   saved_char = *input_line_pointer;
25615   *input_line_pointer = 0;
25616
25617   if (strlen (name) >= 2
25618       && strncmp (name, "no", 2) == 0)
25619     {
25620       adding_value = 0;
25621       name += 2;
25622     }
25623
25624   for (opt = arm_extensions; opt->name != NULL; opt++)
25625     if (streq (opt->name, name))
25626       {
25627         if (!ARM_CPU_HAS_FEATURE (*mcpu_cpu_opt, opt->allowed_archs))
25628           {
25629             as_bad (_("architectural extension `%s' is not allowed for the "
25630                       "current base architecture"), name);
25631             break;
25632           }
25633
25634         if (adding_value)
25635           ARM_MERGE_FEATURE_SETS (selected_cpu, selected_cpu,
25636                                   opt->merge_value);
25637         else
25638           ARM_CLEAR_FEATURE (selected_cpu, selected_cpu, opt->clear_value);
25639
25640         mcpu_cpu_opt = &selected_cpu;
25641         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25642         *input_line_pointer = saved_char;
25643         demand_empty_rest_of_line ();
25644         return;
25645       }
25646
25647   if (opt->name == NULL)
25648     as_bad (_("unknown architecture extension `%s'\n"), name);
25649
25650   *input_line_pointer = saved_char;
25651   ignore_rest_of_line ();
25652 }
25653
25654 /* Parse a .fpu directive.  */
25655
25656 static void
25657 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
25658 {
25659   const struct arm_option_fpu_value_table *opt;
25660   char saved_char;
25661   char *name;
25662
25663   name = input_line_pointer;
25664   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25665     input_line_pointer++;
25666   saved_char = *input_line_pointer;
25667   *input_line_pointer = 0;
25668
25669   for (opt = arm_fpus; opt->name != NULL; opt++)
25670     if (streq (opt->name, name))
25671       {
25672         mfpu_opt = &opt->value;
25673         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25674         *input_line_pointer = saved_char;
25675         demand_empty_rest_of_line ();
25676         return;
25677       }
25678
25679   as_bad (_("unknown floating point format `%s'\n"), name);
25680   *input_line_pointer = saved_char;
25681   ignore_rest_of_line ();
25682 }
25683
25684 /* Copy symbol information.  */
25685
25686 void
25687 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
25688 {
25689   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
25690 }
25691
25692 #ifdef OBJ_ELF
25693 /* Given a symbolic attribute NAME, return the proper integer value.
25694    Returns -1 if the attribute is not known.  */
25695
25696 int
25697 arm_convert_symbolic_attribute (const char *name)
25698 {
25699   static const struct
25700   {
25701     const char * name;
25702     const int    tag;
25703   }
25704   attribute_table[] =
25705     {
25706       /* When you modify this table you should
25707          also modify the list in doc/c-arm.texi.  */
25708 #define T(tag) {#tag, tag}
25709       T (Tag_CPU_raw_name),
25710       T (Tag_CPU_name),
25711       T (Tag_CPU_arch),
25712       T (Tag_CPU_arch_profile),
25713       T (Tag_ARM_ISA_use),
25714       T (Tag_THUMB_ISA_use),
25715       T (Tag_FP_arch),
25716       T (Tag_VFP_arch),
25717       T (Tag_WMMX_arch),
25718       T (Tag_Advanced_SIMD_arch),
25719       T (Tag_PCS_config),
25720       T (Tag_ABI_PCS_R9_use),
25721       T (Tag_ABI_PCS_RW_data),
25722       T (Tag_ABI_PCS_RO_data),
25723       T (Tag_ABI_PCS_GOT_use),
25724       T (Tag_ABI_PCS_wchar_t),
25725       T (Tag_ABI_FP_rounding),
25726       T (Tag_ABI_FP_denormal),
25727       T (Tag_ABI_FP_exceptions),
25728       T (Tag_ABI_FP_user_exceptions),
25729       T (Tag_ABI_FP_number_model),
25730       T (Tag_ABI_align_needed),
25731       T (Tag_ABI_align8_needed),
25732       T (Tag_ABI_align_preserved),
25733       T (Tag_ABI_align8_preserved),
25734       T (Tag_ABI_enum_size),
25735       T (Tag_ABI_HardFP_use),
25736       T (Tag_ABI_VFP_args),
25737       T (Tag_ABI_WMMX_args),
25738       T (Tag_ABI_optimization_goals),
25739       T (Tag_ABI_FP_optimization_goals),
25740       T (Tag_compatibility),
25741       T (Tag_CPU_unaligned_access),
25742       T (Tag_FP_HP_extension),
25743       T (Tag_VFP_HP_extension),
25744       T (Tag_ABI_FP_16bit_format),
25745       T (Tag_MPextension_use),
25746       T (Tag_DIV_use),
25747       T (Tag_nodefaults),
25748       T (Tag_also_compatible_with),
25749       T (Tag_conformance),
25750       T (Tag_T2EE_use),
25751       T (Tag_Virtualization_use),
25752       /* We deliberately do not include Tag_MPextension_use_legacy.  */
25753 #undef T
25754     };
25755   unsigned int i;
25756
25757   if (name == NULL)
25758     return -1;
25759
25760   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
25761     if (streq (name, attribute_table[i].name))
25762       return attribute_table[i].tag;
25763
25764   return -1;
25765 }
25766
25767
25768 /* Apply sym value for relocations only in the case that they are for
25769    local symbols in the same segment as the fixup and you have the
25770    respective architectural feature for blx and simple switches.  */
25771 int
25772 arm_apply_sym_value (struct fix * fixP, segT this_seg)
25773 {
25774   if (fixP->fx_addsy
25775       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
25776       /* PR 17444: If the local symbol is in a different section then a reloc
25777          will always be generated for it, so applying the symbol value now
25778          will result in a double offset being stored in the relocation.  */
25779       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
25780       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
25781     {
25782       switch (fixP->fx_r_type)
25783         {
25784         case BFD_RELOC_ARM_PCREL_BLX:
25785         case BFD_RELOC_THUMB_PCREL_BRANCH23:
25786           if (ARM_IS_FUNC (fixP->fx_addsy))
25787             return 1;
25788           break;
25789
25790         case BFD_RELOC_ARM_PCREL_CALL:
25791         case BFD_RELOC_THUMB_PCREL_BLX:
25792           if (THUMB_IS_FUNC (fixP->fx_addsy))
25793             return 1;
25794           break;
25795
25796         default:
25797           break;
25798         }
25799
25800     }
25801   return 0;
25802 }
25803 #endif /* OBJ_ELF */