Fix snafu.
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright 1994-2013 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
141 /* Variables that we set while parsing command-line options.  Once all
142    options have been read we re-process these values to set the real
143    assembly flags.  */
144 static const arm_feature_set *legacy_cpu = NULL;
145 static const arm_feature_set *legacy_fpu = NULL;
146
147 static const arm_feature_set *mcpu_cpu_opt = NULL;
148 static const arm_feature_set *mcpu_fpu_opt = NULL;
149 static const arm_feature_set *march_cpu_opt = NULL;
150 static const arm_feature_set *march_fpu_opt = NULL;
151 static const arm_feature_set *mfpu_opt = NULL;
152 static const arm_feature_set *object_arch = NULL;
153
154 /* Constants for known architecture features.  */
155 static const arm_feature_set fpu_default = FPU_DEFAULT;
156 static const arm_feature_set fpu_arch_vfp_v1 = FPU_ARCH_VFP_V1;
157 static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
158 static const arm_feature_set fpu_arch_vfp_v3 = FPU_ARCH_VFP_V3;
159 static const arm_feature_set fpu_arch_neon_v1 = FPU_ARCH_NEON_V1;
160 static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
161 static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
162 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
163 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
164
165 #ifdef CPU_DEFAULT
166 static const arm_feature_set cpu_default = CPU_DEFAULT;
167 #endif
168
169 static const arm_feature_set arm_ext_v1 = ARM_FEATURE (ARM_EXT_V1, 0);
170 static const arm_feature_set arm_ext_v2 = ARM_FEATURE (ARM_EXT_V1, 0);
171 static const arm_feature_set arm_ext_v2s = ARM_FEATURE (ARM_EXT_V2S, 0);
172 static const arm_feature_set arm_ext_v3 = ARM_FEATURE (ARM_EXT_V3, 0);
173 static const arm_feature_set arm_ext_v3m = ARM_FEATURE (ARM_EXT_V3M, 0);
174 static const arm_feature_set arm_ext_v4 = ARM_FEATURE (ARM_EXT_V4, 0);
175 static const arm_feature_set arm_ext_v4t = ARM_FEATURE (ARM_EXT_V4T, 0);
176 static const arm_feature_set arm_ext_v5 = ARM_FEATURE (ARM_EXT_V5, 0);
177 static const arm_feature_set arm_ext_v4t_5 =
178   ARM_FEATURE (ARM_EXT_V4T | ARM_EXT_V5, 0);
179 static const arm_feature_set arm_ext_v5t = ARM_FEATURE (ARM_EXT_V5T, 0);
180 static const arm_feature_set arm_ext_v5e = ARM_FEATURE (ARM_EXT_V5E, 0);
181 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE (ARM_EXT_V5ExP, 0);
182 static const arm_feature_set arm_ext_v5j = ARM_FEATURE (ARM_EXT_V5J, 0);
183 static const arm_feature_set arm_ext_v6 = ARM_FEATURE (ARM_EXT_V6, 0);
184 static const arm_feature_set arm_ext_v6k = ARM_FEATURE (ARM_EXT_V6K, 0);
185 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE (ARM_EXT_V6T2, 0);
186 static const arm_feature_set arm_ext_v6m = ARM_FEATURE (ARM_EXT_V6M, 0);
187 static const arm_feature_set arm_ext_v6_notm = ARM_FEATURE (ARM_EXT_V6_NOTM, 0);
188 static const arm_feature_set arm_ext_v6_dsp = ARM_FEATURE (ARM_EXT_V6_DSP, 0);
189 static const arm_feature_set arm_ext_barrier = ARM_FEATURE (ARM_EXT_BARRIER, 0);
190 static const arm_feature_set arm_ext_msr = ARM_FEATURE (ARM_EXT_THUMB_MSR, 0);
191 static const arm_feature_set arm_ext_div = ARM_FEATURE (ARM_EXT_DIV, 0);
192 static const arm_feature_set arm_ext_v7 = ARM_FEATURE (ARM_EXT_V7, 0);
193 static const arm_feature_set arm_ext_v7a = ARM_FEATURE (ARM_EXT_V7A, 0);
194 static const arm_feature_set arm_ext_v7r = ARM_FEATURE (ARM_EXT_V7R, 0);
195 static const arm_feature_set arm_ext_v7m = ARM_FEATURE (ARM_EXT_V7M, 0);
196 static const arm_feature_set arm_ext_v8 = ARM_FEATURE (ARM_EXT_V8, 0);
197 static const arm_feature_set arm_ext_m =
198   ARM_FEATURE (ARM_EXT_V6M | ARM_EXT_OS | ARM_EXT_V7M, 0);
199 static const arm_feature_set arm_ext_mp = ARM_FEATURE (ARM_EXT_MP, 0);
200 static const arm_feature_set arm_ext_sec = ARM_FEATURE (ARM_EXT_SEC, 0);
201 static const arm_feature_set arm_ext_os = ARM_FEATURE (ARM_EXT_OS, 0);
202 static const arm_feature_set arm_ext_adiv = ARM_FEATURE (ARM_EXT_ADIV, 0);
203 static const arm_feature_set arm_ext_virt = ARM_FEATURE (ARM_EXT_VIRT, 0);
204
205 static const arm_feature_set arm_arch_any = ARM_ANY;
206 static const arm_feature_set arm_arch_full = ARM_FEATURE (-1, -1);
207 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
208 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
209 static const arm_feature_set arm_arch_v6m_only = ARM_ARCH_V6M_ONLY;
210
211 static const arm_feature_set arm_cext_iwmmxt2 =
212   ARM_FEATURE (0, ARM_CEXT_IWMMXT2);
213 static const arm_feature_set arm_cext_iwmmxt =
214   ARM_FEATURE (0, ARM_CEXT_IWMMXT);
215 static const arm_feature_set arm_cext_xscale =
216   ARM_FEATURE (0, ARM_CEXT_XSCALE);
217 static const arm_feature_set arm_cext_maverick =
218   ARM_FEATURE (0, ARM_CEXT_MAVERICK);
219 static const arm_feature_set fpu_fpa_ext_v1 = ARM_FEATURE (0, FPU_FPA_EXT_V1);
220 static const arm_feature_set fpu_fpa_ext_v2 = ARM_FEATURE (0, FPU_FPA_EXT_V2);
221 static const arm_feature_set fpu_vfp_ext_v1xd =
222   ARM_FEATURE (0, FPU_VFP_EXT_V1xD);
223 static const arm_feature_set fpu_vfp_ext_v1 = ARM_FEATURE (0, FPU_VFP_EXT_V1);
224 static const arm_feature_set fpu_vfp_ext_v2 = ARM_FEATURE (0, FPU_VFP_EXT_V2);
225 static const arm_feature_set fpu_vfp_ext_v3xd = ARM_FEATURE (0, FPU_VFP_EXT_V3xD);
226 static const arm_feature_set fpu_vfp_ext_v3 = ARM_FEATURE (0, FPU_VFP_EXT_V3);
227 static const arm_feature_set fpu_vfp_ext_d32 =
228   ARM_FEATURE (0, FPU_VFP_EXT_D32);
229 static const arm_feature_set fpu_neon_ext_v1 = ARM_FEATURE (0, FPU_NEON_EXT_V1);
230 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
231   ARM_FEATURE (0, FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
232 static const arm_feature_set fpu_vfp_fp16 = ARM_FEATURE (0, FPU_VFP_EXT_FP16);
233 static const arm_feature_set fpu_neon_ext_fma = ARM_FEATURE (0, FPU_NEON_EXT_FMA);
234 static const arm_feature_set fpu_vfp_ext_fma = ARM_FEATURE (0, FPU_VFP_EXT_FMA);
235 static const arm_feature_set fpu_vfp_ext_armv8 =
236   ARM_FEATURE (0, FPU_VFP_EXT_ARMV8);
237 static const arm_feature_set fpu_neon_ext_armv8 =
238   ARM_FEATURE (0, FPU_NEON_EXT_ARMV8);
239 static const arm_feature_set fpu_crypto_ext_armv8 =
240   ARM_FEATURE (0, FPU_CRYPTO_EXT_ARMV8);
241
242 static int mfloat_abi_opt = -1;
243 /* Record user cpu selection for object attributes.  */
244 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
245 /* Must be long enough to hold any of the names in arm_cpus.  */
246 static char selected_cpu_name[16];
247
248 /* Return if no cpu was selected on command-line.  */
249 static bfd_boolean
250 no_cpu_selected (void)
251 {
252   return selected_cpu.core == arm_arch_none.core
253     && selected_cpu.coproc == arm_arch_none.coproc;
254 }
255
256 #ifdef OBJ_ELF
257 # ifdef EABI_DEFAULT
258 static int meabi_flags = EABI_DEFAULT;
259 # else
260 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
261 # endif
262
263 static int attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
264
265 bfd_boolean
266 arm_is_eabi (void)
267 {
268   return (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4);
269 }
270 #endif
271
272 #ifdef OBJ_ELF
273 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
274 symbolS * GOT_symbol;
275 #endif
276
277 /* 0: assemble for ARM,
278    1: assemble for Thumb,
279    2: assemble for Thumb even though target CPU does not support thumb
280       instructions.  */
281 static int thumb_mode = 0;
282 /* A value distinct from the possible values for thumb_mode that we
283    can use to record whether thumb_mode has been copied into the
284    tc_frag_data field of a frag.  */
285 #define MODE_RECORDED (1 << 4)
286
287 /* Specifies the intrinsic IT insn behavior mode.  */
288 enum implicit_it_mode
289 {
290   IMPLICIT_IT_MODE_NEVER  = 0x00,
291   IMPLICIT_IT_MODE_ARM    = 0x01,
292   IMPLICIT_IT_MODE_THUMB  = 0x02,
293   IMPLICIT_IT_MODE_ALWAYS = (IMPLICIT_IT_MODE_ARM | IMPLICIT_IT_MODE_THUMB)
294 };
295 static int implicit_it_mode = IMPLICIT_IT_MODE_ARM;
296
297 /* If unified_syntax is true, we are processing the new unified
298    ARM/Thumb syntax.  Important differences from the old ARM mode:
299
300      - Immediate operands do not require a # prefix.
301      - Conditional affixes always appear at the end of the
302        instruction.  (For backward compatibility, those instructions
303        that formerly had them in the middle, continue to accept them
304        there.)
305      - The IT instruction may appear, and if it does is validated
306        against subsequent conditional affixes.  It does not generate
307        machine code.
308
309    Important differences from the old Thumb mode:
310
311      - Immediate operands do not require a # prefix.
312      - Most of the V6T2 instructions are only available in unified mode.
313      - The .N and .W suffixes are recognized and honored (it is an error
314        if they cannot be honored).
315      - All instructions set the flags if and only if they have an 's' affix.
316      - Conditional affixes may be used.  They are validated against
317        preceding IT instructions.  Unlike ARM mode, you cannot use a
318        conditional affix except in the scope of an IT instruction.  */
319
320 static bfd_boolean unified_syntax = FALSE;
321
322 /* An immediate operand can start with #, and ld*, st*, pld operands
323    can contain [ and ].  We need to tell APP not to elide whitespace
324    before a [, which can appear as the first operand for pld.  */
325 const char arm_symbol_chars[] = "#[]";
326
327 enum neon_el_type
328 {
329   NT_invtype,
330   NT_untyped,
331   NT_integer,
332   NT_float,
333   NT_poly,
334   NT_signed,
335   NT_unsigned
336 };
337
338 struct neon_type_el
339 {
340   enum neon_el_type type;
341   unsigned size;
342 };
343
344 #define NEON_MAX_TYPE_ELS 4
345
346 struct neon_type
347 {
348   struct neon_type_el el[NEON_MAX_TYPE_ELS];
349   unsigned elems;
350 };
351
352 enum it_instruction_type
353 {
354    OUTSIDE_IT_INSN,
355    INSIDE_IT_INSN,
356    INSIDE_IT_LAST_INSN,
357    IF_INSIDE_IT_LAST_INSN, /* Either outside or inside;
358                               if inside, should be the last one.  */
359    NEUTRAL_IT_INSN,        /* This could be either inside or outside,
360                               i.e. BKPT and NOP.  */
361    IT_INSN                 /* The IT insn has been parsed.  */
362 };
363
364 /* The maximum number of operands we need.  */
365 #define ARM_IT_MAX_OPERANDS 6
366
367 struct arm_it
368 {
369   const char *  error;
370   unsigned long instruction;
371   int           size;
372   int           size_req;
373   int           cond;
374   /* "uncond_value" is set to the value in place of the conditional field in
375      unconditional versions of the instruction, or -1 if nothing is
376      appropriate.  */
377   int           uncond_value;
378   struct neon_type vectype;
379   /* This does not indicate an actual NEON instruction, only that
380      the mnemonic accepts neon-style type suffixes.  */
381   int           is_neon;
382   /* Set to the opcode if the instruction needs relaxation.
383      Zero if the instruction is not relaxed.  */
384   unsigned long relax;
385   struct
386   {
387     bfd_reloc_code_real_type type;
388     expressionS              exp;
389     int                      pc_rel;
390   } reloc;
391
392   enum it_instruction_type it_insn_type;
393
394   struct
395   {
396     unsigned reg;
397     signed int imm;
398     struct neon_type_el vectype;
399     unsigned present    : 1;  /* Operand present.  */
400     unsigned isreg      : 1;  /* Operand was a register.  */
401     unsigned immisreg   : 1;  /* .imm field is a second register.  */
402     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
403     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
404     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
405     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
406        instructions. This allows us to disambiguate ARM <-> vector insns.  */
407     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
408     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
409     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
410     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
411     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
412     unsigned writeback  : 1;  /* Operand has trailing !  */
413     unsigned preind     : 1;  /* Preindexed address.  */
414     unsigned postind    : 1;  /* Postindexed address.  */
415     unsigned negative   : 1;  /* Index register was negated.  */
416     unsigned shifted    : 1;  /* Shift applied to operation.  */
417     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
418   } operands[ARM_IT_MAX_OPERANDS];
419 };
420
421 static struct arm_it inst;
422
423 #define NUM_FLOAT_VALS 8
424
425 const char * fp_const[] =
426 {
427   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
428 };
429
430 /* Number of littlenums required to hold an extended precision number.  */
431 #define MAX_LITTLENUMS 6
432
433 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
434
435 #define FAIL    (-1)
436 #define SUCCESS (0)
437
438 #define SUFF_S 1
439 #define SUFF_D 2
440 #define SUFF_E 3
441 #define SUFF_P 4
442
443 #define CP_T_X   0x00008000
444 #define CP_T_Y   0x00400000
445
446 #define CONDS_BIT        0x00100000
447 #define LOAD_BIT         0x00100000
448
449 #define DOUBLE_LOAD_FLAG 0x00000001
450
451 struct asm_cond
452 {
453   const char *   template_name;
454   unsigned long  value;
455 };
456
457 #define COND_ALWAYS 0xE
458
459 struct asm_psr
460 {
461   const char *   template_name;
462   unsigned long  field;
463 };
464
465 struct asm_barrier_opt
466 {
467   const char *    template_name;
468   unsigned long   value;
469   const arm_feature_set arch;
470 };
471
472 /* The bit that distinguishes CPSR and SPSR.  */
473 #define SPSR_BIT   (1 << 22)
474
475 /* The individual PSR flag bits.  */
476 #define PSR_c   (1 << 16)
477 #define PSR_x   (1 << 17)
478 #define PSR_s   (1 << 18)
479 #define PSR_f   (1 << 19)
480
481 struct reloc_entry
482 {
483   char *                    name;
484   bfd_reloc_code_real_type  reloc;
485 };
486
487 enum vfp_reg_pos
488 {
489   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
490   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
491 };
492
493 enum vfp_ldstm_type
494 {
495   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
496 };
497
498 /* Bits for DEFINED field in neon_typed_alias.  */
499 #define NTA_HASTYPE  1
500 #define NTA_HASINDEX 2
501
502 struct neon_typed_alias
503 {
504   unsigned char        defined;
505   unsigned char        index;
506   struct neon_type_el  eltype;
507 };
508
509 /* ARM register categories.  This includes coprocessor numbers and various
510    architecture extensions' registers.  */
511 enum arm_reg_type
512 {
513   REG_TYPE_RN,
514   REG_TYPE_CP,
515   REG_TYPE_CN,
516   REG_TYPE_FN,
517   REG_TYPE_VFS,
518   REG_TYPE_VFD,
519   REG_TYPE_NQ,
520   REG_TYPE_VFSD,
521   REG_TYPE_NDQ,
522   REG_TYPE_NSDQ,
523   REG_TYPE_VFC,
524   REG_TYPE_MVF,
525   REG_TYPE_MVD,
526   REG_TYPE_MVFX,
527   REG_TYPE_MVDX,
528   REG_TYPE_MVAX,
529   REG_TYPE_DSPSC,
530   REG_TYPE_MMXWR,
531   REG_TYPE_MMXWC,
532   REG_TYPE_MMXWCG,
533   REG_TYPE_XSCALE,
534   REG_TYPE_RNB
535 };
536
537 /* Structure for a hash table entry for a register.
538    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
539    information which states whether a vector type or index is specified (for a
540    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
541 struct reg_entry
542 {
543   const char *               name;
544   unsigned int               number;
545   unsigned char              type;
546   unsigned char              builtin;
547   struct neon_typed_alias *  neon;
548 };
549
550 /* Diagnostics used when we don't get a register of the expected type.  */
551 const char * const reg_expected_msgs[] =
552 {
553   N_("ARM register expected"),
554   N_("bad or missing co-processor number"),
555   N_("co-processor register expected"),
556   N_("FPA register expected"),
557   N_("VFP single precision register expected"),
558   N_("VFP/Neon double precision register expected"),
559   N_("Neon quad precision register expected"),
560   N_("VFP single or double precision register expected"),
561   N_("Neon double or quad precision register expected"),
562   N_("VFP single, double or Neon quad precision register expected"),
563   N_("VFP system register expected"),
564   N_("Maverick MVF register expected"),
565   N_("Maverick MVD register expected"),
566   N_("Maverick MVFX register expected"),
567   N_("Maverick MVDX register expected"),
568   N_("Maverick MVAX register expected"),
569   N_("Maverick DSPSC register expected"),
570   N_("iWMMXt data register expected"),
571   N_("iWMMXt control register expected"),
572   N_("iWMMXt scalar register expected"),
573   N_("XScale accumulator register expected"),
574 };
575
576 /* Some well known registers that we refer to directly elsewhere.  */
577 #define REG_R12 12
578 #define REG_SP  13
579 #define REG_LR  14
580 #define REG_PC  15
581
582 /* ARM instructions take 4bytes in the object file, Thumb instructions
583    take 2:  */
584 #define INSN_SIZE       4
585
586 struct asm_opcode
587 {
588   /* Basic string to match.  */
589   const char * template_name;
590
591   /* Parameters to instruction.  */
592   unsigned int operands[8];
593
594   /* Conditional tag - see opcode_lookup.  */
595   unsigned int tag : 4;
596
597   /* Basic instruction code.  */
598   unsigned int avalue : 28;
599
600   /* Thumb-format instruction code.  */
601   unsigned int tvalue;
602
603   /* Which architecture variant provides this instruction.  */
604   const arm_feature_set * avariant;
605   const arm_feature_set * tvariant;
606
607   /* Function to call to encode instruction in ARM format.  */
608   void (* aencode) (void);
609
610   /* Function to call to encode instruction in Thumb format.  */
611   void (* tencode) (void);
612 };
613
614 /* Defines for various bits that we will want to toggle.  */
615 #define INST_IMMEDIATE  0x02000000
616 #define OFFSET_REG      0x02000000
617 #define HWOFFSET_IMM    0x00400000
618 #define SHIFT_BY_REG    0x00000010
619 #define PRE_INDEX       0x01000000
620 #define INDEX_UP        0x00800000
621 #define WRITE_BACK      0x00200000
622 #define LDM_TYPE_2_OR_3 0x00400000
623 #define CPSI_MMOD       0x00020000
624
625 #define LITERAL_MASK    0xf000f000
626 #define OPCODE_MASK     0xfe1fffff
627 #define V4_STR_BIT      0x00000020
628
629 #define T2_SUBS_PC_LR   0xf3de8f00
630
631 #define DATA_OP_SHIFT   21
632
633 #define T2_OPCODE_MASK  0xfe1fffff
634 #define T2_DATA_OP_SHIFT 21
635
636 #define A_COND_MASK         0xf0000000
637 #define A_PUSH_POP_OP_MASK  0x0fff0000
638
639 /* Opcodes for pushing/poping registers to/from the stack.  */
640 #define A1_OPCODE_PUSH    0x092d0000
641 #define A2_OPCODE_PUSH    0x052d0004
642 #define A2_OPCODE_POP     0x049d0004
643
644 /* Codes to distinguish the arithmetic instructions.  */
645 #define OPCODE_AND      0
646 #define OPCODE_EOR      1
647 #define OPCODE_SUB      2
648 #define OPCODE_RSB      3
649 #define OPCODE_ADD      4
650 #define OPCODE_ADC      5
651 #define OPCODE_SBC      6
652 #define OPCODE_RSC      7
653 #define OPCODE_TST      8
654 #define OPCODE_TEQ      9
655 #define OPCODE_CMP      10
656 #define OPCODE_CMN      11
657 #define OPCODE_ORR      12
658 #define OPCODE_MOV      13
659 #define OPCODE_BIC      14
660 #define OPCODE_MVN      15
661
662 #define T2_OPCODE_AND   0
663 #define T2_OPCODE_BIC   1
664 #define T2_OPCODE_ORR   2
665 #define T2_OPCODE_ORN   3
666 #define T2_OPCODE_EOR   4
667 #define T2_OPCODE_ADD   8
668 #define T2_OPCODE_ADC   10
669 #define T2_OPCODE_SBC   11
670 #define T2_OPCODE_SUB   13
671 #define T2_OPCODE_RSB   14
672
673 #define T_OPCODE_MUL 0x4340
674 #define T_OPCODE_TST 0x4200
675 #define T_OPCODE_CMN 0x42c0
676 #define T_OPCODE_NEG 0x4240
677 #define T_OPCODE_MVN 0x43c0
678
679 #define T_OPCODE_ADD_R3 0x1800
680 #define T_OPCODE_SUB_R3 0x1a00
681 #define T_OPCODE_ADD_HI 0x4400
682 #define T_OPCODE_ADD_ST 0xb000
683 #define T_OPCODE_SUB_ST 0xb080
684 #define T_OPCODE_ADD_SP 0xa800
685 #define T_OPCODE_ADD_PC 0xa000
686 #define T_OPCODE_ADD_I8 0x3000
687 #define T_OPCODE_SUB_I8 0x3800
688 #define T_OPCODE_ADD_I3 0x1c00
689 #define T_OPCODE_SUB_I3 0x1e00
690
691 #define T_OPCODE_ASR_R  0x4100
692 #define T_OPCODE_LSL_R  0x4080
693 #define T_OPCODE_LSR_R  0x40c0
694 #define T_OPCODE_ROR_R  0x41c0
695 #define T_OPCODE_ASR_I  0x1000
696 #define T_OPCODE_LSL_I  0x0000
697 #define T_OPCODE_LSR_I  0x0800
698
699 #define T_OPCODE_MOV_I8 0x2000
700 #define T_OPCODE_CMP_I8 0x2800
701 #define T_OPCODE_CMP_LR 0x4280
702 #define T_OPCODE_MOV_HR 0x4600
703 #define T_OPCODE_CMP_HR 0x4500
704
705 #define T_OPCODE_LDR_PC 0x4800
706 #define T_OPCODE_LDR_SP 0x9800
707 #define T_OPCODE_STR_SP 0x9000
708 #define T_OPCODE_LDR_IW 0x6800
709 #define T_OPCODE_STR_IW 0x6000
710 #define T_OPCODE_LDR_IH 0x8800
711 #define T_OPCODE_STR_IH 0x8000
712 #define T_OPCODE_LDR_IB 0x7800
713 #define T_OPCODE_STR_IB 0x7000
714 #define T_OPCODE_LDR_RW 0x5800
715 #define T_OPCODE_STR_RW 0x5000
716 #define T_OPCODE_LDR_RH 0x5a00
717 #define T_OPCODE_STR_RH 0x5200
718 #define T_OPCODE_LDR_RB 0x5c00
719 #define T_OPCODE_STR_RB 0x5400
720
721 #define T_OPCODE_PUSH   0xb400
722 #define T_OPCODE_POP    0xbc00
723
724 #define T_OPCODE_BRANCH 0xe000
725
726 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
727 #define THUMB_PP_PC_LR 0x0100
728 #define THUMB_LOAD_BIT 0x0800
729 #define THUMB2_LOAD_BIT 0x00100000
730
731 #define BAD_ARGS        _("bad arguments to instruction")
732 #define BAD_SP          _("r13 not allowed here")
733 #define BAD_PC          _("r15 not allowed here")
734 #define BAD_COND        _("instruction cannot be conditional")
735 #define BAD_OVERLAP     _("registers may not be the same")
736 #define BAD_HIREG       _("lo register required")
737 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
738 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
739 #define BAD_BRANCH      _("branch must be last instruction in IT block")
740 #define BAD_NOT_IT      _("instruction not allowed in IT block")
741 #define BAD_FPU         _("selected FPU does not support instruction")
742 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
743 #define BAD_IT_COND     _("incorrect condition in IT block")
744 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
745 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
746 #define BAD_PC_ADDRESSING \
747         _("cannot use register index with PC-relative addressing")
748 #define BAD_PC_WRITEBACK \
749         _("cannot use writeback with PC-relative addressing")
750 #define BAD_RANGE     _("branch out of range")
751
752 static struct hash_control * arm_ops_hsh;
753 static struct hash_control * arm_cond_hsh;
754 static struct hash_control * arm_shift_hsh;
755 static struct hash_control * arm_psr_hsh;
756 static struct hash_control * arm_v7m_psr_hsh;
757 static struct hash_control * arm_reg_hsh;
758 static struct hash_control * arm_reloc_hsh;
759 static struct hash_control * arm_barrier_opt_hsh;
760
761 /* Stuff needed to resolve the label ambiguity
762    As:
763      ...
764      label:   <insn>
765    may differ from:
766      ...
767      label:
768               <insn>  */
769
770 symbolS *  last_label_seen;
771 static int label_is_thumb_function_name = FALSE;
772
773 /* Literal pool structure.  Held on a per-section
774    and per-sub-section basis.  */
775
776 #define MAX_LITERAL_POOL_SIZE 1024
777 typedef struct literal_pool
778 {
779   expressionS            literals [MAX_LITERAL_POOL_SIZE];
780   unsigned int           next_free_entry;
781   unsigned int           id;
782   symbolS *              symbol;
783   segT                   section;
784   subsegT                sub_section;
785 #ifdef OBJ_ELF
786   struct dwarf2_line_info locs [MAX_LITERAL_POOL_SIZE];
787 #endif
788   struct literal_pool *  next;
789 } literal_pool;
790
791 /* Pointer to a linked list of literal pools.  */
792 literal_pool * list_of_pools = NULL;
793
794 #ifdef OBJ_ELF
795 #  define now_it seg_info (now_seg)->tc_segment_info_data.current_it
796 #else
797 static struct current_it now_it;
798 #endif
799
800 static inline int
801 now_it_compatible (int cond)
802 {
803   return (cond & ~1) == (now_it.cc & ~1);
804 }
805
806 static inline int
807 conditional_insn (void)
808 {
809   return inst.cond != COND_ALWAYS;
810 }
811
812 static int in_it_block (void);
813
814 static int handle_it_state (void);
815
816 static void force_automatic_it_block_close (void);
817
818 static void it_fsm_post_encode (void);
819
820 #define set_it_insn_type(type)                  \
821   do                                            \
822     {                                           \
823       inst.it_insn_type = type;                 \
824       if (handle_it_state () == FAIL)           \
825         return;                                 \
826     }                                           \
827   while (0)
828
829 #define set_it_insn_type_nonvoid(type, failret) \
830   do                                            \
831     {                                           \
832       inst.it_insn_type = type;                 \
833       if (handle_it_state () == FAIL)           \
834         return failret;                         \
835     }                                           \
836   while(0)
837
838 #define set_it_insn_type_last()                         \
839   do                                                    \
840     {                                                   \
841       if (inst.cond == COND_ALWAYS)                     \
842         set_it_insn_type (IF_INSIDE_IT_LAST_INSN);      \
843       else                                              \
844         set_it_insn_type (INSIDE_IT_LAST_INSN);         \
845     }                                                   \
846   while (0)
847
848 /* Pure syntax.  */
849
850 /* This array holds the chars that always start a comment.  If the
851    pre-processor is disabled, these aren't very useful.  */
852 const char comment_chars[] = "@";
853
854 /* This array holds the chars that only start a comment at the beginning of
855    a line.  If the line seems to have the form '# 123 filename'
856    .line and .file directives will appear in the pre-processed output.  */
857 /* Note that input_file.c hand checks for '#' at the beginning of the
858    first line of the input file.  This is because the compiler outputs
859    #NO_APP at the beginning of its output.  */
860 /* Also note that comments like this one will always work.  */
861 const char line_comment_chars[] = "#";
862
863 const char line_separator_chars[] = ";";
864
865 /* Chars that can be used to separate mant
866    from exp in floating point numbers.  */
867 const char EXP_CHARS[] = "eE";
868
869 /* Chars that mean this number is a floating point constant.  */
870 /* As in 0f12.456  */
871 /* or    0d1.2345e12  */
872
873 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
874
875 /* Prefix characters that indicate the start of an immediate
876    value.  */
877 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
878
879 /* Separator character handling.  */
880
881 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
882
883 static inline int
884 skip_past_char (char ** str, char c)
885 {
886   /* PR gas/14987: Allow for whitespace before the expected character.  */
887   skip_whitespace (*str);
888
889   if (**str == c)
890     {
891       (*str)++;
892       return SUCCESS;
893     }
894   else
895     return FAIL;
896 }
897
898 #define skip_past_comma(str) skip_past_char (str, ',')
899
900 /* Arithmetic expressions (possibly involving symbols).  */
901
902 /* Return TRUE if anything in the expression is a bignum.  */
903
904 static int
905 walk_no_bignums (symbolS * sp)
906 {
907   if (symbol_get_value_expression (sp)->X_op == O_big)
908     return 1;
909
910   if (symbol_get_value_expression (sp)->X_add_symbol)
911     {
912       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
913               || (symbol_get_value_expression (sp)->X_op_symbol
914                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
915     }
916
917   return 0;
918 }
919
920 static int in_my_get_expression = 0;
921
922 /* Third argument to my_get_expression.  */
923 #define GE_NO_PREFIX 0
924 #define GE_IMM_PREFIX 1
925 #define GE_OPT_PREFIX 2
926 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
927    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
928 #define GE_OPT_PREFIX_BIG 3
929
930 static int
931 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
932 {
933   char * save_in;
934   segT   seg;
935
936   /* In unified syntax, all prefixes are optional.  */
937   if (unified_syntax)
938     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
939                   : GE_OPT_PREFIX;
940
941   switch (prefix_mode)
942     {
943     case GE_NO_PREFIX: break;
944     case GE_IMM_PREFIX:
945       if (!is_immediate_prefix (**str))
946         {
947           inst.error = _("immediate expression requires a # prefix");
948           return FAIL;
949         }
950       (*str)++;
951       break;
952     case GE_OPT_PREFIX:
953     case GE_OPT_PREFIX_BIG:
954       if (is_immediate_prefix (**str))
955         (*str)++;
956       break;
957     default: abort ();
958     }
959
960   memset (ep, 0, sizeof (expressionS));
961
962   save_in = input_line_pointer;
963   input_line_pointer = *str;
964   in_my_get_expression = 1;
965   seg = expression (ep);
966   in_my_get_expression = 0;
967
968   if (ep->X_op == O_illegal || ep->X_op == O_absent)
969     {
970       /* We found a bad or missing expression in md_operand().  */
971       *str = input_line_pointer;
972       input_line_pointer = save_in;
973       if (inst.error == NULL)
974         inst.error = (ep->X_op == O_absent
975                       ? _("missing expression") :_("bad expression"));
976       return 1;
977     }
978
979 #ifdef OBJ_AOUT
980   if (seg != absolute_section
981       && seg != text_section
982       && seg != data_section
983       && seg != bss_section
984       && seg != undefined_section)
985     {
986       inst.error = _("bad segment");
987       *str = input_line_pointer;
988       input_line_pointer = save_in;
989       return 1;
990     }
991 #else
992   (void) seg;
993 #endif
994
995   /* Get rid of any bignums now, so that we don't generate an error for which
996      we can't establish a line number later on.  Big numbers are never valid
997      in instructions, which is where this routine is always called.  */
998   if (prefix_mode != GE_OPT_PREFIX_BIG
999       && (ep->X_op == O_big
1000           || (ep->X_add_symbol
1001               && (walk_no_bignums (ep->X_add_symbol)
1002                   || (ep->X_op_symbol
1003                       && walk_no_bignums (ep->X_op_symbol))))))
1004     {
1005       inst.error = _("invalid constant");
1006       *str = input_line_pointer;
1007       input_line_pointer = save_in;
1008       return 1;
1009     }
1010
1011   *str = input_line_pointer;
1012   input_line_pointer = save_in;
1013   return 0;
1014 }
1015
1016 /* Turn a string in input_line_pointer into a floating point constant
1017    of type TYPE, and store the appropriate bytes in *LITP.  The number
1018    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1019    returned, or NULL on OK.
1020
1021    Note that fp constants aren't represent in the normal way on the ARM.
1022    In big endian mode, things are as expected.  However, in little endian
1023    mode fp constants are big-endian word-wise, and little-endian byte-wise
1024    within the words.  For example, (double) 1.1 in big endian mode is
1025    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1026    the byte sequence 99 99 f1 3f 9a 99 99 99.
1027
1028    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1029
1030 char *
1031 md_atof (int type, char * litP, int * sizeP)
1032 {
1033   int prec;
1034   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1035   char *t;
1036   int i;
1037
1038   switch (type)
1039     {
1040     case 'f':
1041     case 'F':
1042     case 's':
1043     case 'S':
1044       prec = 2;
1045       break;
1046
1047     case 'd':
1048     case 'D':
1049     case 'r':
1050     case 'R':
1051       prec = 4;
1052       break;
1053
1054     case 'x':
1055     case 'X':
1056       prec = 5;
1057       break;
1058
1059     case 'p':
1060     case 'P':
1061       prec = 5;
1062       break;
1063
1064     default:
1065       *sizeP = 0;
1066       return _("Unrecognized or unsupported floating point constant");
1067     }
1068
1069   t = atof_ieee (input_line_pointer, type, words);
1070   if (t)
1071     input_line_pointer = t;
1072   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1073
1074   if (target_big_endian)
1075     {
1076       for (i = 0; i < prec; i++)
1077         {
1078           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1079           litP += sizeof (LITTLENUM_TYPE);
1080         }
1081     }
1082   else
1083     {
1084       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1085         for (i = prec - 1; i >= 0; i--)
1086           {
1087             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1088             litP += sizeof (LITTLENUM_TYPE);
1089           }
1090       else
1091         /* For a 4 byte float the order of elements in `words' is 1 0.
1092            For an 8 byte float the order is 1 0 3 2.  */
1093         for (i = 0; i < prec; i += 2)
1094           {
1095             md_number_to_chars (litP, (valueT) words[i + 1],
1096                                 sizeof (LITTLENUM_TYPE));
1097             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1098                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1099             litP += 2 * sizeof (LITTLENUM_TYPE);
1100           }
1101     }
1102
1103   return NULL;
1104 }
1105
1106 /* We handle all bad expressions here, so that we can report the faulty
1107    instruction in the error message.  */
1108 void
1109 md_operand (expressionS * exp)
1110 {
1111   if (in_my_get_expression)
1112     exp->X_op = O_illegal;
1113 }
1114
1115 /* Immediate values.  */
1116
1117 /* Generic immediate-value read function for use in directives.
1118    Accepts anything that 'expression' can fold to a constant.
1119    *val receives the number.  */
1120 #ifdef OBJ_ELF
1121 static int
1122 immediate_for_directive (int *val)
1123 {
1124   expressionS exp;
1125   exp.X_op = O_illegal;
1126
1127   if (is_immediate_prefix (*input_line_pointer))
1128     {
1129       input_line_pointer++;
1130       expression (&exp);
1131     }
1132
1133   if (exp.X_op != O_constant)
1134     {
1135       as_bad (_("expected #constant"));
1136       ignore_rest_of_line ();
1137       return FAIL;
1138     }
1139   *val = exp.X_add_number;
1140   return SUCCESS;
1141 }
1142 #endif
1143
1144 /* Register parsing.  */
1145
1146 /* Generic register parser.  CCP points to what should be the
1147    beginning of a register name.  If it is indeed a valid register
1148    name, advance CCP over it and return the reg_entry structure;
1149    otherwise return NULL.  Does not issue diagnostics.  */
1150
1151 static struct reg_entry *
1152 arm_reg_parse_multi (char **ccp)
1153 {
1154   char *start = *ccp;
1155   char *p;
1156   struct reg_entry *reg;
1157
1158 #ifdef REGISTER_PREFIX
1159   if (*start != REGISTER_PREFIX)
1160     return NULL;
1161   start++;
1162 #endif
1163 #ifdef OPTIONAL_REGISTER_PREFIX
1164   if (*start == OPTIONAL_REGISTER_PREFIX)
1165     start++;
1166 #endif
1167
1168   p = start;
1169   if (!ISALPHA (*p) || !is_name_beginner (*p))
1170     return NULL;
1171
1172   do
1173     p++;
1174   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1175
1176   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1177
1178   if (!reg)
1179     return NULL;
1180
1181   *ccp = p;
1182   return reg;
1183 }
1184
1185 static int
1186 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1187                     enum arm_reg_type type)
1188 {
1189   /* Alternative syntaxes are accepted for a few register classes.  */
1190   switch (type)
1191     {
1192     case REG_TYPE_MVF:
1193     case REG_TYPE_MVD:
1194     case REG_TYPE_MVFX:
1195     case REG_TYPE_MVDX:
1196       /* Generic coprocessor register names are allowed for these.  */
1197       if (reg && reg->type == REG_TYPE_CN)
1198         return reg->number;
1199       break;
1200
1201     case REG_TYPE_CP:
1202       /* For backward compatibility, a bare number is valid here.  */
1203       {
1204         unsigned long processor = strtoul (start, ccp, 10);
1205         if (*ccp != start && processor <= 15)
1206           return processor;
1207       }
1208
1209     case REG_TYPE_MMXWC:
1210       /* WC includes WCG.  ??? I'm not sure this is true for all
1211          instructions that take WC registers.  */
1212       if (reg && reg->type == REG_TYPE_MMXWCG)
1213         return reg->number;
1214       break;
1215
1216     default:
1217       break;
1218     }
1219
1220   return FAIL;
1221 }
1222
1223 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1224    return value is the register number or FAIL.  */
1225
1226 static int
1227 arm_reg_parse (char **ccp, enum arm_reg_type type)
1228 {
1229   char *start = *ccp;
1230   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1231   int ret;
1232
1233   /* Do not allow a scalar (reg+index) to parse as a register.  */
1234   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1235     return FAIL;
1236
1237   if (reg && reg->type == type)
1238     return reg->number;
1239
1240   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1241     return ret;
1242
1243   *ccp = start;
1244   return FAIL;
1245 }
1246
1247 /* Parse a Neon type specifier. *STR should point at the leading '.'
1248    character. Does no verification at this stage that the type fits the opcode
1249    properly. E.g.,
1250
1251      .i32.i32.s16
1252      .s32.f32
1253      .u16
1254
1255    Can all be legally parsed by this function.
1256
1257    Fills in neon_type struct pointer with parsed information, and updates STR
1258    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1259    type, FAIL if not.  */
1260
1261 static int
1262 parse_neon_type (struct neon_type *type, char **str)
1263 {
1264   char *ptr = *str;
1265
1266   if (type)
1267     type->elems = 0;
1268
1269   while (type->elems < NEON_MAX_TYPE_ELS)
1270     {
1271       enum neon_el_type thistype = NT_untyped;
1272       unsigned thissize = -1u;
1273
1274       if (*ptr != '.')
1275         break;
1276
1277       ptr++;
1278
1279       /* Just a size without an explicit type.  */
1280       if (ISDIGIT (*ptr))
1281         goto parsesize;
1282
1283       switch (TOLOWER (*ptr))
1284         {
1285         case 'i': thistype = NT_integer; break;
1286         case 'f': thistype = NT_float; break;
1287         case 'p': thistype = NT_poly; break;
1288         case 's': thistype = NT_signed; break;
1289         case 'u': thistype = NT_unsigned; break;
1290         case 'd':
1291           thistype = NT_float;
1292           thissize = 64;
1293           ptr++;
1294           goto done;
1295         default:
1296           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1297           return FAIL;
1298         }
1299
1300       ptr++;
1301
1302       /* .f is an abbreviation for .f32.  */
1303       if (thistype == NT_float && !ISDIGIT (*ptr))
1304         thissize = 32;
1305       else
1306         {
1307         parsesize:
1308           thissize = strtoul (ptr, &ptr, 10);
1309
1310           if (thissize != 8 && thissize != 16 && thissize != 32
1311               && thissize != 64)
1312             {
1313               as_bad (_("bad size %d in type specifier"), thissize);
1314               return FAIL;
1315             }
1316         }
1317
1318       done:
1319       if (type)
1320         {
1321           type->el[type->elems].type = thistype;
1322           type->el[type->elems].size = thissize;
1323           type->elems++;
1324         }
1325     }
1326
1327   /* Empty/missing type is not a successful parse.  */
1328   if (type->elems == 0)
1329     return FAIL;
1330
1331   *str = ptr;
1332
1333   return SUCCESS;
1334 }
1335
1336 /* Errors may be set multiple times during parsing or bit encoding
1337    (particularly in the Neon bits), but usually the earliest error which is set
1338    will be the most meaningful. Avoid overwriting it with later (cascading)
1339    errors by calling this function.  */
1340
1341 static void
1342 first_error (const char *err)
1343 {
1344   if (!inst.error)
1345     inst.error = err;
1346 }
1347
1348 /* Parse a single type, e.g. ".s32", leading period included.  */
1349 static int
1350 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1351 {
1352   char *str = *ccp;
1353   struct neon_type optype;
1354
1355   if (*str == '.')
1356     {
1357       if (parse_neon_type (&optype, &str) == SUCCESS)
1358         {
1359           if (optype.elems == 1)
1360             *vectype = optype.el[0];
1361           else
1362             {
1363               first_error (_("only one type should be specified for operand"));
1364               return FAIL;
1365             }
1366         }
1367       else
1368         {
1369           first_error (_("vector type expected"));
1370           return FAIL;
1371         }
1372     }
1373   else
1374     return FAIL;
1375
1376   *ccp = str;
1377
1378   return SUCCESS;
1379 }
1380
1381 /* Special meanings for indices (which have a range of 0-7), which will fit into
1382    a 4-bit integer.  */
1383
1384 #define NEON_ALL_LANES          15
1385 #define NEON_INTERLEAVE_LANES   14
1386
1387 /* Parse either a register or a scalar, with an optional type. Return the
1388    register number, and optionally fill in the actual type of the register
1389    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1390    type/index information in *TYPEINFO.  */
1391
1392 static int
1393 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1394                            enum arm_reg_type *rtype,
1395                            struct neon_typed_alias *typeinfo)
1396 {
1397   char *str = *ccp;
1398   struct reg_entry *reg = arm_reg_parse_multi (&str);
1399   struct neon_typed_alias atype;
1400   struct neon_type_el parsetype;
1401
1402   atype.defined = 0;
1403   atype.index = -1;
1404   atype.eltype.type = NT_invtype;
1405   atype.eltype.size = -1;
1406
1407   /* Try alternate syntax for some types of register. Note these are mutually
1408      exclusive with the Neon syntax extensions.  */
1409   if (reg == NULL)
1410     {
1411       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1412       if (altreg != FAIL)
1413         *ccp = str;
1414       if (typeinfo)
1415         *typeinfo = atype;
1416       return altreg;
1417     }
1418
1419   /* Undo polymorphism when a set of register types may be accepted.  */
1420   if ((type == REG_TYPE_NDQ
1421        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1422       || (type == REG_TYPE_VFSD
1423           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1424       || (type == REG_TYPE_NSDQ
1425           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1426               || reg->type == REG_TYPE_NQ))
1427       || (type == REG_TYPE_MMXWC
1428           && (reg->type == REG_TYPE_MMXWCG)))
1429     type = (enum arm_reg_type) reg->type;
1430
1431   if (type != reg->type)
1432     return FAIL;
1433
1434   if (reg->neon)
1435     atype = *reg->neon;
1436
1437   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1438     {
1439       if ((atype.defined & NTA_HASTYPE) != 0)
1440         {
1441           first_error (_("can't redefine type for operand"));
1442           return FAIL;
1443         }
1444       atype.defined |= NTA_HASTYPE;
1445       atype.eltype = parsetype;
1446     }
1447
1448   if (skip_past_char (&str, '[') == SUCCESS)
1449     {
1450       if (type != REG_TYPE_VFD)
1451         {
1452           first_error (_("only D registers may be indexed"));
1453           return FAIL;
1454         }
1455
1456       if ((atype.defined & NTA_HASINDEX) != 0)
1457         {
1458           first_error (_("can't change index for operand"));
1459           return FAIL;
1460         }
1461
1462       atype.defined |= NTA_HASINDEX;
1463
1464       if (skip_past_char (&str, ']') == SUCCESS)
1465         atype.index = NEON_ALL_LANES;
1466       else
1467         {
1468           expressionS exp;
1469
1470           my_get_expression (&exp, &str, GE_NO_PREFIX);
1471
1472           if (exp.X_op != O_constant)
1473             {
1474               first_error (_("constant expression required"));
1475               return FAIL;
1476             }
1477
1478           if (skip_past_char (&str, ']') == FAIL)
1479             return FAIL;
1480
1481           atype.index = exp.X_add_number;
1482         }
1483     }
1484
1485   if (typeinfo)
1486     *typeinfo = atype;
1487
1488   if (rtype)
1489     *rtype = type;
1490
1491   *ccp = str;
1492
1493   return reg->number;
1494 }
1495
1496 /* Like arm_reg_parse, but allow allow the following extra features:
1497     - If RTYPE is non-zero, return the (possibly restricted) type of the
1498       register (e.g. Neon double or quad reg when either has been requested).
1499     - If this is a Neon vector type with additional type information, fill
1500       in the struct pointed to by VECTYPE (if non-NULL).
1501    This function will fault on encountering a scalar.  */
1502
1503 static int
1504 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1505                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1506 {
1507   struct neon_typed_alias atype;
1508   char *str = *ccp;
1509   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1510
1511   if (reg == FAIL)
1512     return FAIL;
1513
1514   /* Do not allow regname(... to parse as a register.  */
1515   if (*str == '(')
1516     return FAIL;
1517
1518   /* Do not allow a scalar (reg+index) to parse as a register.  */
1519   if ((atype.defined & NTA_HASINDEX) != 0)
1520     {
1521       first_error (_("register operand expected, but got scalar"));
1522       return FAIL;
1523     }
1524
1525   if (vectype)
1526     *vectype = atype.eltype;
1527
1528   *ccp = str;
1529
1530   return reg;
1531 }
1532
1533 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1534 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1535
1536 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1537    have enough information to be able to do a good job bounds-checking. So, we
1538    just do easy checks here, and do further checks later.  */
1539
1540 static int
1541 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1542 {
1543   int reg;
1544   char *str = *ccp;
1545   struct neon_typed_alias atype;
1546
1547   reg = parse_typed_reg_or_scalar (&str, REG_TYPE_VFD, NULL, &atype);
1548
1549   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1550     return FAIL;
1551
1552   if (atype.index == NEON_ALL_LANES)
1553     {
1554       first_error (_("scalar must have an index"));
1555       return FAIL;
1556     }
1557   else if (atype.index >= 64 / elsize)
1558     {
1559       first_error (_("scalar index out of range"));
1560       return FAIL;
1561     }
1562
1563   if (type)
1564     *type = atype.eltype;
1565
1566   *ccp = str;
1567
1568   return reg * 16 + atype.index;
1569 }
1570
1571 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1572
1573 static long
1574 parse_reg_list (char ** strp)
1575 {
1576   char * str = * strp;
1577   long   range = 0;
1578   int    another_range;
1579
1580   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1581   do
1582     {
1583       another_range = 0;
1584
1585       if (*str == '{')
1586         {
1587           int in_range = 0;
1588           int cur_reg = -1;
1589
1590           str++;
1591           do
1592             {
1593               int reg;
1594
1595               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1596                 {
1597                   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
1598                   return FAIL;
1599                 }
1600
1601               if (in_range)
1602                 {
1603                   int i;
1604
1605                   if (reg <= cur_reg)
1606                     {
1607                       first_error (_("bad range in register list"));
1608                       return FAIL;
1609                     }
1610
1611                   for (i = cur_reg + 1; i < reg; i++)
1612                     {
1613                       if (range & (1 << i))
1614                         as_tsktsk
1615                           (_("Warning: duplicated register (r%d) in register list"),
1616                            i);
1617                       else
1618                         range |= 1 << i;
1619                     }
1620                   in_range = 0;
1621                 }
1622
1623               if (range & (1 << reg))
1624                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1625                            reg);
1626               else if (reg <= cur_reg)
1627                 as_tsktsk (_("Warning: register range not in ascending order"));
1628
1629               range |= 1 << reg;
1630               cur_reg = reg;
1631             }
1632           while (skip_past_comma (&str) != FAIL
1633                  || (in_range = 1, *str++ == '-'));
1634           str--;
1635
1636           if (*str++ != '}')
1637             {
1638               first_error (_("missing `}'"));
1639               return FAIL;
1640             }
1641         }
1642       else
1643         {
1644           expressionS exp;
1645
1646           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1647             return FAIL;
1648
1649           if (exp.X_op == O_constant)
1650             {
1651               if (exp.X_add_number
1652                   != (exp.X_add_number & 0x0000ffff))
1653                 {
1654                   inst.error = _("invalid register mask");
1655                   return FAIL;
1656                 }
1657
1658               if ((range & exp.X_add_number) != 0)
1659                 {
1660                   int regno = range & exp.X_add_number;
1661
1662                   regno &= -regno;
1663                   regno = (1 << regno) - 1;
1664                   as_tsktsk
1665                     (_("Warning: duplicated register (r%d) in register list"),
1666                      regno);
1667                 }
1668
1669               range |= exp.X_add_number;
1670             }
1671           else
1672             {
1673               if (inst.reloc.type != 0)
1674                 {
1675                   inst.error = _("expression too complex");
1676                   return FAIL;
1677                 }
1678
1679               memcpy (&inst.reloc.exp, &exp, sizeof (expressionS));
1680               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1681               inst.reloc.pc_rel = 0;
1682             }
1683         }
1684
1685       if (*str == '|' || *str == '+')
1686         {
1687           str++;
1688           another_range = 1;
1689         }
1690     }
1691   while (another_range);
1692
1693   *strp = str;
1694   return range;
1695 }
1696
1697 /* Types of registers in a list.  */
1698
1699 enum reg_list_els
1700 {
1701   REGLIST_VFP_S,
1702   REGLIST_VFP_D,
1703   REGLIST_NEON_D
1704 };
1705
1706 /* Parse a VFP register list.  If the string is invalid return FAIL.
1707    Otherwise return the number of registers, and set PBASE to the first
1708    register.  Parses registers of type ETYPE.
1709    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1710      - Q registers can be used to specify pairs of D registers
1711      - { } can be omitted from around a singleton register list
1712          FIXME: This is not implemented, as it would require backtracking in
1713          some cases, e.g.:
1714            vtbl.8 d3,d4,d5
1715          This could be done (the meaning isn't really ambiguous), but doesn't
1716          fit in well with the current parsing framework.
1717      - 32 D registers may be used (also true for VFPv3).
1718    FIXME: Types are ignored in these register lists, which is probably a
1719    bug.  */
1720
1721 static int
1722 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype)
1723 {
1724   char *str = *ccp;
1725   int base_reg;
1726   int new_base;
1727   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1728   int max_regs = 0;
1729   int count = 0;
1730   int warned = 0;
1731   unsigned long mask = 0;
1732   int i;
1733
1734   if (*str != '{')
1735     {
1736       inst.error = _("expecting {");
1737       return FAIL;
1738     }
1739
1740   str++;
1741
1742   switch (etype)
1743     {
1744     case REGLIST_VFP_S:
1745       regtype = REG_TYPE_VFS;
1746       max_regs = 32;
1747       break;
1748
1749     case REGLIST_VFP_D:
1750       regtype = REG_TYPE_VFD;
1751       break;
1752
1753     case REGLIST_NEON_D:
1754       regtype = REG_TYPE_NDQ;
1755       break;
1756     }
1757
1758   if (etype != REGLIST_VFP_S)
1759     {
1760       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
1761       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
1762         {
1763           max_regs = 32;
1764           if (thumb_mode)
1765             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1766                                     fpu_vfp_ext_d32);
1767           else
1768             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1769                                     fpu_vfp_ext_d32);
1770         }
1771       else
1772         max_regs = 16;
1773     }
1774
1775   base_reg = max_regs;
1776
1777   do
1778     {
1779       int setmask = 1, addregs = 1;
1780
1781       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
1782
1783       if (new_base == FAIL)
1784         {
1785           first_error (_(reg_expected_msgs[regtype]));
1786           return FAIL;
1787         }
1788
1789       if (new_base >= max_regs)
1790         {
1791           first_error (_("register out of range in list"));
1792           return FAIL;
1793         }
1794
1795       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1796       if (regtype == REG_TYPE_NQ)
1797         {
1798           setmask = 3;
1799           addregs = 2;
1800         }
1801
1802       if (new_base < base_reg)
1803         base_reg = new_base;
1804
1805       if (mask & (setmask << new_base))
1806         {
1807           first_error (_("invalid register list"));
1808           return FAIL;
1809         }
1810
1811       if ((mask >> new_base) != 0 && ! warned)
1812         {
1813           as_tsktsk (_("register list not in ascending order"));
1814           warned = 1;
1815         }
1816
1817       mask |= setmask << new_base;
1818       count += addregs;
1819
1820       if (*str == '-') /* We have the start of a range expression */
1821         {
1822           int high_range;
1823
1824           str++;
1825
1826           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
1827               == FAIL)
1828             {
1829               inst.error = gettext (reg_expected_msgs[regtype]);
1830               return FAIL;
1831             }
1832
1833           if (high_range >= max_regs)
1834             {
1835               first_error (_("register out of range in list"));
1836               return FAIL;
1837             }
1838
1839           if (regtype == REG_TYPE_NQ)
1840             high_range = high_range + 1;
1841
1842           if (high_range <= new_base)
1843             {
1844               inst.error = _("register range not in ascending order");
1845               return FAIL;
1846             }
1847
1848           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1849             {
1850               if (mask & (setmask << new_base))
1851                 {
1852                   inst.error = _("invalid register list");
1853                   return FAIL;
1854                 }
1855
1856               mask |= setmask << new_base;
1857               count += addregs;
1858             }
1859         }
1860     }
1861   while (skip_past_comma (&str) != FAIL);
1862
1863   str++;
1864
1865   /* Sanity check -- should have raised a parse error above.  */
1866   if (count == 0 || count > max_regs)
1867     abort ();
1868
1869   *pbase = base_reg;
1870
1871   /* Final test -- the registers must be consecutive.  */
1872   mask >>= base_reg;
1873   for (i = 0; i < count; i++)
1874     {
1875       if ((mask & (1u << i)) == 0)
1876         {
1877           inst.error = _("non-contiguous register range");
1878           return FAIL;
1879         }
1880     }
1881
1882   *ccp = str;
1883
1884   return count;
1885 }
1886
1887 /* True if two alias types are the same.  */
1888
1889 static bfd_boolean
1890 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
1891 {
1892   if (!a && !b)
1893     return TRUE;
1894
1895   if (!a || !b)
1896     return FALSE;
1897
1898   if (a->defined != b->defined)
1899     return FALSE;
1900
1901   if ((a->defined & NTA_HASTYPE) != 0
1902       && (a->eltype.type != b->eltype.type
1903           || a->eltype.size != b->eltype.size))
1904     return FALSE;
1905
1906   if ((a->defined & NTA_HASINDEX) != 0
1907       && (a->index != b->index))
1908     return FALSE;
1909
1910   return TRUE;
1911 }
1912
1913 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
1914    The base register is put in *PBASE.
1915    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
1916    the return value.
1917    The register stride (minus one) is put in bit 4 of the return value.
1918    Bits [6:5] encode the list length (minus one).
1919    The type of the list elements is put in *ELTYPE, if non-NULL.  */
1920
1921 #define NEON_LANE(X)            ((X) & 0xf)
1922 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
1923 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
1924
1925 static int
1926 parse_neon_el_struct_list (char **str, unsigned *pbase,
1927                            struct neon_type_el *eltype)
1928 {
1929   char *ptr = *str;
1930   int base_reg = -1;
1931   int reg_incr = -1;
1932   int count = 0;
1933   int lane = -1;
1934   int leading_brace = 0;
1935   enum arm_reg_type rtype = REG_TYPE_NDQ;
1936   const char *const incr_error = _("register stride must be 1 or 2");
1937   const char *const type_error = _("mismatched element/structure types in list");
1938   struct neon_typed_alias firsttype;
1939
1940   if (skip_past_char (&ptr, '{') == SUCCESS)
1941     leading_brace = 1;
1942
1943   do
1944     {
1945       struct neon_typed_alias atype;
1946       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
1947
1948       if (getreg == FAIL)
1949         {
1950           first_error (_(reg_expected_msgs[rtype]));
1951           return FAIL;
1952         }
1953
1954       if (base_reg == -1)
1955         {
1956           base_reg = getreg;
1957           if (rtype == REG_TYPE_NQ)
1958             {
1959               reg_incr = 1;
1960             }
1961           firsttype = atype;
1962         }
1963       else if (reg_incr == -1)
1964         {
1965           reg_incr = getreg - base_reg;
1966           if (reg_incr < 1 || reg_incr > 2)
1967             {
1968               first_error (_(incr_error));
1969               return FAIL;
1970             }
1971         }
1972       else if (getreg != base_reg + reg_incr * count)
1973         {
1974           first_error (_(incr_error));
1975           return FAIL;
1976         }
1977
1978       if (! neon_alias_types_same (&atype, &firsttype))
1979         {
1980           first_error (_(type_error));
1981           return FAIL;
1982         }
1983
1984       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
1985          modes.  */
1986       if (ptr[0] == '-')
1987         {
1988           struct neon_typed_alias htype;
1989           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
1990           if (lane == -1)
1991             lane = NEON_INTERLEAVE_LANES;
1992           else if (lane != NEON_INTERLEAVE_LANES)
1993             {
1994               first_error (_(type_error));
1995               return FAIL;
1996             }
1997           if (reg_incr == -1)
1998             reg_incr = 1;
1999           else if (reg_incr != 1)
2000             {
2001               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2002               return FAIL;
2003             }
2004           ptr++;
2005           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2006           if (hireg == FAIL)
2007             {
2008               first_error (_(reg_expected_msgs[rtype]));
2009               return FAIL;
2010             }
2011           if (! neon_alias_types_same (&htype, &firsttype))
2012             {
2013               first_error (_(type_error));
2014               return FAIL;
2015             }
2016           count += hireg + dregs - getreg;
2017           continue;
2018         }
2019
2020       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2021       if (rtype == REG_TYPE_NQ)
2022         {
2023           count += 2;
2024           continue;
2025         }
2026
2027       if ((atype.defined & NTA_HASINDEX) != 0)
2028         {
2029           if (lane == -1)
2030             lane = atype.index;
2031           else if (lane != atype.index)
2032             {
2033               first_error (_(type_error));
2034               return FAIL;
2035             }
2036         }
2037       else if (lane == -1)
2038         lane = NEON_INTERLEAVE_LANES;
2039       else if (lane != NEON_INTERLEAVE_LANES)
2040         {
2041           first_error (_(type_error));
2042           return FAIL;
2043         }
2044       count++;
2045     }
2046   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2047
2048   /* No lane set by [x]. We must be interleaving structures.  */
2049   if (lane == -1)
2050     lane = NEON_INTERLEAVE_LANES;
2051
2052   /* Sanity check.  */
2053   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
2054       || (count > 1 && reg_incr == -1))
2055     {
2056       first_error (_("error parsing element/structure list"));
2057       return FAIL;
2058     }
2059
2060   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2061     {
2062       first_error (_("expected }"));
2063       return FAIL;
2064     }
2065
2066   if (reg_incr == -1)
2067     reg_incr = 1;
2068
2069   if (eltype)
2070     *eltype = firsttype.eltype;
2071
2072   *pbase = base_reg;
2073   *str = ptr;
2074
2075   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2076 }
2077
2078 /* Parse an explicit relocation suffix on an expression.  This is
2079    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2080    arm_reloc_hsh contains no entries, so this function can only
2081    succeed if there is no () after the word.  Returns -1 on error,
2082    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2083
2084 static int
2085 parse_reloc (char **str)
2086 {
2087   struct reloc_entry *r;
2088   char *p, *q;
2089
2090   if (**str != '(')
2091     return BFD_RELOC_UNUSED;
2092
2093   p = *str + 1;
2094   q = p;
2095
2096   while (*q && *q != ')' && *q != ',')
2097     q++;
2098   if (*q != ')')
2099     return -1;
2100
2101   if ((r = (struct reloc_entry *)
2102        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2103     return -1;
2104
2105   *str = q + 1;
2106   return r->reloc;
2107 }
2108
2109 /* Directives: register aliases.  */
2110
2111 static struct reg_entry *
2112 insert_reg_alias (char *str, unsigned number, int type)
2113 {
2114   struct reg_entry *new_reg;
2115   const char *name;
2116
2117   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2118     {
2119       if (new_reg->builtin)
2120         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2121
2122       /* Only warn about a redefinition if it's not defined as the
2123          same register.  */
2124       else if (new_reg->number != number || new_reg->type != type)
2125         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2126
2127       return NULL;
2128     }
2129
2130   name = xstrdup (str);
2131   new_reg = (struct reg_entry *) xmalloc (sizeof (struct reg_entry));
2132
2133   new_reg->name = name;
2134   new_reg->number = number;
2135   new_reg->type = type;
2136   new_reg->builtin = FALSE;
2137   new_reg->neon = NULL;
2138
2139   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2140     abort ();
2141
2142   return new_reg;
2143 }
2144
2145 static void
2146 insert_neon_reg_alias (char *str, int number, int type,
2147                        struct neon_typed_alias *atype)
2148 {
2149   struct reg_entry *reg = insert_reg_alias (str, number, type);
2150
2151   if (!reg)
2152     {
2153       first_error (_("attempt to redefine typed alias"));
2154       return;
2155     }
2156
2157   if (atype)
2158     {
2159       reg->neon = (struct neon_typed_alias *)
2160           xmalloc (sizeof (struct neon_typed_alias));
2161       *reg->neon = *atype;
2162     }
2163 }
2164
2165 /* Look for the .req directive.  This is of the form:
2166
2167         new_register_name .req existing_register_name
2168
2169    If we find one, or if it looks sufficiently like one that we want to
2170    handle any error here, return TRUE.  Otherwise return FALSE.  */
2171
2172 static bfd_boolean
2173 create_register_alias (char * newname, char *p)
2174 {
2175   struct reg_entry *old;
2176   char *oldname, *nbuf;
2177   size_t nlen;
2178
2179   /* The input scrubber ensures that whitespace after the mnemonic is
2180      collapsed to single spaces.  */
2181   oldname = p;
2182   if (strncmp (oldname, " .req ", 6) != 0)
2183     return FALSE;
2184
2185   oldname += 6;
2186   if (*oldname == '\0')
2187     return FALSE;
2188
2189   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2190   if (!old)
2191     {
2192       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2193       return TRUE;
2194     }
2195
2196   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2197      the desired alias name, and p points to its end.  If not, then
2198      the desired alias name is in the global original_case_string.  */
2199 #ifdef TC_CASE_SENSITIVE
2200   nlen = p - newname;
2201 #else
2202   newname = original_case_string;
2203   nlen = strlen (newname);
2204 #endif
2205
2206   nbuf = (char *) alloca (nlen + 1);
2207   memcpy (nbuf, newname, nlen);
2208   nbuf[nlen] = '\0';
2209
2210   /* Create aliases under the new name as stated; an all-lowercase
2211      version of the new name; and an all-uppercase version of the new
2212      name.  */
2213   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2214     {
2215       for (p = nbuf; *p; p++)
2216         *p = TOUPPER (*p);
2217
2218       if (strncmp (nbuf, newname, nlen))
2219         {
2220           /* If this attempt to create an additional alias fails, do not bother
2221              trying to create the all-lower case alias.  We will fail and issue
2222              a second, duplicate error message.  This situation arises when the
2223              programmer does something like:
2224                foo .req r0
2225                Foo .req r1
2226              The second .req creates the "Foo" alias but then fails to create
2227              the artificial FOO alias because it has already been created by the
2228              first .req.  */
2229           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2230             return TRUE;
2231         }
2232
2233       for (p = nbuf; *p; p++)
2234         *p = TOLOWER (*p);
2235
2236       if (strncmp (nbuf, newname, nlen))
2237         insert_reg_alias (nbuf, old->number, old->type);
2238     }
2239
2240   return TRUE;
2241 }
2242
2243 /* Create a Neon typed/indexed register alias using directives, e.g.:
2244      X .dn d5.s32[1]
2245      Y .qn 6.s16
2246      Z .dn d7
2247      T .dn Z[0]
2248    These typed registers can be used instead of the types specified after the
2249    Neon mnemonic, so long as all operands given have types. Types can also be
2250    specified directly, e.g.:
2251      vadd d0.s32, d1.s32, d2.s32  */
2252
2253 static bfd_boolean
2254 create_neon_reg_alias (char *newname, char *p)
2255 {
2256   enum arm_reg_type basetype;
2257   struct reg_entry *basereg;
2258   struct reg_entry mybasereg;
2259   struct neon_type ntype;
2260   struct neon_typed_alias typeinfo;
2261   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2262   int namelen;
2263
2264   typeinfo.defined = 0;
2265   typeinfo.eltype.type = NT_invtype;
2266   typeinfo.eltype.size = -1;
2267   typeinfo.index = -1;
2268
2269   nameend = p;
2270
2271   if (strncmp (p, " .dn ", 5) == 0)
2272     basetype = REG_TYPE_VFD;
2273   else if (strncmp (p, " .qn ", 5) == 0)
2274     basetype = REG_TYPE_NQ;
2275   else
2276     return FALSE;
2277
2278   p += 5;
2279
2280   if (*p == '\0')
2281     return FALSE;
2282
2283   basereg = arm_reg_parse_multi (&p);
2284
2285   if (basereg && basereg->type != basetype)
2286     {
2287       as_bad (_("bad type for register"));
2288       return FALSE;
2289     }
2290
2291   if (basereg == NULL)
2292     {
2293       expressionS exp;
2294       /* Try parsing as an integer.  */
2295       my_get_expression (&exp, &p, GE_NO_PREFIX);
2296       if (exp.X_op != O_constant)
2297         {
2298           as_bad (_("expression must be constant"));
2299           return FALSE;
2300         }
2301       basereg = &mybasereg;
2302       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2303                                                   : exp.X_add_number;
2304       basereg->neon = 0;
2305     }
2306
2307   if (basereg->neon)
2308     typeinfo = *basereg->neon;
2309
2310   if (parse_neon_type (&ntype, &p) == SUCCESS)
2311     {
2312       /* We got a type.  */
2313       if (typeinfo.defined & NTA_HASTYPE)
2314         {
2315           as_bad (_("can't redefine the type of a register alias"));
2316           return FALSE;
2317         }
2318
2319       typeinfo.defined |= NTA_HASTYPE;
2320       if (ntype.elems != 1)
2321         {
2322           as_bad (_("you must specify a single type only"));
2323           return FALSE;
2324         }
2325       typeinfo.eltype = ntype.el[0];
2326     }
2327
2328   if (skip_past_char (&p, '[') == SUCCESS)
2329     {
2330       expressionS exp;
2331       /* We got a scalar index.  */
2332
2333       if (typeinfo.defined & NTA_HASINDEX)
2334         {
2335           as_bad (_("can't redefine the index of a scalar alias"));
2336           return FALSE;
2337         }
2338
2339       my_get_expression (&exp, &p, GE_NO_PREFIX);
2340
2341       if (exp.X_op != O_constant)
2342         {
2343           as_bad (_("scalar index must be constant"));
2344           return FALSE;
2345         }
2346
2347       typeinfo.defined |= NTA_HASINDEX;
2348       typeinfo.index = exp.X_add_number;
2349
2350       if (skip_past_char (&p, ']') == FAIL)
2351         {
2352           as_bad (_("expecting ]"));
2353           return FALSE;
2354         }
2355     }
2356
2357   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2358      the desired alias name, and p points to its end.  If not, then
2359      the desired alias name is in the global original_case_string.  */
2360 #ifdef TC_CASE_SENSITIVE
2361   namelen = nameend - newname;
2362 #else
2363   newname = original_case_string;
2364   namelen = strlen (newname);
2365 #endif
2366
2367   namebuf = (char *) alloca (namelen + 1);
2368   strncpy (namebuf, newname, namelen);
2369   namebuf[namelen] = '\0';
2370
2371   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2372                          typeinfo.defined != 0 ? &typeinfo : NULL);
2373
2374   /* Insert name in all uppercase.  */
2375   for (p = namebuf; *p; p++)
2376     *p = TOUPPER (*p);
2377
2378   if (strncmp (namebuf, newname, namelen))
2379     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2380                            typeinfo.defined != 0 ? &typeinfo : NULL);
2381
2382   /* Insert name in all lowercase.  */
2383   for (p = namebuf; *p; p++)
2384     *p = TOLOWER (*p);
2385
2386   if (strncmp (namebuf, newname, namelen))
2387     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2388                            typeinfo.defined != 0 ? &typeinfo : NULL);
2389
2390   return TRUE;
2391 }
2392
2393 /* Should never be called, as .req goes between the alias and the
2394    register name, not at the beginning of the line.  */
2395
2396 static void
2397 s_req (int a ATTRIBUTE_UNUSED)
2398 {
2399   as_bad (_("invalid syntax for .req directive"));
2400 }
2401
2402 static void
2403 s_dn (int a ATTRIBUTE_UNUSED)
2404 {
2405   as_bad (_("invalid syntax for .dn directive"));
2406 }
2407
2408 static void
2409 s_qn (int a ATTRIBUTE_UNUSED)
2410 {
2411   as_bad (_("invalid syntax for .qn directive"));
2412 }
2413
2414 /* The .unreq directive deletes an alias which was previously defined
2415    by .req.  For example:
2416
2417        my_alias .req r11
2418        .unreq my_alias    */
2419
2420 static void
2421 s_unreq (int a ATTRIBUTE_UNUSED)
2422 {
2423   char * name;
2424   char saved_char;
2425
2426   name = input_line_pointer;
2427
2428   while (*input_line_pointer != 0
2429          && *input_line_pointer != ' '
2430          && *input_line_pointer != '\n')
2431     ++input_line_pointer;
2432
2433   saved_char = *input_line_pointer;
2434   *input_line_pointer = 0;
2435
2436   if (!*name)
2437     as_bad (_("invalid syntax for .unreq directive"));
2438   else
2439     {
2440       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2441                                                               name);
2442
2443       if (!reg)
2444         as_bad (_("unknown register alias '%s'"), name);
2445       else if (reg->builtin)
2446         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2447                  name);
2448       else
2449         {
2450           char * p;
2451           char * nbuf;
2452
2453           hash_delete (arm_reg_hsh, name, FALSE);
2454           free ((char *) reg->name);
2455           if (reg->neon)
2456             free (reg->neon);
2457           free (reg);
2458
2459           /* Also locate the all upper case and all lower case versions.
2460              Do not complain if we cannot find one or the other as it
2461              was probably deleted above.  */
2462
2463           nbuf = strdup (name);
2464           for (p = nbuf; *p; p++)
2465             *p = TOUPPER (*p);
2466           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2467           if (reg)
2468             {
2469               hash_delete (arm_reg_hsh, nbuf, FALSE);
2470               free ((char *) reg->name);
2471               if (reg->neon)
2472                 free (reg->neon);
2473               free (reg);
2474             }
2475
2476           for (p = nbuf; *p; p++)
2477             *p = TOLOWER (*p);
2478           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2479           if (reg)
2480             {
2481               hash_delete (arm_reg_hsh, nbuf, FALSE);
2482               free ((char *) reg->name);
2483               if (reg->neon)
2484                 free (reg->neon);
2485               free (reg);
2486             }
2487
2488           free (nbuf);
2489         }
2490     }
2491
2492   *input_line_pointer = saved_char;
2493   demand_empty_rest_of_line ();
2494 }
2495
2496 /* Directives: Instruction set selection.  */
2497
2498 #ifdef OBJ_ELF
2499 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2500    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2501    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2502    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2503
2504 /* Create a new mapping symbol for the transition to STATE.  */
2505
2506 static void
2507 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2508 {
2509   symbolS * symbolP;
2510   const char * symname;
2511   int type;
2512
2513   switch (state)
2514     {
2515     case MAP_DATA:
2516       symname = "$d";
2517       type = BSF_NO_FLAGS;
2518       break;
2519     case MAP_ARM:
2520       symname = "$a";
2521       type = BSF_NO_FLAGS;
2522       break;
2523     case MAP_THUMB:
2524       symname = "$t";
2525       type = BSF_NO_FLAGS;
2526       break;
2527     default:
2528       abort ();
2529     }
2530
2531   symbolP = symbol_new (symname, now_seg, value, frag);
2532   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2533
2534   switch (state)
2535     {
2536     case MAP_ARM:
2537       THUMB_SET_FUNC (symbolP, 0);
2538       ARM_SET_THUMB (symbolP, 0);
2539       ARM_SET_INTERWORK (symbolP, support_interwork);
2540       break;
2541
2542     case MAP_THUMB:
2543       THUMB_SET_FUNC (symbolP, 1);
2544       ARM_SET_THUMB (symbolP, 1);
2545       ARM_SET_INTERWORK (symbolP, support_interwork);
2546       break;
2547
2548     case MAP_DATA:
2549     default:
2550       break;
2551     }
2552
2553   /* Save the mapping symbols for future reference.  Also check that
2554      we do not place two mapping symbols at the same offset within a
2555      frag.  We'll handle overlap between frags in
2556      check_mapping_symbols.
2557
2558      If .fill or other data filling directive generates zero sized data,
2559      the mapping symbol for the following code will have the same value
2560      as the one generated for the data filling directive.  In this case,
2561      we replace the old symbol with the new one at the same address.  */
2562   if (value == 0)
2563     {
2564       if (frag->tc_frag_data.first_map != NULL)
2565         {
2566           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2567           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2568         }
2569       frag->tc_frag_data.first_map = symbolP;
2570     }
2571   if (frag->tc_frag_data.last_map != NULL)
2572     {
2573       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2574       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2575         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2576     }
2577   frag->tc_frag_data.last_map = symbolP;
2578 }
2579
2580 /* We must sometimes convert a region marked as code to data during
2581    code alignment, if an odd number of bytes have to be padded.  The
2582    code mapping symbol is pushed to an aligned address.  */
2583
2584 static void
2585 insert_data_mapping_symbol (enum mstate state,
2586                             valueT value, fragS *frag, offsetT bytes)
2587 {
2588   /* If there was already a mapping symbol, remove it.  */
2589   if (frag->tc_frag_data.last_map != NULL
2590       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2591     {
2592       symbolS *symp = frag->tc_frag_data.last_map;
2593
2594       if (value == 0)
2595         {
2596           know (frag->tc_frag_data.first_map == symp);
2597           frag->tc_frag_data.first_map = NULL;
2598         }
2599       frag->tc_frag_data.last_map = NULL;
2600       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2601     }
2602
2603   make_mapping_symbol (MAP_DATA, value, frag);
2604   make_mapping_symbol (state, value + bytes, frag);
2605 }
2606
2607 static void mapping_state_2 (enum mstate state, int max_chars);
2608
2609 /* Set the mapping state to STATE.  Only call this when about to
2610    emit some STATE bytes to the file.  */
2611
2612 void
2613 mapping_state (enum mstate state)
2614 {
2615   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2616
2617 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2618
2619   if (mapstate == state)
2620     /* The mapping symbol has already been emitted.
2621        There is nothing else to do.  */
2622     return;
2623
2624   if (state == MAP_ARM || state == MAP_THUMB)
2625     /*  PR gas/12931
2626         All ARM instructions require 4-byte alignment.
2627         (Almost) all Thumb instructions require 2-byte alignment.
2628
2629         When emitting instructions into any section, mark the section
2630         appropriately.
2631
2632         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2633         but themselves require 2-byte alignment; this applies to some
2634         PC- relative forms.  However, these cases will invovle implicit
2635         literal pool generation or an explicit .align >=2, both of
2636         which will cause the section to me marked with sufficient
2637         alignment.  Thus, we don't handle those cases here.  */
2638     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2639
2640   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2641     /* This case will be evaluated later in the next else.  */
2642     return;
2643   else if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2644           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2645     {
2646       /* Only add the symbol if the offset is > 0:
2647          if we're at the first frag, check it's size > 0;
2648          if we're not at the first frag, then for sure
2649             the offset is > 0.  */
2650       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2651       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2652
2653       if (add_symbol)
2654         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2655     }
2656
2657   mapping_state_2 (state, 0);
2658 #undef TRANSITION
2659 }
2660
2661 /* Same as mapping_state, but MAX_CHARS bytes have already been
2662    allocated.  Put the mapping symbol that far back.  */
2663
2664 static void
2665 mapping_state_2 (enum mstate state, int max_chars)
2666 {
2667   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2668
2669   if (!SEG_NORMAL (now_seg))
2670     return;
2671
2672   if (mapstate == state)
2673     /* The mapping symbol has already been emitted.
2674        There is nothing else to do.  */
2675     return;
2676
2677   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2678   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2679 }
2680 #else
2681 #define mapping_state(x) ((void)0)
2682 #define mapping_state_2(x, y) ((void)0)
2683 #endif
2684
2685 /* Find the real, Thumb encoded start of a Thumb function.  */
2686
2687 #ifdef OBJ_COFF
2688 static symbolS *
2689 find_real_start (symbolS * symbolP)
2690 {
2691   char *       real_start;
2692   const char * name = S_GET_NAME (symbolP);
2693   symbolS *    new_target;
2694
2695   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2696 #define STUB_NAME ".real_start_of"
2697
2698   if (name == NULL)
2699     abort ();
2700
2701   /* The compiler may generate BL instructions to local labels because
2702      it needs to perform a branch to a far away location. These labels
2703      do not have a corresponding ".real_start_of" label.  We check
2704      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2705      the ".real_start_of" convention for nonlocal branches.  */
2706   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2707     return symbolP;
2708
2709   real_start = ACONCAT ((STUB_NAME, name, NULL));
2710   new_target = symbol_find (real_start);
2711
2712   if (new_target == NULL)
2713     {
2714       as_warn (_("Failed to find real start of function: %s\n"), name);
2715       new_target = symbolP;
2716     }
2717
2718   return new_target;
2719 }
2720 #endif
2721
2722 static void
2723 opcode_select (int width)
2724 {
2725   switch (width)
2726     {
2727     case 16:
2728       if (! thumb_mode)
2729         {
2730           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2731             as_bad (_("selected processor does not support THUMB opcodes"));
2732
2733           thumb_mode = 1;
2734           /* No need to force the alignment, since we will have been
2735              coming from ARM mode, which is word-aligned.  */
2736           record_alignment (now_seg, 1);
2737         }
2738       break;
2739
2740     case 32:
2741       if (thumb_mode)
2742         {
2743           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2744             as_bad (_("selected processor does not support ARM opcodes"));
2745
2746           thumb_mode = 0;
2747
2748           if (!need_pass_2)
2749             frag_align (2, 0, 0);
2750
2751           record_alignment (now_seg, 1);
2752         }
2753       break;
2754
2755     default:
2756       as_bad (_("invalid instruction size selected (%d)"), width);
2757     }
2758 }
2759
2760 static void
2761 s_arm (int ignore ATTRIBUTE_UNUSED)
2762 {
2763   opcode_select (32);
2764   demand_empty_rest_of_line ();
2765 }
2766
2767 static void
2768 s_thumb (int ignore ATTRIBUTE_UNUSED)
2769 {
2770   opcode_select (16);
2771   demand_empty_rest_of_line ();
2772 }
2773
2774 static void
2775 s_code (int unused ATTRIBUTE_UNUSED)
2776 {
2777   int temp;
2778
2779   temp = get_absolute_expression ();
2780   switch (temp)
2781     {
2782     case 16:
2783     case 32:
2784       opcode_select (temp);
2785       break;
2786
2787     default:
2788       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2789     }
2790 }
2791
2792 static void
2793 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2794 {
2795   /* If we are not already in thumb mode go into it, EVEN if
2796      the target processor does not support thumb instructions.
2797      This is used by gcc/config/arm/lib1funcs.asm for example
2798      to compile interworking support functions even if the
2799      target processor should not support interworking.  */
2800   if (! thumb_mode)
2801     {
2802       thumb_mode = 2;
2803       record_alignment (now_seg, 1);
2804     }
2805
2806   demand_empty_rest_of_line ();
2807 }
2808
2809 static void
2810 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2811 {
2812   s_thumb (0);
2813
2814   /* The following label is the name/address of the start of a Thumb function.
2815      We need to know this for the interworking support.  */
2816   label_is_thumb_function_name = TRUE;
2817 }
2818
2819 /* Perform a .set directive, but also mark the alias as
2820    being a thumb function.  */
2821
2822 static void
2823 s_thumb_set (int equiv)
2824 {
2825   /* XXX the following is a duplicate of the code for s_set() in read.c
2826      We cannot just call that code as we need to get at the symbol that
2827      is created.  */
2828   char *    name;
2829   char      delim;
2830   char *    end_name;
2831   symbolS * symbolP;
2832
2833   /* Especial apologies for the random logic:
2834      This just grew, and could be parsed much more simply!
2835      Dean - in haste.  */
2836   name      = input_line_pointer;
2837   delim     = get_symbol_end ();
2838   end_name  = input_line_pointer;
2839   *end_name = delim;
2840
2841   if (*input_line_pointer != ',')
2842     {
2843       *end_name = 0;
2844       as_bad (_("expected comma after name \"%s\""), name);
2845       *end_name = delim;
2846       ignore_rest_of_line ();
2847       return;
2848     }
2849
2850   input_line_pointer++;
2851   *end_name = 0;
2852
2853   if (name[0] == '.' && name[1] == '\0')
2854     {
2855       /* XXX - this should not happen to .thumb_set.  */
2856       abort ();
2857     }
2858
2859   if ((symbolP = symbol_find (name)) == NULL
2860       && (symbolP = md_undefined_symbol (name)) == NULL)
2861     {
2862 #ifndef NO_LISTING
2863       /* When doing symbol listings, play games with dummy fragments living
2864          outside the normal fragment chain to record the file and line info
2865          for this symbol.  */
2866       if (listing & LISTING_SYMBOLS)
2867         {
2868           extern struct list_info_struct * listing_tail;
2869           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2870
2871           memset (dummy_frag, 0, sizeof (fragS));
2872           dummy_frag->fr_type = rs_fill;
2873           dummy_frag->line = listing_tail;
2874           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2875           dummy_frag->fr_symbol = symbolP;
2876         }
2877       else
2878 #endif
2879         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2880
2881 #ifdef OBJ_COFF
2882       /* "set" symbols are local unless otherwise specified.  */
2883       SF_SET_LOCAL (symbolP);
2884 #endif /* OBJ_COFF  */
2885     }                           /* Make a new symbol.  */
2886
2887   symbol_table_insert (symbolP);
2888
2889   * end_name = delim;
2890
2891   if (equiv
2892       && S_IS_DEFINED (symbolP)
2893       && S_GET_SEGMENT (symbolP) != reg_section)
2894     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2895
2896   pseudo_set (symbolP);
2897
2898   demand_empty_rest_of_line ();
2899
2900   /* XXX Now we come to the Thumb specific bit of code.  */
2901
2902   THUMB_SET_FUNC (symbolP, 1);
2903   ARM_SET_THUMB (symbolP, 1);
2904 #if defined OBJ_ELF || defined OBJ_COFF
2905   ARM_SET_INTERWORK (symbolP, support_interwork);
2906 #endif
2907 }
2908
2909 /* Directives: Mode selection.  */
2910
2911 /* .syntax [unified|divided] - choose the new unified syntax
2912    (same for Arm and Thumb encoding, modulo slight differences in what
2913    can be represented) or the old divergent syntax for each mode.  */
2914 static void
2915 s_syntax (int unused ATTRIBUTE_UNUSED)
2916 {
2917   char *name, delim;
2918
2919   name = input_line_pointer;
2920   delim = get_symbol_end ();
2921
2922   if (!strcasecmp (name, "unified"))
2923     unified_syntax = TRUE;
2924   else if (!strcasecmp (name, "divided"))
2925     unified_syntax = FALSE;
2926   else
2927     {
2928       as_bad (_("unrecognized syntax mode \"%s\""), name);
2929       return;
2930     }
2931   *input_line_pointer = delim;
2932   demand_empty_rest_of_line ();
2933 }
2934
2935 /* Directives: sectioning and alignment.  */
2936
2937 /* Same as s_align_ptwo but align 0 => align 2.  */
2938
2939 static void
2940 s_align (int unused ATTRIBUTE_UNUSED)
2941 {
2942   int temp;
2943   bfd_boolean fill_p;
2944   long temp_fill;
2945   long max_alignment = 15;
2946
2947   temp = get_absolute_expression ();
2948   if (temp > max_alignment)
2949     as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
2950   else if (temp < 0)
2951     {
2952       as_bad (_("alignment negative. 0 assumed."));
2953       temp = 0;
2954     }
2955
2956   if (*input_line_pointer == ',')
2957     {
2958       input_line_pointer++;
2959       temp_fill = get_absolute_expression ();
2960       fill_p = TRUE;
2961     }
2962   else
2963     {
2964       fill_p = FALSE;
2965       temp_fill = 0;
2966     }
2967
2968   if (!temp)
2969     temp = 2;
2970
2971   /* Only make a frag if we HAVE to.  */
2972   if (temp && !need_pass_2)
2973     {
2974       if (!fill_p && subseg_text_p (now_seg))
2975         frag_align_code (temp, 0);
2976       else
2977         frag_align (temp, (int) temp_fill, 0);
2978     }
2979   demand_empty_rest_of_line ();
2980
2981   record_alignment (now_seg, temp);
2982 }
2983
2984 static void
2985 s_bss (int ignore ATTRIBUTE_UNUSED)
2986 {
2987   /* We don't support putting frags in the BSS segment, we fake it by
2988      marking in_bss, then looking at s_skip for clues.  */
2989   subseg_set (bss_section, 0);
2990   demand_empty_rest_of_line ();
2991
2992 #ifdef md_elf_section_change_hook
2993   md_elf_section_change_hook ();
2994 #endif
2995 }
2996
2997 static void
2998 s_even (int ignore ATTRIBUTE_UNUSED)
2999 {
3000   /* Never make frag if expect extra pass.  */
3001   if (!need_pass_2)
3002     frag_align (1, 0, 0);
3003
3004   record_alignment (now_seg, 1);
3005
3006   demand_empty_rest_of_line ();
3007 }
3008
3009 /* Directives: Literal pools.  */
3010
3011 static literal_pool *
3012 find_literal_pool (void)
3013 {
3014   literal_pool * pool;
3015
3016   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3017     {
3018       if (pool->section == now_seg
3019           && pool->sub_section == now_subseg)
3020         break;
3021     }
3022
3023   return pool;
3024 }
3025
3026 static literal_pool *
3027 find_or_make_literal_pool (void)
3028 {
3029   /* Next literal pool ID number.  */
3030   static unsigned int latest_pool_num = 1;
3031   literal_pool *      pool;
3032
3033   pool = find_literal_pool ();
3034
3035   if (pool == NULL)
3036     {
3037       /* Create a new pool.  */
3038       pool = (literal_pool *) xmalloc (sizeof (* pool));
3039       if (! pool)
3040         return NULL;
3041
3042       pool->next_free_entry = 0;
3043       pool->section         = now_seg;
3044       pool->sub_section     = now_subseg;
3045       pool->next            = list_of_pools;
3046       pool->symbol          = NULL;
3047
3048       /* Add it to the list.  */
3049       list_of_pools = pool;
3050     }
3051
3052   /* New pools, and emptied pools, will have a NULL symbol.  */
3053   if (pool->symbol == NULL)
3054     {
3055       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3056                                     (valueT) 0, &zero_address_frag);
3057       pool->id = latest_pool_num ++;
3058     }
3059
3060   /* Done.  */
3061   return pool;
3062 }
3063
3064 /* Add the literal in the global 'inst'
3065    structure to the relevant literal pool.  */
3066
3067 static int
3068 add_to_lit_pool (void)
3069 {
3070   literal_pool * pool;
3071   unsigned int entry;
3072
3073   pool = find_or_make_literal_pool ();
3074
3075   /* Check if this literal value is already in the pool.  */
3076   for (entry = 0; entry < pool->next_free_entry; entry ++)
3077     {
3078       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3079           && (inst.reloc.exp.X_op == O_constant)
3080           && (pool->literals[entry].X_add_number
3081               == inst.reloc.exp.X_add_number)
3082           && (pool->literals[entry].X_unsigned
3083               == inst.reloc.exp.X_unsigned))
3084         break;
3085
3086       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3087           && (inst.reloc.exp.X_op == O_symbol)
3088           && (pool->literals[entry].X_add_number
3089               == inst.reloc.exp.X_add_number)
3090           && (pool->literals[entry].X_add_symbol
3091               == inst.reloc.exp.X_add_symbol)
3092           && (pool->literals[entry].X_op_symbol
3093               == inst.reloc.exp.X_op_symbol))
3094         break;
3095     }
3096
3097   /* Do we need to create a new entry?  */
3098   if (entry == pool->next_free_entry)
3099     {
3100       if (entry >= MAX_LITERAL_POOL_SIZE)
3101         {
3102           inst.error = _("literal pool overflow");
3103           return FAIL;
3104         }
3105
3106       pool->literals[entry] = inst.reloc.exp;
3107 #ifdef OBJ_ELF
3108       /* PR ld/12974: Record the location of the first source line to reference
3109          this entry in the literal pool.  If it turns out during linking that the
3110          symbol does not exist we will be able to give an accurate line number for
3111          the (first use of the) missing reference.  */
3112       if (debug_type == DEBUG_DWARF2)
3113         dwarf2_where (pool->locs + entry);
3114 #endif
3115       pool->next_free_entry += 1;
3116     }
3117
3118   inst.reloc.exp.X_op         = O_symbol;
3119   inst.reloc.exp.X_add_number = ((int) entry) * 4;
3120   inst.reloc.exp.X_add_symbol = pool->symbol;
3121
3122   return SUCCESS;
3123 }
3124
3125 /* Can't use symbol_new here, so have to create a symbol and then at
3126    a later date assign it a value. Thats what these functions do.  */
3127
3128 static void
3129 symbol_locate (symbolS *    symbolP,
3130                const char * name,       /* It is copied, the caller can modify.  */
3131                segT         segment,    /* Segment identifier (SEG_<something>).  */
3132                valueT       valu,       /* Symbol value.  */
3133                fragS *      frag)       /* Associated fragment.  */
3134 {
3135   unsigned int name_length;
3136   char * preserved_copy_of_name;
3137
3138   name_length = strlen (name) + 1;   /* +1 for \0.  */
3139   obstack_grow (&notes, name, name_length);
3140   preserved_copy_of_name = (char *) obstack_finish (&notes);
3141
3142 #ifdef tc_canonicalize_symbol_name
3143   preserved_copy_of_name =
3144     tc_canonicalize_symbol_name (preserved_copy_of_name);
3145 #endif
3146
3147   S_SET_NAME (symbolP, preserved_copy_of_name);
3148
3149   S_SET_SEGMENT (symbolP, segment);
3150   S_SET_VALUE (symbolP, valu);
3151   symbol_clear_list_pointers (symbolP);
3152
3153   symbol_set_frag (symbolP, frag);
3154
3155   /* Link to end of symbol chain.  */
3156   {
3157     extern int symbol_table_frozen;
3158
3159     if (symbol_table_frozen)
3160       abort ();
3161   }
3162
3163   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3164
3165   obj_symbol_new_hook (symbolP);
3166
3167 #ifdef tc_symbol_new_hook
3168   tc_symbol_new_hook (symbolP);
3169 #endif
3170
3171 #ifdef DEBUG_SYMS
3172   verify_symbol_chain (symbol_rootP, symbol_lastP);
3173 #endif /* DEBUG_SYMS  */
3174 }
3175
3176
3177 static void
3178 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3179 {
3180   unsigned int entry;
3181   literal_pool * pool;
3182   char sym_name[20];
3183
3184   pool = find_literal_pool ();
3185   if (pool == NULL
3186       || pool->symbol == NULL
3187       || pool->next_free_entry == 0)
3188     return;
3189
3190   mapping_state (MAP_DATA);
3191
3192   /* Align pool as you have word accesses.
3193      Only make a frag if we have to.  */
3194   if (!need_pass_2)
3195     frag_align (2, 0, 0);
3196
3197   record_alignment (now_seg, 2);
3198
3199   sprintf (sym_name, "$$lit_\002%x", pool->id);
3200
3201   symbol_locate (pool->symbol, sym_name, now_seg,
3202                  (valueT) frag_now_fix (), frag_now);
3203   symbol_table_insert (pool->symbol);
3204
3205   ARM_SET_THUMB (pool->symbol, thumb_mode);
3206
3207 #if defined OBJ_COFF || defined OBJ_ELF
3208   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3209 #endif
3210
3211   for (entry = 0; entry < pool->next_free_entry; entry ++)
3212     {
3213 #ifdef OBJ_ELF
3214       if (debug_type == DEBUG_DWARF2)
3215         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3216 #endif
3217       /* First output the expression in the instruction to the pool.  */
3218       emit_expr (&(pool->literals[entry]), 4); /* .word  */
3219     }
3220
3221   /* Mark the pool as empty.  */
3222   pool->next_free_entry = 0;
3223   pool->symbol = NULL;
3224 }
3225
3226 #ifdef OBJ_ELF
3227 /* Forward declarations for functions below, in the MD interface
3228    section.  */
3229 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3230 static valueT create_unwind_entry (int);
3231 static void start_unwind_section (const segT, int);
3232 static void add_unwind_opcode (valueT, int);
3233 static void flush_pending_unwind (void);
3234
3235 /* Directives: Data.  */
3236
3237 static void
3238 s_arm_elf_cons (int nbytes)
3239 {
3240   expressionS exp;
3241
3242 #ifdef md_flush_pending_output
3243   md_flush_pending_output ();
3244 #endif
3245
3246   if (is_it_end_of_statement ())
3247     {
3248       demand_empty_rest_of_line ();
3249       return;
3250     }
3251
3252 #ifdef md_cons_align
3253   md_cons_align (nbytes);
3254 #endif
3255
3256   mapping_state (MAP_DATA);
3257   do
3258     {
3259       int reloc;
3260       char *base = input_line_pointer;
3261
3262       expression (& exp);
3263
3264       if (exp.X_op != O_symbol)
3265         emit_expr (&exp, (unsigned int) nbytes);
3266       else
3267         {
3268           char *before_reloc = input_line_pointer;
3269           reloc = parse_reloc (&input_line_pointer);
3270           if (reloc == -1)
3271             {
3272               as_bad (_("unrecognized relocation suffix"));
3273               ignore_rest_of_line ();
3274               return;
3275             }
3276           else if (reloc == BFD_RELOC_UNUSED)
3277             emit_expr (&exp, (unsigned int) nbytes);
3278           else
3279             {
3280               reloc_howto_type *howto = (reloc_howto_type *)
3281                   bfd_reloc_type_lookup (stdoutput,
3282                                          (bfd_reloc_code_real_type) reloc);
3283               int size = bfd_get_reloc_size (howto);
3284
3285               if (reloc == BFD_RELOC_ARM_PLT32)
3286                 {
3287                   as_bad (_("(plt) is only valid on branch targets"));
3288                   reloc = BFD_RELOC_UNUSED;
3289                   size = 0;
3290                 }
3291
3292               if (size > nbytes)
3293                 as_bad (_("%s relocations do not fit in %d bytes"),
3294                         howto->name, nbytes);
3295               else
3296                 {
3297                   /* We've parsed an expression stopping at O_symbol.
3298                      But there may be more expression left now that we
3299                      have parsed the relocation marker.  Parse it again.
3300                      XXX Surely there is a cleaner way to do this.  */
3301                   char *p = input_line_pointer;
3302                   int offset;
3303                   char *save_buf = (char *) alloca (input_line_pointer - base);
3304                   memcpy (save_buf, base, input_line_pointer - base);
3305                   memmove (base + (input_line_pointer - before_reloc),
3306                            base, before_reloc - base);
3307
3308                   input_line_pointer = base + (input_line_pointer-before_reloc);
3309                   expression (&exp);
3310                   memcpy (base, save_buf, p - base);
3311
3312                   offset = nbytes - size;
3313                   p = frag_more ((int) nbytes);
3314                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3315                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3316                 }
3317             }
3318         }
3319     }
3320   while (*input_line_pointer++ == ',');
3321
3322   /* Put terminator back into stream.  */
3323   input_line_pointer --;
3324   demand_empty_rest_of_line ();
3325 }
3326
3327 /* Emit an expression containing a 32-bit thumb instruction.
3328    Implementation based on put_thumb32_insn.  */
3329
3330 static void
3331 emit_thumb32_expr (expressionS * exp)
3332 {
3333   expressionS exp_high = *exp;
3334
3335   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3336   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3337   exp->X_add_number &= 0xffff;
3338   emit_expr (exp, (unsigned int) THUMB_SIZE);
3339 }
3340
3341 /*  Guess the instruction size based on the opcode.  */
3342
3343 static int
3344 thumb_insn_size (int opcode)
3345 {
3346   if ((unsigned int) opcode < 0xe800u)
3347     return 2;
3348   else if ((unsigned int) opcode >= 0xe8000000u)
3349     return 4;
3350   else
3351     return 0;
3352 }
3353
3354 static bfd_boolean
3355 emit_insn (expressionS *exp, int nbytes)
3356 {
3357   int size = 0;
3358
3359   if (exp->X_op == O_constant)
3360     {
3361       size = nbytes;
3362
3363       if (size == 0)
3364         size = thumb_insn_size (exp->X_add_number);
3365
3366       if (size != 0)
3367         {
3368           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3369             {
3370               as_bad (_(".inst.n operand too big. "\
3371                         "Use .inst.w instead"));
3372               size = 0;
3373             }
3374           else
3375             {
3376               if (now_it.state == AUTOMATIC_IT_BLOCK)
3377                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3378               else
3379                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3380
3381               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3382                 emit_thumb32_expr (exp);
3383               else
3384                 emit_expr (exp, (unsigned int) size);
3385
3386               it_fsm_post_encode ();
3387             }
3388         }
3389       else
3390         as_bad (_("cannot determine Thumb instruction size. "   \
3391                   "Use .inst.n/.inst.w instead"));
3392     }
3393   else
3394     as_bad (_("constant expression required"));
3395
3396   return (size != 0);
3397 }
3398
3399 /* Like s_arm_elf_cons but do not use md_cons_align and
3400    set the mapping state to MAP_ARM/MAP_THUMB.  */
3401
3402 static void
3403 s_arm_elf_inst (int nbytes)
3404 {
3405   if (is_it_end_of_statement ())
3406     {
3407       demand_empty_rest_of_line ();
3408       return;
3409     }
3410
3411   /* Calling mapping_state () here will not change ARM/THUMB,
3412      but will ensure not to be in DATA state.  */
3413
3414   if (thumb_mode)
3415     mapping_state (MAP_THUMB);
3416   else
3417     {
3418       if (nbytes != 0)
3419         {
3420           as_bad (_("width suffixes are invalid in ARM mode"));
3421           ignore_rest_of_line ();
3422           return;
3423         }
3424
3425       nbytes = 4;
3426
3427       mapping_state (MAP_ARM);
3428     }
3429
3430   do
3431     {
3432       expressionS exp;
3433
3434       expression (& exp);
3435
3436       if (! emit_insn (& exp, nbytes))
3437         {
3438           ignore_rest_of_line ();
3439           return;
3440         }
3441     }
3442   while (*input_line_pointer++ == ',');
3443
3444   /* Put terminator back into stream.  */
3445   input_line_pointer --;
3446   demand_empty_rest_of_line ();
3447 }
3448
3449 /* Parse a .rel31 directive.  */
3450
3451 static void
3452 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3453 {
3454   expressionS exp;
3455   char *p;
3456   valueT highbit;
3457
3458   highbit = 0;
3459   if (*input_line_pointer == '1')
3460     highbit = 0x80000000;
3461   else if (*input_line_pointer != '0')
3462     as_bad (_("expected 0 or 1"));
3463
3464   input_line_pointer++;
3465   if (*input_line_pointer != ',')
3466     as_bad (_("missing comma"));
3467   input_line_pointer++;
3468
3469 #ifdef md_flush_pending_output
3470   md_flush_pending_output ();
3471 #endif
3472
3473 #ifdef md_cons_align
3474   md_cons_align (4);
3475 #endif
3476
3477   mapping_state (MAP_DATA);
3478
3479   expression (&exp);
3480
3481   p = frag_more (4);
3482   md_number_to_chars (p, highbit, 4);
3483   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3484                BFD_RELOC_ARM_PREL31);
3485
3486   demand_empty_rest_of_line ();
3487 }
3488
3489 /* Directives: AEABI stack-unwind tables.  */
3490
3491 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3492
3493 static void
3494 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3495 {
3496   demand_empty_rest_of_line ();
3497   if (unwind.proc_start)
3498     {
3499       as_bad (_("duplicate .fnstart directive"));
3500       return;
3501     }
3502
3503   /* Mark the start of the function.  */
3504   unwind.proc_start = expr_build_dot ();
3505
3506   /* Reset the rest of the unwind info.  */
3507   unwind.opcode_count = 0;
3508   unwind.table_entry = NULL;
3509   unwind.personality_routine = NULL;
3510   unwind.personality_index = -1;
3511   unwind.frame_size = 0;
3512   unwind.fp_offset = 0;
3513   unwind.fp_reg = REG_SP;
3514   unwind.fp_used = 0;
3515   unwind.sp_restored = 0;
3516 }
3517
3518
3519 /* Parse a handlerdata directive.  Creates the exception handling table entry
3520    for the function.  */
3521
3522 static void
3523 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3524 {
3525   demand_empty_rest_of_line ();
3526   if (!unwind.proc_start)
3527     as_bad (MISSING_FNSTART);
3528
3529   if (unwind.table_entry)
3530     as_bad (_("duplicate .handlerdata directive"));
3531
3532   create_unwind_entry (1);
3533 }
3534
3535 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3536
3537 static void
3538 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3539 {
3540   long where;
3541   char *ptr;
3542   valueT val;
3543   unsigned int marked_pr_dependency;
3544
3545   demand_empty_rest_of_line ();
3546
3547   if (!unwind.proc_start)
3548     {
3549       as_bad (_(".fnend directive without .fnstart"));
3550       return;
3551     }
3552
3553   /* Add eh table entry.  */
3554   if (unwind.table_entry == NULL)
3555     val = create_unwind_entry (0);
3556   else
3557     val = 0;
3558
3559   /* Add index table entry.  This is two words.  */
3560   start_unwind_section (unwind.saved_seg, 1);
3561   frag_align (2, 0, 0);
3562   record_alignment (now_seg, 2);
3563
3564   ptr = frag_more (8);
3565   memset (ptr, 0, 8);
3566   where = frag_now_fix () - 8;
3567
3568   /* Self relative offset of the function start.  */
3569   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3570            BFD_RELOC_ARM_PREL31);
3571
3572   /* Indicate dependency on EHABI-defined personality routines to the
3573      linker, if it hasn't been done already.  */
3574   marked_pr_dependency
3575     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3576   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3577       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3578     {
3579       static const char *const name[] =
3580         {
3581           "__aeabi_unwind_cpp_pr0",
3582           "__aeabi_unwind_cpp_pr1",
3583           "__aeabi_unwind_cpp_pr2"
3584         };
3585       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3586       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3587       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3588         |= 1 << unwind.personality_index;
3589     }
3590
3591   if (val)
3592     /* Inline exception table entry.  */
3593     md_number_to_chars (ptr + 4, val, 4);
3594   else
3595     /* Self relative offset of the table entry.  */
3596     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3597              BFD_RELOC_ARM_PREL31);
3598
3599   /* Restore the original section.  */
3600   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3601
3602   unwind.proc_start = NULL;
3603 }
3604
3605
3606 /* Parse an unwind_cantunwind directive.  */
3607
3608 static void
3609 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3610 {
3611   demand_empty_rest_of_line ();
3612   if (!unwind.proc_start)
3613     as_bad (MISSING_FNSTART);
3614
3615   if (unwind.personality_routine || unwind.personality_index != -1)
3616     as_bad (_("personality routine specified for cantunwind frame"));
3617
3618   unwind.personality_index = -2;
3619 }
3620
3621
3622 /* Parse a personalityindex directive.  */
3623
3624 static void
3625 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3626 {
3627   expressionS exp;
3628
3629   if (!unwind.proc_start)
3630     as_bad (MISSING_FNSTART);
3631
3632   if (unwind.personality_routine || unwind.personality_index != -1)
3633     as_bad (_("duplicate .personalityindex directive"));
3634
3635   expression (&exp);
3636
3637   if (exp.X_op != O_constant
3638       || exp.X_add_number < 0 || exp.X_add_number > 15)
3639     {
3640       as_bad (_("bad personality routine number"));
3641       ignore_rest_of_line ();
3642       return;
3643     }
3644
3645   unwind.personality_index = exp.X_add_number;
3646
3647   demand_empty_rest_of_line ();
3648 }
3649
3650
3651 /* Parse a personality directive.  */
3652
3653 static void
3654 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3655 {
3656   char *name, *p, c;
3657
3658   if (!unwind.proc_start)
3659     as_bad (MISSING_FNSTART);
3660
3661   if (unwind.personality_routine || unwind.personality_index != -1)
3662     as_bad (_("duplicate .personality directive"));
3663
3664   name = input_line_pointer;
3665   c = get_symbol_end ();
3666   p = input_line_pointer;
3667   unwind.personality_routine = symbol_find_or_make (name);
3668   *p = c;
3669   demand_empty_rest_of_line ();
3670 }
3671
3672
3673 /* Parse a directive saving core registers.  */
3674
3675 static void
3676 s_arm_unwind_save_core (void)
3677 {
3678   valueT op;
3679   long range;
3680   int n;
3681
3682   range = parse_reg_list (&input_line_pointer);
3683   if (range == FAIL)
3684     {
3685       as_bad (_("expected register list"));
3686       ignore_rest_of_line ();
3687       return;
3688     }
3689
3690   demand_empty_rest_of_line ();
3691
3692   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3693      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3694      ip because it is clobbered by calls.  */
3695   if (unwind.sp_restored && unwind.fp_reg == 12
3696       && (range & 0x3000) == 0x1000)
3697     {
3698       unwind.opcode_count--;
3699       unwind.sp_restored = 0;
3700       range = (range | 0x2000) & ~0x1000;
3701       unwind.pending_offset = 0;
3702     }
3703
3704   /* Pop r4-r15.  */
3705   if (range & 0xfff0)
3706     {
3707       /* See if we can use the short opcodes.  These pop a block of up to 8
3708          registers starting with r4, plus maybe r14.  */
3709       for (n = 0; n < 8; n++)
3710         {
3711           /* Break at the first non-saved register.      */
3712           if ((range & (1 << (n + 4))) == 0)
3713             break;
3714         }
3715       /* See if there are any other bits set.  */
3716       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
3717         {
3718           /* Use the long form.  */
3719           op = 0x8000 | ((range >> 4) & 0xfff);
3720           add_unwind_opcode (op, 2);
3721         }
3722       else
3723         {
3724           /* Use the short form.  */
3725           if (range & 0x4000)
3726             op = 0xa8; /* Pop r14.      */
3727           else
3728             op = 0xa0; /* Do not pop r14.  */
3729           op |= (n - 1);
3730           add_unwind_opcode (op, 1);
3731         }
3732     }
3733
3734   /* Pop r0-r3.  */
3735   if (range & 0xf)
3736     {
3737       op = 0xb100 | (range & 0xf);
3738       add_unwind_opcode (op, 2);
3739     }
3740
3741   /* Record the number of bytes pushed.  */
3742   for (n = 0; n < 16; n++)
3743     {
3744       if (range & (1 << n))
3745         unwind.frame_size += 4;
3746     }
3747 }
3748
3749
3750 /* Parse a directive saving FPA registers.  */
3751
3752 static void
3753 s_arm_unwind_save_fpa (int reg)
3754 {
3755   expressionS exp;
3756   int num_regs;
3757   valueT op;
3758
3759   /* Get Number of registers to transfer.  */
3760   if (skip_past_comma (&input_line_pointer) != FAIL)
3761     expression (&exp);
3762   else
3763     exp.X_op = O_illegal;
3764
3765   if (exp.X_op != O_constant)
3766     {
3767       as_bad (_("expected , <constant>"));
3768       ignore_rest_of_line ();
3769       return;
3770     }
3771
3772   num_regs = exp.X_add_number;
3773
3774   if (num_regs < 1 || num_regs > 4)
3775     {
3776       as_bad (_("number of registers must be in the range [1:4]"));
3777       ignore_rest_of_line ();
3778       return;
3779     }
3780
3781   demand_empty_rest_of_line ();
3782
3783   if (reg == 4)
3784     {
3785       /* Short form.  */
3786       op = 0xb4 | (num_regs - 1);
3787       add_unwind_opcode (op, 1);
3788     }
3789   else
3790     {
3791       /* Long form.  */
3792       op = 0xc800 | (reg << 4) | (num_regs - 1);
3793       add_unwind_opcode (op, 2);
3794     }
3795   unwind.frame_size += num_regs * 12;
3796 }
3797
3798
3799 /* Parse a directive saving VFP registers for ARMv6 and above.  */
3800
3801 static void
3802 s_arm_unwind_save_vfp_armv6 (void)
3803 {
3804   int count;
3805   unsigned int start;
3806   valueT op;
3807   int num_vfpv3_regs = 0;
3808   int num_regs_below_16;
3809
3810   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
3811   if (count == FAIL)
3812     {
3813       as_bad (_("expected register list"));
3814       ignore_rest_of_line ();
3815       return;
3816     }
3817
3818   demand_empty_rest_of_line ();
3819
3820   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
3821      than FSTMX/FLDMX-style ones).  */
3822
3823   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
3824   if (start >= 16)
3825     num_vfpv3_regs = count;
3826   else if (start + count > 16)
3827     num_vfpv3_regs = start + count - 16;
3828
3829   if (num_vfpv3_regs > 0)
3830     {
3831       int start_offset = start > 16 ? start - 16 : 0;
3832       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
3833       add_unwind_opcode (op, 2);
3834     }
3835
3836   /* Generate opcode for registers numbered in the range 0 .. 15.  */
3837   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
3838   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
3839   if (num_regs_below_16 > 0)
3840     {
3841       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
3842       add_unwind_opcode (op, 2);
3843     }
3844
3845   unwind.frame_size += count * 8;
3846 }
3847
3848
3849 /* Parse a directive saving VFP registers for pre-ARMv6.  */
3850
3851 static void
3852 s_arm_unwind_save_vfp (void)
3853 {
3854   int count;
3855   unsigned int reg;
3856   valueT op;
3857
3858   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
3859   if (count == FAIL)
3860     {
3861       as_bad (_("expected register list"));
3862       ignore_rest_of_line ();
3863       return;
3864     }
3865
3866   demand_empty_rest_of_line ();
3867
3868   if (reg == 8)
3869     {
3870       /* Short form.  */
3871       op = 0xb8 | (count - 1);
3872       add_unwind_opcode (op, 1);
3873     }
3874   else
3875     {
3876       /* Long form.  */
3877       op = 0xb300 | (reg << 4) | (count - 1);
3878       add_unwind_opcode (op, 2);
3879     }
3880   unwind.frame_size += count * 8 + 4;
3881 }
3882
3883
3884 /* Parse a directive saving iWMMXt data registers.  */
3885
3886 static void
3887 s_arm_unwind_save_mmxwr (void)
3888 {
3889   int reg;
3890   int hi_reg;
3891   int i;
3892   unsigned mask = 0;
3893   valueT op;
3894
3895   if (*input_line_pointer == '{')
3896     input_line_pointer++;
3897
3898   do
3899     {
3900       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3901
3902       if (reg == FAIL)
3903         {
3904           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
3905           goto error;
3906         }
3907
3908       if (mask >> reg)
3909         as_tsktsk (_("register list not in ascending order"));
3910       mask |= 1 << reg;
3911
3912       if (*input_line_pointer == '-')
3913         {
3914           input_line_pointer++;
3915           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3916           if (hi_reg == FAIL)
3917             {
3918               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
3919               goto error;
3920             }
3921           else if (reg >= hi_reg)
3922             {
3923               as_bad (_("bad register range"));
3924               goto error;
3925             }
3926           for (; reg < hi_reg; reg++)
3927             mask |= 1 << reg;
3928         }
3929     }
3930   while (skip_past_comma (&input_line_pointer) != FAIL);
3931
3932   if (*input_line_pointer == '}')
3933     input_line_pointer++;
3934
3935   demand_empty_rest_of_line ();
3936
3937   /* Generate any deferred opcodes because we're going to be looking at
3938      the list.  */
3939   flush_pending_unwind ();
3940
3941   for (i = 0; i < 16; i++)
3942     {
3943       if (mask & (1 << i))
3944         unwind.frame_size += 8;
3945     }
3946
3947   /* Attempt to combine with a previous opcode.  We do this because gcc
3948      likes to output separate unwind directives for a single block of
3949      registers.  */
3950   if (unwind.opcode_count > 0)
3951     {
3952       i = unwind.opcodes[unwind.opcode_count - 1];
3953       if ((i & 0xf8) == 0xc0)
3954         {
3955           i &= 7;
3956           /* Only merge if the blocks are contiguous.  */
3957           if (i < 6)
3958             {
3959               if ((mask & 0xfe00) == (1 << 9))
3960                 {
3961                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
3962                   unwind.opcode_count--;
3963                 }
3964             }
3965           else if (i == 6 && unwind.opcode_count >= 2)
3966             {
3967               i = unwind.opcodes[unwind.opcode_count - 2];
3968               reg = i >> 4;
3969               i &= 0xf;
3970
3971               op = 0xffff << (reg - 1);
3972               if (reg > 0
3973                   && ((mask & op) == (1u << (reg - 1))))
3974                 {
3975                   op = (1 << (reg + i + 1)) - 1;
3976                   op &= ~((1 << reg) - 1);
3977                   mask |= op;
3978                   unwind.opcode_count -= 2;
3979                 }
3980             }
3981         }
3982     }
3983
3984   hi_reg = 15;
3985   /* We want to generate opcodes in the order the registers have been
3986      saved, ie. descending order.  */
3987   for (reg = 15; reg >= -1; reg--)
3988     {
3989       /* Save registers in blocks.  */
3990       if (reg < 0
3991           || !(mask & (1 << reg)))
3992         {
3993           /* We found an unsaved reg.  Generate opcodes to save the
3994              preceding block.   */
3995           if (reg != hi_reg)
3996             {
3997               if (reg == 9)
3998                 {
3999                   /* Short form.  */
4000                   op = 0xc0 | (hi_reg - 10);
4001                   add_unwind_opcode (op, 1);
4002                 }
4003               else
4004                 {
4005                   /* Long form.  */
4006                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4007                   add_unwind_opcode (op, 2);
4008                 }
4009             }
4010           hi_reg = reg - 1;
4011         }
4012     }
4013
4014   return;
4015 error:
4016   ignore_rest_of_line ();
4017 }
4018
4019 static void
4020 s_arm_unwind_save_mmxwcg (void)
4021 {
4022   int reg;
4023   int hi_reg;
4024   unsigned mask = 0;
4025   valueT op;
4026
4027   if (*input_line_pointer == '{')
4028     input_line_pointer++;
4029
4030   do
4031     {
4032       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4033
4034       if (reg == FAIL)
4035         {
4036           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4037           goto error;
4038         }
4039
4040       reg -= 8;
4041       if (mask >> reg)
4042         as_tsktsk (_("register list not in ascending order"));
4043       mask |= 1 << reg;
4044
4045       if (*input_line_pointer == '-')
4046         {
4047           input_line_pointer++;
4048           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4049           if (hi_reg == FAIL)
4050             {
4051               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4052               goto error;
4053             }
4054           else if (reg >= hi_reg)
4055             {
4056               as_bad (_("bad register range"));
4057               goto error;
4058             }
4059           for (; reg < hi_reg; reg++)
4060             mask |= 1 << reg;
4061         }
4062     }
4063   while (skip_past_comma (&input_line_pointer) != FAIL);
4064
4065   if (*input_line_pointer == '}')
4066     input_line_pointer++;
4067
4068   demand_empty_rest_of_line ();
4069
4070   /* Generate any deferred opcodes because we're going to be looking at
4071      the list.  */
4072   flush_pending_unwind ();
4073
4074   for (reg = 0; reg < 16; reg++)
4075     {
4076       if (mask & (1 << reg))
4077         unwind.frame_size += 4;
4078     }
4079   op = 0xc700 | mask;
4080   add_unwind_opcode (op, 2);
4081   return;
4082 error:
4083   ignore_rest_of_line ();
4084 }
4085
4086
4087 /* Parse an unwind_save directive.
4088    If the argument is non-zero, this is a .vsave directive.  */
4089
4090 static void
4091 s_arm_unwind_save (int arch_v6)
4092 {
4093   char *peek;
4094   struct reg_entry *reg;
4095   bfd_boolean had_brace = FALSE;
4096
4097   if (!unwind.proc_start)
4098     as_bad (MISSING_FNSTART);
4099
4100   /* Figure out what sort of save we have.  */
4101   peek = input_line_pointer;
4102
4103   if (*peek == '{')
4104     {
4105       had_brace = TRUE;
4106       peek++;
4107     }
4108
4109   reg = arm_reg_parse_multi (&peek);
4110
4111   if (!reg)
4112     {
4113       as_bad (_("register expected"));
4114       ignore_rest_of_line ();
4115       return;
4116     }
4117
4118   switch (reg->type)
4119     {
4120     case REG_TYPE_FN:
4121       if (had_brace)
4122         {
4123           as_bad (_("FPA .unwind_save does not take a register list"));
4124           ignore_rest_of_line ();
4125           return;
4126         }
4127       input_line_pointer = peek;
4128       s_arm_unwind_save_fpa (reg->number);
4129       return;
4130
4131     case REG_TYPE_RN:     s_arm_unwind_save_core ();   return;
4132     case REG_TYPE_VFD:
4133       if (arch_v6)
4134         s_arm_unwind_save_vfp_armv6 ();
4135       else
4136         s_arm_unwind_save_vfp ();
4137       return;
4138     case REG_TYPE_MMXWR:  s_arm_unwind_save_mmxwr ();  return;
4139     case REG_TYPE_MMXWCG: s_arm_unwind_save_mmxwcg (); return;
4140
4141     default:
4142       as_bad (_(".unwind_save does not support this kind of register"));
4143       ignore_rest_of_line ();
4144     }
4145 }
4146
4147
4148 /* Parse an unwind_movsp directive.  */
4149
4150 static void
4151 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4152 {
4153   int reg;
4154   valueT op;
4155   int offset;
4156
4157   if (!unwind.proc_start)
4158     as_bad (MISSING_FNSTART);
4159
4160   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4161   if (reg == FAIL)
4162     {
4163       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4164       ignore_rest_of_line ();
4165       return;
4166     }
4167
4168   /* Optional constant.  */
4169   if (skip_past_comma (&input_line_pointer) != FAIL)
4170     {
4171       if (immediate_for_directive (&offset) == FAIL)
4172         return;
4173     }
4174   else
4175     offset = 0;
4176
4177   demand_empty_rest_of_line ();
4178
4179   if (reg == REG_SP || reg == REG_PC)
4180     {
4181       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4182       return;
4183     }
4184
4185   if (unwind.fp_reg != REG_SP)
4186     as_bad (_("unexpected .unwind_movsp directive"));
4187
4188   /* Generate opcode to restore the value.  */
4189   op = 0x90 | reg;
4190   add_unwind_opcode (op, 1);
4191
4192   /* Record the information for later.  */
4193   unwind.fp_reg = reg;
4194   unwind.fp_offset = unwind.frame_size - offset;
4195   unwind.sp_restored = 1;
4196 }
4197
4198 /* Parse an unwind_pad directive.  */
4199
4200 static void
4201 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4202 {
4203   int offset;
4204
4205   if (!unwind.proc_start)
4206     as_bad (MISSING_FNSTART);
4207
4208   if (immediate_for_directive (&offset) == FAIL)
4209     return;
4210
4211   if (offset & 3)
4212     {
4213       as_bad (_("stack increment must be multiple of 4"));
4214       ignore_rest_of_line ();
4215       return;
4216     }
4217
4218   /* Don't generate any opcodes, just record the details for later.  */
4219   unwind.frame_size += offset;
4220   unwind.pending_offset += offset;
4221
4222   demand_empty_rest_of_line ();
4223 }
4224
4225 /* Parse an unwind_setfp directive.  */
4226
4227 static void
4228 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4229 {
4230   int sp_reg;
4231   int fp_reg;
4232   int offset;
4233
4234   if (!unwind.proc_start)
4235     as_bad (MISSING_FNSTART);
4236
4237   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4238   if (skip_past_comma (&input_line_pointer) == FAIL)
4239     sp_reg = FAIL;
4240   else
4241     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4242
4243   if (fp_reg == FAIL || sp_reg == FAIL)
4244     {
4245       as_bad (_("expected <reg>, <reg>"));
4246       ignore_rest_of_line ();
4247       return;
4248     }
4249
4250   /* Optional constant.  */
4251   if (skip_past_comma (&input_line_pointer) != FAIL)
4252     {
4253       if (immediate_for_directive (&offset) == FAIL)
4254         return;
4255     }
4256   else
4257     offset = 0;
4258
4259   demand_empty_rest_of_line ();
4260
4261   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4262     {
4263       as_bad (_("register must be either sp or set by a previous"
4264                 "unwind_movsp directive"));
4265       return;
4266     }
4267
4268   /* Don't generate any opcodes, just record the information for later.  */
4269   unwind.fp_reg = fp_reg;
4270   unwind.fp_used = 1;
4271   if (sp_reg == REG_SP)
4272     unwind.fp_offset = unwind.frame_size - offset;
4273   else
4274     unwind.fp_offset -= offset;
4275 }
4276
4277 /* Parse an unwind_raw directive.  */
4278
4279 static void
4280 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4281 {
4282   expressionS exp;
4283   /* This is an arbitrary limit.         */
4284   unsigned char op[16];
4285   int count;
4286
4287   if (!unwind.proc_start)
4288     as_bad (MISSING_FNSTART);
4289
4290   expression (&exp);
4291   if (exp.X_op == O_constant
4292       && skip_past_comma (&input_line_pointer) != FAIL)
4293     {
4294       unwind.frame_size += exp.X_add_number;
4295       expression (&exp);
4296     }
4297   else
4298     exp.X_op = O_illegal;
4299
4300   if (exp.X_op != O_constant)
4301     {
4302       as_bad (_("expected <offset>, <opcode>"));
4303       ignore_rest_of_line ();
4304       return;
4305     }
4306
4307   count = 0;
4308
4309   /* Parse the opcode.  */
4310   for (;;)
4311     {
4312       if (count >= 16)
4313         {
4314           as_bad (_("unwind opcode too long"));
4315           ignore_rest_of_line ();
4316         }
4317       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4318         {
4319           as_bad (_("invalid unwind opcode"));
4320           ignore_rest_of_line ();
4321           return;
4322         }
4323       op[count++] = exp.X_add_number;
4324
4325       /* Parse the next byte.  */
4326       if (skip_past_comma (&input_line_pointer) == FAIL)
4327         break;
4328
4329       expression (&exp);
4330     }
4331
4332   /* Add the opcode bytes in reverse order.  */
4333   while (count--)
4334     add_unwind_opcode (op[count], 1);
4335
4336   demand_empty_rest_of_line ();
4337 }
4338
4339
4340 /* Parse a .eabi_attribute directive.  */
4341
4342 static void
4343 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4344 {
4345   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4346
4347   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4348     attributes_set_explicitly[tag] = 1;
4349 }
4350
4351 /* Emit a tls fix for the symbol.  */
4352
4353 static void
4354 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4355 {
4356   char *p;
4357   expressionS exp;
4358 #ifdef md_flush_pending_output
4359   md_flush_pending_output ();
4360 #endif
4361
4362 #ifdef md_cons_align
4363   md_cons_align (4);
4364 #endif
4365
4366   /* Since we're just labelling the code, there's no need to define a
4367      mapping symbol.  */
4368   expression (&exp);
4369   p = obstack_next_free (&frchain_now->frch_obstack);
4370   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4371                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4372                : BFD_RELOC_ARM_TLS_DESCSEQ);
4373 }
4374 #endif /* OBJ_ELF */
4375
4376 static void s_arm_arch (int);
4377 static void s_arm_object_arch (int);
4378 static void s_arm_cpu (int);
4379 static void s_arm_fpu (int);
4380 static void s_arm_arch_extension (int);
4381
4382 #ifdef TE_PE
4383
4384 static void
4385 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4386 {
4387   expressionS exp;
4388
4389   do
4390     {
4391       expression (&exp);
4392       if (exp.X_op == O_symbol)
4393         exp.X_op = O_secrel;
4394
4395       emit_expr (&exp, 4);
4396     }
4397   while (*input_line_pointer++ == ',');
4398
4399   input_line_pointer--;
4400   demand_empty_rest_of_line ();
4401 }
4402 #endif /* TE_PE */
4403
4404 /* This table describes all the machine specific pseudo-ops the assembler
4405    has to support.  The fields are:
4406      pseudo-op name without dot
4407      function to call to execute this pseudo-op
4408      Integer arg to pass to the function.  */
4409
4410 const pseudo_typeS md_pseudo_table[] =
4411 {
4412   /* Never called because '.req' does not start a line.  */
4413   { "req",         s_req,         0 },
4414   /* Following two are likewise never called.  */
4415   { "dn",          s_dn,          0 },
4416   { "qn",          s_qn,          0 },
4417   { "unreq",       s_unreq,       0 },
4418   { "bss",         s_bss,         0 },
4419   { "align",       s_align,       0 },
4420   { "arm",         s_arm,         0 },
4421   { "thumb",       s_thumb,       0 },
4422   { "code",        s_code,        0 },
4423   { "force_thumb", s_force_thumb, 0 },
4424   { "thumb_func",  s_thumb_func,  0 },
4425   { "thumb_set",   s_thumb_set,   0 },
4426   { "even",        s_even,        0 },
4427   { "ltorg",       s_ltorg,       0 },
4428   { "pool",        s_ltorg,       0 },
4429   { "syntax",      s_syntax,      0 },
4430   { "cpu",         s_arm_cpu,     0 },
4431   { "arch",        s_arm_arch,    0 },
4432   { "object_arch", s_arm_object_arch,   0 },
4433   { "fpu",         s_arm_fpu,     0 },
4434   { "arch_extension", s_arm_arch_extension, 0 },
4435 #ifdef OBJ_ELF
4436   { "word",             s_arm_elf_cons, 4 },
4437   { "long",             s_arm_elf_cons, 4 },
4438   { "inst.n",           s_arm_elf_inst, 2 },
4439   { "inst.w",           s_arm_elf_inst, 4 },
4440   { "inst",             s_arm_elf_inst, 0 },
4441   { "rel31",            s_arm_rel31,      0 },
4442   { "fnstart",          s_arm_unwind_fnstart,   0 },
4443   { "fnend",            s_arm_unwind_fnend,     0 },
4444   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4445   { "personality",      s_arm_unwind_personality, 0 },
4446   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4447   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4448   { "save",             s_arm_unwind_save,      0 },
4449   { "vsave",            s_arm_unwind_save,      1 },
4450   { "movsp",            s_arm_unwind_movsp,     0 },
4451   { "pad",              s_arm_unwind_pad,       0 },
4452   { "setfp",            s_arm_unwind_setfp,     0 },
4453   { "unwind_raw",       s_arm_unwind_raw,       0 },
4454   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4455   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4456 #else
4457   { "word",        cons, 4},
4458
4459   /* These are used for dwarf.  */
4460   {"2byte", cons, 2},
4461   {"4byte", cons, 4},
4462   {"8byte", cons, 8},
4463   /* These are used for dwarf2.  */
4464   { "file", (void (*) (int)) dwarf2_directive_file, 0 },
4465   { "loc",  dwarf2_directive_loc,  0 },
4466   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4467 #endif
4468   { "extend",      float_cons, 'x' },
4469   { "ldouble",     float_cons, 'x' },
4470   { "packed",      float_cons, 'p' },
4471 #ifdef TE_PE
4472   {"secrel32", pe_directive_secrel, 0},
4473 #endif
4474   { 0, 0, 0 }
4475 };
4476 \f
4477 /* Parser functions used exclusively in instruction operands.  */
4478
4479 /* Generic immediate-value read function for use in insn parsing.
4480    STR points to the beginning of the immediate (the leading #);
4481    VAL receives the value; if the value is outside [MIN, MAX]
4482    issue an error.  PREFIX_OPT is true if the immediate prefix is
4483    optional.  */
4484
4485 static int
4486 parse_immediate (char **str, int *val, int min, int max,
4487                  bfd_boolean prefix_opt)
4488 {
4489   expressionS exp;
4490   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4491   if (exp.X_op != O_constant)
4492     {
4493       inst.error = _("constant expression required");
4494       return FAIL;
4495     }
4496
4497   if (exp.X_add_number < min || exp.X_add_number > max)
4498     {
4499       inst.error = _("immediate value out of range");
4500       return FAIL;
4501     }
4502
4503   *val = exp.X_add_number;
4504   return SUCCESS;
4505 }
4506
4507 /* Less-generic immediate-value read function with the possibility of loading a
4508    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4509    instructions. Puts the result directly in inst.operands[i].  */
4510
4511 static int
4512 parse_big_immediate (char **str, int i)
4513 {
4514   expressionS exp;
4515   char *ptr = *str;
4516
4517   my_get_expression (&exp, &ptr, GE_OPT_PREFIX_BIG);
4518
4519   if (exp.X_op == O_constant)
4520     {
4521       inst.operands[i].imm = exp.X_add_number & 0xffffffff;
4522       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4523          O_constant.  We have to be careful not to break compilation for
4524          32-bit X_add_number, though.  */
4525       if ((exp.X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4526         {
4527           /* X >> 32 is illegal if sizeof (exp.X_add_number) == 4.  */
4528           inst.operands[i].reg = ((exp.X_add_number >> 16) >> 16) & 0xffffffff;
4529           inst.operands[i].regisimm = 1;
4530         }
4531     }
4532   else if (exp.X_op == O_big
4533            && LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 32)
4534     {
4535       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4536
4537       /* Bignums have their least significant bits in
4538          generic_bignum[0]. Make sure we put 32 bits in imm and
4539          32 bits in reg,  in a (hopefully) portable way.  */
4540       gas_assert (parts != 0);
4541
4542       /* Make sure that the number is not too big.
4543          PR 11972: Bignums can now be sign-extended to the
4544          size of a .octa so check that the out of range bits
4545          are all zero or all one.  */
4546       if (LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 64)
4547         {
4548           LITTLENUM_TYPE m = -1;
4549
4550           if (generic_bignum[parts * 2] != 0
4551               && generic_bignum[parts * 2] != m)
4552             return FAIL;
4553
4554           for (j = parts * 2 + 1; j < (unsigned) exp.X_add_number; j++)
4555             if (generic_bignum[j] != generic_bignum[j-1])
4556               return FAIL;
4557         }
4558
4559       inst.operands[i].imm = 0;
4560       for (j = 0; j < parts; j++, idx++)
4561         inst.operands[i].imm |= generic_bignum[idx]
4562                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4563       inst.operands[i].reg = 0;
4564       for (j = 0; j < parts; j++, idx++)
4565         inst.operands[i].reg |= generic_bignum[idx]
4566                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4567       inst.operands[i].regisimm = 1;
4568     }
4569   else
4570     return FAIL;
4571
4572   *str = ptr;
4573
4574   return SUCCESS;
4575 }
4576
4577 /* Returns the pseudo-register number of an FPA immediate constant,
4578    or FAIL if there isn't a valid constant here.  */
4579
4580 static int
4581 parse_fpa_immediate (char ** str)
4582 {
4583   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4584   char *         save_in;
4585   expressionS    exp;
4586   int            i;
4587   int            j;
4588
4589   /* First try and match exact strings, this is to guarantee
4590      that some formats will work even for cross assembly.  */
4591
4592   for (i = 0; fp_const[i]; i++)
4593     {
4594       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4595         {
4596           char *start = *str;
4597
4598           *str += strlen (fp_const[i]);
4599           if (is_end_of_line[(unsigned char) **str])
4600             return i + 8;
4601           *str = start;
4602         }
4603     }
4604
4605   /* Just because we didn't get a match doesn't mean that the constant
4606      isn't valid, just that it is in a format that we don't
4607      automatically recognize.  Try parsing it with the standard
4608      expression routines.  */
4609
4610   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4611
4612   /* Look for a raw floating point number.  */
4613   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4614       && is_end_of_line[(unsigned char) *save_in])
4615     {
4616       for (i = 0; i < NUM_FLOAT_VALS; i++)
4617         {
4618           for (j = 0; j < MAX_LITTLENUMS; j++)
4619             {
4620               if (words[j] != fp_values[i][j])
4621                 break;
4622             }
4623
4624           if (j == MAX_LITTLENUMS)
4625             {
4626               *str = save_in;
4627               return i + 8;
4628             }
4629         }
4630     }
4631
4632   /* Try and parse a more complex expression, this will probably fail
4633      unless the code uses a floating point prefix (eg "0f").  */
4634   save_in = input_line_pointer;
4635   input_line_pointer = *str;
4636   if (expression (&exp) == absolute_section
4637       && exp.X_op == O_big
4638       && exp.X_add_number < 0)
4639     {
4640       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4641          Ditto for 15.  */
4642       if (gen_to_words (words, 5, (long) 15) == 0)
4643         {
4644           for (i = 0; i < NUM_FLOAT_VALS; i++)
4645             {
4646               for (j = 0; j < MAX_LITTLENUMS; j++)
4647                 {
4648                   if (words[j] != fp_values[i][j])
4649                     break;
4650                 }
4651
4652               if (j == MAX_LITTLENUMS)
4653                 {
4654                   *str = input_line_pointer;
4655                   input_line_pointer = save_in;
4656                   return i + 8;
4657                 }
4658             }
4659         }
4660     }
4661
4662   *str = input_line_pointer;
4663   input_line_pointer = save_in;
4664   inst.error = _("invalid FPA immediate expression");
4665   return FAIL;
4666 }
4667
4668 /* Returns 1 if a number has "quarter-precision" float format
4669    0baBbbbbbc defgh000 00000000 00000000.  */
4670
4671 static int
4672 is_quarter_float (unsigned imm)
4673 {
4674   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4675   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4676 }
4677
4678 /* Parse an 8-bit "quarter-precision" floating point number of the form:
4679    0baBbbbbbc defgh000 00000000 00000000.
4680    The zero and minus-zero cases need special handling, since they can't be
4681    encoded in the "quarter-precision" float format, but can nonetheless be
4682    loaded as integer constants.  */
4683
4684 static unsigned
4685 parse_qfloat_immediate (char **ccp, int *immed)
4686 {
4687   char *str = *ccp;
4688   char *fpnum;
4689   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4690   int found_fpchar = 0;
4691
4692   skip_past_char (&str, '#');
4693
4694   /* We must not accidentally parse an integer as a floating-point number. Make
4695      sure that the value we parse is not an integer by checking for special
4696      characters '.' or 'e'.
4697      FIXME: This is a horrible hack, but doing better is tricky because type
4698      information isn't in a very usable state at parse time.  */
4699   fpnum = str;
4700   skip_whitespace (fpnum);
4701
4702   if (strncmp (fpnum, "0x", 2) == 0)
4703     return FAIL;
4704   else
4705     {
4706       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
4707         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
4708           {
4709             found_fpchar = 1;
4710             break;
4711           }
4712
4713       if (!found_fpchar)
4714         return FAIL;
4715     }
4716
4717   if ((str = atof_ieee (str, 's', words)) != NULL)
4718     {
4719       unsigned fpword = 0;
4720       int i;
4721
4722       /* Our FP word must be 32 bits (single-precision FP).  */
4723       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
4724         {
4725           fpword <<= LITTLENUM_NUMBER_OF_BITS;
4726           fpword |= words[i];
4727         }
4728
4729       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
4730         *immed = fpword;
4731       else
4732         return FAIL;
4733
4734       *ccp = str;
4735
4736       return SUCCESS;
4737     }
4738
4739   return FAIL;
4740 }
4741
4742 /* Shift operands.  */
4743 enum shift_kind
4744 {
4745   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
4746 };
4747
4748 struct asm_shift_name
4749 {
4750   const char      *name;
4751   enum shift_kind  kind;
4752 };
4753
4754 /* Third argument to parse_shift.  */
4755 enum parse_shift_mode
4756 {
4757   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
4758   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
4759   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
4760   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
4761   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
4762 };
4763
4764 /* Parse a <shift> specifier on an ARM data processing instruction.
4765    This has three forms:
4766
4767      (LSL|LSR|ASL|ASR|ROR) Rs
4768      (LSL|LSR|ASL|ASR|ROR) #imm
4769      RRX
4770
4771    Note that ASL is assimilated to LSL in the instruction encoding, and
4772    RRX to ROR #0 (which cannot be written as such).  */
4773
4774 static int
4775 parse_shift (char **str, int i, enum parse_shift_mode mode)
4776 {
4777   const struct asm_shift_name *shift_name;
4778   enum shift_kind shift;
4779   char *s = *str;
4780   char *p = s;
4781   int reg;
4782
4783   for (p = *str; ISALPHA (*p); p++)
4784     ;
4785
4786   if (p == *str)
4787     {
4788       inst.error = _("shift expression expected");
4789       return FAIL;
4790     }
4791
4792   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
4793                                                             p - *str);
4794
4795   if (shift_name == NULL)
4796     {
4797       inst.error = _("shift expression expected");
4798       return FAIL;
4799     }
4800
4801   shift = shift_name->kind;
4802
4803   switch (mode)
4804     {
4805     case NO_SHIFT_RESTRICT:
4806     case SHIFT_IMMEDIATE:   break;
4807
4808     case SHIFT_LSL_OR_ASR_IMMEDIATE:
4809       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
4810         {
4811           inst.error = _("'LSL' or 'ASR' required");
4812           return FAIL;
4813         }
4814       break;
4815
4816     case SHIFT_LSL_IMMEDIATE:
4817       if (shift != SHIFT_LSL)
4818         {
4819           inst.error = _("'LSL' required");
4820           return FAIL;
4821         }
4822       break;
4823
4824     case SHIFT_ASR_IMMEDIATE:
4825       if (shift != SHIFT_ASR)
4826         {
4827           inst.error = _("'ASR' required");
4828           return FAIL;
4829         }
4830       break;
4831
4832     default: abort ();
4833     }
4834
4835   if (shift != SHIFT_RRX)
4836     {
4837       /* Whitespace can appear here if the next thing is a bare digit.  */
4838       skip_whitespace (p);
4839
4840       if (mode == NO_SHIFT_RESTRICT
4841           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
4842         {
4843           inst.operands[i].imm = reg;
4844           inst.operands[i].immisreg = 1;
4845         }
4846       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
4847         return FAIL;
4848     }
4849   inst.operands[i].shift_kind = shift;
4850   inst.operands[i].shifted = 1;
4851   *str = p;
4852   return SUCCESS;
4853 }
4854
4855 /* Parse a <shifter_operand> for an ARM data processing instruction:
4856
4857       #<immediate>
4858       #<immediate>, <rotate>
4859       <Rm>
4860       <Rm>, <shift>
4861
4862    where <shift> is defined by parse_shift above, and <rotate> is a
4863    multiple of 2 between 0 and 30.  Validation of immediate operands
4864    is deferred to md_apply_fix.  */
4865
4866 static int
4867 parse_shifter_operand (char **str, int i)
4868 {
4869   int value;
4870   expressionS exp;
4871
4872   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
4873     {
4874       inst.operands[i].reg = value;
4875       inst.operands[i].isreg = 1;
4876
4877       /* parse_shift will override this if appropriate */
4878       inst.reloc.exp.X_op = O_constant;
4879       inst.reloc.exp.X_add_number = 0;
4880
4881       if (skip_past_comma (str) == FAIL)
4882         return SUCCESS;
4883
4884       /* Shift operation on register.  */
4885       return parse_shift (str, i, NO_SHIFT_RESTRICT);
4886     }
4887
4888   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
4889     return FAIL;
4890
4891   if (skip_past_comma (str) == SUCCESS)
4892     {
4893       /* #x, y -- ie explicit rotation by Y.  */
4894       if (my_get_expression (&exp, str, GE_NO_PREFIX))
4895         return FAIL;
4896
4897       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
4898         {
4899           inst.error = _("constant expression expected");
4900           return FAIL;
4901         }
4902
4903       value = exp.X_add_number;
4904       if (value < 0 || value > 30 || value % 2 != 0)
4905         {
4906           inst.error = _("invalid rotation");
4907           return FAIL;
4908         }
4909       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
4910         {
4911           inst.error = _("invalid constant");
4912           return FAIL;
4913         }
4914
4915       /* Encode as specified.  */
4916       inst.operands[i].imm = inst.reloc.exp.X_add_number | value << 7;
4917       return SUCCESS;
4918     }
4919
4920   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
4921   inst.reloc.pc_rel = 0;
4922   return SUCCESS;
4923 }
4924
4925 /* Group relocation information.  Each entry in the table contains the
4926    textual name of the relocation as may appear in assembler source
4927    and must end with a colon.
4928    Along with this textual name are the relocation codes to be used if
4929    the corresponding instruction is an ALU instruction (ADD or SUB only),
4930    an LDR, an LDRS, or an LDC.  */
4931
4932 struct group_reloc_table_entry
4933 {
4934   const char *name;
4935   int alu_code;
4936   int ldr_code;
4937   int ldrs_code;
4938   int ldc_code;
4939 };
4940
4941 typedef enum
4942 {
4943   /* Varieties of non-ALU group relocation.  */
4944
4945   GROUP_LDR,
4946   GROUP_LDRS,
4947   GROUP_LDC
4948 } group_reloc_type;
4949
4950 static struct group_reloc_table_entry group_reloc_table[] =
4951   { /* Program counter relative: */
4952     { "pc_g0_nc",
4953       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
4954       0,                                /* LDR */
4955       0,                                /* LDRS */
4956       0 },                              /* LDC */
4957     { "pc_g0",
4958       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
4959       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
4960       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
4961       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
4962     { "pc_g1_nc",
4963       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
4964       0,                                /* LDR */
4965       0,                                /* LDRS */
4966       0 },                              /* LDC */
4967     { "pc_g1",
4968       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
4969       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
4970       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
4971       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
4972     { "pc_g2",
4973       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
4974       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
4975       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
4976       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
4977     /* Section base relative */
4978     { "sb_g0_nc",
4979       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
4980       0,                                /* LDR */
4981       0,                                /* LDRS */
4982       0 },                              /* LDC */
4983     { "sb_g0",
4984       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
4985       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
4986       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
4987       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
4988     { "sb_g1_nc",
4989       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
4990       0,                                /* LDR */
4991       0,                                /* LDRS */
4992       0 },                              /* LDC */
4993     { "sb_g1",
4994       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
4995       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
4996       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
4997       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
4998     { "sb_g2",
4999       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5000       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5001       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5002       BFD_RELOC_ARM_LDC_SB_G2 } };      /* LDC */
5003
5004 /* Given the address of a pointer pointing to the textual name of a group
5005    relocation as may appear in assembler source, attempt to find its details
5006    in group_reloc_table.  The pointer will be updated to the character after
5007    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5008    otherwise.  On success, *entry will be updated to point at the relevant
5009    group_reloc_table entry. */
5010
5011 static int
5012 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5013 {
5014   unsigned int i;
5015   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5016     {
5017       int length = strlen (group_reloc_table[i].name);
5018
5019       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5020           && (*str)[length] == ':')
5021         {
5022           *out = &group_reloc_table[i];
5023           *str += (length + 1);
5024           return SUCCESS;
5025         }
5026     }
5027
5028   return FAIL;
5029 }
5030
5031 /* Parse a <shifter_operand> for an ARM data processing instruction
5032    (as for parse_shifter_operand) where group relocations are allowed:
5033
5034       #<immediate>
5035       #<immediate>, <rotate>
5036       #:<group_reloc>:<expression>
5037       <Rm>
5038       <Rm>, <shift>
5039
5040    where <group_reloc> is one of the strings defined in group_reloc_table.
5041    The hashes are optional.
5042
5043    Everything else is as for parse_shifter_operand.  */
5044
5045 static parse_operand_result
5046 parse_shifter_operand_group_reloc (char **str, int i)
5047 {
5048   /* Determine if we have the sequence of characters #: or just :
5049      coming next.  If we do, then we check for a group relocation.
5050      If we don't, punt the whole lot to parse_shifter_operand.  */
5051
5052   if (((*str)[0] == '#' && (*str)[1] == ':')
5053       || (*str)[0] == ':')
5054     {
5055       struct group_reloc_table_entry *entry;
5056
5057       if ((*str)[0] == '#')
5058         (*str) += 2;
5059       else
5060         (*str)++;
5061
5062       /* Try to parse a group relocation.  Anything else is an error.  */
5063       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5064         {
5065           inst.error = _("unknown group relocation");
5066           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5067         }
5068
5069       /* We now have the group relocation table entry corresponding to
5070          the name in the assembler source.  Next, we parse the expression.  */
5071       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5072         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5073
5074       /* Record the relocation type (always the ALU variant here).  */
5075       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5076       gas_assert (inst.reloc.type != 0);
5077
5078       return PARSE_OPERAND_SUCCESS;
5079     }
5080   else
5081     return parse_shifter_operand (str, i) == SUCCESS
5082            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5083
5084   /* Never reached.  */
5085 }
5086
5087 /* Parse a Neon alignment expression.  Information is written to
5088    inst.operands[i].  We assume the initial ':' has been skipped.
5089
5090    align        .imm = align << 8, .immisalign=1, .preind=0  */
5091 static parse_operand_result
5092 parse_neon_alignment (char **str, int i)
5093 {
5094   char *p = *str;
5095   expressionS exp;
5096
5097   my_get_expression (&exp, &p, GE_NO_PREFIX);
5098
5099   if (exp.X_op != O_constant)
5100     {
5101       inst.error = _("alignment must be constant");
5102       return PARSE_OPERAND_FAIL;
5103     }
5104
5105   inst.operands[i].imm = exp.X_add_number << 8;
5106   inst.operands[i].immisalign = 1;
5107   /* Alignments are not pre-indexes.  */
5108   inst.operands[i].preind = 0;
5109
5110   *str = p;
5111   return PARSE_OPERAND_SUCCESS;
5112 }
5113
5114 /* Parse all forms of an ARM address expression.  Information is written
5115    to inst.operands[i] and/or inst.reloc.
5116
5117    Preindexed addressing (.preind=1):
5118
5119    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5120    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5121    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5122                        .shift_kind=shift .reloc.exp=shift_imm
5123
5124    These three may have a trailing ! which causes .writeback to be set also.
5125
5126    Postindexed addressing (.postind=1, .writeback=1):
5127
5128    [Rn], #offset       .reg=Rn .reloc.exp=offset
5129    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5130    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5131                        .shift_kind=shift .reloc.exp=shift_imm
5132
5133    Unindexed addressing (.preind=0, .postind=0):
5134
5135    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5136
5137    Other:
5138
5139    [Rn]{!}             shorthand for [Rn,#0]{!}
5140    =immediate          .isreg=0 .reloc.exp=immediate
5141    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5142
5143   It is the caller's responsibility to check for addressing modes not
5144   supported by the instruction, and to set inst.reloc.type.  */
5145
5146 static parse_operand_result
5147 parse_address_main (char **str, int i, int group_relocations,
5148                     group_reloc_type group_type)
5149 {
5150   char *p = *str;
5151   int reg;
5152
5153   if (skip_past_char (&p, '[') == FAIL)
5154     {
5155       if (skip_past_char (&p, '=') == FAIL)
5156         {
5157           /* Bare address - translate to PC-relative offset.  */
5158           inst.reloc.pc_rel = 1;
5159           inst.operands[i].reg = REG_PC;
5160           inst.operands[i].isreg = 1;
5161           inst.operands[i].preind = 1;
5162         }
5163       /* Otherwise a load-constant pseudo op, no special treatment needed here.  */
5164
5165       if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5166         return PARSE_OPERAND_FAIL;
5167
5168       *str = p;
5169       return PARSE_OPERAND_SUCCESS;
5170     }
5171
5172   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5173   skip_whitespace (p);
5174
5175   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5176     {
5177       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5178       return PARSE_OPERAND_FAIL;
5179     }
5180   inst.operands[i].reg = reg;
5181   inst.operands[i].isreg = 1;
5182
5183   if (skip_past_comma (&p) == SUCCESS)
5184     {
5185       inst.operands[i].preind = 1;
5186
5187       if (*p == '+') p++;
5188       else if (*p == '-') p++, inst.operands[i].negative = 1;
5189
5190       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5191         {
5192           inst.operands[i].imm = reg;
5193           inst.operands[i].immisreg = 1;
5194
5195           if (skip_past_comma (&p) == SUCCESS)
5196             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5197               return PARSE_OPERAND_FAIL;
5198         }
5199       else if (skip_past_char (&p, ':') == SUCCESS)
5200         {
5201           /* FIXME: '@' should be used here, but it's filtered out by generic
5202              code before we get to see it here. This may be subject to
5203              change.  */
5204           parse_operand_result result = parse_neon_alignment (&p, i);
5205
5206           if (result != PARSE_OPERAND_SUCCESS)
5207             return result;
5208         }
5209       else
5210         {
5211           if (inst.operands[i].negative)
5212             {
5213               inst.operands[i].negative = 0;
5214               p--;
5215             }
5216
5217           if (group_relocations
5218               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5219             {
5220               struct group_reloc_table_entry *entry;
5221
5222               /* Skip over the #: or : sequence.  */
5223               if (*p == '#')
5224                 p += 2;
5225               else
5226                 p++;
5227
5228               /* Try to parse a group relocation.  Anything else is an
5229                  error.  */
5230               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5231                 {
5232                   inst.error = _("unknown group relocation");
5233                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5234                 }
5235
5236               /* We now have the group relocation table entry corresponding to
5237                  the name in the assembler source.  Next, we parse the
5238                  expression.  */
5239               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5240                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5241
5242               /* Record the relocation type.  */
5243               switch (group_type)
5244                 {
5245                   case GROUP_LDR:
5246                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5247                     break;
5248
5249                   case GROUP_LDRS:
5250                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5251                     break;
5252
5253                   case GROUP_LDC:
5254                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5255                     break;
5256
5257                   default:
5258                     gas_assert (0);
5259                 }
5260
5261               if (inst.reloc.type == 0)
5262                 {
5263                   inst.error = _("this group relocation is not allowed on this instruction");
5264                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5265                 }
5266             }
5267           else
5268             {
5269               char *q = p;
5270               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5271                 return PARSE_OPERAND_FAIL;
5272               /* If the offset is 0, find out if it's a +0 or -0.  */
5273               if (inst.reloc.exp.X_op == O_constant
5274                   && inst.reloc.exp.X_add_number == 0)
5275                 {
5276                   skip_whitespace (q);
5277                   if (*q == '#')
5278                     {
5279                       q++;
5280                       skip_whitespace (q);
5281                     }
5282                   if (*q == '-')
5283                     inst.operands[i].negative = 1;
5284                 }
5285             }
5286         }
5287     }
5288   else if (skip_past_char (&p, ':') == SUCCESS)
5289     {
5290       /* FIXME: '@' should be used here, but it's filtered out by generic code
5291          before we get to see it here. This may be subject to change.  */
5292       parse_operand_result result = parse_neon_alignment (&p, i);
5293
5294       if (result != PARSE_OPERAND_SUCCESS)
5295         return result;
5296     }
5297
5298   if (skip_past_char (&p, ']') == FAIL)
5299     {
5300       inst.error = _("']' expected");
5301       return PARSE_OPERAND_FAIL;
5302     }
5303
5304   if (skip_past_char (&p, '!') == SUCCESS)
5305     inst.operands[i].writeback = 1;
5306
5307   else if (skip_past_comma (&p) == SUCCESS)
5308     {
5309       if (skip_past_char (&p, '{') == SUCCESS)
5310         {
5311           /* [Rn], {expr} - unindexed, with option */
5312           if (parse_immediate (&p, &inst.operands[i].imm,
5313                                0, 255, TRUE) == FAIL)
5314             return PARSE_OPERAND_FAIL;
5315
5316           if (skip_past_char (&p, '}') == FAIL)
5317             {
5318               inst.error = _("'}' expected at end of 'option' field");
5319               return PARSE_OPERAND_FAIL;
5320             }
5321           if (inst.operands[i].preind)
5322             {
5323               inst.error = _("cannot combine index with option");
5324               return PARSE_OPERAND_FAIL;
5325             }
5326           *str = p;
5327           return PARSE_OPERAND_SUCCESS;
5328         }
5329       else
5330         {
5331           inst.operands[i].postind = 1;
5332           inst.operands[i].writeback = 1;
5333
5334           if (inst.operands[i].preind)
5335             {
5336               inst.error = _("cannot combine pre- and post-indexing");
5337               return PARSE_OPERAND_FAIL;
5338             }
5339
5340           if (*p == '+') p++;
5341           else if (*p == '-') p++, inst.operands[i].negative = 1;
5342
5343           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5344             {
5345               /* We might be using the immediate for alignment already. If we
5346                  are, OR the register number into the low-order bits.  */
5347               if (inst.operands[i].immisalign)
5348                 inst.operands[i].imm |= reg;
5349               else
5350                 inst.operands[i].imm = reg;
5351               inst.operands[i].immisreg = 1;
5352
5353               if (skip_past_comma (&p) == SUCCESS)
5354                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5355                   return PARSE_OPERAND_FAIL;
5356             }
5357           else
5358             {
5359               char *q = p;
5360               if (inst.operands[i].negative)
5361                 {
5362                   inst.operands[i].negative = 0;
5363                   p--;
5364                 }
5365               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5366                 return PARSE_OPERAND_FAIL;
5367               /* If the offset is 0, find out if it's a +0 or -0.  */
5368               if (inst.reloc.exp.X_op == O_constant
5369                   && inst.reloc.exp.X_add_number == 0)
5370                 {
5371                   skip_whitespace (q);
5372                   if (*q == '#')
5373                     {
5374                       q++;
5375                       skip_whitespace (q);
5376                     }
5377                   if (*q == '-')
5378                     inst.operands[i].negative = 1;
5379                 }
5380             }
5381         }
5382     }
5383
5384   /* If at this point neither .preind nor .postind is set, we have a
5385      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5386   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5387     {
5388       inst.operands[i].preind = 1;
5389       inst.reloc.exp.X_op = O_constant;
5390       inst.reloc.exp.X_add_number = 0;
5391     }
5392   *str = p;
5393   return PARSE_OPERAND_SUCCESS;
5394 }
5395
5396 static int
5397 parse_address (char **str, int i)
5398 {
5399   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5400          ? SUCCESS : FAIL;
5401 }
5402
5403 static parse_operand_result
5404 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5405 {
5406   return parse_address_main (str, i, 1, type);
5407 }
5408
5409 /* Parse an operand for a MOVW or MOVT instruction.  */
5410 static int
5411 parse_half (char **str)
5412 {
5413   char * p;
5414
5415   p = *str;
5416   skip_past_char (&p, '#');
5417   if (strncasecmp (p, ":lower16:", 9) == 0)
5418     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5419   else if (strncasecmp (p, ":upper16:", 9) == 0)
5420     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5421
5422   if (inst.reloc.type != BFD_RELOC_UNUSED)
5423     {
5424       p += 9;
5425       skip_whitespace (p);
5426     }
5427
5428   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5429     return FAIL;
5430
5431   if (inst.reloc.type == BFD_RELOC_UNUSED)
5432     {
5433       if (inst.reloc.exp.X_op != O_constant)
5434         {
5435           inst.error = _("constant expression expected");
5436           return FAIL;
5437         }
5438       if (inst.reloc.exp.X_add_number < 0
5439           || inst.reloc.exp.X_add_number > 0xffff)
5440         {
5441           inst.error = _("immediate value out of range");
5442           return FAIL;
5443         }
5444     }
5445   *str = p;
5446   return SUCCESS;
5447 }
5448
5449 /* Miscellaneous. */
5450
5451 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5452    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5453 static int
5454 parse_psr (char **str, bfd_boolean lhs)
5455 {
5456   char *p;
5457   unsigned long psr_field;
5458   const struct asm_psr *psr;
5459   char *start;
5460   bfd_boolean is_apsr = FALSE;
5461   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5462
5463   /* PR gas/12698:  If the user has specified -march=all then m_profile will
5464      be TRUE, but we want to ignore it in this case as we are building for any
5465      CPU type, including non-m variants.  */
5466   if (selected_cpu.core == arm_arch_any.core)
5467     m_profile = FALSE;
5468
5469   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5470      feature for ease of use and backwards compatibility.  */
5471   p = *str;
5472   if (strncasecmp (p, "SPSR", 4) == 0)
5473     {
5474       if (m_profile)
5475         goto unsupported_psr;
5476
5477       psr_field = SPSR_BIT;
5478     }
5479   else if (strncasecmp (p, "CPSR", 4) == 0)
5480     {
5481       if (m_profile)
5482         goto unsupported_psr;
5483
5484       psr_field = 0;
5485     }
5486   else if (strncasecmp (p, "APSR", 4) == 0)
5487     {
5488       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5489          and ARMv7-R architecture CPUs.  */
5490       is_apsr = TRUE;
5491       psr_field = 0;
5492     }
5493   else if (m_profile)
5494     {
5495       start = p;
5496       do
5497         p++;
5498       while (ISALNUM (*p) || *p == '_');
5499
5500       if (strncasecmp (start, "iapsr", 5) == 0
5501           || strncasecmp (start, "eapsr", 5) == 0
5502           || strncasecmp (start, "xpsr", 4) == 0
5503           || strncasecmp (start, "psr", 3) == 0)
5504         p = start + strcspn (start, "rR") + 1;
5505
5506       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5507                                                   p - start);
5508
5509       if (!psr)
5510         return FAIL;
5511
5512       /* If APSR is being written, a bitfield may be specified.  Note that
5513          APSR itself is handled above.  */
5514       if (psr->field <= 3)
5515         {
5516           psr_field = psr->field;
5517           is_apsr = TRUE;
5518           goto check_suffix;
5519         }
5520
5521       *str = p;
5522       /* M-profile MSR instructions have the mask field set to "10", except
5523          *PSR variants which modify APSR, which may use a different mask (and
5524          have been handled already).  Do that by setting the PSR_f field
5525          here.  */
5526       return psr->field | (lhs ? PSR_f : 0);
5527     }
5528   else
5529     goto unsupported_psr;
5530
5531   p += 4;
5532 check_suffix:
5533   if (*p == '_')
5534     {
5535       /* A suffix follows.  */
5536       p++;
5537       start = p;
5538
5539       do
5540         p++;
5541       while (ISALNUM (*p) || *p == '_');
5542
5543       if (is_apsr)
5544         {
5545           /* APSR uses a notation for bits, rather than fields.  */
5546           unsigned int nzcvq_bits = 0;
5547           unsigned int g_bit = 0;
5548           char *bit;
5549
5550           for (bit = start; bit != p; bit++)
5551             {
5552               switch (TOLOWER (*bit))
5553                 {
5554                 case 'n':
5555                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5556                   break;
5557
5558                 case 'z':
5559                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5560                   break;
5561
5562                 case 'c':
5563                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5564                   break;
5565
5566                 case 'v':
5567                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5568                   break;
5569
5570                 case 'q':
5571                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5572                   break;
5573
5574                 case 'g':
5575                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5576                   break;
5577
5578                 default:
5579                   inst.error = _("unexpected bit specified after APSR");
5580                   return FAIL;
5581                 }
5582             }
5583
5584           if (nzcvq_bits == 0x1f)
5585             psr_field |= PSR_f;
5586
5587           if (g_bit == 0x1)
5588             {
5589               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5590                 {
5591                   inst.error = _("selected processor does not "
5592                                  "support DSP extension");
5593                   return FAIL;
5594                 }
5595
5596               psr_field |= PSR_s;
5597             }
5598
5599           if ((nzcvq_bits & 0x20) != 0
5600               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5601               || (g_bit & 0x2) != 0)
5602             {
5603               inst.error = _("bad bitmask specified after APSR");
5604               return FAIL;
5605             }
5606         }
5607       else
5608         {
5609           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
5610                                                       p - start);
5611           if (!psr)
5612             goto error;
5613
5614           psr_field |= psr->field;
5615         }
5616     }
5617   else
5618     {
5619       if (ISALNUM (*p))
5620         goto error;    /* Garbage after "[CS]PSR".  */
5621
5622       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
5623          is deprecated, but allow it anyway.  */
5624       if (is_apsr && lhs)
5625         {
5626           psr_field |= PSR_f;
5627           as_tsktsk (_("writing to APSR without specifying a bitmask is "
5628                        "deprecated"));
5629         }
5630       else if (!m_profile)
5631         /* These bits are never right for M-profile devices: don't set them
5632            (only code paths which read/write APSR reach here).  */
5633         psr_field |= (PSR_c | PSR_f);
5634     }
5635   *str = p;
5636   return psr_field;
5637
5638  unsupported_psr:
5639   inst.error = _("selected processor does not support requested special "
5640                  "purpose register");
5641   return FAIL;
5642
5643  error:
5644   inst.error = _("flag for {c}psr instruction expected");
5645   return FAIL;
5646 }
5647
5648 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
5649    value suitable for splatting into the AIF field of the instruction.  */
5650
5651 static int
5652 parse_cps_flags (char **str)
5653 {
5654   int val = 0;
5655   int saw_a_flag = 0;
5656   char *s = *str;
5657
5658   for (;;)
5659     switch (*s++)
5660       {
5661       case '\0': case ',':
5662         goto done;
5663
5664       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
5665       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
5666       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
5667
5668       default:
5669         inst.error = _("unrecognized CPS flag");
5670         return FAIL;
5671       }
5672
5673  done:
5674   if (saw_a_flag == 0)
5675     {
5676       inst.error = _("missing CPS flags");
5677       return FAIL;
5678     }
5679
5680   *str = s - 1;
5681   return val;
5682 }
5683
5684 /* Parse an endian specifier ("BE" or "LE", case insensitive);
5685    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
5686
5687 static int
5688 parse_endian_specifier (char **str)
5689 {
5690   int little_endian;
5691   char *s = *str;
5692
5693   if (strncasecmp (s, "BE", 2))
5694     little_endian = 0;
5695   else if (strncasecmp (s, "LE", 2))
5696     little_endian = 1;
5697   else
5698     {
5699       inst.error = _("valid endian specifiers are be or le");
5700       return FAIL;
5701     }
5702
5703   if (ISALNUM (s[2]) || s[2] == '_')
5704     {
5705       inst.error = _("valid endian specifiers are be or le");
5706       return FAIL;
5707     }
5708
5709   *str = s + 2;
5710   return little_endian;
5711 }
5712
5713 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
5714    value suitable for poking into the rotate field of an sxt or sxta
5715    instruction, or FAIL on error.  */
5716
5717 static int
5718 parse_ror (char **str)
5719 {
5720   int rot;
5721   char *s = *str;
5722
5723   if (strncasecmp (s, "ROR", 3) == 0)
5724     s += 3;
5725   else
5726     {
5727       inst.error = _("missing rotation field after comma");
5728       return FAIL;
5729     }
5730
5731   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
5732     return FAIL;
5733
5734   switch (rot)
5735     {
5736     case  0: *str = s; return 0x0;
5737     case  8: *str = s; return 0x1;
5738     case 16: *str = s; return 0x2;
5739     case 24: *str = s; return 0x3;
5740
5741     default:
5742       inst.error = _("rotation can only be 0, 8, 16, or 24");
5743       return FAIL;
5744     }
5745 }
5746
5747 /* Parse a conditional code (from conds[] below).  The value returned is in the
5748    range 0 .. 14, or FAIL.  */
5749 static int
5750 parse_cond (char **str)
5751 {
5752   char *q;
5753   const struct asm_cond *c;
5754   int n;
5755   /* Condition codes are always 2 characters, so matching up to
5756      3 characters is sufficient.  */
5757   char cond[3];
5758
5759   q = *str;
5760   n = 0;
5761   while (ISALPHA (*q) && n < 3)
5762     {
5763       cond[n] = TOLOWER (*q);
5764       q++;
5765       n++;
5766     }
5767
5768   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
5769   if (!c)
5770     {
5771       inst.error = _("condition required");
5772       return FAIL;
5773     }
5774
5775   *str = q;
5776   return c->value;
5777 }
5778
5779 /* If the given feature available in the selected CPU, mark it as used.
5780    Returns TRUE iff feature is available.  */
5781 static bfd_boolean
5782 mark_feature_used (const arm_feature_set *feature)
5783 {
5784   /* Ensure the option is valid on the current architecture.  */
5785   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
5786     return FALSE;
5787
5788   /* Add the appropriate architecture feature for the barrier option used.
5789      */
5790   if (thumb_mode)
5791     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
5792   else
5793     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
5794
5795   return TRUE;
5796 }
5797
5798 /* Parse an option for a barrier instruction.  Returns the encoding for the
5799    option, or FAIL.  */
5800 static int
5801 parse_barrier (char **str)
5802 {
5803   char *p, *q;
5804   const struct asm_barrier_opt *o;
5805
5806   p = q = *str;
5807   while (ISALPHA (*q))
5808     q++;
5809
5810   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
5811                                                     q - p);
5812   if (!o)
5813     return FAIL;
5814
5815   if (!mark_feature_used (&o->arch))
5816     return FAIL;
5817
5818   *str = q;
5819   return o->value;
5820 }
5821
5822 /* Parse the operands of a table branch instruction.  Similar to a memory
5823    operand.  */
5824 static int
5825 parse_tb (char **str)
5826 {
5827   char * p = *str;
5828   int reg;
5829
5830   if (skip_past_char (&p, '[') == FAIL)
5831     {
5832       inst.error = _("'[' expected");
5833       return FAIL;
5834     }
5835
5836   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5837     {
5838       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5839       return FAIL;
5840     }
5841   inst.operands[0].reg = reg;
5842
5843   if (skip_past_comma (&p) == FAIL)
5844     {
5845       inst.error = _("',' expected");
5846       return FAIL;
5847     }
5848
5849   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5850     {
5851       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5852       return FAIL;
5853     }
5854   inst.operands[0].imm = reg;
5855
5856   if (skip_past_comma (&p) == SUCCESS)
5857     {
5858       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
5859         return FAIL;
5860       if (inst.reloc.exp.X_add_number != 1)
5861         {
5862           inst.error = _("invalid shift");
5863           return FAIL;
5864         }
5865       inst.operands[0].shifted = 1;
5866     }
5867
5868   if (skip_past_char (&p, ']') == FAIL)
5869     {
5870       inst.error = _("']' expected");
5871       return FAIL;
5872     }
5873   *str = p;
5874   return SUCCESS;
5875 }
5876
5877 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
5878    information on the types the operands can take and how they are encoded.
5879    Up to four operands may be read; this function handles setting the
5880    ".present" field for each read operand itself.
5881    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
5882    else returns FAIL.  */
5883
5884 static int
5885 parse_neon_mov (char **str, int *which_operand)
5886 {
5887   int i = *which_operand, val;
5888   enum arm_reg_type rtype;
5889   char *ptr = *str;
5890   struct neon_type_el optype;
5891
5892   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
5893     {
5894       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
5895       inst.operands[i].reg = val;
5896       inst.operands[i].isscalar = 1;
5897       inst.operands[i].vectype = optype;
5898       inst.operands[i++].present = 1;
5899
5900       if (skip_past_comma (&ptr) == FAIL)
5901         goto wanted_comma;
5902
5903       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5904         goto wanted_arm;
5905
5906       inst.operands[i].reg = val;
5907       inst.operands[i].isreg = 1;
5908       inst.operands[i].present = 1;
5909     }
5910   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
5911            != FAIL)
5912     {
5913       /* Cases 0, 1, 2, 3, 5 (D only).  */
5914       if (skip_past_comma (&ptr) == FAIL)
5915         goto wanted_comma;
5916
5917       inst.operands[i].reg = val;
5918       inst.operands[i].isreg = 1;
5919       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5920       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5921       inst.operands[i].isvec = 1;
5922       inst.operands[i].vectype = optype;
5923       inst.operands[i++].present = 1;
5924
5925       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5926         {
5927           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
5928              Case 13: VMOV <Sd>, <Rm>  */
5929           inst.operands[i].reg = val;
5930           inst.operands[i].isreg = 1;
5931           inst.operands[i].present = 1;
5932
5933           if (rtype == REG_TYPE_NQ)
5934             {
5935               first_error (_("can't use Neon quad register here"));
5936               return FAIL;
5937             }
5938           else if (rtype != REG_TYPE_VFS)
5939             {
5940               i++;
5941               if (skip_past_comma (&ptr) == FAIL)
5942                 goto wanted_comma;
5943               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5944                 goto wanted_arm;
5945               inst.operands[i].reg = val;
5946               inst.operands[i].isreg = 1;
5947               inst.operands[i].present = 1;
5948             }
5949         }
5950       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
5951                                            &optype)) != FAIL)
5952         {
5953           /* Case 0: VMOV<c><q> <Qd>, <Qm>
5954              Case 1: VMOV<c><q> <Dd>, <Dm>
5955              Case 8: VMOV.F32 <Sd>, <Sm>
5956              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
5957
5958           inst.operands[i].reg = val;
5959           inst.operands[i].isreg = 1;
5960           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5961           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5962           inst.operands[i].isvec = 1;
5963           inst.operands[i].vectype = optype;
5964           inst.operands[i].present = 1;
5965
5966           if (skip_past_comma (&ptr) == SUCCESS)
5967             {
5968               /* Case 15.  */
5969               i++;
5970
5971               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5972                 goto wanted_arm;
5973
5974               inst.operands[i].reg = val;
5975               inst.operands[i].isreg = 1;
5976               inst.operands[i++].present = 1;
5977
5978               if (skip_past_comma (&ptr) == FAIL)
5979                 goto wanted_comma;
5980
5981               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5982                 goto wanted_arm;
5983
5984               inst.operands[i].reg = val;
5985               inst.operands[i].isreg = 1;
5986               inst.operands[i].present = 1;
5987             }
5988         }
5989       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
5990           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
5991              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
5992              Case 10: VMOV.F32 <Sd>, #<imm>
5993              Case 11: VMOV.F64 <Dd>, #<imm>  */
5994         inst.operands[i].immisfloat = 1;
5995       else if (parse_big_immediate (&ptr, i) == SUCCESS)
5996           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
5997              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
5998         ;
5999       else
6000         {
6001           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6002           return FAIL;
6003         }
6004     }
6005   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6006     {
6007       /* Cases 6, 7.  */
6008       inst.operands[i].reg = val;
6009       inst.operands[i].isreg = 1;
6010       inst.operands[i++].present = 1;
6011
6012       if (skip_past_comma (&ptr) == FAIL)
6013         goto wanted_comma;
6014
6015       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6016         {
6017           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6018           inst.operands[i].reg = val;
6019           inst.operands[i].isscalar = 1;
6020           inst.operands[i].present = 1;
6021           inst.operands[i].vectype = optype;
6022         }
6023       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6024         {
6025           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6026           inst.operands[i].reg = val;
6027           inst.operands[i].isreg = 1;
6028           inst.operands[i++].present = 1;
6029
6030           if (skip_past_comma (&ptr) == FAIL)
6031             goto wanted_comma;
6032
6033           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6034               == FAIL)
6035             {
6036               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
6037               return FAIL;
6038             }
6039
6040           inst.operands[i].reg = val;
6041           inst.operands[i].isreg = 1;
6042           inst.operands[i].isvec = 1;
6043           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6044           inst.operands[i].vectype = optype;
6045           inst.operands[i].present = 1;
6046
6047           if (rtype == REG_TYPE_VFS)
6048             {
6049               /* Case 14.  */
6050               i++;
6051               if (skip_past_comma (&ptr) == FAIL)
6052                 goto wanted_comma;
6053               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6054                                               &optype)) == FAIL)
6055                 {
6056                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6057                   return FAIL;
6058                 }
6059               inst.operands[i].reg = val;
6060               inst.operands[i].isreg = 1;
6061               inst.operands[i].isvec = 1;
6062               inst.operands[i].issingle = 1;
6063               inst.operands[i].vectype = optype;
6064               inst.operands[i].present = 1;
6065             }
6066         }
6067       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6068                != FAIL)
6069         {
6070           /* Case 13.  */
6071           inst.operands[i].reg = val;
6072           inst.operands[i].isreg = 1;
6073           inst.operands[i].isvec = 1;
6074           inst.operands[i].issingle = 1;
6075           inst.operands[i].vectype = optype;
6076           inst.operands[i].present = 1;
6077         }
6078     }
6079   else
6080     {
6081       first_error (_("parse error"));
6082       return FAIL;
6083     }
6084
6085   /* Successfully parsed the operands. Update args.  */
6086   *which_operand = i;
6087   *str = ptr;
6088   return SUCCESS;
6089
6090  wanted_comma:
6091   first_error (_("expected comma"));
6092   return FAIL;
6093
6094  wanted_arm:
6095   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6096   return FAIL;
6097 }
6098
6099 /* Use this macro when the operand constraints are different
6100    for ARM and THUMB (e.g. ldrd).  */
6101 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6102         ((arm_operand) | ((thumb_operand) << 16))
6103
6104 /* Matcher codes for parse_operands.  */
6105 enum operand_parse_code
6106 {
6107   OP_stop,      /* end of line */
6108
6109   OP_RR,        /* ARM register */
6110   OP_RRnpc,     /* ARM register, not r15 */
6111   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6112   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6113   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6114                    optional trailing ! */
6115   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6116   OP_RCP,       /* Coprocessor number */
6117   OP_RCN,       /* Coprocessor register */
6118   OP_RF,        /* FPA register */
6119   OP_RVS,       /* VFP single precision register */
6120   OP_RVD,       /* VFP double precision register (0..15) */
6121   OP_RND,       /* Neon double precision register (0..31) */
6122   OP_RNQ,       /* Neon quad precision register */
6123   OP_RVSD,      /* VFP single or double precision register */
6124   OP_RNDQ,      /* Neon double or quad precision register */
6125   OP_RNSDQ,     /* Neon single, double or quad precision register */
6126   OP_RNSC,      /* Neon scalar D[X] */
6127   OP_RVC,       /* VFP control register */
6128   OP_RMF,       /* Maverick F register */
6129   OP_RMD,       /* Maverick D register */
6130   OP_RMFX,      /* Maverick FX register */
6131   OP_RMDX,      /* Maverick DX register */
6132   OP_RMAX,      /* Maverick AX register */
6133   OP_RMDS,      /* Maverick DSPSC register */
6134   OP_RIWR,      /* iWMMXt wR register */
6135   OP_RIWC,      /* iWMMXt wC register */
6136   OP_RIWG,      /* iWMMXt wCG register */
6137   OP_RXA,       /* XScale accumulator register */
6138
6139   OP_REGLST,    /* ARM register list */
6140   OP_VRSLST,    /* VFP single-precision register list */
6141   OP_VRDLST,    /* VFP double-precision register list */
6142   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6143   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6144   OP_NSTRLST,   /* Neon element/structure list */
6145
6146   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6147   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6148   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6149   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6150   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6151   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6152   OP_VMOV,      /* Neon VMOV operands.  */
6153   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6154   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6155   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6156
6157   OP_I0,        /* immediate zero */
6158   OP_I7,        /* immediate value 0 .. 7 */
6159   OP_I15,       /*                 0 .. 15 */
6160   OP_I16,       /*                 1 .. 16 */
6161   OP_I16z,      /*                 0 .. 16 */
6162   OP_I31,       /*                 0 .. 31 */
6163   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6164   OP_I32,       /*                 1 .. 32 */
6165   OP_I32z,      /*                 0 .. 32 */
6166   OP_I63,       /*                 0 .. 63 */
6167   OP_I63s,      /*               -64 .. 63 */
6168   OP_I64,       /*                 1 .. 64 */
6169   OP_I64z,      /*                 0 .. 64 */
6170   OP_I255,      /*                 0 .. 255 */
6171
6172   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6173   OP_I7b,       /*                             0 .. 7 */
6174   OP_I15b,      /*                             0 .. 15 */
6175   OP_I31b,      /*                             0 .. 31 */
6176
6177   OP_SH,        /* shifter operand */
6178   OP_SHG,       /* shifter operand with possible group relocation */
6179   OP_ADDR,      /* Memory address expression (any mode) */
6180   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6181   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6182   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6183   OP_EXP,       /* arbitrary expression */
6184   OP_EXPi,      /* same, with optional immediate prefix */
6185   OP_EXPr,      /* same, with optional relocation suffix */
6186   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6187
6188   OP_CPSF,      /* CPS flags */
6189   OP_ENDI,      /* Endianness specifier */
6190   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6191   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6192   OP_COND,      /* conditional code */
6193   OP_TB,        /* Table branch.  */
6194
6195   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6196
6197   OP_RRnpc_I0,  /* ARM register or literal 0 */
6198   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
6199   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6200   OP_RF_IF,     /* FPA register or immediate */
6201   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6202   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6203
6204   /* Optional operands.  */
6205   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6206   OP_oI31b,      /*                             0 .. 31 */
6207   OP_oI32b,      /*                             1 .. 32 */
6208   OP_oI32z,      /*                             0 .. 32 */
6209   OP_oIffffb,    /*                             0 .. 65535 */
6210   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6211
6212   OP_oRR,        /* ARM register */
6213   OP_oRRnpc,     /* ARM register, not the PC */
6214   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6215   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6216   OP_oRND,       /* Optional Neon double precision register */
6217   OP_oRNQ,       /* Optional Neon quad precision register */
6218   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6219   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6220   OP_oSHll,      /* LSL immediate */
6221   OP_oSHar,      /* ASR immediate */
6222   OP_oSHllar,    /* LSL or ASR immediate */
6223   OP_oROR,       /* ROR 0/8/16/24 */
6224   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6225
6226   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6227   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6228   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6229   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6230
6231   OP_FIRST_OPTIONAL = OP_oI7b
6232 };
6233
6234 /* Generic instruction operand parser.  This does no encoding and no
6235    semantic validation; it merely squirrels values away in the inst
6236    structure.  Returns SUCCESS or FAIL depending on whether the
6237    specified grammar matched.  */
6238 static int
6239 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6240 {
6241   unsigned const int *upat = pattern;
6242   char *backtrack_pos = 0;
6243   const char *backtrack_error = 0;
6244   int i, val = 0, backtrack_index = 0;
6245   enum arm_reg_type rtype;
6246   parse_operand_result result;
6247   unsigned int op_parse_code;
6248
6249 #define po_char_or_fail(chr)                    \
6250   do                                            \
6251     {                                           \
6252       if (skip_past_char (&str, chr) == FAIL)   \
6253         goto bad_args;                          \
6254     }                                           \
6255   while (0)
6256
6257 #define po_reg_or_fail(regtype)                                 \
6258   do                                                            \
6259     {                                                           \
6260       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6261                                  & inst.operands[i].vectype);   \
6262       if (val == FAIL)                                          \
6263         {                                                       \
6264           first_error (_(reg_expected_msgs[regtype]));          \
6265           goto failure;                                         \
6266         }                                                       \
6267       inst.operands[i].reg = val;                               \
6268       inst.operands[i].isreg = 1;                               \
6269       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6270       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6271       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6272                              || rtype == REG_TYPE_VFD           \
6273                              || rtype == REG_TYPE_NQ);          \
6274     }                                                           \
6275   while (0)
6276
6277 #define po_reg_or_goto(regtype, label)                          \
6278   do                                                            \
6279     {                                                           \
6280       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6281                                  & inst.operands[i].vectype);   \
6282       if (val == FAIL)                                          \
6283         goto label;                                             \
6284                                                                 \
6285       inst.operands[i].reg = val;                               \
6286       inst.operands[i].isreg = 1;                               \
6287       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6288       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6289       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6290                              || rtype == REG_TYPE_VFD           \
6291                              || rtype == REG_TYPE_NQ);          \
6292     }                                                           \
6293   while (0)
6294
6295 #define po_imm_or_fail(min, max, popt)                          \
6296   do                                                            \
6297     {                                                           \
6298       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6299         goto failure;                                           \
6300       inst.operands[i].imm = val;                               \
6301     }                                                           \
6302   while (0)
6303
6304 #define po_scalar_or_goto(elsz, label)                                  \
6305   do                                                                    \
6306     {                                                                   \
6307       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6308       if (val == FAIL)                                                  \
6309         goto label;                                                     \
6310       inst.operands[i].reg = val;                                       \
6311       inst.operands[i].isscalar = 1;                                    \
6312     }                                                                   \
6313   while (0)
6314
6315 #define po_misc_or_fail(expr)                   \
6316   do                                            \
6317     {                                           \
6318       if (expr)                                 \
6319         goto failure;                           \
6320     }                                           \
6321   while (0)
6322
6323 #define po_misc_or_fail_no_backtrack(expr)              \
6324   do                                                    \
6325     {                                                   \
6326       result = expr;                                    \
6327       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6328         backtrack_pos = 0;                              \
6329       if (result != PARSE_OPERAND_SUCCESS)              \
6330         goto failure;                                   \
6331     }                                                   \
6332   while (0)
6333
6334 #define po_barrier_or_imm(str)                             \
6335   do                                                       \
6336     {                                                      \
6337       val = parse_barrier (&str);                          \
6338       if (val == FAIL)                                     \
6339         {                                                  \
6340           if (ISALPHA (*str))                              \
6341               goto failure;                                \
6342           else                                             \
6343               goto immediate;                              \
6344         }                                                  \
6345       else                                                 \
6346         {                                                  \
6347           if ((inst.instruction & 0xf0) == 0x60            \
6348               && val != 0xf)                               \
6349             {                                              \
6350                /* ISB can only take SY as an option.  */   \
6351                inst.error = _("invalid barrier type");     \
6352                goto failure;                               \
6353             }                                              \
6354         }                                                  \
6355     }                                                      \
6356   while (0)
6357
6358   skip_whitespace (str);
6359
6360   for (i = 0; upat[i] != OP_stop; i++)
6361     {
6362       op_parse_code = upat[i];
6363       if (op_parse_code >= 1<<16)
6364         op_parse_code = thumb ? (op_parse_code >> 16)
6365                                 : (op_parse_code & ((1<<16)-1));
6366
6367       if (op_parse_code >= OP_FIRST_OPTIONAL)
6368         {
6369           /* Remember where we are in case we need to backtrack.  */
6370           gas_assert (!backtrack_pos);
6371           backtrack_pos = str;
6372           backtrack_error = inst.error;
6373           backtrack_index = i;
6374         }
6375
6376       if (i > 0 && (i > 1 || inst.operands[0].present))
6377         po_char_or_fail (',');
6378
6379       switch (op_parse_code)
6380         {
6381           /* Registers */
6382         case OP_oRRnpc:
6383         case OP_oRRnpcsp:
6384         case OP_RRnpc:
6385         case OP_RRnpcsp:
6386         case OP_oRR:
6387         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6388         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6389         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6390         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6391         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6392         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6393         case OP_oRND:
6394         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6395         case OP_RVC:
6396           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6397           break;
6398           /* Also accept generic coprocessor regs for unknown registers.  */
6399           coproc_reg:
6400           po_reg_or_fail (REG_TYPE_CN);
6401           break;
6402         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6403         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6404         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6405         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6406         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6407         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6408         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6409         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6410         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6411         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6412         case OP_oRNQ:
6413         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6414         case OP_oRNDQ:
6415         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6416         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6417         case OP_oRNSDQ:
6418         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6419
6420         /* Neon scalar. Using an element size of 8 means that some invalid
6421            scalars are accepted here, so deal with those in later code.  */
6422         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6423
6424         case OP_RNDQ_I0:
6425           {
6426             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6427             break;
6428             try_imm0:
6429             po_imm_or_fail (0, 0, TRUE);
6430           }
6431           break;
6432
6433         case OP_RVSD_I0:
6434           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6435           break;
6436
6437         case OP_RR_RNSC:
6438           {
6439             po_scalar_or_goto (8, try_rr);
6440             break;
6441             try_rr:
6442             po_reg_or_fail (REG_TYPE_RN);
6443           }
6444           break;
6445
6446         case OP_RNSDQ_RNSC:
6447           {
6448             po_scalar_or_goto (8, try_nsdq);
6449             break;
6450             try_nsdq:
6451             po_reg_or_fail (REG_TYPE_NSDQ);
6452           }
6453           break;
6454
6455         case OP_RNDQ_RNSC:
6456           {
6457             po_scalar_or_goto (8, try_ndq);
6458             break;
6459             try_ndq:
6460             po_reg_or_fail (REG_TYPE_NDQ);
6461           }
6462           break;
6463
6464         case OP_RND_RNSC:
6465           {
6466             po_scalar_or_goto (8, try_vfd);
6467             break;
6468             try_vfd:
6469             po_reg_or_fail (REG_TYPE_VFD);
6470           }
6471           break;
6472
6473         case OP_VMOV:
6474           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6475              not careful then bad things might happen.  */
6476           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6477           break;
6478
6479         case OP_RNDQ_Ibig:
6480           {
6481             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6482             break;
6483             try_immbig:
6484             /* There's a possibility of getting a 64-bit immediate here, so
6485                we need special handling.  */
6486             if (parse_big_immediate (&str, i) == FAIL)
6487               {
6488                 inst.error = _("immediate value is out of range");
6489                 goto failure;
6490               }
6491           }
6492           break;
6493
6494         case OP_RNDQ_I63b:
6495           {
6496             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6497             break;
6498             try_shimm:
6499             po_imm_or_fail (0, 63, TRUE);
6500           }
6501           break;
6502
6503         case OP_RRnpcb:
6504           po_char_or_fail ('[');
6505           po_reg_or_fail  (REG_TYPE_RN);
6506           po_char_or_fail (']');
6507           break;
6508
6509         case OP_RRnpctw:
6510         case OP_RRw:
6511         case OP_oRRw:
6512           po_reg_or_fail (REG_TYPE_RN);
6513           if (skip_past_char (&str, '!') == SUCCESS)
6514             inst.operands[i].writeback = 1;
6515           break;
6516
6517           /* Immediates */
6518         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6519         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6520         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6521         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6522         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6523         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6524         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6525         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6526         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6527         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6528         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6529         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6530
6531         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6532         case OP_oI7b:
6533         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6534         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6535         case OP_oI31b:
6536         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6537         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6538         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
6539         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6540
6541           /* Immediate variants */
6542         case OP_oI255c:
6543           po_char_or_fail ('{');
6544           po_imm_or_fail (0, 255, TRUE);
6545           po_char_or_fail ('}');
6546           break;
6547
6548         case OP_I31w:
6549           /* The expression parser chokes on a trailing !, so we have
6550              to find it first and zap it.  */
6551           {
6552             char *s = str;
6553             while (*s && *s != ',')
6554               s++;
6555             if (s[-1] == '!')
6556               {
6557                 s[-1] = '\0';
6558                 inst.operands[i].writeback = 1;
6559               }
6560             po_imm_or_fail (0, 31, TRUE);
6561             if (str == s - 1)
6562               str = s;
6563           }
6564           break;
6565
6566           /* Expressions */
6567         case OP_EXPi:   EXPi:
6568           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6569                                               GE_OPT_PREFIX));
6570           break;
6571
6572         case OP_EXP:
6573           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6574                                               GE_NO_PREFIX));
6575           break;
6576
6577         case OP_EXPr:   EXPr:
6578           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6579                                               GE_NO_PREFIX));
6580           if (inst.reloc.exp.X_op == O_symbol)
6581             {
6582               val = parse_reloc (&str);
6583               if (val == -1)
6584                 {
6585                   inst.error = _("unrecognized relocation suffix");
6586                   goto failure;
6587                 }
6588               else if (val != BFD_RELOC_UNUSED)
6589                 {
6590                   inst.operands[i].imm = val;
6591                   inst.operands[i].hasreloc = 1;
6592                 }
6593             }
6594           break;
6595
6596           /* Operand for MOVW or MOVT.  */
6597         case OP_HALF:
6598           po_misc_or_fail (parse_half (&str));
6599           break;
6600
6601           /* Register or expression.  */
6602         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
6603         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
6604
6605           /* Register or immediate.  */
6606         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
6607         I0:               po_imm_or_fail (0, 0, FALSE);       break;
6608
6609         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
6610         IF:
6611           if (!is_immediate_prefix (*str))
6612             goto bad_args;
6613           str++;
6614           val = parse_fpa_immediate (&str);
6615           if (val == FAIL)
6616             goto failure;
6617           /* FPA immediates are encoded as registers 8-15.
6618              parse_fpa_immediate has already applied the offset.  */
6619           inst.operands[i].reg = val;
6620           inst.operands[i].isreg = 1;
6621           break;
6622
6623         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
6624         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
6625
6626           /* Two kinds of register.  */
6627         case OP_RIWR_RIWC:
6628           {
6629             struct reg_entry *rege = arm_reg_parse_multi (&str);
6630             if (!rege
6631                 || (rege->type != REG_TYPE_MMXWR
6632                     && rege->type != REG_TYPE_MMXWC
6633                     && rege->type != REG_TYPE_MMXWCG))
6634               {
6635                 inst.error = _("iWMMXt data or control register expected");
6636                 goto failure;
6637               }
6638             inst.operands[i].reg = rege->number;
6639             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
6640           }
6641           break;
6642
6643         case OP_RIWC_RIWG:
6644           {
6645             struct reg_entry *rege = arm_reg_parse_multi (&str);
6646             if (!rege
6647                 || (rege->type != REG_TYPE_MMXWC
6648                     && rege->type != REG_TYPE_MMXWCG))
6649               {
6650                 inst.error = _("iWMMXt control register expected");
6651                 goto failure;
6652               }
6653             inst.operands[i].reg = rege->number;
6654             inst.operands[i].isreg = 1;
6655           }
6656           break;
6657
6658           /* Misc */
6659         case OP_CPSF:    val = parse_cps_flags (&str);          break;
6660         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
6661         case OP_oROR:    val = parse_ror (&str);                break;
6662         case OP_COND:    val = parse_cond (&str);               break;
6663         case OP_oBARRIER_I15:
6664           po_barrier_or_imm (str); break;
6665           immediate:
6666           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
6667             goto failure;
6668           break;
6669
6670         case OP_wPSR:
6671         case OP_rPSR:
6672           po_reg_or_goto (REG_TYPE_RNB, try_psr);
6673           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
6674             {
6675               inst.error = _("Banked registers are not available with this "
6676                              "architecture.");
6677               goto failure;
6678             }
6679           break;
6680           try_psr:
6681           val = parse_psr (&str, op_parse_code == OP_wPSR);
6682           break;
6683
6684         case OP_APSR_RR:
6685           po_reg_or_goto (REG_TYPE_RN, try_apsr);
6686           break;
6687           try_apsr:
6688           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
6689              instruction).  */
6690           if (strncasecmp (str, "APSR_", 5) == 0)
6691             {
6692               unsigned found = 0;
6693               str += 5;
6694               while (found < 15)
6695                 switch (*str++)
6696                   {
6697                   case 'c': found = (found & 1) ? 16 : found | 1; break;
6698                   case 'n': found = (found & 2) ? 16 : found | 2; break;
6699                   case 'z': found = (found & 4) ? 16 : found | 4; break;
6700                   case 'v': found = (found & 8) ? 16 : found | 8; break;
6701                   default: found = 16;
6702                   }
6703               if (found != 15)
6704                 goto failure;
6705               inst.operands[i].isvec = 1;
6706               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
6707               inst.operands[i].reg = REG_PC;
6708             }
6709           else
6710             goto failure;
6711           break;
6712
6713         case OP_TB:
6714           po_misc_or_fail (parse_tb (&str));
6715           break;
6716
6717           /* Register lists.  */
6718         case OP_REGLST:
6719           val = parse_reg_list (&str);
6720           if (*str == '^')
6721             {
6722               inst.operands[1].writeback = 1;
6723               str++;
6724             }
6725           break;
6726
6727         case OP_VRSLST:
6728           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
6729           break;
6730
6731         case OP_VRDLST:
6732           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
6733           break;
6734
6735         case OP_VRSDLST:
6736           /* Allow Q registers too.  */
6737           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6738                                     REGLIST_NEON_D);
6739           if (val == FAIL)
6740             {
6741               inst.error = NULL;
6742               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6743                                         REGLIST_VFP_S);
6744               inst.operands[i].issingle = 1;
6745             }
6746           break;
6747
6748         case OP_NRDLST:
6749           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6750                                     REGLIST_NEON_D);
6751           break;
6752
6753         case OP_NSTRLST:
6754           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
6755                                            &inst.operands[i].vectype);
6756           break;
6757
6758           /* Addressing modes */
6759         case OP_ADDR:
6760           po_misc_or_fail (parse_address (&str, i));
6761           break;
6762
6763         case OP_ADDRGLDR:
6764           po_misc_or_fail_no_backtrack (
6765             parse_address_group_reloc (&str, i, GROUP_LDR));
6766           break;
6767
6768         case OP_ADDRGLDRS:
6769           po_misc_or_fail_no_backtrack (
6770             parse_address_group_reloc (&str, i, GROUP_LDRS));
6771           break;
6772
6773         case OP_ADDRGLDC:
6774           po_misc_or_fail_no_backtrack (
6775             parse_address_group_reloc (&str, i, GROUP_LDC));
6776           break;
6777
6778         case OP_SH:
6779           po_misc_or_fail (parse_shifter_operand (&str, i));
6780           break;
6781
6782         case OP_SHG:
6783           po_misc_or_fail_no_backtrack (
6784             parse_shifter_operand_group_reloc (&str, i));
6785           break;
6786
6787         case OP_oSHll:
6788           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
6789           break;
6790
6791         case OP_oSHar:
6792           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
6793           break;
6794
6795         case OP_oSHllar:
6796           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
6797           break;
6798
6799         default:
6800           as_fatal (_("unhandled operand code %d"), op_parse_code);
6801         }
6802
6803       /* Various value-based sanity checks and shared operations.  We
6804          do not signal immediate failures for the register constraints;
6805          this allows a syntax error to take precedence.  */
6806       switch (op_parse_code)
6807         {
6808         case OP_oRRnpc:
6809         case OP_RRnpc:
6810         case OP_RRnpcb:
6811         case OP_RRw:
6812         case OP_oRRw:
6813         case OP_RRnpc_I0:
6814           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
6815             inst.error = BAD_PC;
6816           break;
6817
6818         case OP_oRRnpcsp:
6819         case OP_RRnpcsp:
6820           if (inst.operands[i].isreg)
6821             {
6822               if (inst.operands[i].reg == REG_PC)
6823                 inst.error = BAD_PC;
6824               else if (inst.operands[i].reg == REG_SP)
6825                 inst.error = BAD_SP;
6826             }
6827           break;
6828
6829         case OP_RRnpctw:
6830           if (inst.operands[i].isreg
6831               && inst.operands[i].reg == REG_PC
6832               && (inst.operands[i].writeback || thumb))
6833             inst.error = BAD_PC;
6834           break;
6835
6836         case OP_CPSF:
6837         case OP_ENDI:
6838         case OP_oROR:
6839         case OP_wPSR:
6840         case OP_rPSR:
6841         case OP_COND:
6842         case OP_oBARRIER_I15:
6843         case OP_REGLST:
6844         case OP_VRSLST:
6845         case OP_VRDLST:
6846         case OP_VRSDLST:
6847         case OP_NRDLST:
6848         case OP_NSTRLST:
6849           if (val == FAIL)
6850             goto failure;
6851           inst.operands[i].imm = val;
6852           break;
6853
6854         default:
6855           break;
6856         }
6857
6858       /* If we get here, this operand was successfully parsed.  */
6859       inst.operands[i].present = 1;
6860       continue;
6861
6862     bad_args:
6863       inst.error = BAD_ARGS;
6864
6865     failure:
6866       if (!backtrack_pos)
6867         {
6868           /* The parse routine should already have set inst.error, but set a
6869              default here just in case.  */
6870           if (!inst.error)
6871             inst.error = _("syntax error");
6872           return FAIL;
6873         }
6874
6875       /* Do not backtrack over a trailing optional argument that
6876          absorbed some text.  We will only fail again, with the
6877          'garbage following instruction' error message, which is
6878          probably less helpful than the current one.  */
6879       if (backtrack_index == i && backtrack_pos != str
6880           && upat[i+1] == OP_stop)
6881         {
6882           if (!inst.error)
6883             inst.error = _("syntax error");
6884           return FAIL;
6885         }
6886
6887       /* Try again, skipping the optional argument at backtrack_pos.  */
6888       str = backtrack_pos;
6889       inst.error = backtrack_error;
6890       inst.operands[backtrack_index].present = 0;
6891       i = backtrack_index;
6892       backtrack_pos = 0;
6893     }
6894
6895   /* Check that we have parsed all the arguments.  */
6896   if (*str != '\0' && !inst.error)
6897     inst.error = _("garbage following instruction");
6898
6899   return inst.error ? FAIL : SUCCESS;
6900 }
6901
6902 #undef po_char_or_fail
6903 #undef po_reg_or_fail
6904 #undef po_reg_or_goto
6905 #undef po_imm_or_fail
6906 #undef po_scalar_or_fail
6907 #undef po_barrier_or_imm
6908
6909 /* Shorthand macro for instruction encoding functions issuing errors.  */
6910 #define constraint(expr, err)                   \
6911   do                                            \
6912     {                                           \
6913       if (expr)                                 \
6914         {                                       \
6915           inst.error = err;                     \
6916           return;                               \
6917         }                                       \
6918     }                                           \
6919   while (0)
6920
6921 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
6922    instructions are unpredictable if these registers are used.  This
6923    is the BadReg predicate in ARM's Thumb-2 documentation.  */
6924 #define reject_bad_reg(reg)                             \
6925   do                                                    \
6926    if (reg == REG_SP || reg == REG_PC)                  \
6927      {                                                  \
6928        inst.error = (reg == REG_SP) ? BAD_SP : BAD_PC;  \
6929        return;                                          \
6930      }                                                  \
6931   while (0)
6932
6933 /* If REG is R13 (the stack pointer), warn that its use is
6934    deprecated.  */
6935 #define warn_deprecated_sp(reg)                 \
6936   do                                            \
6937     if (warn_on_deprecated && reg == REG_SP)    \
6938        as_warn (_("use of r13 is deprecated")); \
6939   while (0)
6940
6941 /* Functions for operand encoding.  ARM, then Thumb.  */
6942
6943 #define rotate_left(v, n) (v << n | v >> (32 - n))
6944
6945 /* If VAL can be encoded in the immediate field of an ARM instruction,
6946    return the encoded form.  Otherwise, return FAIL.  */
6947
6948 static unsigned int
6949 encode_arm_immediate (unsigned int val)
6950 {
6951   unsigned int a, i;
6952
6953   for (i = 0; i < 32; i += 2)
6954     if ((a = rotate_left (val, i)) <= 0xff)
6955       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
6956
6957   return FAIL;
6958 }
6959
6960 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
6961    return the encoded form.  Otherwise, return FAIL.  */
6962 static unsigned int
6963 encode_thumb32_immediate (unsigned int val)
6964 {
6965   unsigned int a, i;
6966
6967   if (val <= 0xff)
6968     return val;
6969
6970   for (i = 1; i <= 24; i++)
6971     {
6972       a = val >> i;
6973       if ((val & ~(0xff << i)) == 0)
6974         return ((val >> i) & 0x7f) | ((32 - i) << 7);
6975     }
6976
6977   a = val & 0xff;
6978   if (val == ((a << 16) | a))
6979     return 0x100 | a;
6980   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
6981     return 0x300 | a;
6982
6983   a = val & 0xff00;
6984   if (val == ((a << 16) | a))
6985     return 0x200 | (a >> 8);
6986
6987   return FAIL;
6988 }
6989 /* Encode a VFP SP or DP register number into inst.instruction.  */
6990
6991 static void
6992 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
6993 {
6994   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
6995       && reg > 15)
6996     {
6997       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
6998         {
6999           if (thumb_mode)
7000             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
7001                                     fpu_vfp_ext_d32);
7002           else
7003             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
7004                                     fpu_vfp_ext_d32);
7005         }
7006       else
7007         {
7008           first_error (_("D register out of range for selected VFP version"));
7009           return;
7010         }
7011     }
7012
7013   switch (pos)
7014     {
7015     case VFP_REG_Sd:
7016       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
7017       break;
7018
7019     case VFP_REG_Sn:
7020       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
7021       break;
7022
7023     case VFP_REG_Sm:
7024       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
7025       break;
7026
7027     case VFP_REG_Dd:
7028       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
7029       break;
7030
7031     case VFP_REG_Dn:
7032       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
7033       break;
7034
7035     case VFP_REG_Dm:
7036       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
7037       break;
7038
7039     default:
7040       abort ();
7041     }
7042 }
7043
7044 /* Encode a <shift> in an ARM-format instruction.  The immediate,
7045    if any, is handled by md_apply_fix.   */
7046 static void
7047 encode_arm_shift (int i)
7048 {
7049   if (inst.operands[i].shift_kind == SHIFT_RRX)
7050     inst.instruction |= SHIFT_ROR << 5;
7051   else
7052     {
7053       inst.instruction |= inst.operands[i].shift_kind << 5;
7054       if (inst.operands[i].immisreg)
7055         {
7056           inst.instruction |= SHIFT_BY_REG;
7057           inst.instruction |= inst.operands[i].imm << 8;
7058         }
7059       else
7060         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7061     }
7062 }
7063
7064 static void
7065 encode_arm_shifter_operand (int i)
7066 {
7067   if (inst.operands[i].isreg)
7068     {
7069       inst.instruction |= inst.operands[i].reg;
7070       encode_arm_shift (i);
7071     }
7072   else
7073     {
7074       inst.instruction |= INST_IMMEDIATE;
7075       if (inst.reloc.type != BFD_RELOC_ARM_IMMEDIATE)
7076         inst.instruction |= inst.operands[i].imm;
7077     }
7078 }
7079
7080 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7081 static void
7082 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7083 {
7084   /* PR 14260:
7085      Generate an error if the operand is not a register.  */
7086   constraint (!inst.operands[i].isreg,
7087               _("Instruction does not support =N addresses"));
7088
7089   inst.instruction |= inst.operands[i].reg << 16;
7090
7091   if (inst.operands[i].preind)
7092     {
7093       if (is_t)
7094         {
7095           inst.error = _("instruction does not accept preindexed addressing");
7096           return;
7097         }
7098       inst.instruction |= PRE_INDEX;
7099       if (inst.operands[i].writeback)
7100         inst.instruction |= WRITE_BACK;
7101
7102     }
7103   else if (inst.operands[i].postind)
7104     {
7105       gas_assert (inst.operands[i].writeback);
7106       if (is_t)
7107         inst.instruction |= WRITE_BACK;
7108     }
7109   else /* unindexed - only for coprocessor */
7110     {
7111       inst.error = _("instruction does not accept unindexed addressing");
7112       return;
7113     }
7114
7115   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7116       && (((inst.instruction & 0x000f0000) >> 16)
7117           == ((inst.instruction & 0x0000f000) >> 12)))
7118     as_warn ((inst.instruction & LOAD_BIT)
7119              ? _("destination register same as write-back base")
7120              : _("source register same as write-back base"));
7121 }
7122
7123 /* inst.operands[i] was set up by parse_address.  Encode it into an
7124    ARM-format mode 2 load or store instruction.  If is_t is true,
7125    reject forms that cannot be used with a T instruction (i.e. not
7126    post-indexed).  */
7127 static void
7128 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7129 {
7130   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7131
7132   encode_arm_addr_mode_common (i, is_t);
7133
7134   if (inst.operands[i].immisreg)
7135     {
7136       constraint ((inst.operands[i].imm == REG_PC
7137                    || (is_pc && inst.operands[i].writeback)),
7138                   BAD_PC_ADDRESSING);
7139       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7140       inst.instruction |= inst.operands[i].imm;
7141       if (!inst.operands[i].negative)
7142         inst.instruction |= INDEX_UP;
7143       if (inst.operands[i].shifted)
7144         {
7145           if (inst.operands[i].shift_kind == SHIFT_RRX)
7146             inst.instruction |= SHIFT_ROR << 5;
7147           else
7148             {
7149               inst.instruction |= inst.operands[i].shift_kind << 5;
7150               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7151             }
7152         }
7153     }
7154   else /* immediate offset in inst.reloc */
7155     {
7156       if (is_pc && !inst.reloc.pc_rel)
7157         {
7158           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7159
7160           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7161              cannot use PC in addressing.
7162              PC cannot be used in writeback addressing, either.  */
7163           constraint ((is_t || inst.operands[i].writeback),
7164                       BAD_PC_ADDRESSING);
7165
7166           /* Use of PC in str is deprecated for ARMv7.  */
7167           if (warn_on_deprecated
7168               && !is_load
7169               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7170             as_warn (_("use of PC in this instruction is deprecated"));
7171         }
7172
7173       if (inst.reloc.type == BFD_RELOC_UNUSED)
7174         {
7175           /* Prefer + for zero encoded value.  */
7176           if (!inst.operands[i].negative)
7177             inst.instruction |= INDEX_UP;
7178           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7179         }
7180     }
7181 }
7182
7183 /* inst.operands[i] was set up by parse_address.  Encode it into an
7184    ARM-format mode 3 load or store instruction.  Reject forms that
7185    cannot be used with such instructions.  If is_t is true, reject
7186    forms that cannot be used with a T instruction (i.e. not
7187    post-indexed).  */
7188 static void
7189 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7190 {
7191   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7192     {
7193       inst.error = _("instruction does not accept scaled register index");
7194       return;
7195     }
7196
7197   encode_arm_addr_mode_common (i, is_t);
7198
7199   if (inst.operands[i].immisreg)
7200     {
7201       constraint ((inst.operands[i].imm == REG_PC
7202                    || inst.operands[i].reg == REG_PC),
7203                   BAD_PC_ADDRESSING);
7204       inst.instruction |= inst.operands[i].imm;
7205       if (!inst.operands[i].negative)
7206         inst.instruction |= INDEX_UP;
7207     }
7208   else /* immediate offset in inst.reloc */
7209     {
7210       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7211                    && inst.operands[i].writeback),
7212                   BAD_PC_WRITEBACK);
7213       inst.instruction |= HWOFFSET_IMM;
7214       if (inst.reloc.type == BFD_RELOC_UNUSED)
7215         {
7216           /* Prefer + for zero encoded value.  */
7217           if (!inst.operands[i].negative)
7218             inst.instruction |= INDEX_UP;
7219
7220           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7221         }
7222     }
7223 }
7224
7225 /* inst.operands[i] was set up by parse_address.  Encode it into an
7226    ARM-format instruction.  Reject all forms which cannot be encoded
7227    into a coprocessor load/store instruction.  If wb_ok is false,
7228    reject use of writeback; if unind_ok is false, reject use of
7229    unindexed addressing.  If reloc_override is not 0, use it instead
7230    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
7231    (in which case it is preserved).  */
7232
7233 static int
7234 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
7235 {
7236   inst.instruction |= inst.operands[i].reg << 16;
7237
7238   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
7239
7240   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
7241     {
7242       gas_assert (!inst.operands[i].writeback);
7243       if (!unind_ok)
7244         {
7245           inst.error = _("instruction does not support unindexed addressing");
7246           return FAIL;
7247         }
7248       inst.instruction |= inst.operands[i].imm;
7249       inst.instruction |= INDEX_UP;
7250       return SUCCESS;
7251     }
7252
7253   if (inst.operands[i].preind)
7254     inst.instruction |= PRE_INDEX;
7255
7256   if (inst.operands[i].writeback)
7257     {
7258       if (inst.operands[i].reg == REG_PC)
7259         {
7260           inst.error = _("pc may not be used with write-back");
7261           return FAIL;
7262         }
7263       if (!wb_ok)
7264         {
7265           inst.error = _("instruction does not support writeback");
7266           return FAIL;
7267         }
7268       inst.instruction |= WRITE_BACK;
7269     }
7270
7271   if (reloc_override)
7272     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
7273   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
7274             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
7275            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
7276     {
7277       if (thumb_mode)
7278         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
7279       else
7280         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
7281     }
7282
7283   /* Prefer + for zero encoded value.  */
7284   if (!inst.operands[i].negative)
7285     inst.instruction |= INDEX_UP;
7286
7287   return SUCCESS;
7288 }
7289
7290 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7291    Determine whether it can be performed with a move instruction; if
7292    it can, convert inst.instruction to that move instruction and
7293    return TRUE; if it can't, convert inst.instruction to a literal-pool
7294    load and return FALSE.  If this is not a valid thing to do in the
7295    current context, set inst.error and return TRUE.
7296
7297    inst.operands[i] describes the destination register.  */
7298
7299 static bfd_boolean
7300 move_or_literal_pool (int i, bfd_boolean thumb_p, bfd_boolean mode_3)
7301 {
7302   unsigned long tbit;
7303
7304   if (thumb_p)
7305     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7306   else
7307     tbit = LOAD_BIT;
7308
7309   if ((inst.instruction & tbit) == 0)
7310     {
7311       inst.error = _("invalid pseudo operation");
7312       return TRUE;
7313     }
7314   if (inst.reloc.exp.X_op != O_constant && inst.reloc.exp.X_op != O_symbol)
7315     {
7316       inst.error = _("constant expression expected");
7317       return TRUE;
7318     }
7319   if (inst.reloc.exp.X_op == O_constant)
7320     {
7321       if (thumb_p)
7322         {
7323           if (!unified_syntax && (inst.reloc.exp.X_add_number & ~0xFF) == 0)
7324             {
7325               /* This can be done with a mov(1) instruction.  */
7326               inst.instruction  = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
7327               inst.instruction |= inst.reloc.exp.X_add_number;
7328               return TRUE;
7329             }
7330         }
7331       else
7332         {
7333           int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
7334           if (value != FAIL)
7335             {
7336               /* This can be done with a mov instruction.  */
7337               inst.instruction &= LITERAL_MASK;
7338               inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
7339               inst.instruction |= value & 0xfff;
7340               return TRUE;
7341             }
7342
7343           value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
7344           if (value != FAIL)
7345             {
7346               /* This can be done with a mvn instruction.  */
7347               inst.instruction &= LITERAL_MASK;
7348               inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
7349               inst.instruction |= value & 0xfff;
7350               return TRUE;
7351             }
7352         }
7353     }
7354
7355   if (add_to_lit_pool () == FAIL)
7356     {
7357       inst.error = _("literal pool insertion failed");
7358       return TRUE;
7359     }
7360   inst.operands[1].reg = REG_PC;
7361   inst.operands[1].isreg = 1;
7362   inst.operands[1].preind = 1;
7363   inst.reloc.pc_rel = 1;
7364   inst.reloc.type = (thumb_p
7365                      ? BFD_RELOC_ARM_THUMB_OFFSET
7366                      : (mode_3
7367                         ? BFD_RELOC_ARM_HWLITERAL
7368                         : BFD_RELOC_ARM_LITERAL));
7369   return FALSE;
7370 }
7371
7372 /* Functions for instruction encoding, sorted by sub-architecture.
7373    First some generics; their names are taken from the conventional
7374    bit positions for register arguments in ARM format instructions.  */
7375
7376 static void
7377 do_noargs (void)
7378 {
7379 }
7380
7381 static void
7382 do_rd (void)
7383 {
7384   inst.instruction |= inst.operands[0].reg << 12;
7385 }
7386
7387 static void
7388 do_rd_rm (void)
7389 {
7390   inst.instruction |= inst.operands[0].reg << 12;
7391   inst.instruction |= inst.operands[1].reg;
7392 }
7393
7394 static void
7395 do_rm_rn (void)
7396 {
7397   inst.instruction |= inst.operands[0].reg;
7398   inst.instruction |= inst.operands[1].reg << 16;
7399 }
7400
7401 static void
7402 do_rd_rn (void)
7403 {
7404   inst.instruction |= inst.operands[0].reg << 12;
7405   inst.instruction |= inst.operands[1].reg << 16;
7406 }
7407
7408 static void
7409 do_rn_rd (void)
7410 {
7411   inst.instruction |= inst.operands[0].reg << 16;
7412   inst.instruction |= inst.operands[1].reg << 12;
7413 }
7414
7415 static bfd_boolean
7416 check_obsolete (const arm_feature_set *feature, const char *msg)
7417 {
7418   if (ARM_CPU_IS_ANY (cpu_variant))
7419     {
7420       as_warn ("%s", msg);
7421       return TRUE;
7422     }
7423   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
7424     {
7425       as_bad ("%s", msg);
7426       return TRUE;
7427     }
7428
7429   return FALSE;
7430 }
7431
7432 static void
7433 do_rd_rm_rn (void)
7434 {
7435   unsigned Rn = inst.operands[2].reg;
7436   /* Enforce restrictions on SWP instruction.  */
7437   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
7438     {
7439       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
7440                   _("Rn must not overlap other operands"));
7441
7442       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
7443        */
7444       if (!check_obsolete (&arm_ext_v8,
7445                            _("swp{b} use is obsoleted for ARMv8 and later"))
7446           && warn_on_deprecated
7447           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
7448         as_warn (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
7449     }
7450
7451   inst.instruction |= inst.operands[0].reg << 12;
7452   inst.instruction |= inst.operands[1].reg;
7453   inst.instruction |= Rn << 16;
7454 }
7455
7456 static void
7457 do_rd_rn_rm (void)
7458 {
7459   inst.instruction |= inst.operands[0].reg << 12;
7460   inst.instruction |= inst.operands[1].reg << 16;
7461   inst.instruction |= inst.operands[2].reg;
7462 }
7463
7464 static void
7465 do_rm_rd_rn (void)
7466 {
7467   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
7468   constraint (((inst.reloc.exp.X_op != O_constant
7469                 && inst.reloc.exp.X_op != O_illegal)
7470                || inst.reloc.exp.X_add_number != 0),
7471               BAD_ADDR_MODE);
7472   inst.instruction |= inst.operands[0].reg;
7473   inst.instruction |= inst.operands[1].reg << 12;
7474   inst.instruction |= inst.operands[2].reg << 16;
7475 }
7476
7477 static void
7478 do_imm0 (void)
7479 {
7480   inst.instruction |= inst.operands[0].imm;
7481 }
7482
7483 static void
7484 do_rd_cpaddr (void)
7485 {
7486   inst.instruction |= inst.operands[0].reg << 12;
7487   encode_arm_cp_address (1, TRUE, TRUE, 0);
7488 }
7489
7490 /* ARM instructions, in alphabetical order by function name (except
7491    that wrapper functions appear immediately after the function they
7492    wrap).  */
7493
7494 /* This is a pseudo-op of the form "adr rd, label" to be converted
7495    into a relative address of the form "add rd, pc, #label-.-8".  */
7496
7497 static void
7498 do_adr (void)
7499 {
7500   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
7501
7502   /* Frag hacking will turn this into a sub instruction if the offset turns
7503      out to be negative.  */
7504   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
7505   inst.reloc.pc_rel = 1;
7506   inst.reloc.exp.X_add_number -= 8;
7507 }
7508
7509 /* This is a pseudo-op of the form "adrl rd, label" to be converted
7510    into a relative address of the form:
7511    add rd, pc, #low(label-.-8)"
7512    add rd, rd, #high(label-.-8)"  */
7513
7514 static void
7515 do_adrl (void)
7516 {
7517   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
7518
7519   /* Frag hacking will turn this into a sub instruction if the offset turns
7520      out to be negative.  */
7521   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
7522   inst.reloc.pc_rel            = 1;
7523   inst.size                    = INSN_SIZE * 2;
7524   inst.reloc.exp.X_add_number -= 8;
7525 }
7526
7527 static void
7528 do_arit (void)
7529 {
7530   if (!inst.operands[1].present)
7531     inst.operands[1].reg = inst.operands[0].reg;
7532   inst.instruction |= inst.operands[0].reg << 12;
7533   inst.instruction |= inst.operands[1].reg << 16;
7534   encode_arm_shifter_operand (2);
7535 }
7536
7537 static void
7538 do_barrier (void)
7539 {
7540   if (inst.operands[0].present)
7541     {
7542       constraint ((inst.instruction & 0xf0) != 0x40
7543                   && inst.operands[0].imm > 0xf
7544                   && inst.operands[0].imm < 0x0,
7545                   _("bad barrier type"));
7546       inst.instruction |= inst.operands[0].imm;
7547     }
7548   else
7549     inst.instruction |= 0xf;
7550 }
7551
7552 static void
7553 do_bfc (void)
7554 {
7555   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
7556   constraint (msb > 32, _("bit-field extends past end of register"));
7557   /* The instruction encoding stores the LSB and MSB,
7558      not the LSB and width.  */
7559   inst.instruction |= inst.operands[0].reg << 12;
7560   inst.instruction |= inst.operands[1].imm << 7;
7561   inst.instruction |= (msb - 1) << 16;
7562 }
7563
7564 static void
7565 do_bfi (void)
7566 {
7567   unsigned int msb;
7568
7569   /* #0 in second position is alternative syntax for bfc, which is
7570      the same instruction but with REG_PC in the Rm field.  */
7571   if (!inst.operands[1].isreg)
7572     inst.operands[1].reg = REG_PC;
7573
7574   msb = inst.operands[2].imm + inst.operands[3].imm;
7575   constraint (msb > 32, _("bit-field extends past end of register"));
7576   /* The instruction encoding stores the LSB and MSB,
7577      not the LSB and width.  */
7578   inst.instruction |= inst.operands[0].reg << 12;
7579   inst.instruction |= inst.operands[1].reg;
7580   inst.instruction |= inst.operands[2].imm << 7;
7581   inst.instruction |= (msb - 1) << 16;
7582 }
7583
7584 static void
7585 do_bfx (void)
7586 {
7587   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
7588               _("bit-field extends past end of register"));
7589   inst.instruction |= inst.operands[0].reg << 12;
7590   inst.instruction |= inst.operands[1].reg;
7591   inst.instruction |= inst.operands[2].imm << 7;
7592   inst.instruction |= (inst.operands[3].imm - 1) << 16;
7593 }
7594
7595 /* ARM V5 breakpoint instruction (argument parse)
7596      BKPT <16 bit unsigned immediate>
7597      Instruction is not conditional.
7598         The bit pattern given in insns[] has the COND_ALWAYS condition,
7599         and it is an error if the caller tried to override that.  */
7600
7601 static void
7602 do_bkpt (void)
7603 {
7604   /* Top 12 of 16 bits to bits 19:8.  */
7605   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
7606
7607   /* Bottom 4 of 16 bits to bits 3:0.  */
7608   inst.instruction |= inst.operands[0].imm & 0xf;
7609 }
7610
7611 static void
7612 encode_branch (int default_reloc)
7613 {
7614   if (inst.operands[0].hasreloc)
7615     {
7616       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
7617                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
7618                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
7619       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
7620         ? BFD_RELOC_ARM_PLT32
7621         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
7622     }
7623   else
7624     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
7625   inst.reloc.pc_rel = 1;
7626 }
7627
7628 static void
7629 do_branch (void)
7630 {
7631 #ifdef OBJ_ELF
7632   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
7633     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
7634   else
7635 #endif
7636     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
7637 }
7638
7639 static void
7640 do_bl (void)
7641 {
7642 #ifdef OBJ_ELF
7643   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
7644     {
7645       if (inst.cond == COND_ALWAYS)
7646         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
7647       else
7648         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
7649     }
7650   else
7651 #endif
7652     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
7653 }
7654
7655 /* ARM V5 branch-link-exchange instruction (argument parse)
7656      BLX <target_addr>          ie BLX(1)
7657      BLX{<condition>} <Rm>      ie BLX(2)
7658    Unfortunately, there are two different opcodes for this mnemonic.
7659    So, the insns[].value is not used, and the code here zaps values
7660         into inst.instruction.
7661    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
7662
7663 static void
7664 do_blx (void)
7665 {
7666   if (inst.operands[0].isreg)
7667     {
7668       /* Arg is a register; the opcode provided by insns[] is correct.
7669          It is not illegal to do "blx pc", just useless.  */
7670       if (inst.operands[0].reg == REG_PC)
7671         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
7672
7673       inst.instruction |= inst.operands[0].reg;
7674     }
7675   else
7676     {
7677       /* Arg is an address; this instruction cannot be executed
7678          conditionally, and the opcode must be adjusted.
7679          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
7680          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
7681       constraint (inst.cond != COND_ALWAYS, BAD_COND);
7682       inst.instruction = 0xfa000000;
7683       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
7684     }
7685 }
7686
7687 static void
7688 do_bx (void)
7689 {
7690   bfd_boolean want_reloc;
7691
7692   if (inst.operands[0].reg == REG_PC)
7693     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
7694
7695   inst.instruction |= inst.operands[0].reg;
7696   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
7697      it is for ARMv4t or earlier.  */
7698   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
7699   if (object_arch && !ARM_CPU_HAS_FEATURE (*object_arch, arm_ext_v5))
7700       want_reloc = TRUE;
7701
7702 #ifdef OBJ_ELF
7703   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
7704 #endif
7705     want_reloc = FALSE;
7706
7707   if (want_reloc)
7708     inst.reloc.type = BFD_RELOC_ARM_V4BX;
7709 }
7710
7711
7712 /* ARM v5TEJ.  Jump to Jazelle code.  */
7713
7714 static void
7715 do_bxj (void)
7716 {
7717   if (inst.operands[0].reg == REG_PC)
7718     as_tsktsk (_("use of r15 in bxj is not really useful"));
7719
7720   inst.instruction |= inst.operands[0].reg;
7721 }
7722
7723 /* Co-processor data operation:
7724       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
7725       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
7726 static void
7727 do_cdp (void)
7728 {
7729   inst.instruction |= inst.operands[0].reg << 8;
7730   inst.instruction |= inst.operands[1].imm << 20;
7731   inst.instruction |= inst.operands[2].reg << 12;
7732   inst.instruction |= inst.operands[3].reg << 16;
7733   inst.instruction |= inst.operands[4].reg;
7734   inst.instruction |= inst.operands[5].imm << 5;
7735 }
7736
7737 static void
7738 do_cmp (void)
7739 {
7740   inst.instruction |= inst.operands[0].reg << 16;
7741   encode_arm_shifter_operand (1);
7742 }
7743
7744 /* Transfer between coprocessor and ARM registers.
7745    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
7746    MRC2
7747    MCR{cond}
7748    MCR2
7749
7750    No special properties.  */
7751
7752 struct deprecated_coproc_regs_s
7753 {
7754   unsigned cp;
7755   int opc1;
7756   unsigned crn;
7757   unsigned crm;
7758   int opc2;
7759   arm_feature_set deprecated;
7760   arm_feature_set obsoleted;
7761   const char *dep_msg;
7762   const char *obs_msg;
7763 };
7764
7765 #define DEPR_ACCESS_V8 \
7766   N_("This coprocessor register access is deprecated in ARMv8")
7767
7768 /* Table of all deprecated coprocessor registers.  */
7769 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
7770 {
7771     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
7772      ARM_FEATURE (ARM_EXT_V8, 0), ARM_FEATURE (0, 0),
7773      DEPR_ACCESS_V8, NULL},
7774     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
7775      ARM_FEATURE (ARM_EXT_V8, 0), ARM_FEATURE (0, 0),
7776      DEPR_ACCESS_V8, NULL},
7777     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
7778      ARM_FEATURE (ARM_EXT_V8, 0), ARM_FEATURE (0, 0),
7779      DEPR_ACCESS_V8, NULL},
7780     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
7781      ARM_FEATURE (ARM_EXT_V8, 0), ARM_FEATURE (0, 0),
7782      DEPR_ACCESS_V8, NULL},
7783     {14, 6, 0,  0, 0,                                   /* TEECR.  */
7784      ARM_FEATURE (ARM_EXT_V8, 0), ARM_FEATURE (0, 0),
7785      DEPR_ACCESS_V8, NULL},
7786 };
7787
7788 #undef DEPR_ACCESS_V8
7789
7790 static const size_t deprecated_coproc_reg_count =
7791   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
7792
7793 static void
7794 do_co_reg (void)
7795 {
7796   unsigned Rd;
7797   size_t i;
7798
7799   Rd = inst.operands[2].reg;
7800   if (thumb_mode)
7801     {
7802       if (inst.instruction == 0xee000010
7803           || inst.instruction == 0xfe000010)
7804         /* MCR, MCR2  */
7805         reject_bad_reg (Rd);
7806       else
7807         /* MRC, MRC2  */
7808         constraint (Rd == REG_SP, BAD_SP);
7809     }
7810   else
7811     {
7812       /* MCR */
7813       if (inst.instruction == 0xe000010)
7814         constraint (Rd == REG_PC, BAD_PC);
7815     }
7816
7817     for (i = 0; i < deprecated_coproc_reg_count; ++i)
7818       {
7819         const struct deprecated_coproc_regs_s *r =
7820           deprecated_coproc_regs + i;
7821
7822         if (inst.operands[0].reg == r->cp
7823             && inst.operands[1].imm == r->opc1
7824             && inst.operands[3].reg == r->crn
7825             && inst.operands[4].reg == r->crm
7826             && inst.operands[5].imm == r->opc2)
7827           {
7828             if (!check_obsolete (&r->obsoleted, r->obs_msg)
7829                 && warn_on_deprecated
7830                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
7831               as_warn ("%s", r->dep_msg);
7832           }
7833       }
7834
7835   inst.instruction |= inst.operands[0].reg << 8;
7836   inst.instruction |= inst.operands[1].imm << 21;
7837   inst.instruction |= Rd << 12;
7838   inst.instruction |= inst.operands[3].reg << 16;
7839   inst.instruction |= inst.operands[4].reg;
7840   inst.instruction |= inst.operands[5].imm << 5;
7841 }
7842
7843 /* Transfer between coprocessor register and pair of ARM registers.
7844    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
7845    MCRR2
7846    MRRC{cond}
7847    MRRC2
7848
7849    Two XScale instructions are special cases of these:
7850
7851      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
7852      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
7853
7854    Result unpredictable if Rd or Rn is R15.  */
7855
7856 static void
7857 do_co_reg2c (void)
7858 {
7859   unsigned Rd, Rn;
7860
7861   Rd = inst.operands[2].reg;
7862   Rn = inst.operands[3].reg;
7863
7864   if (thumb_mode)
7865     {
7866       reject_bad_reg (Rd);
7867       reject_bad_reg (Rn);
7868     }
7869   else
7870     {
7871       constraint (Rd == REG_PC, BAD_PC);
7872       constraint (Rn == REG_PC, BAD_PC);
7873     }
7874
7875   inst.instruction |= inst.operands[0].reg << 8;
7876   inst.instruction |= inst.operands[1].imm << 4;
7877   inst.instruction |= Rd << 12;
7878   inst.instruction |= Rn << 16;
7879   inst.instruction |= inst.operands[4].reg;
7880 }
7881
7882 static void
7883 do_cpsi (void)
7884 {
7885   inst.instruction |= inst.operands[0].imm << 6;
7886   if (inst.operands[1].present)
7887     {
7888       inst.instruction |= CPSI_MMOD;
7889       inst.instruction |= inst.operands[1].imm;
7890     }
7891 }
7892
7893 static void
7894 do_dbg (void)
7895 {
7896   inst.instruction |= inst.operands[0].imm;
7897 }
7898
7899 static void
7900 do_div (void)
7901 {
7902   unsigned Rd, Rn, Rm;
7903
7904   Rd = inst.operands[0].reg;
7905   Rn = (inst.operands[1].present
7906         ? inst.operands[1].reg : Rd);
7907   Rm = inst.operands[2].reg;
7908
7909   constraint ((Rd == REG_PC), BAD_PC);
7910   constraint ((Rn == REG_PC), BAD_PC);
7911   constraint ((Rm == REG_PC), BAD_PC);
7912
7913   inst.instruction |= Rd << 16;
7914   inst.instruction |= Rn << 0;
7915   inst.instruction |= Rm << 8;
7916 }
7917
7918 static void
7919 do_it (void)
7920 {
7921   /* There is no IT instruction in ARM mode.  We
7922      process it to do the validation as if in
7923      thumb mode, just in case the code gets
7924      assembled for thumb using the unified syntax.  */
7925
7926   inst.size = 0;
7927   if (unified_syntax)
7928     {
7929       set_it_insn_type (IT_INSN);
7930       now_it.mask = (inst.instruction & 0xf) | 0x10;
7931       now_it.cc = inst.operands[0].imm;
7932     }
7933 }
7934
7935 /* If there is only one register in the register list,
7936    then return its register number.  Otherwise return -1.  */
7937 static int
7938 only_one_reg_in_list (int range)
7939 {
7940   int i = ffs (range) - 1;
7941   return (i > 15 || range != (1 << i)) ? -1 : i;
7942 }
7943
7944 static void
7945 encode_ldmstm(int from_push_pop_mnem)
7946 {
7947   int base_reg = inst.operands[0].reg;
7948   int range = inst.operands[1].imm;
7949   int one_reg;
7950
7951   inst.instruction |= base_reg << 16;
7952   inst.instruction |= range;
7953
7954   if (inst.operands[1].writeback)
7955     inst.instruction |= LDM_TYPE_2_OR_3;
7956
7957   if (inst.operands[0].writeback)
7958     {
7959       inst.instruction |= WRITE_BACK;
7960       /* Check for unpredictable uses of writeback.  */
7961       if (inst.instruction & LOAD_BIT)
7962         {
7963           /* Not allowed in LDM type 2.  */
7964           if ((inst.instruction & LDM_TYPE_2_OR_3)
7965               && ((range & (1 << REG_PC)) == 0))
7966             as_warn (_("writeback of base register is UNPREDICTABLE"));
7967           /* Only allowed if base reg not in list for other types.  */
7968           else if (range & (1 << base_reg))
7969             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
7970         }
7971       else /* STM.  */
7972         {
7973           /* Not allowed for type 2.  */
7974           if (inst.instruction & LDM_TYPE_2_OR_3)
7975             as_warn (_("writeback of base register is UNPREDICTABLE"));
7976           /* Only allowed if base reg not in list, or first in list.  */
7977           else if ((range & (1 << base_reg))
7978                    && (range & ((1 << base_reg) - 1)))
7979             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
7980         }
7981     }
7982
7983   /* If PUSH/POP has only one register, then use the A2 encoding.  */
7984   one_reg = only_one_reg_in_list (range);
7985   if (from_push_pop_mnem && one_reg >= 0)
7986     {
7987       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
7988
7989       inst.instruction &= A_COND_MASK;
7990       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
7991       inst.instruction |= one_reg << 12;
7992     }
7993 }
7994
7995 static void
7996 do_ldmstm (void)
7997 {
7998   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
7999 }
8000
8001 /* ARMv5TE load-consecutive (argument parse)
8002    Mode is like LDRH.
8003
8004      LDRccD R, mode
8005      STRccD R, mode.  */
8006
8007 static void
8008 do_ldrd (void)
8009 {
8010   constraint (inst.operands[0].reg % 2 != 0,
8011               _("first transfer register must be even"));
8012   constraint (inst.operands[1].present
8013               && inst.operands[1].reg != inst.operands[0].reg + 1,
8014               _("can only transfer two consecutive registers"));
8015   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8016   constraint (!inst.operands[2].isreg, _("'[' expected"));
8017
8018   if (!inst.operands[1].present)
8019     inst.operands[1].reg = inst.operands[0].reg + 1;
8020
8021   /* encode_arm_addr_mode_3 will diagnose overlap between the base
8022      register and the first register written; we have to diagnose
8023      overlap between the base and the second register written here.  */
8024
8025   if (inst.operands[2].reg == inst.operands[1].reg
8026       && (inst.operands[2].writeback || inst.operands[2].postind))
8027     as_warn (_("base register written back, and overlaps "
8028                "second transfer register"));
8029
8030   if (!(inst.instruction & V4_STR_BIT))
8031     {
8032       /* For an index-register load, the index register must not overlap the
8033         destination (even if not write-back).  */
8034       if (inst.operands[2].immisreg
8035               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
8036               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
8037         as_warn (_("index register overlaps transfer register"));
8038     }
8039   inst.instruction |= inst.operands[0].reg << 12;
8040   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
8041 }
8042
8043 static void
8044 do_ldrex (void)
8045 {
8046   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
8047               || inst.operands[1].postind || inst.operands[1].writeback
8048               || inst.operands[1].immisreg || inst.operands[1].shifted
8049               || inst.operands[1].negative
8050               /* This can arise if the programmer has written
8051                    strex rN, rM, foo
8052                  or if they have mistakenly used a register name as the last
8053                  operand,  eg:
8054                    strex rN, rM, rX
8055                  It is very difficult to distinguish between these two cases
8056                  because "rX" might actually be a label. ie the register
8057                  name has been occluded by a symbol of the same name. So we
8058                  just generate a general 'bad addressing mode' type error
8059                  message and leave it up to the programmer to discover the
8060                  true cause and fix their mistake.  */
8061               || (inst.operands[1].reg == REG_PC),
8062               BAD_ADDR_MODE);
8063
8064   constraint (inst.reloc.exp.X_op != O_constant
8065               || inst.reloc.exp.X_add_number != 0,
8066               _("offset must be zero in ARM encoding"));
8067
8068   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
8069
8070   inst.instruction |= inst.operands[0].reg << 12;
8071   inst.instruction |= inst.operands[1].reg << 16;
8072   inst.reloc.type = BFD_RELOC_UNUSED;
8073 }
8074
8075 static void
8076 do_ldrexd (void)
8077 {
8078   constraint (inst.operands[0].reg % 2 != 0,
8079               _("even register required"));
8080   constraint (inst.operands[1].present
8081               && inst.operands[1].reg != inst.operands[0].reg + 1,
8082               _("can only load two consecutive registers"));
8083   /* If op 1 were present and equal to PC, this function wouldn't
8084      have been called in the first place.  */
8085   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8086
8087   inst.instruction |= inst.operands[0].reg << 12;
8088   inst.instruction |= inst.operands[2].reg << 16;
8089 }
8090
8091 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
8092    which is not a multiple of four is UNPREDICTABLE.  */
8093 static void
8094 check_ldr_r15_aligned (void)
8095 {
8096   constraint (!(inst.operands[1].immisreg)
8097               && (inst.operands[0].reg == REG_PC
8098               && inst.operands[1].reg == REG_PC
8099               && (inst.reloc.exp.X_add_number & 0x3)),
8100               _("ldr to register 15 must be 4-byte alligned"));
8101 }
8102
8103 static void
8104 do_ldst (void)
8105 {
8106   inst.instruction |= inst.operands[0].reg << 12;
8107   if (!inst.operands[1].isreg)
8108     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/FALSE))
8109       return;
8110   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
8111   check_ldr_r15_aligned ();
8112 }
8113
8114 static void
8115 do_ldstt (void)
8116 {
8117   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8118      reject [Rn,...].  */
8119   if (inst.operands[1].preind)
8120     {
8121       constraint (inst.reloc.exp.X_op != O_constant
8122                   || inst.reloc.exp.X_add_number != 0,
8123                   _("this instruction requires a post-indexed address"));
8124
8125       inst.operands[1].preind = 0;
8126       inst.operands[1].postind = 1;
8127       inst.operands[1].writeback = 1;
8128     }
8129   inst.instruction |= inst.operands[0].reg << 12;
8130   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
8131 }
8132
8133 /* Halfword and signed-byte load/store operations.  */
8134
8135 static void
8136 do_ldstv4 (void)
8137 {
8138   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8139   inst.instruction |= inst.operands[0].reg << 12;
8140   if (!inst.operands[1].isreg)
8141     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/TRUE))
8142       return;
8143   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
8144 }
8145
8146 static void
8147 do_ldsttv4 (void)
8148 {
8149   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8150      reject [Rn,...].  */
8151   if (inst.operands[1].preind)
8152     {
8153       constraint (inst.reloc.exp.X_op != O_constant
8154                   || inst.reloc.exp.X_add_number != 0,
8155                   _("this instruction requires a post-indexed address"));
8156
8157       inst.operands[1].preind = 0;
8158       inst.operands[1].postind = 1;
8159       inst.operands[1].writeback = 1;
8160     }
8161   inst.instruction |= inst.operands[0].reg << 12;
8162   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
8163 }
8164
8165 /* Co-processor register load/store.
8166    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
8167 static void
8168 do_lstc (void)
8169 {
8170   inst.instruction |= inst.operands[0].reg << 8;
8171   inst.instruction |= inst.operands[1].reg << 12;
8172   encode_arm_cp_address (2, TRUE, TRUE, 0);
8173 }
8174
8175 static void
8176 do_mlas (void)
8177 {
8178   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
8179   if (inst.operands[0].reg == inst.operands[1].reg
8180       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
8181       && !(inst.instruction & 0x00400000))
8182     as_tsktsk (_("Rd and Rm should be different in mla"));
8183
8184   inst.instruction |= inst.operands[0].reg << 16;
8185   inst.instruction |= inst.operands[1].reg;
8186   inst.instruction |= inst.operands[2].reg << 8;
8187   inst.instruction |= inst.operands[3].reg << 12;
8188 }
8189
8190 static void
8191 do_mov (void)
8192 {
8193   inst.instruction |= inst.operands[0].reg << 12;
8194   encode_arm_shifter_operand (1);
8195 }
8196
8197 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
8198 static void
8199 do_mov16 (void)
8200 {
8201   bfd_vma imm;
8202   bfd_boolean top;
8203
8204   top = (inst.instruction & 0x00400000) != 0;
8205   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
8206               _(":lower16: not allowed this instruction"));
8207   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
8208               _(":upper16: not allowed instruction"));
8209   inst.instruction |= inst.operands[0].reg << 12;
8210   if (inst.reloc.type == BFD_RELOC_UNUSED)
8211     {
8212       imm = inst.reloc.exp.X_add_number;
8213       /* The value is in two pieces: 0:11, 16:19.  */
8214       inst.instruction |= (imm & 0x00000fff);
8215       inst.instruction |= (imm & 0x0000f000) << 4;
8216     }
8217 }
8218
8219 static void do_vfp_nsyn_opcode (const char *);
8220
8221 static int
8222 do_vfp_nsyn_mrs (void)
8223 {
8224   if (inst.operands[0].isvec)
8225     {
8226       if (inst.operands[1].reg != 1)
8227         first_error (_("operand 1 must be FPSCR"));
8228       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
8229       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
8230       do_vfp_nsyn_opcode ("fmstat");
8231     }
8232   else if (inst.operands[1].isvec)
8233     do_vfp_nsyn_opcode ("fmrx");
8234   else
8235     return FAIL;
8236
8237   return SUCCESS;
8238 }
8239
8240 static int
8241 do_vfp_nsyn_msr (void)
8242 {
8243   if (inst.operands[0].isvec)
8244     do_vfp_nsyn_opcode ("fmxr");
8245   else
8246     return FAIL;
8247
8248   return SUCCESS;
8249 }
8250
8251 static void
8252 do_vmrs (void)
8253 {
8254   unsigned Rt = inst.operands[0].reg;
8255
8256   if (thumb_mode && inst.operands[0].reg == REG_SP)
8257     {
8258       inst.error = BAD_SP;
8259       return;
8260     }
8261
8262   /* APSR_ sets isvec. All other refs to PC are illegal.  */
8263   if (!inst.operands[0].isvec && inst.operands[0].reg == REG_PC)
8264     {
8265       inst.error = BAD_PC;
8266       return;
8267     }
8268
8269   switch (inst.operands[1].reg)
8270     {
8271     case 0: /* FPSID */
8272     case 1: /* FPSCR */
8273     case 6: /* MVFR1 */
8274     case 7: /* MVFR0 */
8275     case 8: /* FPEXC */
8276       inst.instruction |= (inst.operands[1].reg << 16);
8277       break;
8278     default:
8279       first_error (_("operand 1 must be a VFP extension System Register"));
8280     }
8281
8282   inst.instruction |= (Rt << 12);
8283 }
8284
8285 static void
8286 do_vmsr (void)
8287 {
8288   unsigned Rt = inst.operands[1].reg;
8289
8290   if (thumb_mode)
8291     reject_bad_reg (Rt);
8292   else if (Rt == REG_PC)
8293     {
8294       inst.error = BAD_PC;
8295       return;
8296     }
8297
8298   switch (inst.operands[0].reg)
8299     {
8300     case 0: /* FPSID  */
8301     case 1: /* FPSCR  */
8302     case 8: /* FPEXC */
8303       inst.instruction |= (inst.operands[0].reg << 16);
8304       break;
8305     default:
8306       first_error (_("operand 0 must be FPSID or FPSCR pr FPEXC"));
8307     }
8308
8309   inst.instruction |= (Rt << 12);
8310 }
8311
8312 static void
8313 do_mrs (void)
8314 {
8315   unsigned br;
8316
8317   if (do_vfp_nsyn_mrs () == SUCCESS)
8318     return;
8319
8320   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8321   inst.instruction |= inst.operands[0].reg << 12;
8322
8323   if (inst.operands[1].isreg)
8324     {
8325       br = inst.operands[1].reg;
8326       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf000))
8327         as_bad (_("bad register for mrs"));
8328     }
8329   else
8330     {
8331       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
8332       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
8333                   != (PSR_c|PSR_f),
8334                   _("'APSR', 'CPSR' or 'SPSR' expected"));
8335       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
8336     }
8337
8338   inst.instruction |= br;
8339 }
8340
8341 /* Two possible forms:
8342       "{C|S}PSR_<field>, Rm",
8343       "{C|S}PSR_f, #expression".  */
8344
8345 static void
8346 do_msr (void)
8347 {
8348   if (do_vfp_nsyn_msr () == SUCCESS)
8349     return;
8350
8351   inst.instruction |= inst.operands[0].imm;
8352   if (inst.operands[1].isreg)
8353     inst.instruction |= inst.operands[1].reg;
8354   else
8355     {
8356       inst.instruction |= INST_IMMEDIATE;
8357       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8358       inst.reloc.pc_rel = 0;
8359     }
8360 }
8361
8362 static void
8363 do_mul (void)
8364 {
8365   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
8366
8367   if (!inst.operands[2].present)
8368     inst.operands[2].reg = inst.operands[0].reg;
8369   inst.instruction |= inst.operands[0].reg << 16;
8370   inst.instruction |= inst.operands[1].reg;
8371   inst.instruction |= inst.operands[2].reg << 8;
8372
8373   if (inst.operands[0].reg == inst.operands[1].reg
8374       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8375     as_tsktsk (_("Rd and Rm should be different in mul"));
8376 }
8377
8378 /* Long Multiply Parser
8379    UMULL RdLo, RdHi, Rm, Rs
8380    SMULL RdLo, RdHi, Rm, Rs
8381    UMLAL RdLo, RdHi, Rm, Rs
8382    SMLAL RdLo, RdHi, Rm, Rs.  */
8383
8384 static void
8385 do_mull (void)
8386 {
8387   inst.instruction |= inst.operands[0].reg << 12;
8388   inst.instruction |= inst.operands[1].reg << 16;
8389   inst.instruction |= inst.operands[2].reg;
8390   inst.instruction |= inst.operands[3].reg << 8;
8391
8392   /* rdhi and rdlo must be different.  */
8393   if (inst.operands[0].reg == inst.operands[1].reg)
8394     as_tsktsk (_("rdhi and rdlo must be different"));
8395
8396   /* rdhi, rdlo and rm must all be different before armv6.  */
8397   if ((inst.operands[0].reg == inst.operands[2].reg
8398       || inst.operands[1].reg == inst.operands[2].reg)
8399       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8400     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
8401 }
8402
8403 static void
8404 do_nop (void)
8405 {
8406   if (inst.operands[0].present
8407       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
8408     {
8409       /* Architectural NOP hints are CPSR sets with no bits selected.  */
8410       inst.instruction &= 0xf0000000;
8411       inst.instruction |= 0x0320f000;
8412       if (inst.operands[0].present)
8413         inst.instruction |= inst.operands[0].imm;
8414     }
8415 }
8416
8417 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
8418    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
8419    Condition defaults to COND_ALWAYS.
8420    Error if Rd, Rn or Rm are R15.  */
8421
8422 static void
8423 do_pkhbt (void)
8424 {
8425   inst.instruction |= inst.operands[0].reg << 12;
8426   inst.instruction |= inst.operands[1].reg << 16;
8427   inst.instruction |= inst.operands[2].reg;
8428   if (inst.operands[3].present)
8429     encode_arm_shift (3);
8430 }
8431
8432 /* ARM V6 PKHTB (Argument Parse).  */
8433
8434 static void
8435 do_pkhtb (void)
8436 {
8437   if (!inst.operands[3].present)
8438     {
8439       /* If the shift specifier is omitted, turn the instruction
8440          into pkhbt rd, rm, rn. */
8441       inst.instruction &= 0xfff00010;
8442       inst.instruction |= inst.operands[0].reg << 12;
8443       inst.instruction |= inst.operands[1].reg;
8444       inst.instruction |= inst.operands[2].reg << 16;
8445     }
8446   else
8447     {
8448       inst.instruction |= inst.operands[0].reg << 12;
8449       inst.instruction |= inst.operands[1].reg << 16;
8450       inst.instruction |= inst.operands[2].reg;
8451       encode_arm_shift (3);
8452     }
8453 }
8454
8455 /* ARMv5TE: Preload-Cache
8456    MP Extensions: Preload for write
8457
8458     PLD(W) <addr_mode>
8459
8460   Syntactically, like LDR with B=1, W=0, L=1.  */
8461
8462 static void
8463 do_pld (void)
8464 {
8465   constraint (!inst.operands[0].isreg,
8466               _("'[' expected after PLD mnemonic"));
8467   constraint (inst.operands[0].postind,
8468               _("post-indexed expression used in preload instruction"));
8469   constraint (inst.operands[0].writeback,
8470               _("writeback used in preload instruction"));
8471   constraint (!inst.operands[0].preind,
8472               _("unindexed addressing used in preload instruction"));
8473   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
8474 }
8475
8476 /* ARMv7: PLI <addr_mode>  */
8477 static void
8478 do_pli (void)
8479 {
8480   constraint (!inst.operands[0].isreg,
8481               _("'[' expected after PLI mnemonic"));
8482   constraint (inst.operands[0].postind,
8483               _("post-indexed expression used in preload instruction"));
8484   constraint (inst.operands[0].writeback,
8485               _("writeback used in preload instruction"));
8486   constraint (!inst.operands[0].preind,
8487               _("unindexed addressing used in preload instruction"));
8488   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
8489   inst.instruction &= ~PRE_INDEX;
8490 }
8491
8492 static void
8493 do_push_pop (void)
8494 {
8495   inst.operands[1] = inst.operands[0];
8496   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
8497   inst.operands[0].isreg = 1;
8498   inst.operands[0].writeback = 1;
8499   inst.operands[0].reg = REG_SP;
8500   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
8501 }
8502
8503 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
8504    word at the specified address and the following word
8505    respectively.
8506    Unconditionally executed.
8507    Error if Rn is R15.  */
8508
8509 static void
8510 do_rfe (void)
8511 {
8512   inst.instruction |= inst.operands[0].reg << 16;
8513   if (inst.operands[0].writeback)
8514     inst.instruction |= WRITE_BACK;
8515 }
8516
8517 /* ARM V6 ssat (argument parse).  */
8518
8519 static void
8520 do_ssat (void)
8521 {
8522   inst.instruction |= inst.operands[0].reg << 12;
8523   inst.instruction |= (inst.operands[1].imm - 1) << 16;
8524   inst.instruction |= inst.operands[2].reg;
8525
8526   if (inst.operands[3].present)
8527     encode_arm_shift (3);
8528 }
8529
8530 /* ARM V6 usat (argument parse).  */
8531
8532 static void
8533 do_usat (void)
8534 {
8535   inst.instruction |= inst.operands[0].reg << 12;
8536   inst.instruction |= inst.operands[1].imm << 16;
8537   inst.instruction |= inst.operands[2].reg;
8538
8539   if (inst.operands[3].present)
8540     encode_arm_shift (3);
8541 }
8542
8543 /* ARM V6 ssat16 (argument parse).  */
8544
8545 static void
8546 do_ssat16 (void)
8547 {
8548   inst.instruction |= inst.operands[0].reg << 12;
8549   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
8550   inst.instruction |= inst.operands[2].reg;
8551 }
8552
8553 static void
8554 do_usat16 (void)
8555 {
8556   inst.instruction |= inst.operands[0].reg << 12;
8557   inst.instruction |= inst.operands[1].imm << 16;
8558   inst.instruction |= inst.operands[2].reg;
8559 }
8560
8561 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
8562    preserving the other bits.
8563
8564    setend <endian_specifier>, where <endian_specifier> is either
8565    BE or LE.  */
8566
8567 static void
8568 do_setend (void)
8569 {
8570   if (warn_on_deprecated
8571       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
8572       as_warn (_("setend use is deprecated for ARMv8"));
8573
8574   if (inst.operands[0].imm)
8575     inst.instruction |= 0x200;
8576 }
8577
8578 static void
8579 do_shift (void)
8580 {
8581   unsigned int Rm = (inst.operands[1].present
8582                      ? inst.operands[1].reg
8583                      : inst.operands[0].reg);
8584
8585   inst.instruction |= inst.operands[0].reg << 12;
8586   inst.instruction |= Rm;
8587   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
8588     {
8589       inst.instruction |= inst.operands[2].reg << 8;
8590       inst.instruction |= SHIFT_BY_REG;
8591       /* PR 12854: Error on extraneous shifts.  */
8592       constraint (inst.operands[2].shifted,
8593                   _("extraneous shift as part of operand to shift insn"));
8594     }
8595   else
8596     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
8597 }
8598
8599 static void
8600 do_smc (void)
8601 {
8602   inst.reloc.type = BFD_RELOC_ARM_SMC;
8603   inst.reloc.pc_rel = 0;
8604 }
8605
8606 static void
8607 do_hvc (void)
8608 {
8609   inst.reloc.type = BFD_RELOC_ARM_HVC;
8610   inst.reloc.pc_rel = 0;
8611 }
8612
8613 static void
8614 do_swi (void)
8615 {
8616   inst.reloc.type = BFD_RELOC_ARM_SWI;
8617   inst.reloc.pc_rel = 0;
8618 }
8619
8620 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
8621    SMLAxy{cond} Rd,Rm,Rs,Rn
8622    SMLAWy{cond} Rd,Rm,Rs,Rn
8623    Error if any register is R15.  */
8624
8625 static void
8626 do_smla (void)
8627 {
8628   inst.instruction |= inst.operands[0].reg << 16;
8629   inst.instruction |= inst.operands[1].reg;
8630   inst.instruction |= inst.operands[2].reg << 8;
8631   inst.instruction |= inst.operands[3].reg << 12;
8632 }
8633
8634 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
8635    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
8636    Error if any register is R15.
8637    Warning if Rdlo == Rdhi.  */
8638
8639 static void
8640 do_smlal (void)
8641 {
8642   inst.instruction |= inst.operands[0].reg << 12;
8643   inst.instruction |= inst.operands[1].reg << 16;
8644   inst.instruction |= inst.operands[2].reg;
8645   inst.instruction |= inst.operands[3].reg << 8;
8646
8647   if (inst.operands[0].reg == inst.operands[1].reg)
8648     as_tsktsk (_("rdhi and rdlo must be different"));
8649 }
8650
8651 /* ARM V5E (El Segundo) signed-multiply (argument parse)
8652    SMULxy{cond} Rd,Rm,Rs
8653    Error if any register is R15.  */
8654
8655 static void
8656 do_smul (void)
8657 {
8658   inst.instruction |= inst.operands[0].reg << 16;
8659   inst.instruction |= inst.operands[1].reg;
8660   inst.instruction |= inst.operands[2].reg << 8;
8661 }
8662
8663 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
8664    the same for both ARM and Thumb-2.  */
8665
8666 static void
8667 do_srs (void)
8668 {
8669   int reg;
8670
8671   if (inst.operands[0].present)
8672     {
8673       reg = inst.operands[0].reg;
8674       constraint (reg != REG_SP, _("SRS base register must be r13"));
8675     }
8676   else
8677     reg = REG_SP;
8678
8679   inst.instruction |= reg << 16;
8680   inst.instruction |= inst.operands[1].imm;
8681   if (inst.operands[0].writeback || inst.operands[1].writeback)
8682     inst.instruction |= WRITE_BACK;
8683 }
8684
8685 /* ARM V6 strex (argument parse).  */
8686
8687 static void
8688 do_strex (void)
8689 {
8690   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
8691               || inst.operands[2].postind || inst.operands[2].writeback
8692               || inst.operands[2].immisreg || inst.operands[2].shifted
8693               || inst.operands[2].negative
8694               /* See comment in do_ldrex().  */
8695               || (inst.operands[2].reg == REG_PC),
8696               BAD_ADDR_MODE);
8697
8698   constraint (inst.operands[0].reg == inst.operands[1].reg
8699               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
8700
8701   constraint (inst.reloc.exp.X_op != O_constant
8702               || inst.reloc.exp.X_add_number != 0,
8703               _("offset must be zero in ARM encoding"));
8704
8705   inst.instruction |= inst.operands[0].reg << 12;
8706   inst.instruction |= inst.operands[1].reg;
8707   inst.instruction |= inst.operands[2].reg << 16;
8708   inst.reloc.type = BFD_RELOC_UNUSED;
8709 }
8710
8711 static void
8712 do_t_strexbh (void)
8713 {
8714   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
8715               || inst.operands[2].postind || inst.operands[2].writeback
8716               || inst.operands[2].immisreg || inst.operands[2].shifted
8717               || inst.operands[2].negative,
8718               BAD_ADDR_MODE);
8719
8720   constraint (inst.operands[0].reg == inst.operands[1].reg
8721               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
8722
8723   do_rm_rd_rn ();
8724 }
8725
8726 static void
8727 do_strexd (void)
8728 {
8729   constraint (inst.operands[1].reg % 2 != 0,
8730               _("even register required"));
8731   constraint (inst.operands[2].present
8732               && inst.operands[2].reg != inst.operands[1].reg + 1,
8733               _("can only store two consecutive registers"));
8734   /* If op 2 were present and equal to PC, this function wouldn't
8735      have been called in the first place.  */
8736   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
8737
8738   constraint (inst.operands[0].reg == inst.operands[1].reg
8739               || inst.operands[0].reg == inst.operands[1].reg + 1
8740               || inst.operands[0].reg == inst.operands[3].reg,
8741               BAD_OVERLAP);
8742
8743   inst.instruction |= inst.operands[0].reg << 12;
8744   inst.instruction |= inst.operands[1].reg;
8745   inst.instruction |= inst.operands[3].reg << 16;
8746 }
8747
8748 /* ARM V8 STRL.  */
8749 static void
8750 do_stlex (void)
8751 {
8752   constraint (inst.operands[0].reg == inst.operands[1].reg
8753               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
8754
8755   do_rd_rm_rn ();
8756 }
8757
8758 static void
8759 do_t_stlex (void)
8760 {
8761   constraint (inst.operands[0].reg == inst.operands[1].reg
8762               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
8763
8764   do_rm_rd_rn ();
8765 }
8766
8767 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
8768    extends it to 32-bits, and adds the result to a value in another
8769    register.  You can specify a rotation by 0, 8, 16, or 24 bits
8770    before extracting the 16-bit value.
8771    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
8772    Condition defaults to COND_ALWAYS.
8773    Error if any register uses R15.  */
8774
8775 static void
8776 do_sxtah (void)
8777 {
8778   inst.instruction |= inst.operands[0].reg << 12;
8779   inst.instruction |= inst.operands[1].reg << 16;
8780   inst.instruction |= inst.operands[2].reg;
8781   inst.instruction |= inst.operands[3].imm << 10;
8782 }
8783
8784 /* ARM V6 SXTH.
8785
8786    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
8787    Condition defaults to COND_ALWAYS.
8788    Error if any register uses R15.  */
8789
8790 static void
8791 do_sxth (void)
8792 {
8793   inst.instruction |= inst.operands[0].reg << 12;
8794   inst.instruction |= inst.operands[1].reg;
8795   inst.instruction |= inst.operands[2].imm << 10;
8796 }
8797 \f
8798 /* VFP instructions.  In a logical order: SP variant first, monad
8799    before dyad, arithmetic then move then load/store.  */
8800
8801 static void
8802 do_vfp_sp_monadic (void)
8803 {
8804   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8805   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
8806 }
8807
8808 static void
8809 do_vfp_sp_dyadic (void)
8810 {
8811   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8812   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
8813   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
8814 }
8815
8816 static void
8817 do_vfp_sp_compare_z (void)
8818 {
8819   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8820 }
8821
8822 static void
8823 do_vfp_dp_sp_cvt (void)
8824 {
8825   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8826   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
8827 }
8828
8829 static void
8830 do_vfp_sp_dp_cvt (void)
8831 {
8832   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8833   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
8834 }
8835
8836 static void
8837 do_vfp_reg_from_sp (void)
8838 {
8839   inst.instruction |= inst.operands[0].reg << 12;
8840   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
8841 }
8842
8843 static void
8844 do_vfp_reg2_from_sp2 (void)
8845 {
8846   constraint (inst.operands[2].imm != 2,
8847               _("only two consecutive VFP SP registers allowed here"));
8848   inst.instruction |= inst.operands[0].reg << 12;
8849   inst.instruction |= inst.operands[1].reg << 16;
8850   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
8851 }
8852
8853 static void
8854 do_vfp_sp_from_reg (void)
8855 {
8856   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
8857   inst.instruction |= inst.operands[1].reg << 12;
8858 }
8859
8860 static void
8861 do_vfp_sp2_from_reg2 (void)
8862 {
8863   constraint (inst.operands[0].imm != 2,
8864               _("only two consecutive VFP SP registers allowed here"));
8865   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
8866   inst.instruction |= inst.operands[1].reg << 12;
8867   inst.instruction |= inst.operands[2].reg << 16;
8868 }
8869
8870 static void
8871 do_vfp_sp_ldst (void)
8872 {
8873   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8874   encode_arm_cp_address (1, FALSE, TRUE, 0);
8875 }
8876
8877 static void
8878 do_vfp_dp_ldst (void)
8879 {
8880   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8881   encode_arm_cp_address (1, FALSE, TRUE, 0);
8882 }
8883
8884
8885 static void
8886 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
8887 {
8888   if (inst.operands[0].writeback)
8889     inst.instruction |= WRITE_BACK;
8890   else
8891     constraint (ldstm_type != VFP_LDSTMIA,
8892                 _("this addressing mode requires base-register writeback"));
8893   inst.instruction |= inst.operands[0].reg << 16;
8894   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
8895   inst.instruction |= inst.operands[1].imm;
8896 }
8897
8898 static void
8899 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
8900 {
8901   int count;
8902
8903   if (inst.operands[0].writeback)
8904     inst.instruction |= WRITE_BACK;
8905   else
8906     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
8907                 _("this addressing mode requires base-register writeback"));
8908
8909   inst.instruction |= inst.operands[0].reg << 16;
8910   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8911
8912   count = inst.operands[1].imm << 1;
8913   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
8914     count += 1;
8915
8916   inst.instruction |= count;
8917 }
8918
8919 static void
8920 do_vfp_sp_ldstmia (void)
8921 {
8922   vfp_sp_ldstm (VFP_LDSTMIA);
8923 }
8924
8925 static void
8926 do_vfp_sp_ldstmdb (void)
8927 {
8928   vfp_sp_ldstm (VFP_LDSTMDB);
8929 }
8930
8931 static void
8932 do_vfp_dp_ldstmia (void)
8933 {
8934   vfp_dp_ldstm (VFP_LDSTMIA);
8935 }
8936
8937 static void
8938 do_vfp_dp_ldstmdb (void)
8939 {
8940   vfp_dp_ldstm (VFP_LDSTMDB);
8941 }
8942
8943 static void
8944 do_vfp_xp_ldstmia (void)
8945 {
8946   vfp_dp_ldstm (VFP_LDSTMIAX);
8947 }
8948
8949 static void
8950 do_vfp_xp_ldstmdb (void)
8951 {
8952   vfp_dp_ldstm (VFP_LDSTMDBX);
8953 }
8954
8955 static void
8956 do_vfp_dp_rd_rm (void)
8957 {
8958   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8959   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
8960 }
8961
8962 static void
8963 do_vfp_dp_rn_rd (void)
8964 {
8965   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
8966   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8967 }
8968
8969 static void
8970 do_vfp_dp_rd_rn (void)
8971 {
8972   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8973   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
8974 }
8975
8976 static void
8977 do_vfp_dp_rd_rn_rm (void)
8978 {
8979   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8980   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
8981   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
8982 }
8983
8984 static void
8985 do_vfp_dp_rd (void)
8986 {
8987   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8988 }
8989
8990 static void
8991 do_vfp_dp_rm_rd_rn (void)
8992 {
8993   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
8994   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8995   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
8996 }
8997
8998 /* VFPv3 instructions.  */
8999 static void
9000 do_vfp_sp_const (void)
9001 {
9002   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9003   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9004   inst.instruction |= (inst.operands[1].imm & 0x0f);
9005 }
9006
9007 static void
9008 do_vfp_dp_const (void)
9009 {
9010   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9011   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9012   inst.instruction |= (inst.operands[1].imm & 0x0f);
9013 }
9014
9015 static void
9016 vfp_conv (int srcsize)
9017 {
9018   int immbits = srcsize - inst.operands[1].imm;
9019
9020   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
9021     {
9022       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
9023          i.e. immbits must be in range 0 - 16.  */
9024       inst.error = _("immediate value out of range, expected range [0, 16]");
9025       return;
9026     }
9027   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
9028     {
9029       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
9030          i.e. immbits must be in range 0 - 31.  */
9031       inst.error = _("immediate value out of range, expected range [1, 32]");
9032       return;
9033     }
9034
9035   inst.instruction |= (immbits & 1) << 5;
9036   inst.instruction |= (immbits >> 1);
9037 }
9038
9039 static void
9040 do_vfp_sp_conv_16 (void)
9041 {
9042   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9043   vfp_conv (16);
9044 }
9045
9046 static void
9047 do_vfp_dp_conv_16 (void)
9048 {
9049   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9050   vfp_conv (16);
9051 }
9052
9053 static void
9054 do_vfp_sp_conv_32 (void)
9055 {
9056   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9057   vfp_conv (32);
9058 }
9059
9060 static void
9061 do_vfp_dp_conv_32 (void)
9062 {
9063   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9064   vfp_conv (32);
9065 }
9066 \f
9067 /* FPA instructions.  Also in a logical order.  */
9068
9069 static void
9070 do_fpa_cmp (void)
9071 {
9072   inst.instruction |= inst.operands[0].reg << 16;
9073   inst.instruction |= inst.operands[1].reg;
9074 }
9075
9076 static void
9077 do_fpa_ldmstm (void)
9078 {
9079   inst.instruction |= inst.operands[0].reg << 12;
9080   switch (inst.operands[1].imm)
9081     {
9082     case 1: inst.instruction |= CP_T_X;          break;
9083     case 2: inst.instruction |= CP_T_Y;          break;
9084     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
9085     case 4:                                      break;
9086     default: abort ();
9087     }
9088
9089   if (inst.instruction & (PRE_INDEX | INDEX_UP))
9090     {
9091       /* The instruction specified "ea" or "fd", so we can only accept
9092          [Rn]{!}.  The instruction does not really support stacking or
9093          unstacking, so we have to emulate these by setting appropriate
9094          bits and offsets.  */
9095       constraint (inst.reloc.exp.X_op != O_constant
9096                   || inst.reloc.exp.X_add_number != 0,
9097                   _("this instruction does not support indexing"));
9098
9099       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
9100         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
9101
9102       if (!(inst.instruction & INDEX_UP))
9103         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
9104
9105       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
9106         {
9107           inst.operands[2].preind = 0;
9108           inst.operands[2].postind = 1;
9109         }
9110     }
9111
9112   encode_arm_cp_address (2, TRUE, TRUE, 0);
9113 }
9114 \f
9115 /* iWMMXt instructions: strictly in alphabetical order.  */
9116
9117 static void
9118 do_iwmmxt_tandorc (void)
9119 {
9120   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
9121 }
9122
9123 static void
9124 do_iwmmxt_textrc (void)
9125 {
9126   inst.instruction |= inst.operands[0].reg << 12;
9127   inst.instruction |= inst.operands[1].imm;
9128 }
9129
9130 static void
9131 do_iwmmxt_textrm (void)
9132 {
9133   inst.instruction |= inst.operands[0].reg << 12;
9134   inst.instruction |= inst.operands[1].reg << 16;
9135   inst.instruction |= inst.operands[2].imm;
9136 }
9137
9138 static void
9139 do_iwmmxt_tinsr (void)
9140 {
9141   inst.instruction |= inst.operands[0].reg << 16;
9142   inst.instruction |= inst.operands[1].reg << 12;
9143   inst.instruction |= inst.operands[2].imm;
9144 }
9145
9146 static void
9147 do_iwmmxt_tmia (void)
9148 {
9149   inst.instruction |= inst.operands[0].reg << 5;
9150   inst.instruction |= inst.operands[1].reg;
9151   inst.instruction |= inst.operands[2].reg << 12;
9152 }
9153
9154 static void
9155 do_iwmmxt_waligni (void)
9156 {
9157   inst.instruction |= inst.operands[0].reg << 12;
9158   inst.instruction |= inst.operands[1].reg << 16;
9159   inst.instruction |= inst.operands[2].reg;
9160   inst.instruction |= inst.operands[3].imm << 20;
9161 }
9162
9163 static void
9164 do_iwmmxt_wmerge (void)
9165 {
9166   inst.instruction |= inst.operands[0].reg << 12;
9167   inst.instruction |= inst.operands[1].reg << 16;
9168   inst.instruction |= inst.operands[2].reg;
9169   inst.instruction |= inst.operands[3].imm << 21;
9170 }
9171
9172 static void
9173 do_iwmmxt_wmov (void)
9174 {
9175   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
9176   inst.instruction |= inst.operands[0].reg << 12;
9177   inst.instruction |= inst.operands[1].reg << 16;
9178   inst.instruction |= inst.operands[1].reg;
9179 }
9180
9181 static void
9182 do_iwmmxt_wldstbh (void)
9183 {
9184   int reloc;
9185   inst.instruction |= inst.operands[0].reg << 12;
9186   if (thumb_mode)
9187     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
9188   else
9189     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
9190   encode_arm_cp_address (1, TRUE, FALSE, reloc);
9191 }
9192
9193 static void
9194 do_iwmmxt_wldstw (void)
9195 {
9196   /* RIWR_RIWC clears .isreg for a control register.  */
9197   if (!inst.operands[0].isreg)
9198     {
9199       constraint (inst.cond != COND_ALWAYS, BAD_COND);
9200       inst.instruction |= 0xf0000000;
9201     }
9202
9203   inst.instruction |= inst.operands[0].reg << 12;
9204   encode_arm_cp_address (1, TRUE, TRUE, 0);
9205 }
9206
9207 static void
9208 do_iwmmxt_wldstd (void)
9209 {
9210   inst.instruction |= inst.operands[0].reg << 12;
9211   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
9212       && inst.operands[1].immisreg)
9213     {
9214       inst.instruction &= ~0x1a000ff;
9215       inst.instruction |= (0xf << 28);
9216       if (inst.operands[1].preind)
9217         inst.instruction |= PRE_INDEX;
9218       if (!inst.operands[1].negative)
9219         inst.instruction |= INDEX_UP;
9220       if (inst.operands[1].writeback)
9221         inst.instruction |= WRITE_BACK;
9222       inst.instruction |= inst.operands[1].reg << 16;
9223       inst.instruction |= inst.reloc.exp.X_add_number << 4;
9224       inst.instruction |= inst.operands[1].imm;
9225     }
9226   else
9227     encode_arm_cp_address (1, TRUE, FALSE, 0);
9228 }
9229
9230 static void
9231 do_iwmmxt_wshufh (void)
9232 {
9233   inst.instruction |= inst.operands[0].reg << 12;
9234   inst.instruction |= inst.operands[1].reg << 16;
9235   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
9236   inst.instruction |= (inst.operands[2].imm & 0x0f);
9237 }
9238
9239 static void
9240 do_iwmmxt_wzero (void)
9241 {
9242   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
9243   inst.instruction |= inst.operands[0].reg;
9244   inst.instruction |= inst.operands[0].reg << 12;
9245   inst.instruction |= inst.operands[0].reg << 16;
9246 }
9247
9248 static void
9249 do_iwmmxt_wrwrwr_or_imm5 (void)
9250 {
9251   if (inst.operands[2].isreg)
9252     do_rd_rn_rm ();
9253   else {
9254     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
9255                 _("immediate operand requires iWMMXt2"));
9256     do_rd_rn ();
9257     if (inst.operands[2].imm == 0)
9258       {
9259         switch ((inst.instruction >> 20) & 0xf)
9260           {
9261           case 4:
9262           case 5:
9263           case 6:
9264           case 7:
9265             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
9266             inst.operands[2].imm = 16;
9267             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
9268             break;
9269           case 8:
9270           case 9:
9271           case 10:
9272           case 11:
9273             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
9274             inst.operands[2].imm = 32;
9275             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
9276             break;
9277           case 12:
9278           case 13:
9279           case 14:
9280           case 15:
9281             {
9282               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
9283               unsigned long wrn;
9284               wrn = (inst.instruction >> 16) & 0xf;
9285               inst.instruction &= 0xff0fff0f;
9286               inst.instruction |= wrn;
9287               /* Bail out here; the instruction is now assembled.  */
9288               return;
9289             }
9290           }
9291       }
9292     /* Map 32 -> 0, etc.  */
9293     inst.operands[2].imm &= 0x1f;
9294     inst.instruction |= (0xf << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
9295   }
9296 }
9297 \f
9298 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
9299    operations first, then control, shift, and load/store.  */
9300
9301 /* Insns like "foo X,Y,Z".  */
9302
9303 static void
9304 do_mav_triple (void)
9305 {
9306   inst.instruction |= inst.operands[0].reg << 16;
9307   inst.instruction |= inst.operands[1].reg;
9308   inst.instruction |= inst.operands[2].reg << 12;
9309 }
9310
9311 /* Insns like "foo W,X,Y,Z".
9312     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
9313
9314 static void
9315 do_mav_quad (void)
9316 {
9317   inst.instruction |= inst.operands[0].reg << 5;
9318   inst.instruction |= inst.operands[1].reg << 12;
9319   inst.instruction |= inst.operands[2].reg << 16;
9320   inst.instruction |= inst.operands[3].reg;
9321 }
9322
9323 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
9324 static void
9325 do_mav_dspsc (void)
9326 {
9327   inst.instruction |= inst.operands[1].reg << 12;
9328 }
9329
9330 /* Maverick shift immediate instructions.
9331    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
9332    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
9333
9334 static void
9335 do_mav_shift (void)
9336 {
9337   int imm = inst.operands[2].imm;
9338
9339   inst.instruction |= inst.operands[0].reg << 12;
9340   inst.instruction |= inst.operands[1].reg << 16;
9341
9342   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
9343      Bits 5-7 of the insn should have bits 4-6 of the immediate.
9344      Bit 4 should be 0.  */
9345   imm = (imm & 0xf) | ((imm & 0x70) << 1);
9346
9347   inst.instruction |= imm;
9348 }
9349 \f
9350 /* XScale instructions.  Also sorted arithmetic before move.  */
9351
9352 /* Xscale multiply-accumulate (argument parse)
9353      MIAcc   acc0,Rm,Rs
9354      MIAPHcc acc0,Rm,Rs
9355      MIAxycc acc0,Rm,Rs.  */
9356
9357 static void
9358 do_xsc_mia (void)
9359 {
9360   inst.instruction |= inst.operands[1].reg;
9361   inst.instruction |= inst.operands[2].reg << 12;
9362 }
9363
9364 /* Xscale move-accumulator-register (argument parse)
9365
9366      MARcc   acc0,RdLo,RdHi.  */
9367
9368 static void
9369 do_xsc_mar (void)
9370 {
9371   inst.instruction |= inst.operands[1].reg << 12;
9372   inst.instruction |= inst.operands[2].reg << 16;
9373 }
9374
9375 /* Xscale move-register-accumulator (argument parse)
9376
9377      MRAcc   RdLo,RdHi,acc0.  */
9378
9379 static void
9380 do_xsc_mra (void)
9381 {
9382   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
9383   inst.instruction |= inst.operands[0].reg << 12;
9384   inst.instruction |= inst.operands[1].reg << 16;
9385 }
9386 \f
9387 /* Encoding functions relevant only to Thumb.  */
9388
9389 /* inst.operands[i] is a shifted-register operand; encode
9390    it into inst.instruction in the format used by Thumb32.  */
9391
9392 static void
9393 encode_thumb32_shifted_operand (int i)
9394 {
9395   unsigned int value = inst.reloc.exp.X_add_number;
9396   unsigned int shift = inst.operands[i].shift_kind;
9397
9398   constraint (inst.operands[i].immisreg,
9399               _("shift by register not allowed in thumb mode"));
9400   inst.instruction |= inst.operands[i].reg;
9401   if (shift == SHIFT_RRX)
9402     inst.instruction |= SHIFT_ROR << 4;
9403   else
9404     {
9405       constraint (inst.reloc.exp.X_op != O_constant,
9406                   _("expression too complex"));
9407
9408       constraint (value > 32
9409                   || (value == 32 && (shift == SHIFT_LSL
9410                                       || shift == SHIFT_ROR)),
9411                   _("shift expression is too large"));
9412
9413       if (value == 0)
9414         shift = SHIFT_LSL;
9415       else if (value == 32)
9416         value = 0;
9417
9418       inst.instruction |= shift << 4;
9419       inst.instruction |= (value & 0x1c) << 10;
9420       inst.instruction |= (value & 0x03) << 6;
9421     }
9422 }
9423
9424
9425 /* inst.operands[i] was set up by parse_address.  Encode it into a
9426    Thumb32 format load or store instruction.  Reject forms that cannot
9427    be used with such instructions.  If is_t is true, reject forms that
9428    cannot be used with a T instruction; if is_d is true, reject forms
9429    that cannot be used with a D instruction.  If it is a store insn,
9430    reject PC in Rn.  */
9431
9432 static void
9433 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
9434 {
9435   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
9436
9437   constraint (!inst.operands[i].isreg,
9438               _("Instruction does not support =N addresses"));
9439
9440   inst.instruction |= inst.operands[i].reg << 16;
9441   if (inst.operands[i].immisreg)
9442     {
9443       constraint (is_pc, BAD_PC_ADDRESSING);
9444       constraint (is_t || is_d, _("cannot use register index with this instruction"));
9445       constraint (inst.operands[i].negative,
9446                   _("Thumb does not support negative register indexing"));
9447       constraint (inst.operands[i].postind,
9448                   _("Thumb does not support register post-indexing"));
9449       constraint (inst.operands[i].writeback,
9450                   _("Thumb does not support register indexing with writeback"));
9451       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
9452                   _("Thumb supports only LSL in shifted register indexing"));
9453
9454       inst.instruction |= inst.operands[i].imm;
9455       if (inst.operands[i].shifted)
9456         {
9457           constraint (inst.reloc.exp.X_op != O_constant,
9458                       _("expression too complex"));
9459           constraint (inst.reloc.exp.X_add_number < 0
9460                       || inst.reloc.exp.X_add_number > 3,
9461                       _("shift out of range"));
9462           inst.instruction |= inst.reloc.exp.X_add_number << 4;
9463         }
9464       inst.reloc.type = BFD_RELOC_UNUSED;
9465     }
9466   else if (inst.operands[i].preind)
9467     {
9468       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
9469       constraint (is_t && inst.operands[i].writeback,
9470                   _("cannot use writeback with this instruction"));
9471       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0)
9472                   && !inst.reloc.pc_rel, BAD_PC_ADDRESSING);
9473
9474       if (is_d)
9475         {
9476           inst.instruction |= 0x01000000;
9477           if (inst.operands[i].writeback)
9478             inst.instruction |= 0x00200000;
9479         }
9480       else
9481         {
9482           inst.instruction |= 0x00000c00;
9483           if (inst.operands[i].writeback)
9484             inst.instruction |= 0x00000100;
9485         }
9486       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
9487     }
9488   else if (inst.operands[i].postind)
9489     {
9490       gas_assert (inst.operands[i].writeback);
9491       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
9492       constraint (is_t, _("cannot use post-indexing with this instruction"));
9493
9494       if (is_d)
9495         inst.instruction |= 0x00200000;
9496       else
9497         inst.instruction |= 0x00000900;
9498       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
9499     }
9500   else /* unindexed - only for coprocessor */
9501     inst.error = _("instruction does not accept unindexed addressing");
9502 }
9503
9504 /* Table of Thumb instructions which exist in both 16- and 32-bit
9505    encodings (the latter only in post-V6T2 cores).  The index is the
9506    value used in the insns table below.  When there is more than one
9507    possible 16-bit encoding for the instruction, this table always
9508    holds variant (1).
9509    Also contains several pseudo-instructions used during relaxation.  */
9510 #define T16_32_TAB                              \
9511   X(_adc,   4140, eb400000),                    \
9512   X(_adcs,  4140, eb500000),                    \
9513   X(_add,   1c00, eb000000),                    \
9514   X(_adds,  1c00, eb100000),                    \
9515   X(_addi,  0000, f1000000),                    \
9516   X(_addis, 0000, f1100000),                    \
9517   X(_add_pc,000f, f20f0000),                    \
9518   X(_add_sp,000d, f10d0000),                    \
9519   X(_adr,   000f, f20f0000),                    \
9520   X(_and,   4000, ea000000),                    \
9521   X(_ands,  4000, ea100000),                    \
9522   X(_asr,   1000, fa40f000),                    \
9523   X(_asrs,  1000, fa50f000),                    \
9524   X(_b,     e000, f000b000),                    \
9525   X(_bcond, d000, f0008000),                    \
9526   X(_bic,   4380, ea200000),                    \
9527   X(_bics,  4380, ea300000),                    \
9528   X(_cmn,   42c0, eb100f00),                    \
9529   X(_cmp,   2800, ebb00f00),                    \
9530   X(_cpsie, b660, f3af8400),                    \
9531   X(_cpsid, b670, f3af8600),                    \
9532   X(_cpy,   4600, ea4f0000),                    \
9533   X(_dec_sp,80dd, f1ad0d00),                    \
9534   X(_eor,   4040, ea800000),                    \
9535   X(_eors,  4040, ea900000),                    \
9536   X(_inc_sp,00dd, f10d0d00),                    \
9537   X(_ldmia, c800, e8900000),                    \
9538   X(_ldr,   6800, f8500000),                    \
9539   X(_ldrb,  7800, f8100000),                    \
9540   X(_ldrh,  8800, f8300000),                    \
9541   X(_ldrsb, 5600, f9100000),                    \
9542   X(_ldrsh, 5e00, f9300000),                    \
9543   X(_ldr_pc,4800, f85f0000),                    \
9544   X(_ldr_pc2,4800, f85f0000),                   \
9545   X(_ldr_sp,9800, f85d0000),                    \
9546   X(_lsl,   0000, fa00f000),                    \
9547   X(_lsls,  0000, fa10f000),                    \
9548   X(_lsr,   0800, fa20f000),                    \
9549   X(_lsrs,  0800, fa30f000),                    \
9550   X(_mov,   2000, ea4f0000),                    \
9551   X(_movs,  2000, ea5f0000),                    \
9552   X(_mul,   4340, fb00f000),                     \
9553   X(_muls,  4340, ffffffff), /* no 32b muls */  \
9554   X(_mvn,   43c0, ea6f0000),                    \
9555   X(_mvns,  43c0, ea7f0000),                    \
9556   X(_neg,   4240, f1c00000), /* rsb #0 */       \
9557   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
9558   X(_orr,   4300, ea400000),                    \
9559   X(_orrs,  4300, ea500000),                    \
9560   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
9561   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
9562   X(_rev,   ba00, fa90f080),                    \
9563   X(_rev16, ba40, fa90f090),                    \
9564   X(_revsh, bac0, fa90f0b0),                    \
9565   X(_ror,   41c0, fa60f000),                    \
9566   X(_rors,  41c0, fa70f000),                    \
9567   X(_sbc,   4180, eb600000),                    \
9568   X(_sbcs,  4180, eb700000),                    \
9569   X(_stmia, c000, e8800000),                    \
9570   X(_str,   6000, f8400000),                    \
9571   X(_strb,  7000, f8000000),                    \
9572   X(_strh,  8000, f8200000),                    \
9573   X(_str_sp,9000, f84d0000),                    \
9574   X(_sub,   1e00, eba00000),                    \
9575   X(_subs,  1e00, ebb00000),                    \
9576   X(_subi,  8000, f1a00000),                    \
9577   X(_subis, 8000, f1b00000),                    \
9578   X(_sxtb,  b240, fa4ff080),                    \
9579   X(_sxth,  b200, fa0ff080),                    \
9580   X(_tst,   4200, ea100f00),                    \
9581   X(_uxtb,  b2c0, fa5ff080),                    \
9582   X(_uxth,  b280, fa1ff080),                    \
9583   X(_nop,   bf00, f3af8000),                    \
9584   X(_yield, bf10, f3af8001),                    \
9585   X(_wfe,   bf20, f3af8002),                    \
9586   X(_wfi,   bf30, f3af8003),                    \
9587   X(_sev,   bf40, f3af8004),                    \
9588   X(_sevl,  bf50, f3af8005)
9589
9590 /* To catch errors in encoding functions, the codes are all offset by
9591    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
9592    as 16-bit instructions.  */
9593 #define X(a,b,c) T_MNEM##a
9594 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
9595 #undef X
9596
9597 #define X(a,b,c) 0x##b
9598 static const unsigned short thumb_op16[] = { T16_32_TAB };
9599 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
9600 #undef X
9601
9602 #define X(a,b,c) 0x##c
9603 static const unsigned int thumb_op32[] = { T16_32_TAB };
9604 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
9605 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
9606 #undef X
9607 #undef T16_32_TAB
9608
9609 /* Thumb instruction encoders, in alphabetical order.  */
9610
9611 /* ADDW or SUBW.  */
9612
9613 static void
9614 do_t_add_sub_w (void)
9615 {
9616   int Rd, Rn;
9617
9618   Rd = inst.operands[0].reg;
9619   Rn = inst.operands[1].reg;
9620
9621   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
9622      is the SP-{plus,minus}-immediate form of the instruction.  */
9623   if (Rn == REG_SP)
9624     constraint (Rd == REG_PC, BAD_PC);
9625   else
9626     reject_bad_reg (Rd);
9627
9628   inst.instruction |= (Rn << 16) | (Rd << 8);
9629   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
9630 }
9631
9632 /* Parse an add or subtract instruction.  We get here with inst.instruction
9633    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
9634
9635 static void
9636 do_t_add_sub (void)
9637 {
9638   int Rd, Rs, Rn;
9639
9640   Rd = inst.operands[0].reg;
9641   Rs = (inst.operands[1].present
9642         ? inst.operands[1].reg    /* Rd, Rs, foo */
9643         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9644
9645   if (Rd == REG_PC)
9646     set_it_insn_type_last ();
9647
9648   if (unified_syntax)
9649     {
9650       bfd_boolean flags;
9651       bfd_boolean narrow;
9652       int opcode;
9653
9654       flags = (inst.instruction == T_MNEM_adds
9655                || inst.instruction == T_MNEM_subs);
9656       if (flags)
9657         narrow = !in_it_block ();
9658       else
9659         narrow = in_it_block ();
9660       if (!inst.operands[2].isreg)
9661         {
9662           int add;
9663
9664           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
9665
9666           add = (inst.instruction == T_MNEM_add
9667                  || inst.instruction == T_MNEM_adds);
9668           opcode = 0;
9669           if (inst.size_req != 4)
9670             {
9671               /* Attempt to use a narrow opcode, with relaxation if
9672                  appropriate.  */
9673               if (Rd == REG_SP && Rs == REG_SP && !flags)
9674                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
9675               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
9676                 opcode = T_MNEM_add_sp;
9677               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
9678                 opcode = T_MNEM_add_pc;
9679               else if (Rd <= 7 && Rs <= 7 && narrow)
9680                 {
9681                   if (flags)
9682                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
9683                   else
9684                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
9685                 }
9686               if (opcode)
9687                 {
9688                   inst.instruction = THUMB_OP16(opcode);
9689                   inst.instruction |= (Rd << 4) | Rs;
9690                   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9691                   if (inst.size_req != 2)
9692                     inst.relax = opcode;
9693                 }
9694               else
9695                 constraint (inst.size_req == 2, BAD_HIREG);
9696             }
9697           if (inst.size_req == 4
9698               || (inst.size_req != 2 && !opcode))
9699             {
9700               if (Rd == REG_PC)
9701                 {
9702                   constraint (add, BAD_PC);
9703                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
9704                              _("only SUBS PC, LR, #const allowed"));
9705                   constraint (inst.reloc.exp.X_op != O_constant,
9706                               _("expression too complex"));
9707                   constraint (inst.reloc.exp.X_add_number < 0
9708                               || inst.reloc.exp.X_add_number > 0xff,
9709                              _("immediate value out of range"));
9710                   inst.instruction = T2_SUBS_PC_LR
9711                                      | inst.reloc.exp.X_add_number;
9712                   inst.reloc.type = BFD_RELOC_UNUSED;
9713                   return;
9714                 }
9715               else if (Rs == REG_PC)
9716                 {
9717                   /* Always use addw/subw.  */
9718                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
9719                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
9720                 }
9721               else
9722                 {
9723                   inst.instruction = THUMB_OP32 (inst.instruction);
9724                   inst.instruction = (inst.instruction & 0xe1ffffff)
9725                                      | 0x10000000;
9726                   if (flags)
9727                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9728                   else
9729                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
9730                 }
9731               inst.instruction |= Rd << 8;
9732               inst.instruction |= Rs << 16;
9733             }
9734         }
9735       else
9736         {
9737           unsigned int value = inst.reloc.exp.X_add_number;
9738           unsigned int shift = inst.operands[2].shift_kind;
9739
9740           Rn = inst.operands[2].reg;
9741           /* See if we can do this with a 16-bit instruction.  */
9742           if (!inst.operands[2].shifted && inst.size_req != 4)
9743             {
9744               if (Rd > 7 || Rs > 7 || Rn > 7)
9745                 narrow = FALSE;
9746
9747               if (narrow)
9748                 {
9749                   inst.instruction = ((inst.instruction == T_MNEM_adds
9750                                        || inst.instruction == T_MNEM_add)
9751                                       ? T_OPCODE_ADD_R3
9752                                       : T_OPCODE_SUB_R3);
9753                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
9754                   return;
9755                 }
9756
9757               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
9758                 {
9759                   /* Thumb-1 cores (except v6-M) require at least one high
9760                      register in a narrow non flag setting add.  */
9761                   if (Rd > 7 || Rn > 7
9762                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
9763                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
9764                     {
9765                       if (Rd == Rn)
9766                         {
9767                           Rn = Rs;
9768                           Rs = Rd;
9769                         }
9770                       inst.instruction = T_OPCODE_ADD_HI;
9771                       inst.instruction |= (Rd & 8) << 4;
9772                       inst.instruction |= (Rd & 7);
9773                       inst.instruction |= Rn << 3;
9774                       return;
9775                     }
9776                 }
9777             }
9778
9779           constraint (Rd == REG_PC, BAD_PC);
9780           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
9781           constraint (Rs == REG_PC, BAD_PC);
9782           reject_bad_reg (Rn);
9783
9784           /* If we get here, it can't be done in 16 bits.  */
9785           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
9786                       _("shift must be constant"));
9787           inst.instruction = THUMB_OP32 (inst.instruction);
9788           inst.instruction |= Rd << 8;
9789           inst.instruction |= Rs << 16;
9790           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
9791                       _("shift value over 3 not allowed in thumb mode"));
9792           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
9793                       _("only LSL shift allowed in thumb mode"));
9794           encode_thumb32_shifted_operand (2);
9795         }
9796     }
9797   else
9798     {
9799       constraint (inst.instruction == T_MNEM_adds
9800                   || inst.instruction == T_MNEM_subs,
9801                   BAD_THUMB32);
9802
9803       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
9804         {
9805           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
9806                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
9807                       BAD_HIREG);
9808
9809           inst.instruction = (inst.instruction == T_MNEM_add
9810                               ? 0x0000 : 0x8000);
9811           inst.instruction |= (Rd << 4) | Rs;
9812           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9813           return;
9814         }
9815
9816       Rn = inst.operands[2].reg;
9817       constraint (inst.operands[2].shifted, _("unshifted register required"));
9818
9819       /* We now have Rd, Rs, and Rn set to registers.  */
9820       if (Rd > 7 || Rs > 7 || Rn > 7)
9821         {
9822           /* Can't do this for SUB.      */
9823           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
9824           inst.instruction = T_OPCODE_ADD_HI;
9825           inst.instruction |= (Rd & 8) << 4;
9826           inst.instruction |= (Rd & 7);
9827           if (Rs == Rd)
9828             inst.instruction |= Rn << 3;
9829           else if (Rn == Rd)
9830             inst.instruction |= Rs << 3;
9831           else
9832             constraint (1, _("dest must overlap one source register"));
9833         }
9834       else
9835         {
9836           inst.instruction = (inst.instruction == T_MNEM_add
9837                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
9838           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
9839         }
9840     }
9841 }
9842
9843 static void
9844 do_t_adr (void)
9845 {
9846   unsigned Rd;
9847
9848   Rd = inst.operands[0].reg;
9849   reject_bad_reg (Rd);
9850
9851   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
9852     {
9853       /* Defer to section relaxation.  */
9854       inst.relax = inst.instruction;
9855       inst.instruction = THUMB_OP16 (inst.instruction);
9856       inst.instruction |= Rd << 4;
9857     }
9858   else if (unified_syntax && inst.size_req != 2)
9859     {
9860       /* Generate a 32-bit opcode.  */
9861       inst.instruction = THUMB_OP32 (inst.instruction);
9862       inst.instruction |= Rd << 8;
9863       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
9864       inst.reloc.pc_rel = 1;
9865     }
9866   else
9867     {
9868       /* Generate a 16-bit opcode.  */
9869       inst.instruction = THUMB_OP16 (inst.instruction);
9870       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9871       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
9872       inst.reloc.pc_rel = 1;
9873
9874       inst.instruction |= Rd << 4;
9875     }
9876 }
9877
9878 /* Arithmetic instructions for which there is just one 16-bit
9879    instruction encoding, and it allows only two low registers.
9880    For maximal compatibility with ARM syntax, we allow three register
9881    operands even when Thumb-32 instructions are not available, as long
9882    as the first two are identical.  For instance, both "sbc r0,r1" and
9883    "sbc r0,r0,r1" are allowed.  */
9884 static void
9885 do_t_arit3 (void)
9886 {
9887   int Rd, Rs, Rn;
9888
9889   Rd = inst.operands[0].reg;
9890   Rs = (inst.operands[1].present
9891         ? inst.operands[1].reg    /* Rd, Rs, foo */
9892         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9893   Rn = inst.operands[2].reg;
9894
9895   reject_bad_reg (Rd);
9896   reject_bad_reg (Rs);
9897   if (inst.operands[2].isreg)
9898     reject_bad_reg (Rn);
9899
9900   if (unified_syntax)
9901     {
9902       if (!inst.operands[2].isreg)
9903         {
9904           /* For an immediate, we always generate a 32-bit opcode;
9905              section relaxation will shrink it later if possible.  */
9906           inst.instruction = THUMB_OP32 (inst.instruction);
9907           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9908           inst.instruction |= Rd << 8;
9909           inst.instruction |= Rs << 16;
9910           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9911         }
9912       else
9913         {
9914           bfd_boolean narrow;
9915
9916           /* See if we can do this with a 16-bit instruction.  */
9917           if (THUMB_SETS_FLAGS (inst.instruction))
9918             narrow = !in_it_block ();
9919           else
9920             narrow = in_it_block ();
9921
9922           if (Rd > 7 || Rn > 7 || Rs > 7)
9923             narrow = FALSE;
9924           if (inst.operands[2].shifted)
9925             narrow = FALSE;
9926           if (inst.size_req == 4)
9927             narrow = FALSE;
9928
9929           if (narrow
9930               && Rd == Rs)
9931             {
9932               inst.instruction = THUMB_OP16 (inst.instruction);
9933               inst.instruction |= Rd;
9934               inst.instruction |= Rn << 3;
9935               return;
9936             }
9937
9938           /* If we get here, it can't be done in 16 bits.  */
9939           constraint (inst.operands[2].shifted
9940                       && inst.operands[2].immisreg,
9941                       _("shift must be constant"));
9942           inst.instruction = THUMB_OP32 (inst.instruction);
9943           inst.instruction |= Rd << 8;
9944           inst.instruction |= Rs << 16;
9945           encode_thumb32_shifted_operand (2);
9946         }
9947     }
9948   else
9949     {
9950       /* On its face this is a lie - the instruction does set the
9951          flags.  However, the only supported mnemonic in this mode
9952          says it doesn't.  */
9953       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
9954
9955       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
9956                   _("unshifted register required"));
9957       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
9958       constraint (Rd != Rs,
9959                   _("dest and source1 must be the same register"));
9960
9961       inst.instruction = THUMB_OP16 (inst.instruction);
9962       inst.instruction |= Rd;
9963       inst.instruction |= Rn << 3;
9964     }
9965 }
9966
9967 /* Similarly, but for instructions where the arithmetic operation is
9968    commutative, so we can allow either of them to be different from
9969    the destination operand in a 16-bit instruction.  For instance, all
9970    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
9971    accepted.  */
9972 static void
9973 do_t_arit3c (void)
9974 {
9975   int Rd, Rs, Rn;
9976
9977   Rd = inst.operands[0].reg;
9978   Rs = (inst.operands[1].present
9979         ? inst.operands[1].reg    /* Rd, Rs, foo */
9980         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9981   Rn = inst.operands[2].reg;
9982
9983   reject_bad_reg (Rd);
9984   reject_bad_reg (Rs);
9985   if (inst.operands[2].isreg)
9986     reject_bad_reg (Rn);
9987
9988   if (unified_syntax)
9989     {
9990       if (!inst.operands[2].isreg)
9991         {
9992           /* For an immediate, we always generate a 32-bit opcode;
9993              section relaxation will shrink it later if possible.  */
9994           inst.instruction = THUMB_OP32 (inst.instruction);
9995           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9996           inst.instruction |= Rd << 8;
9997           inst.instruction |= Rs << 16;
9998           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9999         }
10000       else
10001         {
10002           bfd_boolean narrow;
10003
10004           /* See if we can do this with a 16-bit instruction.  */
10005           if (THUMB_SETS_FLAGS (inst.instruction))
10006             narrow = !in_it_block ();
10007           else
10008             narrow = in_it_block ();
10009
10010           if (Rd > 7 || Rn > 7 || Rs > 7)
10011             narrow = FALSE;
10012           if (inst.operands[2].shifted)
10013             narrow = FALSE;
10014           if (inst.size_req == 4)
10015             narrow = FALSE;
10016
10017           if (narrow)
10018             {
10019               if (Rd == Rs)
10020                 {
10021                   inst.instruction = THUMB_OP16 (inst.instruction);
10022                   inst.instruction |= Rd;
10023                   inst.instruction |= Rn << 3;
10024                   return;
10025                 }
10026               if (Rd == Rn)
10027                 {
10028                   inst.instruction = THUMB_OP16 (inst.instruction);
10029                   inst.instruction |= Rd;
10030                   inst.instruction |= Rs << 3;
10031                   return;
10032                 }
10033             }
10034
10035           /* If we get here, it can't be done in 16 bits.  */
10036           constraint (inst.operands[2].shifted
10037                       && inst.operands[2].immisreg,
10038                       _("shift must be constant"));
10039           inst.instruction = THUMB_OP32 (inst.instruction);
10040           inst.instruction |= Rd << 8;
10041           inst.instruction |= Rs << 16;
10042           encode_thumb32_shifted_operand (2);
10043         }
10044     }
10045   else
10046     {
10047       /* On its face this is a lie - the instruction does set the
10048          flags.  However, the only supported mnemonic in this mode
10049          says it doesn't.  */
10050       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10051
10052       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10053                   _("unshifted register required"));
10054       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10055
10056       inst.instruction = THUMB_OP16 (inst.instruction);
10057       inst.instruction |= Rd;
10058
10059       if (Rd == Rs)
10060         inst.instruction |= Rn << 3;
10061       else if (Rd == Rn)
10062         inst.instruction |= Rs << 3;
10063       else
10064         constraint (1, _("dest must overlap one source register"));
10065     }
10066 }
10067
10068 static void
10069 do_t_barrier (void)
10070 {
10071   if (inst.operands[0].present)
10072     {
10073       constraint ((inst.instruction & 0xf0) != 0x40
10074                   && inst.operands[0].imm > 0xf
10075                   && inst.operands[0].imm < 0x0,
10076                   _("bad barrier type"));
10077       inst.instruction |= inst.operands[0].imm;
10078     }
10079   else
10080     inst.instruction |= 0xf;
10081 }
10082
10083 static void
10084 do_t_bfc (void)
10085 {
10086   unsigned Rd;
10087   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
10088   constraint (msb > 32, _("bit-field extends past end of register"));
10089   /* The instruction encoding stores the LSB and MSB,
10090      not the LSB and width.  */
10091   Rd = inst.operands[0].reg;
10092   reject_bad_reg (Rd);
10093   inst.instruction |= Rd << 8;
10094   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
10095   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
10096   inst.instruction |= msb - 1;
10097 }
10098
10099 static void
10100 do_t_bfi (void)
10101 {
10102   int Rd, Rn;
10103   unsigned int msb;
10104
10105   Rd = inst.operands[0].reg;
10106   reject_bad_reg (Rd);
10107
10108   /* #0 in second position is alternative syntax for bfc, which is
10109      the same instruction but with REG_PC in the Rm field.  */
10110   if (!inst.operands[1].isreg)
10111     Rn = REG_PC;
10112   else
10113     {
10114       Rn = inst.operands[1].reg;
10115       reject_bad_reg (Rn);
10116     }
10117
10118   msb = inst.operands[2].imm + inst.operands[3].imm;
10119   constraint (msb > 32, _("bit-field extends past end of register"));
10120   /* The instruction encoding stores the LSB and MSB,
10121      not the LSB and width.  */
10122   inst.instruction |= Rd << 8;
10123   inst.instruction |= Rn << 16;
10124   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10125   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10126   inst.instruction |= msb - 1;
10127 }
10128
10129 static void
10130 do_t_bfx (void)
10131 {
10132   unsigned Rd, Rn;
10133
10134   Rd = inst.operands[0].reg;
10135   Rn = inst.operands[1].reg;
10136
10137   reject_bad_reg (Rd);
10138   reject_bad_reg (Rn);
10139
10140   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
10141               _("bit-field extends past end of register"));
10142   inst.instruction |= Rd << 8;
10143   inst.instruction |= Rn << 16;
10144   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10145   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10146   inst.instruction |= inst.operands[3].imm - 1;
10147 }
10148
10149 /* ARM V5 Thumb BLX (argument parse)
10150         BLX <target_addr>       which is BLX(1)
10151         BLX <Rm>                which is BLX(2)
10152    Unfortunately, there are two different opcodes for this mnemonic.
10153    So, the insns[].value is not used, and the code here zaps values
10154         into inst.instruction.
10155
10156    ??? How to take advantage of the additional two bits of displacement
10157    available in Thumb32 mode?  Need new relocation?  */
10158
10159 static void
10160 do_t_blx (void)
10161 {
10162   set_it_insn_type_last ();
10163
10164   if (inst.operands[0].isreg)
10165     {
10166       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
10167       /* We have a register, so this is BLX(2).  */
10168       inst.instruction |= inst.operands[0].reg << 3;
10169     }
10170   else
10171     {
10172       /* No register.  This must be BLX(1).  */
10173       inst.instruction = 0xf000e800;
10174       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
10175     }
10176 }
10177
10178 static void
10179 do_t_branch (void)
10180 {
10181   int opcode;
10182   int cond;
10183   int reloc;
10184
10185   cond = inst.cond;
10186   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
10187
10188   if (in_it_block ())
10189     {
10190       /* Conditional branches inside IT blocks are encoded as unconditional
10191          branches.  */
10192       cond = COND_ALWAYS;
10193     }
10194   else
10195     cond = inst.cond;
10196
10197   if (cond != COND_ALWAYS)
10198     opcode = T_MNEM_bcond;
10199   else
10200     opcode = inst.instruction;
10201
10202   if (unified_syntax
10203       && (inst.size_req == 4
10204           || (inst.size_req != 2
10205               && (inst.operands[0].hasreloc
10206                   || inst.reloc.exp.X_op == O_constant))))
10207     {
10208       inst.instruction = THUMB_OP32(opcode);
10209       if (cond == COND_ALWAYS)
10210         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
10211       else
10212         {
10213           gas_assert (cond != 0xF);
10214           inst.instruction |= cond << 22;
10215           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
10216         }
10217     }
10218   else
10219     {
10220       inst.instruction = THUMB_OP16(opcode);
10221       if (cond == COND_ALWAYS)
10222         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
10223       else
10224         {
10225           inst.instruction |= cond << 8;
10226           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
10227         }
10228       /* Allow section relaxation.  */
10229       if (unified_syntax && inst.size_req != 2)
10230         inst.relax = opcode;
10231     }
10232   inst.reloc.type = reloc;
10233   inst.reloc.pc_rel = 1;
10234 }
10235
10236 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
10237    between the two is the maximum immediate allowed - which is passed in
10238    RANGE.  */
10239 static void
10240 do_t_bkpt_hlt1 (int range)
10241 {
10242   constraint (inst.cond != COND_ALWAYS,
10243               _("instruction is always unconditional"));
10244   if (inst.operands[0].present)
10245     {
10246       constraint (inst.operands[0].imm > range,
10247                   _("immediate value out of range"));
10248       inst.instruction |= inst.operands[0].imm;
10249     }
10250
10251   set_it_insn_type (NEUTRAL_IT_INSN);
10252 }
10253
10254 static void
10255 do_t_hlt (void)
10256 {
10257   do_t_bkpt_hlt1 (63);
10258 }
10259
10260 static void
10261 do_t_bkpt (void)
10262 {
10263   do_t_bkpt_hlt1 (255);
10264 }
10265
10266 static void
10267 do_t_branch23 (void)
10268 {
10269   set_it_insn_type_last ();
10270   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
10271
10272   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
10273      this file.  We used to simply ignore the PLT reloc type here --
10274      the branch encoding is now needed to deal with TLSCALL relocs.
10275      So if we see a PLT reloc now, put it back to how it used to be to
10276      keep the preexisting behaviour.  */
10277   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
10278     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
10279
10280 #if defined(OBJ_COFF)
10281   /* If the destination of the branch is a defined symbol which does not have
10282      the THUMB_FUNC attribute, then we must be calling a function which has
10283      the (interfacearm) attribute.  We look for the Thumb entry point to that
10284      function and change the branch to refer to that function instead.  */
10285   if (   inst.reloc.exp.X_op == O_symbol
10286       && inst.reloc.exp.X_add_symbol != NULL
10287       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
10288       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
10289     inst.reloc.exp.X_add_symbol =
10290       find_real_start (inst.reloc.exp.X_add_symbol);
10291 #endif
10292 }
10293
10294 static void
10295 do_t_bx (void)
10296 {
10297   set_it_insn_type_last ();
10298   inst.instruction |= inst.operands[0].reg << 3;
10299   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
10300      should cause the alignment to be checked once it is known.  This is
10301      because BX PC only works if the instruction is word aligned.  */
10302 }
10303
10304 static void
10305 do_t_bxj (void)
10306 {
10307   int Rm;
10308
10309   set_it_insn_type_last ();
10310   Rm = inst.operands[0].reg;
10311   reject_bad_reg (Rm);
10312   inst.instruction |= Rm << 16;
10313 }
10314
10315 static void
10316 do_t_clz (void)
10317 {
10318   unsigned Rd;
10319   unsigned Rm;
10320
10321   Rd = inst.operands[0].reg;
10322   Rm = inst.operands[1].reg;
10323
10324   reject_bad_reg (Rd);
10325   reject_bad_reg (Rm);
10326
10327   inst.instruction |= Rd << 8;
10328   inst.instruction |= Rm << 16;
10329   inst.instruction |= Rm;
10330 }
10331
10332 static void
10333 do_t_cps (void)
10334 {
10335   set_it_insn_type (OUTSIDE_IT_INSN);
10336   inst.instruction |= inst.operands[0].imm;
10337 }
10338
10339 static void
10340 do_t_cpsi (void)
10341 {
10342   set_it_insn_type (OUTSIDE_IT_INSN);
10343   if (unified_syntax
10344       && (inst.operands[1].present || inst.size_req == 4)
10345       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
10346     {
10347       unsigned int imod = (inst.instruction & 0x0030) >> 4;
10348       inst.instruction = 0xf3af8000;
10349       inst.instruction |= imod << 9;
10350       inst.instruction |= inst.operands[0].imm << 5;
10351       if (inst.operands[1].present)
10352         inst.instruction |= 0x100 | inst.operands[1].imm;
10353     }
10354   else
10355     {
10356       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
10357                   && (inst.operands[0].imm & 4),
10358                   _("selected processor does not support 'A' form "
10359                     "of this instruction"));
10360       constraint (inst.operands[1].present || inst.size_req == 4,
10361                   _("Thumb does not support the 2-argument "
10362                     "form of this instruction"));
10363       inst.instruction |= inst.operands[0].imm;
10364     }
10365 }
10366
10367 /* THUMB CPY instruction (argument parse).  */
10368
10369 static void
10370 do_t_cpy (void)
10371 {
10372   if (inst.size_req == 4)
10373     {
10374       inst.instruction = THUMB_OP32 (T_MNEM_mov);
10375       inst.instruction |= inst.operands[0].reg << 8;
10376       inst.instruction |= inst.operands[1].reg;
10377     }
10378   else
10379     {
10380       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
10381       inst.instruction |= (inst.operands[0].reg & 0x7);
10382       inst.instruction |= inst.operands[1].reg << 3;
10383     }
10384 }
10385
10386 static void
10387 do_t_cbz (void)
10388 {
10389   set_it_insn_type (OUTSIDE_IT_INSN);
10390   constraint (inst.operands[0].reg > 7, BAD_HIREG);
10391   inst.instruction |= inst.operands[0].reg;
10392   inst.reloc.pc_rel = 1;
10393   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
10394 }
10395
10396 static void
10397 do_t_dbg (void)
10398 {
10399   inst.instruction |= inst.operands[0].imm;
10400 }
10401
10402 static void
10403 do_t_div (void)
10404 {
10405   unsigned Rd, Rn, Rm;
10406
10407   Rd = inst.operands[0].reg;
10408   Rn = (inst.operands[1].present
10409         ? inst.operands[1].reg : Rd);
10410   Rm = inst.operands[2].reg;
10411
10412   reject_bad_reg (Rd);
10413   reject_bad_reg (Rn);
10414   reject_bad_reg (Rm);
10415
10416   inst.instruction |= Rd << 8;
10417   inst.instruction |= Rn << 16;
10418   inst.instruction |= Rm;
10419 }
10420
10421 static void
10422 do_t_hint (void)
10423 {
10424   if (unified_syntax && inst.size_req == 4)
10425     inst.instruction = THUMB_OP32 (inst.instruction);
10426   else
10427     inst.instruction = THUMB_OP16 (inst.instruction);
10428 }
10429
10430 static void
10431 do_t_it (void)
10432 {
10433   unsigned int cond = inst.operands[0].imm;
10434
10435   set_it_insn_type (IT_INSN);
10436   now_it.mask = (inst.instruction & 0xf) | 0x10;
10437   now_it.cc = cond;
10438   now_it.warn_deprecated = FALSE;
10439
10440   /* If the condition is a negative condition, invert the mask.  */
10441   if ((cond & 0x1) == 0x0)
10442     {
10443       unsigned int mask = inst.instruction & 0x000f;
10444
10445       if ((mask & 0x7) == 0)
10446         {
10447           /* No conversion needed.  */
10448           now_it.block_length = 1;
10449         }
10450       else if ((mask & 0x3) == 0)
10451         {
10452           mask ^= 0x8;
10453           now_it.block_length = 2;
10454         }
10455       else if ((mask & 0x1) == 0)
10456         {
10457           mask ^= 0xC;
10458           now_it.block_length = 3;
10459         }
10460       else
10461         {
10462           mask ^= 0xE;
10463           now_it.block_length = 4;
10464         }
10465
10466       inst.instruction &= 0xfff0;
10467       inst.instruction |= mask;
10468     }
10469
10470   inst.instruction |= cond << 4;
10471 }
10472
10473 /* Helper function used for both push/pop and ldm/stm.  */
10474 static void
10475 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
10476 {
10477   bfd_boolean load;
10478
10479   load = (inst.instruction & (1 << 20)) != 0;
10480
10481   if (mask & (1 << 13))
10482     inst.error =  _("SP not allowed in register list");
10483
10484   if ((mask & (1 << base)) != 0
10485       && writeback)
10486     inst.error = _("having the base register in the register list when "
10487                    "using write back is UNPREDICTABLE");
10488
10489   if (load)
10490     {
10491       if (mask & (1 << 15))
10492         {
10493           if (mask & (1 << 14))
10494             inst.error = _("LR and PC should not both be in register list");
10495           else
10496             set_it_insn_type_last ();
10497         }
10498     }
10499   else
10500     {
10501       if (mask & (1 << 15))
10502         inst.error = _("PC not allowed in register list");
10503     }
10504
10505   if ((mask & (mask - 1)) == 0)
10506     {
10507       /* Single register transfers implemented as str/ldr.  */
10508       if (writeback)
10509         {
10510           if (inst.instruction & (1 << 23))
10511             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
10512           else
10513             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
10514         }
10515       else
10516         {
10517           if (inst.instruction & (1 << 23))
10518             inst.instruction = 0x00800000; /* ia -> [base] */
10519           else
10520             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
10521         }
10522
10523       inst.instruction |= 0xf8400000;
10524       if (load)
10525         inst.instruction |= 0x00100000;
10526
10527       mask = ffs (mask) - 1;
10528       mask <<= 12;
10529     }
10530   else if (writeback)
10531     inst.instruction |= WRITE_BACK;
10532
10533   inst.instruction |= mask;
10534   inst.instruction |= base << 16;
10535 }
10536
10537 static void
10538 do_t_ldmstm (void)
10539 {
10540   /* This really doesn't seem worth it.  */
10541   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
10542               _("expression too complex"));
10543   constraint (inst.operands[1].writeback,
10544               _("Thumb load/store multiple does not support {reglist}^"));
10545
10546   if (unified_syntax)
10547     {
10548       bfd_boolean narrow;
10549       unsigned mask;
10550
10551       narrow = FALSE;
10552       /* See if we can use a 16-bit instruction.  */
10553       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
10554           && inst.size_req != 4
10555           && !(inst.operands[1].imm & ~0xff))
10556         {
10557           mask = 1 << inst.operands[0].reg;
10558
10559           if (inst.operands[0].reg <= 7)
10560             {
10561               if (inst.instruction == T_MNEM_stmia
10562                   ? inst.operands[0].writeback
10563                   : (inst.operands[0].writeback
10564                      == !(inst.operands[1].imm & mask)))
10565                 {
10566                   if (inst.instruction == T_MNEM_stmia
10567                       && (inst.operands[1].imm & mask)
10568                       && (inst.operands[1].imm & (mask - 1)))
10569                     as_warn (_("value stored for r%d is UNKNOWN"),
10570                              inst.operands[0].reg);
10571
10572                   inst.instruction = THUMB_OP16 (inst.instruction);
10573                   inst.instruction |= inst.operands[0].reg << 8;
10574                   inst.instruction |= inst.operands[1].imm;
10575                   narrow = TRUE;
10576                 }
10577               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
10578                 {
10579                   /* This means 1 register in reg list one of 3 situations:
10580                      1. Instruction is stmia, but without writeback.
10581                      2. lmdia without writeback, but with Rn not in
10582                         reglist.
10583                      3. ldmia with writeback, but with Rn in reglist.
10584                      Case 3 is UNPREDICTABLE behaviour, so we handle
10585                      case 1 and 2 which can be converted into a 16-bit
10586                      str or ldr. The SP cases are handled below.  */
10587                   unsigned long opcode;
10588                   /* First, record an error for Case 3.  */
10589                   if (inst.operands[1].imm & mask
10590                       && inst.operands[0].writeback)
10591                     inst.error =
10592                         _("having the base register in the register list when "
10593                           "using write back is UNPREDICTABLE");
10594
10595                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
10596                                                              : T_MNEM_ldr);
10597                   inst.instruction = THUMB_OP16 (opcode);
10598                   inst.instruction |= inst.operands[0].reg << 3;
10599                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
10600                   narrow = TRUE;
10601                 }
10602             }
10603           else if (inst.operands[0] .reg == REG_SP)
10604             {
10605               if (inst.operands[0].writeback)
10606                 {
10607                   inst.instruction =
10608                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
10609                                     ? T_MNEM_push : T_MNEM_pop);
10610                   inst.instruction |= inst.operands[1].imm;
10611                   narrow = TRUE;
10612                 }
10613               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
10614                 {
10615                   inst.instruction =
10616                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
10617                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
10618                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
10619                   narrow = TRUE;
10620                 }
10621             }
10622         }
10623
10624       if (!narrow)
10625         {
10626           if (inst.instruction < 0xffff)
10627             inst.instruction = THUMB_OP32 (inst.instruction);
10628
10629           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
10630                                 inst.operands[0].writeback);
10631         }
10632     }
10633   else
10634     {
10635       constraint (inst.operands[0].reg > 7
10636                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
10637       constraint (inst.instruction != T_MNEM_ldmia
10638                   && inst.instruction != T_MNEM_stmia,
10639                   _("Thumb-2 instruction only valid in unified syntax"));
10640       if (inst.instruction == T_MNEM_stmia)
10641         {
10642           if (!inst.operands[0].writeback)
10643             as_warn (_("this instruction will write back the base register"));
10644           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
10645               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
10646             as_warn (_("value stored for r%d is UNKNOWN"),
10647                      inst.operands[0].reg);
10648         }
10649       else
10650         {
10651           if (!inst.operands[0].writeback
10652               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
10653             as_warn (_("this instruction will write back the base register"));
10654           else if (inst.operands[0].writeback
10655                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
10656             as_warn (_("this instruction will not write back the base register"));
10657         }
10658
10659       inst.instruction = THUMB_OP16 (inst.instruction);
10660       inst.instruction |= inst.operands[0].reg << 8;
10661       inst.instruction |= inst.operands[1].imm;
10662     }
10663 }
10664
10665 static void
10666 do_t_ldrex (void)
10667 {
10668   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
10669               || inst.operands[1].postind || inst.operands[1].writeback
10670               || inst.operands[1].immisreg || inst.operands[1].shifted
10671               || inst.operands[1].negative,
10672               BAD_ADDR_MODE);
10673
10674   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
10675
10676   inst.instruction |= inst.operands[0].reg << 12;
10677   inst.instruction |= inst.operands[1].reg << 16;
10678   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
10679 }
10680
10681 static void
10682 do_t_ldrexd (void)
10683 {
10684   if (!inst.operands[1].present)
10685     {
10686       constraint (inst.operands[0].reg == REG_LR,
10687                   _("r14 not allowed as first register "
10688                     "when second register is omitted"));
10689       inst.operands[1].reg = inst.operands[0].reg + 1;
10690     }
10691   constraint (inst.operands[0].reg == inst.operands[1].reg,
10692               BAD_OVERLAP);
10693
10694   inst.instruction |= inst.operands[0].reg << 12;
10695   inst.instruction |= inst.operands[1].reg << 8;
10696   inst.instruction |= inst.operands[2].reg << 16;
10697 }
10698
10699 static void
10700 do_t_ldst (void)
10701 {
10702   unsigned long opcode;
10703   int Rn;
10704
10705   if (inst.operands[0].isreg
10706       && !inst.operands[0].preind
10707       && inst.operands[0].reg == REG_PC)
10708     set_it_insn_type_last ();
10709
10710   opcode = inst.instruction;
10711   if (unified_syntax)
10712     {
10713       if (!inst.operands[1].isreg)
10714         {
10715           if (opcode <= 0xffff)
10716             inst.instruction = THUMB_OP32 (opcode);
10717           if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
10718             return;
10719         }
10720       if (inst.operands[1].isreg
10721           && !inst.operands[1].writeback
10722           && !inst.operands[1].shifted && !inst.operands[1].postind
10723           && !inst.operands[1].negative && inst.operands[0].reg <= 7
10724           && opcode <= 0xffff
10725           && inst.size_req != 4)
10726         {
10727           /* Insn may have a 16-bit form.  */
10728           Rn = inst.operands[1].reg;
10729           if (inst.operands[1].immisreg)
10730             {
10731               inst.instruction = THUMB_OP16 (opcode);
10732               /* [Rn, Rik] */
10733               if (Rn <= 7 && inst.operands[1].imm <= 7)
10734                 goto op16;
10735               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
10736                 reject_bad_reg (inst.operands[1].imm);
10737             }
10738           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
10739                     && opcode != T_MNEM_ldrsb)
10740                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
10741                    || (Rn == REG_SP && opcode == T_MNEM_str))
10742             {
10743               /* [Rn, #const] */
10744               if (Rn > 7)
10745                 {
10746                   if (Rn == REG_PC)
10747                     {
10748                       if (inst.reloc.pc_rel)
10749                         opcode = T_MNEM_ldr_pc2;
10750                       else
10751                         opcode = T_MNEM_ldr_pc;
10752                     }
10753                   else
10754                     {
10755                       if (opcode == T_MNEM_ldr)
10756                         opcode = T_MNEM_ldr_sp;
10757                       else
10758                         opcode = T_MNEM_str_sp;
10759                     }
10760                   inst.instruction = inst.operands[0].reg << 8;
10761                 }
10762               else
10763                 {
10764                   inst.instruction = inst.operands[0].reg;
10765                   inst.instruction |= inst.operands[1].reg << 3;
10766                 }
10767               inst.instruction |= THUMB_OP16 (opcode);
10768               if (inst.size_req == 2)
10769                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10770               else
10771                 inst.relax = opcode;
10772               return;
10773             }
10774         }
10775       /* Definitely a 32-bit variant.  */
10776
10777       /* Warning for Erratum 752419.  */
10778       if (opcode == T_MNEM_ldr
10779           && inst.operands[0].reg == REG_SP
10780           && inst.operands[1].writeback == 1
10781           && !inst.operands[1].immisreg)
10782         {
10783           if (no_cpu_selected ()
10784               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
10785                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
10786                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
10787             as_warn (_("This instruction may be unpredictable "
10788                        "if executed on M-profile cores "
10789                        "with interrupts enabled."));
10790         }
10791
10792       /* Do some validations regarding addressing modes.  */
10793       if (inst.operands[1].immisreg)
10794         reject_bad_reg (inst.operands[1].imm);
10795
10796       constraint (inst.operands[1].writeback == 1
10797                   && inst.operands[0].reg == inst.operands[1].reg,
10798                   BAD_OVERLAP);
10799
10800       inst.instruction = THUMB_OP32 (opcode);
10801       inst.instruction |= inst.operands[0].reg << 12;
10802       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
10803       check_ldr_r15_aligned ();
10804       return;
10805     }
10806
10807   constraint (inst.operands[0].reg > 7, BAD_HIREG);
10808
10809   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
10810     {
10811       /* Only [Rn,Rm] is acceptable.  */
10812       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
10813       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
10814                   || inst.operands[1].postind || inst.operands[1].shifted
10815                   || inst.operands[1].negative,
10816                   _("Thumb does not support this addressing mode"));
10817       inst.instruction = THUMB_OP16 (inst.instruction);
10818       goto op16;
10819     }
10820
10821   inst.instruction = THUMB_OP16 (inst.instruction);
10822   if (!inst.operands[1].isreg)
10823     if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
10824       return;
10825
10826   constraint (!inst.operands[1].preind
10827               || inst.operands[1].shifted
10828               || inst.operands[1].writeback,
10829               _("Thumb does not support this addressing mode"));
10830   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
10831     {
10832       constraint (inst.instruction & 0x0600,
10833                   _("byte or halfword not valid for base register"));
10834       constraint (inst.operands[1].reg == REG_PC
10835                   && !(inst.instruction & THUMB_LOAD_BIT),
10836                   _("r15 based store not allowed"));
10837       constraint (inst.operands[1].immisreg,
10838                   _("invalid base register for register offset"));
10839
10840       if (inst.operands[1].reg == REG_PC)
10841         inst.instruction = T_OPCODE_LDR_PC;
10842       else if (inst.instruction & THUMB_LOAD_BIT)
10843         inst.instruction = T_OPCODE_LDR_SP;
10844       else
10845         inst.instruction = T_OPCODE_STR_SP;
10846
10847       inst.instruction |= inst.operands[0].reg << 8;
10848       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10849       return;
10850     }
10851
10852   constraint (inst.operands[1].reg > 7, BAD_HIREG);
10853   if (!inst.operands[1].immisreg)
10854     {
10855       /* Immediate offset.  */
10856       inst.instruction |= inst.operands[0].reg;
10857       inst.instruction |= inst.operands[1].reg << 3;
10858       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10859       return;
10860     }
10861
10862   /* Register offset.  */
10863   constraint (inst.operands[1].imm > 7, BAD_HIREG);
10864   constraint (inst.operands[1].negative,
10865               _("Thumb does not support this addressing mode"));
10866
10867  op16:
10868   switch (inst.instruction)
10869     {
10870     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
10871     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
10872     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
10873     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
10874     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
10875     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
10876     case 0x5600 /* ldrsb */:
10877     case 0x5e00 /* ldrsh */: break;
10878     default: abort ();
10879     }
10880
10881   inst.instruction |= inst.operands[0].reg;
10882   inst.instruction |= inst.operands[1].reg << 3;
10883   inst.instruction |= inst.operands[1].imm << 6;
10884 }
10885
10886 static void
10887 do_t_ldstd (void)
10888 {
10889   if (!inst.operands[1].present)
10890     {
10891       inst.operands[1].reg = inst.operands[0].reg + 1;
10892       constraint (inst.operands[0].reg == REG_LR,
10893                   _("r14 not allowed here"));
10894       constraint (inst.operands[0].reg == REG_R12,
10895                   _("r12 not allowed here"));
10896     }
10897
10898   if (inst.operands[2].writeback
10899       && (inst.operands[0].reg == inst.operands[2].reg
10900       || inst.operands[1].reg == inst.operands[2].reg))
10901     as_warn (_("base register written back, and overlaps "
10902                "one of transfer registers"));
10903
10904   inst.instruction |= inst.operands[0].reg << 12;
10905   inst.instruction |= inst.operands[1].reg << 8;
10906   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
10907 }
10908
10909 static void
10910 do_t_ldstt (void)
10911 {
10912   inst.instruction |= inst.operands[0].reg << 12;
10913   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
10914 }
10915
10916 static void
10917 do_t_mla (void)
10918 {
10919   unsigned Rd, Rn, Rm, Ra;
10920
10921   Rd = inst.operands[0].reg;
10922   Rn = inst.operands[1].reg;
10923   Rm = inst.operands[2].reg;
10924   Ra = inst.operands[3].reg;
10925
10926   reject_bad_reg (Rd);
10927   reject_bad_reg (Rn);
10928   reject_bad_reg (Rm);
10929   reject_bad_reg (Ra);
10930
10931   inst.instruction |= Rd << 8;
10932   inst.instruction |= Rn << 16;
10933   inst.instruction |= Rm;
10934   inst.instruction |= Ra << 12;
10935 }
10936
10937 static void
10938 do_t_mlal (void)
10939 {
10940   unsigned RdLo, RdHi, Rn, Rm;
10941
10942   RdLo = inst.operands[0].reg;
10943   RdHi = inst.operands[1].reg;
10944   Rn = inst.operands[2].reg;
10945   Rm = inst.operands[3].reg;
10946
10947   reject_bad_reg (RdLo);
10948   reject_bad_reg (RdHi);
10949   reject_bad_reg (Rn);
10950   reject_bad_reg (Rm);
10951
10952   inst.instruction |= RdLo << 12;
10953   inst.instruction |= RdHi << 8;
10954   inst.instruction |= Rn << 16;
10955   inst.instruction |= Rm;
10956 }
10957
10958 static void
10959 do_t_mov_cmp (void)
10960 {
10961   unsigned Rn, Rm;
10962
10963   Rn = inst.operands[0].reg;
10964   Rm = inst.operands[1].reg;
10965
10966   if (Rn == REG_PC)
10967     set_it_insn_type_last ();
10968
10969   if (unified_syntax)
10970     {
10971       int r0off = (inst.instruction == T_MNEM_mov
10972                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
10973       unsigned long opcode;
10974       bfd_boolean narrow;
10975       bfd_boolean low_regs;
10976
10977       low_regs = (Rn <= 7 && Rm <= 7);
10978       opcode = inst.instruction;
10979       if (in_it_block ())
10980         narrow = opcode != T_MNEM_movs;
10981       else
10982         narrow = opcode != T_MNEM_movs || low_regs;
10983       if (inst.size_req == 4
10984           || inst.operands[1].shifted)
10985         narrow = FALSE;
10986
10987       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
10988       if (opcode == T_MNEM_movs && inst.operands[1].isreg
10989           && !inst.operands[1].shifted
10990           && Rn == REG_PC
10991           && Rm == REG_LR)
10992         {
10993           inst.instruction = T2_SUBS_PC_LR;
10994           return;
10995         }
10996
10997       if (opcode == T_MNEM_cmp)
10998         {
10999           constraint (Rn == REG_PC, BAD_PC);
11000           if (narrow)
11001             {
11002               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
11003                  but valid.  */
11004               warn_deprecated_sp (Rm);
11005               /* R15 was documented as a valid choice for Rm in ARMv6,
11006                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
11007                  tools reject R15, so we do too.  */
11008               constraint (Rm == REG_PC, BAD_PC);
11009             }
11010           else
11011             reject_bad_reg (Rm);
11012         }
11013       else if (opcode == T_MNEM_mov
11014                || opcode == T_MNEM_movs)
11015         {
11016           if (inst.operands[1].isreg)
11017             {
11018               if (opcode == T_MNEM_movs)
11019                 {
11020                   reject_bad_reg (Rn);
11021                   reject_bad_reg (Rm);
11022                 }
11023               else if (narrow)
11024                 {
11025                   /* This is mov.n.  */
11026                   if ((Rn == REG_SP || Rn == REG_PC)
11027                       && (Rm == REG_SP || Rm == REG_PC))
11028                     {
11029                       as_warn (_("Use of r%u as a source register is "
11030                                  "deprecated when r%u is the destination "
11031                                  "register."), Rm, Rn);
11032                     }
11033                 }
11034               else
11035                 {
11036                   /* This is mov.w.  */
11037                   constraint (Rn == REG_PC, BAD_PC);
11038                   constraint (Rm == REG_PC, BAD_PC);
11039                   constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
11040                 }
11041             }
11042           else
11043             reject_bad_reg (Rn);
11044         }
11045
11046       if (!inst.operands[1].isreg)
11047         {
11048           /* Immediate operand.  */
11049           if (!in_it_block () && opcode == T_MNEM_mov)
11050             narrow = 0;
11051           if (low_regs && narrow)
11052             {
11053               inst.instruction = THUMB_OP16 (opcode);
11054               inst.instruction |= Rn << 8;
11055               if (inst.size_req == 2)
11056                 inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
11057               else
11058                 inst.relax = opcode;
11059             }
11060           else
11061             {
11062               inst.instruction = THUMB_OP32 (inst.instruction);
11063               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11064               inst.instruction |= Rn << r0off;
11065               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11066             }
11067         }
11068       else if (inst.operands[1].shifted && inst.operands[1].immisreg
11069                && (inst.instruction == T_MNEM_mov
11070                    || inst.instruction == T_MNEM_movs))
11071         {
11072           /* Register shifts are encoded as separate shift instructions.  */
11073           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
11074
11075           if (in_it_block ())
11076             narrow = !flags;
11077           else
11078             narrow = flags;
11079
11080           if (inst.size_req == 4)
11081             narrow = FALSE;
11082
11083           if (!low_regs || inst.operands[1].imm > 7)
11084             narrow = FALSE;
11085
11086           if (Rn != Rm)
11087             narrow = FALSE;
11088
11089           switch (inst.operands[1].shift_kind)
11090             {
11091             case SHIFT_LSL:
11092               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
11093               break;
11094             case SHIFT_ASR:
11095               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
11096               break;
11097             case SHIFT_LSR:
11098               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
11099               break;
11100             case SHIFT_ROR:
11101               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
11102               break;
11103             default:
11104               abort ();
11105             }
11106
11107           inst.instruction = opcode;
11108           if (narrow)
11109             {
11110               inst.instruction |= Rn;
11111               inst.instruction |= inst.operands[1].imm << 3;
11112             }
11113           else
11114             {
11115               if (flags)
11116                 inst.instruction |= CONDS_BIT;
11117
11118               inst.instruction |= Rn << 8;
11119               inst.instruction |= Rm << 16;
11120               inst.instruction |= inst.operands[1].imm;
11121             }
11122         }
11123       else if (!narrow)
11124         {
11125           /* Some mov with immediate shift have narrow variants.
11126              Register shifts are handled above.  */
11127           if (low_regs && inst.operands[1].shifted
11128               && (inst.instruction == T_MNEM_mov
11129                   || inst.instruction == T_MNEM_movs))
11130             {
11131               if (in_it_block ())
11132                 narrow = (inst.instruction == T_MNEM_mov);
11133               else
11134                 narrow = (inst.instruction == T_MNEM_movs);
11135             }
11136
11137           if (narrow)
11138             {
11139               switch (inst.operands[1].shift_kind)
11140                 {
11141                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
11142                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
11143                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
11144                 default: narrow = FALSE; break;
11145                 }
11146             }
11147
11148           if (narrow)
11149             {
11150               inst.instruction |= Rn;
11151               inst.instruction |= Rm << 3;
11152               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11153             }
11154           else
11155             {
11156               inst.instruction = THUMB_OP32 (inst.instruction);
11157               inst.instruction |= Rn << r0off;
11158               encode_thumb32_shifted_operand (1);
11159             }
11160         }
11161       else
11162         switch (inst.instruction)
11163           {
11164           case T_MNEM_mov:
11165             /* In v4t or v5t a move of two lowregs produces unpredictable
11166                results. Don't allow this.  */
11167             if (low_regs)
11168               {
11169                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
11170                             "MOV Rd, Rs with two low registers is not "
11171                             "permitted on this architecture");
11172                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
11173                                         arm_ext_v6);
11174               }
11175
11176             inst.instruction = T_OPCODE_MOV_HR;
11177             inst.instruction |= (Rn & 0x8) << 4;
11178             inst.instruction |= (Rn & 0x7);
11179             inst.instruction |= Rm << 3;
11180             break;
11181
11182           case T_MNEM_movs:
11183             /* We know we have low registers at this point.
11184                Generate LSLS Rd, Rs, #0.  */
11185             inst.instruction = T_OPCODE_LSL_I;
11186             inst.instruction |= Rn;
11187             inst.instruction |= Rm << 3;
11188             break;
11189
11190           case T_MNEM_cmp:
11191             if (low_regs)
11192               {
11193                 inst.instruction = T_OPCODE_CMP_LR;
11194                 inst.instruction |= Rn;
11195                 inst.instruction |= Rm << 3;
11196               }
11197             else
11198               {
11199                 inst.instruction = T_OPCODE_CMP_HR;
11200                 inst.instruction |= (Rn & 0x8) << 4;
11201                 inst.instruction |= (Rn & 0x7);
11202                 inst.instruction |= Rm << 3;
11203               }
11204             break;
11205           }
11206       return;
11207     }
11208
11209   inst.instruction = THUMB_OP16 (inst.instruction);
11210
11211   /* PR 10443: Do not silently ignore shifted operands.  */
11212   constraint (inst.operands[1].shifted,
11213               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
11214
11215   if (inst.operands[1].isreg)
11216     {
11217       if (Rn < 8 && Rm < 8)
11218         {
11219           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
11220              since a MOV instruction produces unpredictable results.  */
11221           if (inst.instruction == T_OPCODE_MOV_I8)
11222             inst.instruction = T_OPCODE_ADD_I3;
11223           else
11224             inst.instruction = T_OPCODE_CMP_LR;
11225
11226           inst.instruction |= Rn;
11227           inst.instruction |= Rm << 3;
11228         }
11229       else
11230         {
11231           if (inst.instruction == T_OPCODE_MOV_I8)
11232             inst.instruction = T_OPCODE_MOV_HR;
11233           else
11234             inst.instruction = T_OPCODE_CMP_HR;
11235           do_t_cpy ();
11236         }
11237     }
11238   else
11239     {
11240       constraint (Rn > 7,
11241                   _("only lo regs allowed with immediate"));
11242       inst.instruction |= Rn << 8;
11243       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
11244     }
11245 }
11246
11247 static void
11248 do_t_mov16 (void)
11249 {
11250   unsigned Rd;
11251   bfd_vma imm;
11252   bfd_boolean top;
11253
11254   top = (inst.instruction & 0x00800000) != 0;
11255   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
11256     {
11257       constraint (top, _(":lower16: not allowed this instruction"));
11258       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
11259     }
11260   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
11261     {
11262       constraint (!top, _(":upper16: not allowed this instruction"));
11263       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
11264     }
11265
11266   Rd = inst.operands[0].reg;
11267   reject_bad_reg (Rd);
11268
11269   inst.instruction |= Rd << 8;
11270   if (inst.reloc.type == BFD_RELOC_UNUSED)
11271     {
11272       imm = inst.reloc.exp.X_add_number;
11273       inst.instruction |= (imm & 0xf000) << 4;
11274       inst.instruction |= (imm & 0x0800) << 15;
11275       inst.instruction |= (imm & 0x0700) << 4;
11276       inst.instruction |= (imm & 0x00ff);
11277     }
11278 }
11279
11280 static void
11281 do_t_mvn_tst (void)
11282 {
11283   unsigned Rn, Rm;
11284
11285   Rn = inst.operands[0].reg;
11286   Rm = inst.operands[1].reg;
11287
11288   if (inst.instruction == T_MNEM_cmp
11289       || inst.instruction == T_MNEM_cmn)
11290     constraint (Rn == REG_PC, BAD_PC);
11291   else
11292     reject_bad_reg (Rn);
11293   reject_bad_reg (Rm);
11294
11295   if (unified_syntax)
11296     {
11297       int r0off = (inst.instruction == T_MNEM_mvn
11298                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
11299       bfd_boolean narrow;
11300
11301       if (inst.size_req == 4
11302           || inst.instruction > 0xffff
11303           || inst.operands[1].shifted
11304           || Rn > 7 || Rm > 7)
11305         narrow = FALSE;
11306       else if (inst.instruction == T_MNEM_cmn)
11307         narrow = TRUE;
11308       else if (THUMB_SETS_FLAGS (inst.instruction))
11309         narrow = !in_it_block ();
11310       else
11311         narrow = in_it_block ();
11312
11313       if (!inst.operands[1].isreg)
11314         {
11315           /* For an immediate, we always generate a 32-bit opcode;
11316              section relaxation will shrink it later if possible.  */
11317           if (inst.instruction < 0xffff)
11318             inst.instruction = THUMB_OP32 (inst.instruction);
11319           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11320           inst.instruction |= Rn << r0off;
11321           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11322         }
11323       else
11324         {
11325           /* See if we can do this with a 16-bit instruction.  */
11326           if (narrow)
11327             {
11328               inst.instruction = THUMB_OP16 (inst.instruction);
11329               inst.instruction |= Rn;
11330               inst.instruction |= Rm << 3;
11331             }
11332           else
11333             {
11334               constraint (inst.operands[1].shifted
11335                           && inst.operands[1].immisreg,
11336                           _("shift must be constant"));
11337               if (inst.instruction < 0xffff)
11338                 inst.instruction = THUMB_OP32 (inst.instruction);
11339               inst.instruction |= Rn << r0off;
11340               encode_thumb32_shifted_operand (1);
11341             }
11342         }
11343     }
11344   else
11345     {
11346       constraint (inst.instruction > 0xffff
11347                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
11348       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
11349                   _("unshifted register required"));
11350       constraint (Rn > 7 || Rm > 7,
11351                   BAD_HIREG);
11352
11353       inst.instruction = THUMB_OP16 (inst.instruction);
11354       inst.instruction |= Rn;
11355       inst.instruction |= Rm << 3;
11356     }
11357 }
11358
11359 static void
11360 do_t_mrs (void)
11361 {
11362   unsigned Rd;
11363
11364   if (do_vfp_nsyn_mrs () == SUCCESS)
11365     return;
11366
11367   Rd = inst.operands[0].reg;
11368   reject_bad_reg (Rd);
11369   inst.instruction |= Rd << 8;
11370
11371   if (inst.operands[1].isreg)
11372     {
11373       unsigned br = inst.operands[1].reg;
11374       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
11375         as_bad (_("bad register for mrs"));
11376
11377       inst.instruction |= br & (0xf << 16);
11378       inst.instruction |= (br & 0x300) >> 4;
11379       inst.instruction |= (br & SPSR_BIT) >> 2;
11380     }
11381   else
11382     {
11383       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
11384
11385       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
11386         {
11387           /* PR gas/12698:  The constraint is only applied for m_profile.
11388              If the user has specified -march=all, we want to ignore it as
11389              we are building for any CPU type, including non-m variants.  */
11390           bfd_boolean m_profile = selected_cpu.core != arm_arch_any.core;
11391           constraint ((flags != 0) && m_profile, _("selected processor does "
11392                                                    "not support requested special purpose register"));
11393         }
11394       else
11395         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
11396            devices).  */
11397         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
11398                     _("'APSR', 'CPSR' or 'SPSR' expected"));
11399
11400       inst.instruction |= (flags & SPSR_BIT) >> 2;
11401       inst.instruction |= inst.operands[1].imm & 0xff;
11402       inst.instruction |= 0xf0000;
11403     }
11404 }
11405
11406 static void
11407 do_t_msr (void)
11408 {
11409   int flags;
11410   unsigned Rn;
11411
11412   if (do_vfp_nsyn_msr () == SUCCESS)
11413     return;
11414
11415   constraint (!inst.operands[1].isreg,
11416               _("Thumb encoding does not support an immediate here"));
11417
11418   if (inst.operands[0].isreg)
11419     flags = (int)(inst.operands[0].reg);
11420   else
11421     flags = inst.operands[0].imm;
11422
11423   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
11424     {
11425       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
11426
11427       /* PR gas/12698:  The constraint is only applied for m_profile.
11428          If the user has specified -march=all, we want to ignore it as
11429          we are building for any CPU type, including non-m variants.  */
11430       bfd_boolean m_profile = selected_cpu.core != arm_arch_any.core;
11431       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
11432            && (bits & ~(PSR_s | PSR_f)) != 0)
11433           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
11434               && bits != PSR_f)) && m_profile,
11435           _("selected processor does not support requested special "
11436             "purpose register"));
11437     }
11438   else
11439      constraint ((flags & 0xff) != 0, _("selected processor does not support "
11440                  "requested special purpose register"));
11441
11442   Rn = inst.operands[1].reg;
11443   reject_bad_reg (Rn);
11444
11445   inst.instruction |= (flags & SPSR_BIT) >> 2;
11446   inst.instruction |= (flags & 0xf0000) >> 8;
11447   inst.instruction |= (flags & 0x300) >> 4;
11448   inst.instruction |= (flags & 0xff);
11449   inst.instruction |= Rn << 16;
11450 }
11451
11452 static void
11453 do_t_mul (void)
11454 {
11455   bfd_boolean narrow;
11456   unsigned Rd, Rn, Rm;
11457
11458   if (!inst.operands[2].present)
11459     inst.operands[2].reg = inst.operands[0].reg;
11460
11461   Rd = inst.operands[0].reg;
11462   Rn = inst.operands[1].reg;
11463   Rm = inst.operands[2].reg;
11464
11465   if (unified_syntax)
11466     {
11467       if (inst.size_req == 4
11468           || (Rd != Rn
11469               && Rd != Rm)
11470           || Rn > 7
11471           || Rm > 7)
11472         narrow = FALSE;
11473       else if (inst.instruction == T_MNEM_muls)
11474         narrow = !in_it_block ();
11475       else
11476         narrow = in_it_block ();
11477     }
11478   else
11479     {
11480       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
11481       constraint (Rn > 7 || Rm > 7,
11482                   BAD_HIREG);
11483       narrow = TRUE;
11484     }
11485
11486   if (narrow)
11487     {
11488       /* 16-bit MULS/Conditional MUL.  */
11489       inst.instruction = THUMB_OP16 (inst.instruction);
11490       inst.instruction |= Rd;
11491
11492       if (Rd == Rn)
11493         inst.instruction |= Rm << 3;
11494       else if (Rd == Rm)
11495         inst.instruction |= Rn << 3;
11496       else
11497         constraint (1, _("dest must overlap one source register"));
11498     }
11499   else
11500     {
11501       constraint (inst.instruction != T_MNEM_mul,
11502                   _("Thumb-2 MUL must not set flags"));
11503       /* 32-bit MUL.  */
11504       inst.instruction = THUMB_OP32 (inst.instruction);
11505       inst.instruction |= Rd << 8;
11506       inst.instruction |= Rn << 16;
11507       inst.instruction |= Rm << 0;
11508
11509       reject_bad_reg (Rd);
11510       reject_bad_reg (Rn);
11511       reject_bad_reg (Rm);
11512     }
11513 }
11514
11515 static void
11516 do_t_mull (void)
11517 {
11518   unsigned RdLo, RdHi, Rn, Rm;
11519
11520   RdLo = inst.operands[0].reg;
11521   RdHi = inst.operands[1].reg;
11522   Rn = inst.operands[2].reg;
11523   Rm = inst.operands[3].reg;
11524
11525   reject_bad_reg (RdLo);
11526   reject_bad_reg (RdHi);
11527   reject_bad_reg (Rn);
11528   reject_bad_reg (Rm);
11529
11530   inst.instruction |= RdLo << 12;
11531   inst.instruction |= RdHi << 8;
11532   inst.instruction |= Rn << 16;
11533   inst.instruction |= Rm;
11534
11535  if (RdLo == RdHi)
11536     as_tsktsk (_("rdhi and rdlo must be different"));
11537 }
11538
11539 static void
11540 do_t_nop (void)
11541 {
11542   set_it_insn_type (NEUTRAL_IT_INSN);
11543
11544   if (unified_syntax)
11545     {
11546       if (inst.size_req == 4 || inst.operands[0].imm > 15)
11547         {
11548           inst.instruction = THUMB_OP32 (inst.instruction);
11549           inst.instruction |= inst.operands[0].imm;
11550         }
11551       else
11552         {
11553           /* PR9722: Check for Thumb2 availability before
11554              generating a thumb2 nop instruction.  */
11555           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
11556             {
11557               inst.instruction = THUMB_OP16 (inst.instruction);
11558               inst.instruction |= inst.operands[0].imm << 4;
11559             }
11560           else
11561             inst.instruction = 0x46c0;
11562         }
11563     }
11564   else
11565     {
11566       constraint (inst.operands[0].present,
11567                   _("Thumb does not support NOP with hints"));
11568       inst.instruction = 0x46c0;
11569     }
11570 }
11571
11572 static void
11573 do_t_neg (void)
11574 {
11575   if (unified_syntax)
11576     {
11577       bfd_boolean narrow;
11578
11579       if (THUMB_SETS_FLAGS (inst.instruction))
11580         narrow = !in_it_block ();
11581       else
11582         narrow = in_it_block ();
11583       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
11584         narrow = FALSE;
11585       if (inst.size_req == 4)
11586         narrow = FALSE;
11587
11588       if (!narrow)
11589         {
11590           inst.instruction = THUMB_OP32 (inst.instruction);
11591           inst.instruction |= inst.operands[0].reg << 8;
11592           inst.instruction |= inst.operands[1].reg << 16;
11593         }
11594       else
11595         {
11596           inst.instruction = THUMB_OP16 (inst.instruction);
11597           inst.instruction |= inst.operands[0].reg;
11598           inst.instruction |= inst.operands[1].reg << 3;
11599         }
11600     }
11601   else
11602     {
11603       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
11604                   BAD_HIREG);
11605       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11606
11607       inst.instruction = THUMB_OP16 (inst.instruction);
11608       inst.instruction |= inst.operands[0].reg;
11609       inst.instruction |= inst.operands[1].reg << 3;
11610     }
11611 }
11612
11613 static void
11614 do_t_orn (void)
11615 {
11616   unsigned Rd, Rn;
11617
11618   Rd = inst.operands[0].reg;
11619   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
11620
11621   reject_bad_reg (Rd);
11622   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
11623   reject_bad_reg (Rn);
11624
11625   inst.instruction |= Rd << 8;
11626   inst.instruction |= Rn << 16;
11627
11628   if (!inst.operands[2].isreg)
11629     {
11630       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11631       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11632     }
11633   else
11634     {
11635       unsigned Rm;
11636
11637       Rm = inst.operands[2].reg;
11638       reject_bad_reg (Rm);
11639
11640       constraint (inst.operands[2].shifted
11641                   && inst.operands[2].immisreg,
11642                   _("shift must be constant"));
11643       encode_thumb32_shifted_operand (2);
11644     }
11645 }
11646
11647 static void
11648 do_t_pkhbt (void)
11649 {
11650   unsigned Rd, Rn, Rm;
11651
11652   Rd = inst.operands[0].reg;
11653   Rn = inst.operands[1].reg;
11654   Rm = inst.operands[2].reg;
11655
11656   reject_bad_reg (Rd);
11657   reject_bad_reg (Rn);
11658   reject_bad_reg (Rm);
11659
11660   inst.instruction |= Rd << 8;
11661   inst.instruction |= Rn << 16;
11662   inst.instruction |= Rm;
11663   if (inst.operands[3].present)
11664     {
11665       unsigned int val = inst.reloc.exp.X_add_number;
11666       constraint (inst.reloc.exp.X_op != O_constant,
11667                   _("expression too complex"));
11668       inst.instruction |= (val & 0x1c) << 10;
11669       inst.instruction |= (val & 0x03) << 6;
11670     }
11671 }
11672
11673 static void
11674 do_t_pkhtb (void)
11675 {
11676   if (!inst.operands[3].present)
11677     {
11678       unsigned Rtmp;
11679
11680       inst.instruction &= ~0x00000020;
11681
11682       /* PR 10168.  Swap the Rm and Rn registers.  */
11683       Rtmp = inst.operands[1].reg;
11684       inst.operands[1].reg = inst.operands[2].reg;
11685       inst.operands[2].reg = Rtmp;
11686     }
11687   do_t_pkhbt ();
11688 }
11689
11690 static void
11691 do_t_pld (void)
11692 {
11693   if (inst.operands[0].immisreg)
11694     reject_bad_reg (inst.operands[0].imm);
11695
11696   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
11697 }
11698
11699 static void
11700 do_t_push_pop (void)
11701 {
11702   unsigned mask;
11703
11704   constraint (inst.operands[0].writeback,
11705               _("push/pop do not support {reglist}^"));
11706   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11707               _("expression too complex"));
11708
11709   mask = inst.operands[0].imm;
11710   if ((mask & ~0xff) == 0)
11711     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
11712   else if ((inst.instruction == T_MNEM_push
11713             && (mask & ~0xff) == 1 << REG_LR)
11714            || (inst.instruction == T_MNEM_pop
11715                && (mask & ~0xff) == 1 << REG_PC))
11716     {
11717       inst.instruction = THUMB_OP16 (inst.instruction);
11718       inst.instruction |= THUMB_PP_PC_LR;
11719       inst.instruction |= mask & 0xff;
11720     }
11721   else if (unified_syntax)
11722     {
11723       inst.instruction = THUMB_OP32 (inst.instruction);
11724       encode_thumb2_ldmstm (13, mask, TRUE);
11725     }
11726   else
11727     {
11728       inst.error = _("invalid register list to push/pop instruction");
11729       return;
11730     }
11731 }
11732
11733 static void
11734 do_t_rbit (void)
11735 {
11736   unsigned Rd, Rm;
11737
11738   Rd = inst.operands[0].reg;
11739   Rm = inst.operands[1].reg;
11740
11741   reject_bad_reg (Rd);
11742   reject_bad_reg (Rm);
11743
11744   inst.instruction |= Rd << 8;
11745   inst.instruction |= Rm << 16;
11746   inst.instruction |= Rm;
11747 }
11748
11749 static void
11750 do_t_rev (void)
11751 {
11752   unsigned Rd, Rm;
11753
11754   Rd = inst.operands[0].reg;
11755   Rm = inst.operands[1].reg;
11756
11757   reject_bad_reg (Rd);
11758   reject_bad_reg (Rm);
11759
11760   if (Rd <= 7 && Rm <= 7
11761       && inst.size_req != 4)
11762     {
11763       inst.instruction = THUMB_OP16 (inst.instruction);
11764       inst.instruction |= Rd;
11765       inst.instruction |= Rm << 3;
11766     }
11767   else if (unified_syntax)
11768     {
11769       inst.instruction = THUMB_OP32 (inst.instruction);
11770       inst.instruction |= Rd << 8;
11771       inst.instruction |= Rm << 16;
11772       inst.instruction |= Rm;
11773     }
11774   else
11775     inst.error = BAD_HIREG;
11776 }
11777
11778 static void
11779 do_t_rrx (void)
11780 {
11781   unsigned Rd, Rm;
11782
11783   Rd = inst.operands[0].reg;
11784   Rm = inst.operands[1].reg;
11785
11786   reject_bad_reg (Rd);
11787   reject_bad_reg (Rm);
11788
11789   inst.instruction |= Rd << 8;
11790   inst.instruction |= Rm;
11791 }
11792
11793 static void
11794 do_t_rsb (void)
11795 {
11796   unsigned Rd, Rs;
11797
11798   Rd = inst.operands[0].reg;
11799   Rs = (inst.operands[1].present
11800         ? inst.operands[1].reg    /* Rd, Rs, foo */
11801         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11802
11803   reject_bad_reg (Rd);
11804   reject_bad_reg (Rs);
11805   if (inst.operands[2].isreg)
11806     reject_bad_reg (inst.operands[2].reg);
11807
11808   inst.instruction |= Rd << 8;
11809   inst.instruction |= Rs << 16;
11810   if (!inst.operands[2].isreg)
11811     {
11812       bfd_boolean narrow;
11813
11814       if ((inst.instruction & 0x00100000) != 0)
11815         narrow = !in_it_block ();
11816       else
11817         narrow = in_it_block ();
11818
11819       if (Rd > 7 || Rs > 7)
11820         narrow = FALSE;
11821
11822       if (inst.size_req == 4 || !unified_syntax)
11823         narrow = FALSE;
11824
11825       if (inst.reloc.exp.X_op != O_constant
11826           || inst.reloc.exp.X_add_number != 0)
11827         narrow = FALSE;
11828
11829       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
11830          relaxation, but it doesn't seem worth the hassle.  */
11831       if (narrow)
11832         {
11833           inst.reloc.type = BFD_RELOC_UNUSED;
11834           inst.instruction = THUMB_OP16 (T_MNEM_negs);
11835           inst.instruction |= Rs << 3;
11836           inst.instruction |= Rd;
11837         }
11838       else
11839         {
11840           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11841           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11842         }
11843     }
11844   else
11845     encode_thumb32_shifted_operand (2);
11846 }
11847
11848 static void
11849 do_t_setend (void)
11850 {
11851   if (warn_on_deprecated
11852       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11853       as_warn (_("setend use is deprecated for ARMv8"));
11854
11855   set_it_insn_type (OUTSIDE_IT_INSN);
11856   if (inst.operands[0].imm)
11857     inst.instruction |= 0x8;
11858 }
11859
11860 static void
11861 do_t_shift (void)
11862 {
11863   if (!inst.operands[1].present)
11864     inst.operands[1].reg = inst.operands[0].reg;
11865
11866   if (unified_syntax)
11867     {
11868       bfd_boolean narrow;
11869       int shift_kind;
11870
11871       switch (inst.instruction)
11872         {
11873         case T_MNEM_asr:
11874         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
11875         case T_MNEM_lsl:
11876         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
11877         case T_MNEM_lsr:
11878         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
11879         case T_MNEM_ror:
11880         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
11881         default: abort ();
11882         }
11883
11884       if (THUMB_SETS_FLAGS (inst.instruction))
11885         narrow = !in_it_block ();
11886       else
11887         narrow = in_it_block ();
11888       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
11889         narrow = FALSE;
11890       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
11891         narrow = FALSE;
11892       if (inst.operands[2].isreg
11893           && (inst.operands[1].reg != inst.operands[0].reg
11894               || inst.operands[2].reg > 7))
11895         narrow = FALSE;
11896       if (inst.size_req == 4)
11897         narrow = FALSE;
11898
11899       reject_bad_reg (inst.operands[0].reg);
11900       reject_bad_reg (inst.operands[1].reg);
11901
11902       if (!narrow)
11903         {
11904           if (inst.operands[2].isreg)
11905             {
11906               reject_bad_reg (inst.operands[2].reg);
11907               inst.instruction = THUMB_OP32 (inst.instruction);
11908               inst.instruction |= inst.operands[0].reg << 8;
11909               inst.instruction |= inst.operands[1].reg << 16;
11910               inst.instruction |= inst.operands[2].reg;
11911
11912               /* PR 12854: Error on extraneous shifts.  */
11913               constraint (inst.operands[2].shifted,
11914                           _("extraneous shift as part of operand to shift insn"));
11915             }
11916           else
11917             {
11918               inst.operands[1].shifted = 1;
11919               inst.operands[1].shift_kind = shift_kind;
11920               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
11921                                              ? T_MNEM_movs : T_MNEM_mov);
11922               inst.instruction |= inst.operands[0].reg << 8;
11923               encode_thumb32_shifted_operand (1);
11924               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
11925               inst.reloc.type = BFD_RELOC_UNUSED;
11926             }
11927         }
11928       else
11929         {
11930           if (inst.operands[2].isreg)
11931             {
11932               switch (shift_kind)
11933                 {
11934                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
11935                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
11936                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
11937                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
11938                 default: abort ();
11939                 }
11940
11941               inst.instruction |= inst.operands[0].reg;
11942               inst.instruction |= inst.operands[2].reg << 3;
11943
11944               /* PR 12854: Error on extraneous shifts.  */
11945               constraint (inst.operands[2].shifted,
11946                           _("extraneous shift as part of operand to shift insn"));
11947             }
11948           else
11949             {
11950               switch (shift_kind)
11951                 {
11952                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
11953                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
11954                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
11955                 default: abort ();
11956                 }
11957               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11958               inst.instruction |= inst.operands[0].reg;
11959               inst.instruction |= inst.operands[1].reg << 3;
11960             }
11961         }
11962     }
11963   else
11964     {
11965       constraint (inst.operands[0].reg > 7
11966                   || inst.operands[1].reg > 7, BAD_HIREG);
11967       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11968
11969       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
11970         {
11971           constraint (inst.operands[2].reg > 7, BAD_HIREG);
11972           constraint (inst.operands[0].reg != inst.operands[1].reg,
11973                       _("source1 and dest must be same register"));
11974
11975           switch (inst.instruction)
11976             {
11977             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
11978             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
11979             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
11980             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
11981             default: abort ();
11982             }
11983
11984           inst.instruction |= inst.operands[0].reg;
11985           inst.instruction |= inst.operands[2].reg << 3;
11986
11987           /* PR 12854: Error on extraneous shifts.  */
11988           constraint (inst.operands[2].shifted,
11989                       _("extraneous shift as part of operand to shift insn"));
11990         }
11991       else
11992         {
11993           switch (inst.instruction)
11994             {
11995             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
11996             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
11997             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
11998             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
11999             default: abort ();
12000             }
12001           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12002           inst.instruction |= inst.operands[0].reg;
12003           inst.instruction |= inst.operands[1].reg << 3;
12004         }
12005     }
12006 }
12007
12008 static void
12009 do_t_simd (void)
12010 {
12011   unsigned Rd, Rn, Rm;
12012
12013   Rd = inst.operands[0].reg;
12014   Rn = inst.operands[1].reg;
12015   Rm = inst.operands[2].reg;
12016
12017   reject_bad_reg (Rd);
12018   reject_bad_reg (Rn);
12019   reject_bad_reg (Rm);
12020
12021   inst.instruction |= Rd << 8;
12022   inst.instruction |= Rn << 16;
12023   inst.instruction |= Rm;
12024 }
12025
12026 static void
12027 do_t_simd2 (void)
12028 {
12029   unsigned Rd, Rn, Rm;
12030
12031   Rd = inst.operands[0].reg;
12032   Rm = inst.operands[1].reg;
12033   Rn = inst.operands[2].reg;
12034
12035   reject_bad_reg (Rd);
12036   reject_bad_reg (Rn);
12037   reject_bad_reg (Rm);
12038
12039   inst.instruction |= Rd << 8;
12040   inst.instruction |= Rn << 16;
12041   inst.instruction |= Rm;
12042 }
12043
12044 static void
12045 do_t_smc (void)
12046 {
12047   unsigned int value = inst.reloc.exp.X_add_number;
12048   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
12049               _("SMC is not permitted on this architecture"));
12050   constraint (inst.reloc.exp.X_op != O_constant,
12051               _("expression too complex"));
12052   inst.reloc.type = BFD_RELOC_UNUSED;
12053   inst.instruction |= (value & 0xf000) >> 12;
12054   inst.instruction |= (value & 0x0ff0);
12055   inst.instruction |= (value & 0x000f) << 16;
12056 }
12057
12058 static void
12059 do_t_hvc (void)
12060 {
12061   unsigned int value = inst.reloc.exp.X_add_number;
12062
12063   inst.reloc.type = BFD_RELOC_UNUSED;
12064   inst.instruction |= (value & 0x0fff);
12065   inst.instruction |= (value & 0xf000) << 4;
12066 }
12067
12068 static void
12069 do_t_ssat_usat (int bias)
12070 {
12071   unsigned Rd, Rn;
12072
12073   Rd = inst.operands[0].reg;
12074   Rn = inst.operands[2].reg;
12075
12076   reject_bad_reg (Rd);
12077   reject_bad_reg (Rn);
12078
12079   inst.instruction |= Rd << 8;
12080   inst.instruction |= inst.operands[1].imm - bias;
12081   inst.instruction |= Rn << 16;
12082
12083   if (inst.operands[3].present)
12084     {
12085       offsetT shift_amount = inst.reloc.exp.X_add_number;
12086
12087       inst.reloc.type = BFD_RELOC_UNUSED;
12088
12089       constraint (inst.reloc.exp.X_op != O_constant,
12090                   _("expression too complex"));
12091
12092       if (shift_amount != 0)
12093         {
12094           constraint (shift_amount > 31,
12095                       _("shift expression is too large"));
12096
12097           if (inst.operands[3].shift_kind == SHIFT_ASR)
12098             inst.instruction |= 0x00200000;  /* sh bit.  */
12099
12100           inst.instruction |= (shift_amount & 0x1c) << 10;
12101           inst.instruction |= (shift_amount & 0x03) << 6;
12102         }
12103     }
12104 }
12105
12106 static void
12107 do_t_ssat (void)
12108 {
12109   do_t_ssat_usat (1);
12110 }
12111
12112 static void
12113 do_t_ssat16 (void)
12114 {
12115   unsigned Rd, Rn;
12116
12117   Rd = inst.operands[0].reg;
12118   Rn = inst.operands[2].reg;
12119
12120   reject_bad_reg (Rd);
12121   reject_bad_reg (Rn);
12122
12123   inst.instruction |= Rd << 8;
12124   inst.instruction |= inst.operands[1].imm - 1;
12125   inst.instruction |= Rn << 16;
12126 }
12127
12128 static void
12129 do_t_strex (void)
12130 {
12131   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
12132               || inst.operands[2].postind || inst.operands[2].writeback
12133               || inst.operands[2].immisreg || inst.operands[2].shifted
12134               || inst.operands[2].negative,
12135               BAD_ADDR_MODE);
12136
12137   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
12138
12139   inst.instruction |= inst.operands[0].reg << 8;
12140   inst.instruction |= inst.operands[1].reg << 12;
12141   inst.instruction |= inst.operands[2].reg << 16;
12142   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
12143 }
12144
12145 static void
12146 do_t_strexd (void)
12147 {
12148   if (!inst.operands[2].present)
12149     inst.operands[2].reg = inst.operands[1].reg + 1;
12150
12151   constraint (inst.operands[0].reg == inst.operands[1].reg
12152               || inst.operands[0].reg == inst.operands[2].reg
12153               || inst.operands[0].reg == inst.operands[3].reg,
12154               BAD_OVERLAP);
12155
12156   inst.instruction |= inst.operands[0].reg;
12157   inst.instruction |= inst.operands[1].reg << 12;
12158   inst.instruction |= inst.operands[2].reg << 8;
12159   inst.instruction |= inst.operands[3].reg << 16;
12160 }
12161
12162 static void
12163 do_t_sxtah (void)
12164 {
12165   unsigned Rd, Rn, Rm;
12166
12167   Rd = inst.operands[0].reg;
12168   Rn = inst.operands[1].reg;
12169   Rm = inst.operands[2].reg;
12170
12171   reject_bad_reg (Rd);
12172   reject_bad_reg (Rn);
12173   reject_bad_reg (Rm);
12174
12175   inst.instruction |= Rd << 8;
12176   inst.instruction |= Rn << 16;
12177   inst.instruction |= Rm;
12178   inst.instruction |= inst.operands[3].imm << 4;
12179 }
12180
12181 static void
12182 do_t_sxth (void)
12183 {
12184   unsigned Rd, Rm;
12185
12186   Rd = inst.operands[0].reg;
12187   Rm = inst.operands[1].reg;
12188
12189   reject_bad_reg (Rd);
12190   reject_bad_reg (Rm);
12191
12192   if (inst.instruction <= 0xffff
12193       && inst.size_req != 4
12194       && Rd <= 7 && Rm <= 7
12195       && (!inst.operands[2].present || inst.operands[2].imm == 0))
12196     {
12197       inst.instruction = THUMB_OP16 (inst.instruction);
12198       inst.instruction |= Rd;
12199       inst.instruction |= Rm << 3;
12200     }
12201   else if (unified_syntax)
12202     {
12203       if (inst.instruction <= 0xffff)
12204         inst.instruction = THUMB_OP32 (inst.instruction);
12205       inst.instruction |= Rd << 8;
12206       inst.instruction |= Rm;
12207       inst.instruction |= inst.operands[2].imm << 4;
12208     }
12209   else
12210     {
12211       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
12212                   _("Thumb encoding does not support rotation"));
12213       constraint (1, BAD_HIREG);
12214     }
12215 }
12216
12217 static void
12218 do_t_swi (void)
12219 {
12220   /* We have to do the following check manually as ARM_EXT_OS only applies
12221      to ARM_EXT_V6M.  */
12222   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6m))
12223     {
12224       if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_os)
12225           /* This only applies to the v6m howver, not later architectures.  */
12226           && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7))
12227         as_bad (_("SVC is not permitted on this architecture"));
12228       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, arm_ext_os);
12229     }
12230
12231   inst.reloc.type = BFD_RELOC_ARM_SWI;
12232 }
12233
12234 static void
12235 do_t_tb (void)
12236 {
12237   unsigned Rn, Rm;
12238   int half;
12239
12240   half = (inst.instruction & 0x10) != 0;
12241   set_it_insn_type_last ();
12242   constraint (inst.operands[0].immisreg,
12243               _("instruction requires register index"));
12244
12245   Rn = inst.operands[0].reg;
12246   Rm = inst.operands[0].imm;
12247
12248   constraint (Rn == REG_SP, BAD_SP);
12249   reject_bad_reg (Rm);
12250
12251   constraint (!half && inst.operands[0].shifted,
12252               _("instruction does not allow shifted index"));
12253   inst.instruction |= (Rn << 16) | Rm;
12254 }
12255
12256 static void
12257 do_t_usat (void)
12258 {
12259   do_t_ssat_usat (0);
12260 }
12261
12262 static void
12263 do_t_usat16 (void)
12264 {
12265   unsigned Rd, Rn;
12266
12267   Rd = inst.operands[0].reg;
12268   Rn = inst.operands[2].reg;
12269
12270   reject_bad_reg (Rd);
12271   reject_bad_reg (Rn);
12272
12273   inst.instruction |= Rd << 8;
12274   inst.instruction |= inst.operands[1].imm;
12275   inst.instruction |= Rn << 16;
12276 }
12277
12278 /* Neon instruction encoder helpers.  */
12279
12280 /* Encodings for the different types for various Neon opcodes.  */
12281
12282 /* An "invalid" code for the following tables.  */
12283 #define N_INV -1u
12284
12285 struct neon_tab_entry
12286 {
12287   unsigned integer;
12288   unsigned float_or_poly;
12289   unsigned scalar_or_imm;
12290 };
12291
12292 /* Map overloaded Neon opcodes to their respective encodings.  */
12293 #define NEON_ENC_TAB                                    \
12294   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
12295   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
12296   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
12297   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
12298   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
12299   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
12300   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
12301   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
12302   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
12303   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
12304   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
12305   /* Register variants of the following two instructions are encoded as
12306      vcge / vcgt with the operands reversed.  */        \
12307   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
12308   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
12309   X(vfma,       N_INV, 0x0000c10, N_INV),               \
12310   X(vfms,       N_INV, 0x0200c10, N_INV),               \
12311   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
12312   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
12313   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
12314   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
12315   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
12316   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
12317   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
12318   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
12319   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
12320   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
12321   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
12322   X(vshl,       0x0000400, N_INV,     0x0800510),       \
12323   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
12324   X(vand,       0x0000110, N_INV,     0x0800030),       \
12325   X(vbic,       0x0100110, N_INV,     0x0800030),       \
12326   X(veor,       0x1000110, N_INV,     N_INV),           \
12327   X(vorn,       0x0300110, N_INV,     0x0800010),       \
12328   X(vorr,       0x0200110, N_INV,     0x0800010),       \
12329   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
12330   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
12331   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
12332   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
12333   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
12334   X(vst1,       0x0000000, 0x0800000, N_INV),           \
12335   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
12336   X(vst2,       0x0000100, 0x0800100, N_INV),           \
12337   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
12338   X(vst3,       0x0000200, 0x0800200, N_INV),           \
12339   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
12340   X(vst4,       0x0000300, 0x0800300, N_INV),           \
12341   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
12342   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
12343   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
12344   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
12345   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
12346   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
12347   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
12348   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
12349   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
12350   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
12351   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
12352   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
12353   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
12354   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
12355   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
12356   X(vselge,     0xe200a00, N_INV,     N_INV),           \
12357   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
12358   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
12359   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
12360   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
12361   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
12362   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
12363   X(aes,        0x3b00300, N_INV,     N_INV),           \
12364   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
12365   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
12366   X(sha2op,     0x3ba0380, N_INV,     N_INV)
12367
12368 enum neon_opc
12369 {
12370 #define X(OPC,I,F,S) N_MNEM_##OPC
12371 NEON_ENC_TAB
12372 #undef X
12373 };
12374
12375 static const struct neon_tab_entry neon_enc_tab[] =
12376 {
12377 #define X(OPC,I,F,S) { (I), (F), (S) }
12378 NEON_ENC_TAB
12379 #undef X
12380 };
12381
12382 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
12383 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
12384 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
12385 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12386 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12387 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12388 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12389 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
12390 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12391 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12392 #define NEON_ENC_SINGLE_(X) \
12393   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
12394 #define NEON_ENC_DOUBLE_(X) \
12395   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
12396 #define NEON_ENC_FPV8_(X) \
12397   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
12398
12399 #define NEON_ENCODE(type, inst)                                 \
12400   do                                                            \
12401     {                                                           \
12402       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
12403       inst.is_neon = 1;                                         \
12404     }                                                           \
12405   while (0)
12406
12407 #define check_neon_suffixes                                             \
12408   do                                                                    \
12409     {                                                                   \
12410       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
12411         {                                                               \
12412           as_bad (_("invalid neon suffix for non neon instruction"));   \
12413           return;                                                       \
12414         }                                                               \
12415     }                                                                   \
12416   while (0)
12417
12418 /* Define shapes for instruction operands. The following mnemonic characters
12419    are used in this table:
12420
12421      F - VFP S<n> register
12422      D - Neon D<n> register
12423      Q - Neon Q<n> register
12424      I - Immediate
12425      S - Scalar
12426      R - ARM register
12427      L - D<n> register list
12428
12429    This table is used to generate various data:
12430      - enumerations of the form NS_DDR to be used as arguments to
12431        neon_select_shape.
12432      - a table classifying shapes into single, double, quad, mixed.
12433      - a table used to drive neon_select_shape.  */
12434
12435 #define NEON_SHAPE_DEF                  \
12436   X(3, (D, D, D), DOUBLE),              \
12437   X(3, (Q, Q, Q), QUAD),                \
12438   X(3, (D, D, I), DOUBLE),              \
12439   X(3, (Q, Q, I), QUAD),                \
12440   X(3, (D, D, S), DOUBLE),              \
12441   X(3, (Q, Q, S), QUAD),                \
12442   X(2, (D, D), DOUBLE),                 \
12443   X(2, (Q, Q), QUAD),                   \
12444   X(2, (D, S), DOUBLE),                 \
12445   X(2, (Q, S), QUAD),                   \
12446   X(2, (D, R), DOUBLE),                 \
12447   X(2, (Q, R), QUAD),                   \
12448   X(2, (D, I), DOUBLE),                 \
12449   X(2, (Q, I), QUAD),                   \
12450   X(3, (D, L, D), DOUBLE),              \
12451   X(2, (D, Q), MIXED),                  \
12452   X(2, (Q, D), MIXED),                  \
12453   X(3, (D, Q, I), MIXED),               \
12454   X(3, (Q, D, I), MIXED),               \
12455   X(3, (Q, D, D), MIXED),               \
12456   X(3, (D, Q, Q), MIXED),               \
12457   X(3, (Q, Q, D), MIXED),               \
12458   X(3, (Q, D, S), MIXED),               \
12459   X(3, (D, Q, S), MIXED),               \
12460   X(4, (D, D, D, I), DOUBLE),           \
12461   X(4, (Q, Q, Q, I), QUAD),             \
12462   X(2, (F, F), SINGLE),                 \
12463   X(3, (F, F, F), SINGLE),              \
12464   X(2, (F, I), SINGLE),                 \
12465   X(2, (F, D), MIXED),                  \
12466   X(2, (D, F), MIXED),                  \
12467   X(3, (F, F, I), MIXED),               \
12468   X(4, (R, R, F, F), SINGLE),           \
12469   X(4, (F, F, R, R), SINGLE),           \
12470   X(3, (D, R, R), DOUBLE),              \
12471   X(3, (R, R, D), DOUBLE),              \
12472   X(2, (S, R), SINGLE),                 \
12473   X(2, (R, S), SINGLE),                 \
12474   X(2, (F, R), SINGLE),                 \
12475   X(2, (R, F), SINGLE)
12476
12477 #define S2(A,B)         NS_##A##B
12478 #define S3(A,B,C)       NS_##A##B##C
12479 #define S4(A,B,C,D)     NS_##A##B##C##D
12480
12481 #define X(N, L, C) S##N L
12482
12483 enum neon_shape
12484 {
12485   NEON_SHAPE_DEF,
12486   NS_NULL
12487 };
12488
12489 #undef X
12490 #undef S2
12491 #undef S3
12492 #undef S4
12493
12494 enum neon_shape_class
12495 {
12496   SC_SINGLE,
12497   SC_DOUBLE,
12498   SC_QUAD,
12499   SC_MIXED
12500 };
12501
12502 #define X(N, L, C) SC_##C
12503
12504 static enum neon_shape_class neon_shape_class[] =
12505 {
12506   NEON_SHAPE_DEF
12507 };
12508
12509 #undef X
12510
12511 enum neon_shape_el
12512 {
12513   SE_F,
12514   SE_D,
12515   SE_Q,
12516   SE_I,
12517   SE_S,
12518   SE_R,
12519   SE_L
12520 };
12521
12522 /* Register widths of above.  */
12523 static unsigned neon_shape_el_size[] =
12524 {
12525   32,
12526   64,
12527   128,
12528   0,
12529   32,
12530   32,
12531   0
12532 };
12533
12534 struct neon_shape_info
12535 {
12536   unsigned els;
12537   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
12538 };
12539
12540 #define S2(A,B)         { SE_##A, SE_##B }
12541 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
12542 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
12543
12544 #define X(N, L, C) { N, S##N L }
12545
12546 static struct neon_shape_info neon_shape_tab[] =
12547 {
12548   NEON_SHAPE_DEF
12549 };
12550
12551 #undef X
12552 #undef S2
12553 #undef S3
12554 #undef S4
12555
12556 /* Bit masks used in type checking given instructions.
12557   'N_EQK' means the type must be the same as (or based on in some way) the key
12558    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
12559    set, various other bits can be set as well in order to modify the meaning of
12560    the type constraint.  */
12561
12562 enum neon_type_mask
12563 {
12564   N_S8   = 0x0000001,
12565   N_S16  = 0x0000002,
12566   N_S32  = 0x0000004,
12567   N_S64  = 0x0000008,
12568   N_U8   = 0x0000010,
12569   N_U16  = 0x0000020,
12570   N_U32  = 0x0000040,
12571   N_U64  = 0x0000080,
12572   N_I8   = 0x0000100,
12573   N_I16  = 0x0000200,
12574   N_I32  = 0x0000400,
12575   N_I64  = 0x0000800,
12576   N_8    = 0x0001000,
12577   N_16   = 0x0002000,
12578   N_32   = 0x0004000,
12579   N_64   = 0x0008000,
12580   N_P8   = 0x0010000,
12581   N_P16  = 0x0020000,
12582   N_F16  = 0x0040000,
12583   N_F32  = 0x0080000,
12584   N_F64  = 0x0100000,
12585   N_P64  = 0x0200000,
12586   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
12587   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
12588   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
12589   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
12590   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
12591   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
12592   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
12593   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
12594   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
12595   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
12596   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
12597   N_UTYP = 0,
12598   N_MAX_NONSPECIAL = N_P64
12599 };
12600
12601 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
12602
12603 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
12604 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
12605 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
12606 #define N_SUF_32   (N_SU_32 | N_F32)
12607 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
12608 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F32)
12609
12610 /* Pass this as the first type argument to neon_check_type to ignore types
12611    altogether.  */
12612 #define N_IGNORE_TYPE (N_KEY | N_EQK)
12613
12614 /* Select a "shape" for the current instruction (describing register types or
12615    sizes) from a list of alternatives. Return NS_NULL if the current instruction
12616    doesn't fit. For non-polymorphic shapes, checking is usually done as a
12617    function of operand parsing, so this function doesn't need to be called.
12618    Shapes should be listed in order of decreasing length.  */
12619
12620 static enum neon_shape
12621 neon_select_shape (enum neon_shape shape, ...)
12622 {
12623   va_list ap;
12624   enum neon_shape first_shape = shape;
12625
12626   /* Fix missing optional operands. FIXME: we don't know at this point how
12627      many arguments we should have, so this makes the assumption that we have
12628      > 1. This is true of all current Neon opcodes, I think, but may not be
12629      true in the future.  */
12630   if (!inst.operands[1].present)
12631     inst.operands[1] = inst.operands[0];
12632
12633   va_start (ap, shape);
12634
12635   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
12636     {
12637       unsigned j;
12638       int matches = 1;
12639
12640       for (j = 0; j < neon_shape_tab[shape].els; j++)
12641         {
12642           if (!inst.operands[j].present)
12643             {
12644               matches = 0;
12645               break;
12646             }
12647
12648           switch (neon_shape_tab[shape].el[j])
12649             {
12650             case SE_F:
12651               if (!(inst.operands[j].isreg
12652                     && inst.operands[j].isvec
12653                     && inst.operands[j].issingle
12654                     && !inst.operands[j].isquad))
12655                 matches = 0;
12656               break;
12657
12658             case SE_D:
12659               if (!(inst.operands[j].isreg
12660                     && inst.operands[j].isvec
12661                     && !inst.operands[j].isquad
12662                     && !inst.operands[j].issingle))
12663                 matches = 0;
12664               break;
12665
12666             case SE_R:
12667               if (!(inst.operands[j].isreg
12668                     && !inst.operands[j].isvec))
12669                 matches = 0;
12670               break;
12671
12672             case SE_Q:
12673               if (!(inst.operands[j].isreg
12674                     && inst.operands[j].isvec
12675                     && inst.operands[j].isquad
12676                     && !inst.operands[j].issingle))
12677                 matches = 0;
12678               break;
12679
12680             case SE_I:
12681               if (!(!inst.operands[j].isreg
12682                     && !inst.operands[j].isscalar))
12683                 matches = 0;
12684               break;
12685
12686             case SE_S:
12687               if (!(!inst.operands[j].isreg
12688                     && inst.operands[j].isscalar))
12689                 matches = 0;
12690               break;
12691
12692             case SE_L:
12693               break;
12694             }
12695           if (!matches)
12696             break;
12697         }
12698       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
12699         /* We've matched all the entries in the shape table, and we don't
12700            have any left over operands which have not been matched.  */
12701         break;
12702     }
12703
12704   va_end (ap);
12705
12706   if (shape == NS_NULL && first_shape != NS_NULL)
12707     first_error (_("invalid instruction shape"));
12708
12709   return shape;
12710 }
12711
12712 /* True if SHAPE is predominantly a quadword operation (most of the time, this
12713    means the Q bit should be set).  */
12714
12715 static int
12716 neon_quad (enum neon_shape shape)
12717 {
12718   return neon_shape_class[shape] == SC_QUAD;
12719 }
12720
12721 static void
12722 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
12723                        unsigned *g_size)
12724 {
12725   /* Allow modification to be made to types which are constrained to be
12726      based on the key element, based on bits set alongside N_EQK.  */
12727   if ((typebits & N_EQK) != 0)
12728     {
12729       if ((typebits & N_HLF) != 0)
12730         *g_size /= 2;
12731       else if ((typebits & N_DBL) != 0)
12732         *g_size *= 2;
12733       if ((typebits & N_SGN) != 0)
12734         *g_type = NT_signed;
12735       else if ((typebits & N_UNS) != 0)
12736         *g_type = NT_unsigned;
12737       else if ((typebits & N_INT) != 0)
12738         *g_type = NT_integer;
12739       else if ((typebits & N_FLT) != 0)
12740         *g_type = NT_float;
12741       else if ((typebits & N_SIZ) != 0)
12742         *g_type = NT_untyped;
12743     }
12744 }
12745
12746 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
12747    operand type, i.e. the single type specified in a Neon instruction when it
12748    is the only one given.  */
12749
12750 static struct neon_type_el
12751 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
12752 {
12753   struct neon_type_el dest = *key;
12754
12755   gas_assert ((thisarg & N_EQK) != 0);
12756
12757   neon_modify_type_size (thisarg, &dest.type, &dest.size);
12758
12759   return dest;
12760 }
12761
12762 /* Convert Neon type and size into compact bitmask representation.  */
12763
12764 static enum neon_type_mask
12765 type_chk_of_el_type (enum neon_el_type type, unsigned size)
12766 {
12767   switch (type)
12768     {
12769     case NT_untyped:
12770       switch (size)
12771         {
12772         case 8:  return N_8;
12773         case 16: return N_16;
12774         case 32: return N_32;
12775         case 64: return N_64;
12776         default: ;
12777         }
12778       break;
12779
12780     case NT_integer:
12781       switch (size)
12782         {
12783         case 8:  return N_I8;
12784         case 16: return N_I16;
12785         case 32: return N_I32;
12786         case 64: return N_I64;
12787         default: ;
12788         }
12789       break;
12790
12791     case NT_float:
12792       switch (size)
12793         {
12794         case 16: return N_F16;
12795         case 32: return N_F32;
12796         case 64: return N_F64;
12797         default: ;
12798         }
12799       break;
12800
12801     case NT_poly:
12802       switch (size)
12803         {
12804         case 8:  return N_P8;
12805         case 16: return N_P16;
12806         case 64: return N_P64;
12807         default: ;
12808         }
12809       break;
12810
12811     case NT_signed:
12812       switch (size)
12813         {
12814         case 8:  return N_S8;
12815         case 16: return N_S16;
12816         case 32: return N_S32;
12817         case 64: return N_S64;
12818         default: ;
12819         }
12820       break;
12821
12822     case NT_unsigned:
12823       switch (size)
12824         {
12825         case 8:  return N_U8;
12826         case 16: return N_U16;
12827         case 32: return N_U32;
12828         case 64: return N_U64;
12829         default: ;
12830         }
12831       break;
12832
12833     default: ;
12834     }
12835
12836   return N_UTYP;
12837 }
12838
12839 /* Convert compact Neon bitmask type representation to a type and size. Only
12840    handles the case where a single bit is set in the mask.  */
12841
12842 static int
12843 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
12844                      enum neon_type_mask mask)
12845 {
12846   if ((mask & N_EQK) != 0)
12847     return FAIL;
12848
12849   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
12850     *size = 8;
12851   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
12852     *size = 16;
12853   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
12854     *size = 32;
12855   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
12856     *size = 64;
12857   else
12858     return FAIL;
12859
12860   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
12861     *type = NT_signed;
12862   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
12863     *type = NT_unsigned;
12864   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
12865     *type = NT_integer;
12866   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
12867     *type = NT_untyped;
12868   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
12869     *type = NT_poly;
12870   else if ((mask & (N_F16 | N_F32 | N_F64)) != 0)
12871     *type = NT_float;
12872   else
12873     return FAIL;
12874
12875   return SUCCESS;
12876 }
12877
12878 /* Modify a bitmask of allowed types. This is only needed for type
12879    relaxation.  */
12880
12881 static unsigned
12882 modify_types_allowed (unsigned allowed, unsigned mods)
12883 {
12884   unsigned size;
12885   enum neon_el_type type;
12886   unsigned destmask;
12887   int i;
12888
12889   destmask = 0;
12890
12891   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
12892     {
12893       if (el_type_of_type_chk (&type, &size,
12894                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
12895         {
12896           neon_modify_type_size (mods, &type, &size);
12897           destmask |= type_chk_of_el_type (type, size);
12898         }
12899     }
12900
12901   return destmask;
12902 }
12903
12904 /* Check type and return type classification.
12905    The manual states (paraphrase): If one datatype is given, it indicates the
12906    type given in:
12907     - the second operand, if there is one
12908     - the operand, if there is no second operand
12909     - the result, if there are no operands.
12910    This isn't quite good enough though, so we use a concept of a "key" datatype
12911    which is set on a per-instruction basis, which is the one which matters when
12912    only one data type is written.
12913    Note: this function has side-effects (e.g. filling in missing operands). All
12914    Neon instructions should call it before performing bit encoding.  */
12915
12916 static struct neon_type_el
12917 neon_check_type (unsigned els, enum neon_shape ns, ...)
12918 {
12919   va_list ap;
12920   unsigned i, pass, key_el = 0;
12921   unsigned types[NEON_MAX_TYPE_ELS];
12922   enum neon_el_type k_type = NT_invtype;
12923   unsigned k_size = -1u;
12924   struct neon_type_el badtype = {NT_invtype, -1};
12925   unsigned key_allowed = 0;
12926
12927   /* Optional registers in Neon instructions are always (not) in operand 1.
12928      Fill in the missing operand here, if it was omitted.  */
12929   if (els > 1 && !inst.operands[1].present)
12930     inst.operands[1] = inst.operands[0];
12931
12932   /* Suck up all the varargs.  */
12933   va_start (ap, ns);
12934   for (i = 0; i < els; i++)
12935     {
12936       unsigned thisarg = va_arg (ap, unsigned);
12937       if (thisarg == N_IGNORE_TYPE)
12938         {
12939           va_end (ap);
12940           return badtype;
12941         }
12942       types[i] = thisarg;
12943       if ((thisarg & N_KEY) != 0)
12944         key_el = i;
12945     }
12946   va_end (ap);
12947
12948   if (inst.vectype.elems > 0)
12949     for (i = 0; i < els; i++)
12950       if (inst.operands[i].vectype.type != NT_invtype)
12951         {
12952           first_error (_("types specified in both the mnemonic and operands"));
12953           return badtype;
12954         }
12955
12956   /* Duplicate inst.vectype elements here as necessary.
12957      FIXME: No idea if this is exactly the same as the ARM assembler,
12958      particularly when an insn takes one register and one non-register
12959      operand. */
12960   if (inst.vectype.elems == 1 && els > 1)
12961     {
12962       unsigned j;
12963       inst.vectype.elems = els;
12964       inst.vectype.el[key_el] = inst.vectype.el[0];
12965       for (j = 0; j < els; j++)
12966         if (j != key_el)
12967           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
12968                                                   types[j]);
12969     }
12970   else if (inst.vectype.elems == 0 && els > 0)
12971     {
12972       unsigned j;
12973       /* No types were given after the mnemonic, so look for types specified
12974          after each operand. We allow some flexibility here; as long as the
12975          "key" operand has a type, we can infer the others.  */
12976       for (j = 0; j < els; j++)
12977         if (inst.operands[j].vectype.type != NT_invtype)
12978           inst.vectype.el[j] = inst.operands[j].vectype;
12979
12980       if (inst.operands[key_el].vectype.type != NT_invtype)
12981         {
12982           for (j = 0; j < els; j++)
12983             if (inst.operands[j].vectype.type == NT_invtype)
12984               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
12985                                                       types[j]);
12986         }
12987       else
12988         {
12989           first_error (_("operand types can't be inferred"));
12990           return badtype;
12991         }
12992     }
12993   else if (inst.vectype.elems != els)
12994     {
12995       first_error (_("type specifier has the wrong number of parts"));
12996       return badtype;
12997     }
12998
12999   for (pass = 0; pass < 2; pass++)
13000     {
13001       for (i = 0; i < els; i++)
13002         {
13003           unsigned thisarg = types[i];
13004           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
13005             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
13006           enum neon_el_type g_type = inst.vectype.el[i].type;
13007           unsigned g_size = inst.vectype.el[i].size;
13008
13009           /* Decay more-specific signed & unsigned types to sign-insensitive
13010              integer types if sign-specific variants are unavailable.  */
13011           if ((g_type == NT_signed || g_type == NT_unsigned)
13012               && (types_allowed & N_SU_ALL) == 0)
13013             g_type = NT_integer;
13014
13015           /* If only untyped args are allowed, decay any more specific types to
13016              them. Some instructions only care about signs for some element
13017              sizes, so handle that properly.  */
13018           if (((types_allowed & N_UNT) == 0)
13019               && ((g_size == 8 && (types_allowed & N_8) != 0)
13020                   || (g_size == 16 && (types_allowed & N_16) != 0)
13021                   || (g_size == 32 && (types_allowed & N_32) != 0)
13022                   || (g_size == 64 && (types_allowed & N_64) != 0)))
13023             g_type = NT_untyped;
13024
13025           if (pass == 0)
13026             {
13027               if ((thisarg & N_KEY) != 0)
13028                 {
13029                   k_type = g_type;
13030                   k_size = g_size;
13031                   key_allowed = thisarg & ~N_KEY;
13032                 }
13033             }
13034           else
13035             {
13036               if ((thisarg & N_VFP) != 0)
13037                 {
13038                   enum neon_shape_el regshape;
13039                   unsigned regwidth, match;
13040
13041                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
13042                   if (ns == NS_NULL)
13043                     {
13044                       first_error (_("invalid instruction shape"));
13045                       return badtype;
13046                     }
13047                   regshape = neon_shape_tab[ns].el[i];
13048                   regwidth = neon_shape_el_size[regshape];
13049
13050                   /* In VFP mode, operands must match register widths. If we
13051                      have a key operand, use its width, else use the width of
13052                      the current operand.  */
13053                   if (k_size != -1u)
13054                     match = k_size;
13055                   else
13056                     match = g_size;
13057
13058                   if (regwidth != match)
13059                     {
13060                       first_error (_("operand size must match register width"));
13061                       return badtype;
13062                     }
13063                 }
13064
13065               if ((thisarg & N_EQK) == 0)
13066                 {
13067                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
13068
13069                   if ((given_type & types_allowed) == 0)
13070                     {
13071                       first_error (_("bad type in Neon instruction"));
13072                       return badtype;
13073                     }
13074                 }
13075               else
13076                 {
13077                   enum neon_el_type mod_k_type = k_type;
13078                   unsigned mod_k_size = k_size;
13079                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
13080                   if (g_type != mod_k_type || g_size != mod_k_size)
13081                     {
13082                       first_error (_("inconsistent types in Neon instruction"));
13083                       return badtype;
13084                     }
13085                 }
13086             }
13087         }
13088     }
13089
13090   return inst.vectype.el[key_el];
13091 }
13092
13093 /* Neon-style VFP instruction forwarding.  */
13094
13095 /* Thumb VFP instructions have 0xE in the condition field.  */
13096
13097 static void
13098 do_vfp_cond_or_thumb (void)
13099 {
13100   inst.is_neon = 1;
13101
13102   if (thumb_mode)
13103     inst.instruction |= 0xe0000000;
13104   else
13105     inst.instruction |= inst.cond << 28;
13106 }
13107
13108 /* Look up and encode a simple mnemonic, for use as a helper function for the
13109    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
13110    etc.  It is assumed that operand parsing has already been done, and that the
13111    operands are in the form expected by the given opcode (this isn't necessarily
13112    the same as the form in which they were parsed, hence some massaging must
13113    take place before this function is called).
13114    Checks current arch version against that in the looked-up opcode.  */
13115
13116 static void
13117 do_vfp_nsyn_opcode (const char *opname)
13118 {
13119   const struct asm_opcode *opcode;
13120
13121   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
13122
13123   if (!opcode)
13124     abort ();
13125
13126   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
13127                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
13128               _(BAD_FPU));
13129
13130   inst.is_neon = 1;
13131
13132   if (thumb_mode)
13133     {
13134       inst.instruction = opcode->tvalue;
13135       opcode->tencode ();
13136     }
13137   else
13138     {
13139       inst.instruction = (inst.cond << 28) | opcode->avalue;
13140       opcode->aencode ();
13141     }
13142 }
13143
13144 static void
13145 do_vfp_nsyn_add_sub (enum neon_shape rs)
13146 {
13147   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
13148
13149   if (rs == NS_FFF)
13150     {
13151       if (is_add)
13152         do_vfp_nsyn_opcode ("fadds");
13153       else
13154         do_vfp_nsyn_opcode ("fsubs");
13155     }
13156   else
13157     {
13158       if (is_add)
13159         do_vfp_nsyn_opcode ("faddd");
13160       else
13161         do_vfp_nsyn_opcode ("fsubd");
13162     }
13163 }
13164
13165 /* Check operand types to see if this is a VFP instruction, and if so call
13166    PFN ().  */
13167
13168 static int
13169 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
13170 {
13171   enum neon_shape rs;
13172   struct neon_type_el et;
13173
13174   switch (args)
13175     {
13176     case 2:
13177       rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
13178       et = neon_check_type (2, rs,
13179         N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13180       break;
13181
13182     case 3:
13183       rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
13184       et = neon_check_type (3, rs,
13185         N_EQK | N_VFP, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13186       break;
13187
13188     default:
13189       abort ();
13190     }
13191
13192   if (et.type != NT_invtype)
13193     {
13194       pfn (rs);
13195       return SUCCESS;
13196     }
13197
13198   inst.error = NULL;
13199   return FAIL;
13200 }
13201
13202 static void
13203 do_vfp_nsyn_mla_mls (enum neon_shape rs)
13204 {
13205   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
13206
13207   if (rs == NS_FFF)
13208     {
13209       if (is_mla)
13210         do_vfp_nsyn_opcode ("fmacs");
13211       else
13212         do_vfp_nsyn_opcode ("fnmacs");
13213     }
13214   else
13215     {
13216       if (is_mla)
13217         do_vfp_nsyn_opcode ("fmacd");
13218       else
13219         do_vfp_nsyn_opcode ("fnmacd");
13220     }
13221 }
13222
13223 static void
13224 do_vfp_nsyn_fma_fms (enum neon_shape rs)
13225 {
13226   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
13227
13228   if (rs == NS_FFF)
13229     {
13230       if (is_fma)
13231         do_vfp_nsyn_opcode ("ffmas");
13232       else
13233         do_vfp_nsyn_opcode ("ffnmas");
13234     }
13235   else
13236     {
13237       if (is_fma)
13238         do_vfp_nsyn_opcode ("ffmad");
13239       else
13240         do_vfp_nsyn_opcode ("ffnmad");
13241     }
13242 }
13243
13244 static void
13245 do_vfp_nsyn_mul (enum neon_shape rs)
13246 {
13247   if (rs == NS_FFF)
13248     do_vfp_nsyn_opcode ("fmuls");
13249   else
13250     do_vfp_nsyn_opcode ("fmuld");
13251 }
13252
13253 static void
13254 do_vfp_nsyn_abs_neg (enum neon_shape rs)
13255 {
13256   int is_neg = (inst.instruction & 0x80) != 0;
13257   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_VFP | N_KEY);
13258
13259   if (rs == NS_FF)
13260     {
13261       if (is_neg)
13262         do_vfp_nsyn_opcode ("fnegs");
13263       else
13264         do_vfp_nsyn_opcode ("fabss");
13265     }
13266   else
13267     {
13268       if (is_neg)
13269         do_vfp_nsyn_opcode ("fnegd");
13270       else
13271         do_vfp_nsyn_opcode ("fabsd");
13272     }
13273 }
13274
13275 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
13276    insns belong to Neon, and are handled elsewhere.  */
13277
13278 static void
13279 do_vfp_nsyn_ldm_stm (int is_dbmode)
13280 {
13281   int is_ldm = (inst.instruction & (1 << 20)) != 0;
13282   if (is_ldm)
13283     {
13284       if (is_dbmode)
13285         do_vfp_nsyn_opcode ("fldmdbs");
13286       else
13287         do_vfp_nsyn_opcode ("fldmias");
13288     }
13289   else
13290     {
13291       if (is_dbmode)
13292         do_vfp_nsyn_opcode ("fstmdbs");
13293       else
13294         do_vfp_nsyn_opcode ("fstmias");
13295     }
13296 }
13297
13298 static void
13299 do_vfp_nsyn_sqrt (void)
13300 {
13301   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
13302   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13303
13304   if (rs == NS_FF)
13305     do_vfp_nsyn_opcode ("fsqrts");
13306   else
13307     do_vfp_nsyn_opcode ("fsqrtd");
13308 }
13309
13310 static void
13311 do_vfp_nsyn_div (void)
13312 {
13313   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
13314   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
13315     N_F32 | N_F64 | N_KEY | N_VFP);
13316
13317   if (rs == NS_FFF)
13318     do_vfp_nsyn_opcode ("fdivs");
13319   else
13320     do_vfp_nsyn_opcode ("fdivd");
13321 }
13322
13323 static void
13324 do_vfp_nsyn_nmul (void)
13325 {
13326   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
13327   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
13328     N_F32 | N_F64 | N_KEY | N_VFP);
13329
13330   if (rs == NS_FFF)
13331     {
13332       NEON_ENCODE (SINGLE, inst);
13333       do_vfp_sp_dyadic ();
13334     }
13335   else
13336     {
13337       NEON_ENCODE (DOUBLE, inst);
13338       do_vfp_dp_rd_rn_rm ();
13339     }
13340   do_vfp_cond_or_thumb ();
13341 }
13342
13343 static void
13344 do_vfp_nsyn_cmp (void)
13345 {
13346   if (inst.operands[1].isreg)
13347     {
13348       enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
13349       neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13350
13351       if (rs == NS_FF)
13352         {
13353           NEON_ENCODE (SINGLE, inst);
13354           do_vfp_sp_monadic ();
13355         }
13356       else
13357         {
13358           NEON_ENCODE (DOUBLE, inst);
13359           do_vfp_dp_rd_rm ();
13360         }
13361     }
13362   else
13363     {
13364       enum neon_shape rs = neon_select_shape (NS_FI, NS_DI, NS_NULL);
13365       neon_check_type (2, rs, N_F32 | N_F64 | N_KEY | N_VFP, N_EQK);
13366
13367       switch (inst.instruction & 0x0fffffff)
13368         {
13369         case N_MNEM_vcmp:
13370           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
13371           break;
13372         case N_MNEM_vcmpe:
13373           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
13374           break;
13375         default:
13376           abort ();
13377         }
13378
13379       if (rs == NS_FI)
13380         {
13381           NEON_ENCODE (SINGLE, inst);
13382           do_vfp_sp_compare_z ();
13383         }
13384       else
13385         {
13386           NEON_ENCODE (DOUBLE, inst);
13387           do_vfp_dp_rd ();
13388         }
13389     }
13390   do_vfp_cond_or_thumb ();
13391 }
13392
13393 static void
13394 nsyn_insert_sp (void)
13395 {
13396   inst.operands[1] = inst.operands[0];
13397   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
13398   inst.operands[0].reg = REG_SP;
13399   inst.operands[0].isreg = 1;
13400   inst.operands[0].writeback = 1;
13401   inst.operands[0].present = 1;
13402 }
13403
13404 static void
13405 do_vfp_nsyn_push (void)
13406 {
13407   nsyn_insert_sp ();
13408   if (inst.operands[1].issingle)
13409     do_vfp_nsyn_opcode ("fstmdbs");
13410   else
13411     do_vfp_nsyn_opcode ("fstmdbd");
13412 }
13413
13414 static void
13415 do_vfp_nsyn_pop (void)
13416 {
13417   nsyn_insert_sp ();
13418   if (inst.operands[1].issingle)
13419     do_vfp_nsyn_opcode ("fldmias");
13420   else
13421     do_vfp_nsyn_opcode ("fldmiad");
13422 }
13423
13424 /* Fix up Neon data-processing instructions, ORing in the correct bits for
13425    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
13426
13427 static void
13428 neon_dp_fixup (struct arm_it* insn)
13429 {
13430   unsigned int i = insn->instruction;
13431   insn->is_neon = 1;
13432
13433   if (thumb_mode)
13434     {
13435       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
13436       if (i & (1 << 24))
13437         i |= 1 << 28;
13438
13439       i &= ~(1 << 24);
13440
13441       i |= 0xef000000;
13442     }
13443   else
13444     i |= 0xf2000000;
13445
13446   insn->instruction = i;
13447 }
13448
13449 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
13450    (0, 1, 2, 3).  */
13451
13452 static unsigned
13453 neon_logbits (unsigned x)
13454 {
13455   return ffs (x) - 4;
13456 }
13457
13458 #define LOW4(R) ((R) & 0xf)
13459 #define HI1(R) (((R) >> 4) & 1)
13460
13461 /* Encode insns with bit pattern:
13462
13463   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
13464   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
13465
13466   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
13467   different meaning for some instruction.  */
13468
13469 static void
13470 neon_three_same (int isquad, int ubit, int size)
13471 {
13472   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13473   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13474   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
13475   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
13476   inst.instruction |= LOW4 (inst.operands[2].reg);
13477   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
13478   inst.instruction |= (isquad != 0) << 6;
13479   inst.instruction |= (ubit != 0) << 24;
13480   if (size != -1)
13481     inst.instruction |= neon_logbits (size) << 20;
13482
13483   neon_dp_fixup (&inst);
13484 }
13485
13486 /* Encode instructions of the form:
13487
13488   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
13489   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
13490
13491   Don't write size if SIZE == -1.  */
13492
13493 static void
13494 neon_two_same (int qbit, int ubit, int size)
13495 {
13496   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13497   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13498   inst.instruction |= LOW4 (inst.operands[1].reg);
13499   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13500   inst.instruction |= (qbit != 0) << 6;
13501   inst.instruction |= (ubit != 0) << 24;
13502
13503   if (size != -1)
13504     inst.instruction |= neon_logbits (size) << 18;
13505
13506   neon_dp_fixup (&inst);
13507 }
13508
13509 /* Neon instruction encoders, in approximate order of appearance.  */
13510
13511 static void
13512 do_neon_dyadic_i_su (void)
13513 {
13514   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13515   struct neon_type_el et = neon_check_type (3, rs,
13516     N_EQK, N_EQK, N_SU_32 | N_KEY);
13517   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13518 }
13519
13520 static void
13521 do_neon_dyadic_i64_su (void)
13522 {
13523   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13524   struct neon_type_el et = neon_check_type (3, rs,
13525     N_EQK, N_EQK, N_SU_ALL | N_KEY);
13526   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13527 }
13528
13529 static void
13530 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
13531                 unsigned immbits)
13532 {
13533   unsigned size = et.size >> 3;
13534   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13535   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13536   inst.instruction |= LOW4 (inst.operands[1].reg);
13537   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13538   inst.instruction |= (isquad != 0) << 6;
13539   inst.instruction |= immbits << 16;
13540   inst.instruction |= (size >> 3) << 7;
13541   inst.instruction |= (size & 0x7) << 19;
13542   if (write_ubit)
13543     inst.instruction |= (uval != 0) << 24;
13544
13545   neon_dp_fixup (&inst);
13546 }
13547
13548 static void
13549 do_neon_shl_imm (void)
13550 {
13551   if (!inst.operands[2].isreg)
13552     {
13553       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13554       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
13555       NEON_ENCODE (IMMED, inst);
13556       neon_imm_shift (FALSE, 0, neon_quad (rs), et, inst.operands[2].imm);
13557     }
13558   else
13559     {
13560       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13561       struct neon_type_el et = neon_check_type (3, rs,
13562         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
13563       unsigned int tmp;
13564
13565       /* VSHL/VQSHL 3-register variants have syntax such as:
13566            vshl.xx Dd, Dm, Dn
13567          whereas other 3-register operations encoded by neon_three_same have
13568          syntax like:
13569            vadd.xx Dd, Dn, Dm
13570          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
13571          here.  */
13572       tmp = inst.operands[2].reg;
13573       inst.operands[2].reg = inst.operands[1].reg;
13574       inst.operands[1].reg = tmp;
13575       NEON_ENCODE (INTEGER, inst);
13576       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13577     }
13578 }
13579
13580 static void
13581 do_neon_qshl_imm (void)
13582 {
13583   if (!inst.operands[2].isreg)
13584     {
13585       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13586       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
13587
13588       NEON_ENCODE (IMMED, inst);
13589       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
13590                       inst.operands[2].imm);
13591     }
13592   else
13593     {
13594       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13595       struct neon_type_el et = neon_check_type (3, rs,
13596         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
13597       unsigned int tmp;
13598
13599       /* See note in do_neon_shl_imm.  */
13600       tmp = inst.operands[2].reg;
13601       inst.operands[2].reg = inst.operands[1].reg;
13602       inst.operands[1].reg = tmp;
13603       NEON_ENCODE (INTEGER, inst);
13604       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13605     }
13606 }
13607
13608 static void
13609 do_neon_rshl (void)
13610 {
13611   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13612   struct neon_type_el et = neon_check_type (3, rs,
13613     N_EQK, N_EQK, N_SU_ALL | N_KEY);
13614   unsigned int tmp;
13615
13616   tmp = inst.operands[2].reg;
13617   inst.operands[2].reg = inst.operands[1].reg;
13618   inst.operands[1].reg = tmp;
13619   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13620 }
13621
13622 static int
13623 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
13624 {
13625   /* Handle .I8 pseudo-instructions.  */
13626   if (size == 8)
13627     {
13628       /* Unfortunately, this will make everything apart from zero out-of-range.
13629          FIXME is this the intended semantics? There doesn't seem much point in
13630          accepting .I8 if so.  */
13631       immediate |= immediate << 8;
13632       size = 16;
13633     }
13634
13635   if (size >= 32)
13636     {
13637       if (immediate == (immediate & 0x000000ff))
13638         {
13639           *immbits = immediate;
13640           return 0x1;
13641         }
13642       else if (immediate == (immediate & 0x0000ff00))
13643         {
13644           *immbits = immediate >> 8;
13645           return 0x3;
13646         }
13647       else if (immediate == (immediate & 0x00ff0000))
13648         {
13649           *immbits = immediate >> 16;
13650           return 0x5;
13651         }
13652       else if (immediate == (immediate & 0xff000000))
13653         {
13654           *immbits = immediate >> 24;
13655           return 0x7;
13656         }
13657       if ((immediate & 0xffff) != (immediate >> 16))
13658         goto bad_immediate;
13659       immediate &= 0xffff;
13660     }
13661
13662   if (immediate == (immediate & 0x000000ff))
13663     {
13664       *immbits = immediate;
13665       return 0x9;
13666     }
13667   else if (immediate == (immediate & 0x0000ff00))
13668     {
13669       *immbits = immediate >> 8;
13670       return 0xb;
13671     }
13672
13673   bad_immediate:
13674   first_error (_("immediate value out of range"));
13675   return FAIL;
13676 }
13677
13678 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
13679    A, B, C, D.  */
13680
13681 static int
13682 neon_bits_same_in_bytes (unsigned imm)
13683 {
13684   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
13685          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
13686          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
13687          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
13688 }
13689
13690 /* For immediate of above form, return 0bABCD.  */
13691
13692 static unsigned
13693 neon_squash_bits (unsigned imm)
13694 {
13695   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
13696          | ((imm & 0x01000000) >> 21);
13697 }
13698
13699 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
13700
13701 static unsigned
13702 neon_qfloat_bits (unsigned imm)
13703 {
13704   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
13705 }
13706
13707 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
13708    the instruction. *OP is passed as the initial value of the op field, and
13709    may be set to a different value depending on the constant (i.e.
13710    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
13711    MVN).  If the immediate looks like a repeated pattern then also
13712    try smaller element sizes.  */
13713
13714 static int
13715 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
13716                          unsigned *immbits, int *op, int size,
13717                          enum neon_el_type type)
13718 {
13719   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
13720      float.  */
13721   if (type == NT_float && !float_p)
13722     return FAIL;
13723
13724   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
13725     {
13726       if (size != 32 || *op == 1)
13727         return FAIL;
13728       *immbits = neon_qfloat_bits (immlo);
13729       return 0xf;
13730     }
13731
13732   if (size == 64)
13733     {
13734       if (neon_bits_same_in_bytes (immhi)
13735           && neon_bits_same_in_bytes (immlo))
13736         {
13737           if (*op == 1)
13738             return FAIL;
13739           *immbits = (neon_squash_bits (immhi) << 4)
13740                      | neon_squash_bits (immlo);
13741           *op = 1;
13742           return 0xe;
13743         }
13744
13745       if (immhi != immlo)
13746         return FAIL;
13747     }
13748
13749   if (size >= 32)
13750     {
13751       if (immlo == (immlo & 0x000000ff))
13752         {
13753           *immbits = immlo;
13754           return 0x0;
13755         }
13756       else if (immlo == (immlo & 0x0000ff00))
13757         {
13758           *immbits = immlo >> 8;
13759           return 0x2;
13760         }
13761       else if (immlo == (immlo & 0x00ff0000))
13762         {
13763           *immbits = immlo >> 16;
13764           return 0x4;
13765         }
13766       else if (immlo == (immlo & 0xff000000))
13767         {
13768           *immbits = immlo >> 24;
13769           return 0x6;
13770         }
13771       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
13772         {
13773           *immbits = (immlo >> 8) & 0xff;
13774           return 0xc;
13775         }
13776       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
13777         {
13778           *immbits = (immlo >> 16) & 0xff;
13779           return 0xd;
13780         }
13781
13782       if ((immlo & 0xffff) != (immlo >> 16))
13783         return FAIL;
13784       immlo &= 0xffff;
13785     }
13786
13787   if (size >= 16)
13788     {
13789       if (immlo == (immlo & 0x000000ff))
13790         {
13791           *immbits = immlo;
13792           return 0x8;
13793         }
13794       else if (immlo == (immlo & 0x0000ff00))
13795         {
13796           *immbits = immlo >> 8;
13797           return 0xa;
13798         }
13799
13800       if ((immlo & 0xff) != (immlo >> 8))
13801         return FAIL;
13802       immlo &= 0xff;
13803     }
13804
13805   if (immlo == (immlo & 0x000000ff))
13806     {
13807       /* Don't allow MVN with 8-bit immediate.  */
13808       if (*op == 1)
13809         return FAIL;
13810       *immbits = immlo;
13811       return 0xe;
13812     }
13813
13814   return FAIL;
13815 }
13816
13817 /* Write immediate bits [7:0] to the following locations:
13818
13819   |28/24|23     19|18 16|15                    4|3     0|
13820   |  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|
13821
13822   This function is used by VMOV/VMVN/VORR/VBIC.  */
13823
13824 static void
13825 neon_write_immbits (unsigned immbits)
13826 {
13827   inst.instruction |= immbits & 0xf;
13828   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
13829   inst.instruction |= ((immbits >> 7) & 0x1) << 24;
13830 }
13831
13832 /* Invert low-order SIZE bits of XHI:XLO.  */
13833
13834 static void
13835 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
13836 {
13837   unsigned immlo = xlo ? *xlo : 0;
13838   unsigned immhi = xhi ? *xhi : 0;
13839
13840   switch (size)
13841     {
13842     case 8:
13843       immlo = (~immlo) & 0xff;
13844       break;
13845
13846     case 16:
13847       immlo = (~immlo) & 0xffff;
13848       break;
13849
13850     case 64:
13851       immhi = (~immhi) & 0xffffffff;
13852       /* fall through.  */
13853
13854     case 32:
13855       immlo = (~immlo) & 0xffffffff;
13856       break;
13857
13858     default:
13859       abort ();
13860     }
13861
13862   if (xlo)
13863     *xlo = immlo;
13864
13865   if (xhi)
13866     *xhi = immhi;
13867 }
13868
13869 static void
13870 do_neon_logic (void)
13871 {
13872   if (inst.operands[2].present && inst.operands[2].isreg)
13873     {
13874       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13875       neon_check_type (3, rs, N_IGNORE_TYPE);
13876       /* U bit and size field were set as part of the bitmask.  */
13877       NEON_ENCODE (INTEGER, inst);
13878       neon_three_same (neon_quad (rs), 0, -1);
13879     }
13880   else
13881     {
13882       const int three_ops_form = (inst.operands[2].present
13883                                   && !inst.operands[2].isreg);
13884       const int immoperand = (three_ops_form ? 2 : 1);
13885       enum neon_shape rs = (three_ops_form
13886                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
13887                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
13888       struct neon_type_el et = neon_check_type (2, rs,
13889         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
13890       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
13891       unsigned immbits;
13892       int cmode;
13893
13894       if (et.type == NT_invtype)
13895         return;
13896
13897       if (three_ops_form)
13898         constraint (inst.operands[0].reg != inst.operands[1].reg,
13899                     _("first and second operands shall be the same register"));
13900
13901       NEON_ENCODE (IMMED, inst);
13902
13903       immbits = inst.operands[immoperand].imm;
13904       if (et.size == 64)
13905         {
13906           /* .i64 is a pseudo-op, so the immediate must be a repeating
13907              pattern.  */
13908           if (immbits != (inst.operands[immoperand].regisimm ?
13909                           inst.operands[immoperand].reg : 0))
13910             {
13911               /* Set immbits to an invalid constant.  */
13912               immbits = 0xdeadbeef;
13913             }
13914         }
13915
13916       switch (opcode)
13917         {
13918         case N_MNEM_vbic:
13919           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13920           break;
13921
13922         case N_MNEM_vorr:
13923           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13924           break;
13925
13926         case N_MNEM_vand:
13927           /* Pseudo-instruction for VBIC.  */
13928           neon_invert_size (&immbits, 0, et.size);
13929           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13930           break;
13931
13932         case N_MNEM_vorn:
13933           /* Pseudo-instruction for VORR.  */
13934           neon_invert_size (&immbits, 0, et.size);
13935           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13936           break;
13937
13938         default:
13939           abort ();
13940         }
13941
13942       if (cmode == FAIL)
13943         return;
13944
13945       inst.instruction |= neon_quad (rs) << 6;
13946       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13947       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13948       inst.instruction |= cmode << 8;
13949       neon_write_immbits (immbits);
13950
13951       neon_dp_fixup (&inst);
13952     }
13953 }
13954
13955 static void
13956 do_neon_bitfield (void)
13957 {
13958   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13959   neon_check_type (3, rs, N_IGNORE_TYPE);
13960   neon_three_same (neon_quad (rs), 0, -1);
13961 }
13962
13963 static void
13964 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
13965                   unsigned destbits)
13966 {
13967   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13968   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
13969                                             types | N_KEY);
13970   if (et.type == NT_float)
13971     {
13972       NEON_ENCODE (FLOAT, inst);
13973       neon_three_same (neon_quad (rs), 0, -1);
13974     }
13975   else
13976     {
13977       NEON_ENCODE (INTEGER, inst);
13978       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
13979     }
13980 }
13981
13982 static void
13983 do_neon_dyadic_if_su (void)
13984 {
13985   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
13986 }
13987
13988 static void
13989 do_neon_dyadic_if_su_d (void)
13990 {
13991   /* This version only allow D registers, but that constraint is enforced during
13992      operand parsing so we don't need to do anything extra here.  */
13993   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
13994 }
13995
13996 static void
13997 do_neon_dyadic_if_i_d (void)
13998 {
13999   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14000      affected if we specify unsigned args.  */
14001   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14002 }
14003
14004 enum vfp_or_neon_is_neon_bits
14005 {
14006   NEON_CHECK_CC = 1,
14007   NEON_CHECK_ARCH = 2,
14008   NEON_CHECK_ARCH8 = 4
14009 };
14010
14011 /* Call this function if an instruction which may have belonged to the VFP or
14012    Neon instruction sets, but turned out to be a Neon instruction (due to the
14013    operand types involved, etc.). We have to check and/or fix-up a couple of
14014    things:
14015
14016      - Make sure the user hasn't attempted to make a Neon instruction
14017        conditional.
14018      - Alter the value in the condition code field if necessary.
14019      - Make sure that the arch supports Neon instructions.
14020
14021    Which of these operations take place depends on bits from enum
14022    vfp_or_neon_is_neon_bits.
14023
14024    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
14025    current instruction's condition is COND_ALWAYS, the condition field is
14026    changed to inst.uncond_value. This is necessary because instructions shared
14027    between VFP and Neon may be conditional for the VFP variants only, and the
14028    unconditional Neon version must have, e.g., 0xF in the condition field.  */
14029
14030 static int
14031 vfp_or_neon_is_neon (unsigned check)
14032 {
14033   /* Conditions are always legal in Thumb mode (IT blocks).  */
14034   if (!thumb_mode && (check & NEON_CHECK_CC))
14035     {
14036       if (inst.cond != COND_ALWAYS)
14037         {
14038           first_error (_(BAD_COND));
14039           return FAIL;
14040         }
14041       if (inst.uncond_value != -1)
14042         inst.instruction |= inst.uncond_value << 28;
14043     }
14044
14045   if ((check & NEON_CHECK_ARCH)
14046       && !mark_feature_used (&fpu_neon_ext_v1))
14047     {
14048       first_error (_(BAD_FPU));
14049       return FAIL;
14050     }
14051
14052   if ((check & NEON_CHECK_ARCH8)
14053       && !mark_feature_used (&fpu_neon_ext_armv8))
14054     {
14055       first_error (_(BAD_FPU));
14056       return FAIL;
14057     }
14058
14059   return SUCCESS;
14060 }
14061
14062 static void
14063 do_neon_addsub_if_i (void)
14064 {
14065   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
14066     return;
14067
14068   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14069     return;
14070
14071   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14072      affected if we specify unsigned args.  */
14073   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
14074 }
14075
14076 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
14077    result to be:
14078      V<op> A,B     (A is operand 0, B is operand 2)
14079    to mean:
14080      V<op> A,B,A
14081    not:
14082      V<op> A,B,B
14083    so handle that case specially.  */
14084
14085 static void
14086 neon_exchange_operands (void)
14087 {
14088   void *scratch = alloca (sizeof (inst.operands[0]));
14089   if (inst.operands[1].present)
14090     {
14091       /* Swap operands[1] and operands[2].  */
14092       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
14093       inst.operands[1] = inst.operands[2];
14094       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
14095     }
14096   else
14097     {
14098       inst.operands[1] = inst.operands[2];
14099       inst.operands[2] = inst.operands[0];
14100     }
14101 }
14102
14103 static void
14104 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
14105 {
14106   if (inst.operands[2].isreg)
14107     {
14108       if (invert)
14109         neon_exchange_operands ();
14110       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
14111     }
14112   else
14113     {
14114       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14115       struct neon_type_el et = neon_check_type (2, rs,
14116         N_EQK | N_SIZ, immtypes | N_KEY);
14117
14118       NEON_ENCODE (IMMED, inst);
14119       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14120       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14121       inst.instruction |= LOW4 (inst.operands[1].reg);
14122       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14123       inst.instruction |= neon_quad (rs) << 6;
14124       inst.instruction |= (et.type == NT_float) << 10;
14125       inst.instruction |= neon_logbits (et.size) << 18;
14126
14127       neon_dp_fixup (&inst);
14128     }
14129 }
14130
14131 static void
14132 do_neon_cmp (void)
14133 {
14134   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, FALSE);
14135 }
14136
14137 static void
14138 do_neon_cmp_inv (void)
14139 {
14140   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, TRUE);
14141 }
14142
14143 static void
14144 do_neon_ceq (void)
14145 {
14146   neon_compare (N_IF_32, N_IF_32, FALSE);
14147 }
14148
14149 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
14150    scalars, which are encoded in 5 bits, M : Rm.
14151    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
14152    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
14153    index in M.  */
14154
14155 static unsigned
14156 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
14157 {
14158   unsigned regno = NEON_SCALAR_REG (scalar);
14159   unsigned elno = NEON_SCALAR_INDEX (scalar);
14160
14161   switch (elsize)
14162     {
14163     case 16:
14164       if (regno > 7 || elno > 3)
14165         goto bad_scalar;
14166       return regno | (elno << 3);
14167
14168     case 32:
14169       if (regno > 15 || elno > 1)
14170         goto bad_scalar;
14171       return regno | (elno << 4);
14172
14173     default:
14174     bad_scalar:
14175       first_error (_("scalar out of range for multiply instruction"));
14176     }
14177
14178   return 0;
14179 }
14180
14181 /* Encode multiply / multiply-accumulate scalar instructions.  */
14182
14183 static void
14184 neon_mul_mac (struct neon_type_el et, int ubit)
14185 {
14186   unsigned scalar;
14187
14188   /* Give a more helpful error message if we have an invalid type.  */
14189   if (et.type == NT_invtype)
14190     return;
14191
14192   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
14193   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14194   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14195   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14196   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14197   inst.instruction |= LOW4 (scalar);
14198   inst.instruction |= HI1 (scalar) << 5;
14199   inst.instruction |= (et.type == NT_float) << 8;
14200   inst.instruction |= neon_logbits (et.size) << 20;
14201   inst.instruction |= (ubit != 0) << 24;
14202
14203   neon_dp_fixup (&inst);
14204 }
14205
14206 static void
14207 do_neon_mac_maybe_scalar (void)
14208 {
14209   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
14210     return;
14211
14212   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14213     return;
14214
14215   if (inst.operands[2].isscalar)
14216     {
14217       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
14218       struct neon_type_el et = neon_check_type (3, rs,
14219         N_EQK, N_EQK, N_I16 | N_I32 | N_F32 | N_KEY);
14220       NEON_ENCODE (SCALAR, inst);
14221       neon_mul_mac (et, neon_quad (rs));
14222     }
14223   else
14224     {
14225       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
14226          affected if we specify unsigned args.  */
14227       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14228     }
14229 }
14230
14231 static void
14232 do_neon_fmac (void)
14233 {
14234   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
14235     return;
14236
14237   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14238     return;
14239
14240   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14241 }
14242
14243 static void
14244 do_neon_tst (void)
14245 {
14246   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14247   struct neon_type_el et = neon_check_type (3, rs,
14248     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
14249   neon_three_same (neon_quad (rs), 0, et.size);
14250 }
14251
14252 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
14253    same types as the MAC equivalents. The polynomial type for this instruction
14254    is encoded the same as the integer type.  */
14255
14256 static void
14257 do_neon_mul (void)
14258 {
14259   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
14260     return;
14261
14262   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14263     return;
14264
14265   if (inst.operands[2].isscalar)
14266     do_neon_mac_maybe_scalar ();
14267   else
14268     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F32 | N_P8, 0);
14269 }
14270
14271 static void
14272 do_neon_qdmulh (void)
14273 {
14274   if (inst.operands[2].isscalar)
14275     {
14276       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
14277       struct neon_type_el et = neon_check_type (3, rs,
14278         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
14279       NEON_ENCODE (SCALAR, inst);
14280       neon_mul_mac (et, neon_quad (rs));
14281     }
14282   else
14283     {
14284       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14285       struct neon_type_el et = neon_check_type (3, rs,
14286         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
14287       NEON_ENCODE (INTEGER, inst);
14288       /* The U bit (rounding) comes from bit mask.  */
14289       neon_three_same (neon_quad (rs), 0, et.size);
14290     }
14291 }
14292
14293 static void
14294 do_neon_fcmp_absolute (void)
14295 {
14296   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14297   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
14298   /* Size field comes from bit mask.  */
14299   neon_three_same (neon_quad (rs), 1, -1);
14300 }
14301
14302 static void
14303 do_neon_fcmp_absolute_inv (void)
14304 {
14305   neon_exchange_operands ();
14306   do_neon_fcmp_absolute ();
14307 }
14308
14309 static void
14310 do_neon_step (void)
14311 {
14312   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14313   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
14314   neon_three_same (neon_quad (rs), 0, -1);
14315 }
14316
14317 static void
14318 do_neon_abs_neg (void)
14319 {
14320   enum neon_shape rs;
14321   struct neon_type_el et;
14322
14323   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
14324     return;
14325
14326   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14327     return;
14328
14329   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14330   et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_F32 | N_KEY);
14331
14332   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14333   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14334   inst.instruction |= LOW4 (inst.operands[1].reg);
14335   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14336   inst.instruction |= neon_quad (rs) << 6;
14337   inst.instruction |= (et.type == NT_float) << 10;
14338   inst.instruction |= neon_logbits (et.size) << 18;
14339
14340   neon_dp_fixup (&inst);
14341 }
14342
14343 static void
14344 do_neon_sli (void)
14345 {
14346   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14347   struct neon_type_el et = neon_check_type (2, rs,
14348     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14349   int imm = inst.operands[2].imm;
14350   constraint (imm < 0 || (unsigned)imm >= et.size,
14351               _("immediate out of range for insert"));
14352   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14353 }
14354
14355 static void
14356 do_neon_sri (void)
14357 {
14358   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14359   struct neon_type_el et = neon_check_type (2, rs,
14360     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14361   int imm = inst.operands[2].imm;
14362   constraint (imm < 1 || (unsigned)imm > et.size,
14363               _("immediate out of range for insert"));
14364   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
14365 }
14366
14367 static void
14368 do_neon_qshlu_imm (void)
14369 {
14370   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14371   struct neon_type_el et = neon_check_type (2, rs,
14372     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
14373   int imm = inst.operands[2].imm;
14374   constraint (imm < 0 || (unsigned)imm >= et.size,
14375               _("immediate out of range for shift"));
14376   /* Only encodes the 'U present' variant of the instruction.
14377      In this case, signed types have OP (bit 8) set to 0.
14378      Unsigned types have OP set to 1.  */
14379   inst.instruction |= (et.type == NT_unsigned) << 8;
14380   /* The rest of the bits are the same as other immediate shifts.  */
14381   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14382 }
14383
14384 static void
14385 do_neon_qmovn (void)
14386 {
14387   struct neon_type_el et = neon_check_type (2, NS_DQ,
14388     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14389   /* Saturating move where operands can be signed or unsigned, and the
14390      destination has the same signedness.  */
14391   NEON_ENCODE (INTEGER, inst);
14392   if (et.type == NT_unsigned)
14393     inst.instruction |= 0xc0;
14394   else
14395     inst.instruction |= 0x80;
14396   neon_two_same (0, 1, et.size / 2);
14397 }
14398
14399 static void
14400 do_neon_qmovun (void)
14401 {
14402   struct neon_type_el et = neon_check_type (2, NS_DQ,
14403     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
14404   /* Saturating move with unsigned results. Operands must be signed.  */
14405   NEON_ENCODE (INTEGER, inst);
14406   neon_two_same (0, 1, et.size / 2);
14407 }
14408
14409 static void
14410 do_neon_rshift_sat_narrow (void)
14411 {
14412   /* FIXME: Types for narrowing. If operands are signed, results can be signed
14413      or unsigned. If operands are unsigned, results must also be unsigned.  */
14414   struct neon_type_el et = neon_check_type (2, NS_DQI,
14415     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14416   int imm = inst.operands[2].imm;
14417   /* This gets the bounds check, size encoding and immediate bits calculation
14418      right.  */
14419   et.size /= 2;
14420
14421   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
14422      VQMOVN.I<size> <Dd>, <Qm>.  */
14423   if (imm == 0)
14424     {
14425       inst.operands[2].present = 0;
14426       inst.instruction = N_MNEM_vqmovn;
14427       do_neon_qmovn ();
14428       return;
14429     }
14430
14431   constraint (imm < 1 || (unsigned)imm > et.size,
14432               _("immediate out of range"));
14433   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
14434 }
14435
14436 static void
14437 do_neon_rshift_sat_narrow_u (void)
14438 {
14439   /* FIXME: Types for narrowing. If operands are signed, results can be signed
14440      or unsigned. If operands are unsigned, results must also be unsigned.  */
14441   struct neon_type_el et = neon_check_type (2, NS_DQI,
14442     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
14443   int imm = inst.operands[2].imm;
14444   /* This gets the bounds check, size encoding and immediate bits calculation
14445      right.  */
14446   et.size /= 2;
14447
14448   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
14449      VQMOVUN.I<size> <Dd>, <Qm>.  */
14450   if (imm == 0)
14451     {
14452       inst.operands[2].present = 0;
14453       inst.instruction = N_MNEM_vqmovun;
14454       do_neon_qmovun ();
14455       return;
14456     }
14457
14458   constraint (imm < 1 || (unsigned)imm > et.size,
14459               _("immediate out of range"));
14460   /* FIXME: The manual is kind of unclear about what value U should have in
14461      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
14462      must be 1.  */
14463   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
14464 }
14465
14466 static void
14467 do_neon_movn (void)
14468 {
14469   struct neon_type_el et = neon_check_type (2, NS_DQ,
14470     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14471   NEON_ENCODE (INTEGER, inst);
14472   neon_two_same (0, 1, et.size / 2);
14473 }
14474
14475 static void
14476 do_neon_rshift_narrow (void)
14477 {
14478   struct neon_type_el et = neon_check_type (2, NS_DQI,
14479     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14480   int imm = inst.operands[2].imm;
14481   /* This gets the bounds check, size encoding and immediate bits calculation
14482      right.  */
14483   et.size /= 2;
14484
14485   /* If immediate is zero then we are a pseudo-instruction for
14486      VMOVN.I<size> <Dd>, <Qm>  */
14487   if (imm == 0)
14488     {
14489       inst.operands[2].present = 0;
14490       inst.instruction = N_MNEM_vmovn;
14491       do_neon_movn ();
14492       return;
14493     }
14494
14495   constraint (imm < 1 || (unsigned)imm > et.size,
14496               _("immediate out of range for narrowing operation"));
14497   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
14498 }
14499
14500 static void
14501 do_neon_shll (void)
14502 {
14503   /* FIXME: Type checking when lengthening.  */
14504   struct neon_type_el et = neon_check_type (2, NS_QDI,
14505     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
14506   unsigned imm = inst.operands[2].imm;
14507
14508   if (imm == et.size)
14509     {
14510       /* Maximum shift variant.  */
14511       NEON_ENCODE (INTEGER, inst);
14512       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14513       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14514       inst.instruction |= LOW4 (inst.operands[1].reg);
14515       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14516       inst.instruction |= neon_logbits (et.size) << 18;
14517
14518       neon_dp_fixup (&inst);
14519     }
14520   else
14521     {
14522       /* A more-specific type check for non-max versions.  */
14523       et = neon_check_type (2, NS_QDI,
14524         N_EQK | N_DBL, N_SU_32 | N_KEY);
14525       NEON_ENCODE (IMMED, inst);
14526       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
14527     }
14528 }
14529
14530 /* Check the various types for the VCVT instruction, and return which version
14531    the current instruction is.  */
14532
14533 #define CVT_FLAVOUR_VAR                                                       \
14534   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
14535   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
14536   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
14537   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
14538   /* Half-precision conversions.  */                                          \
14539   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
14540   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
14541   /* VFP instructions.  */                                                    \
14542   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
14543   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
14544   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
14545   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
14546   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
14547   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
14548   /* VFP instructions with bitshift.  */                                      \
14549   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
14550   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
14551   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
14552   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
14553   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
14554   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
14555   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
14556   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
14557
14558 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
14559   neon_cvt_flavour_##C,
14560
14561 /* The different types of conversions we can do.  */
14562 enum neon_cvt_flavour
14563 {
14564   CVT_FLAVOUR_VAR
14565   neon_cvt_flavour_invalid,
14566   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
14567 };
14568
14569 #undef CVT_VAR
14570
14571 static enum neon_cvt_flavour
14572 get_neon_cvt_flavour (enum neon_shape rs)
14573 {
14574 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
14575   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
14576   if (et.type != NT_invtype)                            \
14577     {                                                   \
14578       inst.error = NULL;                                \
14579       return (neon_cvt_flavour_##C);                    \
14580     }
14581
14582   struct neon_type_el et;
14583   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
14584                         || rs == NS_FF) ? N_VFP : 0;
14585   /* The instruction versions which take an immediate take one register
14586      argument, which is extended to the width of the full register. Thus the
14587      "source" and "destination" registers must have the same width.  Hack that
14588      here by making the size equal to the key (wider, in this case) operand.  */
14589   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
14590
14591   CVT_FLAVOUR_VAR;
14592
14593   return neon_cvt_flavour_invalid;
14594 #undef CVT_VAR
14595 }
14596
14597 enum neon_cvt_mode
14598 {
14599   neon_cvt_mode_a,
14600   neon_cvt_mode_n,
14601   neon_cvt_mode_p,
14602   neon_cvt_mode_m,
14603   neon_cvt_mode_z,
14604   neon_cvt_mode_x,
14605   neon_cvt_mode_r
14606 };
14607
14608 /* Neon-syntax VFP conversions.  */
14609
14610 static void
14611 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
14612 {
14613   const char *opname = 0;
14614
14615   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI)
14616     {
14617       /* Conversions with immediate bitshift.  */
14618       const char *enc[] =
14619         {
14620 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
14621           CVT_FLAVOUR_VAR
14622           NULL
14623 #undef CVT_VAR
14624         };
14625
14626       if (flavour < (int) ARRAY_SIZE (enc))
14627         {
14628           opname = enc[flavour];
14629           constraint (inst.operands[0].reg != inst.operands[1].reg,
14630                       _("operands 0 and 1 must be the same register"));
14631           inst.operands[1] = inst.operands[2];
14632           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
14633         }
14634     }
14635   else
14636     {
14637       /* Conversions without bitshift.  */
14638       const char *enc[] =
14639         {
14640 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
14641           CVT_FLAVOUR_VAR
14642           NULL
14643 #undef CVT_VAR
14644         };
14645
14646       if (flavour < (int) ARRAY_SIZE (enc))
14647         opname = enc[flavour];
14648     }
14649
14650   if (opname)
14651     do_vfp_nsyn_opcode (opname);
14652 }
14653
14654 static void
14655 do_vfp_nsyn_cvtz (void)
14656 {
14657   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_NULL);
14658   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
14659   const char *enc[] =
14660     {
14661 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
14662       CVT_FLAVOUR_VAR
14663       NULL
14664 #undef CVT_VAR
14665     };
14666
14667   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
14668     do_vfp_nsyn_opcode (enc[flavour]);
14669 }
14670
14671 static void
14672 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
14673                       enum neon_cvt_mode mode)
14674 {
14675   int sz, op;
14676   int rm;
14677
14678   set_it_insn_type (OUTSIDE_IT_INSN);
14679
14680   switch (flavour)
14681     {
14682     case neon_cvt_flavour_s32_f64:
14683       sz = 1;
14684       op = 0;
14685       break;
14686     case neon_cvt_flavour_s32_f32:
14687       sz = 0;
14688       op = 1;
14689       break;
14690     case neon_cvt_flavour_u32_f64:
14691       sz = 1;
14692       op = 0;
14693       break;
14694     case neon_cvt_flavour_u32_f32:
14695       sz = 0;
14696       op = 0;
14697       break;
14698     default:
14699       first_error (_("invalid instruction shape"));
14700       return;
14701     }
14702
14703   switch (mode)
14704     {
14705     case neon_cvt_mode_a: rm = 0; break;
14706     case neon_cvt_mode_n: rm = 1; break;
14707     case neon_cvt_mode_p: rm = 2; break;
14708     case neon_cvt_mode_m: rm = 3; break;
14709     default: first_error (_("invalid rounding mode")); return;
14710     }
14711
14712   NEON_ENCODE (FPV8, inst);
14713   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
14714   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
14715   inst.instruction |= sz << 8;
14716   inst.instruction |= op << 7;
14717   inst.instruction |= rm << 16;
14718   inst.instruction |= 0xf0000000;
14719   inst.is_neon = TRUE;
14720 }
14721
14722 static void
14723 do_neon_cvt_1 (enum neon_cvt_mode mode)
14724 {
14725   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
14726     NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ, NS_NULL);
14727   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
14728
14729   /* PR11109: Handle round-to-zero for VCVT conversions.  */
14730   if (mode == neon_cvt_mode_z
14731       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
14732       && (flavour == neon_cvt_flavour_s32_f32
14733           || flavour == neon_cvt_flavour_u32_f32
14734           || flavour == neon_cvt_flavour_s32_f64
14735           || flavour == neon_cvt_flavour_u32_f64)
14736       && (rs == NS_FD || rs == NS_FF))
14737     {
14738       do_vfp_nsyn_cvtz ();
14739       return;
14740     }
14741
14742   /* VFP rather than Neon conversions.  */
14743   if (flavour >= neon_cvt_flavour_first_fp)
14744     {
14745       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
14746         do_vfp_nsyn_cvt (rs, flavour);
14747       else
14748         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
14749
14750       return;
14751     }
14752
14753   switch (rs)
14754     {
14755     case NS_DDI:
14756     case NS_QQI:
14757       {
14758         unsigned immbits;
14759         unsigned enctab[] = { 0x0000100, 0x1000100, 0x0, 0x1000000 };
14760
14761         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14762           return;
14763
14764         /* Fixed-point conversion with #0 immediate is encoded as an
14765            integer conversion.  */
14766         if (inst.operands[2].present && inst.operands[2].imm == 0)
14767           goto int_encode;
14768        immbits = 32 - inst.operands[2].imm;
14769         NEON_ENCODE (IMMED, inst);
14770         if (flavour != neon_cvt_flavour_invalid)
14771           inst.instruction |= enctab[flavour];
14772         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14773         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14774         inst.instruction |= LOW4 (inst.operands[1].reg);
14775         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14776         inst.instruction |= neon_quad (rs) << 6;
14777         inst.instruction |= 1 << 21;
14778         inst.instruction |= immbits << 16;
14779
14780         neon_dp_fixup (&inst);
14781       }
14782       break;
14783
14784     case NS_DD:
14785     case NS_QQ:
14786       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
14787         {
14788           NEON_ENCODE (FLOAT, inst);
14789           set_it_insn_type (OUTSIDE_IT_INSN);
14790
14791           if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
14792             return;
14793
14794           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14795           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14796           inst.instruction |= LOW4 (inst.operands[1].reg);
14797           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14798           inst.instruction |= neon_quad (rs) << 6;
14799           inst.instruction |= (flavour == neon_cvt_flavour_u32_f32) << 7;
14800           inst.instruction |= mode << 8;
14801           if (thumb_mode)
14802             inst.instruction |= 0xfc000000;
14803           else
14804             inst.instruction |= 0xf0000000;
14805         }
14806       else
14807         {
14808     int_encode:
14809           {
14810             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080 };
14811
14812             NEON_ENCODE (INTEGER, inst);
14813
14814             if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14815               return;
14816
14817             if (flavour != neon_cvt_flavour_invalid)
14818               inst.instruction |= enctab[flavour];
14819
14820             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14821             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14822             inst.instruction |= LOW4 (inst.operands[1].reg);
14823             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14824             inst.instruction |= neon_quad (rs) << 6;
14825             inst.instruction |= 2 << 18;
14826
14827             neon_dp_fixup (&inst);
14828           }
14829         }
14830       break;
14831
14832     /* Half-precision conversions for Advanced SIMD -- neon.  */
14833     case NS_QD:
14834     case NS_DQ:
14835
14836       if ((rs == NS_DQ)
14837           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
14838           {
14839             as_bad (_("operand size must match register width"));
14840             break;
14841           }
14842
14843       if ((rs == NS_QD)
14844           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
14845           {
14846             as_bad (_("operand size must match register width"));
14847             break;
14848           }
14849
14850       if (rs == NS_DQ)
14851         inst.instruction = 0x3b60600;
14852       else
14853         inst.instruction = 0x3b60700;
14854
14855       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14856       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14857       inst.instruction |= LOW4 (inst.operands[1].reg);
14858       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14859       neon_dp_fixup (&inst);
14860       break;
14861
14862     default:
14863       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
14864       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
14865         do_vfp_nsyn_cvt (rs, flavour);
14866       else
14867         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
14868     }
14869 }
14870
14871 static void
14872 do_neon_cvtr (void)
14873 {
14874   do_neon_cvt_1 (neon_cvt_mode_x);
14875 }
14876
14877 static void
14878 do_neon_cvt (void)
14879 {
14880   do_neon_cvt_1 (neon_cvt_mode_z);
14881 }
14882
14883 static void
14884 do_neon_cvta (void)
14885 {
14886   do_neon_cvt_1 (neon_cvt_mode_a);
14887 }
14888
14889 static void
14890 do_neon_cvtn (void)
14891 {
14892   do_neon_cvt_1 (neon_cvt_mode_n);
14893 }
14894
14895 static void
14896 do_neon_cvtp (void)
14897 {
14898   do_neon_cvt_1 (neon_cvt_mode_p);
14899 }
14900
14901 static void
14902 do_neon_cvtm (void)
14903 {
14904   do_neon_cvt_1 (neon_cvt_mode_m);
14905 }
14906
14907 static void
14908 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
14909 {
14910   if (is_double)
14911     mark_feature_used (&fpu_vfp_ext_armv8);
14912
14913   encode_arm_vfp_reg (inst.operands[0].reg,
14914                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
14915   encode_arm_vfp_reg (inst.operands[1].reg,
14916                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
14917   inst.instruction |= to ? 0x10000 : 0;
14918   inst.instruction |= t ? 0x80 : 0;
14919   inst.instruction |= is_double ? 0x100 : 0;
14920   do_vfp_cond_or_thumb ();
14921 }
14922
14923 static void
14924 do_neon_cvttb_1 (bfd_boolean t)
14925 {
14926   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_DF, NS_NULL);
14927
14928   if (rs == NS_NULL)
14929     return;
14930   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
14931     {
14932       inst.error = NULL;
14933       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
14934     }
14935   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
14936     {
14937       inst.error = NULL;
14938       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
14939     }
14940   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
14941     {
14942       inst.error = NULL;
14943       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
14944     }
14945   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
14946     {
14947       inst.error = NULL;
14948       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
14949     }
14950   else
14951     return;
14952 }
14953
14954 static void
14955 do_neon_cvtb (void)
14956 {
14957   do_neon_cvttb_1 (FALSE);
14958 }
14959
14960
14961 static void
14962 do_neon_cvtt (void)
14963 {
14964   do_neon_cvttb_1 (TRUE);
14965 }
14966
14967 static void
14968 neon_move_immediate (void)
14969 {
14970   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
14971   struct neon_type_el et = neon_check_type (2, rs,
14972     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14973   unsigned immlo, immhi = 0, immbits;
14974   int op, cmode, float_p;
14975
14976   constraint (et.type == NT_invtype,
14977               _("operand size must be specified for immediate VMOV"));
14978
14979   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
14980   op = (inst.instruction & (1 << 5)) != 0;
14981
14982   immlo = inst.operands[1].imm;
14983   if (inst.operands[1].regisimm)
14984     immhi = inst.operands[1].reg;
14985
14986   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
14987               _("immediate has bits set outside the operand size"));
14988
14989   float_p = inst.operands[1].immisfloat;
14990
14991   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
14992                                         et.size, et.type)) == FAIL)
14993     {
14994       /* Invert relevant bits only.  */
14995       neon_invert_size (&immlo, &immhi, et.size);
14996       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
14997          with one or the other; those cases are caught by
14998          neon_cmode_for_move_imm.  */
14999       op = !op;
15000       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
15001                                             &op, et.size, et.type)) == FAIL)
15002         {
15003           first_error (_("immediate out of range"));
15004           return;
15005         }
15006     }
15007
15008   inst.instruction &= ~(1 << 5);
15009   inst.instruction |= op << 5;
15010
15011   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15012   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15013   inst.instruction |= neon_quad (rs) << 6;
15014   inst.instruction |= cmode << 8;
15015
15016   neon_write_immbits (immbits);
15017 }
15018
15019 static void
15020 do_neon_mvn (void)
15021 {
15022   if (inst.operands[1].isreg)
15023     {
15024       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15025
15026       NEON_ENCODE (INTEGER, inst);
15027       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15028       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15029       inst.instruction |= LOW4 (inst.operands[1].reg);
15030       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15031       inst.instruction |= neon_quad (rs) << 6;
15032     }
15033   else
15034     {
15035       NEON_ENCODE (IMMED, inst);
15036       neon_move_immediate ();
15037     }
15038
15039   neon_dp_fixup (&inst);
15040 }
15041
15042 /* Encode instructions of form:
15043
15044   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
15045   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
15046
15047 static void
15048 neon_mixed_length (struct neon_type_el et, unsigned size)
15049 {
15050   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15051   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15052   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15053   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15054   inst.instruction |= LOW4 (inst.operands[2].reg);
15055   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15056   inst.instruction |= (et.type == NT_unsigned) << 24;
15057   inst.instruction |= neon_logbits (size) << 20;
15058
15059   neon_dp_fixup (&inst);
15060 }
15061
15062 static void
15063 do_neon_dyadic_long (void)
15064 {
15065   /* FIXME: Type checking for lengthening op.  */
15066   struct neon_type_el et = neon_check_type (3, NS_QDD,
15067     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
15068   neon_mixed_length (et, et.size);
15069 }
15070
15071 static void
15072 do_neon_abal (void)
15073 {
15074   struct neon_type_el et = neon_check_type (3, NS_QDD,
15075     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
15076   neon_mixed_length (et, et.size);
15077 }
15078
15079 static void
15080 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
15081 {
15082   if (inst.operands[2].isscalar)
15083     {
15084       struct neon_type_el et = neon_check_type (3, NS_QDS,
15085         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
15086       NEON_ENCODE (SCALAR, inst);
15087       neon_mul_mac (et, et.type == NT_unsigned);
15088     }
15089   else
15090     {
15091       struct neon_type_el et = neon_check_type (3, NS_QDD,
15092         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
15093       NEON_ENCODE (INTEGER, inst);
15094       neon_mixed_length (et, et.size);
15095     }
15096 }
15097
15098 static void
15099 do_neon_mac_maybe_scalar_long (void)
15100 {
15101   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
15102 }
15103
15104 static void
15105 do_neon_dyadic_wide (void)
15106 {
15107   struct neon_type_el et = neon_check_type (3, NS_QQD,
15108     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
15109   neon_mixed_length (et, et.size);
15110 }
15111
15112 static void
15113 do_neon_dyadic_narrow (void)
15114 {
15115   struct neon_type_el et = neon_check_type (3, NS_QDD,
15116     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
15117   /* Operand sign is unimportant, and the U bit is part of the opcode,
15118      so force the operand type to integer.  */
15119   et.type = NT_integer;
15120   neon_mixed_length (et, et.size / 2);
15121 }
15122
15123 static void
15124 do_neon_mul_sat_scalar_long (void)
15125 {
15126   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
15127 }
15128
15129 static void
15130 do_neon_vmull (void)
15131 {
15132   if (inst.operands[2].isscalar)
15133     do_neon_mac_maybe_scalar_long ();
15134   else
15135     {
15136       struct neon_type_el et = neon_check_type (3, NS_QDD,
15137         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
15138
15139       if (et.type == NT_poly)
15140         NEON_ENCODE (POLY, inst);
15141       else
15142         NEON_ENCODE (INTEGER, inst);
15143
15144       /* For polynomial encoding the U bit must be zero, and the size must
15145          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
15146          obviously, as 0b10).  */
15147       if (et.size == 64)
15148         {
15149           /* Check we're on the correct architecture.  */
15150           if (!mark_feature_used (&fpu_crypto_ext_armv8))
15151             inst.error =
15152               _("Instruction form not available on this architecture.");
15153
15154           et.size = 32;
15155         }
15156
15157       neon_mixed_length (et, et.size);
15158     }
15159 }
15160
15161 static void
15162 do_neon_ext (void)
15163 {
15164   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
15165   struct neon_type_el et = neon_check_type (3, rs,
15166     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15167   unsigned imm = (inst.operands[3].imm * et.size) / 8;
15168
15169   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
15170               _("shift out of range"));
15171   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15172   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15173   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15174   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15175   inst.instruction |= LOW4 (inst.operands[2].reg);
15176   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15177   inst.instruction |= neon_quad (rs) << 6;
15178   inst.instruction |= imm << 8;
15179
15180   neon_dp_fixup (&inst);
15181 }
15182
15183 static void
15184 do_neon_rev (void)
15185 {
15186   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15187   struct neon_type_el et = neon_check_type (2, rs,
15188     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15189   unsigned op = (inst.instruction >> 7) & 3;
15190   /* N (width of reversed regions) is encoded as part of the bitmask. We
15191      extract it here to check the elements to be reversed are smaller.
15192      Otherwise we'd get a reserved instruction.  */
15193   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
15194   gas_assert (elsize != 0);
15195   constraint (et.size >= elsize,
15196               _("elements must be smaller than reversal region"));
15197   neon_two_same (neon_quad (rs), 1, et.size);
15198 }
15199
15200 static void
15201 do_neon_dup (void)
15202 {
15203   if (inst.operands[1].isscalar)
15204     {
15205       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
15206       struct neon_type_el et = neon_check_type (2, rs,
15207         N_EQK, N_8 | N_16 | N_32 | N_KEY);
15208       unsigned sizebits = et.size >> 3;
15209       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
15210       int logsize = neon_logbits (et.size);
15211       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
15212
15213       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
15214         return;
15215
15216       NEON_ENCODE (SCALAR, inst);
15217       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15218       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15219       inst.instruction |= LOW4 (dm);
15220       inst.instruction |= HI1 (dm) << 5;
15221       inst.instruction |= neon_quad (rs) << 6;
15222       inst.instruction |= x << 17;
15223       inst.instruction |= sizebits << 16;
15224
15225       neon_dp_fixup (&inst);
15226     }
15227   else
15228     {
15229       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
15230       struct neon_type_el et = neon_check_type (2, rs,
15231         N_8 | N_16 | N_32 | N_KEY, N_EQK);
15232       /* Duplicate ARM register to lanes of vector.  */
15233       NEON_ENCODE (ARMREG, inst);
15234       switch (et.size)
15235         {
15236         case 8:  inst.instruction |= 0x400000; break;
15237         case 16: inst.instruction |= 0x000020; break;
15238         case 32: inst.instruction |= 0x000000; break;
15239         default: break;
15240         }
15241       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
15242       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
15243       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
15244       inst.instruction |= neon_quad (rs) << 21;
15245       /* The encoding for this instruction is identical for the ARM and Thumb
15246          variants, except for the condition field.  */
15247       do_vfp_cond_or_thumb ();
15248     }
15249 }
15250
15251 /* VMOV has particularly many variations. It can be one of:
15252      0. VMOV<c><q> <Qd>, <Qm>
15253      1. VMOV<c><q> <Dd>, <Dm>
15254    (Register operations, which are VORR with Rm = Rn.)
15255      2. VMOV<c><q>.<dt> <Qd>, #<imm>
15256      3. VMOV<c><q>.<dt> <Dd>, #<imm>
15257    (Immediate loads.)
15258      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
15259    (ARM register to scalar.)
15260      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
15261    (Two ARM registers to vector.)
15262      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
15263    (Scalar to ARM register.)
15264      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
15265    (Vector to two ARM registers.)
15266      8. VMOV.F32 <Sd>, <Sm>
15267      9. VMOV.F64 <Dd>, <Dm>
15268    (VFP register moves.)
15269     10. VMOV.F32 <Sd>, #imm
15270     11. VMOV.F64 <Dd>, #imm
15271    (VFP float immediate load.)
15272     12. VMOV <Rd>, <Sm>
15273    (VFP single to ARM reg.)
15274     13. VMOV <Sd>, <Rm>
15275    (ARM reg to VFP single.)
15276     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
15277    (Two ARM regs to two VFP singles.)
15278     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
15279    (Two VFP singles to two ARM regs.)
15280
15281    These cases can be disambiguated using neon_select_shape, except cases 1/9
15282    and 3/11 which depend on the operand type too.
15283
15284    All the encoded bits are hardcoded by this function.
15285
15286    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
15287    Cases 5, 7 may be used with VFPv2 and above.
15288
15289    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
15290    can specify a type where it doesn't make sense to, and is ignored).  */
15291
15292 static void
15293 do_neon_mov (void)
15294 {
15295   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
15296     NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR, NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
15297     NS_NULL);
15298   struct neon_type_el et;
15299   const char *ldconst = 0;
15300
15301   switch (rs)
15302     {
15303     case NS_DD:  /* case 1/9.  */
15304       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
15305       /* It is not an error here if no type is given.  */
15306       inst.error = NULL;
15307       if (et.type == NT_float && et.size == 64)
15308         {
15309           do_vfp_nsyn_opcode ("fcpyd");
15310           break;
15311         }
15312       /* fall through.  */
15313
15314     case NS_QQ:  /* case 0/1.  */
15315       {
15316         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15317           return;
15318         /* The architecture manual I have doesn't explicitly state which
15319            value the U bit should have for register->register moves, but
15320            the equivalent VORR instruction has U = 0, so do that.  */
15321         inst.instruction = 0x0200110;
15322         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15323         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15324         inst.instruction |= LOW4 (inst.operands[1].reg);
15325         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15326         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15327         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15328         inst.instruction |= neon_quad (rs) << 6;
15329
15330         neon_dp_fixup (&inst);
15331       }
15332       break;
15333
15334     case NS_DI:  /* case 3/11.  */
15335       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
15336       inst.error = NULL;
15337       if (et.type == NT_float && et.size == 64)
15338         {
15339           /* case 11 (fconstd).  */
15340           ldconst = "fconstd";
15341           goto encode_fconstd;
15342         }
15343       /* fall through.  */
15344
15345     case NS_QI:  /* case 2/3.  */
15346       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15347         return;
15348       inst.instruction = 0x0800010;
15349       neon_move_immediate ();
15350       neon_dp_fixup (&inst);
15351       break;
15352
15353     case NS_SR:  /* case 4.  */
15354       {
15355         unsigned bcdebits = 0;
15356         int logsize;
15357         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
15358         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
15359
15360         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
15361         logsize = neon_logbits (et.size);
15362
15363         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
15364                     _(BAD_FPU));
15365         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
15366                     && et.size != 32, _(BAD_FPU));
15367         constraint (et.type == NT_invtype, _("bad type for scalar"));
15368         constraint (x >= 64 / et.size, _("scalar index out of range"));
15369
15370         switch (et.size)
15371           {
15372           case 8:  bcdebits = 0x8; break;
15373           case 16: bcdebits = 0x1; break;
15374           case 32: bcdebits = 0x0; break;
15375           default: ;
15376           }
15377
15378         bcdebits |= x << logsize;
15379
15380         inst.instruction = 0xe000b10;
15381         do_vfp_cond_or_thumb ();
15382         inst.instruction |= LOW4 (dn) << 16;
15383         inst.instruction |= HI1 (dn) << 7;
15384         inst.instruction |= inst.operands[1].reg << 12;
15385         inst.instruction |= (bcdebits & 3) << 5;
15386         inst.instruction |= (bcdebits >> 2) << 21;
15387       }
15388       break;
15389
15390     case NS_DRR:  /* case 5 (fmdrr).  */
15391       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
15392                   _(BAD_FPU));
15393
15394       inst.instruction = 0xc400b10;
15395       do_vfp_cond_or_thumb ();
15396       inst.instruction |= LOW4 (inst.operands[0].reg);
15397       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
15398       inst.instruction |= inst.operands[1].reg << 12;
15399       inst.instruction |= inst.operands[2].reg << 16;
15400       break;
15401
15402     case NS_RS:  /* case 6.  */
15403       {
15404         unsigned logsize;
15405         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
15406         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
15407         unsigned abcdebits = 0;
15408
15409         et = neon_check_type (2, NS_NULL,
15410                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
15411         logsize = neon_logbits (et.size);
15412
15413         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
15414                     _(BAD_FPU));
15415         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
15416                     && et.size != 32, _(BAD_FPU));
15417         constraint (et.type == NT_invtype, _("bad type for scalar"));
15418         constraint (x >= 64 / et.size, _("scalar index out of range"));
15419
15420         switch (et.size)
15421           {
15422           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
15423           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
15424           case 32: abcdebits = 0x00; break;
15425           default: ;
15426           }
15427
15428         abcdebits |= x << logsize;
15429         inst.instruction = 0xe100b10;
15430         do_vfp_cond_or_thumb ();
15431         inst.instruction |= LOW4 (dn) << 16;
15432         inst.instruction |= HI1 (dn) << 7;
15433         inst.instruction |= inst.operands[0].reg << 12;
15434         inst.instruction |= (abcdebits & 3) << 5;
15435         inst.instruction |= (abcdebits >> 2) << 21;
15436       }
15437       break;
15438
15439     case NS_RRD:  /* case 7 (fmrrd).  */
15440       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
15441                   _(BAD_FPU));
15442
15443       inst.instruction = 0xc500b10;
15444       do_vfp_cond_or_thumb ();
15445       inst.instruction |= inst.operands[0].reg << 12;
15446       inst.instruction |= inst.operands[1].reg << 16;
15447       inst.instruction |= LOW4 (inst.operands[2].reg);
15448       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15449       break;
15450
15451     case NS_FF:  /* case 8 (fcpys).  */
15452       do_vfp_nsyn_opcode ("fcpys");
15453       break;
15454
15455     case NS_FI:  /* case 10 (fconsts).  */
15456       ldconst = "fconsts";
15457       encode_fconstd:
15458       if (is_quarter_float (inst.operands[1].imm))
15459         {
15460           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
15461           do_vfp_nsyn_opcode (ldconst);
15462         }
15463       else
15464         first_error (_("immediate out of range"));
15465       break;
15466
15467     case NS_RF:  /* case 12 (fmrs).  */
15468       do_vfp_nsyn_opcode ("fmrs");
15469       break;
15470
15471     case NS_FR:  /* case 13 (fmsr).  */
15472       do_vfp_nsyn_opcode ("fmsr");
15473       break;
15474
15475     /* The encoders for the fmrrs and fmsrr instructions expect three operands
15476        (one of which is a list), but we have parsed four.  Do some fiddling to
15477        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
15478        expect.  */
15479     case NS_RRFF:  /* case 14 (fmrrs).  */
15480       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
15481                   _("VFP registers must be adjacent"));
15482       inst.operands[2].imm = 2;
15483       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
15484       do_vfp_nsyn_opcode ("fmrrs");
15485       break;
15486
15487     case NS_FFRR:  /* case 15 (fmsrr).  */
15488       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
15489                   _("VFP registers must be adjacent"));
15490       inst.operands[1] = inst.operands[2];
15491       inst.operands[2] = inst.operands[3];
15492       inst.operands[0].imm = 2;
15493       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
15494       do_vfp_nsyn_opcode ("fmsrr");
15495       break;
15496
15497     default:
15498       abort ();
15499     }
15500 }
15501
15502 static void
15503 do_neon_rshift_round_imm (void)
15504 {
15505   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15506   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
15507   int imm = inst.operands[2].imm;
15508
15509   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
15510   if (imm == 0)
15511     {
15512       inst.operands[2].present = 0;
15513       do_neon_mov ();
15514       return;
15515     }
15516
15517   constraint (imm < 1 || (unsigned)imm > et.size,
15518               _("immediate out of range for shift"));
15519   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
15520                   et.size - imm);
15521 }
15522
15523 static void
15524 do_neon_movl (void)
15525 {
15526   struct neon_type_el et = neon_check_type (2, NS_QD,
15527     N_EQK | N_DBL, N_SU_32 | N_KEY);
15528   unsigned sizebits = et.size >> 3;
15529   inst.instruction |= sizebits << 19;
15530   neon_two_same (0, et.type == NT_unsigned, -1);
15531 }
15532
15533 static void
15534 do_neon_trn (void)
15535 {
15536   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15537   struct neon_type_el et = neon_check_type (2, rs,
15538     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15539   NEON_ENCODE (INTEGER, inst);
15540   neon_two_same (neon_quad (rs), 1, et.size);
15541 }
15542
15543 static void
15544 do_neon_zip_uzp (void)
15545 {
15546   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15547   struct neon_type_el et = neon_check_type (2, rs,
15548     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15549   if (rs == NS_DD && et.size == 32)
15550     {
15551       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
15552       inst.instruction = N_MNEM_vtrn;
15553       do_neon_trn ();
15554       return;
15555     }
15556   neon_two_same (neon_quad (rs), 1, et.size);
15557 }
15558
15559 static void
15560 do_neon_sat_abs_neg (void)
15561 {
15562   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15563   struct neon_type_el et = neon_check_type (2, rs,
15564     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
15565   neon_two_same (neon_quad (rs), 1, et.size);
15566 }
15567
15568 static void
15569 do_neon_pair_long (void)
15570 {
15571   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15572   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
15573   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
15574   inst.instruction |= (et.type == NT_unsigned) << 7;
15575   neon_two_same (neon_quad (rs), 1, et.size);
15576 }
15577
15578 static void
15579 do_neon_recip_est (void)
15580 {
15581   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15582   struct neon_type_el et = neon_check_type (2, rs,
15583     N_EQK | N_FLT, N_F32 | N_U32 | N_KEY);
15584   inst.instruction |= (et.type == NT_float) << 8;
15585   neon_two_same (neon_quad (rs), 1, et.size);
15586 }
15587
15588 static void
15589 do_neon_cls (void)
15590 {
15591   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15592   struct neon_type_el et = neon_check_type (2, rs,
15593     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
15594   neon_two_same (neon_quad (rs), 1, et.size);
15595 }
15596
15597 static void
15598 do_neon_clz (void)
15599 {
15600   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15601   struct neon_type_el et = neon_check_type (2, rs,
15602     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
15603   neon_two_same (neon_quad (rs), 1, et.size);
15604 }
15605
15606 static void
15607 do_neon_cnt (void)
15608 {
15609   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15610   struct neon_type_el et = neon_check_type (2, rs,
15611     N_EQK | N_INT, N_8 | N_KEY);
15612   neon_two_same (neon_quad (rs), 1, et.size);
15613 }
15614
15615 static void
15616 do_neon_swp (void)
15617 {
15618   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15619   neon_two_same (neon_quad (rs), 1, -1);
15620 }
15621
15622 static void
15623 do_neon_tbl_tbx (void)
15624 {
15625   unsigned listlenbits;
15626   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
15627
15628   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
15629     {
15630       first_error (_("bad list length for table lookup"));
15631       return;
15632     }
15633
15634   listlenbits = inst.operands[1].imm - 1;
15635   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15636   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15637   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15638   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15639   inst.instruction |= LOW4 (inst.operands[2].reg);
15640   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15641   inst.instruction |= listlenbits << 8;
15642
15643   neon_dp_fixup (&inst);
15644 }
15645
15646 static void
15647 do_neon_ldm_stm (void)
15648 {
15649   /* P, U and L bits are part of bitmask.  */
15650   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
15651   unsigned offsetbits = inst.operands[1].imm * 2;
15652
15653   if (inst.operands[1].issingle)
15654     {
15655       do_vfp_nsyn_ldm_stm (is_dbmode);
15656       return;
15657     }
15658
15659   constraint (is_dbmode && !inst.operands[0].writeback,
15660               _("writeback (!) must be used for VLDMDB and VSTMDB"));
15661
15662   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
15663               _("register list must contain at least 1 and at most 16 "
15664                 "registers"));
15665
15666   inst.instruction |= inst.operands[0].reg << 16;
15667   inst.instruction |= inst.operands[0].writeback << 21;
15668   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
15669   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
15670
15671   inst.instruction |= offsetbits;
15672
15673   do_vfp_cond_or_thumb ();
15674 }
15675
15676 static void
15677 do_neon_ldr_str (void)
15678 {
15679   int is_ldr = (inst.instruction & (1 << 20)) != 0;
15680
15681   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
15682      And is UNPREDICTABLE in thumb mode.  */
15683   if (!is_ldr
15684       && inst.operands[1].reg == REG_PC
15685       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
15686     {
15687       if (!thumb_mode && warn_on_deprecated)
15688         as_warn (_("Use of PC here is deprecated"));
15689       else
15690         inst.error = _("Use of PC here is UNPREDICTABLE");
15691     }
15692
15693   if (inst.operands[0].issingle)
15694     {
15695       if (is_ldr)
15696         do_vfp_nsyn_opcode ("flds");
15697       else
15698         do_vfp_nsyn_opcode ("fsts");
15699     }
15700   else
15701     {
15702       if (is_ldr)
15703         do_vfp_nsyn_opcode ("fldd");
15704       else
15705         do_vfp_nsyn_opcode ("fstd");
15706     }
15707 }
15708
15709 /* "interleave" version also handles non-interleaving register VLD1/VST1
15710    instructions.  */
15711
15712 static void
15713 do_neon_ld_st_interleave (void)
15714 {
15715   struct neon_type_el et = neon_check_type (1, NS_NULL,
15716                                             N_8 | N_16 | N_32 | N_64);
15717   unsigned alignbits = 0;
15718   unsigned idx;
15719   /* The bits in this table go:
15720      0: register stride of one (0) or two (1)
15721      1,2: register list length, minus one (1, 2, 3, 4).
15722      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
15723      We use -1 for invalid entries.  */
15724   const int typetable[] =
15725     {
15726       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
15727        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
15728        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
15729        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
15730     };
15731   int typebits;
15732
15733   if (et.type == NT_invtype)
15734     return;
15735
15736   if (inst.operands[1].immisalign)
15737     switch (inst.operands[1].imm >> 8)
15738       {
15739       case 64: alignbits = 1; break;
15740       case 128:
15741         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
15742             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
15743           goto bad_alignment;
15744         alignbits = 2;
15745         break;
15746       case 256:
15747         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
15748           goto bad_alignment;
15749         alignbits = 3;
15750         break;
15751       default:
15752       bad_alignment:
15753         first_error (_("bad alignment"));
15754         return;
15755       }
15756
15757   inst.instruction |= alignbits << 4;
15758   inst.instruction |= neon_logbits (et.size) << 6;
15759
15760   /* Bits [4:6] of the immediate in a list specifier encode register stride
15761      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
15762      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
15763      up the right value for "type" in a table based on this value and the given
15764      list style, then stick it back.  */
15765   idx = ((inst.operands[0].imm >> 4) & 7)
15766         | (((inst.instruction >> 8) & 3) << 3);
15767
15768   typebits = typetable[idx];
15769
15770   constraint (typebits == -1, _("bad list type for instruction"));
15771
15772   inst.instruction &= ~0xf00;
15773   inst.instruction |= typebits << 8;
15774 }
15775
15776 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
15777    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
15778    otherwise. The variable arguments are a list of pairs of legal (size, align)
15779    values, terminated with -1.  */
15780
15781 static int
15782 neon_alignment_bit (int size, int align, int *do_align, ...)
15783 {
15784   va_list ap;
15785   int result = FAIL, thissize, thisalign;
15786
15787   if (!inst.operands[1].immisalign)
15788     {
15789       *do_align = 0;
15790       return SUCCESS;
15791     }
15792
15793   va_start (ap, do_align);
15794
15795   do
15796     {
15797       thissize = va_arg (ap, int);
15798       if (thissize == -1)
15799         break;
15800       thisalign = va_arg (ap, int);
15801
15802       if (size == thissize && align == thisalign)
15803         result = SUCCESS;
15804     }
15805   while (result != SUCCESS);
15806
15807   va_end (ap);
15808
15809   if (result == SUCCESS)
15810     *do_align = 1;
15811   else
15812     first_error (_("unsupported alignment for instruction"));
15813
15814   return result;
15815 }
15816
15817 static void
15818 do_neon_ld_st_lane (void)
15819 {
15820   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
15821   int align_good, do_align = 0;
15822   int logsize = neon_logbits (et.size);
15823   int align = inst.operands[1].imm >> 8;
15824   int n = (inst.instruction >> 8) & 3;
15825   int max_el = 64 / et.size;
15826
15827   if (et.type == NT_invtype)
15828     return;
15829
15830   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
15831               _("bad list length"));
15832   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
15833               _("scalar index out of range"));
15834   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
15835               && et.size == 8,
15836               _("stride of 2 unavailable when element size is 8"));
15837
15838   switch (n)
15839     {
15840     case 0:  /* VLD1 / VST1.  */
15841       align_good = neon_alignment_bit (et.size, align, &do_align, 16, 16,
15842                                        32, 32, -1);
15843       if (align_good == FAIL)
15844         return;
15845       if (do_align)
15846         {
15847           unsigned alignbits = 0;
15848           switch (et.size)
15849             {
15850             case 16: alignbits = 0x1; break;
15851             case 32: alignbits = 0x3; break;
15852             default: ;
15853             }
15854           inst.instruction |= alignbits << 4;
15855         }
15856       break;
15857
15858     case 1:  /* VLD2 / VST2.  */
15859       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 16, 16, 32,
15860                                        32, 64, -1);
15861       if (align_good == FAIL)
15862         return;
15863       if (do_align)
15864         inst.instruction |= 1 << 4;
15865       break;
15866
15867     case 2:  /* VLD3 / VST3.  */
15868       constraint (inst.operands[1].immisalign,
15869                   _("can't use alignment with this instruction"));
15870       break;
15871
15872     case 3:  /* VLD4 / VST4.  */
15873       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
15874                                        16, 64, 32, 64, 32, 128, -1);
15875       if (align_good == FAIL)
15876         return;
15877       if (do_align)
15878         {
15879           unsigned alignbits = 0;
15880           switch (et.size)
15881             {
15882             case 8:  alignbits = 0x1; break;
15883             case 16: alignbits = 0x1; break;
15884             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
15885             default: ;
15886             }
15887           inst.instruction |= alignbits << 4;
15888         }
15889       break;
15890
15891     default: ;
15892     }
15893
15894   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
15895   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15896     inst.instruction |= 1 << (4 + logsize);
15897
15898   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
15899   inst.instruction |= logsize << 10;
15900 }
15901
15902 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
15903
15904 static void
15905 do_neon_ld_dup (void)
15906 {
15907   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
15908   int align_good, do_align = 0;
15909
15910   if (et.type == NT_invtype)
15911     return;
15912
15913   switch ((inst.instruction >> 8) & 3)
15914     {
15915     case 0:  /* VLD1.  */
15916       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
15917       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
15918                                        &do_align, 16, 16, 32, 32, -1);
15919       if (align_good == FAIL)
15920         return;
15921       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
15922         {
15923         case 1: break;
15924         case 2: inst.instruction |= 1 << 5; break;
15925         default: first_error (_("bad list length")); return;
15926         }
15927       inst.instruction |= neon_logbits (et.size) << 6;
15928       break;
15929
15930     case 1:  /* VLD2.  */
15931       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
15932                                        &do_align, 8, 16, 16, 32, 32, 64, -1);
15933       if (align_good == FAIL)
15934         return;
15935       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
15936                   _("bad list length"));
15937       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15938         inst.instruction |= 1 << 5;
15939       inst.instruction |= neon_logbits (et.size) << 6;
15940       break;
15941
15942     case 2:  /* VLD3.  */
15943       constraint (inst.operands[1].immisalign,
15944                   _("can't use alignment with this instruction"));
15945       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
15946                   _("bad list length"));
15947       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15948         inst.instruction |= 1 << 5;
15949       inst.instruction |= neon_logbits (et.size) << 6;
15950       break;
15951
15952     case 3:  /* VLD4.  */
15953       {
15954         int align = inst.operands[1].imm >> 8;
15955         align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
15956                                          16, 64, 32, 64, 32, 128, -1);
15957         if (align_good == FAIL)
15958           return;
15959         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
15960                     _("bad list length"));
15961         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15962           inst.instruction |= 1 << 5;
15963         if (et.size == 32 && align == 128)
15964           inst.instruction |= 0x3 << 6;
15965         else
15966           inst.instruction |= neon_logbits (et.size) << 6;
15967       }
15968       break;
15969
15970     default: ;
15971     }
15972
15973   inst.instruction |= do_align << 4;
15974 }
15975
15976 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
15977    apart from bits [11:4].  */
15978
15979 static void
15980 do_neon_ldx_stx (void)
15981 {
15982   if (inst.operands[1].isreg)
15983     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
15984
15985   switch (NEON_LANE (inst.operands[0].imm))
15986     {
15987     case NEON_INTERLEAVE_LANES:
15988       NEON_ENCODE (INTERLV, inst);
15989       do_neon_ld_st_interleave ();
15990       break;
15991
15992     case NEON_ALL_LANES:
15993       NEON_ENCODE (DUP, inst);
15994       do_neon_ld_dup ();
15995       break;
15996
15997     default:
15998       NEON_ENCODE (LANE, inst);
15999       do_neon_ld_st_lane ();
16000     }
16001
16002   /* L bit comes from bit mask.  */
16003   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16004   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16005   inst.instruction |= inst.operands[1].reg << 16;
16006
16007   if (inst.operands[1].postind)
16008     {
16009       int postreg = inst.operands[1].imm & 0xf;
16010       constraint (!inst.operands[1].immisreg,
16011                   _("post-index must be a register"));
16012       constraint (postreg == 0xd || postreg == 0xf,
16013                   _("bad register for post-index"));
16014       inst.instruction |= postreg;
16015     }
16016   else if (inst.operands[1].writeback)
16017     {
16018       inst.instruction |= 0xd;
16019     }
16020   else
16021     inst.instruction |= 0xf;
16022
16023   if (thumb_mode)
16024     inst.instruction |= 0xf9000000;
16025   else
16026     inst.instruction |= 0xf4000000;
16027 }
16028
16029 /* FP v8.  */
16030 static void
16031 do_vfp_nsyn_fpv8 (enum neon_shape rs)
16032 {
16033   NEON_ENCODE (FPV8, inst);
16034
16035   if (rs == NS_FFF)
16036     do_vfp_sp_dyadic ();
16037   else
16038     do_vfp_dp_rd_rn_rm ();
16039
16040   if (rs == NS_DDD)
16041     inst.instruction |= 0x100;
16042
16043   inst.instruction |= 0xf0000000;
16044 }
16045
16046 static void
16047 do_vsel (void)
16048 {
16049   set_it_insn_type (OUTSIDE_IT_INSN);
16050
16051   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
16052     first_error (_("invalid instruction shape"));
16053 }
16054
16055 static void
16056 do_vmaxnm (void)
16057 {
16058   set_it_insn_type (OUTSIDE_IT_INSN);
16059
16060   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
16061     return;
16062
16063   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
16064     return;
16065
16066   neon_dyadic_misc (NT_untyped, N_F32, 0);
16067 }
16068
16069 static void
16070 do_vrint_1 (enum neon_cvt_mode mode)
16071 {
16072   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_QQ, NS_NULL);
16073   struct neon_type_el et;
16074
16075   if (rs == NS_NULL)
16076     return;
16077
16078   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
16079   if (et.type != NT_invtype)
16080     {
16081       /* VFP encodings.  */
16082       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
16083           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
16084         set_it_insn_type (OUTSIDE_IT_INSN);
16085
16086       NEON_ENCODE (FPV8, inst);
16087       if (rs == NS_FF)
16088         do_vfp_sp_monadic ();
16089       else
16090         do_vfp_dp_rd_rm ();
16091
16092       switch (mode)
16093         {
16094         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
16095         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
16096         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
16097         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
16098         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
16099         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
16100         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
16101         default: abort ();
16102         }
16103
16104       inst.instruction |= (rs == NS_DD) << 8;
16105       do_vfp_cond_or_thumb ();
16106     }
16107   else
16108     {
16109       /* Neon encodings (or something broken...).  */
16110       inst.error = NULL;
16111       et = neon_check_type (2, rs, N_EQK, N_F32 | N_KEY);
16112
16113       if (et.type == NT_invtype)
16114         return;
16115
16116       set_it_insn_type (OUTSIDE_IT_INSN);
16117       NEON_ENCODE (FLOAT, inst);
16118
16119       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
16120         return;
16121
16122       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16123       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16124       inst.instruction |= LOW4 (inst.operands[1].reg);
16125       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16126       inst.instruction |= neon_quad (rs) << 6;
16127       switch (mode)
16128         {
16129         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
16130         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
16131         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
16132         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
16133         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
16134         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
16135         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
16136         default: abort ();
16137         }
16138
16139       if (thumb_mode)
16140         inst.instruction |= 0xfc000000;
16141       else
16142         inst.instruction |= 0xf0000000;
16143     }
16144 }
16145
16146 static void
16147 do_vrintx (void)
16148 {
16149   do_vrint_1 (neon_cvt_mode_x);
16150 }
16151
16152 static void
16153 do_vrintz (void)
16154 {
16155   do_vrint_1 (neon_cvt_mode_z);
16156 }
16157
16158 static void
16159 do_vrintr (void)
16160 {
16161   do_vrint_1 (neon_cvt_mode_r);
16162 }
16163
16164 static void
16165 do_vrinta (void)
16166 {
16167   do_vrint_1 (neon_cvt_mode_a);
16168 }
16169
16170 static void
16171 do_vrintn (void)
16172 {
16173   do_vrint_1 (neon_cvt_mode_n);
16174 }
16175
16176 static void
16177 do_vrintp (void)
16178 {
16179   do_vrint_1 (neon_cvt_mode_p);
16180 }
16181
16182 static void
16183 do_vrintm (void)
16184 {
16185   do_vrint_1 (neon_cvt_mode_m);
16186 }
16187
16188 /* Crypto v1 instructions.  */
16189 static void
16190 do_crypto_2op_1 (unsigned elttype, int op)
16191 {
16192   set_it_insn_type (OUTSIDE_IT_INSN);
16193
16194   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
16195       == NT_invtype)
16196     return;
16197
16198   inst.error = NULL;
16199
16200   NEON_ENCODE (INTEGER, inst);
16201   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16202   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16203   inst.instruction |= LOW4 (inst.operands[1].reg);
16204   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16205   if (op != -1)
16206     inst.instruction |= op << 6;
16207
16208   if (thumb_mode)
16209     inst.instruction |= 0xfc000000;
16210   else
16211     inst.instruction |= 0xf0000000;
16212 }
16213
16214 static void
16215 do_crypto_3op_1 (int u, int op)
16216 {
16217   set_it_insn_type (OUTSIDE_IT_INSN);
16218
16219   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
16220                        N_32 | N_UNT | N_KEY).type == NT_invtype)
16221     return;
16222
16223   inst.error = NULL;
16224
16225   NEON_ENCODE (INTEGER, inst);
16226   neon_three_same (1, u, 8 << op);
16227 }
16228
16229 static void
16230 do_aese (void)
16231 {
16232   do_crypto_2op_1 (N_8, 0);
16233 }
16234
16235 static void
16236 do_aesd (void)
16237 {
16238   do_crypto_2op_1 (N_8, 1);
16239 }
16240
16241 static void
16242 do_aesmc (void)
16243 {
16244   do_crypto_2op_1 (N_8, 2);
16245 }
16246
16247 static void
16248 do_aesimc (void)
16249 {
16250   do_crypto_2op_1 (N_8, 3);
16251 }
16252
16253 static void
16254 do_sha1c (void)
16255 {
16256   do_crypto_3op_1 (0, 0);
16257 }
16258
16259 static void
16260 do_sha1p (void)
16261 {
16262   do_crypto_3op_1 (0, 1);
16263 }
16264
16265 static void
16266 do_sha1m (void)
16267 {
16268   do_crypto_3op_1 (0, 2);
16269 }
16270
16271 static void
16272 do_sha1su0 (void)
16273 {
16274   do_crypto_3op_1 (0, 3);
16275 }
16276
16277 static void
16278 do_sha256h (void)
16279 {
16280   do_crypto_3op_1 (1, 0);
16281 }
16282
16283 static void
16284 do_sha256h2 (void)
16285 {
16286   do_crypto_3op_1 (1, 1);
16287 }
16288
16289 static void
16290 do_sha256su1 (void)
16291 {
16292   do_crypto_3op_1 (1, 2);
16293 }
16294
16295 static void
16296 do_sha1h (void)
16297 {
16298   do_crypto_2op_1 (N_32, -1);
16299 }
16300
16301 static void
16302 do_sha1su1 (void)
16303 {
16304   do_crypto_2op_1 (N_32, 0);
16305 }
16306
16307 static void
16308 do_sha256su0 (void)
16309 {
16310   do_crypto_2op_1 (N_32, 1);
16311 }
16312 \f
16313 /* Overall per-instruction processing.  */
16314
16315 /* We need to be able to fix up arbitrary expressions in some statements.
16316    This is so that we can handle symbols that are an arbitrary distance from
16317    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
16318    which returns part of an address in a form which will be valid for
16319    a data instruction.  We do this by pushing the expression into a symbol
16320    in the expr_section, and creating a fix for that.  */
16321
16322 static void
16323 fix_new_arm (fragS *       frag,
16324              int           where,
16325              short int     size,
16326              expressionS * exp,
16327              int           pc_rel,
16328              int           reloc)
16329 {
16330   fixS *           new_fix;
16331
16332   switch (exp->X_op)
16333     {
16334     case O_constant:
16335       if (pc_rel)
16336         {
16337           /* Create an absolute valued symbol, so we have something to
16338              refer to in the object file.  Unfortunately for us, gas's
16339              generic expression parsing will already have folded out
16340              any use of .set foo/.type foo %function that may have
16341              been used to set type information of the target location,
16342              that's being specified symbolically.  We have to presume
16343              the user knows what they are doing.  */
16344           char name[16 + 8];
16345           symbolS *symbol;
16346
16347           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
16348
16349           symbol = symbol_find_or_make (name);
16350           S_SET_SEGMENT (symbol, absolute_section);
16351           symbol_set_frag (symbol, &zero_address_frag);
16352           S_SET_VALUE (symbol, exp->X_add_number);
16353           exp->X_op = O_symbol;
16354           exp->X_add_symbol = symbol;
16355           exp->X_add_number = 0;
16356         }
16357       /* FALLTHROUGH */
16358     case O_symbol:
16359     case O_add:
16360     case O_subtract:
16361       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
16362                              (enum bfd_reloc_code_real) reloc);
16363       break;
16364
16365     default:
16366       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
16367                                   pc_rel, (enum bfd_reloc_code_real) reloc);
16368       break;
16369     }
16370
16371   /* Mark whether the fix is to a THUMB instruction, or an ARM
16372      instruction.  */
16373   new_fix->tc_fix_data = thumb_mode;
16374 }
16375
16376 /* Create a frg for an instruction requiring relaxation.  */
16377 static void
16378 output_relax_insn (void)
16379 {
16380   char * to;
16381   symbolS *sym;
16382   int offset;
16383
16384   /* The size of the instruction is unknown, so tie the debug info to the
16385      start of the instruction.  */
16386   dwarf2_emit_insn (0);
16387
16388   switch (inst.reloc.exp.X_op)
16389     {
16390     case O_symbol:
16391       sym = inst.reloc.exp.X_add_symbol;
16392       offset = inst.reloc.exp.X_add_number;
16393       break;
16394     case O_constant:
16395       sym = NULL;
16396       offset = inst.reloc.exp.X_add_number;
16397       break;
16398     default:
16399       sym = make_expr_symbol (&inst.reloc.exp);
16400       offset = 0;
16401       break;
16402   }
16403   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
16404                  inst.relax, sym, offset, NULL/*offset, opcode*/);
16405   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
16406 }
16407
16408 /* Write a 32-bit thumb instruction to buf.  */
16409 static void
16410 put_thumb32_insn (char * buf, unsigned long insn)
16411 {
16412   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
16413   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
16414 }
16415
16416 static void
16417 output_inst (const char * str)
16418 {
16419   char * to = NULL;
16420
16421   if (inst.error)
16422     {
16423       as_bad ("%s -- `%s'", inst.error, str);
16424       return;
16425     }
16426   if (inst.relax)
16427     {
16428       output_relax_insn ();
16429       return;
16430     }
16431   if (inst.size == 0)
16432     return;
16433
16434   to = frag_more (inst.size);
16435   /* PR 9814: Record the thumb mode into the current frag so that we know
16436      what type of NOP padding to use, if necessary.  We override any previous
16437      setting so that if the mode has changed then the NOPS that we use will
16438      match the encoding of the last instruction in the frag.  */
16439   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
16440
16441   if (thumb_mode && (inst.size > THUMB_SIZE))
16442     {
16443       gas_assert (inst.size == (2 * THUMB_SIZE));
16444       put_thumb32_insn (to, inst.instruction);
16445     }
16446   else if (inst.size > INSN_SIZE)
16447     {
16448       gas_assert (inst.size == (2 * INSN_SIZE));
16449       md_number_to_chars (to, inst.instruction, INSN_SIZE);
16450       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
16451     }
16452   else
16453     md_number_to_chars (to, inst.instruction, inst.size);
16454
16455   if (inst.reloc.type != BFD_RELOC_UNUSED)
16456     fix_new_arm (frag_now, to - frag_now->fr_literal,
16457                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
16458                  inst.reloc.type);
16459
16460   dwarf2_emit_insn (inst.size);
16461 }
16462
16463 static char *
16464 output_it_inst (int cond, int mask, char * to)
16465 {
16466   unsigned long instruction = 0xbf00;
16467
16468   mask &= 0xf;
16469   instruction |= mask;
16470   instruction |= cond << 4;
16471
16472   if (to == NULL)
16473     {
16474       to = frag_more (2);
16475 #ifdef OBJ_ELF
16476       dwarf2_emit_insn (2);
16477 #endif
16478     }
16479
16480   md_number_to_chars (to, instruction, 2);
16481
16482   return to;
16483 }
16484
16485 /* Tag values used in struct asm_opcode's tag field.  */
16486 enum opcode_tag
16487 {
16488   OT_unconditional,     /* Instruction cannot be conditionalized.
16489                            The ARM condition field is still 0xE.  */
16490   OT_unconditionalF,    /* Instruction cannot be conditionalized
16491                            and carries 0xF in its ARM condition field.  */
16492   OT_csuffix,           /* Instruction takes a conditional suffix.  */
16493   OT_csuffixF,          /* Some forms of the instruction take a conditional
16494                            suffix, others place 0xF where the condition field
16495                            would be.  */
16496   OT_cinfix3,           /* Instruction takes a conditional infix,
16497                            beginning at character index 3.  (In
16498                            unified mode, it becomes a suffix.)  */
16499   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
16500                             tsts, cmps, cmns, and teqs. */
16501   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
16502                            character index 3, even in unified mode.  Used for
16503                            legacy instructions where suffix and infix forms
16504                            may be ambiguous.  */
16505   OT_csuf_or_in3,       /* Instruction takes either a conditional
16506                            suffix or an infix at character index 3.  */
16507   OT_odd_infix_unc,     /* This is the unconditional variant of an
16508                            instruction that takes a conditional infix
16509                            at an unusual position.  In unified mode,
16510                            this variant will accept a suffix.  */
16511   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
16512                            are the conditional variants of instructions that
16513                            take conditional infixes in unusual positions.
16514                            The infix appears at character index
16515                            (tag - OT_odd_infix_0).  These are not accepted
16516                            in unified mode.  */
16517 };
16518
16519 /* Subroutine of md_assemble, responsible for looking up the primary
16520    opcode from the mnemonic the user wrote.  STR points to the
16521    beginning of the mnemonic.
16522
16523    This is not simply a hash table lookup, because of conditional
16524    variants.  Most instructions have conditional variants, which are
16525    expressed with a _conditional affix_ to the mnemonic.  If we were
16526    to encode each conditional variant as a literal string in the opcode
16527    table, it would have approximately 20,000 entries.
16528
16529    Most mnemonics take this affix as a suffix, and in unified syntax,
16530    'most' is upgraded to 'all'.  However, in the divided syntax, some
16531    instructions take the affix as an infix, notably the s-variants of
16532    the arithmetic instructions.  Of those instructions, all but six
16533    have the infix appear after the third character of the mnemonic.
16534
16535    Accordingly, the algorithm for looking up primary opcodes given
16536    an identifier is:
16537
16538    1. Look up the identifier in the opcode table.
16539       If we find a match, go to step U.
16540
16541    2. Look up the last two characters of the identifier in the
16542       conditions table.  If we find a match, look up the first N-2
16543       characters of the identifier in the opcode table.  If we
16544       find a match, go to step CE.
16545
16546    3. Look up the fourth and fifth characters of the identifier in
16547       the conditions table.  If we find a match, extract those
16548       characters from the identifier, and look up the remaining
16549       characters in the opcode table.  If we find a match, go
16550       to step CM.
16551
16552    4. Fail.
16553
16554    U. Examine the tag field of the opcode structure, in case this is
16555       one of the six instructions with its conditional infix in an
16556       unusual place.  If it is, the tag tells us where to find the
16557       infix; look it up in the conditions table and set inst.cond
16558       accordingly.  Otherwise, this is an unconditional instruction.
16559       Again set inst.cond accordingly.  Return the opcode structure.
16560
16561   CE. Examine the tag field to make sure this is an instruction that
16562       should receive a conditional suffix.  If it is not, fail.
16563       Otherwise, set inst.cond from the suffix we already looked up,
16564       and return the opcode structure.
16565
16566   CM. Examine the tag field to make sure this is an instruction that
16567       should receive a conditional infix after the third character.
16568       If it is not, fail.  Otherwise, undo the edits to the current
16569       line of input and proceed as for case CE.  */
16570
16571 static const struct asm_opcode *
16572 opcode_lookup (char **str)
16573 {
16574   char *end, *base;
16575   char *affix;
16576   const struct asm_opcode *opcode;
16577   const struct asm_cond *cond;
16578   char save[2];
16579
16580   /* Scan up to the end of the mnemonic, which must end in white space,
16581      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
16582   for (base = end = *str; *end != '\0'; end++)
16583     if (*end == ' ' || *end == '.')
16584       break;
16585
16586   if (end == base)
16587     return NULL;
16588
16589   /* Handle a possible width suffix and/or Neon type suffix.  */
16590   if (end[0] == '.')
16591     {
16592       int offset = 2;
16593
16594       /* The .w and .n suffixes are only valid if the unified syntax is in
16595          use.  */
16596       if (unified_syntax && end[1] == 'w')
16597         inst.size_req = 4;
16598       else if (unified_syntax && end[1] == 'n')
16599         inst.size_req = 2;
16600       else
16601         offset = 0;
16602
16603       inst.vectype.elems = 0;
16604
16605       *str = end + offset;
16606
16607       if (end[offset] == '.')
16608         {
16609           /* See if we have a Neon type suffix (possible in either unified or
16610              non-unified ARM syntax mode).  */
16611           if (parse_neon_type (&inst.vectype, str) == FAIL)
16612             return NULL;
16613         }
16614       else if (end[offset] != '\0' && end[offset] != ' ')
16615         return NULL;
16616     }
16617   else
16618     *str = end;
16619
16620   /* Look for unaffixed or special-case affixed mnemonic.  */
16621   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
16622                                                     end - base);
16623   if (opcode)
16624     {
16625       /* step U */
16626       if (opcode->tag < OT_odd_infix_0)
16627         {
16628           inst.cond = COND_ALWAYS;
16629           return opcode;
16630         }
16631
16632       if (warn_on_deprecated && unified_syntax)
16633         as_warn (_("conditional infixes are deprecated in unified syntax"));
16634       affix = base + (opcode->tag - OT_odd_infix_0);
16635       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
16636       gas_assert (cond);
16637
16638       inst.cond = cond->value;
16639       return opcode;
16640     }
16641
16642   /* Cannot have a conditional suffix on a mnemonic of less than two
16643      characters.  */
16644   if (end - base < 3)
16645     return NULL;
16646
16647   /* Look for suffixed mnemonic.  */
16648   affix = end - 2;
16649   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
16650   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
16651                                                     affix - base);
16652   if (opcode && cond)
16653     {
16654       /* step CE */
16655       switch (opcode->tag)
16656         {
16657         case OT_cinfix3_legacy:
16658           /* Ignore conditional suffixes matched on infix only mnemonics.  */
16659           break;
16660
16661         case OT_cinfix3:
16662         case OT_cinfix3_deprecated:
16663         case OT_odd_infix_unc:
16664           if (!unified_syntax)
16665             return 0;
16666           /* else fall through */
16667
16668         case OT_csuffix:
16669         case OT_csuffixF:
16670         case OT_csuf_or_in3:
16671           inst.cond = cond->value;
16672           return opcode;
16673
16674         case OT_unconditional:
16675         case OT_unconditionalF:
16676           if (thumb_mode)
16677             inst.cond = cond->value;
16678           else
16679             {
16680               /* Delayed diagnostic.  */
16681               inst.error = BAD_COND;
16682               inst.cond = COND_ALWAYS;
16683             }
16684           return opcode;
16685
16686         default:
16687           return NULL;
16688         }
16689     }
16690
16691   /* Cannot have a usual-position infix on a mnemonic of less than
16692      six characters (five would be a suffix).  */
16693   if (end - base < 6)
16694     return NULL;
16695
16696   /* Look for infixed mnemonic in the usual position.  */
16697   affix = base + 3;
16698   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
16699   if (!cond)
16700     return NULL;
16701
16702   memcpy (save, affix, 2);
16703   memmove (affix, affix + 2, (end - affix) - 2);
16704   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
16705                                                     (end - base) - 2);
16706   memmove (affix + 2, affix, (end - affix) - 2);
16707   memcpy (affix, save, 2);
16708
16709   if (opcode
16710       && (opcode->tag == OT_cinfix3
16711           || opcode->tag == OT_cinfix3_deprecated
16712           || opcode->tag == OT_csuf_or_in3
16713           || opcode->tag == OT_cinfix3_legacy))
16714     {
16715       /* Step CM.  */
16716       if (warn_on_deprecated && unified_syntax
16717           && (opcode->tag == OT_cinfix3
16718               || opcode->tag == OT_cinfix3_deprecated))
16719         as_warn (_("conditional infixes are deprecated in unified syntax"));
16720
16721       inst.cond = cond->value;
16722       return opcode;
16723     }
16724
16725   return NULL;
16726 }
16727
16728 /* This function generates an initial IT instruction, leaving its block
16729    virtually open for the new instructions. Eventually,
16730    the mask will be updated by now_it_add_mask () each time
16731    a new instruction needs to be included in the IT block.
16732    Finally, the block is closed with close_automatic_it_block ().
16733    The block closure can be requested either from md_assemble (),
16734    a tencode (), or due to a label hook.  */
16735
16736 static void
16737 new_automatic_it_block (int cond)
16738 {
16739   now_it.state = AUTOMATIC_IT_BLOCK;
16740   now_it.mask = 0x18;
16741   now_it.cc = cond;
16742   now_it.block_length = 1;
16743   mapping_state (MAP_THUMB);
16744   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
16745   now_it.warn_deprecated = FALSE;
16746   now_it.insn_cond = TRUE;
16747 }
16748
16749 /* Close an automatic IT block.
16750    See comments in new_automatic_it_block ().  */
16751
16752 static void
16753 close_automatic_it_block (void)
16754 {
16755   now_it.mask = 0x10;
16756   now_it.block_length = 0;
16757 }
16758
16759 /* Update the mask of the current automatically-generated IT
16760    instruction. See comments in new_automatic_it_block ().  */
16761
16762 static void
16763 now_it_add_mask (int cond)
16764 {
16765 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
16766 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
16767                                               | ((bitvalue) << (nbit)))
16768   const int resulting_bit = (cond & 1);
16769
16770   now_it.mask &= 0xf;
16771   now_it.mask = SET_BIT_VALUE (now_it.mask,
16772                                    resulting_bit,
16773                                   (5 - now_it.block_length));
16774   now_it.mask = SET_BIT_VALUE (now_it.mask,
16775                                    1,
16776                                    ((5 - now_it.block_length) - 1) );
16777   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
16778
16779 #undef CLEAR_BIT
16780 #undef SET_BIT_VALUE
16781 }
16782
16783 /* The IT blocks handling machinery is accessed through the these functions:
16784      it_fsm_pre_encode ()               from md_assemble ()
16785      set_it_insn_type ()                optional, from the tencode functions
16786      set_it_insn_type_last ()           ditto
16787      in_it_block ()                     ditto
16788      it_fsm_post_encode ()              from md_assemble ()
16789      force_automatic_it_block_close ()  from label habdling functions
16790
16791    Rationale:
16792      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
16793         initializing the IT insn type with a generic initial value depending
16794         on the inst.condition.
16795      2) During the tencode function, two things may happen:
16796         a) The tencode function overrides the IT insn type by
16797            calling either set_it_insn_type (type) or set_it_insn_type_last ().
16798         b) The tencode function queries the IT block state by
16799            calling in_it_block () (i.e. to determine narrow/not narrow mode).
16800
16801         Both set_it_insn_type and in_it_block run the internal FSM state
16802         handling function (handle_it_state), because: a) setting the IT insn
16803         type may incur in an invalid state (exiting the function),
16804         and b) querying the state requires the FSM to be updated.
16805         Specifically we want to avoid creating an IT block for conditional
16806         branches, so it_fsm_pre_encode is actually a guess and we can't
16807         determine whether an IT block is required until the tencode () routine
16808         has decided what type of instruction this actually it.
16809         Because of this, if set_it_insn_type and in_it_block have to be used,
16810         set_it_insn_type has to be called first.
16811
16812         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
16813         determines the insn IT type depending on the inst.cond code.
16814         When a tencode () routine encodes an instruction that can be
16815         either outside an IT block, or, in the case of being inside, has to be
16816         the last one, set_it_insn_type_last () will determine the proper
16817         IT instruction type based on the inst.cond code. Otherwise,
16818         set_it_insn_type can be called for overriding that logic or
16819         for covering other cases.
16820
16821         Calling handle_it_state () may not transition the IT block state to
16822         OUTSIDE_IT_BLOCK immediatelly, since the (current) state could be
16823         still queried. Instead, if the FSM determines that the state should
16824         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
16825         after the tencode () function: that's what it_fsm_post_encode () does.
16826
16827         Since in_it_block () calls the state handling function to get an
16828         updated state, an error may occur (due to invalid insns combination).
16829         In that case, inst.error is set.
16830         Therefore, inst.error has to be checked after the execution of
16831         the tencode () routine.
16832
16833      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
16834         any pending state change (if any) that didn't take place in
16835         handle_it_state () as explained above.  */
16836
16837 static void
16838 it_fsm_pre_encode (void)
16839 {
16840   if (inst.cond != COND_ALWAYS)
16841     inst.it_insn_type = INSIDE_IT_INSN;
16842   else
16843     inst.it_insn_type = OUTSIDE_IT_INSN;
16844
16845   now_it.state_handled = 0;
16846 }
16847
16848 /* IT state FSM handling function.  */
16849
16850 static int
16851 handle_it_state (void)
16852 {
16853   now_it.state_handled = 1;
16854   now_it.insn_cond = FALSE;
16855
16856   switch (now_it.state)
16857     {
16858     case OUTSIDE_IT_BLOCK:
16859       switch (inst.it_insn_type)
16860         {
16861         case OUTSIDE_IT_INSN:
16862           break;
16863
16864         case INSIDE_IT_INSN:
16865         case INSIDE_IT_LAST_INSN:
16866           if (thumb_mode == 0)
16867             {
16868               if (unified_syntax
16869                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
16870                 as_tsktsk (_("Warning: conditional outside an IT block"\
16871                              " for Thumb."));
16872             }
16873           else
16874             {
16875               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
16876                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2))
16877                 {
16878                   /* Automatically generate the IT instruction.  */
16879                   new_automatic_it_block (inst.cond);
16880                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
16881                     close_automatic_it_block ();
16882                 }
16883               else
16884                 {
16885                   inst.error = BAD_OUT_IT;
16886                   return FAIL;
16887                 }
16888             }
16889           break;
16890
16891         case IF_INSIDE_IT_LAST_INSN:
16892         case NEUTRAL_IT_INSN:
16893           break;
16894
16895         case IT_INSN:
16896           now_it.state = MANUAL_IT_BLOCK;
16897           now_it.block_length = 0;
16898           break;
16899         }
16900       break;
16901
16902     case AUTOMATIC_IT_BLOCK:
16903       /* Three things may happen now:
16904          a) We should increment current it block size;
16905          b) We should close current it block (closing insn or 4 insns);
16906          c) We should close current it block and start a new one (due
16907          to incompatible conditions or
16908          4 insns-length block reached).  */
16909
16910       switch (inst.it_insn_type)
16911         {
16912         case OUTSIDE_IT_INSN:
16913           /* The closure of the block shall happen immediatelly,
16914              so any in_it_block () call reports the block as closed.  */
16915           force_automatic_it_block_close ();
16916           break;
16917
16918         case INSIDE_IT_INSN:
16919         case INSIDE_IT_LAST_INSN:
16920         case IF_INSIDE_IT_LAST_INSN:
16921           now_it.block_length++;
16922
16923           if (now_it.block_length > 4
16924               || !now_it_compatible (inst.cond))
16925             {
16926               force_automatic_it_block_close ();
16927               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
16928                 new_automatic_it_block (inst.cond);
16929             }
16930           else
16931             {
16932               now_it.insn_cond = TRUE;
16933               now_it_add_mask (inst.cond);
16934             }
16935
16936           if (now_it.state == AUTOMATIC_IT_BLOCK
16937               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
16938                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
16939             close_automatic_it_block ();
16940           break;
16941
16942         case NEUTRAL_IT_INSN:
16943           now_it.block_length++;
16944           now_it.insn_cond = TRUE;
16945
16946           if (now_it.block_length > 4)
16947             force_automatic_it_block_close ();
16948           else
16949             now_it_add_mask (now_it.cc & 1);
16950           break;
16951
16952         case IT_INSN:
16953           close_automatic_it_block ();
16954           now_it.state = MANUAL_IT_BLOCK;
16955           break;
16956         }
16957       break;
16958
16959     case MANUAL_IT_BLOCK:
16960       {
16961         /* Check conditional suffixes.  */
16962         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
16963         int is_last;
16964         now_it.mask <<= 1;
16965         now_it.mask &= 0x1f;
16966         is_last = (now_it.mask == 0x10);
16967         now_it.insn_cond = TRUE;
16968
16969         switch (inst.it_insn_type)
16970           {
16971           case OUTSIDE_IT_INSN:
16972             inst.error = BAD_NOT_IT;
16973             return FAIL;
16974
16975           case INSIDE_IT_INSN:
16976             if (cond != inst.cond)
16977               {
16978                 inst.error = BAD_IT_COND;
16979                 return FAIL;
16980               }
16981             break;
16982
16983           case INSIDE_IT_LAST_INSN:
16984           case IF_INSIDE_IT_LAST_INSN:
16985             if (cond != inst.cond)
16986               {
16987                 inst.error = BAD_IT_COND;
16988                 return FAIL;
16989               }
16990             if (!is_last)
16991               {
16992                 inst.error = BAD_BRANCH;
16993                 return FAIL;
16994               }
16995             break;
16996
16997           case NEUTRAL_IT_INSN:
16998             /* The BKPT instruction is unconditional even in an IT block.  */
16999             break;
17000
17001           case IT_INSN:
17002             inst.error = BAD_IT_IT;
17003             return FAIL;
17004           }
17005       }
17006       break;
17007     }
17008
17009   return SUCCESS;
17010 }
17011
17012 struct depr_insn_mask
17013 {
17014   unsigned long pattern;
17015   unsigned long mask;
17016   const char* description;
17017 };
17018
17019 /* List of 16-bit instruction patterns deprecated in an IT block in
17020    ARMv8.  */
17021 static const struct depr_insn_mask depr_it_insns[] = {
17022   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
17023   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
17024   { 0xa000, 0xb800, N_("ADR") },
17025   { 0x4800, 0xf800, N_("Literal loads") },
17026   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
17027   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
17028   { 0, 0, NULL }
17029 };
17030
17031 static void
17032 it_fsm_post_encode (void)
17033 {
17034   int is_last;
17035
17036   if (!now_it.state_handled)
17037     handle_it_state ();
17038
17039   if (now_it.insn_cond
17040       && !now_it.warn_deprecated
17041       && warn_on_deprecated
17042       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
17043     {
17044       if (inst.instruction >= 0x10000)
17045         {
17046           as_warn (_("it blocks containing wide Thumb instructions are "
17047                      "deprecated in ARMv8"));
17048           now_it.warn_deprecated = TRUE;
17049         }
17050       else
17051         {
17052           const struct depr_insn_mask *p = depr_it_insns;
17053
17054           while (p->mask != 0)
17055             {
17056               if ((inst.instruction & p->mask) == p->pattern)
17057                 {
17058                   as_warn (_("it blocks containing 16-bit Thumb intsructions "
17059                              "of the following class are deprecated in ARMv8: "
17060                              "%s"), p->description);
17061                   now_it.warn_deprecated = TRUE;
17062                   break;
17063                 }
17064
17065               ++p;
17066             }
17067         }
17068
17069       if (now_it.block_length > 1)
17070         {
17071           as_warn (_("it blocks of more than one conditional instruction are "
17072                      "deprecated in ARMv8"));
17073           now_it.warn_deprecated = TRUE;
17074         }
17075     }
17076
17077   is_last = (now_it.mask == 0x10);
17078   if (is_last)
17079     {
17080       now_it.state = OUTSIDE_IT_BLOCK;
17081       now_it.mask = 0;
17082     }
17083 }
17084
17085 static void
17086 force_automatic_it_block_close (void)
17087 {
17088   if (now_it.state == AUTOMATIC_IT_BLOCK)
17089     {
17090       close_automatic_it_block ();
17091       now_it.state = OUTSIDE_IT_BLOCK;
17092       now_it.mask = 0;
17093     }
17094 }
17095
17096 static int
17097 in_it_block (void)
17098 {
17099   if (!now_it.state_handled)
17100     handle_it_state ();
17101
17102   return now_it.state != OUTSIDE_IT_BLOCK;
17103 }
17104
17105 void
17106 md_assemble (char *str)
17107 {
17108   char *p = str;
17109   const struct asm_opcode * opcode;
17110
17111   /* Align the previous label if needed.  */
17112   if (last_label_seen != NULL)
17113     {
17114       symbol_set_frag (last_label_seen, frag_now);
17115       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
17116       S_SET_SEGMENT (last_label_seen, now_seg);
17117     }
17118
17119   memset (&inst, '\0', sizeof (inst));
17120   inst.reloc.type = BFD_RELOC_UNUSED;
17121
17122   opcode = opcode_lookup (&p);
17123   if (!opcode)
17124     {
17125       /* It wasn't an instruction, but it might be a register alias of
17126          the form alias .req reg, or a Neon .dn/.qn directive.  */
17127       if (! create_register_alias (str, p)
17128           && ! create_neon_reg_alias (str, p))
17129         as_bad (_("bad instruction `%s'"), str);
17130
17131       return;
17132     }
17133
17134   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
17135     as_warn (_("s suffix on comparison instruction is deprecated"));
17136
17137   /* The value which unconditional instructions should have in place of the
17138      condition field.  */
17139   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
17140
17141   if (thumb_mode)
17142     {
17143       arm_feature_set variant;
17144
17145       variant = cpu_variant;
17146       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
17147       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
17148         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
17149       /* Check that this instruction is supported for this CPU.  */
17150       if (!opcode->tvariant
17151           || (thumb_mode == 1
17152               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
17153         {
17154           as_bad (_("selected processor does not support Thumb mode `%s'"), str);
17155           return;
17156         }
17157       if (inst.cond != COND_ALWAYS && !unified_syntax
17158           && opcode->tencode != do_t_branch)
17159         {
17160           as_bad (_("Thumb does not support conditional execution"));
17161           return;
17162         }
17163
17164       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2))
17165         {
17166           if (opcode->tencode != do_t_blx && opcode->tencode != do_t_branch23
17167               && !(ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_msr)
17168                    || ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_barrier)))
17169             {
17170               /* Two things are addressed here.
17171                  1) Implicit require narrow instructions on Thumb-1.
17172                     This avoids relaxation accidentally introducing Thumb-2
17173                      instructions.
17174                  2) Reject wide instructions in non Thumb-2 cores.  */
17175               if (inst.size_req == 0)
17176                 inst.size_req = 2;
17177               else if (inst.size_req == 4)
17178                 {
17179                   as_bad (_("selected processor does not support Thumb-2 mode `%s'"), str);
17180                   return;
17181                 }
17182             }
17183         }
17184
17185       inst.instruction = opcode->tvalue;
17186
17187       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
17188         {
17189           /* Prepare the it_insn_type for those encodings that don't set
17190              it.  */
17191           it_fsm_pre_encode ();
17192
17193           opcode->tencode ();
17194
17195           it_fsm_post_encode ();
17196         }
17197
17198       if (!(inst.error || inst.relax))
17199         {
17200           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
17201           inst.size = (inst.instruction > 0xffff ? 4 : 2);
17202           if (inst.size_req && inst.size_req != inst.size)
17203             {
17204               as_bad (_("cannot honor width suffix -- `%s'"), str);
17205               return;
17206             }
17207         }
17208
17209       /* Something has gone badly wrong if we try to relax a fixed size
17210          instruction.  */
17211       gas_assert (inst.size_req == 0 || !inst.relax);
17212
17213       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
17214                               *opcode->tvariant);
17215       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
17216          set those bits when Thumb-2 32-bit instructions are seen.  ie.
17217          anything other than bl/blx and v6-M instructions.
17218          This is overly pessimistic for relaxable instructions.  */
17219       if (((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
17220            || inst.relax)
17221           && !(ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
17222                || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier)))
17223         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
17224                                 arm_ext_v6t2);
17225
17226       check_neon_suffixes;
17227
17228       if (!inst.error)
17229         {
17230           mapping_state (MAP_THUMB);
17231         }
17232     }
17233   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
17234     {
17235       bfd_boolean is_bx;
17236
17237       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
17238       is_bx = (opcode->aencode == do_bx);
17239
17240       /* Check that this instruction is supported for this CPU.  */
17241       if (!(is_bx && fix_v4bx)
17242           && !(opcode->avariant &&
17243                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
17244         {
17245           as_bad (_("selected processor does not support ARM mode `%s'"), str);
17246           return;
17247         }
17248       if (inst.size_req)
17249         {
17250           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
17251           return;
17252         }
17253
17254       inst.instruction = opcode->avalue;
17255       if (opcode->tag == OT_unconditionalF)
17256         inst.instruction |= 0xF << 28;
17257       else
17258         inst.instruction |= inst.cond << 28;
17259       inst.size = INSN_SIZE;
17260       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
17261         {
17262           it_fsm_pre_encode ();
17263           opcode->aencode ();
17264           it_fsm_post_encode ();
17265         }
17266       /* Arm mode bx is marked as both v4T and v5 because it's still required
17267          on a hypothetical non-thumb v5 core.  */
17268       if (is_bx)
17269         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
17270       else
17271         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
17272                                 *opcode->avariant);
17273
17274       check_neon_suffixes;
17275
17276       if (!inst.error)
17277         {
17278           mapping_state (MAP_ARM);
17279         }
17280     }
17281   else
17282     {
17283       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
17284                 "-- `%s'"), str);
17285       return;
17286     }
17287   output_inst (str);
17288 }
17289
17290 static void
17291 check_it_blocks_finished (void)
17292 {
17293 #ifdef OBJ_ELF
17294   asection *sect;
17295
17296   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
17297     if (seg_info (sect)->tc_segment_info_data.current_it.state
17298         == MANUAL_IT_BLOCK)
17299       {
17300         as_warn (_("section '%s' finished with an open IT block."),
17301                  sect->name);
17302       }
17303 #else
17304   if (now_it.state == MANUAL_IT_BLOCK)
17305     as_warn (_("file finished with an open IT block."));
17306 #endif
17307 }
17308
17309 /* Various frobbings of labels and their addresses.  */
17310
17311 void
17312 arm_start_line_hook (void)
17313 {
17314   last_label_seen = NULL;
17315 }
17316
17317 void
17318 arm_frob_label (symbolS * sym)
17319 {
17320   last_label_seen = sym;
17321
17322   ARM_SET_THUMB (sym, thumb_mode);
17323
17324 #if defined OBJ_COFF || defined OBJ_ELF
17325   ARM_SET_INTERWORK (sym, support_interwork);
17326 #endif
17327
17328   force_automatic_it_block_close ();
17329
17330   /* Note - do not allow local symbols (.Lxxx) to be labelled
17331      as Thumb functions.  This is because these labels, whilst
17332      they exist inside Thumb code, are not the entry points for
17333      possible ARM->Thumb calls.  Also, these labels can be used
17334      as part of a computed goto or switch statement.  eg gcc
17335      can generate code that looks like this:
17336
17337                 ldr  r2, [pc, .Laaa]
17338                 lsl  r3, r3, #2
17339                 ldr  r2, [r3, r2]
17340                 mov  pc, r2
17341
17342        .Lbbb:  .word .Lxxx
17343        .Lccc:  .word .Lyyy
17344        ..etc...
17345        .Laaa:   .word Lbbb
17346
17347      The first instruction loads the address of the jump table.
17348      The second instruction converts a table index into a byte offset.
17349      The third instruction gets the jump address out of the table.
17350      The fourth instruction performs the jump.
17351
17352      If the address stored at .Laaa is that of a symbol which has the
17353      Thumb_Func bit set, then the linker will arrange for this address
17354      to have the bottom bit set, which in turn would mean that the
17355      address computation performed by the third instruction would end
17356      up with the bottom bit set.  Since the ARM is capable of unaligned
17357      word loads, the instruction would then load the incorrect address
17358      out of the jump table, and chaos would ensue.  */
17359   if (label_is_thumb_function_name
17360       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
17361       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
17362     {
17363       /* When the address of a Thumb function is taken the bottom
17364          bit of that address should be set.  This will allow
17365          interworking between Arm and Thumb functions to work
17366          correctly.  */
17367
17368       THUMB_SET_FUNC (sym, 1);
17369
17370       label_is_thumb_function_name = FALSE;
17371     }
17372
17373   dwarf2_emit_label (sym);
17374 }
17375
17376 bfd_boolean
17377 arm_data_in_code (void)
17378 {
17379   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
17380     {
17381       *input_line_pointer = '/';
17382       input_line_pointer += 5;
17383       *input_line_pointer = 0;
17384       return TRUE;
17385     }
17386
17387   return FALSE;
17388 }
17389
17390 char *
17391 arm_canonicalize_symbol_name (char * name)
17392 {
17393   int len;
17394
17395   if (thumb_mode && (len = strlen (name)) > 5
17396       && streq (name + len - 5, "/data"))
17397     *(name + len - 5) = 0;
17398
17399   return name;
17400 }
17401 \f
17402 /* Table of all register names defined by default.  The user can
17403    define additional names with .req.  Note that all register names
17404    should appear in both upper and lowercase variants.  Some registers
17405    also have mixed-case names.  */
17406
17407 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
17408 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
17409 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
17410 #define REGSET(p,t) \
17411   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
17412   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
17413   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
17414   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
17415 #define REGSETH(p,t) \
17416   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
17417   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
17418   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
17419   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
17420 #define REGSET2(p,t) \
17421   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
17422   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
17423   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
17424   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
17425 #define SPLRBANK(base,bank,t) \
17426   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
17427   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
17428   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
17429   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
17430   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
17431   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
17432
17433 static const struct reg_entry reg_names[] =
17434 {
17435   /* ARM integer registers.  */
17436   REGSET(r, RN), REGSET(R, RN),
17437
17438   /* ATPCS synonyms.  */
17439   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
17440   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
17441   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
17442
17443   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
17444   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
17445   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
17446
17447   /* Well-known aliases.  */
17448   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
17449   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
17450
17451   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
17452   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
17453
17454   /* Coprocessor numbers.  */
17455   REGSET(p, CP), REGSET(P, CP),
17456
17457   /* Coprocessor register numbers.  The "cr" variants are for backward
17458      compatibility.  */
17459   REGSET(c,  CN), REGSET(C, CN),
17460   REGSET(cr, CN), REGSET(CR, CN),
17461
17462   /* ARM banked registers.  */
17463   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
17464   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
17465   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
17466   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
17467   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
17468   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
17469   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
17470
17471   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
17472   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
17473   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
17474   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
17475   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
17476   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(SP_fiq,512|(13<<16),RNB),
17477   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
17478   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
17479
17480   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
17481   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
17482   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
17483   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
17484   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
17485   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
17486   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
17487   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
17488   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
17489
17490   /* FPA registers.  */
17491   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
17492   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
17493
17494   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
17495   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
17496
17497   /* VFP SP registers.  */
17498   REGSET(s,VFS),  REGSET(S,VFS),
17499   REGSETH(s,VFS), REGSETH(S,VFS),
17500
17501   /* VFP DP Registers.  */
17502   REGSET(d,VFD),  REGSET(D,VFD),
17503   /* Extra Neon DP registers.  */
17504   REGSETH(d,VFD), REGSETH(D,VFD),
17505
17506   /* Neon QP registers.  */
17507   REGSET2(q,NQ),  REGSET2(Q,NQ),
17508
17509   /* VFP control registers.  */
17510   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
17511   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
17512   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
17513   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
17514   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
17515   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
17516
17517   /* Maverick DSP coprocessor registers.  */
17518   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
17519   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
17520
17521   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
17522   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
17523   REGDEF(dspsc,0,DSPSC),
17524
17525   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
17526   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
17527   REGDEF(DSPSC,0,DSPSC),
17528
17529   /* iWMMXt data registers - p0, c0-15.  */
17530   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
17531
17532   /* iWMMXt control registers - p1, c0-3.  */
17533   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
17534   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
17535   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
17536   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
17537
17538   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
17539   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
17540   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
17541   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
17542   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
17543
17544   /* XScale accumulator registers.  */
17545   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
17546 };
17547 #undef REGDEF
17548 #undef REGNUM
17549 #undef REGSET
17550
17551 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
17552    within psr_required_here.  */
17553 static const struct asm_psr psrs[] =
17554 {
17555   /* Backward compatibility notation.  Note that "all" is no longer
17556      truly all possible PSR bits.  */
17557   {"all",  PSR_c | PSR_f},
17558   {"flg",  PSR_f},
17559   {"ctl",  PSR_c},
17560
17561   /* Individual flags.  */
17562   {"f",    PSR_f},
17563   {"c",    PSR_c},
17564   {"x",    PSR_x},
17565   {"s",    PSR_s},
17566
17567   /* Combinations of flags.  */
17568   {"fs",   PSR_f | PSR_s},
17569   {"fx",   PSR_f | PSR_x},
17570   {"fc",   PSR_f | PSR_c},
17571   {"sf",   PSR_s | PSR_f},
17572   {"sx",   PSR_s | PSR_x},
17573   {"sc",   PSR_s | PSR_c},
17574   {"xf",   PSR_x | PSR_f},
17575   {"xs",   PSR_x | PSR_s},
17576   {"xc",   PSR_x | PSR_c},
17577   {"cf",   PSR_c | PSR_f},
17578   {"cs",   PSR_c | PSR_s},
17579   {"cx",   PSR_c | PSR_x},
17580   {"fsx",  PSR_f | PSR_s | PSR_x},
17581   {"fsc",  PSR_f | PSR_s | PSR_c},
17582   {"fxs",  PSR_f | PSR_x | PSR_s},
17583   {"fxc",  PSR_f | PSR_x | PSR_c},
17584   {"fcs",  PSR_f | PSR_c | PSR_s},
17585   {"fcx",  PSR_f | PSR_c | PSR_x},
17586   {"sfx",  PSR_s | PSR_f | PSR_x},
17587   {"sfc",  PSR_s | PSR_f | PSR_c},
17588   {"sxf",  PSR_s | PSR_x | PSR_f},
17589   {"sxc",  PSR_s | PSR_x | PSR_c},
17590   {"scf",  PSR_s | PSR_c | PSR_f},
17591   {"scx",  PSR_s | PSR_c | PSR_x},
17592   {"xfs",  PSR_x | PSR_f | PSR_s},
17593   {"xfc",  PSR_x | PSR_f | PSR_c},
17594   {"xsf",  PSR_x | PSR_s | PSR_f},
17595   {"xsc",  PSR_x | PSR_s | PSR_c},
17596   {"xcf",  PSR_x | PSR_c | PSR_f},
17597   {"xcs",  PSR_x | PSR_c | PSR_s},
17598   {"cfs",  PSR_c | PSR_f | PSR_s},
17599   {"cfx",  PSR_c | PSR_f | PSR_x},
17600   {"csf",  PSR_c | PSR_s | PSR_f},
17601   {"csx",  PSR_c | PSR_s | PSR_x},
17602   {"cxf",  PSR_c | PSR_x | PSR_f},
17603   {"cxs",  PSR_c | PSR_x | PSR_s},
17604   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
17605   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
17606   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
17607   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
17608   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
17609   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
17610   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
17611   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
17612   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
17613   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
17614   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
17615   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
17616   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
17617   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
17618   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
17619   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
17620   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
17621   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
17622   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
17623   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
17624   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
17625   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
17626   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
17627   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
17628 };
17629
17630 /* Table of V7M psr names.  */
17631 static const struct asm_psr v7m_psrs[] =
17632 {
17633   {"apsr",        0 }, {"APSR",         0 },
17634   {"iapsr",       1 }, {"IAPSR",        1 },
17635   {"eapsr",       2 }, {"EAPSR",        2 },
17636   {"psr",         3 }, {"PSR",          3 },
17637   {"xpsr",        3 }, {"XPSR",         3 }, {"xPSR",     3 },
17638   {"ipsr",        5 }, {"IPSR",         5 },
17639   {"epsr",        6 }, {"EPSR",         6 },
17640   {"iepsr",       7 }, {"IEPSR",        7 },
17641   {"msp",         8 }, {"MSP",          8 },
17642   {"psp",         9 }, {"PSP",          9 },
17643   {"primask",     16}, {"PRIMASK",      16},
17644   {"basepri",     17}, {"BASEPRI",      17},
17645   {"basepri_max", 18}, {"BASEPRI_MAX",  18},
17646   {"basepri_max", 18}, {"BASEPRI_MASK", 18}, /* Typo, preserved for backwards compatibility.  */
17647   {"faultmask",   19}, {"FAULTMASK",    19},
17648   {"control",     20}, {"CONTROL",      20}
17649 };
17650
17651 /* Table of all shift-in-operand names.  */
17652 static const struct asm_shift_name shift_names [] =
17653 {
17654   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
17655   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
17656   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
17657   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
17658   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
17659   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
17660 };
17661
17662 /* Table of all explicit relocation names.  */
17663 #ifdef OBJ_ELF
17664 static struct reloc_entry reloc_names[] =
17665 {
17666   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
17667   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
17668   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
17669   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
17670   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
17671   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
17672   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
17673   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
17674   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
17675   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
17676   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
17677   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
17678   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
17679         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
17680   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
17681         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
17682   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
17683         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ}
17684 };
17685 #endif
17686
17687 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
17688 static const struct asm_cond conds[] =
17689 {
17690   {"eq", 0x0},
17691   {"ne", 0x1},
17692   {"cs", 0x2}, {"hs", 0x2},
17693   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
17694   {"mi", 0x4},
17695   {"pl", 0x5},
17696   {"vs", 0x6},
17697   {"vc", 0x7},
17698   {"hi", 0x8},
17699   {"ls", 0x9},
17700   {"ge", 0xa},
17701   {"lt", 0xb},
17702   {"gt", 0xc},
17703   {"le", 0xd},
17704   {"al", 0xe}
17705 };
17706
17707 #define UL_BARRIER(L,U,CODE,FEAT) \
17708   { L, CODE, ARM_FEATURE (FEAT, 0) }, \
17709   { U, CODE, ARM_FEATURE (FEAT, 0) }
17710
17711 static struct asm_barrier_opt barrier_opt_names[] =
17712 {
17713   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
17714   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
17715   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
17716   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
17717   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
17718   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
17719   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
17720   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
17721   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
17722   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
17723   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
17724   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
17725   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
17726   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
17727   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
17728   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
17729 };
17730
17731 #undef UL_BARRIER
17732
17733 /* Table of ARM-format instructions.    */
17734
17735 /* Macros for gluing together operand strings.  N.B. In all cases
17736    other than OPS0, the trailing OP_stop comes from default
17737    zero-initialization of the unspecified elements of the array.  */
17738 #define OPS0()            { OP_stop, }
17739 #define OPS1(a)           { OP_##a, }
17740 #define OPS2(a,b)         { OP_##a,OP_##b, }
17741 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
17742 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
17743 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
17744 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
17745
17746 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
17747    This is useful when mixing operands for ARM and THUMB, i.e. using the
17748    MIX_ARM_THUMB_OPERANDS macro.
17749    In order to use these macros, prefix the number of operands with _
17750    e.g. _3.  */
17751 #define OPS_1(a)           { a, }
17752 #define OPS_2(a,b)         { a,b, }
17753 #define OPS_3(a,b,c)       { a,b,c, }
17754 #define OPS_4(a,b,c,d)     { a,b,c,d, }
17755 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
17756 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
17757
17758 /* These macros abstract out the exact format of the mnemonic table and
17759    save some repeated characters.  */
17760
17761 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
17762 #define TxCE(mnem, op, top, nops, ops, ae, te) \
17763   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
17764     THUMB_VARIANT, do_##ae, do_##te }
17765
17766 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
17767    a T_MNEM_xyz enumerator.  */
17768 #define TCE(mnem, aop, top, nops, ops, ae, te) \
17769       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
17770 #define tCE(mnem, aop, top, nops, ops, ae, te) \
17771       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
17772
17773 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
17774    infix after the third character.  */
17775 #define TxC3(mnem, op, top, nops, ops, ae, te) \
17776   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
17777     THUMB_VARIANT, do_##ae, do_##te }
17778 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
17779   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
17780     THUMB_VARIANT, do_##ae, do_##te }
17781 #define TC3(mnem, aop, top, nops, ops, ae, te) \
17782       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
17783 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
17784       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
17785 #define tC3(mnem, aop, top, nops, ops, ae, te) \
17786       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
17787 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
17788       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
17789
17790 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
17791    field is still 0xE.  Many of the Thumb variants can be executed
17792    conditionally, so this is checked separately.  */
17793 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
17794   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
17795     THUMB_VARIANT, do_##ae, do_##te }
17796
17797 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
17798    condition code field.  */
17799 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
17800   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
17801     THUMB_VARIANT, do_##ae, do_##te }
17802
17803 /* ARM-only variants of all the above.  */
17804 #define CE(mnem,  op, nops, ops, ae)    \
17805   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
17806
17807 #define C3(mnem, op, nops, ops, ae)     \
17808   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
17809
17810 /* Legacy mnemonics that always have conditional infix after the third
17811    character.  */
17812 #define CL(mnem, op, nops, ops, ae)     \
17813   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
17814     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
17815
17816 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
17817 #define cCE(mnem,  op, nops, ops, ae)   \
17818   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
17819
17820 /* Legacy coprocessor instructions where conditional infix and conditional
17821    suffix are ambiguous.  For consistency this includes all FPA instructions,
17822    not just the potentially ambiguous ones.  */
17823 #define cCL(mnem, op, nops, ops, ae)    \
17824   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
17825     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
17826
17827 /* Coprocessor, takes either a suffix or a position-3 infix
17828    (for an FPA corner case). */
17829 #define C3E(mnem, op, nops, ops, ae) \
17830   { mnem, OPS##nops ops, OT_csuf_or_in3, \
17831     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
17832
17833 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
17834   { m1 #m2 m3, OPS##nops ops, \
17835     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
17836     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
17837
17838 #define CM(m1, m2, op, nops, ops, ae)   \
17839   xCM_ (m1,   , m2, op, nops, ops, ae), \
17840   xCM_ (m1, eq, m2, op, nops, ops, ae), \
17841   xCM_ (m1, ne, m2, op, nops, ops, ae), \
17842   xCM_ (m1, cs, m2, op, nops, ops, ae), \
17843   xCM_ (m1, hs, m2, op, nops, ops, ae), \
17844   xCM_ (m1, cc, m2, op, nops, ops, ae), \
17845   xCM_ (m1, ul, m2, op, nops, ops, ae), \
17846   xCM_ (m1, lo, m2, op, nops, ops, ae), \
17847   xCM_ (m1, mi, m2, op, nops, ops, ae), \
17848   xCM_ (m1, pl, m2, op, nops, ops, ae), \
17849   xCM_ (m1, vs, m2, op, nops, ops, ae), \
17850   xCM_ (m1, vc, m2, op, nops, ops, ae), \
17851   xCM_ (m1, hi, m2, op, nops, ops, ae), \
17852   xCM_ (m1, ls, m2, op, nops, ops, ae), \
17853   xCM_ (m1, ge, m2, op, nops, ops, ae), \
17854   xCM_ (m1, lt, m2, op, nops, ops, ae), \
17855   xCM_ (m1, gt, m2, op, nops, ops, ae), \
17856   xCM_ (m1, le, m2, op, nops, ops, ae), \
17857   xCM_ (m1, al, m2, op, nops, ops, ae)
17858
17859 #define UE(mnem, op, nops, ops, ae)     \
17860   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
17861
17862 #define UF(mnem, op, nops, ops, ae)     \
17863   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
17864
17865 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
17866    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
17867    use the same encoding function for each.  */
17868 #define NUF(mnem, op, nops, ops, enc)                                   \
17869   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
17870     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
17871
17872 /* Neon data processing, version which indirects through neon_enc_tab for
17873    the various overloaded versions of opcodes.  */
17874 #define nUF(mnem, op, nops, ops, enc)                                   \
17875   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
17876     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
17877
17878 /* Neon insn with conditional suffix for the ARM version, non-overloaded
17879    version.  */
17880 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
17881   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
17882     THUMB_VARIANT, do_##enc, do_##enc }
17883
17884 #define NCE(mnem, op, nops, ops, enc)                                   \
17885    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
17886
17887 #define NCEF(mnem, op, nops, ops, enc)                                  \
17888     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
17889
17890 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
17891 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
17892   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
17893     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
17894
17895 #define nCE(mnem, op, nops, ops, enc)                                   \
17896    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
17897
17898 #define nCEF(mnem, op, nops, ops, enc)                                  \
17899     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
17900
17901 #define do_0 0
17902
17903 static const struct asm_opcode insns[] =
17904 {
17905 #define ARM_VARIANT &arm_ext_v1 /* Core ARM Instructions.  */
17906 #define THUMB_VARIANT &arm_ext_v4t
17907  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
17908  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
17909  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
17910  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
17911  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
17912  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
17913  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
17914  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
17915  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
17916  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
17917  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
17918  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
17919  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
17920  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
17921  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
17922  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
17923
17924  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
17925     for setting PSR flag bits.  They are obsolete in V6 and do not
17926     have Thumb equivalents. */
17927  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
17928  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
17929   CL("tstp",    110f000,           2, (RR, SH),      cmp),
17930  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
17931  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
17932   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
17933  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
17934  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
17935   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
17936
17937  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
17938  tC3("movs",    1b00000, _movs,    2, (RR, SH),      mov,  t_mov_cmp),
17939  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
17940  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
17941
17942  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
17943  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
17944  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
17945                                                                 OP_RRnpc),
17946                                         OP_ADDRGLDR),ldst, t_ldst),
17947  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
17948
17949  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17950  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17951  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17952  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17953  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17954  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17955
17956  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
17957  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
17958  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
17959  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
17960
17961   /* Pseudo ops.  */
17962  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
17963   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
17964  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
17965
17966   /* Thumb-compatibility pseudo ops.  */
17967  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
17968  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
17969  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
17970  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
17971  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
17972  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
17973  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
17974  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
17975  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
17976  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
17977  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
17978  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
17979
17980  /* These may simplify to neg.  */
17981  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
17982  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
17983
17984 #undef  THUMB_VARIANT
17985 #define THUMB_VARIANT  & arm_ext_v6
17986
17987  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
17988
17989  /* V1 instructions with no Thumb analogue prior to V6T2.  */
17990 #undef  THUMB_VARIANT
17991 #define THUMB_VARIANT  & arm_ext_v6t2
17992
17993  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
17994  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
17995   CL("teqp",    130f000,           2, (RR, SH),      cmp),
17996
17997  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17998  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17999  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
18000  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
18001
18002  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18003  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18004
18005  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18006  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18007
18008  /* V1 instructions with no Thumb analogue at all.  */
18009   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
18010   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
18011
18012   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
18013   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
18014   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
18015   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
18016   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
18017   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
18018   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
18019   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
18020
18021 #undef  ARM_VARIANT
18022 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
18023 #undef  THUMB_VARIANT
18024 #define THUMB_VARIANT  & arm_ext_v4t
18025
18026  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
18027  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
18028
18029 #undef  THUMB_VARIANT
18030 #define THUMB_VARIANT  & arm_ext_v6t2
18031
18032  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
18033   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
18034
18035   /* Generic coprocessor instructions.  */
18036  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
18037  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18038  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18039  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18040  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18041  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
18042  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
18043
18044 #undef  ARM_VARIANT
18045 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
18046
18047   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
18048   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
18049
18050 #undef  ARM_VARIANT
18051 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
18052 #undef  THUMB_VARIANT
18053 #define THUMB_VARIANT  & arm_ext_msr
18054
18055  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
18056  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
18057
18058 #undef  ARM_VARIANT
18059 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
18060 #undef  THUMB_VARIANT
18061 #define THUMB_VARIANT  & arm_ext_v6t2
18062
18063  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18064   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18065  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18066   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18067  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18068   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18069  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18070   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18071
18072 #undef  ARM_VARIANT
18073 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
18074 #undef  THUMB_VARIANT
18075 #define THUMB_VARIANT  & arm_ext_v4t
18076
18077  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18078  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18079  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18080  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18081  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18082  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18083
18084 #undef  ARM_VARIANT
18085 #define ARM_VARIANT  & arm_ext_v4t_5
18086
18087   /* ARM Architecture 4T.  */
18088   /* Note: bx (and blx) are required on V5, even if the processor does
18089      not support Thumb.  */
18090  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
18091
18092 #undef  ARM_VARIANT
18093 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
18094 #undef  THUMB_VARIANT
18095 #define THUMB_VARIANT  & arm_ext_v5t
18096
18097   /* Note: blx has 2 variants; the .value coded here is for
18098      BLX(2).  Only this variant has conditional execution.  */
18099  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
18100  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
18101
18102 #undef  THUMB_VARIANT
18103 #define THUMB_VARIANT  & arm_ext_v6t2
18104
18105  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
18106  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18107  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
18108  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18109  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
18110  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
18111  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
18112  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
18113
18114 #undef  ARM_VARIANT
18115 #define ARM_VARIANT  & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
18116 #undef THUMB_VARIANT
18117 #define THUMB_VARIANT &arm_ext_v5exp
18118
18119  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18120  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18121  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18122  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18123
18124  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18125  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18126
18127  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18128  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18129  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18130  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18131
18132  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18133  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18134  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18135  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18136
18137  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18138  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18139
18140  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18141  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18142  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18143  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18144
18145 #undef  ARM_VARIANT
18146 #define ARM_VARIANT  & arm_ext_v5e /*  ARM Architecture 5TE.  */
18147 #undef THUMB_VARIANT
18148 #define THUMB_VARIANT &arm_ext_v6t2
18149
18150  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
18151  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
18152      ldrd, t_ldstd),
18153  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
18154                                        ADDRGLDRS), ldrd, t_ldstd),
18155
18156  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18157  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18158
18159 #undef  ARM_VARIANT
18160 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
18161
18162  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
18163
18164 #undef  ARM_VARIANT
18165 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
18166 #undef  THUMB_VARIANT
18167 #define THUMB_VARIANT  & arm_ext_v6
18168
18169  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
18170  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
18171  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
18172  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
18173  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
18174  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18175  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18176  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18177  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18178  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
18179
18180 #undef  THUMB_VARIANT
18181 #define THUMB_VARIANT  & arm_ext_v6t2
18182
18183  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
18184  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
18185                                       strex,  t_strex),
18186  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18187  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18188
18189  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
18190  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
18191
18192 /*  ARM V6 not included in V7M.  */
18193 #undef  THUMB_VARIANT
18194 #define THUMB_VARIANT  & arm_ext_v6_notm
18195  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
18196  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
18197   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
18198   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
18199  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
18200  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
18201   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
18202  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
18203   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
18204  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
18205  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
18206  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
18207   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
18208   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
18209   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
18210   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
18211  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
18212  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
18213
18214 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
18215 #undef  THUMB_VARIANT
18216 #define THUMB_VARIANT  & arm_ext_v6_dsp
18217  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
18218  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
18219  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
18220  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18221  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18222  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18223  /* Old name for QASX.  */
18224  TCE("qaddsubx",        6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18225  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18226  /* Old name for QSAX.  */
18227  TCE("qsubaddx",        6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18228  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18229  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18230  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18231  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18232  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18233  /* Old name for SASX.  */
18234  TCE("saddsubx",        6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18235  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18236  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18237  TCE("shasx",     6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18238  /* Old name for SHASX.  */
18239  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18240  TCE("shsax",      6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),    rd_rn_rm, t_simd),
18241  /* Old name for SHSAX.  */
18242  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18243  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18244  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18245  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18246  /* Old name for SSAX.  */
18247  TCE("ssubaddx",        6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18248  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18249  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18250  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18251  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18252  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18253  /* Old name for UASX.  */
18254  TCE("uaddsubx",        6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18255  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18256  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18257  TCE("uhasx",     6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18258  /* Old name for UHASX.  */
18259  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18260  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18261  /* Old name for UHSAX.  */
18262  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18263  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18264  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18265  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18266  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18267  TCE("uqasx",     6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18268  /* Old name for UQASX.  */
18269  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18270  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18271  /* Old name for UQSAX.  */
18272  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18273  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18274  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18275  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18276  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18277  /* Old name for USAX.  */
18278  TCE("usubaddx",        6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18279  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18280  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18281  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18282  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18283  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
18284  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18285  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18286  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18287  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
18288  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18289  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18290  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18291  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18292  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18293  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18294  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18295  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18296  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18297  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18298  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18299  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18300  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18301  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18302  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18303  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18304  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18305  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18306  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
18307  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
18308  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
18309  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
18310  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
18311  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
18312
18313 #undef  ARM_VARIANT
18314 #define ARM_VARIANT   & arm_ext_v6k
18315 #undef  THUMB_VARIANT
18316 #define THUMB_VARIANT & arm_ext_v6k
18317
18318  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
18319  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
18320  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
18321  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
18322
18323 #undef  THUMB_VARIANT
18324 #define THUMB_VARIANT  & arm_ext_v6_notm
18325  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
18326                                       ldrexd, t_ldrexd),
18327  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
18328                                        RRnpcb), strexd, t_strexd),
18329
18330 #undef  THUMB_VARIANT
18331 #define THUMB_VARIANT  & arm_ext_v6t2
18332  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
18333      rd_rn,  rd_rn),
18334  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
18335      rd_rn,  rd_rn),
18336  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
18337      strex, t_strexbh),
18338  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
18339      strex, t_strexbh),
18340  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
18341
18342 #undef  ARM_VARIANT
18343 #define ARM_VARIANT    & arm_ext_sec
18344 #undef THUMB_VARIANT
18345 #define THUMB_VARIANT  & arm_ext_sec
18346
18347  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
18348
18349 #undef  ARM_VARIANT
18350 #define ARM_VARIANT    & arm_ext_virt
18351 #undef  THUMB_VARIANT
18352 #define THUMB_VARIANT    & arm_ext_virt
18353
18354  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
18355  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
18356
18357 #undef  ARM_VARIANT
18358 #define ARM_VARIANT  & arm_ext_v6t2
18359 #undef  THUMB_VARIANT
18360 #define THUMB_VARIANT  & arm_ext_v6t2
18361
18362  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
18363  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
18364  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
18365  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
18366
18367  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
18368  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
18369  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
18370  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
18371
18372  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
18373  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
18374  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
18375  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
18376
18377  /* Thumb-only instructions.  */
18378 #undef ARM_VARIANT
18379 #define ARM_VARIANT NULL
18380   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
18381   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
18382
18383  /* ARM does not really have an IT instruction, so always allow it.
18384     The opcode is copied from Thumb in order to allow warnings in
18385     -mimplicit-it=[never | arm] modes.  */
18386 #undef  ARM_VARIANT
18387 #define ARM_VARIANT  & arm_ext_v1
18388
18389  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
18390  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
18391  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
18392  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
18393  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
18394  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
18395  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
18396  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
18397  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
18398  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
18399  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
18400  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
18401  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
18402  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
18403  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
18404  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
18405  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
18406  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
18407
18408  /* Thumb2 only instructions.  */
18409 #undef  ARM_VARIANT
18410 #define ARM_VARIANT  NULL
18411
18412  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
18413  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
18414  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
18415  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
18416  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
18417  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
18418
18419  /* Hardware division instructions.  */
18420 #undef  ARM_VARIANT
18421 #define ARM_VARIANT    & arm_ext_adiv
18422 #undef  THUMB_VARIANT
18423 #define THUMB_VARIANT  & arm_ext_div
18424
18425  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
18426  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
18427
18428  /* ARM V6M/V7 instructions.  */
18429 #undef  ARM_VARIANT
18430 #define ARM_VARIANT    & arm_ext_barrier
18431 #undef  THUMB_VARIANT
18432 #define THUMB_VARIANT  & arm_ext_barrier
18433
18434  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier,  t_barrier),
18435  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier,  t_barrier),
18436  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier,  t_barrier),
18437
18438  /* ARM V7 instructions.  */
18439 #undef  ARM_VARIANT
18440 #define ARM_VARIANT    & arm_ext_v7
18441 #undef  THUMB_VARIANT
18442 #define THUMB_VARIANT  & arm_ext_v7
18443
18444  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
18445  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
18446
18447 #undef ARM_VARIANT
18448 #define ARM_VARIANT    & arm_ext_mp
18449 #undef THUMB_VARIANT
18450 #define THUMB_VARIANT  & arm_ext_mp
18451
18452  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
18453
18454  /* AArchv8 instructions.  */
18455 #undef  ARM_VARIANT
18456 #define ARM_VARIANT   & arm_ext_v8
18457 #undef  THUMB_VARIANT
18458 #define THUMB_VARIANT & arm_ext_v8
18459
18460  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
18461  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
18462  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
18463  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
18464                                                         ldrexd, t_ldrexd),
18465  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
18466  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
18467  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
18468                                                         stlex,  t_stlex),
18469  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
18470                                                         strexd, t_strexd),
18471  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
18472                                                         stlex, t_stlex),
18473  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
18474                                                         stlex, t_stlex),
18475  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
18476  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
18477  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
18478  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
18479  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
18480  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
18481
18482  /* ARMv8 T32 only.  */
18483 #undef ARM_VARIANT
18484 #define ARM_VARIANT  NULL
18485  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
18486  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
18487  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
18488
18489   /* FP for ARMv8.  */
18490 #undef  ARM_VARIANT
18491 #define ARM_VARIANT & fpu_vfp_ext_armv8
18492 #undef  THUMB_VARIANT
18493 #define THUMB_VARIANT & fpu_vfp_ext_armv8
18494
18495   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
18496   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
18497   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
18498   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
18499   nUF(vmaxnm, _vmaxnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
18500   nUF(vminnm, _vminnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
18501   nUF(vcvta,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvta),
18502   nUF(vcvtn,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtn),
18503   nUF(vcvtp,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtp),
18504   nUF(vcvtm,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtm),
18505   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
18506   nCE(vrintz, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintz),
18507   nCE(vrintx, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintx),
18508   nUF(vrinta, _vrinta, 2, (RNSDQ, oRNSDQ),              vrinta),
18509   nUF(vrintn, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintn),
18510   nUF(vrintp, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintp),
18511   nUF(vrintm, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintm),
18512
18513   /* Crypto v1 extensions.  */
18514 #undef  ARM_VARIANT
18515 #define ARM_VARIANT & fpu_crypto_ext_armv8
18516 #undef  THUMB_VARIANT
18517 #define THUMB_VARIANT & fpu_crypto_ext_armv8
18518
18519   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
18520   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
18521   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
18522   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
18523   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
18524   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
18525   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
18526   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
18527   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
18528   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
18529   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
18530   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
18531   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
18532   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
18533
18534 #undef  ARM_VARIANT
18535 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
18536 #undef  THUMB_VARIANT
18537 #define THUMB_VARIANT NULL
18538
18539  cCE("wfs",     e200110, 1, (RR),            rd),
18540  cCE("rfs",     e300110, 1, (RR),            rd),
18541  cCE("wfc",     e400110, 1, (RR),            rd),
18542  cCE("rfc",     e500110, 1, (RR),            rd),
18543
18544  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
18545  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
18546  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
18547  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
18548
18549  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
18550  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
18551  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
18552  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
18553
18554  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
18555  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
18556  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
18557  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
18558  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
18559  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
18560  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
18561  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
18562  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
18563  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
18564  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
18565  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
18566
18567  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
18568  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
18569  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
18570  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
18571  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
18572  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
18573  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
18574  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
18575  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
18576  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
18577  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
18578  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
18579
18580  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
18581  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
18582  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
18583  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
18584  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
18585  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
18586  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
18587  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
18588  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
18589  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
18590  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
18591  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
18592
18593  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
18594  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
18595  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
18596  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
18597  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
18598  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
18599  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
18600  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
18601  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
18602  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
18603  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
18604  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
18605
18606  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
18607  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
18608  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
18609  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
18610  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
18611  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
18612  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
18613  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
18614  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
18615  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
18616  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
18617  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
18618
18619  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
18620  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
18621  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
18622  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
18623  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
18624  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
18625  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
18626  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
18627  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
18628  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
18629  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
18630  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
18631
18632  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
18633  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
18634  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
18635  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
18636  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
18637  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
18638  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
18639  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
18640  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
18641  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
18642  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
18643  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
18644
18645  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
18646  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
18647  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
18648  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
18649  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
18650  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
18651  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
18652  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
18653  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
18654  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
18655  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
18656  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
18657
18658  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
18659  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
18660  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
18661  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
18662  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
18663  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
18664  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
18665  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
18666  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
18667  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
18668  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
18669  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
18670
18671  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
18672  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
18673  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
18674  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
18675  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
18676  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
18677  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
18678  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
18679  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
18680  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
18681  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
18682  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
18683
18684  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
18685  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
18686  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
18687  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
18688  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
18689  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
18690  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
18691  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
18692  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
18693  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
18694  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
18695  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
18696
18697  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
18698  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
18699  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
18700  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
18701  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
18702  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
18703  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
18704  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
18705  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
18706  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
18707  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
18708  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
18709
18710  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
18711  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
18712  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
18713  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
18714  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
18715  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
18716  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
18717  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
18718  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
18719  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
18720  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
18721  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
18722
18723  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
18724  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
18725  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
18726  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
18727  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
18728  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
18729  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
18730  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
18731  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
18732  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
18733  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
18734  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
18735
18736  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
18737  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
18738  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
18739  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
18740  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
18741  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
18742  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
18743  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
18744  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
18745  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
18746  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
18747  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
18748
18749  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
18750  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
18751  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
18752  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
18753  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
18754  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
18755  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
18756  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
18757  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
18758  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
18759  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
18760  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
18761
18762  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
18763  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
18764  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
18765  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
18766  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
18767  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18768  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18769  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18770  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
18771  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
18772  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
18773  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
18774
18775  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
18776  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
18777  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
18778  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
18779  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
18780  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18781  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18782  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18783  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
18784  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
18785  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
18786  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
18787
18788  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
18789  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
18790  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
18791  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
18792  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
18793  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18794  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18795  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18796  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
18797  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
18798  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
18799  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
18800
18801  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
18802  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
18803  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
18804  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
18805  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
18806  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18807  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18808  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18809  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
18810  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
18811  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
18812  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
18813
18814  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
18815  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
18816  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
18817  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
18818  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
18819  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18820  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18821  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18822  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
18823  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
18824  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
18825  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
18826
18827  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
18828  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
18829  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
18830  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
18831  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
18832  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18833  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18834  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18835  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
18836  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
18837  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
18838  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
18839
18840  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
18841  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
18842  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
18843  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
18844  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
18845  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18846  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18847  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18848  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
18849  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
18850  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
18851  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
18852
18853  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
18854  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
18855  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
18856  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
18857  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
18858  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18859  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18860  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18861  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
18862  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
18863  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
18864  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
18865
18866  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
18867  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
18868  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
18869  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
18870  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
18871  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18872  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18873  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18874  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
18875  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
18876  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
18877  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
18878
18879  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
18880  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
18881  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
18882  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
18883  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
18884  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18885  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18886  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18887  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
18888  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
18889  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
18890  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
18891
18892  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
18893  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
18894  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
18895  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
18896  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
18897  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18898  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18899  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18900  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
18901  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
18902  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
18903  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
18904
18905  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
18906  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
18907  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
18908  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
18909  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
18910  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18911  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18912  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18913  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
18914  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
18915  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
18916  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
18917
18918  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
18919  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
18920  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
18921  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
18922  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
18923  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18924  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18925  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18926  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
18927  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
18928  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
18929  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
18930
18931  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
18932  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
18933  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
18934  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
18935
18936  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
18937  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
18938  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
18939  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
18940  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
18941  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
18942  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
18943  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
18944  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
18945  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
18946  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
18947  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
18948
18949   /* The implementation of the FIX instruction is broken on some
18950      assemblers, in that it accepts a precision specifier as well as a
18951      rounding specifier, despite the fact that this is meaningless.
18952      To be more compatible, we accept it as well, though of course it
18953      does not set any bits.  */
18954  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
18955  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
18956  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
18957  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
18958  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
18959  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
18960  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
18961  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
18962  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
18963  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
18964  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
18965  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
18966  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
18967
18968   /* Instructions that were new with the real FPA, call them V2.  */
18969 #undef  ARM_VARIANT
18970 #define ARM_VARIANT  & fpu_fpa_ext_v2
18971
18972  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18973  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18974  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18975  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18976  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18977  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18978
18979 #undef  ARM_VARIANT
18980 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
18981
18982   /* Moves and type conversions.  */
18983  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
18984  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
18985  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
18986  cCE("fmstat",  ef1fa10, 0, (),               noargs),
18987  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
18988  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
18989  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18990  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
18991  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
18992  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18993  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
18994  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18995  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
18996  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
18997
18998   /* Memory operations.  */
18999  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
19000  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
19001  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19002  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19003  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19004  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19005  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19006  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19007  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19008  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19009  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19010  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19011  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19012  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19013  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19014  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19015  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19016  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19017
19018   /* Monadic operations.  */
19019  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19020  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
19021  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19022
19023   /* Dyadic operations.  */
19024  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19025  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19026  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19027  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19028  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19029  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19030  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19031  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19032  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19033
19034   /* Comparisons.  */
19035  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
19036  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
19037  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19038  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
19039
19040  /* Double precision load/store are still present on single precision
19041     implementations.  */
19042  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
19043  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
19044  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19045  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19046  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19047  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19048  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19049  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19050  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19051  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19052
19053 #undef  ARM_VARIANT
19054 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
19055
19056   /* Moves and type conversions.  */
19057  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
19058  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
19059  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19060  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
19061  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
19062  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
19063  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
19064  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
19065  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
19066  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19067  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19068  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19069  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19070
19071   /* Monadic operations.  */
19072  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
19073  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
19074  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
19075
19076   /* Dyadic operations.  */
19077  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19078  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19079  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19080  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19081  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19082  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19083  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19084  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19085  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19086
19087   /* Comparisons.  */
19088  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
19089  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
19090  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
19091  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
19092
19093 #undef  ARM_VARIANT
19094 #define ARM_VARIANT  & fpu_vfp_ext_v2
19095
19096  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
19097  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
19098  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
19099  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
19100
19101 /* Instructions which may belong to either the Neon or VFP instruction sets.
19102    Individual encoder functions perform additional architecture checks.  */
19103 #undef  ARM_VARIANT
19104 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
19105 #undef  THUMB_VARIANT
19106 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
19107
19108   /* These mnemonics are unique to VFP.  */
19109  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
19110  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
19111  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19112  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19113  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19114  nCE(vcmp,      _vcmp,    2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
19115  nCE(vcmpe,     _vcmpe,   2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
19116  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
19117  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
19118  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
19119
19120   /* Mnemonics shared by Neon and VFP.  */
19121  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
19122  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
19123  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
19124
19125  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
19126  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
19127
19128  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
19129  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
19130
19131  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19132  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19133  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19134  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19135  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19136  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19137  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
19138  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
19139
19140  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
19141  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
19142  NCEF(vcvtb,    eb20a40, 2, (RVSD, RVSD), neon_cvtb),
19143  NCEF(vcvtt,    eb20a40, 2, (RVSD, RVSD), neon_cvtt),
19144
19145
19146   /* NOTE: All VMOV encoding is special-cased!  */
19147  NCE(vmov,      0,       1, (VMOV), neon_mov),
19148  NCE(vmovq,     0,       1, (VMOV), neon_mov),
19149
19150 #undef  THUMB_VARIANT
19151 #define THUMB_VARIANT  & fpu_neon_ext_v1
19152 #undef  ARM_VARIANT
19153 #define ARM_VARIANT    & fpu_neon_ext_v1
19154
19155   /* Data processing with three registers of the same length.  */
19156   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
19157  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
19158  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
19159  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
19160  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
19161  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
19162  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
19163  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
19164  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
19165   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
19166  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
19167  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
19168  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
19169  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
19170  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
19171  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
19172  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
19173  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
19174   /* If not immediate, fall back to neon_dyadic_i64_su.
19175      shl_imm should accept I8 I16 I32 I64,
19176      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
19177  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
19178  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
19179  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
19180  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
19181   /* Logic ops, types optional & ignored.  */
19182  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19183  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19184  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19185  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19186  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19187  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19188  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19189  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19190  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
19191  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
19192   /* Bitfield ops, untyped.  */
19193  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
19194  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
19195  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
19196  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
19197  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
19198  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
19199   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F32.  */
19200  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
19201  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
19202  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
19203  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
19204  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
19205  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
19206   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
19207      back to neon_dyadic_if_su.  */
19208  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
19209  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
19210  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
19211  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
19212  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
19213  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
19214  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
19215  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
19216   /* Comparison. Type I8 I16 I32 F32.  */
19217  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
19218  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
19219   /* As above, D registers only.  */
19220  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
19221  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
19222   /* Int and float variants, signedness unimportant.  */
19223  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
19224  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
19225  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
19226   /* Add/sub take types I8 I16 I32 I64 F32.  */
19227  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
19228  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
19229   /* vtst takes sizes 8, 16, 32.  */
19230  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
19231  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
19232   /* VMUL takes I8 I16 I32 F32 P8.  */
19233  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
19234   /* VQD{R}MULH takes S16 S32.  */
19235  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19236  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19237  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19238  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19239  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
19240  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
19241  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
19242  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
19243  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
19244  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
19245  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
19246  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
19247  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
19248  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
19249  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
19250  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
19251
19252   /* Two address, int/float. Types S8 S16 S32 F32.  */
19253  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
19254  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
19255
19256   /* Data processing with two registers and a shift amount.  */
19257   /* Right shifts, and variants with rounding.
19258      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
19259  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
19260  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
19261  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
19262  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
19263  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
19264  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
19265  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
19266  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
19267   /* Shift and insert. Sizes accepted 8 16 32 64.  */
19268  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
19269  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
19270  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
19271  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
19272   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
19273  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
19274  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
19275   /* Right shift immediate, saturating & narrowing, with rounding variants.
19276      Types accepted S16 S32 S64 U16 U32 U64.  */
19277  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
19278  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
19279   /* As above, unsigned. Types accepted S16 S32 S64.  */
19280  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
19281  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
19282   /* Right shift narrowing. Types accepted I16 I32 I64.  */
19283  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
19284  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
19285   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
19286  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
19287   /* CVT with optional immediate for fixed-point variant.  */
19288  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
19289
19290  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
19291  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
19292
19293   /* Data processing, three registers of different lengths.  */
19294   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
19295  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
19296  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
19297  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
19298  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
19299   /* If not scalar, fall back to neon_dyadic_long.
19300      Vector types as above, scalar types S16 S32 U16 U32.  */
19301  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
19302  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
19303   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
19304  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
19305  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
19306   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
19307  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
19308  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
19309  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
19310  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
19311   /* Saturating doubling multiplies. Types S16 S32.  */
19312  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
19313  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
19314  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
19315   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
19316      S16 S32 U16 U32.  */
19317  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
19318
19319   /* Extract. Size 8.  */
19320  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
19321  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
19322
19323   /* Two registers, miscellaneous.  */
19324   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
19325  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
19326  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
19327  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
19328  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
19329  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
19330  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
19331   /* Vector replicate. Sizes 8 16 32.  */
19332  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
19333  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
19334   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
19335  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
19336   /* VMOVN. Types I16 I32 I64.  */
19337  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
19338   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
19339  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
19340   /* VQMOVUN. Types S16 S32 S64.  */
19341  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
19342   /* VZIP / VUZP. Sizes 8 16 32.  */
19343  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
19344  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
19345  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
19346  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
19347   /* VQABS / VQNEG. Types S8 S16 S32.  */
19348  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
19349  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
19350  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
19351  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
19352   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
19353  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
19354  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
19355  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
19356  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
19357   /* Reciprocal estimates. Types U32 F32.  */
19358  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
19359  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
19360  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
19361  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
19362   /* VCLS. Types S8 S16 S32.  */
19363  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
19364  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
19365   /* VCLZ. Types I8 I16 I32.  */
19366  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
19367  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
19368   /* VCNT. Size 8.  */
19369  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
19370  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
19371   /* Two address, untyped.  */
19372  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
19373  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
19374   /* VTRN. Sizes 8 16 32.  */
19375  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
19376  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
19377
19378   /* Table lookup. Size 8.  */
19379  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
19380  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
19381
19382 #undef  THUMB_VARIANT
19383 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
19384 #undef  ARM_VARIANT
19385 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
19386
19387   /* Neon element/structure load/store.  */
19388  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19389  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19390  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19391  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19392  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19393  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19394  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19395  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
19396
19397 #undef  THUMB_VARIANT
19398 #define THUMB_VARIANT &fpu_vfp_ext_v3xd
19399 #undef ARM_VARIANT
19400 #define ARM_VARIANT &fpu_vfp_ext_v3xd
19401  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
19402  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
19403  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
19404  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
19405  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
19406  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
19407  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
19408  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
19409  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
19410
19411 #undef THUMB_VARIANT
19412 #define THUMB_VARIANT  & fpu_vfp_ext_v3
19413 #undef  ARM_VARIANT
19414 #define ARM_VARIANT    & fpu_vfp_ext_v3
19415
19416  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
19417  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
19418  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
19419  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
19420  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
19421  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
19422  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
19423  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
19424  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
19425
19426 #undef ARM_VARIANT
19427 #define ARM_VARIANT &fpu_vfp_ext_fma
19428 #undef THUMB_VARIANT
19429 #define THUMB_VARIANT &fpu_vfp_ext_fma
19430  /* Mnemonics shared by Neon and VFP.  These are included in the
19431     VFP FMA variant; NEON and VFP FMA always includes the NEON
19432     FMA instructions.  */
19433  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
19434  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
19435  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
19436     the v form should always be used.  */
19437  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19438  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19439  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19440  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19441  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19442  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19443
19444 #undef THUMB_VARIANT
19445 #undef  ARM_VARIANT
19446 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
19447
19448  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
19449  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
19450  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
19451  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
19452  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
19453  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
19454  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
19455  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
19456
19457 #undef  ARM_VARIANT
19458 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
19459
19460  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
19461  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
19462  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
19463  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
19464  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
19465  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
19466  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
19467  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
19468  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
19469  cCE("textrmub",        e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
19470  cCE("textrmuh",        e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
19471  cCE("textrmuw",        e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
19472  cCE("textrmsb",        e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
19473  cCE("textrmsh",        e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
19474  cCE("textrmsw",        e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
19475  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
19476  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
19477  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
19478  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
19479  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
19480  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
19481  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
19482  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
19483  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
19484  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
19485  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
19486  cCE("tmovmskb",        e100030, 2, (RR, RIWR),             rd_rn),
19487  cCE("tmovmskh",        e500030, 2, (RR, RIWR),             rd_rn),
19488  cCE("tmovmskw",        e900030, 2, (RR, RIWR),             rd_rn),
19489  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
19490  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
19491  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
19492  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
19493  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
19494  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
19495  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
19496  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
19497  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19498  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19499  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19500  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19501  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19502  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19503  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19504  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19505  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19506  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
19507  cCE("walignr0",        e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19508  cCE("walignr1",        e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19509  cCE("walignr2",        ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19510  cCE("walignr3",        eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19511  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19512  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19513  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19514  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19515  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19516  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19517  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19518  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19519  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19520  cCE("wcmpgtub",        e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19521  cCE("wcmpgtuh",        e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19522  cCE("wcmpgtuw",        e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19523  cCE("wcmpgtsb",        e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19524  cCE("wcmpgtsh",        e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19525  cCE("wcmpgtsw",        eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19526  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
19527  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
19528  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
19529  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
19530  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19531  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19532  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19533  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19534  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19535  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19536  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19537  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19538  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19539  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19540  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19541  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19542  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19543  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19544  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19545  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19546  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19547  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19548  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
19549  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19550  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19551  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19552  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19553  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19554  cCE("wpackhss",        e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19555  cCE("wpackhus",        e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19556  cCE("wpackwss",        eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19557  cCE("wpackwus",        e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19558  cCE("wpackdss",        ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19559  cCE("wpackdus",        ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19560  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19561  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19562  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19563  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19564  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19565  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19566  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19567  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19568  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19569  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19570  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
19571  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19572  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19573  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19574  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19575  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19576  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19577  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19578  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19579  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19580  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19581  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19582  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19583  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19584  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19585  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19586  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19587  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
19588  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
19589  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
19590  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
19591  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
19592  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
19593  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19594  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19595  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19596  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19597  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19598  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19599  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19600  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19601  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19602  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
19603  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
19604  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
19605  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
19606  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
19607  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
19608  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
19609  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
19610  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
19611  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
19612  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
19613  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
19614  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
19615  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
19616  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
19617  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
19618  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
19619  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
19620  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19621  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
19622
19623 #undef  ARM_VARIANT
19624 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
19625
19626  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
19627  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
19628  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
19629  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
19630  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
19631  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
19632  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19633  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19634  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19635  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19636  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19637  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19638  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19639  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19640  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19641  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19642  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19643  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19644  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19645  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19646  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
19647  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19648  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19649  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19650  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19651  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19652  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19653  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19654  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19655  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19656  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19657  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19658  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19659  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19660  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19661  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19662  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19663  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19664  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19665  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19666  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19667  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19668  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19669  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19670  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19671  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19672  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19673  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19674  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19675  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19676  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19677  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19678  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19679  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19680  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19681  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19682  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
19683
19684 #undef  ARM_VARIANT
19685 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
19686
19687  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
19688  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
19689  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
19690  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
19691  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
19692  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
19693  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
19694  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
19695  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
19696  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
19697  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
19698  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
19699  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
19700  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
19701  cCE("cfmv64lr",        e000510, 2, (RMDX, RR),               rn_rd),
19702  cCE("cfmvr64l",        e100510, 2, (RR, RMDX),               rd_rn),
19703  cCE("cfmv64hr",        e000530, 2, (RMDX, RR),               rn_rd),
19704  cCE("cfmvr64h",        e100530, 2, (RR, RMDX),               rd_rn),
19705  cCE("cfmval32",        e200440, 2, (RMAX, RMFX),             rd_rn),
19706  cCE("cfmv32al",        e100440, 2, (RMFX, RMAX),             rd_rn),
19707  cCE("cfmvam32",        e200460, 2, (RMAX, RMFX),             rd_rn),
19708  cCE("cfmv32am",        e100460, 2, (RMFX, RMAX),             rd_rn),
19709  cCE("cfmvah32",        e200480, 2, (RMAX, RMFX),             rd_rn),
19710  cCE("cfmv32ah",        e100480, 2, (RMFX, RMAX),             rd_rn),
19711  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
19712  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
19713  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
19714  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
19715  cCE("cfmvsc32",        e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
19716  cCE("cfmv32sc",        e1004e0, 2, (RMDX, RMDS),             rd),
19717  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
19718  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
19719  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
19720  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
19721  cCE("cfcvt32s",        e000480, 2, (RMF, RMFX),              rd_rn),
19722  cCE("cfcvt32d",        e0004a0, 2, (RMD, RMFX),              rd_rn),
19723  cCE("cfcvt64s",        e0004c0, 2, (RMF, RMDX),              rd_rn),
19724  cCE("cfcvt64d",        e0004e0, 2, (RMD, RMDX),              rd_rn),
19725  cCE("cfcvts32",        e100580, 2, (RMFX, RMF),              rd_rn),
19726  cCE("cfcvtd32",        e1005a0, 2, (RMFX, RMD),              rd_rn),
19727  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
19728  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
19729  cCE("cfrshl32",        e000550, 3, (RMFX, RMFX, RR),         mav_triple),
19730  cCE("cfrshl64",        e000570, 3, (RMDX, RMDX, RR),         mav_triple),
19731  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
19732  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
19733  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
19734  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
19735  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
19736  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
19737  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
19738  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
19739  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
19740  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
19741  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
19742  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
19743  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
19744  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
19745  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
19746  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
19747  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
19748  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
19749  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
19750  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
19751  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
19752  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
19753  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
19754  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
19755  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
19756  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
19757  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
19758  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
19759  cCE("cfmadd32",        e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
19760  cCE("cfmsub32",        e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
19761  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
19762  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
19763 };
19764 #undef ARM_VARIANT
19765 #undef THUMB_VARIANT
19766 #undef TCE
19767 #undef TUE
19768 #undef TUF
19769 #undef TCC
19770 #undef cCE
19771 #undef cCL
19772 #undef C3E
19773 #undef CE
19774 #undef CM
19775 #undef UE
19776 #undef UF
19777 #undef UT
19778 #undef NUF
19779 #undef nUF
19780 #undef NCE
19781 #undef nCE
19782 #undef OPS0
19783 #undef OPS1
19784 #undef OPS2
19785 #undef OPS3
19786 #undef OPS4
19787 #undef OPS5
19788 #undef OPS6
19789 #undef do_0
19790 \f
19791 /* MD interface: bits in the object file.  */
19792
19793 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
19794    for use in the a.out file, and stores them in the array pointed to by buf.
19795    This knows about the endian-ness of the target machine and does
19796    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
19797    2 (short) and 4 (long)  Floating numbers are put out as a series of
19798    LITTLENUMS (shorts, here at least).  */
19799
19800 void
19801 md_number_to_chars (char * buf, valueT val, int n)
19802 {
19803   if (target_big_endian)
19804     number_to_chars_bigendian (buf, val, n);
19805   else
19806     number_to_chars_littleendian (buf, val, n);
19807 }
19808
19809 static valueT
19810 md_chars_to_number (char * buf, int n)
19811 {
19812   valueT result = 0;
19813   unsigned char * where = (unsigned char *) buf;
19814
19815   if (target_big_endian)
19816     {
19817       while (n--)
19818         {
19819           result <<= 8;
19820           result |= (*where++ & 255);
19821         }
19822     }
19823   else
19824     {
19825       while (n--)
19826         {
19827           result <<= 8;
19828           result |= (where[n] & 255);
19829         }
19830     }
19831
19832   return result;
19833 }
19834
19835 /* MD interface: Sections.  */
19836
19837 /* Calculate the maximum variable size (i.e., excluding fr_fix)
19838    that an rs_machine_dependent frag may reach.  */
19839
19840 unsigned int
19841 arm_frag_max_var (fragS *fragp)
19842 {
19843   /* We only use rs_machine_dependent for variable-size Thumb instructions,
19844      which are either THUMB_SIZE (2) or INSN_SIZE (4).
19845
19846      Note that we generate relaxable instructions even for cases that don't
19847      really need it, like an immediate that's a trivial constant.  So we're
19848      overestimating the instruction size for some of those cases.  Rather
19849      than putting more intelligence here, it would probably be better to
19850      avoid generating a relaxation frag in the first place when it can be
19851      determined up front that a short instruction will suffice.  */
19852
19853   gas_assert (fragp->fr_type == rs_machine_dependent);
19854   return INSN_SIZE;
19855 }
19856
19857 /* Estimate the size of a frag before relaxing.  Assume everything fits in
19858    2 bytes.  */
19859
19860 int
19861 md_estimate_size_before_relax (fragS * fragp,
19862                                segT    segtype ATTRIBUTE_UNUSED)
19863 {
19864   fragp->fr_var = 2;
19865   return 2;
19866 }
19867
19868 /* Convert a machine dependent frag.  */
19869
19870 void
19871 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
19872 {
19873   unsigned long insn;
19874   unsigned long old_op;
19875   char *buf;
19876   expressionS exp;
19877   fixS *fixp;
19878   int reloc_type;
19879   int pc_rel;
19880   int opcode;
19881
19882   buf = fragp->fr_literal + fragp->fr_fix;
19883
19884   old_op = bfd_get_16(abfd, buf);
19885   if (fragp->fr_symbol)
19886     {
19887       exp.X_op = O_symbol;
19888       exp.X_add_symbol = fragp->fr_symbol;
19889     }
19890   else
19891     {
19892       exp.X_op = O_constant;
19893     }
19894   exp.X_add_number = fragp->fr_offset;
19895   opcode = fragp->fr_subtype;
19896   switch (opcode)
19897     {
19898     case T_MNEM_ldr_pc:
19899     case T_MNEM_ldr_pc2:
19900     case T_MNEM_ldr_sp:
19901     case T_MNEM_str_sp:
19902     case T_MNEM_ldr:
19903     case T_MNEM_ldrb:
19904     case T_MNEM_ldrh:
19905     case T_MNEM_str:
19906     case T_MNEM_strb:
19907     case T_MNEM_strh:
19908       if (fragp->fr_var == 4)
19909         {
19910           insn = THUMB_OP32 (opcode);
19911           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
19912             {
19913               insn |= (old_op & 0x700) << 4;
19914             }
19915           else
19916             {
19917               insn |= (old_op & 7) << 12;
19918               insn |= (old_op & 0x38) << 13;
19919             }
19920           insn |= 0x00000c00;
19921           put_thumb32_insn (buf, insn);
19922           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
19923         }
19924       else
19925         {
19926           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
19927         }
19928       pc_rel = (opcode == T_MNEM_ldr_pc2);
19929       break;
19930     case T_MNEM_adr:
19931       if (fragp->fr_var == 4)
19932         {
19933           insn = THUMB_OP32 (opcode);
19934           insn |= (old_op & 0xf0) << 4;
19935           put_thumb32_insn (buf, insn);
19936           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
19937         }
19938       else
19939         {
19940           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
19941           exp.X_add_number -= 4;
19942         }
19943       pc_rel = 1;
19944       break;
19945     case T_MNEM_mov:
19946     case T_MNEM_movs:
19947     case T_MNEM_cmp:
19948     case T_MNEM_cmn:
19949       if (fragp->fr_var == 4)
19950         {
19951           int r0off = (opcode == T_MNEM_mov
19952                        || opcode == T_MNEM_movs) ? 0 : 8;
19953           insn = THUMB_OP32 (opcode);
19954           insn = (insn & 0xe1ffffff) | 0x10000000;
19955           insn |= (old_op & 0x700) << r0off;
19956           put_thumb32_insn (buf, insn);
19957           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
19958         }
19959       else
19960         {
19961           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
19962         }
19963       pc_rel = 0;
19964       break;
19965     case T_MNEM_b:
19966       if (fragp->fr_var == 4)
19967         {
19968           insn = THUMB_OP32(opcode);
19969           put_thumb32_insn (buf, insn);
19970           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
19971         }
19972       else
19973         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
19974       pc_rel = 1;
19975       break;
19976     case T_MNEM_bcond:
19977       if (fragp->fr_var == 4)
19978         {
19979           insn = THUMB_OP32(opcode);
19980           insn |= (old_op & 0xf00) << 14;
19981           put_thumb32_insn (buf, insn);
19982           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
19983         }
19984       else
19985         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
19986       pc_rel = 1;
19987       break;
19988     case T_MNEM_add_sp:
19989     case T_MNEM_add_pc:
19990     case T_MNEM_inc_sp:
19991     case T_MNEM_dec_sp:
19992       if (fragp->fr_var == 4)
19993         {
19994           /* ??? Choose between add and addw.  */
19995           insn = THUMB_OP32 (opcode);
19996           insn |= (old_op & 0xf0) << 4;
19997           put_thumb32_insn (buf, insn);
19998           if (opcode == T_MNEM_add_pc)
19999             reloc_type = BFD_RELOC_ARM_T32_IMM12;
20000           else
20001             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
20002         }
20003       else
20004         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
20005       pc_rel = 0;
20006       break;
20007
20008     case T_MNEM_addi:
20009     case T_MNEM_addis:
20010     case T_MNEM_subi:
20011     case T_MNEM_subis:
20012       if (fragp->fr_var == 4)
20013         {
20014           insn = THUMB_OP32 (opcode);
20015           insn |= (old_op & 0xf0) << 4;
20016           insn |= (old_op & 0xf) << 16;
20017           put_thumb32_insn (buf, insn);
20018           if (insn & (1 << 20))
20019             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
20020           else
20021             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
20022         }
20023       else
20024         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
20025       pc_rel = 0;
20026       break;
20027     default:
20028       abort ();
20029     }
20030   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
20031                       (enum bfd_reloc_code_real) reloc_type);
20032   fixp->fx_file = fragp->fr_file;
20033   fixp->fx_line = fragp->fr_line;
20034   fragp->fr_fix += fragp->fr_var;
20035 }
20036
20037 /* Return the size of a relaxable immediate operand instruction.
20038    SHIFT and SIZE specify the form of the allowable immediate.  */
20039 static int
20040 relax_immediate (fragS *fragp, int size, int shift)
20041 {
20042   offsetT offset;
20043   offsetT mask;
20044   offsetT low;
20045
20046   /* ??? Should be able to do better than this.  */
20047   if (fragp->fr_symbol)
20048     return 4;
20049
20050   low = (1 << shift) - 1;
20051   mask = (1 << (shift + size)) - (1 << shift);
20052   offset = fragp->fr_offset;
20053   /* Force misaligned offsets to 32-bit variant.  */
20054   if (offset & low)
20055     return 4;
20056   if (offset & ~mask)
20057     return 4;
20058   return 2;
20059 }
20060
20061 /* Get the address of a symbol during relaxation.  */
20062 static addressT
20063 relaxed_symbol_addr (fragS *fragp, long stretch)
20064 {
20065   fragS *sym_frag;
20066   addressT addr;
20067   symbolS *sym;
20068
20069   sym = fragp->fr_symbol;
20070   sym_frag = symbol_get_frag (sym);
20071   know (S_GET_SEGMENT (sym) != absolute_section
20072         || sym_frag == &zero_address_frag);
20073   addr = S_GET_VALUE (sym) + fragp->fr_offset;
20074
20075   /* If frag has yet to be reached on this pass, assume it will
20076      move by STRETCH just as we did.  If this is not so, it will
20077      be because some frag between grows, and that will force
20078      another pass.  */
20079
20080   if (stretch != 0
20081       && sym_frag->relax_marker != fragp->relax_marker)
20082     {
20083       fragS *f;
20084
20085       /* Adjust stretch for any alignment frag.  Note that if have
20086          been expanding the earlier code, the symbol may be
20087          defined in what appears to be an earlier frag.  FIXME:
20088          This doesn't handle the fr_subtype field, which specifies
20089          a maximum number of bytes to skip when doing an
20090          alignment.  */
20091       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
20092         {
20093           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
20094             {
20095               if (stretch < 0)
20096                 stretch = - ((- stretch)
20097                              & ~ ((1 << (int) f->fr_offset) - 1));
20098               else
20099                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
20100               if (stretch == 0)
20101                 break;
20102             }
20103         }
20104       if (f != NULL)
20105         addr += stretch;
20106     }
20107
20108   return addr;
20109 }
20110
20111 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
20112    load.  */
20113 static int
20114 relax_adr (fragS *fragp, asection *sec, long stretch)
20115 {
20116   addressT addr;
20117   offsetT val;
20118
20119   /* Assume worst case for symbols not known to be in the same section.  */
20120   if (fragp->fr_symbol == NULL
20121       || !S_IS_DEFINED (fragp->fr_symbol)
20122       || sec != S_GET_SEGMENT (fragp->fr_symbol)
20123       || S_IS_WEAK (fragp->fr_symbol))
20124     return 4;
20125
20126   val = relaxed_symbol_addr (fragp, stretch);
20127   addr = fragp->fr_address + fragp->fr_fix;
20128   addr = (addr + 4) & ~3;
20129   /* Force misaligned targets to 32-bit variant.  */
20130   if (val & 3)
20131     return 4;
20132   val -= addr;
20133   if (val < 0 || val > 1020)
20134     return 4;
20135   return 2;
20136 }
20137
20138 /* Return the size of a relaxable add/sub immediate instruction.  */
20139 static int
20140 relax_addsub (fragS *fragp, asection *sec)
20141 {
20142   char *buf;
20143   int op;
20144
20145   buf = fragp->fr_literal + fragp->fr_fix;
20146   op = bfd_get_16(sec->owner, buf);
20147   if ((op & 0xf) == ((op >> 4) & 0xf))
20148     return relax_immediate (fragp, 8, 0);
20149   else
20150     return relax_immediate (fragp, 3, 0);
20151 }
20152
20153
20154 /* Return the size of a relaxable branch instruction.  BITS is the
20155    size of the offset field in the narrow instruction.  */
20156
20157 static int
20158 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
20159 {
20160   addressT addr;
20161   offsetT val;
20162   offsetT limit;
20163
20164   /* Assume worst case for symbols not known to be in the same section.  */
20165   if (!S_IS_DEFINED (fragp->fr_symbol)
20166       || sec != S_GET_SEGMENT (fragp->fr_symbol)
20167       || S_IS_WEAK (fragp->fr_symbol))
20168     return 4;
20169
20170 #ifdef OBJ_ELF
20171   if (S_IS_DEFINED (fragp->fr_symbol)
20172       && ARM_IS_FUNC (fragp->fr_symbol))
20173       return 4;
20174
20175   /* PR 12532.  Global symbols with default visibility might
20176      be preempted, so do not relax relocations to them.  */
20177   if ((ELF_ST_VISIBILITY (S_GET_OTHER (fragp->fr_symbol)) == STV_DEFAULT)
20178       && (! S_IS_LOCAL (fragp->fr_symbol)))
20179     return 4;
20180 #endif
20181
20182   val = relaxed_symbol_addr (fragp, stretch);
20183   addr = fragp->fr_address + fragp->fr_fix + 4;
20184   val -= addr;
20185
20186   /* Offset is a signed value *2 */
20187   limit = 1 << bits;
20188   if (val >= limit || val < -limit)
20189     return 4;
20190   return 2;
20191 }
20192
20193
20194 /* Relax a machine dependent frag.  This returns the amount by which
20195    the current size of the frag should change.  */
20196
20197 int
20198 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
20199 {
20200   int oldsize;
20201   int newsize;
20202
20203   oldsize = fragp->fr_var;
20204   switch (fragp->fr_subtype)
20205     {
20206     case T_MNEM_ldr_pc2:
20207       newsize = relax_adr (fragp, sec, stretch);
20208       break;
20209     case T_MNEM_ldr_pc:
20210     case T_MNEM_ldr_sp:
20211     case T_MNEM_str_sp:
20212       newsize = relax_immediate (fragp, 8, 2);
20213       break;
20214     case T_MNEM_ldr:
20215     case T_MNEM_str:
20216       newsize = relax_immediate (fragp, 5, 2);
20217       break;
20218     case T_MNEM_ldrh:
20219     case T_MNEM_strh:
20220       newsize = relax_immediate (fragp, 5, 1);
20221       break;
20222     case T_MNEM_ldrb:
20223     case T_MNEM_strb:
20224       newsize = relax_immediate (fragp, 5, 0);
20225       break;
20226     case T_MNEM_adr:
20227       newsize = relax_adr (fragp, sec, stretch);
20228       break;
20229     case T_MNEM_mov:
20230     case T_MNEM_movs:
20231     case T_MNEM_cmp:
20232     case T_MNEM_cmn:
20233       newsize = relax_immediate (fragp, 8, 0);
20234       break;
20235     case T_MNEM_b:
20236       newsize = relax_branch (fragp, sec, 11, stretch);
20237       break;
20238     case T_MNEM_bcond:
20239       newsize = relax_branch (fragp, sec, 8, stretch);
20240       break;
20241     case T_MNEM_add_sp:
20242     case T_MNEM_add_pc:
20243       newsize = relax_immediate (fragp, 8, 2);
20244       break;
20245     case T_MNEM_inc_sp:
20246     case T_MNEM_dec_sp:
20247       newsize = relax_immediate (fragp, 7, 2);
20248       break;
20249     case T_MNEM_addi:
20250     case T_MNEM_addis:
20251     case T_MNEM_subi:
20252     case T_MNEM_subis:
20253       newsize = relax_addsub (fragp, sec);
20254       break;
20255     default:
20256       abort ();
20257     }
20258
20259   fragp->fr_var = newsize;
20260   /* Freeze wide instructions that are at or before the same location as
20261      in the previous pass.  This avoids infinite loops.
20262      Don't freeze them unconditionally because targets may be artificially
20263      misaligned by the expansion of preceding frags.  */
20264   if (stretch <= 0 && newsize > 2)
20265     {
20266       md_convert_frag (sec->owner, sec, fragp);
20267       frag_wane (fragp);
20268     }
20269
20270   return newsize - oldsize;
20271 }
20272
20273 /* Round up a section size to the appropriate boundary.  */
20274
20275 valueT
20276 md_section_align (segT   segment ATTRIBUTE_UNUSED,
20277                   valueT size)
20278 {
20279 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
20280   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
20281     {
20282       /* For a.out, force the section size to be aligned.  If we don't do
20283          this, BFD will align it for us, but it will not write out the
20284          final bytes of the section.  This may be a bug in BFD, but it is
20285          easier to fix it here since that is how the other a.out targets
20286          work.  */
20287       int align;
20288
20289       align = bfd_get_section_alignment (stdoutput, segment);
20290       size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
20291     }
20292 #endif
20293
20294   return size;
20295 }
20296
20297 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
20298    of an rs_align_code fragment.  */
20299
20300 void
20301 arm_handle_align (fragS * fragP)
20302 {
20303   static char const arm_noop[2][2][4] =
20304     {
20305       {  /* ARMv1 */
20306         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
20307         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
20308       },
20309       {  /* ARMv6k */
20310         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
20311         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
20312       },
20313     };
20314   static char const thumb_noop[2][2][2] =
20315     {
20316       {  /* Thumb-1 */
20317         {0xc0, 0x46},  /* LE */
20318         {0x46, 0xc0},  /* BE */
20319       },
20320       {  /* Thumb-2 */
20321         {0x00, 0xbf},  /* LE */
20322         {0xbf, 0x00}   /* BE */
20323       }
20324     };
20325   static char const wide_thumb_noop[2][4] =
20326     {  /* Wide Thumb-2 */
20327       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
20328       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
20329     };
20330
20331   unsigned bytes, fix, noop_size;
20332   char * p;
20333   const char * noop;
20334   const char *narrow_noop = NULL;
20335 #ifdef OBJ_ELF
20336   enum mstate state;
20337 #endif
20338
20339   if (fragP->fr_type != rs_align_code)
20340     return;
20341
20342   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
20343   p = fragP->fr_literal + fragP->fr_fix;
20344   fix = 0;
20345
20346   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
20347     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
20348
20349   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
20350
20351   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
20352     {
20353       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
20354         {
20355           narrow_noop = thumb_noop[1][target_big_endian];
20356           noop = wide_thumb_noop[target_big_endian];
20357         }
20358       else
20359         noop = thumb_noop[0][target_big_endian];
20360       noop_size = 2;
20361 #ifdef OBJ_ELF
20362       state = MAP_THUMB;
20363 #endif
20364     }
20365   else
20366     {
20367       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k) != 0]
20368                      [target_big_endian];
20369       noop_size = 4;
20370 #ifdef OBJ_ELF
20371       state = MAP_ARM;
20372 #endif
20373     }
20374
20375   fragP->fr_var = noop_size;
20376
20377   if (bytes & (noop_size - 1))
20378     {
20379       fix = bytes & (noop_size - 1);
20380 #ifdef OBJ_ELF
20381       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
20382 #endif
20383       memset (p, 0, fix);
20384       p += fix;
20385       bytes -= fix;
20386     }
20387
20388   if (narrow_noop)
20389     {
20390       if (bytes & noop_size)
20391         {
20392           /* Insert a narrow noop.  */
20393           memcpy (p, narrow_noop, noop_size);
20394           p += noop_size;
20395           bytes -= noop_size;
20396           fix += noop_size;
20397         }
20398
20399       /* Use wide noops for the remainder */
20400       noop_size = 4;
20401     }
20402
20403   while (bytes >= noop_size)
20404     {
20405       memcpy (p, noop, noop_size);
20406       p += noop_size;
20407       bytes -= noop_size;
20408       fix += noop_size;
20409     }
20410
20411   fragP->fr_fix += fix;
20412 }
20413
20414 /* Called from md_do_align.  Used to create an alignment
20415    frag in a code section.  */
20416
20417 void
20418 arm_frag_align_code (int n, int max)
20419 {
20420   char * p;
20421
20422   /* We assume that there will never be a requirement
20423      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
20424   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
20425     {
20426       char err_msg[128];
20427
20428       sprintf (err_msg,
20429         _("alignments greater than %d bytes not supported in .text sections."),
20430         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
20431       as_fatal ("%s", err_msg);
20432     }
20433
20434   p = frag_var (rs_align_code,
20435                 MAX_MEM_FOR_RS_ALIGN_CODE,
20436                 1,
20437                 (relax_substateT) max,
20438                 (symbolS *) NULL,
20439                 (offsetT) n,
20440                 (char *) NULL);
20441   *p = 0;
20442 }
20443
20444 /* Perform target specific initialisation of a frag.
20445    Note - despite the name this initialisation is not done when the frag
20446    is created, but only when its type is assigned.  A frag can be created
20447    and used a long time before its type is set, so beware of assuming that
20448    this initialisationis performed first.  */
20449
20450 #ifndef OBJ_ELF
20451 void
20452 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
20453 {
20454   /* Record whether this frag is in an ARM or a THUMB area.  */
20455   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
20456 }
20457
20458 #else /* OBJ_ELF is defined.  */
20459 void
20460 arm_init_frag (fragS * fragP, int max_chars)
20461 {
20462   /* If the current ARM vs THUMB mode has not already
20463      been recorded into this frag then do so now.  */
20464   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
20465     {
20466       fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
20467
20468       /* Record a mapping symbol for alignment frags.  We will delete this
20469          later if the alignment ends up empty.  */
20470       switch (fragP->fr_type)
20471         {
20472           case rs_align:
20473           case rs_align_test:
20474           case rs_fill:
20475             mapping_state_2 (MAP_DATA, max_chars);
20476             break;
20477           case rs_align_code:
20478             mapping_state_2 (thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
20479             break;
20480           default:
20481             break;
20482         }
20483     }
20484 }
20485
20486 /* When we change sections we need to issue a new mapping symbol.  */
20487
20488 void
20489 arm_elf_change_section (void)
20490 {
20491   /* Link an unlinked unwind index table section to the .text section.  */
20492   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
20493       && elf_linked_to_section (now_seg) == NULL)
20494     elf_linked_to_section (now_seg) = text_section;
20495 }
20496
20497 int
20498 arm_elf_section_type (const char * str, size_t len)
20499 {
20500   if (len == 5 && strncmp (str, "exidx", 5) == 0)
20501     return SHT_ARM_EXIDX;
20502
20503   return -1;
20504 }
20505 \f
20506 /* Code to deal with unwinding tables.  */
20507
20508 static void add_unwind_adjustsp (offsetT);
20509
20510 /* Generate any deferred unwind frame offset.  */
20511
20512 static void
20513 flush_pending_unwind (void)
20514 {
20515   offsetT offset;
20516
20517   offset = unwind.pending_offset;
20518   unwind.pending_offset = 0;
20519   if (offset != 0)
20520     add_unwind_adjustsp (offset);
20521 }
20522
20523 /* Add an opcode to this list for this function.  Two-byte opcodes should
20524    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
20525    order.  */
20526
20527 static void
20528 add_unwind_opcode (valueT op, int length)
20529 {
20530   /* Add any deferred stack adjustment.  */
20531   if (unwind.pending_offset)
20532     flush_pending_unwind ();
20533
20534   unwind.sp_restored = 0;
20535
20536   if (unwind.opcode_count + length > unwind.opcode_alloc)
20537     {
20538       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
20539       if (unwind.opcodes)
20540         unwind.opcodes = (unsigned char *) xrealloc (unwind.opcodes,
20541                                                      unwind.opcode_alloc);
20542       else
20543         unwind.opcodes = (unsigned char *) xmalloc (unwind.opcode_alloc);
20544     }
20545   while (length > 0)
20546     {
20547       length--;
20548       unwind.opcodes[unwind.opcode_count] = op & 0xff;
20549       op >>= 8;
20550       unwind.opcode_count++;
20551     }
20552 }
20553
20554 /* Add unwind opcodes to adjust the stack pointer.  */
20555
20556 static void
20557 add_unwind_adjustsp (offsetT offset)
20558 {
20559   valueT op;
20560
20561   if (offset > 0x200)
20562     {
20563       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
20564       char bytes[5];
20565       int n;
20566       valueT o;
20567
20568       /* Long form: 0xb2, uleb128.  */
20569       /* This might not fit in a word so add the individual bytes,
20570          remembering the list is built in reverse order.  */
20571       o = (valueT) ((offset - 0x204) >> 2);
20572       if (o == 0)
20573         add_unwind_opcode (0, 1);
20574
20575       /* Calculate the uleb128 encoding of the offset.  */
20576       n = 0;
20577       while (o)
20578         {
20579           bytes[n] = o & 0x7f;
20580           o >>= 7;
20581           if (o)
20582             bytes[n] |= 0x80;
20583           n++;
20584         }
20585       /* Add the insn.  */
20586       for (; n; n--)
20587         add_unwind_opcode (bytes[n - 1], 1);
20588       add_unwind_opcode (0xb2, 1);
20589     }
20590   else if (offset > 0x100)
20591     {
20592       /* Two short opcodes.  */
20593       add_unwind_opcode (0x3f, 1);
20594       op = (offset - 0x104) >> 2;
20595       add_unwind_opcode (op, 1);
20596     }
20597   else if (offset > 0)
20598     {
20599       /* Short opcode.  */
20600       op = (offset - 4) >> 2;
20601       add_unwind_opcode (op, 1);
20602     }
20603   else if (offset < 0)
20604     {
20605       offset = -offset;
20606       while (offset > 0x100)
20607         {
20608           add_unwind_opcode (0x7f, 1);
20609           offset -= 0x100;
20610         }
20611       op = ((offset - 4) >> 2) | 0x40;
20612       add_unwind_opcode (op, 1);
20613     }
20614 }
20615
20616 /* Finish the list of unwind opcodes for this function.  */
20617 static void
20618 finish_unwind_opcodes (void)
20619 {
20620   valueT op;
20621
20622   if (unwind.fp_used)
20623     {
20624       /* Adjust sp as necessary.  */
20625       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
20626       flush_pending_unwind ();
20627
20628       /* After restoring sp from the frame pointer.  */
20629       op = 0x90 | unwind.fp_reg;
20630       add_unwind_opcode (op, 1);
20631     }
20632   else
20633     flush_pending_unwind ();
20634 }
20635
20636
20637 /* Start an exception table entry.  If idx is nonzero this is an index table
20638    entry.  */
20639
20640 static void
20641 start_unwind_section (const segT text_seg, int idx)
20642 {
20643   const char * text_name;
20644   const char * prefix;
20645   const char * prefix_once;
20646   const char * group_name;
20647   size_t prefix_len;
20648   size_t text_len;
20649   char * sec_name;
20650   size_t sec_name_len;
20651   int type;
20652   int flags;
20653   int linkonce;
20654
20655   if (idx)
20656     {
20657       prefix = ELF_STRING_ARM_unwind;
20658       prefix_once = ELF_STRING_ARM_unwind_once;
20659       type = SHT_ARM_EXIDX;
20660     }
20661   else
20662     {
20663       prefix = ELF_STRING_ARM_unwind_info;
20664       prefix_once = ELF_STRING_ARM_unwind_info_once;
20665       type = SHT_PROGBITS;
20666     }
20667
20668   text_name = segment_name (text_seg);
20669   if (streq (text_name, ".text"))
20670     text_name = "";
20671
20672   if (strncmp (text_name, ".gnu.linkonce.t.",
20673                strlen (".gnu.linkonce.t.")) == 0)
20674     {
20675       prefix = prefix_once;
20676       text_name += strlen (".gnu.linkonce.t.");
20677     }
20678
20679   prefix_len = strlen (prefix);
20680   text_len = strlen (text_name);
20681   sec_name_len = prefix_len + text_len;
20682   sec_name = (char *) xmalloc (sec_name_len + 1);
20683   memcpy (sec_name, prefix, prefix_len);
20684   memcpy (sec_name + prefix_len, text_name, text_len);
20685   sec_name[prefix_len + text_len] = '\0';
20686
20687   flags = SHF_ALLOC;
20688   linkonce = 0;
20689   group_name = 0;
20690
20691   /* Handle COMDAT group.  */
20692   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
20693     {
20694       group_name = elf_group_name (text_seg);
20695       if (group_name == NULL)
20696         {
20697           as_bad (_("Group section `%s' has no group signature"),
20698                   segment_name (text_seg));
20699           ignore_rest_of_line ();
20700           return;
20701         }
20702       flags |= SHF_GROUP;
20703       linkonce = 1;
20704     }
20705
20706   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
20707
20708   /* Set the section link for index tables.  */
20709   if (idx)
20710     elf_linked_to_section (now_seg) = text_seg;
20711 }
20712
20713
20714 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
20715    personality routine data.  Returns zero, or the index table value for
20716    and inline entry.  */
20717
20718 static valueT
20719 create_unwind_entry (int have_data)
20720 {
20721   int size;
20722   addressT where;
20723   char *ptr;
20724   /* The current word of data.  */
20725   valueT data;
20726   /* The number of bytes left in this word.  */
20727   int n;
20728
20729   finish_unwind_opcodes ();
20730
20731   /* Remember the current text section.  */
20732   unwind.saved_seg = now_seg;
20733   unwind.saved_subseg = now_subseg;
20734
20735   start_unwind_section (now_seg, 0);
20736
20737   if (unwind.personality_routine == NULL)
20738     {
20739       if (unwind.personality_index == -2)
20740         {
20741           if (have_data)
20742             as_bad (_("handlerdata in cantunwind frame"));
20743           return 1; /* EXIDX_CANTUNWIND.  */
20744         }
20745
20746       /* Use a default personality routine if none is specified.  */
20747       if (unwind.personality_index == -1)
20748         {
20749           if (unwind.opcode_count > 3)
20750             unwind.personality_index = 1;
20751           else
20752             unwind.personality_index = 0;
20753         }
20754
20755       /* Space for the personality routine entry.  */
20756       if (unwind.personality_index == 0)
20757         {
20758           if (unwind.opcode_count > 3)
20759             as_bad (_("too many unwind opcodes for personality routine 0"));
20760
20761           if (!have_data)
20762             {
20763               /* All the data is inline in the index table.  */
20764               data = 0x80;
20765               n = 3;
20766               while (unwind.opcode_count > 0)
20767                 {
20768                   unwind.opcode_count--;
20769                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
20770                   n--;
20771                 }
20772
20773               /* Pad with "finish" opcodes.  */
20774               while (n--)
20775                 data = (data << 8) | 0xb0;
20776
20777               return data;
20778             }
20779           size = 0;
20780         }
20781       else
20782         /* We get two opcodes "free" in the first word.  */
20783         size = unwind.opcode_count - 2;
20784     }
20785   else
20786     {
20787       gas_assert (unwind.personality_index == -1);
20788
20789       /* An extra byte is required for the opcode count.        */
20790       size = unwind.opcode_count + 1;
20791     }
20792
20793   size = (size + 3) >> 2;
20794   if (size > 0xff)
20795     as_bad (_("too many unwind opcodes"));
20796
20797   frag_align (2, 0, 0);
20798   record_alignment (now_seg, 2);
20799   unwind.table_entry = expr_build_dot ();
20800
20801   /* Allocate the table entry.  */
20802   ptr = frag_more ((size << 2) + 4);
20803   /* PR 13449: Zero the table entries in case some of them are not used.  */
20804   memset (ptr, 0, (size << 2) + 4);
20805   where = frag_now_fix () - ((size << 2) + 4);
20806
20807   switch (unwind.personality_index)
20808     {
20809     case -1:
20810       /* ??? Should this be a PLT generating relocation?  */
20811       /* Custom personality routine.  */
20812       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
20813                BFD_RELOC_ARM_PREL31);
20814
20815       where += 4;
20816       ptr += 4;
20817
20818       /* Set the first byte to the number of additional words.  */
20819       data = size > 0 ? size - 1 : 0;
20820       n = 3;
20821       break;
20822
20823     /* ABI defined personality routines.  */
20824     case 0:
20825       /* Three opcodes bytes are packed into the first word.  */
20826       data = 0x80;
20827       n = 3;
20828       break;
20829
20830     case 1:
20831     case 2:
20832       /* The size and first two opcode bytes go in the first word.  */
20833       data = ((0x80 + unwind.personality_index) << 8) | size;
20834       n = 2;
20835       break;
20836
20837     default:
20838       /* Should never happen.  */
20839       abort ();
20840     }
20841
20842   /* Pack the opcodes into words (MSB first), reversing the list at the same
20843      time.  */
20844   while (unwind.opcode_count > 0)
20845     {
20846       if (n == 0)
20847         {
20848           md_number_to_chars (ptr, data, 4);
20849           ptr += 4;
20850           n = 4;
20851           data = 0;
20852         }
20853       unwind.opcode_count--;
20854       n--;
20855       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
20856     }
20857
20858   /* Finish off the last word.  */
20859   if (n < 4)
20860     {
20861       /* Pad with "finish" opcodes.  */
20862       while (n--)
20863         data = (data << 8) | 0xb0;
20864
20865       md_number_to_chars (ptr, data, 4);
20866     }
20867
20868   if (!have_data)
20869     {
20870       /* Add an empty descriptor if there is no user-specified data.   */
20871       ptr = frag_more (4);
20872       md_number_to_chars (ptr, 0, 4);
20873     }
20874
20875   return 0;
20876 }
20877
20878
20879 /* Initialize the DWARF-2 unwind information for this procedure.  */
20880
20881 void
20882 tc_arm_frame_initial_instructions (void)
20883 {
20884   cfi_add_CFA_def_cfa (REG_SP, 0);
20885 }
20886 #endif /* OBJ_ELF */
20887
20888 /* Convert REGNAME to a DWARF-2 register number.  */
20889
20890 int
20891 tc_arm_regname_to_dw2regnum (char *regname)
20892 {
20893   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
20894
20895   if (reg == FAIL)
20896     return -1;
20897
20898   return reg;
20899 }
20900
20901 #ifdef TE_PE
20902 void
20903 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
20904 {
20905   expressionS exp;
20906
20907   exp.X_op = O_secrel;
20908   exp.X_add_symbol = symbol;
20909   exp.X_add_number = 0;
20910   emit_expr (&exp, size);
20911 }
20912 #endif
20913
20914 /* MD interface: Symbol and relocation handling.  */
20915
20916 /* Return the address within the segment that a PC-relative fixup is
20917    relative to.  For ARM, PC-relative fixups applied to instructions
20918    are generally relative to the location of the fixup plus 8 bytes.
20919    Thumb branches are offset by 4, and Thumb loads relative to PC
20920    require special handling.  */
20921
20922 long
20923 md_pcrel_from_section (fixS * fixP, segT seg)
20924 {
20925   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
20926
20927   /* If this is pc-relative and we are going to emit a relocation
20928      then we just want to put out any pipeline compensation that the linker
20929      will need.  Otherwise we want to use the calculated base.
20930      For WinCE we skip the bias for externals as well, since this
20931      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
20932   if (fixP->fx_pcrel
20933       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
20934           || (arm_force_relocation (fixP)
20935 #ifdef TE_WINCE
20936               && !S_IS_EXTERNAL (fixP->fx_addsy)
20937 #endif
20938               )))
20939     base = 0;
20940
20941
20942   switch (fixP->fx_r_type)
20943     {
20944       /* PC relative addressing on the Thumb is slightly odd as the
20945          bottom two bits of the PC are forced to zero for the
20946          calculation.  This happens *after* application of the
20947          pipeline offset.  However, Thumb adrl already adjusts for
20948          this, so we need not do it again.  */
20949     case BFD_RELOC_ARM_THUMB_ADD:
20950       return base & ~3;
20951
20952     case BFD_RELOC_ARM_THUMB_OFFSET:
20953     case BFD_RELOC_ARM_T32_OFFSET_IMM:
20954     case BFD_RELOC_ARM_T32_ADD_PC12:
20955     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
20956       return (base + 4) & ~3;
20957
20958       /* Thumb branches are simply offset by +4.  */
20959     case BFD_RELOC_THUMB_PCREL_BRANCH7:
20960     case BFD_RELOC_THUMB_PCREL_BRANCH9:
20961     case BFD_RELOC_THUMB_PCREL_BRANCH12:
20962     case BFD_RELOC_THUMB_PCREL_BRANCH20:
20963     case BFD_RELOC_THUMB_PCREL_BRANCH25:
20964       return base + 4;
20965
20966     case BFD_RELOC_THUMB_PCREL_BRANCH23:
20967       if (fixP->fx_addsy
20968           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20969           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20970           && ARM_IS_FUNC (fixP->fx_addsy)
20971           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20972         base = fixP->fx_where + fixP->fx_frag->fr_address;
20973        return base + 4;
20974
20975       /* BLX is like branches above, but forces the low two bits of PC to
20976          zero.  */
20977     case BFD_RELOC_THUMB_PCREL_BLX:
20978       if (fixP->fx_addsy
20979           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20980           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20981           && THUMB_IS_FUNC (fixP->fx_addsy)
20982           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20983         base = fixP->fx_where + fixP->fx_frag->fr_address;
20984       return (base + 4) & ~3;
20985
20986       /* ARM mode branches are offset by +8.  However, the Windows CE
20987          loader expects the relocation not to take this into account.  */
20988     case BFD_RELOC_ARM_PCREL_BLX:
20989       if (fixP->fx_addsy
20990           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20991           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20992           && ARM_IS_FUNC (fixP->fx_addsy)
20993           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20994         base = fixP->fx_where + fixP->fx_frag->fr_address;
20995       return base + 8;
20996
20997     case BFD_RELOC_ARM_PCREL_CALL:
20998       if (fixP->fx_addsy
20999           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21000           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21001           && THUMB_IS_FUNC (fixP->fx_addsy)
21002           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21003         base = fixP->fx_where + fixP->fx_frag->fr_address;
21004       return base + 8;
21005
21006     case BFD_RELOC_ARM_PCREL_BRANCH:
21007     case BFD_RELOC_ARM_PCREL_JUMP:
21008     case BFD_RELOC_ARM_PLT32:
21009 #ifdef TE_WINCE
21010       /* When handling fixups immediately, because we have already
21011          discovered the value of a symbol, or the address of the frag involved
21012          we must account for the offset by +8, as the OS loader will never see the reloc.
21013          see fixup_segment() in write.c
21014          The S_IS_EXTERNAL test handles the case of global symbols.
21015          Those need the calculated base, not just the pipe compensation the linker will need.  */
21016       if (fixP->fx_pcrel
21017           && fixP->fx_addsy != NULL
21018           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21019           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
21020         return base + 8;
21021       return base;
21022 #else
21023       return base + 8;
21024 #endif
21025
21026
21027       /* ARM mode loads relative to PC are also offset by +8.  Unlike
21028          branches, the Windows CE loader *does* expect the relocation
21029          to take this into account.  */
21030     case BFD_RELOC_ARM_OFFSET_IMM:
21031     case BFD_RELOC_ARM_OFFSET_IMM8:
21032     case BFD_RELOC_ARM_HWLITERAL:
21033     case BFD_RELOC_ARM_LITERAL:
21034     case BFD_RELOC_ARM_CP_OFF_IMM:
21035       return base + 8;
21036
21037
21038       /* Other PC-relative relocations are un-offset.  */
21039     default:
21040       return base;
21041     }
21042 }
21043
21044 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
21045    Otherwise we have no need to default values of symbols.  */
21046
21047 symbolS *
21048 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
21049 {
21050 #ifdef OBJ_ELF
21051   if (name[0] == '_' && name[1] == 'G'
21052       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
21053     {
21054       if (!GOT_symbol)
21055         {
21056           if (symbol_find (name))
21057             as_bad (_("GOT already in the symbol table"));
21058
21059           GOT_symbol = symbol_new (name, undefined_section,
21060                                    (valueT) 0, & zero_address_frag);
21061         }
21062
21063       return GOT_symbol;
21064     }
21065 #endif
21066
21067   return NULL;
21068 }
21069
21070 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
21071    computed as two separate immediate values, added together.  We
21072    already know that this value cannot be computed by just one ARM
21073    instruction.  */
21074
21075 static unsigned int
21076 validate_immediate_twopart (unsigned int   val,
21077                             unsigned int * highpart)
21078 {
21079   unsigned int a;
21080   unsigned int i;
21081
21082   for (i = 0; i < 32; i += 2)
21083     if (((a = rotate_left (val, i)) & 0xff) != 0)
21084       {
21085         if (a & 0xff00)
21086           {
21087             if (a & ~ 0xffff)
21088               continue;
21089             * highpart = (a  >> 8) | ((i + 24) << 7);
21090           }
21091         else if (a & 0xff0000)
21092           {
21093             if (a & 0xff000000)
21094               continue;
21095             * highpart = (a >> 16) | ((i + 16) << 7);
21096           }
21097         else
21098           {
21099             gas_assert (a & 0xff000000);
21100             * highpart = (a >> 24) | ((i + 8) << 7);
21101           }
21102
21103         return (a & 0xff) | (i << 7);
21104       }
21105
21106   return FAIL;
21107 }
21108
21109 static int
21110 validate_offset_imm (unsigned int val, int hwse)
21111 {
21112   if ((hwse && val > 255) || val > 4095)
21113     return FAIL;
21114   return val;
21115 }
21116
21117 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
21118    negative immediate constant by altering the instruction.  A bit of
21119    a hack really.
21120         MOV <-> MVN
21121         AND <-> BIC
21122         ADC <-> SBC
21123         by inverting the second operand, and
21124         ADD <-> SUB
21125         CMP <-> CMN
21126         by negating the second operand.  */
21127
21128 static int
21129 negate_data_op (unsigned long * instruction,
21130                 unsigned long   value)
21131 {
21132   int op, new_inst;
21133   unsigned long negated, inverted;
21134
21135   negated = encode_arm_immediate (-value);
21136   inverted = encode_arm_immediate (~value);
21137
21138   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
21139   switch (op)
21140     {
21141       /* First negates.  */
21142     case OPCODE_SUB:             /* ADD <-> SUB  */
21143       new_inst = OPCODE_ADD;
21144       value = negated;
21145       break;
21146
21147     case OPCODE_ADD:
21148       new_inst = OPCODE_SUB;
21149       value = negated;
21150       break;
21151
21152     case OPCODE_CMP:             /* CMP <-> CMN  */
21153       new_inst = OPCODE_CMN;
21154       value = negated;
21155       break;
21156
21157     case OPCODE_CMN:
21158       new_inst = OPCODE_CMP;
21159       value = negated;
21160       break;
21161
21162       /* Now Inverted ops.  */
21163     case OPCODE_MOV:             /* MOV <-> MVN  */
21164       new_inst = OPCODE_MVN;
21165       value = inverted;
21166       break;
21167
21168     case OPCODE_MVN:
21169       new_inst = OPCODE_MOV;
21170       value = inverted;
21171       break;
21172
21173     case OPCODE_AND:             /* AND <-> BIC  */
21174       new_inst = OPCODE_BIC;
21175       value = inverted;
21176       break;
21177
21178     case OPCODE_BIC:
21179       new_inst = OPCODE_AND;
21180       value = inverted;
21181       break;
21182
21183     case OPCODE_ADC:              /* ADC <-> SBC  */
21184       new_inst = OPCODE_SBC;
21185       value = inverted;
21186       break;
21187
21188     case OPCODE_SBC:
21189       new_inst = OPCODE_ADC;
21190       value = inverted;
21191       break;
21192
21193       /* We cannot do anything.  */
21194     default:
21195       return FAIL;
21196     }
21197
21198   if (value == (unsigned) FAIL)
21199     return FAIL;
21200
21201   *instruction &= OPCODE_MASK;
21202   *instruction |= new_inst << DATA_OP_SHIFT;
21203   return value;
21204 }
21205
21206 /* Like negate_data_op, but for Thumb-2.   */
21207
21208 static unsigned int
21209 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
21210 {
21211   int op, new_inst;
21212   int rd;
21213   unsigned int negated, inverted;
21214
21215   negated = encode_thumb32_immediate (-value);
21216   inverted = encode_thumb32_immediate (~value);
21217
21218   rd = (*instruction >> 8) & 0xf;
21219   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
21220   switch (op)
21221     {
21222       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
21223     case T2_OPCODE_SUB:
21224       new_inst = T2_OPCODE_ADD;
21225       value = negated;
21226       break;
21227
21228     case T2_OPCODE_ADD:
21229       new_inst = T2_OPCODE_SUB;
21230       value = negated;
21231       break;
21232
21233       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
21234     case T2_OPCODE_ORR:
21235       new_inst = T2_OPCODE_ORN;
21236       value = inverted;
21237       break;
21238
21239     case T2_OPCODE_ORN:
21240       new_inst = T2_OPCODE_ORR;
21241       value = inverted;
21242       break;
21243
21244       /* AND <-> BIC.  TST has no inverted equivalent.  */
21245     case T2_OPCODE_AND:
21246       new_inst = T2_OPCODE_BIC;
21247       if (rd == 15)
21248         value = FAIL;
21249       else
21250         value = inverted;
21251       break;
21252
21253     case T2_OPCODE_BIC:
21254       new_inst = T2_OPCODE_AND;
21255       value = inverted;
21256       break;
21257
21258       /* ADC <-> SBC  */
21259     case T2_OPCODE_ADC:
21260       new_inst = T2_OPCODE_SBC;
21261       value = inverted;
21262       break;
21263
21264     case T2_OPCODE_SBC:
21265       new_inst = T2_OPCODE_ADC;
21266       value = inverted;
21267       break;
21268
21269       /* We cannot do anything.  */
21270     default:
21271       return FAIL;
21272     }
21273
21274   if (value == (unsigned int)FAIL)
21275     return FAIL;
21276
21277   *instruction &= T2_OPCODE_MASK;
21278   *instruction |= new_inst << T2_DATA_OP_SHIFT;
21279   return value;
21280 }
21281
21282 /* Read a 32-bit thumb instruction from buf.  */
21283 static unsigned long
21284 get_thumb32_insn (char * buf)
21285 {
21286   unsigned long insn;
21287   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
21288   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
21289
21290   return insn;
21291 }
21292
21293
21294 /* We usually want to set the low bit on the address of thumb function
21295    symbols.  In particular .word foo - . should have the low bit set.
21296    Generic code tries to fold the difference of two symbols to
21297    a constant.  Prevent this and force a relocation when the first symbols
21298    is a thumb function.  */
21299
21300 bfd_boolean
21301 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
21302 {
21303   if (op == O_subtract
21304       && l->X_op == O_symbol
21305       && r->X_op == O_symbol
21306       && THUMB_IS_FUNC (l->X_add_symbol))
21307     {
21308       l->X_op = O_subtract;
21309       l->X_op_symbol = r->X_add_symbol;
21310       l->X_add_number -= r->X_add_number;
21311       return TRUE;
21312     }
21313
21314   /* Process as normal.  */
21315   return FALSE;
21316 }
21317
21318 /* Encode Thumb2 unconditional branches and calls. The encoding
21319    for the 2 are identical for the immediate values.  */
21320
21321 static void
21322 encode_thumb2_b_bl_offset (char * buf, offsetT value)
21323 {
21324 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
21325   offsetT newval;
21326   offsetT newval2;
21327   addressT S, I1, I2, lo, hi;
21328
21329   S = (value >> 24) & 0x01;
21330   I1 = (value >> 23) & 0x01;
21331   I2 = (value >> 22) & 0x01;
21332   hi = (value >> 12) & 0x3ff;
21333   lo = (value >> 1) & 0x7ff;
21334   newval   = md_chars_to_number (buf, THUMB_SIZE);
21335   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
21336   newval  |= (S << 10) | hi;
21337   newval2 &=  ~T2I1I2MASK;
21338   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
21339   md_number_to_chars (buf, newval, THUMB_SIZE);
21340   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
21341 }
21342
21343 void
21344 md_apply_fix (fixS *    fixP,
21345                valueT * valP,
21346                segT     seg)
21347 {
21348   offsetT        value = * valP;
21349   offsetT        newval;
21350   unsigned int   newimm;
21351   unsigned long  temp;
21352   int            sign;
21353   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
21354
21355   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
21356
21357   /* Note whether this will delete the relocation.  */
21358
21359   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
21360     fixP->fx_done = 1;
21361
21362   /* On a 64-bit host, silently truncate 'value' to 32 bits for
21363      consistency with the behaviour on 32-bit hosts.  Remember value
21364      for emit_reloc.  */
21365   value &= 0xffffffff;
21366   value ^= 0x80000000;
21367   value -= 0x80000000;
21368
21369   *valP = value;
21370   fixP->fx_addnumber = value;
21371
21372   /* Same treatment for fixP->fx_offset.  */
21373   fixP->fx_offset &= 0xffffffff;
21374   fixP->fx_offset ^= 0x80000000;
21375   fixP->fx_offset -= 0x80000000;
21376
21377   switch (fixP->fx_r_type)
21378     {
21379     case BFD_RELOC_NONE:
21380       /* This will need to go in the object file.  */
21381       fixP->fx_done = 0;
21382       break;
21383
21384     case BFD_RELOC_ARM_IMMEDIATE:
21385       /* We claim that this fixup has been processed here,
21386          even if in fact we generate an error because we do
21387          not have a reloc for it, so tc_gen_reloc will reject it.  */
21388       fixP->fx_done = 1;
21389
21390       if (fixP->fx_addsy)
21391         {
21392           const char *msg = 0;
21393
21394           if (! S_IS_DEFINED (fixP->fx_addsy))
21395             msg = _("undefined symbol %s used as an immediate value");
21396           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
21397             msg = _("symbol %s is in a different section");
21398           else if (S_IS_WEAK (fixP->fx_addsy))
21399             msg = _("symbol %s is weak and may be overridden later");
21400
21401           if (msg)
21402             {
21403               as_bad_where (fixP->fx_file, fixP->fx_line,
21404                             msg, S_GET_NAME (fixP->fx_addsy));
21405               break;
21406             }
21407         }
21408
21409       temp = md_chars_to_number (buf, INSN_SIZE);
21410
21411       /* If the offset is negative, we should use encoding A2 for ADR.  */
21412       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
21413         newimm = negate_data_op (&temp, value);
21414       else
21415         {
21416           newimm = encode_arm_immediate (value);
21417
21418           /* If the instruction will fail, see if we can fix things up by
21419              changing the opcode.  */
21420           if (newimm == (unsigned int) FAIL)
21421             newimm = negate_data_op (&temp, value);
21422         }
21423
21424       if (newimm == (unsigned int) FAIL)
21425         {
21426           as_bad_where (fixP->fx_file, fixP->fx_line,
21427                         _("invalid constant (%lx) after fixup"),
21428                         (unsigned long) value);
21429           break;
21430         }
21431
21432       newimm |= (temp & 0xfffff000);
21433       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
21434       break;
21435
21436     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
21437       {
21438         unsigned int highpart = 0;
21439         unsigned int newinsn  = 0xe1a00000; /* nop.  */
21440
21441         if (fixP->fx_addsy)
21442           {
21443             const char *msg = 0;
21444
21445             if (! S_IS_DEFINED (fixP->fx_addsy))
21446               msg = _("undefined symbol %s used as an immediate value");
21447             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
21448               msg = _("symbol %s is in a different section");
21449             else if (S_IS_WEAK (fixP->fx_addsy))
21450               msg = _("symbol %s is weak and may be overridden later");
21451
21452             if (msg)
21453               {
21454                 as_bad_where (fixP->fx_file, fixP->fx_line,
21455                               msg, S_GET_NAME (fixP->fx_addsy));
21456                 break;
21457               }
21458           }
21459
21460         newimm = encode_arm_immediate (value);
21461         temp = md_chars_to_number (buf, INSN_SIZE);
21462
21463         /* If the instruction will fail, see if we can fix things up by
21464            changing the opcode.  */
21465         if (newimm == (unsigned int) FAIL
21466             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
21467           {
21468             /* No ?  OK - try using two ADD instructions to generate
21469                the value.  */
21470             newimm = validate_immediate_twopart (value, & highpart);
21471
21472             /* Yes - then make sure that the second instruction is
21473                also an add.  */
21474             if (newimm != (unsigned int) FAIL)
21475               newinsn = temp;
21476             /* Still No ?  Try using a negated value.  */
21477             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
21478               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
21479             /* Otherwise - give up.  */
21480             else
21481               {
21482                 as_bad_where (fixP->fx_file, fixP->fx_line,
21483                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
21484                               (long) value);
21485                 break;
21486               }
21487
21488             /* Replace the first operand in the 2nd instruction (which
21489                is the PC) with the destination register.  We have
21490                already added in the PC in the first instruction and we
21491                do not want to do it again.  */
21492             newinsn &= ~ 0xf0000;
21493             newinsn |= ((newinsn & 0x0f000) << 4);
21494           }
21495
21496         newimm |= (temp & 0xfffff000);
21497         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
21498
21499         highpart |= (newinsn & 0xfffff000);
21500         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
21501       }
21502       break;
21503
21504     case BFD_RELOC_ARM_OFFSET_IMM:
21505       if (!fixP->fx_done && seg->use_rela_p)
21506         value = 0;
21507
21508     case BFD_RELOC_ARM_LITERAL:
21509       sign = value > 0;
21510
21511       if (value < 0)
21512         value = - value;
21513
21514       if (validate_offset_imm (value, 0) == FAIL)
21515         {
21516           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
21517             as_bad_where (fixP->fx_file, fixP->fx_line,
21518                           _("invalid literal constant: pool needs to be closer"));
21519           else
21520             as_bad_where (fixP->fx_file, fixP->fx_line,
21521                           _("bad immediate value for offset (%ld)"),
21522                           (long) value);
21523           break;
21524         }
21525
21526       newval = md_chars_to_number (buf, INSN_SIZE);
21527       if (value == 0)
21528         newval &= 0xfffff000;
21529       else
21530         {
21531           newval &= 0xff7ff000;
21532           newval |= value | (sign ? INDEX_UP : 0);
21533         }
21534       md_number_to_chars (buf, newval, INSN_SIZE);
21535       break;
21536
21537     case BFD_RELOC_ARM_OFFSET_IMM8:
21538     case BFD_RELOC_ARM_HWLITERAL:
21539       sign = value > 0;
21540
21541       if (value < 0)
21542         value = - value;
21543
21544       if (validate_offset_imm (value, 1) == FAIL)
21545         {
21546           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
21547             as_bad_where (fixP->fx_file, fixP->fx_line,
21548                           _("invalid literal constant: pool needs to be closer"));
21549           else
21550             as_bad_where (fixP->fx_file, fixP->fx_line,
21551                           _("bad immediate value for 8-bit offset (%ld)"),
21552                           (long) value);
21553           break;
21554         }
21555
21556       newval = md_chars_to_number (buf, INSN_SIZE);
21557       if (value == 0)
21558         newval &= 0xfffff0f0;
21559       else
21560         {
21561           newval &= 0xff7ff0f0;
21562           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
21563         }
21564       md_number_to_chars (buf, newval, INSN_SIZE);
21565       break;
21566
21567     case BFD_RELOC_ARM_T32_OFFSET_U8:
21568       if (value < 0 || value > 1020 || value % 4 != 0)
21569         as_bad_where (fixP->fx_file, fixP->fx_line,
21570                       _("bad immediate value for offset (%ld)"), (long) value);
21571       value /= 4;
21572
21573       newval = md_chars_to_number (buf+2, THUMB_SIZE);
21574       newval |= value;
21575       md_number_to_chars (buf+2, newval, THUMB_SIZE);
21576       break;
21577
21578     case BFD_RELOC_ARM_T32_OFFSET_IMM:
21579       /* This is a complicated relocation used for all varieties of Thumb32
21580          load/store instruction with immediate offset:
21581
21582          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
21583                                                    *4, optional writeback(W)
21584                                                    (doubleword load/store)
21585
21586          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
21587          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
21588          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
21589          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
21590          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
21591
21592          Uppercase letters indicate bits that are already encoded at
21593          this point.  Lowercase letters are our problem.  For the
21594          second block of instructions, the secondary opcode nybble
21595          (bits 8..11) is present, and bit 23 is zero, even if this is
21596          a PC-relative operation.  */
21597       newval = md_chars_to_number (buf, THUMB_SIZE);
21598       newval <<= 16;
21599       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
21600
21601       if ((newval & 0xf0000000) == 0xe0000000)
21602         {
21603           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
21604           if (value >= 0)
21605             newval |= (1 << 23);
21606           else
21607             value = -value;
21608           if (value % 4 != 0)
21609             {
21610               as_bad_where (fixP->fx_file, fixP->fx_line,
21611                             _("offset not a multiple of 4"));
21612               break;
21613             }
21614           value /= 4;
21615           if (value > 0xff)
21616             {
21617               as_bad_where (fixP->fx_file, fixP->fx_line,
21618                             _("offset out of range"));
21619               break;
21620             }
21621           newval &= ~0xff;
21622         }
21623       else if ((newval & 0x000f0000) == 0x000f0000)
21624         {
21625           /* PC-relative, 12-bit offset.  */
21626           if (value >= 0)
21627             newval |= (1 << 23);
21628           else
21629             value = -value;
21630           if (value > 0xfff)
21631             {
21632               as_bad_where (fixP->fx_file, fixP->fx_line,
21633                             _("offset out of range"));
21634               break;
21635             }
21636           newval &= ~0xfff;
21637         }
21638       else if ((newval & 0x00000100) == 0x00000100)
21639         {
21640           /* Writeback: 8-bit, +/- offset.  */
21641           if (value >= 0)
21642             newval |= (1 << 9);
21643           else
21644             value = -value;
21645           if (value > 0xff)
21646             {
21647               as_bad_where (fixP->fx_file, fixP->fx_line,
21648                             _("offset out of range"));
21649               break;
21650             }
21651           newval &= ~0xff;
21652         }
21653       else if ((newval & 0x00000f00) == 0x00000e00)
21654         {
21655           /* T-instruction: positive 8-bit offset.  */
21656           if (value < 0 || value > 0xff)
21657             {
21658               as_bad_where (fixP->fx_file, fixP->fx_line,
21659                             _("offset out of range"));
21660               break;
21661             }
21662           newval &= ~0xff;
21663           newval |= value;
21664         }
21665       else
21666         {
21667           /* Positive 12-bit or negative 8-bit offset.  */
21668           int limit;
21669           if (value >= 0)
21670             {
21671               newval |= (1 << 23);
21672               limit = 0xfff;
21673             }
21674           else
21675             {
21676               value = -value;
21677               limit = 0xff;
21678             }
21679           if (value > limit)
21680             {
21681               as_bad_where (fixP->fx_file, fixP->fx_line,
21682                             _("offset out of range"));
21683               break;
21684             }
21685           newval &= ~limit;
21686         }
21687
21688       newval |= value;
21689       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
21690       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
21691       break;
21692
21693     case BFD_RELOC_ARM_SHIFT_IMM:
21694       newval = md_chars_to_number (buf, INSN_SIZE);
21695       if (((unsigned long) value) > 32
21696           || (value == 32
21697               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
21698         {
21699           as_bad_where (fixP->fx_file, fixP->fx_line,
21700                         _("shift expression is too large"));
21701           break;
21702         }
21703
21704       if (value == 0)
21705         /* Shifts of zero must be done as lsl.  */
21706         newval &= ~0x60;
21707       else if (value == 32)
21708         value = 0;
21709       newval &= 0xfffff07f;
21710       newval |= (value & 0x1f) << 7;
21711       md_number_to_chars (buf, newval, INSN_SIZE);
21712       break;
21713
21714     case BFD_RELOC_ARM_T32_IMMEDIATE:
21715     case BFD_RELOC_ARM_T32_ADD_IMM:
21716     case BFD_RELOC_ARM_T32_IMM12:
21717     case BFD_RELOC_ARM_T32_ADD_PC12:
21718       /* We claim that this fixup has been processed here,
21719          even if in fact we generate an error because we do
21720          not have a reloc for it, so tc_gen_reloc will reject it.  */
21721       fixP->fx_done = 1;
21722
21723       if (fixP->fx_addsy
21724           && ! S_IS_DEFINED (fixP->fx_addsy))
21725         {
21726           as_bad_where (fixP->fx_file, fixP->fx_line,
21727                         _("undefined symbol %s used as an immediate value"),
21728                         S_GET_NAME (fixP->fx_addsy));
21729           break;
21730         }
21731
21732       newval = md_chars_to_number (buf, THUMB_SIZE);
21733       newval <<= 16;
21734       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
21735
21736       newimm = FAIL;
21737       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
21738           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
21739         {
21740           newimm = encode_thumb32_immediate (value);
21741           if (newimm == (unsigned int) FAIL)
21742             newimm = thumb32_negate_data_op (&newval, value);
21743         }
21744       if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE
21745           && newimm == (unsigned int) FAIL)
21746         {
21747           /* Turn add/sum into addw/subw.  */
21748           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
21749             newval = (newval & 0xfeffffff) | 0x02000000;
21750           /* No flat 12-bit imm encoding for addsw/subsw.  */
21751           if ((newval & 0x00100000) == 0)
21752             {
21753               /* 12 bit immediate for addw/subw.  */
21754               if (value < 0)
21755                 {
21756                   value = -value;
21757                   newval ^= 0x00a00000;
21758                 }
21759               if (value > 0xfff)
21760                 newimm = (unsigned int) FAIL;
21761               else
21762                 newimm = value;
21763             }
21764         }
21765
21766       if (newimm == (unsigned int)FAIL)
21767         {
21768           as_bad_where (fixP->fx_file, fixP->fx_line,
21769                         _("invalid constant (%lx) after fixup"),
21770                         (unsigned long) value);
21771           break;
21772         }
21773
21774       newval |= (newimm & 0x800) << 15;
21775       newval |= (newimm & 0x700) << 4;
21776       newval |= (newimm & 0x0ff);
21777
21778       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
21779       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
21780       break;
21781
21782     case BFD_RELOC_ARM_SMC:
21783       if (((unsigned long) value) > 0xffff)
21784         as_bad_where (fixP->fx_file, fixP->fx_line,
21785                       _("invalid smc expression"));
21786       newval = md_chars_to_number (buf, INSN_SIZE);
21787       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
21788       md_number_to_chars (buf, newval, INSN_SIZE);
21789       break;
21790
21791     case BFD_RELOC_ARM_HVC:
21792       if (((unsigned long) value) > 0xffff)
21793         as_bad_where (fixP->fx_file, fixP->fx_line,
21794                       _("invalid hvc expression"));
21795       newval = md_chars_to_number (buf, INSN_SIZE);
21796       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
21797       md_number_to_chars (buf, newval, INSN_SIZE);
21798       break;
21799
21800     case BFD_RELOC_ARM_SWI:
21801       if (fixP->tc_fix_data != 0)
21802         {
21803           if (((unsigned long) value) > 0xff)
21804             as_bad_where (fixP->fx_file, fixP->fx_line,
21805                           _("invalid swi expression"));
21806           newval = md_chars_to_number (buf, THUMB_SIZE);
21807           newval |= value;
21808           md_number_to_chars (buf, newval, THUMB_SIZE);
21809         }
21810       else
21811         {
21812           if (((unsigned long) value) > 0x00ffffff)
21813             as_bad_where (fixP->fx_file, fixP->fx_line,
21814                           _("invalid swi expression"));
21815           newval = md_chars_to_number (buf, INSN_SIZE);
21816           newval |= value;
21817           md_number_to_chars (buf, newval, INSN_SIZE);
21818         }
21819       break;
21820
21821     case BFD_RELOC_ARM_MULTI:
21822       if (((unsigned long) value) > 0xffff)
21823         as_bad_where (fixP->fx_file, fixP->fx_line,
21824                       _("invalid expression in load/store multiple"));
21825       newval = value | md_chars_to_number (buf, INSN_SIZE);
21826       md_number_to_chars (buf, newval, INSN_SIZE);
21827       break;
21828
21829 #ifdef OBJ_ELF
21830     case BFD_RELOC_ARM_PCREL_CALL:
21831
21832       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
21833           && fixP->fx_addsy
21834           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21835           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21836           && THUMB_IS_FUNC (fixP->fx_addsy))
21837         /* Flip the bl to blx. This is a simple flip
21838            bit here because we generate PCREL_CALL for
21839            unconditional bls.  */
21840         {
21841           newval = md_chars_to_number (buf, INSN_SIZE);
21842           newval = newval | 0x10000000;
21843           md_number_to_chars (buf, newval, INSN_SIZE);
21844           temp = 1;
21845           fixP->fx_done = 1;
21846         }
21847       else
21848         temp = 3;
21849       goto arm_branch_common;
21850
21851     case BFD_RELOC_ARM_PCREL_JUMP:
21852       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
21853           && fixP->fx_addsy
21854           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21855           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21856           && THUMB_IS_FUNC (fixP->fx_addsy))
21857         {
21858           /* This would map to a bl<cond>, b<cond>,
21859              b<always> to a Thumb function. We
21860              need to force a relocation for this particular
21861              case.  */
21862           newval = md_chars_to_number (buf, INSN_SIZE);
21863           fixP->fx_done = 0;
21864         }
21865
21866     case BFD_RELOC_ARM_PLT32:
21867 #endif
21868     case BFD_RELOC_ARM_PCREL_BRANCH:
21869       temp = 3;
21870       goto arm_branch_common;
21871
21872     case BFD_RELOC_ARM_PCREL_BLX:
21873
21874       temp = 1;
21875       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
21876           && fixP->fx_addsy
21877           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21878           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21879           && ARM_IS_FUNC (fixP->fx_addsy))
21880         {
21881           /* Flip the blx to a bl and warn.  */
21882           const char *name = S_GET_NAME (fixP->fx_addsy);
21883           newval = 0xeb000000;
21884           as_warn_where (fixP->fx_file, fixP->fx_line,
21885                          _("blx to '%s' an ARM ISA state function changed to bl"),
21886                           name);
21887           md_number_to_chars (buf, newval, INSN_SIZE);
21888           temp = 3;
21889           fixP->fx_done = 1;
21890         }
21891
21892 #ifdef OBJ_ELF
21893        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
21894          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
21895 #endif
21896
21897     arm_branch_common:
21898       /* We are going to store value (shifted right by two) in the
21899          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
21900          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
21901          also be be clear.  */
21902       if (value & temp)
21903         as_bad_where (fixP->fx_file, fixP->fx_line,
21904                       _("misaligned branch destination"));
21905       if ((value & (offsetT)0xfe000000) != (offsetT)0
21906           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
21907         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21908
21909       if (fixP->fx_done || !seg->use_rela_p)
21910         {
21911           newval = md_chars_to_number (buf, INSN_SIZE);
21912           newval |= (value >> 2) & 0x00ffffff;
21913           /* Set the H bit on BLX instructions.  */
21914           if (temp == 1)
21915             {
21916               if (value & 2)
21917                 newval |= 0x01000000;
21918               else
21919                 newval &= ~0x01000000;
21920             }
21921           md_number_to_chars (buf, newval, INSN_SIZE);
21922         }
21923       break;
21924
21925     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
21926       /* CBZ can only branch forward.  */
21927
21928       /* Attempts to use CBZ to branch to the next instruction
21929          (which, strictly speaking, are prohibited) will be turned into
21930          no-ops.
21931
21932          FIXME: It may be better to remove the instruction completely and
21933          perform relaxation.  */
21934       if (value == -2)
21935         {
21936           newval = md_chars_to_number (buf, THUMB_SIZE);
21937           newval = 0xbf00; /* NOP encoding T1 */
21938           md_number_to_chars (buf, newval, THUMB_SIZE);
21939         }
21940       else
21941         {
21942           if (value & ~0x7e)
21943             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21944
21945           if (fixP->fx_done || !seg->use_rela_p)
21946             {
21947               newval = md_chars_to_number (buf, THUMB_SIZE);
21948               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
21949               md_number_to_chars (buf, newval, THUMB_SIZE);
21950             }
21951         }
21952       break;
21953
21954     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
21955       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
21956         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21957
21958       if (fixP->fx_done || !seg->use_rela_p)
21959         {
21960           newval = md_chars_to_number (buf, THUMB_SIZE);
21961           newval |= (value & 0x1ff) >> 1;
21962           md_number_to_chars (buf, newval, THUMB_SIZE);
21963         }
21964       break;
21965
21966     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
21967       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
21968         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21969
21970       if (fixP->fx_done || !seg->use_rela_p)
21971         {
21972           newval = md_chars_to_number (buf, THUMB_SIZE);
21973           newval |= (value & 0xfff) >> 1;
21974           md_number_to_chars (buf, newval, THUMB_SIZE);
21975         }
21976       break;
21977
21978     case BFD_RELOC_THUMB_PCREL_BRANCH20:
21979       if (fixP->fx_addsy
21980           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21981           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21982           && ARM_IS_FUNC (fixP->fx_addsy)
21983           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21984         {
21985           /* Force a relocation for a branch 20 bits wide.  */
21986           fixP->fx_done = 0;
21987         }
21988       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
21989         as_bad_where (fixP->fx_file, fixP->fx_line,
21990                       _("conditional branch out of range"));
21991
21992       if (fixP->fx_done || !seg->use_rela_p)
21993         {
21994           offsetT newval2;
21995           addressT S, J1, J2, lo, hi;
21996
21997           S  = (value & 0x00100000) >> 20;
21998           J2 = (value & 0x00080000) >> 19;
21999           J1 = (value & 0x00040000) >> 18;
22000           hi = (value & 0x0003f000) >> 12;
22001           lo = (value & 0x00000ffe) >> 1;
22002
22003           newval   = md_chars_to_number (buf, THUMB_SIZE);
22004           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22005           newval  |= (S << 10) | hi;
22006           newval2 |= (J1 << 13) | (J2 << 11) | lo;
22007           md_number_to_chars (buf, newval, THUMB_SIZE);
22008           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
22009         }
22010       break;
22011
22012     case BFD_RELOC_THUMB_PCREL_BLX:
22013       /* If there is a blx from a thumb state function to
22014          another thumb function flip this to a bl and warn
22015          about it.  */
22016
22017       if (fixP->fx_addsy
22018           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22019           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22020           && THUMB_IS_FUNC (fixP->fx_addsy))
22021         {
22022           const char *name = S_GET_NAME (fixP->fx_addsy);
22023           as_warn_where (fixP->fx_file, fixP->fx_line,
22024                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
22025                          name);
22026           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22027           newval = newval | 0x1000;
22028           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
22029           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
22030           fixP->fx_done = 1;
22031         }
22032
22033
22034       goto thumb_bl_common;
22035
22036     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22037       /* A bl from Thumb state ISA to an internal ARM state function
22038          is converted to a blx.  */
22039       if (fixP->fx_addsy
22040           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22041           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22042           && ARM_IS_FUNC (fixP->fx_addsy)
22043           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22044         {
22045           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22046           newval = newval & ~0x1000;
22047           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
22048           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
22049           fixP->fx_done = 1;
22050         }
22051
22052     thumb_bl_common:
22053
22054       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
22055         /* For a BLX instruction, make sure that the relocation is rounded up
22056            to a word boundary.  This follows the semantics of the instruction
22057            which specifies that bit 1 of the target address will come from bit
22058            1 of the base address.  */
22059         value = (value + 3) & ~ 3;
22060
22061 #ifdef OBJ_ELF
22062        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
22063            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
22064          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
22065 #endif
22066
22067       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
22068         {
22069           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2)))
22070             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22071           else if ((value & ~0x1ffffff)
22072                    && ((value & ~0x1ffffff) != ~0x1ffffff))
22073             as_bad_where (fixP->fx_file, fixP->fx_line,
22074                           _("Thumb2 branch out of range"));
22075         }
22076
22077       if (fixP->fx_done || !seg->use_rela_p)
22078         encode_thumb2_b_bl_offset (buf, value);
22079
22080       break;
22081
22082     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22083       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
22084         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22085
22086       if (fixP->fx_done || !seg->use_rela_p)
22087           encode_thumb2_b_bl_offset (buf, value);
22088
22089       break;
22090
22091     case BFD_RELOC_8:
22092       if (fixP->fx_done || !seg->use_rela_p)
22093         md_number_to_chars (buf, value, 1);
22094       break;
22095
22096     case BFD_RELOC_16:
22097       if (fixP->fx_done || !seg->use_rela_p)
22098         md_number_to_chars (buf, value, 2);
22099       break;
22100
22101 #ifdef OBJ_ELF
22102     case BFD_RELOC_ARM_TLS_CALL:
22103     case BFD_RELOC_ARM_THM_TLS_CALL:
22104     case BFD_RELOC_ARM_TLS_DESCSEQ:
22105     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
22106       S_SET_THREAD_LOCAL (fixP->fx_addsy);
22107       break;
22108
22109     case BFD_RELOC_ARM_TLS_GOTDESC:
22110     case BFD_RELOC_ARM_TLS_GD32:
22111     case BFD_RELOC_ARM_TLS_LE32:
22112     case BFD_RELOC_ARM_TLS_IE32:
22113     case BFD_RELOC_ARM_TLS_LDM32:
22114     case BFD_RELOC_ARM_TLS_LDO32:
22115       S_SET_THREAD_LOCAL (fixP->fx_addsy);
22116       /* fall through */
22117
22118     case BFD_RELOC_ARM_GOT32:
22119     case BFD_RELOC_ARM_GOTOFF:
22120       if (fixP->fx_done || !seg->use_rela_p)
22121         md_number_to_chars (buf, 0, 4);
22122       break;
22123
22124     case BFD_RELOC_ARM_GOT_PREL:
22125       if (fixP->fx_done || !seg->use_rela_p)
22126         md_number_to_chars (buf, value, 4);
22127       break;
22128
22129     case BFD_RELOC_ARM_TARGET2:
22130       /* TARGET2 is not partial-inplace, so we need to write the
22131          addend here for REL targets, because it won't be written out
22132          during reloc processing later.  */
22133       if (fixP->fx_done || !seg->use_rela_p)
22134         md_number_to_chars (buf, fixP->fx_offset, 4);
22135       break;
22136 #endif
22137
22138     case BFD_RELOC_RVA:
22139     case BFD_RELOC_32:
22140     case BFD_RELOC_ARM_TARGET1:
22141     case BFD_RELOC_ARM_ROSEGREL32:
22142     case BFD_RELOC_ARM_SBREL32:
22143     case BFD_RELOC_32_PCREL:
22144 #ifdef TE_PE
22145     case BFD_RELOC_32_SECREL:
22146 #endif
22147       if (fixP->fx_done || !seg->use_rela_p)
22148 #ifdef TE_WINCE
22149         /* For WinCE we only do this for pcrel fixups.  */
22150         if (fixP->fx_done || fixP->fx_pcrel)
22151 #endif
22152           md_number_to_chars (buf, value, 4);
22153       break;
22154
22155 #ifdef OBJ_ELF
22156     case BFD_RELOC_ARM_PREL31:
22157       if (fixP->fx_done || !seg->use_rela_p)
22158         {
22159           newval = md_chars_to_number (buf, 4) & 0x80000000;
22160           if ((value ^ (value >> 1)) & 0x40000000)
22161             {
22162               as_bad_where (fixP->fx_file, fixP->fx_line,
22163                             _("rel31 relocation overflow"));
22164             }
22165           newval |= value & 0x7fffffff;
22166           md_number_to_chars (buf, newval, 4);
22167         }
22168       break;
22169 #endif
22170
22171     case BFD_RELOC_ARM_CP_OFF_IMM:
22172     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
22173       if (value < -1023 || value > 1023 || (value & 3))
22174         as_bad_where (fixP->fx_file, fixP->fx_line,
22175                       _("co-processor offset out of range"));
22176     cp_off_common:
22177       sign = value > 0;
22178       if (value < 0)
22179         value = -value;
22180       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
22181           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
22182         newval = md_chars_to_number (buf, INSN_SIZE);
22183       else
22184         newval = get_thumb32_insn (buf);
22185       if (value == 0)
22186         newval &= 0xffffff00;
22187       else
22188         {
22189           newval &= 0xff7fff00;
22190           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
22191         }
22192       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
22193           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
22194         md_number_to_chars (buf, newval, INSN_SIZE);
22195       else
22196         put_thumb32_insn (buf, newval);
22197       break;
22198
22199     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
22200     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
22201       if (value < -255 || value > 255)
22202         as_bad_where (fixP->fx_file, fixP->fx_line,
22203                       _("co-processor offset out of range"));
22204       value *= 4;
22205       goto cp_off_common;
22206
22207     case BFD_RELOC_ARM_THUMB_OFFSET:
22208       newval = md_chars_to_number (buf, THUMB_SIZE);
22209       /* Exactly what ranges, and where the offset is inserted depends
22210          on the type of instruction, we can establish this from the
22211          top 4 bits.  */
22212       switch (newval >> 12)
22213         {
22214         case 4: /* PC load.  */
22215           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
22216              forced to zero for these loads; md_pcrel_from has already
22217              compensated for this.  */
22218           if (value & 3)
22219             as_bad_where (fixP->fx_file, fixP->fx_line,
22220                           _("invalid offset, target not word aligned (0x%08lX)"),
22221                           (((unsigned long) fixP->fx_frag->fr_address
22222                             + (unsigned long) fixP->fx_where) & ~3)
22223                           + (unsigned long) value);
22224
22225           if (value & ~0x3fc)
22226             as_bad_where (fixP->fx_file, fixP->fx_line,
22227                           _("invalid offset, value too big (0x%08lX)"),
22228                           (long) value);
22229
22230           newval |= value >> 2;
22231           break;
22232
22233         case 9: /* SP load/store.  */
22234           if (value & ~0x3fc)
22235             as_bad_where (fixP->fx_file, fixP->fx_line,
22236                           _("invalid offset, value too big (0x%08lX)"),
22237                           (long) value);
22238           newval |= value >> 2;
22239           break;
22240
22241         case 6: /* Word load/store.  */
22242           if (value & ~0x7c)
22243             as_bad_where (fixP->fx_file, fixP->fx_line,
22244                           _("invalid offset, value too big (0x%08lX)"),
22245                           (long) value);
22246           newval |= value << 4; /* 6 - 2.  */
22247           break;
22248
22249         case 7: /* Byte load/store.  */
22250           if (value & ~0x1f)
22251             as_bad_where (fixP->fx_file, fixP->fx_line,
22252                           _("invalid offset, value too big (0x%08lX)"),
22253                           (long) value);
22254           newval |= value << 6;
22255           break;
22256
22257         case 8: /* Halfword load/store.  */
22258           if (value & ~0x3e)
22259             as_bad_where (fixP->fx_file, fixP->fx_line,
22260                           _("invalid offset, value too big (0x%08lX)"),
22261                           (long) value);
22262           newval |= value << 5; /* 6 - 1.  */
22263           break;
22264
22265         default:
22266           as_bad_where (fixP->fx_file, fixP->fx_line,
22267                         "Unable to process relocation for thumb opcode: %lx",
22268                         (unsigned long) newval);
22269           break;
22270         }
22271       md_number_to_chars (buf, newval, THUMB_SIZE);
22272       break;
22273
22274     case BFD_RELOC_ARM_THUMB_ADD:
22275       /* This is a complicated relocation, since we use it for all of
22276          the following immediate relocations:
22277
22278             3bit ADD/SUB
22279             8bit ADD/SUB
22280             9bit ADD/SUB SP word-aligned
22281            10bit ADD PC/SP word-aligned
22282
22283          The type of instruction being processed is encoded in the
22284          instruction field:
22285
22286            0x8000  SUB
22287            0x00F0  Rd
22288            0x000F  Rs
22289       */
22290       newval = md_chars_to_number (buf, THUMB_SIZE);
22291       {
22292         int rd = (newval >> 4) & 0xf;
22293         int rs = newval & 0xf;
22294         int subtract = !!(newval & 0x8000);
22295
22296         /* Check for HI regs, only very restricted cases allowed:
22297            Adjusting SP, and using PC or SP to get an address.  */
22298         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
22299             || (rs > 7 && rs != REG_SP && rs != REG_PC))
22300           as_bad_where (fixP->fx_file, fixP->fx_line,
22301                         _("invalid Hi register with immediate"));
22302
22303         /* If value is negative, choose the opposite instruction.  */
22304         if (value < 0)
22305           {
22306             value = -value;
22307             subtract = !subtract;
22308             if (value < 0)
22309               as_bad_where (fixP->fx_file, fixP->fx_line,
22310                             _("immediate value out of range"));
22311           }
22312
22313         if (rd == REG_SP)
22314           {
22315             if (value & ~0x1fc)
22316               as_bad_where (fixP->fx_file, fixP->fx_line,
22317                             _("invalid immediate for stack address calculation"));
22318             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
22319             newval |= value >> 2;
22320           }
22321         else if (rs == REG_PC || rs == REG_SP)
22322           {
22323             if (subtract || value & ~0x3fc)
22324               as_bad_where (fixP->fx_file, fixP->fx_line,
22325                             _("invalid immediate for address calculation (value = 0x%08lX)"),
22326                             (unsigned long) value);
22327             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
22328             newval |= rd << 8;
22329             newval |= value >> 2;
22330           }
22331         else if (rs == rd)
22332           {
22333             if (value & ~0xff)
22334               as_bad_where (fixP->fx_file, fixP->fx_line,
22335                             _("immediate value out of range"));
22336             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
22337             newval |= (rd << 8) | value;
22338           }
22339         else
22340           {
22341             if (value & ~0x7)
22342               as_bad_where (fixP->fx_file, fixP->fx_line,
22343                             _("immediate value out of range"));
22344             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
22345             newval |= rd | (rs << 3) | (value << 6);
22346           }
22347       }
22348       md_number_to_chars (buf, newval, THUMB_SIZE);
22349       break;
22350
22351     case BFD_RELOC_ARM_THUMB_IMM:
22352       newval = md_chars_to_number (buf, THUMB_SIZE);
22353       if (value < 0 || value > 255)
22354         as_bad_where (fixP->fx_file, fixP->fx_line,
22355                       _("invalid immediate: %ld is out of range"),
22356                       (long) value);
22357       newval |= value;
22358       md_number_to_chars (buf, newval, THUMB_SIZE);
22359       break;
22360
22361     case BFD_RELOC_ARM_THUMB_SHIFT:
22362       /* 5bit shift value (0..32).  LSL cannot take 32.  */
22363       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
22364       temp = newval & 0xf800;
22365       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
22366         as_bad_where (fixP->fx_file, fixP->fx_line,
22367                       _("invalid shift value: %ld"), (long) value);
22368       /* Shifts of zero must be encoded as LSL.  */
22369       if (value == 0)
22370         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
22371       /* Shifts of 32 are encoded as zero.  */
22372       else if (value == 32)
22373         value = 0;
22374       newval |= value << 6;
22375       md_number_to_chars (buf, newval, THUMB_SIZE);
22376       break;
22377
22378     case BFD_RELOC_VTABLE_INHERIT:
22379     case BFD_RELOC_VTABLE_ENTRY:
22380       fixP->fx_done = 0;
22381       return;
22382
22383     case BFD_RELOC_ARM_MOVW:
22384     case BFD_RELOC_ARM_MOVT:
22385     case BFD_RELOC_ARM_THUMB_MOVW:
22386     case BFD_RELOC_ARM_THUMB_MOVT:
22387       if (fixP->fx_done || !seg->use_rela_p)
22388         {
22389           /* REL format relocations are limited to a 16-bit addend.  */
22390           if (!fixP->fx_done)
22391             {
22392               if (value < -0x8000 || value > 0x7fff)
22393                   as_bad_where (fixP->fx_file, fixP->fx_line,
22394                                 _("offset out of range"));
22395             }
22396           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
22397                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
22398             {
22399               value >>= 16;
22400             }
22401
22402           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
22403               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
22404             {
22405               newval = get_thumb32_insn (buf);
22406               newval &= 0xfbf08f00;
22407               newval |= (value & 0xf000) << 4;
22408               newval |= (value & 0x0800) << 15;
22409               newval |= (value & 0x0700) << 4;
22410               newval |= (value & 0x00ff);
22411               put_thumb32_insn (buf, newval);
22412             }
22413           else
22414             {
22415               newval = md_chars_to_number (buf, 4);
22416               newval &= 0xfff0f000;
22417               newval |= value & 0x0fff;
22418               newval |= (value & 0xf000) << 4;
22419               md_number_to_chars (buf, newval, 4);
22420             }
22421         }
22422       return;
22423
22424    case BFD_RELOC_ARM_ALU_PC_G0_NC:
22425    case BFD_RELOC_ARM_ALU_PC_G0:
22426    case BFD_RELOC_ARM_ALU_PC_G1_NC:
22427    case BFD_RELOC_ARM_ALU_PC_G1:
22428    case BFD_RELOC_ARM_ALU_PC_G2:
22429    case BFD_RELOC_ARM_ALU_SB_G0_NC:
22430    case BFD_RELOC_ARM_ALU_SB_G0:
22431    case BFD_RELOC_ARM_ALU_SB_G1_NC:
22432    case BFD_RELOC_ARM_ALU_SB_G1:
22433    case BFD_RELOC_ARM_ALU_SB_G2:
22434      gas_assert (!fixP->fx_done);
22435      if (!seg->use_rela_p)
22436        {
22437          bfd_vma insn;
22438          bfd_vma encoded_addend;
22439          bfd_vma addend_abs = abs (value);
22440
22441          /* Check that the absolute value of the addend can be
22442             expressed as an 8-bit constant plus a rotation.  */
22443          encoded_addend = encode_arm_immediate (addend_abs);
22444          if (encoded_addend == (unsigned int) FAIL)
22445            as_bad_where (fixP->fx_file, fixP->fx_line,
22446                          _("the offset 0x%08lX is not representable"),
22447                          (unsigned long) addend_abs);
22448
22449          /* Extract the instruction.  */
22450          insn = md_chars_to_number (buf, INSN_SIZE);
22451
22452          /* If the addend is positive, use an ADD instruction.
22453             Otherwise use a SUB.  Take care not to destroy the S bit.  */
22454          insn &= 0xff1fffff;
22455          if (value < 0)
22456            insn |= 1 << 22;
22457          else
22458            insn |= 1 << 23;
22459
22460          /* Place the encoded addend into the first 12 bits of the
22461             instruction.  */
22462          insn &= 0xfffff000;
22463          insn |= encoded_addend;
22464
22465          /* Update the instruction.  */
22466          md_number_to_chars (buf, insn, INSN_SIZE);
22467        }
22468      break;
22469
22470     case BFD_RELOC_ARM_LDR_PC_G0:
22471     case BFD_RELOC_ARM_LDR_PC_G1:
22472     case BFD_RELOC_ARM_LDR_PC_G2:
22473     case BFD_RELOC_ARM_LDR_SB_G0:
22474     case BFD_RELOC_ARM_LDR_SB_G1:
22475     case BFD_RELOC_ARM_LDR_SB_G2:
22476       gas_assert (!fixP->fx_done);
22477       if (!seg->use_rela_p)
22478         {
22479           bfd_vma insn;
22480           bfd_vma addend_abs = abs (value);
22481
22482           /* Check that the absolute value of the addend can be
22483              encoded in 12 bits.  */
22484           if (addend_abs >= 0x1000)
22485             as_bad_where (fixP->fx_file, fixP->fx_line,
22486                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
22487                           (unsigned long) addend_abs);
22488
22489           /* Extract the instruction.  */
22490           insn = md_chars_to_number (buf, INSN_SIZE);
22491
22492           /* If the addend is negative, clear bit 23 of the instruction.
22493              Otherwise set it.  */
22494           if (value < 0)
22495             insn &= ~(1 << 23);
22496           else
22497             insn |= 1 << 23;
22498
22499           /* Place the absolute value of the addend into the first 12 bits
22500              of the instruction.  */
22501           insn &= 0xfffff000;
22502           insn |= addend_abs;
22503
22504           /* Update the instruction.  */
22505           md_number_to_chars (buf, insn, INSN_SIZE);
22506         }
22507       break;
22508
22509     case BFD_RELOC_ARM_LDRS_PC_G0:
22510     case BFD_RELOC_ARM_LDRS_PC_G1:
22511     case BFD_RELOC_ARM_LDRS_PC_G2:
22512     case BFD_RELOC_ARM_LDRS_SB_G0:
22513     case BFD_RELOC_ARM_LDRS_SB_G1:
22514     case BFD_RELOC_ARM_LDRS_SB_G2:
22515       gas_assert (!fixP->fx_done);
22516       if (!seg->use_rela_p)
22517         {
22518           bfd_vma insn;
22519           bfd_vma addend_abs = abs (value);
22520
22521           /* Check that the absolute value of the addend can be
22522              encoded in 8 bits.  */
22523           if (addend_abs >= 0x100)
22524             as_bad_where (fixP->fx_file, fixP->fx_line,
22525                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
22526                           (unsigned long) addend_abs);
22527
22528           /* Extract the instruction.  */
22529           insn = md_chars_to_number (buf, INSN_SIZE);
22530
22531           /* If the addend is negative, clear bit 23 of the instruction.
22532              Otherwise set it.  */
22533           if (value < 0)
22534             insn &= ~(1 << 23);
22535           else
22536             insn |= 1 << 23;
22537
22538           /* Place the first four bits of the absolute value of the addend
22539              into the first 4 bits of the instruction, and the remaining
22540              four into bits 8 .. 11.  */
22541           insn &= 0xfffff0f0;
22542           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
22543
22544           /* Update the instruction.  */
22545           md_number_to_chars (buf, insn, INSN_SIZE);
22546         }
22547       break;
22548
22549     case BFD_RELOC_ARM_LDC_PC_G0:
22550     case BFD_RELOC_ARM_LDC_PC_G1:
22551     case BFD_RELOC_ARM_LDC_PC_G2:
22552     case BFD_RELOC_ARM_LDC_SB_G0:
22553     case BFD_RELOC_ARM_LDC_SB_G1:
22554     case BFD_RELOC_ARM_LDC_SB_G2:
22555       gas_assert (!fixP->fx_done);
22556       if (!seg->use_rela_p)
22557         {
22558           bfd_vma insn;
22559           bfd_vma addend_abs = abs (value);
22560
22561           /* Check that the absolute value of the addend is a multiple of
22562              four and, when divided by four, fits in 8 bits.  */
22563           if (addend_abs & 0x3)
22564             as_bad_where (fixP->fx_file, fixP->fx_line,
22565                           _("bad offset 0x%08lX (must be word-aligned)"),
22566                           (unsigned long) addend_abs);
22567
22568           if ((addend_abs >> 2) > 0xff)
22569             as_bad_where (fixP->fx_file, fixP->fx_line,
22570                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
22571                           (unsigned long) addend_abs);
22572
22573           /* Extract the instruction.  */
22574           insn = md_chars_to_number (buf, INSN_SIZE);
22575
22576           /* If the addend is negative, clear bit 23 of the instruction.
22577              Otherwise set it.  */
22578           if (value < 0)
22579             insn &= ~(1 << 23);
22580           else
22581             insn |= 1 << 23;
22582
22583           /* Place the addend (divided by four) into the first eight
22584              bits of the instruction.  */
22585           insn &= 0xfffffff0;
22586           insn |= addend_abs >> 2;
22587
22588           /* Update the instruction.  */
22589           md_number_to_chars (buf, insn, INSN_SIZE);
22590         }
22591       break;
22592
22593     case BFD_RELOC_ARM_V4BX:
22594       /* This will need to go in the object file.  */
22595       fixP->fx_done = 0;
22596       break;
22597
22598     case BFD_RELOC_UNUSED:
22599     default:
22600       as_bad_where (fixP->fx_file, fixP->fx_line,
22601                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
22602     }
22603 }
22604
22605 /* Translate internal representation of relocation info to BFD target
22606    format.  */
22607
22608 arelent *
22609 tc_gen_reloc (asection *section, fixS *fixp)
22610 {
22611   arelent * reloc;
22612   bfd_reloc_code_real_type code;
22613
22614   reloc = (arelent *) xmalloc (sizeof (arelent));
22615
22616   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
22617   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
22618   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
22619
22620   if (fixp->fx_pcrel)
22621     {
22622       if (section->use_rela_p)
22623         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
22624       else
22625         fixp->fx_offset = reloc->address;
22626     }
22627   reloc->addend = fixp->fx_offset;
22628
22629   switch (fixp->fx_r_type)
22630     {
22631     case BFD_RELOC_8:
22632       if (fixp->fx_pcrel)
22633         {
22634           code = BFD_RELOC_8_PCREL;
22635           break;
22636         }
22637
22638     case BFD_RELOC_16:
22639       if (fixp->fx_pcrel)
22640         {
22641           code = BFD_RELOC_16_PCREL;
22642           break;
22643         }
22644
22645     case BFD_RELOC_32:
22646       if (fixp->fx_pcrel)
22647         {
22648           code = BFD_RELOC_32_PCREL;
22649           break;
22650         }
22651
22652     case BFD_RELOC_ARM_MOVW:
22653       if (fixp->fx_pcrel)
22654         {
22655           code = BFD_RELOC_ARM_MOVW_PCREL;
22656           break;
22657         }
22658
22659     case BFD_RELOC_ARM_MOVT:
22660       if (fixp->fx_pcrel)
22661         {
22662           code = BFD_RELOC_ARM_MOVT_PCREL;
22663           break;
22664         }
22665
22666     case BFD_RELOC_ARM_THUMB_MOVW:
22667       if (fixp->fx_pcrel)
22668         {
22669           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
22670           break;
22671         }
22672
22673     case BFD_RELOC_ARM_THUMB_MOVT:
22674       if (fixp->fx_pcrel)
22675         {
22676           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
22677           break;
22678         }
22679
22680     case BFD_RELOC_NONE:
22681     case BFD_RELOC_ARM_PCREL_BRANCH:
22682     case BFD_RELOC_ARM_PCREL_BLX:
22683     case BFD_RELOC_RVA:
22684     case BFD_RELOC_THUMB_PCREL_BRANCH7:
22685     case BFD_RELOC_THUMB_PCREL_BRANCH9:
22686     case BFD_RELOC_THUMB_PCREL_BRANCH12:
22687     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22688     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22689     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22690     case BFD_RELOC_VTABLE_ENTRY:
22691     case BFD_RELOC_VTABLE_INHERIT:
22692 #ifdef TE_PE
22693     case BFD_RELOC_32_SECREL:
22694 #endif
22695       code = fixp->fx_r_type;
22696       break;
22697
22698     case BFD_RELOC_THUMB_PCREL_BLX:
22699 #ifdef OBJ_ELF
22700       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
22701         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
22702       else
22703 #endif
22704         code = BFD_RELOC_THUMB_PCREL_BLX;
22705       break;
22706
22707     case BFD_RELOC_ARM_LITERAL:
22708     case BFD_RELOC_ARM_HWLITERAL:
22709       /* If this is called then the a literal has
22710          been referenced across a section boundary.  */
22711       as_bad_where (fixp->fx_file, fixp->fx_line,
22712                     _("literal referenced across section boundary"));
22713       return NULL;
22714
22715 #ifdef OBJ_ELF
22716     case BFD_RELOC_ARM_TLS_CALL:
22717     case BFD_RELOC_ARM_THM_TLS_CALL:
22718     case BFD_RELOC_ARM_TLS_DESCSEQ:
22719     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
22720     case BFD_RELOC_ARM_GOT32:
22721     case BFD_RELOC_ARM_GOTOFF:
22722     case BFD_RELOC_ARM_GOT_PREL:
22723     case BFD_RELOC_ARM_PLT32:
22724     case BFD_RELOC_ARM_TARGET1:
22725     case BFD_RELOC_ARM_ROSEGREL32:
22726     case BFD_RELOC_ARM_SBREL32:
22727     case BFD_RELOC_ARM_PREL31:
22728     case BFD_RELOC_ARM_TARGET2:
22729     case BFD_RELOC_ARM_TLS_LE32:
22730     case BFD_RELOC_ARM_TLS_LDO32:
22731     case BFD_RELOC_ARM_PCREL_CALL:
22732     case BFD_RELOC_ARM_PCREL_JUMP:
22733     case BFD_RELOC_ARM_ALU_PC_G0_NC:
22734     case BFD_RELOC_ARM_ALU_PC_G0:
22735     case BFD_RELOC_ARM_ALU_PC_G1_NC:
22736     case BFD_RELOC_ARM_ALU_PC_G1:
22737     case BFD_RELOC_ARM_ALU_PC_G2:
22738     case BFD_RELOC_ARM_LDR_PC_G0:
22739     case BFD_RELOC_ARM_LDR_PC_G1:
22740     case BFD_RELOC_ARM_LDR_PC_G2:
22741     case BFD_RELOC_ARM_LDRS_PC_G0:
22742     case BFD_RELOC_ARM_LDRS_PC_G1:
22743     case BFD_RELOC_ARM_LDRS_PC_G2:
22744     case BFD_RELOC_ARM_LDC_PC_G0:
22745     case BFD_RELOC_ARM_LDC_PC_G1:
22746     case BFD_RELOC_ARM_LDC_PC_G2:
22747     case BFD_RELOC_ARM_ALU_SB_G0_NC:
22748     case BFD_RELOC_ARM_ALU_SB_G0:
22749     case BFD_RELOC_ARM_ALU_SB_G1_NC:
22750     case BFD_RELOC_ARM_ALU_SB_G1:
22751     case BFD_RELOC_ARM_ALU_SB_G2:
22752     case BFD_RELOC_ARM_LDR_SB_G0:
22753     case BFD_RELOC_ARM_LDR_SB_G1:
22754     case BFD_RELOC_ARM_LDR_SB_G2:
22755     case BFD_RELOC_ARM_LDRS_SB_G0:
22756     case BFD_RELOC_ARM_LDRS_SB_G1:
22757     case BFD_RELOC_ARM_LDRS_SB_G2:
22758     case BFD_RELOC_ARM_LDC_SB_G0:
22759     case BFD_RELOC_ARM_LDC_SB_G1:
22760     case BFD_RELOC_ARM_LDC_SB_G2:
22761     case BFD_RELOC_ARM_V4BX:
22762       code = fixp->fx_r_type;
22763       break;
22764
22765     case BFD_RELOC_ARM_TLS_GOTDESC:
22766     case BFD_RELOC_ARM_TLS_GD32:
22767     case BFD_RELOC_ARM_TLS_IE32:
22768     case BFD_RELOC_ARM_TLS_LDM32:
22769       /* BFD will include the symbol's address in the addend.
22770          But we don't want that, so subtract it out again here.  */
22771       if (!S_IS_COMMON (fixp->fx_addsy))
22772         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
22773       code = fixp->fx_r_type;
22774       break;
22775 #endif
22776
22777     case BFD_RELOC_ARM_IMMEDIATE:
22778       as_bad_where (fixp->fx_file, fixp->fx_line,
22779                     _("internal relocation (type: IMMEDIATE) not fixed up"));
22780       return NULL;
22781
22782     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
22783       as_bad_where (fixp->fx_file, fixp->fx_line,
22784                     _("ADRL used for a symbol not defined in the same file"));
22785       return NULL;
22786
22787     case BFD_RELOC_ARM_OFFSET_IMM:
22788       if (section->use_rela_p)
22789         {
22790           code = fixp->fx_r_type;
22791           break;
22792         }
22793
22794       if (fixp->fx_addsy != NULL
22795           && !S_IS_DEFINED (fixp->fx_addsy)
22796           && S_IS_LOCAL (fixp->fx_addsy))
22797         {
22798           as_bad_where (fixp->fx_file, fixp->fx_line,
22799                         _("undefined local label `%s'"),
22800                         S_GET_NAME (fixp->fx_addsy));
22801           return NULL;
22802         }
22803
22804       as_bad_where (fixp->fx_file, fixp->fx_line,
22805                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
22806       return NULL;
22807
22808     default:
22809       {
22810         char * type;
22811
22812         switch (fixp->fx_r_type)
22813           {
22814           case BFD_RELOC_NONE:             type = "NONE";         break;
22815           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
22816           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
22817           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
22818           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
22819           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
22820           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
22821           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
22822           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
22823           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
22824           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
22825           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
22826           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
22827           default:                         type = _("<unknown>"); break;
22828           }
22829         as_bad_where (fixp->fx_file, fixp->fx_line,
22830                       _("cannot represent %s relocation in this object file format"),
22831                       type);
22832         return NULL;
22833       }
22834     }
22835
22836 #ifdef OBJ_ELF
22837   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
22838       && GOT_symbol
22839       && fixp->fx_addsy == GOT_symbol)
22840     {
22841       code = BFD_RELOC_ARM_GOTPC;
22842       reloc->addend = fixp->fx_offset = reloc->address;
22843     }
22844 #endif
22845
22846   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
22847
22848   if (reloc->howto == NULL)
22849     {
22850       as_bad_where (fixp->fx_file, fixp->fx_line,
22851                     _("cannot represent %s relocation in this object file format"),
22852                     bfd_get_reloc_code_name (code));
22853       return NULL;
22854     }
22855
22856   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
22857      vtable entry to be used in the relocation's section offset.  */
22858   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
22859     reloc->address = fixp->fx_offset;
22860
22861   return reloc;
22862 }
22863
22864 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
22865
22866 void
22867 cons_fix_new_arm (fragS *       frag,
22868                   int           where,
22869                   int           size,
22870                   expressionS * exp)
22871 {
22872   bfd_reloc_code_real_type type;
22873   int pcrel = 0;
22874
22875   /* Pick a reloc.
22876      FIXME: @@ Should look at CPU word size.  */
22877   switch (size)
22878     {
22879     case 1:
22880       type = BFD_RELOC_8;
22881       break;
22882     case 2:
22883       type = BFD_RELOC_16;
22884       break;
22885     case 4:
22886     default:
22887       type = BFD_RELOC_32;
22888       break;
22889     case 8:
22890       type = BFD_RELOC_64;
22891       break;
22892     }
22893
22894 #ifdef TE_PE
22895   if (exp->X_op == O_secrel)
22896   {
22897     exp->X_op = O_symbol;
22898     type = BFD_RELOC_32_SECREL;
22899   }
22900 #endif
22901
22902   fix_new_exp (frag, where, (int) size, exp, pcrel, type);
22903 }
22904
22905 #if defined (OBJ_COFF)
22906 void
22907 arm_validate_fix (fixS * fixP)
22908 {
22909   /* If the destination of the branch is a defined symbol which does not have
22910      the THUMB_FUNC attribute, then we must be calling a function which has
22911      the (interfacearm) attribute.  We look for the Thumb entry point to that
22912      function and change the branch to refer to that function instead.  */
22913   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
22914       && fixP->fx_addsy != NULL
22915       && S_IS_DEFINED (fixP->fx_addsy)
22916       && ! THUMB_IS_FUNC (fixP->fx_addsy))
22917     {
22918       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
22919     }
22920 }
22921 #endif
22922
22923
22924 int
22925 arm_force_relocation (struct fix * fixp)
22926 {
22927 #if defined (OBJ_COFF) && defined (TE_PE)
22928   if (fixp->fx_r_type == BFD_RELOC_RVA)
22929     return 1;
22930 #endif
22931
22932   /* In case we have a call or a branch to a function in ARM ISA mode from
22933      a thumb function or vice-versa force the relocation. These relocations
22934      are cleared off for some cores that might have blx and simple transformations
22935      are possible.  */
22936
22937 #ifdef OBJ_ELF
22938   switch (fixp->fx_r_type)
22939     {
22940     case BFD_RELOC_ARM_PCREL_JUMP:
22941     case BFD_RELOC_ARM_PCREL_CALL:
22942     case BFD_RELOC_THUMB_PCREL_BLX:
22943       if (THUMB_IS_FUNC (fixp->fx_addsy))
22944         return 1;
22945       break;
22946
22947     case BFD_RELOC_ARM_PCREL_BLX:
22948     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22949     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22950     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22951       if (ARM_IS_FUNC (fixp->fx_addsy))
22952         return 1;
22953       break;
22954
22955     default:
22956       break;
22957     }
22958 #endif
22959
22960   /* Resolve these relocations even if the symbol is extern or weak.
22961      Technically this is probably wrong due to symbol preemption.
22962      In practice these relocations do not have enough range to be useful
22963      at dynamic link time, and some code (e.g. in the Linux kernel)
22964      expects these references to be resolved.  */
22965   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
22966       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
22967       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
22968       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
22969       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
22970       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
22971       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
22972       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
22973       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
22974       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
22975       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
22976       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
22977       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
22978       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
22979     return 0;
22980
22981   /* Always leave these relocations for the linker.  */
22982   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
22983        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
22984       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
22985     return 1;
22986
22987   /* Always generate relocations against function symbols.  */
22988   if (fixp->fx_r_type == BFD_RELOC_32
22989       && fixp->fx_addsy
22990       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
22991     return 1;
22992
22993   return generic_force_reloc (fixp);
22994 }
22995
22996 #if defined (OBJ_ELF) || defined (OBJ_COFF)
22997 /* Relocations against function names must be left unadjusted,
22998    so that the linker can use this information to generate interworking
22999    stubs.  The MIPS version of this function
23000    also prevents relocations that are mips-16 specific, but I do not
23001    know why it does this.
23002
23003    FIXME:
23004    There is one other problem that ought to be addressed here, but
23005    which currently is not:  Taking the address of a label (rather
23006    than a function) and then later jumping to that address.  Such
23007    addresses also ought to have their bottom bit set (assuming that
23008    they reside in Thumb code), but at the moment they will not.  */
23009
23010 bfd_boolean
23011 arm_fix_adjustable (fixS * fixP)
23012 {
23013   if (fixP->fx_addsy == NULL)
23014     return 1;
23015
23016   /* Preserve relocations against symbols with function type.  */
23017   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
23018     return FALSE;
23019
23020   if (THUMB_IS_FUNC (fixP->fx_addsy)
23021       && fixP->fx_subsy == NULL)
23022     return FALSE;
23023
23024   /* We need the symbol name for the VTABLE entries.  */
23025   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
23026       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
23027     return FALSE;
23028
23029   /* Don't allow symbols to be discarded on GOT related relocs.  */
23030   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
23031       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
23032       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
23033       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
23034       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
23035       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
23036       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
23037       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
23038       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
23039       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
23040       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
23041       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
23042       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
23043       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
23044     return FALSE;
23045
23046   /* Similarly for group relocations.  */
23047   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
23048        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
23049       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
23050     return FALSE;
23051
23052   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
23053   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
23054       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
23055       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
23056       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
23057       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
23058       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
23059       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
23060       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
23061     return FALSE;
23062
23063   return TRUE;
23064 }
23065 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
23066
23067 #ifdef OBJ_ELF
23068
23069 const char *
23070 elf32_arm_target_format (void)
23071 {
23072 #ifdef TE_SYMBIAN
23073   return (target_big_endian
23074           ? "elf32-bigarm-symbian"
23075           : "elf32-littlearm-symbian");
23076 #elif defined (TE_VXWORKS)
23077   return (target_big_endian
23078           ? "elf32-bigarm-vxworks"
23079           : "elf32-littlearm-vxworks");
23080 #elif defined (TE_NACL)
23081   return (target_big_endian
23082           ? "elf32-bigarm-nacl"
23083           : "elf32-littlearm-nacl");
23084 #else
23085   if (target_big_endian)
23086     return "elf32-bigarm";
23087   else
23088     return "elf32-littlearm";
23089 #endif
23090 }
23091
23092 void
23093 armelf_frob_symbol (symbolS * symp,
23094                     int *     puntp)
23095 {
23096   elf_frob_symbol (symp, puntp);
23097 }
23098 #endif
23099
23100 /* MD interface: Finalization.  */
23101
23102 void
23103 arm_cleanup (void)
23104 {
23105   literal_pool * pool;
23106
23107   /* Ensure that all the IT blocks are properly closed.  */
23108   check_it_blocks_finished ();
23109
23110   for (pool = list_of_pools; pool; pool = pool->next)
23111     {
23112       /* Put it at the end of the relevant section.  */
23113       subseg_set (pool->section, pool->sub_section);
23114 #ifdef OBJ_ELF
23115       arm_elf_change_section ();
23116 #endif
23117       s_ltorg (0);
23118     }
23119 }
23120
23121 #ifdef OBJ_ELF
23122 /* Remove any excess mapping symbols generated for alignment frags in
23123    SEC.  We may have created a mapping symbol before a zero byte
23124    alignment; remove it if there's a mapping symbol after the
23125    alignment.  */
23126 static void
23127 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
23128                        void *dummy ATTRIBUTE_UNUSED)
23129 {
23130   segment_info_type *seginfo = seg_info (sec);
23131   fragS *fragp;
23132
23133   if (seginfo == NULL || seginfo->frchainP == NULL)
23134     return;
23135
23136   for (fragp = seginfo->frchainP->frch_root;
23137        fragp != NULL;
23138        fragp = fragp->fr_next)
23139     {
23140       symbolS *sym = fragp->tc_frag_data.last_map;
23141       fragS *next = fragp->fr_next;
23142
23143       /* Variable-sized frags have been converted to fixed size by
23144          this point.  But if this was variable-sized to start with,
23145          there will be a fixed-size frag after it.  So don't handle
23146          next == NULL.  */
23147       if (sym == NULL || next == NULL)
23148         continue;
23149
23150       if (S_GET_VALUE (sym) < next->fr_address)
23151         /* Not at the end of this frag.  */
23152         continue;
23153       know (S_GET_VALUE (sym) == next->fr_address);
23154
23155       do
23156         {
23157           if (next->tc_frag_data.first_map != NULL)
23158             {
23159               /* Next frag starts with a mapping symbol.  Discard this
23160                  one.  */
23161               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
23162               break;
23163             }
23164
23165           if (next->fr_next == NULL)
23166             {
23167               /* This mapping symbol is at the end of the section.  Discard
23168                  it.  */
23169               know (next->fr_fix == 0 && next->fr_var == 0);
23170               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
23171               break;
23172             }
23173
23174           /* As long as we have empty frags without any mapping symbols,
23175              keep looking.  */
23176           /* If the next frag is non-empty and does not start with a
23177              mapping symbol, then this mapping symbol is required.  */
23178           if (next->fr_address != next->fr_next->fr_address)
23179             break;
23180
23181           next = next->fr_next;
23182         }
23183       while (next != NULL);
23184     }
23185 }
23186 #endif
23187
23188 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
23189    ARM ones.  */
23190
23191 void
23192 arm_adjust_symtab (void)
23193 {
23194 #ifdef OBJ_COFF
23195   symbolS * sym;
23196
23197   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
23198     {
23199       if (ARM_IS_THUMB (sym))
23200         {
23201           if (THUMB_IS_FUNC (sym))
23202             {
23203               /* Mark the symbol as a Thumb function.  */
23204               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
23205                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
23206                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
23207
23208               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
23209                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
23210               else
23211                 as_bad (_("%s: unexpected function type: %d"),
23212                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
23213             }
23214           else switch (S_GET_STORAGE_CLASS (sym))
23215             {
23216             case C_EXT:
23217               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
23218               break;
23219             case C_STAT:
23220               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
23221               break;
23222             case C_LABEL:
23223               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
23224               break;
23225             default:
23226               /* Do nothing.  */
23227               break;
23228             }
23229         }
23230
23231       if (ARM_IS_INTERWORK (sym))
23232         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
23233     }
23234 #endif
23235 #ifdef OBJ_ELF
23236   symbolS * sym;
23237   char      bind;
23238
23239   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
23240     {
23241       if (ARM_IS_THUMB (sym))
23242         {
23243           elf_symbol_type * elf_sym;
23244
23245           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
23246           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
23247
23248           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
23249                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
23250             {
23251               /* If it's a .thumb_func, declare it as so,
23252                  otherwise tag label as .code 16.  */
23253               if (THUMB_IS_FUNC (sym))
23254                 elf_sym->internal_elf_sym.st_target_internal
23255                   = ST_BRANCH_TO_THUMB;
23256               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
23257                 elf_sym->internal_elf_sym.st_info =
23258                   ELF_ST_INFO (bind, STT_ARM_16BIT);
23259             }
23260         }
23261     }
23262
23263   /* Remove any overlapping mapping symbols generated by alignment frags.  */
23264   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
23265   /* Now do generic ELF adjustments.  */
23266   elf_adjust_symtab ();
23267 #endif
23268 }
23269
23270 /* MD interface: Initialization.  */
23271
23272 static void
23273 set_constant_flonums (void)
23274 {
23275   int i;
23276
23277   for (i = 0; i < NUM_FLOAT_VALS; i++)
23278     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
23279       abort ();
23280 }
23281
23282 /* Auto-select Thumb mode if it's the only available instruction set for the
23283    given architecture.  */
23284
23285 static void
23286 autoselect_thumb_from_cpu_variant (void)
23287 {
23288   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
23289     opcode_select (16);
23290 }
23291
23292 void
23293 md_begin (void)
23294 {
23295   unsigned mach;
23296   unsigned int i;
23297
23298   if (   (arm_ops_hsh = hash_new ()) == NULL
23299       || (arm_cond_hsh = hash_new ()) == NULL
23300       || (arm_shift_hsh = hash_new ()) == NULL
23301       || (arm_psr_hsh = hash_new ()) == NULL
23302       || (arm_v7m_psr_hsh = hash_new ()) == NULL
23303       || (arm_reg_hsh = hash_new ()) == NULL
23304       || (arm_reloc_hsh = hash_new ()) == NULL
23305       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
23306     as_fatal (_("virtual memory exhausted"));
23307
23308   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
23309     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
23310   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
23311     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
23312   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
23313     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
23314   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
23315     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
23316   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
23317     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
23318                  (void *) (v7m_psrs + i));
23319   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
23320     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
23321   for (i = 0;
23322        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
23323        i++)
23324     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
23325                  (void *) (barrier_opt_names + i));
23326 #ifdef OBJ_ELF
23327   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
23328     {
23329       struct reloc_entry * entry = reloc_names + i;
23330
23331       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
23332         /* This makes encode_branch() use the EABI versions of this relocation.  */
23333         entry->reloc = BFD_RELOC_UNUSED;
23334
23335       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
23336     }
23337 #endif
23338
23339   set_constant_flonums ();
23340
23341   /* Set the cpu variant based on the command-line options.  We prefer
23342      -mcpu= over -march= if both are set (as for GCC); and we prefer
23343      -mfpu= over any other way of setting the floating point unit.
23344      Use of legacy options with new options are faulted.  */
23345   if (legacy_cpu)
23346     {
23347       if (mcpu_cpu_opt || march_cpu_opt)
23348         as_bad (_("use of old and new-style options to set CPU type"));
23349
23350       mcpu_cpu_opt = legacy_cpu;
23351     }
23352   else if (!mcpu_cpu_opt)
23353     mcpu_cpu_opt = march_cpu_opt;
23354
23355   if (legacy_fpu)
23356     {
23357       if (mfpu_opt)
23358         as_bad (_("use of old and new-style options to set FPU type"));
23359
23360       mfpu_opt = legacy_fpu;
23361     }
23362   else if (!mfpu_opt)
23363     {
23364 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
23365         || defined (TE_NetBSD) || defined (TE_VXWORKS))
23366       /* Some environments specify a default FPU.  If they don't, infer it
23367          from the processor.  */
23368       if (mcpu_fpu_opt)
23369         mfpu_opt = mcpu_fpu_opt;
23370       else
23371         mfpu_opt = march_fpu_opt;
23372 #else
23373       mfpu_opt = &fpu_default;
23374 #endif
23375     }
23376
23377   if (!mfpu_opt)
23378     {
23379       if (mcpu_cpu_opt != NULL)
23380         mfpu_opt = &fpu_default;
23381       else if (mcpu_fpu_opt != NULL && ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
23382         mfpu_opt = &fpu_arch_vfp_v2;
23383       else
23384         mfpu_opt = &fpu_arch_fpa;
23385     }
23386
23387 #ifdef CPU_DEFAULT
23388   if (!mcpu_cpu_opt)
23389     {
23390       mcpu_cpu_opt = &cpu_default;
23391       selected_cpu = cpu_default;
23392     }
23393 #else
23394   if (mcpu_cpu_opt)
23395     selected_cpu = *mcpu_cpu_opt;
23396   else
23397     mcpu_cpu_opt = &arm_arch_any;
23398 #endif
23399
23400   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23401
23402   autoselect_thumb_from_cpu_variant ();
23403
23404   arm_arch_used = thumb_arch_used = arm_arch_none;
23405
23406 #if defined OBJ_COFF || defined OBJ_ELF
23407   {
23408     unsigned int flags = 0;
23409
23410 #if defined OBJ_ELF
23411     flags = meabi_flags;
23412
23413     switch (meabi_flags)
23414       {
23415       case EF_ARM_EABI_UNKNOWN:
23416 #endif
23417         /* Set the flags in the private structure.  */
23418         if (uses_apcs_26)      flags |= F_APCS26;
23419         if (support_interwork) flags |= F_INTERWORK;
23420         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
23421         if (pic_code)          flags |= F_PIC;
23422         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
23423           flags |= F_SOFT_FLOAT;
23424
23425         switch (mfloat_abi_opt)
23426           {
23427           case ARM_FLOAT_ABI_SOFT:
23428           case ARM_FLOAT_ABI_SOFTFP:
23429             flags |= F_SOFT_FLOAT;
23430             break;
23431
23432           case ARM_FLOAT_ABI_HARD:
23433             if (flags & F_SOFT_FLOAT)
23434               as_bad (_("hard-float conflicts with specified fpu"));
23435             break;
23436           }
23437
23438         /* Using pure-endian doubles (even if soft-float).      */
23439         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
23440           flags |= F_VFP_FLOAT;
23441
23442 #if defined OBJ_ELF
23443         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
23444             flags |= EF_ARM_MAVERICK_FLOAT;
23445         break;
23446
23447       case EF_ARM_EABI_VER4:
23448       case EF_ARM_EABI_VER5:
23449         /* No additional flags to set.  */
23450         break;
23451
23452       default:
23453         abort ();
23454       }
23455 #endif
23456     bfd_set_private_flags (stdoutput, flags);
23457
23458     /* We have run out flags in the COFF header to encode the
23459        status of ATPCS support, so instead we create a dummy,
23460        empty, debug section called .arm.atpcs.  */
23461     if (atpcs)
23462       {
23463         asection * sec;
23464
23465         sec = bfd_make_section (stdoutput, ".arm.atpcs");
23466
23467         if (sec != NULL)
23468           {
23469             bfd_set_section_flags
23470               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
23471             bfd_set_section_size (stdoutput, sec, 0);
23472             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
23473           }
23474       }
23475   }
23476 #endif
23477
23478   /* Record the CPU type as well.  */
23479   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
23480     mach = bfd_mach_arm_iWMMXt2;
23481   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
23482     mach = bfd_mach_arm_iWMMXt;
23483   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
23484     mach = bfd_mach_arm_XScale;
23485   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
23486     mach = bfd_mach_arm_ep9312;
23487   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
23488     mach = bfd_mach_arm_5TE;
23489   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
23490     {
23491       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
23492         mach = bfd_mach_arm_5T;
23493       else
23494         mach = bfd_mach_arm_5;
23495     }
23496   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
23497     {
23498       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
23499         mach = bfd_mach_arm_4T;
23500       else
23501         mach = bfd_mach_arm_4;
23502     }
23503   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
23504     mach = bfd_mach_arm_3M;
23505   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
23506     mach = bfd_mach_arm_3;
23507   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
23508     mach = bfd_mach_arm_2a;
23509   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
23510     mach = bfd_mach_arm_2;
23511   else
23512     mach = bfd_mach_arm_unknown;
23513
23514   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
23515 }
23516
23517 /* Command line processing.  */
23518
23519 /* md_parse_option
23520       Invocation line includes a switch not recognized by the base assembler.
23521       See if it's a processor-specific option.
23522
23523       This routine is somewhat complicated by the need for backwards
23524       compatibility (since older releases of gcc can't be changed).
23525       The new options try to make the interface as compatible as
23526       possible with GCC.
23527
23528       New options (supported) are:
23529
23530               -mcpu=<cpu name>           Assemble for selected processor
23531               -march=<architecture name> Assemble for selected architecture
23532               -mfpu=<fpu architecture>   Assemble for selected FPU.
23533               -EB/-mbig-endian           Big-endian
23534               -EL/-mlittle-endian        Little-endian
23535               -k                         Generate PIC code
23536               -mthumb                    Start in Thumb mode
23537               -mthumb-interwork          Code supports ARM/Thumb interworking
23538
23539               -m[no-]warn-deprecated     Warn about deprecated features
23540
23541       For now we will also provide support for:
23542
23543               -mapcs-32                  32-bit Program counter
23544               -mapcs-26                  26-bit Program counter
23545               -macps-float               Floats passed in FP registers
23546               -mapcs-reentrant           Reentrant code
23547               -matpcs
23548       (sometime these will probably be replaced with -mapcs=<list of options>
23549       and -matpcs=<list of options>)
23550
23551       The remaining options are only supported for back-wards compatibility.
23552       Cpu variants, the arm part is optional:
23553               -m[arm]1                Currently not supported.
23554               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
23555               -m[arm]3                Arm 3 processor
23556               -m[arm]6[xx],           Arm 6 processors
23557               -m[arm]7[xx][t][[d]m]   Arm 7 processors
23558               -m[arm]8[10]            Arm 8 processors
23559               -m[arm]9[20][tdmi]      Arm 9 processors
23560               -mstrongarm[110[0]]     StrongARM processors
23561               -mxscale                XScale processors
23562               -m[arm]v[2345[t[e]]]    Arm architectures
23563               -mall                   All (except the ARM1)
23564       FP variants:
23565               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
23566               -mfpe-old               (No float load/store multiples)
23567               -mvfpxd                 VFP Single precision
23568               -mvfp                   All VFP
23569               -mno-fpu                Disable all floating point instructions
23570
23571       The following CPU names are recognized:
23572               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
23573               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
23574               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
23575               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
23576               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
23577               arm10t arm10e, arm1020t, arm1020e, arm10200e,
23578               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
23579
23580       */
23581
23582 const char * md_shortopts = "m:k";
23583
23584 #ifdef ARM_BI_ENDIAN
23585 #define OPTION_EB (OPTION_MD_BASE + 0)
23586 #define OPTION_EL (OPTION_MD_BASE + 1)
23587 #else
23588 #if TARGET_BYTES_BIG_ENDIAN
23589 #define OPTION_EB (OPTION_MD_BASE + 0)
23590 #else
23591 #define OPTION_EL (OPTION_MD_BASE + 1)
23592 #endif
23593 #endif
23594 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
23595
23596 struct option md_longopts[] =
23597 {
23598 #ifdef OPTION_EB
23599   {"EB", no_argument, NULL, OPTION_EB},
23600 #endif
23601 #ifdef OPTION_EL
23602   {"EL", no_argument, NULL, OPTION_EL},
23603 #endif
23604   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
23605   {NULL, no_argument, NULL, 0}
23606 };
23607
23608 size_t md_longopts_size = sizeof (md_longopts);
23609
23610 struct arm_option_table
23611 {
23612   char *option;         /* Option name to match.  */
23613   char *help;           /* Help information.  */
23614   int  *var;            /* Variable to change.  */
23615   int   value;          /* What to change it to.  */
23616   char *deprecated;     /* If non-null, print this message.  */
23617 };
23618
23619 struct arm_option_table arm_opts[] =
23620 {
23621   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
23622   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
23623   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
23624    &support_interwork, 1, NULL},
23625   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
23626   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
23627   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
23628    1, NULL},
23629   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
23630   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
23631   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
23632   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
23633    NULL},
23634
23635   /* These are recognized by the assembler, but have no affect on code.  */
23636   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
23637   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
23638
23639   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
23640   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
23641    &warn_on_deprecated, 0, NULL},
23642   {NULL, NULL, NULL, 0, NULL}
23643 };
23644
23645 struct arm_legacy_option_table
23646 {
23647   char *option;                         /* Option name to match.  */
23648   const arm_feature_set **var;          /* Variable to change.  */
23649   const arm_feature_set value;          /* What to change it to.  */
23650   char *deprecated;                     /* If non-null, print this message.  */
23651 };
23652
23653 const struct arm_legacy_option_table arm_legacy_opts[] =
23654 {
23655   /* DON'T add any new processors to this list -- we want the whole list
23656      to go away...  Add them to the processors table instead.  */
23657   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
23658   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
23659   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
23660   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
23661   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
23662   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
23663   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
23664   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
23665   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
23666   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
23667   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
23668   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
23669   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
23670   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
23671   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
23672   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
23673   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
23674   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
23675   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
23676   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
23677   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
23678   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
23679   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
23680   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
23681   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
23682   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
23683   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
23684   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
23685   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
23686   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
23687   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
23688   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
23689   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
23690   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
23691   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
23692   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
23693   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
23694   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
23695   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
23696   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
23697   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
23698   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
23699   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
23700   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
23701   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
23702   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
23703   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
23704   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
23705   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
23706   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
23707   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
23708   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
23709   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
23710   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
23711   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
23712   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
23713   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
23714   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
23715   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
23716   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
23717   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
23718   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
23719   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
23720   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
23721   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
23722   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
23723   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
23724   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
23725   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
23726   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
23727    N_("use -mcpu=strongarm110")},
23728   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
23729    N_("use -mcpu=strongarm1100")},
23730   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
23731    N_("use -mcpu=strongarm1110")},
23732   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
23733   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
23734   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
23735
23736   /* Architecture variants -- don't add any more to this list either.  */
23737   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
23738   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
23739   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
23740   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
23741   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
23742   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
23743   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
23744   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
23745   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
23746   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
23747   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
23748   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
23749   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
23750   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
23751   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
23752   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
23753   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
23754   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
23755
23756   /* Floating point variants -- don't add any more to this list either.  */
23757   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
23758   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
23759   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
23760   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
23761    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
23762
23763   {NULL, NULL, ARM_ARCH_NONE, NULL}
23764 };
23765
23766 struct arm_cpu_option_table
23767 {
23768   char *name;
23769   size_t name_len;
23770   const arm_feature_set value;
23771   /* For some CPUs we assume an FPU unless the user explicitly sets
23772      -mfpu=...  */
23773   const arm_feature_set default_fpu;
23774   /* The canonical name of the CPU, or NULL to use NAME converted to upper
23775      case.  */
23776   const char *canonical_name;
23777 };
23778
23779 /* This list should, at a minimum, contain all the cpu names
23780    recognized by GCC.  */
23781 #define ARM_CPU_OPT(N, V, DF, CN) { N, sizeof (N) - 1, V, DF, CN }
23782 static const struct arm_cpu_option_table arm_cpus[] =
23783 {
23784   ARM_CPU_OPT ("all",           ARM_ANY,         FPU_ARCH_FPA,    NULL),
23785   ARM_CPU_OPT ("arm1",          ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL),
23786   ARM_CPU_OPT ("arm2",          ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL),
23787   ARM_CPU_OPT ("arm250",        ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
23788   ARM_CPU_OPT ("arm3",          ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
23789   ARM_CPU_OPT ("arm6",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23790   ARM_CPU_OPT ("arm60",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23791   ARM_CPU_OPT ("arm600",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23792   ARM_CPU_OPT ("arm610",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23793   ARM_CPU_OPT ("arm620",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23794   ARM_CPU_OPT ("arm7",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23795   ARM_CPU_OPT ("arm7m",         ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
23796   ARM_CPU_OPT ("arm7d",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23797   ARM_CPU_OPT ("arm7dm",        ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
23798   ARM_CPU_OPT ("arm7di",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23799   ARM_CPU_OPT ("arm7dmi",       ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
23800   ARM_CPU_OPT ("arm70",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23801   ARM_CPU_OPT ("arm700",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23802   ARM_CPU_OPT ("arm700i",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23803   ARM_CPU_OPT ("arm710",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23804   ARM_CPU_OPT ("arm710t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23805   ARM_CPU_OPT ("arm720",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23806   ARM_CPU_OPT ("arm720t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23807   ARM_CPU_OPT ("arm740t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23808   ARM_CPU_OPT ("arm710c",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23809   ARM_CPU_OPT ("arm7100",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23810   ARM_CPU_OPT ("arm7500",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23811   ARM_CPU_OPT ("arm7500fe",     ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23812   ARM_CPU_OPT ("arm7t",         ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23813   ARM_CPU_OPT ("arm7tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23814   ARM_CPU_OPT ("arm7tdmi-s",    ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23815   ARM_CPU_OPT ("arm8",          ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23816   ARM_CPU_OPT ("arm810",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23817   ARM_CPU_OPT ("strongarm",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23818   ARM_CPU_OPT ("strongarm1",    ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23819   ARM_CPU_OPT ("strongarm110",  ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23820   ARM_CPU_OPT ("strongarm1100", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23821   ARM_CPU_OPT ("strongarm1110", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23822   ARM_CPU_OPT ("arm9",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23823   ARM_CPU_OPT ("arm920",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"),
23824   ARM_CPU_OPT ("arm920t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23825   ARM_CPU_OPT ("arm922t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23826   ARM_CPU_OPT ("arm940t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23827   ARM_CPU_OPT ("arm9tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23828   ARM_CPU_OPT ("fa526",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23829   ARM_CPU_OPT ("fa626",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23830   /* For V5 or later processors we default to using VFP; but the user
23831      should really set the FPU type explicitly.  */
23832   ARM_CPU_OPT ("arm9e-r0",      ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
23833   ARM_CPU_OPT ("arm9e",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23834   ARM_CPU_OPT ("arm926ej",      ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
23835   ARM_CPU_OPT ("arm926ejs",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
23836   ARM_CPU_OPT ("arm926ej-s",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
23837   ARM_CPU_OPT ("arm946e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
23838   ARM_CPU_OPT ("arm946e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"),
23839   ARM_CPU_OPT ("arm946e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23840   ARM_CPU_OPT ("arm966e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
23841   ARM_CPU_OPT ("arm966e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"),
23842   ARM_CPU_OPT ("arm966e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23843   ARM_CPU_OPT ("arm968e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23844   ARM_CPU_OPT ("arm10t",        ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
23845   ARM_CPU_OPT ("arm10tdmi",     ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
23846   ARM_CPU_OPT ("arm10e",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23847   ARM_CPU_OPT ("arm1020",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"),
23848   ARM_CPU_OPT ("arm1020t",      ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
23849   ARM_CPU_OPT ("arm1020e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23850   ARM_CPU_OPT ("arm1022e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23851   ARM_CPU_OPT ("arm1026ejs",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2,
23852                                                                  "ARM1026EJ-S"),
23853   ARM_CPU_OPT ("arm1026ej-s",   ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
23854   ARM_CPU_OPT ("fa606te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23855   ARM_CPU_OPT ("fa616te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23856   ARM_CPU_OPT ("fa626te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23857   ARM_CPU_OPT ("fmp626",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23858   ARM_CPU_OPT ("fa726te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23859   ARM_CPU_OPT ("arm1136js",     ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"),
23860   ARM_CPU_OPT ("arm1136j-s",    ARM_ARCH_V6,     FPU_NONE,        NULL),
23861   ARM_CPU_OPT ("arm1136jfs",    ARM_ARCH_V6,     FPU_ARCH_VFP_V2,
23862                                                                  "ARM1136JF-S"),
23863   ARM_CPU_OPT ("arm1136jf-s",   ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL),
23864   ARM_CPU_OPT ("mpcore",        ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, "MPCore"),
23865   ARM_CPU_OPT ("mpcorenovfp",   ARM_ARCH_V6K,    FPU_NONE,        "MPCore"),
23866   ARM_CPU_OPT ("arm1156t2-s",   ARM_ARCH_V6T2,   FPU_NONE,        NULL),
23867   ARM_CPU_OPT ("arm1156t2f-s",  ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL),
23868   ARM_CPU_OPT ("arm1176jz-s",   ARM_ARCH_V6ZK,   FPU_NONE,        NULL),
23869   ARM_CPU_OPT ("arm1176jzf-s",  ARM_ARCH_V6ZK,   FPU_ARCH_VFP_V2, NULL),
23870   ARM_CPU_OPT ("cortex-a5",     ARM_ARCH_V7A_MP_SEC,
23871                                                  FPU_NONE,        "Cortex-A5"),
23872   ARM_CPU_OPT ("cortex-a7",     ARM_ARCH_V7A_IDIV_MP_SEC_VIRT,
23873                                                  FPU_ARCH_NEON_VFP_V4,
23874                                                                   "Cortex-A7"),
23875   ARM_CPU_OPT ("cortex-a8",     ARM_ARCH_V7A_SEC,
23876                                                  ARM_FEATURE (0, FPU_VFP_V3
23877                                                         | FPU_NEON_EXT_V1),
23878                                                                   "Cortex-A8"),
23879   ARM_CPU_OPT ("cortex-a9",     ARM_ARCH_V7A_MP_SEC,
23880                                                  ARM_FEATURE (0, FPU_VFP_V3
23881                                                         | FPU_NEON_EXT_V1),
23882                                                                   "Cortex-A9"),
23883   ARM_CPU_OPT ("cortex-a15",    ARM_ARCH_V7A_IDIV_MP_SEC_VIRT,
23884                                                  FPU_ARCH_NEON_VFP_V4,
23885                                                                   "Cortex-A15"),
23886   ARM_CPU_OPT ("cortex-r4",     ARM_ARCH_V7R,    FPU_NONE,        "Cortex-R4"),
23887   ARM_CPU_OPT ("cortex-r4f",    ARM_ARCH_V7R,    FPU_ARCH_VFP_V3D16,
23888                                                                   "Cortex-R4F"),
23889   ARM_CPU_OPT ("cortex-r5",     ARM_ARCH_V7R_IDIV,
23890                                                  FPU_NONE,        "Cortex-R5"),
23891   ARM_CPU_OPT ("cortex-m4",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M4"),
23892   ARM_CPU_OPT ("cortex-m3",     ARM_ARCH_V7M,    FPU_NONE,        "Cortex-M3"),
23893   ARM_CPU_OPT ("cortex-m1",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M1"),
23894   ARM_CPU_OPT ("cortex-m0",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0"),
23895   ARM_CPU_OPT ("cortex-m0plus", ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0+"),
23896   /* ??? XSCALE is really an architecture.  */
23897   ARM_CPU_OPT ("xscale",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
23898   /* ??? iwmmxt is not a processor.  */
23899   ARM_CPU_OPT ("iwmmxt",        ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL),
23900   ARM_CPU_OPT ("iwmmxt2",       ARM_ARCH_IWMMXT2,FPU_ARCH_VFP_V2, NULL),
23901   ARM_CPU_OPT ("i80200",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
23902   /* Maverick */
23903   ARM_CPU_OPT ("ep9312",        ARM_FEATURE (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
23904                                                  FPU_ARCH_MAVERICK, "ARM920T"),
23905   /* Marvell processors.  */
23906   ARM_CPU_OPT ("marvell-pj4",   ARM_FEATURE (ARM_AEXT_V7A | ARM_EXT_MP | ARM_EXT_SEC, 0),
23907                                                 FPU_ARCH_VFP_V3D16, NULL),
23908
23909   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
23910 };
23911 #undef ARM_CPU_OPT
23912
23913 struct arm_arch_option_table
23914 {
23915   char *name;
23916   size_t name_len;
23917   const arm_feature_set value;
23918   const arm_feature_set default_fpu;
23919 };
23920
23921 /* This list should, at a minimum, contain all the architecture names
23922    recognized by GCC.  */
23923 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF }
23924 static const struct arm_arch_option_table arm_archs[] =
23925 {
23926   ARM_ARCH_OPT ("all",          ARM_ANY,         FPU_ARCH_FPA),
23927   ARM_ARCH_OPT ("armv1",        ARM_ARCH_V1,     FPU_ARCH_FPA),
23928   ARM_ARCH_OPT ("armv2",        ARM_ARCH_V2,     FPU_ARCH_FPA),
23929   ARM_ARCH_OPT ("armv2a",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
23930   ARM_ARCH_OPT ("armv2s",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
23931   ARM_ARCH_OPT ("armv3",        ARM_ARCH_V3,     FPU_ARCH_FPA),
23932   ARM_ARCH_OPT ("armv3m",       ARM_ARCH_V3M,    FPU_ARCH_FPA),
23933   ARM_ARCH_OPT ("armv4",        ARM_ARCH_V4,     FPU_ARCH_FPA),
23934   ARM_ARCH_OPT ("armv4xm",      ARM_ARCH_V4xM,   FPU_ARCH_FPA),
23935   ARM_ARCH_OPT ("armv4t",       ARM_ARCH_V4T,    FPU_ARCH_FPA),
23936   ARM_ARCH_OPT ("armv4txm",     ARM_ARCH_V4TxM,  FPU_ARCH_FPA),
23937   ARM_ARCH_OPT ("armv5",        ARM_ARCH_V5,     FPU_ARCH_VFP),
23938   ARM_ARCH_OPT ("armv5t",       ARM_ARCH_V5T,    FPU_ARCH_VFP),
23939   ARM_ARCH_OPT ("armv5txm",     ARM_ARCH_V5TxM,  FPU_ARCH_VFP),
23940   ARM_ARCH_OPT ("armv5te",      ARM_ARCH_V5TE,   FPU_ARCH_VFP),
23941   ARM_ARCH_OPT ("armv5texp",    ARM_ARCH_V5TExP, FPU_ARCH_VFP),
23942   ARM_ARCH_OPT ("armv5tej",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP),
23943   ARM_ARCH_OPT ("armv6",        ARM_ARCH_V6,     FPU_ARCH_VFP),
23944   ARM_ARCH_OPT ("armv6j",       ARM_ARCH_V6,     FPU_ARCH_VFP),
23945   ARM_ARCH_OPT ("armv6k",       ARM_ARCH_V6K,    FPU_ARCH_VFP),
23946   ARM_ARCH_OPT ("armv6z",       ARM_ARCH_V6Z,    FPU_ARCH_VFP),
23947   ARM_ARCH_OPT ("armv6zk",      ARM_ARCH_V6ZK,   FPU_ARCH_VFP),
23948   ARM_ARCH_OPT ("armv6t2",      ARM_ARCH_V6T2,   FPU_ARCH_VFP),
23949   ARM_ARCH_OPT ("armv6kt2",     ARM_ARCH_V6KT2,  FPU_ARCH_VFP),
23950   ARM_ARCH_OPT ("armv6zt2",     ARM_ARCH_V6ZT2,  FPU_ARCH_VFP),
23951   ARM_ARCH_OPT ("armv6zkt2",    ARM_ARCH_V6ZKT2, FPU_ARCH_VFP),
23952   ARM_ARCH_OPT ("armv6-m",      ARM_ARCH_V6M,    FPU_ARCH_VFP),
23953   ARM_ARCH_OPT ("armv6s-m",     ARM_ARCH_V6SM,   FPU_ARCH_VFP),
23954   ARM_ARCH_OPT ("armv7",        ARM_ARCH_V7,     FPU_ARCH_VFP),
23955   /* The official spelling of the ARMv7 profile variants is the dashed form.
23956      Accept the non-dashed form for compatibility with old toolchains.  */
23957   ARM_ARCH_OPT ("armv7a",       ARM_ARCH_V7A,    FPU_ARCH_VFP),
23958   ARM_ARCH_OPT ("armv7r",       ARM_ARCH_V7R,    FPU_ARCH_VFP),
23959   ARM_ARCH_OPT ("armv7m",       ARM_ARCH_V7M,    FPU_ARCH_VFP),
23960   ARM_ARCH_OPT ("armv7-a",      ARM_ARCH_V7A,    FPU_ARCH_VFP),
23961   ARM_ARCH_OPT ("armv7-r",      ARM_ARCH_V7R,    FPU_ARCH_VFP),
23962   ARM_ARCH_OPT ("armv7-m",      ARM_ARCH_V7M,    FPU_ARCH_VFP),
23963   ARM_ARCH_OPT ("armv7e-m",     ARM_ARCH_V7EM,   FPU_ARCH_VFP),
23964   ARM_ARCH_OPT ("armv8-a",      ARM_ARCH_V8A,    FPU_ARCH_VFP),
23965   ARM_ARCH_OPT ("xscale",       ARM_ARCH_XSCALE, FPU_ARCH_VFP),
23966   ARM_ARCH_OPT ("iwmmxt",       ARM_ARCH_IWMMXT, FPU_ARCH_VFP),
23967   ARM_ARCH_OPT ("iwmmxt2",      ARM_ARCH_IWMMXT2,FPU_ARCH_VFP),
23968   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
23969 };
23970 #undef ARM_ARCH_OPT
23971
23972 /* ISA extensions in the co-processor and main instruction set space.  */
23973 struct arm_option_extension_value_table
23974 {
23975   char *name;
23976   size_t name_len;
23977   const arm_feature_set value;
23978   const arm_feature_set allowed_archs;
23979 };
23980
23981 /* The following table must be in alphabetical order with a NULL last entry.
23982    */
23983 #define ARM_EXT_OPT(N, V, AA) { N, sizeof (N) - 1, V, AA }
23984 static const struct arm_option_extension_value_table arm_extensions[] =
23985 {
23986   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
23987                                    ARM_FEATURE (ARM_EXT_V8, 0)),
23988   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8,
23989                                    ARM_FEATURE (ARM_EXT_V8, 0)),
23990   ARM_EXT_OPT ("idiv",  ARM_FEATURE (ARM_EXT_ADIV | ARM_EXT_DIV, 0),
23991                                    ARM_FEATURE (ARM_EXT_V7A | ARM_EXT_V7R, 0)),
23992   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE (0, ARM_CEXT_IWMMXT),       ARM_ANY),
23993   ARM_EXT_OPT ("iwmmxt2",
23994                         ARM_FEATURE (0, ARM_CEXT_IWMMXT2),      ARM_ANY),
23995   ARM_EXT_OPT ("maverick",
23996                         ARM_FEATURE (0, ARM_CEXT_MAVERICK),     ARM_ANY),
23997   ARM_EXT_OPT ("mp",    ARM_FEATURE (ARM_EXT_MP, 0),
23998                                    ARM_FEATURE (ARM_EXT_V7A | ARM_EXT_V7R, 0)),
23999   ARM_EXT_OPT ("simd",   FPU_ARCH_NEON_VFP_ARMV8,
24000                                    ARM_FEATURE (ARM_EXT_V8, 0)),
24001   ARM_EXT_OPT ("os",    ARM_FEATURE (ARM_EXT_OS, 0),
24002                                    ARM_FEATURE (ARM_EXT_V6M, 0)),
24003   ARM_EXT_OPT ("sec",   ARM_FEATURE (ARM_EXT_SEC, 0),
24004                                    ARM_FEATURE (ARM_EXT_V6K | ARM_EXT_V7A, 0)),
24005   ARM_EXT_OPT ("virt",  ARM_FEATURE (ARM_EXT_VIRT | ARM_EXT_ADIV
24006                                      | ARM_EXT_DIV, 0),
24007                                    ARM_FEATURE (ARM_EXT_V7A, 0)),
24008   ARM_EXT_OPT ("xscale",ARM_FEATURE (0, ARM_CEXT_XSCALE),       ARM_ANY),
24009   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
24010 };
24011 #undef ARM_EXT_OPT
24012
24013 /* ISA floating-point and Advanced SIMD extensions.  */
24014 struct arm_option_fpu_value_table
24015 {
24016   char *name;
24017   const arm_feature_set value;
24018 };
24019
24020 /* This list should, at a minimum, contain all the fpu names
24021    recognized by GCC.  */
24022 static const struct arm_option_fpu_value_table arm_fpus[] =
24023 {
24024   {"softfpa",           FPU_NONE},
24025   {"fpe",               FPU_ARCH_FPE},
24026   {"fpe2",              FPU_ARCH_FPE},
24027   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
24028   {"fpa",               FPU_ARCH_FPA},
24029   {"fpa10",             FPU_ARCH_FPA},
24030   {"fpa11",             FPU_ARCH_FPA},
24031   {"arm7500fe",         FPU_ARCH_FPA},
24032   {"softvfp",           FPU_ARCH_VFP},
24033   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
24034   {"vfp",               FPU_ARCH_VFP_V2},
24035   {"vfp9",              FPU_ARCH_VFP_V2},
24036   {"vfp3",              FPU_ARCH_VFP_V3}, /* For backwards compatbility.  */
24037   {"vfp10",             FPU_ARCH_VFP_V2},
24038   {"vfp10-r0",          FPU_ARCH_VFP_V1},
24039   {"vfpxd",             FPU_ARCH_VFP_V1xD},
24040   {"vfpv2",             FPU_ARCH_VFP_V2},
24041   {"vfpv3",             FPU_ARCH_VFP_V3},
24042   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
24043   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
24044   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
24045   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
24046   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
24047   {"arm1020t",          FPU_ARCH_VFP_V1},
24048   {"arm1020e",          FPU_ARCH_VFP_V2},
24049   {"arm1136jfs",        FPU_ARCH_VFP_V2},
24050   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
24051   {"maverick",          FPU_ARCH_MAVERICK},
24052   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
24053   {"neon-fp16",         FPU_ARCH_NEON_FP16},
24054   {"vfpv4",             FPU_ARCH_VFP_V4},
24055   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
24056   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
24057   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
24058   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
24059   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
24060   {"crypto-neon-fp-armv8",
24061                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
24062   {NULL,                ARM_ARCH_NONE}
24063 };
24064
24065 struct arm_option_value_table
24066 {
24067   char *name;
24068   long value;
24069 };
24070
24071 static const struct arm_option_value_table arm_float_abis[] =
24072 {
24073   {"hard",      ARM_FLOAT_ABI_HARD},
24074   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
24075   {"soft",      ARM_FLOAT_ABI_SOFT},
24076   {NULL,        0}
24077 };
24078
24079 #ifdef OBJ_ELF
24080 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
24081 static const struct arm_option_value_table arm_eabis[] =
24082 {
24083   {"gnu",       EF_ARM_EABI_UNKNOWN},
24084   {"4",         EF_ARM_EABI_VER4},
24085   {"5",         EF_ARM_EABI_VER5},
24086   {NULL,        0}
24087 };
24088 #endif
24089
24090 struct arm_long_option_table
24091 {
24092   char * option;                /* Substring to match.  */
24093   char * help;                  /* Help information.  */
24094   int (* func) (char * subopt); /* Function to decode sub-option.  */
24095   char * deprecated;            /* If non-null, print this message.  */
24096 };
24097
24098 static bfd_boolean
24099 arm_parse_extension (char *str, const arm_feature_set **opt_p)
24100 {
24101   arm_feature_set *ext_set = (arm_feature_set *)
24102       xmalloc (sizeof (arm_feature_set));
24103
24104   /* We insist on extensions being specified in alphabetical order, and with
24105      extensions being added before being removed.  We achieve this by having
24106      the global ARM_EXTENSIONS table in alphabetical order, and using the
24107      ADDING_VALUE variable to indicate whether we are adding an extension (1)
24108      or removing it (0) and only allowing it to change in the order
24109      -1 -> 1 -> 0.  */
24110   const struct arm_option_extension_value_table * opt = NULL;
24111   int adding_value = -1;
24112
24113   /* Copy the feature set, so that we can modify it.  */
24114   *ext_set = **opt_p;
24115   *opt_p = ext_set;
24116
24117   while (str != NULL && *str != 0)
24118     {
24119       char *ext;
24120       size_t len;
24121
24122       if (*str != '+')
24123         {
24124           as_bad (_("invalid architectural extension"));
24125           return FALSE;
24126         }
24127
24128       str++;
24129       ext = strchr (str, '+');
24130
24131       if (ext != NULL)
24132         len = ext - str;
24133       else
24134         len = strlen (str);
24135
24136       if (len >= 2 && strncmp (str, "no", 2) == 0)
24137         {
24138           if (adding_value != 0)
24139             {
24140               adding_value = 0;
24141               opt = arm_extensions;
24142             }
24143
24144           len -= 2;
24145           str += 2;
24146         }
24147       else if (len > 0)
24148         {
24149           if (adding_value == -1)
24150             {
24151               adding_value = 1;
24152               opt = arm_extensions;
24153             }
24154           else if (adding_value != 1)
24155             {
24156               as_bad (_("must specify extensions to add before specifying "
24157                         "those to remove"));
24158               return FALSE;
24159             }
24160         }
24161
24162       if (len == 0)
24163         {
24164           as_bad (_("missing architectural extension"));
24165           return FALSE;
24166         }
24167
24168       gas_assert (adding_value != -1);
24169       gas_assert (opt != NULL);
24170
24171       /* Scan over the options table trying to find an exact match. */
24172       for (; opt->name != NULL; opt++)
24173         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
24174           {
24175             /* Check we can apply the extension to this architecture.  */
24176             if (!ARM_CPU_HAS_FEATURE (*ext_set, opt->allowed_archs))
24177               {
24178                 as_bad (_("extension does not apply to the base architecture"));
24179                 return FALSE;
24180               }
24181
24182             /* Add or remove the extension.  */
24183             if (adding_value)
24184               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->value);
24185             else
24186               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->value);
24187
24188             break;
24189           }
24190
24191       if (opt->name == NULL)
24192         {
24193           /* Did we fail to find an extension because it wasn't specified in
24194              alphabetical order, or because it does not exist?  */
24195
24196           for (opt = arm_extensions; opt->name != NULL; opt++)
24197             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
24198               break;
24199
24200           if (opt->name == NULL)
24201             as_bad (_("unknown architectural extension `%s'"), str);
24202           else
24203             as_bad (_("architectural extensions must be specified in "
24204                       "alphabetical order"));
24205
24206           return FALSE;
24207         }
24208       else
24209         {
24210           /* We should skip the extension we've just matched the next time
24211              round.  */
24212           opt++;
24213         }
24214
24215       str = ext;
24216     };
24217
24218   return TRUE;
24219 }
24220
24221 static bfd_boolean
24222 arm_parse_cpu (char *str)
24223 {
24224   const struct arm_cpu_option_table *opt;
24225   char *ext = strchr (str, '+');
24226   size_t len;
24227
24228   if (ext != NULL)
24229     len = ext - str;
24230   else
24231     len = strlen (str);
24232
24233   if (len == 0)
24234     {
24235       as_bad (_("missing cpu name `%s'"), str);
24236       return FALSE;
24237     }
24238
24239   for (opt = arm_cpus; opt->name != NULL; opt++)
24240     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
24241       {
24242         mcpu_cpu_opt = &opt->value;
24243         mcpu_fpu_opt = &opt->default_fpu;
24244         if (opt->canonical_name)
24245           strcpy (selected_cpu_name, opt->canonical_name);
24246         else
24247           {
24248             size_t i;
24249
24250             for (i = 0; i < len; i++)
24251               selected_cpu_name[i] = TOUPPER (opt->name[i]);
24252             selected_cpu_name[i] = 0;
24253           }
24254
24255         if (ext != NULL)
24256           return arm_parse_extension (ext, &mcpu_cpu_opt);
24257
24258         return TRUE;
24259       }
24260
24261   as_bad (_("unknown cpu `%s'"), str);
24262   return FALSE;
24263 }
24264
24265 static bfd_boolean
24266 arm_parse_arch (char *str)
24267 {
24268   const struct arm_arch_option_table *opt;
24269   char *ext = strchr (str, '+');
24270   size_t len;
24271
24272   if (ext != NULL)
24273     len = ext - str;
24274   else
24275     len = strlen (str);
24276
24277   if (len == 0)
24278     {
24279       as_bad (_("missing architecture name `%s'"), str);
24280       return FALSE;
24281     }
24282
24283   for (opt = arm_archs; opt->name != NULL; opt++)
24284     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
24285       {
24286         march_cpu_opt = &opt->value;
24287         march_fpu_opt = &opt->default_fpu;
24288         strcpy (selected_cpu_name, opt->name);
24289
24290         if (ext != NULL)
24291           return arm_parse_extension (ext, &march_cpu_opt);
24292
24293         return TRUE;
24294       }
24295
24296   as_bad (_("unknown architecture `%s'\n"), str);
24297   return FALSE;
24298 }
24299
24300 static bfd_boolean
24301 arm_parse_fpu (char * str)
24302 {
24303   const struct arm_option_fpu_value_table * opt;
24304
24305   for (opt = arm_fpus; opt->name != NULL; opt++)
24306     if (streq (opt->name, str))
24307       {
24308         mfpu_opt = &opt->value;
24309         return TRUE;
24310       }
24311
24312   as_bad (_("unknown floating point format `%s'\n"), str);
24313   return FALSE;
24314 }
24315
24316 static bfd_boolean
24317 arm_parse_float_abi (char * str)
24318 {
24319   const struct arm_option_value_table * opt;
24320
24321   for (opt = arm_float_abis; opt->name != NULL; opt++)
24322     if (streq (opt->name, str))
24323       {
24324         mfloat_abi_opt = opt->value;
24325         return TRUE;
24326       }
24327
24328   as_bad (_("unknown floating point abi `%s'\n"), str);
24329   return FALSE;
24330 }
24331
24332 #ifdef OBJ_ELF
24333 static bfd_boolean
24334 arm_parse_eabi (char * str)
24335 {
24336   const struct arm_option_value_table *opt;
24337
24338   for (opt = arm_eabis; opt->name != NULL; opt++)
24339     if (streq (opt->name, str))
24340       {
24341         meabi_flags = opt->value;
24342         return TRUE;
24343       }
24344   as_bad (_("unknown EABI `%s'\n"), str);
24345   return FALSE;
24346 }
24347 #endif
24348
24349 static bfd_boolean
24350 arm_parse_it_mode (char * str)
24351 {
24352   bfd_boolean ret = TRUE;
24353
24354   if (streq ("arm", str))
24355     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
24356   else if (streq ("thumb", str))
24357     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
24358   else if (streq ("always", str))
24359     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
24360   else if (streq ("never", str))
24361     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
24362   else
24363     {
24364       as_bad (_("unknown implicit IT mode `%s', should be "\
24365                 "arm, thumb, always, or never."), str);
24366       ret = FALSE;
24367     }
24368
24369   return ret;
24370 }
24371
24372 struct arm_long_option_table arm_long_opts[] =
24373 {
24374   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
24375    arm_parse_cpu, NULL},
24376   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
24377    arm_parse_arch, NULL},
24378   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
24379    arm_parse_fpu, NULL},
24380   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
24381    arm_parse_float_abi, NULL},
24382 #ifdef OBJ_ELF
24383   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
24384    arm_parse_eabi, NULL},
24385 #endif
24386   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
24387    arm_parse_it_mode, NULL},
24388   {NULL, NULL, 0, NULL}
24389 };
24390
24391 int
24392 md_parse_option (int c, char * arg)
24393 {
24394   struct arm_option_table *opt;
24395   const struct arm_legacy_option_table *fopt;
24396   struct arm_long_option_table *lopt;
24397
24398   switch (c)
24399     {
24400 #ifdef OPTION_EB
24401     case OPTION_EB:
24402       target_big_endian = 1;
24403       break;
24404 #endif
24405
24406 #ifdef OPTION_EL
24407     case OPTION_EL:
24408       target_big_endian = 0;
24409       break;
24410 #endif
24411
24412     case OPTION_FIX_V4BX:
24413       fix_v4bx = TRUE;
24414       break;
24415
24416     case 'a':
24417       /* Listing option.  Just ignore these, we don't support additional
24418          ones.  */
24419       return 0;
24420
24421     default:
24422       for (opt = arm_opts; opt->option != NULL; opt++)
24423         {
24424           if (c == opt->option[0]
24425               && ((arg == NULL && opt->option[1] == 0)
24426                   || streq (arg, opt->option + 1)))
24427             {
24428               /* If the option is deprecated, tell the user.  */
24429               if (warn_on_deprecated && opt->deprecated != NULL)
24430                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
24431                            arg ? arg : "", _(opt->deprecated));
24432
24433               if (opt->var != NULL)
24434                 *opt->var = opt->value;
24435
24436               return 1;
24437             }
24438         }
24439
24440       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
24441         {
24442           if (c == fopt->option[0]
24443               && ((arg == NULL && fopt->option[1] == 0)
24444                   || streq (arg, fopt->option + 1)))
24445             {
24446               /* If the option is deprecated, tell the user.  */
24447               if (warn_on_deprecated && fopt->deprecated != NULL)
24448                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
24449                            arg ? arg : "", _(fopt->deprecated));
24450
24451               if (fopt->var != NULL)
24452                 *fopt->var = &fopt->value;
24453
24454               return 1;
24455             }
24456         }
24457
24458       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
24459         {
24460           /* These options are expected to have an argument.  */
24461           if (c == lopt->option[0]
24462               && arg != NULL
24463               && strncmp (arg, lopt->option + 1,
24464                           strlen (lopt->option + 1)) == 0)
24465             {
24466               /* If the option is deprecated, tell the user.  */
24467               if (warn_on_deprecated && lopt->deprecated != NULL)
24468                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
24469                            _(lopt->deprecated));
24470
24471               /* Call the sup-option parser.  */
24472               return lopt->func (arg + strlen (lopt->option) - 1);
24473             }
24474         }
24475
24476       return 0;
24477     }
24478
24479   return 1;
24480 }
24481
24482 void
24483 md_show_usage (FILE * fp)
24484 {
24485   struct arm_option_table *opt;
24486   struct arm_long_option_table *lopt;
24487
24488   fprintf (fp, _(" ARM-specific assembler options:\n"));
24489
24490   for (opt = arm_opts; opt->option != NULL; opt++)
24491     if (opt->help != NULL)
24492       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
24493
24494   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
24495     if (lopt->help != NULL)
24496       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
24497
24498 #ifdef OPTION_EB
24499   fprintf (fp, _("\
24500   -EB                     assemble code for a big-endian cpu\n"));
24501 #endif
24502
24503 #ifdef OPTION_EL
24504   fprintf (fp, _("\
24505   -EL                     assemble code for a little-endian cpu\n"));
24506 #endif
24507
24508   fprintf (fp, _("\
24509   --fix-v4bx              Allow BX in ARMv4 code\n"));
24510 }
24511
24512
24513 #ifdef OBJ_ELF
24514 typedef struct
24515 {
24516   int val;
24517   arm_feature_set flags;
24518 } cpu_arch_ver_table;
24519
24520 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
24521    least features first.  */
24522 static const cpu_arch_ver_table cpu_arch_ver[] =
24523 {
24524     {1, ARM_ARCH_V4},
24525     {2, ARM_ARCH_V4T},
24526     {3, ARM_ARCH_V5},
24527     {3, ARM_ARCH_V5T},
24528     {4, ARM_ARCH_V5TE},
24529     {5, ARM_ARCH_V5TEJ},
24530     {6, ARM_ARCH_V6},
24531     {9, ARM_ARCH_V6K},
24532     {7, ARM_ARCH_V6Z},
24533     {11, ARM_ARCH_V6M},
24534     {12, ARM_ARCH_V6SM},
24535     {8, ARM_ARCH_V6T2},
24536     {10, ARM_ARCH_V7A_IDIV_MP_SEC_VIRT},
24537     {10, ARM_ARCH_V7R},
24538     {10, ARM_ARCH_V7M},
24539     {14, ARM_ARCH_V8A},
24540     {0, ARM_ARCH_NONE}
24541 };
24542
24543 /* Set an attribute if it has not already been set by the user.  */
24544 static void
24545 aeabi_set_attribute_int (int tag, int value)
24546 {
24547   if (tag < 1
24548       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
24549       || !attributes_set_explicitly[tag])
24550     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
24551 }
24552
24553 static void
24554 aeabi_set_attribute_string (int tag, const char *value)
24555 {
24556   if (tag < 1
24557       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
24558       || !attributes_set_explicitly[tag])
24559     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
24560 }
24561
24562 /* Set the public EABI object attributes.  */
24563 static void
24564 aeabi_set_public_attributes (void)
24565 {
24566   int arch;
24567   char profile;
24568   int virt_sec = 0;
24569   int fp16_optional = 0;
24570   arm_feature_set flags;
24571   arm_feature_set tmp;
24572   const cpu_arch_ver_table *p;
24573
24574   /* Choose the architecture based on the capabilities of the requested cpu
24575      (if any) and/or the instructions actually used.  */
24576   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
24577   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
24578   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
24579
24580   if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
24581     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
24582
24583   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
24584     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
24585
24586   /* Allow the user to override the reported architecture.  */
24587   if (object_arch)
24588     {
24589       ARM_CLEAR_FEATURE (flags, flags, arm_arch_any);
24590       ARM_MERGE_FEATURE_SETS (flags, flags, *object_arch);
24591     }
24592
24593   /* We need to make sure that the attributes do not identify us as v6S-M
24594      when the only v6S-M feature in use is the Operating System Extensions.  */
24595   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_os))
24596       if (!ARM_CPU_HAS_FEATURE (flags, arm_arch_v6m_only))
24597         ARM_CLEAR_FEATURE (flags, flags, arm_ext_os);
24598
24599   tmp = flags;
24600   arch = 0;
24601   for (p = cpu_arch_ver; p->val; p++)
24602     {
24603       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
24604         {
24605           arch = p->val;
24606           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
24607         }
24608     }
24609
24610   /* The table lookup above finds the last architecture to contribute
24611      a new feature.  Unfortunately, Tag13 is a subset of the union of
24612      v6T2 and v7-M, so it is never seen as contributing a new feature.
24613      We can not search for the last entry which is entirely used,
24614      because if no CPU is specified we build up only those flags
24615      actually used.  Perhaps we should separate out the specified
24616      and implicit cases.  Avoid taking this path for -march=all by
24617      checking for contradictory v7-A / v7-M features.  */
24618   if (arch == 10
24619       && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
24620       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m)
24621       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v6_dsp))
24622     arch = 13;
24623
24624   /* Tag_CPU_name.  */
24625   if (selected_cpu_name[0])
24626     {
24627       char *q;
24628
24629       q = selected_cpu_name;
24630       if (strncmp (q, "armv", 4) == 0)
24631         {
24632           int i;
24633
24634           q += 4;
24635           for (i = 0; q[i]; i++)
24636             q[i] = TOUPPER (q[i]);
24637         }
24638       aeabi_set_attribute_string (Tag_CPU_name, q);
24639     }
24640
24641   /* Tag_CPU_arch.  */
24642   aeabi_set_attribute_int (Tag_CPU_arch, arch);
24643
24644   /* Tag_CPU_arch_profile.  */
24645   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a))
24646     profile = 'A';
24647   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
24648     profile = 'R';
24649   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_m))
24650     profile = 'M';
24651   else
24652     profile = '\0';
24653
24654   if (profile != '\0')
24655     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
24656
24657   /* Tag_ARM_ISA_use.  */
24658   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
24659       || arch == 0)
24660     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
24661
24662   /* Tag_THUMB_ISA_use.  */
24663   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
24664       || arch == 0)
24665     aeabi_set_attribute_int (Tag_THUMB_ISA_use,
24666         ARM_CPU_HAS_FEATURE (flags, arm_arch_t2) ? 2 : 1);
24667
24668   /* Tag_VFP_arch.  */
24669   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8))
24670     aeabi_set_attribute_int (Tag_VFP_arch, 7);
24671   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
24672     aeabi_set_attribute_int (Tag_VFP_arch,
24673                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
24674                              ? 5 : 6);
24675   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
24676     {
24677       fp16_optional = 1;
24678       aeabi_set_attribute_int (Tag_VFP_arch, 3);
24679     }
24680   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
24681     {
24682       aeabi_set_attribute_int (Tag_VFP_arch, 4);
24683       fp16_optional = 1;
24684     }
24685   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
24686     aeabi_set_attribute_int (Tag_VFP_arch, 2);
24687   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
24688            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
24689     aeabi_set_attribute_int (Tag_VFP_arch, 1);
24690
24691   /* Tag_ABI_HardFP_use.  */
24692   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
24693       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
24694     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
24695
24696   /* Tag_WMMX_arch.  */
24697   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
24698     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
24699   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
24700     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
24701
24702   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
24703   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
24704     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
24705   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
24706     {
24707       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
24708         {
24709           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
24710         }
24711       else
24712         {
24713           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
24714           fp16_optional = 1;
24715         }
24716     }
24717
24718   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
24719   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
24720     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
24721
24722   /* Tag_DIV_use.
24723
24724      We set Tag_DIV_use to two when integer divide instructions have been used
24725      in ARM state, or when Thumb integer divide instructions have been used,
24726      but we have no architecture profile set, nor have we any ARM instructions.
24727
24728      For ARMv8 we set the tag to 0 as integer divide is implied by the base
24729      architecture.
24730
24731      For new architectures we will have to check these tests.  */
24732   gas_assert (arch <= TAG_CPU_ARCH_V8);
24733   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8))
24734     aeabi_set_attribute_int (Tag_DIV_use, 0);
24735   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
24736            || (profile == '\0'
24737                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
24738                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
24739     aeabi_set_attribute_int (Tag_DIV_use, 2);
24740
24741   /* Tag_MP_extension_use.  */
24742   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
24743     aeabi_set_attribute_int (Tag_MPextension_use, 1);
24744
24745   /* Tag Virtualization_use.  */
24746   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
24747     virt_sec |= 1;
24748   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
24749     virt_sec |= 2;
24750   if (virt_sec != 0)
24751     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
24752 }
24753
24754 /* Add the default contents for the .ARM.attributes section.  */
24755 void
24756 arm_md_end (void)
24757 {
24758   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
24759     return;
24760
24761   aeabi_set_public_attributes ();
24762 }
24763 #endif /* OBJ_ELF */
24764
24765
24766 /* Parse a .cpu directive.  */
24767
24768 static void
24769 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
24770 {
24771   const struct arm_cpu_option_table *opt;
24772   char *name;
24773   char saved_char;
24774
24775   name = input_line_pointer;
24776   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
24777     input_line_pointer++;
24778   saved_char = *input_line_pointer;
24779   *input_line_pointer = 0;
24780
24781   /* Skip the first "all" entry.  */
24782   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
24783     if (streq (opt->name, name))
24784       {
24785         mcpu_cpu_opt = &opt->value;
24786         selected_cpu = opt->value;
24787         if (opt->canonical_name)
24788           strcpy (selected_cpu_name, opt->canonical_name);
24789         else
24790           {
24791             int i;
24792             for (i = 0; opt->name[i]; i++)
24793               selected_cpu_name[i] = TOUPPER (opt->name[i]);
24794
24795             selected_cpu_name[i] = 0;
24796           }
24797         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
24798         *input_line_pointer = saved_char;
24799         demand_empty_rest_of_line ();
24800         return;
24801       }
24802   as_bad (_("unknown cpu `%s'"), name);
24803   *input_line_pointer = saved_char;
24804   ignore_rest_of_line ();
24805 }
24806
24807
24808 /* Parse a .arch directive.  */
24809
24810 static void
24811 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
24812 {
24813   const struct arm_arch_option_table *opt;
24814   char saved_char;
24815   char *name;
24816
24817   name = input_line_pointer;
24818   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
24819     input_line_pointer++;
24820   saved_char = *input_line_pointer;
24821   *input_line_pointer = 0;
24822
24823   /* Skip the first "all" entry.  */
24824   for (opt = arm_archs + 1; opt->name != NULL; opt++)
24825     if (streq (opt->name, name))
24826       {
24827         mcpu_cpu_opt = &opt->value;
24828         selected_cpu = opt->value;
24829         strcpy (selected_cpu_name, opt->name);
24830         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
24831         *input_line_pointer = saved_char;
24832         demand_empty_rest_of_line ();
24833         return;
24834       }
24835
24836   as_bad (_("unknown architecture `%s'\n"), name);
24837   *input_line_pointer = saved_char;
24838   ignore_rest_of_line ();
24839 }
24840
24841
24842 /* Parse a .object_arch directive.  */
24843
24844 static void
24845 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
24846 {
24847   const struct arm_arch_option_table *opt;
24848   char saved_char;
24849   char *name;
24850
24851   name = input_line_pointer;
24852   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
24853     input_line_pointer++;
24854   saved_char = *input_line_pointer;
24855   *input_line_pointer = 0;
24856
24857   /* Skip the first "all" entry.  */
24858   for (opt = arm_archs + 1; opt->name != NULL; opt++)
24859     if (streq (opt->name, name))
24860       {
24861         object_arch = &opt->value;
24862         *input_line_pointer = saved_char;
24863         demand_empty_rest_of_line ();
24864         return;
24865       }
24866
24867   as_bad (_("unknown architecture `%s'\n"), name);
24868   *input_line_pointer = saved_char;
24869   ignore_rest_of_line ();
24870 }
24871
24872 /* Parse a .arch_extension directive.  */
24873
24874 static void
24875 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
24876 {
24877   const struct arm_option_extension_value_table *opt;
24878   char saved_char;
24879   char *name;
24880   int adding_value = 1;
24881
24882   name = input_line_pointer;
24883   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
24884     input_line_pointer++;
24885   saved_char = *input_line_pointer;
24886   *input_line_pointer = 0;
24887
24888   if (strlen (name) >= 2
24889       && strncmp (name, "no", 2) == 0)
24890     {
24891       adding_value = 0;
24892       name += 2;
24893     }
24894
24895   for (opt = arm_extensions; opt->name != NULL; opt++)
24896     if (streq (opt->name, name))
24897       {
24898         if (!ARM_CPU_HAS_FEATURE (*mcpu_cpu_opt, opt->allowed_archs))
24899           {
24900             as_bad (_("architectural extension `%s' is not allowed for the "
24901                       "current base architecture"), name);
24902             break;
24903           }
24904
24905         if (adding_value)
24906           ARM_MERGE_FEATURE_SETS (selected_cpu, selected_cpu, opt->value);
24907         else
24908           ARM_CLEAR_FEATURE (selected_cpu, selected_cpu, opt->value);
24909
24910         mcpu_cpu_opt = &selected_cpu;
24911         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
24912         *input_line_pointer = saved_char;
24913         demand_empty_rest_of_line ();
24914         return;
24915       }
24916
24917   if (opt->name == NULL)
24918     as_bad (_("unknown architecture `%s'\n"), name);
24919
24920   *input_line_pointer = saved_char;
24921   ignore_rest_of_line ();
24922 }
24923
24924 /* Parse a .fpu directive.  */
24925
24926 static void
24927 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
24928 {
24929   const struct arm_option_fpu_value_table *opt;
24930   char saved_char;
24931   char *name;
24932
24933   name = input_line_pointer;
24934   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
24935     input_line_pointer++;
24936   saved_char = *input_line_pointer;
24937   *input_line_pointer = 0;
24938
24939   for (opt = arm_fpus; opt->name != NULL; opt++)
24940     if (streq (opt->name, name))
24941       {
24942         mfpu_opt = &opt->value;
24943         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
24944         *input_line_pointer = saved_char;
24945         demand_empty_rest_of_line ();
24946         return;
24947       }
24948
24949   as_bad (_("unknown floating point format `%s'\n"), name);
24950   *input_line_pointer = saved_char;
24951   ignore_rest_of_line ();
24952 }
24953
24954 /* Copy symbol information.  */
24955
24956 void
24957 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
24958 {
24959   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
24960 }
24961
24962 #ifdef OBJ_ELF
24963 /* Given a symbolic attribute NAME, return the proper integer value.
24964    Returns -1 if the attribute is not known.  */
24965
24966 int
24967 arm_convert_symbolic_attribute (const char *name)
24968 {
24969   static const struct
24970   {
24971     const char * name;
24972     const int    tag;
24973   }
24974   attribute_table[] =
24975     {
24976       /* When you modify this table you should
24977          also modify the list in doc/c-arm.texi.  */
24978 #define T(tag) {#tag, tag}
24979       T (Tag_CPU_raw_name),
24980       T (Tag_CPU_name),
24981       T (Tag_CPU_arch),
24982       T (Tag_CPU_arch_profile),
24983       T (Tag_ARM_ISA_use),
24984       T (Tag_THUMB_ISA_use),
24985       T (Tag_FP_arch),
24986       T (Tag_VFP_arch),
24987       T (Tag_WMMX_arch),
24988       T (Tag_Advanced_SIMD_arch),
24989       T (Tag_PCS_config),
24990       T (Tag_ABI_PCS_R9_use),
24991       T (Tag_ABI_PCS_RW_data),
24992       T (Tag_ABI_PCS_RO_data),
24993       T (Tag_ABI_PCS_GOT_use),
24994       T (Tag_ABI_PCS_wchar_t),
24995       T (Tag_ABI_FP_rounding),
24996       T (Tag_ABI_FP_denormal),
24997       T (Tag_ABI_FP_exceptions),
24998       T (Tag_ABI_FP_user_exceptions),
24999       T (Tag_ABI_FP_number_model),
25000       T (Tag_ABI_align_needed),
25001       T (Tag_ABI_align8_needed),
25002       T (Tag_ABI_align_preserved),
25003       T (Tag_ABI_align8_preserved),
25004       T (Tag_ABI_enum_size),
25005       T (Tag_ABI_HardFP_use),
25006       T (Tag_ABI_VFP_args),
25007       T (Tag_ABI_WMMX_args),
25008       T (Tag_ABI_optimization_goals),
25009       T (Tag_ABI_FP_optimization_goals),
25010       T (Tag_compatibility),
25011       T (Tag_CPU_unaligned_access),
25012       T (Tag_FP_HP_extension),
25013       T (Tag_VFP_HP_extension),
25014       T (Tag_ABI_FP_16bit_format),
25015       T (Tag_MPextension_use),
25016       T (Tag_DIV_use),
25017       T (Tag_nodefaults),
25018       T (Tag_also_compatible_with),
25019       T (Tag_conformance),
25020       T (Tag_T2EE_use),
25021       T (Tag_Virtualization_use),
25022       /* We deliberately do not include Tag_MPextension_use_legacy.  */
25023 #undef T
25024     };
25025   unsigned int i;
25026
25027   if (name == NULL)
25028     return -1;
25029
25030   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
25031     if (streq (name, attribute_table[i].name))
25032       return attribute_table[i].tag;
25033
25034   return -1;
25035 }
25036
25037
25038 /* Apply sym value for relocations only in the case that
25039    they are for local symbols and you have the respective
25040    architectural feature for blx and simple switches.  */
25041 int
25042 arm_apply_sym_value (struct fix * fixP)
25043 {
25044   if (fixP->fx_addsy
25045       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
25046       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
25047     {
25048       switch (fixP->fx_r_type)
25049         {
25050         case BFD_RELOC_ARM_PCREL_BLX:
25051         case BFD_RELOC_THUMB_PCREL_BRANCH23:
25052           if (ARM_IS_FUNC (fixP->fx_addsy))
25053             return 1;
25054           break;
25055
25056         case BFD_RELOC_ARM_PCREL_CALL:
25057         case BFD_RELOC_THUMB_PCREL_BLX:
25058           if (THUMB_IS_FUNC (fixP->fx_addsy))
25059               return 1;
25060           break;
25061
25062         default:
25063           break;
25064         }
25065
25066     }
25067   return 0;
25068 }
25069 #endif /* OBJ_ELF */