* config/tc-arm.c (only_one_reg_in_list): New function.
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4    Free Software Foundation, Inc.
5    Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
6         Modified by David Taylor (dtaylor@armltd.co.uk)
7         Cirrus coprocessor mods by Aldy Hernandez (aldyh@redhat.com)
8         Cirrus coprocessor fixes by Petko Manolov (petkan@nucleusys.com)
9         Cirrus coprocessor fixes by Vladimir Ivanov (vladitx@nucleusys.com)
10
11    This file is part of GAS, the GNU Assembler.
12
13    GAS is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 3, or (at your option)
16    any later version.
17
18    GAS is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with GAS; see the file COPYING.  If not, write to the Free
25    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
26    02110-1301, USA.  */
27
28 #include "as.h"
29 #include <limits.h>
30 #include <stdarg.h>
31 #define  NO_RELOC 0
32 #include "safe-ctype.h"
33 #include "subsegs.h"
34 #include "obstack.h"
35 #include "libiberty.h"
36 #include "opcode/arm.h"
37
38 #ifdef OBJ_ELF
39 #include "elf/arm.h"
40 #include "dw2gencfi.h"
41 #endif
42
43 #include "dwarf2dbg.h"
44
45 #ifdef OBJ_ELF
46 /* Must be at least the size of the largest unwind opcode (currently two).  */
47 #define ARM_OPCODE_CHUNK_SIZE 8
48
49 /* This structure holds the unwinding state.  */
50
51 static struct
52 {
53   symbolS *       proc_start;
54   symbolS *       table_entry;
55   symbolS *       personality_routine;
56   int             personality_index;
57   /* The segment containing the function.  */
58   segT            saved_seg;
59   subsegT         saved_subseg;
60   /* Opcodes generated from this function.  */
61   unsigned char * opcodes;
62   int             opcode_count;
63   int             opcode_alloc;
64   /* The number of bytes pushed to the stack.  */
65   offsetT         frame_size;
66   /* We don't add stack adjustment opcodes immediately so that we can merge
67      multiple adjustments.  We can also omit the final adjustment
68      when using a frame pointer.  */
69   offsetT         pending_offset;
70   /* These two fields are set by both unwind_movsp and unwind_setfp.  They
71      hold the reg+offset to use when restoring sp from a frame pointer.  */
72   offsetT         fp_offset;
73   int             fp_reg;
74   /* Nonzero if an unwind_setfp directive has been seen.  */
75   unsigned        fp_used:1;
76   /* Nonzero if the last opcode restores sp from fp_reg.  */
77   unsigned        sp_restored:1;
78 } unwind;
79
80 #endif /* OBJ_ELF */
81
82 /* Results from operand parsing worker functions.  */
83
84 typedef enum
85 {
86   PARSE_OPERAND_SUCCESS,
87   PARSE_OPERAND_FAIL,
88   PARSE_OPERAND_FAIL_NO_BACKTRACK
89 } parse_operand_result;
90
91 enum arm_float_abi
92 {
93   ARM_FLOAT_ABI_HARD,
94   ARM_FLOAT_ABI_SOFTFP,
95   ARM_FLOAT_ABI_SOFT
96 };
97
98 /* Types of processor to assemble for.  */
99 #ifndef CPU_DEFAULT
100 /* The code that was here used to select a default CPU depending on compiler
101    pre-defines which were only present when doing native builds, thus
102    changing gas' default behaviour depending upon the build host.
103
104    If you have a target that requires a default CPU option then the you
105    should define CPU_DEFAULT here.  */
106 #endif
107
108 #ifndef FPU_DEFAULT
109 # ifdef TE_LINUX
110 #  define FPU_DEFAULT FPU_ARCH_FPA
111 # elif defined (TE_NetBSD)
112 #  ifdef OBJ_ELF
113 #   define FPU_DEFAULT FPU_ARCH_VFP     /* Soft-float, but VFP order.  */
114 #  else
115     /* Legacy a.out format.  */
116 #   define FPU_DEFAULT FPU_ARCH_FPA     /* Soft-float, but FPA order.  */
117 #  endif
118 # elif defined (TE_VXWORKS)
119 #  define FPU_DEFAULT FPU_ARCH_VFP      /* Soft-float, VFP order.  */
120 # else
121    /* For backwards compatibility, default to FPA.  */
122 #  define FPU_DEFAULT FPU_ARCH_FPA
123 # endif
124 #endif /* ifndef FPU_DEFAULT */
125
126 #define streq(a, b)           (strcmp (a, b) == 0)
127
128 static arm_feature_set cpu_variant;
129 static arm_feature_set arm_arch_used;
130 static arm_feature_set thumb_arch_used;
131
132 /* Flags stored in private area of BFD structure.  */
133 static int uses_apcs_26      = FALSE;
134 static int atpcs             = FALSE;
135 static int support_interwork = FALSE;
136 static int uses_apcs_float   = FALSE;
137 static int pic_code          = FALSE;
138 static int fix_v4bx          = FALSE;
139 /* Warn on using deprecated features.  */
140 static int warn_on_deprecated = TRUE;
141
142
143 /* Variables that we set while parsing command-line options.  Once all
144    options have been read we re-process these values to set the real
145    assembly flags.  */
146 static const arm_feature_set *legacy_cpu = NULL;
147 static const arm_feature_set *legacy_fpu = NULL;
148
149 static const arm_feature_set *mcpu_cpu_opt = NULL;
150 static const arm_feature_set *mcpu_fpu_opt = NULL;
151 static const arm_feature_set *march_cpu_opt = NULL;
152 static const arm_feature_set *march_fpu_opt = NULL;
153 static const arm_feature_set *mfpu_opt = NULL;
154 static const arm_feature_set *object_arch = NULL;
155
156 /* Constants for known architecture features.  */
157 static const arm_feature_set fpu_default = FPU_DEFAULT;
158 static const arm_feature_set fpu_arch_vfp_v1 = FPU_ARCH_VFP_V1;
159 static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
160 static const arm_feature_set fpu_arch_vfp_v3 = FPU_ARCH_VFP_V3;
161 static const arm_feature_set fpu_arch_neon_v1 = FPU_ARCH_NEON_V1;
162 static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
163 static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
164 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
165 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
166
167 #ifdef CPU_DEFAULT
168 static const arm_feature_set cpu_default = CPU_DEFAULT;
169 #endif
170
171 static const arm_feature_set arm_ext_v1 = ARM_FEATURE (ARM_EXT_V1, 0);
172 static const arm_feature_set arm_ext_v2 = ARM_FEATURE (ARM_EXT_V1, 0);
173 static const arm_feature_set arm_ext_v2s = ARM_FEATURE (ARM_EXT_V2S, 0);
174 static const arm_feature_set arm_ext_v3 = ARM_FEATURE (ARM_EXT_V3, 0);
175 static const arm_feature_set arm_ext_v3m = ARM_FEATURE (ARM_EXT_V3M, 0);
176 static const arm_feature_set arm_ext_v4 = ARM_FEATURE (ARM_EXT_V4, 0);
177 static const arm_feature_set arm_ext_v4t = ARM_FEATURE (ARM_EXT_V4T, 0);
178 static const arm_feature_set arm_ext_v5 = ARM_FEATURE (ARM_EXT_V5, 0);
179 static const arm_feature_set arm_ext_v4t_5 =
180   ARM_FEATURE (ARM_EXT_V4T | ARM_EXT_V5, 0);
181 static const arm_feature_set arm_ext_v5t = ARM_FEATURE (ARM_EXT_V5T, 0);
182 static const arm_feature_set arm_ext_v5e = ARM_FEATURE (ARM_EXT_V5E, 0);
183 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE (ARM_EXT_V5ExP, 0);
184 static const arm_feature_set arm_ext_v5j = ARM_FEATURE (ARM_EXT_V5J, 0);
185 static const arm_feature_set arm_ext_v6 = ARM_FEATURE (ARM_EXT_V6, 0);
186 static const arm_feature_set arm_ext_v6k = ARM_FEATURE (ARM_EXT_V6K, 0);
187 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE (ARM_EXT_V6T2, 0);
188 static const arm_feature_set arm_ext_v6m = ARM_FEATURE (ARM_EXT_V6M, 0);
189 static const arm_feature_set arm_ext_v6_notm = ARM_FEATURE (ARM_EXT_V6_NOTM, 0);
190 static const arm_feature_set arm_ext_v6_dsp = ARM_FEATURE (ARM_EXT_V6_DSP, 0);
191 static const arm_feature_set arm_ext_barrier = ARM_FEATURE (ARM_EXT_BARRIER, 0);
192 static const arm_feature_set arm_ext_msr = ARM_FEATURE (ARM_EXT_THUMB_MSR, 0);
193 static const arm_feature_set arm_ext_div = ARM_FEATURE (ARM_EXT_DIV, 0);
194 static const arm_feature_set arm_ext_v7 = ARM_FEATURE (ARM_EXT_V7, 0);
195 static const arm_feature_set arm_ext_v7a = ARM_FEATURE (ARM_EXT_V7A, 0);
196 static const arm_feature_set arm_ext_v7r = ARM_FEATURE (ARM_EXT_V7R, 0);
197 static const arm_feature_set arm_ext_v7m = ARM_FEATURE (ARM_EXT_V7M, 0);
198 static const arm_feature_set arm_ext_m =
199   ARM_FEATURE (ARM_EXT_V6M | ARM_EXT_OS | ARM_EXT_V7M, 0);
200 static const arm_feature_set arm_ext_mp = ARM_FEATURE (ARM_EXT_MP, 0);
201 static const arm_feature_set arm_ext_sec = ARM_FEATURE (ARM_EXT_SEC, 0);
202 static const arm_feature_set arm_ext_os = ARM_FEATURE (ARM_EXT_OS, 0);
203 static const arm_feature_set arm_ext_adiv = ARM_FEATURE (ARM_EXT_ADIV, 0);
204 static const arm_feature_set arm_ext_virt = ARM_FEATURE (ARM_EXT_VIRT, 0);
205
206 static const arm_feature_set arm_arch_any = ARM_ANY;
207 static const arm_feature_set arm_arch_full = ARM_FEATURE (-1, -1);
208 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
209 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
210 static const arm_feature_set arm_arch_v6m_only = ARM_ARCH_V6M_ONLY;
211
212 static const arm_feature_set arm_cext_iwmmxt2 =
213   ARM_FEATURE (0, ARM_CEXT_IWMMXT2);
214 static const arm_feature_set arm_cext_iwmmxt =
215   ARM_FEATURE (0, ARM_CEXT_IWMMXT);
216 static const arm_feature_set arm_cext_xscale =
217   ARM_FEATURE (0, ARM_CEXT_XSCALE);
218 static const arm_feature_set arm_cext_maverick =
219   ARM_FEATURE (0, ARM_CEXT_MAVERICK);
220 static const arm_feature_set fpu_fpa_ext_v1 = ARM_FEATURE (0, FPU_FPA_EXT_V1);
221 static const arm_feature_set fpu_fpa_ext_v2 = ARM_FEATURE (0, FPU_FPA_EXT_V2);
222 static const arm_feature_set fpu_vfp_ext_v1xd =
223   ARM_FEATURE (0, FPU_VFP_EXT_V1xD);
224 static const arm_feature_set fpu_vfp_ext_v1 = ARM_FEATURE (0, FPU_VFP_EXT_V1);
225 static const arm_feature_set fpu_vfp_ext_v2 = ARM_FEATURE (0, FPU_VFP_EXT_V2);
226 static const arm_feature_set fpu_vfp_ext_v3xd = ARM_FEATURE (0, FPU_VFP_EXT_V3xD);
227 static const arm_feature_set fpu_vfp_ext_v3 = ARM_FEATURE (0, FPU_VFP_EXT_V3);
228 static const arm_feature_set fpu_vfp_ext_d32 =
229   ARM_FEATURE (0, FPU_VFP_EXT_D32);
230 static const arm_feature_set fpu_neon_ext_v1 = ARM_FEATURE (0, FPU_NEON_EXT_V1);
231 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
232   ARM_FEATURE (0, FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
233 static const arm_feature_set fpu_vfp_fp16 = ARM_FEATURE (0, FPU_VFP_EXT_FP16);
234 static const arm_feature_set fpu_neon_ext_fma = ARM_FEATURE (0, FPU_NEON_EXT_FMA);
235 static const arm_feature_set fpu_vfp_ext_fma = ARM_FEATURE (0, FPU_VFP_EXT_FMA);
236
237 static int mfloat_abi_opt = -1;
238 /* Record user cpu selection for object attributes.  */
239 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
240 /* Must be long enough to hold any of the names in arm_cpus.  */
241 static char selected_cpu_name[16];
242
243 /* Return if no cpu was selected on command-line.  */
244 static bfd_boolean
245 no_cpu_selected (void)
246 {
247   return selected_cpu.core == arm_arch_none.core
248     && selected_cpu.coproc == arm_arch_none.coproc;
249 }
250
251 #ifdef OBJ_ELF
252 # ifdef EABI_DEFAULT
253 static int meabi_flags = EABI_DEFAULT;
254 # else
255 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
256 # endif
257
258 static int attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
259
260 bfd_boolean
261 arm_is_eabi (void)
262 {
263   return (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4);
264 }
265 #endif
266
267 #ifdef OBJ_ELF
268 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
269 symbolS * GOT_symbol;
270 #endif
271
272 /* 0: assemble for ARM,
273    1: assemble for Thumb,
274    2: assemble for Thumb even though target CPU does not support thumb
275       instructions.  */
276 static int thumb_mode = 0;
277 /* A value distinct from the possible values for thumb_mode that we
278    can use to record whether thumb_mode has been copied into the
279    tc_frag_data field of a frag.  */
280 #define MODE_RECORDED (1 << 4)
281
282 /* Specifies the intrinsic IT insn behavior mode.  */
283 enum implicit_it_mode
284 {
285   IMPLICIT_IT_MODE_NEVER  = 0x00,
286   IMPLICIT_IT_MODE_ARM    = 0x01,
287   IMPLICIT_IT_MODE_THUMB  = 0x02,
288   IMPLICIT_IT_MODE_ALWAYS = (IMPLICIT_IT_MODE_ARM | IMPLICIT_IT_MODE_THUMB)
289 };
290 static int implicit_it_mode = IMPLICIT_IT_MODE_ARM;
291
292 /* If unified_syntax is true, we are processing the new unified
293    ARM/Thumb syntax.  Important differences from the old ARM mode:
294
295      - Immediate operands do not require a # prefix.
296      - Conditional affixes always appear at the end of the
297        instruction.  (For backward compatibility, those instructions
298        that formerly had them in the middle, continue to accept them
299        there.)
300      - The IT instruction may appear, and if it does is validated
301        against subsequent conditional affixes.  It does not generate
302        machine code.
303
304    Important differences from the old Thumb mode:
305
306      - Immediate operands do not require a # prefix.
307      - Most of the V6T2 instructions are only available in unified mode.
308      - The .N and .W suffixes are recognized and honored (it is an error
309        if they cannot be honored).
310      - All instructions set the flags if and only if they have an 's' affix.
311      - Conditional affixes may be used.  They are validated against
312        preceding IT instructions.  Unlike ARM mode, you cannot use a
313        conditional affix except in the scope of an IT instruction.  */
314
315 static bfd_boolean unified_syntax = FALSE;
316
317 enum neon_el_type
318 {
319   NT_invtype,
320   NT_untyped,
321   NT_integer,
322   NT_float,
323   NT_poly,
324   NT_signed,
325   NT_unsigned
326 };
327
328 struct neon_type_el
329 {
330   enum neon_el_type type;
331   unsigned size;
332 };
333
334 #define NEON_MAX_TYPE_ELS 4
335
336 struct neon_type
337 {
338   struct neon_type_el el[NEON_MAX_TYPE_ELS];
339   unsigned elems;
340 };
341
342 enum it_instruction_type
343 {
344    OUTSIDE_IT_INSN,
345    INSIDE_IT_INSN,
346    INSIDE_IT_LAST_INSN,
347    IF_INSIDE_IT_LAST_INSN, /* Either outside or inside;
348                               if inside, should be the last one.  */
349    NEUTRAL_IT_INSN,        /* This could be either inside or outside,
350                               i.e. BKPT and NOP.  */
351    IT_INSN                 /* The IT insn has been parsed.  */
352 };
353
354 /* The maximum number of operands we need.  */
355 #define ARM_IT_MAX_OPERANDS 6
356
357 struct arm_it
358 {
359   const char *  error;
360   unsigned long instruction;
361   int           size;
362   int           size_req;
363   int           cond;
364   /* "uncond_value" is set to the value in place of the conditional field in
365      unconditional versions of the instruction, or -1 if nothing is
366      appropriate.  */
367   int           uncond_value;
368   struct neon_type vectype;
369   /* This does not indicate an actual NEON instruction, only that
370      the mnemonic accepts neon-style type suffixes.  */
371   int           is_neon;
372   /* Set to the opcode if the instruction needs relaxation.
373      Zero if the instruction is not relaxed.  */
374   unsigned long relax;
375   struct
376   {
377     bfd_reloc_code_real_type type;
378     expressionS              exp;
379     int                      pc_rel;
380   } reloc;
381
382   enum it_instruction_type it_insn_type;
383
384   struct
385   {
386     unsigned reg;
387     signed int imm;
388     struct neon_type_el vectype;
389     unsigned present    : 1;  /* Operand present.  */
390     unsigned isreg      : 1;  /* Operand was a register.  */
391     unsigned immisreg   : 1;  /* .imm field is a second register.  */
392     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
393     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
394     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
395     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
396        instructions. This allows us to disambiguate ARM <-> vector insns.  */
397     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
398     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
399     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
400     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
401     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
402     unsigned writeback  : 1;  /* Operand has trailing !  */
403     unsigned preind     : 1;  /* Preindexed address.  */
404     unsigned postind    : 1;  /* Postindexed address.  */
405     unsigned negative   : 1;  /* Index register was negated.  */
406     unsigned shifted    : 1;  /* Shift applied to operation.  */
407     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
408   } operands[ARM_IT_MAX_OPERANDS];
409 };
410
411 static struct arm_it inst;
412
413 #define NUM_FLOAT_VALS 8
414
415 const char * fp_const[] =
416 {
417   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
418 };
419
420 /* Number of littlenums required to hold an extended precision number.  */
421 #define MAX_LITTLENUMS 6
422
423 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
424
425 #define FAIL    (-1)
426 #define SUCCESS (0)
427
428 #define SUFF_S 1
429 #define SUFF_D 2
430 #define SUFF_E 3
431 #define SUFF_P 4
432
433 #define CP_T_X   0x00008000
434 #define CP_T_Y   0x00400000
435
436 #define CONDS_BIT        0x00100000
437 #define LOAD_BIT         0x00100000
438
439 #define DOUBLE_LOAD_FLAG 0x00000001
440
441 struct asm_cond
442 {
443   const char *   template_name;
444   unsigned long  value;
445 };
446
447 #define COND_ALWAYS 0xE
448
449 struct asm_psr
450 {
451   const char *   template_name;
452   unsigned long  field;
453 };
454
455 struct asm_barrier_opt
456 {
457   const char *   template_name;
458   unsigned long  value;
459 };
460
461 /* The bit that distinguishes CPSR and SPSR.  */
462 #define SPSR_BIT   (1 << 22)
463
464 /* The individual PSR flag bits.  */
465 #define PSR_c   (1 << 16)
466 #define PSR_x   (1 << 17)
467 #define PSR_s   (1 << 18)
468 #define PSR_f   (1 << 19)
469
470 struct reloc_entry
471 {
472   char *                    name;
473   bfd_reloc_code_real_type  reloc;
474 };
475
476 enum vfp_reg_pos
477 {
478   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
479   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
480 };
481
482 enum vfp_ldstm_type
483 {
484   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
485 };
486
487 /* Bits for DEFINED field in neon_typed_alias.  */
488 #define NTA_HASTYPE  1
489 #define NTA_HASINDEX 2
490
491 struct neon_typed_alias
492 {
493   unsigned char        defined;
494   unsigned char        index;
495   struct neon_type_el  eltype;
496 };
497
498 /* ARM register categories.  This includes coprocessor numbers and various
499    architecture extensions' registers.  */
500 enum arm_reg_type
501 {
502   REG_TYPE_RN,
503   REG_TYPE_CP,
504   REG_TYPE_CN,
505   REG_TYPE_FN,
506   REG_TYPE_VFS,
507   REG_TYPE_VFD,
508   REG_TYPE_NQ,
509   REG_TYPE_VFSD,
510   REG_TYPE_NDQ,
511   REG_TYPE_NSDQ,
512   REG_TYPE_VFC,
513   REG_TYPE_MVF,
514   REG_TYPE_MVD,
515   REG_TYPE_MVFX,
516   REG_TYPE_MVDX,
517   REG_TYPE_MVAX,
518   REG_TYPE_DSPSC,
519   REG_TYPE_MMXWR,
520   REG_TYPE_MMXWC,
521   REG_TYPE_MMXWCG,
522   REG_TYPE_XSCALE,
523   REG_TYPE_RNB
524 };
525
526 /* Structure for a hash table entry for a register.
527    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
528    information which states whether a vector type or index is specified (for a
529    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
530 struct reg_entry
531 {
532   const char *               name;
533   unsigned int               number;
534   unsigned char              type;
535   unsigned char              builtin;
536   struct neon_typed_alias *  neon;
537 };
538
539 /* Diagnostics used when we don't get a register of the expected type.  */
540 const char * const reg_expected_msgs[] =
541 {
542   N_("ARM register expected"),
543   N_("bad or missing co-processor number"),
544   N_("co-processor register expected"),
545   N_("FPA register expected"),
546   N_("VFP single precision register expected"),
547   N_("VFP/Neon double precision register expected"),
548   N_("Neon quad precision register expected"),
549   N_("VFP single or double precision register expected"),
550   N_("Neon double or quad precision register expected"),
551   N_("VFP single, double or Neon quad precision register expected"),
552   N_("VFP system register expected"),
553   N_("Maverick MVF register expected"),
554   N_("Maverick MVD register expected"),
555   N_("Maverick MVFX register expected"),
556   N_("Maverick MVDX register expected"),
557   N_("Maverick MVAX register expected"),
558   N_("Maverick DSPSC register expected"),
559   N_("iWMMXt data register expected"),
560   N_("iWMMXt control register expected"),
561   N_("iWMMXt scalar register expected"),
562   N_("XScale accumulator register expected"),
563 };
564
565 /* Some well known registers that we refer to directly elsewhere.  */
566 #define REG_R12 12
567 #define REG_SP  13
568 #define REG_LR  14
569 #define REG_PC  15
570
571 /* ARM instructions take 4bytes in the object file, Thumb instructions
572    take 2:  */
573 #define INSN_SIZE       4
574
575 struct asm_opcode
576 {
577   /* Basic string to match.  */
578   const char * template_name;
579
580   /* Parameters to instruction.  */
581   unsigned int operands[8];
582
583   /* Conditional tag - see opcode_lookup.  */
584   unsigned int tag : 4;
585
586   /* Basic instruction code.  */
587   unsigned int avalue : 28;
588
589   /* Thumb-format instruction code.  */
590   unsigned int tvalue;
591
592   /* Which architecture variant provides this instruction.  */
593   const arm_feature_set * avariant;
594   const arm_feature_set * tvariant;
595
596   /* Function to call to encode instruction in ARM format.  */
597   void (* aencode) (void);
598
599   /* Function to call to encode instruction in Thumb format.  */
600   void (* tencode) (void);
601 };
602
603 /* Defines for various bits that we will want to toggle.  */
604 #define INST_IMMEDIATE  0x02000000
605 #define OFFSET_REG      0x02000000
606 #define HWOFFSET_IMM    0x00400000
607 #define SHIFT_BY_REG    0x00000010
608 #define PRE_INDEX       0x01000000
609 #define INDEX_UP        0x00800000
610 #define WRITE_BACK      0x00200000
611 #define LDM_TYPE_2_OR_3 0x00400000
612 #define CPSI_MMOD       0x00020000
613
614 #define LITERAL_MASK    0xf000f000
615 #define OPCODE_MASK     0xfe1fffff
616 #define V4_STR_BIT      0x00000020
617
618 #define T2_SUBS_PC_LR   0xf3de8f00
619
620 #define DATA_OP_SHIFT   21
621
622 #define T2_OPCODE_MASK  0xfe1fffff
623 #define T2_DATA_OP_SHIFT 21
624
625 #define A_COND_MASK         0xf0000000
626 #define A_PUSH_POP_OP_MASK  0x0fff0000
627
628 /* Opcodes for pushing/poping registers to/from the stack.  */
629 #define A1_OPCODE_PUSH    0x092d0000
630 #define A2_OPCODE_PUSH    0x052d0004
631 #define A2_OPCODE_POP     0x049d0004
632
633 /* Codes to distinguish the arithmetic instructions.  */
634 #define OPCODE_AND      0
635 #define OPCODE_EOR      1
636 #define OPCODE_SUB      2
637 #define OPCODE_RSB      3
638 #define OPCODE_ADD      4
639 #define OPCODE_ADC      5
640 #define OPCODE_SBC      6
641 #define OPCODE_RSC      7
642 #define OPCODE_TST      8
643 #define OPCODE_TEQ      9
644 #define OPCODE_CMP      10
645 #define OPCODE_CMN      11
646 #define OPCODE_ORR      12
647 #define OPCODE_MOV      13
648 #define OPCODE_BIC      14
649 #define OPCODE_MVN      15
650
651 #define T2_OPCODE_AND   0
652 #define T2_OPCODE_BIC   1
653 #define T2_OPCODE_ORR   2
654 #define T2_OPCODE_ORN   3
655 #define T2_OPCODE_EOR   4
656 #define T2_OPCODE_ADD   8
657 #define T2_OPCODE_ADC   10
658 #define T2_OPCODE_SBC   11
659 #define T2_OPCODE_SUB   13
660 #define T2_OPCODE_RSB   14
661
662 #define T_OPCODE_MUL 0x4340
663 #define T_OPCODE_TST 0x4200
664 #define T_OPCODE_CMN 0x42c0
665 #define T_OPCODE_NEG 0x4240
666 #define T_OPCODE_MVN 0x43c0
667
668 #define T_OPCODE_ADD_R3 0x1800
669 #define T_OPCODE_SUB_R3 0x1a00
670 #define T_OPCODE_ADD_HI 0x4400
671 #define T_OPCODE_ADD_ST 0xb000
672 #define T_OPCODE_SUB_ST 0xb080
673 #define T_OPCODE_ADD_SP 0xa800
674 #define T_OPCODE_ADD_PC 0xa000
675 #define T_OPCODE_ADD_I8 0x3000
676 #define T_OPCODE_SUB_I8 0x3800
677 #define T_OPCODE_ADD_I3 0x1c00
678 #define T_OPCODE_SUB_I3 0x1e00
679
680 #define T_OPCODE_ASR_R  0x4100
681 #define T_OPCODE_LSL_R  0x4080
682 #define T_OPCODE_LSR_R  0x40c0
683 #define T_OPCODE_ROR_R  0x41c0
684 #define T_OPCODE_ASR_I  0x1000
685 #define T_OPCODE_LSL_I  0x0000
686 #define T_OPCODE_LSR_I  0x0800
687
688 #define T_OPCODE_MOV_I8 0x2000
689 #define T_OPCODE_CMP_I8 0x2800
690 #define T_OPCODE_CMP_LR 0x4280
691 #define T_OPCODE_MOV_HR 0x4600
692 #define T_OPCODE_CMP_HR 0x4500
693
694 #define T_OPCODE_LDR_PC 0x4800
695 #define T_OPCODE_LDR_SP 0x9800
696 #define T_OPCODE_STR_SP 0x9000
697 #define T_OPCODE_LDR_IW 0x6800
698 #define T_OPCODE_STR_IW 0x6000
699 #define T_OPCODE_LDR_IH 0x8800
700 #define T_OPCODE_STR_IH 0x8000
701 #define T_OPCODE_LDR_IB 0x7800
702 #define T_OPCODE_STR_IB 0x7000
703 #define T_OPCODE_LDR_RW 0x5800
704 #define T_OPCODE_STR_RW 0x5000
705 #define T_OPCODE_LDR_RH 0x5a00
706 #define T_OPCODE_STR_RH 0x5200
707 #define T_OPCODE_LDR_RB 0x5c00
708 #define T_OPCODE_STR_RB 0x5400
709
710 #define T_OPCODE_PUSH   0xb400
711 #define T_OPCODE_POP    0xbc00
712
713 #define T_OPCODE_BRANCH 0xe000
714
715 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
716 #define THUMB_PP_PC_LR 0x0100
717 #define THUMB_LOAD_BIT 0x0800
718 #define THUMB2_LOAD_BIT 0x00100000
719
720 #define BAD_ARGS        _("bad arguments to instruction")
721 #define BAD_SP          _("r13 not allowed here")
722 #define BAD_PC          _("r15 not allowed here")
723 #define BAD_COND        _("instruction cannot be conditional")
724 #define BAD_OVERLAP     _("registers may not be the same")
725 #define BAD_HIREG       _("lo register required")
726 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
727 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
728 #define BAD_BRANCH      _("branch must be last instruction in IT block")
729 #define BAD_NOT_IT      _("instruction not allowed in IT block")
730 #define BAD_FPU         _("selected FPU does not support instruction")
731 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
732 #define BAD_IT_COND     _("incorrect condition in IT block")
733 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
734 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
735 #define BAD_PC_ADDRESSING \
736         _("cannot use register index with PC-relative addressing")
737 #define BAD_PC_WRITEBACK \
738         _("cannot use writeback with PC-relative addressing")
739 #define BAD_RANGE     _("branch out of range")
740
741 static struct hash_control * arm_ops_hsh;
742 static struct hash_control * arm_cond_hsh;
743 static struct hash_control * arm_shift_hsh;
744 static struct hash_control * arm_psr_hsh;
745 static struct hash_control * arm_v7m_psr_hsh;
746 static struct hash_control * arm_reg_hsh;
747 static struct hash_control * arm_reloc_hsh;
748 static struct hash_control * arm_barrier_opt_hsh;
749
750 /* Stuff needed to resolve the label ambiguity
751    As:
752      ...
753      label:   <insn>
754    may differ from:
755      ...
756      label:
757               <insn>  */
758
759 symbolS *  last_label_seen;
760 static int label_is_thumb_function_name = FALSE;
761
762 /* Literal pool structure.  Held on a per-section
763    and per-sub-section basis.  */
764
765 #define MAX_LITERAL_POOL_SIZE 1024
766 typedef struct literal_pool
767 {
768   expressionS            literals [MAX_LITERAL_POOL_SIZE];
769   unsigned int           next_free_entry;
770   unsigned int           id;
771   symbolS *              symbol;
772   segT                   section;
773   subsegT                sub_section;
774 #ifdef OBJ_ELF
775   struct dwarf2_line_info locs [MAX_LITERAL_POOL_SIZE];
776 #endif
777   struct literal_pool *  next;
778 } literal_pool;
779
780 /* Pointer to a linked list of literal pools.  */
781 literal_pool * list_of_pools = NULL;
782
783 #ifdef OBJ_ELF
784 #  define now_it seg_info (now_seg)->tc_segment_info_data.current_it
785 #else
786 static struct current_it now_it;
787 #endif
788
789 static inline int
790 now_it_compatible (int cond)
791 {
792   return (cond & ~1) == (now_it.cc & ~1);
793 }
794
795 static inline int
796 conditional_insn (void)
797 {
798   return inst.cond != COND_ALWAYS;
799 }
800
801 static int in_it_block (void);
802
803 static int handle_it_state (void);
804
805 static void force_automatic_it_block_close (void);
806
807 static void it_fsm_post_encode (void);
808
809 #define set_it_insn_type(type)                  \
810   do                                            \
811     {                                           \
812       inst.it_insn_type = type;                 \
813       if (handle_it_state () == FAIL)           \
814         return;                                 \
815     }                                           \
816   while (0)
817
818 #define set_it_insn_type_nonvoid(type, failret) \
819   do                                            \
820     {                                           \
821       inst.it_insn_type = type;                 \
822       if (handle_it_state () == FAIL)           \
823         return failret;                         \
824     }                                           \
825   while(0)
826
827 #define set_it_insn_type_last()                         \
828   do                                                    \
829     {                                                   \
830       if (inst.cond == COND_ALWAYS)                     \
831         set_it_insn_type (IF_INSIDE_IT_LAST_INSN);      \
832       else                                              \
833         set_it_insn_type (INSIDE_IT_LAST_INSN);         \
834     }                                                   \
835   while (0)
836
837 /* Pure syntax.  */
838
839 /* This array holds the chars that always start a comment.  If the
840    pre-processor is disabled, these aren't very useful.  */
841 const char comment_chars[] = "@";
842
843 /* This array holds the chars that only start a comment at the beginning of
844    a line.  If the line seems to have the form '# 123 filename'
845    .line and .file directives will appear in the pre-processed output.  */
846 /* Note that input_file.c hand checks for '#' at the beginning of the
847    first line of the input file.  This is because the compiler outputs
848    #NO_APP at the beginning of its output.  */
849 /* Also note that comments like this one will always work.  */
850 const char line_comment_chars[] = "#";
851
852 const char line_separator_chars[] = ";";
853
854 /* Chars that can be used to separate mant
855    from exp in floating point numbers.  */
856 const char EXP_CHARS[] = "eE";
857
858 /* Chars that mean this number is a floating point constant.  */
859 /* As in 0f12.456  */
860 /* or    0d1.2345e12  */
861
862 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
863
864 /* Prefix characters that indicate the start of an immediate
865    value.  */
866 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
867
868 /* Separator character handling.  */
869
870 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
871
872 static inline int
873 skip_past_char (char ** str, char c)
874 {
875   if (**str == c)
876     {
877       (*str)++;
878       return SUCCESS;
879     }
880   else
881     return FAIL;
882 }
883
884 #define skip_past_comma(str) skip_past_char (str, ',')
885
886 /* Arithmetic expressions (possibly involving symbols).  */
887
888 /* Return TRUE if anything in the expression is a bignum.  */
889
890 static int
891 walk_no_bignums (symbolS * sp)
892 {
893   if (symbol_get_value_expression (sp)->X_op == O_big)
894     return 1;
895
896   if (symbol_get_value_expression (sp)->X_add_symbol)
897     {
898       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
899               || (symbol_get_value_expression (sp)->X_op_symbol
900                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
901     }
902
903   return 0;
904 }
905
906 static int in_my_get_expression = 0;
907
908 /* Third argument to my_get_expression.  */
909 #define GE_NO_PREFIX 0
910 #define GE_IMM_PREFIX 1
911 #define GE_OPT_PREFIX 2
912 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
913    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
914 #define GE_OPT_PREFIX_BIG 3
915
916 static int
917 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
918 {
919   char * save_in;
920   segT   seg;
921
922   /* In unified syntax, all prefixes are optional.  */
923   if (unified_syntax)
924     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
925                   : GE_OPT_PREFIX;
926
927   switch (prefix_mode)
928     {
929     case GE_NO_PREFIX: break;
930     case GE_IMM_PREFIX:
931       if (!is_immediate_prefix (**str))
932         {
933           inst.error = _("immediate expression requires a # prefix");
934           return FAIL;
935         }
936       (*str)++;
937       break;
938     case GE_OPT_PREFIX:
939     case GE_OPT_PREFIX_BIG:
940       if (is_immediate_prefix (**str))
941         (*str)++;
942       break;
943     default: abort ();
944     }
945
946   memset (ep, 0, sizeof (expressionS));
947
948   save_in = input_line_pointer;
949   input_line_pointer = *str;
950   in_my_get_expression = 1;
951   seg = expression (ep);
952   in_my_get_expression = 0;
953
954   if (ep->X_op == O_illegal || ep->X_op == O_absent)
955     {
956       /* We found a bad or missing expression in md_operand().  */
957       *str = input_line_pointer;
958       input_line_pointer = save_in;
959       if (inst.error == NULL)
960         inst.error = (ep->X_op == O_absent
961                       ? _("missing expression") :_("bad expression"));
962       return 1;
963     }
964
965 #ifdef OBJ_AOUT
966   if (seg != absolute_section
967       && seg != text_section
968       && seg != data_section
969       && seg != bss_section
970       && seg != undefined_section)
971     {
972       inst.error = _("bad segment");
973       *str = input_line_pointer;
974       input_line_pointer = save_in;
975       return 1;
976     }
977 #else
978   (void) seg;
979 #endif
980
981   /* Get rid of any bignums now, so that we don't generate an error for which
982      we can't establish a line number later on.  Big numbers are never valid
983      in instructions, which is where this routine is always called.  */
984   if (prefix_mode != GE_OPT_PREFIX_BIG
985       && (ep->X_op == O_big
986           || (ep->X_add_symbol
987               && (walk_no_bignums (ep->X_add_symbol)
988                   || (ep->X_op_symbol
989                       && walk_no_bignums (ep->X_op_symbol))))))
990     {
991       inst.error = _("invalid constant");
992       *str = input_line_pointer;
993       input_line_pointer = save_in;
994       return 1;
995     }
996
997   *str = input_line_pointer;
998   input_line_pointer = save_in;
999   return 0;
1000 }
1001
1002 /* Turn a string in input_line_pointer into a floating point constant
1003    of type TYPE, and store the appropriate bytes in *LITP.  The number
1004    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1005    returned, or NULL on OK.
1006
1007    Note that fp constants aren't represent in the normal way on the ARM.
1008    In big endian mode, things are as expected.  However, in little endian
1009    mode fp constants are big-endian word-wise, and little-endian byte-wise
1010    within the words.  For example, (double) 1.1 in big endian mode is
1011    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1012    the byte sequence 99 99 f1 3f 9a 99 99 99.
1013
1014    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1015
1016 char *
1017 md_atof (int type, char * litP, int * sizeP)
1018 {
1019   int prec;
1020   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1021   char *t;
1022   int i;
1023
1024   switch (type)
1025     {
1026     case 'f':
1027     case 'F':
1028     case 's':
1029     case 'S':
1030       prec = 2;
1031       break;
1032
1033     case 'd':
1034     case 'D':
1035     case 'r':
1036     case 'R':
1037       prec = 4;
1038       break;
1039
1040     case 'x':
1041     case 'X':
1042       prec = 5;
1043       break;
1044
1045     case 'p':
1046     case 'P':
1047       prec = 5;
1048       break;
1049
1050     default:
1051       *sizeP = 0;
1052       return _("Unrecognized or unsupported floating point constant");
1053     }
1054
1055   t = atof_ieee (input_line_pointer, type, words);
1056   if (t)
1057     input_line_pointer = t;
1058   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1059
1060   if (target_big_endian)
1061     {
1062       for (i = 0; i < prec; i++)
1063         {
1064           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1065           litP += sizeof (LITTLENUM_TYPE);
1066         }
1067     }
1068   else
1069     {
1070       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1071         for (i = prec - 1; i >= 0; i--)
1072           {
1073             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1074             litP += sizeof (LITTLENUM_TYPE);
1075           }
1076       else
1077         /* For a 4 byte float the order of elements in `words' is 1 0.
1078            For an 8 byte float the order is 1 0 3 2.  */
1079         for (i = 0; i < prec; i += 2)
1080           {
1081             md_number_to_chars (litP, (valueT) words[i + 1],
1082                                 sizeof (LITTLENUM_TYPE));
1083             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1084                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1085             litP += 2 * sizeof (LITTLENUM_TYPE);
1086           }
1087     }
1088
1089   return NULL;
1090 }
1091
1092 /* We handle all bad expressions here, so that we can report the faulty
1093    instruction in the error message.  */
1094 void
1095 md_operand (expressionS * exp)
1096 {
1097   if (in_my_get_expression)
1098     exp->X_op = O_illegal;
1099 }
1100
1101 /* Immediate values.  */
1102
1103 /* Generic immediate-value read function for use in directives.
1104    Accepts anything that 'expression' can fold to a constant.
1105    *val receives the number.  */
1106 #ifdef OBJ_ELF
1107 static int
1108 immediate_for_directive (int *val)
1109 {
1110   expressionS exp;
1111   exp.X_op = O_illegal;
1112
1113   if (is_immediate_prefix (*input_line_pointer))
1114     {
1115       input_line_pointer++;
1116       expression (&exp);
1117     }
1118
1119   if (exp.X_op != O_constant)
1120     {
1121       as_bad (_("expected #constant"));
1122       ignore_rest_of_line ();
1123       return FAIL;
1124     }
1125   *val = exp.X_add_number;
1126   return SUCCESS;
1127 }
1128 #endif
1129
1130 /* Register parsing.  */
1131
1132 /* Generic register parser.  CCP points to what should be the
1133    beginning of a register name.  If it is indeed a valid register
1134    name, advance CCP over it and return the reg_entry structure;
1135    otherwise return NULL.  Does not issue diagnostics.  */
1136
1137 static struct reg_entry *
1138 arm_reg_parse_multi (char **ccp)
1139 {
1140   char *start = *ccp;
1141   char *p;
1142   struct reg_entry *reg;
1143
1144 #ifdef REGISTER_PREFIX
1145   if (*start != REGISTER_PREFIX)
1146     return NULL;
1147   start++;
1148 #endif
1149 #ifdef OPTIONAL_REGISTER_PREFIX
1150   if (*start == OPTIONAL_REGISTER_PREFIX)
1151     start++;
1152 #endif
1153
1154   p = start;
1155   if (!ISALPHA (*p) || !is_name_beginner (*p))
1156     return NULL;
1157
1158   do
1159     p++;
1160   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1161
1162   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1163
1164   if (!reg)
1165     return NULL;
1166
1167   *ccp = p;
1168   return reg;
1169 }
1170
1171 static int
1172 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1173                     enum arm_reg_type type)
1174 {
1175   /* Alternative syntaxes are accepted for a few register classes.  */
1176   switch (type)
1177     {
1178     case REG_TYPE_MVF:
1179     case REG_TYPE_MVD:
1180     case REG_TYPE_MVFX:
1181     case REG_TYPE_MVDX:
1182       /* Generic coprocessor register names are allowed for these.  */
1183       if (reg && reg->type == REG_TYPE_CN)
1184         return reg->number;
1185       break;
1186
1187     case REG_TYPE_CP:
1188       /* For backward compatibility, a bare number is valid here.  */
1189       {
1190         unsigned long processor = strtoul (start, ccp, 10);
1191         if (*ccp != start && processor <= 15)
1192           return processor;
1193       }
1194
1195     case REG_TYPE_MMXWC:
1196       /* WC includes WCG.  ??? I'm not sure this is true for all
1197          instructions that take WC registers.  */
1198       if (reg && reg->type == REG_TYPE_MMXWCG)
1199         return reg->number;
1200       break;
1201
1202     default:
1203       break;
1204     }
1205
1206   return FAIL;
1207 }
1208
1209 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1210    return value is the register number or FAIL.  */
1211
1212 static int
1213 arm_reg_parse (char **ccp, enum arm_reg_type type)
1214 {
1215   char *start = *ccp;
1216   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1217   int ret;
1218
1219   /* Do not allow a scalar (reg+index) to parse as a register.  */
1220   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1221     return FAIL;
1222
1223   if (reg && reg->type == type)
1224     return reg->number;
1225
1226   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1227     return ret;
1228
1229   *ccp = start;
1230   return FAIL;
1231 }
1232
1233 /* Parse a Neon type specifier. *STR should point at the leading '.'
1234    character. Does no verification at this stage that the type fits the opcode
1235    properly. E.g.,
1236
1237      .i32.i32.s16
1238      .s32.f32
1239      .u16
1240
1241    Can all be legally parsed by this function.
1242
1243    Fills in neon_type struct pointer with parsed information, and updates STR
1244    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1245    type, FAIL if not.  */
1246
1247 static int
1248 parse_neon_type (struct neon_type *type, char **str)
1249 {
1250   char *ptr = *str;
1251
1252   if (type)
1253     type->elems = 0;
1254
1255   while (type->elems < NEON_MAX_TYPE_ELS)
1256     {
1257       enum neon_el_type thistype = NT_untyped;
1258       unsigned thissize = -1u;
1259
1260       if (*ptr != '.')
1261         break;
1262
1263       ptr++;
1264
1265       /* Just a size without an explicit type.  */
1266       if (ISDIGIT (*ptr))
1267         goto parsesize;
1268
1269       switch (TOLOWER (*ptr))
1270         {
1271         case 'i': thistype = NT_integer; break;
1272         case 'f': thistype = NT_float; break;
1273         case 'p': thistype = NT_poly; break;
1274         case 's': thistype = NT_signed; break;
1275         case 'u': thistype = NT_unsigned; break;
1276         case 'd':
1277           thistype = NT_float;
1278           thissize = 64;
1279           ptr++;
1280           goto done;
1281         default:
1282           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1283           return FAIL;
1284         }
1285
1286       ptr++;
1287
1288       /* .f is an abbreviation for .f32.  */
1289       if (thistype == NT_float && !ISDIGIT (*ptr))
1290         thissize = 32;
1291       else
1292         {
1293         parsesize:
1294           thissize = strtoul (ptr, &ptr, 10);
1295
1296           if (thissize != 8 && thissize != 16 && thissize != 32
1297               && thissize != 64)
1298             {
1299               as_bad (_("bad size %d in type specifier"), thissize);
1300               return FAIL;
1301             }
1302         }
1303
1304       done:
1305       if (type)
1306         {
1307           type->el[type->elems].type = thistype;
1308           type->el[type->elems].size = thissize;
1309           type->elems++;
1310         }
1311     }
1312
1313   /* Empty/missing type is not a successful parse.  */
1314   if (type->elems == 0)
1315     return FAIL;
1316
1317   *str = ptr;
1318
1319   return SUCCESS;
1320 }
1321
1322 /* Errors may be set multiple times during parsing or bit encoding
1323    (particularly in the Neon bits), but usually the earliest error which is set
1324    will be the most meaningful. Avoid overwriting it with later (cascading)
1325    errors by calling this function.  */
1326
1327 static void
1328 first_error (const char *err)
1329 {
1330   if (!inst.error)
1331     inst.error = err;
1332 }
1333
1334 /* Parse a single type, e.g. ".s32", leading period included.  */
1335 static int
1336 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1337 {
1338   char *str = *ccp;
1339   struct neon_type optype;
1340
1341   if (*str == '.')
1342     {
1343       if (parse_neon_type (&optype, &str) == SUCCESS)
1344         {
1345           if (optype.elems == 1)
1346             *vectype = optype.el[0];
1347           else
1348             {
1349               first_error (_("only one type should be specified for operand"));
1350               return FAIL;
1351             }
1352         }
1353       else
1354         {
1355           first_error (_("vector type expected"));
1356           return FAIL;
1357         }
1358     }
1359   else
1360     return FAIL;
1361
1362   *ccp = str;
1363
1364   return SUCCESS;
1365 }
1366
1367 /* Special meanings for indices (which have a range of 0-7), which will fit into
1368    a 4-bit integer.  */
1369
1370 #define NEON_ALL_LANES          15
1371 #define NEON_INTERLEAVE_LANES   14
1372
1373 /* Parse either a register or a scalar, with an optional type. Return the
1374    register number, and optionally fill in the actual type of the register
1375    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1376    type/index information in *TYPEINFO.  */
1377
1378 static int
1379 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1380                            enum arm_reg_type *rtype,
1381                            struct neon_typed_alias *typeinfo)
1382 {
1383   char *str = *ccp;
1384   struct reg_entry *reg = arm_reg_parse_multi (&str);
1385   struct neon_typed_alias atype;
1386   struct neon_type_el parsetype;
1387
1388   atype.defined = 0;
1389   atype.index = -1;
1390   atype.eltype.type = NT_invtype;
1391   atype.eltype.size = -1;
1392
1393   /* Try alternate syntax for some types of register. Note these are mutually
1394      exclusive with the Neon syntax extensions.  */
1395   if (reg == NULL)
1396     {
1397       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1398       if (altreg != FAIL)
1399         *ccp = str;
1400       if (typeinfo)
1401         *typeinfo = atype;
1402       return altreg;
1403     }
1404
1405   /* Undo polymorphism when a set of register types may be accepted.  */
1406   if ((type == REG_TYPE_NDQ
1407        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1408       || (type == REG_TYPE_VFSD
1409           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1410       || (type == REG_TYPE_NSDQ
1411           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1412               || reg->type == REG_TYPE_NQ))
1413       || (type == REG_TYPE_MMXWC
1414           && (reg->type == REG_TYPE_MMXWCG)))
1415     type = (enum arm_reg_type) reg->type;
1416
1417   if (type != reg->type)
1418     return FAIL;
1419
1420   if (reg->neon)
1421     atype = *reg->neon;
1422
1423   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1424     {
1425       if ((atype.defined & NTA_HASTYPE) != 0)
1426         {
1427           first_error (_("can't redefine type for operand"));
1428           return FAIL;
1429         }
1430       atype.defined |= NTA_HASTYPE;
1431       atype.eltype = parsetype;
1432     }
1433
1434   if (skip_past_char (&str, '[') == SUCCESS)
1435     {
1436       if (type != REG_TYPE_VFD)
1437         {
1438           first_error (_("only D registers may be indexed"));
1439           return FAIL;
1440         }
1441
1442       if ((atype.defined & NTA_HASINDEX) != 0)
1443         {
1444           first_error (_("can't change index for operand"));
1445           return FAIL;
1446         }
1447
1448       atype.defined |= NTA_HASINDEX;
1449
1450       if (skip_past_char (&str, ']') == SUCCESS)
1451         atype.index = NEON_ALL_LANES;
1452       else
1453         {
1454           expressionS exp;
1455
1456           my_get_expression (&exp, &str, GE_NO_PREFIX);
1457
1458           if (exp.X_op != O_constant)
1459             {
1460               first_error (_("constant expression required"));
1461               return FAIL;
1462             }
1463
1464           if (skip_past_char (&str, ']') == FAIL)
1465             return FAIL;
1466
1467           atype.index = exp.X_add_number;
1468         }
1469     }
1470
1471   if (typeinfo)
1472     *typeinfo = atype;
1473
1474   if (rtype)
1475     *rtype = type;
1476
1477   *ccp = str;
1478
1479   return reg->number;
1480 }
1481
1482 /* Like arm_reg_parse, but allow allow the following extra features:
1483     - If RTYPE is non-zero, return the (possibly restricted) type of the
1484       register (e.g. Neon double or quad reg when either has been requested).
1485     - If this is a Neon vector type with additional type information, fill
1486       in the struct pointed to by VECTYPE (if non-NULL).
1487    This function will fault on encountering a scalar.  */
1488
1489 static int
1490 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1491                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1492 {
1493   struct neon_typed_alias atype;
1494   char *str = *ccp;
1495   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1496
1497   if (reg == FAIL)
1498     return FAIL;
1499
1500   /* Do not allow regname(... to parse as a register.  */
1501   if (*str == '(')
1502     return FAIL;
1503
1504   /* Do not allow a scalar (reg+index) to parse as a register.  */
1505   if ((atype.defined & NTA_HASINDEX) != 0)
1506     {
1507       first_error (_("register operand expected, but got scalar"));
1508       return FAIL;
1509     }
1510
1511   if (vectype)
1512     *vectype = atype.eltype;
1513
1514   *ccp = str;
1515
1516   return reg;
1517 }
1518
1519 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1520 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1521
1522 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1523    have enough information to be able to do a good job bounds-checking. So, we
1524    just do easy checks here, and do further checks later.  */
1525
1526 static int
1527 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1528 {
1529   int reg;
1530   char *str = *ccp;
1531   struct neon_typed_alias atype;
1532
1533   reg = parse_typed_reg_or_scalar (&str, REG_TYPE_VFD, NULL, &atype);
1534
1535   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1536     return FAIL;
1537
1538   if (atype.index == NEON_ALL_LANES)
1539     {
1540       first_error (_("scalar must have an index"));
1541       return FAIL;
1542     }
1543   else if (atype.index >= 64 / elsize)
1544     {
1545       first_error (_("scalar index out of range"));
1546       return FAIL;
1547     }
1548
1549   if (type)
1550     *type = atype.eltype;
1551
1552   *ccp = str;
1553
1554   return reg * 16 + atype.index;
1555 }
1556
1557 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1558
1559 static long
1560 parse_reg_list (char ** strp)
1561 {
1562   char * str = * strp;
1563   long   range = 0;
1564   int    another_range;
1565
1566   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1567   do
1568     {
1569       another_range = 0;
1570
1571       if (*str == '{')
1572         {
1573           int in_range = 0;
1574           int cur_reg = -1;
1575
1576           str++;
1577           do
1578             {
1579               int reg;
1580
1581               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1582                 {
1583                   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
1584                   return FAIL;
1585                 }
1586
1587               if (in_range)
1588                 {
1589                   int i;
1590
1591                   if (reg <= cur_reg)
1592                     {
1593                       first_error (_("bad range in register list"));
1594                       return FAIL;
1595                     }
1596
1597                   for (i = cur_reg + 1; i < reg; i++)
1598                     {
1599                       if (range & (1 << i))
1600                         as_tsktsk
1601                           (_("Warning: duplicated register (r%d) in register list"),
1602                            i);
1603                       else
1604                         range |= 1 << i;
1605                     }
1606                   in_range = 0;
1607                 }
1608
1609               if (range & (1 << reg))
1610                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1611                            reg);
1612               else if (reg <= cur_reg)
1613                 as_tsktsk (_("Warning: register range not in ascending order"));
1614
1615               range |= 1 << reg;
1616               cur_reg = reg;
1617             }
1618           while (skip_past_comma (&str) != FAIL
1619                  || (in_range = 1, *str++ == '-'));
1620           str--;
1621
1622           if (*str++ != '}')
1623             {
1624               first_error (_("missing `}'"));
1625               return FAIL;
1626             }
1627         }
1628       else
1629         {
1630           expressionS exp;
1631
1632           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1633             return FAIL;
1634
1635           if (exp.X_op == O_constant)
1636             {
1637               if (exp.X_add_number
1638                   != (exp.X_add_number & 0x0000ffff))
1639                 {
1640                   inst.error = _("invalid register mask");
1641                   return FAIL;
1642                 }
1643
1644               if ((range & exp.X_add_number) != 0)
1645                 {
1646                   int regno = range & exp.X_add_number;
1647
1648                   regno &= -regno;
1649                   regno = (1 << regno) - 1;
1650                   as_tsktsk
1651                     (_("Warning: duplicated register (r%d) in register list"),
1652                      regno);
1653                 }
1654
1655               range |= exp.X_add_number;
1656             }
1657           else
1658             {
1659               if (inst.reloc.type != 0)
1660                 {
1661                   inst.error = _("expression too complex");
1662                   return FAIL;
1663                 }
1664
1665               memcpy (&inst.reloc.exp, &exp, sizeof (expressionS));
1666               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1667               inst.reloc.pc_rel = 0;
1668             }
1669         }
1670
1671       if (*str == '|' || *str == '+')
1672         {
1673           str++;
1674           another_range = 1;
1675         }
1676     }
1677   while (another_range);
1678
1679   *strp = str;
1680   return range;
1681 }
1682
1683 /* Types of registers in a list.  */
1684
1685 enum reg_list_els
1686 {
1687   REGLIST_VFP_S,
1688   REGLIST_VFP_D,
1689   REGLIST_NEON_D
1690 };
1691
1692 /* Parse a VFP register list.  If the string is invalid return FAIL.
1693    Otherwise return the number of registers, and set PBASE to the first
1694    register.  Parses registers of type ETYPE.
1695    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1696      - Q registers can be used to specify pairs of D registers
1697      - { } can be omitted from around a singleton register list
1698          FIXME: This is not implemented, as it would require backtracking in
1699          some cases, e.g.:
1700            vtbl.8 d3,d4,d5
1701          This could be done (the meaning isn't really ambiguous), but doesn't
1702          fit in well with the current parsing framework.
1703      - 32 D registers may be used (also true for VFPv3).
1704    FIXME: Types are ignored in these register lists, which is probably a
1705    bug.  */
1706
1707 static int
1708 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype)
1709 {
1710   char *str = *ccp;
1711   int base_reg;
1712   int new_base;
1713   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1714   int max_regs = 0;
1715   int count = 0;
1716   int warned = 0;
1717   unsigned long mask = 0;
1718   int i;
1719
1720   if (*str != '{')
1721     {
1722       inst.error = _("expecting {");
1723       return FAIL;
1724     }
1725
1726   str++;
1727
1728   switch (etype)
1729     {
1730     case REGLIST_VFP_S:
1731       regtype = REG_TYPE_VFS;
1732       max_regs = 32;
1733       break;
1734
1735     case REGLIST_VFP_D:
1736       regtype = REG_TYPE_VFD;
1737       break;
1738
1739     case REGLIST_NEON_D:
1740       regtype = REG_TYPE_NDQ;
1741       break;
1742     }
1743
1744   if (etype != REGLIST_VFP_S)
1745     {
1746       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
1747       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
1748         {
1749           max_regs = 32;
1750           if (thumb_mode)
1751             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1752                                     fpu_vfp_ext_d32);
1753           else
1754             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1755                                     fpu_vfp_ext_d32);
1756         }
1757       else
1758         max_regs = 16;
1759     }
1760
1761   base_reg = max_regs;
1762
1763   do
1764     {
1765       int setmask = 1, addregs = 1;
1766
1767       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
1768
1769       if (new_base == FAIL)
1770         {
1771           first_error (_(reg_expected_msgs[regtype]));
1772           return FAIL;
1773         }
1774
1775       if (new_base >= max_regs)
1776         {
1777           first_error (_("register out of range in list"));
1778           return FAIL;
1779         }
1780
1781       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1782       if (regtype == REG_TYPE_NQ)
1783         {
1784           setmask = 3;
1785           addregs = 2;
1786         }
1787
1788       if (new_base < base_reg)
1789         base_reg = new_base;
1790
1791       if (mask & (setmask << new_base))
1792         {
1793           first_error (_("invalid register list"));
1794           return FAIL;
1795         }
1796
1797       if ((mask >> new_base) != 0 && ! warned)
1798         {
1799           as_tsktsk (_("register list not in ascending order"));
1800           warned = 1;
1801         }
1802
1803       mask |= setmask << new_base;
1804       count += addregs;
1805
1806       if (*str == '-') /* We have the start of a range expression */
1807         {
1808           int high_range;
1809
1810           str++;
1811
1812           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
1813               == FAIL)
1814             {
1815               inst.error = gettext (reg_expected_msgs[regtype]);
1816               return FAIL;
1817             }
1818
1819           if (high_range >= max_regs)
1820             {
1821               first_error (_("register out of range in list"));
1822               return FAIL;
1823             }
1824
1825           if (regtype == REG_TYPE_NQ)
1826             high_range = high_range + 1;
1827
1828           if (high_range <= new_base)
1829             {
1830               inst.error = _("register range not in ascending order");
1831               return FAIL;
1832             }
1833
1834           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1835             {
1836               if (mask & (setmask << new_base))
1837                 {
1838                   inst.error = _("invalid register list");
1839                   return FAIL;
1840                 }
1841
1842               mask |= setmask << new_base;
1843               count += addregs;
1844             }
1845         }
1846     }
1847   while (skip_past_comma (&str) != FAIL);
1848
1849   str++;
1850
1851   /* Sanity check -- should have raised a parse error above.  */
1852   if (count == 0 || count > max_regs)
1853     abort ();
1854
1855   *pbase = base_reg;
1856
1857   /* Final test -- the registers must be consecutive.  */
1858   mask >>= base_reg;
1859   for (i = 0; i < count; i++)
1860     {
1861       if ((mask & (1u << i)) == 0)
1862         {
1863           inst.error = _("non-contiguous register range");
1864           return FAIL;
1865         }
1866     }
1867
1868   *ccp = str;
1869
1870   return count;
1871 }
1872
1873 /* True if two alias types are the same.  */
1874
1875 static bfd_boolean
1876 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
1877 {
1878   if (!a && !b)
1879     return TRUE;
1880
1881   if (!a || !b)
1882     return FALSE;
1883
1884   if (a->defined != b->defined)
1885     return FALSE;
1886
1887   if ((a->defined & NTA_HASTYPE) != 0
1888       && (a->eltype.type != b->eltype.type
1889           || a->eltype.size != b->eltype.size))
1890     return FALSE;
1891
1892   if ((a->defined & NTA_HASINDEX) != 0
1893       && (a->index != b->index))
1894     return FALSE;
1895
1896   return TRUE;
1897 }
1898
1899 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
1900    The base register is put in *PBASE.
1901    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
1902    the return value.
1903    The register stride (minus one) is put in bit 4 of the return value.
1904    Bits [6:5] encode the list length (minus one).
1905    The type of the list elements is put in *ELTYPE, if non-NULL.  */
1906
1907 #define NEON_LANE(X)            ((X) & 0xf)
1908 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
1909 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
1910
1911 static int
1912 parse_neon_el_struct_list (char **str, unsigned *pbase,
1913                            struct neon_type_el *eltype)
1914 {
1915   char *ptr = *str;
1916   int base_reg = -1;
1917   int reg_incr = -1;
1918   int count = 0;
1919   int lane = -1;
1920   int leading_brace = 0;
1921   enum arm_reg_type rtype = REG_TYPE_NDQ;
1922   const char *const incr_error = _("register stride must be 1 or 2");
1923   const char *const type_error = _("mismatched element/structure types in list");
1924   struct neon_typed_alias firsttype;
1925
1926   if (skip_past_char (&ptr, '{') == SUCCESS)
1927     leading_brace = 1;
1928
1929   do
1930     {
1931       struct neon_typed_alias atype;
1932       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
1933
1934       if (getreg == FAIL)
1935         {
1936           first_error (_(reg_expected_msgs[rtype]));
1937           return FAIL;
1938         }
1939
1940       if (base_reg == -1)
1941         {
1942           base_reg = getreg;
1943           if (rtype == REG_TYPE_NQ)
1944             {
1945               reg_incr = 1;
1946             }
1947           firsttype = atype;
1948         }
1949       else if (reg_incr == -1)
1950         {
1951           reg_incr = getreg - base_reg;
1952           if (reg_incr < 1 || reg_incr > 2)
1953             {
1954               first_error (_(incr_error));
1955               return FAIL;
1956             }
1957         }
1958       else if (getreg != base_reg + reg_incr * count)
1959         {
1960           first_error (_(incr_error));
1961           return FAIL;
1962         }
1963
1964       if (! neon_alias_types_same (&atype, &firsttype))
1965         {
1966           first_error (_(type_error));
1967           return FAIL;
1968         }
1969
1970       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
1971          modes.  */
1972       if (ptr[0] == '-')
1973         {
1974           struct neon_typed_alias htype;
1975           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
1976           if (lane == -1)
1977             lane = NEON_INTERLEAVE_LANES;
1978           else if (lane != NEON_INTERLEAVE_LANES)
1979             {
1980               first_error (_(type_error));
1981               return FAIL;
1982             }
1983           if (reg_incr == -1)
1984             reg_incr = 1;
1985           else if (reg_incr != 1)
1986             {
1987               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
1988               return FAIL;
1989             }
1990           ptr++;
1991           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
1992           if (hireg == FAIL)
1993             {
1994               first_error (_(reg_expected_msgs[rtype]));
1995               return FAIL;
1996             }
1997           if (! neon_alias_types_same (&htype, &firsttype))
1998             {
1999               first_error (_(type_error));
2000               return FAIL;
2001             }
2002           count += hireg + dregs - getreg;
2003           continue;
2004         }
2005
2006       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2007       if (rtype == REG_TYPE_NQ)
2008         {
2009           count += 2;
2010           continue;
2011         }
2012
2013       if ((atype.defined & NTA_HASINDEX) != 0)
2014         {
2015           if (lane == -1)
2016             lane = atype.index;
2017           else if (lane != atype.index)
2018             {
2019               first_error (_(type_error));
2020               return FAIL;
2021             }
2022         }
2023       else if (lane == -1)
2024         lane = NEON_INTERLEAVE_LANES;
2025       else if (lane != NEON_INTERLEAVE_LANES)
2026         {
2027           first_error (_(type_error));
2028           return FAIL;
2029         }
2030       count++;
2031     }
2032   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2033
2034   /* No lane set by [x]. We must be interleaving structures.  */
2035   if (lane == -1)
2036     lane = NEON_INTERLEAVE_LANES;
2037
2038   /* Sanity check.  */
2039   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
2040       || (count > 1 && reg_incr == -1))
2041     {
2042       first_error (_("error parsing element/structure list"));
2043       return FAIL;
2044     }
2045
2046   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2047     {
2048       first_error (_("expected }"));
2049       return FAIL;
2050     }
2051
2052   if (reg_incr == -1)
2053     reg_incr = 1;
2054
2055   if (eltype)
2056     *eltype = firsttype.eltype;
2057
2058   *pbase = base_reg;
2059   *str = ptr;
2060
2061   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2062 }
2063
2064 /* Parse an explicit relocation suffix on an expression.  This is
2065    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2066    arm_reloc_hsh contains no entries, so this function can only
2067    succeed if there is no () after the word.  Returns -1 on error,
2068    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2069
2070 static int
2071 parse_reloc (char **str)
2072 {
2073   struct reloc_entry *r;
2074   char *p, *q;
2075
2076   if (**str != '(')
2077     return BFD_RELOC_UNUSED;
2078
2079   p = *str + 1;
2080   q = p;
2081
2082   while (*q && *q != ')' && *q != ',')
2083     q++;
2084   if (*q != ')')
2085     return -1;
2086
2087   if ((r = (struct reloc_entry *)
2088        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2089     return -1;
2090
2091   *str = q + 1;
2092   return r->reloc;
2093 }
2094
2095 /* Directives: register aliases.  */
2096
2097 static struct reg_entry *
2098 insert_reg_alias (char *str, unsigned number, int type)
2099 {
2100   struct reg_entry *new_reg;
2101   const char *name;
2102
2103   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2104     {
2105       if (new_reg->builtin)
2106         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2107
2108       /* Only warn about a redefinition if it's not defined as the
2109          same register.  */
2110       else if (new_reg->number != number || new_reg->type != type)
2111         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2112
2113       return NULL;
2114     }
2115
2116   name = xstrdup (str);
2117   new_reg = (struct reg_entry *) xmalloc (sizeof (struct reg_entry));
2118
2119   new_reg->name = name;
2120   new_reg->number = number;
2121   new_reg->type = type;
2122   new_reg->builtin = FALSE;
2123   new_reg->neon = NULL;
2124
2125   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2126     abort ();
2127
2128   return new_reg;
2129 }
2130
2131 static void
2132 insert_neon_reg_alias (char *str, int number, int type,
2133                        struct neon_typed_alias *atype)
2134 {
2135   struct reg_entry *reg = insert_reg_alias (str, number, type);
2136
2137   if (!reg)
2138     {
2139       first_error (_("attempt to redefine typed alias"));
2140       return;
2141     }
2142
2143   if (atype)
2144     {
2145       reg->neon = (struct neon_typed_alias *)
2146           xmalloc (sizeof (struct neon_typed_alias));
2147       *reg->neon = *atype;
2148     }
2149 }
2150
2151 /* Look for the .req directive.  This is of the form:
2152
2153         new_register_name .req existing_register_name
2154
2155    If we find one, or if it looks sufficiently like one that we want to
2156    handle any error here, return TRUE.  Otherwise return FALSE.  */
2157
2158 static bfd_boolean
2159 create_register_alias (char * newname, char *p)
2160 {
2161   struct reg_entry *old;
2162   char *oldname, *nbuf;
2163   size_t nlen;
2164
2165   /* The input scrubber ensures that whitespace after the mnemonic is
2166      collapsed to single spaces.  */
2167   oldname = p;
2168   if (strncmp (oldname, " .req ", 6) != 0)
2169     return FALSE;
2170
2171   oldname += 6;
2172   if (*oldname == '\0')
2173     return FALSE;
2174
2175   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2176   if (!old)
2177     {
2178       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2179       return TRUE;
2180     }
2181
2182   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2183      the desired alias name, and p points to its end.  If not, then
2184      the desired alias name is in the global original_case_string.  */
2185 #ifdef TC_CASE_SENSITIVE
2186   nlen = p - newname;
2187 #else
2188   newname = original_case_string;
2189   nlen = strlen (newname);
2190 #endif
2191
2192   nbuf = (char *) alloca (nlen + 1);
2193   memcpy (nbuf, newname, nlen);
2194   nbuf[nlen] = '\0';
2195
2196   /* Create aliases under the new name as stated; an all-lowercase
2197      version of the new name; and an all-uppercase version of the new
2198      name.  */
2199   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2200     {
2201       for (p = nbuf; *p; p++)
2202         *p = TOUPPER (*p);
2203
2204       if (strncmp (nbuf, newname, nlen))
2205         {
2206           /* If this attempt to create an additional alias fails, do not bother
2207              trying to create the all-lower case alias.  We will fail and issue
2208              a second, duplicate error message.  This situation arises when the
2209              programmer does something like:
2210                foo .req r0
2211                Foo .req r1
2212              The second .req creates the "Foo" alias but then fails to create
2213              the artificial FOO alias because it has already been created by the
2214              first .req.  */
2215           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2216             return TRUE;
2217         }
2218
2219       for (p = nbuf; *p; p++)
2220         *p = TOLOWER (*p);
2221
2222       if (strncmp (nbuf, newname, nlen))
2223         insert_reg_alias (nbuf, old->number, old->type);
2224     }
2225
2226   return TRUE;
2227 }
2228
2229 /* Create a Neon typed/indexed register alias using directives, e.g.:
2230      X .dn d5.s32[1]
2231      Y .qn 6.s16
2232      Z .dn d7
2233      T .dn Z[0]
2234    These typed registers can be used instead of the types specified after the
2235    Neon mnemonic, so long as all operands given have types. Types can also be
2236    specified directly, e.g.:
2237      vadd d0.s32, d1.s32, d2.s32  */
2238
2239 static bfd_boolean
2240 create_neon_reg_alias (char *newname, char *p)
2241 {
2242   enum arm_reg_type basetype;
2243   struct reg_entry *basereg;
2244   struct reg_entry mybasereg;
2245   struct neon_type ntype;
2246   struct neon_typed_alias typeinfo;
2247   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2248   int namelen;
2249
2250   typeinfo.defined = 0;
2251   typeinfo.eltype.type = NT_invtype;
2252   typeinfo.eltype.size = -1;
2253   typeinfo.index = -1;
2254
2255   nameend = p;
2256
2257   if (strncmp (p, " .dn ", 5) == 0)
2258     basetype = REG_TYPE_VFD;
2259   else if (strncmp (p, " .qn ", 5) == 0)
2260     basetype = REG_TYPE_NQ;
2261   else
2262     return FALSE;
2263
2264   p += 5;
2265
2266   if (*p == '\0')
2267     return FALSE;
2268
2269   basereg = arm_reg_parse_multi (&p);
2270
2271   if (basereg && basereg->type != basetype)
2272     {
2273       as_bad (_("bad type for register"));
2274       return FALSE;
2275     }
2276
2277   if (basereg == NULL)
2278     {
2279       expressionS exp;
2280       /* Try parsing as an integer.  */
2281       my_get_expression (&exp, &p, GE_NO_PREFIX);
2282       if (exp.X_op != O_constant)
2283         {
2284           as_bad (_("expression must be constant"));
2285           return FALSE;
2286         }
2287       basereg = &mybasereg;
2288       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2289                                                   : exp.X_add_number;
2290       basereg->neon = 0;
2291     }
2292
2293   if (basereg->neon)
2294     typeinfo = *basereg->neon;
2295
2296   if (parse_neon_type (&ntype, &p) == SUCCESS)
2297     {
2298       /* We got a type.  */
2299       if (typeinfo.defined & NTA_HASTYPE)
2300         {
2301           as_bad (_("can't redefine the type of a register alias"));
2302           return FALSE;
2303         }
2304
2305       typeinfo.defined |= NTA_HASTYPE;
2306       if (ntype.elems != 1)
2307         {
2308           as_bad (_("you must specify a single type only"));
2309           return FALSE;
2310         }
2311       typeinfo.eltype = ntype.el[0];
2312     }
2313
2314   if (skip_past_char (&p, '[') == SUCCESS)
2315     {
2316       expressionS exp;
2317       /* We got a scalar index.  */
2318
2319       if (typeinfo.defined & NTA_HASINDEX)
2320         {
2321           as_bad (_("can't redefine the index of a scalar alias"));
2322           return FALSE;
2323         }
2324
2325       my_get_expression (&exp, &p, GE_NO_PREFIX);
2326
2327       if (exp.X_op != O_constant)
2328         {
2329           as_bad (_("scalar index must be constant"));
2330           return FALSE;
2331         }
2332
2333       typeinfo.defined |= NTA_HASINDEX;
2334       typeinfo.index = exp.X_add_number;
2335
2336       if (skip_past_char (&p, ']') == FAIL)
2337         {
2338           as_bad (_("expecting ]"));
2339           return FALSE;
2340         }
2341     }
2342
2343   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2344      the desired alias name, and p points to its end.  If not, then
2345      the desired alias name is in the global original_case_string.  */
2346 #ifdef TC_CASE_SENSITIVE
2347   namelen = nameend - newname;
2348 #else
2349   newname = original_case_string;
2350   namelen = strlen (newname);
2351 #endif
2352
2353   namebuf = (char *) alloca (namelen + 1);
2354   strncpy (namebuf, newname, namelen);
2355   namebuf[namelen] = '\0';
2356
2357   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2358                          typeinfo.defined != 0 ? &typeinfo : NULL);
2359
2360   /* Insert name in all uppercase.  */
2361   for (p = namebuf; *p; p++)
2362     *p = TOUPPER (*p);
2363
2364   if (strncmp (namebuf, newname, namelen))
2365     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2366                            typeinfo.defined != 0 ? &typeinfo : NULL);
2367
2368   /* Insert name in all lowercase.  */
2369   for (p = namebuf; *p; p++)
2370     *p = TOLOWER (*p);
2371
2372   if (strncmp (namebuf, newname, namelen))
2373     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2374                            typeinfo.defined != 0 ? &typeinfo : NULL);
2375
2376   return TRUE;
2377 }
2378
2379 /* Should never be called, as .req goes between the alias and the
2380    register name, not at the beginning of the line.  */
2381
2382 static void
2383 s_req (int a ATTRIBUTE_UNUSED)
2384 {
2385   as_bad (_("invalid syntax for .req directive"));
2386 }
2387
2388 static void
2389 s_dn (int a ATTRIBUTE_UNUSED)
2390 {
2391   as_bad (_("invalid syntax for .dn directive"));
2392 }
2393
2394 static void
2395 s_qn (int a ATTRIBUTE_UNUSED)
2396 {
2397   as_bad (_("invalid syntax for .qn directive"));
2398 }
2399
2400 /* The .unreq directive deletes an alias which was previously defined
2401    by .req.  For example:
2402
2403        my_alias .req r11
2404        .unreq my_alias    */
2405
2406 static void
2407 s_unreq (int a ATTRIBUTE_UNUSED)
2408 {
2409   char * name;
2410   char saved_char;
2411
2412   name = input_line_pointer;
2413
2414   while (*input_line_pointer != 0
2415          && *input_line_pointer != ' '
2416          && *input_line_pointer != '\n')
2417     ++input_line_pointer;
2418
2419   saved_char = *input_line_pointer;
2420   *input_line_pointer = 0;
2421
2422   if (!*name)
2423     as_bad (_("invalid syntax for .unreq directive"));
2424   else
2425     {
2426       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2427                                                               name);
2428
2429       if (!reg)
2430         as_bad (_("unknown register alias '%s'"), name);
2431       else if (reg->builtin)
2432         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2433                  name);
2434       else
2435         {
2436           char * p;
2437           char * nbuf;
2438
2439           hash_delete (arm_reg_hsh, name, FALSE);
2440           free ((char *) reg->name);
2441           if (reg->neon)
2442             free (reg->neon);
2443           free (reg);
2444
2445           /* Also locate the all upper case and all lower case versions.
2446              Do not complain if we cannot find one or the other as it
2447              was probably deleted above.  */
2448
2449           nbuf = strdup (name);
2450           for (p = nbuf; *p; p++)
2451             *p = TOUPPER (*p);
2452           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2453           if (reg)
2454             {
2455               hash_delete (arm_reg_hsh, nbuf, FALSE);
2456               free ((char *) reg->name);
2457               if (reg->neon)
2458                 free (reg->neon);
2459               free (reg);
2460             }
2461
2462           for (p = nbuf; *p; p++)
2463             *p = TOLOWER (*p);
2464           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2465           if (reg)
2466             {
2467               hash_delete (arm_reg_hsh, nbuf, FALSE);
2468               free ((char *) reg->name);
2469               if (reg->neon)
2470                 free (reg->neon);
2471               free (reg);
2472             }
2473
2474           free (nbuf);
2475         }
2476     }
2477
2478   *input_line_pointer = saved_char;
2479   demand_empty_rest_of_line ();
2480 }
2481
2482 /* Directives: Instruction set selection.  */
2483
2484 #ifdef OBJ_ELF
2485 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2486    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2487    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2488    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2489
2490 /* Create a new mapping symbol for the transition to STATE.  */
2491
2492 static void
2493 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2494 {
2495   symbolS * symbolP;
2496   const char * symname;
2497   int type;
2498
2499   switch (state)
2500     {
2501     case MAP_DATA:
2502       symname = "$d";
2503       type = BSF_NO_FLAGS;
2504       break;
2505     case MAP_ARM:
2506       symname = "$a";
2507       type = BSF_NO_FLAGS;
2508       break;
2509     case MAP_THUMB:
2510       symname = "$t";
2511       type = BSF_NO_FLAGS;
2512       break;
2513     default:
2514       abort ();
2515     }
2516
2517   symbolP = symbol_new (symname, now_seg, value, frag);
2518   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2519
2520   switch (state)
2521     {
2522     case MAP_ARM:
2523       THUMB_SET_FUNC (symbolP, 0);
2524       ARM_SET_THUMB (symbolP, 0);
2525       ARM_SET_INTERWORK (symbolP, support_interwork);
2526       break;
2527
2528     case MAP_THUMB:
2529       THUMB_SET_FUNC (symbolP, 1);
2530       ARM_SET_THUMB (symbolP, 1);
2531       ARM_SET_INTERWORK (symbolP, support_interwork);
2532       break;
2533
2534     case MAP_DATA:
2535     default:
2536       break;
2537     }
2538
2539   /* Save the mapping symbols for future reference.  Also check that
2540      we do not place two mapping symbols at the same offset within a
2541      frag.  We'll handle overlap between frags in
2542      check_mapping_symbols.
2543
2544      If .fill or other data filling directive generates zero sized data,
2545      the mapping symbol for the following code will have the same value
2546      as the one generated for the data filling directive.  In this case,
2547      we replace the old symbol with the new one at the same address.  */
2548   if (value == 0)
2549     {
2550       if (frag->tc_frag_data.first_map != NULL)
2551         {
2552           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2553           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2554         }
2555       frag->tc_frag_data.first_map = symbolP;
2556     }
2557   if (frag->tc_frag_data.last_map != NULL)
2558     {
2559       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2560       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2561         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2562     }
2563   frag->tc_frag_data.last_map = symbolP;
2564 }
2565
2566 /* We must sometimes convert a region marked as code to data during
2567    code alignment, if an odd number of bytes have to be padded.  The
2568    code mapping symbol is pushed to an aligned address.  */
2569
2570 static void
2571 insert_data_mapping_symbol (enum mstate state,
2572                             valueT value, fragS *frag, offsetT bytes)
2573 {
2574   /* If there was already a mapping symbol, remove it.  */
2575   if (frag->tc_frag_data.last_map != NULL
2576       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2577     {
2578       symbolS *symp = frag->tc_frag_data.last_map;
2579
2580       if (value == 0)
2581         {
2582           know (frag->tc_frag_data.first_map == symp);
2583           frag->tc_frag_data.first_map = NULL;
2584         }
2585       frag->tc_frag_data.last_map = NULL;
2586       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2587     }
2588
2589   make_mapping_symbol (MAP_DATA, value, frag);
2590   make_mapping_symbol (state, value + bytes, frag);
2591 }
2592
2593 static void mapping_state_2 (enum mstate state, int max_chars);
2594
2595 /* Set the mapping state to STATE.  Only call this when about to
2596    emit some STATE bytes to the file.  */
2597
2598 void
2599 mapping_state (enum mstate state)
2600 {
2601   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2602
2603 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2604
2605   if (mapstate == state)
2606     /* The mapping symbol has already been emitted.
2607        There is nothing else to do.  */
2608     return;
2609
2610   if (state == MAP_ARM || state == MAP_THUMB)
2611     /*  PR gas/12931
2612         All ARM instructions require 4-byte alignment.
2613         (Almost) all Thumb instructions require 2-byte alignment.
2614
2615         When emitting instructions into any section, mark the section
2616         appropriately.
2617
2618         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2619         but themselves require 2-byte alignment; this applies to some
2620         PC- relative forms.  However, these cases will invovle implicit
2621         literal pool generation or an explicit .align >=2, both of
2622         which will cause the section to me marked with sufficient
2623         alignment.  Thus, we don't handle those cases here.  */
2624     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2625
2626   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2627     /* This case will be evaluated later in the next else.  */
2628     return;
2629   else if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2630           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2631     {
2632       /* Only add the symbol if the offset is > 0:
2633          if we're at the first frag, check it's size > 0;
2634          if we're not at the first frag, then for sure
2635             the offset is > 0.  */
2636       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2637       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2638
2639       if (add_symbol)
2640         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2641     }
2642
2643   mapping_state_2 (state, 0);
2644 #undef TRANSITION
2645 }
2646
2647 /* Same as mapping_state, but MAX_CHARS bytes have already been
2648    allocated.  Put the mapping symbol that far back.  */
2649
2650 static void
2651 mapping_state_2 (enum mstate state, int max_chars)
2652 {
2653   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2654
2655   if (!SEG_NORMAL (now_seg))
2656     return;
2657
2658   if (mapstate == state)
2659     /* The mapping symbol has already been emitted.
2660        There is nothing else to do.  */
2661     return;
2662
2663   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2664   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2665 }
2666 #else
2667 #define mapping_state(x) ((void)0)
2668 #define mapping_state_2(x, y) ((void)0)
2669 #endif
2670
2671 /* Find the real, Thumb encoded start of a Thumb function.  */
2672
2673 #ifdef OBJ_COFF
2674 static symbolS *
2675 find_real_start (symbolS * symbolP)
2676 {
2677   char *       real_start;
2678   const char * name = S_GET_NAME (symbolP);
2679   symbolS *    new_target;
2680
2681   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2682 #define STUB_NAME ".real_start_of"
2683
2684   if (name == NULL)
2685     abort ();
2686
2687   /* The compiler may generate BL instructions to local labels because
2688      it needs to perform a branch to a far away location. These labels
2689      do not have a corresponding ".real_start_of" label.  We check
2690      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2691      the ".real_start_of" convention for nonlocal branches.  */
2692   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2693     return symbolP;
2694
2695   real_start = ACONCAT ((STUB_NAME, name, NULL));
2696   new_target = symbol_find (real_start);
2697
2698   if (new_target == NULL)
2699     {
2700       as_warn (_("Failed to find real start of function: %s\n"), name);
2701       new_target = symbolP;
2702     }
2703
2704   return new_target;
2705 }
2706 #endif
2707
2708 static void
2709 opcode_select (int width)
2710 {
2711   switch (width)
2712     {
2713     case 16:
2714       if (! thumb_mode)
2715         {
2716           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2717             as_bad (_("selected processor does not support THUMB opcodes"));
2718
2719           thumb_mode = 1;
2720           /* No need to force the alignment, since we will have been
2721              coming from ARM mode, which is word-aligned.  */
2722           record_alignment (now_seg, 1);
2723         }
2724       break;
2725
2726     case 32:
2727       if (thumb_mode)
2728         {
2729           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2730             as_bad (_("selected processor does not support ARM opcodes"));
2731
2732           thumb_mode = 0;
2733
2734           if (!need_pass_2)
2735             frag_align (2, 0, 0);
2736
2737           record_alignment (now_seg, 1);
2738         }
2739       break;
2740
2741     default:
2742       as_bad (_("invalid instruction size selected (%d)"), width);
2743     }
2744 }
2745
2746 static void
2747 s_arm (int ignore ATTRIBUTE_UNUSED)
2748 {
2749   opcode_select (32);
2750   demand_empty_rest_of_line ();
2751 }
2752
2753 static void
2754 s_thumb (int ignore ATTRIBUTE_UNUSED)
2755 {
2756   opcode_select (16);
2757   demand_empty_rest_of_line ();
2758 }
2759
2760 static void
2761 s_code (int unused ATTRIBUTE_UNUSED)
2762 {
2763   int temp;
2764
2765   temp = get_absolute_expression ();
2766   switch (temp)
2767     {
2768     case 16:
2769     case 32:
2770       opcode_select (temp);
2771       break;
2772
2773     default:
2774       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2775     }
2776 }
2777
2778 static void
2779 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2780 {
2781   /* If we are not already in thumb mode go into it, EVEN if
2782      the target processor does not support thumb instructions.
2783      This is used by gcc/config/arm/lib1funcs.asm for example
2784      to compile interworking support functions even if the
2785      target processor should not support interworking.  */
2786   if (! thumb_mode)
2787     {
2788       thumb_mode = 2;
2789       record_alignment (now_seg, 1);
2790     }
2791
2792   demand_empty_rest_of_line ();
2793 }
2794
2795 static void
2796 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2797 {
2798   s_thumb (0);
2799
2800   /* The following label is the name/address of the start of a Thumb function.
2801      We need to know this for the interworking support.  */
2802   label_is_thumb_function_name = TRUE;
2803 }
2804
2805 /* Perform a .set directive, but also mark the alias as
2806    being a thumb function.  */
2807
2808 static void
2809 s_thumb_set (int equiv)
2810 {
2811   /* XXX the following is a duplicate of the code for s_set() in read.c
2812      We cannot just call that code as we need to get at the symbol that
2813      is created.  */
2814   char *    name;
2815   char      delim;
2816   char *    end_name;
2817   symbolS * symbolP;
2818
2819   /* Especial apologies for the random logic:
2820      This just grew, and could be parsed much more simply!
2821      Dean - in haste.  */
2822   name      = input_line_pointer;
2823   delim     = get_symbol_end ();
2824   end_name  = input_line_pointer;
2825   *end_name = delim;
2826
2827   if (*input_line_pointer != ',')
2828     {
2829       *end_name = 0;
2830       as_bad (_("expected comma after name \"%s\""), name);
2831       *end_name = delim;
2832       ignore_rest_of_line ();
2833       return;
2834     }
2835
2836   input_line_pointer++;
2837   *end_name = 0;
2838
2839   if (name[0] == '.' && name[1] == '\0')
2840     {
2841       /* XXX - this should not happen to .thumb_set.  */
2842       abort ();
2843     }
2844
2845   if ((symbolP = symbol_find (name)) == NULL
2846       && (symbolP = md_undefined_symbol (name)) == NULL)
2847     {
2848 #ifndef NO_LISTING
2849       /* When doing symbol listings, play games with dummy fragments living
2850          outside the normal fragment chain to record the file and line info
2851          for this symbol.  */
2852       if (listing & LISTING_SYMBOLS)
2853         {
2854           extern struct list_info_struct * listing_tail;
2855           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2856
2857           memset (dummy_frag, 0, sizeof (fragS));
2858           dummy_frag->fr_type = rs_fill;
2859           dummy_frag->line = listing_tail;
2860           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2861           dummy_frag->fr_symbol = symbolP;
2862         }
2863       else
2864 #endif
2865         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2866
2867 #ifdef OBJ_COFF
2868       /* "set" symbols are local unless otherwise specified.  */
2869       SF_SET_LOCAL (symbolP);
2870 #endif /* OBJ_COFF  */
2871     }                           /* Make a new symbol.  */
2872
2873   symbol_table_insert (symbolP);
2874
2875   * end_name = delim;
2876
2877   if (equiv
2878       && S_IS_DEFINED (symbolP)
2879       && S_GET_SEGMENT (symbolP) != reg_section)
2880     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2881
2882   pseudo_set (symbolP);
2883
2884   demand_empty_rest_of_line ();
2885
2886   /* XXX Now we come to the Thumb specific bit of code.  */
2887
2888   THUMB_SET_FUNC (symbolP, 1);
2889   ARM_SET_THUMB (symbolP, 1);
2890 #if defined OBJ_ELF || defined OBJ_COFF
2891   ARM_SET_INTERWORK (symbolP, support_interwork);
2892 #endif
2893 }
2894
2895 /* Directives: Mode selection.  */
2896
2897 /* .syntax [unified|divided] - choose the new unified syntax
2898    (same for Arm and Thumb encoding, modulo slight differences in what
2899    can be represented) or the old divergent syntax for each mode.  */
2900 static void
2901 s_syntax (int unused ATTRIBUTE_UNUSED)
2902 {
2903   char *name, delim;
2904
2905   name = input_line_pointer;
2906   delim = get_symbol_end ();
2907
2908   if (!strcasecmp (name, "unified"))
2909     unified_syntax = TRUE;
2910   else if (!strcasecmp (name, "divided"))
2911     unified_syntax = FALSE;
2912   else
2913     {
2914       as_bad (_("unrecognized syntax mode \"%s\""), name);
2915       return;
2916     }
2917   *input_line_pointer = delim;
2918   demand_empty_rest_of_line ();
2919 }
2920
2921 /* Directives: sectioning and alignment.  */
2922
2923 /* Same as s_align_ptwo but align 0 => align 2.  */
2924
2925 static void
2926 s_align (int unused ATTRIBUTE_UNUSED)
2927 {
2928   int temp;
2929   bfd_boolean fill_p;
2930   long temp_fill;
2931   long max_alignment = 15;
2932
2933   temp = get_absolute_expression ();
2934   if (temp > max_alignment)
2935     as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
2936   else if (temp < 0)
2937     {
2938       as_bad (_("alignment negative. 0 assumed."));
2939       temp = 0;
2940     }
2941
2942   if (*input_line_pointer == ',')
2943     {
2944       input_line_pointer++;
2945       temp_fill = get_absolute_expression ();
2946       fill_p = TRUE;
2947     }
2948   else
2949     {
2950       fill_p = FALSE;
2951       temp_fill = 0;
2952     }
2953
2954   if (!temp)
2955     temp = 2;
2956
2957   /* Only make a frag if we HAVE to.  */
2958   if (temp && !need_pass_2)
2959     {
2960       if (!fill_p && subseg_text_p (now_seg))
2961         frag_align_code (temp, 0);
2962       else
2963         frag_align (temp, (int) temp_fill, 0);
2964     }
2965   demand_empty_rest_of_line ();
2966
2967   record_alignment (now_seg, temp);
2968 }
2969
2970 static void
2971 s_bss (int ignore ATTRIBUTE_UNUSED)
2972 {
2973   /* We don't support putting frags in the BSS segment, we fake it by
2974      marking in_bss, then looking at s_skip for clues.  */
2975   subseg_set (bss_section, 0);
2976   demand_empty_rest_of_line ();
2977
2978 #ifdef md_elf_section_change_hook
2979   md_elf_section_change_hook ();
2980 #endif
2981 }
2982
2983 static void
2984 s_even (int ignore ATTRIBUTE_UNUSED)
2985 {
2986   /* Never make frag if expect extra pass.  */
2987   if (!need_pass_2)
2988     frag_align (1, 0, 0);
2989
2990   record_alignment (now_seg, 1);
2991
2992   demand_empty_rest_of_line ();
2993 }
2994
2995 /* Directives: Literal pools.  */
2996
2997 static literal_pool *
2998 find_literal_pool (void)
2999 {
3000   literal_pool * pool;
3001
3002   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3003     {
3004       if (pool->section == now_seg
3005           && pool->sub_section == now_subseg)
3006         break;
3007     }
3008
3009   return pool;
3010 }
3011
3012 static literal_pool *
3013 find_or_make_literal_pool (void)
3014 {
3015   /* Next literal pool ID number.  */
3016   static unsigned int latest_pool_num = 1;
3017   literal_pool *      pool;
3018
3019   pool = find_literal_pool ();
3020
3021   if (pool == NULL)
3022     {
3023       /* Create a new pool.  */
3024       pool = (literal_pool *) xmalloc (sizeof (* pool));
3025       if (! pool)
3026         return NULL;
3027
3028       pool->next_free_entry = 0;
3029       pool->section         = now_seg;
3030       pool->sub_section     = now_subseg;
3031       pool->next            = list_of_pools;
3032       pool->symbol          = NULL;
3033
3034       /* Add it to the list.  */
3035       list_of_pools = pool;
3036     }
3037
3038   /* New pools, and emptied pools, will have a NULL symbol.  */
3039   if (pool->symbol == NULL)
3040     {
3041       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3042                                     (valueT) 0, &zero_address_frag);
3043       pool->id = latest_pool_num ++;
3044     }
3045
3046   /* Done.  */
3047   return pool;
3048 }
3049
3050 /* Add the literal in the global 'inst'
3051    structure to the relevant literal pool.  */
3052
3053 static int
3054 add_to_lit_pool (void)
3055 {
3056   literal_pool * pool;
3057   unsigned int entry;
3058
3059   pool = find_or_make_literal_pool ();
3060
3061   /* Check if this literal value is already in the pool.  */
3062   for (entry = 0; entry < pool->next_free_entry; entry ++)
3063     {
3064       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3065           && (inst.reloc.exp.X_op == O_constant)
3066           && (pool->literals[entry].X_add_number
3067               == inst.reloc.exp.X_add_number)
3068           && (pool->literals[entry].X_unsigned
3069               == inst.reloc.exp.X_unsigned))
3070         break;
3071
3072       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3073           && (inst.reloc.exp.X_op == O_symbol)
3074           && (pool->literals[entry].X_add_number
3075               == inst.reloc.exp.X_add_number)
3076           && (pool->literals[entry].X_add_symbol
3077               == inst.reloc.exp.X_add_symbol)
3078           && (pool->literals[entry].X_op_symbol
3079               == inst.reloc.exp.X_op_symbol))
3080         break;
3081     }
3082
3083   /* Do we need to create a new entry?  */
3084   if (entry == pool->next_free_entry)
3085     {
3086       if (entry >= MAX_LITERAL_POOL_SIZE)
3087         {
3088           inst.error = _("literal pool overflow");
3089           return FAIL;
3090         }
3091
3092       pool->literals[entry] = inst.reloc.exp;
3093 #ifdef OBJ_ELF
3094       /* PR ld/12974: Record the location of the first source line to reference
3095          this entry in the literal pool.  If it turns out during linking that the
3096          symbol does not exist we will be able to give an accurate line number for
3097          the (first use of the) missing reference.  */
3098       if (debug_type == DEBUG_DWARF2)
3099         dwarf2_where (pool->locs + entry);
3100 #endif
3101       pool->next_free_entry += 1;
3102     }
3103
3104   inst.reloc.exp.X_op         = O_symbol;
3105   inst.reloc.exp.X_add_number = ((int) entry) * 4;
3106   inst.reloc.exp.X_add_symbol = pool->symbol;
3107
3108   return SUCCESS;
3109 }
3110
3111 /* Can't use symbol_new here, so have to create a symbol and then at
3112    a later date assign it a value. Thats what these functions do.  */
3113
3114 static void
3115 symbol_locate (symbolS *    symbolP,
3116                const char * name,       /* It is copied, the caller can modify.  */
3117                segT         segment,    /* Segment identifier (SEG_<something>).  */
3118                valueT       valu,       /* Symbol value.  */
3119                fragS *      frag)       /* Associated fragment.  */
3120 {
3121   unsigned int name_length;
3122   char * preserved_copy_of_name;
3123
3124   name_length = strlen (name) + 1;   /* +1 for \0.  */
3125   obstack_grow (&notes, name, name_length);
3126   preserved_copy_of_name = (char *) obstack_finish (&notes);
3127
3128 #ifdef tc_canonicalize_symbol_name
3129   preserved_copy_of_name =
3130     tc_canonicalize_symbol_name (preserved_copy_of_name);
3131 #endif
3132
3133   S_SET_NAME (symbolP, preserved_copy_of_name);
3134
3135   S_SET_SEGMENT (symbolP, segment);
3136   S_SET_VALUE (symbolP, valu);
3137   symbol_clear_list_pointers (symbolP);
3138
3139   symbol_set_frag (symbolP, frag);
3140
3141   /* Link to end of symbol chain.  */
3142   {
3143     extern int symbol_table_frozen;
3144
3145     if (symbol_table_frozen)
3146       abort ();
3147   }
3148
3149   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3150
3151   obj_symbol_new_hook (symbolP);
3152
3153 #ifdef tc_symbol_new_hook
3154   tc_symbol_new_hook (symbolP);
3155 #endif
3156
3157 #ifdef DEBUG_SYMS
3158   verify_symbol_chain (symbol_rootP, symbol_lastP);
3159 #endif /* DEBUG_SYMS  */
3160 }
3161
3162
3163 static void
3164 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3165 {
3166   unsigned int entry;
3167   literal_pool * pool;
3168   char sym_name[20];
3169
3170   pool = find_literal_pool ();
3171   if (pool == NULL
3172       || pool->symbol == NULL
3173       || pool->next_free_entry == 0)
3174     return;
3175
3176   mapping_state (MAP_DATA);
3177
3178   /* Align pool as you have word accesses.
3179      Only make a frag if we have to.  */
3180   if (!need_pass_2)
3181     frag_align (2, 0, 0);
3182
3183   record_alignment (now_seg, 2);
3184
3185   sprintf (sym_name, "$$lit_\002%x", pool->id);
3186
3187   symbol_locate (pool->symbol, sym_name, now_seg,
3188                  (valueT) frag_now_fix (), frag_now);
3189   symbol_table_insert (pool->symbol);
3190
3191   ARM_SET_THUMB (pool->symbol, thumb_mode);
3192
3193 #if defined OBJ_COFF || defined OBJ_ELF
3194   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3195 #endif
3196
3197   for (entry = 0; entry < pool->next_free_entry; entry ++)
3198     {
3199 #ifdef OBJ_ELF
3200       if (debug_type == DEBUG_DWARF2)
3201         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3202 #endif
3203       /* First output the expression in the instruction to the pool.  */
3204       emit_expr (&(pool->literals[entry]), 4); /* .word  */
3205     }
3206
3207   /* Mark the pool as empty.  */
3208   pool->next_free_entry = 0;
3209   pool->symbol = NULL;
3210 }
3211
3212 #ifdef OBJ_ELF
3213 /* Forward declarations for functions below, in the MD interface
3214    section.  */
3215 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3216 static valueT create_unwind_entry (int);
3217 static void start_unwind_section (const segT, int);
3218 static void add_unwind_opcode (valueT, int);
3219 static void flush_pending_unwind (void);
3220
3221 /* Directives: Data.  */
3222
3223 static void
3224 s_arm_elf_cons (int nbytes)
3225 {
3226   expressionS exp;
3227
3228 #ifdef md_flush_pending_output
3229   md_flush_pending_output ();
3230 #endif
3231
3232   if (is_it_end_of_statement ())
3233     {
3234       demand_empty_rest_of_line ();
3235       return;
3236     }
3237
3238 #ifdef md_cons_align
3239   md_cons_align (nbytes);
3240 #endif
3241
3242   mapping_state (MAP_DATA);
3243   do
3244     {
3245       int reloc;
3246       char *base = input_line_pointer;
3247
3248       expression (& exp);
3249
3250       if (exp.X_op != O_symbol)
3251         emit_expr (&exp, (unsigned int) nbytes);
3252       else
3253         {
3254           char *before_reloc = input_line_pointer;
3255           reloc = parse_reloc (&input_line_pointer);
3256           if (reloc == -1)
3257             {
3258               as_bad (_("unrecognized relocation suffix"));
3259               ignore_rest_of_line ();
3260               return;
3261             }
3262           else if (reloc == BFD_RELOC_UNUSED)
3263             emit_expr (&exp, (unsigned int) nbytes);
3264           else
3265             {
3266               reloc_howto_type *howto = (reloc_howto_type *)
3267                   bfd_reloc_type_lookup (stdoutput,
3268                                          (bfd_reloc_code_real_type) reloc);
3269               int size = bfd_get_reloc_size (howto);
3270
3271               if (reloc == BFD_RELOC_ARM_PLT32)
3272                 {
3273                   as_bad (_("(plt) is only valid on branch targets"));
3274                   reloc = BFD_RELOC_UNUSED;
3275                   size = 0;
3276                 }
3277
3278               if (size > nbytes)
3279                 as_bad (_("%s relocations do not fit in %d bytes"),
3280                         howto->name, nbytes);
3281               else
3282                 {
3283                   /* We've parsed an expression stopping at O_symbol.
3284                      But there may be more expression left now that we
3285                      have parsed the relocation marker.  Parse it again.
3286                      XXX Surely there is a cleaner way to do this.  */
3287                   char *p = input_line_pointer;
3288                   int offset;
3289                   char *save_buf = (char *) alloca (input_line_pointer - base);
3290                   memcpy (save_buf, base, input_line_pointer - base);
3291                   memmove (base + (input_line_pointer - before_reloc),
3292                            base, before_reloc - base);
3293
3294                   input_line_pointer = base + (input_line_pointer-before_reloc);
3295                   expression (&exp);
3296                   memcpy (base, save_buf, p - base);
3297
3298                   offset = nbytes - size;
3299                   p = frag_more ((int) nbytes);
3300                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3301                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3302                 }
3303             }
3304         }
3305     }
3306   while (*input_line_pointer++ == ',');
3307
3308   /* Put terminator back into stream.  */
3309   input_line_pointer --;
3310   demand_empty_rest_of_line ();
3311 }
3312
3313 /* Emit an expression containing a 32-bit thumb instruction.
3314    Implementation based on put_thumb32_insn.  */
3315
3316 static void
3317 emit_thumb32_expr (expressionS * exp)
3318 {
3319   expressionS exp_high = *exp;
3320
3321   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3322   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3323   exp->X_add_number &= 0xffff;
3324   emit_expr (exp, (unsigned int) THUMB_SIZE);
3325 }
3326
3327 /*  Guess the instruction size based on the opcode.  */
3328
3329 static int
3330 thumb_insn_size (int opcode)
3331 {
3332   if ((unsigned int) opcode < 0xe800u)
3333     return 2;
3334   else if ((unsigned int) opcode >= 0xe8000000u)
3335     return 4;
3336   else
3337     return 0;
3338 }
3339
3340 static bfd_boolean
3341 emit_insn (expressionS *exp, int nbytes)
3342 {
3343   int size = 0;
3344
3345   if (exp->X_op == O_constant)
3346     {
3347       size = nbytes;
3348
3349       if (size == 0)
3350         size = thumb_insn_size (exp->X_add_number);
3351
3352       if (size != 0)
3353         {
3354           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3355             {
3356               as_bad (_(".inst.n operand too big. "\
3357                         "Use .inst.w instead"));
3358               size = 0;
3359             }
3360           else
3361             {
3362               if (now_it.state == AUTOMATIC_IT_BLOCK)
3363                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3364               else
3365                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3366
3367               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3368                 emit_thumb32_expr (exp);
3369               else
3370                 emit_expr (exp, (unsigned int) size);
3371
3372               it_fsm_post_encode ();
3373             }
3374         }
3375       else
3376         as_bad (_("cannot determine Thumb instruction size. "   \
3377                   "Use .inst.n/.inst.w instead"));
3378     }
3379   else
3380     as_bad (_("constant expression required"));
3381
3382   return (size != 0);
3383 }
3384
3385 /* Like s_arm_elf_cons but do not use md_cons_align and
3386    set the mapping state to MAP_ARM/MAP_THUMB.  */
3387
3388 static void
3389 s_arm_elf_inst (int nbytes)
3390 {
3391   if (is_it_end_of_statement ())
3392     {
3393       demand_empty_rest_of_line ();
3394       return;
3395     }
3396
3397   /* Calling mapping_state () here will not change ARM/THUMB,
3398      but will ensure not to be in DATA state.  */
3399
3400   if (thumb_mode)
3401     mapping_state (MAP_THUMB);
3402   else
3403     {
3404       if (nbytes != 0)
3405         {
3406           as_bad (_("width suffixes are invalid in ARM mode"));
3407           ignore_rest_of_line ();
3408           return;
3409         }
3410
3411       nbytes = 4;
3412
3413       mapping_state (MAP_ARM);
3414     }
3415
3416   do
3417     {
3418       expressionS exp;
3419
3420       expression (& exp);
3421
3422       if (! emit_insn (& exp, nbytes))
3423         {
3424           ignore_rest_of_line ();
3425           return;
3426         }
3427     }
3428   while (*input_line_pointer++ == ',');
3429
3430   /* Put terminator back into stream.  */
3431   input_line_pointer --;
3432   demand_empty_rest_of_line ();
3433 }
3434
3435 /* Parse a .rel31 directive.  */
3436
3437 static void
3438 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3439 {
3440   expressionS exp;
3441   char *p;
3442   valueT highbit;
3443
3444   highbit = 0;
3445   if (*input_line_pointer == '1')
3446     highbit = 0x80000000;
3447   else if (*input_line_pointer != '0')
3448     as_bad (_("expected 0 or 1"));
3449
3450   input_line_pointer++;
3451   if (*input_line_pointer != ',')
3452     as_bad (_("missing comma"));
3453   input_line_pointer++;
3454
3455 #ifdef md_flush_pending_output
3456   md_flush_pending_output ();
3457 #endif
3458
3459 #ifdef md_cons_align
3460   md_cons_align (4);
3461 #endif
3462
3463   mapping_state (MAP_DATA);
3464
3465   expression (&exp);
3466
3467   p = frag_more (4);
3468   md_number_to_chars (p, highbit, 4);
3469   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3470                BFD_RELOC_ARM_PREL31);
3471
3472   demand_empty_rest_of_line ();
3473 }
3474
3475 /* Directives: AEABI stack-unwind tables.  */
3476
3477 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3478
3479 static void
3480 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3481 {
3482   demand_empty_rest_of_line ();
3483   if (unwind.proc_start)
3484     {
3485       as_bad (_("duplicate .fnstart directive"));
3486       return;
3487     }
3488
3489   /* Mark the start of the function.  */
3490   unwind.proc_start = expr_build_dot ();
3491
3492   /* Reset the rest of the unwind info.  */
3493   unwind.opcode_count = 0;
3494   unwind.table_entry = NULL;
3495   unwind.personality_routine = NULL;
3496   unwind.personality_index = -1;
3497   unwind.frame_size = 0;
3498   unwind.fp_offset = 0;
3499   unwind.fp_reg = REG_SP;
3500   unwind.fp_used = 0;
3501   unwind.sp_restored = 0;
3502 }
3503
3504
3505 /* Parse a handlerdata directive.  Creates the exception handling table entry
3506    for the function.  */
3507
3508 static void
3509 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3510 {
3511   demand_empty_rest_of_line ();
3512   if (!unwind.proc_start)
3513     as_bad (MISSING_FNSTART);
3514
3515   if (unwind.table_entry)
3516     as_bad (_("duplicate .handlerdata directive"));
3517
3518   create_unwind_entry (1);
3519 }
3520
3521 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3522
3523 static void
3524 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3525 {
3526   long where;
3527   char *ptr;
3528   valueT val;
3529   unsigned int marked_pr_dependency;
3530
3531   demand_empty_rest_of_line ();
3532
3533   if (!unwind.proc_start)
3534     {
3535       as_bad (_(".fnend directive without .fnstart"));
3536       return;
3537     }
3538
3539   /* Add eh table entry.  */
3540   if (unwind.table_entry == NULL)
3541     val = create_unwind_entry (0);
3542   else
3543     val = 0;
3544
3545   /* Add index table entry.  This is two words.  */
3546   start_unwind_section (unwind.saved_seg, 1);
3547   frag_align (2, 0, 0);
3548   record_alignment (now_seg, 2);
3549
3550   ptr = frag_more (8);
3551   memset (ptr, 0, 8);
3552   where = frag_now_fix () - 8;
3553
3554   /* Self relative offset of the function start.  */
3555   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3556            BFD_RELOC_ARM_PREL31);
3557
3558   /* Indicate dependency on EHABI-defined personality routines to the
3559      linker, if it hasn't been done already.  */
3560   marked_pr_dependency
3561     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3562   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3563       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3564     {
3565       static const char *const name[] =
3566         {
3567           "__aeabi_unwind_cpp_pr0",
3568           "__aeabi_unwind_cpp_pr1",
3569           "__aeabi_unwind_cpp_pr2"
3570         };
3571       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3572       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3573       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3574         |= 1 << unwind.personality_index;
3575     }
3576
3577   if (val)
3578     /* Inline exception table entry.  */
3579     md_number_to_chars (ptr + 4, val, 4);
3580   else
3581     /* Self relative offset of the table entry.  */
3582     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3583              BFD_RELOC_ARM_PREL31);
3584
3585   /* Restore the original section.  */
3586   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3587
3588   unwind.proc_start = NULL;
3589 }
3590
3591
3592 /* Parse an unwind_cantunwind directive.  */
3593
3594 static void
3595 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3596 {
3597   demand_empty_rest_of_line ();
3598   if (!unwind.proc_start)
3599     as_bad (MISSING_FNSTART);
3600
3601   if (unwind.personality_routine || unwind.personality_index != -1)
3602     as_bad (_("personality routine specified for cantunwind frame"));
3603
3604   unwind.personality_index = -2;
3605 }
3606
3607
3608 /* Parse a personalityindex directive.  */
3609
3610 static void
3611 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3612 {
3613   expressionS exp;
3614
3615   if (!unwind.proc_start)
3616     as_bad (MISSING_FNSTART);
3617
3618   if (unwind.personality_routine || unwind.personality_index != -1)
3619     as_bad (_("duplicate .personalityindex directive"));
3620
3621   expression (&exp);
3622
3623   if (exp.X_op != O_constant
3624       || exp.X_add_number < 0 || exp.X_add_number > 15)
3625     {
3626       as_bad (_("bad personality routine number"));
3627       ignore_rest_of_line ();
3628       return;
3629     }
3630
3631   unwind.personality_index = exp.X_add_number;
3632
3633   demand_empty_rest_of_line ();
3634 }
3635
3636
3637 /* Parse a personality directive.  */
3638
3639 static void
3640 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3641 {
3642   char *name, *p, c;
3643
3644   if (!unwind.proc_start)
3645     as_bad (MISSING_FNSTART);
3646
3647   if (unwind.personality_routine || unwind.personality_index != -1)
3648     as_bad (_("duplicate .personality directive"));
3649
3650   name = input_line_pointer;
3651   c = get_symbol_end ();
3652   p = input_line_pointer;
3653   unwind.personality_routine = symbol_find_or_make (name);
3654   *p = c;
3655   demand_empty_rest_of_line ();
3656 }
3657
3658
3659 /* Parse a directive saving core registers.  */
3660
3661 static void
3662 s_arm_unwind_save_core (void)
3663 {
3664   valueT op;
3665   long range;
3666   int n;
3667
3668   range = parse_reg_list (&input_line_pointer);
3669   if (range == FAIL)
3670     {
3671       as_bad (_("expected register list"));
3672       ignore_rest_of_line ();
3673       return;
3674     }
3675
3676   demand_empty_rest_of_line ();
3677
3678   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3679      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3680      ip because it is clobbered by calls.  */
3681   if (unwind.sp_restored && unwind.fp_reg == 12
3682       && (range & 0x3000) == 0x1000)
3683     {
3684       unwind.opcode_count--;
3685       unwind.sp_restored = 0;
3686       range = (range | 0x2000) & ~0x1000;
3687       unwind.pending_offset = 0;
3688     }
3689
3690   /* Pop r4-r15.  */
3691   if (range & 0xfff0)
3692     {
3693       /* See if we can use the short opcodes.  These pop a block of up to 8
3694          registers starting with r4, plus maybe r14.  */
3695       for (n = 0; n < 8; n++)
3696         {
3697           /* Break at the first non-saved register.      */
3698           if ((range & (1 << (n + 4))) == 0)
3699             break;
3700         }
3701       /* See if there are any other bits set.  */
3702       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
3703         {
3704           /* Use the long form.  */
3705           op = 0x8000 | ((range >> 4) & 0xfff);
3706           add_unwind_opcode (op, 2);
3707         }
3708       else
3709         {
3710           /* Use the short form.  */
3711           if (range & 0x4000)
3712             op = 0xa8; /* Pop r14.      */
3713           else
3714             op = 0xa0; /* Do not pop r14.  */
3715           op |= (n - 1);
3716           add_unwind_opcode (op, 1);
3717         }
3718     }
3719
3720   /* Pop r0-r3.  */
3721   if (range & 0xf)
3722     {
3723       op = 0xb100 | (range & 0xf);
3724       add_unwind_opcode (op, 2);
3725     }
3726
3727   /* Record the number of bytes pushed.  */
3728   for (n = 0; n < 16; n++)
3729     {
3730       if (range & (1 << n))
3731         unwind.frame_size += 4;
3732     }
3733 }
3734
3735
3736 /* Parse a directive saving FPA registers.  */
3737
3738 static void
3739 s_arm_unwind_save_fpa (int reg)
3740 {
3741   expressionS exp;
3742   int num_regs;
3743   valueT op;
3744
3745   /* Get Number of registers to transfer.  */
3746   if (skip_past_comma (&input_line_pointer) != FAIL)
3747     expression (&exp);
3748   else
3749     exp.X_op = O_illegal;
3750
3751   if (exp.X_op != O_constant)
3752     {
3753       as_bad (_("expected , <constant>"));
3754       ignore_rest_of_line ();
3755       return;
3756     }
3757
3758   num_regs = exp.X_add_number;
3759
3760   if (num_regs < 1 || num_regs > 4)
3761     {
3762       as_bad (_("number of registers must be in the range [1:4]"));
3763       ignore_rest_of_line ();
3764       return;
3765     }
3766
3767   demand_empty_rest_of_line ();
3768
3769   if (reg == 4)
3770     {
3771       /* Short form.  */
3772       op = 0xb4 | (num_regs - 1);
3773       add_unwind_opcode (op, 1);
3774     }
3775   else
3776     {
3777       /* Long form.  */
3778       op = 0xc800 | (reg << 4) | (num_regs - 1);
3779       add_unwind_opcode (op, 2);
3780     }
3781   unwind.frame_size += num_regs * 12;
3782 }
3783
3784
3785 /* Parse a directive saving VFP registers for ARMv6 and above.  */
3786
3787 static void
3788 s_arm_unwind_save_vfp_armv6 (void)
3789 {
3790   int count;
3791   unsigned int start;
3792   valueT op;
3793   int num_vfpv3_regs = 0;
3794   int num_regs_below_16;
3795
3796   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
3797   if (count == FAIL)
3798     {
3799       as_bad (_("expected register list"));
3800       ignore_rest_of_line ();
3801       return;
3802     }
3803
3804   demand_empty_rest_of_line ();
3805
3806   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
3807      than FSTMX/FLDMX-style ones).  */
3808
3809   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
3810   if (start >= 16)
3811     num_vfpv3_regs = count;
3812   else if (start + count > 16)
3813     num_vfpv3_regs = start + count - 16;
3814
3815   if (num_vfpv3_regs > 0)
3816     {
3817       int start_offset = start > 16 ? start - 16 : 0;
3818       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
3819       add_unwind_opcode (op, 2);
3820     }
3821
3822   /* Generate opcode for registers numbered in the range 0 .. 15.  */
3823   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
3824   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
3825   if (num_regs_below_16 > 0)
3826     {
3827       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
3828       add_unwind_opcode (op, 2);
3829     }
3830
3831   unwind.frame_size += count * 8;
3832 }
3833
3834
3835 /* Parse a directive saving VFP registers for pre-ARMv6.  */
3836
3837 static void
3838 s_arm_unwind_save_vfp (void)
3839 {
3840   int count;
3841   unsigned int reg;
3842   valueT op;
3843
3844   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
3845   if (count == FAIL)
3846     {
3847       as_bad (_("expected register list"));
3848       ignore_rest_of_line ();
3849       return;
3850     }
3851
3852   demand_empty_rest_of_line ();
3853
3854   if (reg == 8)
3855     {
3856       /* Short form.  */
3857       op = 0xb8 | (count - 1);
3858       add_unwind_opcode (op, 1);
3859     }
3860   else
3861     {
3862       /* Long form.  */
3863       op = 0xb300 | (reg << 4) | (count - 1);
3864       add_unwind_opcode (op, 2);
3865     }
3866   unwind.frame_size += count * 8 + 4;
3867 }
3868
3869
3870 /* Parse a directive saving iWMMXt data registers.  */
3871
3872 static void
3873 s_arm_unwind_save_mmxwr (void)
3874 {
3875   int reg;
3876   int hi_reg;
3877   int i;
3878   unsigned mask = 0;
3879   valueT op;
3880
3881   if (*input_line_pointer == '{')
3882     input_line_pointer++;
3883
3884   do
3885     {
3886       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3887
3888       if (reg == FAIL)
3889         {
3890           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
3891           goto error;
3892         }
3893
3894       if (mask >> reg)
3895         as_tsktsk (_("register list not in ascending order"));
3896       mask |= 1 << reg;
3897
3898       if (*input_line_pointer == '-')
3899         {
3900           input_line_pointer++;
3901           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3902           if (hi_reg == FAIL)
3903             {
3904               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
3905               goto error;
3906             }
3907           else if (reg >= hi_reg)
3908             {
3909               as_bad (_("bad register range"));
3910               goto error;
3911             }
3912           for (; reg < hi_reg; reg++)
3913             mask |= 1 << reg;
3914         }
3915     }
3916   while (skip_past_comma (&input_line_pointer) != FAIL);
3917
3918   if (*input_line_pointer == '}')
3919     input_line_pointer++;
3920
3921   demand_empty_rest_of_line ();
3922
3923   /* Generate any deferred opcodes because we're going to be looking at
3924      the list.  */
3925   flush_pending_unwind ();
3926
3927   for (i = 0; i < 16; i++)
3928     {
3929       if (mask & (1 << i))
3930         unwind.frame_size += 8;
3931     }
3932
3933   /* Attempt to combine with a previous opcode.  We do this because gcc
3934      likes to output separate unwind directives for a single block of
3935      registers.  */
3936   if (unwind.opcode_count > 0)
3937     {
3938       i = unwind.opcodes[unwind.opcode_count - 1];
3939       if ((i & 0xf8) == 0xc0)
3940         {
3941           i &= 7;
3942           /* Only merge if the blocks are contiguous.  */
3943           if (i < 6)
3944             {
3945               if ((mask & 0xfe00) == (1 << 9))
3946                 {
3947                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
3948                   unwind.opcode_count--;
3949                 }
3950             }
3951           else if (i == 6 && unwind.opcode_count >= 2)
3952             {
3953               i = unwind.opcodes[unwind.opcode_count - 2];
3954               reg = i >> 4;
3955               i &= 0xf;
3956
3957               op = 0xffff << (reg - 1);
3958               if (reg > 0
3959                   && ((mask & op) == (1u << (reg - 1))))
3960                 {
3961                   op = (1 << (reg + i + 1)) - 1;
3962                   op &= ~((1 << reg) - 1);
3963                   mask |= op;
3964                   unwind.opcode_count -= 2;
3965                 }
3966             }
3967         }
3968     }
3969
3970   hi_reg = 15;
3971   /* We want to generate opcodes in the order the registers have been
3972      saved, ie. descending order.  */
3973   for (reg = 15; reg >= -1; reg--)
3974     {
3975       /* Save registers in blocks.  */
3976       if (reg < 0
3977           || !(mask & (1 << reg)))
3978         {
3979           /* We found an unsaved reg.  Generate opcodes to save the
3980              preceding block.   */
3981           if (reg != hi_reg)
3982             {
3983               if (reg == 9)
3984                 {
3985                   /* Short form.  */
3986                   op = 0xc0 | (hi_reg - 10);
3987                   add_unwind_opcode (op, 1);
3988                 }
3989               else
3990                 {
3991                   /* Long form.  */
3992                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
3993                   add_unwind_opcode (op, 2);
3994                 }
3995             }
3996           hi_reg = reg - 1;
3997         }
3998     }
3999
4000   return;
4001 error:
4002   ignore_rest_of_line ();
4003 }
4004
4005 static void
4006 s_arm_unwind_save_mmxwcg (void)
4007 {
4008   int reg;
4009   int hi_reg;
4010   unsigned mask = 0;
4011   valueT op;
4012
4013   if (*input_line_pointer == '{')
4014     input_line_pointer++;
4015
4016   do
4017     {
4018       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4019
4020       if (reg == FAIL)
4021         {
4022           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4023           goto error;
4024         }
4025
4026       reg -= 8;
4027       if (mask >> reg)
4028         as_tsktsk (_("register list not in ascending order"));
4029       mask |= 1 << reg;
4030
4031       if (*input_line_pointer == '-')
4032         {
4033           input_line_pointer++;
4034           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4035           if (hi_reg == FAIL)
4036             {
4037               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4038               goto error;
4039             }
4040           else if (reg >= hi_reg)
4041             {
4042               as_bad (_("bad register range"));
4043               goto error;
4044             }
4045           for (; reg < hi_reg; reg++)
4046             mask |= 1 << reg;
4047         }
4048     }
4049   while (skip_past_comma (&input_line_pointer) != FAIL);
4050
4051   if (*input_line_pointer == '}')
4052     input_line_pointer++;
4053
4054   demand_empty_rest_of_line ();
4055
4056   /* Generate any deferred opcodes because we're going to be looking at
4057      the list.  */
4058   flush_pending_unwind ();
4059
4060   for (reg = 0; reg < 16; reg++)
4061     {
4062       if (mask & (1 << reg))
4063         unwind.frame_size += 4;
4064     }
4065   op = 0xc700 | mask;
4066   add_unwind_opcode (op, 2);
4067   return;
4068 error:
4069   ignore_rest_of_line ();
4070 }
4071
4072
4073 /* Parse an unwind_save directive.
4074    If the argument is non-zero, this is a .vsave directive.  */
4075
4076 static void
4077 s_arm_unwind_save (int arch_v6)
4078 {
4079   char *peek;
4080   struct reg_entry *reg;
4081   bfd_boolean had_brace = FALSE;
4082
4083   if (!unwind.proc_start)
4084     as_bad (MISSING_FNSTART);
4085
4086   /* Figure out what sort of save we have.  */
4087   peek = input_line_pointer;
4088
4089   if (*peek == '{')
4090     {
4091       had_brace = TRUE;
4092       peek++;
4093     }
4094
4095   reg = arm_reg_parse_multi (&peek);
4096
4097   if (!reg)
4098     {
4099       as_bad (_("register expected"));
4100       ignore_rest_of_line ();
4101       return;
4102     }
4103
4104   switch (reg->type)
4105     {
4106     case REG_TYPE_FN:
4107       if (had_brace)
4108         {
4109           as_bad (_("FPA .unwind_save does not take a register list"));
4110           ignore_rest_of_line ();
4111           return;
4112         }
4113       input_line_pointer = peek;
4114       s_arm_unwind_save_fpa (reg->number);
4115       return;
4116
4117     case REG_TYPE_RN:     s_arm_unwind_save_core ();   return;
4118     case REG_TYPE_VFD:
4119       if (arch_v6)
4120         s_arm_unwind_save_vfp_armv6 ();
4121       else
4122         s_arm_unwind_save_vfp ();
4123       return;
4124     case REG_TYPE_MMXWR:  s_arm_unwind_save_mmxwr ();  return;
4125     case REG_TYPE_MMXWCG: s_arm_unwind_save_mmxwcg (); return;
4126
4127     default:
4128       as_bad (_(".unwind_save does not support this kind of register"));
4129       ignore_rest_of_line ();
4130     }
4131 }
4132
4133
4134 /* Parse an unwind_movsp directive.  */
4135
4136 static void
4137 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4138 {
4139   int reg;
4140   valueT op;
4141   int offset;
4142
4143   if (!unwind.proc_start)
4144     as_bad (MISSING_FNSTART);
4145
4146   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4147   if (reg == FAIL)
4148     {
4149       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4150       ignore_rest_of_line ();
4151       return;
4152     }
4153
4154   /* Optional constant.  */
4155   if (skip_past_comma (&input_line_pointer) != FAIL)
4156     {
4157       if (immediate_for_directive (&offset) == FAIL)
4158         return;
4159     }
4160   else
4161     offset = 0;
4162
4163   demand_empty_rest_of_line ();
4164
4165   if (reg == REG_SP || reg == REG_PC)
4166     {
4167       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4168       return;
4169     }
4170
4171   if (unwind.fp_reg != REG_SP)
4172     as_bad (_("unexpected .unwind_movsp directive"));
4173
4174   /* Generate opcode to restore the value.  */
4175   op = 0x90 | reg;
4176   add_unwind_opcode (op, 1);
4177
4178   /* Record the information for later.  */
4179   unwind.fp_reg = reg;
4180   unwind.fp_offset = unwind.frame_size - offset;
4181   unwind.sp_restored = 1;
4182 }
4183
4184 /* Parse an unwind_pad directive.  */
4185
4186 static void
4187 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4188 {
4189   int offset;
4190
4191   if (!unwind.proc_start)
4192     as_bad (MISSING_FNSTART);
4193
4194   if (immediate_for_directive (&offset) == FAIL)
4195     return;
4196
4197   if (offset & 3)
4198     {
4199       as_bad (_("stack increment must be multiple of 4"));
4200       ignore_rest_of_line ();
4201       return;
4202     }
4203
4204   /* Don't generate any opcodes, just record the details for later.  */
4205   unwind.frame_size += offset;
4206   unwind.pending_offset += offset;
4207
4208   demand_empty_rest_of_line ();
4209 }
4210
4211 /* Parse an unwind_setfp directive.  */
4212
4213 static void
4214 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4215 {
4216   int sp_reg;
4217   int fp_reg;
4218   int offset;
4219
4220   if (!unwind.proc_start)
4221     as_bad (MISSING_FNSTART);
4222
4223   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4224   if (skip_past_comma (&input_line_pointer) == FAIL)
4225     sp_reg = FAIL;
4226   else
4227     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4228
4229   if (fp_reg == FAIL || sp_reg == FAIL)
4230     {
4231       as_bad (_("expected <reg>, <reg>"));
4232       ignore_rest_of_line ();
4233       return;
4234     }
4235
4236   /* Optional constant.  */
4237   if (skip_past_comma (&input_line_pointer) != FAIL)
4238     {
4239       if (immediate_for_directive (&offset) == FAIL)
4240         return;
4241     }
4242   else
4243     offset = 0;
4244
4245   demand_empty_rest_of_line ();
4246
4247   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4248     {
4249       as_bad (_("register must be either sp or set by a previous"
4250                 "unwind_movsp directive"));
4251       return;
4252     }
4253
4254   /* Don't generate any opcodes, just record the information for later.  */
4255   unwind.fp_reg = fp_reg;
4256   unwind.fp_used = 1;
4257   if (sp_reg == REG_SP)
4258     unwind.fp_offset = unwind.frame_size - offset;
4259   else
4260     unwind.fp_offset -= offset;
4261 }
4262
4263 /* Parse an unwind_raw directive.  */
4264
4265 static void
4266 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4267 {
4268   expressionS exp;
4269   /* This is an arbitrary limit.         */
4270   unsigned char op[16];
4271   int count;
4272
4273   if (!unwind.proc_start)
4274     as_bad (MISSING_FNSTART);
4275
4276   expression (&exp);
4277   if (exp.X_op == O_constant
4278       && skip_past_comma (&input_line_pointer) != FAIL)
4279     {
4280       unwind.frame_size += exp.X_add_number;
4281       expression (&exp);
4282     }
4283   else
4284     exp.X_op = O_illegal;
4285
4286   if (exp.X_op != O_constant)
4287     {
4288       as_bad (_("expected <offset>, <opcode>"));
4289       ignore_rest_of_line ();
4290       return;
4291     }
4292
4293   count = 0;
4294
4295   /* Parse the opcode.  */
4296   for (;;)
4297     {
4298       if (count >= 16)
4299         {
4300           as_bad (_("unwind opcode too long"));
4301           ignore_rest_of_line ();
4302         }
4303       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4304         {
4305           as_bad (_("invalid unwind opcode"));
4306           ignore_rest_of_line ();
4307           return;
4308         }
4309       op[count++] = exp.X_add_number;
4310
4311       /* Parse the next byte.  */
4312       if (skip_past_comma (&input_line_pointer) == FAIL)
4313         break;
4314
4315       expression (&exp);
4316     }
4317
4318   /* Add the opcode bytes in reverse order.  */
4319   while (count--)
4320     add_unwind_opcode (op[count], 1);
4321
4322   demand_empty_rest_of_line ();
4323 }
4324
4325
4326 /* Parse a .eabi_attribute directive.  */
4327
4328 static void
4329 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4330 {
4331   int tag = s_vendor_attribute (OBJ_ATTR_PROC);
4332
4333   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4334     attributes_set_explicitly[tag] = 1;
4335 }
4336
4337 /* Emit a tls fix for the symbol.  */
4338
4339 static void
4340 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4341 {
4342   char *p;
4343   expressionS exp;
4344 #ifdef md_flush_pending_output
4345   md_flush_pending_output ();
4346 #endif
4347
4348 #ifdef md_cons_align
4349   md_cons_align (4);
4350 #endif
4351
4352   /* Since we're just labelling the code, there's no need to define a
4353      mapping symbol.  */
4354   expression (&exp);
4355   p = obstack_next_free (&frchain_now->frch_obstack);
4356   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4357                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4358                : BFD_RELOC_ARM_TLS_DESCSEQ);
4359 }
4360 #endif /* OBJ_ELF */
4361
4362 static void s_arm_arch (int);
4363 static void s_arm_object_arch (int);
4364 static void s_arm_cpu (int);
4365 static void s_arm_fpu (int);
4366 static void s_arm_arch_extension (int);
4367
4368 #ifdef TE_PE
4369
4370 static void
4371 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4372 {
4373   expressionS exp;
4374
4375   do
4376     {
4377       expression (&exp);
4378       if (exp.X_op == O_symbol)
4379         exp.X_op = O_secrel;
4380
4381       emit_expr (&exp, 4);
4382     }
4383   while (*input_line_pointer++ == ',');
4384
4385   input_line_pointer--;
4386   demand_empty_rest_of_line ();
4387 }
4388 #endif /* TE_PE */
4389
4390 /* This table describes all the machine specific pseudo-ops the assembler
4391    has to support.  The fields are:
4392      pseudo-op name without dot
4393      function to call to execute this pseudo-op
4394      Integer arg to pass to the function.  */
4395
4396 const pseudo_typeS md_pseudo_table[] =
4397 {
4398   /* Never called because '.req' does not start a line.  */
4399   { "req",         s_req,         0 },
4400   /* Following two are likewise never called.  */
4401   { "dn",          s_dn,          0 },
4402   { "qn",          s_qn,          0 },
4403   { "unreq",       s_unreq,       0 },
4404   { "bss",         s_bss,         0 },
4405   { "align",       s_align,       0 },
4406   { "arm",         s_arm,         0 },
4407   { "thumb",       s_thumb,       0 },
4408   { "code",        s_code,        0 },
4409   { "force_thumb", s_force_thumb, 0 },
4410   { "thumb_func",  s_thumb_func,  0 },
4411   { "thumb_set",   s_thumb_set,   0 },
4412   { "even",        s_even,        0 },
4413   { "ltorg",       s_ltorg,       0 },
4414   { "pool",        s_ltorg,       0 },
4415   { "syntax",      s_syntax,      0 },
4416   { "cpu",         s_arm_cpu,     0 },
4417   { "arch",        s_arm_arch,    0 },
4418   { "object_arch", s_arm_object_arch,   0 },
4419   { "fpu",         s_arm_fpu,     0 },
4420   { "arch_extension", s_arm_arch_extension, 0 },
4421 #ifdef OBJ_ELF
4422   { "word",             s_arm_elf_cons, 4 },
4423   { "long",             s_arm_elf_cons, 4 },
4424   { "inst.n",           s_arm_elf_inst, 2 },
4425   { "inst.w",           s_arm_elf_inst, 4 },
4426   { "inst",             s_arm_elf_inst, 0 },
4427   { "rel31",            s_arm_rel31,      0 },
4428   { "fnstart",          s_arm_unwind_fnstart,   0 },
4429   { "fnend",            s_arm_unwind_fnend,     0 },
4430   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4431   { "personality",      s_arm_unwind_personality, 0 },
4432   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4433   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4434   { "save",             s_arm_unwind_save,      0 },
4435   { "vsave",            s_arm_unwind_save,      1 },
4436   { "movsp",            s_arm_unwind_movsp,     0 },
4437   { "pad",              s_arm_unwind_pad,       0 },
4438   { "setfp",            s_arm_unwind_setfp,     0 },
4439   { "unwind_raw",       s_arm_unwind_raw,       0 },
4440   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4441   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4442 #else
4443   { "word",        cons, 4},
4444
4445   /* These are used for dwarf.  */
4446   {"2byte", cons, 2},
4447   {"4byte", cons, 4},
4448   {"8byte", cons, 8},
4449   /* These are used for dwarf2.  */
4450   { "file", (void (*) (int)) dwarf2_directive_file, 0 },
4451   { "loc",  dwarf2_directive_loc,  0 },
4452   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4453 #endif
4454   { "extend",      float_cons, 'x' },
4455   { "ldouble",     float_cons, 'x' },
4456   { "packed",      float_cons, 'p' },
4457 #ifdef TE_PE
4458   {"secrel32", pe_directive_secrel, 0},
4459 #endif
4460   { 0, 0, 0 }
4461 };
4462 \f
4463 /* Parser functions used exclusively in instruction operands.  */
4464
4465 /* Generic immediate-value read function for use in insn parsing.
4466    STR points to the beginning of the immediate (the leading #);
4467    VAL receives the value; if the value is outside [MIN, MAX]
4468    issue an error.  PREFIX_OPT is true if the immediate prefix is
4469    optional.  */
4470
4471 static int
4472 parse_immediate (char **str, int *val, int min, int max,
4473                  bfd_boolean prefix_opt)
4474 {
4475   expressionS exp;
4476   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4477   if (exp.X_op != O_constant)
4478     {
4479       inst.error = _("constant expression required");
4480       return FAIL;
4481     }
4482
4483   if (exp.X_add_number < min || exp.X_add_number > max)
4484     {
4485       inst.error = _("immediate value out of range");
4486       return FAIL;
4487     }
4488
4489   *val = exp.X_add_number;
4490   return SUCCESS;
4491 }
4492
4493 /* Less-generic immediate-value read function with the possibility of loading a
4494    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4495    instructions. Puts the result directly in inst.operands[i].  */
4496
4497 static int
4498 parse_big_immediate (char **str, int i)
4499 {
4500   expressionS exp;
4501   char *ptr = *str;
4502
4503   my_get_expression (&exp, &ptr, GE_OPT_PREFIX_BIG);
4504
4505   if (exp.X_op == O_constant)
4506     {
4507       inst.operands[i].imm = exp.X_add_number & 0xffffffff;
4508       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4509          O_constant.  We have to be careful not to break compilation for
4510          32-bit X_add_number, though.  */
4511       if ((exp.X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4512         {
4513           /* X >> 32 is illegal if sizeof (exp.X_add_number) == 4.  */
4514           inst.operands[i].reg = ((exp.X_add_number >> 16) >> 16) & 0xffffffff;
4515           inst.operands[i].regisimm = 1;
4516         }
4517     }
4518   else if (exp.X_op == O_big
4519            && LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 32)
4520     {
4521       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4522
4523       /* Bignums have their least significant bits in
4524          generic_bignum[0]. Make sure we put 32 bits in imm and
4525          32 bits in reg,  in a (hopefully) portable way.  */
4526       gas_assert (parts != 0);
4527
4528       /* Make sure that the number is not too big.
4529          PR 11972: Bignums can now be sign-extended to the
4530          size of a .octa so check that the out of range bits
4531          are all zero or all one.  */
4532       if (LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 64)
4533         {
4534           LITTLENUM_TYPE m = -1;
4535
4536           if (generic_bignum[parts * 2] != 0
4537               && generic_bignum[parts * 2] != m)
4538             return FAIL;
4539
4540           for (j = parts * 2 + 1; j < (unsigned) exp.X_add_number; j++)
4541             if (generic_bignum[j] != generic_bignum[j-1])
4542               return FAIL;
4543         }
4544
4545       inst.operands[i].imm = 0;
4546       for (j = 0; j < parts; j++, idx++)
4547         inst.operands[i].imm |= generic_bignum[idx]
4548                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4549       inst.operands[i].reg = 0;
4550       for (j = 0; j < parts; j++, idx++)
4551         inst.operands[i].reg |= generic_bignum[idx]
4552                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4553       inst.operands[i].regisimm = 1;
4554     }
4555   else
4556     return FAIL;
4557
4558   *str = ptr;
4559
4560   return SUCCESS;
4561 }
4562
4563 /* Returns the pseudo-register number of an FPA immediate constant,
4564    or FAIL if there isn't a valid constant here.  */
4565
4566 static int
4567 parse_fpa_immediate (char ** str)
4568 {
4569   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4570   char *         save_in;
4571   expressionS    exp;
4572   int            i;
4573   int            j;
4574
4575   /* First try and match exact strings, this is to guarantee
4576      that some formats will work even for cross assembly.  */
4577
4578   for (i = 0; fp_const[i]; i++)
4579     {
4580       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4581         {
4582           char *start = *str;
4583
4584           *str += strlen (fp_const[i]);
4585           if (is_end_of_line[(unsigned char) **str])
4586             return i + 8;
4587           *str = start;
4588         }
4589     }
4590
4591   /* Just because we didn't get a match doesn't mean that the constant
4592      isn't valid, just that it is in a format that we don't
4593      automatically recognize.  Try parsing it with the standard
4594      expression routines.  */
4595
4596   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4597
4598   /* Look for a raw floating point number.  */
4599   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4600       && is_end_of_line[(unsigned char) *save_in])
4601     {
4602       for (i = 0; i < NUM_FLOAT_VALS; i++)
4603         {
4604           for (j = 0; j < MAX_LITTLENUMS; j++)
4605             {
4606               if (words[j] != fp_values[i][j])
4607                 break;
4608             }
4609
4610           if (j == MAX_LITTLENUMS)
4611             {
4612               *str = save_in;
4613               return i + 8;
4614             }
4615         }
4616     }
4617
4618   /* Try and parse a more complex expression, this will probably fail
4619      unless the code uses a floating point prefix (eg "0f").  */
4620   save_in = input_line_pointer;
4621   input_line_pointer = *str;
4622   if (expression (&exp) == absolute_section
4623       && exp.X_op == O_big
4624       && exp.X_add_number < 0)
4625     {
4626       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4627          Ditto for 15.  */
4628       if (gen_to_words (words, 5, (long) 15) == 0)
4629         {
4630           for (i = 0; i < NUM_FLOAT_VALS; i++)
4631             {
4632               for (j = 0; j < MAX_LITTLENUMS; j++)
4633                 {
4634                   if (words[j] != fp_values[i][j])
4635                     break;
4636                 }
4637
4638               if (j == MAX_LITTLENUMS)
4639                 {
4640                   *str = input_line_pointer;
4641                   input_line_pointer = save_in;
4642                   return i + 8;
4643                 }
4644             }
4645         }
4646     }
4647
4648   *str = input_line_pointer;
4649   input_line_pointer = save_in;
4650   inst.error = _("invalid FPA immediate expression");
4651   return FAIL;
4652 }
4653
4654 /* Returns 1 if a number has "quarter-precision" float format
4655    0baBbbbbbc defgh000 00000000 00000000.  */
4656
4657 static int
4658 is_quarter_float (unsigned imm)
4659 {
4660   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4661   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4662 }
4663
4664 /* Parse an 8-bit "quarter-precision" floating point number of the form:
4665    0baBbbbbbc defgh000 00000000 00000000.
4666    The zero and minus-zero cases need special handling, since they can't be
4667    encoded in the "quarter-precision" float format, but can nonetheless be
4668    loaded as integer constants.  */
4669
4670 static unsigned
4671 parse_qfloat_immediate (char **ccp, int *immed)
4672 {
4673   char *str = *ccp;
4674   char *fpnum;
4675   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4676   int found_fpchar = 0;
4677
4678   skip_past_char (&str, '#');
4679
4680   /* We must not accidentally parse an integer as a floating-point number. Make
4681      sure that the value we parse is not an integer by checking for special
4682      characters '.' or 'e'.
4683      FIXME: This is a horrible hack, but doing better is tricky because type
4684      information isn't in a very usable state at parse time.  */
4685   fpnum = str;
4686   skip_whitespace (fpnum);
4687
4688   if (strncmp (fpnum, "0x", 2) == 0)
4689     return FAIL;
4690   else
4691     {
4692       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
4693         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
4694           {
4695             found_fpchar = 1;
4696             break;
4697           }
4698
4699       if (!found_fpchar)
4700         return FAIL;
4701     }
4702
4703   if ((str = atof_ieee (str, 's', words)) != NULL)
4704     {
4705       unsigned fpword = 0;
4706       int i;
4707
4708       /* Our FP word must be 32 bits (single-precision FP).  */
4709       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
4710         {
4711           fpword <<= LITTLENUM_NUMBER_OF_BITS;
4712           fpword |= words[i];
4713         }
4714
4715       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
4716         *immed = fpword;
4717       else
4718         return FAIL;
4719
4720       *ccp = str;
4721
4722       return SUCCESS;
4723     }
4724
4725   return FAIL;
4726 }
4727
4728 /* Shift operands.  */
4729 enum shift_kind
4730 {
4731   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
4732 };
4733
4734 struct asm_shift_name
4735 {
4736   const char      *name;
4737   enum shift_kind  kind;
4738 };
4739
4740 /* Third argument to parse_shift.  */
4741 enum parse_shift_mode
4742 {
4743   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
4744   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
4745   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
4746   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
4747   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
4748 };
4749
4750 /* Parse a <shift> specifier on an ARM data processing instruction.
4751    This has three forms:
4752
4753      (LSL|LSR|ASL|ASR|ROR) Rs
4754      (LSL|LSR|ASL|ASR|ROR) #imm
4755      RRX
4756
4757    Note that ASL is assimilated to LSL in the instruction encoding, and
4758    RRX to ROR #0 (which cannot be written as such).  */
4759
4760 static int
4761 parse_shift (char **str, int i, enum parse_shift_mode mode)
4762 {
4763   const struct asm_shift_name *shift_name;
4764   enum shift_kind shift;
4765   char *s = *str;
4766   char *p = s;
4767   int reg;
4768
4769   for (p = *str; ISALPHA (*p); p++)
4770     ;
4771
4772   if (p == *str)
4773     {
4774       inst.error = _("shift expression expected");
4775       return FAIL;
4776     }
4777
4778   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
4779                                                             p - *str);
4780
4781   if (shift_name == NULL)
4782     {
4783       inst.error = _("shift expression expected");
4784       return FAIL;
4785     }
4786
4787   shift = shift_name->kind;
4788
4789   switch (mode)
4790     {
4791     case NO_SHIFT_RESTRICT:
4792     case SHIFT_IMMEDIATE:   break;
4793
4794     case SHIFT_LSL_OR_ASR_IMMEDIATE:
4795       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
4796         {
4797           inst.error = _("'LSL' or 'ASR' required");
4798           return FAIL;
4799         }
4800       break;
4801
4802     case SHIFT_LSL_IMMEDIATE:
4803       if (shift != SHIFT_LSL)
4804         {
4805           inst.error = _("'LSL' required");
4806           return FAIL;
4807         }
4808       break;
4809
4810     case SHIFT_ASR_IMMEDIATE:
4811       if (shift != SHIFT_ASR)
4812         {
4813           inst.error = _("'ASR' required");
4814           return FAIL;
4815         }
4816       break;
4817
4818     default: abort ();
4819     }
4820
4821   if (shift != SHIFT_RRX)
4822     {
4823       /* Whitespace can appear here if the next thing is a bare digit.  */
4824       skip_whitespace (p);
4825
4826       if (mode == NO_SHIFT_RESTRICT
4827           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
4828         {
4829           inst.operands[i].imm = reg;
4830           inst.operands[i].immisreg = 1;
4831         }
4832       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
4833         return FAIL;
4834     }
4835   inst.operands[i].shift_kind = shift;
4836   inst.operands[i].shifted = 1;
4837   *str = p;
4838   return SUCCESS;
4839 }
4840
4841 /* Parse a <shifter_operand> for an ARM data processing instruction:
4842
4843       #<immediate>
4844       #<immediate>, <rotate>
4845       <Rm>
4846       <Rm>, <shift>
4847
4848    where <shift> is defined by parse_shift above, and <rotate> is a
4849    multiple of 2 between 0 and 30.  Validation of immediate operands
4850    is deferred to md_apply_fix.  */
4851
4852 static int
4853 parse_shifter_operand (char **str, int i)
4854 {
4855   int value;
4856   expressionS exp;
4857
4858   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
4859     {
4860       inst.operands[i].reg = value;
4861       inst.operands[i].isreg = 1;
4862
4863       /* parse_shift will override this if appropriate */
4864       inst.reloc.exp.X_op = O_constant;
4865       inst.reloc.exp.X_add_number = 0;
4866
4867       if (skip_past_comma (str) == FAIL)
4868         return SUCCESS;
4869
4870       /* Shift operation on register.  */
4871       return parse_shift (str, i, NO_SHIFT_RESTRICT);
4872     }
4873
4874   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
4875     return FAIL;
4876
4877   if (skip_past_comma (str) == SUCCESS)
4878     {
4879       /* #x, y -- ie explicit rotation by Y.  */
4880       if (my_get_expression (&exp, str, GE_NO_PREFIX))
4881         return FAIL;
4882
4883       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
4884         {
4885           inst.error = _("constant expression expected");
4886           return FAIL;
4887         }
4888
4889       value = exp.X_add_number;
4890       if (value < 0 || value > 30 || value % 2 != 0)
4891         {
4892           inst.error = _("invalid rotation");
4893           return FAIL;
4894         }
4895       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
4896         {
4897           inst.error = _("invalid constant");
4898           return FAIL;
4899         }
4900
4901       /* Encode as specified.  */
4902       inst.operands[i].imm = inst.reloc.exp.X_add_number | value << 7;
4903       return SUCCESS;
4904     }
4905
4906   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
4907   inst.reloc.pc_rel = 0;
4908   return SUCCESS;
4909 }
4910
4911 /* Group relocation information.  Each entry in the table contains the
4912    textual name of the relocation as may appear in assembler source
4913    and must end with a colon.
4914    Along with this textual name are the relocation codes to be used if
4915    the corresponding instruction is an ALU instruction (ADD or SUB only),
4916    an LDR, an LDRS, or an LDC.  */
4917
4918 struct group_reloc_table_entry
4919 {
4920   const char *name;
4921   int alu_code;
4922   int ldr_code;
4923   int ldrs_code;
4924   int ldc_code;
4925 };
4926
4927 typedef enum
4928 {
4929   /* Varieties of non-ALU group relocation.  */
4930
4931   GROUP_LDR,
4932   GROUP_LDRS,
4933   GROUP_LDC
4934 } group_reloc_type;
4935
4936 static struct group_reloc_table_entry group_reloc_table[] =
4937   { /* Program counter relative: */
4938     { "pc_g0_nc",
4939       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
4940       0,                                /* LDR */
4941       0,                                /* LDRS */
4942       0 },                              /* LDC */
4943     { "pc_g0",
4944       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
4945       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
4946       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
4947       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
4948     { "pc_g1_nc",
4949       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
4950       0,                                /* LDR */
4951       0,                                /* LDRS */
4952       0 },                              /* LDC */
4953     { "pc_g1",
4954       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
4955       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
4956       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
4957       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
4958     { "pc_g2",
4959       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
4960       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
4961       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
4962       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
4963     /* Section base relative */
4964     { "sb_g0_nc",
4965       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
4966       0,                                /* LDR */
4967       0,                                /* LDRS */
4968       0 },                              /* LDC */
4969     { "sb_g0",
4970       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
4971       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
4972       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
4973       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
4974     { "sb_g1_nc",
4975       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
4976       0,                                /* LDR */
4977       0,                                /* LDRS */
4978       0 },                              /* LDC */
4979     { "sb_g1",
4980       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
4981       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
4982       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
4983       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
4984     { "sb_g2",
4985       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
4986       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
4987       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
4988       BFD_RELOC_ARM_LDC_SB_G2 } };      /* LDC */
4989
4990 /* Given the address of a pointer pointing to the textual name of a group
4991    relocation as may appear in assembler source, attempt to find its details
4992    in group_reloc_table.  The pointer will be updated to the character after
4993    the trailing colon.  On failure, FAIL will be returned; SUCCESS
4994    otherwise.  On success, *entry will be updated to point at the relevant
4995    group_reloc_table entry. */
4996
4997 static int
4998 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
4999 {
5000   unsigned int i;
5001   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5002     {
5003       int length = strlen (group_reloc_table[i].name);
5004
5005       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5006           && (*str)[length] == ':')
5007         {
5008           *out = &group_reloc_table[i];
5009           *str += (length + 1);
5010           return SUCCESS;
5011         }
5012     }
5013
5014   return FAIL;
5015 }
5016
5017 /* Parse a <shifter_operand> for an ARM data processing instruction
5018    (as for parse_shifter_operand) where group relocations are allowed:
5019
5020       #<immediate>
5021       #<immediate>, <rotate>
5022       #:<group_reloc>:<expression>
5023       <Rm>
5024       <Rm>, <shift>
5025
5026    where <group_reloc> is one of the strings defined in group_reloc_table.
5027    The hashes are optional.
5028
5029    Everything else is as for parse_shifter_operand.  */
5030
5031 static parse_operand_result
5032 parse_shifter_operand_group_reloc (char **str, int i)
5033 {
5034   /* Determine if we have the sequence of characters #: or just :
5035      coming next.  If we do, then we check for a group relocation.
5036      If we don't, punt the whole lot to parse_shifter_operand.  */
5037
5038   if (((*str)[0] == '#' && (*str)[1] == ':')
5039       || (*str)[0] == ':')
5040     {
5041       struct group_reloc_table_entry *entry;
5042
5043       if ((*str)[0] == '#')
5044         (*str) += 2;
5045       else
5046         (*str)++;
5047
5048       /* Try to parse a group relocation.  Anything else is an error.  */
5049       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5050         {
5051           inst.error = _("unknown group relocation");
5052           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5053         }
5054
5055       /* We now have the group relocation table entry corresponding to
5056          the name in the assembler source.  Next, we parse the expression.  */
5057       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5058         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5059
5060       /* Record the relocation type (always the ALU variant here).  */
5061       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5062       gas_assert (inst.reloc.type != 0);
5063
5064       return PARSE_OPERAND_SUCCESS;
5065     }
5066   else
5067     return parse_shifter_operand (str, i) == SUCCESS
5068            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5069
5070   /* Never reached.  */
5071 }
5072
5073 /* Parse a Neon alignment expression.  Information is written to
5074    inst.operands[i].  We assume the initial ':' has been skipped.
5075
5076    align        .imm = align << 8, .immisalign=1, .preind=0  */
5077 static parse_operand_result
5078 parse_neon_alignment (char **str, int i)
5079 {
5080   char *p = *str;
5081   expressionS exp;
5082
5083   my_get_expression (&exp, &p, GE_NO_PREFIX);
5084
5085   if (exp.X_op != O_constant)
5086     {
5087       inst.error = _("alignment must be constant");
5088       return PARSE_OPERAND_FAIL;
5089     }
5090
5091   inst.operands[i].imm = exp.X_add_number << 8;
5092   inst.operands[i].immisalign = 1;
5093   /* Alignments are not pre-indexes.  */
5094   inst.operands[i].preind = 0;
5095
5096   *str = p;
5097   return PARSE_OPERAND_SUCCESS;
5098 }
5099
5100 /* Parse all forms of an ARM address expression.  Information is written
5101    to inst.operands[i] and/or inst.reloc.
5102
5103    Preindexed addressing (.preind=1):
5104
5105    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5106    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5107    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5108                        .shift_kind=shift .reloc.exp=shift_imm
5109
5110    These three may have a trailing ! which causes .writeback to be set also.
5111
5112    Postindexed addressing (.postind=1, .writeback=1):
5113
5114    [Rn], #offset       .reg=Rn .reloc.exp=offset
5115    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5116    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5117                        .shift_kind=shift .reloc.exp=shift_imm
5118
5119    Unindexed addressing (.preind=0, .postind=0):
5120
5121    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5122
5123    Other:
5124
5125    [Rn]{!}             shorthand for [Rn,#0]{!}
5126    =immediate          .isreg=0 .reloc.exp=immediate
5127    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5128
5129   It is the caller's responsibility to check for addressing modes not
5130   supported by the instruction, and to set inst.reloc.type.  */
5131
5132 static parse_operand_result
5133 parse_address_main (char **str, int i, int group_relocations,
5134                     group_reloc_type group_type)
5135 {
5136   char *p = *str;
5137   int reg;
5138
5139   if (skip_past_char (&p, '[') == FAIL)
5140     {
5141       if (skip_past_char (&p, '=') == FAIL)
5142         {
5143           /* Bare address - translate to PC-relative offset.  */
5144           inst.reloc.pc_rel = 1;
5145           inst.operands[i].reg = REG_PC;
5146           inst.operands[i].isreg = 1;
5147           inst.operands[i].preind = 1;
5148         }
5149       /* Otherwise a load-constant pseudo op, no special treatment needed here.  */
5150
5151       if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5152         return PARSE_OPERAND_FAIL;
5153
5154       *str = p;
5155       return PARSE_OPERAND_SUCCESS;
5156     }
5157
5158   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5159     {
5160       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5161       return PARSE_OPERAND_FAIL;
5162     }
5163   inst.operands[i].reg = reg;
5164   inst.operands[i].isreg = 1;
5165
5166   if (skip_past_comma (&p) == SUCCESS)
5167     {
5168       inst.operands[i].preind = 1;
5169
5170       if (*p == '+') p++;
5171       else if (*p == '-') p++, inst.operands[i].negative = 1;
5172
5173       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5174         {
5175           inst.operands[i].imm = reg;
5176           inst.operands[i].immisreg = 1;
5177
5178           if (skip_past_comma (&p) == SUCCESS)
5179             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5180               return PARSE_OPERAND_FAIL;
5181         }
5182       else if (skip_past_char (&p, ':') == SUCCESS)
5183         {
5184           /* FIXME: '@' should be used here, but it's filtered out by generic
5185              code before we get to see it here. This may be subject to
5186              change.  */
5187           parse_operand_result result = parse_neon_alignment (&p, i);
5188
5189           if (result != PARSE_OPERAND_SUCCESS)
5190             return result;
5191         }
5192       else
5193         {
5194           if (inst.operands[i].negative)
5195             {
5196               inst.operands[i].negative = 0;
5197               p--;
5198             }
5199
5200           if (group_relocations
5201               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5202             {
5203               struct group_reloc_table_entry *entry;
5204
5205               /* Skip over the #: or : sequence.  */
5206               if (*p == '#')
5207                 p += 2;
5208               else
5209                 p++;
5210
5211               /* Try to parse a group relocation.  Anything else is an
5212                  error.  */
5213               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5214                 {
5215                   inst.error = _("unknown group relocation");
5216                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5217                 }
5218
5219               /* We now have the group relocation table entry corresponding to
5220                  the name in the assembler source.  Next, we parse the
5221                  expression.  */
5222               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5223                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5224
5225               /* Record the relocation type.  */
5226               switch (group_type)
5227                 {
5228                   case GROUP_LDR:
5229                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5230                     break;
5231
5232                   case GROUP_LDRS:
5233                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5234                     break;
5235
5236                   case GROUP_LDC:
5237                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5238                     break;
5239
5240                   default:
5241                     gas_assert (0);
5242                 }
5243
5244               if (inst.reloc.type == 0)
5245                 {
5246                   inst.error = _("this group relocation is not allowed on this instruction");
5247                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5248                 }
5249             }
5250           else
5251             {
5252               char *q = p;
5253               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5254                 return PARSE_OPERAND_FAIL;
5255               /* If the offset is 0, find out if it's a +0 or -0.  */
5256               if (inst.reloc.exp.X_op == O_constant
5257                   && inst.reloc.exp.X_add_number == 0)
5258                 {
5259                   skip_whitespace (q);
5260                   if (*q == '#')
5261                     {
5262                       q++;
5263                       skip_whitespace (q);
5264                     }
5265                   if (*q == '-')
5266                     inst.operands[i].negative = 1;
5267                 }
5268             }
5269         }
5270     }
5271   else if (skip_past_char (&p, ':') == SUCCESS)
5272     {
5273       /* FIXME: '@' should be used here, but it's filtered out by generic code
5274          before we get to see it here. This may be subject to change.  */
5275       parse_operand_result result = parse_neon_alignment (&p, i);
5276
5277       if (result != PARSE_OPERAND_SUCCESS)
5278         return result;
5279     }
5280
5281   if (skip_past_char (&p, ']') == FAIL)
5282     {
5283       inst.error = _("']' expected");
5284       return PARSE_OPERAND_FAIL;
5285     }
5286
5287   if (skip_past_char (&p, '!') == SUCCESS)
5288     inst.operands[i].writeback = 1;
5289
5290   else if (skip_past_comma (&p) == SUCCESS)
5291     {
5292       if (skip_past_char (&p, '{') == SUCCESS)
5293         {
5294           /* [Rn], {expr} - unindexed, with option */
5295           if (parse_immediate (&p, &inst.operands[i].imm,
5296                                0, 255, TRUE) == FAIL)
5297             return PARSE_OPERAND_FAIL;
5298
5299           if (skip_past_char (&p, '}') == FAIL)
5300             {
5301               inst.error = _("'}' expected at end of 'option' field");
5302               return PARSE_OPERAND_FAIL;
5303             }
5304           if (inst.operands[i].preind)
5305             {
5306               inst.error = _("cannot combine index with option");
5307               return PARSE_OPERAND_FAIL;
5308             }
5309           *str = p;
5310           return PARSE_OPERAND_SUCCESS;
5311         }
5312       else
5313         {
5314           inst.operands[i].postind = 1;
5315           inst.operands[i].writeback = 1;
5316
5317           if (inst.operands[i].preind)
5318             {
5319               inst.error = _("cannot combine pre- and post-indexing");
5320               return PARSE_OPERAND_FAIL;
5321             }
5322
5323           if (*p == '+') p++;
5324           else if (*p == '-') p++, inst.operands[i].negative = 1;
5325
5326           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5327             {
5328               /* We might be using the immediate for alignment already. If we
5329                  are, OR the register number into the low-order bits.  */
5330               if (inst.operands[i].immisalign)
5331                 inst.operands[i].imm |= reg;
5332               else
5333                 inst.operands[i].imm = reg;
5334               inst.operands[i].immisreg = 1;
5335
5336               if (skip_past_comma (&p) == SUCCESS)
5337                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5338                   return PARSE_OPERAND_FAIL;
5339             }
5340           else
5341             {
5342               char *q = p;
5343               if (inst.operands[i].negative)
5344                 {
5345                   inst.operands[i].negative = 0;
5346                   p--;
5347                 }
5348               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5349                 return PARSE_OPERAND_FAIL;
5350               /* If the offset is 0, find out if it's a +0 or -0.  */
5351               if (inst.reloc.exp.X_op == O_constant
5352                   && inst.reloc.exp.X_add_number == 0)
5353                 {
5354                   skip_whitespace (q);
5355                   if (*q == '#')
5356                     {
5357                       q++;
5358                       skip_whitespace (q);
5359                     }
5360                   if (*q == '-')
5361                     inst.operands[i].negative = 1;
5362                 }
5363             }
5364         }
5365     }
5366
5367   /* If at this point neither .preind nor .postind is set, we have a
5368      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5369   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5370     {
5371       inst.operands[i].preind = 1;
5372       inst.reloc.exp.X_op = O_constant;
5373       inst.reloc.exp.X_add_number = 0;
5374     }
5375   *str = p;
5376   return PARSE_OPERAND_SUCCESS;
5377 }
5378
5379 static int
5380 parse_address (char **str, int i)
5381 {
5382   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5383          ? SUCCESS : FAIL;
5384 }
5385
5386 static parse_operand_result
5387 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5388 {
5389   return parse_address_main (str, i, 1, type);
5390 }
5391
5392 /* Parse an operand for a MOVW or MOVT instruction.  */
5393 static int
5394 parse_half (char **str)
5395 {
5396   char * p;
5397
5398   p = *str;
5399   skip_past_char (&p, '#');
5400   if (strncasecmp (p, ":lower16:", 9) == 0)
5401     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5402   else if (strncasecmp (p, ":upper16:", 9) == 0)
5403     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5404
5405   if (inst.reloc.type != BFD_RELOC_UNUSED)
5406     {
5407       p += 9;
5408       skip_whitespace (p);
5409     }
5410
5411   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5412     return FAIL;
5413
5414   if (inst.reloc.type == BFD_RELOC_UNUSED)
5415     {
5416       if (inst.reloc.exp.X_op != O_constant)
5417         {
5418           inst.error = _("constant expression expected");
5419           return FAIL;
5420         }
5421       if (inst.reloc.exp.X_add_number < 0
5422           || inst.reloc.exp.X_add_number > 0xffff)
5423         {
5424           inst.error = _("immediate value out of range");
5425           return FAIL;
5426         }
5427     }
5428   *str = p;
5429   return SUCCESS;
5430 }
5431
5432 /* Miscellaneous. */
5433
5434 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5435    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5436 static int
5437 parse_psr (char **str, bfd_boolean lhs)
5438 {
5439   char *p;
5440   unsigned long psr_field;
5441   const struct asm_psr *psr;
5442   char *start;
5443   bfd_boolean is_apsr = FALSE;
5444   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5445
5446   /* PR gas/12698:  If the user has specified -march=all then m_profile will
5447      be TRUE, but we want to ignore it in this case as we are building for any
5448      CPU type, including non-m variants.  */
5449   if (selected_cpu.core == arm_arch_any.core)
5450     m_profile = FALSE;
5451
5452   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5453      feature for ease of use and backwards compatibility.  */
5454   p = *str;
5455   if (strncasecmp (p, "SPSR", 4) == 0)
5456     {
5457       if (m_profile)
5458         goto unsupported_psr;
5459
5460       psr_field = SPSR_BIT;
5461     }
5462   else if (strncasecmp (p, "CPSR", 4) == 0)
5463     {
5464       if (m_profile)
5465         goto unsupported_psr;
5466
5467       psr_field = 0;
5468     }
5469   else if (strncasecmp (p, "APSR", 4) == 0)
5470     {
5471       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5472          and ARMv7-R architecture CPUs.  */
5473       is_apsr = TRUE;
5474       psr_field = 0;
5475     }
5476   else if (m_profile)
5477     {
5478       start = p;
5479       do
5480         p++;
5481       while (ISALNUM (*p) || *p == '_');
5482
5483       if (strncasecmp (start, "iapsr", 5) == 0
5484           || strncasecmp (start, "eapsr", 5) == 0
5485           || strncasecmp (start, "xpsr", 4) == 0
5486           || strncasecmp (start, "psr", 3) == 0)
5487         p = start + strcspn (start, "rR") + 1;
5488
5489       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5490                                                   p - start);
5491
5492       if (!psr)
5493         return FAIL;
5494
5495       /* If APSR is being written, a bitfield may be specified.  Note that
5496          APSR itself is handled above.  */
5497       if (psr->field <= 3)
5498         {
5499           psr_field = psr->field;
5500           is_apsr = TRUE;
5501           goto check_suffix;
5502         }
5503
5504       *str = p;
5505       /* M-profile MSR instructions have the mask field set to "10", except
5506          *PSR variants which modify APSR, which may use a different mask (and
5507          have been handled already).  Do that by setting the PSR_f field
5508          here.  */
5509       return psr->field | (lhs ? PSR_f : 0);
5510     }
5511   else
5512     goto unsupported_psr;
5513
5514   p += 4;
5515 check_suffix:
5516   if (*p == '_')
5517     {
5518       /* A suffix follows.  */
5519       p++;
5520       start = p;
5521
5522       do
5523         p++;
5524       while (ISALNUM (*p) || *p == '_');
5525
5526       if (is_apsr)
5527         {
5528           /* APSR uses a notation for bits, rather than fields.  */
5529           unsigned int nzcvq_bits = 0;
5530           unsigned int g_bit = 0;
5531           char *bit;
5532
5533           for (bit = start; bit != p; bit++)
5534             {
5535               switch (TOLOWER (*bit))
5536                 {
5537                 case 'n':
5538                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5539                   break;
5540
5541                 case 'z':
5542                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5543                   break;
5544
5545                 case 'c':
5546                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5547                   break;
5548
5549                 case 'v':
5550                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5551                   break;
5552
5553                 case 'q':
5554                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5555                   break;
5556
5557                 case 'g':
5558                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5559                   break;
5560
5561                 default:
5562                   inst.error = _("unexpected bit specified after APSR");
5563                   return FAIL;
5564                 }
5565             }
5566
5567           if (nzcvq_bits == 0x1f)
5568             psr_field |= PSR_f;
5569
5570           if (g_bit == 0x1)
5571             {
5572               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5573                 {
5574                   inst.error = _("selected processor does not "
5575                                  "support DSP extension");
5576                   return FAIL;
5577                 }
5578
5579               psr_field |= PSR_s;
5580             }
5581
5582           if ((nzcvq_bits & 0x20) != 0
5583               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5584               || (g_bit & 0x2) != 0)
5585             {
5586               inst.error = _("bad bitmask specified after APSR");
5587               return FAIL;
5588             }
5589         }
5590       else
5591         {
5592           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
5593                                                       p - start);
5594           if (!psr)
5595             goto error;
5596
5597           psr_field |= psr->field;
5598         }
5599     }
5600   else
5601     {
5602       if (ISALNUM (*p))
5603         goto error;    /* Garbage after "[CS]PSR".  */
5604
5605       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
5606          is deprecated, but allow it anyway.  */
5607       if (is_apsr && lhs)
5608         {
5609           psr_field |= PSR_f;
5610           as_tsktsk (_("writing to APSR without specifying a bitmask is "
5611                        "deprecated"));
5612         }
5613       else if (!m_profile)
5614         /* These bits are never right for M-profile devices: don't set them
5615            (only code paths which read/write APSR reach here).  */
5616         psr_field |= (PSR_c | PSR_f);
5617     }
5618   *str = p;
5619   return psr_field;
5620
5621  unsupported_psr:
5622   inst.error = _("selected processor does not support requested special "
5623                  "purpose register");
5624   return FAIL;
5625
5626  error:
5627   inst.error = _("flag for {c}psr instruction expected");
5628   return FAIL;
5629 }
5630
5631 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
5632    value suitable for splatting into the AIF field of the instruction.  */
5633
5634 static int
5635 parse_cps_flags (char **str)
5636 {
5637   int val = 0;
5638   int saw_a_flag = 0;
5639   char *s = *str;
5640
5641   for (;;)
5642     switch (*s++)
5643       {
5644       case '\0': case ',':
5645         goto done;
5646
5647       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
5648       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
5649       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
5650
5651       default:
5652         inst.error = _("unrecognized CPS flag");
5653         return FAIL;
5654       }
5655
5656  done:
5657   if (saw_a_flag == 0)
5658     {
5659       inst.error = _("missing CPS flags");
5660       return FAIL;
5661     }
5662
5663   *str = s - 1;
5664   return val;
5665 }
5666
5667 /* Parse an endian specifier ("BE" or "LE", case insensitive);
5668    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
5669
5670 static int
5671 parse_endian_specifier (char **str)
5672 {
5673   int little_endian;
5674   char *s = *str;
5675
5676   if (strncasecmp (s, "BE", 2))
5677     little_endian = 0;
5678   else if (strncasecmp (s, "LE", 2))
5679     little_endian = 1;
5680   else
5681     {
5682       inst.error = _("valid endian specifiers are be or le");
5683       return FAIL;
5684     }
5685
5686   if (ISALNUM (s[2]) || s[2] == '_')
5687     {
5688       inst.error = _("valid endian specifiers are be or le");
5689       return FAIL;
5690     }
5691
5692   *str = s + 2;
5693   return little_endian;
5694 }
5695
5696 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
5697    value suitable for poking into the rotate field of an sxt or sxta
5698    instruction, or FAIL on error.  */
5699
5700 static int
5701 parse_ror (char **str)
5702 {
5703   int rot;
5704   char *s = *str;
5705
5706   if (strncasecmp (s, "ROR", 3) == 0)
5707     s += 3;
5708   else
5709     {
5710       inst.error = _("missing rotation field after comma");
5711       return FAIL;
5712     }
5713
5714   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
5715     return FAIL;
5716
5717   switch (rot)
5718     {
5719     case  0: *str = s; return 0x0;
5720     case  8: *str = s; return 0x1;
5721     case 16: *str = s; return 0x2;
5722     case 24: *str = s; return 0x3;
5723
5724     default:
5725       inst.error = _("rotation can only be 0, 8, 16, or 24");
5726       return FAIL;
5727     }
5728 }
5729
5730 /* Parse a conditional code (from conds[] below).  The value returned is in the
5731    range 0 .. 14, or FAIL.  */
5732 static int
5733 parse_cond (char **str)
5734 {
5735   char *q;
5736   const struct asm_cond *c;
5737   int n;
5738   /* Condition codes are always 2 characters, so matching up to
5739      3 characters is sufficient.  */
5740   char cond[3];
5741
5742   q = *str;
5743   n = 0;
5744   while (ISALPHA (*q) && n < 3)
5745     {
5746       cond[n] = TOLOWER (*q);
5747       q++;
5748       n++;
5749     }
5750
5751   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
5752   if (!c)
5753     {
5754       inst.error = _("condition required");
5755       return FAIL;
5756     }
5757
5758   *str = q;
5759   return c->value;
5760 }
5761
5762 /* Parse an option for a barrier instruction.  Returns the encoding for the
5763    option, or FAIL.  */
5764 static int
5765 parse_barrier (char **str)
5766 {
5767   char *p, *q;
5768   const struct asm_barrier_opt *o;
5769
5770   p = q = *str;
5771   while (ISALPHA (*q))
5772     q++;
5773
5774   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
5775                                                     q - p);
5776   if (!o)
5777     return FAIL;
5778
5779   *str = q;
5780   return o->value;
5781 }
5782
5783 /* Parse the operands of a table branch instruction.  Similar to a memory
5784    operand.  */
5785 static int
5786 parse_tb (char **str)
5787 {
5788   char * p = *str;
5789   int reg;
5790
5791   if (skip_past_char (&p, '[') == FAIL)
5792     {
5793       inst.error = _("'[' expected");
5794       return FAIL;
5795     }
5796
5797   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5798     {
5799       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5800       return FAIL;
5801     }
5802   inst.operands[0].reg = reg;
5803
5804   if (skip_past_comma (&p) == FAIL)
5805     {
5806       inst.error = _("',' expected");
5807       return FAIL;
5808     }
5809
5810   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5811     {
5812       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5813       return FAIL;
5814     }
5815   inst.operands[0].imm = reg;
5816
5817   if (skip_past_comma (&p) == SUCCESS)
5818     {
5819       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
5820         return FAIL;
5821       if (inst.reloc.exp.X_add_number != 1)
5822         {
5823           inst.error = _("invalid shift");
5824           return FAIL;
5825         }
5826       inst.operands[0].shifted = 1;
5827     }
5828
5829   if (skip_past_char (&p, ']') == FAIL)
5830     {
5831       inst.error = _("']' expected");
5832       return FAIL;
5833     }
5834   *str = p;
5835   return SUCCESS;
5836 }
5837
5838 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
5839    information on the types the operands can take and how they are encoded.
5840    Up to four operands may be read; this function handles setting the
5841    ".present" field for each read operand itself.
5842    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
5843    else returns FAIL.  */
5844
5845 static int
5846 parse_neon_mov (char **str, int *which_operand)
5847 {
5848   int i = *which_operand, val;
5849   enum arm_reg_type rtype;
5850   char *ptr = *str;
5851   struct neon_type_el optype;
5852
5853   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
5854     {
5855       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
5856       inst.operands[i].reg = val;
5857       inst.operands[i].isscalar = 1;
5858       inst.operands[i].vectype = optype;
5859       inst.operands[i++].present = 1;
5860
5861       if (skip_past_comma (&ptr) == FAIL)
5862         goto wanted_comma;
5863
5864       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5865         goto wanted_arm;
5866
5867       inst.operands[i].reg = val;
5868       inst.operands[i].isreg = 1;
5869       inst.operands[i].present = 1;
5870     }
5871   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
5872            != FAIL)
5873     {
5874       /* Cases 0, 1, 2, 3, 5 (D only).  */
5875       if (skip_past_comma (&ptr) == FAIL)
5876         goto wanted_comma;
5877
5878       inst.operands[i].reg = val;
5879       inst.operands[i].isreg = 1;
5880       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5881       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5882       inst.operands[i].isvec = 1;
5883       inst.operands[i].vectype = optype;
5884       inst.operands[i++].present = 1;
5885
5886       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5887         {
5888           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
5889              Case 13: VMOV <Sd>, <Rm>  */
5890           inst.operands[i].reg = val;
5891           inst.operands[i].isreg = 1;
5892           inst.operands[i].present = 1;
5893
5894           if (rtype == REG_TYPE_NQ)
5895             {
5896               first_error (_("can't use Neon quad register here"));
5897               return FAIL;
5898             }
5899           else if (rtype != REG_TYPE_VFS)
5900             {
5901               i++;
5902               if (skip_past_comma (&ptr) == FAIL)
5903                 goto wanted_comma;
5904               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5905                 goto wanted_arm;
5906               inst.operands[i].reg = val;
5907               inst.operands[i].isreg = 1;
5908               inst.operands[i].present = 1;
5909             }
5910         }
5911       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
5912                                            &optype)) != FAIL)
5913         {
5914           /* Case 0: VMOV<c><q> <Qd>, <Qm>
5915              Case 1: VMOV<c><q> <Dd>, <Dm>
5916              Case 8: VMOV.F32 <Sd>, <Sm>
5917              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
5918
5919           inst.operands[i].reg = val;
5920           inst.operands[i].isreg = 1;
5921           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5922           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5923           inst.operands[i].isvec = 1;
5924           inst.operands[i].vectype = optype;
5925           inst.operands[i].present = 1;
5926
5927           if (skip_past_comma (&ptr) == SUCCESS)
5928             {
5929               /* Case 15.  */
5930               i++;
5931
5932               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5933                 goto wanted_arm;
5934
5935               inst.operands[i].reg = val;
5936               inst.operands[i].isreg = 1;
5937               inst.operands[i++].present = 1;
5938
5939               if (skip_past_comma (&ptr) == FAIL)
5940                 goto wanted_comma;
5941
5942               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5943                 goto wanted_arm;
5944
5945               inst.operands[i].reg = val;
5946               inst.operands[i].isreg = 1;
5947               inst.operands[i].present = 1;
5948             }
5949         }
5950       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
5951           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
5952              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
5953              Case 10: VMOV.F32 <Sd>, #<imm>
5954              Case 11: VMOV.F64 <Dd>, #<imm>  */
5955         inst.operands[i].immisfloat = 1;
5956       else if (parse_big_immediate (&ptr, i) == SUCCESS)
5957           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
5958              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
5959         ;
5960       else
5961         {
5962           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
5963           return FAIL;
5964         }
5965     }
5966   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5967     {
5968       /* Cases 6, 7.  */
5969       inst.operands[i].reg = val;
5970       inst.operands[i].isreg = 1;
5971       inst.operands[i++].present = 1;
5972
5973       if (skip_past_comma (&ptr) == FAIL)
5974         goto wanted_comma;
5975
5976       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
5977         {
5978           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
5979           inst.operands[i].reg = val;
5980           inst.operands[i].isscalar = 1;
5981           inst.operands[i].present = 1;
5982           inst.operands[i].vectype = optype;
5983         }
5984       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5985         {
5986           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
5987           inst.operands[i].reg = val;
5988           inst.operands[i].isreg = 1;
5989           inst.operands[i++].present = 1;
5990
5991           if (skip_past_comma (&ptr) == FAIL)
5992             goto wanted_comma;
5993
5994           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
5995               == FAIL)
5996             {
5997               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
5998               return FAIL;
5999             }
6000
6001           inst.operands[i].reg = val;
6002           inst.operands[i].isreg = 1;
6003           inst.operands[i].isvec = 1;
6004           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6005           inst.operands[i].vectype = optype;
6006           inst.operands[i].present = 1;
6007
6008           if (rtype == REG_TYPE_VFS)
6009             {
6010               /* Case 14.  */
6011               i++;
6012               if (skip_past_comma (&ptr) == FAIL)
6013                 goto wanted_comma;
6014               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6015                                               &optype)) == FAIL)
6016                 {
6017                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6018                   return FAIL;
6019                 }
6020               inst.operands[i].reg = val;
6021               inst.operands[i].isreg = 1;
6022               inst.operands[i].isvec = 1;
6023               inst.operands[i].issingle = 1;
6024               inst.operands[i].vectype = optype;
6025               inst.operands[i].present = 1;
6026             }
6027         }
6028       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6029                != FAIL)
6030         {
6031           /* Case 13.  */
6032           inst.operands[i].reg = val;
6033           inst.operands[i].isreg = 1;
6034           inst.operands[i].isvec = 1;
6035           inst.operands[i].issingle = 1;
6036           inst.operands[i].vectype = optype;
6037           inst.operands[i].present = 1;
6038         }
6039     }
6040   else
6041     {
6042       first_error (_("parse error"));
6043       return FAIL;
6044     }
6045
6046   /* Successfully parsed the operands. Update args.  */
6047   *which_operand = i;
6048   *str = ptr;
6049   return SUCCESS;
6050
6051  wanted_comma:
6052   first_error (_("expected comma"));
6053   return FAIL;
6054
6055  wanted_arm:
6056   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6057   return FAIL;
6058 }
6059
6060 /* Use this macro when the operand constraints are different
6061    for ARM and THUMB (e.g. ldrd).  */
6062 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6063         ((arm_operand) | ((thumb_operand) << 16))
6064
6065 /* Matcher codes for parse_operands.  */
6066 enum operand_parse_code
6067 {
6068   OP_stop,      /* end of line */
6069
6070   OP_RR,        /* ARM register */
6071   OP_RRnpc,     /* ARM register, not r15 */
6072   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6073   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6074   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6075                    optional trailing ! */
6076   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6077   OP_RCP,       /* Coprocessor number */
6078   OP_RCN,       /* Coprocessor register */
6079   OP_RF,        /* FPA register */
6080   OP_RVS,       /* VFP single precision register */
6081   OP_RVD,       /* VFP double precision register (0..15) */
6082   OP_RND,       /* Neon double precision register (0..31) */
6083   OP_RNQ,       /* Neon quad precision register */
6084   OP_RVSD,      /* VFP single or double precision register */
6085   OP_RNDQ,      /* Neon double or quad precision register */
6086   OP_RNSDQ,     /* Neon single, double or quad precision register */
6087   OP_RNSC,      /* Neon scalar D[X] */
6088   OP_RVC,       /* VFP control register */
6089   OP_RMF,       /* Maverick F register */
6090   OP_RMD,       /* Maverick D register */
6091   OP_RMFX,      /* Maverick FX register */
6092   OP_RMDX,      /* Maverick DX register */
6093   OP_RMAX,      /* Maverick AX register */
6094   OP_RMDS,      /* Maverick DSPSC register */
6095   OP_RIWR,      /* iWMMXt wR register */
6096   OP_RIWC,      /* iWMMXt wC register */
6097   OP_RIWG,      /* iWMMXt wCG register */
6098   OP_RXA,       /* XScale accumulator register */
6099
6100   OP_REGLST,    /* ARM register list */
6101   OP_VRSLST,    /* VFP single-precision register list */
6102   OP_VRDLST,    /* VFP double-precision register list */
6103   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6104   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6105   OP_NSTRLST,   /* Neon element/structure list */
6106
6107   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6108   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6109   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6110   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6111   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6112   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6113   OP_VMOV,      /* Neon VMOV operands.  */
6114   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6115   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6116   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6117
6118   OP_I0,        /* immediate zero */
6119   OP_I7,        /* immediate value 0 .. 7 */
6120   OP_I15,       /*                 0 .. 15 */
6121   OP_I16,       /*                 1 .. 16 */
6122   OP_I16z,      /*                 0 .. 16 */
6123   OP_I31,       /*                 0 .. 31 */
6124   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6125   OP_I32,       /*                 1 .. 32 */
6126   OP_I32z,      /*                 0 .. 32 */
6127   OP_I63,       /*                 0 .. 63 */
6128   OP_I63s,      /*               -64 .. 63 */
6129   OP_I64,       /*                 1 .. 64 */
6130   OP_I64z,      /*                 0 .. 64 */
6131   OP_I255,      /*                 0 .. 255 */
6132
6133   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6134   OP_I7b,       /*                             0 .. 7 */
6135   OP_I15b,      /*                             0 .. 15 */
6136   OP_I31b,      /*                             0 .. 31 */
6137
6138   OP_SH,        /* shifter operand */
6139   OP_SHG,       /* shifter operand with possible group relocation */
6140   OP_ADDR,      /* Memory address expression (any mode) */
6141   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6142   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6143   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6144   OP_EXP,       /* arbitrary expression */
6145   OP_EXPi,      /* same, with optional immediate prefix */
6146   OP_EXPr,      /* same, with optional relocation suffix */
6147   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6148
6149   OP_CPSF,      /* CPS flags */
6150   OP_ENDI,      /* Endianness specifier */
6151   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6152   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6153   OP_COND,      /* conditional code */
6154   OP_TB,        /* Table branch.  */
6155
6156   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6157
6158   OP_RRnpc_I0,  /* ARM register or literal 0 */
6159   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
6160   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6161   OP_RF_IF,     /* FPA register or immediate */
6162   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6163   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6164
6165   /* Optional operands.  */
6166   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6167   OP_oI31b,      /*                             0 .. 31 */
6168   OP_oI32b,      /*                             1 .. 32 */
6169   OP_oI32z,      /*                             0 .. 32 */
6170   OP_oIffffb,    /*                             0 .. 65535 */
6171   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6172
6173   OP_oRR,        /* ARM register */
6174   OP_oRRnpc,     /* ARM register, not the PC */
6175   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6176   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6177   OP_oRND,       /* Optional Neon double precision register */
6178   OP_oRNQ,       /* Optional Neon quad precision register */
6179   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6180   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6181   OP_oSHll,      /* LSL immediate */
6182   OP_oSHar,      /* ASR immediate */
6183   OP_oSHllar,    /* LSL or ASR immediate */
6184   OP_oROR,       /* ROR 0/8/16/24 */
6185   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6186
6187   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6188   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6189   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6190   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6191
6192   OP_FIRST_OPTIONAL = OP_oI7b
6193 };
6194
6195 /* Generic instruction operand parser.  This does no encoding and no
6196    semantic validation; it merely squirrels values away in the inst
6197    structure.  Returns SUCCESS or FAIL depending on whether the
6198    specified grammar matched.  */
6199 static int
6200 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6201 {
6202   unsigned const int *upat = pattern;
6203   char *backtrack_pos = 0;
6204   const char *backtrack_error = 0;
6205   int i, val, backtrack_index = 0;
6206   enum arm_reg_type rtype;
6207   parse_operand_result result;
6208   unsigned int op_parse_code;
6209
6210 #define po_char_or_fail(chr)                    \
6211   do                                            \
6212     {                                           \
6213       if (skip_past_char (&str, chr) == FAIL)   \
6214         goto bad_args;                          \
6215     }                                           \
6216   while (0)
6217
6218 #define po_reg_or_fail(regtype)                                 \
6219   do                                                            \
6220     {                                                           \
6221       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6222                                  & inst.operands[i].vectype);   \
6223       if (val == FAIL)                                          \
6224         {                                                       \
6225           first_error (_(reg_expected_msgs[regtype]));          \
6226           goto failure;                                         \
6227         }                                                       \
6228       inst.operands[i].reg = val;                               \
6229       inst.operands[i].isreg = 1;                               \
6230       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6231       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6232       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6233                              || rtype == REG_TYPE_VFD           \
6234                              || rtype == REG_TYPE_NQ);          \
6235     }                                                           \
6236   while (0)
6237
6238 #define po_reg_or_goto(regtype, label)                          \
6239   do                                                            \
6240     {                                                           \
6241       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6242                                  & inst.operands[i].vectype);   \
6243       if (val == FAIL)                                          \
6244         goto label;                                             \
6245                                                                 \
6246       inst.operands[i].reg = val;                               \
6247       inst.operands[i].isreg = 1;                               \
6248       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6249       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6250       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6251                              || rtype == REG_TYPE_VFD           \
6252                              || rtype == REG_TYPE_NQ);          \
6253     }                                                           \
6254   while (0)
6255
6256 #define po_imm_or_fail(min, max, popt)                          \
6257   do                                                            \
6258     {                                                           \
6259       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6260         goto failure;                                           \
6261       inst.operands[i].imm = val;                               \
6262     }                                                           \
6263   while (0)
6264
6265 #define po_scalar_or_goto(elsz, label)                                  \
6266   do                                                                    \
6267     {                                                                   \
6268       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6269       if (val == FAIL)                                                  \
6270         goto label;                                                     \
6271       inst.operands[i].reg = val;                                       \
6272       inst.operands[i].isscalar = 1;                                    \
6273     }                                                                   \
6274   while (0)
6275
6276 #define po_misc_or_fail(expr)                   \
6277   do                                            \
6278     {                                           \
6279       if (expr)                                 \
6280         goto failure;                           \
6281     }                                           \
6282   while (0)
6283
6284 #define po_misc_or_fail_no_backtrack(expr)              \
6285   do                                                    \
6286     {                                                   \
6287       result = expr;                                    \
6288       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6289         backtrack_pos = 0;                              \
6290       if (result != PARSE_OPERAND_SUCCESS)              \
6291         goto failure;                                   \
6292     }                                                   \
6293   while (0)
6294
6295 #define po_barrier_or_imm(str)                             \
6296   do                                                       \
6297     {                                                      \
6298       val = parse_barrier (&str);                          \
6299       if (val == FAIL)                                     \
6300         {                                                  \
6301           if (ISALPHA (*str))                              \
6302               goto failure;                                \
6303           else                                             \
6304               goto immediate;                              \
6305         }                                                  \
6306       else                                                 \
6307         {                                                  \
6308           if ((inst.instruction & 0xf0) == 0x60            \
6309               && val != 0xf)                               \
6310             {                                              \
6311                /* ISB can only take SY as an option.  */   \
6312                inst.error = _("invalid barrier type");     \
6313                goto failure;                               \
6314             }                                              \
6315         }                                                  \
6316     }                                                      \
6317   while (0)
6318
6319   skip_whitespace (str);
6320
6321   for (i = 0; upat[i] != OP_stop; i++)
6322     {
6323       op_parse_code = upat[i];
6324       if (op_parse_code >= 1<<16)
6325         op_parse_code = thumb ? (op_parse_code >> 16)
6326                                 : (op_parse_code & ((1<<16)-1));
6327
6328       if (op_parse_code >= OP_FIRST_OPTIONAL)
6329         {
6330           /* Remember where we are in case we need to backtrack.  */
6331           gas_assert (!backtrack_pos);
6332           backtrack_pos = str;
6333           backtrack_error = inst.error;
6334           backtrack_index = i;
6335         }
6336
6337       if (i > 0 && (i > 1 || inst.operands[0].present))
6338         po_char_or_fail (',');
6339
6340       switch (op_parse_code)
6341         {
6342           /* Registers */
6343         case OP_oRRnpc:
6344         case OP_oRRnpcsp:
6345         case OP_RRnpc:
6346         case OP_RRnpcsp:
6347         case OP_oRR:
6348         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6349         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6350         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6351         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6352         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6353         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6354         case OP_oRND:
6355         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6356         case OP_RVC:
6357           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6358           break;
6359           /* Also accept generic coprocessor regs for unknown registers.  */
6360           coproc_reg:
6361           po_reg_or_fail (REG_TYPE_CN);
6362           break;
6363         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6364         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6365         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6366         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6367         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6368         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6369         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6370         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6371         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6372         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6373         case OP_oRNQ:
6374         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6375         case OP_oRNDQ:
6376         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6377         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6378         case OP_oRNSDQ:
6379         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6380
6381         /* Neon scalar. Using an element size of 8 means that some invalid
6382            scalars are accepted here, so deal with those in later code.  */
6383         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6384
6385         case OP_RNDQ_I0:
6386           {
6387             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6388             break;
6389             try_imm0:
6390             po_imm_or_fail (0, 0, TRUE);
6391           }
6392           break;
6393
6394         case OP_RVSD_I0:
6395           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6396           break;
6397
6398         case OP_RR_RNSC:
6399           {
6400             po_scalar_or_goto (8, try_rr);
6401             break;
6402             try_rr:
6403             po_reg_or_fail (REG_TYPE_RN);
6404           }
6405           break;
6406
6407         case OP_RNSDQ_RNSC:
6408           {
6409             po_scalar_or_goto (8, try_nsdq);
6410             break;
6411             try_nsdq:
6412             po_reg_or_fail (REG_TYPE_NSDQ);
6413           }
6414           break;
6415
6416         case OP_RNDQ_RNSC:
6417           {
6418             po_scalar_or_goto (8, try_ndq);
6419             break;
6420             try_ndq:
6421             po_reg_or_fail (REG_TYPE_NDQ);
6422           }
6423           break;
6424
6425         case OP_RND_RNSC:
6426           {
6427             po_scalar_or_goto (8, try_vfd);
6428             break;
6429             try_vfd:
6430             po_reg_or_fail (REG_TYPE_VFD);
6431           }
6432           break;
6433
6434         case OP_VMOV:
6435           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6436              not careful then bad things might happen.  */
6437           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6438           break;
6439
6440         case OP_RNDQ_Ibig:
6441           {
6442             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6443             break;
6444             try_immbig:
6445             /* There's a possibility of getting a 64-bit immediate here, so
6446                we need special handling.  */
6447             if (parse_big_immediate (&str, i) == FAIL)
6448               {
6449                 inst.error = _("immediate value is out of range");
6450                 goto failure;
6451               }
6452           }
6453           break;
6454
6455         case OP_RNDQ_I63b:
6456           {
6457             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6458             break;
6459             try_shimm:
6460             po_imm_or_fail (0, 63, TRUE);
6461           }
6462           break;
6463
6464         case OP_RRnpcb:
6465           po_char_or_fail ('[');
6466           po_reg_or_fail  (REG_TYPE_RN);
6467           po_char_or_fail (']');
6468           break;
6469
6470         case OP_RRnpctw:
6471         case OP_RRw:
6472         case OP_oRRw:
6473           po_reg_or_fail (REG_TYPE_RN);
6474           if (skip_past_char (&str, '!') == SUCCESS)
6475             inst.operands[i].writeback = 1;
6476           break;
6477
6478           /* Immediates */
6479         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6480         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6481         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6482         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6483         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6484         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6485         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6486         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6487         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6488         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6489         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6490         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6491
6492         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6493         case OP_oI7b:
6494         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6495         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6496         case OP_oI31b:
6497         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6498         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6499         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
6500         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6501
6502           /* Immediate variants */
6503         case OP_oI255c:
6504           po_char_or_fail ('{');
6505           po_imm_or_fail (0, 255, TRUE);
6506           po_char_or_fail ('}');
6507           break;
6508
6509         case OP_I31w:
6510           /* The expression parser chokes on a trailing !, so we have
6511              to find it first and zap it.  */
6512           {
6513             char *s = str;
6514             while (*s && *s != ',')
6515               s++;
6516             if (s[-1] == '!')
6517               {
6518                 s[-1] = '\0';
6519                 inst.operands[i].writeback = 1;
6520               }
6521             po_imm_or_fail (0, 31, TRUE);
6522             if (str == s - 1)
6523               str = s;
6524           }
6525           break;
6526
6527           /* Expressions */
6528         case OP_EXPi:   EXPi:
6529           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6530                                               GE_OPT_PREFIX));
6531           break;
6532
6533         case OP_EXP:
6534           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6535                                               GE_NO_PREFIX));
6536           break;
6537
6538         case OP_EXPr:   EXPr:
6539           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6540                                               GE_NO_PREFIX));
6541           if (inst.reloc.exp.X_op == O_symbol)
6542             {
6543               val = parse_reloc (&str);
6544               if (val == -1)
6545                 {
6546                   inst.error = _("unrecognized relocation suffix");
6547                   goto failure;
6548                 }
6549               else if (val != BFD_RELOC_UNUSED)
6550                 {
6551                   inst.operands[i].imm = val;
6552                   inst.operands[i].hasreloc = 1;
6553                 }
6554             }
6555           break;
6556
6557           /* Operand for MOVW or MOVT.  */
6558         case OP_HALF:
6559           po_misc_or_fail (parse_half (&str));
6560           break;
6561
6562           /* Register or expression.  */
6563         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
6564         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
6565
6566           /* Register or immediate.  */
6567         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
6568         I0:               po_imm_or_fail (0, 0, FALSE);       break;
6569
6570         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
6571         IF:
6572           if (!is_immediate_prefix (*str))
6573             goto bad_args;
6574           str++;
6575           val = parse_fpa_immediate (&str);
6576           if (val == FAIL)
6577             goto failure;
6578           /* FPA immediates are encoded as registers 8-15.
6579              parse_fpa_immediate has already applied the offset.  */
6580           inst.operands[i].reg = val;
6581           inst.operands[i].isreg = 1;
6582           break;
6583
6584         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
6585         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
6586
6587           /* Two kinds of register.  */
6588         case OP_RIWR_RIWC:
6589           {
6590             struct reg_entry *rege = arm_reg_parse_multi (&str);
6591             if (!rege
6592                 || (rege->type != REG_TYPE_MMXWR
6593                     && rege->type != REG_TYPE_MMXWC
6594                     && rege->type != REG_TYPE_MMXWCG))
6595               {
6596                 inst.error = _("iWMMXt data or control register expected");
6597                 goto failure;
6598               }
6599             inst.operands[i].reg = rege->number;
6600             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
6601           }
6602           break;
6603
6604         case OP_RIWC_RIWG:
6605           {
6606             struct reg_entry *rege = arm_reg_parse_multi (&str);
6607             if (!rege
6608                 || (rege->type != REG_TYPE_MMXWC
6609                     && rege->type != REG_TYPE_MMXWCG))
6610               {
6611                 inst.error = _("iWMMXt control register expected");
6612                 goto failure;
6613               }
6614             inst.operands[i].reg = rege->number;
6615             inst.operands[i].isreg = 1;
6616           }
6617           break;
6618
6619           /* Misc */
6620         case OP_CPSF:    val = parse_cps_flags (&str);          break;
6621         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
6622         case OP_oROR:    val = parse_ror (&str);                break;
6623         case OP_COND:    val = parse_cond (&str);               break;
6624         case OP_oBARRIER_I15:
6625           po_barrier_or_imm (str); break;
6626           immediate:
6627           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
6628             goto failure;
6629           break;
6630
6631         case OP_wPSR:
6632         case OP_rPSR:
6633           po_reg_or_goto (REG_TYPE_RNB, try_psr);
6634           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
6635             {
6636               inst.error = _("Banked registers are not available with this "
6637                              "architecture.");
6638               goto failure;
6639             }
6640           break;
6641           try_psr:
6642           val = parse_psr (&str, op_parse_code == OP_wPSR);
6643           break;
6644
6645         case OP_APSR_RR:
6646           po_reg_or_goto (REG_TYPE_RN, try_apsr);
6647           break;
6648           try_apsr:
6649           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
6650              instruction).  */
6651           if (strncasecmp (str, "APSR_", 5) == 0)
6652             {
6653               unsigned found = 0;
6654               str += 5;
6655               while (found < 15)
6656                 switch (*str++)
6657                   {
6658                   case 'c': found = (found & 1) ? 16 : found | 1; break;
6659                   case 'n': found = (found & 2) ? 16 : found | 2; break;
6660                   case 'z': found = (found & 4) ? 16 : found | 4; break;
6661                   case 'v': found = (found & 8) ? 16 : found | 8; break;
6662                   default: found = 16;
6663                   }
6664               if (found != 15)
6665                 goto failure;
6666               inst.operands[i].isvec = 1;
6667               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
6668               inst.operands[i].reg = REG_PC;
6669             }
6670           else
6671             goto failure;
6672           break;
6673
6674         case OP_TB:
6675           po_misc_or_fail (parse_tb (&str));
6676           break;
6677
6678           /* Register lists.  */
6679         case OP_REGLST:
6680           val = parse_reg_list (&str);
6681           if (*str == '^')
6682             {
6683               inst.operands[1].writeback = 1;
6684               str++;
6685             }
6686           break;
6687
6688         case OP_VRSLST:
6689           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
6690           break;
6691
6692         case OP_VRDLST:
6693           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
6694           break;
6695
6696         case OP_VRSDLST:
6697           /* Allow Q registers too.  */
6698           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6699                                     REGLIST_NEON_D);
6700           if (val == FAIL)
6701             {
6702               inst.error = NULL;
6703               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6704                                         REGLIST_VFP_S);
6705               inst.operands[i].issingle = 1;
6706             }
6707           break;
6708
6709         case OP_NRDLST:
6710           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6711                                     REGLIST_NEON_D);
6712           break;
6713
6714         case OP_NSTRLST:
6715           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
6716                                            &inst.operands[i].vectype);
6717           break;
6718
6719           /* Addressing modes */
6720         case OP_ADDR:
6721           po_misc_or_fail (parse_address (&str, i));
6722           break;
6723
6724         case OP_ADDRGLDR:
6725           po_misc_or_fail_no_backtrack (
6726             parse_address_group_reloc (&str, i, GROUP_LDR));
6727           break;
6728
6729         case OP_ADDRGLDRS:
6730           po_misc_or_fail_no_backtrack (
6731             parse_address_group_reloc (&str, i, GROUP_LDRS));
6732           break;
6733
6734         case OP_ADDRGLDC:
6735           po_misc_or_fail_no_backtrack (
6736             parse_address_group_reloc (&str, i, GROUP_LDC));
6737           break;
6738
6739         case OP_SH:
6740           po_misc_or_fail (parse_shifter_operand (&str, i));
6741           break;
6742
6743         case OP_SHG:
6744           po_misc_or_fail_no_backtrack (
6745             parse_shifter_operand_group_reloc (&str, i));
6746           break;
6747
6748         case OP_oSHll:
6749           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
6750           break;
6751
6752         case OP_oSHar:
6753           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
6754           break;
6755
6756         case OP_oSHllar:
6757           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
6758           break;
6759
6760         default:
6761           as_fatal (_("unhandled operand code %d"), op_parse_code);
6762         }
6763
6764       /* Various value-based sanity checks and shared operations.  We
6765          do not signal immediate failures for the register constraints;
6766          this allows a syntax error to take precedence.  */
6767       switch (op_parse_code)
6768         {
6769         case OP_oRRnpc:
6770         case OP_RRnpc:
6771         case OP_RRnpcb:
6772         case OP_RRw:
6773         case OP_oRRw:
6774         case OP_RRnpc_I0:
6775           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
6776             inst.error = BAD_PC;
6777           break;
6778
6779         case OP_oRRnpcsp:
6780         case OP_RRnpcsp:
6781           if (inst.operands[i].isreg)
6782             {
6783               if (inst.operands[i].reg == REG_PC)
6784                 inst.error = BAD_PC;
6785               else if (inst.operands[i].reg == REG_SP)
6786                 inst.error = BAD_SP;
6787             }
6788           break;
6789
6790         case OP_RRnpctw:
6791           if (inst.operands[i].isreg
6792               && inst.operands[i].reg == REG_PC
6793               && (inst.operands[i].writeback || thumb))
6794             inst.error = BAD_PC;
6795           break;
6796
6797         case OP_CPSF:
6798         case OP_ENDI:
6799         case OP_oROR:
6800         case OP_wPSR:
6801         case OP_rPSR:
6802         case OP_COND:
6803         case OP_oBARRIER_I15:
6804         case OP_REGLST:
6805         case OP_VRSLST:
6806         case OP_VRDLST:
6807         case OP_VRSDLST:
6808         case OP_NRDLST:
6809         case OP_NSTRLST:
6810           if (val == FAIL)
6811             goto failure;
6812           inst.operands[i].imm = val;
6813           break;
6814
6815         default:
6816           break;
6817         }
6818
6819       /* If we get here, this operand was successfully parsed.  */
6820       inst.operands[i].present = 1;
6821       continue;
6822
6823     bad_args:
6824       inst.error = BAD_ARGS;
6825
6826     failure:
6827       if (!backtrack_pos)
6828         {
6829           /* The parse routine should already have set inst.error, but set a
6830              default here just in case.  */
6831           if (!inst.error)
6832             inst.error = _("syntax error");
6833           return FAIL;
6834         }
6835
6836       /* Do not backtrack over a trailing optional argument that
6837          absorbed some text.  We will only fail again, with the
6838          'garbage following instruction' error message, which is
6839          probably less helpful than the current one.  */
6840       if (backtrack_index == i && backtrack_pos != str
6841           && upat[i+1] == OP_stop)
6842         {
6843           if (!inst.error)
6844             inst.error = _("syntax error");
6845           return FAIL;
6846         }
6847
6848       /* Try again, skipping the optional argument at backtrack_pos.  */
6849       str = backtrack_pos;
6850       inst.error = backtrack_error;
6851       inst.operands[backtrack_index].present = 0;
6852       i = backtrack_index;
6853       backtrack_pos = 0;
6854     }
6855
6856   /* Check that we have parsed all the arguments.  */
6857   if (*str != '\0' && !inst.error)
6858     inst.error = _("garbage following instruction");
6859
6860   return inst.error ? FAIL : SUCCESS;
6861 }
6862
6863 #undef po_char_or_fail
6864 #undef po_reg_or_fail
6865 #undef po_reg_or_goto
6866 #undef po_imm_or_fail
6867 #undef po_scalar_or_fail
6868 #undef po_barrier_or_imm
6869
6870 /* Shorthand macro for instruction encoding functions issuing errors.  */
6871 #define constraint(expr, err)                   \
6872   do                                            \
6873     {                                           \
6874       if (expr)                                 \
6875         {                                       \
6876           inst.error = err;                     \
6877           return;                               \
6878         }                                       \
6879     }                                           \
6880   while (0)
6881
6882 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
6883    instructions are unpredictable if these registers are used.  This
6884    is the BadReg predicate in ARM's Thumb-2 documentation.  */
6885 #define reject_bad_reg(reg)                             \
6886   do                                                    \
6887    if (reg == REG_SP || reg == REG_PC)                  \
6888      {                                                  \
6889        inst.error = (reg == REG_SP) ? BAD_SP : BAD_PC;  \
6890        return;                                          \
6891      }                                                  \
6892   while (0)
6893
6894 /* If REG is R13 (the stack pointer), warn that its use is
6895    deprecated.  */
6896 #define warn_deprecated_sp(reg)                 \
6897   do                                            \
6898     if (warn_on_deprecated && reg == REG_SP)    \
6899        as_warn (_("use of r13 is deprecated")); \
6900   while (0)
6901
6902 /* Functions for operand encoding.  ARM, then Thumb.  */
6903
6904 #define rotate_left(v, n) (v << n | v >> (32 - n))
6905
6906 /* If VAL can be encoded in the immediate field of an ARM instruction,
6907    return the encoded form.  Otherwise, return FAIL.  */
6908
6909 static unsigned int
6910 encode_arm_immediate (unsigned int val)
6911 {
6912   unsigned int a, i;
6913
6914   for (i = 0; i < 32; i += 2)
6915     if ((a = rotate_left (val, i)) <= 0xff)
6916       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
6917
6918   return FAIL;
6919 }
6920
6921 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
6922    return the encoded form.  Otherwise, return FAIL.  */
6923 static unsigned int
6924 encode_thumb32_immediate (unsigned int val)
6925 {
6926   unsigned int a, i;
6927
6928   if (val <= 0xff)
6929     return val;
6930
6931   for (i = 1; i <= 24; i++)
6932     {
6933       a = val >> i;
6934       if ((val & ~(0xff << i)) == 0)
6935         return ((val >> i) & 0x7f) | ((32 - i) << 7);
6936     }
6937
6938   a = val & 0xff;
6939   if (val == ((a << 16) | a))
6940     return 0x100 | a;
6941   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
6942     return 0x300 | a;
6943
6944   a = val & 0xff00;
6945   if (val == ((a << 16) | a))
6946     return 0x200 | (a >> 8);
6947
6948   return FAIL;
6949 }
6950 /* Encode a VFP SP or DP register number into inst.instruction.  */
6951
6952 static void
6953 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
6954 {
6955   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
6956       && reg > 15)
6957     {
6958       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
6959         {
6960           if (thumb_mode)
6961             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
6962                                     fpu_vfp_ext_d32);
6963           else
6964             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
6965                                     fpu_vfp_ext_d32);
6966         }
6967       else
6968         {
6969           first_error (_("D register out of range for selected VFP version"));
6970           return;
6971         }
6972     }
6973
6974   switch (pos)
6975     {
6976     case VFP_REG_Sd:
6977       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
6978       break;
6979
6980     case VFP_REG_Sn:
6981       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
6982       break;
6983
6984     case VFP_REG_Sm:
6985       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
6986       break;
6987
6988     case VFP_REG_Dd:
6989       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
6990       break;
6991
6992     case VFP_REG_Dn:
6993       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
6994       break;
6995
6996     case VFP_REG_Dm:
6997       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
6998       break;
6999
7000     default:
7001       abort ();
7002     }
7003 }
7004
7005 /* Encode a <shift> in an ARM-format instruction.  The immediate,
7006    if any, is handled by md_apply_fix.   */
7007 static void
7008 encode_arm_shift (int i)
7009 {
7010   if (inst.operands[i].shift_kind == SHIFT_RRX)
7011     inst.instruction |= SHIFT_ROR << 5;
7012   else
7013     {
7014       inst.instruction |= inst.operands[i].shift_kind << 5;
7015       if (inst.operands[i].immisreg)
7016         {
7017           inst.instruction |= SHIFT_BY_REG;
7018           inst.instruction |= inst.operands[i].imm << 8;
7019         }
7020       else
7021         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7022     }
7023 }
7024
7025 static void
7026 encode_arm_shifter_operand (int i)
7027 {
7028   if (inst.operands[i].isreg)
7029     {
7030       inst.instruction |= inst.operands[i].reg;
7031       encode_arm_shift (i);
7032     }
7033   else
7034     {
7035       inst.instruction |= INST_IMMEDIATE;
7036       if (inst.reloc.type != BFD_RELOC_ARM_IMMEDIATE)
7037         inst.instruction |= inst.operands[i].imm;
7038     }
7039 }
7040
7041 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7042 static void
7043 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7044 {
7045   gas_assert (inst.operands[i].isreg);
7046   inst.instruction |= inst.operands[i].reg << 16;
7047
7048   if (inst.operands[i].preind)
7049     {
7050       if (is_t)
7051         {
7052           inst.error = _("instruction does not accept preindexed addressing");
7053           return;
7054         }
7055       inst.instruction |= PRE_INDEX;
7056       if (inst.operands[i].writeback)
7057         inst.instruction |= WRITE_BACK;
7058
7059     }
7060   else if (inst.operands[i].postind)
7061     {
7062       gas_assert (inst.operands[i].writeback);
7063       if (is_t)
7064         inst.instruction |= WRITE_BACK;
7065     }
7066   else /* unindexed - only for coprocessor */
7067     {
7068       inst.error = _("instruction does not accept unindexed addressing");
7069       return;
7070     }
7071
7072   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7073       && (((inst.instruction & 0x000f0000) >> 16)
7074           == ((inst.instruction & 0x0000f000) >> 12)))
7075     as_warn ((inst.instruction & LOAD_BIT)
7076              ? _("destination register same as write-back base")
7077              : _("source register same as write-back base"));
7078 }
7079
7080 /* inst.operands[i] was set up by parse_address.  Encode it into an
7081    ARM-format mode 2 load or store instruction.  If is_t is true,
7082    reject forms that cannot be used with a T instruction (i.e. not
7083    post-indexed).  */
7084 static void
7085 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7086 {
7087   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7088
7089   encode_arm_addr_mode_common (i, is_t);
7090
7091   if (inst.operands[i].immisreg)
7092     {
7093       constraint ((inst.operands[i].imm == REG_PC
7094                    || (is_pc && inst.operands[i].writeback)),
7095                   BAD_PC_ADDRESSING);
7096       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7097       inst.instruction |= inst.operands[i].imm;
7098       if (!inst.operands[i].negative)
7099         inst.instruction |= INDEX_UP;
7100       if (inst.operands[i].shifted)
7101         {
7102           if (inst.operands[i].shift_kind == SHIFT_RRX)
7103             inst.instruction |= SHIFT_ROR << 5;
7104           else
7105             {
7106               inst.instruction |= inst.operands[i].shift_kind << 5;
7107               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7108             }
7109         }
7110     }
7111   else /* immediate offset in inst.reloc */
7112     {
7113       if (is_pc && !inst.reloc.pc_rel)
7114         {
7115           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7116
7117           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7118              cannot use PC in addressing.
7119              PC cannot be used in writeback addressing, either.  */
7120           constraint ((is_t || inst.operands[i].writeback),
7121                       BAD_PC_ADDRESSING);
7122
7123           /* Use of PC in str is deprecated for ARMv7.  */
7124           if (warn_on_deprecated
7125               && !is_load
7126               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7127             as_warn (_("use of PC in this instruction is deprecated"));
7128         }
7129
7130       if (inst.reloc.type == BFD_RELOC_UNUSED)
7131         {
7132           /* Prefer + for zero encoded value.  */
7133           if (!inst.operands[i].negative)
7134             inst.instruction |= INDEX_UP;
7135           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7136         }
7137     }
7138 }
7139
7140 /* inst.operands[i] was set up by parse_address.  Encode it into an
7141    ARM-format mode 3 load or store instruction.  Reject forms that
7142    cannot be used with such instructions.  If is_t is true, reject
7143    forms that cannot be used with a T instruction (i.e. not
7144    post-indexed).  */
7145 static void
7146 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7147 {
7148   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7149     {
7150       inst.error = _("instruction does not accept scaled register index");
7151       return;
7152     }
7153
7154   encode_arm_addr_mode_common (i, is_t);
7155
7156   if (inst.operands[i].immisreg)
7157     {
7158       constraint ((inst.operands[i].imm == REG_PC
7159                    || inst.operands[i].reg == REG_PC),
7160                   BAD_PC_ADDRESSING);
7161       inst.instruction |= inst.operands[i].imm;
7162       if (!inst.operands[i].negative)
7163         inst.instruction |= INDEX_UP;
7164     }
7165   else /* immediate offset in inst.reloc */
7166     {
7167       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7168                    && inst.operands[i].writeback),
7169                   BAD_PC_WRITEBACK);
7170       inst.instruction |= HWOFFSET_IMM;
7171       if (inst.reloc.type == BFD_RELOC_UNUSED)
7172         {
7173           /* Prefer + for zero encoded value.  */
7174           if (!inst.operands[i].negative)
7175             inst.instruction |= INDEX_UP;
7176
7177           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7178         }
7179     }
7180 }
7181
7182 /* inst.operands[i] was set up by parse_address.  Encode it into an
7183    ARM-format instruction.  Reject all forms which cannot be encoded
7184    into a coprocessor load/store instruction.  If wb_ok is false,
7185    reject use of writeback; if unind_ok is false, reject use of
7186    unindexed addressing.  If reloc_override is not 0, use it instead
7187    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
7188    (in which case it is preserved).  */
7189
7190 static int
7191 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
7192 {
7193   inst.instruction |= inst.operands[i].reg << 16;
7194
7195   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
7196
7197   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
7198     {
7199       gas_assert (!inst.operands[i].writeback);
7200       if (!unind_ok)
7201         {
7202           inst.error = _("instruction does not support unindexed addressing");
7203           return FAIL;
7204         }
7205       inst.instruction |= inst.operands[i].imm;
7206       inst.instruction |= INDEX_UP;
7207       return SUCCESS;
7208     }
7209
7210   if (inst.operands[i].preind)
7211     inst.instruction |= PRE_INDEX;
7212
7213   if (inst.operands[i].writeback)
7214     {
7215       if (inst.operands[i].reg == REG_PC)
7216         {
7217           inst.error = _("pc may not be used with write-back");
7218           return FAIL;
7219         }
7220       if (!wb_ok)
7221         {
7222           inst.error = _("instruction does not support writeback");
7223           return FAIL;
7224         }
7225       inst.instruction |= WRITE_BACK;
7226     }
7227
7228   if (reloc_override)
7229     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
7230   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
7231             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
7232            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
7233     {
7234       if (thumb_mode)
7235         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
7236       else
7237         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
7238     }
7239
7240   /* Prefer + for zero encoded value.  */
7241   if (!inst.operands[i].negative)
7242     inst.instruction |= INDEX_UP;
7243
7244   return SUCCESS;
7245 }
7246
7247 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7248    Determine whether it can be performed with a move instruction; if
7249    it can, convert inst.instruction to that move instruction and
7250    return TRUE; if it can't, convert inst.instruction to a literal-pool
7251    load and return FALSE.  If this is not a valid thing to do in the
7252    current context, set inst.error and return TRUE.
7253
7254    inst.operands[i] describes the destination register.  */
7255
7256 static bfd_boolean
7257 move_or_literal_pool (int i, bfd_boolean thumb_p, bfd_boolean mode_3)
7258 {
7259   unsigned long tbit;
7260
7261   if (thumb_p)
7262     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7263   else
7264     tbit = LOAD_BIT;
7265
7266   if ((inst.instruction & tbit) == 0)
7267     {
7268       inst.error = _("invalid pseudo operation");
7269       return TRUE;
7270     }
7271   if (inst.reloc.exp.X_op != O_constant && inst.reloc.exp.X_op != O_symbol)
7272     {
7273       inst.error = _("constant expression expected");
7274       return TRUE;
7275     }
7276   if (inst.reloc.exp.X_op == O_constant)
7277     {
7278       if (thumb_p)
7279         {
7280           if (!unified_syntax && (inst.reloc.exp.X_add_number & ~0xFF) == 0)
7281             {
7282               /* This can be done with a mov(1) instruction.  */
7283               inst.instruction  = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
7284               inst.instruction |= inst.reloc.exp.X_add_number;
7285               return TRUE;
7286             }
7287         }
7288       else
7289         {
7290           int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
7291           if (value != FAIL)
7292             {
7293               /* This can be done with a mov instruction.  */
7294               inst.instruction &= LITERAL_MASK;
7295               inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
7296               inst.instruction |= value & 0xfff;
7297               return TRUE;
7298             }
7299
7300           value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
7301           if (value != FAIL)
7302             {
7303               /* This can be done with a mvn instruction.  */
7304               inst.instruction &= LITERAL_MASK;
7305               inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
7306               inst.instruction |= value & 0xfff;
7307               return TRUE;
7308             }
7309         }
7310     }
7311
7312   if (add_to_lit_pool () == FAIL)
7313     {
7314       inst.error = _("literal pool insertion failed");
7315       return TRUE;
7316     }
7317   inst.operands[1].reg = REG_PC;
7318   inst.operands[1].isreg = 1;
7319   inst.operands[1].preind = 1;
7320   inst.reloc.pc_rel = 1;
7321   inst.reloc.type = (thumb_p
7322                      ? BFD_RELOC_ARM_THUMB_OFFSET
7323                      : (mode_3
7324                         ? BFD_RELOC_ARM_HWLITERAL
7325                         : BFD_RELOC_ARM_LITERAL));
7326   return FALSE;
7327 }
7328
7329 /* Functions for instruction encoding, sorted by sub-architecture.
7330    First some generics; their names are taken from the conventional
7331    bit positions for register arguments in ARM format instructions.  */
7332
7333 static void
7334 do_noargs (void)
7335 {
7336 }
7337
7338 static void
7339 do_rd (void)
7340 {
7341   inst.instruction |= inst.operands[0].reg << 12;
7342 }
7343
7344 static void
7345 do_rd_rm (void)
7346 {
7347   inst.instruction |= inst.operands[0].reg << 12;
7348   inst.instruction |= inst.operands[1].reg;
7349 }
7350
7351 static void
7352 do_rd_rn (void)
7353 {
7354   inst.instruction |= inst.operands[0].reg << 12;
7355   inst.instruction |= inst.operands[1].reg << 16;
7356 }
7357
7358 static void
7359 do_rn_rd (void)
7360 {
7361   inst.instruction |= inst.operands[0].reg << 16;
7362   inst.instruction |= inst.operands[1].reg << 12;
7363 }
7364
7365 static void
7366 do_rd_rm_rn (void)
7367 {
7368   unsigned Rn = inst.operands[2].reg;
7369   /* Enforce restrictions on SWP instruction.  */
7370   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
7371     {
7372       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
7373                   _("Rn must not overlap other operands"));
7374
7375       /* SWP{b} is deprecated for ARMv6* and ARMv7.  */
7376       if (warn_on_deprecated
7377           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
7378         as_warn (_("swp{b} use is deprecated for this architecture"));
7379
7380     }
7381   inst.instruction |= inst.operands[0].reg << 12;
7382   inst.instruction |= inst.operands[1].reg;
7383   inst.instruction |= Rn << 16;
7384 }
7385
7386 static void
7387 do_rd_rn_rm (void)
7388 {
7389   inst.instruction |= inst.operands[0].reg << 12;
7390   inst.instruction |= inst.operands[1].reg << 16;
7391   inst.instruction |= inst.operands[2].reg;
7392 }
7393
7394 static void
7395 do_rm_rd_rn (void)
7396 {
7397   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
7398   constraint (((inst.reloc.exp.X_op != O_constant
7399                 && inst.reloc.exp.X_op != O_illegal)
7400                || inst.reloc.exp.X_add_number != 0),
7401               BAD_ADDR_MODE);
7402   inst.instruction |= inst.operands[0].reg;
7403   inst.instruction |= inst.operands[1].reg << 12;
7404   inst.instruction |= inst.operands[2].reg << 16;
7405 }
7406
7407 static void
7408 do_imm0 (void)
7409 {
7410   inst.instruction |= inst.operands[0].imm;
7411 }
7412
7413 static void
7414 do_rd_cpaddr (void)
7415 {
7416   inst.instruction |= inst.operands[0].reg << 12;
7417   encode_arm_cp_address (1, TRUE, TRUE, 0);
7418 }
7419
7420 /* ARM instructions, in alphabetical order by function name (except
7421    that wrapper functions appear immediately after the function they
7422    wrap).  */
7423
7424 /* This is a pseudo-op of the form "adr rd, label" to be converted
7425    into a relative address of the form "add rd, pc, #label-.-8".  */
7426
7427 static void
7428 do_adr (void)
7429 {
7430   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
7431
7432   /* Frag hacking will turn this into a sub instruction if the offset turns
7433      out to be negative.  */
7434   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
7435   inst.reloc.pc_rel = 1;
7436   inst.reloc.exp.X_add_number -= 8;
7437 }
7438
7439 /* This is a pseudo-op of the form "adrl rd, label" to be converted
7440    into a relative address of the form:
7441    add rd, pc, #low(label-.-8)"
7442    add rd, rd, #high(label-.-8)"  */
7443
7444 static void
7445 do_adrl (void)
7446 {
7447   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
7448
7449   /* Frag hacking will turn this into a sub instruction if the offset turns
7450      out to be negative.  */
7451   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
7452   inst.reloc.pc_rel            = 1;
7453   inst.size                    = INSN_SIZE * 2;
7454   inst.reloc.exp.X_add_number -= 8;
7455 }
7456
7457 static void
7458 do_arit (void)
7459 {
7460   if (!inst.operands[1].present)
7461     inst.operands[1].reg = inst.operands[0].reg;
7462   inst.instruction |= inst.operands[0].reg << 12;
7463   inst.instruction |= inst.operands[1].reg << 16;
7464   encode_arm_shifter_operand (2);
7465 }
7466
7467 static void
7468 do_barrier (void)
7469 {
7470   if (inst.operands[0].present)
7471     {
7472       constraint ((inst.instruction & 0xf0) != 0x40
7473                   && inst.operands[0].imm > 0xf
7474                   && inst.operands[0].imm < 0x0,
7475                   _("bad barrier type"));
7476       inst.instruction |= inst.operands[0].imm;
7477     }
7478   else
7479     inst.instruction |= 0xf;
7480 }
7481
7482 static void
7483 do_bfc (void)
7484 {
7485   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
7486   constraint (msb > 32, _("bit-field extends past end of register"));
7487   /* The instruction encoding stores the LSB and MSB,
7488      not the LSB and width.  */
7489   inst.instruction |= inst.operands[0].reg << 12;
7490   inst.instruction |= inst.operands[1].imm << 7;
7491   inst.instruction |= (msb - 1) << 16;
7492 }
7493
7494 static void
7495 do_bfi (void)
7496 {
7497   unsigned int msb;
7498
7499   /* #0 in second position is alternative syntax for bfc, which is
7500      the same instruction but with REG_PC in the Rm field.  */
7501   if (!inst.operands[1].isreg)
7502     inst.operands[1].reg = REG_PC;
7503
7504   msb = inst.operands[2].imm + inst.operands[3].imm;
7505   constraint (msb > 32, _("bit-field extends past end of register"));
7506   /* The instruction encoding stores the LSB and MSB,
7507      not the LSB and width.  */
7508   inst.instruction |= inst.operands[0].reg << 12;
7509   inst.instruction |= inst.operands[1].reg;
7510   inst.instruction |= inst.operands[2].imm << 7;
7511   inst.instruction |= (msb - 1) << 16;
7512 }
7513
7514 static void
7515 do_bfx (void)
7516 {
7517   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
7518               _("bit-field extends past end of register"));
7519   inst.instruction |= inst.operands[0].reg << 12;
7520   inst.instruction |= inst.operands[1].reg;
7521   inst.instruction |= inst.operands[2].imm << 7;
7522   inst.instruction |= (inst.operands[3].imm - 1) << 16;
7523 }
7524
7525 /* ARM V5 breakpoint instruction (argument parse)
7526      BKPT <16 bit unsigned immediate>
7527      Instruction is not conditional.
7528         The bit pattern given in insns[] has the COND_ALWAYS condition,
7529         and it is an error if the caller tried to override that.  */
7530
7531 static void
7532 do_bkpt (void)
7533 {
7534   /* Top 12 of 16 bits to bits 19:8.  */
7535   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
7536
7537   /* Bottom 4 of 16 bits to bits 3:0.  */
7538   inst.instruction |= inst.operands[0].imm & 0xf;
7539 }
7540
7541 static void
7542 encode_branch (int default_reloc)
7543 {
7544   if (inst.operands[0].hasreloc)
7545     {
7546       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
7547                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
7548                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
7549       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
7550         ? BFD_RELOC_ARM_PLT32
7551         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
7552     }
7553   else
7554     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
7555   inst.reloc.pc_rel = 1;
7556 }
7557
7558 static void
7559 do_branch (void)
7560 {
7561 #ifdef OBJ_ELF
7562   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
7563     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
7564   else
7565 #endif
7566     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
7567 }
7568
7569 static void
7570 do_bl (void)
7571 {
7572 #ifdef OBJ_ELF
7573   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
7574     {
7575       if (inst.cond == COND_ALWAYS)
7576         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
7577       else
7578         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
7579     }
7580   else
7581 #endif
7582     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
7583 }
7584
7585 /* ARM V5 branch-link-exchange instruction (argument parse)
7586      BLX <target_addr>          ie BLX(1)
7587      BLX{<condition>} <Rm>      ie BLX(2)
7588    Unfortunately, there are two different opcodes for this mnemonic.
7589    So, the insns[].value is not used, and the code here zaps values
7590         into inst.instruction.
7591    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
7592
7593 static void
7594 do_blx (void)
7595 {
7596   if (inst.operands[0].isreg)
7597     {
7598       /* Arg is a register; the opcode provided by insns[] is correct.
7599          It is not illegal to do "blx pc", just useless.  */
7600       if (inst.operands[0].reg == REG_PC)
7601         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
7602
7603       inst.instruction |= inst.operands[0].reg;
7604     }
7605   else
7606     {
7607       /* Arg is an address; this instruction cannot be executed
7608          conditionally, and the opcode must be adjusted.
7609          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
7610          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
7611       constraint (inst.cond != COND_ALWAYS, BAD_COND);
7612       inst.instruction = 0xfa000000;
7613       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
7614     }
7615 }
7616
7617 static void
7618 do_bx (void)
7619 {
7620   bfd_boolean want_reloc;
7621
7622   if (inst.operands[0].reg == REG_PC)
7623     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
7624
7625   inst.instruction |= inst.operands[0].reg;
7626   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
7627      it is for ARMv4t or earlier.  */
7628   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
7629   if (object_arch && !ARM_CPU_HAS_FEATURE (*object_arch, arm_ext_v5))
7630       want_reloc = TRUE;
7631
7632 #ifdef OBJ_ELF
7633   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
7634 #endif
7635     want_reloc = FALSE;
7636
7637   if (want_reloc)
7638     inst.reloc.type = BFD_RELOC_ARM_V4BX;
7639 }
7640
7641
7642 /* ARM v5TEJ.  Jump to Jazelle code.  */
7643
7644 static void
7645 do_bxj (void)
7646 {
7647   if (inst.operands[0].reg == REG_PC)
7648     as_tsktsk (_("use of r15 in bxj is not really useful"));
7649
7650   inst.instruction |= inst.operands[0].reg;
7651 }
7652
7653 /* Co-processor data operation:
7654       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
7655       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
7656 static void
7657 do_cdp (void)
7658 {
7659   inst.instruction |= inst.operands[0].reg << 8;
7660   inst.instruction |= inst.operands[1].imm << 20;
7661   inst.instruction |= inst.operands[2].reg << 12;
7662   inst.instruction |= inst.operands[3].reg << 16;
7663   inst.instruction |= inst.operands[4].reg;
7664   inst.instruction |= inst.operands[5].imm << 5;
7665 }
7666
7667 static void
7668 do_cmp (void)
7669 {
7670   inst.instruction |= inst.operands[0].reg << 16;
7671   encode_arm_shifter_operand (1);
7672 }
7673
7674 /* Transfer between coprocessor and ARM registers.
7675    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
7676    MRC2
7677    MCR{cond}
7678    MCR2
7679
7680    No special properties.  */
7681
7682 static void
7683 do_co_reg (void)
7684 {
7685   unsigned Rd;
7686
7687   Rd = inst.operands[2].reg;
7688   if (thumb_mode)
7689     {
7690       if (inst.instruction == 0xee000010
7691           || inst.instruction == 0xfe000010)
7692         /* MCR, MCR2  */
7693         reject_bad_reg (Rd);
7694       else
7695         /* MRC, MRC2  */
7696         constraint (Rd == REG_SP, BAD_SP);
7697     }
7698   else
7699     {
7700       /* MCR */
7701       if (inst.instruction == 0xe000010)
7702         constraint (Rd == REG_PC, BAD_PC);
7703     }
7704
7705
7706   inst.instruction |= inst.operands[0].reg << 8;
7707   inst.instruction |= inst.operands[1].imm << 21;
7708   inst.instruction |= Rd << 12;
7709   inst.instruction |= inst.operands[3].reg << 16;
7710   inst.instruction |= inst.operands[4].reg;
7711   inst.instruction |= inst.operands[5].imm << 5;
7712 }
7713
7714 /* Transfer between coprocessor register and pair of ARM registers.
7715    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
7716    MCRR2
7717    MRRC{cond}
7718    MRRC2
7719
7720    Two XScale instructions are special cases of these:
7721
7722      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
7723      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
7724
7725    Result unpredictable if Rd or Rn is R15.  */
7726
7727 static void
7728 do_co_reg2c (void)
7729 {
7730   unsigned Rd, Rn;
7731
7732   Rd = inst.operands[2].reg;
7733   Rn = inst.operands[3].reg;
7734
7735   if (thumb_mode)
7736     {
7737       reject_bad_reg (Rd);
7738       reject_bad_reg (Rn);
7739     }
7740   else
7741     {
7742       constraint (Rd == REG_PC, BAD_PC);
7743       constraint (Rn == REG_PC, BAD_PC);
7744     }
7745
7746   inst.instruction |= inst.operands[0].reg << 8;
7747   inst.instruction |= inst.operands[1].imm << 4;
7748   inst.instruction |= Rd << 12;
7749   inst.instruction |= Rn << 16;
7750   inst.instruction |= inst.operands[4].reg;
7751 }
7752
7753 static void
7754 do_cpsi (void)
7755 {
7756   inst.instruction |= inst.operands[0].imm << 6;
7757   if (inst.operands[1].present)
7758     {
7759       inst.instruction |= CPSI_MMOD;
7760       inst.instruction |= inst.operands[1].imm;
7761     }
7762 }
7763
7764 static void
7765 do_dbg (void)
7766 {
7767   inst.instruction |= inst.operands[0].imm;
7768 }
7769
7770 static void
7771 do_div (void)
7772 {
7773   unsigned Rd, Rn, Rm;
7774
7775   Rd = inst.operands[0].reg;
7776   Rn = (inst.operands[1].present
7777         ? inst.operands[1].reg : Rd);
7778   Rm = inst.operands[2].reg;
7779
7780   constraint ((Rd == REG_PC), BAD_PC);
7781   constraint ((Rn == REG_PC), BAD_PC);
7782   constraint ((Rm == REG_PC), BAD_PC);
7783
7784   inst.instruction |= Rd << 16;
7785   inst.instruction |= Rn << 0;
7786   inst.instruction |= Rm << 8;
7787 }
7788
7789 static void
7790 do_it (void)
7791 {
7792   /* There is no IT instruction in ARM mode.  We
7793      process it to do the validation as if in
7794      thumb mode, just in case the code gets
7795      assembled for thumb using the unified syntax.  */
7796
7797   inst.size = 0;
7798   if (unified_syntax)
7799     {
7800       set_it_insn_type (IT_INSN);
7801       now_it.mask = (inst.instruction & 0xf) | 0x10;
7802       now_it.cc = inst.operands[0].imm;
7803     }
7804 }
7805
7806 /* If there is only one register in the register list,
7807    then return its register number.  Otherwise return -1.  */
7808 static int
7809 only_one_reg_in_list (int range)
7810 {
7811   int i = ffs (range) - 1;
7812   return (i > 15 || range != (1 << i)) ? -1 : i;
7813 }
7814
7815 static void
7816 encode_ldmstm(int from_push_pop_mnem)
7817 {
7818   int base_reg = inst.operands[0].reg;
7819   int range = inst.operands[1].imm;
7820   int one_reg;
7821
7822   inst.instruction |= base_reg << 16;
7823   inst.instruction |= range;
7824
7825   if (inst.operands[1].writeback)
7826     inst.instruction |= LDM_TYPE_2_OR_3;
7827
7828   if (inst.operands[0].writeback)
7829     {
7830       inst.instruction |= WRITE_BACK;
7831       /* Check for unpredictable uses of writeback.  */
7832       if (inst.instruction & LOAD_BIT)
7833         {
7834           /* Not allowed in LDM type 2.  */
7835           if ((inst.instruction & LDM_TYPE_2_OR_3)
7836               && ((range & (1 << REG_PC)) == 0))
7837             as_warn (_("writeback of base register is UNPREDICTABLE"));
7838           /* Only allowed if base reg not in list for other types.  */
7839           else if (range & (1 << base_reg))
7840             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
7841         }
7842       else /* STM.  */
7843         {
7844           /* Not allowed for type 2.  */
7845           if (inst.instruction & LDM_TYPE_2_OR_3)
7846             as_warn (_("writeback of base register is UNPREDICTABLE"));
7847           /* Only allowed if base reg not in list, or first in list.  */
7848           else if ((range & (1 << base_reg))
7849                    && (range & ((1 << base_reg) - 1)))
7850             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
7851         }
7852     }
7853
7854   /* If PUSH/POP has only one register, then use the A2 encoding.  */
7855   one_reg = only_one_reg_in_list (range);
7856   if (from_push_pop_mnem && one_reg >= 0)
7857     {
7858       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
7859
7860       inst.instruction &= A_COND_MASK;
7861       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
7862       inst.instruction |= one_reg << 12;
7863     }
7864 }
7865
7866 static void
7867 do_ldmstm (void)
7868 {
7869   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
7870 }
7871
7872 /* ARMv5TE load-consecutive (argument parse)
7873    Mode is like LDRH.
7874
7875      LDRccD R, mode
7876      STRccD R, mode.  */
7877
7878 static void
7879 do_ldrd (void)
7880 {
7881   constraint (inst.operands[0].reg % 2 != 0,
7882               _("first transfer register must be even"));
7883   constraint (inst.operands[1].present
7884               && inst.operands[1].reg != inst.operands[0].reg + 1,
7885               _("can only transfer two consecutive registers"));
7886   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
7887   constraint (!inst.operands[2].isreg, _("'[' expected"));
7888
7889   if (!inst.operands[1].present)
7890     inst.operands[1].reg = inst.operands[0].reg + 1;
7891
7892   /* encode_arm_addr_mode_3 will diagnose overlap between the base
7893      register and the first register written; we have to diagnose
7894      overlap between the base and the second register written here.  */
7895
7896   if (inst.operands[2].reg == inst.operands[1].reg
7897       && (inst.operands[2].writeback || inst.operands[2].postind))
7898     as_warn (_("base register written back, and overlaps "
7899                "second transfer register"));
7900
7901   if (!(inst.instruction & V4_STR_BIT))
7902     {
7903       /* For an index-register load, the index register must not overlap the
7904         destination (even if not write-back).  */
7905       if (inst.operands[2].immisreg
7906               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
7907               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
7908         as_warn (_("index register overlaps transfer register"));
7909     }
7910   inst.instruction |= inst.operands[0].reg << 12;
7911   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
7912 }
7913
7914 static void
7915 do_ldrex (void)
7916 {
7917   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
7918               || inst.operands[1].postind || inst.operands[1].writeback
7919               || inst.operands[1].immisreg || inst.operands[1].shifted
7920               || inst.operands[1].negative
7921               /* This can arise if the programmer has written
7922                    strex rN, rM, foo
7923                  or if they have mistakenly used a register name as the last
7924                  operand,  eg:
7925                    strex rN, rM, rX
7926                  It is very difficult to distinguish between these two cases
7927                  because "rX" might actually be a label. ie the register
7928                  name has been occluded by a symbol of the same name. So we
7929                  just generate a general 'bad addressing mode' type error
7930                  message and leave it up to the programmer to discover the
7931                  true cause and fix their mistake.  */
7932               || (inst.operands[1].reg == REG_PC),
7933               BAD_ADDR_MODE);
7934
7935   constraint (inst.reloc.exp.X_op != O_constant
7936               || inst.reloc.exp.X_add_number != 0,
7937               _("offset must be zero in ARM encoding"));
7938
7939   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
7940
7941   inst.instruction |= inst.operands[0].reg << 12;
7942   inst.instruction |= inst.operands[1].reg << 16;
7943   inst.reloc.type = BFD_RELOC_UNUSED;
7944 }
7945
7946 static void
7947 do_ldrexd (void)
7948 {
7949   constraint (inst.operands[0].reg % 2 != 0,
7950               _("even register required"));
7951   constraint (inst.operands[1].present
7952               && inst.operands[1].reg != inst.operands[0].reg + 1,
7953               _("can only load two consecutive registers"));
7954   /* If op 1 were present and equal to PC, this function wouldn't
7955      have been called in the first place.  */
7956   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
7957
7958   inst.instruction |= inst.operands[0].reg << 12;
7959   inst.instruction |= inst.operands[2].reg << 16;
7960 }
7961
7962 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
7963    which is not a multiple of four is UNPREDICTABLE.  */
7964 static void
7965 check_ldr_r15_aligned (void)
7966 {
7967   constraint (!(inst.operands[1].immisreg)
7968               && (inst.operands[0].reg == REG_PC
7969               && inst.operands[1].reg == REG_PC
7970               && (inst.reloc.exp.X_add_number & 0x3)),
7971               _("ldr to register 15 must be 4-byte alligned"));
7972 }
7973
7974 static void
7975 do_ldst (void)
7976 {
7977   inst.instruction |= inst.operands[0].reg << 12;
7978   if (!inst.operands[1].isreg)
7979     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/FALSE))
7980       return;
7981   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
7982   check_ldr_r15_aligned ();
7983 }
7984
7985 static void
7986 do_ldstt (void)
7987 {
7988   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
7989      reject [Rn,...].  */
7990   if (inst.operands[1].preind)
7991     {
7992       constraint (inst.reloc.exp.X_op != O_constant
7993                   || inst.reloc.exp.X_add_number != 0,
7994                   _("this instruction requires a post-indexed address"));
7995
7996       inst.operands[1].preind = 0;
7997       inst.operands[1].postind = 1;
7998       inst.operands[1].writeback = 1;
7999     }
8000   inst.instruction |= inst.operands[0].reg << 12;
8001   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
8002 }
8003
8004 /* Halfword and signed-byte load/store operations.  */
8005
8006 static void
8007 do_ldstv4 (void)
8008 {
8009   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8010   inst.instruction |= inst.operands[0].reg << 12;
8011   if (!inst.operands[1].isreg)
8012     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/TRUE))
8013       return;
8014   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
8015 }
8016
8017 static void
8018 do_ldsttv4 (void)
8019 {
8020   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8021      reject [Rn,...].  */
8022   if (inst.operands[1].preind)
8023     {
8024       constraint (inst.reloc.exp.X_op != O_constant
8025                   || inst.reloc.exp.X_add_number != 0,
8026                   _("this instruction requires a post-indexed address"));
8027
8028       inst.operands[1].preind = 0;
8029       inst.operands[1].postind = 1;
8030       inst.operands[1].writeback = 1;
8031     }
8032   inst.instruction |= inst.operands[0].reg << 12;
8033   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
8034 }
8035
8036 /* Co-processor register load/store.
8037    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
8038 static void
8039 do_lstc (void)
8040 {
8041   inst.instruction |= inst.operands[0].reg << 8;
8042   inst.instruction |= inst.operands[1].reg << 12;
8043   encode_arm_cp_address (2, TRUE, TRUE, 0);
8044 }
8045
8046 static void
8047 do_mlas (void)
8048 {
8049   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
8050   if (inst.operands[0].reg == inst.operands[1].reg
8051       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
8052       && !(inst.instruction & 0x00400000))
8053     as_tsktsk (_("Rd and Rm should be different in mla"));
8054
8055   inst.instruction |= inst.operands[0].reg << 16;
8056   inst.instruction |= inst.operands[1].reg;
8057   inst.instruction |= inst.operands[2].reg << 8;
8058   inst.instruction |= inst.operands[3].reg << 12;
8059 }
8060
8061 static void
8062 do_mov (void)
8063 {
8064   inst.instruction |= inst.operands[0].reg << 12;
8065   encode_arm_shifter_operand (1);
8066 }
8067
8068 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
8069 static void
8070 do_mov16 (void)
8071 {
8072   bfd_vma imm;
8073   bfd_boolean top;
8074
8075   top = (inst.instruction & 0x00400000) != 0;
8076   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
8077               _(":lower16: not allowed this instruction"));
8078   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
8079               _(":upper16: not allowed instruction"));
8080   inst.instruction |= inst.operands[0].reg << 12;
8081   if (inst.reloc.type == BFD_RELOC_UNUSED)
8082     {
8083       imm = inst.reloc.exp.X_add_number;
8084       /* The value is in two pieces: 0:11, 16:19.  */
8085       inst.instruction |= (imm & 0x00000fff);
8086       inst.instruction |= (imm & 0x0000f000) << 4;
8087     }
8088 }
8089
8090 static void do_vfp_nsyn_opcode (const char *);
8091
8092 static int
8093 do_vfp_nsyn_mrs (void)
8094 {
8095   if (inst.operands[0].isvec)
8096     {
8097       if (inst.operands[1].reg != 1)
8098         first_error (_("operand 1 must be FPSCR"));
8099       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
8100       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
8101       do_vfp_nsyn_opcode ("fmstat");
8102     }
8103   else if (inst.operands[1].isvec)
8104     do_vfp_nsyn_opcode ("fmrx");
8105   else
8106     return FAIL;
8107
8108   return SUCCESS;
8109 }
8110
8111 static int
8112 do_vfp_nsyn_msr (void)
8113 {
8114   if (inst.operands[0].isvec)
8115     do_vfp_nsyn_opcode ("fmxr");
8116   else
8117     return FAIL;
8118
8119   return SUCCESS;
8120 }
8121
8122 static void
8123 do_vmrs (void)
8124 {
8125   unsigned Rt = inst.operands[0].reg;
8126
8127   if (thumb_mode && inst.operands[0].reg == REG_SP)
8128     {
8129       inst.error = BAD_SP;
8130       return;
8131     }
8132
8133   /* APSR_ sets isvec. All other refs to PC are illegal.  */
8134   if (!inst.operands[0].isvec && inst.operands[0].reg == REG_PC)
8135     {
8136       inst.error = BAD_PC;
8137       return;
8138     }
8139
8140   switch (inst.operands[1].reg)
8141     {
8142     case 0: /* FPSID */
8143     case 1: /* FPSCR */
8144     case 6: /* MVFR1 */
8145     case 7: /* MVFR0 */
8146     case 8: /* FPEXC */
8147       inst.instruction |= (inst.operands[1].reg << 16);
8148       break;
8149     default:
8150       first_error (_("operand 1 must be a VFP extension System Register"));
8151     }
8152
8153   inst.instruction |= (Rt << 12);
8154 }
8155
8156 static void
8157 do_vmsr (void)
8158 {
8159   unsigned Rt = inst.operands[1].reg;
8160
8161   if (thumb_mode)
8162     reject_bad_reg (Rt);
8163   else if (Rt == REG_PC)
8164     {
8165       inst.error = BAD_PC;
8166       return;
8167     }
8168
8169   switch (inst.operands[0].reg)
8170     {
8171     case 0: /* FPSID  */
8172     case 1: /* FPSCR  */
8173     case 8: /* FPEXC */
8174       inst.instruction |= (inst.operands[0].reg << 16);
8175       break;
8176     default:
8177       first_error (_("operand 0 must be FPSID or FPSCR pr FPEXC"));
8178     }
8179
8180   inst.instruction |= (Rt << 12);
8181 }
8182
8183 static void
8184 do_mrs (void)
8185 {
8186   unsigned br;
8187
8188   if (do_vfp_nsyn_mrs () == SUCCESS)
8189     return;
8190
8191   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8192   inst.instruction |= inst.operands[0].reg << 12;
8193
8194   if (inst.operands[1].isreg)
8195     {
8196       br = inst.operands[1].reg;
8197       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf000))
8198         as_bad (_("bad register for mrs"));
8199     }
8200   else
8201     {
8202       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
8203       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
8204                   != (PSR_c|PSR_f),
8205                   _("'APSR', 'CPSR' or 'SPSR' expected"));
8206       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
8207     }
8208
8209   inst.instruction |= br;
8210 }
8211
8212 /* Two possible forms:
8213       "{C|S}PSR_<field>, Rm",
8214       "{C|S}PSR_f, #expression".  */
8215
8216 static void
8217 do_msr (void)
8218 {
8219   if (do_vfp_nsyn_msr () == SUCCESS)
8220     return;
8221
8222   inst.instruction |= inst.operands[0].imm;
8223   if (inst.operands[1].isreg)
8224     inst.instruction |= inst.operands[1].reg;
8225   else
8226     {
8227       inst.instruction |= INST_IMMEDIATE;
8228       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8229       inst.reloc.pc_rel = 0;
8230     }
8231 }
8232
8233 static void
8234 do_mul (void)
8235 {
8236   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
8237
8238   if (!inst.operands[2].present)
8239     inst.operands[2].reg = inst.operands[0].reg;
8240   inst.instruction |= inst.operands[0].reg << 16;
8241   inst.instruction |= inst.operands[1].reg;
8242   inst.instruction |= inst.operands[2].reg << 8;
8243
8244   if (inst.operands[0].reg == inst.operands[1].reg
8245       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8246     as_tsktsk (_("Rd and Rm should be different in mul"));
8247 }
8248
8249 /* Long Multiply Parser
8250    UMULL RdLo, RdHi, Rm, Rs
8251    SMULL RdLo, RdHi, Rm, Rs
8252    UMLAL RdLo, RdHi, Rm, Rs
8253    SMLAL RdLo, RdHi, Rm, Rs.  */
8254
8255 static void
8256 do_mull (void)
8257 {
8258   inst.instruction |= inst.operands[0].reg << 12;
8259   inst.instruction |= inst.operands[1].reg << 16;
8260   inst.instruction |= inst.operands[2].reg;
8261   inst.instruction |= inst.operands[3].reg << 8;
8262
8263   /* rdhi and rdlo must be different.  */
8264   if (inst.operands[0].reg == inst.operands[1].reg)
8265     as_tsktsk (_("rdhi and rdlo must be different"));
8266
8267   /* rdhi, rdlo and rm must all be different before armv6.  */
8268   if ((inst.operands[0].reg == inst.operands[2].reg
8269       || inst.operands[1].reg == inst.operands[2].reg)
8270       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8271     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
8272 }
8273
8274 static void
8275 do_nop (void)
8276 {
8277   if (inst.operands[0].present
8278       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
8279     {
8280       /* Architectural NOP hints are CPSR sets with no bits selected.  */
8281       inst.instruction &= 0xf0000000;
8282       inst.instruction |= 0x0320f000;
8283       if (inst.operands[0].present)
8284         inst.instruction |= inst.operands[0].imm;
8285     }
8286 }
8287
8288 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
8289    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
8290    Condition defaults to COND_ALWAYS.
8291    Error if Rd, Rn or Rm are R15.  */
8292
8293 static void
8294 do_pkhbt (void)
8295 {
8296   inst.instruction |= inst.operands[0].reg << 12;
8297   inst.instruction |= inst.operands[1].reg << 16;
8298   inst.instruction |= inst.operands[2].reg;
8299   if (inst.operands[3].present)
8300     encode_arm_shift (3);
8301 }
8302
8303 /* ARM V6 PKHTB (Argument Parse).  */
8304
8305 static void
8306 do_pkhtb (void)
8307 {
8308   if (!inst.operands[3].present)
8309     {
8310       /* If the shift specifier is omitted, turn the instruction
8311          into pkhbt rd, rm, rn. */
8312       inst.instruction &= 0xfff00010;
8313       inst.instruction |= inst.operands[0].reg << 12;
8314       inst.instruction |= inst.operands[1].reg;
8315       inst.instruction |= inst.operands[2].reg << 16;
8316     }
8317   else
8318     {
8319       inst.instruction |= inst.operands[0].reg << 12;
8320       inst.instruction |= inst.operands[1].reg << 16;
8321       inst.instruction |= inst.operands[2].reg;
8322       encode_arm_shift (3);
8323     }
8324 }
8325
8326 /* ARMv5TE: Preload-Cache
8327    MP Extensions: Preload for write
8328
8329     PLD(W) <addr_mode>
8330
8331   Syntactically, like LDR with B=1, W=0, L=1.  */
8332
8333 static void
8334 do_pld (void)
8335 {
8336   constraint (!inst.operands[0].isreg,
8337               _("'[' expected after PLD mnemonic"));
8338   constraint (inst.operands[0].postind,
8339               _("post-indexed expression used in preload instruction"));
8340   constraint (inst.operands[0].writeback,
8341               _("writeback used in preload instruction"));
8342   constraint (!inst.operands[0].preind,
8343               _("unindexed addressing used in preload instruction"));
8344   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
8345 }
8346
8347 /* ARMv7: PLI <addr_mode>  */
8348 static void
8349 do_pli (void)
8350 {
8351   constraint (!inst.operands[0].isreg,
8352               _("'[' expected after PLI mnemonic"));
8353   constraint (inst.operands[0].postind,
8354               _("post-indexed expression used in preload instruction"));
8355   constraint (inst.operands[0].writeback,
8356               _("writeback used in preload instruction"));
8357   constraint (!inst.operands[0].preind,
8358               _("unindexed addressing used in preload instruction"));
8359   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
8360   inst.instruction &= ~PRE_INDEX;
8361 }
8362
8363 static void
8364 do_push_pop (void)
8365 {
8366   inst.operands[1] = inst.operands[0];
8367   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
8368   inst.operands[0].isreg = 1;
8369   inst.operands[0].writeback = 1;
8370   inst.operands[0].reg = REG_SP;
8371   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
8372 }
8373
8374 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
8375    word at the specified address and the following word
8376    respectively.
8377    Unconditionally executed.
8378    Error if Rn is R15.  */
8379
8380 static void
8381 do_rfe (void)
8382 {
8383   inst.instruction |= inst.operands[0].reg << 16;
8384   if (inst.operands[0].writeback)
8385     inst.instruction |= WRITE_BACK;
8386 }
8387
8388 /* ARM V6 ssat (argument parse).  */
8389
8390 static void
8391 do_ssat (void)
8392 {
8393   inst.instruction |= inst.operands[0].reg << 12;
8394   inst.instruction |= (inst.operands[1].imm - 1) << 16;
8395   inst.instruction |= inst.operands[2].reg;
8396
8397   if (inst.operands[3].present)
8398     encode_arm_shift (3);
8399 }
8400
8401 /* ARM V6 usat (argument parse).  */
8402
8403 static void
8404 do_usat (void)
8405 {
8406   inst.instruction |= inst.operands[0].reg << 12;
8407   inst.instruction |= inst.operands[1].imm << 16;
8408   inst.instruction |= inst.operands[2].reg;
8409
8410   if (inst.operands[3].present)
8411     encode_arm_shift (3);
8412 }
8413
8414 /* ARM V6 ssat16 (argument parse).  */
8415
8416 static void
8417 do_ssat16 (void)
8418 {
8419   inst.instruction |= inst.operands[0].reg << 12;
8420   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
8421   inst.instruction |= inst.operands[2].reg;
8422 }
8423
8424 static void
8425 do_usat16 (void)
8426 {
8427   inst.instruction |= inst.operands[0].reg << 12;
8428   inst.instruction |= inst.operands[1].imm << 16;
8429   inst.instruction |= inst.operands[2].reg;
8430 }
8431
8432 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
8433    preserving the other bits.
8434
8435    setend <endian_specifier>, where <endian_specifier> is either
8436    BE or LE.  */
8437
8438 static void
8439 do_setend (void)
8440 {
8441   if (inst.operands[0].imm)
8442     inst.instruction |= 0x200;
8443 }
8444
8445 static void
8446 do_shift (void)
8447 {
8448   unsigned int Rm = (inst.operands[1].present
8449                      ? inst.operands[1].reg
8450                      : inst.operands[0].reg);
8451
8452   inst.instruction |= inst.operands[0].reg << 12;
8453   inst.instruction |= Rm;
8454   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
8455     {
8456       inst.instruction |= inst.operands[2].reg << 8;
8457       inst.instruction |= SHIFT_BY_REG;
8458       /* PR 12854: Error on extraneous shifts.  */
8459       constraint (inst.operands[2].shifted,
8460                   _("extraneous shift as part of operand to shift insn"));
8461     }
8462   else
8463     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
8464 }
8465
8466 static void
8467 do_smc (void)
8468 {
8469   inst.reloc.type = BFD_RELOC_ARM_SMC;
8470   inst.reloc.pc_rel = 0;
8471 }
8472
8473 static void
8474 do_hvc (void)
8475 {
8476   inst.reloc.type = BFD_RELOC_ARM_HVC;
8477   inst.reloc.pc_rel = 0;
8478 }
8479
8480 static void
8481 do_swi (void)
8482 {
8483   inst.reloc.type = BFD_RELOC_ARM_SWI;
8484   inst.reloc.pc_rel = 0;
8485 }
8486
8487 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
8488    SMLAxy{cond} Rd,Rm,Rs,Rn
8489    SMLAWy{cond} Rd,Rm,Rs,Rn
8490    Error if any register is R15.  */
8491
8492 static void
8493 do_smla (void)
8494 {
8495   inst.instruction |= inst.operands[0].reg << 16;
8496   inst.instruction |= inst.operands[1].reg;
8497   inst.instruction |= inst.operands[2].reg << 8;
8498   inst.instruction |= inst.operands[3].reg << 12;
8499 }
8500
8501 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
8502    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
8503    Error if any register is R15.
8504    Warning if Rdlo == Rdhi.  */
8505
8506 static void
8507 do_smlal (void)
8508 {
8509   inst.instruction |= inst.operands[0].reg << 12;
8510   inst.instruction |= inst.operands[1].reg << 16;
8511   inst.instruction |= inst.operands[2].reg;
8512   inst.instruction |= inst.operands[3].reg << 8;
8513
8514   if (inst.operands[0].reg == inst.operands[1].reg)
8515     as_tsktsk (_("rdhi and rdlo must be different"));
8516 }
8517
8518 /* ARM V5E (El Segundo) signed-multiply (argument parse)
8519    SMULxy{cond} Rd,Rm,Rs
8520    Error if any register is R15.  */
8521
8522 static void
8523 do_smul (void)
8524 {
8525   inst.instruction |= inst.operands[0].reg << 16;
8526   inst.instruction |= inst.operands[1].reg;
8527   inst.instruction |= inst.operands[2].reg << 8;
8528 }
8529
8530 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
8531    the same for both ARM and Thumb-2.  */
8532
8533 static void
8534 do_srs (void)
8535 {
8536   int reg;
8537
8538   if (inst.operands[0].present)
8539     {
8540       reg = inst.operands[0].reg;
8541       constraint (reg != REG_SP, _("SRS base register must be r13"));
8542     }
8543   else
8544     reg = REG_SP;
8545
8546   inst.instruction |= reg << 16;
8547   inst.instruction |= inst.operands[1].imm;
8548   if (inst.operands[0].writeback || inst.operands[1].writeback)
8549     inst.instruction |= WRITE_BACK;
8550 }
8551
8552 /* ARM V6 strex (argument parse).  */
8553
8554 static void
8555 do_strex (void)
8556 {
8557   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
8558               || inst.operands[2].postind || inst.operands[2].writeback
8559               || inst.operands[2].immisreg || inst.operands[2].shifted
8560               || inst.operands[2].negative
8561               /* See comment in do_ldrex().  */
8562               || (inst.operands[2].reg == REG_PC),
8563               BAD_ADDR_MODE);
8564
8565   constraint (inst.operands[0].reg == inst.operands[1].reg
8566               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
8567
8568   constraint (inst.reloc.exp.X_op != O_constant
8569               || inst.reloc.exp.X_add_number != 0,
8570               _("offset must be zero in ARM encoding"));
8571
8572   inst.instruction |= inst.operands[0].reg << 12;
8573   inst.instruction |= inst.operands[1].reg;
8574   inst.instruction |= inst.operands[2].reg << 16;
8575   inst.reloc.type = BFD_RELOC_UNUSED;
8576 }
8577
8578 static void
8579 do_t_strexbh (void)
8580 {
8581   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
8582               || inst.operands[2].postind || inst.operands[2].writeback
8583               || inst.operands[2].immisreg || inst.operands[2].shifted
8584               || inst.operands[2].negative,
8585               BAD_ADDR_MODE);
8586
8587   constraint (inst.operands[0].reg == inst.operands[1].reg
8588               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
8589
8590   do_rm_rd_rn ();
8591 }
8592
8593 static void
8594 do_strexd (void)
8595 {
8596   constraint (inst.operands[1].reg % 2 != 0,
8597               _("even register required"));
8598   constraint (inst.operands[2].present
8599               && inst.operands[2].reg != inst.operands[1].reg + 1,
8600               _("can only store two consecutive registers"));
8601   /* If op 2 were present and equal to PC, this function wouldn't
8602      have been called in the first place.  */
8603   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
8604
8605   constraint (inst.operands[0].reg == inst.operands[1].reg
8606               || inst.operands[0].reg == inst.operands[1].reg + 1
8607               || inst.operands[0].reg == inst.operands[3].reg,
8608               BAD_OVERLAP);
8609
8610   inst.instruction |= inst.operands[0].reg << 12;
8611   inst.instruction |= inst.operands[1].reg;
8612   inst.instruction |= inst.operands[3].reg << 16;
8613 }
8614
8615 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
8616    extends it to 32-bits, and adds the result to a value in another
8617    register.  You can specify a rotation by 0, 8, 16, or 24 bits
8618    before extracting the 16-bit value.
8619    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
8620    Condition defaults to COND_ALWAYS.
8621    Error if any register uses R15.  */
8622
8623 static void
8624 do_sxtah (void)
8625 {
8626   inst.instruction |= inst.operands[0].reg << 12;
8627   inst.instruction |= inst.operands[1].reg << 16;
8628   inst.instruction |= inst.operands[2].reg;
8629   inst.instruction |= inst.operands[3].imm << 10;
8630 }
8631
8632 /* ARM V6 SXTH.
8633
8634    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
8635    Condition defaults to COND_ALWAYS.
8636    Error if any register uses R15.  */
8637
8638 static void
8639 do_sxth (void)
8640 {
8641   inst.instruction |= inst.operands[0].reg << 12;
8642   inst.instruction |= inst.operands[1].reg;
8643   inst.instruction |= inst.operands[2].imm << 10;
8644 }
8645 \f
8646 /* VFP instructions.  In a logical order: SP variant first, monad
8647    before dyad, arithmetic then move then load/store.  */
8648
8649 static void
8650 do_vfp_sp_monadic (void)
8651 {
8652   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8653   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
8654 }
8655
8656 static void
8657 do_vfp_sp_dyadic (void)
8658 {
8659   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8660   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
8661   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
8662 }
8663
8664 static void
8665 do_vfp_sp_compare_z (void)
8666 {
8667   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8668 }
8669
8670 static void
8671 do_vfp_dp_sp_cvt (void)
8672 {
8673   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8674   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
8675 }
8676
8677 static void
8678 do_vfp_sp_dp_cvt (void)
8679 {
8680   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8681   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
8682 }
8683
8684 static void
8685 do_vfp_reg_from_sp (void)
8686 {
8687   inst.instruction |= inst.operands[0].reg << 12;
8688   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
8689 }
8690
8691 static void
8692 do_vfp_reg2_from_sp2 (void)
8693 {
8694   constraint (inst.operands[2].imm != 2,
8695               _("only two consecutive VFP SP registers allowed here"));
8696   inst.instruction |= inst.operands[0].reg << 12;
8697   inst.instruction |= inst.operands[1].reg << 16;
8698   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
8699 }
8700
8701 static void
8702 do_vfp_sp_from_reg (void)
8703 {
8704   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
8705   inst.instruction |= inst.operands[1].reg << 12;
8706 }
8707
8708 static void
8709 do_vfp_sp2_from_reg2 (void)
8710 {
8711   constraint (inst.operands[0].imm != 2,
8712               _("only two consecutive VFP SP registers allowed here"));
8713   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
8714   inst.instruction |= inst.operands[1].reg << 12;
8715   inst.instruction |= inst.operands[2].reg << 16;
8716 }
8717
8718 static void
8719 do_vfp_sp_ldst (void)
8720 {
8721   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8722   encode_arm_cp_address (1, FALSE, TRUE, 0);
8723 }
8724
8725 static void
8726 do_vfp_dp_ldst (void)
8727 {
8728   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8729   encode_arm_cp_address (1, FALSE, TRUE, 0);
8730 }
8731
8732
8733 static void
8734 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
8735 {
8736   if (inst.operands[0].writeback)
8737     inst.instruction |= WRITE_BACK;
8738   else
8739     constraint (ldstm_type != VFP_LDSTMIA,
8740                 _("this addressing mode requires base-register writeback"));
8741   inst.instruction |= inst.operands[0].reg << 16;
8742   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
8743   inst.instruction |= inst.operands[1].imm;
8744 }
8745
8746 static void
8747 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
8748 {
8749   int count;
8750
8751   if (inst.operands[0].writeback)
8752     inst.instruction |= WRITE_BACK;
8753   else
8754     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
8755                 _("this addressing mode requires base-register writeback"));
8756
8757   inst.instruction |= inst.operands[0].reg << 16;
8758   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8759
8760   count = inst.operands[1].imm << 1;
8761   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
8762     count += 1;
8763
8764   inst.instruction |= count;
8765 }
8766
8767 static void
8768 do_vfp_sp_ldstmia (void)
8769 {
8770   vfp_sp_ldstm (VFP_LDSTMIA);
8771 }
8772
8773 static void
8774 do_vfp_sp_ldstmdb (void)
8775 {
8776   vfp_sp_ldstm (VFP_LDSTMDB);
8777 }
8778
8779 static void
8780 do_vfp_dp_ldstmia (void)
8781 {
8782   vfp_dp_ldstm (VFP_LDSTMIA);
8783 }
8784
8785 static void
8786 do_vfp_dp_ldstmdb (void)
8787 {
8788   vfp_dp_ldstm (VFP_LDSTMDB);
8789 }
8790
8791 static void
8792 do_vfp_xp_ldstmia (void)
8793 {
8794   vfp_dp_ldstm (VFP_LDSTMIAX);
8795 }
8796
8797 static void
8798 do_vfp_xp_ldstmdb (void)
8799 {
8800   vfp_dp_ldstm (VFP_LDSTMDBX);
8801 }
8802
8803 static void
8804 do_vfp_dp_rd_rm (void)
8805 {
8806   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8807   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
8808 }
8809
8810 static void
8811 do_vfp_dp_rn_rd (void)
8812 {
8813   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
8814   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8815 }
8816
8817 static void
8818 do_vfp_dp_rd_rn (void)
8819 {
8820   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8821   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
8822 }
8823
8824 static void
8825 do_vfp_dp_rd_rn_rm (void)
8826 {
8827   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8828   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
8829   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
8830 }
8831
8832 static void
8833 do_vfp_dp_rd (void)
8834 {
8835   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8836 }
8837
8838 static void
8839 do_vfp_dp_rm_rd_rn (void)
8840 {
8841   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
8842   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8843   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
8844 }
8845
8846 /* VFPv3 instructions.  */
8847 static void
8848 do_vfp_sp_const (void)
8849 {
8850   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8851   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
8852   inst.instruction |= (inst.operands[1].imm & 0x0f);
8853 }
8854
8855 static void
8856 do_vfp_dp_const (void)
8857 {
8858   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8859   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
8860   inst.instruction |= (inst.operands[1].imm & 0x0f);
8861 }
8862
8863 static void
8864 vfp_conv (int srcsize)
8865 {
8866   int immbits = srcsize - inst.operands[1].imm;
8867
8868   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
8869     {
8870       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
8871          i.e. immbits must be in range 0 - 16.  */
8872       inst.error = _("immediate value out of range, expected range [0, 16]");
8873       return;
8874     }
8875   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
8876     {
8877       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
8878          i.e. immbits must be in range 0 - 31.  */
8879       inst.error = _("immediate value out of range, expected range [1, 32]");
8880       return;
8881     }
8882
8883   inst.instruction |= (immbits & 1) << 5;
8884   inst.instruction |= (immbits >> 1);
8885 }
8886
8887 static void
8888 do_vfp_sp_conv_16 (void)
8889 {
8890   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8891   vfp_conv (16);
8892 }
8893
8894 static void
8895 do_vfp_dp_conv_16 (void)
8896 {
8897   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8898   vfp_conv (16);
8899 }
8900
8901 static void
8902 do_vfp_sp_conv_32 (void)
8903 {
8904   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8905   vfp_conv (32);
8906 }
8907
8908 static void
8909 do_vfp_dp_conv_32 (void)
8910 {
8911   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8912   vfp_conv (32);
8913 }
8914 \f
8915 /* FPA instructions.  Also in a logical order.  */
8916
8917 static void
8918 do_fpa_cmp (void)
8919 {
8920   inst.instruction |= inst.operands[0].reg << 16;
8921   inst.instruction |= inst.operands[1].reg;
8922 }
8923
8924 static void
8925 do_fpa_ldmstm (void)
8926 {
8927   inst.instruction |= inst.operands[0].reg << 12;
8928   switch (inst.operands[1].imm)
8929     {
8930     case 1: inst.instruction |= CP_T_X;          break;
8931     case 2: inst.instruction |= CP_T_Y;          break;
8932     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
8933     case 4:                                      break;
8934     default: abort ();
8935     }
8936
8937   if (inst.instruction & (PRE_INDEX | INDEX_UP))
8938     {
8939       /* The instruction specified "ea" or "fd", so we can only accept
8940          [Rn]{!}.  The instruction does not really support stacking or
8941          unstacking, so we have to emulate these by setting appropriate
8942          bits and offsets.  */
8943       constraint (inst.reloc.exp.X_op != O_constant
8944                   || inst.reloc.exp.X_add_number != 0,
8945                   _("this instruction does not support indexing"));
8946
8947       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
8948         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
8949
8950       if (!(inst.instruction & INDEX_UP))
8951         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
8952
8953       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
8954         {
8955           inst.operands[2].preind = 0;
8956           inst.operands[2].postind = 1;
8957         }
8958     }
8959
8960   encode_arm_cp_address (2, TRUE, TRUE, 0);
8961 }
8962 \f
8963 /* iWMMXt instructions: strictly in alphabetical order.  */
8964
8965 static void
8966 do_iwmmxt_tandorc (void)
8967 {
8968   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
8969 }
8970
8971 static void
8972 do_iwmmxt_textrc (void)
8973 {
8974   inst.instruction |= inst.operands[0].reg << 12;
8975   inst.instruction |= inst.operands[1].imm;
8976 }
8977
8978 static void
8979 do_iwmmxt_textrm (void)
8980 {
8981   inst.instruction |= inst.operands[0].reg << 12;
8982   inst.instruction |= inst.operands[1].reg << 16;
8983   inst.instruction |= inst.operands[2].imm;
8984 }
8985
8986 static void
8987 do_iwmmxt_tinsr (void)
8988 {
8989   inst.instruction |= inst.operands[0].reg << 16;
8990   inst.instruction |= inst.operands[1].reg << 12;
8991   inst.instruction |= inst.operands[2].imm;
8992 }
8993
8994 static void
8995 do_iwmmxt_tmia (void)
8996 {
8997   inst.instruction |= inst.operands[0].reg << 5;
8998   inst.instruction |= inst.operands[1].reg;
8999   inst.instruction |= inst.operands[2].reg << 12;
9000 }
9001
9002 static void
9003 do_iwmmxt_waligni (void)
9004 {
9005   inst.instruction |= inst.operands[0].reg << 12;
9006   inst.instruction |= inst.operands[1].reg << 16;
9007   inst.instruction |= inst.operands[2].reg;
9008   inst.instruction |= inst.operands[3].imm << 20;
9009 }
9010
9011 static void
9012 do_iwmmxt_wmerge (void)
9013 {
9014   inst.instruction |= inst.operands[0].reg << 12;
9015   inst.instruction |= inst.operands[1].reg << 16;
9016   inst.instruction |= inst.operands[2].reg;
9017   inst.instruction |= inst.operands[3].imm << 21;
9018 }
9019
9020 static void
9021 do_iwmmxt_wmov (void)
9022 {
9023   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
9024   inst.instruction |= inst.operands[0].reg << 12;
9025   inst.instruction |= inst.operands[1].reg << 16;
9026   inst.instruction |= inst.operands[1].reg;
9027 }
9028
9029 static void
9030 do_iwmmxt_wldstbh (void)
9031 {
9032   int reloc;
9033   inst.instruction |= inst.operands[0].reg << 12;
9034   if (thumb_mode)
9035     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
9036   else
9037     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
9038   encode_arm_cp_address (1, TRUE, FALSE, reloc);
9039 }
9040
9041 static void
9042 do_iwmmxt_wldstw (void)
9043 {
9044   /* RIWR_RIWC clears .isreg for a control register.  */
9045   if (!inst.operands[0].isreg)
9046     {
9047       constraint (inst.cond != COND_ALWAYS, BAD_COND);
9048       inst.instruction |= 0xf0000000;
9049     }
9050
9051   inst.instruction |= inst.operands[0].reg << 12;
9052   encode_arm_cp_address (1, TRUE, TRUE, 0);
9053 }
9054
9055 static void
9056 do_iwmmxt_wldstd (void)
9057 {
9058   inst.instruction |= inst.operands[0].reg << 12;
9059   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
9060       && inst.operands[1].immisreg)
9061     {
9062       inst.instruction &= ~0x1a000ff;
9063       inst.instruction |= (0xf << 28);
9064       if (inst.operands[1].preind)
9065         inst.instruction |= PRE_INDEX;
9066       if (!inst.operands[1].negative)
9067         inst.instruction |= INDEX_UP;
9068       if (inst.operands[1].writeback)
9069         inst.instruction |= WRITE_BACK;
9070       inst.instruction |= inst.operands[1].reg << 16;
9071       inst.instruction |= inst.reloc.exp.X_add_number << 4;
9072       inst.instruction |= inst.operands[1].imm;
9073     }
9074   else
9075     encode_arm_cp_address (1, TRUE, FALSE, 0);
9076 }
9077
9078 static void
9079 do_iwmmxt_wshufh (void)
9080 {
9081   inst.instruction |= inst.operands[0].reg << 12;
9082   inst.instruction |= inst.operands[1].reg << 16;
9083   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
9084   inst.instruction |= (inst.operands[2].imm & 0x0f);
9085 }
9086
9087 static void
9088 do_iwmmxt_wzero (void)
9089 {
9090   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
9091   inst.instruction |= inst.operands[0].reg;
9092   inst.instruction |= inst.operands[0].reg << 12;
9093   inst.instruction |= inst.operands[0].reg << 16;
9094 }
9095
9096 static void
9097 do_iwmmxt_wrwrwr_or_imm5 (void)
9098 {
9099   if (inst.operands[2].isreg)
9100     do_rd_rn_rm ();
9101   else {
9102     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
9103                 _("immediate operand requires iWMMXt2"));
9104     do_rd_rn ();
9105     if (inst.operands[2].imm == 0)
9106       {
9107         switch ((inst.instruction >> 20) & 0xf)
9108           {
9109           case 4:
9110           case 5:
9111           case 6:
9112           case 7:
9113             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
9114             inst.operands[2].imm = 16;
9115             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
9116             break;
9117           case 8:
9118           case 9:
9119           case 10:
9120           case 11:
9121             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
9122             inst.operands[2].imm = 32;
9123             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
9124             break;
9125           case 12:
9126           case 13:
9127           case 14:
9128           case 15:
9129             {
9130               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
9131               unsigned long wrn;
9132               wrn = (inst.instruction >> 16) & 0xf;
9133               inst.instruction &= 0xff0fff0f;
9134               inst.instruction |= wrn;
9135               /* Bail out here; the instruction is now assembled.  */
9136               return;
9137             }
9138           }
9139       }
9140     /* Map 32 -> 0, etc.  */
9141     inst.operands[2].imm &= 0x1f;
9142     inst.instruction |= (0xf << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
9143   }
9144 }
9145 \f
9146 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
9147    operations first, then control, shift, and load/store.  */
9148
9149 /* Insns like "foo X,Y,Z".  */
9150
9151 static void
9152 do_mav_triple (void)
9153 {
9154   inst.instruction |= inst.operands[0].reg << 16;
9155   inst.instruction |= inst.operands[1].reg;
9156   inst.instruction |= inst.operands[2].reg << 12;
9157 }
9158
9159 /* Insns like "foo W,X,Y,Z".
9160     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
9161
9162 static void
9163 do_mav_quad (void)
9164 {
9165   inst.instruction |= inst.operands[0].reg << 5;
9166   inst.instruction |= inst.operands[1].reg << 12;
9167   inst.instruction |= inst.operands[2].reg << 16;
9168   inst.instruction |= inst.operands[3].reg;
9169 }
9170
9171 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
9172 static void
9173 do_mav_dspsc (void)
9174 {
9175   inst.instruction |= inst.operands[1].reg << 12;
9176 }
9177
9178 /* Maverick shift immediate instructions.
9179    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
9180    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
9181
9182 static void
9183 do_mav_shift (void)
9184 {
9185   int imm = inst.operands[2].imm;
9186
9187   inst.instruction |= inst.operands[0].reg << 12;
9188   inst.instruction |= inst.operands[1].reg << 16;
9189
9190   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
9191      Bits 5-7 of the insn should have bits 4-6 of the immediate.
9192      Bit 4 should be 0.  */
9193   imm = (imm & 0xf) | ((imm & 0x70) << 1);
9194
9195   inst.instruction |= imm;
9196 }
9197 \f
9198 /* XScale instructions.  Also sorted arithmetic before move.  */
9199
9200 /* Xscale multiply-accumulate (argument parse)
9201      MIAcc   acc0,Rm,Rs
9202      MIAPHcc acc0,Rm,Rs
9203      MIAxycc acc0,Rm,Rs.  */
9204
9205 static void
9206 do_xsc_mia (void)
9207 {
9208   inst.instruction |= inst.operands[1].reg;
9209   inst.instruction |= inst.operands[2].reg << 12;
9210 }
9211
9212 /* Xscale move-accumulator-register (argument parse)
9213
9214      MARcc   acc0,RdLo,RdHi.  */
9215
9216 static void
9217 do_xsc_mar (void)
9218 {
9219   inst.instruction |= inst.operands[1].reg << 12;
9220   inst.instruction |= inst.operands[2].reg << 16;
9221 }
9222
9223 /* Xscale move-register-accumulator (argument parse)
9224
9225      MRAcc   RdLo,RdHi,acc0.  */
9226
9227 static void
9228 do_xsc_mra (void)
9229 {
9230   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
9231   inst.instruction |= inst.operands[0].reg << 12;
9232   inst.instruction |= inst.operands[1].reg << 16;
9233 }
9234 \f
9235 /* Encoding functions relevant only to Thumb.  */
9236
9237 /* inst.operands[i] is a shifted-register operand; encode
9238    it into inst.instruction in the format used by Thumb32.  */
9239
9240 static void
9241 encode_thumb32_shifted_operand (int i)
9242 {
9243   unsigned int value = inst.reloc.exp.X_add_number;
9244   unsigned int shift = inst.operands[i].shift_kind;
9245
9246   constraint (inst.operands[i].immisreg,
9247               _("shift by register not allowed in thumb mode"));
9248   inst.instruction |= inst.operands[i].reg;
9249   if (shift == SHIFT_RRX)
9250     inst.instruction |= SHIFT_ROR << 4;
9251   else
9252     {
9253       constraint (inst.reloc.exp.X_op != O_constant,
9254                   _("expression too complex"));
9255
9256       constraint (value > 32
9257                   || (value == 32 && (shift == SHIFT_LSL
9258                                       || shift == SHIFT_ROR)),
9259                   _("shift expression is too large"));
9260
9261       if (value == 0)
9262         shift = SHIFT_LSL;
9263       else if (value == 32)
9264         value = 0;
9265
9266       inst.instruction |= shift << 4;
9267       inst.instruction |= (value & 0x1c) << 10;
9268       inst.instruction |= (value & 0x03) << 6;
9269     }
9270 }
9271
9272
9273 /* inst.operands[i] was set up by parse_address.  Encode it into a
9274    Thumb32 format load or store instruction.  Reject forms that cannot
9275    be used with such instructions.  If is_t is true, reject forms that
9276    cannot be used with a T instruction; if is_d is true, reject forms
9277    that cannot be used with a D instruction.  If it is a store insn,
9278    reject PC in Rn.  */
9279
9280 static void
9281 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
9282 {
9283   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
9284
9285   constraint (!inst.operands[i].isreg,
9286               _("Instruction does not support =N addresses"));
9287
9288   inst.instruction |= inst.operands[i].reg << 16;
9289   if (inst.operands[i].immisreg)
9290     {
9291       constraint (is_pc, BAD_PC_ADDRESSING);
9292       constraint (is_t || is_d, _("cannot use register index with this instruction"));
9293       constraint (inst.operands[i].negative,
9294                   _("Thumb does not support negative register indexing"));
9295       constraint (inst.operands[i].postind,
9296                   _("Thumb does not support register post-indexing"));
9297       constraint (inst.operands[i].writeback,
9298                   _("Thumb does not support register indexing with writeback"));
9299       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
9300                   _("Thumb supports only LSL in shifted register indexing"));
9301
9302       inst.instruction |= inst.operands[i].imm;
9303       if (inst.operands[i].shifted)
9304         {
9305           constraint (inst.reloc.exp.X_op != O_constant,
9306                       _("expression too complex"));
9307           constraint (inst.reloc.exp.X_add_number < 0
9308                       || inst.reloc.exp.X_add_number > 3,
9309                       _("shift out of range"));
9310           inst.instruction |= inst.reloc.exp.X_add_number << 4;
9311         }
9312       inst.reloc.type = BFD_RELOC_UNUSED;
9313     }
9314   else if (inst.operands[i].preind)
9315     {
9316       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
9317       constraint (is_t && inst.operands[i].writeback,
9318                   _("cannot use writeback with this instruction"));
9319       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0)
9320                   && !inst.reloc.pc_rel, BAD_PC_ADDRESSING);
9321
9322       if (is_d)
9323         {
9324           inst.instruction |= 0x01000000;
9325           if (inst.operands[i].writeback)
9326             inst.instruction |= 0x00200000;
9327         }
9328       else
9329         {
9330           inst.instruction |= 0x00000c00;
9331           if (inst.operands[i].writeback)
9332             inst.instruction |= 0x00000100;
9333         }
9334       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
9335     }
9336   else if (inst.operands[i].postind)
9337     {
9338       gas_assert (inst.operands[i].writeback);
9339       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
9340       constraint (is_t, _("cannot use post-indexing with this instruction"));
9341
9342       if (is_d)
9343         inst.instruction |= 0x00200000;
9344       else
9345         inst.instruction |= 0x00000900;
9346       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
9347     }
9348   else /* unindexed - only for coprocessor */
9349     inst.error = _("instruction does not accept unindexed addressing");
9350 }
9351
9352 /* Table of Thumb instructions which exist in both 16- and 32-bit
9353    encodings (the latter only in post-V6T2 cores).  The index is the
9354    value used in the insns table below.  When there is more than one
9355    possible 16-bit encoding for the instruction, this table always
9356    holds variant (1).
9357    Also contains several pseudo-instructions used during relaxation.  */
9358 #define T16_32_TAB                              \
9359   X(_adc,   4140, eb400000),                    \
9360   X(_adcs,  4140, eb500000),                    \
9361   X(_add,   1c00, eb000000),                    \
9362   X(_adds,  1c00, eb100000),                    \
9363   X(_addi,  0000, f1000000),                    \
9364   X(_addis, 0000, f1100000),                    \
9365   X(_add_pc,000f, f20f0000),                    \
9366   X(_add_sp,000d, f10d0000),                    \
9367   X(_adr,   000f, f20f0000),                    \
9368   X(_and,   4000, ea000000),                    \
9369   X(_ands,  4000, ea100000),                    \
9370   X(_asr,   1000, fa40f000),                    \
9371   X(_asrs,  1000, fa50f000),                    \
9372   X(_b,     e000, f000b000),                    \
9373   X(_bcond, d000, f0008000),                    \
9374   X(_bic,   4380, ea200000),                    \
9375   X(_bics,  4380, ea300000),                    \
9376   X(_cmn,   42c0, eb100f00),                    \
9377   X(_cmp,   2800, ebb00f00),                    \
9378   X(_cpsie, b660, f3af8400),                    \
9379   X(_cpsid, b670, f3af8600),                    \
9380   X(_cpy,   4600, ea4f0000),                    \
9381   X(_dec_sp,80dd, f1ad0d00),                    \
9382   X(_eor,   4040, ea800000),                    \
9383   X(_eors,  4040, ea900000),                    \
9384   X(_inc_sp,00dd, f10d0d00),                    \
9385   X(_ldmia, c800, e8900000),                    \
9386   X(_ldr,   6800, f8500000),                    \
9387   X(_ldrb,  7800, f8100000),                    \
9388   X(_ldrh,  8800, f8300000),                    \
9389   X(_ldrsb, 5600, f9100000),                    \
9390   X(_ldrsh, 5e00, f9300000),                    \
9391   X(_ldr_pc,4800, f85f0000),                    \
9392   X(_ldr_pc2,4800, f85f0000),                   \
9393   X(_ldr_sp,9800, f85d0000),                    \
9394   X(_lsl,   0000, fa00f000),                    \
9395   X(_lsls,  0000, fa10f000),                    \
9396   X(_lsr,   0800, fa20f000),                    \
9397   X(_lsrs,  0800, fa30f000),                    \
9398   X(_mov,   2000, ea4f0000),                    \
9399   X(_movs,  2000, ea5f0000),                    \
9400   X(_mul,   4340, fb00f000),                     \
9401   X(_muls,  4340, ffffffff), /* no 32b muls */  \
9402   X(_mvn,   43c0, ea6f0000),                    \
9403   X(_mvns,  43c0, ea7f0000),                    \
9404   X(_neg,   4240, f1c00000), /* rsb #0 */       \
9405   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
9406   X(_orr,   4300, ea400000),                    \
9407   X(_orrs,  4300, ea500000),                    \
9408   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
9409   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
9410   X(_rev,   ba00, fa90f080),                    \
9411   X(_rev16, ba40, fa90f090),                    \
9412   X(_revsh, bac0, fa90f0b0),                    \
9413   X(_ror,   41c0, fa60f000),                    \
9414   X(_rors,  41c0, fa70f000),                    \
9415   X(_sbc,   4180, eb600000),                    \
9416   X(_sbcs,  4180, eb700000),                    \
9417   X(_stmia, c000, e8800000),                    \
9418   X(_str,   6000, f8400000),                    \
9419   X(_strb,  7000, f8000000),                    \
9420   X(_strh,  8000, f8200000),                    \
9421   X(_str_sp,9000, f84d0000),                    \
9422   X(_sub,   1e00, eba00000),                    \
9423   X(_subs,  1e00, ebb00000),                    \
9424   X(_subi,  8000, f1a00000),                    \
9425   X(_subis, 8000, f1b00000),                    \
9426   X(_sxtb,  b240, fa4ff080),                    \
9427   X(_sxth,  b200, fa0ff080),                    \
9428   X(_tst,   4200, ea100f00),                    \
9429   X(_uxtb,  b2c0, fa5ff080),                    \
9430   X(_uxth,  b280, fa1ff080),                    \
9431   X(_nop,   bf00, f3af8000),                    \
9432   X(_yield, bf10, f3af8001),                    \
9433   X(_wfe,   bf20, f3af8002),                    \
9434   X(_wfi,   bf30, f3af8003),                    \
9435   X(_sev,   bf40, f3af8004),
9436
9437 /* To catch errors in encoding functions, the codes are all offset by
9438    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
9439    as 16-bit instructions.  */
9440 #define X(a,b,c) T_MNEM##a
9441 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
9442 #undef X
9443
9444 #define X(a,b,c) 0x##b
9445 static const unsigned short thumb_op16[] = { T16_32_TAB };
9446 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
9447 #undef X
9448
9449 #define X(a,b,c) 0x##c
9450 static const unsigned int thumb_op32[] = { T16_32_TAB };
9451 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
9452 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
9453 #undef X
9454 #undef T16_32_TAB
9455
9456 /* Thumb instruction encoders, in alphabetical order.  */
9457
9458 /* ADDW or SUBW.  */
9459
9460 static void
9461 do_t_add_sub_w (void)
9462 {
9463   int Rd, Rn;
9464
9465   Rd = inst.operands[0].reg;
9466   Rn = inst.operands[1].reg;
9467
9468   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
9469      is the SP-{plus,minus}-immediate form of the instruction.  */
9470   if (Rn == REG_SP)
9471     constraint (Rd == REG_PC, BAD_PC);
9472   else
9473     reject_bad_reg (Rd);
9474
9475   inst.instruction |= (Rn << 16) | (Rd << 8);
9476   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
9477 }
9478
9479 /* Parse an add or subtract instruction.  We get here with inst.instruction
9480    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
9481
9482 static void
9483 do_t_add_sub (void)
9484 {
9485   int Rd, Rs, Rn;
9486
9487   Rd = inst.operands[0].reg;
9488   Rs = (inst.operands[1].present
9489         ? inst.operands[1].reg    /* Rd, Rs, foo */
9490         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9491
9492   if (Rd == REG_PC)
9493     set_it_insn_type_last ();
9494
9495   if (unified_syntax)
9496     {
9497       bfd_boolean flags;
9498       bfd_boolean narrow;
9499       int opcode;
9500
9501       flags = (inst.instruction == T_MNEM_adds
9502                || inst.instruction == T_MNEM_subs);
9503       if (flags)
9504         narrow = !in_it_block ();
9505       else
9506         narrow = in_it_block ();
9507       if (!inst.operands[2].isreg)
9508         {
9509           int add;
9510
9511           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
9512
9513           add = (inst.instruction == T_MNEM_add
9514                  || inst.instruction == T_MNEM_adds);
9515           opcode = 0;
9516           if (inst.size_req != 4)
9517             {
9518               /* Attempt to use a narrow opcode, with relaxation if
9519                  appropriate.  */
9520               if (Rd == REG_SP && Rs == REG_SP && !flags)
9521                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
9522               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
9523                 opcode = T_MNEM_add_sp;
9524               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
9525                 opcode = T_MNEM_add_pc;
9526               else if (Rd <= 7 && Rs <= 7 && narrow)
9527                 {
9528                   if (flags)
9529                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
9530                   else
9531                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
9532                 }
9533               if (opcode)
9534                 {
9535                   inst.instruction = THUMB_OP16(opcode);
9536                   inst.instruction |= (Rd << 4) | Rs;
9537                   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9538                   if (inst.size_req != 2)
9539                     inst.relax = opcode;
9540                 }
9541               else
9542                 constraint (inst.size_req == 2, BAD_HIREG);
9543             }
9544           if (inst.size_req == 4
9545               || (inst.size_req != 2 && !opcode))
9546             {
9547               if (Rd == REG_PC)
9548                 {
9549                   constraint (add, BAD_PC);
9550                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
9551                              _("only SUBS PC, LR, #const allowed"));
9552                   constraint (inst.reloc.exp.X_op != O_constant,
9553                               _("expression too complex"));
9554                   constraint (inst.reloc.exp.X_add_number < 0
9555                               || inst.reloc.exp.X_add_number > 0xff,
9556                              _("immediate value out of range"));
9557                   inst.instruction = T2_SUBS_PC_LR
9558                                      | inst.reloc.exp.X_add_number;
9559                   inst.reloc.type = BFD_RELOC_UNUSED;
9560                   return;
9561                 }
9562               else if (Rs == REG_PC)
9563                 {
9564                   /* Always use addw/subw.  */
9565                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
9566                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
9567                 }
9568               else
9569                 {
9570                   inst.instruction = THUMB_OP32 (inst.instruction);
9571                   inst.instruction = (inst.instruction & 0xe1ffffff)
9572                                      | 0x10000000;
9573                   if (flags)
9574                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9575                   else
9576                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
9577                 }
9578               inst.instruction |= Rd << 8;
9579               inst.instruction |= Rs << 16;
9580             }
9581         }
9582       else
9583         {
9584           unsigned int value = inst.reloc.exp.X_add_number;
9585           unsigned int shift = inst.operands[2].shift_kind;
9586
9587           Rn = inst.operands[2].reg;
9588           /* See if we can do this with a 16-bit instruction.  */
9589           if (!inst.operands[2].shifted && inst.size_req != 4)
9590             {
9591               if (Rd > 7 || Rs > 7 || Rn > 7)
9592                 narrow = FALSE;
9593
9594               if (narrow)
9595                 {
9596                   inst.instruction = ((inst.instruction == T_MNEM_adds
9597                                        || inst.instruction == T_MNEM_add)
9598                                       ? T_OPCODE_ADD_R3
9599                                       : T_OPCODE_SUB_R3);
9600                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
9601                   return;
9602                 }
9603
9604               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
9605                 {
9606                   /* Thumb-1 cores (except v6-M) require at least one high
9607                      register in a narrow non flag setting add.  */
9608                   if (Rd > 7 || Rn > 7
9609                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
9610                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
9611                     {
9612                       if (Rd == Rn)
9613                         {
9614                           Rn = Rs;
9615                           Rs = Rd;
9616                         }
9617                       inst.instruction = T_OPCODE_ADD_HI;
9618                       inst.instruction |= (Rd & 8) << 4;
9619                       inst.instruction |= (Rd & 7);
9620                       inst.instruction |= Rn << 3;
9621                       return;
9622                     }
9623                 }
9624             }
9625
9626           constraint (Rd == REG_PC, BAD_PC);
9627           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
9628           constraint (Rs == REG_PC, BAD_PC);
9629           reject_bad_reg (Rn);
9630
9631           /* If we get here, it can't be done in 16 bits.  */
9632           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
9633                       _("shift must be constant"));
9634           inst.instruction = THUMB_OP32 (inst.instruction);
9635           inst.instruction |= Rd << 8;
9636           inst.instruction |= Rs << 16;
9637           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
9638                       _("shift value over 3 not allowed in thumb mode"));
9639           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
9640                       _("only LSL shift allowed in thumb mode"));
9641           encode_thumb32_shifted_operand (2);
9642         }
9643     }
9644   else
9645     {
9646       constraint (inst.instruction == T_MNEM_adds
9647                   || inst.instruction == T_MNEM_subs,
9648                   BAD_THUMB32);
9649
9650       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
9651         {
9652           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
9653                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
9654                       BAD_HIREG);
9655
9656           inst.instruction = (inst.instruction == T_MNEM_add
9657                               ? 0x0000 : 0x8000);
9658           inst.instruction |= (Rd << 4) | Rs;
9659           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9660           return;
9661         }
9662
9663       Rn = inst.operands[2].reg;
9664       constraint (inst.operands[2].shifted, _("unshifted register required"));
9665
9666       /* We now have Rd, Rs, and Rn set to registers.  */
9667       if (Rd > 7 || Rs > 7 || Rn > 7)
9668         {
9669           /* Can't do this for SUB.      */
9670           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
9671           inst.instruction = T_OPCODE_ADD_HI;
9672           inst.instruction |= (Rd & 8) << 4;
9673           inst.instruction |= (Rd & 7);
9674           if (Rs == Rd)
9675             inst.instruction |= Rn << 3;
9676           else if (Rn == Rd)
9677             inst.instruction |= Rs << 3;
9678           else
9679             constraint (1, _("dest must overlap one source register"));
9680         }
9681       else
9682         {
9683           inst.instruction = (inst.instruction == T_MNEM_add
9684                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
9685           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
9686         }
9687     }
9688 }
9689
9690 static void
9691 do_t_adr (void)
9692 {
9693   unsigned Rd;
9694
9695   Rd = inst.operands[0].reg;
9696   reject_bad_reg (Rd);
9697
9698   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
9699     {
9700       /* Defer to section relaxation.  */
9701       inst.relax = inst.instruction;
9702       inst.instruction = THUMB_OP16 (inst.instruction);
9703       inst.instruction |= Rd << 4;
9704     }
9705   else if (unified_syntax && inst.size_req != 2)
9706     {
9707       /* Generate a 32-bit opcode.  */
9708       inst.instruction = THUMB_OP32 (inst.instruction);
9709       inst.instruction |= Rd << 8;
9710       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
9711       inst.reloc.pc_rel = 1;
9712     }
9713   else
9714     {
9715       /* Generate a 16-bit opcode.  */
9716       inst.instruction = THUMB_OP16 (inst.instruction);
9717       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9718       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
9719       inst.reloc.pc_rel = 1;
9720
9721       inst.instruction |= Rd << 4;
9722     }
9723 }
9724
9725 /* Arithmetic instructions for which there is just one 16-bit
9726    instruction encoding, and it allows only two low registers.
9727    For maximal compatibility with ARM syntax, we allow three register
9728    operands even when Thumb-32 instructions are not available, as long
9729    as the first two are identical.  For instance, both "sbc r0,r1" and
9730    "sbc r0,r0,r1" are allowed.  */
9731 static void
9732 do_t_arit3 (void)
9733 {
9734   int Rd, Rs, Rn;
9735
9736   Rd = inst.operands[0].reg;
9737   Rs = (inst.operands[1].present
9738         ? inst.operands[1].reg    /* Rd, Rs, foo */
9739         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9740   Rn = inst.operands[2].reg;
9741
9742   reject_bad_reg (Rd);
9743   reject_bad_reg (Rs);
9744   if (inst.operands[2].isreg)
9745     reject_bad_reg (Rn);
9746
9747   if (unified_syntax)
9748     {
9749       if (!inst.operands[2].isreg)
9750         {
9751           /* For an immediate, we always generate a 32-bit opcode;
9752              section relaxation will shrink it later if possible.  */
9753           inst.instruction = THUMB_OP32 (inst.instruction);
9754           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9755           inst.instruction |= Rd << 8;
9756           inst.instruction |= Rs << 16;
9757           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9758         }
9759       else
9760         {
9761           bfd_boolean narrow;
9762
9763           /* See if we can do this with a 16-bit instruction.  */
9764           if (THUMB_SETS_FLAGS (inst.instruction))
9765             narrow = !in_it_block ();
9766           else
9767             narrow = in_it_block ();
9768
9769           if (Rd > 7 || Rn > 7 || Rs > 7)
9770             narrow = FALSE;
9771           if (inst.operands[2].shifted)
9772             narrow = FALSE;
9773           if (inst.size_req == 4)
9774             narrow = FALSE;
9775
9776           if (narrow
9777               && Rd == Rs)
9778             {
9779               inst.instruction = THUMB_OP16 (inst.instruction);
9780               inst.instruction |= Rd;
9781               inst.instruction |= Rn << 3;
9782               return;
9783             }
9784
9785           /* If we get here, it can't be done in 16 bits.  */
9786           constraint (inst.operands[2].shifted
9787                       && inst.operands[2].immisreg,
9788                       _("shift must be constant"));
9789           inst.instruction = THUMB_OP32 (inst.instruction);
9790           inst.instruction |= Rd << 8;
9791           inst.instruction |= Rs << 16;
9792           encode_thumb32_shifted_operand (2);
9793         }
9794     }
9795   else
9796     {
9797       /* On its face this is a lie - the instruction does set the
9798          flags.  However, the only supported mnemonic in this mode
9799          says it doesn't.  */
9800       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
9801
9802       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
9803                   _("unshifted register required"));
9804       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
9805       constraint (Rd != Rs,
9806                   _("dest and source1 must be the same register"));
9807
9808       inst.instruction = THUMB_OP16 (inst.instruction);
9809       inst.instruction |= Rd;
9810       inst.instruction |= Rn << 3;
9811     }
9812 }
9813
9814 /* Similarly, but for instructions where the arithmetic operation is
9815    commutative, so we can allow either of them to be different from
9816    the destination operand in a 16-bit instruction.  For instance, all
9817    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
9818    accepted.  */
9819 static void
9820 do_t_arit3c (void)
9821 {
9822   int Rd, Rs, Rn;
9823
9824   Rd = inst.operands[0].reg;
9825   Rs = (inst.operands[1].present
9826         ? inst.operands[1].reg    /* Rd, Rs, foo */
9827         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9828   Rn = inst.operands[2].reg;
9829
9830   reject_bad_reg (Rd);
9831   reject_bad_reg (Rs);
9832   if (inst.operands[2].isreg)
9833     reject_bad_reg (Rn);
9834
9835   if (unified_syntax)
9836     {
9837       if (!inst.operands[2].isreg)
9838         {
9839           /* For an immediate, we always generate a 32-bit opcode;
9840              section relaxation will shrink it later if possible.  */
9841           inst.instruction = THUMB_OP32 (inst.instruction);
9842           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9843           inst.instruction |= Rd << 8;
9844           inst.instruction |= Rs << 16;
9845           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9846         }
9847       else
9848         {
9849           bfd_boolean narrow;
9850
9851           /* See if we can do this with a 16-bit instruction.  */
9852           if (THUMB_SETS_FLAGS (inst.instruction))
9853             narrow = !in_it_block ();
9854           else
9855             narrow = in_it_block ();
9856
9857           if (Rd > 7 || Rn > 7 || Rs > 7)
9858             narrow = FALSE;
9859           if (inst.operands[2].shifted)
9860             narrow = FALSE;
9861           if (inst.size_req == 4)
9862             narrow = FALSE;
9863
9864           if (narrow)
9865             {
9866               if (Rd == Rs)
9867                 {
9868                   inst.instruction = THUMB_OP16 (inst.instruction);
9869                   inst.instruction |= Rd;
9870                   inst.instruction |= Rn << 3;
9871                   return;
9872                 }
9873               if (Rd == Rn)
9874                 {
9875                   inst.instruction = THUMB_OP16 (inst.instruction);
9876                   inst.instruction |= Rd;
9877                   inst.instruction |= Rs << 3;
9878                   return;
9879                 }
9880             }
9881
9882           /* If we get here, it can't be done in 16 bits.  */
9883           constraint (inst.operands[2].shifted
9884                       && inst.operands[2].immisreg,
9885                       _("shift must be constant"));
9886           inst.instruction = THUMB_OP32 (inst.instruction);
9887           inst.instruction |= Rd << 8;
9888           inst.instruction |= Rs << 16;
9889           encode_thumb32_shifted_operand (2);
9890         }
9891     }
9892   else
9893     {
9894       /* On its face this is a lie - the instruction does set the
9895          flags.  However, the only supported mnemonic in this mode
9896          says it doesn't.  */
9897       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
9898
9899       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
9900                   _("unshifted register required"));
9901       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
9902
9903       inst.instruction = THUMB_OP16 (inst.instruction);
9904       inst.instruction |= Rd;
9905
9906       if (Rd == Rs)
9907         inst.instruction |= Rn << 3;
9908       else if (Rd == Rn)
9909         inst.instruction |= Rs << 3;
9910       else
9911         constraint (1, _("dest must overlap one source register"));
9912     }
9913 }
9914
9915 static void
9916 do_t_barrier (void)
9917 {
9918   if (inst.operands[0].present)
9919     {
9920       constraint ((inst.instruction & 0xf0) != 0x40
9921                   && inst.operands[0].imm > 0xf
9922                   && inst.operands[0].imm < 0x0,
9923                   _("bad barrier type"));
9924       inst.instruction |= inst.operands[0].imm;
9925     }
9926   else
9927     inst.instruction |= 0xf;
9928 }
9929
9930 static void
9931 do_t_bfc (void)
9932 {
9933   unsigned Rd;
9934   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
9935   constraint (msb > 32, _("bit-field extends past end of register"));
9936   /* The instruction encoding stores the LSB and MSB,
9937      not the LSB and width.  */
9938   Rd = inst.operands[0].reg;
9939   reject_bad_reg (Rd);
9940   inst.instruction |= Rd << 8;
9941   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
9942   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
9943   inst.instruction |= msb - 1;
9944 }
9945
9946 static void
9947 do_t_bfi (void)
9948 {
9949   int Rd, Rn;
9950   unsigned int msb;
9951
9952   Rd = inst.operands[0].reg;
9953   reject_bad_reg (Rd);
9954
9955   /* #0 in second position is alternative syntax for bfc, which is
9956      the same instruction but with REG_PC in the Rm field.  */
9957   if (!inst.operands[1].isreg)
9958     Rn = REG_PC;
9959   else
9960     {
9961       Rn = inst.operands[1].reg;
9962       reject_bad_reg (Rn);
9963     }
9964
9965   msb = inst.operands[2].imm + inst.operands[3].imm;
9966   constraint (msb > 32, _("bit-field extends past end of register"));
9967   /* The instruction encoding stores the LSB and MSB,
9968      not the LSB and width.  */
9969   inst.instruction |= Rd << 8;
9970   inst.instruction |= Rn << 16;
9971   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
9972   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
9973   inst.instruction |= msb - 1;
9974 }
9975
9976 static void
9977 do_t_bfx (void)
9978 {
9979   unsigned Rd, Rn;
9980
9981   Rd = inst.operands[0].reg;
9982   Rn = inst.operands[1].reg;
9983
9984   reject_bad_reg (Rd);
9985   reject_bad_reg (Rn);
9986
9987   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
9988               _("bit-field extends past end of register"));
9989   inst.instruction |= Rd << 8;
9990   inst.instruction |= Rn << 16;
9991   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
9992   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
9993   inst.instruction |= inst.operands[3].imm - 1;
9994 }
9995
9996 /* ARM V5 Thumb BLX (argument parse)
9997         BLX <target_addr>       which is BLX(1)
9998         BLX <Rm>                which is BLX(2)
9999    Unfortunately, there are two different opcodes for this mnemonic.
10000    So, the insns[].value is not used, and the code here zaps values
10001         into inst.instruction.
10002
10003    ??? How to take advantage of the additional two bits of displacement
10004    available in Thumb32 mode?  Need new relocation?  */
10005
10006 static void
10007 do_t_blx (void)
10008 {
10009   set_it_insn_type_last ();
10010
10011   if (inst.operands[0].isreg)
10012     {
10013       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
10014       /* We have a register, so this is BLX(2).  */
10015       inst.instruction |= inst.operands[0].reg << 3;
10016     }
10017   else
10018     {
10019       /* No register.  This must be BLX(1).  */
10020       inst.instruction = 0xf000e800;
10021       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
10022     }
10023 }
10024
10025 static void
10026 do_t_branch (void)
10027 {
10028   int opcode;
10029   int cond;
10030   int reloc;
10031
10032   cond = inst.cond;
10033   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
10034
10035   if (in_it_block ())
10036     {
10037       /* Conditional branches inside IT blocks are encoded as unconditional
10038          branches.  */
10039       cond = COND_ALWAYS;
10040     }
10041   else
10042     cond = inst.cond;
10043
10044   if (cond != COND_ALWAYS)
10045     opcode = T_MNEM_bcond;
10046   else
10047     opcode = inst.instruction;
10048
10049   if (unified_syntax
10050       && (inst.size_req == 4
10051           || (inst.size_req != 2
10052               && (inst.operands[0].hasreloc
10053                   || inst.reloc.exp.X_op == O_constant))))
10054     {
10055       inst.instruction = THUMB_OP32(opcode);
10056       if (cond == COND_ALWAYS)
10057         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
10058       else
10059         {
10060           gas_assert (cond != 0xF);
10061           inst.instruction |= cond << 22;
10062           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
10063         }
10064     }
10065   else
10066     {
10067       inst.instruction = THUMB_OP16(opcode);
10068       if (cond == COND_ALWAYS)
10069         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
10070       else
10071         {
10072           inst.instruction |= cond << 8;
10073           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
10074         }
10075       /* Allow section relaxation.  */
10076       if (unified_syntax && inst.size_req != 2)
10077         inst.relax = opcode;
10078     }
10079   inst.reloc.type = reloc;
10080   inst.reloc.pc_rel = 1;
10081 }
10082
10083 static void
10084 do_t_bkpt (void)
10085 {
10086   constraint (inst.cond != COND_ALWAYS,
10087               _("instruction is always unconditional"));
10088   if (inst.operands[0].present)
10089     {
10090       constraint (inst.operands[0].imm > 255,
10091                   _("immediate value out of range"));
10092       inst.instruction |= inst.operands[0].imm;
10093       set_it_insn_type (NEUTRAL_IT_INSN);
10094     }
10095 }
10096
10097 static void
10098 do_t_branch23 (void)
10099 {
10100   set_it_insn_type_last ();
10101   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
10102
10103   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
10104      this file.  We used to simply ignore the PLT reloc type here --
10105      the branch encoding is now needed to deal with TLSCALL relocs.
10106      So if we see a PLT reloc now, put it back to how it used to be to
10107      keep the preexisting behaviour.  */
10108   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
10109     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
10110
10111 #if defined(OBJ_COFF)
10112   /* If the destination of the branch is a defined symbol which does not have
10113      the THUMB_FUNC attribute, then we must be calling a function which has
10114      the (interfacearm) attribute.  We look for the Thumb entry point to that
10115      function and change the branch to refer to that function instead.  */
10116   if (   inst.reloc.exp.X_op == O_symbol
10117       && inst.reloc.exp.X_add_symbol != NULL
10118       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
10119       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
10120     inst.reloc.exp.X_add_symbol =
10121       find_real_start (inst.reloc.exp.X_add_symbol);
10122 #endif
10123 }
10124
10125 static void
10126 do_t_bx (void)
10127 {
10128   set_it_insn_type_last ();
10129   inst.instruction |= inst.operands[0].reg << 3;
10130   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
10131      should cause the alignment to be checked once it is known.  This is
10132      because BX PC only works if the instruction is word aligned.  */
10133 }
10134
10135 static void
10136 do_t_bxj (void)
10137 {
10138   int Rm;
10139
10140   set_it_insn_type_last ();
10141   Rm = inst.operands[0].reg;
10142   reject_bad_reg (Rm);
10143   inst.instruction |= Rm << 16;
10144 }
10145
10146 static void
10147 do_t_clz (void)
10148 {
10149   unsigned Rd;
10150   unsigned Rm;
10151
10152   Rd = inst.operands[0].reg;
10153   Rm = inst.operands[1].reg;
10154
10155   reject_bad_reg (Rd);
10156   reject_bad_reg (Rm);
10157
10158   inst.instruction |= Rd << 8;
10159   inst.instruction |= Rm << 16;
10160   inst.instruction |= Rm;
10161 }
10162
10163 static void
10164 do_t_cps (void)
10165 {
10166   set_it_insn_type (OUTSIDE_IT_INSN);
10167   inst.instruction |= inst.operands[0].imm;
10168 }
10169
10170 static void
10171 do_t_cpsi (void)
10172 {
10173   set_it_insn_type (OUTSIDE_IT_INSN);
10174   if (unified_syntax
10175       && (inst.operands[1].present || inst.size_req == 4)
10176       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
10177     {
10178       unsigned int imod = (inst.instruction & 0x0030) >> 4;
10179       inst.instruction = 0xf3af8000;
10180       inst.instruction |= imod << 9;
10181       inst.instruction |= inst.operands[0].imm << 5;
10182       if (inst.operands[1].present)
10183         inst.instruction |= 0x100 | inst.operands[1].imm;
10184     }
10185   else
10186     {
10187       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
10188                   && (inst.operands[0].imm & 4),
10189                   _("selected processor does not support 'A' form "
10190                     "of this instruction"));
10191       constraint (inst.operands[1].present || inst.size_req == 4,
10192                   _("Thumb does not support the 2-argument "
10193                     "form of this instruction"));
10194       inst.instruction |= inst.operands[0].imm;
10195     }
10196 }
10197
10198 /* THUMB CPY instruction (argument parse).  */
10199
10200 static void
10201 do_t_cpy (void)
10202 {
10203   if (inst.size_req == 4)
10204     {
10205       inst.instruction = THUMB_OP32 (T_MNEM_mov);
10206       inst.instruction |= inst.operands[0].reg << 8;
10207       inst.instruction |= inst.operands[1].reg;
10208     }
10209   else
10210     {
10211       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
10212       inst.instruction |= (inst.operands[0].reg & 0x7);
10213       inst.instruction |= inst.operands[1].reg << 3;
10214     }
10215 }
10216
10217 static void
10218 do_t_cbz (void)
10219 {
10220   set_it_insn_type (OUTSIDE_IT_INSN);
10221   constraint (inst.operands[0].reg > 7, BAD_HIREG);
10222   inst.instruction |= inst.operands[0].reg;
10223   inst.reloc.pc_rel = 1;
10224   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
10225 }
10226
10227 static void
10228 do_t_dbg (void)
10229 {
10230   inst.instruction |= inst.operands[0].imm;
10231 }
10232
10233 static void
10234 do_t_div (void)
10235 {
10236   unsigned Rd, Rn, Rm;
10237
10238   Rd = inst.operands[0].reg;
10239   Rn = (inst.operands[1].present
10240         ? inst.operands[1].reg : Rd);
10241   Rm = inst.operands[2].reg;
10242
10243   reject_bad_reg (Rd);
10244   reject_bad_reg (Rn);
10245   reject_bad_reg (Rm);
10246
10247   inst.instruction |= Rd << 8;
10248   inst.instruction |= Rn << 16;
10249   inst.instruction |= Rm;
10250 }
10251
10252 static void
10253 do_t_hint (void)
10254 {
10255   if (unified_syntax && inst.size_req == 4)
10256     inst.instruction = THUMB_OP32 (inst.instruction);
10257   else
10258     inst.instruction = THUMB_OP16 (inst.instruction);
10259 }
10260
10261 static void
10262 do_t_it (void)
10263 {
10264   unsigned int cond = inst.operands[0].imm;
10265
10266   set_it_insn_type (IT_INSN);
10267   now_it.mask = (inst.instruction & 0xf) | 0x10;
10268   now_it.cc = cond;
10269
10270   /* If the condition is a negative condition, invert the mask.  */
10271   if ((cond & 0x1) == 0x0)
10272     {
10273       unsigned int mask = inst.instruction & 0x000f;
10274
10275       if ((mask & 0x7) == 0)
10276         /* no conversion needed */;
10277       else if ((mask & 0x3) == 0)
10278         mask ^= 0x8;
10279       else if ((mask & 0x1) == 0)
10280         mask ^= 0xC;
10281       else
10282         mask ^= 0xE;
10283
10284       inst.instruction &= 0xfff0;
10285       inst.instruction |= mask;
10286     }
10287
10288   inst.instruction |= cond << 4;
10289 }
10290
10291 /* Helper function used for both push/pop and ldm/stm.  */
10292 static void
10293 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
10294 {
10295   bfd_boolean load;
10296
10297   load = (inst.instruction & (1 << 20)) != 0;
10298
10299   if (mask & (1 << 13))
10300     inst.error =  _("SP not allowed in register list");
10301
10302   if ((mask & (1 << base)) != 0
10303       && writeback)
10304     inst.error = _("having the base register in the register list when "
10305                    "using write back is UNPREDICTABLE");
10306
10307   if (load)
10308     {
10309       if (mask & (1 << 15))
10310         {
10311           if (mask & (1 << 14))
10312             inst.error = _("LR and PC should not both be in register list");
10313           else
10314             set_it_insn_type_last ();
10315         }
10316     }
10317   else
10318     {
10319       if (mask & (1 << 15))
10320         inst.error = _("PC not allowed in register list");
10321     }
10322
10323   if ((mask & (mask - 1)) == 0)
10324     {
10325       /* Single register transfers implemented as str/ldr.  */
10326       if (writeback)
10327         {
10328           if (inst.instruction & (1 << 23))
10329             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
10330           else
10331             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
10332         }
10333       else
10334         {
10335           if (inst.instruction & (1 << 23))
10336             inst.instruction = 0x00800000; /* ia -> [base] */
10337           else
10338             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
10339         }
10340
10341       inst.instruction |= 0xf8400000;
10342       if (load)
10343         inst.instruction |= 0x00100000;
10344
10345       mask = ffs (mask) - 1;
10346       mask <<= 12;
10347     }
10348   else if (writeback)
10349     inst.instruction |= WRITE_BACK;
10350
10351   inst.instruction |= mask;
10352   inst.instruction |= base << 16;
10353 }
10354
10355 static void
10356 do_t_ldmstm (void)
10357 {
10358   /* This really doesn't seem worth it.  */
10359   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
10360               _("expression too complex"));
10361   constraint (inst.operands[1].writeback,
10362               _("Thumb load/store multiple does not support {reglist}^"));
10363
10364   if (unified_syntax)
10365     {
10366       bfd_boolean narrow;
10367       unsigned mask;
10368
10369       narrow = FALSE;
10370       /* See if we can use a 16-bit instruction.  */
10371       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
10372           && inst.size_req != 4
10373           && !(inst.operands[1].imm & ~0xff))
10374         {
10375           mask = 1 << inst.operands[0].reg;
10376
10377           if (inst.operands[0].reg <= 7)
10378             {
10379               if (inst.instruction == T_MNEM_stmia
10380                   ? inst.operands[0].writeback
10381                   : (inst.operands[0].writeback
10382                      == !(inst.operands[1].imm & mask)))
10383                 {
10384                   if (inst.instruction == T_MNEM_stmia
10385                       && (inst.operands[1].imm & mask)
10386                       && (inst.operands[1].imm & (mask - 1)))
10387                     as_warn (_("value stored for r%d is UNKNOWN"),
10388                              inst.operands[0].reg);
10389
10390                   inst.instruction = THUMB_OP16 (inst.instruction);
10391                   inst.instruction |= inst.operands[0].reg << 8;
10392                   inst.instruction |= inst.operands[1].imm;
10393                   narrow = TRUE;
10394                 }
10395               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
10396                 {
10397                   /* This means 1 register in reg list one of 3 situations:
10398                      1. Instruction is stmia, but without writeback.
10399                      2. lmdia without writeback, but with Rn not in
10400                         reglist.
10401                      3. ldmia with writeback, but with Rn in reglist.
10402                      Case 3 is UNPREDICTABLE behaviour, so we handle
10403                      case 1 and 2 which can be converted into a 16-bit
10404                      str or ldr. The SP cases are handled below.  */
10405                   unsigned long opcode;
10406                   /* First, record an error for Case 3.  */
10407                   if (inst.operands[1].imm & mask
10408                       && inst.operands[0].writeback)
10409                     inst.error =
10410                         _("having the base register in the register list when "
10411                           "using write back is UNPREDICTABLE");
10412
10413                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
10414                                                              : T_MNEM_ldr);
10415                   inst.instruction = THUMB_OP16 (opcode);
10416                   inst.instruction |= inst.operands[0].reg << 3;
10417                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
10418                   narrow = TRUE;
10419                 }
10420             }
10421           else if (inst.operands[0] .reg == REG_SP)
10422             {
10423               if (inst.operands[0].writeback)
10424                 {
10425                   inst.instruction =
10426                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
10427                                     ? T_MNEM_push : T_MNEM_pop);
10428                   inst.instruction |= inst.operands[1].imm;
10429                   narrow = TRUE;
10430                 }
10431               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
10432                 {
10433                   inst.instruction =
10434                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
10435                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
10436                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
10437                   narrow = TRUE;
10438                 }
10439             }
10440         }
10441
10442       if (!narrow)
10443         {
10444           if (inst.instruction < 0xffff)
10445             inst.instruction = THUMB_OP32 (inst.instruction);
10446
10447           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
10448                                 inst.operands[0].writeback);
10449         }
10450     }
10451   else
10452     {
10453       constraint (inst.operands[0].reg > 7
10454                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
10455       constraint (inst.instruction != T_MNEM_ldmia
10456                   && inst.instruction != T_MNEM_stmia,
10457                   _("Thumb-2 instruction only valid in unified syntax"));
10458       if (inst.instruction == T_MNEM_stmia)
10459         {
10460           if (!inst.operands[0].writeback)
10461             as_warn (_("this instruction will write back the base register"));
10462           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
10463               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
10464             as_warn (_("value stored for r%d is UNKNOWN"),
10465                      inst.operands[0].reg);
10466         }
10467       else
10468         {
10469           if (!inst.operands[0].writeback
10470               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
10471             as_warn (_("this instruction will write back the base register"));
10472           else if (inst.operands[0].writeback
10473                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
10474             as_warn (_("this instruction will not write back the base register"));
10475         }
10476
10477       inst.instruction = THUMB_OP16 (inst.instruction);
10478       inst.instruction |= inst.operands[0].reg << 8;
10479       inst.instruction |= inst.operands[1].imm;
10480     }
10481 }
10482
10483 static void
10484 do_t_ldrex (void)
10485 {
10486   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
10487               || inst.operands[1].postind || inst.operands[1].writeback
10488               || inst.operands[1].immisreg || inst.operands[1].shifted
10489               || inst.operands[1].negative,
10490               BAD_ADDR_MODE);
10491
10492   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
10493
10494   inst.instruction |= inst.operands[0].reg << 12;
10495   inst.instruction |= inst.operands[1].reg << 16;
10496   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
10497 }
10498
10499 static void
10500 do_t_ldrexd (void)
10501 {
10502   if (!inst.operands[1].present)
10503     {
10504       constraint (inst.operands[0].reg == REG_LR,
10505                   _("r14 not allowed as first register "
10506                     "when second register is omitted"));
10507       inst.operands[1].reg = inst.operands[0].reg + 1;
10508     }
10509   constraint (inst.operands[0].reg == inst.operands[1].reg,
10510               BAD_OVERLAP);
10511
10512   inst.instruction |= inst.operands[0].reg << 12;
10513   inst.instruction |= inst.operands[1].reg << 8;
10514   inst.instruction |= inst.operands[2].reg << 16;
10515 }
10516
10517 static void
10518 do_t_ldst (void)
10519 {
10520   unsigned long opcode;
10521   int Rn;
10522
10523   if (inst.operands[0].isreg
10524       && !inst.operands[0].preind
10525       && inst.operands[0].reg == REG_PC)
10526     set_it_insn_type_last ();
10527
10528   opcode = inst.instruction;
10529   if (unified_syntax)
10530     {
10531       if (!inst.operands[1].isreg)
10532         {
10533           if (opcode <= 0xffff)
10534             inst.instruction = THUMB_OP32 (opcode);
10535           if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
10536             return;
10537         }
10538       if (inst.operands[1].isreg
10539           && !inst.operands[1].writeback
10540           && !inst.operands[1].shifted && !inst.operands[1].postind
10541           && !inst.operands[1].negative && inst.operands[0].reg <= 7
10542           && opcode <= 0xffff
10543           && inst.size_req != 4)
10544         {
10545           /* Insn may have a 16-bit form.  */
10546           Rn = inst.operands[1].reg;
10547           if (inst.operands[1].immisreg)
10548             {
10549               inst.instruction = THUMB_OP16 (opcode);
10550               /* [Rn, Rik] */
10551               if (Rn <= 7 && inst.operands[1].imm <= 7)
10552                 goto op16;
10553               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
10554                 reject_bad_reg (inst.operands[1].imm);
10555             }
10556           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
10557                     && opcode != T_MNEM_ldrsb)
10558                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
10559                    || (Rn == REG_SP && opcode == T_MNEM_str))
10560             {
10561               /* [Rn, #const] */
10562               if (Rn > 7)
10563                 {
10564                   if (Rn == REG_PC)
10565                     {
10566                       if (inst.reloc.pc_rel)
10567                         opcode = T_MNEM_ldr_pc2;
10568                       else
10569                         opcode = T_MNEM_ldr_pc;
10570                     }
10571                   else
10572                     {
10573                       if (opcode == T_MNEM_ldr)
10574                         opcode = T_MNEM_ldr_sp;
10575                       else
10576                         opcode = T_MNEM_str_sp;
10577                     }
10578                   inst.instruction = inst.operands[0].reg << 8;
10579                 }
10580               else
10581                 {
10582                   inst.instruction = inst.operands[0].reg;
10583                   inst.instruction |= inst.operands[1].reg << 3;
10584                 }
10585               inst.instruction |= THUMB_OP16 (opcode);
10586               if (inst.size_req == 2)
10587                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10588               else
10589                 inst.relax = opcode;
10590               return;
10591             }
10592         }
10593       /* Definitely a 32-bit variant.  */
10594
10595       /* Warning for Erratum 752419.  */
10596       if (opcode == T_MNEM_ldr
10597           && inst.operands[0].reg == REG_SP
10598           && inst.operands[1].writeback == 1
10599           && !inst.operands[1].immisreg)
10600         {
10601           if (no_cpu_selected ()
10602               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
10603                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
10604                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
10605             as_warn (_("This instruction may be unpredictable "
10606                        "if executed on M-profile cores "
10607                        "with interrupts enabled."));
10608         }
10609
10610       /* Do some validations regarding addressing modes.  */
10611       if (inst.operands[1].immisreg)
10612         reject_bad_reg (inst.operands[1].imm);
10613
10614       constraint (inst.operands[1].writeback == 1
10615                   && inst.operands[0].reg == inst.operands[1].reg,
10616                   BAD_OVERLAP);
10617
10618       inst.instruction = THUMB_OP32 (opcode);
10619       inst.instruction |= inst.operands[0].reg << 12;
10620       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
10621       check_ldr_r15_aligned ();
10622       return;
10623     }
10624
10625   constraint (inst.operands[0].reg > 7, BAD_HIREG);
10626
10627   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
10628     {
10629       /* Only [Rn,Rm] is acceptable.  */
10630       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
10631       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
10632                   || inst.operands[1].postind || inst.operands[1].shifted
10633                   || inst.operands[1].negative,
10634                   _("Thumb does not support this addressing mode"));
10635       inst.instruction = THUMB_OP16 (inst.instruction);
10636       goto op16;
10637     }
10638
10639   inst.instruction = THUMB_OP16 (inst.instruction);
10640   if (!inst.operands[1].isreg)
10641     if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
10642       return;
10643
10644   constraint (!inst.operands[1].preind
10645               || inst.operands[1].shifted
10646               || inst.operands[1].writeback,
10647               _("Thumb does not support this addressing mode"));
10648   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
10649     {
10650       constraint (inst.instruction & 0x0600,
10651                   _("byte or halfword not valid for base register"));
10652       constraint (inst.operands[1].reg == REG_PC
10653                   && !(inst.instruction & THUMB_LOAD_BIT),
10654                   _("r15 based store not allowed"));
10655       constraint (inst.operands[1].immisreg,
10656                   _("invalid base register for register offset"));
10657
10658       if (inst.operands[1].reg == REG_PC)
10659         inst.instruction = T_OPCODE_LDR_PC;
10660       else if (inst.instruction & THUMB_LOAD_BIT)
10661         inst.instruction = T_OPCODE_LDR_SP;
10662       else
10663         inst.instruction = T_OPCODE_STR_SP;
10664
10665       inst.instruction |= inst.operands[0].reg << 8;
10666       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10667       return;
10668     }
10669
10670   constraint (inst.operands[1].reg > 7, BAD_HIREG);
10671   if (!inst.operands[1].immisreg)
10672     {
10673       /* Immediate offset.  */
10674       inst.instruction |= inst.operands[0].reg;
10675       inst.instruction |= inst.operands[1].reg << 3;
10676       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10677       return;
10678     }
10679
10680   /* Register offset.  */
10681   constraint (inst.operands[1].imm > 7, BAD_HIREG);
10682   constraint (inst.operands[1].negative,
10683               _("Thumb does not support this addressing mode"));
10684
10685  op16:
10686   switch (inst.instruction)
10687     {
10688     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
10689     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
10690     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
10691     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
10692     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
10693     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
10694     case 0x5600 /* ldrsb */:
10695     case 0x5e00 /* ldrsh */: break;
10696     default: abort ();
10697     }
10698
10699   inst.instruction |= inst.operands[0].reg;
10700   inst.instruction |= inst.operands[1].reg << 3;
10701   inst.instruction |= inst.operands[1].imm << 6;
10702 }
10703
10704 static void
10705 do_t_ldstd (void)
10706 {
10707   if (!inst.operands[1].present)
10708     {
10709       inst.operands[1].reg = inst.operands[0].reg + 1;
10710       constraint (inst.operands[0].reg == REG_LR,
10711                   _("r14 not allowed here"));
10712       constraint (inst.operands[0].reg == REG_R12,
10713                   _("r12 not allowed here"));
10714     }
10715
10716   if (inst.operands[2].writeback
10717       && (inst.operands[0].reg == inst.operands[2].reg
10718       || inst.operands[1].reg == inst.operands[2].reg))
10719     as_warn (_("base register written back, and overlaps "
10720                "one of transfer registers"));
10721
10722   inst.instruction |= inst.operands[0].reg << 12;
10723   inst.instruction |= inst.operands[1].reg << 8;
10724   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
10725 }
10726
10727 static void
10728 do_t_ldstt (void)
10729 {
10730   inst.instruction |= inst.operands[0].reg << 12;
10731   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
10732 }
10733
10734 static void
10735 do_t_mla (void)
10736 {
10737   unsigned Rd, Rn, Rm, Ra;
10738
10739   Rd = inst.operands[0].reg;
10740   Rn = inst.operands[1].reg;
10741   Rm = inst.operands[2].reg;
10742   Ra = inst.operands[3].reg;
10743
10744   reject_bad_reg (Rd);
10745   reject_bad_reg (Rn);
10746   reject_bad_reg (Rm);
10747   reject_bad_reg (Ra);
10748
10749   inst.instruction |= Rd << 8;
10750   inst.instruction |= Rn << 16;
10751   inst.instruction |= Rm;
10752   inst.instruction |= Ra << 12;
10753 }
10754
10755 static void
10756 do_t_mlal (void)
10757 {
10758   unsigned RdLo, RdHi, Rn, Rm;
10759
10760   RdLo = inst.operands[0].reg;
10761   RdHi = inst.operands[1].reg;
10762   Rn = inst.operands[2].reg;
10763   Rm = inst.operands[3].reg;
10764
10765   reject_bad_reg (RdLo);
10766   reject_bad_reg (RdHi);
10767   reject_bad_reg (Rn);
10768   reject_bad_reg (Rm);
10769
10770   inst.instruction |= RdLo << 12;
10771   inst.instruction |= RdHi << 8;
10772   inst.instruction |= Rn << 16;
10773   inst.instruction |= Rm;
10774 }
10775
10776 static void
10777 do_t_mov_cmp (void)
10778 {
10779   unsigned Rn, Rm;
10780
10781   Rn = inst.operands[0].reg;
10782   Rm = inst.operands[1].reg;
10783
10784   if (Rn == REG_PC)
10785     set_it_insn_type_last ();
10786
10787   if (unified_syntax)
10788     {
10789       int r0off = (inst.instruction == T_MNEM_mov
10790                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
10791       unsigned long opcode;
10792       bfd_boolean narrow;
10793       bfd_boolean low_regs;
10794
10795       low_regs = (Rn <= 7 && Rm <= 7);
10796       opcode = inst.instruction;
10797       if (in_it_block ())
10798         narrow = opcode != T_MNEM_movs;
10799       else
10800         narrow = opcode != T_MNEM_movs || low_regs;
10801       if (inst.size_req == 4
10802           || inst.operands[1].shifted)
10803         narrow = FALSE;
10804
10805       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
10806       if (opcode == T_MNEM_movs && inst.operands[1].isreg
10807           && !inst.operands[1].shifted
10808           && Rn == REG_PC
10809           && Rm == REG_LR)
10810         {
10811           inst.instruction = T2_SUBS_PC_LR;
10812           return;
10813         }
10814
10815       if (opcode == T_MNEM_cmp)
10816         {
10817           constraint (Rn == REG_PC, BAD_PC);
10818           if (narrow)
10819             {
10820               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
10821                  but valid.  */
10822               warn_deprecated_sp (Rm);
10823               /* R15 was documented as a valid choice for Rm in ARMv6,
10824                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
10825                  tools reject R15, so we do too.  */
10826               constraint (Rm == REG_PC, BAD_PC);
10827             }
10828           else
10829             reject_bad_reg (Rm);
10830         }
10831       else if (opcode == T_MNEM_mov
10832                || opcode == T_MNEM_movs)
10833         {
10834           if (inst.operands[1].isreg)
10835             {
10836               if (opcode == T_MNEM_movs)
10837                 {
10838                   reject_bad_reg (Rn);
10839                   reject_bad_reg (Rm);
10840                 }
10841               else if (narrow)
10842                 {
10843                   /* This is mov.n.  */
10844                   if ((Rn == REG_SP || Rn == REG_PC)
10845                       && (Rm == REG_SP || Rm == REG_PC))
10846                     {
10847                       as_warn (_("Use of r%u as a source register is "
10848                                  "deprecated when r%u is the destination "
10849                                  "register."), Rm, Rn);
10850                     }
10851                 }
10852               else
10853                 {
10854                   /* This is mov.w.  */
10855                   constraint (Rn == REG_PC, BAD_PC);
10856                   constraint (Rm == REG_PC, BAD_PC);
10857                   constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
10858                 }
10859             }
10860           else
10861             reject_bad_reg (Rn);
10862         }
10863
10864       if (!inst.operands[1].isreg)
10865         {
10866           /* Immediate operand.  */
10867           if (!in_it_block () && opcode == T_MNEM_mov)
10868             narrow = 0;
10869           if (low_regs && narrow)
10870             {
10871               inst.instruction = THUMB_OP16 (opcode);
10872               inst.instruction |= Rn << 8;
10873               if (inst.size_req == 2)
10874                 inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
10875               else
10876                 inst.relax = opcode;
10877             }
10878           else
10879             {
10880               inst.instruction = THUMB_OP32 (inst.instruction);
10881               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10882               inst.instruction |= Rn << r0off;
10883               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10884             }
10885         }
10886       else if (inst.operands[1].shifted && inst.operands[1].immisreg
10887                && (inst.instruction == T_MNEM_mov
10888                    || inst.instruction == T_MNEM_movs))
10889         {
10890           /* Register shifts are encoded as separate shift instructions.  */
10891           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
10892
10893           if (in_it_block ())
10894             narrow = !flags;
10895           else
10896             narrow = flags;
10897
10898           if (inst.size_req == 4)
10899             narrow = FALSE;
10900
10901           if (!low_regs || inst.operands[1].imm > 7)
10902             narrow = FALSE;
10903
10904           if (Rn != Rm)
10905             narrow = FALSE;
10906
10907           switch (inst.operands[1].shift_kind)
10908             {
10909             case SHIFT_LSL:
10910               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
10911               break;
10912             case SHIFT_ASR:
10913               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
10914               break;
10915             case SHIFT_LSR:
10916               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
10917               break;
10918             case SHIFT_ROR:
10919               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
10920               break;
10921             default:
10922               abort ();
10923             }
10924
10925           inst.instruction = opcode;
10926           if (narrow)
10927             {
10928               inst.instruction |= Rn;
10929               inst.instruction |= inst.operands[1].imm << 3;
10930             }
10931           else
10932             {
10933               if (flags)
10934                 inst.instruction |= CONDS_BIT;
10935
10936               inst.instruction |= Rn << 8;
10937               inst.instruction |= Rm << 16;
10938               inst.instruction |= inst.operands[1].imm;
10939             }
10940         }
10941       else if (!narrow)
10942         {
10943           /* Some mov with immediate shift have narrow variants.
10944              Register shifts are handled above.  */
10945           if (low_regs && inst.operands[1].shifted
10946               && (inst.instruction == T_MNEM_mov
10947                   || inst.instruction == T_MNEM_movs))
10948             {
10949               if (in_it_block ())
10950                 narrow = (inst.instruction == T_MNEM_mov);
10951               else
10952                 narrow = (inst.instruction == T_MNEM_movs);
10953             }
10954
10955           if (narrow)
10956             {
10957               switch (inst.operands[1].shift_kind)
10958                 {
10959                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
10960                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
10961                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
10962                 default: narrow = FALSE; break;
10963                 }
10964             }
10965
10966           if (narrow)
10967             {
10968               inst.instruction |= Rn;
10969               inst.instruction |= Rm << 3;
10970               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
10971             }
10972           else
10973             {
10974               inst.instruction = THUMB_OP32 (inst.instruction);
10975               inst.instruction |= Rn << r0off;
10976               encode_thumb32_shifted_operand (1);
10977             }
10978         }
10979       else
10980         switch (inst.instruction)
10981           {
10982           case T_MNEM_mov:
10983             /* In v4t or v5t a move of two lowregs produces unpredictable
10984                results. Don't allow this.  */
10985             if (low_regs)
10986               {
10987                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
10988                             "MOV Rd, Rs with two low registers is not "
10989                             "permitted on this architecture");
10990                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
10991                                         arm_ext_v6);
10992               }
10993
10994             inst.instruction = T_OPCODE_MOV_HR;
10995             inst.instruction |= (Rn & 0x8) << 4;
10996             inst.instruction |= (Rn & 0x7);
10997             inst.instruction |= Rm << 3;
10998             break;
10999
11000           case T_MNEM_movs:
11001             /* We know we have low registers at this point.
11002                Generate LSLS Rd, Rs, #0.  */
11003             inst.instruction = T_OPCODE_LSL_I;
11004             inst.instruction |= Rn;
11005             inst.instruction |= Rm << 3;
11006             break;
11007
11008           case T_MNEM_cmp:
11009             if (low_regs)
11010               {
11011                 inst.instruction = T_OPCODE_CMP_LR;
11012                 inst.instruction |= Rn;
11013                 inst.instruction |= Rm << 3;
11014               }
11015             else
11016               {
11017                 inst.instruction = T_OPCODE_CMP_HR;
11018                 inst.instruction |= (Rn & 0x8) << 4;
11019                 inst.instruction |= (Rn & 0x7);
11020                 inst.instruction |= Rm << 3;
11021               }
11022             break;
11023           }
11024       return;
11025     }
11026
11027   inst.instruction = THUMB_OP16 (inst.instruction);
11028
11029   /* PR 10443: Do not silently ignore shifted operands.  */
11030   constraint (inst.operands[1].shifted,
11031               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
11032
11033   if (inst.operands[1].isreg)
11034     {
11035       if (Rn < 8 && Rm < 8)
11036         {
11037           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
11038              since a MOV instruction produces unpredictable results.  */
11039           if (inst.instruction == T_OPCODE_MOV_I8)
11040             inst.instruction = T_OPCODE_ADD_I3;
11041           else
11042             inst.instruction = T_OPCODE_CMP_LR;
11043
11044           inst.instruction |= Rn;
11045           inst.instruction |= Rm << 3;
11046         }
11047       else
11048         {
11049           if (inst.instruction == T_OPCODE_MOV_I8)
11050             inst.instruction = T_OPCODE_MOV_HR;
11051           else
11052             inst.instruction = T_OPCODE_CMP_HR;
11053           do_t_cpy ();
11054         }
11055     }
11056   else
11057     {
11058       constraint (Rn > 7,
11059                   _("only lo regs allowed with immediate"));
11060       inst.instruction |= Rn << 8;
11061       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
11062     }
11063 }
11064
11065 static void
11066 do_t_mov16 (void)
11067 {
11068   unsigned Rd;
11069   bfd_vma imm;
11070   bfd_boolean top;
11071
11072   top = (inst.instruction & 0x00800000) != 0;
11073   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
11074     {
11075       constraint (top, _(":lower16: not allowed this instruction"));
11076       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
11077     }
11078   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
11079     {
11080       constraint (!top, _(":upper16: not allowed this instruction"));
11081       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
11082     }
11083
11084   Rd = inst.operands[0].reg;
11085   reject_bad_reg (Rd);
11086
11087   inst.instruction |= Rd << 8;
11088   if (inst.reloc.type == BFD_RELOC_UNUSED)
11089     {
11090       imm = inst.reloc.exp.X_add_number;
11091       inst.instruction |= (imm & 0xf000) << 4;
11092       inst.instruction |= (imm & 0x0800) << 15;
11093       inst.instruction |= (imm & 0x0700) << 4;
11094       inst.instruction |= (imm & 0x00ff);
11095     }
11096 }
11097
11098 static void
11099 do_t_mvn_tst (void)
11100 {
11101   unsigned Rn, Rm;
11102
11103   Rn = inst.operands[0].reg;
11104   Rm = inst.operands[1].reg;
11105
11106   if (inst.instruction == T_MNEM_cmp
11107       || inst.instruction == T_MNEM_cmn)
11108     constraint (Rn == REG_PC, BAD_PC);
11109   else
11110     reject_bad_reg (Rn);
11111   reject_bad_reg (Rm);
11112
11113   if (unified_syntax)
11114     {
11115       int r0off = (inst.instruction == T_MNEM_mvn
11116                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
11117       bfd_boolean narrow;
11118
11119       if (inst.size_req == 4
11120           || inst.instruction > 0xffff
11121           || inst.operands[1].shifted
11122           || Rn > 7 || Rm > 7)
11123         narrow = FALSE;
11124       else if (inst.instruction == T_MNEM_cmn)
11125         narrow = TRUE;
11126       else if (THUMB_SETS_FLAGS (inst.instruction))
11127         narrow = !in_it_block ();
11128       else
11129         narrow = in_it_block ();
11130
11131       if (!inst.operands[1].isreg)
11132         {
11133           /* For an immediate, we always generate a 32-bit opcode;
11134              section relaxation will shrink it later if possible.  */
11135           if (inst.instruction < 0xffff)
11136             inst.instruction = THUMB_OP32 (inst.instruction);
11137           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11138           inst.instruction |= Rn << r0off;
11139           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11140         }
11141       else
11142         {
11143           /* See if we can do this with a 16-bit instruction.  */
11144           if (narrow)
11145             {
11146               inst.instruction = THUMB_OP16 (inst.instruction);
11147               inst.instruction |= Rn;
11148               inst.instruction |= Rm << 3;
11149             }
11150           else
11151             {
11152               constraint (inst.operands[1].shifted
11153                           && inst.operands[1].immisreg,
11154                           _("shift must be constant"));
11155               if (inst.instruction < 0xffff)
11156                 inst.instruction = THUMB_OP32 (inst.instruction);
11157               inst.instruction |= Rn << r0off;
11158               encode_thumb32_shifted_operand (1);
11159             }
11160         }
11161     }
11162   else
11163     {
11164       constraint (inst.instruction > 0xffff
11165                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
11166       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
11167                   _("unshifted register required"));
11168       constraint (Rn > 7 || Rm > 7,
11169                   BAD_HIREG);
11170
11171       inst.instruction = THUMB_OP16 (inst.instruction);
11172       inst.instruction |= Rn;
11173       inst.instruction |= Rm << 3;
11174     }
11175 }
11176
11177 static void
11178 do_t_mrs (void)
11179 {
11180   unsigned Rd;
11181
11182   if (do_vfp_nsyn_mrs () == SUCCESS)
11183     return;
11184
11185   Rd = inst.operands[0].reg;
11186   reject_bad_reg (Rd);
11187   inst.instruction |= Rd << 8;
11188
11189   if (inst.operands[1].isreg)
11190     {
11191       unsigned br = inst.operands[1].reg;
11192       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
11193         as_bad (_("bad register for mrs"));
11194
11195       inst.instruction |= br & (0xf << 16);
11196       inst.instruction |= (br & 0x300) >> 4;
11197       inst.instruction |= (br & SPSR_BIT) >> 2;
11198     }
11199   else
11200     {
11201       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
11202
11203       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
11204         constraint (flags != 0, _("selected processor does not support "
11205                     "requested special purpose register"));
11206       else
11207         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
11208            devices).  */
11209         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
11210                     _("'APSR', 'CPSR' or 'SPSR' expected"));
11211
11212       inst.instruction |= (flags & SPSR_BIT) >> 2;
11213       inst.instruction |= inst.operands[1].imm & 0xff;
11214       inst.instruction |= 0xf0000;
11215     }
11216 }
11217
11218 static void
11219 do_t_msr (void)
11220 {
11221   int flags;
11222   unsigned Rn;
11223
11224   if (do_vfp_nsyn_msr () == SUCCESS)
11225     return;
11226
11227   constraint (!inst.operands[1].isreg,
11228               _("Thumb encoding does not support an immediate here"));
11229
11230   if (inst.operands[0].isreg)
11231     flags = (int)(inst.operands[0].reg);
11232   else
11233     flags = inst.operands[0].imm;
11234
11235   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
11236     {
11237       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
11238
11239       constraint ((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
11240                    && (bits & ~(PSR_s | PSR_f)) != 0)
11241                   || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
11242                       && bits != PSR_f),
11243                   _("selected processor does not support requested special "
11244                     "purpose register"));
11245     }
11246   else
11247      constraint ((flags & 0xff) != 0, _("selected processor does not support "
11248                  "requested special purpose register"));
11249
11250   Rn = inst.operands[1].reg;
11251   reject_bad_reg (Rn);
11252
11253   inst.instruction |= (flags & SPSR_BIT) >> 2;
11254   inst.instruction |= (flags & 0xf0000) >> 8;
11255   inst.instruction |= (flags & 0x300) >> 4;
11256   inst.instruction |= (flags & 0xff);
11257   inst.instruction |= Rn << 16;
11258 }
11259
11260 static void
11261 do_t_mul (void)
11262 {
11263   bfd_boolean narrow;
11264   unsigned Rd, Rn, Rm;
11265
11266   if (!inst.operands[2].present)
11267     inst.operands[2].reg = inst.operands[0].reg;
11268
11269   Rd = inst.operands[0].reg;
11270   Rn = inst.operands[1].reg;
11271   Rm = inst.operands[2].reg;
11272
11273   if (unified_syntax)
11274     {
11275       if (inst.size_req == 4
11276           || (Rd != Rn
11277               && Rd != Rm)
11278           || Rn > 7
11279           || Rm > 7)
11280         narrow = FALSE;
11281       else if (inst.instruction == T_MNEM_muls)
11282         narrow = !in_it_block ();
11283       else
11284         narrow = in_it_block ();
11285     }
11286   else
11287     {
11288       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
11289       constraint (Rn > 7 || Rm > 7,
11290                   BAD_HIREG);
11291       narrow = TRUE;
11292     }
11293
11294   if (narrow)
11295     {
11296       /* 16-bit MULS/Conditional MUL.  */
11297       inst.instruction = THUMB_OP16 (inst.instruction);
11298       inst.instruction |= Rd;
11299
11300       if (Rd == Rn)
11301         inst.instruction |= Rm << 3;
11302       else if (Rd == Rm)
11303         inst.instruction |= Rn << 3;
11304       else
11305         constraint (1, _("dest must overlap one source register"));
11306     }
11307   else
11308     {
11309       constraint (inst.instruction != T_MNEM_mul,
11310                   _("Thumb-2 MUL must not set flags"));
11311       /* 32-bit MUL.  */
11312       inst.instruction = THUMB_OP32 (inst.instruction);
11313       inst.instruction |= Rd << 8;
11314       inst.instruction |= Rn << 16;
11315       inst.instruction |= Rm << 0;
11316
11317       reject_bad_reg (Rd);
11318       reject_bad_reg (Rn);
11319       reject_bad_reg (Rm);
11320     }
11321 }
11322
11323 static void
11324 do_t_mull (void)
11325 {
11326   unsigned RdLo, RdHi, Rn, Rm;
11327
11328   RdLo = inst.operands[0].reg;
11329   RdHi = inst.operands[1].reg;
11330   Rn = inst.operands[2].reg;
11331   Rm = inst.operands[3].reg;
11332
11333   reject_bad_reg (RdLo);
11334   reject_bad_reg (RdHi);
11335   reject_bad_reg (Rn);
11336   reject_bad_reg (Rm);
11337
11338   inst.instruction |= RdLo << 12;
11339   inst.instruction |= RdHi << 8;
11340   inst.instruction |= Rn << 16;
11341   inst.instruction |= Rm;
11342
11343  if (RdLo == RdHi)
11344     as_tsktsk (_("rdhi and rdlo must be different"));
11345 }
11346
11347 static void
11348 do_t_nop (void)
11349 {
11350   set_it_insn_type (NEUTRAL_IT_INSN);
11351
11352   if (unified_syntax)
11353     {
11354       if (inst.size_req == 4 || inst.operands[0].imm > 15)
11355         {
11356           inst.instruction = THUMB_OP32 (inst.instruction);
11357           inst.instruction |= inst.operands[0].imm;
11358         }
11359       else
11360         {
11361           /* PR9722: Check for Thumb2 availability before
11362              generating a thumb2 nop instruction.  */
11363           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
11364             {
11365               inst.instruction = THUMB_OP16 (inst.instruction);
11366               inst.instruction |= inst.operands[0].imm << 4;
11367             }
11368           else
11369             inst.instruction = 0x46c0;
11370         }
11371     }
11372   else
11373     {
11374       constraint (inst.operands[0].present,
11375                   _("Thumb does not support NOP with hints"));
11376       inst.instruction = 0x46c0;
11377     }
11378 }
11379
11380 static void
11381 do_t_neg (void)
11382 {
11383   if (unified_syntax)
11384     {
11385       bfd_boolean narrow;
11386
11387       if (THUMB_SETS_FLAGS (inst.instruction))
11388         narrow = !in_it_block ();
11389       else
11390         narrow = in_it_block ();
11391       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
11392         narrow = FALSE;
11393       if (inst.size_req == 4)
11394         narrow = FALSE;
11395
11396       if (!narrow)
11397         {
11398           inst.instruction = THUMB_OP32 (inst.instruction);
11399           inst.instruction |= inst.operands[0].reg << 8;
11400           inst.instruction |= inst.operands[1].reg << 16;
11401         }
11402       else
11403         {
11404           inst.instruction = THUMB_OP16 (inst.instruction);
11405           inst.instruction |= inst.operands[0].reg;
11406           inst.instruction |= inst.operands[1].reg << 3;
11407         }
11408     }
11409   else
11410     {
11411       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
11412                   BAD_HIREG);
11413       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11414
11415       inst.instruction = THUMB_OP16 (inst.instruction);
11416       inst.instruction |= inst.operands[0].reg;
11417       inst.instruction |= inst.operands[1].reg << 3;
11418     }
11419 }
11420
11421 static void
11422 do_t_orn (void)
11423 {
11424   unsigned Rd, Rn;
11425
11426   Rd = inst.operands[0].reg;
11427   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
11428
11429   reject_bad_reg (Rd);
11430   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
11431   reject_bad_reg (Rn);
11432
11433   inst.instruction |= Rd << 8;
11434   inst.instruction |= Rn << 16;
11435
11436   if (!inst.operands[2].isreg)
11437     {
11438       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11439       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11440     }
11441   else
11442     {
11443       unsigned Rm;
11444
11445       Rm = inst.operands[2].reg;
11446       reject_bad_reg (Rm);
11447
11448       constraint (inst.operands[2].shifted
11449                   && inst.operands[2].immisreg,
11450                   _("shift must be constant"));
11451       encode_thumb32_shifted_operand (2);
11452     }
11453 }
11454
11455 static void
11456 do_t_pkhbt (void)
11457 {
11458   unsigned Rd, Rn, Rm;
11459
11460   Rd = inst.operands[0].reg;
11461   Rn = inst.operands[1].reg;
11462   Rm = inst.operands[2].reg;
11463
11464   reject_bad_reg (Rd);
11465   reject_bad_reg (Rn);
11466   reject_bad_reg (Rm);
11467
11468   inst.instruction |= Rd << 8;
11469   inst.instruction |= Rn << 16;
11470   inst.instruction |= Rm;
11471   if (inst.operands[3].present)
11472     {
11473       unsigned int val = inst.reloc.exp.X_add_number;
11474       constraint (inst.reloc.exp.X_op != O_constant,
11475                   _("expression too complex"));
11476       inst.instruction |= (val & 0x1c) << 10;
11477       inst.instruction |= (val & 0x03) << 6;
11478     }
11479 }
11480
11481 static void
11482 do_t_pkhtb (void)
11483 {
11484   if (!inst.operands[3].present)
11485     {
11486       unsigned Rtmp;
11487
11488       inst.instruction &= ~0x00000020;
11489
11490       /* PR 10168.  Swap the Rm and Rn registers.  */
11491       Rtmp = inst.operands[1].reg;
11492       inst.operands[1].reg = inst.operands[2].reg;
11493       inst.operands[2].reg = Rtmp;
11494     }
11495   do_t_pkhbt ();
11496 }
11497
11498 static void
11499 do_t_pld (void)
11500 {
11501   if (inst.operands[0].immisreg)
11502     reject_bad_reg (inst.operands[0].imm);
11503
11504   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
11505 }
11506
11507 static void
11508 do_t_push_pop (void)
11509 {
11510   unsigned mask;
11511
11512   constraint (inst.operands[0].writeback,
11513               _("push/pop do not support {reglist}^"));
11514   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11515               _("expression too complex"));
11516
11517   mask = inst.operands[0].imm;
11518   if ((mask & ~0xff) == 0)
11519     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
11520   else if ((inst.instruction == T_MNEM_push
11521             && (mask & ~0xff) == 1 << REG_LR)
11522            || (inst.instruction == T_MNEM_pop
11523                && (mask & ~0xff) == 1 << REG_PC))
11524     {
11525       inst.instruction = THUMB_OP16 (inst.instruction);
11526       inst.instruction |= THUMB_PP_PC_LR;
11527       inst.instruction |= mask & 0xff;
11528     }
11529   else if (unified_syntax)
11530     {
11531       inst.instruction = THUMB_OP32 (inst.instruction);
11532       encode_thumb2_ldmstm (13, mask, TRUE);
11533     }
11534   else
11535     {
11536       inst.error = _("invalid register list to push/pop instruction");
11537       return;
11538     }
11539 }
11540
11541 static void
11542 do_t_rbit (void)
11543 {
11544   unsigned Rd, Rm;
11545
11546   Rd = inst.operands[0].reg;
11547   Rm = inst.operands[1].reg;
11548
11549   reject_bad_reg (Rd);
11550   reject_bad_reg (Rm);
11551
11552   inst.instruction |= Rd << 8;
11553   inst.instruction |= Rm << 16;
11554   inst.instruction |= Rm;
11555 }
11556
11557 static void
11558 do_t_rev (void)
11559 {
11560   unsigned Rd, Rm;
11561
11562   Rd = inst.operands[0].reg;
11563   Rm = inst.operands[1].reg;
11564
11565   reject_bad_reg (Rd);
11566   reject_bad_reg (Rm);
11567
11568   if (Rd <= 7 && Rm <= 7
11569       && inst.size_req != 4)
11570     {
11571       inst.instruction = THUMB_OP16 (inst.instruction);
11572       inst.instruction |= Rd;
11573       inst.instruction |= Rm << 3;
11574     }
11575   else if (unified_syntax)
11576     {
11577       inst.instruction = THUMB_OP32 (inst.instruction);
11578       inst.instruction |= Rd << 8;
11579       inst.instruction |= Rm << 16;
11580       inst.instruction |= Rm;
11581     }
11582   else
11583     inst.error = BAD_HIREG;
11584 }
11585
11586 static void
11587 do_t_rrx (void)
11588 {
11589   unsigned Rd, Rm;
11590
11591   Rd = inst.operands[0].reg;
11592   Rm = inst.operands[1].reg;
11593
11594   reject_bad_reg (Rd);
11595   reject_bad_reg (Rm);
11596
11597   inst.instruction |= Rd << 8;
11598   inst.instruction |= Rm;
11599 }
11600
11601 static void
11602 do_t_rsb (void)
11603 {
11604   unsigned Rd, Rs;
11605
11606   Rd = inst.operands[0].reg;
11607   Rs = (inst.operands[1].present
11608         ? inst.operands[1].reg    /* Rd, Rs, foo */
11609         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11610
11611   reject_bad_reg (Rd);
11612   reject_bad_reg (Rs);
11613   if (inst.operands[2].isreg)
11614     reject_bad_reg (inst.operands[2].reg);
11615
11616   inst.instruction |= Rd << 8;
11617   inst.instruction |= Rs << 16;
11618   if (!inst.operands[2].isreg)
11619     {
11620       bfd_boolean narrow;
11621
11622       if ((inst.instruction & 0x00100000) != 0)
11623         narrow = !in_it_block ();
11624       else
11625         narrow = in_it_block ();
11626
11627       if (Rd > 7 || Rs > 7)
11628         narrow = FALSE;
11629
11630       if (inst.size_req == 4 || !unified_syntax)
11631         narrow = FALSE;
11632
11633       if (inst.reloc.exp.X_op != O_constant
11634           || inst.reloc.exp.X_add_number != 0)
11635         narrow = FALSE;
11636
11637       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
11638          relaxation, but it doesn't seem worth the hassle.  */
11639       if (narrow)
11640         {
11641           inst.reloc.type = BFD_RELOC_UNUSED;
11642           inst.instruction = THUMB_OP16 (T_MNEM_negs);
11643           inst.instruction |= Rs << 3;
11644           inst.instruction |= Rd;
11645         }
11646       else
11647         {
11648           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11649           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11650         }
11651     }
11652   else
11653     encode_thumb32_shifted_operand (2);
11654 }
11655
11656 static void
11657 do_t_setend (void)
11658 {
11659   set_it_insn_type (OUTSIDE_IT_INSN);
11660   if (inst.operands[0].imm)
11661     inst.instruction |= 0x8;
11662 }
11663
11664 static void
11665 do_t_shift (void)
11666 {
11667   if (!inst.operands[1].present)
11668     inst.operands[1].reg = inst.operands[0].reg;
11669
11670   if (unified_syntax)
11671     {
11672       bfd_boolean narrow;
11673       int shift_kind;
11674
11675       switch (inst.instruction)
11676         {
11677         case T_MNEM_asr:
11678         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
11679         case T_MNEM_lsl:
11680         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
11681         case T_MNEM_lsr:
11682         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
11683         case T_MNEM_ror:
11684         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
11685         default: abort ();
11686         }
11687
11688       if (THUMB_SETS_FLAGS (inst.instruction))
11689         narrow = !in_it_block ();
11690       else
11691         narrow = in_it_block ();
11692       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
11693         narrow = FALSE;
11694       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
11695         narrow = FALSE;
11696       if (inst.operands[2].isreg
11697           && (inst.operands[1].reg != inst.operands[0].reg
11698               || inst.operands[2].reg > 7))
11699         narrow = FALSE;
11700       if (inst.size_req == 4)
11701         narrow = FALSE;
11702
11703       reject_bad_reg (inst.operands[0].reg);
11704       reject_bad_reg (inst.operands[1].reg);
11705
11706       if (!narrow)
11707         {
11708           if (inst.operands[2].isreg)
11709             {
11710               reject_bad_reg (inst.operands[2].reg);
11711               inst.instruction = THUMB_OP32 (inst.instruction);
11712               inst.instruction |= inst.operands[0].reg << 8;
11713               inst.instruction |= inst.operands[1].reg << 16;
11714               inst.instruction |= inst.operands[2].reg;
11715
11716               /* PR 12854: Error on extraneous shifts.  */
11717               constraint (inst.operands[2].shifted,
11718                           _("extraneous shift as part of operand to shift insn"));
11719             }
11720           else
11721             {
11722               inst.operands[1].shifted = 1;
11723               inst.operands[1].shift_kind = shift_kind;
11724               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
11725                                              ? T_MNEM_movs : T_MNEM_mov);
11726               inst.instruction |= inst.operands[0].reg << 8;
11727               encode_thumb32_shifted_operand (1);
11728               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
11729               inst.reloc.type = BFD_RELOC_UNUSED;
11730             }
11731         }
11732       else
11733         {
11734           if (inst.operands[2].isreg)
11735             {
11736               switch (shift_kind)
11737                 {
11738                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
11739                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
11740                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
11741                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
11742                 default: abort ();
11743                 }
11744
11745               inst.instruction |= inst.operands[0].reg;
11746               inst.instruction |= inst.operands[2].reg << 3;
11747
11748               /* PR 12854: Error on extraneous shifts.  */
11749               constraint (inst.operands[2].shifted,
11750                           _("extraneous shift as part of operand to shift insn"));
11751             }
11752           else
11753             {
11754               switch (shift_kind)
11755                 {
11756                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
11757                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
11758                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
11759                 default: abort ();
11760                 }
11761               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11762               inst.instruction |= inst.operands[0].reg;
11763               inst.instruction |= inst.operands[1].reg << 3;
11764             }
11765         }
11766     }
11767   else
11768     {
11769       constraint (inst.operands[0].reg > 7
11770                   || inst.operands[1].reg > 7, BAD_HIREG);
11771       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11772
11773       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
11774         {
11775           constraint (inst.operands[2].reg > 7, BAD_HIREG);
11776           constraint (inst.operands[0].reg != inst.operands[1].reg,
11777                       _("source1 and dest must be same register"));
11778
11779           switch (inst.instruction)
11780             {
11781             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
11782             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
11783             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
11784             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
11785             default: abort ();
11786             }
11787
11788           inst.instruction |= inst.operands[0].reg;
11789           inst.instruction |= inst.operands[2].reg << 3;
11790
11791           /* PR 12854: Error on extraneous shifts.  */
11792           constraint (inst.operands[2].shifted,
11793                       _("extraneous shift as part of operand to shift insn"));
11794         }
11795       else
11796         {
11797           switch (inst.instruction)
11798             {
11799             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
11800             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
11801             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
11802             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
11803             default: abort ();
11804             }
11805           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11806           inst.instruction |= inst.operands[0].reg;
11807           inst.instruction |= inst.operands[1].reg << 3;
11808         }
11809     }
11810 }
11811
11812 static void
11813 do_t_simd (void)
11814 {
11815   unsigned Rd, Rn, Rm;
11816
11817   Rd = inst.operands[0].reg;
11818   Rn = inst.operands[1].reg;
11819   Rm = inst.operands[2].reg;
11820
11821   reject_bad_reg (Rd);
11822   reject_bad_reg (Rn);
11823   reject_bad_reg (Rm);
11824
11825   inst.instruction |= Rd << 8;
11826   inst.instruction |= Rn << 16;
11827   inst.instruction |= Rm;
11828 }
11829
11830 static void
11831 do_t_simd2 (void)
11832 {
11833   unsigned Rd, Rn, Rm;
11834
11835   Rd = inst.operands[0].reg;
11836   Rm = inst.operands[1].reg;
11837   Rn = inst.operands[2].reg;
11838
11839   reject_bad_reg (Rd);
11840   reject_bad_reg (Rn);
11841   reject_bad_reg (Rm);
11842
11843   inst.instruction |= Rd << 8;
11844   inst.instruction |= Rn << 16;
11845   inst.instruction |= Rm;
11846 }
11847
11848 static void
11849 do_t_smc (void)
11850 {
11851   unsigned int value = inst.reloc.exp.X_add_number;
11852   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
11853               _("SMC is not permitted on this architecture"));
11854   constraint (inst.reloc.exp.X_op != O_constant,
11855               _("expression too complex"));
11856   inst.reloc.type = BFD_RELOC_UNUSED;
11857   inst.instruction |= (value & 0xf000) >> 12;
11858   inst.instruction |= (value & 0x0ff0);
11859   inst.instruction |= (value & 0x000f) << 16;
11860 }
11861
11862 static void
11863 do_t_hvc (void)
11864 {
11865   unsigned int value = inst.reloc.exp.X_add_number;
11866
11867   inst.reloc.type = BFD_RELOC_UNUSED;
11868   inst.instruction |= (value & 0x0fff);
11869   inst.instruction |= (value & 0xf000) << 4;
11870 }
11871
11872 static void
11873 do_t_ssat_usat (int bias)
11874 {
11875   unsigned Rd, Rn;
11876
11877   Rd = inst.operands[0].reg;
11878   Rn = inst.operands[2].reg;
11879
11880   reject_bad_reg (Rd);
11881   reject_bad_reg (Rn);
11882
11883   inst.instruction |= Rd << 8;
11884   inst.instruction |= inst.operands[1].imm - bias;
11885   inst.instruction |= Rn << 16;
11886
11887   if (inst.operands[3].present)
11888     {
11889       offsetT shift_amount = inst.reloc.exp.X_add_number;
11890
11891       inst.reloc.type = BFD_RELOC_UNUSED;
11892
11893       constraint (inst.reloc.exp.X_op != O_constant,
11894                   _("expression too complex"));
11895
11896       if (shift_amount != 0)
11897         {
11898           constraint (shift_amount > 31,
11899                       _("shift expression is too large"));
11900
11901           if (inst.operands[3].shift_kind == SHIFT_ASR)
11902             inst.instruction |= 0x00200000;  /* sh bit.  */
11903
11904           inst.instruction |= (shift_amount & 0x1c) << 10;
11905           inst.instruction |= (shift_amount & 0x03) << 6;
11906         }
11907     }
11908 }
11909
11910 static void
11911 do_t_ssat (void)
11912 {
11913   do_t_ssat_usat (1);
11914 }
11915
11916 static void
11917 do_t_ssat16 (void)
11918 {
11919   unsigned Rd, Rn;
11920
11921   Rd = inst.operands[0].reg;
11922   Rn = inst.operands[2].reg;
11923
11924   reject_bad_reg (Rd);
11925   reject_bad_reg (Rn);
11926
11927   inst.instruction |= Rd << 8;
11928   inst.instruction |= inst.operands[1].imm - 1;
11929   inst.instruction |= Rn << 16;
11930 }
11931
11932 static void
11933 do_t_strex (void)
11934 {
11935   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
11936               || inst.operands[2].postind || inst.operands[2].writeback
11937               || inst.operands[2].immisreg || inst.operands[2].shifted
11938               || inst.operands[2].negative,
11939               BAD_ADDR_MODE);
11940
11941   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
11942
11943   inst.instruction |= inst.operands[0].reg << 8;
11944   inst.instruction |= inst.operands[1].reg << 12;
11945   inst.instruction |= inst.operands[2].reg << 16;
11946   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
11947 }
11948
11949 static void
11950 do_t_strexd (void)
11951 {
11952   if (!inst.operands[2].present)
11953     inst.operands[2].reg = inst.operands[1].reg + 1;
11954
11955   constraint (inst.operands[0].reg == inst.operands[1].reg
11956               || inst.operands[0].reg == inst.operands[2].reg
11957               || inst.operands[0].reg == inst.operands[3].reg,
11958               BAD_OVERLAP);
11959
11960   inst.instruction |= inst.operands[0].reg;
11961   inst.instruction |= inst.operands[1].reg << 12;
11962   inst.instruction |= inst.operands[2].reg << 8;
11963   inst.instruction |= inst.operands[3].reg << 16;
11964 }
11965
11966 static void
11967 do_t_sxtah (void)
11968 {
11969   unsigned Rd, Rn, Rm;
11970
11971   Rd = inst.operands[0].reg;
11972   Rn = inst.operands[1].reg;
11973   Rm = inst.operands[2].reg;
11974
11975   reject_bad_reg (Rd);
11976   reject_bad_reg (Rn);
11977   reject_bad_reg (Rm);
11978
11979   inst.instruction |= Rd << 8;
11980   inst.instruction |= Rn << 16;
11981   inst.instruction |= Rm;
11982   inst.instruction |= inst.operands[3].imm << 4;
11983 }
11984
11985 static void
11986 do_t_sxth (void)
11987 {
11988   unsigned Rd, Rm;
11989
11990   Rd = inst.operands[0].reg;
11991   Rm = inst.operands[1].reg;
11992
11993   reject_bad_reg (Rd);
11994   reject_bad_reg (Rm);
11995
11996   if (inst.instruction <= 0xffff
11997       && inst.size_req != 4
11998       && Rd <= 7 && Rm <= 7
11999       && (!inst.operands[2].present || inst.operands[2].imm == 0))
12000     {
12001       inst.instruction = THUMB_OP16 (inst.instruction);
12002       inst.instruction |= Rd;
12003       inst.instruction |= Rm << 3;
12004     }
12005   else if (unified_syntax)
12006     {
12007       if (inst.instruction <= 0xffff)
12008         inst.instruction = THUMB_OP32 (inst.instruction);
12009       inst.instruction |= Rd << 8;
12010       inst.instruction |= Rm;
12011       inst.instruction |= inst.operands[2].imm << 4;
12012     }
12013   else
12014     {
12015       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
12016                   _("Thumb encoding does not support rotation"));
12017       constraint (1, BAD_HIREG);
12018     }
12019 }
12020
12021 static void
12022 do_t_swi (void)
12023 {
12024   /* We have to do the following check manually as ARM_EXT_OS only applies
12025      to ARM_EXT_V6M.  */
12026   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6m))
12027     {
12028       if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_os)
12029           /* This only applies to the v6m howver, not later architectures.  */
12030           && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7))
12031         as_bad (_("SVC is not permitted on this architecture"));
12032       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, arm_ext_os);
12033     }
12034
12035   inst.reloc.type = BFD_RELOC_ARM_SWI;
12036 }
12037
12038 static void
12039 do_t_tb (void)
12040 {
12041   unsigned Rn, Rm;
12042   int half;
12043
12044   half = (inst.instruction & 0x10) != 0;
12045   set_it_insn_type_last ();
12046   constraint (inst.operands[0].immisreg,
12047               _("instruction requires register index"));
12048
12049   Rn = inst.operands[0].reg;
12050   Rm = inst.operands[0].imm;
12051
12052   constraint (Rn == REG_SP, BAD_SP);
12053   reject_bad_reg (Rm);
12054
12055   constraint (!half && inst.operands[0].shifted,
12056               _("instruction does not allow shifted index"));
12057   inst.instruction |= (Rn << 16) | Rm;
12058 }
12059
12060 static void
12061 do_t_usat (void)
12062 {
12063   do_t_ssat_usat (0);
12064 }
12065
12066 static void
12067 do_t_usat16 (void)
12068 {
12069   unsigned Rd, Rn;
12070
12071   Rd = inst.operands[0].reg;
12072   Rn = inst.operands[2].reg;
12073
12074   reject_bad_reg (Rd);
12075   reject_bad_reg (Rn);
12076
12077   inst.instruction |= Rd << 8;
12078   inst.instruction |= inst.operands[1].imm;
12079   inst.instruction |= Rn << 16;
12080 }
12081
12082 /* Neon instruction encoder helpers.  */
12083
12084 /* Encodings for the different types for various Neon opcodes.  */
12085
12086 /* An "invalid" code for the following tables.  */
12087 #define N_INV -1u
12088
12089 struct neon_tab_entry
12090 {
12091   unsigned integer;
12092   unsigned float_or_poly;
12093   unsigned scalar_or_imm;
12094 };
12095
12096 /* Map overloaded Neon opcodes to their respective encodings.  */
12097 #define NEON_ENC_TAB                                    \
12098   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
12099   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
12100   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
12101   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
12102   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
12103   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
12104   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
12105   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
12106   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
12107   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
12108   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
12109   /* Register variants of the following two instructions are encoded as
12110      vcge / vcgt with the operands reversed.  */        \
12111   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
12112   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
12113   X(vfma,       N_INV, 0x0000c10, N_INV),               \
12114   X(vfms,       N_INV, 0x0200c10, N_INV),               \
12115   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
12116   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
12117   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
12118   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
12119   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
12120   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
12121   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
12122   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
12123   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
12124   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
12125   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
12126   X(vshl,       0x0000400, N_INV,     0x0800510),       \
12127   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
12128   X(vand,       0x0000110, N_INV,     0x0800030),       \
12129   X(vbic,       0x0100110, N_INV,     0x0800030),       \
12130   X(veor,       0x1000110, N_INV,     N_INV),           \
12131   X(vorn,       0x0300110, N_INV,     0x0800010),       \
12132   X(vorr,       0x0200110, N_INV,     0x0800010),       \
12133   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
12134   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
12135   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
12136   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
12137   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
12138   X(vst1,       0x0000000, 0x0800000, N_INV),           \
12139   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
12140   X(vst2,       0x0000100, 0x0800100, N_INV),           \
12141   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
12142   X(vst3,       0x0000200, 0x0800200, N_INV),           \
12143   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
12144   X(vst4,       0x0000300, 0x0800300, N_INV),           \
12145   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
12146   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
12147   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
12148   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
12149   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
12150   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
12151   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
12152   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
12153   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
12154   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
12155   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
12156   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
12157   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV)
12158
12159 enum neon_opc
12160 {
12161 #define X(OPC,I,F,S) N_MNEM_##OPC
12162 NEON_ENC_TAB
12163 #undef X
12164 };
12165
12166 static const struct neon_tab_entry neon_enc_tab[] =
12167 {
12168 #define X(OPC,I,F,S) { (I), (F), (S) }
12169 NEON_ENC_TAB
12170 #undef X
12171 };
12172
12173 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
12174 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
12175 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
12176 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12177 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12178 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12179 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12180 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
12181 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12182 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12183 #define NEON_ENC_SINGLE_(X) \
12184   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
12185 #define NEON_ENC_DOUBLE_(X) \
12186   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
12187
12188 #define NEON_ENCODE(type, inst)                                 \
12189   do                                                            \
12190     {                                                           \
12191       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
12192       inst.is_neon = 1;                                         \
12193     }                                                           \
12194   while (0)
12195
12196 #define check_neon_suffixes                                             \
12197   do                                                                    \
12198     {                                                                   \
12199       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
12200         {                                                               \
12201           as_bad (_("invalid neon suffix for non neon instruction"));   \
12202           return;                                                       \
12203         }                                                               \
12204     }                                                                   \
12205   while (0)
12206
12207 /* Define shapes for instruction operands. The following mnemonic characters
12208    are used in this table:
12209
12210      F - VFP S<n> register
12211      D - Neon D<n> register
12212      Q - Neon Q<n> register
12213      I - Immediate
12214      S - Scalar
12215      R - ARM register
12216      L - D<n> register list
12217
12218    This table is used to generate various data:
12219      - enumerations of the form NS_DDR to be used as arguments to
12220        neon_select_shape.
12221      - a table classifying shapes into single, double, quad, mixed.
12222      - a table used to drive neon_select_shape.  */
12223
12224 #define NEON_SHAPE_DEF                  \
12225   X(3, (D, D, D), DOUBLE),              \
12226   X(3, (Q, Q, Q), QUAD),                \
12227   X(3, (D, D, I), DOUBLE),              \
12228   X(3, (Q, Q, I), QUAD),                \
12229   X(3, (D, D, S), DOUBLE),              \
12230   X(3, (Q, Q, S), QUAD),                \
12231   X(2, (D, D), DOUBLE),                 \
12232   X(2, (Q, Q), QUAD),                   \
12233   X(2, (D, S), DOUBLE),                 \
12234   X(2, (Q, S), QUAD),                   \
12235   X(2, (D, R), DOUBLE),                 \
12236   X(2, (Q, R), QUAD),                   \
12237   X(2, (D, I), DOUBLE),                 \
12238   X(2, (Q, I), QUAD),                   \
12239   X(3, (D, L, D), DOUBLE),              \
12240   X(2, (D, Q), MIXED),                  \
12241   X(2, (Q, D), MIXED),                  \
12242   X(3, (D, Q, I), MIXED),               \
12243   X(3, (Q, D, I), MIXED),               \
12244   X(3, (Q, D, D), MIXED),               \
12245   X(3, (D, Q, Q), MIXED),               \
12246   X(3, (Q, Q, D), MIXED),               \
12247   X(3, (Q, D, S), MIXED),               \
12248   X(3, (D, Q, S), MIXED),               \
12249   X(4, (D, D, D, I), DOUBLE),           \
12250   X(4, (Q, Q, Q, I), QUAD),             \
12251   X(2, (F, F), SINGLE),                 \
12252   X(3, (F, F, F), SINGLE),              \
12253   X(2, (F, I), SINGLE),                 \
12254   X(2, (F, D), MIXED),                  \
12255   X(2, (D, F), MIXED),                  \
12256   X(3, (F, F, I), MIXED),               \
12257   X(4, (R, R, F, F), SINGLE),           \
12258   X(4, (F, F, R, R), SINGLE),           \
12259   X(3, (D, R, R), DOUBLE),              \
12260   X(3, (R, R, D), DOUBLE),              \
12261   X(2, (S, R), SINGLE),                 \
12262   X(2, (R, S), SINGLE),                 \
12263   X(2, (F, R), SINGLE),                 \
12264   X(2, (R, F), SINGLE)
12265
12266 #define S2(A,B)         NS_##A##B
12267 #define S3(A,B,C)       NS_##A##B##C
12268 #define S4(A,B,C,D)     NS_##A##B##C##D
12269
12270 #define X(N, L, C) S##N L
12271
12272 enum neon_shape
12273 {
12274   NEON_SHAPE_DEF,
12275   NS_NULL
12276 };
12277
12278 #undef X
12279 #undef S2
12280 #undef S3
12281 #undef S4
12282
12283 enum neon_shape_class
12284 {
12285   SC_SINGLE,
12286   SC_DOUBLE,
12287   SC_QUAD,
12288   SC_MIXED
12289 };
12290
12291 #define X(N, L, C) SC_##C
12292
12293 static enum neon_shape_class neon_shape_class[] =
12294 {
12295   NEON_SHAPE_DEF
12296 };
12297
12298 #undef X
12299
12300 enum neon_shape_el
12301 {
12302   SE_F,
12303   SE_D,
12304   SE_Q,
12305   SE_I,
12306   SE_S,
12307   SE_R,
12308   SE_L
12309 };
12310
12311 /* Register widths of above.  */
12312 static unsigned neon_shape_el_size[] =
12313 {
12314   32,
12315   64,
12316   128,
12317   0,
12318   32,
12319   32,
12320   0
12321 };
12322
12323 struct neon_shape_info
12324 {
12325   unsigned els;
12326   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
12327 };
12328
12329 #define S2(A,B)         { SE_##A, SE_##B }
12330 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
12331 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
12332
12333 #define X(N, L, C) { N, S##N L }
12334
12335 static struct neon_shape_info neon_shape_tab[] =
12336 {
12337   NEON_SHAPE_DEF
12338 };
12339
12340 #undef X
12341 #undef S2
12342 #undef S3
12343 #undef S4
12344
12345 /* Bit masks used in type checking given instructions.
12346   'N_EQK' means the type must be the same as (or based on in some way) the key
12347    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
12348    set, various other bits can be set as well in order to modify the meaning of
12349    the type constraint.  */
12350
12351 enum neon_type_mask
12352 {
12353   N_S8   = 0x0000001,
12354   N_S16  = 0x0000002,
12355   N_S32  = 0x0000004,
12356   N_S64  = 0x0000008,
12357   N_U8   = 0x0000010,
12358   N_U16  = 0x0000020,
12359   N_U32  = 0x0000040,
12360   N_U64  = 0x0000080,
12361   N_I8   = 0x0000100,
12362   N_I16  = 0x0000200,
12363   N_I32  = 0x0000400,
12364   N_I64  = 0x0000800,
12365   N_8    = 0x0001000,
12366   N_16   = 0x0002000,
12367   N_32   = 0x0004000,
12368   N_64   = 0x0008000,
12369   N_P8   = 0x0010000,
12370   N_P16  = 0x0020000,
12371   N_F16  = 0x0040000,
12372   N_F32  = 0x0080000,
12373   N_F64  = 0x0100000,
12374   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
12375   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
12376   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
12377   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
12378   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
12379   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
12380   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
12381   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
12382   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
12383   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
12384   N_UTYP = 0,
12385   N_MAX_NONSPECIAL = N_F64
12386 };
12387
12388 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
12389
12390 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
12391 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
12392 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
12393 #define N_SUF_32   (N_SU_32 | N_F32)
12394 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
12395 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F32)
12396
12397 /* Pass this as the first type argument to neon_check_type to ignore types
12398    altogether.  */
12399 #define N_IGNORE_TYPE (N_KEY | N_EQK)
12400
12401 /* Select a "shape" for the current instruction (describing register types or
12402    sizes) from a list of alternatives. Return NS_NULL if the current instruction
12403    doesn't fit. For non-polymorphic shapes, checking is usually done as a
12404    function of operand parsing, so this function doesn't need to be called.
12405    Shapes should be listed in order of decreasing length.  */
12406
12407 static enum neon_shape
12408 neon_select_shape (enum neon_shape shape, ...)
12409 {
12410   va_list ap;
12411   enum neon_shape first_shape = shape;
12412
12413   /* Fix missing optional operands. FIXME: we don't know at this point how
12414      many arguments we should have, so this makes the assumption that we have
12415      > 1. This is true of all current Neon opcodes, I think, but may not be
12416      true in the future.  */
12417   if (!inst.operands[1].present)
12418     inst.operands[1] = inst.operands[0];
12419
12420   va_start (ap, shape);
12421
12422   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
12423     {
12424       unsigned j;
12425       int matches = 1;
12426
12427       for (j = 0; j < neon_shape_tab[shape].els; j++)
12428         {
12429           if (!inst.operands[j].present)
12430             {
12431               matches = 0;
12432               break;
12433             }
12434
12435           switch (neon_shape_tab[shape].el[j])
12436             {
12437             case SE_F:
12438               if (!(inst.operands[j].isreg
12439                     && inst.operands[j].isvec
12440                     && inst.operands[j].issingle
12441                     && !inst.operands[j].isquad))
12442                 matches = 0;
12443               break;
12444
12445             case SE_D:
12446               if (!(inst.operands[j].isreg
12447                     && inst.operands[j].isvec
12448                     && !inst.operands[j].isquad
12449                     && !inst.operands[j].issingle))
12450                 matches = 0;
12451               break;
12452
12453             case SE_R:
12454               if (!(inst.operands[j].isreg
12455                     && !inst.operands[j].isvec))
12456                 matches = 0;
12457               break;
12458
12459             case SE_Q:
12460               if (!(inst.operands[j].isreg
12461                     && inst.operands[j].isvec
12462                     && inst.operands[j].isquad
12463                     && !inst.operands[j].issingle))
12464                 matches = 0;
12465               break;
12466
12467             case SE_I:
12468               if (!(!inst.operands[j].isreg
12469                     && !inst.operands[j].isscalar))
12470                 matches = 0;
12471               break;
12472
12473             case SE_S:
12474               if (!(!inst.operands[j].isreg
12475                     && inst.operands[j].isscalar))
12476                 matches = 0;
12477               break;
12478
12479             case SE_L:
12480               break;
12481             }
12482           if (!matches)
12483             break;
12484         }
12485       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
12486         /* We've matched all the entries in the shape table, and we don't
12487            have any left over operands which have not been matched.  */
12488         break;
12489     }
12490
12491   va_end (ap);
12492
12493   if (shape == NS_NULL && first_shape != NS_NULL)
12494     first_error (_("invalid instruction shape"));
12495
12496   return shape;
12497 }
12498
12499 /* True if SHAPE is predominantly a quadword operation (most of the time, this
12500    means the Q bit should be set).  */
12501
12502 static int
12503 neon_quad (enum neon_shape shape)
12504 {
12505   return neon_shape_class[shape] == SC_QUAD;
12506 }
12507
12508 static void
12509 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
12510                        unsigned *g_size)
12511 {
12512   /* Allow modification to be made to types which are constrained to be
12513      based on the key element, based on bits set alongside N_EQK.  */
12514   if ((typebits & N_EQK) != 0)
12515     {
12516       if ((typebits & N_HLF) != 0)
12517         *g_size /= 2;
12518       else if ((typebits & N_DBL) != 0)
12519         *g_size *= 2;
12520       if ((typebits & N_SGN) != 0)
12521         *g_type = NT_signed;
12522       else if ((typebits & N_UNS) != 0)
12523         *g_type = NT_unsigned;
12524       else if ((typebits & N_INT) != 0)
12525         *g_type = NT_integer;
12526       else if ((typebits & N_FLT) != 0)
12527         *g_type = NT_float;
12528       else if ((typebits & N_SIZ) != 0)
12529         *g_type = NT_untyped;
12530     }
12531 }
12532
12533 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
12534    operand type, i.e. the single type specified in a Neon instruction when it
12535    is the only one given.  */
12536
12537 static struct neon_type_el
12538 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
12539 {
12540   struct neon_type_el dest = *key;
12541
12542   gas_assert ((thisarg & N_EQK) != 0);
12543
12544   neon_modify_type_size (thisarg, &dest.type, &dest.size);
12545
12546   return dest;
12547 }
12548
12549 /* Convert Neon type and size into compact bitmask representation.  */
12550
12551 static enum neon_type_mask
12552 type_chk_of_el_type (enum neon_el_type type, unsigned size)
12553 {
12554   switch (type)
12555     {
12556     case NT_untyped:
12557       switch (size)
12558         {
12559         case 8:  return N_8;
12560         case 16: return N_16;
12561         case 32: return N_32;
12562         case 64: return N_64;
12563         default: ;
12564         }
12565       break;
12566
12567     case NT_integer:
12568       switch (size)
12569         {
12570         case 8:  return N_I8;
12571         case 16: return N_I16;
12572         case 32: return N_I32;
12573         case 64: return N_I64;
12574         default: ;
12575         }
12576       break;
12577
12578     case NT_float:
12579       switch (size)
12580         {
12581         case 16: return N_F16;
12582         case 32: return N_F32;
12583         case 64: return N_F64;
12584         default: ;
12585         }
12586       break;
12587
12588     case NT_poly:
12589       switch (size)
12590         {
12591         case 8:  return N_P8;
12592         case 16: return N_P16;
12593         default: ;
12594         }
12595       break;
12596
12597     case NT_signed:
12598       switch (size)
12599         {
12600         case 8:  return N_S8;
12601         case 16: return N_S16;
12602         case 32: return N_S32;
12603         case 64: return N_S64;
12604         default: ;
12605         }
12606       break;
12607
12608     case NT_unsigned:
12609       switch (size)
12610         {
12611         case 8:  return N_U8;
12612         case 16: return N_U16;
12613         case 32: return N_U32;
12614         case 64: return N_U64;
12615         default: ;
12616         }
12617       break;
12618
12619     default: ;
12620     }
12621
12622   return N_UTYP;
12623 }
12624
12625 /* Convert compact Neon bitmask type representation to a type and size. Only
12626    handles the case where a single bit is set in the mask.  */
12627
12628 static int
12629 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
12630                      enum neon_type_mask mask)
12631 {
12632   if ((mask & N_EQK) != 0)
12633     return FAIL;
12634
12635   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
12636     *size = 8;
12637   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_P16)) != 0)
12638     *size = 16;
12639   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
12640     *size = 32;
12641   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64)) != 0)
12642     *size = 64;
12643   else
12644     return FAIL;
12645
12646   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
12647     *type = NT_signed;
12648   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
12649     *type = NT_unsigned;
12650   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
12651     *type = NT_integer;
12652   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
12653     *type = NT_untyped;
12654   else if ((mask & (N_P8 | N_P16)) != 0)
12655     *type = NT_poly;
12656   else if ((mask & (N_F32 | N_F64)) != 0)
12657     *type = NT_float;
12658   else
12659     return FAIL;
12660
12661   return SUCCESS;
12662 }
12663
12664 /* Modify a bitmask of allowed types. This is only needed for type
12665    relaxation.  */
12666
12667 static unsigned
12668 modify_types_allowed (unsigned allowed, unsigned mods)
12669 {
12670   unsigned size;
12671   enum neon_el_type type;
12672   unsigned destmask;
12673   int i;
12674
12675   destmask = 0;
12676
12677   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
12678     {
12679       if (el_type_of_type_chk (&type, &size,
12680                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
12681         {
12682           neon_modify_type_size (mods, &type, &size);
12683           destmask |= type_chk_of_el_type (type, size);
12684         }
12685     }
12686
12687   return destmask;
12688 }
12689
12690 /* Check type and return type classification.
12691    The manual states (paraphrase): If one datatype is given, it indicates the
12692    type given in:
12693     - the second operand, if there is one
12694     - the operand, if there is no second operand
12695     - the result, if there are no operands.
12696    This isn't quite good enough though, so we use a concept of a "key" datatype
12697    which is set on a per-instruction basis, which is the one which matters when
12698    only one data type is written.
12699    Note: this function has side-effects (e.g. filling in missing operands). All
12700    Neon instructions should call it before performing bit encoding.  */
12701
12702 static struct neon_type_el
12703 neon_check_type (unsigned els, enum neon_shape ns, ...)
12704 {
12705   va_list ap;
12706   unsigned i, pass, key_el = 0;
12707   unsigned types[NEON_MAX_TYPE_ELS];
12708   enum neon_el_type k_type = NT_invtype;
12709   unsigned k_size = -1u;
12710   struct neon_type_el badtype = {NT_invtype, -1};
12711   unsigned key_allowed = 0;
12712
12713   /* Optional registers in Neon instructions are always (not) in operand 1.
12714      Fill in the missing operand here, if it was omitted.  */
12715   if (els > 1 && !inst.operands[1].present)
12716     inst.operands[1] = inst.operands[0];
12717
12718   /* Suck up all the varargs.  */
12719   va_start (ap, ns);
12720   for (i = 0; i < els; i++)
12721     {
12722       unsigned thisarg = va_arg (ap, unsigned);
12723       if (thisarg == N_IGNORE_TYPE)
12724         {
12725           va_end (ap);
12726           return badtype;
12727         }
12728       types[i] = thisarg;
12729       if ((thisarg & N_KEY) != 0)
12730         key_el = i;
12731     }
12732   va_end (ap);
12733
12734   if (inst.vectype.elems > 0)
12735     for (i = 0; i < els; i++)
12736       if (inst.operands[i].vectype.type != NT_invtype)
12737         {
12738           first_error (_("types specified in both the mnemonic and operands"));
12739           return badtype;
12740         }
12741
12742   /* Duplicate inst.vectype elements here as necessary.
12743      FIXME: No idea if this is exactly the same as the ARM assembler,
12744      particularly when an insn takes one register and one non-register
12745      operand. */
12746   if (inst.vectype.elems == 1 && els > 1)
12747     {
12748       unsigned j;
12749       inst.vectype.elems = els;
12750       inst.vectype.el[key_el] = inst.vectype.el[0];
12751       for (j = 0; j < els; j++)
12752         if (j != key_el)
12753           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
12754                                                   types[j]);
12755     }
12756   else if (inst.vectype.elems == 0 && els > 0)
12757     {
12758       unsigned j;
12759       /* No types were given after the mnemonic, so look for types specified
12760          after each operand. We allow some flexibility here; as long as the
12761          "key" operand has a type, we can infer the others.  */
12762       for (j = 0; j < els; j++)
12763         if (inst.operands[j].vectype.type != NT_invtype)
12764           inst.vectype.el[j] = inst.operands[j].vectype;
12765
12766       if (inst.operands[key_el].vectype.type != NT_invtype)
12767         {
12768           for (j = 0; j < els; j++)
12769             if (inst.operands[j].vectype.type == NT_invtype)
12770               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
12771                                                       types[j]);
12772         }
12773       else
12774         {
12775           first_error (_("operand types can't be inferred"));
12776           return badtype;
12777         }
12778     }
12779   else if (inst.vectype.elems != els)
12780     {
12781       first_error (_("type specifier has the wrong number of parts"));
12782       return badtype;
12783     }
12784
12785   for (pass = 0; pass < 2; pass++)
12786     {
12787       for (i = 0; i < els; i++)
12788         {
12789           unsigned thisarg = types[i];
12790           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
12791             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
12792           enum neon_el_type g_type = inst.vectype.el[i].type;
12793           unsigned g_size = inst.vectype.el[i].size;
12794
12795           /* Decay more-specific signed & unsigned types to sign-insensitive
12796              integer types if sign-specific variants are unavailable.  */
12797           if ((g_type == NT_signed || g_type == NT_unsigned)
12798               && (types_allowed & N_SU_ALL) == 0)
12799             g_type = NT_integer;
12800
12801           /* If only untyped args are allowed, decay any more specific types to
12802              them. Some instructions only care about signs for some element
12803              sizes, so handle that properly.  */
12804           if ((g_size == 8 && (types_allowed & N_8) != 0)
12805               || (g_size == 16 && (types_allowed & N_16) != 0)
12806               || (g_size == 32 && (types_allowed & N_32) != 0)
12807               || (g_size == 64 && (types_allowed & N_64) != 0))
12808             g_type = NT_untyped;
12809
12810           if (pass == 0)
12811             {
12812               if ((thisarg & N_KEY) != 0)
12813                 {
12814                   k_type = g_type;
12815                   k_size = g_size;
12816                   key_allowed = thisarg & ~N_KEY;
12817                 }
12818             }
12819           else
12820             {
12821               if ((thisarg & N_VFP) != 0)
12822                 {
12823                   enum neon_shape_el regshape;
12824                   unsigned regwidth, match;
12825
12826                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
12827                   if (ns == NS_NULL)
12828                     {
12829                       first_error (_("invalid instruction shape"));
12830                       return badtype;
12831                     }
12832                   regshape = neon_shape_tab[ns].el[i];
12833                   regwidth = neon_shape_el_size[regshape];
12834
12835                   /* In VFP mode, operands must match register widths. If we
12836                      have a key operand, use its width, else use the width of
12837                      the current operand.  */
12838                   if (k_size != -1u)
12839                     match = k_size;
12840                   else
12841                     match = g_size;
12842
12843                   if (regwidth != match)
12844                     {
12845                       first_error (_("operand size must match register width"));
12846                       return badtype;
12847                     }
12848                 }
12849
12850               if ((thisarg & N_EQK) == 0)
12851                 {
12852                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
12853
12854                   if ((given_type & types_allowed) == 0)
12855                     {
12856                       first_error (_("bad type in Neon instruction"));
12857                       return badtype;
12858                     }
12859                 }
12860               else
12861                 {
12862                   enum neon_el_type mod_k_type = k_type;
12863                   unsigned mod_k_size = k_size;
12864                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
12865                   if (g_type != mod_k_type || g_size != mod_k_size)
12866                     {
12867                       first_error (_("inconsistent types in Neon instruction"));
12868                       return badtype;
12869                     }
12870                 }
12871             }
12872         }
12873     }
12874
12875   return inst.vectype.el[key_el];
12876 }
12877
12878 /* Neon-style VFP instruction forwarding.  */
12879
12880 /* Thumb VFP instructions have 0xE in the condition field.  */
12881
12882 static void
12883 do_vfp_cond_or_thumb (void)
12884 {
12885   inst.is_neon = 1;
12886
12887   if (thumb_mode)
12888     inst.instruction |= 0xe0000000;
12889   else
12890     inst.instruction |= inst.cond << 28;
12891 }
12892
12893 /* Look up and encode a simple mnemonic, for use as a helper function for the
12894    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
12895    etc.  It is assumed that operand parsing has already been done, and that the
12896    operands are in the form expected by the given opcode (this isn't necessarily
12897    the same as the form in which they were parsed, hence some massaging must
12898    take place before this function is called).
12899    Checks current arch version against that in the looked-up opcode.  */
12900
12901 static void
12902 do_vfp_nsyn_opcode (const char *opname)
12903 {
12904   const struct asm_opcode *opcode;
12905
12906   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
12907
12908   if (!opcode)
12909     abort ();
12910
12911   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
12912                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
12913               _(BAD_FPU));
12914
12915   inst.is_neon = 1;
12916
12917   if (thumb_mode)
12918     {
12919       inst.instruction = opcode->tvalue;
12920       opcode->tencode ();
12921     }
12922   else
12923     {
12924       inst.instruction = (inst.cond << 28) | opcode->avalue;
12925       opcode->aencode ();
12926     }
12927 }
12928
12929 static void
12930 do_vfp_nsyn_add_sub (enum neon_shape rs)
12931 {
12932   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
12933
12934   if (rs == NS_FFF)
12935     {
12936       if (is_add)
12937         do_vfp_nsyn_opcode ("fadds");
12938       else
12939         do_vfp_nsyn_opcode ("fsubs");
12940     }
12941   else
12942     {
12943       if (is_add)
12944         do_vfp_nsyn_opcode ("faddd");
12945       else
12946         do_vfp_nsyn_opcode ("fsubd");
12947     }
12948 }
12949
12950 /* Check operand types to see if this is a VFP instruction, and if so call
12951    PFN ().  */
12952
12953 static int
12954 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
12955 {
12956   enum neon_shape rs;
12957   struct neon_type_el et;
12958
12959   switch (args)
12960     {
12961     case 2:
12962       rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
12963       et = neon_check_type (2, rs,
12964         N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12965       break;
12966
12967     case 3:
12968       rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
12969       et = neon_check_type (3, rs,
12970         N_EQK | N_VFP, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12971       break;
12972
12973     default:
12974       abort ();
12975     }
12976
12977   if (et.type != NT_invtype)
12978     {
12979       pfn (rs);
12980       return SUCCESS;
12981     }
12982
12983   inst.error = NULL;
12984   return FAIL;
12985 }
12986
12987 static void
12988 do_vfp_nsyn_mla_mls (enum neon_shape rs)
12989 {
12990   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
12991
12992   if (rs == NS_FFF)
12993     {
12994       if (is_mla)
12995         do_vfp_nsyn_opcode ("fmacs");
12996       else
12997         do_vfp_nsyn_opcode ("fnmacs");
12998     }
12999   else
13000     {
13001       if (is_mla)
13002         do_vfp_nsyn_opcode ("fmacd");
13003       else
13004         do_vfp_nsyn_opcode ("fnmacd");
13005     }
13006 }
13007
13008 static void
13009 do_vfp_nsyn_fma_fms (enum neon_shape rs)
13010 {
13011   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
13012
13013   if (rs == NS_FFF)
13014     {
13015       if (is_fma)
13016         do_vfp_nsyn_opcode ("ffmas");
13017       else
13018         do_vfp_nsyn_opcode ("ffnmas");
13019     }
13020   else
13021     {
13022       if (is_fma)
13023         do_vfp_nsyn_opcode ("ffmad");
13024       else
13025         do_vfp_nsyn_opcode ("ffnmad");
13026     }
13027 }
13028
13029 static void
13030 do_vfp_nsyn_mul (enum neon_shape rs)
13031 {
13032   if (rs == NS_FFF)
13033     do_vfp_nsyn_opcode ("fmuls");
13034   else
13035     do_vfp_nsyn_opcode ("fmuld");
13036 }
13037
13038 static void
13039 do_vfp_nsyn_abs_neg (enum neon_shape rs)
13040 {
13041   int is_neg = (inst.instruction & 0x80) != 0;
13042   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_VFP | N_KEY);
13043
13044   if (rs == NS_FF)
13045     {
13046       if (is_neg)
13047         do_vfp_nsyn_opcode ("fnegs");
13048       else
13049         do_vfp_nsyn_opcode ("fabss");
13050     }
13051   else
13052     {
13053       if (is_neg)
13054         do_vfp_nsyn_opcode ("fnegd");
13055       else
13056         do_vfp_nsyn_opcode ("fabsd");
13057     }
13058 }
13059
13060 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
13061    insns belong to Neon, and are handled elsewhere.  */
13062
13063 static void
13064 do_vfp_nsyn_ldm_stm (int is_dbmode)
13065 {
13066   int is_ldm = (inst.instruction & (1 << 20)) != 0;
13067   if (is_ldm)
13068     {
13069       if (is_dbmode)
13070         do_vfp_nsyn_opcode ("fldmdbs");
13071       else
13072         do_vfp_nsyn_opcode ("fldmias");
13073     }
13074   else
13075     {
13076       if (is_dbmode)
13077         do_vfp_nsyn_opcode ("fstmdbs");
13078       else
13079         do_vfp_nsyn_opcode ("fstmias");
13080     }
13081 }
13082
13083 static void
13084 do_vfp_nsyn_sqrt (void)
13085 {
13086   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
13087   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13088
13089   if (rs == NS_FF)
13090     do_vfp_nsyn_opcode ("fsqrts");
13091   else
13092     do_vfp_nsyn_opcode ("fsqrtd");
13093 }
13094
13095 static void
13096 do_vfp_nsyn_div (void)
13097 {
13098   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
13099   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
13100     N_F32 | N_F64 | N_KEY | N_VFP);
13101
13102   if (rs == NS_FFF)
13103     do_vfp_nsyn_opcode ("fdivs");
13104   else
13105     do_vfp_nsyn_opcode ("fdivd");
13106 }
13107
13108 static void
13109 do_vfp_nsyn_nmul (void)
13110 {
13111   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
13112   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
13113     N_F32 | N_F64 | N_KEY | N_VFP);
13114
13115   if (rs == NS_FFF)
13116     {
13117       NEON_ENCODE (SINGLE, inst);
13118       do_vfp_sp_dyadic ();
13119     }
13120   else
13121     {
13122       NEON_ENCODE (DOUBLE, inst);
13123       do_vfp_dp_rd_rn_rm ();
13124     }
13125   do_vfp_cond_or_thumb ();
13126 }
13127
13128 static void
13129 do_vfp_nsyn_cmp (void)
13130 {
13131   if (inst.operands[1].isreg)
13132     {
13133       enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
13134       neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13135
13136       if (rs == NS_FF)
13137         {
13138           NEON_ENCODE (SINGLE, inst);
13139           do_vfp_sp_monadic ();
13140         }
13141       else
13142         {
13143           NEON_ENCODE (DOUBLE, inst);
13144           do_vfp_dp_rd_rm ();
13145         }
13146     }
13147   else
13148     {
13149       enum neon_shape rs = neon_select_shape (NS_FI, NS_DI, NS_NULL);
13150       neon_check_type (2, rs, N_F32 | N_F64 | N_KEY | N_VFP, N_EQK);
13151
13152       switch (inst.instruction & 0x0fffffff)
13153         {
13154         case N_MNEM_vcmp:
13155           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
13156           break;
13157         case N_MNEM_vcmpe:
13158           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
13159           break;
13160         default:
13161           abort ();
13162         }
13163
13164       if (rs == NS_FI)
13165         {
13166           NEON_ENCODE (SINGLE, inst);
13167           do_vfp_sp_compare_z ();
13168         }
13169       else
13170         {
13171           NEON_ENCODE (DOUBLE, inst);
13172           do_vfp_dp_rd ();
13173         }
13174     }
13175   do_vfp_cond_or_thumb ();
13176 }
13177
13178 static void
13179 nsyn_insert_sp (void)
13180 {
13181   inst.operands[1] = inst.operands[0];
13182   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
13183   inst.operands[0].reg = REG_SP;
13184   inst.operands[0].isreg = 1;
13185   inst.operands[0].writeback = 1;
13186   inst.operands[0].present = 1;
13187 }
13188
13189 static void
13190 do_vfp_nsyn_push (void)
13191 {
13192   nsyn_insert_sp ();
13193   if (inst.operands[1].issingle)
13194     do_vfp_nsyn_opcode ("fstmdbs");
13195   else
13196     do_vfp_nsyn_opcode ("fstmdbd");
13197 }
13198
13199 static void
13200 do_vfp_nsyn_pop (void)
13201 {
13202   nsyn_insert_sp ();
13203   if (inst.operands[1].issingle)
13204     do_vfp_nsyn_opcode ("fldmias");
13205   else
13206     do_vfp_nsyn_opcode ("fldmiad");
13207 }
13208
13209 /* Fix up Neon data-processing instructions, ORing in the correct bits for
13210    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
13211
13212 static void
13213 neon_dp_fixup (struct arm_it* insn)
13214 {
13215   unsigned int i = insn->instruction;
13216   insn->is_neon = 1;
13217
13218   if (thumb_mode)
13219     {
13220       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
13221       if (i & (1 << 24))
13222         i |= 1 << 28;
13223
13224       i &= ~(1 << 24);
13225
13226       i |= 0xef000000;
13227     }
13228   else
13229     i |= 0xf2000000;
13230
13231   insn->instruction = i;
13232 }
13233
13234 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
13235    (0, 1, 2, 3).  */
13236
13237 static unsigned
13238 neon_logbits (unsigned x)
13239 {
13240   return ffs (x) - 4;
13241 }
13242
13243 #define LOW4(R) ((R) & 0xf)
13244 #define HI1(R) (((R) >> 4) & 1)
13245
13246 /* Encode insns with bit pattern:
13247
13248   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
13249   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
13250
13251   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
13252   different meaning for some instruction.  */
13253
13254 static void
13255 neon_three_same (int isquad, int ubit, int size)
13256 {
13257   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13258   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13259   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
13260   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
13261   inst.instruction |= LOW4 (inst.operands[2].reg);
13262   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
13263   inst.instruction |= (isquad != 0) << 6;
13264   inst.instruction |= (ubit != 0) << 24;
13265   if (size != -1)
13266     inst.instruction |= neon_logbits (size) << 20;
13267
13268   neon_dp_fixup (&inst);
13269 }
13270
13271 /* Encode instructions of the form:
13272
13273   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
13274   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
13275
13276   Don't write size if SIZE == -1.  */
13277
13278 static void
13279 neon_two_same (int qbit, int ubit, int size)
13280 {
13281   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13282   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13283   inst.instruction |= LOW4 (inst.operands[1].reg);
13284   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13285   inst.instruction |= (qbit != 0) << 6;
13286   inst.instruction |= (ubit != 0) << 24;
13287
13288   if (size != -1)
13289     inst.instruction |= neon_logbits (size) << 18;
13290
13291   neon_dp_fixup (&inst);
13292 }
13293
13294 /* Neon instruction encoders, in approximate order of appearance.  */
13295
13296 static void
13297 do_neon_dyadic_i_su (void)
13298 {
13299   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13300   struct neon_type_el et = neon_check_type (3, rs,
13301     N_EQK, N_EQK, N_SU_32 | N_KEY);
13302   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13303 }
13304
13305 static void
13306 do_neon_dyadic_i64_su (void)
13307 {
13308   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13309   struct neon_type_el et = neon_check_type (3, rs,
13310     N_EQK, N_EQK, N_SU_ALL | N_KEY);
13311   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13312 }
13313
13314 static void
13315 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
13316                 unsigned immbits)
13317 {
13318   unsigned size = et.size >> 3;
13319   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13320   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13321   inst.instruction |= LOW4 (inst.operands[1].reg);
13322   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13323   inst.instruction |= (isquad != 0) << 6;
13324   inst.instruction |= immbits << 16;
13325   inst.instruction |= (size >> 3) << 7;
13326   inst.instruction |= (size & 0x7) << 19;
13327   if (write_ubit)
13328     inst.instruction |= (uval != 0) << 24;
13329
13330   neon_dp_fixup (&inst);
13331 }
13332
13333 static void
13334 do_neon_shl_imm (void)
13335 {
13336   if (!inst.operands[2].isreg)
13337     {
13338       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13339       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
13340       NEON_ENCODE (IMMED, inst);
13341       neon_imm_shift (FALSE, 0, neon_quad (rs), et, inst.operands[2].imm);
13342     }
13343   else
13344     {
13345       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13346       struct neon_type_el et = neon_check_type (3, rs,
13347         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
13348       unsigned int tmp;
13349
13350       /* VSHL/VQSHL 3-register variants have syntax such as:
13351            vshl.xx Dd, Dm, Dn
13352          whereas other 3-register operations encoded by neon_three_same have
13353          syntax like:
13354            vadd.xx Dd, Dn, Dm
13355          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
13356          here.  */
13357       tmp = inst.operands[2].reg;
13358       inst.operands[2].reg = inst.operands[1].reg;
13359       inst.operands[1].reg = tmp;
13360       NEON_ENCODE (INTEGER, inst);
13361       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13362     }
13363 }
13364
13365 static void
13366 do_neon_qshl_imm (void)
13367 {
13368   if (!inst.operands[2].isreg)
13369     {
13370       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13371       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
13372
13373       NEON_ENCODE (IMMED, inst);
13374       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
13375                       inst.operands[2].imm);
13376     }
13377   else
13378     {
13379       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13380       struct neon_type_el et = neon_check_type (3, rs,
13381         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
13382       unsigned int tmp;
13383
13384       /* See note in do_neon_shl_imm.  */
13385       tmp = inst.operands[2].reg;
13386       inst.operands[2].reg = inst.operands[1].reg;
13387       inst.operands[1].reg = tmp;
13388       NEON_ENCODE (INTEGER, inst);
13389       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13390     }
13391 }
13392
13393 static void
13394 do_neon_rshl (void)
13395 {
13396   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13397   struct neon_type_el et = neon_check_type (3, rs,
13398     N_EQK, N_EQK, N_SU_ALL | N_KEY);
13399   unsigned int tmp;
13400
13401   tmp = inst.operands[2].reg;
13402   inst.operands[2].reg = inst.operands[1].reg;
13403   inst.operands[1].reg = tmp;
13404   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13405 }
13406
13407 static int
13408 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
13409 {
13410   /* Handle .I8 pseudo-instructions.  */
13411   if (size == 8)
13412     {
13413       /* Unfortunately, this will make everything apart from zero out-of-range.
13414          FIXME is this the intended semantics? There doesn't seem much point in
13415          accepting .I8 if so.  */
13416       immediate |= immediate << 8;
13417       size = 16;
13418     }
13419
13420   if (size >= 32)
13421     {
13422       if (immediate == (immediate & 0x000000ff))
13423         {
13424           *immbits = immediate;
13425           return 0x1;
13426         }
13427       else if (immediate == (immediate & 0x0000ff00))
13428         {
13429           *immbits = immediate >> 8;
13430           return 0x3;
13431         }
13432       else if (immediate == (immediate & 0x00ff0000))
13433         {
13434           *immbits = immediate >> 16;
13435           return 0x5;
13436         }
13437       else if (immediate == (immediate & 0xff000000))
13438         {
13439           *immbits = immediate >> 24;
13440           return 0x7;
13441         }
13442       if ((immediate & 0xffff) != (immediate >> 16))
13443         goto bad_immediate;
13444       immediate &= 0xffff;
13445     }
13446
13447   if (immediate == (immediate & 0x000000ff))
13448     {
13449       *immbits = immediate;
13450       return 0x9;
13451     }
13452   else if (immediate == (immediate & 0x0000ff00))
13453     {
13454       *immbits = immediate >> 8;
13455       return 0xb;
13456     }
13457
13458   bad_immediate:
13459   first_error (_("immediate value out of range"));
13460   return FAIL;
13461 }
13462
13463 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
13464    A, B, C, D.  */
13465
13466 static int
13467 neon_bits_same_in_bytes (unsigned imm)
13468 {
13469   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
13470          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
13471          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
13472          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
13473 }
13474
13475 /* For immediate of above form, return 0bABCD.  */
13476
13477 static unsigned
13478 neon_squash_bits (unsigned imm)
13479 {
13480   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
13481          | ((imm & 0x01000000) >> 21);
13482 }
13483
13484 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
13485
13486 static unsigned
13487 neon_qfloat_bits (unsigned imm)
13488 {
13489   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
13490 }
13491
13492 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
13493    the instruction. *OP is passed as the initial value of the op field, and
13494    may be set to a different value depending on the constant (i.e.
13495    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
13496    MVN).  If the immediate looks like a repeated pattern then also
13497    try smaller element sizes.  */
13498
13499 static int
13500 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
13501                          unsigned *immbits, int *op, int size,
13502                          enum neon_el_type type)
13503 {
13504   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
13505      float.  */
13506   if (type == NT_float && !float_p)
13507     return FAIL;
13508
13509   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
13510     {
13511       if (size != 32 || *op == 1)
13512         return FAIL;
13513       *immbits = neon_qfloat_bits (immlo);
13514       return 0xf;
13515     }
13516
13517   if (size == 64)
13518     {
13519       if (neon_bits_same_in_bytes (immhi)
13520           && neon_bits_same_in_bytes (immlo))
13521         {
13522           if (*op == 1)
13523             return FAIL;
13524           *immbits = (neon_squash_bits (immhi) << 4)
13525                      | neon_squash_bits (immlo);
13526           *op = 1;
13527           return 0xe;
13528         }
13529
13530       if (immhi != immlo)
13531         return FAIL;
13532     }
13533
13534   if (size >= 32)
13535     {
13536       if (immlo == (immlo & 0x000000ff))
13537         {
13538           *immbits = immlo;
13539           return 0x0;
13540         }
13541       else if (immlo == (immlo & 0x0000ff00))
13542         {
13543           *immbits = immlo >> 8;
13544           return 0x2;
13545         }
13546       else if (immlo == (immlo & 0x00ff0000))
13547         {
13548           *immbits = immlo >> 16;
13549           return 0x4;
13550         }
13551       else if (immlo == (immlo & 0xff000000))
13552         {
13553           *immbits = immlo >> 24;
13554           return 0x6;
13555         }
13556       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
13557         {
13558           *immbits = (immlo >> 8) & 0xff;
13559           return 0xc;
13560         }
13561       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
13562         {
13563           *immbits = (immlo >> 16) & 0xff;
13564           return 0xd;
13565         }
13566
13567       if ((immlo & 0xffff) != (immlo >> 16))
13568         return FAIL;
13569       immlo &= 0xffff;
13570     }
13571
13572   if (size >= 16)
13573     {
13574       if (immlo == (immlo & 0x000000ff))
13575         {
13576           *immbits = immlo;
13577           return 0x8;
13578         }
13579       else if (immlo == (immlo & 0x0000ff00))
13580         {
13581           *immbits = immlo >> 8;
13582           return 0xa;
13583         }
13584
13585       if ((immlo & 0xff) != (immlo >> 8))
13586         return FAIL;
13587       immlo &= 0xff;
13588     }
13589
13590   if (immlo == (immlo & 0x000000ff))
13591     {
13592       /* Don't allow MVN with 8-bit immediate.  */
13593       if (*op == 1)
13594         return FAIL;
13595       *immbits = immlo;
13596       return 0xe;
13597     }
13598
13599   return FAIL;
13600 }
13601
13602 /* Write immediate bits [7:0] to the following locations:
13603
13604   |28/24|23     19|18 16|15                    4|3     0|
13605   |  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|
13606
13607   This function is used by VMOV/VMVN/VORR/VBIC.  */
13608
13609 static void
13610 neon_write_immbits (unsigned immbits)
13611 {
13612   inst.instruction |= immbits & 0xf;
13613   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
13614   inst.instruction |= ((immbits >> 7) & 0x1) << 24;
13615 }
13616
13617 /* Invert low-order SIZE bits of XHI:XLO.  */
13618
13619 static void
13620 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
13621 {
13622   unsigned immlo = xlo ? *xlo : 0;
13623   unsigned immhi = xhi ? *xhi : 0;
13624
13625   switch (size)
13626     {
13627     case 8:
13628       immlo = (~immlo) & 0xff;
13629       break;
13630
13631     case 16:
13632       immlo = (~immlo) & 0xffff;
13633       break;
13634
13635     case 64:
13636       immhi = (~immhi) & 0xffffffff;
13637       /* fall through.  */
13638
13639     case 32:
13640       immlo = (~immlo) & 0xffffffff;
13641       break;
13642
13643     default:
13644       abort ();
13645     }
13646
13647   if (xlo)
13648     *xlo = immlo;
13649
13650   if (xhi)
13651     *xhi = immhi;
13652 }
13653
13654 static void
13655 do_neon_logic (void)
13656 {
13657   if (inst.operands[2].present && inst.operands[2].isreg)
13658     {
13659       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13660       neon_check_type (3, rs, N_IGNORE_TYPE);
13661       /* U bit and size field were set as part of the bitmask.  */
13662       NEON_ENCODE (INTEGER, inst);
13663       neon_three_same (neon_quad (rs), 0, -1);
13664     }
13665   else
13666     {
13667       const int three_ops_form = (inst.operands[2].present
13668                                   && !inst.operands[2].isreg);
13669       const int immoperand = (three_ops_form ? 2 : 1);
13670       enum neon_shape rs = (three_ops_form
13671                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
13672                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
13673       struct neon_type_el et = neon_check_type (2, rs,
13674         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
13675       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
13676       unsigned immbits;
13677       int cmode;
13678
13679       if (et.type == NT_invtype)
13680         return;
13681
13682       if (three_ops_form)
13683         constraint (inst.operands[0].reg != inst.operands[1].reg,
13684                     _("first and second operands shall be the same register"));
13685
13686       NEON_ENCODE (IMMED, inst);
13687
13688       immbits = inst.operands[immoperand].imm;
13689       if (et.size == 64)
13690         {
13691           /* .i64 is a pseudo-op, so the immediate must be a repeating
13692              pattern.  */
13693           if (immbits != (inst.operands[immoperand].regisimm ?
13694                           inst.operands[immoperand].reg : 0))
13695             {
13696               /* Set immbits to an invalid constant.  */
13697               immbits = 0xdeadbeef;
13698             }
13699         }
13700
13701       switch (opcode)
13702         {
13703         case N_MNEM_vbic:
13704           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13705           break;
13706
13707         case N_MNEM_vorr:
13708           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13709           break;
13710
13711         case N_MNEM_vand:
13712           /* Pseudo-instruction for VBIC.  */
13713           neon_invert_size (&immbits, 0, et.size);
13714           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13715           break;
13716
13717         case N_MNEM_vorn:
13718           /* Pseudo-instruction for VORR.  */
13719           neon_invert_size (&immbits, 0, et.size);
13720           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13721           break;
13722
13723         default:
13724           abort ();
13725         }
13726
13727       if (cmode == FAIL)
13728         return;
13729
13730       inst.instruction |= neon_quad (rs) << 6;
13731       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13732       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13733       inst.instruction |= cmode << 8;
13734       neon_write_immbits (immbits);
13735
13736       neon_dp_fixup (&inst);
13737     }
13738 }
13739
13740 static void
13741 do_neon_bitfield (void)
13742 {
13743   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13744   neon_check_type (3, rs, N_IGNORE_TYPE);
13745   neon_three_same (neon_quad (rs), 0, -1);
13746 }
13747
13748 static void
13749 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
13750                   unsigned destbits)
13751 {
13752   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13753   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
13754                                             types | N_KEY);
13755   if (et.type == NT_float)
13756     {
13757       NEON_ENCODE (FLOAT, inst);
13758       neon_three_same (neon_quad (rs), 0, -1);
13759     }
13760   else
13761     {
13762       NEON_ENCODE (INTEGER, inst);
13763       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
13764     }
13765 }
13766
13767 static void
13768 do_neon_dyadic_if_su (void)
13769 {
13770   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
13771 }
13772
13773 static void
13774 do_neon_dyadic_if_su_d (void)
13775 {
13776   /* This version only allow D registers, but that constraint is enforced during
13777      operand parsing so we don't need to do anything extra here.  */
13778   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
13779 }
13780
13781 static void
13782 do_neon_dyadic_if_i_d (void)
13783 {
13784   /* The "untyped" case can't happen. Do this to stop the "U" bit being
13785      affected if we specify unsigned args.  */
13786   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
13787 }
13788
13789 enum vfp_or_neon_is_neon_bits
13790 {
13791   NEON_CHECK_CC = 1,
13792   NEON_CHECK_ARCH = 2
13793 };
13794
13795 /* Call this function if an instruction which may have belonged to the VFP or
13796    Neon instruction sets, but turned out to be a Neon instruction (due to the
13797    operand types involved, etc.). We have to check and/or fix-up a couple of
13798    things:
13799
13800      - Make sure the user hasn't attempted to make a Neon instruction
13801        conditional.
13802      - Alter the value in the condition code field if necessary.
13803      - Make sure that the arch supports Neon instructions.
13804
13805    Which of these operations take place depends on bits from enum
13806    vfp_or_neon_is_neon_bits.
13807
13808    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
13809    current instruction's condition is COND_ALWAYS, the condition field is
13810    changed to inst.uncond_value. This is necessary because instructions shared
13811    between VFP and Neon may be conditional for the VFP variants only, and the
13812    unconditional Neon version must have, e.g., 0xF in the condition field.  */
13813
13814 static int
13815 vfp_or_neon_is_neon (unsigned check)
13816 {
13817   /* Conditions are always legal in Thumb mode (IT blocks).  */
13818   if (!thumb_mode && (check & NEON_CHECK_CC))
13819     {
13820       if (inst.cond != COND_ALWAYS)
13821         {
13822           first_error (_(BAD_COND));
13823           return FAIL;
13824         }
13825       if (inst.uncond_value != -1)
13826         inst.instruction |= inst.uncond_value << 28;
13827     }
13828
13829   if ((check & NEON_CHECK_ARCH)
13830       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
13831     {
13832       first_error (_(BAD_FPU));
13833       return FAIL;
13834     }
13835
13836   return SUCCESS;
13837 }
13838
13839 static void
13840 do_neon_addsub_if_i (void)
13841 {
13842   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
13843     return;
13844
13845   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13846     return;
13847
13848   /* The "untyped" case can't happen. Do this to stop the "U" bit being
13849      affected if we specify unsigned args.  */
13850   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
13851 }
13852
13853 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
13854    result to be:
13855      V<op> A,B     (A is operand 0, B is operand 2)
13856    to mean:
13857      V<op> A,B,A
13858    not:
13859      V<op> A,B,B
13860    so handle that case specially.  */
13861
13862 static void
13863 neon_exchange_operands (void)
13864 {
13865   void *scratch = alloca (sizeof (inst.operands[0]));
13866   if (inst.operands[1].present)
13867     {
13868       /* Swap operands[1] and operands[2].  */
13869       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
13870       inst.operands[1] = inst.operands[2];
13871       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
13872     }
13873   else
13874     {
13875       inst.operands[1] = inst.operands[2];
13876       inst.operands[2] = inst.operands[0];
13877     }
13878 }
13879
13880 static void
13881 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
13882 {
13883   if (inst.operands[2].isreg)
13884     {
13885       if (invert)
13886         neon_exchange_operands ();
13887       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
13888     }
13889   else
13890     {
13891       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13892       struct neon_type_el et = neon_check_type (2, rs,
13893         N_EQK | N_SIZ, immtypes | N_KEY);
13894
13895       NEON_ENCODE (IMMED, inst);
13896       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13897       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13898       inst.instruction |= LOW4 (inst.operands[1].reg);
13899       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13900       inst.instruction |= neon_quad (rs) << 6;
13901       inst.instruction |= (et.type == NT_float) << 10;
13902       inst.instruction |= neon_logbits (et.size) << 18;
13903
13904       neon_dp_fixup (&inst);
13905     }
13906 }
13907
13908 static void
13909 do_neon_cmp (void)
13910 {
13911   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, FALSE);
13912 }
13913
13914 static void
13915 do_neon_cmp_inv (void)
13916 {
13917   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, TRUE);
13918 }
13919
13920 static void
13921 do_neon_ceq (void)
13922 {
13923   neon_compare (N_IF_32, N_IF_32, FALSE);
13924 }
13925
13926 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
13927    scalars, which are encoded in 5 bits, M : Rm.
13928    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
13929    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
13930    index in M.  */
13931
13932 static unsigned
13933 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
13934 {
13935   unsigned regno = NEON_SCALAR_REG (scalar);
13936   unsigned elno = NEON_SCALAR_INDEX (scalar);
13937
13938   switch (elsize)
13939     {
13940     case 16:
13941       if (regno > 7 || elno > 3)
13942         goto bad_scalar;
13943       return regno | (elno << 3);
13944
13945     case 32:
13946       if (regno > 15 || elno > 1)
13947         goto bad_scalar;
13948       return regno | (elno << 4);
13949
13950     default:
13951     bad_scalar:
13952       first_error (_("scalar out of range for multiply instruction"));
13953     }
13954
13955   return 0;
13956 }
13957
13958 /* Encode multiply / multiply-accumulate scalar instructions.  */
13959
13960 static void
13961 neon_mul_mac (struct neon_type_el et, int ubit)
13962 {
13963   unsigned scalar;
13964
13965   /* Give a more helpful error message if we have an invalid type.  */
13966   if (et.type == NT_invtype)
13967     return;
13968
13969   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
13970   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13971   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13972   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
13973   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
13974   inst.instruction |= LOW4 (scalar);
13975   inst.instruction |= HI1 (scalar) << 5;
13976   inst.instruction |= (et.type == NT_float) << 8;
13977   inst.instruction |= neon_logbits (et.size) << 20;
13978   inst.instruction |= (ubit != 0) << 24;
13979
13980   neon_dp_fixup (&inst);
13981 }
13982
13983 static void
13984 do_neon_mac_maybe_scalar (void)
13985 {
13986   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
13987     return;
13988
13989   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13990     return;
13991
13992   if (inst.operands[2].isscalar)
13993     {
13994       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
13995       struct neon_type_el et = neon_check_type (3, rs,
13996         N_EQK, N_EQK, N_I16 | N_I32 | N_F32 | N_KEY);
13997       NEON_ENCODE (SCALAR, inst);
13998       neon_mul_mac (et, neon_quad (rs));
13999     }
14000   else
14001     {
14002       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
14003          affected if we specify unsigned args.  */
14004       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14005     }
14006 }
14007
14008 static void
14009 do_neon_fmac (void)
14010 {
14011   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
14012     return;
14013
14014   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14015     return;
14016
14017   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14018 }
14019
14020 static void
14021 do_neon_tst (void)
14022 {
14023   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14024   struct neon_type_el et = neon_check_type (3, rs,
14025     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
14026   neon_three_same (neon_quad (rs), 0, et.size);
14027 }
14028
14029 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
14030    same types as the MAC equivalents. The polynomial type for this instruction
14031    is encoded the same as the integer type.  */
14032
14033 static void
14034 do_neon_mul (void)
14035 {
14036   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
14037     return;
14038
14039   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14040     return;
14041
14042   if (inst.operands[2].isscalar)
14043     do_neon_mac_maybe_scalar ();
14044   else
14045     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F32 | N_P8, 0);
14046 }
14047
14048 static void
14049 do_neon_qdmulh (void)
14050 {
14051   if (inst.operands[2].isscalar)
14052     {
14053       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
14054       struct neon_type_el et = neon_check_type (3, rs,
14055         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
14056       NEON_ENCODE (SCALAR, inst);
14057       neon_mul_mac (et, neon_quad (rs));
14058     }
14059   else
14060     {
14061       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14062       struct neon_type_el et = neon_check_type (3, rs,
14063         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
14064       NEON_ENCODE (INTEGER, inst);
14065       /* The U bit (rounding) comes from bit mask.  */
14066       neon_three_same (neon_quad (rs), 0, et.size);
14067     }
14068 }
14069
14070 static void
14071 do_neon_fcmp_absolute (void)
14072 {
14073   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14074   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
14075   /* Size field comes from bit mask.  */
14076   neon_three_same (neon_quad (rs), 1, -1);
14077 }
14078
14079 static void
14080 do_neon_fcmp_absolute_inv (void)
14081 {
14082   neon_exchange_operands ();
14083   do_neon_fcmp_absolute ();
14084 }
14085
14086 static void
14087 do_neon_step (void)
14088 {
14089   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14090   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
14091   neon_three_same (neon_quad (rs), 0, -1);
14092 }
14093
14094 static void
14095 do_neon_abs_neg (void)
14096 {
14097   enum neon_shape rs;
14098   struct neon_type_el et;
14099
14100   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
14101     return;
14102
14103   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14104     return;
14105
14106   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14107   et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_F32 | N_KEY);
14108
14109   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14110   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14111   inst.instruction |= LOW4 (inst.operands[1].reg);
14112   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14113   inst.instruction |= neon_quad (rs) << 6;
14114   inst.instruction |= (et.type == NT_float) << 10;
14115   inst.instruction |= neon_logbits (et.size) << 18;
14116
14117   neon_dp_fixup (&inst);
14118 }
14119
14120 static void
14121 do_neon_sli (void)
14122 {
14123   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14124   struct neon_type_el et = neon_check_type (2, rs,
14125     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14126   int imm = inst.operands[2].imm;
14127   constraint (imm < 0 || (unsigned)imm >= et.size,
14128               _("immediate out of range for insert"));
14129   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14130 }
14131
14132 static void
14133 do_neon_sri (void)
14134 {
14135   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14136   struct neon_type_el et = neon_check_type (2, rs,
14137     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14138   int imm = inst.operands[2].imm;
14139   constraint (imm < 1 || (unsigned)imm > et.size,
14140               _("immediate out of range for insert"));
14141   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
14142 }
14143
14144 static void
14145 do_neon_qshlu_imm (void)
14146 {
14147   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14148   struct neon_type_el et = neon_check_type (2, rs,
14149     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
14150   int imm = inst.operands[2].imm;
14151   constraint (imm < 0 || (unsigned)imm >= et.size,
14152               _("immediate out of range for shift"));
14153   /* Only encodes the 'U present' variant of the instruction.
14154      In this case, signed types have OP (bit 8) set to 0.
14155      Unsigned types have OP set to 1.  */
14156   inst.instruction |= (et.type == NT_unsigned) << 8;
14157   /* The rest of the bits are the same as other immediate shifts.  */
14158   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14159 }
14160
14161 static void
14162 do_neon_qmovn (void)
14163 {
14164   struct neon_type_el et = neon_check_type (2, NS_DQ,
14165     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14166   /* Saturating move where operands can be signed or unsigned, and the
14167      destination has the same signedness.  */
14168   NEON_ENCODE (INTEGER, inst);
14169   if (et.type == NT_unsigned)
14170     inst.instruction |= 0xc0;
14171   else
14172     inst.instruction |= 0x80;
14173   neon_two_same (0, 1, et.size / 2);
14174 }
14175
14176 static void
14177 do_neon_qmovun (void)
14178 {
14179   struct neon_type_el et = neon_check_type (2, NS_DQ,
14180     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
14181   /* Saturating move with unsigned results. Operands must be signed.  */
14182   NEON_ENCODE (INTEGER, inst);
14183   neon_two_same (0, 1, et.size / 2);
14184 }
14185
14186 static void
14187 do_neon_rshift_sat_narrow (void)
14188 {
14189   /* FIXME: Types for narrowing. If operands are signed, results can be signed
14190      or unsigned. If operands are unsigned, results must also be unsigned.  */
14191   struct neon_type_el et = neon_check_type (2, NS_DQI,
14192     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14193   int imm = inst.operands[2].imm;
14194   /* This gets the bounds check, size encoding and immediate bits calculation
14195      right.  */
14196   et.size /= 2;
14197
14198   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
14199      VQMOVN.I<size> <Dd>, <Qm>.  */
14200   if (imm == 0)
14201     {
14202       inst.operands[2].present = 0;
14203       inst.instruction = N_MNEM_vqmovn;
14204       do_neon_qmovn ();
14205       return;
14206     }
14207
14208   constraint (imm < 1 || (unsigned)imm > et.size,
14209               _("immediate out of range"));
14210   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
14211 }
14212
14213 static void
14214 do_neon_rshift_sat_narrow_u (void)
14215 {
14216   /* FIXME: Types for narrowing. If operands are signed, results can be signed
14217      or unsigned. If operands are unsigned, results must also be unsigned.  */
14218   struct neon_type_el et = neon_check_type (2, NS_DQI,
14219     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
14220   int imm = inst.operands[2].imm;
14221   /* This gets the bounds check, size encoding and immediate bits calculation
14222      right.  */
14223   et.size /= 2;
14224
14225   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
14226      VQMOVUN.I<size> <Dd>, <Qm>.  */
14227   if (imm == 0)
14228     {
14229       inst.operands[2].present = 0;
14230       inst.instruction = N_MNEM_vqmovun;
14231       do_neon_qmovun ();
14232       return;
14233     }
14234
14235   constraint (imm < 1 || (unsigned)imm > et.size,
14236               _("immediate out of range"));
14237   /* FIXME: The manual is kind of unclear about what value U should have in
14238      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
14239      must be 1.  */
14240   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
14241 }
14242
14243 static void
14244 do_neon_movn (void)
14245 {
14246   struct neon_type_el et = neon_check_type (2, NS_DQ,
14247     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14248   NEON_ENCODE (INTEGER, inst);
14249   neon_two_same (0, 1, et.size / 2);
14250 }
14251
14252 static void
14253 do_neon_rshift_narrow (void)
14254 {
14255   struct neon_type_el et = neon_check_type (2, NS_DQI,
14256     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14257   int imm = inst.operands[2].imm;
14258   /* This gets the bounds check, size encoding and immediate bits calculation
14259      right.  */
14260   et.size /= 2;
14261
14262   /* If immediate is zero then we are a pseudo-instruction for
14263      VMOVN.I<size> <Dd>, <Qm>  */
14264   if (imm == 0)
14265     {
14266       inst.operands[2].present = 0;
14267       inst.instruction = N_MNEM_vmovn;
14268       do_neon_movn ();
14269       return;
14270     }
14271
14272   constraint (imm < 1 || (unsigned)imm > et.size,
14273               _("immediate out of range for narrowing operation"));
14274   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
14275 }
14276
14277 static void
14278 do_neon_shll (void)
14279 {
14280   /* FIXME: Type checking when lengthening.  */
14281   struct neon_type_el et = neon_check_type (2, NS_QDI,
14282     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
14283   unsigned imm = inst.operands[2].imm;
14284
14285   if (imm == et.size)
14286     {
14287       /* Maximum shift variant.  */
14288       NEON_ENCODE (INTEGER, inst);
14289       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14290       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14291       inst.instruction |= LOW4 (inst.operands[1].reg);
14292       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14293       inst.instruction |= neon_logbits (et.size) << 18;
14294
14295       neon_dp_fixup (&inst);
14296     }
14297   else
14298     {
14299       /* A more-specific type check for non-max versions.  */
14300       et = neon_check_type (2, NS_QDI,
14301         N_EQK | N_DBL, N_SU_32 | N_KEY);
14302       NEON_ENCODE (IMMED, inst);
14303       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
14304     }
14305 }
14306
14307 /* Check the various types for the VCVT instruction, and return which version
14308    the current instruction is.  */
14309
14310 static int
14311 neon_cvt_flavour (enum neon_shape rs)
14312 {
14313 #define CVT_VAR(C,X,Y)                                                  \
14314   et = neon_check_type (2, rs, whole_reg | (X), whole_reg | (Y));       \
14315   if (et.type != NT_invtype)                                            \
14316     {                                                                   \
14317       inst.error = NULL;                                                \
14318       return (C);                                                       \
14319     }
14320   struct neon_type_el et;
14321   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
14322                         || rs == NS_FF) ? N_VFP : 0;
14323   /* The instruction versions which take an immediate take one register
14324      argument, which is extended to the width of the full register. Thus the
14325      "source" and "destination" registers must have the same width.  Hack that
14326      here by making the size equal to the key (wider, in this case) operand.  */
14327   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
14328
14329   CVT_VAR (0, N_S32, N_F32);
14330   CVT_VAR (1, N_U32, N_F32);
14331   CVT_VAR (2, N_F32, N_S32);
14332   CVT_VAR (3, N_F32, N_U32);
14333   /* Half-precision conversions.  */
14334   CVT_VAR (4, N_F32, N_F16);
14335   CVT_VAR (5, N_F16, N_F32);
14336
14337   whole_reg = N_VFP;
14338
14339   /* VFP instructions.  */
14340   CVT_VAR (6, N_F32, N_F64);
14341   CVT_VAR (7, N_F64, N_F32);
14342   CVT_VAR (8, N_S32, N_F64 | key);
14343   CVT_VAR (9, N_U32, N_F64 | key);
14344   CVT_VAR (10, N_F64 | key, N_S32);
14345   CVT_VAR (11, N_F64 | key, N_U32);
14346   /* VFP instructions with bitshift.  */
14347   CVT_VAR (12, N_F32 | key, N_S16);
14348   CVT_VAR (13, N_F32 | key, N_U16);
14349   CVT_VAR (14, N_F64 | key, N_S16);
14350   CVT_VAR (15, N_F64 | key, N_U16);
14351   CVT_VAR (16, N_S16, N_F32 | key);
14352   CVT_VAR (17, N_U16, N_F32 | key);
14353   CVT_VAR (18, N_S16, N_F64 | key);
14354   CVT_VAR (19, N_U16, N_F64 | key);
14355
14356   return -1;
14357 #undef CVT_VAR
14358 }
14359
14360 /* Neon-syntax VFP conversions.  */
14361
14362 static void
14363 do_vfp_nsyn_cvt (enum neon_shape rs, int flavour)
14364 {
14365   const char *opname = 0;
14366
14367   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI)
14368     {
14369       /* Conversions with immediate bitshift.  */
14370       const char *enc[] =
14371         {
14372           "ftosls",
14373           "ftouls",
14374           "fsltos",
14375           "fultos",
14376           NULL,
14377           NULL,
14378           NULL,
14379           NULL,
14380           "ftosld",
14381           "ftould",
14382           "fsltod",
14383           "fultod",
14384           "fshtos",
14385           "fuhtos",
14386           "fshtod",
14387           "fuhtod",
14388           "ftoshs",
14389           "ftouhs",
14390           "ftoshd",
14391           "ftouhd"
14392         };
14393
14394       if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc))
14395         {
14396           opname = enc[flavour];
14397           constraint (inst.operands[0].reg != inst.operands[1].reg,
14398                       _("operands 0 and 1 must be the same register"));
14399           inst.operands[1] = inst.operands[2];
14400           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
14401         }
14402     }
14403   else
14404     {
14405       /* Conversions without bitshift.  */
14406       const char *enc[] =
14407         {
14408           "ftosis",
14409           "ftouis",
14410           "fsitos",
14411           "fuitos",
14412           "NULL",
14413           "NULL",
14414           "fcvtsd",
14415           "fcvtds",
14416           "ftosid",
14417           "ftouid",
14418           "fsitod",
14419           "fuitod"
14420         };
14421
14422       if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc))
14423         opname = enc[flavour];
14424     }
14425
14426   if (opname)
14427     do_vfp_nsyn_opcode (opname);
14428 }
14429
14430 static void
14431 do_vfp_nsyn_cvtz (void)
14432 {
14433   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_NULL);
14434   int flavour = neon_cvt_flavour (rs);
14435   const char *enc[] =
14436     {
14437       "ftosizs",
14438       "ftouizs",
14439       NULL,
14440       NULL,
14441       NULL,
14442       NULL,
14443       NULL,
14444       NULL,
14445       "ftosizd",
14446       "ftouizd"
14447     };
14448
14449   if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
14450     do_vfp_nsyn_opcode (enc[flavour]);
14451 }
14452
14453 static void
14454 do_neon_cvt_1 (bfd_boolean round_to_zero ATTRIBUTE_UNUSED)
14455 {
14456   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
14457     NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ, NS_NULL);
14458   int flavour = neon_cvt_flavour (rs);
14459
14460   /* PR11109: Handle round-to-zero for VCVT conversions.  */
14461   if (round_to_zero
14462       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
14463       && (flavour == 0 || flavour == 1 || flavour == 8 || flavour == 9)
14464       && (rs == NS_FD || rs == NS_FF))
14465     {
14466       do_vfp_nsyn_cvtz ();
14467       return;
14468     }
14469
14470   /* VFP rather than Neon conversions.  */
14471   if (flavour >= 6)
14472     {
14473       do_vfp_nsyn_cvt (rs, flavour);
14474       return;
14475     }
14476
14477   switch (rs)
14478     {
14479     case NS_DDI:
14480     case NS_QQI:
14481       {
14482         unsigned immbits;
14483         unsigned enctab[] = { 0x0000100, 0x1000100, 0x0, 0x1000000 };
14484
14485         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14486           return;
14487
14488         /* Fixed-point conversion with #0 immediate is encoded as an
14489            integer conversion.  */
14490         if (inst.operands[2].present && inst.operands[2].imm == 0)
14491           goto int_encode;
14492        immbits = 32 - inst.operands[2].imm;
14493         NEON_ENCODE (IMMED, inst);
14494         if (flavour != -1)
14495           inst.instruction |= enctab[flavour];
14496         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14497         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14498         inst.instruction |= LOW4 (inst.operands[1].reg);
14499         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14500         inst.instruction |= neon_quad (rs) << 6;
14501         inst.instruction |= 1 << 21;
14502         inst.instruction |= immbits << 16;
14503
14504         neon_dp_fixup (&inst);
14505       }
14506       break;
14507
14508     case NS_DD:
14509     case NS_QQ:
14510     int_encode:
14511       {
14512         unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080 };
14513
14514         NEON_ENCODE (INTEGER, inst);
14515
14516         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14517           return;
14518
14519         if (flavour != -1)
14520           inst.instruction |= enctab[flavour];
14521
14522         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14523         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14524         inst.instruction |= LOW4 (inst.operands[1].reg);
14525         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14526         inst.instruction |= neon_quad (rs) << 6;
14527         inst.instruction |= 2 << 18;
14528
14529         neon_dp_fixup (&inst);
14530       }
14531     break;
14532
14533     /* Half-precision conversions for Advanced SIMD -- neon.  */
14534     case NS_QD:
14535     case NS_DQ:
14536
14537       if ((rs == NS_DQ)
14538           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
14539           {
14540             as_bad (_("operand size must match register width"));
14541             break;
14542           }
14543
14544       if ((rs == NS_QD)
14545           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
14546           {
14547             as_bad (_("operand size must match register width"));
14548             break;
14549           }
14550
14551       if (rs == NS_DQ)
14552         inst.instruction = 0x3b60600;
14553       else
14554         inst.instruction = 0x3b60700;
14555
14556       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14557       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14558       inst.instruction |= LOW4 (inst.operands[1].reg);
14559       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14560       neon_dp_fixup (&inst);
14561       break;
14562
14563     default:
14564       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
14565       do_vfp_nsyn_cvt (rs, flavour);
14566     }
14567 }
14568
14569 static void
14570 do_neon_cvtr (void)
14571 {
14572   do_neon_cvt_1 (FALSE);
14573 }
14574
14575 static void
14576 do_neon_cvt (void)
14577 {
14578   do_neon_cvt_1 (TRUE);
14579 }
14580
14581 static void
14582 do_neon_cvtb (void)
14583 {
14584   inst.instruction = 0xeb20a40;
14585
14586   /* The sizes are attached to the mnemonic.  */
14587   if (inst.vectype.el[0].type != NT_invtype
14588       && inst.vectype.el[0].size == 16)
14589     inst.instruction |= 0x00010000;
14590
14591   /* Programmer's syntax: the sizes are attached to the operands.  */
14592   else if (inst.operands[0].vectype.type != NT_invtype
14593            && inst.operands[0].vectype.size == 16)
14594     inst.instruction |= 0x00010000;
14595
14596   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
14597   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
14598   do_vfp_cond_or_thumb ();
14599 }
14600
14601
14602 static void
14603 do_neon_cvtt (void)
14604 {
14605   do_neon_cvtb ();
14606   inst.instruction |= 0x80;
14607 }
14608
14609 static void
14610 neon_move_immediate (void)
14611 {
14612   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
14613   struct neon_type_el et = neon_check_type (2, rs,
14614     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14615   unsigned immlo, immhi = 0, immbits;
14616   int op, cmode, float_p;
14617
14618   constraint (et.type == NT_invtype,
14619               _("operand size must be specified for immediate VMOV"));
14620
14621   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
14622   op = (inst.instruction & (1 << 5)) != 0;
14623
14624   immlo = inst.operands[1].imm;
14625   if (inst.operands[1].regisimm)
14626     immhi = inst.operands[1].reg;
14627
14628   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
14629               _("immediate has bits set outside the operand size"));
14630
14631   float_p = inst.operands[1].immisfloat;
14632
14633   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
14634                                         et.size, et.type)) == FAIL)
14635     {
14636       /* Invert relevant bits only.  */
14637       neon_invert_size (&immlo, &immhi, et.size);
14638       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
14639          with one or the other; those cases are caught by
14640          neon_cmode_for_move_imm.  */
14641       op = !op;
14642       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
14643                                             &op, et.size, et.type)) == FAIL)
14644         {
14645           first_error (_("immediate out of range"));
14646           return;
14647         }
14648     }
14649
14650   inst.instruction &= ~(1 << 5);
14651   inst.instruction |= op << 5;
14652
14653   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14654   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14655   inst.instruction |= neon_quad (rs) << 6;
14656   inst.instruction |= cmode << 8;
14657
14658   neon_write_immbits (immbits);
14659 }
14660
14661 static void
14662 do_neon_mvn (void)
14663 {
14664   if (inst.operands[1].isreg)
14665     {
14666       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14667
14668       NEON_ENCODE (INTEGER, inst);
14669       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14670       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14671       inst.instruction |= LOW4 (inst.operands[1].reg);
14672       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14673       inst.instruction |= neon_quad (rs) << 6;
14674     }
14675   else
14676     {
14677       NEON_ENCODE (IMMED, inst);
14678       neon_move_immediate ();
14679     }
14680
14681   neon_dp_fixup (&inst);
14682 }
14683
14684 /* Encode instructions of form:
14685
14686   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
14687   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
14688
14689 static void
14690 neon_mixed_length (struct neon_type_el et, unsigned size)
14691 {
14692   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14693   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14694   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14695   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14696   inst.instruction |= LOW4 (inst.operands[2].reg);
14697   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14698   inst.instruction |= (et.type == NT_unsigned) << 24;
14699   inst.instruction |= neon_logbits (size) << 20;
14700
14701   neon_dp_fixup (&inst);
14702 }
14703
14704 static void
14705 do_neon_dyadic_long (void)
14706 {
14707   /* FIXME: Type checking for lengthening op.  */
14708   struct neon_type_el et = neon_check_type (3, NS_QDD,
14709     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
14710   neon_mixed_length (et, et.size);
14711 }
14712
14713 static void
14714 do_neon_abal (void)
14715 {
14716   struct neon_type_el et = neon_check_type (3, NS_QDD,
14717     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
14718   neon_mixed_length (et, et.size);
14719 }
14720
14721 static void
14722 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
14723 {
14724   if (inst.operands[2].isscalar)
14725     {
14726       struct neon_type_el et = neon_check_type (3, NS_QDS,
14727         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
14728       NEON_ENCODE (SCALAR, inst);
14729       neon_mul_mac (et, et.type == NT_unsigned);
14730     }
14731   else
14732     {
14733       struct neon_type_el et = neon_check_type (3, NS_QDD,
14734         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
14735       NEON_ENCODE (INTEGER, inst);
14736       neon_mixed_length (et, et.size);
14737     }
14738 }
14739
14740 static void
14741 do_neon_mac_maybe_scalar_long (void)
14742 {
14743   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
14744 }
14745
14746 static void
14747 do_neon_dyadic_wide (void)
14748 {
14749   struct neon_type_el et = neon_check_type (3, NS_QQD,
14750     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
14751   neon_mixed_length (et, et.size);
14752 }
14753
14754 static void
14755 do_neon_dyadic_narrow (void)
14756 {
14757   struct neon_type_el et = neon_check_type (3, NS_QDD,
14758     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
14759   /* Operand sign is unimportant, and the U bit is part of the opcode,
14760      so force the operand type to integer.  */
14761   et.type = NT_integer;
14762   neon_mixed_length (et, et.size / 2);
14763 }
14764
14765 static void
14766 do_neon_mul_sat_scalar_long (void)
14767 {
14768   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
14769 }
14770
14771 static void
14772 do_neon_vmull (void)
14773 {
14774   if (inst.operands[2].isscalar)
14775     do_neon_mac_maybe_scalar_long ();
14776   else
14777     {
14778       struct neon_type_el et = neon_check_type (3, NS_QDD,
14779         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_KEY);
14780       if (et.type == NT_poly)
14781         NEON_ENCODE (POLY, inst);
14782       else
14783         NEON_ENCODE (INTEGER, inst);
14784       /* For polynomial encoding, size field must be 0b00 and the U bit must be
14785          zero. Should be OK as-is.  */
14786       neon_mixed_length (et, et.size);
14787     }
14788 }
14789
14790 static void
14791 do_neon_ext (void)
14792 {
14793   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
14794   struct neon_type_el et = neon_check_type (3, rs,
14795     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14796   unsigned imm = (inst.operands[3].imm * et.size) / 8;
14797
14798   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
14799               _("shift out of range"));
14800   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14801   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14802   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14803   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14804   inst.instruction |= LOW4 (inst.operands[2].reg);
14805   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14806   inst.instruction |= neon_quad (rs) << 6;
14807   inst.instruction |= imm << 8;
14808
14809   neon_dp_fixup (&inst);
14810 }
14811
14812 static void
14813 do_neon_rev (void)
14814 {
14815   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14816   struct neon_type_el et = neon_check_type (2, rs,
14817     N_EQK, N_8 | N_16 | N_32 | N_KEY);
14818   unsigned op = (inst.instruction >> 7) & 3;
14819   /* N (width of reversed regions) is encoded as part of the bitmask. We
14820      extract it here to check the elements to be reversed are smaller.
14821      Otherwise we'd get a reserved instruction.  */
14822   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
14823   gas_assert (elsize != 0);
14824   constraint (et.size >= elsize,
14825               _("elements must be smaller than reversal region"));
14826   neon_two_same (neon_quad (rs), 1, et.size);
14827 }
14828
14829 static void
14830 do_neon_dup (void)
14831 {
14832   if (inst.operands[1].isscalar)
14833     {
14834       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
14835       struct neon_type_el et = neon_check_type (2, rs,
14836         N_EQK, N_8 | N_16 | N_32 | N_KEY);
14837       unsigned sizebits = et.size >> 3;
14838       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
14839       int logsize = neon_logbits (et.size);
14840       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
14841
14842       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
14843         return;
14844
14845       NEON_ENCODE (SCALAR, inst);
14846       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14847       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14848       inst.instruction |= LOW4 (dm);
14849       inst.instruction |= HI1 (dm) << 5;
14850       inst.instruction |= neon_quad (rs) << 6;
14851       inst.instruction |= x << 17;
14852       inst.instruction |= sizebits << 16;
14853
14854       neon_dp_fixup (&inst);
14855     }
14856   else
14857     {
14858       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
14859       struct neon_type_el et = neon_check_type (2, rs,
14860         N_8 | N_16 | N_32 | N_KEY, N_EQK);
14861       /* Duplicate ARM register to lanes of vector.  */
14862       NEON_ENCODE (ARMREG, inst);
14863       switch (et.size)
14864         {
14865         case 8:  inst.instruction |= 0x400000; break;
14866         case 16: inst.instruction |= 0x000020; break;
14867         case 32: inst.instruction |= 0x000000; break;
14868         default: break;
14869         }
14870       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
14871       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
14872       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
14873       inst.instruction |= neon_quad (rs) << 21;
14874       /* The encoding for this instruction is identical for the ARM and Thumb
14875          variants, except for the condition field.  */
14876       do_vfp_cond_or_thumb ();
14877     }
14878 }
14879
14880 /* VMOV has particularly many variations. It can be one of:
14881      0. VMOV<c><q> <Qd>, <Qm>
14882      1. VMOV<c><q> <Dd>, <Dm>
14883    (Register operations, which are VORR with Rm = Rn.)
14884      2. VMOV<c><q>.<dt> <Qd>, #<imm>
14885      3. VMOV<c><q>.<dt> <Dd>, #<imm>
14886    (Immediate loads.)
14887      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
14888    (ARM register to scalar.)
14889      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
14890    (Two ARM registers to vector.)
14891      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
14892    (Scalar to ARM register.)
14893      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
14894    (Vector to two ARM registers.)
14895      8. VMOV.F32 <Sd>, <Sm>
14896      9. VMOV.F64 <Dd>, <Dm>
14897    (VFP register moves.)
14898     10. VMOV.F32 <Sd>, #imm
14899     11. VMOV.F64 <Dd>, #imm
14900    (VFP float immediate load.)
14901     12. VMOV <Rd>, <Sm>
14902    (VFP single to ARM reg.)
14903     13. VMOV <Sd>, <Rm>
14904    (ARM reg to VFP single.)
14905     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
14906    (Two ARM regs to two VFP singles.)
14907     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
14908    (Two VFP singles to two ARM regs.)
14909
14910    These cases can be disambiguated using neon_select_shape, except cases 1/9
14911    and 3/11 which depend on the operand type too.
14912
14913    All the encoded bits are hardcoded by this function.
14914
14915    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
14916    Cases 5, 7 may be used with VFPv2 and above.
14917
14918    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
14919    can specify a type where it doesn't make sense to, and is ignored).  */
14920
14921 static void
14922 do_neon_mov (void)
14923 {
14924   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
14925     NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR, NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
14926     NS_NULL);
14927   struct neon_type_el et;
14928   const char *ldconst = 0;
14929
14930   switch (rs)
14931     {
14932     case NS_DD:  /* case 1/9.  */
14933       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
14934       /* It is not an error here if no type is given.  */
14935       inst.error = NULL;
14936       if (et.type == NT_float && et.size == 64)
14937         {
14938           do_vfp_nsyn_opcode ("fcpyd");
14939           break;
14940         }
14941       /* fall through.  */
14942
14943     case NS_QQ:  /* case 0/1.  */
14944       {
14945         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14946           return;
14947         /* The architecture manual I have doesn't explicitly state which
14948            value the U bit should have for register->register moves, but
14949            the equivalent VORR instruction has U = 0, so do that.  */
14950         inst.instruction = 0x0200110;
14951         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14952         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14953         inst.instruction |= LOW4 (inst.operands[1].reg);
14954         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14955         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14956         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14957         inst.instruction |= neon_quad (rs) << 6;
14958
14959         neon_dp_fixup (&inst);
14960       }
14961       break;
14962
14963     case NS_DI:  /* case 3/11.  */
14964       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
14965       inst.error = NULL;
14966       if (et.type == NT_float && et.size == 64)
14967         {
14968           /* case 11 (fconstd).  */
14969           ldconst = "fconstd";
14970           goto encode_fconstd;
14971         }
14972       /* fall through.  */
14973
14974     case NS_QI:  /* case 2/3.  */
14975       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14976         return;
14977       inst.instruction = 0x0800010;
14978       neon_move_immediate ();
14979       neon_dp_fixup (&inst);
14980       break;
14981
14982     case NS_SR:  /* case 4.  */
14983       {
14984         unsigned bcdebits = 0;
14985         int logsize;
14986         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
14987         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
14988
14989         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
14990         logsize = neon_logbits (et.size);
14991
14992         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
14993                     _(BAD_FPU));
14994         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
14995                     && et.size != 32, _(BAD_FPU));
14996         constraint (et.type == NT_invtype, _("bad type for scalar"));
14997         constraint (x >= 64 / et.size, _("scalar index out of range"));
14998
14999         switch (et.size)
15000           {
15001           case 8:  bcdebits = 0x8; break;
15002           case 16: bcdebits = 0x1; break;
15003           case 32: bcdebits = 0x0; break;
15004           default: ;
15005           }
15006
15007         bcdebits |= x << logsize;
15008
15009         inst.instruction = 0xe000b10;
15010         do_vfp_cond_or_thumb ();
15011         inst.instruction |= LOW4 (dn) << 16;
15012         inst.instruction |= HI1 (dn) << 7;
15013         inst.instruction |= inst.operands[1].reg << 12;
15014         inst.instruction |= (bcdebits & 3) << 5;
15015         inst.instruction |= (bcdebits >> 2) << 21;
15016       }
15017       break;
15018
15019     case NS_DRR:  /* case 5 (fmdrr).  */
15020       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
15021                   _(BAD_FPU));
15022
15023       inst.instruction = 0xc400b10;
15024       do_vfp_cond_or_thumb ();
15025       inst.instruction |= LOW4 (inst.operands[0].reg);
15026       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
15027       inst.instruction |= inst.operands[1].reg << 12;
15028       inst.instruction |= inst.operands[2].reg << 16;
15029       break;
15030
15031     case NS_RS:  /* case 6.  */
15032       {
15033         unsigned logsize;
15034         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
15035         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
15036         unsigned abcdebits = 0;
15037
15038         et = neon_check_type (2, NS_NULL,
15039                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
15040         logsize = neon_logbits (et.size);
15041
15042         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
15043                     _(BAD_FPU));
15044         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
15045                     && et.size != 32, _(BAD_FPU));
15046         constraint (et.type == NT_invtype, _("bad type for scalar"));
15047         constraint (x >= 64 / et.size, _("scalar index out of range"));
15048
15049         switch (et.size)
15050           {
15051           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
15052           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
15053           case 32: abcdebits = 0x00; break;
15054           default: ;
15055           }
15056
15057         abcdebits |= x << logsize;
15058         inst.instruction = 0xe100b10;
15059         do_vfp_cond_or_thumb ();
15060         inst.instruction |= LOW4 (dn) << 16;
15061         inst.instruction |= HI1 (dn) << 7;
15062         inst.instruction |= inst.operands[0].reg << 12;
15063         inst.instruction |= (abcdebits & 3) << 5;
15064         inst.instruction |= (abcdebits >> 2) << 21;
15065       }
15066       break;
15067
15068     case NS_RRD:  /* case 7 (fmrrd).  */
15069       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
15070                   _(BAD_FPU));
15071
15072       inst.instruction = 0xc500b10;
15073       do_vfp_cond_or_thumb ();
15074       inst.instruction |= inst.operands[0].reg << 12;
15075       inst.instruction |= inst.operands[1].reg << 16;
15076       inst.instruction |= LOW4 (inst.operands[2].reg);
15077       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15078       break;
15079
15080     case NS_FF:  /* case 8 (fcpys).  */
15081       do_vfp_nsyn_opcode ("fcpys");
15082       break;
15083
15084     case NS_FI:  /* case 10 (fconsts).  */
15085       ldconst = "fconsts";
15086       encode_fconstd:
15087       if (is_quarter_float (inst.operands[1].imm))
15088         {
15089           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
15090           do_vfp_nsyn_opcode (ldconst);
15091         }
15092       else
15093         first_error (_("immediate out of range"));
15094       break;
15095
15096     case NS_RF:  /* case 12 (fmrs).  */
15097       do_vfp_nsyn_opcode ("fmrs");
15098       break;
15099
15100     case NS_FR:  /* case 13 (fmsr).  */
15101       do_vfp_nsyn_opcode ("fmsr");
15102       break;
15103
15104     /* The encoders for the fmrrs and fmsrr instructions expect three operands
15105        (one of which is a list), but we have parsed four.  Do some fiddling to
15106        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
15107        expect.  */
15108     case NS_RRFF:  /* case 14 (fmrrs).  */
15109       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
15110                   _("VFP registers must be adjacent"));
15111       inst.operands[2].imm = 2;
15112       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
15113       do_vfp_nsyn_opcode ("fmrrs");
15114       break;
15115
15116     case NS_FFRR:  /* case 15 (fmsrr).  */
15117       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
15118                   _("VFP registers must be adjacent"));
15119       inst.operands[1] = inst.operands[2];
15120       inst.operands[2] = inst.operands[3];
15121       inst.operands[0].imm = 2;
15122       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
15123       do_vfp_nsyn_opcode ("fmsrr");
15124       break;
15125
15126     default:
15127       abort ();
15128     }
15129 }
15130
15131 static void
15132 do_neon_rshift_round_imm (void)
15133 {
15134   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15135   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
15136   int imm = inst.operands[2].imm;
15137
15138   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
15139   if (imm == 0)
15140     {
15141       inst.operands[2].present = 0;
15142       do_neon_mov ();
15143       return;
15144     }
15145
15146   constraint (imm < 1 || (unsigned)imm > et.size,
15147               _("immediate out of range for shift"));
15148   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
15149                   et.size - imm);
15150 }
15151
15152 static void
15153 do_neon_movl (void)
15154 {
15155   struct neon_type_el et = neon_check_type (2, NS_QD,
15156     N_EQK | N_DBL, N_SU_32 | N_KEY);
15157   unsigned sizebits = et.size >> 3;
15158   inst.instruction |= sizebits << 19;
15159   neon_two_same (0, et.type == NT_unsigned, -1);
15160 }
15161
15162 static void
15163 do_neon_trn (void)
15164 {
15165   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15166   struct neon_type_el et = neon_check_type (2, rs,
15167     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15168   NEON_ENCODE (INTEGER, inst);
15169   neon_two_same (neon_quad (rs), 1, et.size);
15170 }
15171
15172 static void
15173 do_neon_zip_uzp (void)
15174 {
15175   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15176   struct neon_type_el et = neon_check_type (2, rs,
15177     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15178   if (rs == NS_DD && et.size == 32)
15179     {
15180       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
15181       inst.instruction = N_MNEM_vtrn;
15182       do_neon_trn ();
15183       return;
15184     }
15185   neon_two_same (neon_quad (rs), 1, et.size);
15186 }
15187
15188 static void
15189 do_neon_sat_abs_neg (void)
15190 {
15191   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15192   struct neon_type_el et = neon_check_type (2, rs,
15193     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
15194   neon_two_same (neon_quad (rs), 1, et.size);
15195 }
15196
15197 static void
15198 do_neon_pair_long (void)
15199 {
15200   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15201   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
15202   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
15203   inst.instruction |= (et.type == NT_unsigned) << 7;
15204   neon_two_same (neon_quad (rs), 1, et.size);
15205 }
15206
15207 static void
15208 do_neon_recip_est (void)
15209 {
15210   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15211   struct neon_type_el et = neon_check_type (2, rs,
15212     N_EQK | N_FLT, N_F32 | N_U32 | N_KEY);
15213   inst.instruction |= (et.type == NT_float) << 8;
15214   neon_two_same (neon_quad (rs), 1, et.size);
15215 }
15216
15217 static void
15218 do_neon_cls (void)
15219 {
15220   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15221   struct neon_type_el et = neon_check_type (2, rs,
15222     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
15223   neon_two_same (neon_quad (rs), 1, et.size);
15224 }
15225
15226 static void
15227 do_neon_clz (void)
15228 {
15229   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15230   struct neon_type_el et = neon_check_type (2, rs,
15231     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
15232   neon_two_same (neon_quad (rs), 1, et.size);
15233 }
15234
15235 static void
15236 do_neon_cnt (void)
15237 {
15238   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15239   struct neon_type_el et = neon_check_type (2, rs,
15240     N_EQK | N_INT, N_8 | N_KEY);
15241   neon_two_same (neon_quad (rs), 1, et.size);
15242 }
15243
15244 static void
15245 do_neon_swp (void)
15246 {
15247   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15248   neon_two_same (neon_quad (rs), 1, -1);
15249 }
15250
15251 static void
15252 do_neon_tbl_tbx (void)
15253 {
15254   unsigned listlenbits;
15255   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
15256
15257   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
15258     {
15259       first_error (_("bad list length for table lookup"));
15260       return;
15261     }
15262
15263   listlenbits = inst.operands[1].imm - 1;
15264   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15265   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15266   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15267   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15268   inst.instruction |= LOW4 (inst.operands[2].reg);
15269   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15270   inst.instruction |= listlenbits << 8;
15271
15272   neon_dp_fixup (&inst);
15273 }
15274
15275 static void
15276 do_neon_ldm_stm (void)
15277 {
15278   /* P, U and L bits are part of bitmask.  */
15279   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
15280   unsigned offsetbits = inst.operands[1].imm * 2;
15281
15282   if (inst.operands[1].issingle)
15283     {
15284       do_vfp_nsyn_ldm_stm (is_dbmode);
15285       return;
15286     }
15287
15288   constraint (is_dbmode && !inst.operands[0].writeback,
15289               _("writeback (!) must be used for VLDMDB and VSTMDB"));
15290
15291   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
15292               _("register list must contain at least 1 and at most 16 "
15293                 "registers"));
15294
15295   inst.instruction |= inst.operands[0].reg << 16;
15296   inst.instruction |= inst.operands[0].writeback << 21;
15297   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
15298   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
15299
15300   inst.instruction |= offsetbits;
15301
15302   do_vfp_cond_or_thumb ();
15303 }
15304
15305 static void
15306 do_neon_ldr_str (void)
15307 {
15308   int is_ldr = (inst.instruction & (1 << 20)) != 0;
15309
15310   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
15311      And is UNPREDICTABLE in thumb mode.  */
15312   if (!is_ldr
15313       && inst.operands[1].reg == REG_PC
15314       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
15315     {
15316       if (!thumb_mode && warn_on_deprecated)
15317         as_warn (_("Use of PC here is deprecated"));
15318       else
15319         inst.error = _("Use of PC here is UNPREDICTABLE");
15320     }
15321
15322   if (inst.operands[0].issingle)
15323     {
15324       if (is_ldr)
15325         do_vfp_nsyn_opcode ("flds");
15326       else
15327         do_vfp_nsyn_opcode ("fsts");
15328     }
15329   else
15330     {
15331       if (is_ldr)
15332         do_vfp_nsyn_opcode ("fldd");
15333       else
15334         do_vfp_nsyn_opcode ("fstd");
15335     }
15336 }
15337
15338 /* "interleave" version also handles non-interleaving register VLD1/VST1
15339    instructions.  */
15340
15341 static void
15342 do_neon_ld_st_interleave (void)
15343 {
15344   struct neon_type_el et = neon_check_type (1, NS_NULL,
15345                                             N_8 | N_16 | N_32 | N_64);
15346   unsigned alignbits = 0;
15347   unsigned idx;
15348   /* The bits in this table go:
15349      0: register stride of one (0) or two (1)
15350      1,2: register list length, minus one (1, 2, 3, 4).
15351      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
15352      We use -1 for invalid entries.  */
15353   const int typetable[] =
15354     {
15355       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
15356        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
15357        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
15358        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
15359     };
15360   int typebits;
15361
15362   if (et.type == NT_invtype)
15363     return;
15364
15365   if (inst.operands[1].immisalign)
15366     switch (inst.operands[1].imm >> 8)
15367       {
15368       case 64: alignbits = 1; break;
15369       case 128:
15370         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
15371             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
15372           goto bad_alignment;
15373         alignbits = 2;
15374         break;
15375       case 256:
15376         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
15377           goto bad_alignment;
15378         alignbits = 3;
15379         break;
15380       default:
15381       bad_alignment:
15382         first_error (_("bad alignment"));
15383         return;
15384       }
15385
15386   inst.instruction |= alignbits << 4;
15387   inst.instruction |= neon_logbits (et.size) << 6;
15388
15389   /* Bits [4:6] of the immediate in a list specifier encode register stride
15390      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
15391      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
15392      up the right value for "type" in a table based on this value and the given
15393      list style, then stick it back.  */
15394   idx = ((inst.operands[0].imm >> 4) & 7)
15395         | (((inst.instruction >> 8) & 3) << 3);
15396
15397   typebits = typetable[idx];
15398
15399   constraint (typebits == -1, _("bad list type for instruction"));
15400
15401   inst.instruction &= ~0xf00;
15402   inst.instruction |= typebits << 8;
15403 }
15404
15405 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
15406    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
15407    otherwise. The variable arguments are a list of pairs of legal (size, align)
15408    values, terminated with -1.  */
15409
15410 static int
15411 neon_alignment_bit (int size, int align, int *do_align, ...)
15412 {
15413   va_list ap;
15414   int result = FAIL, thissize, thisalign;
15415
15416   if (!inst.operands[1].immisalign)
15417     {
15418       *do_align = 0;
15419       return SUCCESS;
15420     }
15421
15422   va_start (ap, do_align);
15423
15424   do
15425     {
15426       thissize = va_arg (ap, int);
15427       if (thissize == -1)
15428         break;
15429       thisalign = va_arg (ap, int);
15430
15431       if (size == thissize && align == thisalign)
15432         result = SUCCESS;
15433     }
15434   while (result != SUCCESS);
15435
15436   va_end (ap);
15437
15438   if (result == SUCCESS)
15439     *do_align = 1;
15440   else
15441     first_error (_("unsupported alignment for instruction"));
15442
15443   return result;
15444 }
15445
15446 static void
15447 do_neon_ld_st_lane (void)
15448 {
15449   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
15450   int align_good, do_align = 0;
15451   int logsize = neon_logbits (et.size);
15452   int align = inst.operands[1].imm >> 8;
15453   int n = (inst.instruction >> 8) & 3;
15454   int max_el = 64 / et.size;
15455
15456   if (et.type == NT_invtype)
15457     return;
15458
15459   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
15460               _("bad list length"));
15461   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
15462               _("scalar index out of range"));
15463   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
15464               && et.size == 8,
15465               _("stride of 2 unavailable when element size is 8"));
15466
15467   switch (n)
15468     {
15469     case 0:  /* VLD1 / VST1.  */
15470       align_good = neon_alignment_bit (et.size, align, &do_align, 16, 16,
15471                                        32, 32, -1);
15472       if (align_good == FAIL)
15473         return;
15474       if (do_align)
15475         {
15476           unsigned alignbits = 0;
15477           switch (et.size)
15478             {
15479             case 16: alignbits = 0x1; break;
15480             case 32: alignbits = 0x3; break;
15481             default: ;
15482             }
15483           inst.instruction |= alignbits << 4;
15484         }
15485       break;
15486
15487     case 1:  /* VLD2 / VST2.  */
15488       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 16, 16, 32,
15489                                        32, 64, -1);
15490       if (align_good == FAIL)
15491         return;
15492       if (do_align)
15493         inst.instruction |= 1 << 4;
15494       break;
15495
15496     case 2:  /* VLD3 / VST3.  */
15497       constraint (inst.operands[1].immisalign,
15498                   _("can't use alignment with this instruction"));
15499       break;
15500
15501     case 3:  /* VLD4 / VST4.  */
15502       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
15503                                        16, 64, 32, 64, 32, 128, -1);
15504       if (align_good == FAIL)
15505         return;
15506       if (do_align)
15507         {
15508           unsigned alignbits = 0;
15509           switch (et.size)
15510             {
15511             case 8:  alignbits = 0x1; break;
15512             case 16: alignbits = 0x1; break;
15513             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
15514             default: ;
15515             }
15516           inst.instruction |= alignbits << 4;
15517         }
15518       break;
15519
15520     default: ;
15521     }
15522
15523   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
15524   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15525     inst.instruction |= 1 << (4 + logsize);
15526
15527   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
15528   inst.instruction |= logsize << 10;
15529 }
15530
15531 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
15532
15533 static void
15534 do_neon_ld_dup (void)
15535 {
15536   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
15537   int align_good, do_align = 0;
15538
15539   if (et.type == NT_invtype)
15540     return;
15541
15542   switch ((inst.instruction >> 8) & 3)
15543     {
15544     case 0:  /* VLD1.  */
15545       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
15546       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
15547                                        &do_align, 16, 16, 32, 32, -1);
15548       if (align_good == FAIL)
15549         return;
15550       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
15551         {
15552         case 1: break;
15553         case 2: inst.instruction |= 1 << 5; break;
15554         default: first_error (_("bad list length")); return;
15555         }
15556       inst.instruction |= neon_logbits (et.size) << 6;
15557       break;
15558
15559     case 1:  /* VLD2.  */
15560       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
15561                                        &do_align, 8, 16, 16, 32, 32, 64, -1);
15562       if (align_good == FAIL)
15563         return;
15564       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
15565                   _("bad list length"));
15566       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15567         inst.instruction |= 1 << 5;
15568       inst.instruction |= neon_logbits (et.size) << 6;
15569       break;
15570
15571     case 2:  /* VLD3.  */
15572       constraint (inst.operands[1].immisalign,
15573                   _("can't use alignment with this instruction"));
15574       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
15575                   _("bad list length"));
15576       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15577         inst.instruction |= 1 << 5;
15578       inst.instruction |= neon_logbits (et.size) << 6;
15579       break;
15580
15581     case 3:  /* VLD4.  */
15582       {
15583         int align = inst.operands[1].imm >> 8;
15584         align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
15585                                          16, 64, 32, 64, 32, 128, -1);
15586         if (align_good == FAIL)
15587           return;
15588         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
15589                     _("bad list length"));
15590         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15591           inst.instruction |= 1 << 5;
15592         if (et.size == 32 && align == 128)
15593           inst.instruction |= 0x3 << 6;
15594         else
15595           inst.instruction |= neon_logbits (et.size) << 6;
15596       }
15597       break;
15598
15599     default: ;
15600     }
15601
15602   inst.instruction |= do_align << 4;
15603 }
15604
15605 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
15606    apart from bits [11:4].  */
15607
15608 static void
15609 do_neon_ldx_stx (void)
15610 {
15611   if (inst.operands[1].isreg)
15612     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
15613
15614   switch (NEON_LANE (inst.operands[0].imm))
15615     {
15616     case NEON_INTERLEAVE_LANES:
15617       NEON_ENCODE (INTERLV, inst);
15618       do_neon_ld_st_interleave ();
15619       break;
15620
15621     case NEON_ALL_LANES:
15622       NEON_ENCODE (DUP, inst);
15623       do_neon_ld_dup ();
15624       break;
15625
15626     default:
15627       NEON_ENCODE (LANE, inst);
15628       do_neon_ld_st_lane ();
15629     }
15630
15631   /* L bit comes from bit mask.  */
15632   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15633   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15634   inst.instruction |= inst.operands[1].reg << 16;
15635
15636   if (inst.operands[1].postind)
15637     {
15638       int postreg = inst.operands[1].imm & 0xf;
15639       constraint (!inst.operands[1].immisreg,
15640                   _("post-index must be a register"));
15641       constraint (postreg == 0xd || postreg == 0xf,
15642                   _("bad register for post-index"));
15643       inst.instruction |= postreg;
15644     }
15645   else if (inst.operands[1].writeback)
15646     {
15647       inst.instruction |= 0xd;
15648     }
15649   else
15650     inst.instruction |= 0xf;
15651
15652   if (thumb_mode)
15653     inst.instruction |= 0xf9000000;
15654   else
15655     inst.instruction |= 0xf4000000;
15656 }
15657 \f
15658 /* Overall per-instruction processing.  */
15659
15660 /* We need to be able to fix up arbitrary expressions in some statements.
15661    This is so that we can handle symbols that are an arbitrary distance from
15662    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
15663    which returns part of an address in a form which will be valid for
15664    a data instruction.  We do this by pushing the expression into a symbol
15665    in the expr_section, and creating a fix for that.  */
15666
15667 static void
15668 fix_new_arm (fragS *       frag,
15669              int           where,
15670              short int     size,
15671              expressionS * exp,
15672              int           pc_rel,
15673              int           reloc)
15674 {
15675   fixS *           new_fix;
15676
15677   switch (exp->X_op)
15678     {
15679     case O_constant:
15680       if (pc_rel)
15681         {
15682           /* Create an absolute valued symbol, so we have something to
15683              refer to in the object file.  Unfortunately for us, gas's
15684              generic expression parsing will already have folded out
15685              any use of .set foo/.type foo %function that may have
15686              been used to set type information of the target location,
15687              that's being specified symbolically.  We have to presume
15688              the user knows what they are doing.  */
15689           char name[16 + 8];
15690           symbolS *symbol;
15691
15692           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
15693
15694           symbol = symbol_find_or_make (name);
15695           S_SET_SEGMENT (symbol, absolute_section);
15696           symbol_set_frag (symbol, &zero_address_frag);
15697           S_SET_VALUE (symbol, exp->X_add_number);
15698           exp->X_op = O_symbol;
15699           exp->X_add_symbol = symbol;
15700           exp->X_add_number = 0;
15701         }
15702       /* FALLTHROUGH */
15703     case O_symbol:
15704     case O_add:
15705     case O_subtract:
15706       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
15707                              (enum bfd_reloc_code_real) reloc);
15708       break;
15709
15710     default:
15711       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
15712                                   pc_rel, (enum bfd_reloc_code_real) reloc);
15713       break;
15714     }
15715
15716   /* Mark whether the fix is to a THUMB instruction, or an ARM
15717      instruction.  */
15718   new_fix->tc_fix_data = thumb_mode;
15719 }
15720
15721 /* Create a frg for an instruction requiring relaxation.  */
15722 static void
15723 output_relax_insn (void)
15724 {
15725   char * to;
15726   symbolS *sym;
15727   int offset;
15728
15729   /* The size of the instruction is unknown, so tie the debug info to the
15730      start of the instruction.  */
15731   dwarf2_emit_insn (0);
15732
15733   switch (inst.reloc.exp.X_op)
15734     {
15735     case O_symbol:
15736       sym = inst.reloc.exp.X_add_symbol;
15737       offset = inst.reloc.exp.X_add_number;
15738       break;
15739     case O_constant:
15740       sym = NULL;
15741       offset = inst.reloc.exp.X_add_number;
15742       break;
15743     default:
15744       sym = make_expr_symbol (&inst.reloc.exp);
15745       offset = 0;
15746       break;
15747   }
15748   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
15749                  inst.relax, sym, offset, NULL/*offset, opcode*/);
15750   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
15751 }
15752
15753 /* Write a 32-bit thumb instruction to buf.  */
15754 static void
15755 put_thumb32_insn (char * buf, unsigned long insn)
15756 {
15757   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
15758   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
15759 }
15760
15761 static void
15762 output_inst (const char * str)
15763 {
15764   char * to = NULL;
15765
15766   if (inst.error)
15767     {
15768       as_bad ("%s -- `%s'", inst.error, str);
15769       return;
15770     }
15771   if (inst.relax)
15772     {
15773       output_relax_insn ();
15774       return;
15775     }
15776   if (inst.size == 0)
15777     return;
15778
15779   to = frag_more (inst.size);
15780   /* PR 9814: Record the thumb mode into the current frag so that we know
15781      what type of NOP padding to use, if necessary.  We override any previous
15782      setting so that if the mode has changed then the NOPS that we use will
15783      match the encoding of the last instruction in the frag.  */
15784   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
15785
15786   if (thumb_mode && (inst.size > THUMB_SIZE))
15787     {
15788       gas_assert (inst.size == (2 * THUMB_SIZE));
15789       put_thumb32_insn (to, inst.instruction);
15790     }
15791   else if (inst.size > INSN_SIZE)
15792     {
15793       gas_assert (inst.size == (2 * INSN_SIZE));
15794       md_number_to_chars (to, inst.instruction, INSN_SIZE);
15795       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
15796     }
15797   else
15798     md_number_to_chars (to, inst.instruction, inst.size);
15799
15800   if (inst.reloc.type != BFD_RELOC_UNUSED)
15801     fix_new_arm (frag_now, to - frag_now->fr_literal,
15802                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
15803                  inst.reloc.type);
15804
15805   dwarf2_emit_insn (inst.size);
15806 }
15807
15808 static char *
15809 output_it_inst (int cond, int mask, char * to)
15810 {
15811   unsigned long instruction = 0xbf00;
15812
15813   mask &= 0xf;
15814   instruction |= mask;
15815   instruction |= cond << 4;
15816
15817   if (to == NULL)
15818     {
15819       to = frag_more (2);
15820 #ifdef OBJ_ELF
15821       dwarf2_emit_insn (2);
15822 #endif
15823     }
15824
15825   md_number_to_chars (to, instruction, 2);
15826
15827   return to;
15828 }
15829
15830 /* Tag values used in struct asm_opcode's tag field.  */
15831 enum opcode_tag
15832 {
15833   OT_unconditional,     /* Instruction cannot be conditionalized.
15834                            The ARM condition field is still 0xE.  */
15835   OT_unconditionalF,    /* Instruction cannot be conditionalized
15836                            and carries 0xF in its ARM condition field.  */
15837   OT_csuffix,           /* Instruction takes a conditional suffix.  */
15838   OT_csuffixF,          /* Some forms of the instruction take a conditional
15839                            suffix, others place 0xF where the condition field
15840                            would be.  */
15841   OT_cinfix3,           /* Instruction takes a conditional infix,
15842                            beginning at character index 3.  (In
15843                            unified mode, it becomes a suffix.)  */
15844   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
15845                             tsts, cmps, cmns, and teqs. */
15846   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
15847                            character index 3, even in unified mode.  Used for
15848                            legacy instructions where suffix and infix forms
15849                            may be ambiguous.  */
15850   OT_csuf_or_in3,       /* Instruction takes either a conditional
15851                            suffix or an infix at character index 3.  */
15852   OT_odd_infix_unc,     /* This is the unconditional variant of an
15853                            instruction that takes a conditional infix
15854                            at an unusual position.  In unified mode,
15855                            this variant will accept a suffix.  */
15856   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
15857                            are the conditional variants of instructions that
15858                            take conditional infixes in unusual positions.
15859                            The infix appears at character index
15860                            (tag - OT_odd_infix_0).  These are not accepted
15861                            in unified mode.  */
15862 };
15863
15864 /* Subroutine of md_assemble, responsible for looking up the primary
15865    opcode from the mnemonic the user wrote.  STR points to the
15866    beginning of the mnemonic.
15867
15868    This is not simply a hash table lookup, because of conditional
15869    variants.  Most instructions have conditional variants, which are
15870    expressed with a _conditional affix_ to the mnemonic.  If we were
15871    to encode each conditional variant as a literal string in the opcode
15872    table, it would have approximately 20,000 entries.
15873
15874    Most mnemonics take this affix as a suffix, and in unified syntax,
15875    'most' is upgraded to 'all'.  However, in the divided syntax, some
15876    instructions take the affix as an infix, notably the s-variants of
15877    the arithmetic instructions.  Of those instructions, all but six
15878    have the infix appear after the third character of the mnemonic.
15879
15880    Accordingly, the algorithm for looking up primary opcodes given
15881    an identifier is:
15882
15883    1. Look up the identifier in the opcode table.
15884       If we find a match, go to step U.
15885
15886    2. Look up the last two characters of the identifier in the
15887       conditions table.  If we find a match, look up the first N-2
15888       characters of the identifier in the opcode table.  If we
15889       find a match, go to step CE.
15890
15891    3. Look up the fourth and fifth characters of the identifier in
15892       the conditions table.  If we find a match, extract those
15893       characters from the identifier, and look up the remaining
15894       characters in the opcode table.  If we find a match, go
15895       to step CM.
15896
15897    4. Fail.
15898
15899    U. Examine the tag field of the opcode structure, in case this is
15900       one of the six instructions with its conditional infix in an
15901       unusual place.  If it is, the tag tells us where to find the
15902       infix; look it up in the conditions table and set inst.cond
15903       accordingly.  Otherwise, this is an unconditional instruction.
15904       Again set inst.cond accordingly.  Return the opcode structure.
15905
15906   CE. Examine the tag field to make sure this is an instruction that
15907       should receive a conditional suffix.  If it is not, fail.
15908       Otherwise, set inst.cond from the suffix we already looked up,
15909       and return the opcode structure.
15910
15911   CM. Examine the tag field to make sure this is an instruction that
15912       should receive a conditional infix after the third character.
15913       If it is not, fail.  Otherwise, undo the edits to the current
15914       line of input and proceed as for case CE.  */
15915
15916 static const struct asm_opcode *
15917 opcode_lookup (char **str)
15918 {
15919   char *end, *base;
15920   char *affix;
15921   const struct asm_opcode *opcode;
15922   const struct asm_cond *cond;
15923   char save[2];
15924
15925   /* Scan up to the end of the mnemonic, which must end in white space,
15926      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
15927   for (base = end = *str; *end != '\0'; end++)
15928     if (*end == ' ' || *end == '.')
15929       break;
15930
15931   if (end == base)
15932     return NULL;
15933
15934   /* Handle a possible width suffix and/or Neon type suffix.  */
15935   if (end[0] == '.')
15936     {
15937       int offset = 2;
15938
15939       /* The .w and .n suffixes are only valid if the unified syntax is in
15940          use.  */
15941       if (unified_syntax && end[1] == 'w')
15942         inst.size_req = 4;
15943       else if (unified_syntax && end[1] == 'n')
15944         inst.size_req = 2;
15945       else
15946         offset = 0;
15947
15948       inst.vectype.elems = 0;
15949
15950       *str = end + offset;
15951
15952       if (end[offset] == '.')
15953         {
15954           /* See if we have a Neon type suffix (possible in either unified or
15955              non-unified ARM syntax mode).  */
15956           if (parse_neon_type (&inst.vectype, str) == FAIL)
15957             return NULL;
15958         }
15959       else if (end[offset] != '\0' && end[offset] != ' ')
15960         return NULL;
15961     }
15962   else
15963     *str = end;
15964
15965   /* Look for unaffixed or special-case affixed mnemonic.  */
15966   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
15967                                                     end - base);
15968   if (opcode)
15969     {
15970       /* step U */
15971       if (opcode->tag < OT_odd_infix_0)
15972         {
15973           inst.cond = COND_ALWAYS;
15974           return opcode;
15975         }
15976
15977       if (warn_on_deprecated && unified_syntax)
15978         as_warn (_("conditional infixes are deprecated in unified syntax"));
15979       affix = base + (opcode->tag - OT_odd_infix_0);
15980       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
15981       gas_assert (cond);
15982
15983       inst.cond = cond->value;
15984       return opcode;
15985     }
15986
15987   /* Cannot have a conditional suffix on a mnemonic of less than two
15988      characters.  */
15989   if (end - base < 3)
15990     return NULL;
15991
15992   /* Look for suffixed mnemonic.  */
15993   affix = end - 2;
15994   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
15995   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
15996                                                     affix - base);
15997   if (opcode && cond)
15998     {
15999       /* step CE */
16000       switch (opcode->tag)
16001         {
16002         case OT_cinfix3_legacy:
16003           /* Ignore conditional suffixes matched on infix only mnemonics.  */
16004           break;
16005
16006         case OT_cinfix3:
16007         case OT_cinfix3_deprecated:
16008         case OT_odd_infix_unc:
16009           if (!unified_syntax)
16010             return 0;
16011           /* else fall through */
16012
16013         case OT_csuffix:
16014         case OT_csuffixF:
16015         case OT_csuf_or_in3:
16016           inst.cond = cond->value;
16017           return opcode;
16018
16019         case OT_unconditional:
16020         case OT_unconditionalF:
16021           if (thumb_mode)
16022             inst.cond = cond->value;
16023           else
16024             {
16025               /* Delayed diagnostic.  */
16026               inst.error = BAD_COND;
16027               inst.cond = COND_ALWAYS;
16028             }
16029           return opcode;
16030
16031         default:
16032           return NULL;
16033         }
16034     }
16035
16036   /* Cannot have a usual-position infix on a mnemonic of less than
16037      six characters (five would be a suffix).  */
16038   if (end - base < 6)
16039     return NULL;
16040
16041   /* Look for infixed mnemonic in the usual position.  */
16042   affix = base + 3;
16043   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
16044   if (!cond)
16045     return NULL;
16046
16047   memcpy (save, affix, 2);
16048   memmove (affix, affix + 2, (end - affix) - 2);
16049   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
16050                                                     (end - base) - 2);
16051   memmove (affix + 2, affix, (end - affix) - 2);
16052   memcpy (affix, save, 2);
16053
16054   if (opcode
16055       && (opcode->tag == OT_cinfix3
16056           || opcode->tag == OT_cinfix3_deprecated
16057           || opcode->tag == OT_csuf_or_in3
16058           || opcode->tag == OT_cinfix3_legacy))
16059     {
16060       /* Step CM.  */
16061       if (warn_on_deprecated && unified_syntax
16062           && (opcode->tag == OT_cinfix3
16063               || opcode->tag == OT_cinfix3_deprecated))
16064         as_warn (_("conditional infixes are deprecated in unified syntax"));
16065
16066       inst.cond = cond->value;
16067       return opcode;
16068     }
16069
16070   return NULL;
16071 }
16072
16073 /* This function generates an initial IT instruction, leaving its block
16074    virtually open for the new instructions. Eventually,
16075    the mask will be updated by now_it_add_mask () each time
16076    a new instruction needs to be included in the IT block.
16077    Finally, the block is closed with close_automatic_it_block ().
16078    The block closure can be requested either from md_assemble (),
16079    a tencode (), or due to a label hook.  */
16080
16081 static void
16082 new_automatic_it_block (int cond)
16083 {
16084   now_it.state = AUTOMATIC_IT_BLOCK;
16085   now_it.mask = 0x18;
16086   now_it.cc = cond;
16087   now_it.block_length = 1;
16088   mapping_state (MAP_THUMB);
16089   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
16090 }
16091
16092 /* Close an automatic IT block.
16093    See comments in new_automatic_it_block ().  */
16094
16095 static void
16096 close_automatic_it_block (void)
16097 {
16098   now_it.mask = 0x10;
16099   now_it.block_length = 0;
16100 }
16101
16102 /* Update the mask of the current automatically-generated IT
16103    instruction. See comments in new_automatic_it_block ().  */
16104
16105 static void
16106 now_it_add_mask (int cond)
16107 {
16108 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
16109 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
16110                                               | ((bitvalue) << (nbit)))
16111   const int resulting_bit = (cond & 1);
16112
16113   now_it.mask &= 0xf;
16114   now_it.mask = SET_BIT_VALUE (now_it.mask,
16115                                    resulting_bit,
16116                                   (5 - now_it.block_length));
16117   now_it.mask = SET_BIT_VALUE (now_it.mask,
16118                                    1,
16119                                    ((5 - now_it.block_length) - 1) );
16120   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
16121
16122 #undef CLEAR_BIT
16123 #undef SET_BIT_VALUE
16124 }
16125
16126 /* The IT blocks handling machinery is accessed through the these functions:
16127      it_fsm_pre_encode ()               from md_assemble ()
16128      set_it_insn_type ()                optional, from the tencode functions
16129      set_it_insn_type_last ()           ditto
16130      in_it_block ()                     ditto
16131      it_fsm_post_encode ()              from md_assemble ()
16132      force_automatic_it_block_close ()  from label habdling functions
16133
16134    Rationale:
16135      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
16136         initializing the IT insn type with a generic initial value depending
16137         on the inst.condition.
16138      2) During the tencode function, two things may happen:
16139         a) The tencode function overrides the IT insn type by
16140            calling either set_it_insn_type (type) or set_it_insn_type_last ().
16141         b) The tencode function queries the IT block state by
16142            calling in_it_block () (i.e. to determine narrow/not narrow mode).
16143
16144         Both set_it_insn_type and in_it_block run the internal FSM state
16145         handling function (handle_it_state), because: a) setting the IT insn
16146         type may incur in an invalid state (exiting the function),
16147         and b) querying the state requires the FSM to be updated.
16148         Specifically we want to avoid creating an IT block for conditional
16149         branches, so it_fsm_pre_encode is actually a guess and we can't
16150         determine whether an IT block is required until the tencode () routine
16151         has decided what type of instruction this actually it.
16152         Because of this, if set_it_insn_type and in_it_block have to be used,
16153         set_it_insn_type has to be called first.
16154
16155         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
16156         determines the insn IT type depending on the inst.cond code.
16157         When a tencode () routine encodes an instruction that can be
16158         either outside an IT block, or, in the case of being inside, has to be
16159         the last one, set_it_insn_type_last () will determine the proper
16160         IT instruction type based on the inst.cond code. Otherwise,
16161         set_it_insn_type can be called for overriding that logic or
16162         for covering other cases.
16163
16164         Calling handle_it_state () may not transition the IT block state to
16165         OUTSIDE_IT_BLOCK immediatelly, since the (current) state could be
16166         still queried. Instead, if the FSM determines that the state should
16167         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
16168         after the tencode () function: that's what it_fsm_post_encode () does.
16169
16170         Since in_it_block () calls the state handling function to get an
16171         updated state, an error may occur (due to invalid insns combination).
16172         In that case, inst.error is set.
16173         Therefore, inst.error has to be checked after the execution of
16174         the tencode () routine.
16175
16176      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
16177         any pending state change (if any) that didn't take place in
16178         handle_it_state () as explained above.  */
16179
16180 static void
16181 it_fsm_pre_encode (void)
16182 {
16183   if (inst.cond != COND_ALWAYS)
16184     inst.it_insn_type = INSIDE_IT_INSN;
16185   else
16186     inst.it_insn_type = OUTSIDE_IT_INSN;
16187
16188   now_it.state_handled = 0;
16189 }
16190
16191 /* IT state FSM handling function.  */
16192
16193 static int
16194 handle_it_state (void)
16195 {
16196   now_it.state_handled = 1;
16197
16198   switch (now_it.state)
16199     {
16200     case OUTSIDE_IT_BLOCK:
16201       switch (inst.it_insn_type)
16202         {
16203         case OUTSIDE_IT_INSN:
16204           break;
16205
16206         case INSIDE_IT_INSN:
16207         case INSIDE_IT_LAST_INSN:
16208           if (thumb_mode == 0)
16209             {
16210               if (unified_syntax
16211                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
16212                 as_tsktsk (_("Warning: conditional outside an IT block"\
16213                              " for Thumb."));
16214             }
16215           else
16216             {
16217               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
16218                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2))
16219                 {
16220                   /* Automatically generate the IT instruction.  */
16221                   new_automatic_it_block (inst.cond);
16222                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
16223                     close_automatic_it_block ();
16224                 }
16225               else
16226                 {
16227                   inst.error = BAD_OUT_IT;
16228                   return FAIL;
16229                 }
16230             }
16231           break;
16232
16233         case IF_INSIDE_IT_LAST_INSN:
16234         case NEUTRAL_IT_INSN:
16235           break;
16236
16237         case IT_INSN:
16238           now_it.state = MANUAL_IT_BLOCK;
16239           now_it.block_length = 0;
16240           break;
16241         }
16242       break;
16243
16244     case AUTOMATIC_IT_BLOCK:
16245       /* Three things may happen now:
16246          a) We should increment current it block size;
16247          b) We should close current it block (closing insn or 4 insns);
16248          c) We should close current it block and start a new one (due
16249          to incompatible conditions or
16250          4 insns-length block reached).  */
16251
16252       switch (inst.it_insn_type)
16253         {
16254         case OUTSIDE_IT_INSN:
16255           /* The closure of the block shall happen immediatelly,
16256              so any in_it_block () call reports the block as closed.  */
16257           force_automatic_it_block_close ();
16258           break;
16259
16260         case INSIDE_IT_INSN:
16261         case INSIDE_IT_LAST_INSN:
16262         case IF_INSIDE_IT_LAST_INSN:
16263           now_it.block_length++;
16264
16265           if (now_it.block_length > 4
16266               || !now_it_compatible (inst.cond))
16267             {
16268               force_automatic_it_block_close ();
16269               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
16270                 new_automatic_it_block (inst.cond);
16271             }
16272           else
16273             {
16274               now_it_add_mask (inst.cond);
16275             }
16276
16277           if (now_it.state == AUTOMATIC_IT_BLOCK
16278               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
16279                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
16280             close_automatic_it_block ();
16281           break;
16282
16283         case NEUTRAL_IT_INSN:
16284           now_it.block_length++;
16285
16286           if (now_it.block_length > 4)
16287             force_automatic_it_block_close ();
16288           else
16289             now_it_add_mask (now_it.cc & 1);
16290           break;
16291
16292         case IT_INSN:
16293           close_automatic_it_block ();
16294           now_it.state = MANUAL_IT_BLOCK;
16295           break;
16296         }
16297       break;
16298
16299     case MANUAL_IT_BLOCK:
16300       {
16301         /* Check conditional suffixes.  */
16302         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
16303         int is_last;
16304         now_it.mask <<= 1;
16305         now_it.mask &= 0x1f;
16306         is_last = (now_it.mask == 0x10);
16307
16308         switch (inst.it_insn_type)
16309           {
16310           case OUTSIDE_IT_INSN:
16311             inst.error = BAD_NOT_IT;
16312             return FAIL;
16313
16314           case INSIDE_IT_INSN:
16315             if (cond != inst.cond)
16316               {
16317                 inst.error = BAD_IT_COND;
16318                 return FAIL;
16319               }
16320             break;
16321
16322           case INSIDE_IT_LAST_INSN:
16323           case IF_INSIDE_IT_LAST_INSN:
16324             if (cond != inst.cond)
16325               {
16326                 inst.error = BAD_IT_COND;
16327                 return FAIL;
16328               }
16329             if (!is_last)
16330               {
16331                 inst.error = BAD_BRANCH;
16332                 return FAIL;
16333               }
16334             break;
16335
16336           case NEUTRAL_IT_INSN:
16337             /* The BKPT instruction is unconditional even in an IT block.  */
16338             break;
16339
16340           case IT_INSN:
16341             inst.error = BAD_IT_IT;
16342             return FAIL;
16343           }
16344       }
16345       break;
16346     }
16347
16348   return SUCCESS;
16349 }
16350
16351 static void
16352 it_fsm_post_encode (void)
16353 {
16354   int is_last;
16355
16356   if (!now_it.state_handled)
16357     handle_it_state ();
16358
16359   is_last = (now_it.mask == 0x10);
16360   if (is_last)
16361     {
16362       now_it.state = OUTSIDE_IT_BLOCK;
16363       now_it.mask = 0;
16364     }
16365 }
16366
16367 static void
16368 force_automatic_it_block_close (void)
16369 {
16370   if (now_it.state == AUTOMATIC_IT_BLOCK)
16371     {
16372       close_automatic_it_block ();
16373       now_it.state = OUTSIDE_IT_BLOCK;
16374       now_it.mask = 0;
16375     }
16376 }
16377
16378 static int
16379 in_it_block (void)
16380 {
16381   if (!now_it.state_handled)
16382     handle_it_state ();
16383
16384   return now_it.state != OUTSIDE_IT_BLOCK;
16385 }
16386
16387 void
16388 md_assemble (char *str)
16389 {
16390   char *p = str;
16391   const struct asm_opcode * opcode;
16392
16393   /* Align the previous label if needed.  */
16394   if (last_label_seen != NULL)
16395     {
16396       symbol_set_frag (last_label_seen, frag_now);
16397       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
16398       S_SET_SEGMENT (last_label_seen, now_seg);
16399     }
16400
16401   memset (&inst, '\0', sizeof (inst));
16402   inst.reloc.type = BFD_RELOC_UNUSED;
16403
16404   opcode = opcode_lookup (&p);
16405   if (!opcode)
16406     {
16407       /* It wasn't an instruction, but it might be a register alias of
16408          the form alias .req reg, or a Neon .dn/.qn directive.  */
16409       if (! create_register_alias (str, p)
16410           && ! create_neon_reg_alias (str, p))
16411         as_bad (_("bad instruction `%s'"), str);
16412
16413       return;
16414     }
16415
16416   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
16417     as_warn (_("s suffix on comparison instruction is deprecated"));
16418
16419   /* The value which unconditional instructions should have in place of the
16420      condition field.  */
16421   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
16422
16423   if (thumb_mode)
16424     {
16425       arm_feature_set variant;
16426
16427       variant = cpu_variant;
16428       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
16429       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
16430         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
16431       /* Check that this instruction is supported for this CPU.  */
16432       if (!opcode->tvariant
16433           || (thumb_mode == 1
16434               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
16435         {
16436           as_bad (_("selected processor does not support Thumb mode `%s'"), str);
16437           return;
16438         }
16439       if (inst.cond != COND_ALWAYS && !unified_syntax
16440           && opcode->tencode != do_t_branch)
16441         {
16442           as_bad (_("Thumb does not support conditional execution"));
16443           return;
16444         }
16445
16446       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2))
16447         {
16448           if (opcode->tencode != do_t_blx && opcode->tencode != do_t_branch23
16449               && !(ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_msr)
16450                    || ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_barrier)))
16451             {
16452               /* Two things are addressed here.
16453                  1) Implicit require narrow instructions on Thumb-1.
16454                     This avoids relaxation accidentally introducing Thumb-2
16455                      instructions.
16456                  2) Reject wide instructions in non Thumb-2 cores.  */
16457               if (inst.size_req == 0)
16458                 inst.size_req = 2;
16459               else if (inst.size_req == 4)
16460                 {
16461                   as_bad (_("selected processor does not support Thumb-2 mode `%s'"), str);
16462                   return;
16463                 }
16464             }
16465         }
16466
16467       inst.instruction = opcode->tvalue;
16468
16469       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
16470         {
16471           /* Prepare the it_insn_type for those encodings that don't set
16472              it.  */
16473           it_fsm_pre_encode ();
16474
16475           opcode->tencode ();
16476
16477           it_fsm_post_encode ();
16478         }
16479
16480       if (!(inst.error || inst.relax))
16481         {
16482           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
16483           inst.size = (inst.instruction > 0xffff ? 4 : 2);
16484           if (inst.size_req && inst.size_req != inst.size)
16485             {
16486               as_bad (_("cannot honor width suffix -- `%s'"), str);
16487               return;
16488             }
16489         }
16490
16491       /* Something has gone badly wrong if we try to relax a fixed size
16492          instruction.  */
16493       gas_assert (inst.size_req == 0 || !inst.relax);
16494
16495       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
16496                               *opcode->tvariant);
16497       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
16498          set those bits when Thumb-2 32-bit instructions are seen.  ie.
16499          anything other than bl/blx and v6-M instructions.
16500          This is overly pessimistic for relaxable instructions.  */
16501       if (((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
16502            || inst.relax)
16503           && !(ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
16504                || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier)))
16505         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
16506                                 arm_ext_v6t2);
16507
16508       check_neon_suffixes;
16509
16510       if (!inst.error)
16511         {
16512           mapping_state (MAP_THUMB);
16513         }
16514     }
16515   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
16516     {
16517       bfd_boolean is_bx;
16518
16519       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
16520       is_bx = (opcode->aencode == do_bx);
16521
16522       /* Check that this instruction is supported for this CPU.  */
16523       if (!(is_bx && fix_v4bx)
16524           && !(opcode->avariant &&
16525                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
16526         {
16527           as_bad (_("selected processor does not support ARM mode `%s'"), str);
16528           return;
16529         }
16530       if (inst.size_req)
16531         {
16532           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
16533           return;
16534         }
16535
16536       inst.instruction = opcode->avalue;
16537       if (opcode->tag == OT_unconditionalF)
16538         inst.instruction |= 0xF << 28;
16539       else
16540         inst.instruction |= inst.cond << 28;
16541       inst.size = INSN_SIZE;
16542       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
16543         {
16544           it_fsm_pre_encode ();
16545           opcode->aencode ();
16546           it_fsm_post_encode ();
16547         }
16548       /* Arm mode bx is marked as both v4T and v5 because it's still required
16549          on a hypothetical non-thumb v5 core.  */
16550       if (is_bx)
16551         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
16552       else
16553         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
16554                                 *opcode->avariant);
16555
16556       check_neon_suffixes;
16557
16558       if (!inst.error)
16559         {
16560           mapping_state (MAP_ARM);
16561         }
16562     }
16563   else
16564     {
16565       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
16566                 "-- `%s'"), str);
16567       return;
16568     }
16569   output_inst (str);
16570 }
16571
16572 static void
16573 check_it_blocks_finished (void)
16574 {
16575 #ifdef OBJ_ELF
16576   asection *sect;
16577
16578   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
16579     if (seg_info (sect)->tc_segment_info_data.current_it.state
16580         == MANUAL_IT_BLOCK)
16581       {
16582         as_warn (_("section '%s' finished with an open IT block."),
16583                  sect->name);
16584       }
16585 #else
16586   if (now_it.state == MANUAL_IT_BLOCK)
16587     as_warn (_("file finished with an open IT block."));
16588 #endif
16589 }
16590
16591 /* Various frobbings of labels and their addresses.  */
16592
16593 void
16594 arm_start_line_hook (void)
16595 {
16596   last_label_seen = NULL;
16597 }
16598
16599 void
16600 arm_frob_label (symbolS * sym)
16601 {
16602   last_label_seen = sym;
16603
16604   ARM_SET_THUMB (sym, thumb_mode);
16605
16606 #if defined OBJ_COFF || defined OBJ_ELF
16607   ARM_SET_INTERWORK (sym, support_interwork);
16608 #endif
16609
16610   force_automatic_it_block_close ();
16611
16612   /* Note - do not allow local symbols (.Lxxx) to be labelled
16613      as Thumb functions.  This is because these labels, whilst
16614      they exist inside Thumb code, are not the entry points for
16615      possible ARM->Thumb calls.  Also, these labels can be used
16616      as part of a computed goto or switch statement.  eg gcc
16617      can generate code that looks like this:
16618
16619                 ldr  r2, [pc, .Laaa]
16620                 lsl  r3, r3, #2
16621                 ldr  r2, [r3, r2]
16622                 mov  pc, r2
16623
16624        .Lbbb:  .word .Lxxx
16625        .Lccc:  .word .Lyyy
16626        ..etc...
16627        .Laaa:   .word Lbbb
16628
16629      The first instruction loads the address of the jump table.
16630      The second instruction converts a table index into a byte offset.
16631      The third instruction gets the jump address out of the table.
16632      The fourth instruction performs the jump.
16633
16634      If the address stored at .Laaa is that of a symbol which has the
16635      Thumb_Func bit set, then the linker will arrange for this address
16636      to have the bottom bit set, which in turn would mean that the
16637      address computation performed by the third instruction would end
16638      up with the bottom bit set.  Since the ARM is capable of unaligned
16639      word loads, the instruction would then load the incorrect address
16640      out of the jump table, and chaos would ensue.  */
16641   if (label_is_thumb_function_name
16642       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
16643       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
16644     {
16645       /* When the address of a Thumb function is taken the bottom
16646          bit of that address should be set.  This will allow
16647          interworking between Arm and Thumb functions to work
16648          correctly.  */
16649
16650       THUMB_SET_FUNC (sym, 1);
16651
16652       label_is_thumb_function_name = FALSE;
16653     }
16654
16655   dwarf2_emit_label (sym);
16656 }
16657
16658 bfd_boolean
16659 arm_data_in_code (void)
16660 {
16661   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
16662     {
16663       *input_line_pointer = '/';
16664       input_line_pointer += 5;
16665       *input_line_pointer = 0;
16666       return TRUE;
16667     }
16668
16669   return FALSE;
16670 }
16671
16672 char *
16673 arm_canonicalize_symbol_name (char * name)
16674 {
16675   int len;
16676
16677   if (thumb_mode && (len = strlen (name)) > 5
16678       && streq (name + len - 5, "/data"))
16679     *(name + len - 5) = 0;
16680
16681   return name;
16682 }
16683 \f
16684 /* Table of all register names defined by default.  The user can
16685    define additional names with .req.  Note that all register names
16686    should appear in both upper and lowercase variants.  Some registers
16687    also have mixed-case names.  */
16688
16689 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
16690 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
16691 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
16692 #define REGSET(p,t) \
16693   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
16694   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
16695   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
16696   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
16697 #define REGSETH(p,t) \
16698   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
16699   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
16700   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
16701   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
16702 #define REGSET2(p,t) \
16703   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
16704   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
16705   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
16706   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
16707 #define SPLRBANK(base,bank,t) \
16708   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
16709   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
16710   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
16711   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
16712   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
16713   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
16714
16715 static const struct reg_entry reg_names[] =
16716 {
16717   /* ARM integer registers.  */
16718   REGSET(r, RN), REGSET(R, RN),
16719
16720   /* ATPCS synonyms.  */
16721   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
16722   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
16723   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
16724
16725   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
16726   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
16727   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
16728
16729   /* Well-known aliases.  */
16730   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
16731   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
16732
16733   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
16734   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
16735
16736   /* Coprocessor numbers.  */
16737   REGSET(p, CP), REGSET(P, CP),
16738
16739   /* Coprocessor register numbers.  The "cr" variants are for backward
16740      compatibility.  */
16741   REGSET(c,  CN), REGSET(C, CN),
16742   REGSET(cr, CN), REGSET(CR, CN),
16743
16744   /* ARM banked registers.  */
16745   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
16746   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
16747   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
16748   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
16749   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
16750   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
16751   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
16752
16753   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
16754   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
16755   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
16756   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
16757   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
16758   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(SP_fiq,512|(13<<16),RNB),
16759   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
16760   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
16761
16762   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
16763   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
16764   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
16765   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
16766   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
16767   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
16768   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
16769   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
16770   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
16771
16772   /* FPA registers.  */
16773   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
16774   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
16775
16776   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
16777   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
16778
16779   /* VFP SP registers.  */
16780   REGSET(s,VFS),  REGSET(S,VFS),
16781   REGSETH(s,VFS), REGSETH(S,VFS),
16782
16783   /* VFP DP Registers.  */
16784   REGSET(d,VFD),  REGSET(D,VFD),
16785   /* Extra Neon DP registers.  */
16786   REGSETH(d,VFD), REGSETH(D,VFD),
16787
16788   /* Neon QP registers.  */
16789   REGSET2(q,NQ),  REGSET2(Q,NQ),
16790
16791   /* VFP control registers.  */
16792   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
16793   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
16794   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
16795   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
16796   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
16797   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
16798
16799   /* Maverick DSP coprocessor registers.  */
16800   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
16801   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
16802
16803   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
16804   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
16805   REGDEF(dspsc,0,DSPSC),
16806
16807   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
16808   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
16809   REGDEF(DSPSC,0,DSPSC),
16810
16811   /* iWMMXt data registers - p0, c0-15.  */
16812   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
16813
16814   /* iWMMXt control registers - p1, c0-3.  */
16815   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
16816   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
16817   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
16818   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
16819
16820   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
16821   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
16822   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
16823   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
16824   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
16825
16826   /* XScale accumulator registers.  */
16827   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
16828 };
16829 #undef REGDEF
16830 #undef REGNUM
16831 #undef REGSET
16832
16833 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
16834    within psr_required_here.  */
16835 static const struct asm_psr psrs[] =
16836 {
16837   /* Backward compatibility notation.  Note that "all" is no longer
16838      truly all possible PSR bits.  */
16839   {"all",  PSR_c | PSR_f},
16840   {"flg",  PSR_f},
16841   {"ctl",  PSR_c},
16842
16843   /* Individual flags.  */
16844   {"f",    PSR_f},
16845   {"c",    PSR_c},
16846   {"x",    PSR_x},
16847   {"s",    PSR_s},
16848
16849   /* Combinations of flags.  */
16850   {"fs",   PSR_f | PSR_s},
16851   {"fx",   PSR_f | PSR_x},
16852   {"fc",   PSR_f | PSR_c},
16853   {"sf",   PSR_s | PSR_f},
16854   {"sx",   PSR_s | PSR_x},
16855   {"sc",   PSR_s | PSR_c},
16856   {"xf",   PSR_x | PSR_f},
16857   {"xs",   PSR_x | PSR_s},
16858   {"xc",   PSR_x | PSR_c},
16859   {"cf",   PSR_c | PSR_f},
16860   {"cs",   PSR_c | PSR_s},
16861   {"cx",   PSR_c | PSR_x},
16862   {"fsx",  PSR_f | PSR_s | PSR_x},
16863   {"fsc",  PSR_f | PSR_s | PSR_c},
16864   {"fxs",  PSR_f | PSR_x | PSR_s},
16865   {"fxc",  PSR_f | PSR_x | PSR_c},
16866   {"fcs",  PSR_f | PSR_c | PSR_s},
16867   {"fcx",  PSR_f | PSR_c | PSR_x},
16868   {"sfx",  PSR_s | PSR_f | PSR_x},
16869   {"sfc",  PSR_s | PSR_f | PSR_c},
16870   {"sxf",  PSR_s | PSR_x | PSR_f},
16871   {"sxc",  PSR_s | PSR_x | PSR_c},
16872   {"scf",  PSR_s | PSR_c | PSR_f},
16873   {"scx",  PSR_s | PSR_c | PSR_x},
16874   {"xfs",  PSR_x | PSR_f | PSR_s},
16875   {"xfc",  PSR_x | PSR_f | PSR_c},
16876   {"xsf",  PSR_x | PSR_s | PSR_f},
16877   {"xsc",  PSR_x | PSR_s | PSR_c},
16878   {"xcf",  PSR_x | PSR_c | PSR_f},
16879   {"xcs",  PSR_x | PSR_c | PSR_s},
16880   {"cfs",  PSR_c | PSR_f | PSR_s},
16881   {"cfx",  PSR_c | PSR_f | PSR_x},
16882   {"csf",  PSR_c | PSR_s | PSR_f},
16883   {"csx",  PSR_c | PSR_s | PSR_x},
16884   {"cxf",  PSR_c | PSR_x | PSR_f},
16885   {"cxs",  PSR_c | PSR_x | PSR_s},
16886   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
16887   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
16888   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
16889   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
16890   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
16891   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
16892   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
16893   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
16894   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
16895   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
16896   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
16897   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
16898   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
16899   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
16900   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
16901   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
16902   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
16903   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
16904   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
16905   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
16906   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
16907   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
16908   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
16909   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
16910 };
16911
16912 /* Table of V7M psr names.  */
16913 static const struct asm_psr v7m_psrs[] =
16914 {
16915   {"apsr",        0 }, {"APSR",         0 },
16916   {"iapsr",       1 }, {"IAPSR",        1 },
16917   {"eapsr",       2 }, {"EAPSR",        2 },
16918   {"psr",         3 }, {"PSR",          3 },
16919   {"xpsr",        3 }, {"XPSR",         3 }, {"xPSR",     3 },
16920   {"ipsr",        5 }, {"IPSR",         5 },
16921   {"epsr",        6 }, {"EPSR",         6 },
16922   {"iepsr",       7 }, {"IEPSR",        7 },
16923   {"msp",         8 }, {"MSP",          8 },
16924   {"psp",         9 }, {"PSP",          9 },
16925   {"primask",     16}, {"PRIMASK",      16},
16926   {"basepri",     17}, {"BASEPRI",      17},
16927   {"basepri_max", 18}, {"BASEPRI_MAX",  18},
16928   {"basepri_max", 18}, {"BASEPRI_MASK", 18}, /* Typo, preserved for backwards compatibility.  */
16929   {"faultmask",   19}, {"FAULTMASK",    19},
16930   {"control",     20}, {"CONTROL",      20}
16931 };
16932
16933 /* Table of all shift-in-operand names.  */
16934 static const struct asm_shift_name shift_names [] =
16935 {
16936   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
16937   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
16938   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
16939   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
16940   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
16941   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
16942 };
16943
16944 /* Table of all explicit relocation names.  */
16945 #ifdef OBJ_ELF
16946 static struct reloc_entry reloc_names[] =
16947 {
16948   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
16949   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
16950   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
16951   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
16952   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
16953   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
16954   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
16955   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
16956   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
16957   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
16958   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
16959   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
16960   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
16961         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
16962   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
16963         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
16964   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
16965         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ}
16966 };
16967 #endif
16968
16969 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
16970 static const struct asm_cond conds[] =
16971 {
16972   {"eq", 0x0},
16973   {"ne", 0x1},
16974   {"cs", 0x2}, {"hs", 0x2},
16975   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
16976   {"mi", 0x4},
16977   {"pl", 0x5},
16978   {"vs", 0x6},
16979   {"vc", 0x7},
16980   {"hi", 0x8},
16981   {"ls", 0x9},
16982   {"ge", 0xa},
16983   {"lt", 0xb},
16984   {"gt", 0xc},
16985   {"le", 0xd},
16986   {"al", 0xe}
16987 };
16988
16989 static struct asm_barrier_opt barrier_opt_names[] =
16990 {
16991   { "sy",    0xf }, { "SY",    0xf },
16992   { "un",    0x7 }, { "UN",    0x7 },
16993   { "st",    0xe }, { "ST",    0xe },
16994   { "unst",  0x6 }, { "UNST",  0x6 },
16995   { "ish",   0xb }, { "ISH",   0xb },
16996   { "sh",    0xb }, { "SH",    0xb },
16997   { "ishst", 0xa }, { "ISHST", 0xa },
16998   { "shst",  0xa }, { "SHST",  0xa },
16999   { "nsh",   0x7 }, { "NSH",   0x7 },
17000   { "nshst", 0x6 }, { "NSHST", 0x6 },
17001   { "osh",   0x3 }, { "OSH",   0x3 },
17002   { "oshst", 0x2 }, { "OSHST", 0x2 }
17003 };
17004
17005 /* Table of ARM-format instructions.    */
17006
17007 /* Macros for gluing together operand strings.  N.B. In all cases
17008    other than OPS0, the trailing OP_stop comes from default
17009    zero-initialization of the unspecified elements of the array.  */
17010 #define OPS0()            { OP_stop, }
17011 #define OPS1(a)           { OP_##a, }
17012 #define OPS2(a,b)         { OP_##a,OP_##b, }
17013 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
17014 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
17015 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
17016 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
17017
17018 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
17019    This is useful when mixing operands for ARM and THUMB, i.e. using the
17020    MIX_ARM_THUMB_OPERANDS macro.
17021    In order to use these macros, prefix the number of operands with _
17022    e.g. _3.  */
17023 #define OPS_1(a)           { a, }
17024 #define OPS_2(a,b)         { a,b, }
17025 #define OPS_3(a,b,c)       { a,b,c, }
17026 #define OPS_4(a,b,c,d)     { a,b,c,d, }
17027 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
17028 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
17029
17030 /* These macros abstract out the exact format of the mnemonic table and
17031    save some repeated characters.  */
17032
17033 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
17034 #define TxCE(mnem, op, top, nops, ops, ae, te) \
17035   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
17036     THUMB_VARIANT, do_##ae, do_##te }
17037
17038 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
17039    a T_MNEM_xyz enumerator.  */
17040 #define TCE(mnem, aop, top, nops, ops, ae, te) \
17041       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
17042 #define tCE(mnem, aop, top, nops, ops, ae, te) \
17043       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
17044
17045 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
17046    infix after the third character.  */
17047 #define TxC3(mnem, op, top, nops, ops, ae, te) \
17048   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
17049     THUMB_VARIANT, do_##ae, do_##te }
17050 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
17051   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
17052     THUMB_VARIANT, do_##ae, do_##te }
17053 #define TC3(mnem, aop, top, nops, ops, ae, te) \
17054       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
17055 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
17056       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
17057 #define tC3(mnem, aop, top, nops, ops, ae, te) \
17058       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
17059 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
17060       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
17061
17062 /* Mnemonic with a conditional infix in an unusual place.  Each and every variant has to
17063    appear in the condition table.  */
17064 #define TxCM_(m1, m2, m3, op, top, nops, ops, ae, te)   \
17065   { m1 #m2 m3, OPS##nops ops, sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
17066     0x##op, top, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##te }
17067
17068 #define TxCM(m1, m2, op, top, nops, ops, ae, te)        \
17069   TxCM_ (m1,   , m2, op, top, nops, ops, ae, te),       \
17070   TxCM_ (m1, eq, m2, op, top, nops, ops, ae, te),       \
17071   TxCM_ (m1, ne, m2, op, top, nops, ops, ae, te),       \
17072   TxCM_ (m1, cs, m2, op, top, nops, ops, ae, te),       \
17073   TxCM_ (m1, hs, m2, op, top, nops, ops, ae, te),       \
17074   TxCM_ (m1, cc, m2, op, top, nops, ops, ae, te),       \
17075   TxCM_ (m1, ul, m2, op, top, nops, ops, ae, te),       \
17076   TxCM_ (m1, lo, m2, op, top, nops, ops, ae, te),       \
17077   TxCM_ (m1, mi, m2, op, top, nops, ops, ae, te),       \
17078   TxCM_ (m1, pl, m2, op, top, nops, ops, ae, te),       \
17079   TxCM_ (m1, vs, m2, op, top, nops, ops, ae, te),       \
17080   TxCM_ (m1, vc, m2, op, top, nops, ops, ae, te),       \
17081   TxCM_ (m1, hi, m2, op, top, nops, ops, ae, te),       \
17082   TxCM_ (m1, ls, m2, op, top, nops, ops, ae, te),       \
17083   TxCM_ (m1, ge, m2, op, top, nops, ops, ae, te),       \
17084   TxCM_ (m1, lt, m2, op, top, nops, ops, ae, te),       \
17085   TxCM_ (m1, gt, m2, op, top, nops, ops, ae, te),       \
17086   TxCM_ (m1, le, m2, op, top, nops, ops, ae, te),       \
17087   TxCM_ (m1, al, m2, op, top, nops, ops, ae, te)
17088
17089 #define TCM(m1,m2, aop, top, nops, ops, ae, te)         \
17090       TxCM (m1,m2, aop, 0x##top, nops, ops, ae, te)
17091 #define tCM(m1,m2, aop, top, nops, ops, ae, te)         \
17092       TxCM (m1,m2, aop, T_MNEM##top, nops, ops, ae, te)
17093
17094 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
17095    field is still 0xE.  Many of the Thumb variants can be executed
17096    conditionally, so this is checked separately.  */
17097 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
17098   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
17099     THUMB_VARIANT, do_##ae, do_##te }
17100
17101 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
17102    condition code field.  */
17103 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
17104   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
17105     THUMB_VARIANT, do_##ae, do_##te }
17106
17107 /* ARM-only variants of all the above.  */
17108 #define CE(mnem,  op, nops, ops, ae)    \
17109   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
17110
17111 #define C3(mnem, op, nops, ops, ae)     \
17112   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
17113
17114 /* Legacy mnemonics that always have conditional infix after the third
17115    character.  */
17116 #define CL(mnem, op, nops, ops, ae)     \
17117   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
17118     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
17119
17120 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
17121 #define cCE(mnem,  op, nops, ops, ae)   \
17122   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
17123
17124 /* Legacy coprocessor instructions where conditional infix and conditional
17125    suffix are ambiguous.  For consistency this includes all FPA instructions,
17126    not just the potentially ambiguous ones.  */
17127 #define cCL(mnem, op, nops, ops, ae)    \
17128   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
17129     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
17130
17131 /* Coprocessor, takes either a suffix or a position-3 infix
17132    (for an FPA corner case). */
17133 #define C3E(mnem, op, nops, ops, ae) \
17134   { mnem, OPS##nops ops, OT_csuf_or_in3, \
17135     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
17136
17137 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
17138   { m1 #m2 m3, OPS##nops ops, \
17139     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
17140     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
17141
17142 #define CM(m1, m2, op, nops, ops, ae)   \
17143   xCM_ (m1,   , m2, op, nops, ops, ae), \
17144   xCM_ (m1, eq, m2, op, nops, ops, ae), \
17145   xCM_ (m1, ne, m2, op, nops, ops, ae), \
17146   xCM_ (m1, cs, m2, op, nops, ops, ae), \
17147   xCM_ (m1, hs, m2, op, nops, ops, ae), \
17148   xCM_ (m1, cc, m2, op, nops, ops, ae), \
17149   xCM_ (m1, ul, m2, op, nops, ops, ae), \
17150   xCM_ (m1, lo, m2, op, nops, ops, ae), \
17151   xCM_ (m1, mi, m2, op, nops, ops, ae), \
17152   xCM_ (m1, pl, m2, op, nops, ops, ae), \
17153   xCM_ (m1, vs, m2, op, nops, ops, ae), \
17154   xCM_ (m1, vc, m2, op, nops, ops, ae), \
17155   xCM_ (m1, hi, m2, op, nops, ops, ae), \
17156   xCM_ (m1, ls, m2, op, nops, ops, ae), \
17157   xCM_ (m1, ge, m2, op, nops, ops, ae), \
17158   xCM_ (m1, lt, m2, op, nops, ops, ae), \
17159   xCM_ (m1, gt, m2, op, nops, ops, ae), \
17160   xCM_ (m1, le, m2, op, nops, ops, ae), \
17161   xCM_ (m1, al, m2, op, nops, ops, ae)
17162
17163 #define UE(mnem, op, nops, ops, ae)     \
17164   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
17165
17166 #define UF(mnem, op, nops, ops, ae)     \
17167   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
17168
17169 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
17170    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
17171    use the same encoding function for each.  */
17172 #define NUF(mnem, op, nops, ops, enc)                                   \
17173   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
17174     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
17175
17176 /* Neon data processing, version which indirects through neon_enc_tab for
17177    the various overloaded versions of opcodes.  */
17178 #define nUF(mnem, op, nops, ops, enc)                                   \
17179   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
17180     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
17181
17182 /* Neon insn with conditional suffix for the ARM version, non-overloaded
17183    version.  */
17184 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
17185   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
17186     THUMB_VARIANT, do_##enc, do_##enc }
17187
17188 #define NCE(mnem, op, nops, ops, enc)                                   \
17189    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
17190
17191 #define NCEF(mnem, op, nops, ops, enc)                                  \
17192     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
17193
17194 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
17195 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
17196   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
17197     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
17198
17199 #define nCE(mnem, op, nops, ops, enc)                                   \
17200    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
17201
17202 #define nCEF(mnem, op, nops, ops, enc)                                  \
17203     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
17204
17205 #define do_0 0
17206
17207 static const struct asm_opcode insns[] =
17208 {
17209 #define ARM_VARIANT &arm_ext_v1 /* Core ARM Instructions.  */
17210 #define THUMB_VARIANT &arm_ext_v4t
17211  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
17212  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
17213  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
17214  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
17215  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
17216  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
17217  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
17218  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
17219  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
17220  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
17221  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
17222  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
17223  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
17224  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
17225  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
17226  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
17227
17228  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
17229     for setting PSR flag bits.  They are obsolete in V6 and do not
17230     have Thumb equivalents. */
17231  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
17232  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
17233   CL("tstp",    110f000,           2, (RR, SH),      cmp),
17234  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
17235  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
17236   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
17237  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
17238  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
17239   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
17240
17241  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
17242  tC3("movs",    1b00000, _movs,    2, (RR, SH),      mov,  t_mov_cmp),
17243  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
17244  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
17245
17246  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
17247  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
17248  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
17249                                                                 OP_RRnpc),
17250                                         OP_ADDRGLDR),ldst, t_ldst),
17251  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
17252
17253  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17254  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17255  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17256  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17257  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17258  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17259
17260  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
17261  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
17262  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
17263  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
17264
17265   /* Pseudo ops.  */
17266  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
17267   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
17268  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
17269
17270   /* Thumb-compatibility pseudo ops.  */
17271  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
17272  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
17273  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
17274  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
17275  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
17276  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
17277  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
17278  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
17279  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
17280  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
17281  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
17282  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
17283
17284  /* These may simplify to neg.  */
17285  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
17286  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
17287
17288 #undef  THUMB_VARIANT
17289 #define THUMB_VARIANT  & arm_ext_v6
17290
17291  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
17292
17293  /* V1 instructions with no Thumb analogue prior to V6T2.  */
17294 #undef  THUMB_VARIANT
17295 #define THUMB_VARIANT  & arm_ext_v6t2
17296
17297  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
17298  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
17299   CL("teqp",    130f000,           2, (RR, SH),      cmp),
17300
17301  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17302  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17303  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
17304  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17305
17306  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17307  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17308
17309  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17310  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17311
17312  /* V1 instructions with no Thumb analogue at all.  */
17313   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
17314   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
17315
17316   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
17317   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
17318   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
17319   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
17320   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
17321   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
17322   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
17323   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
17324
17325 #undef  ARM_VARIANT
17326 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
17327 #undef  THUMB_VARIANT
17328 #define THUMB_VARIANT  & arm_ext_v4t
17329
17330  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
17331  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
17332
17333 #undef  THUMB_VARIANT
17334 #define THUMB_VARIANT  & arm_ext_v6t2
17335
17336  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
17337   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
17338
17339   /* Generic coprocessor instructions.  */
17340  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
17341  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17342  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17343  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17344  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17345  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
17346  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
17347
17348 #undef  ARM_VARIANT
17349 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
17350
17351   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
17352   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
17353
17354 #undef  ARM_VARIANT
17355 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
17356 #undef  THUMB_VARIANT
17357 #define THUMB_VARIANT  & arm_ext_msr
17358
17359  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
17360  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
17361
17362 #undef  ARM_VARIANT
17363 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
17364 #undef  THUMB_VARIANT
17365 #define THUMB_VARIANT  & arm_ext_v6t2
17366
17367  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17368   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17369  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17370   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17371  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17372   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17373  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17374   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17375
17376 #undef  ARM_VARIANT
17377 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
17378 #undef  THUMB_VARIANT
17379 #define THUMB_VARIANT  & arm_ext_v4t
17380
17381  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17382  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17383  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17384  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17385  tCM("ld","sh", 01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17386  tCM("ld","sb", 01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17387
17388 #undef  ARM_VARIANT
17389 #define ARM_VARIANT  & arm_ext_v4t_5
17390
17391   /* ARM Architecture 4T.  */
17392   /* Note: bx (and blx) are required on V5, even if the processor does
17393      not support Thumb.  */
17394  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
17395
17396 #undef  ARM_VARIANT
17397 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
17398 #undef  THUMB_VARIANT
17399 #define THUMB_VARIANT  & arm_ext_v5t
17400
17401   /* Note: blx has 2 variants; the .value coded here is for
17402      BLX(2).  Only this variant has conditional execution.  */
17403  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
17404  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
17405
17406 #undef  THUMB_VARIANT
17407 #define THUMB_VARIANT  & arm_ext_v6t2
17408
17409  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
17410  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17411  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
17412  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17413  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
17414  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
17415  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
17416  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
17417
17418 #undef  ARM_VARIANT
17419 #define ARM_VARIANT  & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
17420 #undef THUMB_VARIANT
17421 #define THUMB_VARIANT &arm_ext_v5exp
17422
17423  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17424  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17425  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17426  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17427
17428  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17429  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17430
17431  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17432  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17433  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17434  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17435
17436  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17437  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17438  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17439  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17440
17441  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17442  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17443
17444  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17445  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17446  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17447  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17448
17449 #undef  ARM_VARIANT
17450 #define ARM_VARIANT  & arm_ext_v5e /*  ARM Architecture 5TE.  */
17451 #undef THUMB_VARIANT
17452 #define THUMB_VARIANT &arm_ext_v6t2
17453
17454  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
17455  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
17456      ldrd, t_ldstd),
17457  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
17458                                        ADDRGLDRS), ldrd, t_ldstd),
17459
17460  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17461  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17462
17463 #undef  ARM_VARIANT
17464 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
17465
17466  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
17467
17468 #undef  ARM_VARIANT
17469 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
17470 #undef  THUMB_VARIANT
17471 #define THUMB_VARIANT  & arm_ext_v6
17472
17473  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
17474  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
17475  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
17476  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
17477  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
17478  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17479  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17480  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17481  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17482  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
17483
17484 #undef  THUMB_VARIANT
17485 #define THUMB_VARIANT  & arm_ext_v6t2
17486
17487  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
17488  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
17489                                       strex,  t_strex),
17490  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17491  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17492
17493  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
17494  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
17495
17496 /*  ARM V6 not included in V7M.  */
17497 #undef  THUMB_VARIANT
17498 #define THUMB_VARIANT  & arm_ext_v6_notm
17499  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
17500   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
17501   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
17502  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
17503  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
17504   UF(rfefa,     9900a00,           1, (RRw),                       rfe),
17505   UF(rfeea,     8100a00,           1, (RRw),                       rfe),
17506  TUF("rfeed",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
17507  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
17508   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
17509   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
17510  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
17511
17512 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
17513 #undef  THUMB_VARIANT
17514 #define THUMB_VARIANT  & arm_ext_v6_dsp
17515  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
17516  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
17517  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
17518  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17519  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17520  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17521  /* Old name for QASX.  */
17522  TCE("qaddsubx",        6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17523  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17524  /* Old name for QSAX.  */
17525  TCE("qsubaddx",        6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17526  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17527  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17528  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17529  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17530  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17531  /* Old name for SASX.  */
17532  TCE("saddsubx",        6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17533  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17534  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17535  TCE("shasx",     6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17536  /* Old name for SHASX.  */
17537  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17538  TCE("shsax",      6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),    rd_rn_rm, t_simd),
17539  /* Old name for SHSAX.  */
17540  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17541  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17542  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17543  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17544  /* Old name for SSAX.  */
17545  TCE("ssubaddx",        6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17546  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17547  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17548  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17549  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17550  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17551  /* Old name for UASX.  */
17552  TCE("uaddsubx",        6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17553  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17554  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17555  TCE("uhasx",     6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17556  /* Old name for UHASX.  */
17557  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17558  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17559  /* Old name for UHSAX.  */
17560  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17561  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17562  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17563  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17564  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17565  TCE("uqasx",     6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17566  /* Old name for UQASX.  */
17567  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17568  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17569  /* Old name for UQSAX.  */
17570  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17571  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17572  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17573  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17574  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17575  /* Old name for USAX.  */
17576  TCE("usubaddx",        6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17577  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17578  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17579  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17580  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17581  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
17582  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17583  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17584  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17585  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
17586  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17587  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17588  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17589  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17590  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17591  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17592  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17593  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17594  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17595  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17596  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17597  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17598  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17599  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17600  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17601  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17602  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17603  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17604  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17605  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
17606  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
17607  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
17608  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
17609  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
17610
17611 #undef  ARM_VARIANT
17612 #define ARM_VARIANT   & arm_ext_v6k
17613 #undef  THUMB_VARIANT
17614 #define THUMB_VARIANT & arm_ext_v6k
17615
17616  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
17617  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
17618  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
17619  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
17620
17621 #undef  THUMB_VARIANT
17622 #define THUMB_VARIANT  & arm_ext_v6_notm
17623  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
17624                                       ldrexd, t_ldrexd),
17625  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
17626                                        RRnpcb), strexd, t_strexd),
17627
17628 #undef  THUMB_VARIANT
17629 #define THUMB_VARIANT  & arm_ext_v6t2
17630  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
17631      rd_rn,  rd_rn),
17632  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
17633      rd_rn,  rd_rn),
17634  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
17635      strex, t_strexbh),
17636  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
17637      strex, t_strexbh),
17638  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
17639
17640 #undef  ARM_VARIANT
17641 #define ARM_VARIANT    & arm_ext_sec
17642 #undef THUMB_VARIANT
17643 #define THUMB_VARIANT  & arm_ext_sec
17644
17645  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
17646
17647 #undef  ARM_VARIANT
17648 #define ARM_VARIANT    & arm_ext_virt
17649 #undef  THUMB_VARIANT
17650 #define THUMB_VARIANT    & arm_ext_virt
17651
17652  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
17653  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
17654
17655 #undef  ARM_VARIANT
17656 #define ARM_VARIANT  & arm_ext_v6t2
17657 #undef  THUMB_VARIANT
17658 #define THUMB_VARIANT  & arm_ext_v6t2
17659
17660  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
17661  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
17662  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
17663  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
17664
17665  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
17666  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
17667  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
17668  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
17669
17670  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17671  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17672  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17673  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17674
17675  /* Thumb-only instructions.  */
17676 #undef ARM_VARIANT
17677 #define ARM_VARIANT NULL
17678   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
17679   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
17680
17681  /* ARM does not really have an IT instruction, so always allow it.
17682     The opcode is copied from Thumb in order to allow warnings in
17683     -mimplicit-it=[never | arm] modes.  */
17684 #undef  ARM_VARIANT
17685 #define ARM_VARIANT  & arm_ext_v1
17686
17687  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
17688  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
17689  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
17690  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
17691  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
17692  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
17693  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
17694  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
17695  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
17696  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
17697  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
17698  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
17699  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
17700  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
17701  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
17702  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
17703  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
17704  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
17705
17706  /* Thumb2 only instructions.  */
17707 #undef  ARM_VARIANT
17708 #define ARM_VARIANT  NULL
17709
17710  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
17711  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
17712  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
17713  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
17714  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
17715  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
17716
17717  /* Hardware division instructions.  */
17718 #undef  ARM_VARIANT
17719 #define ARM_VARIANT    & arm_ext_adiv
17720 #undef  THUMB_VARIANT
17721 #define THUMB_VARIANT  & arm_ext_div
17722
17723  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
17724  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
17725
17726  /* ARM V6M/V7 instructions.  */
17727 #undef  ARM_VARIANT
17728 #define ARM_VARIANT    & arm_ext_barrier
17729 #undef  THUMB_VARIANT
17730 #define THUMB_VARIANT  & arm_ext_barrier
17731
17732  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier,  t_barrier),
17733  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier,  t_barrier),
17734  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier,  t_barrier),
17735
17736  /* ARM V7 instructions.  */
17737 #undef  ARM_VARIANT
17738 #define ARM_VARIANT    & arm_ext_v7
17739 #undef  THUMB_VARIANT
17740 #define THUMB_VARIANT  & arm_ext_v7
17741
17742  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
17743  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
17744
17745 #undef ARM_VARIANT
17746 #define ARM_VARIANT    & arm_ext_mp
17747 #undef THUMB_VARIANT
17748 #define THUMB_VARIANT  & arm_ext_mp
17749
17750  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
17751
17752 #undef  ARM_VARIANT
17753 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
17754
17755  cCE("wfs",     e200110, 1, (RR),            rd),
17756  cCE("rfs",     e300110, 1, (RR),            rd),
17757  cCE("wfc",     e400110, 1, (RR),            rd),
17758  cCE("rfc",     e500110, 1, (RR),            rd),
17759
17760  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17761  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17762  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17763  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17764
17765  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17766  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17767  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17768  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17769
17770  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
17771  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
17772  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
17773  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
17774  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
17775  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
17776  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
17777  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
17778  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
17779  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
17780  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
17781  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
17782
17783  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
17784  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
17785  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
17786  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
17787  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
17788  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
17789  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
17790  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
17791  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
17792  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
17793  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
17794  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
17795
17796  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
17797  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
17798  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
17799  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
17800  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
17801  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
17802  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
17803  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
17804  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
17805  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
17806  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
17807  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
17808
17809  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
17810  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
17811  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
17812  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
17813  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
17814  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
17815  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
17816  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
17817  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
17818  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
17819  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
17820  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
17821
17822  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
17823  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
17824  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
17825  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
17826  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
17827  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
17828  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
17829  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
17830  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
17831  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
17832  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
17833  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
17834
17835  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
17836  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
17837  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
17838  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
17839  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
17840  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
17841  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
17842  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
17843  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
17844  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
17845  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
17846  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
17847
17848  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
17849  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
17850  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
17851  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
17852  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
17853  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
17854  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
17855  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
17856  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
17857  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
17858  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
17859  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
17860
17861  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
17862  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
17863  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
17864  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
17865  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
17866  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
17867  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
17868  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
17869  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
17870  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
17871  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
17872  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
17873
17874  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
17875  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
17876  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
17877  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
17878  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
17879  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
17880  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
17881  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
17882  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
17883  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
17884  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
17885  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
17886
17887  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
17888  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
17889  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
17890  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
17891  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
17892  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
17893  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
17894  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
17895  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
17896  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
17897  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
17898  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
17899
17900  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
17901  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
17902  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
17903  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
17904  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
17905  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
17906  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
17907  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
17908  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
17909  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
17910  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
17911  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
17912
17913  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
17914  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
17915  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
17916  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
17917  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
17918  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
17919  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
17920  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
17921  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
17922  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
17923  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
17924  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
17925
17926  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
17927  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
17928  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
17929  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
17930  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
17931  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
17932  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
17933  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
17934  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
17935  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
17936  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
17937  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
17938
17939  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
17940  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
17941  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
17942  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
17943  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
17944  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
17945  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
17946  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
17947  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
17948  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
17949  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
17950  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
17951
17952  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
17953  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
17954  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
17955  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
17956  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
17957  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
17958  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
17959  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
17960  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
17961  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
17962  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
17963  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
17964
17965  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
17966  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
17967  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
17968  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
17969  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
17970  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
17971  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
17972  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
17973  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
17974  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
17975  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
17976  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
17977
17978  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
17979  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
17980  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
17981  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
17982  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
17983  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17984  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17985  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17986  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
17987  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
17988  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
17989  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
17990
17991  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
17992  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
17993  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
17994  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
17995  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
17996  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17997  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17998  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17999  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
18000  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
18001  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
18002  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
18003
18004  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
18005  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
18006  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
18007  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
18008  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
18009  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18010  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18011  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18012  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
18013  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
18014  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
18015  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
18016
18017  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
18018  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
18019  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
18020  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
18021  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
18022  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18023  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18024  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18025  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
18026  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
18027  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
18028  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
18029
18030  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
18031  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
18032  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
18033  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
18034  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
18035  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18036  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18037  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18038  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
18039  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
18040  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
18041  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
18042
18043  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
18044  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
18045  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
18046  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
18047  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
18048  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18049  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18050  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18051  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
18052  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
18053  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
18054  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
18055
18056  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
18057  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
18058  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
18059  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
18060  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
18061  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18062  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18063  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18064  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
18065  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
18066  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
18067  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
18068
18069  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
18070  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
18071  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
18072  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
18073  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
18074  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18075  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18076  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18077  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
18078  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
18079  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
18080  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
18081
18082  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
18083  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
18084  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
18085  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
18086  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
18087  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18088  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18089  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18090  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
18091  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
18092  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
18093  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
18094
18095  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
18096  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
18097  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
18098  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
18099  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
18100  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18101  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18102  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18103  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
18104  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
18105  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
18106  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
18107
18108  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
18109  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
18110  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
18111  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
18112  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
18113  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18114  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18115  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18116  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
18117  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
18118  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
18119  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
18120
18121  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
18122  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
18123  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
18124  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
18125  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
18126  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18127  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18128  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18129  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
18130  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
18131  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
18132  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
18133
18134  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
18135  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
18136  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
18137  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
18138  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
18139  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
18140  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18141  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18142  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
18143  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
18144  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
18145  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
18146
18147  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
18148  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
18149  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
18150  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
18151
18152  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
18153  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
18154  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
18155  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
18156  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
18157  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
18158  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
18159  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
18160  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
18161  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
18162  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
18163  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
18164
18165   /* The implementation of the FIX instruction is broken on some
18166      assemblers, in that it accepts a precision specifier as well as a
18167      rounding specifier, despite the fact that this is meaningless.
18168      To be more compatible, we accept it as well, though of course it
18169      does not set any bits.  */
18170  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
18171  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
18172  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
18173  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
18174  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
18175  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
18176  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
18177  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
18178  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
18179  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
18180  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
18181  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
18182  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
18183
18184   /* Instructions that were new with the real FPA, call them V2.  */
18185 #undef  ARM_VARIANT
18186 #define ARM_VARIANT  & fpu_fpa_ext_v2
18187
18188  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18189  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18190  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18191  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18192  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18193  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18194
18195 #undef  ARM_VARIANT
18196 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
18197
18198   /* Moves and type conversions.  */
18199  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
18200  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
18201  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
18202  cCE("fmstat",  ef1fa10, 0, (),               noargs),
18203  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
18204  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
18205  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18206  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
18207  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
18208  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18209  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
18210  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18211  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
18212  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
18213
18214   /* Memory operations.  */
18215  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
18216  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
18217  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
18218  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
18219  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
18220  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
18221  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
18222  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
18223  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
18224  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
18225  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
18226  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
18227  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
18228  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
18229  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
18230  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
18231  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
18232  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
18233
18234   /* Monadic operations.  */
18235  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18236  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
18237  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18238
18239   /* Dyadic operations.  */
18240  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18241  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18242  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18243  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18244  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18245  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18246  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18247  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18248  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18249
18250   /* Comparisons.  */
18251  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
18252  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
18253  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18254  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
18255
18256  /* Double precision load/store are still present on single precision
18257     implementations.  */
18258  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
18259  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
18260  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18261  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18262  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18263  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18264  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18265  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18266  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18267  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18268
18269 #undef  ARM_VARIANT
18270 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
18271
18272   /* Moves and type conversions.  */
18273  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
18274  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
18275  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18276  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
18277  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
18278  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
18279  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
18280  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
18281  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
18282  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18283  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18284  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18285  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18286
18287   /* Monadic operations.  */
18288  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
18289  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
18290  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
18291
18292   /* Dyadic operations.  */
18293  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18294  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18295  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18296  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18297  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18298  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18299  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18300  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18301  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18302
18303   /* Comparisons.  */
18304  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
18305  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
18306  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
18307  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
18308
18309 #undef  ARM_VARIANT
18310 #define ARM_VARIANT  & fpu_vfp_ext_v2
18311
18312  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
18313  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
18314  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
18315  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
18316
18317 /* Instructions which may belong to either the Neon or VFP instruction sets.
18318    Individual encoder functions perform additional architecture checks.  */
18319 #undef  ARM_VARIANT
18320 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
18321 #undef  THUMB_VARIANT
18322 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
18323
18324   /* These mnemonics are unique to VFP.  */
18325  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
18326  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
18327  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18328  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18329  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18330  nCE(vcmp,      _vcmp,    2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
18331  nCE(vcmpe,     _vcmpe,   2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
18332  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
18333  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
18334  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
18335
18336   /* Mnemonics shared by Neon and VFP.  */
18337  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
18338  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
18339  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
18340
18341  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
18342  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
18343
18344  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
18345  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
18346
18347  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18348  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18349  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18350  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18351  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18352  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18353  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
18354  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
18355
18356  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
18357  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
18358  nCEF(vcvtb,    _vcvt,   2, (RVS, RVS), neon_cvtb),
18359  nCEF(vcvtt,    _vcvt,   2, (RVS, RVS), neon_cvtt),
18360
18361
18362   /* NOTE: All VMOV encoding is special-cased!  */
18363  NCE(vmov,      0,       1, (VMOV), neon_mov),
18364  NCE(vmovq,     0,       1, (VMOV), neon_mov),
18365
18366 #undef  THUMB_VARIANT
18367 #define THUMB_VARIANT  & fpu_neon_ext_v1
18368 #undef  ARM_VARIANT
18369 #define ARM_VARIANT    & fpu_neon_ext_v1
18370
18371   /* Data processing with three registers of the same length.  */
18372   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
18373  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
18374  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
18375  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
18376  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
18377  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
18378  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
18379  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
18380  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
18381   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
18382  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
18383  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
18384  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
18385  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
18386  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
18387  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
18388  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
18389  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
18390   /* If not immediate, fall back to neon_dyadic_i64_su.
18391      shl_imm should accept I8 I16 I32 I64,
18392      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
18393  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
18394  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
18395  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
18396  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
18397   /* Logic ops, types optional & ignored.  */
18398  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18399  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18400  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18401  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18402  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18403  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18404  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18405  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18406  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
18407  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
18408   /* Bitfield ops, untyped.  */
18409  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
18410  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
18411  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
18412  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
18413  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
18414  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
18415   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F32.  */
18416  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
18417  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
18418  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
18419  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
18420  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
18421  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
18422   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
18423      back to neon_dyadic_if_su.  */
18424  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
18425  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
18426  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
18427  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
18428  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
18429  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
18430  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
18431  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
18432   /* Comparison. Type I8 I16 I32 F32.  */
18433  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
18434  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
18435   /* As above, D registers only.  */
18436  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
18437  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
18438   /* Int and float variants, signedness unimportant.  */
18439  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
18440  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
18441  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
18442   /* Add/sub take types I8 I16 I32 I64 F32.  */
18443  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
18444  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
18445   /* vtst takes sizes 8, 16, 32.  */
18446  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
18447  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
18448   /* VMUL takes I8 I16 I32 F32 P8.  */
18449  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
18450   /* VQD{R}MULH takes S16 S32.  */
18451  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
18452  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
18453  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
18454  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
18455  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
18456  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
18457  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
18458  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
18459  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
18460  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
18461  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
18462  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
18463  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
18464  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
18465  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
18466  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
18467
18468   /* Two address, int/float. Types S8 S16 S32 F32.  */
18469  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
18470  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
18471
18472   /* Data processing with two registers and a shift amount.  */
18473   /* Right shifts, and variants with rounding.
18474      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
18475  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
18476  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
18477  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
18478  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
18479  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
18480  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
18481  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
18482  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
18483   /* Shift and insert. Sizes accepted 8 16 32 64.  */
18484  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
18485  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
18486  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
18487  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
18488   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
18489  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
18490  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
18491   /* Right shift immediate, saturating & narrowing, with rounding variants.
18492      Types accepted S16 S32 S64 U16 U32 U64.  */
18493  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
18494  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
18495   /* As above, unsigned. Types accepted S16 S32 S64.  */
18496  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
18497  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
18498   /* Right shift narrowing. Types accepted I16 I32 I64.  */
18499  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
18500  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
18501   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
18502  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
18503   /* CVT with optional immediate for fixed-point variant.  */
18504  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
18505
18506  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
18507  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
18508
18509   /* Data processing, three registers of different lengths.  */
18510   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
18511  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
18512  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
18513  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
18514  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
18515   /* If not scalar, fall back to neon_dyadic_long.
18516      Vector types as above, scalar types S16 S32 U16 U32.  */
18517  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
18518  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
18519   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
18520  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
18521  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
18522   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
18523  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18524  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18525  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18526  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18527   /* Saturating doubling multiplies. Types S16 S32.  */
18528  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
18529  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
18530  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
18531   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
18532      S16 S32 U16 U32.  */
18533  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
18534
18535   /* Extract. Size 8.  */
18536  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
18537  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
18538
18539   /* Two registers, miscellaneous.  */
18540   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
18541  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
18542  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
18543  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
18544  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
18545  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
18546  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
18547   /* Vector replicate. Sizes 8 16 32.  */
18548  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
18549  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
18550   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
18551  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
18552   /* VMOVN. Types I16 I32 I64.  */
18553  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
18554   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
18555  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
18556   /* VQMOVUN. Types S16 S32 S64.  */
18557  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
18558   /* VZIP / VUZP. Sizes 8 16 32.  */
18559  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
18560  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
18561  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
18562  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
18563   /* VQABS / VQNEG. Types S8 S16 S32.  */
18564  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
18565  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
18566  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
18567  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
18568   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
18569  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
18570  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
18571  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
18572  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
18573   /* Reciprocal estimates. Types U32 F32.  */
18574  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
18575  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
18576  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
18577  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
18578   /* VCLS. Types S8 S16 S32.  */
18579  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
18580  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
18581   /* VCLZ. Types I8 I16 I32.  */
18582  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
18583  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
18584   /* VCNT. Size 8.  */
18585  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
18586  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
18587   /* Two address, untyped.  */
18588  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
18589  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
18590   /* VTRN. Sizes 8 16 32.  */
18591  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
18592  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
18593
18594   /* Table lookup. Size 8.  */
18595  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
18596  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
18597
18598 #undef  THUMB_VARIANT
18599 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
18600 #undef  ARM_VARIANT
18601 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
18602
18603   /* Neon element/structure load/store.  */
18604  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18605  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18606  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18607  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18608  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18609  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18610  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18611  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18612
18613 #undef  THUMB_VARIANT
18614 #define THUMB_VARIANT &fpu_vfp_ext_v3xd
18615 #undef ARM_VARIANT
18616 #define ARM_VARIANT &fpu_vfp_ext_v3xd
18617  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
18618  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18619  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18620  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18621  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18622  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18623  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18624  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18625  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18626
18627 #undef THUMB_VARIANT
18628 #define THUMB_VARIANT  & fpu_vfp_ext_v3
18629 #undef  ARM_VARIANT
18630 #define ARM_VARIANT    & fpu_vfp_ext_v3
18631
18632  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
18633  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18634  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18635  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18636  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18637  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18638  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18639  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18640  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18641
18642 #undef ARM_VARIANT
18643 #define ARM_VARIANT &fpu_vfp_ext_fma
18644 #undef THUMB_VARIANT
18645 #define THUMB_VARIANT &fpu_vfp_ext_fma
18646  /* Mnemonics shared by Neon and VFP.  These are included in the
18647     VFP FMA variant; NEON and VFP FMA always includes the NEON
18648     FMA instructions.  */
18649  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
18650  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
18651  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
18652     the v form should always be used.  */
18653  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18654  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18655  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18656  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18657  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18658  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18659
18660 #undef THUMB_VARIANT
18661 #undef  ARM_VARIANT
18662 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
18663
18664  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18665  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18666  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18667  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18668  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18669  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18670  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
18671  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
18672
18673 #undef  ARM_VARIANT
18674 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
18675
18676  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
18677  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
18678  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
18679  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
18680  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
18681  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
18682  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
18683  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
18684  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
18685  cCE("textrmub",        e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18686  cCE("textrmuh",        e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18687  cCE("textrmuw",        e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18688  cCE("textrmsb",        e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18689  cCE("textrmsh",        e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18690  cCE("textrmsw",        e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18691  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
18692  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
18693  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
18694  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
18695  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
18696  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18697  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18698  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18699  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18700  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18701  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18702  cCE("tmovmskb",        e100030, 2, (RR, RIWR),             rd_rn),
18703  cCE("tmovmskh",        e500030, 2, (RR, RIWR),             rd_rn),
18704  cCE("tmovmskw",        e900030, 2, (RR, RIWR),             rd_rn),
18705  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
18706  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
18707  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
18708  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
18709  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
18710  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
18711  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
18712  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
18713  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18714  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18715  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18716  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18717  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18718  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18719  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18720  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18721  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18722  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
18723  cCE("walignr0",        e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18724  cCE("walignr1",        e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18725  cCE("walignr2",        ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18726  cCE("walignr3",        eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18727  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18728  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18729  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18730  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18731  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18732  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18733  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18734  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18735  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18736  cCE("wcmpgtub",        e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18737  cCE("wcmpgtuh",        e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18738  cCE("wcmpgtuw",        e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18739  cCE("wcmpgtsb",        e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18740  cCE("wcmpgtsh",        e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18741  cCE("wcmpgtsw",        eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18742  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18743  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18744  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
18745  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
18746  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18747  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18748  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18749  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18750  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18751  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18752  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18753  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18754  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18755  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18756  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18757  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18758  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18759  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18760  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18761  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18762  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18763  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18764  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
18765  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18766  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18767  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18768  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18769  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18770  cCE("wpackhss",        e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18771  cCE("wpackhus",        e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18772  cCE("wpackwss",        eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18773  cCE("wpackwus",        e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18774  cCE("wpackdss",        ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18775  cCE("wpackdus",        ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18776  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18777  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18778  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18779  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18780  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18781  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18782  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18783  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18784  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18785  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18786  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
18787  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18788  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18789  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18790  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18791  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18792  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18793  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18794  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18795  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18796  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18797  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18798  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18799  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18800  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18801  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18802  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18803  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18804  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18805  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18806  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18807  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
18808  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
18809  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18810  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18811  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18812  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18813  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18814  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18815  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18816  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18817  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18818  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
18819  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
18820  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
18821  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
18822  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
18823  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
18824  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18825  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18826  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18827  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
18828  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
18829  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
18830  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
18831  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
18832  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
18833  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18834  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18835  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18836  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18837  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
18838
18839 #undef  ARM_VARIANT
18840 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
18841
18842  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
18843  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
18844  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
18845  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
18846  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
18847  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
18848  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18849  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18850  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18851  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18852  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18853  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18854  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18855  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18856  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18857  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18858  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18859  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18860  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18861  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18862  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
18863  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18864  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18865  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18866  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18867  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18868  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18869  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18870  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18871  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18872  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18873  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18874  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18875  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18876  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18877  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18878  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18879  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18880  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18881  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18882  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18883  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18884  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18885  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18886  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18887  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18888  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18889  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18890  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18891  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18892  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18893  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18894  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18895  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18896  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18897  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18898  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18899
18900 #undef  ARM_VARIANT
18901 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
18902
18903  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
18904  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
18905  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
18906  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
18907  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
18908  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
18909  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
18910  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
18911  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
18912  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
18913  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
18914  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
18915  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
18916  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
18917  cCE("cfmv64lr",        e000510, 2, (RMDX, RR),               rn_rd),
18918  cCE("cfmvr64l",        e100510, 2, (RR, RMDX),               rd_rn),
18919  cCE("cfmv64hr",        e000530, 2, (RMDX, RR),               rn_rd),
18920  cCE("cfmvr64h",        e100530, 2, (RR, RMDX),               rd_rn),
18921  cCE("cfmval32",        e200440, 2, (RMAX, RMFX),             rd_rn),
18922  cCE("cfmv32al",        e100440, 2, (RMFX, RMAX),             rd_rn),
18923  cCE("cfmvam32",        e200460, 2, (RMAX, RMFX),             rd_rn),
18924  cCE("cfmv32am",        e100460, 2, (RMFX, RMAX),             rd_rn),
18925  cCE("cfmvah32",        e200480, 2, (RMAX, RMFX),             rd_rn),
18926  cCE("cfmv32ah",        e100480, 2, (RMFX, RMAX),             rd_rn),
18927  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
18928  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
18929  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
18930  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
18931  cCE("cfmvsc32",        e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
18932  cCE("cfmv32sc",        e1004e0, 2, (RMDX, RMDS),             rd),
18933  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
18934  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
18935  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
18936  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
18937  cCE("cfcvt32s",        e000480, 2, (RMF, RMFX),              rd_rn),
18938  cCE("cfcvt32d",        e0004a0, 2, (RMD, RMFX),              rd_rn),
18939  cCE("cfcvt64s",        e0004c0, 2, (RMF, RMDX),              rd_rn),
18940  cCE("cfcvt64d",        e0004e0, 2, (RMD, RMDX),              rd_rn),
18941  cCE("cfcvts32",        e100580, 2, (RMFX, RMF),              rd_rn),
18942  cCE("cfcvtd32",        e1005a0, 2, (RMFX, RMD),              rd_rn),
18943  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
18944  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
18945  cCE("cfrshl32",        e000550, 3, (RMFX, RMFX, RR),         mav_triple),
18946  cCE("cfrshl64",        e000570, 3, (RMDX, RMDX, RR),         mav_triple),
18947  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
18948  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
18949  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
18950  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
18951  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
18952  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
18953  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
18954  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
18955  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
18956  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
18957  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
18958  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
18959  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
18960  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
18961  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
18962  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
18963  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
18964  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
18965  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
18966  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
18967  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18968  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
18969  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18970  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
18971  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18972  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
18973  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18974  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18975  cCE("cfmadd32",        e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
18976  cCE("cfmsub32",        e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
18977  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
18978  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
18979 };
18980 #undef ARM_VARIANT
18981 #undef THUMB_VARIANT
18982 #undef TCE
18983 #undef TCM
18984 #undef TUE
18985 #undef TUF
18986 #undef TCC
18987 #undef cCE
18988 #undef cCL
18989 #undef C3E
18990 #undef CE
18991 #undef CM
18992 #undef UE
18993 #undef UF
18994 #undef UT
18995 #undef NUF
18996 #undef nUF
18997 #undef NCE
18998 #undef nCE
18999 #undef OPS0
19000 #undef OPS1
19001 #undef OPS2
19002 #undef OPS3
19003 #undef OPS4
19004 #undef OPS5
19005 #undef OPS6
19006 #undef do_0
19007 \f
19008 /* MD interface: bits in the object file.  */
19009
19010 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
19011    for use in the a.out file, and stores them in the array pointed to by buf.
19012    This knows about the endian-ness of the target machine and does
19013    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
19014    2 (short) and 4 (long)  Floating numbers are put out as a series of
19015    LITTLENUMS (shorts, here at least).  */
19016
19017 void
19018 md_number_to_chars (char * buf, valueT val, int n)
19019 {
19020   if (target_big_endian)
19021     number_to_chars_bigendian (buf, val, n);
19022   else
19023     number_to_chars_littleendian (buf, val, n);
19024 }
19025
19026 static valueT
19027 md_chars_to_number (char * buf, int n)
19028 {
19029   valueT result = 0;
19030   unsigned char * where = (unsigned char *) buf;
19031
19032   if (target_big_endian)
19033     {
19034       while (n--)
19035         {
19036           result <<= 8;
19037           result |= (*where++ & 255);
19038         }
19039     }
19040   else
19041     {
19042       while (n--)
19043         {
19044           result <<= 8;
19045           result |= (where[n] & 255);
19046         }
19047     }
19048
19049   return result;
19050 }
19051
19052 /* MD interface: Sections.  */
19053
19054 /* Calculate the maximum variable size (i.e., excluding fr_fix)
19055    that an rs_machine_dependent frag may reach.  */
19056
19057 unsigned int
19058 arm_frag_max_var (fragS *fragp)
19059 {
19060   /* We only use rs_machine_dependent for variable-size Thumb instructions,
19061      which are either THUMB_SIZE (2) or INSN_SIZE (4).
19062
19063      Note that we generate relaxable instructions even for cases that don't
19064      really need it, like an immediate that's a trivial constant.  So we're
19065      overestimating the instruction size for some of those cases.  Rather
19066      than putting more intelligence here, it would probably be better to
19067      avoid generating a relaxation frag in the first place when it can be
19068      determined up front that a short instruction will suffice.  */
19069
19070   gas_assert (fragp->fr_type == rs_machine_dependent);
19071   return INSN_SIZE;
19072 }
19073
19074 /* Estimate the size of a frag before relaxing.  Assume everything fits in
19075    2 bytes.  */
19076
19077 int
19078 md_estimate_size_before_relax (fragS * fragp,
19079                                segT    segtype ATTRIBUTE_UNUSED)
19080 {
19081   fragp->fr_var = 2;
19082   return 2;
19083 }
19084
19085 /* Convert a machine dependent frag.  */
19086
19087 void
19088 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
19089 {
19090   unsigned long insn;
19091   unsigned long old_op;
19092   char *buf;
19093   expressionS exp;
19094   fixS *fixp;
19095   int reloc_type;
19096   int pc_rel;
19097   int opcode;
19098
19099   buf = fragp->fr_literal + fragp->fr_fix;
19100
19101   old_op = bfd_get_16(abfd, buf);
19102   if (fragp->fr_symbol)
19103     {
19104       exp.X_op = O_symbol;
19105       exp.X_add_symbol = fragp->fr_symbol;
19106     }
19107   else
19108     {
19109       exp.X_op = O_constant;
19110     }
19111   exp.X_add_number = fragp->fr_offset;
19112   opcode = fragp->fr_subtype;
19113   switch (opcode)
19114     {
19115     case T_MNEM_ldr_pc:
19116     case T_MNEM_ldr_pc2:
19117     case T_MNEM_ldr_sp:
19118     case T_MNEM_str_sp:
19119     case T_MNEM_ldr:
19120     case T_MNEM_ldrb:
19121     case T_MNEM_ldrh:
19122     case T_MNEM_str:
19123     case T_MNEM_strb:
19124     case T_MNEM_strh:
19125       if (fragp->fr_var == 4)
19126         {
19127           insn = THUMB_OP32 (opcode);
19128           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
19129             {
19130               insn |= (old_op & 0x700) << 4;
19131             }
19132           else
19133             {
19134               insn |= (old_op & 7) << 12;
19135               insn |= (old_op & 0x38) << 13;
19136             }
19137           insn |= 0x00000c00;
19138           put_thumb32_insn (buf, insn);
19139           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
19140         }
19141       else
19142         {
19143           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
19144         }
19145       pc_rel = (opcode == T_MNEM_ldr_pc2);
19146       break;
19147     case T_MNEM_adr:
19148       if (fragp->fr_var == 4)
19149         {
19150           insn = THUMB_OP32 (opcode);
19151           insn |= (old_op & 0xf0) << 4;
19152           put_thumb32_insn (buf, insn);
19153           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
19154         }
19155       else
19156         {
19157           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
19158           exp.X_add_number -= 4;
19159         }
19160       pc_rel = 1;
19161       break;
19162     case T_MNEM_mov:
19163     case T_MNEM_movs:
19164     case T_MNEM_cmp:
19165     case T_MNEM_cmn:
19166       if (fragp->fr_var == 4)
19167         {
19168           int r0off = (opcode == T_MNEM_mov
19169                        || opcode == T_MNEM_movs) ? 0 : 8;
19170           insn = THUMB_OP32 (opcode);
19171           insn = (insn & 0xe1ffffff) | 0x10000000;
19172           insn |= (old_op & 0x700) << r0off;
19173           put_thumb32_insn (buf, insn);
19174           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
19175         }
19176       else
19177         {
19178           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
19179         }
19180       pc_rel = 0;
19181       break;
19182     case T_MNEM_b:
19183       if (fragp->fr_var == 4)
19184         {
19185           insn = THUMB_OP32(opcode);
19186           put_thumb32_insn (buf, insn);
19187           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
19188         }
19189       else
19190         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
19191       pc_rel = 1;
19192       break;
19193     case T_MNEM_bcond:
19194       if (fragp->fr_var == 4)
19195         {
19196           insn = THUMB_OP32(opcode);
19197           insn |= (old_op & 0xf00) << 14;
19198           put_thumb32_insn (buf, insn);
19199           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
19200         }
19201       else
19202         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
19203       pc_rel = 1;
19204       break;
19205     case T_MNEM_add_sp:
19206     case T_MNEM_add_pc:
19207     case T_MNEM_inc_sp:
19208     case T_MNEM_dec_sp:
19209       if (fragp->fr_var == 4)
19210         {
19211           /* ??? Choose between add and addw.  */
19212           insn = THUMB_OP32 (opcode);
19213           insn |= (old_op & 0xf0) << 4;
19214           put_thumb32_insn (buf, insn);
19215           if (opcode == T_MNEM_add_pc)
19216             reloc_type = BFD_RELOC_ARM_T32_IMM12;
19217           else
19218             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
19219         }
19220       else
19221         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
19222       pc_rel = 0;
19223       break;
19224
19225     case T_MNEM_addi:
19226     case T_MNEM_addis:
19227     case T_MNEM_subi:
19228     case T_MNEM_subis:
19229       if (fragp->fr_var == 4)
19230         {
19231           insn = THUMB_OP32 (opcode);
19232           insn |= (old_op & 0xf0) << 4;
19233           insn |= (old_op & 0xf) << 16;
19234           put_thumb32_insn (buf, insn);
19235           if (insn & (1 << 20))
19236             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
19237           else
19238             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
19239         }
19240       else
19241         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
19242       pc_rel = 0;
19243       break;
19244     default:
19245       abort ();
19246     }
19247   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
19248                       (enum bfd_reloc_code_real) reloc_type);
19249   fixp->fx_file = fragp->fr_file;
19250   fixp->fx_line = fragp->fr_line;
19251   fragp->fr_fix += fragp->fr_var;
19252 }
19253
19254 /* Return the size of a relaxable immediate operand instruction.
19255    SHIFT and SIZE specify the form of the allowable immediate.  */
19256 static int
19257 relax_immediate (fragS *fragp, int size, int shift)
19258 {
19259   offsetT offset;
19260   offsetT mask;
19261   offsetT low;
19262
19263   /* ??? Should be able to do better than this.  */
19264   if (fragp->fr_symbol)
19265     return 4;
19266
19267   low = (1 << shift) - 1;
19268   mask = (1 << (shift + size)) - (1 << shift);
19269   offset = fragp->fr_offset;
19270   /* Force misaligned offsets to 32-bit variant.  */
19271   if (offset & low)
19272     return 4;
19273   if (offset & ~mask)
19274     return 4;
19275   return 2;
19276 }
19277
19278 /* Get the address of a symbol during relaxation.  */
19279 static addressT
19280 relaxed_symbol_addr (fragS *fragp, long stretch)
19281 {
19282   fragS *sym_frag;
19283   addressT addr;
19284   symbolS *sym;
19285
19286   sym = fragp->fr_symbol;
19287   sym_frag = symbol_get_frag (sym);
19288   know (S_GET_SEGMENT (sym) != absolute_section
19289         || sym_frag == &zero_address_frag);
19290   addr = S_GET_VALUE (sym) + fragp->fr_offset;
19291
19292   /* If frag has yet to be reached on this pass, assume it will
19293      move by STRETCH just as we did.  If this is not so, it will
19294      be because some frag between grows, and that will force
19295      another pass.  */
19296
19297   if (stretch != 0
19298       && sym_frag->relax_marker != fragp->relax_marker)
19299     {
19300       fragS *f;
19301
19302       /* Adjust stretch for any alignment frag.  Note that if have
19303          been expanding the earlier code, the symbol may be
19304          defined in what appears to be an earlier frag.  FIXME:
19305          This doesn't handle the fr_subtype field, which specifies
19306          a maximum number of bytes to skip when doing an
19307          alignment.  */
19308       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
19309         {
19310           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
19311             {
19312               if (stretch < 0)
19313                 stretch = - ((- stretch)
19314                              & ~ ((1 << (int) f->fr_offset) - 1));
19315               else
19316                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
19317               if (stretch == 0)
19318                 break;
19319             }
19320         }
19321       if (f != NULL)
19322         addr += stretch;
19323     }
19324
19325   return addr;
19326 }
19327
19328 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
19329    load.  */
19330 static int
19331 relax_adr (fragS *fragp, asection *sec, long stretch)
19332 {
19333   addressT addr;
19334   offsetT val;
19335
19336   /* Assume worst case for symbols not known to be in the same section.  */
19337   if (fragp->fr_symbol == NULL
19338       || !S_IS_DEFINED (fragp->fr_symbol)
19339       || sec != S_GET_SEGMENT (fragp->fr_symbol)
19340       || S_IS_WEAK (fragp->fr_symbol))
19341     return 4;
19342
19343   val = relaxed_symbol_addr (fragp, stretch);
19344   addr = fragp->fr_address + fragp->fr_fix;
19345   addr = (addr + 4) & ~3;
19346   /* Force misaligned targets to 32-bit variant.  */
19347   if (val & 3)
19348     return 4;
19349   val -= addr;
19350   if (val < 0 || val > 1020)
19351     return 4;
19352   return 2;
19353 }
19354
19355 /* Return the size of a relaxable add/sub immediate instruction.  */
19356 static int
19357 relax_addsub (fragS *fragp, asection *sec)
19358 {
19359   char *buf;
19360   int op;
19361
19362   buf = fragp->fr_literal + fragp->fr_fix;
19363   op = bfd_get_16(sec->owner, buf);
19364   if ((op & 0xf) == ((op >> 4) & 0xf))
19365     return relax_immediate (fragp, 8, 0);
19366   else
19367     return relax_immediate (fragp, 3, 0);
19368 }
19369
19370
19371 /* Return the size of a relaxable branch instruction.  BITS is the
19372    size of the offset field in the narrow instruction.  */
19373
19374 static int
19375 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
19376 {
19377   addressT addr;
19378   offsetT val;
19379   offsetT limit;
19380
19381   /* Assume worst case for symbols not known to be in the same section.  */
19382   if (!S_IS_DEFINED (fragp->fr_symbol)
19383       || sec != S_GET_SEGMENT (fragp->fr_symbol)
19384       || S_IS_WEAK (fragp->fr_symbol))
19385     return 4;
19386
19387 #ifdef OBJ_ELF
19388   if (S_IS_DEFINED (fragp->fr_symbol)
19389       && ARM_IS_FUNC (fragp->fr_symbol))
19390       return 4;
19391
19392   /* PR 12532.  Global symbols with default visibility might
19393      be preempted, so do not relax relocations to them.  */
19394   if ((ELF_ST_VISIBILITY (S_GET_OTHER (fragp->fr_symbol)) == STV_DEFAULT)
19395       && (! S_IS_LOCAL (fragp->fr_symbol)))
19396     return 4;
19397 #endif
19398
19399   val = relaxed_symbol_addr (fragp, stretch);
19400   addr = fragp->fr_address + fragp->fr_fix + 4;
19401   val -= addr;
19402
19403   /* Offset is a signed value *2 */
19404   limit = 1 << bits;
19405   if (val >= limit || val < -limit)
19406     return 4;
19407   return 2;
19408 }
19409
19410
19411 /* Relax a machine dependent frag.  This returns the amount by which
19412    the current size of the frag should change.  */
19413
19414 int
19415 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
19416 {
19417   int oldsize;
19418   int newsize;
19419
19420   oldsize = fragp->fr_var;
19421   switch (fragp->fr_subtype)
19422     {
19423     case T_MNEM_ldr_pc2:
19424       newsize = relax_adr (fragp, sec, stretch);
19425       break;
19426     case T_MNEM_ldr_pc:
19427     case T_MNEM_ldr_sp:
19428     case T_MNEM_str_sp:
19429       newsize = relax_immediate (fragp, 8, 2);
19430       break;
19431     case T_MNEM_ldr:
19432     case T_MNEM_str:
19433       newsize = relax_immediate (fragp, 5, 2);
19434       break;
19435     case T_MNEM_ldrh:
19436     case T_MNEM_strh:
19437       newsize = relax_immediate (fragp, 5, 1);
19438       break;
19439     case T_MNEM_ldrb:
19440     case T_MNEM_strb:
19441       newsize = relax_immediate (fragp, 5, 0);
19442       break;
19443     case T_MNEM_adr:
19444       newsize = relax_adr (fragp, sec, stretch);
19445       break;
19446     case T_MNEM_mov:
19447     case T_MNEM_movs:
19448     case T_MNEM_cmp:
19449     case T_MNEM_cmn:
19450       newsize = relax_immediate (fragp, 8, 0);
19451       break;
19452     case T_MNEM_b:
19453       newsize = relax_branch (fragp, sec, 11, stretch);
19454       break;
19455     case T_MNEM_bcond:
19456       newsize = relax_branch (fragp, sec, 8, stretch);
19457       break;
19458     case T_MNEM_add_sp:
19459     case T_MNEM_add_pc:
19460       newsize = relax_immediate (fragp, 8, 2);
19461       break;
19462     case T_MNEM_inc_sp:
19463     case T_MNEM_dec_sp:
19464       newsize = relax_immediate (fragp, 7, 2);
19465       break;
19466     case T_MNEM_addi:
19467     case T_MNEM_addis:
19468     case T_MNEM_subi:
19469     case T_MNEM_subis:
19470       newsize = relax_addsub (fragp, sec);
19471       break;
19472     default:
19473       abort ();
19474     }
19475
19476   fragp->fr_var = newsize;
19477   /* Freeze wide instructions that are at or before the same location as
19478      in the previous pass.  This avoids infinite loops.
19479      Don't freeze them unconditionally because targets may be artificially
19480      misaligned by the expansion of preceding frags.  */
19481   if (stretch <= 0 && newsize > 2)
19482     {
19483       md_convert_frag (sec->owner, sec, fragp);
19484       frag_wane (fragp);
19485     }
19486
19487   return newsize - oldsize;
19488 }
19489
19490 /* Round up a section size to the appropriate boundary.  */
19491
19492 valueT
19493 md_section_align (segT   segment ATTRIBUTE_UNUSED,
19494                   valueT size)
19495 {
19496 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
19497   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
19498     {
19499       /* For a.out, force the section size to be aligned.  If we don't do
19500          this, BFD will align it for us, but it will not write out the
19501          final bytes of the section.  This may be a bug in BFD, but it is
19502          easier to fix it here since that is how the other a.out targets
19503          work.  */
19504       int align;
19505
19506       align = bfd_get_section_alignment (stdoutput, segment);
19507       size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
19508     }
19509 #endif
19510
19511   return size;
19512 }
19513
19514 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
19515    of an rs_align_code fragment.  */
19516
19517 void
19518 arm_handle_align (fragS * fragP)
19519 {
19520   static char const arm_noop[2][2][4] =
19521     {
19522       {  /* ARMv1 */
19523         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
19524         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
19525       },
19526       {  /* ARMv6k */
19527         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
19528         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
19529       },
19530     };
19531   static char const thumb_noop[2][2][2] =
19532     {
19533       {  /* Thumb-1 */
19534         {0xc0, 0x46},  /* LE */
19535         {0x46, 0xc0},  /* BE */
19536       },
19537       {  /* Thumb-2 */
19538         {0x00, 0xbf},  /* LE */
19539         {0xbf, 0x00}   /* BE */
19540       }
19541     };
19542   static char const wide_thumb_noop[2][4] =
19543     {  /* Wide Thumb-2 */
19544       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
19545       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
19546     };
19547
19548   unsigned bytes, fix, noop_size;
19549   char * p;
19550   const char * noop;
19551   const char *narrow_noop = NULL;
19552 #ifdef OBJ_ELF
19553   enum mstate state;
19554 #endif
19555
19556   if (fragP->fr_type != rs_align_code)
19557     return;
19558
19559   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
19560   p = fragP->fr_literal + fragP->fr_fix;
19561   fix = 0;
19562
19563   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
19564     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
19565
19566   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
19567
19568   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
19569     {
19570       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
19571         {
19572           narrow_noop = thumb_noop[1][target_big_endian];
19573           noop = wide_thumb_noop[target_big_endian];
19574         }
19575       else
19576         noop = thumb_noop[0][target_big_endian];
19577       noop_size = 2;
19578 #ifdef OBJ_ELF
19579       state = MAP_THUMB;
19580 #endif
19581     }
19582   else
19583     {
19584       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k) != 0]
19585                      [target_big_endian];
19586       noop_size = 4;
19587 #ifdef OBJ_ELF
19588       state = MAP_ARM;
19589 #endif
19590     }
19591
19592   fragP->fr_var = noop_size;
19593
19594   if (bytes & (noop_size - 1))
19595     {
19596       fix = bytes & (noop_size - 1);
19597 #ifdef OBJ_ELF
19598       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
19599 #endif
19600       memset (p, 0, fix);
19601       p += fix;
19602       bytes -= fix;
19603     }
19604
19605   if (narrow_noop)
19606     {
19607       if (bytes & noop_size)
19608         {
19609           /* Insert a narrow noop.  */
19610           memcpy (p, narrow_noop, noop_size);
19611           p += noop_size;
19612           bytes -= noop_size;
19613           fix += noop_size;
19614         }
19615
19616       /* Use wide noops for the remainder */
19617       noop_size = 4;
19618     }
19619
19620   while (bytes >= noop_size)
19621     {
19622       memcpy (p, noop, noop_size);
19623       p += noop_size;
19624       bytes -= noop_size;
19625       fix += noop_size;
19626     }
19627
19628   fragP->fr_fix += fix;
19629 }
19630
19631 /* Called from md_do_align.  Used to create an alignment
19632    frag in a code section.  */
19633
19634 void
19635 arm_frag_align_code (int n, int max)
19636 {
19637   char * p;
19638
19639   /* We assume that there will never be a requirement
19640      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
19641   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
19642     {
19643       char err_msg[128];
19644
19645       sprintf (err_msg,
19646         _("alignments greater than %d bytes not supported in .text sections."),
19647         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
19648       as_fatal ("%s", err_msg);
19649     }
19650
19651   p = frag_var (rs_align_code,
19652                 MAX_MEM_FOR_RS_ALIGN_CODE,
19653                 1,
19654                 (relax_substateT) max,
19655                 (symbolS *) NULL,
19656                 (offsetT) n,
19657                 (char *) NULL);
19658   *p = 0;
19659 }
19660
19661 /* Perform target specific initialisation of a frag.
19662    Note - despite the name this initialisation is not done when the frag
19663    is created, but only when its type is assigned.  A frag can be created
19664    and used a long time before its type is set, so beware of assuming that
19665    this initialisationis performed first.  */
19666
19667 #ifndef OBJ_ELF
19668 void
19669 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
19670 {
19671   /* Record whether this frag is in an ARM or a THUMB area.  */
19672   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
19673 }
19674
19675 #else /* OBJ_ELF is defined.  */
19676 void
19677 arm_init_frag (fragS * fragP, int max_chars)
19678 {
19679   /* If the current ARM vs THUMB mode has not already
19680      been recorded into this frag then do so now.  */
19681   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
19682     {
19683       fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
19684
19685       /* Record a mapping symbol for alignment frags.  We will delete this
19686          later if the alignment ends up empty.  */
19687       switch (fragP->fr_type)
19688         {
19689           case rs_align:
19690           case rs_align_test:
19691           case rs_fill:
19692             mapping_state_2 (MAP_DATA, max_chars);
19693             break;
19694           case rs_align_code:
19695             mapping_state_2 (thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
19696             break;
19697           default:
19698             break;
19699         }
19700     }
19701 }
19702
19703 /* When we change sections we need to issue a new mapping symbol.  */
19704
19705 void
19706 arm_elf_change_section (void)
19707 {
19708   /* Link an unlinked unwind index table section to the .text section.  */
19709   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
19710       && elf_linked_to_section (now_seg) == NULL)
19711     elf_linked_to_section (now_seg) = text_section;
19712 }
19713
19714 int
19715 arm_elf_section_type (const char * str, size_t len)
19716 {
19717   if (len == 5 && strncmp (str, "exidx", 5) == 0)
19718     return SHT_ARM_EXIDX;
19719
19720   return -1;
19721 }
19722 \f
19723 /* Code to deal with unwinding tables.  */
19724
19725 static void add_unwind_adjustsp (offsetT);
19726
19727 /* Generate any deferred unwind frame offset.  */
19728
19729 static void
19730 flush_pending_unwind (void)
19731 {
19732   offsetT offset;
19733
19734   offset = unwind.pending_offset;
19735   unwind.pending_offset = 0;
19736   if (offset != 0)
19737     add_unwind_adjustsp (offset);
19738 }
19739
19740 /* Add an opcode to this list for this function.  Two-byte opcodes should
19741    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
19742    order.  */
19743
19744 static void
19745 add_unwind_opcode (valueT op, int length)
19746 {
19747   /* Add any deferred stack adjustment.  */
19748   if (unwind.pending_offset)
19749     flush_pending_unwind ();
19750
19751   unwind.sp_restored = 0;
19752
19753   if (unwind.opcode_count + length > unwind.opcode_alloc)
19754     {
19755       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
19756       if (unwind.opcodes)
19757         unwind.opcodes = (unsigned char *) xrealloc (unwind.opcodes,
19758                                                      unwind.opcode_alloc);
19759       else
19760         unwind.opcodes = (unsigned char *) xmalloc (unwind.opcode_alloc);
19761     }
19762   while (length > 0)
19763     {
19764       length--;
19765       unwind.opcodes[unwind.opcode_count] = op & 0xff;
19766       op >>= 8;
19767       unwind.opcode_count++;
19768     }
19769 }
19770
19771 /* Add unwind opcodes to adjust the stack pointer.  */
19772
19773 static void
19774 add_unwind_adjustsp (offsetT offset)
19775 {
19776   valueT op;
19777
19778   if (offset > 0x200)
19779     {
19780       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
19781       char bytes[5];
19782       int n;
19783       valueT o;
19784
19785       /* Long form: 0xb2, uleb128.  */
19786       /* This might not fit in a word so add the individual bytes,
19787          remembering the list is built in reverse order.  */
19788       o = (valueT) ((offset - 0x204) >> 2);
19789       if (o == 0)
19790         add_unwind_opcode (0, 1);
19791
19792       /* Calculate the uleb128 encoding of the offset.  */
19793       n = 0;
19794       while (o)
19795         {
19796           bytes[n] = o & 0x7f;
19797           o >>= 7;
19798           if (o)
19799             bytes[n] |= 0x80;
19800           n++;
19801         }
19802       /* Add the insn.  */
19803       for (; n; n--)
19804         add_unwind_opcode (bytes[n - 1], 1);
19805       add_unwind_opcode (0xb2, 1);
19806     }
19807   else if (offset > 0x100)
19808     {
19809       /* Two short opcodes.  */
19810       add_unwind_opcode (0x3f, 1);
19811       op = (offset - 0x104) >> 2;
19812       add_unwind_opcode (op, 1);
19813     }
19814   else if (offset > 0)
19815     {
19816       /* Short opcode.  */
19817       op = (offset - 4) >> 2;
19818       add_unwind_opcode (op, 1);
19819     }
19820   else if (offset < 0)
19821     {
19822       offset = -offset;
19823       while (offset > 0x100)
19824         {
19825           add_unwind_opcode (0x7f, 1);
19826           offset -= 0x100;
19827         }
19828       op = ((offset - 4) >> 2) | 0x40;
19829       add_unwind_opcode (op, 1);
19830     }
19831 }
19832
19833 /* Finish the list of unwind opcodes for this function.  */
19834 static void
19835 finish_unwind_opcodes (void)
19836 {
19837   valueT op;
19838
19839   if (unwind.fp_used)
19840     {
19841       /* Adjust sp as necessary.  */
19842       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
19843       flush_pending_unwind ();
19844
19845       /* After restoring sp from the frame pointer.  */
19846       op = 0x90 | unwind.fp_reg;
19847       add_unwind_opcode (op, 1);
19848     }
19849   else
19850     flush_pending_unwind ();
19851 }
19852
19853
19854 /* Start an exception table entry.  If idx is nonzero this is an index table
19855    entry.  */
19856
19857 static void
19858 start_unwind_section (const segT text_seg, int idx)
19859 {
19860   const char * text_name;
19861   const char * prefix;
19862   const char * prefix_once;
19863   const char * group_name;
19864   size_t prefix_len;
19865   size_t text_len;
19866   char * sec_name;
19867   size_t sec_name_len;
19868   int type;
19869   int flags;
19870   int linkonce;
19871
19872   if (idx)
19873     {
19874       prefix = ELF_STRING_ARM_unwind;
19875       prefix_once = ELF_STRING_ARM_unwind_once;
19876       type = SHT_ARM_EXIDX;
19877     }
19878   else
19879     {
19880       prefix = ELF_STRING_ARM_unwind_info;
19881       prefix_once = ELF_STRING_ARM_unwind_info_once;
19882       type = SHT_PROGBITS;
19883     }
19884
19885   text_name = segment_name (text_seg);
19886   if (streq (text_name, ".text"))
19887     text_name = "";
19888
19889   if (strncmp (text_name, ".gnu.linkonce.t.",
19890                strlen (".gnu.linkonce.t.")) == 0)
19891     {
19892       prefix = prefix_once;
19893       text_name += strlen (".gnu.linkonce.t.");
19894     }
19895
19896   prefix_len = strlen (prefix);
19897   text_len = strlen (text_name);
19898   sec_name_len = prefix_len + text_len;
19899   sec_name = (char *) xmalloc (sec_name_len + 1);
19900   memcpy (sec_name, prefix, prefix_len);
19901   memcpy (sec_name + prefix_len, text_name, text_len);
19902   sec_name[prefix_len + text_len] = '\0';
19903
19904   flags = SHF_ALLOC;
19905   linkonce = 0;
19906   group_name = 0;
19907
19908   /* Handle COMDAT group.  */
19909   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
19910     {
19911       group_name = elf_group_name (text_seg);
19912       if (group_name == NULL)
19913         {
19914           as_bad (_("Group section `%s' has no group signature"),
19915                   segment_name (text_seg));
19916           ignore_rest_of_line ();
19917           return;
19918         }
19919       flags |= SHF_GROUP;
19920       linkonce = 1;
19921     }
19922
19923   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
19924
19925   /* Set the section link for index tables.  */
19926   if (idx)
19927     elf_linked_to_section (now_seg) = text_seg;
19928 }
19929
19930
19931 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
19932    personality routine data.  Returns zero, or the index table value for
19933    and inline entry.  */
19934
19935 static valueT
19936 create_unwind_entry (int have_data)
19937 {
19938   int size;
19939   addressT where;
19940   char *ptr;
19941   /* The current word of data.  */
19942   valueT data;
19943   /* The number of bytes left in this word.  */
19944   int n;
19945
19946   finish_unwind_opcodes ();
19947
19948   /* Remember the current text section.  */
19949   unwind.saved_seg = now_seg;
19950   unwind.saved_subseg = now_subseg;
19951
19952   start_unwind_section (now_seg, 0);
19953
19954   if (unwind.personality_routine == NULL)
19955     {
19956       if (unwind.personality_index == -2)
19957         {
19958           if (have_data)
19959             as_bad (_("handlerdata in cantunwind frame"));
19960           return 1; /* EXIDX_CANTUNWIND.  */
19961         }
19962
19963       /* Use a default personality routine if none is specified.  */
19964       if (unwind.personality_index == -1)
19965         {
19966           if (unwind.opcode_count > 3)
19967             unwind.personality_index = 1;
19968           else
19969             unwind.personality_index = 0;
19970         }
19971
19972       /* Space for the personality routine entry.  */
19973       if (unwind.personality_index == 0)
19974         {
19975           if (unwind.opcode_count > 3)
19976             as_bad (_("too many unwind opcodes for personality routine 0"));
19977
19978           if (!have_data)
19979             {
19980               /* All the data is inline in the index table.  */
19981               data = 0x80;
19982               n = 3;
19983               while (unwind.opcode_count > 0)
19984                 {
19985                   unwind.opcode_count--;
19986                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
19987                   n--;
19988                 }
19989
19990               /* Pad with "finish" opcodes.  */
19991               while (n--)
19992                 data = (data << 8) | 0xb0;
19993
19994               return data;
19995             }
19996           size = 0;
19997         }
19998       else
19999         /* We get two opcodes "free" in the first word.  */
20000         size = unwind.opcode_count - 2;
20001     }
20002   else
20003     {
20004       gas_assert (unwind.personality_index == -1);
20005
20006       /* An extra byte is required for the opcode count.        */
20007       size = unwind.opcode_count + 1;
20008     }
20009
20010   size = (size + 3) >> 2;
20011   if (size > 0xff)
20012     as_bad (_("too many unwind opcodes"));
20013
20014   frag_align (2, 0, 0);
20015   record_alignment (now_seg, 2);
20016   unwind.table_entry = expr_build_dot ();
20017
20018   /* Allocate the table entry.  */
20019   ptr = frag_more ((size << 2) + 4);
20020   /* PR 13449: Zero the table entries in case some of them are not used.  */
20021   memset (ptr, 0, (size << 2) + 4);
20022   where = frag_now_fix () - ((size << 2) + 4);
20023
20024   switch (unwind.personality_index)
20025     {
20026     case -1:
20027       /* ??? Should this be a PLT generating relocation?  */
20028       /* Custom personality routine.  */
20029       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
20030                BFD_RELOC_ARM_PREL31);
20031
20032       where += 4;
20033       ptr += 4;
20034
20035       /* Set the first byte to the number of additional words.  */
20036       data = size > 0 ? size - 1 : 0;
20037       n = 3;
20038       break;
20039
20040     /* ABI defined personality routines.  */
20041     case 0:
20042       /* Three opcodes bytes are packed into the first word.  */
20043       data = 0x80;
20044       n = 3;
20045       break;
20046
20047     case 1:
20048     case 2:
20049       /* The size and first two opcode bytes go in the first word.  */
20050       data = ((0x80 + unwind.personality_index) << 8) | size;
20051       n = 2;
20052       break;
20053
20054     default:
20055       /* Should never happen.  */
20056       abort ();
20057     }
20058
20059   /* Pack the opcodes into words (MSB first), reversing the list at the same
20060      time.  */
20061   while (unwind.opcode_count > 0)
20062     {
20063       if (n == 0)
20064         {
20065           md_number_to_chars (ptr, data, 4);
20066           ptr += 4;
20067           n = 4;
20068           data = 0;
20069         }
20070       unwind.opcode_count--;
20071       n--;
20072       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
20073     }
20074
20075   /* Finish off the last word.  */
20076   if (n < 4)
20077     {
20078       /* Pad with "finish" opcodes.  */
20079       while (n--)
20080         data = (data << 8) | 0xb0;
20081
20082       md_number_to_chars (ptr, data, 4);
20083     }
20084
20085   if (!have_data)
20086     {
20087       /* Add an empty descriptor if there is no user-specified data.   */
20088       ptr = frag_more (4);
20089       md_number_to_chars (ptr, 0, 4);
20090     }
20091
20092   return 0;
20093 }
20094
20095
20096 /* Initialize the DWARF-2 unwind information for this procedure.  */
20097
20098 void
20099 tc_arm_frame_initial_instructions (void)
20100 {
20101   cfi_add_CFA_def_cfa (REG_SP, 0);
20102 }
20103 #endif /* OBJ_ELF */
20104
20105 /* Convert REGNAME to a DWARF-2 register number.  */
20106
20107 int
20108 tc_arm_regname_to_dw2regnum (char *regname)
20109 {
20110   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
20111
20112   if (reg == FAIL)
20113     return -1;
20114
20115   return reg;
20116 }
20117
20118 #ifdef TE_PE
20119 void
20120 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
20121 {
20122   expressionS exp;
20123
20124   exp.X_op = O_secrel;
20125   exp.X_add_symbol = symbol;
20126   exp.X_add_number = 0;
20127   emit_expr (&exp, size);
20128 }
20129 #endif
20130
20131 /* MD interface: Symbol and relocation handling.  */
20132
20133 /* Return the address within the segment that a PC-relative fixup is
20134    relative to.  For ARM, PC-relative fixups applied to instructions
20135    are generally relative to the location of the fixup plus 8 bytes.
20136    Thumb branches are offset by 4, and Thumb loads relative to PC
20137    require special handling.  */
20138
20139 long
20140 md_pcrel_from_section (fixS * fixP, segT seg)
20141 {
20142   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
20143
20144   /* If this is pc-relative and we are going to emit a relocation
20145      then we just want to put out any pipeline compensation that the linker
20146      will need.  Otherwise we want to use the calculated base.
20147      For WinCE we skip the bias for externals as well, since this
20148      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
20149   if (fixP->fx_pcrel
20150       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
20151           || (arm_force_relocation (fixP)
20152 #ifdef TE_WINCE
20153               && !S_IS_EXTERNAL (fixP->fx_addsy)
20154 #endif
20155               )))
20156     base = 0;
20157
20158
20159   switch (fixP->fx_r_type)
20160     {
20161       /* PC relative addressing on the Thumb is slightly odd as the
20162          bottom two bits of the PC are forced to zero for the
20163          calculation.  This happens *after* application of the
20164          pipeline offset.  However, Thumb adrl already adjusts for
20165          this, so we need not do it again.  */
20166     case BFD_RELOC_ARM_THUMB_ADD:
20167       return base & ~3;
20168
20169     case BFD_RELOC_ARM_THUMB_OFFSET:
20170     case BFD_RELOC_ARM_T32_OFFSET_IMM:
20171     case BFD_RELOC_ARM_T32_ADD_PC12:
20172     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
20173       return (base + 4) & ~3;
20174
20175       /* Thumb branches are simply offset by +4.  */
20176     case BFD_RELOC_THUMB_PCREL_BRANCH7:
20177     case BFD_RELOC_THUMB_PCREL_BRANCH9:
20178     case BFD_RELOC_THUMB_PCREL_BRANCH12:
20179     case BFD_RELOC_THUMB_PCREL_BRANCH20:
20180     case BFD_RELOC_THUMB_PCREL_BRANCH25:
20181       return base + 4;
20182
20183     case BFD_RELOC_THUMB_PCREL_BRANCH23:
20184       if (fixP->fx_addsy
20185           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20186           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20187           && ARM_IS_FUNC (fixP->fx_addsy)
20188           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20189         base = fixP->fx_where + fixP->fx_frag->fr_address;
20190        return base + 4;
20191
20192       /* BLX is like branches above, but forces the low two bits of PC to
20193          zero.  */
20194     case BFD_RELOC_THUMB_PCREL_BLX:
20195       if (fixP->fx_addsy
20196           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20197           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20198           && THUMB_IS_FUNC (fixP->fx_addsy)
20199           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20200         base = fixP->fx_where + fixP->fx_frag->fr_address;
20201       return (base + 4) & ~3;
20202
20203       /* ARM mode branches are offset by +8.  However, the Windows CE
20204          loader expects the relocation not to take this into account.  */
20205     case BFD_RELOC_ARM_PCREL_BLX:
20206       if (fixP->fx_addsy
20207           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20208           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20209           && ARM_IS_FUNC (fixP->fx_addsy)
20210           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20211         base = fixP->fx_where + fixP->fx_frag->fr_address;
20212       return base + 8;
20213
20214     case BFD_RELOC_ARM_PCREL_CALL:
20215       if (fixP->fx_addsy
20216           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20217           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20218           && THUMB_IS_FUNC (fixP->fx_addsy)
20219           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20220         base = fixP->fx_where + fixP->fx_frag->fr_address;
20221       return base + 8;
20222
20223     case BFD_RELOC_ARM_PCREL_BRANCH:
20224     case BFD_RELOC_ARM_PCREL_JUMP:
20225     case BFD_RELOC_ARM_PLT32:
20226 #ifdef TE_WINCE
20227       /* When handling fixups immediately, because we have already
20228          discovered the value of a symbol, or the address of the frag involved
20229          we must account for the offset by +8, as the OS loader will never see the reloc.
20230          see fixup_segment() in write.c
20231          The S_IS_EXTERNAL test handles the case of global symbols.
20232          Those need the calculated base, not just the pipe compensation the linker will need.  */
20233       if (fixP->fx_pcrel
20234           && fixP->fx_addsy != NULL
20235           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20236           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
20237         return base + 8;
20238       return base;
20239 #else
20240       return base + 8;
20241 #endif
20242
20243
20244       /* ARM mode loads relative to PC are also offset by +8.  Unlike
20245          branches, the Windows CE loader *does* expect the relocation
20246          to take this into account.  */
20247     case BFD_RELOC_ARM_OFFSET_IMM:
20248     case BFD_RELOC_ARM_OFFSET_IMM8:
20249     case BFD_RELOC_ARM_HWLITERAL:
20250     case BFD_RELOC_ARM_LITERAL:
20251     case BFD_RELOC_ARM_CP_OFF_IMM:
20252       return base + 8;
20253
20254
20255       /* Other PC-relative relocations are un-offset.  */
20256     default:
20257       return base;
20258     }
20259 }
20260
20261 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
20262    Otherwise we have no need to default values of symbols.  */
20263
20264 symbolS *
20265 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
20266 {
20267 #ifdef OBJ_ELF
20268   if (name[0] == '_' && name[1] == 'G'
20269       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
20270     {
20271       if (!GOT_symbol)
20272         {
20273           if (symbol_find (name))
20274             as_bad (_("GOT already in the symbol table"));
20275
20276           GOT_symbol = symbol_new (name, undefined_section,
20277                                    (valueT) 0, & zero_address_frag);
20278         }
20279
20280       return GOT_symbol;
20281     }
20282 #endif
20283
20284   return NULL;
20285 }
20286
20287 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
20288    computed as two separate immediate values, added together.  We
20289    already know that this value cannot be computed by just one ARM
20290    instruction.  */
20291
20292 static unsigned int
20293 validate_immediate_twopart (unsigned int   val,
20294                             unsigned int * highpart)
20295 {
20296   unsigned int a;
20297   unsigned int i;
20298
20299   for (i = 0; i < 32; i += 2)
20300     if (((a = rotate_left (val, i)) & 0xff) != 0)
20301       {
20302         if (a & 0xff00)
20303           {
20304             if (a & ~ 0xffff)
20305               continue;
20306             * highpart = (a  >> 8) | ((i + 24) << 7);
20307           }
20308         else if (a & 0xff0000)
20309           {
20310             if (a & 0xff000000)
20311               continue;
20312             * highpart = (a >> 16) | ((i + 16) << 7);
20313           }
20314         else
20315           {
20316             gas_assert (a & 0xff000000);
20317             * highpart = (a >> 24) | ((i + 8) << 7);
20318           }
20319
20320         return (a & 0xff) | (i << 7);
20321       }
20322
20323   return FAIL;
20324 }
20325
20326 static int
20327 validate_offset_imm (unsigned int val, int hwse)
20328 {
20329   if ((hwse && val > 255) || val > 4095)
20330     return FAIL;
20331   return val;
20332 }
20333
20334 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
20335    negative immediate constant by altering the instruction.  A bit of
20336    a hack really.
20337         MOV <-> MVN
20338         AND <-> BIC
20339         ADC <-> SBC
20340         by inverting the second operand, and
20341         ADD <-> SUB
20342         CMP <-> CMN
20343         by negating the second operand.  */
20344
20345 static int
20346 negate_data_op (unsigned long * instruction,
20347                 unsigned long   value)
20348 {
20349   int op, new_inst;
20350   unsigned long negated, inverted;
20351
20352   negated = encode_arm_immediate (-value);
20353   inverted = encode_arm_immediate (~value);
20354
20355   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
20356   switch (op)
20357     {
20358       /* First negates.  */
20359     case OPCODE_SUB:             /* ADD <-> SUB  */
20360       new_inst = OPCODE_ADD;
20361       value = negated;
20362       break;
20363
20364     case OPCODE_ADD:
20365       new_inst = OPCODE_SUB;
20366       value = negated;
20367       break;
20368
20369     case OPCODE_CMP:             /* CMP <-> CMN  */
20370       new_inst = OPCODE_CMN;
20371       value = negated;
20372       break;
20373
20374     case OPCODE_CMN:
20375       new_inst = OPCODE_CMP;
20376       value = negated;
20377       break;
20378
20379       /* Now Inverted ops.  */
20380     case OPCODE_MOV:             /* MOV <-> MVN  */
20381       new_inst = OPCODE_MVN;
20382       value = inverted;
20383       break;
20384
20385     case OPCODE_MVN:
20386       new_inst = OPCODE_MOV;
20387       value = inverted;
20388       break;
20389
20390     case OPCODE_AND:             /* AND <-> BIC  */
20391       new_inst = OPCODE_BIC;
20392       value = inverted;
20393       break;
20394
20395     case OPCODE_BIC:
20396       new_inst = OPCODE_AND;
20397       value = inverted;
20398       break;
20399
20400     case OPCODE_ADC:              /* ADC <-> SBC  */
20401       new_inst = OPCODE_SBC;
20402       value = inverted;
20403       break;
20404
20405     case OPCODE_SBC:
20406       new_inst = OPCODE_ADC;
20407       value = inverted;
20408       break;
20409
20410       /* We cannot do anything.  */
20411     default:
20412       return FAIL;
20413     }
20414
20415   if (value == (unsigned) FAIL)
20416     return FAIL;
20417
20418   *instruction &= OPCODE_MASK;
20419   *instruction |= new_inst << DATA_OP_SHIFT;
20420   return value;
20421 }
20422
20423 /* Like negate_data_op, but for Thumb-2.   */
20424
20425 static unsigned int
20426 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
20427 {
20428   int op, new_inst;
20429   int rd;
20430   unsigned int negated, inverted;
20431
20432   negated = encode_thumb32_immediate (-value);
20433   inverted = encode_thumb32_immediate (~value);
20434
20435   rd = (*instruction >> 8) & 0xf;
20436   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
20437   switch (op)
20438     {
20439       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
20440     case T2_OPCODE_SUB:
20441       new_inst = T2_OPCODE_ADD;
20442       value = negated;
20443       break;
20444
20445     case T2_OPCODE_ADD:
20446       new_inst = T2_OPCODE_SUB;
20447       value = negated;
20448       break;
20449
20450       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
20451     case T2_OPCODE_ORR:
20452       new_inst = T2_OPCODE_ORN;
20453       value = inverted;
20454       break;
20455
20456     case T2_OPCODE_ORN:
20457       new_inst = T2_OPCODE_ORR;
20458       value = inverted;
20459       break;
20460
20461       /* AND <-> BIC.  TST has no inverted equivalent.  */
20462     case T2_OPCODE_AND:
20463       new_inst = T2_OPCODE_BIC;
20464       if (rd == 15)
20465         value = FAIL;
20466       else
20467         value = inverted;
20468       break;
20469
20470     case T2_OPCODE_BIC:
20471       new_inst = T2_OPCODE_AND;
20472       value = inverted;
20473       break;
20474
20475       /* ADC <-> SBC  */
20476     case T2_OPCODE_ADC:
20477       new_inst = T2_OPCODE_SBC;
20478       value = inverted;
20479       break;
20480
20481     case T2_OPCODE_SBC:
20482       new_inst = T2_OPCODE_ADC;
20483       value = inverted;
20484       break;
20485
20486       /* We cannot do anything.  */
20487     default:
20488       return FAIL;
20489     }
20490
20491   if (value == (unsigned int)FAIL)
20492     return FAIL;
20493
20494   *instruction &= T2_OPCODE_MASK;
20495   *instruction |= new_inst << T2_DATA_OP_SHIFT;
20496   return value;
20497 }
20498
20499 /* Read a 32-bit thumb instruction from buf.  */
20500 static unsigned long
20501 get_thumb32_insn (char * buf)
20502 {
20503   unsigned long insn;
20504   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
20505   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
20506
20507   return insn;
20508 }
20509
20510
20511 /* We usually want to set the low bit on the address of thumb function
20512    symbols.  In particular .word foo - . should have the low bit set.
20513    Generic code tries to fold the difference of two symbols to
20514    a constant.  Prevent this and force a relocation when the first symbols
20515    is a thumb function.  */
20516
20517 bfd_boolean
20518 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
20519 {
20520   if (op == O_subtract
20521       && l->X_op == O_symbol
20522       && r->X_op == O_symbol
20523       && THUMB_IS_FUNC (l->X_add_symbol))
20524     {
20525       l->X_op = O_subtract;
20526       l->X_op_symbol = r->X_add_symbol;
20527       l->X_add_number -= r->X_add_number;
20528       return TRUE;
20529     }
20530
20531   /* Process as normal.  */
20532   return FALSE;
20533 }
20534
20535 /* Encode Thumb2 unconditional branches and calls. The encoding
20536    for the 2 are identical for the immediate values.  */
20537
20538 static void
20539 encode_thumb2_b_bl_offset (char * buf, offsetT value)
20540 {
20541 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
20542   offsetT newval;
20543   offsetT newval2;
20544   addressT S, I1, I2, lo, hi;
20545
20546   S = (value >> 24) & 0x01;
20547   I1 = (value >> 23) & 0x01;
20548   I2 = (value >> 22) & 0x01;
20549   hi = (value >> 12) & 0x3ff;
20550   lo = (value >> 1) & 0x7ff;
20551   newval   = md_chars_to_number (buf, THUMB_SIZE);
20552   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
20553   newval  |= (S << 10) | hi;
20554   newval2 &=  ~T2I1I2MASK;
20555   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
20556   md_number_to_chars (buf, newval, THUMB_SIZE);
20557   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
20558 }
20559
20560 void
20561 md_apply_fix (fixS *    fixP,
20562                valueT * valP,
20563                segT     seg)
20564 {
20565   offsetT        value = * valP;
20566   offsetT        newval;
20567   unsigned int   newimm;
20568   unsigned long  temp;
20569   int            sign;
20570   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
20571
20572   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
20573
20574   /* Note whether this will delete the relocation.  */
20575
20576   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
20577     fixP->fx_done = 1;
20578
20579   /* On a 64-bit host, silently truncate 'value' to 32 bits for
20580      consistency with the behaviour on 32-bit hosts.  Remember value
20581      for emit_reloc.  */
20582   value &= 0xffffffff;
20583   value ^= 0x80000000;
20584   value -= 0x80000000;
20585
20586   *valP = value;
20587   fixP->fx_addnumber = value;
20588
20589   /* Same treatment for fixP->fx_offset.  */
20590   fixP->fx_offset &= 0xffffffff;
20591   fixP->fx_offset ^= 0x80000000;
20592   fixP->fx_offset -= 0x80000000;
20593
20594   switch (fixP->fx_r_type)
20595     {
20596     case BFD_RELOC_NONE:
20597       /* This will need to go in the object file.  */
20598       fixP->fx_done = 0;
20599       break;
20600
20601     case BFD_RELOC_ARM_IMMEDIATE:
20602       /* We claim that this fixup has been processed here,
20603          even if in fact we generate an error because we do
20604          not have a reloc for it, so tc_gen_reloc will reject it.  */
20605       fixP->fx_done = 1;
20606
20607       if (fixP->fx_addsy)
20608         {
20609           const char *msg = 0;
20610
20611           if (! S_IS_DEFINED (fixP->fx_addsy))
20612             msg = _("undefined symbol %s used as an immediate value");
20613           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
20614             msg = _("symbol %s is in a different section");
20615           else if (S_IS_WEAK (fixP->fx_addsy))
20616             msg = _("symbol %s is weak and may be overridden later");
20617
20618           if (msg)
20619             {
20620               as_bad_where (fixP->fx_file, fixP->fx_line,
20621                             msg, S_GET_NAME (fixP->fx_addsy));
20622               break;
20623             }
20624         }
20625
20626       newimm = encode_arm_immediate (value);
20627       temp = md_chars_to_number (buf, INSN_SIZE);
20628
20629       /* If the instruction will fail, see if we can fix things up by
20630          changing the opcode.  */
20631       if (newimm == (unsigned int) FAIL
20632           && (newimm = negate_data_op (&temp, value)) == (unsigned int) FAIL)
20633         {
20634           as_bad_where (fixP->fx_file, fixP->fx_line,
20635                         _("invalid constant (%lx) after fixup"),
20636                         (unsigned long) value);
20637           break;
20638         }
20639
20640       newimm |= (temp & 0xfffff000);
20641       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
20642       break;
20643
20644     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
20645       {
20646         unsigned int highpart = 0;
20647         unsigned int newinsn  = 0xe1a00000; /* nop.  */
20648
20649         if (fixP->fx_addsy)
20650           {
20651             const char *msg = 0;
20652
20653             if (! S_IS_DEFINED (fixP->fx_addsy))
20654               msg = _("undefined symbol %s used as an immediate value");
20655             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
20656               msg = _("symbol %s is in a different section");
20657             else if (S_IS_WEAK (fixP->fx_addsy))
20658               msg = _("symbol %s is weak and may be overridden later");
20659
20660             if (msg)
20661               {
20662                 as_bad_where (fixP->fx_file, fixP->fx_line,
20663                               msg, S_GET_NAME (fixP->fx_addsy));
20664                 break;
20665               }
20666           }
20667
20668         newimm = encode_arm_immediate (value);
20669         temp = md_chars_to_number (buf, INSN_SIZE);
20670
20671         /* If the instruction will fail, see if we can fix things up by
20672            changing the opcode.  */
20673         if (newimm == (unsigned int) FAIL
20674             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
20675           {
20676             /* No ?  OK - try using two ADD instructions to generate
20677                the value.  */
20678             newimm = validate_immediate_twopart (value, & highpart);
20679
20680             /* Yes - then make sure that the second instruction is
20681                also an add.  */
20682             if (newimm != (unsigned int) FAIL)
20683               newinsn = temp;
20684             /* Still No ?  Try using a negated value.  */
20685             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
20686               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
20687             /* Otherwise - give up.  */
20688             else
20689               {
20690                 as_bad_where (fixP->fx_file, fixP->fx_line,
20691                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
20692                               (long) value);
20693                 break;
20694               }
20695
20696             /* Replace the first operand in the 2nd instruction (which
20697                is the PC) with the destination register.  We have
20698                already added in the PC in the first instruction and we
20699                do not want to do it again.  */
20700             newinsn &= ~ 0xf0000;
20701             newinsn |= ((newinsn & 0x0f000) << 4);
20702           }
20703
20704         newimm |= (temp & 0xfffff000);
20705         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
20706
20707         highpart |= (newinsn & 0xfffff000);
20708         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
20709       }
20710       break;
20711
20712     case BFD_RELOC_ARM_OFFSET_IMM:
20713       if (!fixP->fx_done && seg->use_rela_p)
20714         value = 0;
20715
20716     case BFD_RELOC_ARM_LITERAL:
20717       sign = value > 0;
20718
20719       if (value < 0)
20720         value = - value;
20721
20722       if (validate_offset_imm (value, 0) == FAIL)
20723         {
20724           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
20725             as_bad_where (fixP->fx_file, fixP->fx_line,
20726                           _("invalid literal constant: pool needs to be closer"));
20727           else
20728             as_bad_where (fixP->fx_file, fixP->fx_line,
20729                           _("bad immediate value for offset (%ld)"),
20730                           (long) value);
20731           break;
20732         }
20733
20734       newval = md_chars_to_number (buf, INSN_SIZE);
20735       if (value == 0)
20736         newval &= 0xfffff000;
20737       else
20738         {
20739           newval &= 0xff7ff000;
20740           newval |= value | (sign ? INDEX_UP : 0);
20741         }
20742       md_number_to_chars (buf, newval, INSN_SIZE);
20743       break;
20744
20745     case BFD_RELOC_ARM_OFFSET_IMM8:
20746     case BFD_RELOC_ARM_HWLITERAL:
20747       sign = value > 0;
20748
20749       if (value < 0)
20750         value = - value;
20751
20752       if (validate_offset_imm (value, 1) == FAIL)
20753         {
20754           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
20755             as_bad_where (fixP->fx_file, fixP->fx_line,
20756                           _("invalid literal constant: pool needs to be closer"));
20757           else
20758             as_bad (_("bad immediate value for 8-bit offset (%ld)"),
20759                     (long) value);
20760           break;
20761         }
20762
20763       newval = md_chars_to_number (buf, INSN_SIZE);
20764       if (value == 0)
20765         newval &= 0xfffff0f0;
20766       else
20767         {
20768           newval &= 0xff7ff0f0;
20769           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
20770         }
20771       md_number_to_chars (buf, newval, INSN_SIZE);
20772       break;
20773
20774     case BFD_RELOC_ARM_T32_OFFSET_U8:
20775       if (value < 0 || value > 1020 || value % 4 != 0)
20776         as_bad_where (fixP->fx_file, fixP->fx_line,
20777                       _("bad immediate value for offset (%ld)"), (long) value);
20778       value /= 4;
20779
20780       newval = md_chars_to_number (buf+2, THUMB_SIZE);
20781       newval |= value;
20782       md_number_to_chars (buf+2, newval, THUMB_SIZE);
20783       break;
20784
20785     case BFD_RELOC_ARM_T32_OFFSET_IMM:
20786       /* This is a complicated relocation used for all varieties of Thumb32
20787          load/store instruction with immediate offset:
20788
20789          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
20790                                                    *4, optional writeback(W)
20791                                                    (doubleword load/store)
20792
20793          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
20794          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
20795          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
20796          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
20797          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
20798
20799          Uppercase letters indicate bits that are already encoded at
20800          this point.  Lowercase letters are our problem.  For the
20801          second block of instructions, the secondary opcode nybble
20802          (bits 8..11) is present, and bit 23 is zero, even if this is
20803          a PC-relative operation.  */
20804       newval = md_chars_to_number (buf, THUMB_SIZE);
20805       newval <<= 16;
20806       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
20807
20808       if ((newval & 0xf0000000) == 0xe0000000)
20809         {
20810           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
20811           if (value >= 0)
20812             newval |= (1 << 23);
20813           else
20814             value = -value;
20815           if (value % 4 != 0)
20816             {
20817               as_bad_where (fixP->fx_file, fixP->fx_line,
20818                             _("offset not a multiple of 4"));
20819               break;
20820             }
20821           value /= 4;
20822           if (value > 0xff)
20823             {
20824               as_bad_where (fixP->fx_file, fixP->fx_line,
20825                             _("offset out of range"));
20826               break;
20827             }
20828           newval &= ~0xff;
20829         }
20830       else if ((newval & 0x000f0000) == 0x000f0000)
20831         {
20832           /* PC-relative, 12-bit offset.  */
20833           if (value >= 0)
20834             newval |= (1 << 23);
20835           else
20836             value = -value;
20837           if (value > 0xfff)
20838             {
20839               as_bad_where (fixP->fx_file, fixP->fx_line,
20840                             _("offset out of range"));
20841               break;
20842             }
20843           newval &= ~0xfff;
20844         }
20845       else if ((newval & 0x00000100) == 0x00000100)
20846         {
20847           /* Writeback: 8-bit, +/- offset.  */
20848           if (value >= 0)
20849             newval |= (1 << 9);
20850           else
20851             value = -value;
20852           if (value > 0xff)
20853             {
20854               as_bad_where (fixP->fx_file, fixP->fx_line,
20855                             _("offset out of range"));
20856               break;
20857             }
20858           newval &= ~0xff;
20859         }
20860       else if ((newval & 0x00000f00) == 0x00000e00)
20861         {
20862           /* T-instruction: positive 8-bit offset.  */
20863           if (value < 0 || value > 0xff)
20864             {
20865               as_bad_where (fixP->fx_file, fixP->fx_line,
20866                             _("offset out of range"));
20867               break;
20868             }
20869           newval &= ~0xff;
20870           newval |= value;
20871         }
20872       else
20873         {
20874           /* Positive 12-bit or negative 8-bit offset.  */
20875           int limit;
20876           if (value >= 0)
20877             {
20878               newval |= (1 << 23);
20879               limit = 0xfff;
20880             }
20881           else
20882             {
20883               value = -value;
20884               limit = 0xff;
20885             }
20886           if (value > limit)
20887             {
20888               as_bad_where (fixP->fx_file, fixP->fx_line,
20889                             _("offset out of range"));
20890               break;
20891             }
20892           newval &= ~limit;
20893         }
20894
20895       newval |= value;
20896       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
20897       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
20898       break;
20899
20900     case BFD_RELOC_ARM_SHIFT_IMM:
20901       newval = md_chars_to_number (buf, INSN_SIZE);
20902       if (((unsigned long) value) > 32
20903           || (value == 32
20904               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
20905         {
20906           as_bad_where (fixP->fx_file, fixP->fx_line,
20907                         _("shift expression is too large"));
20908           break;
20909         }
20910
20911       if (value == 0)
20912         /* Shifts of zero must be done as lsl.  */
20913         newval &= ~0x60;
20914       else if (value == 32)
20915         value = 0;
20916       newval &= 0xfffff07f;
20917       newval |= (value & 0x1f) << 7;
20918       md_number_to_chars (buf, newval, INSN_SIZE);
20919       break;
20920
20921     case BFD_RELOC_ARM_T32_IMMEDIATE:
20922     case BFD_RELOC_ARM_T32_ADD_IMM:
20923     case BFD_RELOC_ARM_T32_IMM12:
20924     case BFD_RELOC_ARM_T32_ADD_PC12:
20925       /* We claim that this fixup has been processed here,
20926          even if in fact we generate an error because we do
20927          not have a reloc for it, so tc_gen_reloc will reject it.  */
20928       fixP->fx_done = 1;
20929
20930       if (fixP->fx_addsy
20931           && ! S_IS_DEFINED (fixP->fx_addsy))
20932         {
20933           as_bad_where (fixP->fx_file, fixP->fx_line,
20934                         _("undefined symbol %s used as an immediate value"),
20935                         S_GET_NAME (fixP->fx_addsy));
20936           break;
20937         }
20938
20939       newval = md_chars_to_number (buf, THUMB_SIZE);
20940       newval <<= 16;
20941       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
20942
20943       newimm = FAIL;
20944       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
20945           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
20946         {
20947           newimm = encode_thumb32_immediate (value);
20948           if (newimm == (unsigned int) FAIL)
20949             newimm = thumb32_negate_data_op (&newval, value);
20950         }
20951       if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE
20952           && newimm == (unsigned int) FAIL)
20953         {
20954           /* Turn add/sum into addw/subw.  */
20955           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
20956             newval = (newval & 0xfeffffff) | 0x02000000;
20957           /* No flat 12-bit imm encoding for addsw/subsw.  */
20958           if ((newval & 0x00100000) == 0)
20959             {
20960               /* 12 bit immediate for addw/subw.  */
20961               if (value < 0)
20962                 {
20963                   value = -value;
20964                   newval ^= 0x00a00000;
20965                 }
20966               if (value > 0xfff)
20967                 newimm = (unsigned int) FAIL;
20968               else
20969                 newimm = value;
20970             }
20971         }
20972
20973       if (newimm == (unsigned int)FAIL)
20974         {
20975           as_bad_where (fixP->fx_file, fixP->fx_line,
20976                         _("invalid constant (%lx) after fixup"),
20977                         (unsigned long) value);
20978           break;
20979         }
20980
20981       newval |= (newimm & 0x800) << 15;
20982       newval |= (newimm & 0x700) << 4;
20983       newval |= (newimm & 0x0ff);
20984
20985       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
20986       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
20987       break;
20988
20989     case BFD_RELOC_ARM_SMC:
20990       if (((unsigned long) value) > 0xffff)
20991         as_bad_where (fixP->fx_file, fixP->fx_line,
20992                       _("invalid smc expression"));
20993       newval = md_chars_to_number (buf, INSN_SIZE);
20994       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
20995       md_number_to_chars (buf, newval, INSN_SIZE);
20996       break;
20997
20998     case BFD_RELOC_ARM_HVC:
20999       if (((unsigned long) value) > 0xffff)
21000         as_bad_where (fixP->fx_file, fixP->fx_line,
21001                       _("invalid hvc expression"));
21002       newval = md_chars_to_number (buf, INSN_SIZE);
21003       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
21004       md_number_to_chars (buf, newval, INSN_SIZE);
21005       break;
21006
21007     case BFD_RELOC_ARM_SWI:
21008       if (fixP->tc_fix_data != 0)
21009         {
21010           if (((unsigned long) value) > 0xff)
21011             as_bad_where (fixP->fx_file, fixP->fx_line,
21012                           _("invalid swi expression"));
21013           newval = md_chars_to_number (buf, THUMB_SIZE);
21014           newval |= value;
21015           md_number_to_chars (buf, newval, THUMB_SIZE);
21016         }
21017       else
21018         {
21019           if (((unsigned long) value) > 0x00ffffff)
21020             as_bad_where (fixP->fx_file, fixP->fx_line,
21021                           _("invalid swi expression"));
21022           newval = md_chars_to_number (buf, INSN_SIZE);
21023           newval |= value;
21024           md_number_to_chars (buf, newval, INSN_SIZE);
21025         }
21026       break;
21027
21028     case BFD_RELOC_ARM_MULTI:
21029       if (((unsigned long) value) > 0xffff)
21030         as_bad_where (fixP->fx_file, fixP->fx_line,
21031                       _("invalid expression in load/store multiple"));
21032       newval = value | md_chars_to_number (buf, INSN_SIZE);
21033       md_number_to_chars (buf, newval, INSN_SIZE);
21034       break;
21035
21036 #ifdef OBJ_ELF
21037     case BFD_RELOC_ARM_PCREL_CALL:
21038
21039       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
21040           && fixP->fx_addsy
21041           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21042           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21043           && THUMB_IS_FUNC (fixP->fx_addsy))
21044         /* Flip the bl to blx. This is a simple flip
21045            bit here because we generate PCREL_CALL for
21046            unconditional bls.  */
21047         {
21048           newval = md_chars_to_number (buf, INSN_SIZE);
21049           newval = newval | 0x10000000;
21050           md_number_to_chars (buf, newval, INSN_SIZE);
21051           temp = 1;
21052           fixP->fx_done = 1;
21053         }
21054       else
21055         temp = 3;
21056       goto arm_branch_common;
21057
21058     case BFD_RELOC_ARM_PCREL_JUMP:
21059       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
21060           && fixP->fx_addsy
21061           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21062           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21063           && THUMB_IS_FUNC (fixP->fx_addsy))
21064         {
21065           /* This would map to a bl<cond>, b<cond>,
21066              b<always> to a Thumb function. We
21067              need to force a relocation for this particular
21068              case.  */
21069           newval = md_chars_to_number (buf, INSN_SIZE);
21070           fixP->fx_done = 0;
21071         }
21072
21073     case BFD_RELOC_ARM_PLT32:
21074 #endif
21075     case BFD_RELOC_ARM_PCREL_BRANCH:
21076       temp = 3;
21077       goto arm_branch_common;
21078
21079     case BFD_RELOC_ARM_PCREL_BLX:
21080
21081       temp = 1;
21082       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
21083           && fixP->fx_addsy
21084           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21085           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21086           && ARM_IS_FUNC (fixP->fx_addsy))
21087         {
21088           /* Flip the blx to a bl and warn.  */
21089           const char *name = S_GET_NAME (fixP->fx_addsy);
21090           newval = 0xeb000000;
21091           as_warn_where (fixP->fx_file, fixP->fx_line,
21092                          _("blx to '%s' an ARM ISA state function changed to bl"),
21093                           name);
21094           md_number_to_chars (buf, newval, INSN_SIZE);
21095           temp = 3;
21096           fixP->fx_done = 1;
21097         }
21098
21099 #ifdef OBJ_ELF
21100        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
21101          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
21102 #endif
21103
21104     arm_branch_common:
21105       /* We are going to store value (shifted right by two) in the
21106          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
21107          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
21108          also be be clear.  */
21109       if (value & temp)
21110         as_bad_where (fixP->fx_file, fixP->fx_line,
21111                       _("misaligned branch destination"));
21112       if ((value & (offsetT)0xfe000000) != (offsetT)0
21113           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
21114         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21115
21116       if (fixP->fx_done || !seg->use_rela_p)
21117         {
21118           newval = md_chars_to_number (buf, INSN_SIZE);
21119           newval |= (value >> 2) & 0x00ffffff;
21120           /* Set the H bit on BLX instructions.  */
21121           if (temp == 1)
21122             {
21123               if (value & 2)
21124                 newval |= 0x01000000;
21125               else
21126                 newval &= ~0x01000000;
21127             }
21128           md_number_to_chars (buf, newval, INSN_SIZE);
21129         }
21130       break;
21131
21132     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
21133       /* CBZ can only branch forward.  */
21134
21135       /* Attempts to use CBZ to branch to the next instruction
21136          (which, strictly speaking, are prohibited) will be turned into
21137          no-ops.
21138
21139          FIXME: It may be better to remove the instruction completely and
21140          perform relaxation.  */
21141       if (value == -2)
21142         {
21143           newval = md_chars_to_number (buf, THUMB_SIZE);
21144           newval = 0xbf00; /* NOP encoding T1 */
21145           md_number_to_chars (buf, newval, THUMB_SIZE);
21146         }
21147       else
21148         {
21149           if (value & ~0x7e)
21150             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21151
21152           if (fixP->fx_done || !seg->use_rela_p)
21153             {
21154               newval = md_chars_to_number (buf, THUMB_SIZE);
21155               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
21156               md_number_to_chars (buf, newval, THUMB_SIZE);
21157             }
21158         }
21159       break;
21160
21161     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
21162       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
21163         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21164
21165       if (fixP->fx_done || !seg->use_rela_p)
21166         {
21167           newval = md_chars_to_number (buf, THUMB_SIZE);
21168           newval |= (value & 0x1ff) >> 1;
21169           md_number_to_chars (buf, newval, THUMB_SIZE);
21170         }
21171       break;
21172
21173     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
21174       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
21175         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21176
21177       if (fixP->fx_done || !seg->use_rela_p)
21178         {
21179           newval = md_chars_to_number (buf, THUMB_SIZE);
21180           newval |= (value & 0xfff) >> 1;
21181           md_number_to_chars (buf, newval, THUMB_SIZE);
21182         }
21183       break;
21184
21185     case BFD_RELOC_THUMB_PCREL_BRANCH20:
21186       if (fixP->fx_addsy
21187           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21188           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21189           && ARM_IS_FUNC (fixP->fx_addsy)
21190           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21191         {
21192           /* Force a relocation for a branch 20 bits wide.  */
21193           fixP->fx_done = 0;
21194         }
21195       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
21196         as_bad_where (fixP->fx_file, fixP->fx_line,
21197                       _("conditional branch out of range"));
21198
21199       if (fixP->fx_done || !seg->use_rela_p)
21200         {
21201           offsetT newval2;
21202           addressT S, J1, J2, lo, hi;
21203
21204           S  = (value & 0x00100000) >> 20;
21205           J2 = (value & 0x00080000) >> 19;
21206           J1 = (value & 0x00040000) >> 18;
21207           hi = (value & 0x0003f000) >> 12;
21208           lo = (value & 0x00000ffe) >> 1;
21209
21210           newval   = md_chars_to_number (buf, THUMB_SIZE);
21211           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
21212           newval  |= (S << 10) | hi;
21213           newval2 |= (J1 << 13) | (J2 << 11) | lo;
21214           md_number_to_chars (buf, newval, THUMB_SIZE);
21215           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
21216         }
21217       break;
21218
21219     case BFD_RELOC_THUMB_PCREL_BLX:
21220       /* If there is a blx from a thumb state function to
21221          another thumb function flip this to a bl and warn
21222          about it.  */
21223
21224       if (fixP->fx_addsy
21225           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21226           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21227           && THUMB_IS_FUNC (fixP->fx_addsy))
21228         {
21229           const char *name = S_GET_NAME (fixP->fx_addsy);
21230           as_warn_where (fixP->fx_file, fixP->fx_line,
21231                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
21232                          name);
21233           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
21234           newval = newval | 0x1000;
21235           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
21236           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
21237           fixP->fx_done = 1;
21238         }
21239
21240
21241       goto thumb_bl_common;
21242
21243     case BFD_RELOC_THUMB_PCREL_BRANCH23:
21244       /* A bl from Thumb state ISA to an internal ARM state function
21245          is converted to a blx.  */
21246       if (fixP->fx_addsy
21247           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21248           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21249           && ARM_IS_FUNC (fixP->fx_addsy)
21250           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21251         {
21252           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
21253           newval = newval & ~0x1000;
21254           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
21255           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
21256           fixP->fx_done = 1;
21257         }
21258
21259     thumb_bl_common:
21260
21261 #ifdef OBJ_ELF
21262        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4 &&
21263            fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
21264          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
21265 #endif
21266
21267       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
21268         /* For a BLX instruction, make sure that the relocation is rounded up
21269            to a word boundary.  This follows the semantics of the instruction
21270            which specifies that bit 1 of the target address will come from bit
21271            1 of the base address.  */
21272         value = (value + 1) & ~ 1;
21273
21274        if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
21275          {
21276            if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2)))
21277              as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21278            else if ((value & ~0x1ffffff)
21279                     && ((value & ~0x1ffffff) != ~0x1ffffff))
21280              as_bad_where (fixP->fx_file, fixP->fx_line,
21281                            _("Thumb2 branch out of range"));
21282          }
21283
21284       if (fixP->fx_done || !seg->use_rela_p)
21285         encode_thumb2_b_bl_offset (buf, value);
21286
21287       break;
21288
21289     case BFD_RELOC_THUMB_PCREL_BRANCH25:
21290       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
21291         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
21292
21293       if (fixP->fx_done || !seg->use_rela_p)
21294           encode_thumb2_b_bl_offset (buf, value);
21295
21296       break;
21297
21298     case BFD_RELOC_8:
21299       if (fixP->fx_done || !seg->use_rela_p)
21300         md_number_to_chars (buf, value, 1);
21301       break;
21302
21303     case BFD_RELOC_16:
21304       if (fixP->fx_done || !seg->use_rela_p)
21305         md_number_to_chars (buf, value, 2);
21306       break;
21307
21308 #ifdef OBJ_ELF
21309     case BFD_RELOC_ARM_TLS_CALL:
21310     case BFD_RELOC_ARM_THM_TLS_CALL:
21311     case BFD_RELOC_ARM_TLS_DESCSEQ:
21312     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
21313       S_SET_THREAD_LOCAL (fixP->fx_addsy);
21314       break;
21315
21316     case BFD_RELOC_ARM_TLS_GOTDESC:
21317     case BFD_RELOC_ARM_TLS_GD32:
21318     case BFD_RELOC_ARM_TLS_LE32:
21319     case BFD_RELOC_ARM_TLS_IE32:
21320     case BFD_RELOC_ARM_TLS_LDM32:
21321     case BFD_RELOC_ARM_TLS_LDO32:
21322       S_SET_THREAD_LOCAL (fixP->fx_addsy);
21323       /* fall through */
21324
21325     case BFD_RELOC_ARM_GOT32:
21326     case BFD_RELOC_ARM_GOTOFF:
21327       if (fixP->fx_done || !seg->use_rela_p)
21328         md_number_to_chars (buf, 0, 4);
21329       break;
21330
21331     case BFD_RELOC_ARM_GOT_PREL:
21332       if (fixP->fx_done || !seg->use_rela_p)
21333         md_number_to_chars (buf, value, 4);
21334       break;
21335
21336     case BFD_RELOC_ARM_TARGET2:
21337       /* TARGET2 is not partial-inplace, so we need to write the
21338          addend here for REL targets, because it won't be written out
21339          during reloc processing later.  */
21340       if (fixP->fx_done || !seg->use_rela_p)
21341         md_number_to_chars (buf, fixP->fx_offset, 4);
21342       break;
21343 #endif
21344
21345     case BFD_RELOC_RVA:
21346     case BFD_RELOC_32:
21347     case BFD_RELOC_ARM_TARGET1:
21348     case BFD_RELOC_ARM_ROSEGREL32:
21349     case BFD_RELOC_ARM_SBREL32:
21350     case BFD_RELOC_32_PCREL:
21351 #ifdef TE_PE
21352     case BFD_RELOC_32_SECREL:
21353 #endif
21354       if (fixP->fx_done || !seg->use_rela_p)
21355 #ifdef TE_WINCE
21356         /* For WinCE we only do this for pcrel fixups.  */
21357         if (fixP->fx_done || fixP->fx_pcrel)
21358 #endif
21359           md_number_to_chars (buf, value, 4);
21360       break;
21361
21362 #ifdef OBJ_ELF
21363     case BFD_RELOC_ARM_PREL31:
21364       if (fixP->fx_done || !seg->use_rela_p)
21365         {
21366           newval = md_chars_to_number (buf, 4) & 0x80000000;
21367           if ((value ^ (value >> 1)) & 0x40000000)
21368             {
21369               as_bad_where (fixP->fx_file, fixP->fx_line,
21370                             _("rel31 relocation overflow"));
21371             }
21372           newval |= value & 0x7fffffff;
21373           md_number_to_chars (buf, newval, 4);
21374         }
21375       break;
21376 #endif
21377
21378     case BFD_RELOC_ARM_CP_OFF_IMM:
21379     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
21380       if (value < -1023 || value > 1023 || (value & 3))
21381         as_bad_where (fixP->fx_file, fixP->fx_line,
21382                       _("co-processor offset out of range"));
21383     cp_off_common:
21384       sign = value > 0;
21385       if (value < 0)
21386         value = -value;
21387       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
21388           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
21389         newval = md_chars_to_number (buf, INSN_SIZE);
21390       else
21391         newval = get_thumb32_insn (buf);
21392       if (value == 0)
21393         newval &= 0xffffff00;
21394       else
21395         {
21396           newval &= 0xff7fff00;
21397           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
21398         }
21399       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
21400           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
21401         md_number_to_chars (buf, newval, INSN_SIZE);
21402       else
21403         put_thumb32_insn (buf, newval);
21404       break;
21405
21406     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
21407     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
21408       if (value < -255 || value > 255)
21409         as_bad_where (fixP->fx_file, fixP->fx_line,
21410                       _("co-processor offset out of range"));
21411       value *= 4;
21412       goto cp_off_common;
21413
21414     case BFD_RELOC_ARM_THUMB_OFFSET:
21415       newval = md_chars_to_number (buf, THUMB_SIZE);
21416       /* Exactly what ranges, and where the offset is inserted depends
21417          on the type of instruction, we can establish this from the
21418          top 4 bits.  */
21419       switch (newval >> 12)
21420         {
21421         case 4: /* PC load.  */
21422           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
21423              forced to zero for these loads; md_pcrel_from has already
21424              compensated for this.  */
21425           if (value & 3)
21426             as_bad_where (fixP->fx_file, fixP->fx_line,
21427                           _("invalid offset, target not word aligned (0x%08lX)"),
21428                           (((unsigned long) fixP->fx_frag->fr_address
21429                             + (unsigned long) fixP->fx_where) & ~3)
21430                           + (unsigned long) value);
21431
21432           if (value & ~0x3fc)
21433             as_bad_where (fixP->fx_file, fixP->fx_line,
21434                           _("invalid offset, value too big (0x%08lX)"),
21435                           (long) value);
21436
21437           newval |= value >> 2;
21438           break;
21439
21440         case 9: /* SP load/store.  */
21441           if (value & ~0x3fc)
21442             as_bad_where (fixP->fx_file, fixP->fx_line,
21443                           _("invalid offset, value too big (0x%08lX)"),
21444                           (long) value);
21445           newval |= value >> 2;
21446           break;
21447
21448         case 6: /* Word load/store.  */
21449           if (value & ~0x7c)
21450             as_bad_where (fixP->fx_file, fixP->fx_line,
21451                           _("invalid offset, value too big (0x%08lX)"),
21452                           (long) value);
21453           newval |= value << 4; /* 6 - 2.  */
21454           break;
21455
21456         case 7: /* Byte load/store.  */
21457           if (value & ~0x1f)
21458             as_bad_where (fixP->fx_file, fixP->fx_line,
21459                           _("invalid offset, value too big (0x%08lX)"),
21460                           (long) value);
21461           newval |= value << 6;
21462           break;
21463
21464         case 8: /* Halfword load/store.  */
21465           if (value & ~0x3e)
21466             as_bad_where (fixP->fx_file, fixP->fx_line,
21467                           _("invalid offset, value too big (0x%08lX)"),
21468                           (long) value);
21469           newval |= value << 5; /* 6 - 1.  */
21470           break;
21471
21472         default:
21473           as_bad_where (fixP->fx_file, fixP->fx_line,
21474                         "Unable to process relocation for thumb opcode: %lx",
21475                         (unsigned long) newval);
21476           break;
21477         }
21478       md_number_to_chars (buf, newval, THUMB_SIZE);
21479       break;
21480
21481     case BFD_RELOC_ARM_THUMB_ADD:
21482       /* This is a complicated relocation, since we use it for all of
21483          the following immediate relocations:
21484
21485             3bit ADD/SUB
21486             8bit ADD/SUB
21487             9bit ADD/SUB SP word-aligned
21488            10bit ADD PC/SP word-aligned
21489
21490          The type of instruction being processed is encoded in the
21491          instruction field:
21492
21493            0x8000  SUB
21494            0x00F0  Rd
21495            0x000F  Rs
21496       */
21497       newval = md_chars_to_number (buf, THUMB_SIZE);
21498       {
21499         int rd = (newval >> 4) & 0xf;
21500         int rs = newval & 0xf;
21501         int subtract = !!(newval & 0x8000);
21502
21503         /* Check for HI regs, only very restricted cases allowed:
21504            Adjusting SP, and using PC or SP to get an address.  */
21505         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
21506             || (rs > 7 && rs != REG_SP && rs != REG_PC))
21507           as_bad_where (fixP->fx_file, fixP->fx_line,
21508                         _("invalid Hi register with immediate"));
21509
21510         /* If value is negative, choose the opposite instruction.  */
21511         if (value < 0)
21512           {
21513             value = -value;
21514             subtract = !subtract;
21515             if (value < 0)
21516               as_bad_where (fixP->fx_file, fixP->fx_line,
21517                             _("immediate value out of range"));
21518           }
21519
21520         if (rd == REG_SP)
21521           {
21522             if (value & ~0x1fc)
21523               as_bad_where (fixP->fx_file, fixP->fx_line,
21524                             _("invalid immediate for stack address calculation"));
21525             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
21526             newval |= value >> 2;
21527           }
21528         else if (rs == REG_PC || rs == REG_SP)
21529           {
21530             if (subtract || value & ~0x3fc)
21531               as_bad_where (fixP->fx_file, fixP->fx_line,
21532                             _("invalid immediate for address calculation (value = 0x%08lX)"),
21533                             (unsigned long) value);
21534             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
21535             newval |= rd << 8;
21536             newval |= value >> 2;
21537           }
21538         else if (rs == rd)
21539           {
21540             if (value & ~0xff)
21541               as_bad_where (fixP->fx_file, fixP->fx_line,
21542                             _("immediate value out of range"));
21543             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
21544             newval |= (rd << 8) | value;
21545           }
21546         else
21547           {
21548             if (value & ~0x7)
21549               as_bad_where (fixP->fx_file, fixP->fx_line,
21550                             _("immediate value out of range"));
21551             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
21552             newval |= rd | (rs << 3) | (value << 6);
21553           }
21554       }
21555       md_number_to_chars (buf, newval, THUMB_SIZE);
21556       break;
21557
21558     case BFD_RELOC_ARM_THUMB_IMM:
21559       newval = md_chars_to_number (buf, THUMB_SIZE);
21560       if (value < 0 || value > 255)
21561         as_bad_where (fixP->fx_file, fixP->fx_line,
21562                       _("invalid immediate: %ld is out of range"),
21563                       (long) value);
21564       newval |= value;
21565       md_number_to_chars (buf, newval, THUMB_SIZE);
21566       break;
21567
21568     case BFD_RELOC_ARM_THUMB_SHIFT:
21569       /* 5bit shift value (0..32).  LSL cannot take 32.  */
21570       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
21571       temp = newval & 0xf800;
21572       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
21573         as_bad_where (fixP->fx_file, fixP->fx_line,
21574                       _("invalid shift value: %ld"), (long) value);
21575       /* Shifts of zero must be encoded as LSL.  */
21576       if (value == 0)
21577         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
21578       /* Shifts of 32 are encoded as zero.  */
21579       else if (value == 32)
21580         value = 0;
21581       newval |= value << 6;
21582       md_number_to_chars (buf, newval, THUMB_SIZE);
21583       break;
21584
21585     case BFD_RELOC_VTABLE_INHERIT:
21586     case BFD_RELOC_VTABLE_ENTRY:
21587       fixP->fx_done = 0;
21588       return;
21589
21590     case BFD_RELOC_ARM_MOVW:
21591     case BFD_RELOC_ARM_MOVT:
21592     case BFD_RELOC_ARM_THUMB_MOVW:
21593     case BFD_RELOC_ARM_THUMB_MOVT:
21594       if (fixP->fx_done || !seg->use_rela_p)
21595         {
21596           /* REL format relocations are limited to a 16-bit addend.  */
21597           if (!fixP->fx_done)
21598             {
21599               if (value < -0x8000 || value > 0x7fff)
21600                   as_bad_where (fixP->fx_file, fixP->fx_line,
21601                                 _("offset out of range"));
21602             }
21603           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
21604                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
21605             {
21606               value >>= 16;
21607             }
21608
21609           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
21610               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
21611             {
21612               newval = get_thumb32_insn (buf);
21613               newval &= 0xfbf08f00;
21614               newval |= (value & 0xf000) << 4;
21615               newval |= (value & 0x0800) << 15;
21616               newval |= (value & 0x0700) << 4;
21617               newval |= (value & 0x00ff);
21618               put_thumb32_insn (buf, newval);
21619             }
21620           else
21621             {
21622               newval = md_chars_to_number (buf, 4);
21623               newval &= 0xfff0f000;
21624               newval |= value & 0x0fff;
21625               newval |= (value & 0xf000) << 4;
21626               md_number_to_chars (buf, newval, 4);
21627             }
21628         }
21629       return;
21630
21631    case BFD_RELOC_ARM_ALU_PC_G0_NC:
21632    case BFD_RELOC_ARM_ALU_PC_G0:
21633    case BFD_RELOC_ARM_ALU_PC_G1_NC:
21634    case BFD_RELOC_ARM_ALU_PC_G1:
21635    case BFD_RELOC_ARM_ALU_PC_G2:
21636    case BFD_RELOC_ARM_ALU_SB_G0_NC:
21637    case BFD_RELOC_ARM_ALU_SB_G0:
21638    case BFD_RELOC_ARM_ALU_SB_G1_NC:
21639    case BFD_RELOC_ARM_ALU_SB_G1:
21640    case BFD_RELOC_ARM_ALU_SB_G2:
21641      gas_assert (!fixP->fx_done);
21642      if (!seg->use_rela_p)
21643        {
21644          bfd_vma insn;
21645          bfd_vma encoded_addend;
21646          bfd_vma addend_abs = abs (value);
21647
21648          /* Check that the absolute value of the addend can be
21649             expressed as an 8-bit constant plus a rotation.  */
21650          encoded_addend = encode_arm_immediate (addend_abs);
21651          if (encoded_addend == (unsigned int) FAIL)
21652            as_bad_where (fixP->fx_file, fixP->fx_line,
21653                          _("the offset 0x%08lX is not representable"),
21654                          (unsigned long) addend_abs);
21655
21656          /* Extract the instruction.  */
21657          insn = md_chars_to_number (buf, INSN_SIZE);
21658
21659          /* If the addend is positive, use an ADD instruction.
21660             Otherwise use a SUB.  Take care not to destroy the S bit.  */
21661          insn &= 0xff1fffff;
21662          if (value < 0)
21663            insn |= 1 << 22;
21664          else
21665            insn |= 1 << 23;
21666
21667          /* Place the encoded addend into the first 12 bits of the
21668             instruction.  */
21669          insn &= 0xfffff000;
21670          insn |= encoded_addend;
21671
21672          /* Update the instruction.  */
21673          md_number_to_chars (buf, insn, INSN_SIZE);
21674        }
21675      break;
21676
21677     case BFD_RELOC_ARM_LDR_PC_G0:
21678     case BFD_RELOC_ARM_LDR_PC_G1:
21679     case BFD_RELOC_ARM_LDR_PC_G2:
21680     case BFD_RELOC_ARM_LDR_SB_G0:
21681     case BFD_RELOC_ARM_LDR_SB_G1:
21682     case BFD_RELOC_ARM_LDR_SB_G2:
21683       gas_assert (!fixP->fx_done);
21684       if (!seg->use_rela_p)
21685         {
21686           bfd_vma insn;
21687           bfd_vma addend_abs = abs (value);
21688
21689           /* Check that the absolute value of the addend can be
21690              encoded in 12 bits.  */
21691           if (addend_abs >= 0x1000)
21692             as_bad_where (fixP->fx_file, fixP->fx_line,
21693                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
21694                           (unsigned long) addend_abs);
21695
21696           /* Extract the instruction.  */
21697           insn = md_chars_to_number (buf, INSN_SIZE);
21698
21699           /* If the addend is negative, clear bit 23 of the instruction.
21700              Otherwise set it.  */
21701           if (value < 0)
21702             insn &= ~(1 << 23);
21703           else
21704             insn |= 1 << 23;
21705
21706           /* Place the absolute value of the addend into the first 12 bits
21707              of the instruction.  */
21708           insn &= 0xfffff000;
21709           insn |= addend_abs;
21710
21711           /* Update the instruction.  */
21712           md_number_to_chars (buf, insn, INSN_SIZE);
21713         }
21714       break;
21715
21716     case BFD_RELOC_ARM_LDRS_PC_G0:
21717     case BFD_RELOC_ARM_LDRS_PC_G1:
21718     case BFD_RELOC_ARM_LDRS_PC_G2:
21719     case BFD_RELOC_ARM_LDRS_SB_G0:
21720     case BFD_RELOC_ARM_LDRS_SB_G1:
21721     case BFD_RELOC_ARM_LDRS_SB_G2:
21722       gas_assert (!fixP->fx_done);
21723       if (!seg->use_rela_p)
21724         {
21725           bfd_vma insn;
21726           bfd_vma addend_abs = abs (value);
21727
21728           /* Check that the absolute value of the addend can be
21729              encoded in 8 bits.  */
21730           if (addend_abs >= 0x100)
21731             as_bad_where (fixP->fx_file, fixP->fx_line,
21732                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
21733                           (unsigned long) addend_abs);
21734
21735           /* Extract the instruction.  */
21736           insn = md_chars_to_number (buf, INSN_SIZE);
21737
21738           /* If the addend is negative, clear bit 23 of the instruction.
21739              Otherwise set it.  */
21740           if (value < 0)
21741             insn &= ~(1 << 23);
21742           else
21743             insn |= 1 << 23;
21744
21745           /* Place the first four bits of the absolute value of the addend
21746              into the first 4 bits of the instruction, and the remaining
21747              four into bits 8 .. 11.  */
21748           insn &= 0xfffff0f0;
21749           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
21750
21751           /* Update the instruction.  */
21752           md_number_to_chars (buf, insn, INSN_SIZE);
21753         }
21754       break;
21755
21756     case BFD_RELOC_ARM_LDC_PC_G0:
21757     case BFD_RELOC_ARM_LDC_PC_G1:
21758     case BFD_RELOC_ARM_LDC_PC_G2:
21759     case BFD_RELOC_ARM_LDC_SB_G0:
21760     case BFD_RELOC_ARM_LDC_SB_G1:
21761     case BFD_RELOC_ARM_LDC_SB_G2:
21762       gas_assert (!fixP->fx_done);
21763       if (!seg->use_rela_p)
21764         {
21765           bfd_vma insn;
21766           bfd_vma addend_abs = abs (value);
21767
21768           /* Check that the absolute value of the addend is a multiple of
21769              four and, when divided by four, fits in 8 bits.  */
21770           if (addend_abs & 0x3)
21771             as_bad_where (fixP->fx_file, fixP->fx_line,
21772                           _("bad offset 0x%08lX (must be word-aligned)"),
21773                           (unsigned long) addend_abs);
21774
21775           if ((addend_abs >> 2) > 0xff)
21776             as_bad_where (fixP->fx_file, fixP->fx_line,
21777                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
21778                           (unsigned long) addend_abs);
21779
21780           /* Extract the instruction.  */
21781           insn = md_chars_to_number (buf, INSN_SIZE);
21782
21783           /* If the addend is negative, clear bit 23 of the instruction.
21784              Otherwise set it.  */
21785           if (value < 0)
21786             insn &= ~(1 << 23);
21787           else
21788             insn |= 1 << 23;
21789
21790           /* Place the addend (divided by four) into the first eight
21791              bits of the instruction.  */
21792           insn &= 0xfffffff0;
21793           insn |= addend_abs >> 2;
21794
21795           /* Update the instruction.  */
21796           md_number_to_chars (buf, insn, INSN_SIZE);
21797         }
21798       break;
21799
21800     case BFD_RELOC_ARM_V4BX:
21801       /* This will need to go in the object file.  */
21802       fixP->fx_done = 0;
21803       break;
21804
21805     case BFD_RELOC_UNUSED:
21806     default:
21807       as_bad_where (fixP->fx_file, fixP->fx_line,
21808                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
21809     }
21810 }
21811
21812 /* Translate internal representation of relocation info to BFD target
21813    format.  */
21814
21815 arelent *
21816 tc_gen_reloc (asection *section, fixS *fixp)
21817 {
21818   arelent * reloc;
21819   bfd_reloc_code_real_type code;
21820
21821   reloc = (arelent *) xmalloc (sizeof (arelent));
21822
21823   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
21824   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
21825   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
21826
21827   if (fixp->fx_pcrel)
21828     {
21829       if (section->use_rela_p)
21830         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
21831       else
21832         fixp->fx_offset = reloc->address;
21833     }
21834   reloc->addend = fixp->fx_offset;
21835
21836   switch (fixp->fx_r_type)
21837     {
21838     case BFD_RELOC_8:
21839       if (fixp->fx_pcrel)
21840         {
21841           code = BFD_RELOC_8_PCREL;
21842           break;
21843         }
21844
21845     case BFD_RELOC_16:
21846       if (fixp->fx_pcrel)
21847         {
21848           code = BFD_RELOC_16_PCREL;
21849           break;
21850         }
21851
21852     case BFD_RELOC_32:
21853       if (fixp->fx_pcrel)
21854         {
21855           code = BFD_RELOC_32_PCREL;
21856           break;
21857         }
21858
21859     case BFD_RELOC_ARM_MOVW:
21860       if (fixp->fx_pcrel)
21861         {
21862           code = BFD_RELOC_ARM_MOVW_PCREL;
21863           break;
21864         }
21865
21866     case BFD_RELOC_ARM_MOVT:
21867       if (fixp->fx_pcrel)
21868         {
21869           code = BFD_RELOC_ARM_MOVT_PCREL;
21870           break;
21871         }
21872
21873     case BFD_RELOC_ARM_THUMB_MOVW:
21874       if (fixp->fx_pcrel)
21875         {
21876           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
21877           break;
21878         }
21879
21880     case BFD_RELOC_ARM_THUMB_MOVT:
21881       if (fixp->fx_pcrel)
21882         {
21883           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
21884           break;
21885         }
21886
21887     case BFD_RELOC_NONE:
21888     case BFD_RELOC_ARM_PCREL_BRANCH:
21889     case BFD_RELOC_ARM_PCREL_BLX:
21890     case BFD_RELOC_RVA:
21891     case BFD_RELOC_THUMB_PCREL_BRANCH7:
21892     case BFD_RELOC_THUMB_PCREL_BRANCH9:
21893     case BFD_RELOC_THUMB_PCREL_BRANCH12:
21894     case BFD_RELOC_THUMB_PCREL_BRANCH20:
21895     case BFD_RELOC_THUMB_PCREL_BRANCH23:
21896     case BFD_RELOC_THUMB_PCREL_BRANCH25:
21897     case BFD_RELOC_VTABLE_ENTRY:
21898     case BFD_RELOC_VTABLE_INHERIT:
21899 #ifdef TE_PE
21900     case BFD_RELOC_32_SECREL:
21901 #endif
21902       code = fixp->fx_r_type;
21903       break;
21904
21905     case BFD_RELOC_THUMB_PCREL_BLX:
21906 #ifdef OBJ_ELF
21907       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
21908         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
21909       else
21910 #endif
21911         code = BFD_RELOC_THUMB_PCREL_BLX;
21912       break;
21913
21914     case BFD_RELOC_ARM_LITERAL:
21915     case BFD_RELOC_ARM_HWLITERAL:
21916       /* If this is called then the a literal has
21917          been referenced across a section boundary.  */
21918       as_bad_where (fixp->fx_file, fixp->fx_line,
21919                     _("literal referenced across section boundary"));
21920       return NULL;
21921
21922 #ifdef OBJ_ELF
21923     case BFD_RELOC_ARM_TLS_CALL:
21924     case BFD_RELOC_ARM_THM_TLS_CALL:
21925     case BFD_RELOC_ARM_TLS_DESCSEQ:
21926     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
21927     case BFD_RELOC_ARM_GOT32:
21928     case BFD_RELOC_ARM_GOTOFF:
21929     case BFD_RELOC_ARM_GOT_PREL:
21930     case BFD_RELOC_ARM_PLT32:
21931     case BFD_RELOC_ARM_TARGET1:
21932     case BFD_RELOC_ARM_ROSEGREL32:
21933     case BFD_RELOC_ARM_SBREL32:
21934     case BFD_RELOC_ARM_PREL31:
21935     case BFD_RELOC_ARM_TARGET2:
21936     case BFD_RELOC_ARM_TLS_LE32:
21937     case BFD_RELOC_ARM_TLS_LDO32:
21938     case BFD_RELOC_ARM_PCREL_CALL:
21939     case BFD_RELOC_ARM_PCREL_JUMP:
21940     case BFD_RELOC_ARM_ALU_PC_G0_NC:
21941     case BFD_RELOC_ARM_ALU_PC_G0:
21942     case BFD_RELOC_ARM_ALU_PC_G1_NC:
21943     case BFD_RELOC_ARM_ALU_PC_G1:
21944     case BFD_RELOC_ARM_ALU_PC_G2:
21945     case BFD_RELOC_ARM_LDR_PC_G0:
21946     case BFD_RELOC_ARM_LDR_PC_G1:
21947     case BFD_RELOC_ARM_LDR_PC_G2:
21948     case BFD_RELOC_ARM_LDRS_PC_G0:
21949     case BFD_RELOC_ARM_LDRS_PC_G1:
21950     case BFD_RELOC_ARM_LDRS_PC_G2:
21951     case BFD_RELOC_ARM_LDC_PC_G0:
21952     case BFD_RELOC_ARM_LDC_PC_G1:
21953     case BFD_RELOC_ARM_LDC_PC_G2:
21954     case BFD_RELOC_ARM_ALU_SB_G0_NC:
21955     case BFD_RELOC_ARM_ALU_SB_G0:
21956     case BFD_RELOC_ARM_ALU_SB_G1_NC:
21957     case BFD_RELOC_ARM_ALU_SB_G1:
21958     case BFD_RELOC_ARM_ALU_SB_G2:
21959     case BFD_RELOC_ARM_LDR_SB_G0:
21960     case BFD_RELOC_ARM_LDR_SB_G1:
21961     case BFD_RELOC_ARM_LDR_SB_G2:
21962     case BFD_RELOC_ARM_LDRS_SB_G0:
21963     case BFD_RELOC_ARM_LDRS_SB_G1:
21964     case BFD_RELOC_ARM_LDRS_SB_G2:
21965     case BFD_RELOC_ARM_LDC_SB_G0:
21966     case BFD_RELOC_ARM_LDC_SB_G1:
21967     case BFD_RELOC_ARM_LDC_SB_G2:
21968     case BFD_RELOC_ARM_V4BX:
21969       code = fixp->fx_r_type;
21970       break;
21971
21972     case BFD_RELOC_ARM_TLS_GOTDESC:
21973     case BFD_RELOC_ARM_TLS_GD32:
21974     case BFD_RELOC_ARM_TLS_IE32:
21975     case BFD_RELOC_ARM_TLS_LDM32:
21976       /* BFD will include the symbol's address in the addend.
21977          But we don't want that, so subtract it out again here.  */
21978       if (!S_IS_COMMON (fixp->fx_addsy))
21979         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
21980       code = fixp->fx_r_type;
21981       break;
21982 #endif
21983
21984     case BFD_RELOC_ARM_IMMEDIATE:
21985       as_bad_where (fixp->fx_file, fixp->fx_line,
21986                     _("internal relocation (type: IMMEDIATE) not fixed up"));
21987       return NULL;
21988
21989     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
21990       as_bad_where (fixp->fx_file, fixp->fx_line,
21991                     _("ADRL used for a symbol not defined in the same file"));
21992       return NULL;
21993
21994     case BFD_RELOC_ARM_OFFSET_IMM:
21995       if (section->use_rela_p)
21996         {
21997           code = fixp->fx_r_type;
21998           break;
21999         }
22000
22001       if (fixp->fx_addsy != NULL
22002           && !S_IS_DEFINED (fixp->fx_addsy)
22003           && S_IS_LOCAL (fixp->fx_addsy))
22004         {
22005           as_bad_where (fixp->fx_file, fixp->fx_line,
22006                         _("undefined local label `%s'"),
22007                         S_GET_NAME (fixp->fx_addsy));
22008           return NULL;
22009         }
22010
22011       as_bad_where (fixp->fx_file, fixp->fx_line,
22012                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
22013       return NULL;
22014
22015     default:
22016       {
22017         char * type;
22018
22019         switch (fixp->fx_r_type)
22020           {
22021           case BFD_RELOC_NONE:             type = "NONE";         break;
22022           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
22023           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
22024           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
22025           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
22026           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
22027           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
22028           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
22029           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
22030           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
22031           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
22032           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
22033           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
22034           default:                         type = _("<unknown>"); break;
22035           }
22036         as_bad_where (fixp->fx_file, fixp->fx_line,
22037                       _("cannot represent %s relocation in this object file format"),
22038                       type);
22039         return NULL;
22040       }
22041     }
22042
22043 #ifdef OBJ_ELF
22044   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
22045       && GOT_symbol
22046       && fixp->fx_addsy == GOT_symbol)
22047     {
22048       code = BFD_RELOC_ARM_GOTPC;
22049       reloc->addend = fixp->fx_offset = reloc->address;
22050     }
22051 #endif
22052
22053   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
22054
22055   if (reloc->howto == NULL)
22056     {
22057       as_bad_where (fixp->fx_file, fixp->fx_line,
22058                     _("cannot represent %s relocation in this object file format"),
22059                     bfd_get_reloc_code_name (code));
22060       return NULL;
22061     }
22062
22063   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
22064      vtable entry to be used in the relocation's section offset.  */
22065   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
22066     reloc->address = fixp->fx_offset;
22067
22068   return reloc;
22069 }
22070
22071 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
22072
22073 void
22074 cons_fix_new_arm (fragS *       frag,
22075                   int           where,
22076                   int           size,
22077                   expressionS * exp)
22078 {
22079   bfd_reloc_code_real_type type;
22080   int pcrel = 0;
22081
22082   /* Pick a reloc.
22083      FIXME: @@ Should look at CPU word size.  */
22084   switch (size)
22085     {
22086     case 1:
22087       type = BFD_RELOC_8;
22088       break;
22089     case 2:
22090       type = BFD_RELOC_16;
22091       break;
22092     case 4:
22093     default:
22094       type = BFD_RELOC_32;
22095       break;
22096     case 8:
22097       type = BFD_RELOC_64;
22098       break;
22099     }
22100
22101 #ifdef TE_PE
22102   if (exp->X_op == O_secrel)
22103   {
22104     exp->X_op = O_symbol;
22105     type = BFD_RELOC_32_SECREL;
22106   }
22107 #endif
22108
22109   fix_new_exp (frag, where, (int) size, exp, pcrel, type);
22110 }
22111
22112 #if defined (OBJ_COFF)
22113 void
22114 arm_validate_fix (fixS * fixP)
22115 {
22116   /* If the destination of the branch is a defined symbol which does not have
22117      the THUMB_FUNC attribute, then we must be calling a function which has
22118      the (interfacearm) attribute.  We look for the Thumb entry point to that
22119      function and change the branch to refer to that function instead.  */
22120   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
22121       && fixP->fx_addsy != NULL
22122       && S_IS_DEFINED (fixP->fx_addsy)
22123       && ! THUMB_IS_FUNC (fixP->fx_addsy))
22124     {
22125       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
22126     }
22127 }
22128 #endif
22129
22130
22131 int
22132 arm_force_relocation (struct fix * fixp)
22133 {
22134 #if defined (OBJ_COFF) && defined (TE_PE)
22135   if (fixp->fx_r_type == BFD_RELOC_RVA)
22136     return 1;
22137 #endif
22138
22139   /* In case we have a call or a branch to a function in ARM ISA mode from
22140      a thumb function or vice-versa force the relocation. These relocations
22141      are cleared off for some cores that might have blx and simple transformations
22142      are possible.  */
22143
22144 #ifdef OBJ_ELF
22145   switch (fixp->fx_r_type)
22146     {
22147     case BFD_RELOC_ARM_PCREL_JUMP:
22148     case BFD_RELOC_ARM_PCREL_CALL:
22149     case BFD_RELOC_THUMB_PCREL_BLX:
22150       if (THUMB_IS_FUNC (fixp->fx_addsy))
22151         return 1;
22152       break;
22153
22154     case BFD_RELOC_ARM_PCREL_BLX:
22155     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22156     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22157     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22158       if (ARM_IS_FUNC (fixp->fx_addsy))
22159         return 1;
22160       break;
22161
22162     default:
22163       break;
22164     }
22165 #endif
22166
22167   /* Resolve these relocations even if the symbol is extern or weak.
22168      Technically this is probably wrong due to symbol preemption.
22169      In practice these relocations do not have enough range to be useful
22170      at dynamic link time, and some code (e.g. in the Linux kernel)
22171      expects these references to be resolved.  */
22172   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
22173       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
22174       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
22175       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
22176       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
22177       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
22178       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
22179       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
22180       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
22181       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
22182       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
22183       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
22184       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
22185       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
22186     return 0;
22187
22188   /* Always leave these relocations for the linker.  */
22189   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
22190        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
22191       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
22192     return 1;
22193
22194   /* Always generate relocations against function symbols.  */
22195   if (fixp->fx_r_type == BFD_RELOC_32
22196       && fixp->fx_addsy
22197       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
22198     return 1;
22199
22200   return generic_force_reloc (fixp);
22201 }
22202
22203 #if defined (OBJ_ELF) || defined (OBJ_COFF)
22204 /* Relocations against function names must be left unadjusted,
22205    so that the linker can use this information to generate interworking
22206    stubs.  The MIPS version of this function
22207    also prevents relocations that are mips-16 specific, but I do not
22208    know why it does this.
22209
22210    FIXME:
22211    There is one other problem that ought to be addressed here, but
22212    which currently is not:  Taking the address of a label (rather
22213    than a function) and then later jumping to that address.  Such
22214    addresses also ought to have their bottom bit set (assuming that
22215    they reside in Thumb code), but at the moment they will not.  */
22216
22217 bfd_boolean
22218 arm_fix_adjustable (fixS * fixP)
22219 {
22220   if (fixP->fx_addsy == NULL)
22221     return 1;
22222
22223   /* Preserve relocations against symbols with function type.  */
22224   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
22225     return FALSE;
22226
22227   if (THUMB_IS_FUNC (fixP->fx_addsy)
22228       && fixP->fx_subsy == NULL)
22229     return FALSE;
22230
22231   /* We need the symbol name for the VTABLE entries.  */
22232   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
22233       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
22234     return FALSE;
22235
22236   /* Don't allow symbols to be discarded on GOT related relocs.  */
22237   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
22238       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
22239       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
22240       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
22241       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
22242       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
22243       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
22244       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
22245       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
22246       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
22247       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
22248       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
22249       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
22250       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
22251     return FALSE;
22252
22253   /* Similarly for group relocations.  */
22254   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
22255        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
22256       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
22257     return FALSE;
22258
22259   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
22260   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
22261       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
22262       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
22263       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
22264       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
22265       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
22266       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
22267       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
22268     return FALSE;
22269
22270   return TRUE;
22271 }
22272 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
22273
22274 #ifdef OBJ_ELF
22275
22276 const char *
22277 elf32_arm_target_format (void)
22278 {
22279 #ifdef TE_SYMBIAN
22280   return (target_big_endian
22281           ? "elf32-bigarm-symbian"
22282           : "elf32-littlearm-symbian");
22283 #elif defined (TE_VXWORKS)
22284   return (target_big_endian
22285           ? "elf32-bigarm-vxworks"
22286           : "elf32-littlearm-vxworks");
22287 #else
22288   if (target_big_endian)
22289     return "elf32-bigarm";
22290   else
22291     return "elf32-littlearm";
22292 #endif
22293 }
22294
22295 void
22296 armelf_frob_symbol (symbolS * symp,
22297                     int *     puntp)
22298 {
22299   elf_frob_symbol (symp, puntp);
22300 }
22301 #endif
22302
22303 /* MD interface: Finalization.  */
22304
22305 void
22306 arm_cleanup (void)
22307 {
22308   literal_pool * pool;
22309
22310   /* Ensure that all the IT blocks are properly closed.  */
22311   check_it_blocks_finished ();
22312
22313   for (pool = list_of_pools; pool; pool = pool->next)
22314     {
22315       /* Put it at the end of the relevant section.  */
22316       subseg_set (pool->section, pool->sub_section);
22317 #ifdef OBJ_ELF
22318       arm_elf_change_section ();
22319 #endif
22320       s_ltorg (0);
22321     }
22322 }
22323
22324 #ifdef OBJ_ELF
22325 /* Remove any excess mapping symbols generated for alignment frags in
22326    SEC.  We may have created a mapping symbol before a zero byte
22327    alignment; remove it if there's a mapping symbol after the
22328    alignment.  */
22329 static void
22330 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
22331                        void *dummy ATTRIBUTE_UNUSED)
22332 {
22333   segment_info_type *seginfo = seg_info (sec);
22334   fragS *fragp;
22335
22336   if (seginfo == NULL || seginfo->frchainP == NULL)
22337     return;
22338
22339   for (fragp = seginfo->frchainP->frch_root;
22340        fragp != NULL;
22341        fragp = fragp->fr_next)
22342     {
22343       symbolS *sym = fragp->tc_frag_data.last_map;
22344       fragS *next = fragp->fr_next;
22345
22346       /* Variable-sized frags have been converted to fixed size by
22347          this point.  But if this was variable-sized to start with,
22348          there will be a fixed-size frag after it.  So don't handle
22349          next == NULL.  */
22350       if (sym == NULL || next == NULL)
22351         continue;
22352
22353       if (S_GET_VALUE (sym) < next->fr_address)
22354         /* Not at the end of this frag.  */
22355         continue;
22356       know (S_GET_VALUE (sym) == next->fr_address);
22357
22358       do
22359         {
22360           if (next->tc_frag_data.first_map != NULL)
22361             {
22362               /* Next frag starts with a mapping symbol.  Discard this
22363                  one.  */
22364               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
22365               break;
22366             }
22367
22368           if (next->fr_next == NULL)
22369             {
22370               /* This mapping symbol is at the end of the section.  Discard
22371                  it.  */
22372               know (next->fr_fix == 0 && next->fr_var == 0);
22373               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
22374               break;
22375             }
22376
22377           /* As long as we have empty frags without any mapping symbols,
22378              keep looking.  */
22379           /* If the next frag is non-empty and does not start with a
22380              mapping symbol, then this mapping symbol is required.  */
22381           if (next->fr_address != next->fr_next->fr_address)
22382             break;
22383
22384           next = next->fr_next;
22385         }
22386       while (next != NULL);
22387     }
22388 }
22389 #endif
22390
22391 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
22392    ARM ones.  */
22393
22394 void
22395 arm_adjust_symtab (void)
22396 {
22397 #ifdef OBJ_COFF
22398   symbolS * sym;
22399
22400   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
22401     {
22402       if (ARM_IS_THUMB (sym))
22403         {
22404           if (THUMB_IS_FUNC (sym))
22405             {
22406               /* Mark the symbol as a Thumb function.  */
22407               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
22408                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
22409                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
22410
22411               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
22412                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
22413               else
22414                 as_bad (_("%s: unexpected function type: %d"),
22415                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
22416             }
22417           else switch (S_GET_STORAGE_CLASS (sym))
22418             {
22419             case C_EXT:
22420               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
22421               break;
22422             case C_STAT:
22423               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
22424               break;
22425             case C_LABEL:
22426               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
22427               break;
22428             default:
22429               /* Do nothing.  */
22430               break;
22431             }
22432         }
22433
22434       if (ARM_IS_INTERWORK (sym))
22435         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
22436     }
22437 #endif
22438 #ifdef OBJ_ELF
22439   symbolS * sym;
22440   char      bind;
22441
22442   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
22443     {
22444       if (ARM_IS_THUMB (sym))
22445         {
22446           elf_symbol_type * elf_sym;
22447
22448           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
22449           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
22450
22451           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
22452                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
22453             {
22454               /* If it's a .thumb_func, declare it as so,
22455                  otherwise tag label as .code 16.  */
22456               if (THUMB_IS_FUNC (sym))
22457                 elf_sym->internal_elf_sym.st_target_internal
22458                   = ST_BRANCH_TO_THUMB;
22459               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
22460                 elf_sym->internal_elf_sym.st_info =
22461                   ELF_ST_INFO (bind, STT_ARM_16BIT);
22462             }
22463         }
22464     }
22465
22466   /* Remove any overlapping mapping symbols generated by alignment frags.  */
22467   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
22468   /* Now do generic ELF adjustments.  */
22469   elf_adjust_symtab ();
22470 #endif
22471 }
22472
22473 /* MD interface: Initialization.  */
22474
22475 static void
22476 set_constant_flonums (void)
22477 {
22478   int i;
22479
22480   for (i = 0; i < NUM_FLOAT_VALS; i++)
22481     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
22482       abort ();
22483 }
22484
22485 /* Auto-select Thumb mode if it's the only available instruction set for the
22486    given architecture.  */
22487
22488 static void
22489 autoselect_thumb_from_cpu_variant (void)
22490 {
22491   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
22492     opcode_select (16);
22493 }
22494
22495 void
22496 md_begin (void)
22497 {
22498   unsigned mach;
22499   unsigned int i;
22500
22501   if (   (arm_ops_hsh = hash_new ()) == NULL
22502       || (arm_cond_hsh = hash_new ()) == NULL
22503       || (arm_shift_hsh = hash_new ()) == NULL
22504       || (arm_psr_hsh = hash_new ()) == NULL
22505       || (arm_v7m_psr_hsh = hash_new ()) == NULL
22506       || (arm_reg_hsh = hash_new ()) == NULL
22507       || (arm_reloc_hsh = hash_new ()) == NULL
22508       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
22509     as_fatal (_("virtual memory exhausted"));
22510
22511   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
22512     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
22513   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
22514     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
22515   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
22516     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
22517   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
22518     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
22519   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
22520     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
22521                  (void *) (v7m_psrs + i));
22522   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
22523     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
22524   for (i = 0;
22525        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
22526        i++)
22527     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
22528                  (void *) (barrier_opt_names + i));
22529 #ifdef OBJ_ELF
22530   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
22531     {
22532       struct reloc_entry * entry = reloc_names + i;
22533
22534       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
22535         /* This makes encode_branch() use the EABI versions of this relocation.  */
22536         entry->reloc = BFD_RELOC_UNUSED;
22537
22538       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
22539     }
22540 #endif
22541
22542   set_constant_flonums ();
22543
22544   /* Set the cpu variant based on the command-line options.  We prefer
22545      -mcpu= over -march= if both are set (as for GCC); and we prefer
22546      -mfpu= over any other way of setting the floating point unit.
22547      Use of legacy options with new options are faulted.  */
22548   if (legacy_cpu)
22549     {
22550       if (mcpu_cpu_opt || march_cpu_opt)
22551         as_bad (_("use of old and new-style options to set CPU type"));
22552
22553       mcpu_cpu_opt = legacy_cpu;
22554     }
22555   else if (!mcpu_cpu_opt)
22556     mcpu_cpu_opt = march_cpu_opt;
22557
22558   if (legacy_fpu)
22559     {
22560       if (mfpu_opt)
22561         as_bad (_("use of old and new-style options to set FPU type"));
22562
22563       mfpu_opt = legacy_fpu;
22564     }
22565   else if (!mfpu_opt)
22566     {
22567 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
22568         || defined (TE_NetBSD) || defined (TE_VXWORKS))
22569       /* Some environments specify a default FPU.  If they don't, infer it
22570          from the processor.  */
22571       if (mcpu_fpu_opt)
22572         mfpu_opt = mcpu_fpu_opt;
22573       else
22574         mfpu_opt = march_fpu_opt;
22575 #else
22576       mfpu_opt = &fpu_default;
22577 #endif
22578     }
22579
22580   if (!mfpu_opt)
22581     {
22582       if (mcpu_cpu_opt != NULL)
22583         mfpu_opt = &fpu_default;
22584       else if (mcpu_fpu_opt != NULL && ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
22585         mfpu_opt = &fpu_arch_vfp_v2;
22586       else
22587         mfpu_opt = &fpu_arch_fpa;
22588     }
22589
22590 #ifdef CPU_DEFAULT
22591   if (!mcpu_cpu_opt)
22592     {
22593       mcpu_cpu_opt = &cpu_default;
22594       selected_cpu = cpu_default;
22595     }
22596 #else
22597   if (mcpu_cpu_opt)
22598     selected_cpu = *mcpu_cpu_opt;
22599   else
22600     mcpu_cpu_opt = &arm_arch_any;
22601 #endif
22602
22603   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
22604
22605   autoselect_thumb_from_cpu_variant ();
22606
22607   arm_arch_used = thumb_arch_used = arm_arch_none;
22608
22609 #if defined OBJ_COFF || defined OBJ_ELF
22610   {
22611     unsigned int flags = 0;
22612
22613 #if defined OBJ_ELF
22614     flags = meabi_flags;
22615
22616     switch (meabi_flags)
22617       {
22618       case EF_ARM_EABI_UNKNOWN:
22619 #endif
22620         /* Set the flags in the private structure.  */
22621         if (uses_apcs_26)      flags |= F_APCS26;
22622         if (support_interwork) flags |= F_INTERWORK;
22623         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
22624         if (pic_code)          flags |= F_PIC;
22625         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
22626           flags |= F_SOFT_FLOAT;
22627
22628         switch (mfloat_abi_opt)
22629           {
22630           case ARM_FLOAT_ABI_SOFT:
22631           case ARM_FLOAT_ABI_SOFTFP:
22632             flags |= F_SOFT_FLOAT;
22633             break;
22634
22635           case ARM_FLOAT_ABI_HARD:
22636             if (flags & F_SOFT_FLOAT)
22637               as_bad (_("hard-float conflicts with specified fpu"));
22638             break;
22639           }
22640
22641         /* Using pure-endian doubles (even if soft-float).      */
22642         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
22643           flags |= F_VFP_FLOAT;
22644
22645 #if defined OBJ_ELF
22646         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
22647             flags |= EF_ARM_MAVERICK_FLOAT;
22648         break;
22649
22650       case EF_ARM_EABI_VER4:
22651       case EF_ARM_EABI_VER5:
22652         /* No additional flags to set.  */
22653         break;
22654
22655       default:
22656         abort ();
22657       }
22658 #endif
22659     bfd_set_private_flags (stdoutput, flags);
22660
22661     /* We have run out flags in the COFF header to encode the
22662        status of ATPCS support, so instead we create a dummy,
22663        empty, debug section called .arm.atpcs.  */
22664     if (atpcs)
22665       {
22666         asection * sec;
22667
22668         sec = bfd_make_section (stdoutput, ".arm.atpcs");
22669
22670         if (sec != NULL)
22671           {
22672             bfd_set_section_flags
22673               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
22674             bfd_set_section_size (stdoutput, sec, 0);
22675             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
22676           }
22677       }
22678   }
22679 #endif
22680
22681   /* Record the CPU type as well.  */
22682   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
22683     mach = bfd_mach_arm_iWMMXt2;
22684   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
22685     mach = bfd_mach_arm_iWMMXt;
22686   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
22687     mach = bfd_mach_arm_XScale;
22688   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
22689     mach = bfd_mach_arm_ep9312;
22690   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
22691     mach = bfd_mach_arm_5TE;
22692   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
22693     {
22694       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
22695         mach = bfd_mach_arm_5T;
22696       else
22697         mach = bfd_mach_arm_5;
22698     }
22699   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
22700     {
22701       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
22702         mach = bfd_mach_arm_4T;
22703       else
22704         mach = bfd_mach_arm_4;
22705     }
22706   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
22707     mach = bfd_mach_arm_3M;
22708   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
22709     mach = bfd_mach_arm_3;
22710   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
22711     mach = bfd_mach_arm_2a;
22712   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
22713     mach = bfd_mach_arm_2;
22714   else
22715     mach = bfd_mach_arm_unknown;
22716
22717   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
22718 }
22719
22720 /* Command line processing.  */
22721
22722 /* md_parse_option
22723       Invocation line includes a switch not recognized by the base assembler.
22724       See if it's a processor-specific option.
22725
22726       This routine is somewhat complicated by the need for backwards
22727       compatibility (since older releases of gcc can't be changed).
22728       The new options try to make the interface as compatible as
22729       possible with GCC.
22730
22731       New options (supported) are:
22732
22733               -mcpu=<cpu name>           Assemble for selected processor
22734               -march=<architecture name> Assemble for selected architecture
22735               -mfpu=<fpu architecture>   Assemble for selected FPU.
22736               -EB/-mbig-endian           Big-endian
22737               -EL/-mlittle-endian        Little-endian
22738               -k                         Generate PIC code
22739               -mthumb                    Start in Thumb mode
22740               -mthumb-interwork          Code supports ARM/Thumb interworking
22741
22742               -m[no-]warn-deprecated     Warn about deprecated features
22743
22744       For now we will also provide support for:
22745
22746               -mapcs-32                  32-bit Program counter
22747               -mapcs-26                  26-bit Program counter
22748               -macps-float               Floats passed in FP registers
22749               -mapcs-reentrant           Reentrant code
22750               -matpcs
22751       (sometime these will probably be replaced with -mapcs=<list of options>
22752       and -matpcs=<list of options>)
22753
22754       The remaining options are only supported for back-wards compatibility.
22755       Cpu variants, the arm part is optional:
22756               -m[arm]1                Currently not supported.
22757               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
22758               -m[arm]3                Arm 3 processor
22759               -m[arm]6[xx],           Arm 6 processors
22760               -m[arm]7[xx][t][[d]m]   Arm 7 processors
22761               -m[arm]8[10]            Arm 8 processors
22762               -m[arm]9[20][tdmi]      Arm 9 processors
22763               -mstrongarm[110[0]]     StrongARM processors
22764               -mxscale                XScale processors
22765               -m[arm]v[2345[t[e]]]    Arm architectures
22766               -mall                   All (except the ARM1)
22767       FP variants:
22768               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
22769               -mfpe-old               (No float load/store multiples)
22770               -mvfpxd                 VFP Single precision
22771               -mvfp                   All VFP
22772               -mno-fpu                Disable all floating point instructions
22773
22774       The following CPU names are recognized:
22775               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
22776               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
22777               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
22778               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
22779               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
22780               arm10t arm10e, arm1020t, arm1020e, arm10200e,
22781               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
22782
22783       */
22784
22785 const char * md_shortopts = "m:k";
22786
22787 #ifdef ARM_BI_ENDIAN
22788 #define OPTION_EB (OPTION_MD_BASE + 0)
22789 #define OPTION_EL (OPTION_MD_BASE + 1)
22790 #else
22791 #if TARGET_BYTES_BIG_ENDIAN
22792 #define OPTION_EB (OPTION_MD_BASE + 0)
22793 #else
22794 #define OPTION_EL (OPTION_MD_BASE + 1)
22795 #endif
22796 #endif
22797 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
22798
22799 struct option md_longopts[] =
22800 {
22801 #ifdef OPTION_EB
22802   {"EB", no_argument, NULL, OPTION_EB},
22803 #endif
22804 #ifdef OPTION_EL
22805   {"EL", no_argument, NULL, OPTION_EL},
22806 #endif
22807   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
22808   {NULL, no_argument, NULL, 0}
22809 };
22810
22811 size_t md_longopts_size = sizeof (md_longopts);
22812
22813 struct arm_option_table
22814 {
22815   char *option;         /* Option name to match.  */
22816   char *help;           /* Help information.  */
22817   int  *var;            /* Variable to change.  */
22818   int   value;          /* What to change it to.  */
22819   char *deprecated;     /* If non-null, print this message.  */
22820 };
22821
22822 struct arm_option_table arm_opts[] =
22823 {
22824   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
22825   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
22826   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
22827    &support_interwork, 1, NULL},
22828   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
22829   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
22830   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
22831    1, NULL},
22832   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
22833   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
22834   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
22835   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
22836    NULL},
22837
22838   /* These are recognized by the assembler, but have no affect on code.  */
22839   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
22840   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
22841
22842   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
22843   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
22844    &warn_on_deprecated, 0, NULL},
22845   {NULL, NULL, NULL, 0, NULL}
22846 };
22847
22848 struct arm_legacy_option_table
22849 {
22850   char *option;                         /* Option name to match.  */
22851   const arm_feature_set **var;          /* Variable to change.  */
22852   const arm_feature_set value;          /* What to change it to.  */
22853   char *deprecated;                     /* If non-null, print this message.  */
22854 };
22855
22856 const struct arm_legacy_option_table arm_legacy_opts[] =
22857 {
22858   /* DON'T add any new processors to this list -- we want the whole list
22859      to go away...  Add them to the processors table instead.  */
22860   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
22861   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
22862   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
22863   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
22864   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
22865   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
22866   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
22867   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
22868   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
22869   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
22870   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
22871   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
22872   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
22873   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
22874   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
22875   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
22876   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
22877   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
22878   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
22879   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
22880   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
22881   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
22882   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
22883   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
22884   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
22885   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
22886   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
22887   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
22888   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
22889   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
22890   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
22891   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
22892   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
22893   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
22894   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
22895   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
22896   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
22897   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
22898   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
22899   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
22900   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
22901   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
22902   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
22903   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
22904   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
22905   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
22906   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22907   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22908   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22909   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22910   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
22911   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
22912   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
22913   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
22914   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
22915   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
22916   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
22917   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
22918   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
22919   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
22920   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
22921   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
22922   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
22923   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
22924   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
22925   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
22926   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
22927   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
22928   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
22929   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
22930    N_("use -mcpu=strongarm110")},
22931   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
22932    N_("use -mcpu=strongarm1100")},
22933   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
22934    N_("use -mcpu=strongarm1110")},
22935   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
22936   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
22937   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
22938
22939   /* Architecture variants -- don't add any more to this list either.  */
22940   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
22941   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
22942   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
22943   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
22944   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
22945   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
22946   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
22947   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
22948   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
22949   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
22950   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
22951   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
22952   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
22953   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
22954   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
22955   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
22956   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
22957   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
22958
22959   /* Floating point variants -- don't add any more to this list either.  */
22960   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
22961   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
22962   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
22963   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
22964    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
22965
22966   {NULL, NULL, ARM_ARCH_NONE, NULL}
22967 };
22968
22969 struct arm_cpu_option_table
22970 {
22971   char *name;
22972   size_t name_len;
22973   const arm_feature_set value;
22974   /* For some CPUs we assume an FPU unless the user explicitly sets
22975      -mfpu=...  */
22976   const arm_feature_set default_fpu;
22977   /* The canonical name of the CPU, or NULL to use NAME converted to upper
22978      case.  */
22979   const char *canonical_name;
22980 };
22981
22982 /* This list should, at a minimum, contain all the cpu names
22983    recognized by GCC.  */
22984 #define ARM_CPU_OPT(N, V, DF, CN) { N, sizeof (N) - 1, V, DF, CN }
22985 static const struct arm_cpu_option_table arm_cpus[] =
22986 {
22987   ARM_CPU_OPT ("all",           ARM_ANY,         FPU_ARCH_FPA,    NULL),
22988   ARM_CPU_OPT ("arm1",          ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL),
22989   ARM_CPU_OPT ("arm2",          ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL),
22990   ARM_CPU_OPT ("arm250",        ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
22991   ARM_CPU_OPT ("arm3",          ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
22992   ARM_CPU_OPT ("arm6",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
22993   ARM_CPU_OPT ("arm60",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
22994   ARM_CPU_OPT ("arm600",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
22995   ARM_CPU_OPT ("arm610",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
22996   ARM_CPU_OPT ("arm620",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
22997   ARM_CPU_OPT ("arm7",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
22998   ARM_CPU_OPT ("arm7m",         ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
22999   ARM_CPU_OPT ("arm7d",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23000   ARM_CPU_OPT ("arm7dm",        ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
23001   ARM_CPU_OPT ("arm7di",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23002   ARM_CPU_OPT ("arm7dmi",       ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
23003   ARM_CPU_OPT ("arm70",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23004   ARM_CPU_OPT ("arm700",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23005   ARM_CPU_OPT ("arm700i",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23006   ARM_CPU_OPT ("arm710",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23007   ARM_CPU_OPT ("arm710t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23008   ARM_CPU_OPT ("arm720",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23009   ARM_CPU_OPT ("arm720t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23010   ARM_CPU_OPT ("arm740t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23011   ARM_CPU_OPT ("arm710c",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23012   ARM_CPU_OPT ("arm7100",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23013   ARM_CPU_OPT ("arm7500",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23014   ARM_CPU_OPT ("arm7500fe",     ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
23015   ARM_CPU_OPT ("arm7t",         ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23016   ARM_CPU_OPT ("arm7tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23017   ARM_CPU_OPT ("arm7tdmi-s",    ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23018   ARM_CPU_OPT ("arm8",          ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23019   ARM_CPU_OPT ("arm810",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23020   ARM_CPU_OPT ("strongarm",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23021   ARM_CPU_OPT ("strongarm1",    ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23022   ARM_CPU_OPT ("strongarm110",  ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23023   ARM_CPU_OPT ("strongarm1100", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23024   ARM_CPU_OPT ("strongarm1110", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23025   ARM_CPU_OPT ("arm9",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23026   ARM_CPU_OPT ("arm920",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"),
23027   ARM_CPU_OPT ("arm920t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23028   ARM_CPU_OPT ("arm922t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23029   ARM_CPU_OPT ("arm940t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23030   ARM_CPU_OPT ("arm9tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
23031   ARM_CPU_OPT ("fa526",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23032   ARM_CPU_OPT ("fa626",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
23033   /* For V5 or later processors we default to using VFP; but the user
23034      should really set the FPU type explicitly.  */
23035   ARM_CPU_OPT ("arm9e-r0",      ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
23036   ARM_CPU_OPT ("arm9e",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23037   ARM_CPU_OPT ("arm926ej",      ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
23038   ARM_CPU_OPT ("arm926ejs",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
23039   ARM_CPU_OPT ("arm926ej-s",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
23040   ARM_CPU_OPT ("arm946e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
23041   ARM_CPU_OPT ("arm946e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"),
23042   ARM_CPU_OPT ("arm946e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23043   ARM_CPU_OPT ("arm966e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
23044   ARM_CPU_OPT ("arm966e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"),
23045   ARM_CPU_OPT ("arm966e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23046   ARM_CPU_OPT ("arm968e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23047   ARM_CPU_OPT ("arm10t",        ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
23048   ARM_CPU_OPT ("arm10tdmi",     ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
23049   ARM_CPU_OPT ("arm10e",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23050   ARM_CPU_OPT ("arm1020",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"),
23051   ARM_CPU_OPT ("arm1020t",      ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
23052   ARM_CPU_OPT ("arm1020e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23053   ARM_CPU_OPT ("arm1022e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23054   ARM_CPU_OPT ("arm1026ejs",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2,
23055                                                                  "ARM1026EJ-S"),
23056   ARM_CPU_OPT ("arm1026ej-s",   ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
23057   ARM_CPU_OPT ("fa606te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23058   ARM_CPU_OPT ("fa616te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23059   ARM_CPU_OPT ("fa626te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23060   ARM_CPU_OPT ("fmp626",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23061   ARM_CPU_OPT ("fa726te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
23062   ARM_CPU_OPT ("arm1136js",     ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"),
23063   ARM_CPU_OPT ("arm1136j-s",    ARM_ARCH_V6,     FPU_NONE,        NULL),
23064   ARM_CPU_OPT ("arm1136jfs",    ARM_ARCH_V6,     FPU_ARCH_VFP_V2,
23065                                                                  "ARM1136JF-S"),
23066   ARM_CPU_OPT ("arm1136jf-s",   ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL),
23067   ARM_CPU_OPT ("mpcore",        ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, "MPCore"),
23068   ARM_CPU_OPT ("mpcorenovfp",   ARM_ARCH_V6K,    FPU_NONE,        "MPCore"),
23069   ARM_CPU_OPT ("arm1156t2-s",   ARM_ARCH_V6T2,   FPU_NONE,        NULL),
23070   ARM_CPU_OPT ("arm1156t2f-s",  ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL),
23071   ARM_CPU_OPT ("arm1176jz-s",   ARM_ARCH_V6ZK,   FPU_NONE,        NULL),
23072   ARM_CPU_OPT ("arm1176jzf-s",  ARM_ARCH_V6ZK,   FPU_ARCH_VFP_V2, NULL),
23073   ARM_CPU_OPT ("cortex-a5",     ARM_ARCH_V7A_MP_SEC,
23074                                                  FPU_NONE,        "Cortex-A5"),
23075   ARM_CPU_OPT ("cortex-a7",     ARM_ARCH_V7A_IDIV_MP_SEC_VIRT,
23076                                                  FPU_ARCH_NEON_VFP_V4,
23077                                                                   "Cortex-A7"),
23078   ARM_CPU_OPT ("cortex-a8",     ARM_ARCH_V7A_SEC,
23079                                                  ARM_FEATURE (0, FPU_VFP_V3
23080                                                         | FPU_NEON_EXT_V1),
23081                                                                   "Cortex-A8"),
23082   ARM_CPU_OPT ("cortex-a9",     ARM_ARCH_V7A_MP_SEC,
23083                                                  ARM_FEATURE (0, FPU_VFP_V3
23084                                                         | FPU_NEON_EXT_V1),
23085                                                                   "Cortex-A9"),
23086   ARM_CPU_OPT ("cortex-a15",    ARM_ARCH_V7A_IDIV_MP_SEC_VIRT,
23087                                                  FPU_ARCH_NEON_VFP_V4,
23088                                                                   "Cortex-A15"),
23089   ARM_CPU_OPT ("cortex-r4",     ARM_ARCH_V7R,    FPU_NONE,        "Cortex-R4"),
23090   ARM_CPU_OPT ("cortex-r4f",    ARM_ARCH_V7R,    FPU_ARCH_VFP_V3D16,
23091                                                                   "Cortex-R4F"),
23092   ARM_CPU_OPT ("cortex-r5",     ARM_ARCH_V7R_IDIV,
23093                                                  FPU_NONE,        "Cortex-R5"),
23094   ARM_CPU_OPT ("cortex-m4",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M4"),
23095   ARM_CPU_OPT ("cortex-m3",     ARM_ARCH_V7M,    FPU_NONE,        "Cortex-M3"),
23096   ARM_CPU_OPT ("cortex-m1",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M1"),
23097   ARM_CPU_OPT ("cortex-m0",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0"),
23098   ARM_CPU_OPT ("cortex-m0plus", ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0+"),
23099   /* ??? XSCALE is really an architecture.  */
23100   ARM_CPU_OPT ("xscale",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
23101   /* ??? iwmmxt is not a processor.  */
23102   ARM_CPU_OPT ("iwmmxt",        ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL),
23103   ARM_CPU_OPT ("iwmmxt2",       ARM_ARCH_IWMMXT2,FPU_ARCH_VFP_V2, NULL),
23104   ARM_CPU_OPT ("i80200",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
23105   /* Maverick */
23106   ARM_CPU_OPT ("ep9312",        ARM_FEATURE (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
23107                                                  FPU_ARCH_MAVERICK,
23108                                                                   "ARM920T"),
23109   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
23110 };
23111 #undef ARM_CPU_OPT
23112
23113 struct arm_arch_option_table
23114 {
23115   char *name;
23116   size_t name_len;
23117   const arm_feature_set value;
23118   const arm_feature_set default_fpu;
23119 };
23120
23121 /* This list should, at a minimum, contain all the architecture names
23122    recognized by GCC.  */
23123 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF }
23124 static const struct arm_arch_option_table arm_archs[] =
23125 {
23126   ARM_ARCH_OPT ("all",          ARM_ANY,         FPU_ARCH_FPA),
23127   ARM_ARCH_OPT ("armv1",        ARM_ARCH_V1,     FPU_ARCH_FPA),
23128   ARM_ARCH_OPT ("armv2",        ARM_ARCH_V2,     FPU_ARCH_FPA),
23129   ARM_ARCH_OPT ("armv2a",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
23130   ARM_ARCH_OPT ("armv2s",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
23131   ARM_ARCH_OPT ("armv3",        ARM_ARCH_V3,     FPU_ARCH_FPA),
23132   ARM_ARCH_OPT ("armv3m",       ARM_ARCH_V3M,    FPU_ARCH_FPA),
23133   ARM_ARCH_OPT ("armv4",        ARM_ARCH_V4,     FPU_ARCH_FPA),
23134   ARM_ARCH_OPT ("armv4xm",      ARM_ARCH_V4xM,   FPU_ARCH_FPA),
23135   ARM_ARCH_OPT ("armv4t",       ARM_ARCH_V4T,    FPU_ARCH_FPA),
23136   ARM_ARCH_OPT ("armv4txm",     ARM_ARCH_V4TxM,  FPU_ARCH_FPA),
23137   ARM_ARCH_OPT ("armv5",        ARM_ARCH_V5,     FPU_ARCH_VFP),
23138   ARM_ARCH_OPT ("armv5t",       ARM_ARCH_V5T,    FPU_ARCH_VFP),
23139   ARM_ARCH_OPT ("armv5txm",     ARM_ARCH_V5TxM,  FPU_ARCH_VFP),
23140   ARM_ARCH_OPT ("armv5te",      ARM_ARCH_V5TE,   FPU_ARCH_VFP),
23141   ARM_ARCH_OPT ("armv5texp",    ARM_ARCH_V5TExP, FPU_ARCH_VFP),
23142   ARM_ARCH_OPT ("armv5tej",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP),
23143   ARM_ARCH_OPT ("armv6",        ARM_ARCH_V6,     FPU_ARCH_VFP),
23144   ARM_ARCH_OPT ("armv6j",       ARM_ARCH_V6,     FPU_ARCH_VFP),
23145   ARM_ARCH_OPT ("armv6k",       ARM_ARCH_V6K,    FPU_ARCH_VFP),
23146   ARM_ARCH_OPT ("armv6z",       ARM_ARCH_V6Z,    FPU_ARCH_VFP),
23147   ARM_ARCH_OPT ("armv6zk",      ARM_ARCH_V6ZK,   FPU_ARCH_VFP),
23148   ARM_ARCH_OPT ("armv6t2",      ARM_ARCH_V6T2,   FPU_ARCH_VFP),
23149   ARM_ARCH_OPT ("armv6kt2",     ARM_ARCH_V6KT2,  FPU_ARCH_VFP),
23150   ARM_ARCH_OPT ("armv6zt2",     ARM_ARCH_V6ZT2,  FPU_ARCH_VFP),
23151   ARM_ARCH_OPT ("armv6zkt2",    ARM_ARCH_V6ZKT2, FPU_ARCH_VFP),
23152   ARM_ARCH_OPT ("armv6-m",      ARM_ARCH_V6M,    FPU_ARCH_VFP),
23153   ARM_ARCH_OPT ("armv6s-m",     ARM_ARCH_V6SM,   FPU_ARCH_VFP),
23154   ARM_ARCH_OPT ("armv7",        ARM_ARCH_V7,     FPU_ARCH_VFP),
23155   /* The official spelling of the ARMv7 profile variants is the dashed form.
23156      Accept the non-dashed form for compatibility with old toolchains.  */
23157   ARM_ARCH_OPT ("armv7a",       ARM_ARCH_V7A,    FPU_ARCH_VFP),
23158   ARM_ARCH_OPT ("armv7r",       ARM_ARCH_V7R,    FPU_ARCH_VFP),
23159   ARM_ARCH_OPT ("armv7m",       ARM_ARCH_V7M,    FPU_ARCH_VFP),
23160   ARM_ARCH_OPT ("armv7-a",      ARM_ARCH_V7A,    FPU_ARCH_VFP),
23161   ARM_ARCH_OPT ("armv7-r",      ARM_ARCH_V7R,    FPU_ARCH_VFP),
23162   ARM_ARCH_OPT ("armv7-m",      ARM_ARCH_V7M,    FPU_ARCH_VFP),
23163   ARM_ARCH_OPT ("armv7e-m",     ARM_ARCH_V7EM,   FPU_ARCH_VFP),
23164   ARM_ARCH_OPT ("xscale",       ARM_ARCH_XSCALE, FPU_ARCH_VFP),
23165   ARM_ARCH_OPT ("iwmmxt",       ARM_ARCH_IWMMXT, FPU_ARCH_VFP),
23166   ARM_ARCH_OPT ("iwmmxt2",      ARM_ARCH_IWMMXT2,FPU_ARCH_VFP),
23167   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
23168 };
23169 #undef ARM_ARCH_OPT
23170
23171 /* ISA extensions in the co-processor and main instruction set space.  */
23172 struct arm_option_extension_value_table
23173 {
23174   char *name;
23175   size_t name_len;
23176   const arm_feature_set value;
23177   const arm_feature_set allowed_archs;
23178 };
23179
23180 /* The following table must be in alphabetical order with a NULL last entry.
23181    */
23182 #define ARM_EXT_OPT(N, V, AA) { N, sizeof (N) - 1, V, AA }
23183 static const struct arm_option_extension_value_table arm_extensions[] =
23184 {
23185   ARM_EXT_OPT ("idiv",  ARM_FEATURE (ARM_EXT_ADIV | ARM_EXT_DIV, 0),
23186                                    ARM_FEATURE (ARM_EXT_V7A | ARM_EXT_V7R, 0)),
23187   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE (0, ARM_CEXT_IWMMXT),       ARM_ANY),
23188   ARM_EXT_OPT ("iwmmxt2",
23189                         ARM_FEATURE (0, ARM_CEXT_IWMMXT2),      ARM_ANY),
23190   ARM_EXT_OPT ("maverick",
23191                         ARM_FEATURE (0, ARM_CEXT_MAVERICK),     ARM_ANY),
23192   ARM_EXT_OPT ("mp",    ARM_FEATURE (ARM_EXT_MP, 0),
23193                                    ARM_FEATURE (ARM_EXT_V7A | ARM_EXT_V7R, 0)),
23194   ARM_EXT_OPT ("os",    ARM_FEATURE (ARM_EXT_OS, 0),
23195                                    ARM_FEATURE (ARM_EXT_V6M, 0)),
23196   ARM_EXT_OPT ("sec",   ARM_FEATURE (ARM_EXT_SEC, 0),
23197                                    ARM_FEATURE (ARM_EXT_V6K | ARM_EXT_V7A, 0)),
23198   ARM_EXT_OPT ("virt",  ARM_FEATURE (ARM_EXT_VIRT | ARM_EXT_ADIV
23199                                      | ARM_EXT_DIV, 0),
23200                                    ARM_FEATURE (ARM_EXT_V7A, 0)),
23201   ARM_EXT_OPT ("xscale",ARM_FEATURE (0, ARM_CEXT_XSCALE),       ARM_ANY),
23202   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
23203 };
23204 #undef ARM_EXT_OPT
23205
23206 /* ISA floating-point and Advanced SIMD extensions.  */
23207 struct arm_option_fpu_value_table
23208 {
23209   char *name;
23210   const arm_feature_set value;
23211 };
23212
23213 /* This list should, at a minimum, contain all the fpu names
23214    recognized by GCC.  */
23215 static const struct arm_option_fpu_value_table arm_fpus[] =
23216 {
23217   {"softfpa",           FPU_NONE},
23218   {"fpe",               FPU_ARCH_FPE},
23219   {"fpe2",              FPU_ARCH_FPE},
23220   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
23221   {"fpa",               FPU_ARCH_FPA},
23222   {"fpa10",             FPU_ARCH_FPA},
23223   {"fpa11",             FPU_ARCH_FPA},
23224   {"arm7500fe",         FPU_ARCH_FPA},
23225   {"softvfp",           FPU_ARCH_VFP},
23226   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
23227   {"vfp",               FPU_ARCH_VFP_V2},
23228   {"vfp9",              FPU_ARCH_VFP_V2},
23229   {"vfp3",              FPU_ARCH_VFP_V3}, /* For backwards compatbility.  */
23230   {"vfp10",             FPU_ARCH_VFP_V2},
23231   {"vfp10-r0",          FPU_ARCH_VFP_V1},
23232   {"vfpxd",             FPU_ARCH_VFP_V1xD},
23233   {"vfpv2",             FPU_ARCH_VFP_V2},
23234   {"vfpv3",             FPU_ARCH_VFP_V3},
23235   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
23236   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
23237   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
23238   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
23239   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
23240   {"arm1020t",          FPU_ARCH_VFP_V1},
23241   {"arm1020e",          FPU_ARCH_VFP_V2},
23242   {"arm1136jfs",        FPU_ARCH_VFP_V2},
23243   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
23244   {"maverick",          FPU_ARCH_MAVERICK},
23245   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
23246   {"neon-fp16",         FPU_ARCH_NEON_FP16},
23247   {"vfpv4",             FPU_ARCH_VFP_V4},
23248   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
23249   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
23250   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
23251   {NULL,                ARM_ARCH_NONE}
23252 };
23253
23254 struct arm_option_value_table
23255 {
23256   char *name;
23257   long value;
23258 };
23259
23260 static const struct arm_option_value_table arm_float_abis[] =
23261 {
23262   {"hard",      ARM_FLOAT_ABI_HARD},
23263   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
23264   {"soft",      ARM_FLOAT_ABI_SOFT},
23265   {NULL,        0}
23266 };
23267
23268 #ifdef OBJ_ELF
23269 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
23270 static const struct arm_option_value_table arm_eabis[] =
23271 {
23272   {"gnu",       EF_ARM_EABI_UNKNOWN},
23273   {"4",         EF_ARM_EABI_VER4},
23274   {"5",         EF_ARM_EABI_VER5},
23275   {NULL,        0}
23276 };
23277 #endif
23278
23279 struct arm_long_option_table
23280 {
23281   char * option;                /* Substring to match.  */
23282   char * help;                  /* Help information.  */
23283   int (* func) (char * subopt); /* Function to decode sub-option.  */
23284   char * deprecated;            /* If non-null, print this message.  */
23285 };
23286
23287 static bfd_boolean
23288 arm_parse_extension (char *str, const arm_feature_set **opt_p)
23289 {
23290   arm_feature_set *ext_set = (arm_feature_set *)
23291       xmalloc (sizeof (arm_feature_set));
23292
23293   /* We insist on extensions being specified in alphabetical order, and with
23294      extensions being added before being removed.  We achieve this by having
23295      the global ARM_EXTENSIONS table in alphabetical order, and using the
23296      ADDING_VALUE variable to indicate whether we are adding an extension (1)
23297      or removing it (0) and only allowing it to change in the order
23298      -1 -> 1 -> 0.  */
23299   const struct arm_option_extension_value_table * opt = NULL;
23300   int adding_value = -1;
23301
23302   /* Copy the feature set, so that we can modify it.  */
23303   *ext_set = **opt_p;
23304   *opt_p = ext_set;
23305
23306   while (str != NULL && *str != 0)
23307     {
23308       char *ext;
23309       size_t len;
23310
23311       if (*str != '+')
23312         {
23313           as_bad (_("invalid architectural extension"));
23314           return FALSE;
23315         }
23316
23317       str++;
23318       ext = strchr (str, '+');
23319
23320       if (ext != NULL)
23321         len = ext - str;
23322       else
23323         len = strlen (str);
23324
23325       if (len >= 2 && strncmp (str, "no", 2) == 0)
23326         {
23327           if (adding_value != 0)
23328             {
23329               adding_value = 0;
23330               opt = arm_extensions;
23331             }
23332
23333           len -= 2;
23334           str += 2;
23335         }
23336       else if (len > 0)
23337         {
23338           if (adding_value == -1)
23339             {
23340               adding_value = 1;
23341               opt = arm_extensions;
23342             }
23343           else if (adding_value != 1)
23344             {
23345               as_bad (_("must specify extensions to add before specifying "
23346                         "those to remove"));
23347               return FALSE;
23348             }
23349         }
23350
23351       if (len == 0)
23352         {
23353           as_bad (_("missing architectural extension"));
23354           return FALSE;
23355         }
23356
23357       gas_assert (adding_value != -1);
23358       gas_assert (opt != NULL);
23359
23360       /* Scan over the options table trying to find an exact match. */
23361       for (; opt->name != NULL; opt++)
23362         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
23363           {
23364             /* Check we can apply the extension to this architecture.  */
23365             if (!ARM_CPU_HAS_FEATURE (*ext_set, opt->allowed_archs))
23366               {
23367                 as_bad (_("extension does not apply to the base architecture"));
23368                 return FALSE;
23369               }
23370
23371             /* Add or remove the extension.  */
23372             if (adding_value)
23373               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->value);
23374             else
23375               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->value);
23376
23377             break;
23378           }
23379
23380       if (opt->name == NULL)
23381         {
23382           /* Did we fail to find an extension because it wasn't specified in
23383              alphabetical order, or because it does not exist?  */
23384
23385           for (opt = arm_extensions; opt->name != NULL; opt++)
23386             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
23387               break;
23388
23389           if (opt->name == NULL)
23390             as_bad (_("unknown architectural extension `%s'"), str);
23391           else
23392             as_bad (_("architectural extensions must be specified in "
23393                       "alphabetical order"));
23394
23395           return FALSE;
23396         }
23397       else
23398         {
23399           /* We should skip the extension we've just matched the next time
23400              round.  */
23401           opt++;
23402         }
23403
23404       str = ext;
23405     };
23406
23407   return TRUE;
23408 }
23409
23410 static bfd_boolean
23411 arm_parse_cpu (char *str)
23412 {
23413   const struct arm_cpu_option_table *opt;
23414   char *ext = strchr (str, '+');
23415   size_t len;
23416
23417   if (ext != NULL)
23418     len = ext - str;
23419   else
23420     len = strlen (str);
23421
23422   if (len == 0)
23423     {
23424       as_bad (_("missing cpu name `%s'"), str);
23425       return FALSE;
23426     }
23427
23428   for (opt = arm_cpus; opt->name != NULL; opt++)
23429     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
23430       {
23431         mcpu_cpu_opt = &opt->value;
23432         mcpu_fpu_opt = &opt->default_fpu;
23433         if (opt->canonical_name)
23434           strcpy (selected_cpu_name, opt->canonical_name);
23435         else
23436           {
23437             size_t i;
23438
23439             for (i = 0; i < len; i++)
23440               selected_cpu_name[i] = TOUPPER (opt->name[i]);
23441             selected_cpu_name[i] = 0;
23442           }
23443
23444         if (ext != NULL)
23445           return arm_parse_extension (ext, &mcpu_cpu_opt);
23446
23447         return TRUE;
23448       }
23449
23450   as_bad (_("unknown cpu `%s'"), str);
23451   return FALSE;
23452 }
23453
23454 static bfd_boolean
23455 arm_parse_arch (char *str)
23456 {
23457   const struct arm_arch_option_table *opt;
23458   char *ext = strchr (str, '+');
23459   size_t len;
23460
23461   if (ext != NULL)
23462     len = ext - str;
23463   else
23464     len = strlen (str);
23465
23466   if (len == 0)
23467     {
23468       as_bad (_("missing architecture name `%s'"), str);
23469       return FALSE;
23470     }
23471
23472   for (opt = arm_archs; opt->name != NULL; opt++)
23473     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
23474       {
23475         march_cpu_opt = &opt->value;
23476         march_fpu_opt = &opt->default_fpu;
23477         strcpy (selected_cpu_name, opt->name);
23478
23479         if (ext != NULL)
23480           return arm_parse_extension (ext, &march_cpu_opt);
23481
23482         return TRUE;
23483       }
23484
23485   as_bad (_("unknown architecture `%s'\n"), str);
23486   return FALSE;
23487 }
23488
23489 static bfd_boolean
23490 arm_parse_fpu (char * str)
23491 {
23492   const struct arm_option_fpu_value_table * opt;
23493
23494   for (opt = arm_fpus; opt->name != NULL; opt++)
23495     if (streq (opt->name, str))
23496       {
23497         mfpu_opt = &opt->value;
23498         return TRUE;
23499       }
23500
23501   as_bad (_("unknown floating point format `%s'\n"), str);
23502   return FALSE;
23503 }
23504
23505 static bfd_boolean
23506 arm_parse_float_abi (char * str)
23507 {
23508   const struct arm_option_value_table * opt;
23509
23510   for (opt = arm_float_abis; opt->name != NULL; opt++)
23511     if (streq (opt->name, str))
23512       {
23513         mfloat_abi_opt = opt->value;
23514         return TRUE;
23515       }
23516
23517   as_bad (_("unknown floating point abi `%s'\n"), str);
23518   return FALSE;
23519 }
23520
23521 #ifdef OBJ_ELF
23522 static bfd_boolean
23523 arm_parse_eabi (char * str)
23524 {
23525   const struct arm_option_value_table *opt;
23526
23527   for (opt = arm_eabis; opt->name != NULL; opt++)
23528     if (streq (opt->name, str))
23529       {
23530         meabi_flags = opt->value;
23531         return TRUE;
23532       }
23533   as_bad (_("unknown EABI `%s'\n"), str);
23534   return FALSE;
23535 }
23536 #endif
23537
23538 static bfd_boolean
23539 arm_parse_it_mode (char * str)
23540 {
23541   bfd_boolean ret = TRUE;
23542
23543   if (streq ("arm", str))
23544     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
23545   else if (streq ("thumb", str))
23546     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
23547   else if (streq ("always", str))
23548     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
23549   else if (streq ("never", str))
23550     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
23551   else
23552     {
23553       as_bad (_("unknown implicit IT mode `%s', should be "\
23554                 "arm, thumb, always, or never."), str);
23555       ret = FALSE;
23556     }
23557
23558   return ret;
23559 }
23560
23561 struct arm_long_option_table arm_long_opts[] =
23562 {
23563   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
23564    arm_parse_cpu, NULL},
23565   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
23566    arm_parse_arch, NULL},
23567   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
23568    arm_parse_fpu, NULL},
23569   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
23570    arm_parse_float_abi, NULL},
23571 #ifdef OBJ_ELF
23572   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
23573    arm_parse_eabi, NULL},
23574 #endif
23575   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
23576    arm_parse_it_mode, NULL},
23577   {NULL, NULL, 0, NULL}
23578 };
23579
23580 int
23581 md_parse_option (int c, char * arg)
23582 {
23583   struct arm_option_table *opt;
23584   const struct arm_legacy_option_table *fopt;
23585   struct arm_long_option_table *lopt;
23586
23587   switch (c)
23588     {
23589 #ifdef OPTION_EB
23590     case OPTION_EB:
23591       target_big_endian = 1;
23592       break;
23593 #endif
23594
23595 #ifdef OPTION_EL
23596     case OPTION_EL:
23597       target_big_endian = 0;
23598       break;
23599 #endif
23600
23601     case OPTION_FIX_V4BX:
23602       fix_v4bx = TRUE;
23603       break;
23604
23605     case 'a':
23606       /* Listing option.  Just ignore these, we don't support additional
23607          ones.  */
23608       return 0;
23609
23610     default:
23611       for (opt = arm_opts; opt->option != NULL; opt++)
23612         {
23613           if (c == opt->option[0]
23614               && ((arg == NULL && opt->option[1] == 0)
23615                   || streq (arg, opt->option + 1)))
23616             {
23617               /* If the option is deprecated, tell the user.  */
23618               if (warn_on_deprecated && opt->deprecated != NULL)
23619                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
23620                            arg ? arg : "", _(opt->deprecated));
23621
23622               if (opt->var != NULL)
23623                 *opt->var = opt->value;
23624
23625               return 1;
23626             }
23627         }
23628
23629       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
23630         {
23631           if (c == fopt->option[0]
23632               && ((arg == NULL && fopt->option[1] == 0)
23633                   || streq (arg, fopt->option + 1)))
23634             {
23635               /* If the option is deprecated, tell the user.  */
23636               if (warn_on_deprecated && fopt->deprecated != NULL)
23637                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
23638                            arg ? arg : "", _(fopt->deprecated));
23639
23640               if (fopt->var != NULL)
23641                 *fopt->var = &fopt->value;
23642
23643               return 1;
23644             }
23645         }
23646
23647       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
23648         {
23649           /* These options are expected to have an argument.  */
23650           if (c == lopt->option[0]
23651               && arg != NULL
23652               && strncmp (arg, lopt->option + 1,
23653                           strlen (lopt->option + 1)) == 0)
23654             {
23655               /* If the option is deprecated, tell the user.  */
23656               if (warn_on_deprecated && lopt->deprecated != NULL)
23657                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
23658                            _(lopt->deprecated));
23659
23660               /* Call the sup-option parser.  */
23661               return lopt->func (arg + strlen (lopt->option) - 1);
23662             }
23663         }
23664
23665       return 0;
23666     }
23667
23668   return 1;
23669 }
23670
23671 void
23672 md_show_usage (FILE * fp)
23673 {
23674   struct arm_option_table *opt;
23675   struct arm_long_option_table *lopt;
23676
23677   fprintf (fp, _(" ARM-specific assembler options:\n"));
23678
23679   for (opt = arm_opts; opt->option != NULL; opt++)
23680     if (opt->help != NULL)
23681       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
23682
23683   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
23684     if (lopt->help != NULL)
23685       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
23686
23687 #ifdef OPTION_EB
23688   fprintf (fp, _("\
23689   -EB                     assemble code for a big-endian cpu\n"));
23690 #endif
23691
23692 #ifdef OPTION_EL
23693   fprintf (fp, _("\
23694   -EL                     assemble code for a little-endian cpu\n"));
23695 #endif
23696
23697   fprintf (fp, _("\
23698   --fix-v4bx              Allow BX in ARMv4 code\n"));
23699 }
23700
23701
23702 #ifdef OBJ_ELF
23703 typedef struct
23704 {
23705   int val;
23706   arm_feature_set flags;
23707 } cpu_arch_ver_table;
23708
23709 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
23710    least features first.  */
23711 static const cpu_arch_ver_table cpu_arch_ver[] =
23712 {
23713     {1, ARM_ARCH_V4},
23714     {2, ARM_ARCH_V4T},
23715     {3, ARM_ARCH_V5},
23716     {3, ARM_ARCH_V5T},
23717     {4, ARM_ARCH_V5TE},
23718     {5, ARM_ARCH_V5TEJ},
23719     {6, ARM_ARCH_V6},
23720     {9, ARM_ARCH_V6K},
23721     {7, ARM_ARCH_V6Z},
23722     {11, ARM_ARCH_V6M},
23723     {12, ARM_ARCH_V6SM},
23724     {8, ARM_ARCH_V6T2},
23725     {10, ARM_ARCH_V7A},
23726     {10, ARM_ARCH_V7R},
23727     {10, ARM_ARCH_V7M},
23728     {0, ARM_ARCH_NONE}
23729 };
23730
23731 /* Set an attribute if it has not already been set by the user.  */
23732 static void
23733 aeabi_set_attribute_int (int tag, int value)
23734 {
23735   if (tag < 1
23736       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
23737       || !attributes_set_explicitly[tag])
23738     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
23739 }
23740
23741 static void
23742 aeabi_set_attribute_string (int tag, const char *value)
23743 {
23744   if (tag < 1
23745       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
23746       || !attributes_set_explicitly[tag])
23747     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
23748 }
23749
23750 /* Set the public EABI object attributes.  */
23751 static void
23752 aeabi_set_public_attributes (void)
23753 {
23754   int arch;
23755   char profile;
23756   int virt_sec = 0;
23757   arm_feature_set flags;
23758   arm_feature_set tmp;
23759   const cpu_arch_ver_table *p;
23760
23761   /* Choose the architecture based on the capabilities of the requested cpu
23762      (if any) and/or the instructions actually used.  */
23763   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
23764   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
23765   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
23766
23767   if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
23768     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
23769
23770   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
23771     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
23772
23773   /* Allow the user to override the reported architecture.  */
23774   if (object_arch)
23775     {
23776       ARM_CLEAR_FEATURE (flags, flags, arm_arch_any);
23777       ARM_MERGE_FEATURE_SETS (flags, flags, *object_arch);
23778     }
23779
23780   /* We need to make sure that the attributes do not identify us as v6S-M
23781      when the only v6S-M feature in use is the Operating System Extensions.  */
23782   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_os))
23783       if (!ARM_CPU_HAS_FEATURE (flags, arm_arch_v6m_only))
23784         ARM_CLEAR_FEATURE (flags, flags, arm_ext_os);
23785
23786   tmp = flags;
23787   arch = 0;
23788   for (p = cpu_arch_ver; p->val; p++)
23789     {
23790       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
23791         {
23792           arch = p->val;
23793           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
23794         }
23795     }
23796
23797   /* The table lookup above finds the last architecture to contribute
23798      a new feature.  Unfortunately, Tag13 is a subset of the union of
23799      v6T2 and v7-M, so it is never seen as contributing a new feature.
23800      We can not search for the last entry which is entirely used,
23801      because if no CPU is specified we build up only those flags
23802      actually used.  Perhaps we should separate out the specified
23803      and implicit cases.  Avoid taking this path for -march=all by
23804      checking for contradictory v7-A / v7-M features.  */
23805   if (arch == 10
23806       && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
23807       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m)
23808       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v6_dsp))
23809     arch = 13;
23810
23811   /* Tag_CPU_name.  */
23812   if (selected_cpu_name[0])
23813     {
23814       char *q;
23815
23816       q = selected_cpu_name;
23817       if (strncmp (q, "armv", 4) == 0)
23818         {
23819           int i;
23820
23821           q += 4;
23822           for (i = 0; q[i]; i++)
23823             q[i] = TOUPPER (q[i]);
23824         }
23825       aeabi_set_attribute_string (Tag_CPU_name, q);
23826     }
23827
23828   /* Tag_CPU_arch.  */
23829   aeabi_set_attribute_int (Tag_CPU_arch, arch);
23830
23831   /* Tag_CPU_arch_profile.  */
23832   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a))
23833     profile = 'A';
23834   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
23835     profile = 'R';
23836   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_m))
23837     profile = 'M';
23838   else
23839     profile = '\0';
23840
23841   if (profile != '\0')
23842     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
23843
23844   /* Tag_ARM_ISA_use.  */
23845   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
23846       || arch == 0)
23847     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
23848
23849   /* Tag_THUMB_ISA_use.  */
23850   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
23851       || arch == 0)
23852     aeabi_set_attribute_int (Tag_THUMB_ISA_use,
23853         ARM_CPU_HAS_FEATURE (flags, arm_arch_t2) ? 2 : 1);
23854
23855   /* Tag_VFP_arch.  */
23856   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
23857     aeabi_set_attribute_int (Tag_VFP_arch,
23858                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
23859                              ? 5 : 6);
23860   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
23861     aeabi_set_attribute_int (Tag_VFP_arch, 3);
23862   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
23863     aeabi_set_attribute_int (Tag_VFP_arch, 4);
23864   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
23865     aeabi_set_attribute_int (Tag_VFP_arch, 2);
23866   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
23867            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
23868     aeabi_set_attribute_int (Tag_VFP_arch, 1);
23869
23870   /* Tag_ABI_HardFP_use.  */
23871   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
23872       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
23873     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
23874
23875   /* Tag_WMMX_arch.  */
23876   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
23877     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
23878   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
23879     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
23880
23881   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
23882   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
23883     aeabi_set_attribute_int
23884       (Tag_Advanced_SIMD_arch, (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma)
23885                                 ? 2 : 1));
23886
23887   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
23888   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16))
23889     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
23890
23891   /* Tag_DIV_use.
23892
23893      We set Tag_DIV_use to two when integer divide instructions have been used
23894      in ARM state, or when Thumb integer divide instructions have been used,
23895      but we have no architecture profile set, nor have we any ARM instructions.
23896
23897      For new architectures we will have to check these tests.  */
23898   gas_assert (arch <= TAG_CPU_ARCH_V7E_M);
23899   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
23900       || (profile == '\0'
23901           && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
23902           && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
23903     aeabi_set_attribute_int (Tag_DIV_use, 2);
23904
23905   /* Tag_MP_extension_use.  */
23906   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
23907     aeabi_set_attribute_int (Tag_MPextension_use, 1);
23908
23909   /* Tag Virtualization_use.  */
23910   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
23911     virt_sec |= 1;
23912   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
23913     virt_sec |= 2;
23914   if (virt_sec != 0)
23915     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
23916 }
23917
23918 /* Add the default contents for the .ARM.attributes section.  */
23919 void
23920 arm_md_end (void)
23921 {
23922   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
23923     return;
23924
23925   aeabi_set_public_attributes ();
23926 }
23927 #endif /* OBJ_ELF */
23928
23929
23930 /* Parse a .cpu directive.  */
23931
23932 static void
23933 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
23934 {
23935   const struct arm_cpu_option_table *opt;
23936   char *name;
23937   char saved_char;
23938
23939   name = input_line_pointer;
23940   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23941     input_line_pointer++;
23942   saved_char = *input_line_pointer;
23943   *input_line_pointer = 0;
23944
23945   /* Skip the first "all" entry.  */
23946   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
23947     if (streq (opt->name, name))
23948       {
23949         mcpu_cpu_opt = &opt->value;
23950         selected_cpu = opt->value;
23951         if (opt->canonical_name)
23952           strcpy (selected_cpu_name, opt->canonical_name);
23953         else
23954           {
23955             int i;
23956             for (i = 0; opt->name[i]; i++)
23957               selected_cpu_name[i] = TOUPPER (opt->name[i]);
23958
23959             selected_cpu_name[i] = 0;
23960           }
23961         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23962         *input_line_pointer = saved_char;
23963         demand_empty_rest_of_line ();
23964         return;
23965       }
23966   as_bad (_("unknown cpu `%s'"), name);
23967   *input_line_pointer = saved_char;
23968   ignore_rest_of_line ();
23969 }
23970
23971
23972 /* Parse a .arch directive.  */
23973
23974 static void
23975 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
23976 {
23977   const struct arm_arch_option_table *opt;
23978   char saved_char;
23979   char *name;
23980
23981   name = input_line_pointer;
23982   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23983     input_line_pointer++;
23984   saved_char = *input_line_pointer;
23985   *input_line_pointer = 0;
23986
23987   /* Skip the first "all" entry.  */
23988   for (opt = arm_archs + 1; opt->name != NULL; opt++)
23989     if (streq (opt->name, name))
23990       {
23991         mcpu_cpu_opt = &opt->value;
23992         selected_cpu = opt->value;
23993         strcpy (selected_cpu_name, opt->name);
23994         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23995         *input_line_pointer = saved_char;
23996         demand_empty_rest_of_line ();
23997         return;
23998       }
23999
24000   as_bad (_("unknown architecture `%s'\n"), name);
24001   *input_line_pointer = saved_char;
24002   ignore_rest_of_line ();
24003 }
24004
24005
24006 /* Parse a .object_arch directive.  */
24007
24008 static void
24009 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
24010 {
24011   const struct arm_arch_option_table *opt;
24012   char saved_char;
24013   char *name;
24014
24015   name = input_line_pointer;
24016   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
24017     input_line_pointer++;
24018   saved_char = *input_line_pointer;
24019   *input_line_pointer = 0;
24020
24021   /* Skip the first "all" entry.  */
24022   for (opt = arm_archs + 1; opt->name != NULL; opt++)
24023     if (streq (opt->name, name))
24024       {
24025         object_arch = &opt->value;
24026         *input_line_pointer = saved_char;
24027         demand_empty_rest_of_line ();
24028         return;
24029       }
24030
24031   as_bad (_("unknown architecture `%s'\n"), name);
24032   *input_line_pointer = saved_char;
24033   ignore_rest_of_line ();
24034 }
24035
24036 /* Parse a .arch_extension directive.  */
24037
24038 static void
24039 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
24040 {
24041   const struct arm_option_extension_value_table *opt;
24042   char saved_char;
24043   char *name;
24044   int adding_value = 1;
24045
24046   name = input_line_pointer;
24047   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
24048     input_line_pointer++;
24049   saved_char = *input_line_pointer;
24050   *input_line_pointer = 0;
24051
24052   if (strlen (name) >= 2
24053       && strncmp (name, "no", 2) == 0)
24054     {
24055       adding_value = 0;
24056       name += 2;
24057     }
24058
24059   for (opt = arm_extensions; opt->name != NULL; opt++)
24060     if (streq (opt->name, name))
24061       {
24062         if (!ARM_CPU_HAS_FEATURE (*mcpu_cpu_opt, opt->allowed_archs))
24063           {
24064             as_bad (_("architectural extension `%s' is not allowed for the "
24065                       "current base architecture"), name);
24066             break;
24067           }
24068
24069         if (adding_value)
24070           ARM_MERGE_FEATURE_SETS (selected_cpu, selected_cpu, opt->value);
24071         else
24072           ARM_CLEAR_FEATURE (selected_cpu, selected_cpu, opt->value);
24073
24074         mcpu_cpu_opt = &selected_cpu;
24075         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
24076         *input_line_pointer = saved_char;
24077         demand_empty_rest_of_line ();
24078         return;
24079       }
24080
24081   if (opt->name == NULL)
24082     as_bad (_("unknown architecture `%s'\n"), name);
24083
24084   *input_line_pointer = saved_char;
24085   ignore_rest_of_line ();
24086 }
24087
24088 /* Parse a .fpu directive.  */
24089
24090 static void
24091 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
24092 {
24093   const struct arm_option_fpu_value_table *opt;
24094   char saved_char;
24095   char *name;
24096
24097   name = input_line_pointer;
24098   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
24099     input_line_pointer++;
24100   saved_char = *input_line_pointer;
24101   *input_line_pointer = 0;
24102
24103   for (opt = arm_fpus; opt->name != NULL; opt++)
24104     if (streq (opt->name, name))
24105       {
24106         mfpu_opt = &opt->value;
24107         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
24108         *input_line_pointer = saved_char;
24109         demand_empty_rest_of_line ();
24110         return;
24111       }
24112
24113   as_bad (_("unknown floating point format `%s'\n"), name);
24114   *input_line_pointer = saved_char;
24115   ignore_rest_of_line ();
24116 }
24117
24118 /* Copy symbol information.  */
24119
24120 void
24121 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
24122 {
24123   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
24124 }
24125
24126 #ifdef OBJ_ELF
24127 /* Given a symbolic attribute NAME, return the proper integer value.
24128    Returns -1 if the attribute is not known.  */
24129
24130 int
24131 arm_convert_symbolic_attribute (const char *name)
24132 {
24133   static const struct
24134   {
24135     const char * name;
24136     const int    tag;
24137   }
24138   attribute_table[] =
24139     {
24140       /* When you modify this table you should
24141          also modify the list in doc/c-arm.texi.  */
24142 #define T(tag) {#tag, tag}
24143       T (Tag_CPU_raw_name),
24144       T (Tag_CPU_name),
24145       T (Tag_CPU_arch),
24146       T (Tag_CPU_arch_profile),
24147       T (Tag_ARM_ISA_use),
24148       T (Tag_THUMB_ISA_use),
24149       T (Tag_FP_arch),
24150       T (Tag_VFP_arch),
24151       T (Tag_WMMX_arch),
24152       T (Tag_Advanced_SIMD_arch),
24153       T (Tag_PCS_config),
24154       T (Tag_ABI_PCS_R9_use),
24155       T (Tag_ABI_PCS_RW_data),
24156       T (Tag_ABI_PCS_RO_data),
24157       T (Tag_ABI_PCS_GOT_use),
24158       T (Tag_ABI_PCS_wchar_t),
24159       T (Tag_ABI_FP_rounding),
24160       T (Tag_ABI_FP_denormal),
24161       T (Tag_ABI_FP_exceptions),
24162       T (Tag_ABI_FP_user_exceptions),
24163       T (Tag_ABI_FP_number_model),
24164       T (Tag_ABI_align_needed),
24165       T (Tag_ABI_align8_needed),
24166       T (Tag_ABI_align_preserved),
24167       T (Tag_ABI_align8_preserved),
24168       T (Tag_ABI_enum_size),
24169       T (Tag_ABI_HardFP_use),
24170       T (Tag_ABI_VFP_args),
24171       T (Tag_ABI_WMMX_args),
24172       T (Tag_ABI_optimization_goals),
24173       T (Tag_ABI_FP_optimization_goals),
24174       T (Tag_compatibility),
24175       T (Tag_CPU_unaligned_access),
24176       T (Tag_FP_HP_extension),
24177       T (Tag_VFP_HP_extension),
24178       T (Tag_ABI_FP_16bit_format),
24179       T (Tag_MPextension_use),
24180       T (Tag_DIV_use),
24181       T (Tag_nodefaults),
24182       T (Tag_also_compatible_with),
24183       T (Tag_conformance),
24184       T (Tag_T2EE_use),
24185       T (Tag_Virtualization_use),
24186       /* We deliberately do not include Tag_MPextension_use_legacy.  */
24187 #undef T
24188     };
24189   unsigned int i;
24190
24191   if (name == NULL)
24192     return -1;
24193
24194   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
24195     if (streq (name, attribute_table[i].name))
24196       return attribute_table[i].tag;
24197
24198   return -1;
24199 }
24200
24201
24202 /* Apply sym value for relocations only in the case that
24203    they are for local symbols and you have the respective
24204    architectural feature for blx and simple switches.  */
24205 int
24206 arm_apply_sym_value (struct fix * fixP)
24207 {
24208   if (fixP->fx_addsy
24209       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
24210       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
24211     {
24212       switch (fixP->fx_r_type)
24213         {
24214         case BFD_RELOC_ARM_PCREL_BLX:
24215         case BFD_RELOC_THUMB_PCREL_BRANCH23:
24216           if (ARM_IS_FUNC (fixP->fx_addsy))
24217             return 1;
24218           break;
24219
24220         case BFD_RELOC_ARM_PCREL_CALL:
24221         case BFD_RELOC_THUMB_PCREL_BLX:
24222           if (THUMB_IS_FUNC (fixP->fx_addsy))
24223               return 1;
24224           break;
24225
24226         default:
24227           break;
24228         }
24229
24230     }
24231   return 0;
24232 }
24233 #endif /* OBJ_ELF */