2011-05-31 Paul Brook <paul@codesourcery.com>
[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
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
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 struct arm_it
355 {
356   const char *  error;
357   unsigned long instruction;
358   int           size;
359   int           size_req;
360   int           cond;
361   /* "uncond_value" is set to the value in place of the conditional field in
362      unconditional versions of the instruction, or -1 if nothing is
363      appropriate.  */
364   int           uncond_value;
365   struct neon_type vectype;
366   /* This does not indicate an actual NEON instruction, only that
367      the mnemonic accepts neon-style type suffixes.  */
368   int           is_neon;
369   /* Set to the opcode if the instruction needs relaxation.
370      Zero if the instruction is not relaxed.  */
371   unsigned long relax;
372   struct
373   {
374     bfd_reloc_code_real_type type;
375     expressionS              exp;
376     int                      pc_rel;
377   } reloc;
378
379   enum it_instruction_type it_insn_type;
380
381   struct
382   {
383     unsigned reg;
384     signed int imm;
385     struct neon_type_el vectype;
386     unsigned present    : 1;  /* Operand present.  */
387     unsigned isreg      : 1;  /* Operand was a register.  */
388     unsigned immisreg   : 1;  /* .imm field is a second register.  */
389     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
390     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
391     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
392     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
393        instructions. This allows us to disambiguate ARM <-> vector insns.  */
394     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
395     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
396     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
397     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
398     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
399     unsigned writeback  : 1;  /* Operand has trailing !  */
400     unsigned preind     : 1;  /* Preindexed address.  */
401     unsigned postind    : 1;  /* Postindexed address.  */
402     unsigned negative   : 1;  /* Index register was negated.  */
403     unsigned shifted    : 1;  /* Shift applied to operation.  */
404     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
405   } operands[6];
406 };
407
408 static struct arm_it inst;
409
410 #define NUM_FLOAT_VALS 8
411
412 const char * fp_const[] =
413 {
414   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
415 };
416
417 /* Number of littlenums required to hold an extended precision number.  */
418 #define MAX_LITTLENUMS 6
419
420 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
421
422 #define FAIL    (-1)
423 #define SUCCESS (0)
424
425 #define SUFF_S 1
426 #define SUFF_D 2
427 #define SUFF_E 3
428 #define SUFF_P 4
429
430 #define CP_T_X   0x00008000
431 #define CP_T_Y   0x00400000
432
433 #define CONDS_BIT        0x00100000
434 #define LOAD_BIT         0x00100000
435
436 #define DOUBLE_LOAD_FLAG 0x00000001
437
438 struct asm_cond
439 {
440   const char *   template_name;
441   unsigned long  value;
442 };
443
444 #define COND_ALWAYS 0xE
445
446 struct asm_psr
447 {
448   const char *   template_name;
449   unsigned long  field;
450 };
451
452 struct asm_barrier_opt
453 {
454   const char *   template_name;
455   unsigned long  value;
456 };
457
458 /* The bit that distinguishes CPSR and SPSR.  */
459 #define SPSR_BIT   (1 << 22)
460
461 /* The individual PSR flag bits.  */
462 #define PSR_c   (1 << 16)
463 #define PSR_x   (1 << 17)
464 #define PSR_s   (1 << 18)
465 #define PSR_f   (1 << 19)
466
467 struct reloc_entry
468 {
469   char *                    name;
470   bfd_reloc_code_real_type  reloc;
471 };
472
473 enum vfp_reg_pos
474 {
475   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
476   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
477 };
478
479 enum vfp_ldstm_type
480 {
481   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
482 };
483
484 /* Bits for DEFINED field in neon_typed_alias.  */
485 #define NTA_HASTYPE  1
486 #define NTA_HASINDEX 2
487
488 struct neon_typed_alias
489 {
490   unsigned char        defined;
491   unsigned char        index;
492   struct neon_type_el  eltype;
493 };
494
495 /* ARM register categories.  This includes coprocessor numbers and various
496    architecture extensions' registers.  */
497 enum arm_reg_type
498 {
499   REG_TYPE_RN,
500   REG_TYPE_CP,
501   REG_TYPE_CN,
502   REG_TYPE_FN,
503   REG_TYPE_VFS,
504   REG_TYPE_VFD,
505   REG_TYPE_NQ,
506   REG_TYPE_VFSD,
507   REG_TYPE_NDQ,
508   REG_TYPE_NSDQ,
509   REG_TYPE_VFC,
510   REG_TYPE_MVF,
511   REG_TYPE_MVD,
512   REG_TYPE_MVFX,
513   REG_TYPE_MVDX,
514   REG_TYPE_MVAX,
515   REG_TYPE_DSPSC,
516   REG_TYPE_MMXWR,
517   REG_TYPE_MMXWC,
518   REG_TYPE_MMXWCG,
519   REG_TYPE_XSCALE,
520   REG_TYPE_RNB
521 };
522
523 /* Structure for a hash table entry for a register.
524    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
525    information which states whether a vector type or index is specified (for a
526    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
527 struct reg_entry
528 {
529   const char *               name;
530   unsigned int               number;
531   unsigned char              type;
532   unsigned char              builtin;
533   struct neon_typed_alias *  neon;
534 };
535
536 /* Diagnostics used when we don't get a register of the expected type.  */
537 const char * const reg_expected_msgs[] =
538 {
539   N_("ARM register expected"),
540   N_("bad or missing co-processor number"),
541   N_("co-processor register expected"),
542   N_("FPA register expected"),
543   N_("VFP single precision register expected"),
544   N_("VFP/Neon double precision register expected"),
545   N_("Neon quad precision register expected"),
546   N_("VFP single or double precision register expected"),
547   N_("Neon double or quad precision register expected"),
548   N_("VFP single, double or Neon quad precision register expected"),
549   N_("VFP system register expected"),
550   N_("Maverick MVF register expected"),
551   N_("Maverick MVD register expected"),
552   N_("Maverick MVFX register expected"),
553   N_("Maverick MVDX register expected"),
554   N_("Maverick MVAX register expected"),
555   N_("Maverick DSPSC register expected"),
556   N_("iWMMXt data register expected"),
557   N_("iWMMXt control register expected"),
558   N_("iWMMXt scalar register expected"),
559   N_("XScale accumulator register expected"),
560 };
561
562 /* Some well known registers that we refer to directly elsewhere.  */
563 #define REG_SP  13
564 #define REG_LR  14
565 #define REG_PC  15
566
567 /* ARM instructions take 4bytes in the object file, Thumb instructions
568    take 2:  */
569 #define INSN_SIZE       4
570
571 struct asm_opcode
572 {
573   /* Basic string to match.  */
574   const char * template_name;
575
576   /* Parameters to instruction.  */
577   unsigned int operands[8];
578
579   /* Conditional tag - see opcode_lookup.  */
580   unsigned int tag : 4;
581
582   /* Basic instruction code.  */
583   unsigned int avalue : 28;
584
585   /* Thumb-format instruction code.  */
586   unsigned int tvalue;
587
588   /* Which architecture variant provides this instruction.  */
589   const arm_feature_set * avariant;
590   const arm_feature_set * tvariant;
591
592   /* Function to call to encode instruction in ARM format.  */
593   void (* aencode) (void);
594
595   /* Function to call to encode instruction in Thumb format.  */
596   void (* tencode) (void);
597 };
598
599 /* Defines for various bits that we will want to toggle.  */
600 #define INST_IMMEDIATE  0x02000000
601 #define OFFSET_REG      0x02000000
602 #define HWOFFSET_IMM    0x00400000
603 #define SHIFT_BY_REG    0x00000010
604 #define PRE_INDEX       0x01000000
605 #define INDEX_UP        0x00800000
606 #define WRITE_BACK      0x00200000
607 #define LDM_TYPE_2_OR_3 0x00400000
608 #define CPSI_MMOD       0x00020000
609
610 #define LITERAL_MASK    0xf000f000
611 #define OPCODE_MASK     0xfe1fffff
612 #define V4_STR_BIT      0x00000020
613
614 #define T2_SUBS_PC_LR   0xf3de8f00
615
616 #define DATA_OP_SHIFT   21
617
618 #define T2_OPCODE_MASK  0xfe1fffff
619 #define T2_DATA_OP_SHIFT 21
620
621 /* Codes to distinguish the arithmetic instructions.  */
622 #define OPCODE_AND      0
623 #define OPCODE_EOR      1
624 #define OPCODE_SUB      2
625 #define OPCODE_RSB      3
626 #define OPCODE_ADD      4
627 #define OPCODE_ADC      5
628 #define OPCODE_SBC      6
629 #define OPCODE_RSC      7
630 #define OPCODE_TST      8
631 #define OPCODE_TEQ      9
632 #define OPCODE_CMP      10
633 #define OPCODE_CMN      11
634 #define OPCODE_ORR      12
635 #define OPCODE_MOV      13
636 #define OPCODE_BIC      14
637 #define OPCODE_MVN      15
638
639 #define T2_OPCODE_AND   0
640 #define T2_OPCODE_BIC   1
641 #define T2_OPCODE_ORR   2
642 #define T2_OPCODE_ORN   3
643 #define T2_OPCODE_EOR   4
644 #define T2_OPCODE_ADD   8
645 #define T2_OPCODE_ADC   10
646 #define T2_OPCODE_SBC   11
647 #define T2_OPCODE_SUB   13
648 #define T2_OPCODE_RSB   14
649
650 #define T_OPCODE_MUL 0x4340
651 #define T_OPCODE_TST 0x4200
652 #define T_OPCODE_CMN 0x42c0
653 #define T_OPCODE_NEG 0x4240
654 #define T_OPCODE_MVN 0x43c0
655
656 #define T_OPCODE_ADD_R3 0x1800
657 #define T_OPCODE_SUB_R3 0x1a00
658 #define T_OPCODE_ADD_HI 0x4400
659 #define T_OPCODE_ADD_ST 0xb000
660 #define T_OPCODE_SUB_ST 0xb080
661 #define T_OPCODE_ADD_SP 0xa800
662 #define T_OPCODE_ADD_PC 0xa000
663 #define T_OPCODE_ADD_I8 0x3000
664 #define T_OPCODE_SUB_I8 0x3800
665 #define T_OPCODE_ADD_I3 0x1c00
666 #define T_OPCODE_SUB_I3 0x1e00
667
668 #define T_OPCODE_ASR_R  0x4100
669 #define T_OPCODE_LSL_R  0x4080
670 #define T_OPCODE_LSR_R  0x40c0
671 #define T_OPCODE_ROR_R  0x41c0
672 #define T_OPCODE_ASR_I  0x1000
673 #define T_OPCODE_LSL_I  0x0000
674 #define T_OPCODE_LSR_I  0x0800
675
676 #define T_OPCODE_MOV_I8 0x2000
677 #define T_OPCODE_CMP_I8 0x2800
678 #define T_OPCODE_CMP_LR 0x4280
679 #define T_OPCODE_MOV_HR 0x4600
680 #define T_OPCODE_CMP_HR 0x4500
681
682 #define T_OPCODE_LDR_PC 0x4800
683 #define T_OPCODE_LDR_SP 0x9800
684 #define T_OPCODE_STR_SP 0x9000
685 #define T_OPCODE_LDR_IW 0x6800
686 #define T_OPCODE_STR_IW 0x6000
687 #define T_OPCODE_LDR_IH 0x8800
688 #define T_OPCODE_STR_IH 0x8000
689 #define T_OPCODE_LDR_IB 0x7800
690 #define T_OPCODE_STR_IB 0x7000
691 #define T_OPCODE_LDR_RW 0x5800
692 #define T_OPCODE_STR_RW 0x5000
693 #define T_OPCODE_LDR_RH 0x5a00
694 #define T_OPCODE_STR_RH 0x5200
695 #define T_OPCODE_LDR_RB 0x5c00
696 #define T_OPCODE_STR_RB 0x5400
697
698 #define T_OPCODE_PUSH   0xb400
699 #define T_OPCODE_POP    0xbc00
700
701 #define T_OPCODE_BRANCH 0xe000
702
703 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
704 #define THUMB_PP_PC_LR 0x0100
705 #define THUMB_LOAD_BIT 0x0800
706 #define THUMB2_LOAD_BIT 0x00100000
707
708 #define BAD_ARGS        _("bad arguments to instruction")
709 #define BAD_SP          _("r13 not allowed here")
710 #define BAD_PC          _("r15 not allowed here")
711 #define BAD_COND        _("instruction cannot be conditional")
712 #define BAD_OVERLAP     _("registers may not be the same")
713 #define BAD_HIREG       _("lo register required")
714 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
715 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
716 #define BAD_BRANCH      _("branch must be last instruction in IT block")
717 #define BAD_NOT_IT      _("instruction not allowed in IT block")
718 #define BAD_FPU         _("selected FPU does not support instruction")
719 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
720 #define BAD_IT_COND     _("incorrect condition in IT block")
721 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
722 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
723 #define BAD_PC_ADDRESSING \
724         _("cannot use register index with PC-relative addressing")
725 #define BAD_PC_WRITEBACK \
726         _("cannot use writeback with PC-relative addressing")
727
728 static struct hash_control * arm_ops_hsh;
729 static struct hash_control * arm_cond_hsh;
730 static struct hash_control * arm_shift_hsh;
731 static struct hash_control * arm_psr_hsh;
732 static struct hash_control * arm_v7m_psr_hsh;
733 static struct hash_control * arm_reg_hsh;
734 static struct hash_control * arm_reloc_hsh;
735 static struct hash_control * arm_barrier_opt_hsh;
736
737 /* Stuff needed to resolve the label ambiguity
738    As:
739      ...
740      label:   <insn>
741    may differ from:
742      ...
743      label:
744               <insn>  */
745
746 symbolS *  last_label_seen;
747 static int label_is_thumb_function_name = FALSE;
748
749 /* Literal pool structure.  Held on a per-section
750    and per-sub-section basis.  */
751
752 #define MAX_LITERAL_POOL_SIZE 1024
753 typedef struct literal_pool
754 {
755   expressionS            literals [MAX_LITERAL_POOL_SIZE];
756   unsigned int           next_free_entry;
757   unsigned int           id;
758   symbolS *              symbol;
759   segT                   section;
760   subsegT                sub_section;
761   struct literal_pool *  next;
762 } literal_pool;
763
764 /* Pointer to a linked list of literal pools.  */
765 literal_pool * list_of_pools = NULL;
766
767 #ifdef OBJ_ELF
768 #  define now_it seg_info (now_seg)->tc_segment_info_data.current_it
769 #else
770 static struct current_it now_it;
771 #endif
772
773 static inline int
774 now_it_compatible (int cond)
775 {
776   return (cond & ~1) == (now_it.cc & ~1);
777 }
778
779 static inline int
780 conditional_insn (void)
781 {
782   return inst.cond != COND_ALWAYS;
783 }
784
785 static int in_it_block (void);
786
787 static int handle_it_state (void);
788
789 static void force_automatic_it_block_close (void);
790
791 static void it_fsm_post_encode (void);
792
793 #define set_it_insn_type(type)                  \
794   do                                            \
795     {                                           \
796       inst.it_insn_type = type;                 \
797       if (handle_it_state () == FAIL)           \
798         return;                                 \
799     }                                           \
800   while (0)
801
802 #define set_it_insn_type_nonvoid(type, failret) \
803   do                                            \
804     {                                           \
805       inst.it_insn_type = type;                 \
806       if (handle_it_state () == FAIL)           \
807         return failret;                         \
808     }                                           \
809   while(0)
810
811 #define set_it_insn_type_last()                         \
812   do                                                    \
813     {                                                   \
814       if (inst.cond == COND_ALWAYS)                     \
815         set_it_insn_type (IF_INSIDE_IT_LAST_INSN);      \
816       else                                              \
817         set_it_insn_type (INSIDE_IT_LAST_INSN);         \
818     }                                                   \
819   while (0)
820
821 /* Pure syntax.  */
822
823 /* This array holds the chars that always start a comment.  If the
824    pre-processor is disabled, these aren't very useful.  */
825 const char comment_chars[] = "@";
826
827 /* This array holds the chars that only start a comment at the beginning of
828    a line.  If the line seems to have the form '# 123 filename'
829    .line and .file directives will appear in the pre-processed output.  */
830 /* Note that input_file.c hand checks for '#' at the beginning of the
831    first line of the input file.  This is because the compiler outputs
832    #NO_APP at the beginning of its output.  */
833 /* Also note that comments like this one will always work.  */
834 const char line_comment_chars[] = "#";
835
836 const char line_separator_chars[] = ";";
837
838 /* Chars that can be used to separate mant
839    from exp in floating point numbers.  */
840 const char EXP_CHARS[] = "eE";
841
842 /* Chars that mean this number is a floating point constant.  */
843 /* As in 0f12.456  */
844 /* or    0d1.2345e12  */
845
846 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
847
848 /* Prefix characters that indicate the start of an immediate
849    value.  */
850 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
851
852 /* Separator character handling.  */
853
854 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
855
856 static inline int
857 skip_past_char (char ** str, char c)
858 {
859   if (**str == c)
860     {
861       (*str)++;
862       return SUCCESS;
863     }
864   else
865     return FAIL;
866 }
867
868 #define skip_past_comma(str) skip_past_char (str, ',')
869
870 /* Arithmetic expressions (possibly involving symbols).  */
871
872 /* Return TRUE if anything in the expression is a bignum.  */
873
874 static int
875 walk_no_bignums (symbolS * sp)
876 {
877   if (symbol_get_value_expression (sp)->X_op == O_big)
878     return 1;
879
880   if (symbol_get_value_expression (sp)->X_add_symbol)
881     {
882       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
883               || (symbol_get_value_expression (sp)->X_op_symbol
884                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
885     }
886
887   return 0;
888 }
889
890 static int in_my_get_expression = 0;
891
892 /* Third argument to my_get_expression.  */
893 #define GE_NO_PREFIX 0
894 #define GE_IMM_PREFIX 1
895 #define GE_OPT_PREFIX 2
896 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
897    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
898 #define GE_OPT_PREFIX_BIG 3
899
900 static int
901 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
902 {
903   char * save_in;
904   segT   seg;
905
906   /* In unified syntax, all prefixes are optional.  */
907   if (unified_syntax)
908     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
909                   : GE_OPT_PREFIX;
910
911   switch (prefix_mode)
912     {
913     case GE_NO_PREFIX: break;
914     case GE_IMM_PREFIX:
915       if (!is_immediate_prefix (**str))
916         {
917           inst.error = _("immediate expression requires a # prefix");
918           return FAIL;
919         }
920       (*str)++;
921       break;
922     case GE_OPT_PREFIX:
923     case GE_OPT_PREFIX_BIG:
924       if (is_immediate_prefix (**str))
925         (*str)++;
926       break;
927     default: abort ();
928     }
929
930   memset (ep, 0, sizeof (expressionS));
931
932   save_in = input_line_pointer;
933   input_line_pointer = *str;
934   in_my_get_expression = 1;
935   seg = expression (ep);
936   in_my_get_expression = 0;
937
938   if (ep->X_op == O_illegal || ep->X_op == O_absent)
939     {
940       /* We found a bad or missing expression in md_operand().  */
941       *str = input_line_pointer;
942       input_line_pointer = save_in;
943       if (inst.error == NULL)
944         inst.error = (ep->X_op == O_absent
945                       ? _("missing expression") :_("bad expression"));
946       return 1;
947     }
948
949 #ifdef OBJ_AOUT
950   if (seg != absolute_section
951       && seg != text_section
952       && seg != data_section
953       && seg != bss_section
954       && seg != undefined_section)
955     {
956       inst.error = _("bad segment");
957       *str = input_line_pointer;
958       input_line_pointer = save_in;
959       return 1;
960     }
961 #else
962   (void) seg;
963 #endif
964
965   /* Get rid of any bignums now, so that we don't generate an error for which
966      we can't establish a line number later on.  Big numbers are never valid
967      in instructions, which is where this routine is always called.  */
968   if (prefix_mode != GE_OPT_PREFIX_BIG
969       && (ep->X_op == O_big
970           || (ep->X_add_symbol
971               && (walk_no_bignums (ep->X_add_symbol)
972                   || (ep->X_op_symbol
973                       && walk_no_bignums (ep->X_op_symbol))))))
974     {
975       inst.error = _("invalid constant");
976       *str = input_line_pointer;
977       input_line_pointer = save_in;
978       return 1;
979     }
980
981   *str = input_line_pointer;
982   input_line_pointer = save_in;
983   return 0;
984 }
985
986 /* Turn a string in input_line_pointer into a floating point constant
987    of type TYPE, and store the appropriate bytes in *LITP.  The number
988    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
989    returned, or NULL on OK.
990
991    Note that fp constants aren't represent in the normal way on the ARM.
992    In big endian mode, things are as expected.  However, in little endian
993    mode fp constants are big-endian word-wise, and little-endian byte-wise
994    within the words.  For example, (double) 1.1 in big endian mode is
995    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
996    the byte sequence 99 99 f1 3f 9a 99 99 99.
997
998    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
999
1000 char *
1001 md_atof (int type, char * litP, int * sizeP)
1002 {
1003   int prec;
1004   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1005   char *t;
1006   int i;
1007
1008   switch (type)
1009     {
1010     case 'f':
1011     case 'F':
1012     case 's':
1013     case 'S':
1014       prec = 2;
1015       break;
1016
1017     case 'd':
1018     case 'D':
1019     case 'r':
1020     case 'R':
1021       prec = 4;
1022       break;
1023
1024     case 'x':
1025     case 'X':
1026       prec = 5;
1027       break;
1028
1029     case 'p':
1030     case 'P':
1031       prec = 5;
1032       break;
1033
1034     default:
1035       *sizeP = 0;
1036       return _("Unrecognized or unsupported floating point constant");
1037     }
1038
1039   t = atof_ieee (input_line_pointer, type, words);
1040   if (t)
1041     input_line_pointer = t;
1042   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1043
1044   if (target_big_endian)
1045     {
1046       for (i = 0; i < prec; i++)
1047         {
1048           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1049           litP += sizeof (LITTLENUM_TYPE);
1050         }
1051     }
1052   else
1053     {
1054       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1055         for (i = prec - 1; i >= 0; i--)
1056           {
1057             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1058             litP += sizeof (LITTLENUM_TYPE);
1059           }
1060       else
1061         /* For a 4 byte float the order of elements in `words' is 1 0.
1062            For an 8 byte float the order is 1 0 3 2.  */
1063         for (i = 0; i < prec; i += 2)
1064           {
1065             md_number_to_chars (litP, (valueT) words[i + 1],
1066                                 sizeof (LITTLENUM_TYPE));
1067             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1068                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1069             litP += 2 * sizeof (LITTLENUM_TYPE);
1070           }
1071     }
1072
1073   return NULL;
1074 }
1075
1076 /* We handle all bad expressions here, so that we can report the faulty
1077    instruction in the error message.  */
1078 void
1079 md_operand (expressionS * exp)
1080 {
1081   if (in_my_get_expression)
1082     exp->X_op = O_illegal;
1083 }
1084
1085 /* Immediate values.  */
1086
1087 /* Generic immediate-value read function for use in directives.
1088    Accepts anything that 'expression' can fold to a constant.
1089    *val receives the number.  */
1090 #ifdef OBJ_ELF
1091 static int
1092 immediate_for_directive (int *val)
1093 {
1094   expressionS exp;
1095   exp.X_op = O_illegal;
1096
1097   if (is_immediate_prefix (*input_line_pointer))
1098     {
1099       input_line_pointer++;
1100       expression (&exp);
1101     }
1102
1103   if (exp.X_op != O_constant)
1104     {
1105       as_bad (_("expected #constant"));
1106       ignore_rest_of_line ();
1107       return FAIL;
1108     }
1109   *val = exp.X_add_number;
1110   return SUCCESS;
1111 }
1112 #endif
1113
1114 /* Register parsing.  */
1115
1116 /* Generic register parser.  CCP points to what should be the
1117    beginning of a register name.  If it is indeed a valid register
1118    name, advance CCP over it and return the reg_entry structure;
1119    otherwise return NULL.  Does not issue diagnostics.  */
1120
1121 static struct reg_entry *
1122 arm_reg_parse_multi (char **ccp)
1123 {
1124   char *start = *ccp;
1125   char *p;
1126   struct reg_entry *reg;
1127
1128 #ifdef REGISTER_PREFIX
1129   if (*start != REGISTER_PREFIX)
1130     return NULL;
1131   start++;
1132 #endif
1133 #ifdef OPTIONAL_REGISTER_PREFIX
1134   if (*start == OPTIONAL_REGISTER_PREFIX)
1135     start++;
1136 #endif
1137
1138   p = start;
1139   if (!ISALPHA (*p) || !is_name_beginner (*p))
1140     return NULL;
1141
1142   do
1143     p++;
1144   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1145
1146   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1147
1148   if (!reg)
1149     return NULL;
1150
1151   *ccp = p;
1152   return reg;
1153 }
1154
1155 static int
1156 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1157                     enum arm_reg_type type)
1158 {
1159   /* Alternative syntaxes are accepted for a few register classes.  */
1160   switch (type)
1161     {
1162     case REG_TYPE_MVF:
1163     case REG_TYPE_MVD:
1164     case REG_TYPE_MVFX:
1165     case REG_TYPE_MVDX:
1166       /* Generic coprocessor register names are allowed for these.  */
1167       if (reg && reg->type == REG_TYPE_CN)
1168         return reg->number;
1169       break;
1170
1171     case REG_TYPE_CP:
1172       /* For backward compatibility, a bare number is valid here.  */
1173       {
1174         unsigned long processor = strtoul (start, ccp, 10);
1175         if (*ccp != start && processor <= 15)
1176           return processor;
1177       }
1178
1179     case REG_TYPE_MMXWC:
1180       /* WC includes WCG.  ??? I'm not sure this is true for all
1181          instructions that take WC registers.  */
1182       if (reg && reg->type == REG_TYPE_MMXWCG)
1183         return reg->number;
1184       break;
1185
1186     default:
1187       break;
1188     }
1189
1190   return FAIL;
1191 }
1192
1193 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1194    return value is the register number or FAIL.  */
1195
1196 static int
1197 arm_reg_parse (char **ccp, enum arm_reg_type type)
1198 {
1199   char *start = *ccp;
1200   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1201   int ret;
1202
1203   /* Do not allow a scalar (reg+index) to parse as a register.  */
1204   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1205     return FAIL;
1206
1207   if (reg && reg->type == type)
1208     return reg->number;
1209
1210   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1211     return ret;
1212
1213   *ccp = start;
1214   return FAIL;
1215 }
1216
1217 /* Parse a Neon type specifier. *STR should point at the leading '.'
1218    character. Does no verification at this stage that the type fits the opcode
1219    properly. E.g.,
1220
1221      .i32.i32.s16
1222      .s32.f32
1223      .u16
1224
1225    Can all be legally parsed by this function.
1226
1227    Fills in neon_type struct pointer with parsed information, and updates STR
1228    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1229    type, FAIL if not.  */
1230
1231 static int
1232 parse_neon_type (struct neon_type *type, char **str)
1233 {
1234   char *ptr = *str;
1235
1236   if (type)
1237     type->elems = 0;
1238
1239   while (type->elems < NEON_MAX_TYPE_ELS)
1240     {
1241       enum neon_el_type thistype = NT_untyped;
1242       unsigned thissize = -1u;
1243
1244       if (*ptr != '.')
1245         break;
1246
1247       ptr++;
1248
1249       /* Just a size without an explicit type.  */
1250       if (ISDIGIT (*ptr))
1251         goto parsesize;
1252
1253       switch (TOLOWER (*ptr))
1254         {
1255         case 'i': thistype = NT_integer; break;
1256         case 'f': thistype = NT_float; break;
1257         case 'p': thistype = NT_poly; break;
1258         case 's': thistype = NT_signed; break;
1259         case 'u': thistype = NT_unsigned; break;
1260         case 'd':
1261           thistype = NT_float;
1262           thissize = 64;
1263           ptr++;
1264           goto done;
1265         default:
1266           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1267           return FAIL;
1268         }
1269
1270       ptr++;
1271
1272       /* .f is an abbreviation for .f32.  */
1273       if (thistype == NT_float && !ISDIGIT (*ptr))
1274         thissize = 32;
1275       else
1276         {
1277         parsesize:
1278           thissize = strtoul (ptr, &ptr, 10);
1279
1280           if (thissize != 8 && thissize != 16 && thissize != 32
1281               && thissize != 64)
1282             {
1283               as_bad (_("bad size %d in type specifier"), thissize);
1284               return FAIL;
1285             }
1286         }
1287
1288       done:
1289       if (type)
1290         {
1291           type->el[type->elems].type = thistype;
1292           type->el[type->elems].size = thissize;
1293           type->elems++;
1294         }
1295     }
1296
1297   /* Empty/missing type is not a successful parse.  */
1298   if (type->elems == 0)
1299     return FAIL;
1300
1301   *str = ptr;
1302
1303   return SUCCESS;
1304 }
1305
1306 /* Errors may be set multiple times during parsing or bit encoding
1307    (particularly in the Neon bits), but usually the earliest error which is set
1308    will be the most meaningful. Avoid overwriting it with later (cascading)
1309    errors by calling this function.  */
1310
1311 static void
1312 first_error (const char *err)
1313 {
1314   if (!inst.error)
1315     inst.error = err;
1316 }
1317
1318 /* Parse a single type, e.g. ".s32", leading period included.  */
1319 static int
1320 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1321 {
1322   char *str = *ccp;
1323   struct neon_type optype;
1324
1325   if (*str == '.')
1326     {
1327       if (parse_neon_type (&optype, &str) == SUCCESS)
1328         {
1329           if (optype.elems == 1)
1330             *vectype = optype.el[0];
1331           else
1332             {
1333               first_error (_("only one type should be specified for operand"));
1334               return FAIL;
1335             }
1336         }
1337       else
1338         {
1339           first_error (_("vector type expected"));
1340           return FAIL;
1341         }
1342     }
1343   else
1344     return FAIL;
1345
1346   *ccp = str;
1347
1348   return SUCCESS;
1349 }
1350
1351 /* Special meanings for indices (which have a range of 0-7), which will fit into
1352    a 4-bit integer.  */
1353
1354 #define NEON_ALL_LANES          15
1355 #define NEON_INTERLEAVE_LANES   14
1356
1357 /* Parse either a register or a scalar, with an optional type. Return the
1358    register number, and optionally fill in the actual type of the register
1359    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1360    type/index information in *TYPEINFO.  */
1361
1362 static int
1363 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1364                            enum arm_reg_type *rtype,
1365                            struct neon_typed_alias *typeinfo)
1366 {
1367   char *str = *ccp;
1368   struct reg_entry *reg = arm_reg_parse_multi (&str);
1369   struct neon_typed_alias atype;
1370   struct neon_type_el parsetype;
1371
1372   atype.defined = 0;
1373   atype.index = -1;
1374   atype.eltype.type = NT_invtype;
1375   atype.eltype.size = -1;
1376
1377   /* Try alternate syntax for some types of register. Note these are mutually
1378      exclusive with the Neon syntax extensions.  */
1379   if (reg == NULL)
1380     {
1381       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1382       if (altreg != FAIL)
1383         *ccp = str;
1384       if (typeinfo)
1385         *typeinfo = atype;
1386       return altreg;
1387     }
1388
1389   /* Undo polymorphism when a set of register types may be accepted.  */
1390   if ((type == REG_TYPE_NDQ
1391        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1392       || (type == REG_TYPE_VFSD
1393           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1394       || (type == REG_TYPE_NSDQ
1395           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1396               || reg->type == REG_TYPE_NQ))
1397       || (type == REG_TYPE_MMXWC
1398           && (reg->type == REG_TYPE_MMXWCG)))
1399     type = (enum arm_reg_type) reg->type;
1400
1401   if (type != reg->type)
1402     return FAIL;
1403
1404   if (reg->neon)
1405     atype = *reg->neon;
1406
1407   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1408     {
1409       if ((atype.defined & NTA_HASTYPE) != 0)
1410         {
1411           first_error (_("can't redefine type for operand"));
1412           return FAIL;
1413         }
1414       atype.defined |= NTA_HASTYPE;
1415       atype.eltype = parsetype;
1416     }
1417
1418   if (skip_past_char (&str, '[') == SUCCESS)
1419     {
1420       if (type != REG_TYPE_VFD)
1421         {
1422           first_error (_("only D registers may be indexed"));
1423           return FAIL;
1424         }
1425
1426       if ((atype.defined & NTA_HASINDEX) != 0)
1427         {
1428           first_error (_("can't change index for operand"));
1429           return FAIL;
1430         }
1431
1432       atype.defined |= NTA_HASINDEX;
1433
1434       if (skip_past_char (&str, ']') == SUCCESS)
1435         atype.index = NEON_ALL_LANES;
1436       else
1437         {
1438           expressionS exp;
1439
1440           my_get_expression (&exp, &str, GE_NO_PREFIX);
1441
1442           if (exp.X_op != O_constant)
1443             {
1444               first_error (_("constant expression required"));
1445               return FAIL;
1446             }
1447
1448           if (skip_past_char (&str, ']') == FAIL)
1449             return FAIL;
1450
1451           atype.index = exp.X_add_number;
1452         }
1453     }
1454
1455   if (typeinfo)
1456     *typeinfo = atype;
1457
1458   if (rtype)
1459     *rtype = type;
1460
1461   *ccp = str;
1462
1463   return reg->number;
1464 }
1465
1466 /* Like arm_reg_parse, but allow allow the following extra features:
1467     - If RTYPE is non-zero, return the (possibly restricted) type of the
1468       register (e.g. Neon double or quad reg when either has been requested).
1469     - If this is a Neon vector type with additional type information, fill
1470       in the struct pointed to by VECTYPE (if non-NULL).
1471    This function will fault on encountering a scalar.  */
1472
1473 static int
1474 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1475                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1476 {
1477   struct neon_typed_alias atype;
1478   char *str = *ccp;
1479   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1480
1481   if (reg == FAIL)
1482     return FAIL;
1483
1484   /* Do not allow regname(... to parse as a register.  */
1485   if (*str == '(')
1486     return FAIL;
1487
1488   /* Do not allow a scalar (reg+index) to parse as a register.  */
1489   if ((atype.defined & NTA_HASINDEX) != 0)
1490     {
1491       first_error (_("register operand expected, but got scalar"));
1492       return FAIL;
1493     }
1494
1495   if (vectype)
1496     *vectype = atype.eltype;
1497
1498   *ccp = str;
1499
1500   return reg;
1501 }
1502
1503 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1504 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1505
1506 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1507    have enough information to be able to do a good job bounds-checking. So, we
1508    just do easy checks here, and do further checks later.  */
1509
1510 static int
1511 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1512 {
1513   int reg;
1514   char *str = *ccp;
1515   struct neon_typed_alias atype;
1516
1517   reg = parse_typed_reg_or_scalar (&str, REG_TYPE_VFD, NULL, &atype);
1518
1519   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1520     return FAIL;
1521
1522   if (atype.index == NEON_ALL_LANES)
1523     {
1524       first_error (_("scalar must have an index"));
1525       return FAIL;
1526     }
1527   else if (atype.index >= 64 / elsize)
1528     {
1529       first_error (_("scalar index out of range"));
1530       return FAIL;
1531     }
1532
1533   if (type)
1534     *type = atype.eltype;
1535
1536   *ccp = str;
1537
1538   return reg * 16 + atype.index;
1539 }
1540
1541 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1542
1543 static long
1544 parse_reg_list (char ** strp)
1545 {
1546   char * str = * strp;
1547   long   range = 0;
1548   int    another_range;
1549
1550   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1551   do
1552     {
1553       another_range = 0;
1554
1555       if (*str == '{')
1556         {
1557           int in_range = 0;
1558           int cur_reg = -1;
1559
1560           str++;
1561           do
1562             {
1563               int reg;
1564
1565               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1566                 {
1567                   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
1568                   return FAIL;
1569                 }
1570
1571               if (in_range)
1572                 {
1573                   int i;
1574
1575                   if (reg <= cur_reg)
1576                     {
1577                       first_error (_("bad range in register list"));
1578                       return FAIL;
1579                     }
1580
1581                   for (i = cur_reg + 1; i < reg; i++)
1582                     {
1583                       if (range & (1 << i))
1584                         as_tsktsk
1585                           (_("Warning: duplicated register (r%d) in register list"),
1586                            i);
1587                       else
1588                         range |= 1 << i;
1589                     }
1590                   in_range = 0;
1591                 }
1592
1593               if (range & (1 << reg))
1594                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1595                            reg);
1596               else if (reg <= cur_reg)
1597                 as_tsktsk (_("Warning: register range not in ascending order"));
1598
1599               range |= 1 << reg;
1600               cur_reg = reg;
1601             }
1602           while (skip_past_comma (&str) != FAIL
1603                  || (in_range = 1, *str++ == '-'));
1604           str--;
1605
1606           if (*str++ != '}')
1607             {
1608               first_error (_("missing `}'"));
1609               return FAIL;
1610             }
1611         }
1612       else
1613         {
1614           expressionS exp;
1615
1616           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1617             return FAIL;
1618
1619           if (exp.X_op == O_constant)
1620             {
1621               if (exp.X_add_number
1622                   != (exp.X_add_number & 0x0000ffff))
1623                 {
1624                   inst.error = _("invalid register mask");
1625                   return FAIL;
1626                 }
1627
1628               if ((range & exp.X_add_number) != 0)
1629                 {
1630                   int regno = range & exp.X_add_number;
1631
1632                   regno &= -regno;
1633                   regno = (1 << regno) - 1;
1634                   as_tsktsk
1635                     (_("Warning: duplicated register (r%d) in register list"),
1636                      regno);
1637                 }
1638
1639               range |= exp.X_add_number;
1640             }
1641           else
1642             {
1643               if (inst.reloc.type != 0)
1644                 {
1645                   inst.error = _("expression too complex");
1646                   return FAIL;
1647                 }
1648
1649               memcpy (&inst.reloc.exp, &exp, sizeof (expressionS));
1650               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1651               inst.reloc.pc_rel = 0;
1652             }
1653         }
1654
1655       if (*str == '|' || *str == '+')
1656         {
1657           str++;
1658           another_range = 1;
1659         }
1660     }
1661   while (another_range);
1662
1663   *strp = str;
1664   return range;
1665 }
1666
1667 /* Types of registers in a list.  */
1668
1669 enum reg_list_els
1670 {
1671   REGLIST_VFP_S,
1672   REGLIST_VFP_D,
1673   REGLIST_NEON_D
1674 };
1675
1676 /* Parse a VFP register list.  If the string is invalid return FAIL.
1677    Otherwise return the number of registers, and set PBASE to the first
1678    register.  Parses registers of type ETYPE.
1679    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1680      - Q registers can be used to specify pairs of D registers
1681      - { } can be omitted from around a singleton register list
1682          FIXME: This is not implemented, as it would require backtracking in
1683          some cases, e.g.:
1684            vtbl.8 d3,d4,d5
1685          This could be done (the meaning isn't really ambiguous), but doesn't
1686          fit in well with the current parsing framework.
1687      - 32 D registers may be used (also true for VFPv3).
1688    FIXME: Types are ignored in these register lists, which is probably a
1689    bug.  */
1690
1691 static int
1692 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype)
1693 {
1694   char *str = *ccp;
1695   int base_reg;
1696   int new_base;
1697   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1698   int max_regs = 0;
1699   int count = 0;
1700   int warned = 0;
1701   unsigned long mask = 0;
1702   int i;
1703
1704   if (*str != '{')
1705     {
1706       inst.error = _("expecting {");
1707       return FAIL;
1708     }
1709
1710   str++;
1711
1712   switch (etype)
1713     {
1714     case REGLIST_VFP_S:
1715       regtype = REG_TYPE_VFS;
1716       max_regs = 32;
1717       break;
1718
1719     case REGLIST_VFP_D:
1720       regtype = REG_TYPE_VFD;
1721       break;
1722
1723     case REGLIST_NEON_D:
1724       regtype = REG_TYPE_NDQ;
1725       break;
1726     }
1727
1728   if (etype != REGLIST_VFP_S)
1729     {
1730       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
1731       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
1732         {
1733           max_regs = 32;
1734           if (thumb_mode)
1735             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1736                                     fpu_vfp_ext_d32);
1737           else
1738             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1739                                     fpu_vfp_ext_d32);
1740         }
1741       else
1742         max_regs = 16;
1743     }
1744
1745   base_reg = max_regs;
1746
1747   do
1748     {
1749       int setmask = 1, addregs = 1;
1750
1751       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
1752
1753       if (new_base == FAIL)
1754         {
1755           first_error (_(reg_expected_msgs[regtype]));
1756           return FAIL;
1757         }
1758
1759       if (new_base >= max_regs)
1760         {
1761           first_error (_("register out of range in list"));
1762           return FAIL;
1763         }
1764
1765       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1766       if (regtype == REG_TYPE_NQ)
1767         {
1768           setmask = 3;
1769           addregs = 2;
1770         }
1771
1772       if (new_base < base_reg)
1773         base_reg = new_base;
1774
1775       if (mask & (setmask << new_base))
1776         {
1777           first_error (_("invalid register list"));
1778           return FAIL;
1779         }
1780
1781       if ((mask >> new_base) != 0 && ! warned)
1782         {
1783           as_tsktsk (_("register list not in ascending order"));
1784           warned = 1;
1785         }
1786
1787       mask |= setmask << new_base;
1788       count += addregs;
1789
1790       if (*str == '-') /* We have the start of a range expression */
1791         {
1792           int high_range;
1793
1794           str++;
1795
1796           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
1797               == FAIL)
1798             {
1799               inst.error = gettext (reg_expected_msgs[regtype]);
1800               return FAIL;
1801             }
1802
1803           if (high_range >= max_regs)
1804             {
1805               first_error (_("register out of range in list"));
1806               return FAIL;
1807             }
1808
1809           if (regtype == REG_TYPE_NQ)
1810             high_range = high_range + 1;
1811
1812           if (high_range <= new_base)
1813             {
1814               inst.error = _("register range not in ascending order");
1815               return FAIL;
1816             }
1817
1818           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1819             {
1820               if (mask & (setmask << new_base))
1821                 {
1822                   inst.error = _("invalid register list");
1823                   return FAIL;
1824                 }
1825
1826               mask |= setmask << new_base;
1827               count += addregs;
1828             }
1829         }
1830     }
1831   while (skip_past_comma (&str) != FAIL);
1832
1833   str++;
1834
1835   /* Sanity check -- should have raised a parse error above.  */
1836   if (count == 0 || count > max_regs)
1837     abort ();
1838
1839   *pbase = base_reg;
1840
1841   /* Final test -- the registers must be consecutive.  */
1842   mask >>= base_reg;
1843   for (i = 0; i < count; i++)
1844     {
1845       if ((mask & (1u << i)) == 0)
1846         {
1847           inst.error = _("non-contiguous register range");
1848           return FAIL;
1849         }
1850     }
1851
1852   *ccp = str;
1853
1854   return count;
1855 }
1856
1857 /* True if two alias types are the same.  */
1858
1859 static bfd_boolean
1860 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
1861 {
1862   if (!a && !b)
1863     return TRUE;
1864
1865   if (!a || !b)
1866     return FALSE;
1867
1868   if (a->defined != b->defined)
1869     return FALSE;
1870
1871   if ((a->defined & NTA_HASTYPE) != 0
1872       && (a->eltype.type != b->eltype.type
1873           || a->eltype.size != b->eltype.size))
1874     return FALSE;
1875
1876   if ((a->defined & NTA_HASINDEX) != 0
1877       && (a->index != b->index))
1878     return FALSE;
1879
1880   return TRUE;
1881 }
1882
1883 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
1884    The base register is put in *PBASE.
1885    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
1886    the return value.
1887    The register stride (minus one) is put in bit 4 of the return value.
1888    Bits [6:5] encode the list length (minus one).
1889    The type of the list elements is put in *ELTYPE, if non-NULL.  */
1890
1891 #define NEON_LANE(X)            ((X) & 0xf)
1892 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
1893 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
1894
1895 static int
1896 parse_neon_el_struct_list (char **str, unsigned *pbase,
1897                            struct neon_type_el *eltype)
1898 {
1899   char *ptr = *str;
1900   int base_reg = -1;
1901   int reg_incr = -1;
1902   int count = 0;
1903   int lane = -1;
1904   int leading_brace = 0;
1905   enum arm_reg_type rtype = REG_TYPE_NDQ;
1906   const char *const incr_error = _("register stride must be 1 or 2");
1907   const char *const type_error = _("mismatched element/structure types in list");
1908   struct neon_typed_alias firsttype;
1909
1910   if (skip_past_char (&ptr, '{') == SUCCESS)
1911     leading_brace = 1;
1912
1913   do
1914     {
1915       struct neon_typed_alias atype;
1916       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
1917
1918       if (getreg == FAIL)
1919         {
1920           first_error (_(reg_expected_msgs[rtype]));
1921           return FAIL;
1922         }
1923
1924       if (base_reg == -1)
1925         {
1926           base_reg = getreg;
1927           if (rtype == REG_TYPE_NQ)
1928             {
1929               reg_incr = 1;
1930             }
1931           firsttype = atype;
1932         }
1933       else if (reg_incr == -1)
1934         {
1935           reg_incr = getreg - base_reg;
1936           if (reg_incr < 1 || reg_incr > 2)
1937             {
1938               first_error (_(incr_error));
1939               return FAIL;
1940             }
1941         }
1942       else if (getreg != base_reg + reg_incr * count)
1943         {
1944           first_error (_(incr_error));
1945           return FAIL;
1946         }
1947
1948       if (! neon_alias_types_same (&atype, &firsttype))
1949         {
1950           first_error (_(type_error));
1951           return FAIL;
1952         }
1953
1954       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
1955          modes.  */
1956       if (ptr[0] == '-')
1957         {
1958           struct neon_typed_alias htype;
1959           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
1960           if (lane == -1)
1961             lane = NEON_INTERLEAVE_LANES;
1962           else if (lane != NEON_INTERLEAVE_LANES)
1963             {
1964               first_error (_(type_error));
1965               return FAIL;
1966             }
1967           if (reg_incr == -1)
1968             reg_incr = 1;
1969           else if (reg_incr != 1)
1970             {
1971               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
1972               return FAIL;
1973             }
1974           ptr++;
1975           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
1976           if (hireg == FAIL)
1977             {
1978               first_error (_(reg_expected_msgs[rtype]));
1979               return FAIL;
1980             }
1981           if (! neon_alias_types_same (&htype, &firsttype))
1982             {
1983               first_error (_(type_error));
1984               return FAIL;
1985             }
1986           count += hireg + dregs - getreg;
1987           continue;
1988         }
1989
1990       /* If we're using Q registers, we can't use [] or [n] syntax.  */
1991       if (rtype == REG_TYPE_NQ)
1992         {
1993           count += 2;
1994           continue;
1995         }
1996
1997       if ((atype.defined & NTA_HASINDEX) != 0)
1998         {
1999           if (lane == -1)
2000             lane = atype.index;
2001           else if (lane != atype.index)
2002             {
2003               first_error (_(type_error));
2004               return FAIL;
2005             }
2006         }
2007       else if (lane == -1)
2008         lane = NEON_INTERLEAVE_LANES;
2009       else if (lane != NEON_INTERLEAVE_LANES)
2010         {
2011           first_error (_(type_error));
2012           return FAIL;
2013         }
2014       count++;
2015     }
2016   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2017
2018   /* No lane set by [x]. We must be interleaving structures.  */
2019   if (lane == -1)
2020     lane = NEON_INTERLEAVE_LANES;
2021
2022   /* Sanity check.  */
2023   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
2024       || (count > 1 && reg_incr == -1))
2025     {
2026       first_error (_("error parsing element/structure list"));
2027       return FAIL;
2028     }
2029
2030   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2031     {
2032       first_error (_("expected }"));
2033       return FAIL;
2034     }
2035
2036   if (reg_incr == -1)
2037     reg_incr = 1;
2038
2039   if (eltype)
2040     *eltype = firsttype.eltype;
2041
2042   *pbase = base_reg;
2043   *str = ptr;
2044
2045   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2046 }
2047
2048 /* Parse an explicit relocation suffix on an expression.  This is
2049    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2050    arm_reloc_hsh contains no entries, so this function can only
2051    succeed if there is no () after the word.  Returns -1 on error,
2052    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2053 static int
2054 parse_reloc (char **str)
2055 {
2056   struct reloc_entry *r;
2057   char *p, *q;
2058
2059   if (**str != '(')
2060     return BFD_RELOC_UNUSED;
2061
2062   p = *str + 1;
2063   q = p;
2064
2065   while (*q && *q != ')' && *q != ',')
2066     q++;
2067   if (*q != ')')
2068     return -1;
2069
2070   if ((r = (struct reloc_entry *)
2071        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2072     return -1;
2073
2074   *str = q + 1;
2075   return r->reloc;
2076 }
2077
2078 /* Directives: register aliases.  */
2079
2080 static struct reg_entry *
2081 insert_reg_alias (char *str, unsigned number, int type)
2082 {
2083   struct reg_entry *new_reg;
2084   const char *name;
2085
2086   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2087     {
2088       if (new_reg->builtin)
2089         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2090
2091       /* Only warn about a redefinition if it's not defined as the
2092          same register.  */
2093       else if (new_reg->number != number || new_reg->type != type)
2094         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2095
2096       return NULL;
2097     }
2098
2099   name = xstrdup (str);
2100   new_reg = (struct reg_entry *) xmalloc (sizeof (struct reg_entry));
2101
2102   new_reg->name = name;
2103   new_reg->number = number;
2104   new_reg->type = type;
2105   new_reg->builtin = FALSE;
2106   new_reg->neon = NULL;
2107
2108   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2109     abort ();
2110
2111   return new_reg;
2112 }
2113
2114 static void
2115 insert_neon_reg_alias (char *str, int number, int type,
2116                        struct neon_typed_alias *atype)
2117 {
2118   struct reg_entry *reg = insert_reg_alias (str, number, type);
2119
2120   if (!reg)
2121     {
2122       first_error (_("attempt to redefine typed alias"));
2123       return;
2124     }
2125
2126   if (atype)
2127     {
2128       reg->neon = (struct neon_typed_alias *)
2129           xmalloc (sizeof (struct neon_typed_alias));
2130       *reg->neon = *atype;
2131     }
2132 }
2133
2134 /* Look for the .req directive.  This is of the form:
2135
2136         new_register_name .req existing_register_name
2137
2138    If we find one, or if it looks sufficiently like one that we want to
2139    handle any error here, return TRUE.  Otherwise return FALSE.  */
2140
2141 static bfd_boolean
2142 create_register_alias (char * newname, char *p)
2143 {
2144   struct reg_entry *old;
2145   char *oldname, *nbuf;
2146   size_t nlen;
2147
2148   /* The input scrubber ensures that whitespace after the mnemonic is
2149      collapsed to single spaces.  */
2150   oldname = p;
2151   if (strncmp (oldname, " .req ", 6) != 0)
2152     return FALSE;
2153
2154   oldname += 6;
2155   if (*oldname == '\0')
2156     return FALSE;
2157
2158   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2159   if (!old)
2160     {
2161       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2162       return TRUE;
2163     }
2164
2165   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2166      the desired alias name, and p points to its end.  If not, then
2167      the desired alias name is in the global original_case_string.  */
2168 #ifdef TC_CASE_SENSITIVE
2169   nlen = p - newname;
2170 #else
2171   newname = original_case_string;
2172   nlen = strlen (newname);
2173 #endif
2174
2175   nbuf = (char *) alloca (nlen + 1);
2176   memcpy (nbuf, newname, nlen);
2177   nbuf[nlen] = '\0';
2178
2179   /* Create aliases under the new name as stated; an all-lowercase
2180      version of the new name; and an all-uppercase version of the new
2181      name.  */
2182   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2183     {
2184       for (p = nbuf; *p; p++)
2185         *p = TOUPPER (*p);
2186
2187       if (strncmp (nbuf, newname, nlen))
2188         {
2189           /* If this attempt to create an additional alias fails, do not bother
2190              trying to create the all-lower case alias.  We will fail and issue
2191              a second, duplicate error message.  This situation arises when the
2192              programmer does something like:
2193                foo .req r0
2194                Foo .req r1
2195              The second .req creates the "Foo" alias but then fails to create
2196              the artificial FOO alias because it has already been created by the
2197              first .req.  */
2198           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2199             return TRUE;
2200         }
2201
2202       for (p = nbuf; *p; p++)
2203         *p = TOLOWER (*p);
2204
2205       if (strncmp (nbuf, newname, nlen))
2206         insert_reg_alias (nbuf, old->number, old->type);
2207     }
2208
2209   return TRUE;
2210 }
2211
2212 /* Create a Neon typed/indexed register alias using directives, e.g.:
2213      X .dn d5.s32[1]
2214      Y .qn 6.s16
2215      Z .dn d7
2216      T .dn Z[0]
2217    These typed registers can be used instead of the types specified after the
2218    Neon mnemonic, so long as all operands given have types. Types can also be
2219    specified directly, e.g.:
2220      vadd d0.s32, d1.s32, d2.s32  */
2221
2222 static bfd_boolean
2223 create_neon_reg_alias (char *newname, char *p)
2224 {
2225   enum arm_reg_type basetype;
2226   struct reg_entry *basereg;
2227   struct reg_entry mybasereg;
2228   struct neon_type ntype;
2229   struct neon_typed_alias typeinfo;
2230   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2231   int namelen;
2232
2233   typeinfo.defined = 0;
2234   typeinfo.eltype.type = NT_invtype;
2235   typeinfo.eltype.size = -1;
2236   typeinfo.index = -1;
2237
2238   nameend = p;
2239
2240   if (strncmp (p, " .dn ", 5) == 0)
2241     basetype = REG_TYPE_VFD;
2242   else if (strncmp (p, " .qn ", 5) == 0)
2243     basetype = REG_TYPE_NQ;
2244   else
2245     return FALSE;
2246
2247   p += 5;
2248
2249   if (*p == '\0')
2250     return FALSE;
2251
2252   basereg = arm_reg_parse_multi (&p);
2253
2254   if (basereg && basereg->type != basetype)
2255     {
2256       as_bad (_("bad type for register"));
2257       return FALSE;
2258     }
2259
2260   if (basereg == NULL)
2261     {
2262       expressionS exp;
2263       /* Try parsing as an integer.  */
2264       my_get_expression (&exp, &p, GE_NO_PREFIX);
2265       if (exp.X_op != O_constant)
2266         {
2267           as_bad (_("expression must be constant"));
2268           return FALSE;
2269         }
2270       basereg = &mybasereg;
2271       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2272                                                   : exp.X_add_number;
2273       basereg->neon = 0;
2274     }
2275
2276   if (basereg->neon)
2277     typeinfo = *basereg->neon;
2278
2279   if (parse_neon_type (&ntype, &p) == SUCCESS)
2280     {
2281       /* We got a type.  */
2282       if (typeinfo.defined & NTA_HASTYPE)
2283         {
2284           as_bad (_("can't redefine the type of a register alias"));
2285           return FALSE;
2286         }
2287
2288       typeinfo.defined |= NTA_HASTYPE;
2289       if (ntype.elems != 1)
2290         {
2291           as_bad (_("you must specify a single type only"));
2292           return FALSE;
2293         }
2294       typeinfo.eltype = ntype.el[0];
2295     }
2296
2297   if (skip_past_char (&p, '[') == SUCCESS)
2298     {
2299       expressionS exp;
2300       /* We got a scalar index.  */
2301
2302       if (typeinfo.defined & NTA_HASINDEX)
2303         {
2304           as_bad (_("can't redefine the index of a scalar alias"));
2305           return FALSE;
2306         }
2307
2308       my_get_expression (&exp, &p, GE_NO_PREFIX);
2309
2310       if (exp.X_op != O_constant)
2311         {
2312           as_bad (_("scalar index must be constant"));
2313           return FALSE;
2314         }
2315
2316       typeinfo.defined |= NTA_HASINDEX;
2317       typeinfo.index = exp.X_add_number;
2318
2319       if (skip_past_char (&p, ']') == FAIL)
2320         {
2321           as_bad (_("expecting ]"));
2322           return FALSE;
2323         }
2324     }
2325
2326   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2327      the desired alias name, and p points to its end.  If not, then
2328      the desired alias name is in the global original_case_string.  */
2329 #ifdef TC_CASE_SENSITIVE
2330   namelen = nameend - newname;
2331 #else
2332   newname = original_case_string;
2333   namelen = strlen (newname);
2334 #endif
2335
2336   namebuf = (char *) alloca (namelen + 1);
2337   strncpy (namebuf, newname, namelen);
2338   namebuf[namelen] = '\0';
2339
2340   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2341                          typeinfo.defined != 0 ? &typeinfo : NULL);
2342
2343   /* Insert name in all uppercase.  */
2344   for (p = namebuf; *p; p++)
2345     *p = TOUPPER (*p);
2346
2347   if (strncmp (namebuf, newname, namelen))
2348     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2349                            typeinfo.defined != 0 ? &typeinfo : NULL);
2350
2351   /* Insert name in all lowercase.  */
2352   for (p = namebuf; *p; p++)
2353     *p = TOLOWER (*p);
2354
2355   if (strncmp (namebuf, newname, namelen))
2356     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2357                            typeinfo.defined != 0 ? &typeinfo : NULL);
2358
2359   return TRUE;
2360 }
2361
2362 /* Should never be called, as .req goes between the alias and the
2363    register name, not at the beginning of the line.  */
2364
2365 static void
2366 s_req (int a ATTRIBUTE_UNUSED)
2367 {
2368   as_bad (_("invalid syntax for .req directive"));
2369 }
2370
2371 static void
2372 s_dn (int a ATTRIBUTE_UNUSED)
2373 {
2374   as_bad (_("invalid syntax for .dn directive"));
2375 }
2376
2377 static void
2378 s_qn (int a ATTRIBUTE_UNUSED)
2379 {
2380   as_bad (_("invalid syntax for .qn directive"));
2381 }
2382
2383 /* The .unreq directive deletes an alias which was previously defined
2384    by .req.  For example:
2385
2386        my_alias .req r11
2387        .unreq my_alias    */
2388
2389 static void
2390 s_unreq (int a ATTRIBUTE_UNUSED)
2391 {
2392   char * name;
2393   char saved_char;
2394
2395   name = input_line_pointer;
2396
2397   while (*input_line_pointer != 0
2398          && *input_line_pointer != ' '
2399          && *input_line_pointer != '\n')
2400     ++input_line_pointer;
2401
2402   saved_char = *input_line_pointer;
2403   *input_line_pointer = 0;
2404
2405   if (!*name)
2406     as_bad (_("invalid syntax for .unreq directive"));
2407   else
2408     {
2409       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2410                                                               name);
2411
2412       if (!reg)
2413         as_bad (_("unknown register alias '%s'"), name);
2414       else if (reg->builtin)
2415         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2416                  name);
2417       else
2418         {
2419           char * p;
2420           char * nbuf;
2421
2422           hash_delete (arm_reg_hsh, name, FALSE);
2423           free ((char *) reg->name);
2424           if (reg->neon)
2425             free (reg->neon);
2426           free (reg);
2427
2428           /* Also locate the all upper case and all lower case versions.
2429              Do not complain if we cannot find one or the other as it
2430              was probably deleted above.  */
2431
2432           nbuf = strdup (name);
2433           for (p = nbuf; *p; p++)
2434             *p = TOUPPER (*p);
2435           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2436           if (reg)
2437             {
2438               hash_delete (arm_reg_hsh, nbuf, FALSE);
2439               free ((char *) reg->name);
2440               if (reg->neon)
2441                 free (reg->neon);
2442               free (reg);
2443             }
2444
2445           for (p = nbuf; *p; p++)
2446             *p = TOLOWER (*p);
2447           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2448           if (reg)
2449             {
2450               hash_delete (arm_reg_hsh, nbuf, FALSE);
2451               free ((char *) reg->name);
2452               if (reg->neon)
2453                 free (reg->neon);
2454               free (reg);
2455             }
2456
2457           free (nbuf);
2458         }
2459     }
2460
2461   *input_line_pointer = saved_char;
2462   demand_empty_rest_of_line ();
2463 }
2464
2465 /* Directives: Instruction set selection.  */
2466
2467 #ifdef OBJ_ELF
2468 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2469    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2470    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2471    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2472
2473 /* Create a new mapping symbol for the transition to STATE.  */
2474
2475 static void
2476 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2477 {
2478   symbolS * symbolP;
2479   const char * symname;
2480   int type;
2481
2482   switch (state)
2483     {
2484     case MAP_DATA:
2485       symname = "$d";
2486       type = BSF_NO_FLAGS;
2487       break;
2488     case MAP_ARM:
2489       symname = "$a";
2490       type = BSF_NO_FLAGS;
2491       break;
2492     case MAP_THUMB:
2493       symname = "$t";
2494       type = BSF_NO_FLAGS;
2495       break;
2496     default:
2497       abort ();
2498     }
2499
2500   symbolP = symbol_new (symname, now_seg, value, frag);
2501   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2502
2503   switch (state)
2504     {
2505     case MAP_ARM:
2506       THUMB_SET_FUNC (symbolP, 0);
2507       ARM_SET_THUMB (symbolP, 0);
2508       ARM_SET_INTERWORK (symbolP, support_interwork);
2509       break;
2510
2511     case MAP_THUMB:
2512       THUMB_SET_FUNC (symbolP, 1);
2513       ARM_SET_THUMB (symbolP, 1);
2514       ARM_SET_INTERWORK (symbolP, support_interwork);
2515       break;
2516
2517     case MAP_DATA:
2518     default:
2519       break;
2520     }
2521
2522   /* Save the mapping symbols for future reference.  Also check that
2523      we do not place two mapping symbols at the same offset within a
2524      frag.  We'll handle overlap between frags in
2525      check_mapping_symbols.
2526
2527      If .fill or other data filling directive generates zero sized data,
2528      the mapping symbol for the following code will have the same value
2529      as the one generated for the data filling directive.  In this case,
2530      we replace the old symbol with the new one at the same address.  */
2531   if (value == 0)
2532     {
2533       if (frag->tc_frag_data.first_map != NULL)
2534         {
2535           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2536           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2537         }
2538       frag->tc_frag_data.first_map = symbolP;
2539     }
2540   if (frag->tc_frag_data.last_map != NULL)
2541     {
2542       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2543       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2544         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2545     }
2546   frag->tc_frag_data.last_map = symbolP;
2547 }
2548
2549 /* We must sometimes convert a region marked as code to data during
2550    code alignment, if an odd number of bytes have to be padded.  The
2551    code mapping symbol is pushed to an aligned address.  */
2552
2553 static void
2554 insert_data_mapping_symbol (enum mstate state,
2555                             valueT value, fragS *frag, offsetT bytes)
2556 {
2557   /* If there was already a mapping symbol, remove it.  */
2558   if (frag->tc_frag_data.last_map != NULL
2559       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2560     {
2561       symbolS *symp = frag->tc_frag_data.last_map;
2562
2563       if (value == 0)
2564         {
2565           know (frag->tc_frag_data.first_map == symp);
2566           frag->tc_frag_data.first_map = NULL;
2567         }
2568       frag->tc_frag_data.last_map = NULL;
2569       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2570     }
2571
2572   make_mapping_symbol (MAP_DATA, value, frag);
2573   make_mapping_symbol (state, value + bytes, frag);
2574 }
2575
2576 static void mapping_state_2 (enum mstate state, int max_chars);
2577
2578 /* Set the mapping state to STATE.  Only call this when about to
2579    emit some STATE bytes to the file.  */
2580
2581 void
2582 mapping_state (enum mstate state)
2583 {
2584   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2585
2586 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2587
2588   if (mapstate == state)
2589     /* The mapping symbol has already been emitted.
2590        There is nothing else to do.  */
2591     return;
2592   else if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2593     /* This case will be evaluated later in the next else.  */
2594     return;
2595   else if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2596           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2597     {
2598       /* Only add the symbol if the offset is > 0:
2599          if we're at the first frag, check it's size > 0;
2600          if we're not at the first frag, then for sure
2601             the offset is > 0.  */
2602       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2603       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2604
2605       if (add_symbol)
2606         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2607     }
2608
2609   mapping_state_2 (state, 0);
2610 #undef TRANSITION
2611 }
2612
2613 /* Same as mapping_state, but MAX_CHARS bytes have already been
2614    allocated.  Put the mapping symbol that far back.  */
2615
2616 static void
2617 mapping_state_2 (enum mstate state, int max_chars)
2618 {
2619   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2620
2621   if (!SEG_NORMAL (now_seg))
2622     return;
2623
2624   if (mapstate == state)
2625     /* The mapping symbol has already been emitted.
2626        There is nothing else to do.  */
2627     return;
2628
2629   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2630   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2631 }
2632 #else
2633 #define mapping_state(x) ((void)0)
2634 #define mapping_state_2(x, y) ((void)0)
2635 #endif
2636
2637 /* Find the real, Thumb encoded start of a Thumb function.  */
2638
2639 #ifdef OBJ_COFF
2640 static symbolS *
2641 find_real_start (symbolS * symbolP)
2642 {
2643   char *       real_start;
2644   const char * name = S_GET_NAME (symbolP);
2645   symbolS *    new_target;
2646
2647   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2648 #define STUB_NAME ".real_start_of"
2649
2650   if (name == NULL)
2651     abort ();
2652
2653   /* The compiler may generate BL instructions to local labels because
2654      it needs to perform a branch to a far away location. These labels
2655      do not have a corresponding ".real_start_of" label.  We check
2656      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2657      the ".real_start_of" convention for nonlocal branches.  */
2658   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2659     return symbolP;
2660
2661   real_start = ACONCAT ((STUB_NAME, name, NULL));
2662   new_target = symbol_find (real_start);
2663
2664   if (new_target == NULL)
2665     {
2666       as_warn (_("Failed to find real start of function: %s\n"), name);
2667       new_target = symbolP;
2668     }
2669
2670   return new_target;
2671 }
2672 #endif
2673
2674 static void
2675 opcode_select (int width)
2676 {
2677   switch (width)
2678     {
2679     case 16:
2680       if (! thumb_mode)
2681         {
2682           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2683             as_bad (_("selected processor does not support THUMB opcodes"));
2684
2685           thumb_mode = 1;
2686           /* No need to force the alignment, since we will have been
2687              coming from ARM mode, which is word-aligned.  */
2688           record_alignment (now_seg, 1);
2689         }
2690       break;
2691
2692     case 32:
2693       if (thumb_mode)
2694         {
2695           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2696             as_bad (_("selected processor does not support ARM opcodes"));
2697
2698           thumb_mode = 0;
2699
2700           if (!need_pass_2)
2701             frag_align (2, 0, 0);
2702
2703           record_alignment (now_seg, 1);
2704         }
2705       break;
2706
2707     default:
2708       as_bad (_("invalid instruction size selected (%d)"), width);
2709     }
2710 }
2711
2712 static void
2713 s_arm (int ignore ATTRIBUTE_UNUSED)
2714 {
2715   opcode_select (32);
2716   demand_empty_rest_of_line ();
2717 }
2718
2719 static void
2720 s_thumb (int ignore ATTRIBUTE_UNUSED)
2721 {
2722   opcode_select (16);
2723   demand_empty_rest_of_line ();
2724 }
2725
2726 static void
2727 s_code (int unused ATTRIBUTE_UNUSED)
2728 {
2729   int temp;
2730
2731   temp = get_absolute_expression ();
2732   switch (temp)
2733     {
2734     case 16:
2735     case 32:
2736       opcode_select (temp);
2737       break;
2738
2739     default:
2740       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2741     }
2742 }
2743
2744 static void
2745 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2746 {
2747   /* If we are not already in thumb mode go into it, EVEN if
2748      the target processor does not support thumb instructions.
2749      This is used by gcc/config/arm/lib1funcs.asm for example
2750      to compile interworking support functions even if the
2751      target processor should not support interworking.  */
2752   if (! thumb_mode)
2753     {
2754       thumb_mode = 2;
2755       record_alignment (now_seg, 1);
2756     }
2757
2758   demand_empty_rest_of_line ();
2759 }
2760
2761 static void
2762 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2763 {
2764   s_thumb (0);
2765
2766   /* The following label is the name/address of the start of a Thumb function.
2767      We need to know this for the interworking support.  */
2768   label_is_thumb_function_name = TRUE;
2769 }
2770
2771 /* Perform a .set directive, but also mark the alias as
2772    being a thumb function.  */
2773
2774 static void
2775 s_thumb_set (int equiv)
2776 {
2777   /* XXX the following is a duplicate of the code for s_set() in read.c
2778      We cannot just call that code as we need to get at the symbol that
2779      is created.  */
2780   char *    name;
2781   char      delim;
2782   char *    end_name;
2783   symbolS * symbolP;
2784
2785   /* Especial apologies for the random logic:
2786      This just grew, and could be parsed much more simply!
2787      Dean - in haste.  */
2788   name      = input_line_pointer;
2789   delim     = get_symbol_end ();
2790   end_name  = input_line_pointer;
2791   *end_name = delim;
2792
2793   if (*input_line_pointer != ',')
2794     {
2795       *end_name = 0;
2796       as_bad (_("expected comma after name \"%s\""), name);
2797       *end_name = delim;
2798       ignore_rest_of_line ();
2799       return;
2800     }
2801
2802   input_line_pointer++;
2803   *end_name = 0;
2804
2805   if (name[0] == '.' && name[1] == '\0')
2806     {
2807       /* XXX - this should not happen to .thumb_set.  */
2808       abort ();
2809     }
2810
2811   if ((symbolP = symbol_find (name)) == NULL
2812       && (symbolP = md_undefined_symbol (name)) == NULL)
2813     {
2814 #ifndef NO_LISTING
2815       /* When doing symbol listings, play games with dummy fragments living
2816          outside the normal fragment chain to record the file and line info
2817          for this symbol.  */
2818       if (listing & LISTING_SYMBOLS)
2819         {
2820           extern struct list_info_struct * listing_tail;
2821           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2822
2823           memset (dummy_frag, 0, sizeof (fragS));
2824           dummy_frag->fr_type = rs_fill;
2825           dummy_frag->line = listing_tail;
2826           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2827           dummy_frag->fr_symbol = symbolP;
2828         }
2829       else
2830 #endif
2831         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2832
2833 #ifdef OBJ_COFF
2834       /* "set" symbols are local unless otherwise specified.  */
2835       SF_SET_LOCAL (symbolP);
2836 #endif /* OBJ_COFF  */
2837     }                           /* Make a new symbol.  */
2838
2839   symbol_table_insert (symbolP);
2840
2841   * end_name = delim;
2842
2843   if (equiv
2844       && S_IS_DEFINED (symbolP)
2845       && S_GET_SEGMENT (symbolP) != reg_section)
2846     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2847
2848   pseudo_set (symbolP);
2849
2850   demand_empty_rest_of_line ();
2851
2852   /* XXX Now we come to the Thumb specific bit of code.  */
2853
2854   THUMB_SET_FUNC (symbolP, 1);
2855   ARM_SET_THUMB (symbolP, 1);
2856 #if defined OBJ_ELF || defined OBJ_COFF
2857   ARM_SET_INTERWORK (symbolP, support_interwork);
2858 #endif
2859 }
2860
2861 /* Directives: Mode selection.  */
2862
2863 /* .syntax [unified|divided] - choose the new unified syntax
2864    (same for Arm and Thumb encoding, modulo slight differences in what
2865    can be represented) or the old divergent syntax for each mode.  */
2866 static void
2867 s_syntax (int unused ATTRIBUTE_UNUSED)
2868 {
2869   char *name, delim;
2870
2871   name = input_line_pointer;
2872   delim = get_symbol_end ();
2873
2874   if (!strcasecmp (name, "unified"))
2875     unified_syntax = TRUE;
2876   else if (!strcasecmp (name, "divided"))
2877     unified_syntax = FALSE;
2878   else
2879     {
2880       as_bad (_("unrecognized syntax mode \"%s\""), name);
2881       return;
2882     }
2883   *input_line_pointer = delim;
2884   demand_empty_rest_of_line ();
2885 }
2886
2887 /* Directives: sectioning and alignment.  */
2888
2889 /* Same as s_align_ptwo but align 0 => align 2.  */
2890
2891 static void
2892 s_align (int unused ATTRIBUTE_UNUSED)
2893 {
2894   int temp;
2895   bfd_boolean fill_p;
2896   long temp_fill;
2897   long max_alignment = 15;
2898
2899   temp = get_absolute_expression ();
2900   if (temp > max_alignment)
2901     as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
2902   else if (temp < 0)
2903     {
2904       as_bad (_("alignment negative. 0 assumed."));
2905       temp = 0;
2906     }
2907
2908   if (*input_line_pointer == ',')
2909     {
2910       input_line_pointer++;
2911       temp_fill = get_absolute_expression ();
2912       fill_p = TRUE;
2913     }
2914   else
2915     {
2916       fill_p = FALSE;
2917       temp_fill = 0;
2918     }
2919
2920   if (!temp)
2921     temp = 2;
2922
2923   /* Only make a frag if we HAVE to.  */
2924   if (temp && !need_pass_2)
2925     {
2926       if (!fill_p && subseg_text_p (now_seg))
2927         frag_align_code (temp, 0);
2928       else
2929         frag_align (temp, (int) temp_fill, 0);
2930     }
2931   demand_empty_rest_of_line ();
2932
2933   record_alignment (now_seg, temp);
2934 }
2935
2936 static void
2937 s_bss (int ignore ATTRIBUTE_UNUSED)
2938 {
2939   /* We don't support putting frags in the BSS segment, we fake it by
2940      marking in_bss, then looking at s_skip for clues.  */
2941   subseg_set (bss_section, 0);
2942   demand_empty_rest_of_line ();
2943
2944 #ifdef md_elf_section_change_hook
2945   md_elf_section_change_hook ();
2946 #endif
2947 }
2948
2949 static void
2950 s_even (int ignore ATTRIBUTE_UNUSED)
2951 {
2952   /* Never make frag if expect extra pass.  */
2953   if (!need_pass_2)
2954     frag_align (1, 0, 0);
2955
2956   record_alignment (now_seg, 1);
2957
2958   demand_empty_rest_of_line ();
2959 }
2960
2961 /* Directives: Literal pools.  */
2962
2963 static literal_pool *
2964 find_literal_pool (void)
2965 {
2966   literal_pool * pool;
2967
2968   for (pool = list_of_pools; pool != NULL; pool = pool->next)
2969     {
2970       if (pool->section == now_seg
2971           && pool->sub_section == now_subseg)
2972         break;
2973     }
2974
2975   return pool;
2976 }
2977
2978 static literal_pool *
2979 find_or_make_literal_pool (void)
2980 {
2981   /* Next literal pool ID number.  */
2982   static unsigned int latest_pool_num = 1;
2983   literal_pool *      pool;
2984
2985   pool = find_literal_pool ();
2986
2987   if (pool == NULL)
2988     {
2989       /* Create a new pool.  */
2990       pool = (literal_pool *) xmalloc (sizeof (* pool));
2991       if (! pool)
2992         return NULL;
2993
2994       pool->next_free_entry = 0;
2995       pool->section         = now_seg;
2996       pool->sub_section     = now_subseg;
2997       pool->next            = list_of_pools;
2998       pool->symbol          = NULL;
2999
3000       /* Add it to the list.  */
3001       list_of_pools = pool;
3002     }
3003
3004   /* New pools, and emptied pools, will have a NULL symbol.  */
3005   if (pool->symbol == NULL)
3006     {
3007       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3008                                     (valueT) 0, &zero_address_frag);
3009       pool->id = latest_pool_num ++;
3010     }
3011
3012   /* Done.  */
3013   return pool;
3014 }
3015
3016 /* Add the literal in the global 'inst'
3017    structure to the relevant literal pool.  */
3018
3019 static int
3020 add_to_lit_pool (void)
3021 {
3022   literal_pool * pool;
3023   unsigned int entry;
3024
3025   pool = find_or_make_literal_pool ();
3026
3027   /* Check if this literal value is already in the pool.  */
3028   for (entry = 0; entry < pool->next_free_entry; entry ++)
3029     {
3030       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3031           && (inst.reloc.exp.X_op == O_constant)
3032           && (pool->literals[entry].X_add_number
3033               == inst.reloc.exp.X_add_number)
3034           && (pool->literals[entry].X_unsigned
3035               == inst.reloc.exp.X_unsigned))
3036         break;
3037
3038       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3039           && (inst.reloc.exp.X_op == O_symbol)
3040           && (pool->literals[entry].X_add_number
3041               == inst.reloc.exp.X_add_number)
3042           && (pool->literals[entry].X_add_symbol
3043               == inst.reloc.exp.X_add_symbol)
3044           && (pool->literals[entry].X_op_symbol
3045               == inst.reloc.exp.X_op_symbol))
3046         break;
3047     }
3048
3049   /* Do we need to create a new entry?  */
3050   if (entry == pool->next_free_entry)
3051     {
3052       if (entry >= MAX_LITERAL_POOL_SIZE)
3053         {
3054           inst.error = _("literal pool overflow");
3055           return FAIL;
3056         }
3057
3058       pool->literals[entry] = inst.reloc.exp;
3059       pool->next_free_entry += 1;
3060     }
3061
3062   inst.reloc.exp.X_op         = O_symbol;
3063   inst.reloc.exp.X_add_number = ((int) entry) * 4;
3064   inst.reloc.exp.X_add_symbol = pool->symbol;
3065
3066   return SUCCESS;
3067 }
3068
3069 /* Can't use symbol_new here, so have to create a symbol and then at
3070    a later date assign it a value. Thats what these functions do.  */
3071
3072 static void
3073 symbol_locate (symbolS *    symbolP,
3074                const char * name,       /* It is copied, the caller can modify.  */
3075                segT         segment,    /* Segment identifier (SEG_<something>).  */
3076                valueT       valu,       /* Symbol value.  */
3077                fragS *      frag)       /* Associated fragment.  */
3078 {
3079   unsigned int name_length;
3080   char * preserved_copy_of_name;
3081
3082   name_length = strlen (name) + 1;   /* +1 for \0.  */
3083   obstack_grow (&notes, name, name_length);
3084   preserved_copy_of_name = (char *) obstack_finish (&notes);
3085
3086 #ifdef tc_canonicalize_symbol_name
3087   preserved_copy_of_name =
3088     tc_canonicalize_symbol_name (preserved_copy_of_name);
3089 #endif
3090
3091   S_SET_NAME (symbolP, preserved_copy_of_name);
3092
3093   S_SET_SEGMENT (symbolP, segment);
3094   S_SET_VALUE (symbolP, valu);
3095   symbol_clear_list_pointers (symbolP);
3096
3097   symbol_set_frag (symbolP, frag);
3098
3099   /* Link to end of symbol chain.  */
3100   {
3101     extern int symbol_table_frozen;
3102
3103     if (symbol_table_frozen)
3104       abort ();
3105   }
3106
3107   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3108
3109   obj_symbol_new_hook (symbolP);
3110
3111 #ifdef tc_symbol_new_hook
3112   tc_symbol_new_hook (symbolP);
3113 #endif
3114
3115 #ifdef DEBUG_SYMS
3116   verify_symbol_chain (symbol_rootP, symbol_lastP);
3117 #endif /* DEBUG_SYMS  */
3118 }
3119
3120
3121 static void
3122 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3123 {
3124   unsigned int entry;
3125   literal_pool * pool;
3126   char sym_name[20];
3127
3128   pool = find_literal_pool ();
3129   if (pool == NULL
3130       || pool->symbol == NULL
3131       || pool->next_free_entry == 0)
3132     return;
3133
3134   mapping_state (MAP_DATA);
3135
3136   /* Align pool as you have word accesses.
3137      Only make a frag if we have to.  */
3138   if (!need_pass_2)
3139     frag_align (2, 0, 0);
3140
3141   record_alignment (now_seg, 2);
3142
3143   sprintf (sym_name, "$$lit_\002%x", pool->id);
3144
3145   symbol_locate (pool->symbol, sym_name, now_seg,
3146                  (valueT) frag_now_fix (), frag_now);
3147   symbol_table_insert (pool->symbol);
3148
3149   ARM_SET_THUMB (pool->symbol, thumb_mode);
3150
3151 #if defined OBJ_COFF || defined OBJ_ELF
3152   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3153 #endif
3154
3155   for (entry = 0; entry < pool->next_free_entry; entry ++)
3156     /* First output the expression in the instruction to the pool.  */
3157     emit_expr (&(pool->literals[entry]), 4); /* .word  */
3158
3159   /* Mark the pool as empty.  */
3160   pool->next_free_entry = 0;
3161   pool->symbol = NULL;
3162 }
3163
3164 #ifdef OBJ_ELF
3165 /* Forward declarations for functions below, in the MD interface
3166    section.  */
3167 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3168 static valueT create_unwind_entry (int);
3169 static void start_unwind_section (const segT, int);
3170 static void add_unwind_opcode (valueT, int);
3171 static void flush_pending_unwind (void);
3172
3173 /* Directives: Data.  */
3174
3175 static void
3176 s_arm_elf_cons (int nbytes)
3177 {
3178   expressionS exp;
3179
3180 #ifdef md_flush_pending_output
3181   md_flush_pending_output ();
3182 #endif
3183
3184   if (is_it_end_of_statement ())
3185     {
3186       demand_empty_rest_of_line ();
3187       return;
3188     }
3189
3190 #ifdef md_cons_align
3191   md_cons_align (nbytes);
3192 #endif
3193
3194   mapping_state (MAP_DATA);
3195   do
3196     {
3197       int reloc;
3198       char *base = input_line_pointer;
3199
3200       expression (& exp);
3201
3202       if (exp.X_op != O_symbol)
3203         emit_expr (&exp, (unsigned int) nbytes);
3204       else
3205         {
3206           char *before_reloc = input_line_pointer;
3207           reloc = parse_reloc (&input_line_pointer);
3208           if (reloc == -1)
3209             {
3210               as_bad (_("unrecognized relocation suffix"));
3211               ignore_rest_of_line ();
3212               return;
3213             }
3214           else if (reloc == BFD_RELOC_UNUSED)
3215             emit_expr (&exp, (unsigned int) nbytes);
3216           else
3217             {
3218               reloc_howto_type *howto = (reloc_howto_type *)
3219                   bfd_reloc_type_lookup (stdoutput,
3220                                          (bfd_reloc_code_real_type) reloc);
3221               int size = bfd_get_reloc_size (howto);
3222
3223               if (reloc == BFD_RELOC_ARM_PLT32)
3224                 {
3225                   as_bad (_("(plt) is only valid on branch targets"));
3226                   reloc = BFD_RELOC_UNUSED;
3227                   size = 0;
3228                 }
3229
3230               if (size > nbytes)
3231                 as_bad (_("%s relocations do not fit in %d bytes"),
3232                         howto->name, nbytes);
3233               else
3234                 {
3235                   /* We've parsed an expression stopping at O_symbol.
3236                      But there may be more expression left now that we
3237                      have parsed the relocation marker.  Parse it again.
3238                      XXX Surely there is a cleaner way to do this.  */
3239                   char *p = input_line_pointer;
3240                   int offset;
3241                   char *save_buf = (char *) alloca (input_line_pointer - base);
3242                   memcpy (save_buf, base, input_line_pointer - base);
3243                   memmove (base + (input_line_pointer - before_reloc),
3244                            base, before_reloc - base);
3245
3246                   input_line_pointer = base + (input_line_pointer-before_reloc);
3247                   expression (&exp);
3248                   memcpy (base, save_buf, p - base);
3249
3250                   offset = nbytes - size;
3251                   p = frag_more ((int) nbytes);
3252                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3253                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3254                 }
3255             }
3256         }
3257     }
3258   while (*input_line_pointer++ == ',');
3259
3260   /* Put terminator back into stream.  */
3261   input_line_pointer --;
3262   demand_empty_rest_of_line ();
3263 }
3264
3265 /* Emit an expression containing a 32-bit thumb instruction.
3266    Implementation based on put_thumb32_insn.  */
3267
3268 static void
3269 emit_thumb32_expr (expressionS * exp)
3270 {
3271   expressionS exp_high = *exp;
3272
3273   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3274   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3275   exp->X_add_number &= 0xffff;
3276   emit_expr (exp, (unsigned int) THUMB_SIZE);
3277 }
3278
3279 /*  Guess the instruction size based on the opcode.  */
3280
3281 static int
3282 thumb_insn_size (int opcode)
3283 {
3284   if ((unsigned int) opcode < 0xe800u)
3285     return 2;
3286   else if ((unsigned int) opcode >= 0xe8000000u)
3287     return 4;
3288   else
3289     return 0;
3290 }
3291
3292 static bfd_boolean
3293 emit_insn (expressionS *exp, int nbytes)
3294 {
3295   int size = 0;
3296
3297   if (exp->X_op == O_constant)
3298     {
3299       size = nbytes;
3300
3301       if (size == 0)
3302         size = thumb_insn_size (exp->X_add_number);
3303
3304       if (size != 0)
3305         {
3306           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3307             {
3308               as_bad (_(".inst.n operand too big. "\
3309                         "Use .inst.w instead"));
3310               size = 0;
3311             }
3312           else
3313             {
3314               if (now_it.state == AUTOMATIC_IT_BLOCK)
3315                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3316               else
3317                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3318
3319               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3320                 emit_thumb32_expr (exp);
3321               else
3322                 emit_expr (exp, (unsigned int) size);
3323
3324               it_fsm_post_encode ();
3325             }
3326         }
3327       else
3328         as_bad (_("cannot determine Thumb instruction size. "   \
3329                   "Use .inst.n/.inst.w instead"));
3330     }
3331   else
3332     as_bad (_("constant expression required"));
3333
3334   return (size != 0);
3335 }
3336
3337 /* Like s_arm_elf_cons but do not use md_cons_align and
3338    set the mapping state to MAP_ARM/MAP_THUMB.  */
3339
3340 static void
3341 s_arm_elf_inst (int nbytes)
3342 {
3343   if (is_it_end_of_statement ())
3344     {
3345       demand_empty_rest_of_line ();
3346       return;
3347     }
3348
3349   /* Calling mapping_state () here will not change ARM/THUMB,
3350      but will ensure not to be in DATA state.  */
3351
3352   if (thumb_mode)
3353     mapping_state (MAP_THUMB);
3354   else
3355     {
3356       if (nbytes != 0)
3357         {
3358           as_bad (_("width suffixes are invalid in ARM mode"));
3359           ignore_rest_of_line ();
3360           return;
3361         }
3362
3363       nbytes = 4;
3364
3365       mapping_state (MAP_ARM);
3366     }
3367
3368   do
3369     {
3370       expressionS exp;
3371
3372       expression (& exp);
3373
3374       if (! emit_insn (& exp, nbytes))
3375         {
3376           ignore_rest_of_line ();
3377           return;
3378         }
3379     }
3380   while (*input_line_pointer++ == ',');
3381
3382   /* Put terminator back into stream.  */
3383   input_line_pointer --;
3384   demand_empty_rest_of_line ();
3385 }
3386
3387 /* Parse a .rel31 directive.  */
3388
3389 static void
3390 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3391 {
3392   expressionS exp;
3393   char *p;
3394   valueT highbit;
3395
3396   highbit = 0;
3397   if (*input_line_pointer == '1')
3398     highbit = 0x80000000;
3399   else if (*input_line_pointer != '0')
3400     as_bad (_("expected 0 or 1"));
3401
3402   input_line_pointer++;
3403   if (*input_line_pointer != ',')
3404     as_bad (_("missing comma"));
3405   input_line_pointer++;
3406
3407 #ifdef md_flush_pending_output
3408   md_flush_pending_output ();
3409 #endif
3410
3411 #ifdef md_cons_align
3412   md_cons_align (4);
3413 #endif
3414
3415   mapping_state (MAP_DATA);
3416
3417   expression (&exp);
3418
3419   p = frag_more (4);
3420   md_number_to_chars (p, highbit, 4);
3421   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3422                BFD_RELOC_ARM_PREL31);
3423
3424   demand_empty_rest_of_line ();
3425 }
3426
3427 /* Directives: AEABI stack-unwind tables.  */
3428
3429 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3430
3431 static void
3432 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3433 {
3434   demand_empty_rest_of_line ();
3435   if (unwind.proc_start)
3436     {
3437       as_bad (_("duplicate .fnstart directive"));
3438       return;
3439     }
3440
3441   /* Mark the start of the function.  */
3442   unwind.proc_start = expr_build_dot ();
3443
3444   /* Reset the rest of the unwind info.  */
3445   unwind.opcode_count = 0;
3446   unwind.table_entry = NULL;
3447   unwind.personality_routine = NULL;
3448   unwind.personality_index = -1;
3449   unwind.frame_size = 0;
3450   unwind.fp_offset = 0;
3451   unwind.fp_reg = REG_SP;
3452   unwind.fp_used = 0;
3453   unwind.sp_restored = 0;
3454 }
3455
3456
3457 /* Parse a handlerdata directive.  Creates the exception handling table entry
3458    for the function.  */
3459
3460 static void
3461 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3462 {
3463   demand_empty_rest_of_line ();
3464   if (!unwind.proc_start)
3465     as_bad (MISSING_FNSTART);
3466
3467   if (unwind.table_entry)
3468     as_bad (_("duplicate .handlerdata directive"));
3469
3470   create_unwind_entry (1);
3471 }
3472
3473 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3474
3475 static void
3476 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3477 {
3478   long where;
3479   char *ptr;
3480   valueT val;
3481   unsigned int marked_pr_dependency;
3482
3483   demand_empty_rest_of_line ();
3484
3485   if (!unwind.proc_start)
3486     {
3487       as_bad (_(".fnend directive without .fnstart"));
3488       return;
3489     }
3490
3491   /* Add eh table entry.  */
3492   if (unwind.table_entry == NULL)
3493     val = create_unwind_entry (0);
3494   else
3495     val = 0;
3496
3497   /* Add index table entry.  This is two words.  */
3498   start_unwind_section (unwind.saved_seg, 1);
3499   frag_align (2, 0, 0);
3500   record_alignment (now_seg, 2);
3501
3502   ptr = frag_more (8);
3503   where = frag_now_fix () - 8;
3504
3505   /* Self relative offset of the function start.  */
3506   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3507            BFD_RELOC_ARM_PREL31);
3508
3509   /* Indicate dependency on EHABI-defined personality routines to the
3510      linker, if it hasn't been done already.  */
3511   marked_pr_dependency
3512     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3513   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3514       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3515     {
3516       static const char *const name[] =
3517         {
3518           "__aeabi_unwind_cpp_pr0",
3519           "__aeabi_unwind_cpp_pr1",
3520           "__aeabi_unwind_cpp_pr2"
3521         };
3522       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3523       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3524       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3525         |= 1 << unwind.personality_index;
3526     }
3527
3528   if (val)
3529     /* Inline exception table entry.  */
3530     md_number_to_chars (ptr + 4, val, 4);
3531   else
3532     /* Self relative offset of the table entry.  */
3533     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3534              BFD_RELOC_ARM_PREL31);
3535
3536   /* Restore the original section.  */
3537   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3538
3539   unwind.proc_start = NULL;
3540 }
3541
3542
3543 /* Parse an unwind_cantunwind directive.  */
3544
3545 static void
3546 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3547 {
3548   demand_empty_rest_of_line ();
3549   if (!unwind.proc_start)
3550     as_bad (MISSING_FNSTART);
3551
3552   if (unwind.personality_routine || unwind.personality_index != -1)
3553     as_bad (_("personality routine specified for cantunwind frame"));
3554
3555   unwind.personality_index = -2;
3556 }
3557
3558
3559 /* Parse a personalityindex directive.  */
3560
3561 static void
3562 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3563 {
3564   expressionS exp;
3565
3566   if (!unwind.proc_start)
3567     as_bad (MISSING_FNSTART);
3568
3569   if (unwind.personality_routine || unwind.personality_index != -1)
3570     as_bad (_("duplicate .personalityindex directive"));
3571
3572   expression (&exp);
3573
3574   if (exp.X_op != O_constant
3575       || exp.X_add_number < 0 || exp.X_add_number > 15)
3576     {
3577       as_bad (_("bad personality routine number"));
3578       ignore_rest_of_line ();
3579       return;
3580     }
3581
3582   unwind.personality_index = exp.X_add_number;
3583
3584   demand_empty_rest_of_line ();
3585 }
3586
3587
3588 /* Parse a personality directive.  */
3589
3590 static void
3591 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3592 {
3593   char *name, *p, c;
3594
3595   if (!unwind.proc_start)
3596     as_bad (MISSING_FNSTART);
3597
3598   if (unwind.personality_routine || unwind.personality_index != -1)
3599     as_bad (_("duplicate .personality directive"));
3600
3601   name = input_line_pointer;
3602   c = get_symbol_end ();
3603   p = input_line_pointer;
3604   unwind.personality_routine = symbol_find_or_make (name);
3605   *p = c;
3606   demand_empty_rest_of_line ();
3607 }
3608
3609
3610 /* Parse a directive saving core registers.  */
3611
3612 static void
3613 s_arm_unwind_save_core (void)
3614 {
3615   valueT op;
3616   long range;
3617   int n;
3618
3619   range = parse_reg_list (&input_line_pointer);
3620   if (range == FAIL)
3621     {
3622       as_bad (_("expected register list"));
3623       ignore_rest_of_line ();
3624       return;
3625     }
3626
3627   demand_empty_rest_of_line ();
3628
3629   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3630      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3631      ip because it is clobbered by calls.  */
3632   if (unwind.sp_restored && unwind.fp_reg == 12
3633       && (range & 0x3000) == 0x1000)
3634     {
3635       unwind.opcode_count--;
3636       unwind.sp_restored = 0;
3637       range = (range | 0x2000) & ~0x1000;
3638       unwind.pending_offset = 0;
3639     }
3640
3641   /* Pop r4-r15.  */
3642   if (range & 0xfff0)
3643     {
3644       /* See if we can use the short opcodes.  These pop a block of up to 8
3645          registers starting with r4, plus maybe r14.  */
3646       for (n = 0; n < 8; n++)
3647         {
3648           /* Break at the first non-saved register.      */
3649           if ((range & (1 << (n + 4))) == 0)
3650             break;
3651         }
3652       /* See if there are any other bits set.  */
3653       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
3654         {
3655           /* Use the long form.  */
3656           op = 0x8000 | ((range >> 4) & 0xfff);
3657           add_unwind_opcode (op, 2);
3658         }
3659       else
3660         {
3661           /* Use the short form.  */
3662           if (range & 0x4000)
3663             op = 0xa8; /* Pop r14.      */
3664           else
3665             op = 0xa0; /* Do not pop r14.  */
3666           op |= (n - 1);
3667           add_unwind_opcode (op, 1);
3668         }
3669     }
3670
3671   /* Pop r0-r3.  */
3672   if (range & 0xf)
3673     {
3674       op = 0xb100 | (range & 0xf);
3675       add_unwind_opcode (op, 2);
3676     }
3677
3678   /* Record the number of bytes pushed.  */
3679   for (n = 0; n < 16; n++)
3680     {
3681       if (range & (1 << n))
3682         unwind.frame_size += 4;
3683     }
3684 }
3685
3686
3687 /* Parse a directive saving FPA registers.  */
3688
3689 static void
3690 s_arm_unwind_save_fpa (int reg)
3691 {
3692   expressionS exp;
3693   int num_regs;
3694   valueT op;
3695
3696   /* Get Number of registers to transfer.  */
3697   if (skip_past_comma (&input_line_pointer) != FAIL)
3698     expression (&exp);
3699   else
3700     exp.X_op = O_illegal;
3701
3702   if (exp.X_op != O_constant)
3703     {
3704       as_bad (_("expected , <constant>"));
3705       ignore_rest_of_line ();
3706       return;
3707     }
3708
3709   num_regs = exp.X_add_number;
3710
3711   if (num_regs < 1 || num_regs > 4)
3712     {
3713       as_bad (_("number of registers must be in the range [1:4]"));
3714       ignore_rest_of_line ();
3715       return;
3716     }
3717
3718   demand_empty_rest_of_line ();
3719
3720   if (reg == 4)
3721     {
3722       /* Short form.  */
3723       op = 0xb4 | (num_regs - 1);
3724       add_unwind_opcode (op, 1);
3725     }
3726   else
3727     {
3728       /* Long form.  */
3729       op = 0xc800 | (reg << 4) | (num_regs - 1);
3730       add_unwind_opcode (op, 2);
3731     }
3732   unwind.frame_size += num_regs * 12;
3733 }
3734
3735
3736 /* Parse a directive saving VFP registers for ARMv6 and above.  */
3737
3738 static void
3739 s_arm_unwind_save_vfp_armv6 (void)
3740 {
3741   int count;
3742   unsigned int start;
3743   valueT op;
3744   int num_vfpv3_regs = 0;
3745   int num_regs_below_16;
3746
3747   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
3748   if (count == FAIL)
3749     {
3750       as_bad (_("expected register list"));
3751       ignore_rest_of_line ();
3752       return;
3753     }
3754
3755   demand_empty_rest_of_line ();
3756
3757   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
3758      than FSTMX/FLDMX-style ones).  */
3759
3760   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
3761   if (start >= 16)
3762     num_vfpv3_regs = count;
3763   else if (start + count > 16)
3764     num_vfpv3_regs = start + count - 16;
3765
3766   if (num_vfpv3_regs > 0)
3767     {
3768       int start_offset = start > 16 ? start - 16 : 0;
3769       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
3770       add_unwind_opcode (op, 2);
3771     }
3772
3773   /* Generate opcode for registers numbered in the range 0 .. 15.  */
3774   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
3775   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
3776   if (num_regs_below_16 > 0)
3777     {
3778       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
3779       add_unwind_opcode (op, 2);
3780     }
3781
3782   unwind.frame_size += count * 8;
3783 }
3784
3785
3786 /* Parse a directive saving VFP registers for pre-ARMv6.  */
3787
3788 static void
3789 s_arm_unwind_save_vfp (void)
3790 {
3791   int count;
3792   unsigned int reg;
3793   valueT op;
3794
3795   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
3796   if (count == FAIL)
3797     {
3798       as_bad (_("expected register list"));
3799       ignore_rest_of_line ();
3800       return;
3801     }
3802
3803   demand_empty_rest_of_line ();
3804
3805   if (reg == 8)
3806     {
3807       /* Short form.  */
3808       op = 0xb8 | (count - 1);
3809       add_unwind_opcode (op, 1);
3810     }
3811   else
3812     {
3813       /* Long form.  */
3814       op = 0xb300 | (reg << 4) | (count - 1);
3815       add_unwind_opcode (op, 2);
3816     }
3817   unwind.frame_size += count * 8 + 4;
3818 }
3819
3820
3821 /* Parse a directive saving iWMMXt data registers.  */
3822
3823 static void
3824 s_arm_unwind_save_mmxwr (void)
3825 {
3826   int reg;
3827   int hi_reg;
3828   int i;
3829   unsigned mask = 0;
3830   valueT op;
3831
3832   if (*input_line_pointer == '{')
3833     input_line_pointer++;
3834
3835   do
3836     {
3837       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3838
3839       if (reg == FAIL)
3840         {
3841           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
3842           goto error;
3843         }
3844
3845       if (mask >> reg)
3846         as_tsktsk (_("register list not in ascending order"));
3847       mask |= 1 << reg;
3848
3849       if (*input_line_pointer == '-')
3850         {
3851           input_line_pointer++;
3852           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3853           if (hi_reg == FAIL)
3854             {
3855               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
3856               goto error;
3857             }
3858           else if (reg >= hi_reg)
3859             {
3860               as_bad (_("bad register range"));
3861               goto error;
3862             }
3863           for (; reg < hi_reg; reg++)
3864             mask |= 1 << reg;
3865         }
3866     }
3867   while (skip_past_comma (&input_line_pointer) != FAIL);
3868
3869   if (*input_line_pointer == '}')
3870     input_line_pointer++;
3871
3872   demand_empty_rest_of_line ();
3873
3874   /* Generate any deferred opcodes because we're going to be looking at
3875      the list.  */
3876   flush_pending_unwind ();
3877
3878   for (i = 0; i < 16; i++)
3879     {
3880       if (mask & (1 << i))
3881         unwind.frame_size += 8;
3882     }
3883
3884   /* Attempt to combine with a previous opcode.  We do this because gcc
3885      likes to output separate unwind directives for a single block of
3886      registers.  */
3887   if (unwind.opcode_count > 0)
3888     {
3889       i = unwind.opcodes[unwind.opcode_count - 1];
3890       if ((i & 0xf8) == 0xc0)
3891         {
3892           i &= 7;
3893           /* Only merge if the blocks are contiguous.  */
3894           if (i < 6)
3895             {
3896               if ((mask & 0xfe00) == (1 << 9))
3897                 {
3898                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
3899                   unwind.opcode_count--;
3900                 }
3901             }
3902           else if (i == 6 && unwind.opcode_count >= 2)
3903             {
3904               i = unwind.opcodes[unwind.opcode_count - 2];
3905               reg = i >> 4;
3906               i &= 0xf;
3907
3908               op = 0xffff << (reg - 1);
3909               if (reg > 0
3910                   && ((mask & op) == (1u << (reg - 1))))
3911                 {
3912                   op = (1 << (reg + i + 1)) - 1;
3913                   op &= ~((1 << reg) - 1);
3914                   mask |= op;
3915                   unwind.opcode_count -= 2;
3916                 }
3917             }
3918         }
3919     }
3920
3921   hi_reg = 15;
3922   /* We want to generate opcodes in the order the registers have been
3923      saved, ie. descending order.  */
3924   for (reg = 15; reg >= -1; reg--)
3925     {
3926       /* Save registers in blocks.  */
3927       if (reg < 0
3928           || !(mask & (1 << reg)))
3929         {
3930           /* We found an unsaved reg.  Generate opcodes to save the
3931              preceding block.   */
3932           if (reg != hi_reg)
3933             {
3934               if (reg == 9)
3935                 {
3936                   /* Short form.  */
3937                   op = 0xc0 | (hi_reg - 10);
3938                   add_unwind_opcode (op, 1);
3939                 }
3940               else
3941                 {
3942                   /* Long form.  */
3943                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
3944                   add_unwind_opcode (op, 2);
3945                 }
3946             }
3947           hi_reg = reg - 1;
3948         }
3949     }
3950
3951   return;
3952 error:
3953   ignore_rest_of_line ();
3954 }
3955
3956 static void
3957 s_arm_unwind_save_mmxwcg (void)
3958 {
3959   int reg;
3960   int hi_reg;
3961   unsigned mask = 0;
3962   valueT op;
3963
3964   if (*input_line_pointer == '{')
3965     input_line_pointer++;
3966
3967   do
3968     {
3969       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
3970
3971       if (reg == FAIL)
3972         {
3973           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
3974           goto error;
3975         }
3976
3977       reg -= 8;
3978       if (mask >> reg)
3979         as_tsktsk (_("register list not in ascending order"));
3980       mask |= 1 << reg;
3981
3982       if (*input_line_pointer == '-')
3983         {
3984           input_line_pointer++;
3985           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
3986           if (hi_reg == FAIL)
3987             {
3988               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
3989               goto error;
3990             }
3991           else if (reg >= hi_reg)
3992             {
3993               as_bad (_("bad register range"));
3994               goto error;
3995             }
3996           for (; reg < hi_reg; reg++)
3997             mask |= 1 << reg;
3998         }
3999     }
4000   while (skip_past_comma (&input_line_pointer) != FAIL);
4001
4002   if (*input_line_pointer == '}')
4003     input_line_pointer++;
4004
4005   demand_empty_rest_of_line ();
4006
4007   /* Generate any deferred opcodes because we're going to be looking at
4008      the list.  */
4009   flush_pending_unwind ();
4010
4011   for (reg = 0; reg < 16; reg++)
4012     {
4013       if (mask & (1 << reg))
4014         unwind.frame_size += 4;
4015     }
4016   op = 0xc700 | mask;
4017   add_unwind_opcode (op, 2);
4018   return;
4019 error:
4020   ignore_rest_of_line ();
4021 }
4022
4023
4024 /* Parse an unwind_save directive.
4025    If the argument is non-zero, this is a .vsave directive.  */
4026
4027 static void
4028 s_arm_unwind_save (int arch_v6)
4029 {
4030   char *peek;
4031   struct reg_entry *reg;
4032   bfd_boolean had_brace = FALSE;
4033
4034   if (!unwind.proc_start)
4035     as_bad (MISSING_FNSTART);
4036
4037   /* Figure out what sort of save we have.  */
4038   peek = input_line_pointer;
4039
4040   if (*peek == '{')
4041     {
4042       had_brace = TRUE;
4043       peek++;
4044     }
4045
4046   reg = arm_reg_parse_multi (&peek);
4047
4048   if (!reg)
4049     {
4050       as_bad (_("register expected"));
4051       ignore_rest_of_line ();
4052       return;
4053     }
4054
4055   switch (reg->type)
4056     {
4057     case REG_TYPE_FN:
4058       if (had_brace)
4059         {
4060           as_bad (_("FPA .unwind_save does not take a register list"));
4061           ignore_rest_of_line ();
4062           return;
4063         }
4064       input_line_pointer = peek;
4065       s_arm_unwind_save_fpa (reg->number);
4066       return;
4067
4068     case REG_TYPE_RN:     s_arm_unwind_save_core ();   return;
4069     case REG_TYPE_VFD:
4070       if (arch_v6)
4071         s_arm_unwind_save_vfp_armv6 ();
4072       else
4073         s_arm_unwind_save_vfp ();
4074       return;
4075     case REG_TYPE_MMXWR:  s_arm_unwind_save_mmxwr ();  return;
4076     case REG_TYPE_MMXWCG: s_arm_unwind_save_mmxwcg (); return;
4077
4078     default:
4079       as_bad (_(".unwind_save does not support this kind of register"));
4080       ignore_rest_of_line ();
4081     }
4082 }
4083
4084
4085 /* Parse an unwind_movsp directive.  */
4086
4087 static void
4088 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4089 {
4090   int reg;
4091   valueT op;
4092   int offset;
4093
4094   if (!unwind.proc_start)
4095     as_bad (MISSING_FNSTART);
4096
4097   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4098   if (reg == FAIL)
4099     {
4100       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4101       ignore_rest_of_line ();
4102       return;
4103     }
4104
4105   /* Optional constant.  */
4106   if (skip_past_comma (&input_line_pointer) != FAIL)
4107     {
4108       if (immediate_for_directive (&offset) == FAIL)
4109         return;
4110     }
4111   else
4112     offset = 0;
4113
4114   demand_empty_rest_of_line ();
4115
4116   if (reg == REG_SP || reg == REG_PC)
4117     {
4118       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4119       return;
4120     }
4121
4122   if (unwind.fp_reg != REG_SP)
4123     as_bad (_("unexpected .unwind_movsp directive"));
4124
4125   /* Generate opcode to restore the value.  */
4126   op = 0x90 | reg;
4127   add_unwind_opcode (op, 1);
4128
4129   /* Record the information for later.  */
4130   unwind.fp_reg = reg;
4131   unwind.fp_offset = unwind.frame_size - offset;
4132   unwind.sp_restored = 1;
4133 }
4134
4135 /* Parse an unwind_pad directive.  */
4136
4137 static void
4138 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4139 {
4140   int offset;
4141
4142   if (!unwind.proc_start)
4143     as_bad (MISSING_FNSTART);
4144
4145   if (immediate_for_directive (&offset) == FAIL)
4146     return;
4147
4148   if (offset & 3)
4149     {
4150       as_bad (_("stack increment must be multiple of 4"));
4151       ignore_rest_of_line ();
4152       return;
4153     }
4154
4155   /* Don't generate any opcodes, just record the details for later.  */
4156   unwind.frame_size += offset;
4157   unwind.pending_offset += offset;
4158
4159   demand_empty_rest_of_line ();
4160 }
4161
4162 /* Parse an unwind_setfp directive.  */
4163
4164 static void
4165 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4166 {
4167   int sp_reg;
4168   int fp_reg;
4169   int offset;
4170
4171   if (!unwind.proc_start)
4172     as_bad (MISSING_FNSTART);
4173
4174   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4175   if (skip_past_comma (&input_line_pointer) == FAIL)
4176     sp_reg = FAIL;
4177   else
4178     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4179
4180   if (fp_reg == FAIL || sp_reg == FAIL)
4181     {
4182       as_bad (_("expected <reg>, <reg>"));
4183       ignore_rest_of_line ();
4184       return;
4185     }
4186
4187   /* Optional constant.  */
4188   if (skip_past_comma (&input_line_pointer) != FAIL)
4189     {
4190       if (immediate_for_directive (&offset) == FAIL)
4191         return;
4192     }
4193   else
4194     offset = 0;
4195
4196   demand_empty_rest_of_line ();
4197
4198   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4199     {
4200       as_bad (_("register must be either sp or set by a previous"
4201                 "unwind_movsp directive"));
4202       return;
4203     }
4204
4205   /* Don't generate any opcodes, just record the information for later.  */
4206   unwind.fp_reg = fp_reg;
4207   unwind.fp_used = 1;
4208   if (sp_reg == REG_SP)
4209     unwind.fp_offset = unwind.frame_size - offset;
4210   else
4211     unwind.fp_offset -= offset;
4212 }
4213
4214 /* Parse an unwind_raw directive.  */
4215
4216 static void
4217 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4218 {
4219   expressionS exp;
4220   /* This is an arbitrary limit.         */
4221   unsigned char op[16];
4222   int count;
4223
4224   if (!unwind.proc_start)
4225     as_bad (MISSING_FNSTART);
4226
4227   expression (&exp);
4228   if (exp.X_op == O_constant
4229       && skip_past_comma (&input_line_pointer) != FAIL)
4230     {
4231       unwind.frame_size += exp.X_add_number;
4232       expression (&exp);
4233     }
4234   else
4235     exp.X_op = O_illegal;
4236
4237   if (exp.X_op != O_constant)
4238     {
4239       as_bad (_("expected <offset>, <opcode>"));
4240       ignore_rest_of_line ();
4241       return;
4242     }
4243
4244   count = 0;
4245
4246   /* Parse the opcode.  */
4247   for (;;)
4248     {
4249       if (count >= 16)
4250         {
4251           as_bad (_("unwind opcode too long"));
4252           ignore_rest_of_line ();
4253         }
4254       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4255         {
4256           as_bad (_("invalid unwind opcode"));
4257           ignore_rest_of_line ();
4258           return;
4259         }
4260       op[count++] = exp.X_add_number;
4261
4262       /* Parse the next byte.  */
4263       if (skip_past_comma (&input_line_pointer) == FAIL)
4264         break;
4265
4266       expression (&exp);
4267     }
4268
4269   /* Add the opcode bytes in reverse order.  */
4270   while (count--)
4271     add_unwind_opcode (op[count], 1);
4272
4273   demand_empty_rest_of_line ();
4274 }
4275
4276
4277 /* Parse a .eabi_attribute directive.  */
4278
4279 static void
4280 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4281 {
4282   int tag = s_vendor_attribute (OBJ_ATTR_PROC);
4283
4284   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4285     attributes_set_explicitly[tag] = 1;
4286 }
4287
4288 /* Emit a tls fix for the symbol.  */
4289
4290 static void
4291 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4292 {
4293   char *p;
4294   expressionS exp;
4295 #ifdef md_flush_pending_output
4296   md_flush_pending_output ();
4297 #endif
4298
4299 #ifdef md_cons_align
4300   md_cons_align (4);
4301 #endif
4302
4303   /* Since we're just labelling the code, there's no need to define a
4304      mapping symbol.  */
4305   expression (&exp);
4306   p = obstack_next_free (&frchain_now->frch_obstack);
4307   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4308                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4309                : BFD_RELOC_ARM_TLS_DESCSEQ);
4310 }
4311 #endif /* OBJ_ELF */
4312
4313 static void s_arm_arch (int);
4314 static void s_arm_object_arch (int);
4315 static void s_arm_cpu (int);
4316 static void s_arm_fpu (int);
4317 static void s_arm_arch_extension (int);
4318
4319 #ifdef TE_PE
4320
4321 static void
4322 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4323 {
4324   expressionS exp;
4325
4326   do
4327     {
4328       expression (&exp);
4329       if (exp.X_op == O_symbol)
4330         exp.X_op = O_secrel;
4331
4332       emit_expr (&exp, 4);
4333     }
4334   while (*input_line_pointer++ == ',');
4335
4336   input_line_pointer--;
4337   demand_empty_rest_of_line ();
4338 }
4339 #endif /* TE_PE */
4340
4341 /* This table describes all the machine specific pseudo-ops the assembler
4342    has to support.  The fields are:
4343      pseudo-op name without dot
4344      function to call to execute this pseudo-op
4345      Integer arg to pass to the function.  */
4346
4347 const pseudo_typeS md_pseudo_table[] =
4348 {
4349   /* Never called because '.req' does not start a line.  */
4350   { "req",         s_req,         0 },
4351   /* Following two are likewise never called.  */
4352   { "dn",          s_dn,          0 },
4353   { "qn",          s_qn,          0 },
4354   { "unreq",       s_unreq,       0 },
4355   { "bss",         s_bss,         0 },
4356   { "align",       s_align,       0 },
4357   { "arm",         s_arm,         0 },
4358   { "thumb",       s_thumb,       0 },
4359   { "code",        s_code,        0 },
4360   { "force_thumb", s_force_thumb, 0 },
4361   { "thumb_func",  s_thumb_func,  0 },
4362   { "thumb_set",   s_thumb_set,   0 },
4363   { "even",        s_even,        0 },
4364   { "ltorg",       s_ltorg,       0 },
4365   { "pool",        s_ltorg,       0 },
4366   { "syntax",      s_syntax,      0 },
4367   { "cpu",         s_arm_cpu,     0 },
4368   { "arch",        s_arm_arch,    0 },
4369   { "object_arch", s_arm_object_arch,   0 },
4370   { "fpu",         s_arm_fpu,     0 },
4371   { "arch_extension", s_arm_arch_extension, 0 },
4372 #ifdef OBJ_ELF
4373   { "word",             s_arm_elf_cons, 4 },
4374   { "long",             s_arm_elf_cons, 4 },
4375   { "inst.n",           s_arm_elf_inst, 2 },
4376   { "inst.w",           s_arm_elf_inst, 4 },
4377   { "inst",             s_arm_elf_inst, 0 },
4378   { "rel31",            s_arm_rel31,      0 },
4379   { "fnstart",          s_arm_unwind_fnstart,   0 },
4380   { "fnend",            s_arm_unwind_fnend,     0 },
4381   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4382   { "personality",      s_arm_unwind_personality, 0 },
4383   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4384   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4385   { "save",             s_arm_unwind_save,      0 },
4386   { "vsave",            s_arm_unwind_save,      1 },
4387   { "movsp",            s_arm_unwind_movsp,     0 },
4388   { "pad",              s_arm_unwind_pad,       0 },
4389   { "setfp",            s_arm_unwind_setfp,     0 },
4390   { "unwind_raw",       s_arm_unwind_raw,       0 },
4391   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4392   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4393 #else
4394   { "word",        cons, 4},
4395
4396   /* These are used for dwarf.  */
4397   {"2byte", cons, 2},
4398   {"4byte", cons, 4},
4399   {"8byte", cons, 8},
4400   /* These are used for dwarf2.  */
4401   { "file", (void (*) (int)) dwarf2_directive_file, 0 },
4402   { "loc",  dwarf2_directive_loc,  0 },
4403   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4404 #endif
4405   { "extend",      float_cons, 'x' },
4406   { "ldouble",     float_cons, 'x' },
4407   { "packed",      float_cons, 'p' },
4408 #ifdef TE_PE
4409   {"secrel32", pe_directive_secrel, 0},
4410 #endif
4411   { 0, 0, 0 }
4412 };
4413 \f
4414 /* Parser functions used exclusively in instruction operands.  */
4415
4416 /* Generic immediate-value read function for use in insn parsing.
4417    STR points to the beginning of the immediate (the leading #);
4418    VAL receives the value; if the value is outside [MIN, MAX]
4419    issue an error.  PREFIX_OPT is true if the immediate prefix is
4420    optional.  */
4421
4422 static int
4423 parse_immediate (char **str, int *val, int min, int max,
4424                  bfd_boolean prefix_opt)
4425 {
4426   expressionS exp;
4427   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4428   if (exp.X_op != O_constant)
4429     {
4430       inst.error = _("constant expression required");
4431       return FAIL;
4432     }
4433
4434   if (exp.X_add_number < min || exp.X_add_number > max)
4435     {
4436       inst.error = _("immediate value out of range");
4437       return FAIL;
4438     }
4439
4440   *val = exp.X_add_number;
4441   return SUCCESS;
4442 }
4443
4444 /* Less-generic immediate-value read function with the possibility of loading a
4445    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4446    instructions. Puts the result directly in inst.operands[i].  */
4447
4448 static int
4449 parse_big_immediate (char **str, int i)
4450 {
4451   expressionS exp;
4452   char *ptr = *str;
4453
4454   my_get_expression (&exp, &ptr, GE_OPT_PREFIX_BIG);
4455
4456   if (exp.X_op == O_constant)
4457     {
4458       inst.operands[i].imm = exp.X_add_number & 0xffffffff;
4459       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4460          O_constant.  We have to be careful not to break compilation for
4461          32-bit X_add_number, though.  */
4462       if ((exp.X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4463         {
4464           /* X >> 32 is illegal if sizeof (exp.X_add_number) == 4.  */
4465           inst.operands[i].reg = ((exp.X_add_number >> 16) >> 16) & 0xffffffff;
4466           inst.operands[i].regisimm = 1;
4467         }
4468     }
4469   else if (exp.X_op == O_big
4470            && LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 32)
4471     {
4472       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4473
4474       /* Bignums have their least significant bits in
4475          generic_bignum[0]. Make sure we put 32 bits in imm and
4476          32 bits in reg,  in a (hopefully) portable way.  */
4477       gas_assert (parts != 0);
4478
4479       /* Make sure that the number is not too big.
4480          PR 11972: Bignums can now be sign-extended to the
4481          size of a .octa so check that the out of range bits
4482          are all zero or all one.  */
4483       if (LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 64)
4484         {
4485           LITTLENUM_TYPE m = -1;
4486
4487           if (generic_bignum[parts * 2] != 0
4488               && generic_bignum[parts * 2] != m)
4489             return FAIL;
4490
4491           for (j = parts * 2 + 1; j < (unsigned) exp.X_add_number; j++)
4492             if (generic_bignum[j] != generic_bignum[j-1])
4493               return FAIL;
4494         }
4495
4496       inst.operands[i].imm = 0;
4497       for (j = 0; j < parts; j++, idx++)
4498         inst.operands[i].imm |= generic_bignum[idx]
4499                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4500       inst.operands[i].reg = 0;
4501       for (j = 0; j < parts; j++, idx++)
4502         inst.operands[i].reg |= generic_bignum[idx]
4503                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4504       inst.operands[i].regisimm = 1;
4505     }
4506   else
4507     return FAIL;
4508
4509   *str = ptr;
4510
4511   return SUCCESS;
4512 }
4513
4514 /* Returns the pseudo-register number of an FPA immediate constant,
4515    or FAIL if there isn't a valid constant here.  */
4516
4517 static int
4518 parse_fpa_immediate (char ** str)
4519 {
4520   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4521   char *         save_in;
4522   expressionS    exp;
4523   int            i;
4524   int            j;
4525
4526   /* First try and match exact strings, this is to guarantee
4527      that some formats will work even for cross assembly.  */
4528
4529   for (i = 0; fp_const[i]; i++)
4530     {
4531       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4532         {
4533           char *start = *str;
4534
4535           *str += strlen (fp_const[i]);
4536           if (is_end_of_line[(unsigned char) **str])
4537             return i + 8;
4538           *str = start;
4539         }
4540     }
4541
4542   /* Just because we didn't get a match doesn't mean that the constant
4543      isn't valid, just that it is in a format that we don't
4544      automatically recognize.  Try parsing it with the standard
4545      expression routines.  */
4546
4547   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4548
4549   /* Look for a raw floating point number.  */
4550   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4551       && is_end_of_line[(unsigned char) *save_in])
4552     {
4553       for (i = 0; i < NUM_FLOAT_VALS; i++)
4554         {
4555           for (j = 0; j < MAX_LITTLENUMS; j++)
4556             {
4557               if (words[j] != fp_values[i][j])
4558                 break;
4559             }
4560
4561           if (j == MAX_LITTLENUMS)
4562             {
4563               *str = save_in;
4564               return i + 8;
4565             }
4566         }
4567     }
4568
4569   /* Try and parse a more complex expression, this will probably fail
4570      unless the code uses a floating point prefix (eg "0f").  */
4571   save_in = input_line_pointer;
4572   input_line_pointer = *str;
4573   if (expression (&exp) == absolute_section
4574       && exp.X_op == O_big
4575       && exp.X_add_number < 0)
4576     {
4577       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4578          Ditto for 15.  */
4579       if (gen_to_words (words, 5, (long) 15) == 0)
4580         {
4581           for (i = 0; i < NUM_FLOAT_VALS; i++)
4582             {
4583               for (j = 0; j < MAX_LITTLENUMS; j++)
4584                 {
4585                   if (words[j] != fp_values[i][j])
4586                     break;
4587                 }
4588
4589               if (j == MAX_LITTLENUMS)
4590                 {
4591                   *str = input_line_pointer;
4592                   input_line_pointer = save_in;
4593                   return i + 8;
4594                 }
4595             }
4596         }
4597     }
4598
4599   *str = input_line_pointer;
4600   input_line_pointer = save_in;
4601   inst.error = _("invalid FPA immediate expression");
4602   return FAIL;
4603 }
4604
4605 /* Returns 1 if a number has "quarter-precision" float format
4606    0baBbbbbbc defgh000 00000000 00000000.  */
4607
4608 static int
4609 is_quarter_float (unsigned imm)
4610 {
4611   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4612   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4613 }
4614
4615 /* Parse an 8-bit "quarter-precision" floating point number of the form:
4616    0baBbbbbbc defgh000 00000000 00000000.
4617    The zero and minus-zero cases need special handling, since they can't be
4618    encoded in the "quarter-precision" float format, but can nonetheless be
4619    loaded as integer constants.  */
4620
4621 static unsigned
4622 parse_qfloat_immediate (char **ccp, int *immed)
4623 {
4624   char *str = *ccp;
4625   char *fpnum;
4626   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4627   int found_fpchar = 0;
4628
4629   skip_past_char (&str, '#');
4630
4631   /* We must not accidentally parse an integer as a floating-point number. Make
4632      sure that the value we parse is not an integer by checking for special
4633      characters '.' or 'e'.
4634      FIXME: This is a horrible hack, but doing better is tricky because type
4635      information isn't in a very usable state at parse time.  */
4636   fpnum = str;
4637   skip_whitespace (fpnum);
4638
4639   if (strncmp (fpnum, "0x", 2) == 0)
4640     return FAIL;
4641   else
4642     {
4643       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
4644         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
4645           {
4646             found_fpchar = 1;
4647             break;
4648           }
4649
4650       if (!found_fpchar)
4651         return FAIL;
4652     }
4653
4654   if ((str = atof_ieee (str, 's', words)) != NULL)
4655     {
4656       unsigned fpword = 0;
4657       int i;
4658
4659       /* Our FP word must be 32 bits (single-precision FP).  */
4660       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
4661         {
4662           fpword <<= LITTLENUM_NUMBER_OF_BITS;
4663           fpword |= words[i];
4664         }
4665
4666       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
4667         *immed = fpword;
4668       else
4669         return FAIL;
4670
4671       *ccp = str;
4672
4673       return SUCCESS;
4674     }
4675
4676   return FAIL;
4677 }
4678
4679 /* Shift operands.  */
4680 enum shift_kind
4681 {
4682   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
4683 };
4684
4685 struct asm_shift_name
4686 {
4687   const char      *name;
4688   enum shift_kind  kind;
4689 };
4690
4691 /* Third argument to parse_shift.  */
4692 enum parse_shift_mode
4693 {
4694   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
4695   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
4696   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
4697   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
4698   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
4699 };
4700
4701 /* Parse a <shift> specifier on an ARM data processing instruction.
4702    This has three forms:
4703
4704      (LSL|LSR|ASL|ASR|ROR) Rs
4705      (LSL|LSR|ASL|ASR|ROR) #imm
4706      RRX
4707
4708    Note that ASL is assimilated to LSL in the instruction encoding, and
4709    RRX to ROR #0 (which cannot be written as such).  */
4710
4711 static int
4712 parse_shift (char **str, int i, enum parse_shift_mode mode)
4713 {
4714   const struct asm_shift_name *shift_name;
4715   enum shift_kind shift;
4716   char *s = *str;
4717   char *p = s;
4718   int reg;
4719
4720   for (p = *str; ISALPHA (*p); p++)
4721     ;
4722
4723   if (p == *str)
4724     {
4725       inst.error = _("shift expression expected");
4726       return FAIL;
4727     }
4728
4729   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
4730                                                             p - *str);
4731
4732   if (shift_name == NULL)
4733     {
4734       inst.error = _("shift expression expected");
4735       return FAIL;
4736     }
4737
4738   shift = shift_name->kind;
4739
4740   switch (mode)
4741     {
4742     case NO_SHIFT_RESTRICT:
4743     case SHIFT_IMMEDIATE:   break;
4744
4745     case SHIFT_LSL_OR_ASR_IMMEDIATE:
4746       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
4747         {
4748           inst.error = _("'LSL' or 'ASR' required");
4749           return FAIL;
4750         }
4751       break;
4752
4753     case SHIFT_LSL_IMMEDIATE:
4754       if (shift != SHIFT_LSL)
4755         {
4756           inst.error = _("'LSL' required");
4757           return FAIL;
4758         }
4759       break;
4760
4761     case SHIFT_ASR_IMMEDIATE:
4762       if (shift != SHIFT_ASR)
4763         {
4764           inst.error = _("'ASR' required");
4765           return FAIL;
4766         }
4767       break;
4768
4769     default: abort ();
4770     }
4771
4772   if (shift != SHIFT_RRX)
4773     {
4774       /* Whitespace can appear here if the next thing is a bare digit.  */
4775       skip_whitespace (p);
4776
4777       if (mode == NO_SHIFT_RESTRICT
4778           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
4779         {
4780           inst.operands[i].imm = reg;
4781           inst.operands[i].immisreg = 1;
4782         }
4783       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
4784         return FAIL;
4785     }
4786   inst.operands[i].shift_kind = shift;
4787   inst.operands[i].shifted = 1;
4788   *str = p;
4789   return SUCCESS;
4790 }
4791
4792 /* Parse a <shifter_operand> for an ARM data processing instruction:
4793
4794       #<immediate>
4795       #<immediate>, <rotate>
4796       <Rm>
4797       <Rm>, <shift>
4798
4799    where <shift> is defined by parse_shift above, and <rotate> is a
4800    multiple of 2 between 0 and 30.  Validation of immediate operands
4801    is deferred to md_apply_fix.  */
4802
4803 static int
4804 parse_shifter_operand (char **str, int i)
4805 {
4806   int value;
4807   expressionS exp;
4808
4809   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
4810     {
4811       inst.operands[i].reg = value;
4812       inst.operands[i].isreg = 1;
4813
4814       /* parse_shift will override this if appropriate */
4815       inst.reloc.exp.X_op = O_constant;
4816       inst.reloc.exp.X_add_number = 0;
4817
4818       if (skip_past_comma (str) == FAIL)
4819         return SUCCESS;
4820
4821       /* Shift operation on register.  */
4822       return parse_shift (str, i, NO_SHIFT_RESTRICT);
4823     }
4824
4825   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
4826     return FAIL;
4827
4828   if (skip_past_comma (str) == SUCCESS)
4829     {
4830       /* #x, y -- ie explicit rotation by Y.  */
4831       if (my_get_expression (&exp, str, GE_NO_PREFIX))
4832         return FAIL;
4833
4834       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
4835         {
4836           inst.error = _("constant expression expected");
4837           return FAIL;
4838         }
4839
4840       value = exp.X_add_number;
4841       if (value < 0 || value > 30 || value % 2 != 0)
4842         {
4843           inst.error = _("invalid rotation");
4844           return FAIL;
4845         }
4846       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
4847         {
4848           inst.error = _("invalid constant");
4849           return FAIL;
4850         }
4851
4852       /* Convert to decoded value.  md_apply_fix will put it back.  */
4853       inst.reloc.exp.X_add_number
4854         = (((inst.reloc.exp.X_add_number << (32 - value))
4855             | (inst.reloc.exp.X_add_number >> value)) & 0xffffffff);
4856     }
4857
4858   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
4859   inst.reloc.pc_rel = 0;
4860   return SUCCESS;
4861 }
4862
4863 /* Group relocation information.  Each entry in the table contains the
4864    textual name of the relocation as may appear in assembler source
4865    and must end with a colon.
4866    Along with this textual name are the relocation codes to be used if
4867    the corresponding instruction is an ALU instruction (ADD or SUB only),
4868    an LDR, an LDRS, or an LDC.  */
4869
4870 struct group_reloc_table_entry
4871 {
4872   const char *name;
4873   int alu_code;
4874   int ldr_code;
4875   int ldrs_code;
4876   int ldc_code;
4877 };
4878
4879 typedef enum
4880 {
4881   /* Varieties of non-ALU group relocation.  */
4882
4883   GROUP_LDR,
4884   GROUP_LDRS,
4885   GROUP_LDC
4886 } group_reloc_type;
4887
4888 static struct group_reloc_table_entry group_reloc_table[] =
4889   { /* Program counter relative: */
4890     { "pc_g0_nc",
4891       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
4892       0,                                /* LDR */
4893       0,                                /* LDRS */
4894       0 },                              /* LDC */
4895     { "pc_g0",
4896       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
4897       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
4898       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
4899       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
4900     { "pc_g1_nc",
4901       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
4902       0,                                /* LDR */
4903       0,                                /* LDRS */
4904       0 },                              /* LDC */
4905     { "pc_g1",
4906       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
4907       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
4908       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
4909       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
4910     { "pc_g2",
4911       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
4912       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
4913       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
4914       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
4915     /* Section base relative */
4916     { "sb_g0_nc",
4917       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
4918       0,                                /* LDR */
4919       0,                                /* LDRS */
4920       0 },                              /* LDC */
4921     { "sb_g0",
4922       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
4923       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
4924       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
4925       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
4926     { "sb_g1_nc",
4927       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
4928       0,                                /* LDR */
4929       0,                                /* LDRS */
4930       0 },                              /* LDC */
4931     { "sb_g1",
4932       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
4933       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
4934       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
4935       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
4936     { "sb_g2",
4937       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
4938       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
4939       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
4940       BFD_RELOC_ARM_LDC_SB_G2 } };      /* LDC */
4941
4942 /* Given the address of a pointer pointing to the textual name of a group
4943    relocation as may appear in assembler source, attempt to find its details
4944    in group_reloc_table.  The pointer will be updated to the character after
4945    the trailing colon.  On failure, FAIL will be returned; SUCCESS
4946    otherwise.  On success, *entry will be updated to point at the relevant
4947    group_reloc_table entry. */
4948
4949 static int
4950 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
4951 {
4952   unsigned int i;
4953   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
4954     {
4955       int length = strlen (group_reloc_table[i].name);
4956
4957       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
4958           && (*str)[length] == ':')
4959         {
4960           *out = &group_reloc_table[i];
4961           *str += (length + 1);
4962           return SUCCESS;
4963         }
4964     }
4965
4966   return FAIL;
4967 }
4968
4969 /* Parse a <shifter_operand> for an ARM data processing instruction
4970    (as for parse_shifter_operand) where group relocations are allowed:
4971
4972       #<immediate>
4973       #<immediate>, <rotate>
4974       #:<group_reloc>:<expression>
4975       <Rm>
4976       <Rm>, <shift>
4977
4978    where <group_reloc> is one of the strings defined in group_reloc_table.
4979    The hashes are optional.
4980
4981    Everything else is as for parse_shifter_operand.  */
4982
4983 static parse_operand_result
4984 parse_shifter_operand_group_reloc (char **str, int i)
4985 {
4986   /* Determine if we have the sequence of characters #: or just :
4987      coming next.  If we do, then we check for a group relocation.
4988      If we don't, punt the whole lot to parse_shifter_operand.  */
4989
4990   if (((*str)[0] == '#' && (*str)[1] == ':')
4991       || (*str)[0] == ':')
4992     {
4993       struct group_reloc_table_entry *entry;
4994
4995       if ((*str)[0] == '#')
4996         (*str) += 2;
4997       else
4998         (*str)++;
4999
5000       /* Try to parse a group relocation.  Anything else is an error.  */
5001       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5002         {
5003           inst.error = _("unknown group relocation");
5004           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5005         }
5006
5007       /* We now have the group relocation table entry corresponding to
5008          the name in the assembler source.  Next, we parse the expression.  */
5009       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5010         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5011
5012       /* Record the relocation type (always the ALU variant here).  */
5013       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5014       gas_assert (inst.reloc.type != 0);
5015
5016       return PARSE_OPERAND_SUCCESS;
5017     }
5018   else
5019     return parse_shifter_operand (str, i) == SUCCESS
5020            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5021
5022   /* Never reached.  */
5023 }
5024
5025 /* Parse a Neon alignment expression.  Information is written to
5026    inst.operands[i].  We assume the initial ':' has been skipped.
5027    
5028    align        .imm = align << 8, .immisalign=1, .preind=0  */
5029 static parse_operand_result
5030 parse_neon_alignment (char **str, int i)
5031 {
5032   char *p = *str;
5033   expressionS exp;
5034
5035   my_get_expression (&exp, &p, GE_NO_PREFIX);
5036
5037   if (exp.X_op != O_constant)
5038     {
5039       inst.error = _("alignment must be constant");
5040       return PARSE_OPERAND_FAIL;
5041     }
5042
5043   inst.operands[i].imm = exp.X_add_number << 8;
5044   inst.operands[i].immisalign = 1;
5045   /* Alignments are not pre-indexes.  */
5046   inst.operands[i].preind = 0;
5047
5048   *str = p;
5049   return PARSE_OPERAND_SUCCESS;
5050 }
5051
5052 /* Parse all forms of an ARM address expression.  Information is written
5053    to inst.operands[i] and/or inst.reloc.
5054
5055    Preindexed addressing (.preind=1):
5056
5057    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5058    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5059    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5060                        .shift_kind=shift .reloc.exp=shift_imm
5061
5062    These three may have a trailing ! which causes .writeback to be set also.
5063
5064    Postindexed addressing (.postind=1, .writeback=1):
5065
5066    [Rn], #offset       .reg=Rn .reloc.exp=offset
5067    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5068    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5069                        .shift_kind=shift .reloc.exp=shift_imm
5070
5071    Unindexed addressing (.preind=0, .postind=0):
5072
5073    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5074
5075    Other:
5076
5077    [Rn]{!}             shorthand for [Rn,#0]{!}
5078    =immediate          .isreg=0 .reloc.exp=immediate
5079    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5080
5081   It is the caller's responsibility to check for addressing modes not
5082   supported by the instruction, and to set inst.reloc.type.  */
5083
5084 static parse_operand_result
5085 parse_address_main (char **str, int i, int group_relocations,
5086                     group_reloc_type group_type)
5087 {
5088   char *p = *str;
5089   int reg;
5090
5091   if (skip_past_char (&p, '[') == FAIL)
5092     {
5093       if (skip_past_char (&p, '=') == FAIL)
5094         {
5095           /* Bare address - translate to PC-relative offset.  */
5096           inst.reloc.pc_rel = 1;
5097           inst.operands[i].reg = REG_PC;
5098           inst.operands[i].isreg = 1;
5099           inst.operands[i].preind = 1;
5100         }
5101       /* Otherwise a load-constant pseudo op, no special treatment needed here.  */
5102
5103       if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5104         return PARSE_OPERAND_FAIL;
5105
5106       *str = p;
5107       return PARSE_OPERAND_SUCCESS;
5108     }
5109
5110   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5111     {
5112       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5113       return PARSE_OPERAND_FAIL;
5114     }
5115   inst.operands[i].reg = reg;
5116   inst.operands[i].isreg = 1;
5117
5118   if (skip_past_comma (&p) == SUCCESS)
5119     {
5120       inst.operands[i].preind = 1;
5121
5122       if (*p == '+') p++;
5123       else if (*p == '-') p++, inst.operands[i].negative = 1;
5124
5125       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5126         {
5127           inst.operands[i].imm = reg;
5128           inst.operands[i].immisreg = 1;
5129
5130           if (skip_past_comma (&p) == SUCCESS)
5131             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5132               return PARSE_OPERAND_FAIL;
5133         }
5134       else if (skip_past_char (&p, ':') == SUCCESS)
5135         {
5136           /* FIXME: '@' should be used here, but it's filtered out by generic
5137              code before we get to see it here. This may be subject to
5138              change.  */
5139           parse_operand_result result = parse_neon_alignment (&p, i);
5140           
5141           if (result != PARSE_OPERAND_SUCCESS)
5142             return result;
5143         }
5144       else
5145         {
5146           if (inst.operands[i].negative)
5147             {
5148               inst.operands[i].negative = 0;
5149               p--;
5150             }
5151
5152           if (group_relocations
5153               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5154             {
5155               struct group_reloc_table_entry *entry;
5156
5157               /* Skip over the #: or : sequence.  */
5158               if (*p == '#')
5159                 p += 2;
5160               else
5161                 p++;
5162
5163               /* Try to parse a group relocation.  Anything else is an
5164                  error.  */
5165               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5166                 {
5167                   inst.error = _("unknown group relocation");
5168                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5169                 }
5170
5171               /* We now have the group relocation table entry corresponding to
5172                  the name in the assembler source.  Next, we parse the
5173                  expression.  */
5174               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5175                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5176
5177               /* Record the relocation type.  */
5178               switch (group_type)
5179                 {
5180                   case GROUP_LDR:
5181                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5182                     break;
5183
5184                   case GROUP_LDRS:
5185                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5186                     break;
5187
5188                   case GROUP_LDC:
5189                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5190                     break;
5191
5192                   default:
5193                     gas_assert (0);
5194                 }
5195
5196               if (inst.reloc.type == 0)
5197                 {
5198                   inst.error = _("this group relocation is not allowed on this instruction");
5199                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5200                 }
5201             }
5202           else
5203             if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5204               return PARSE_OPERAND_FAIL;
5205         }
5206     }
5207   else if (skip_past_char (&p, ':') == SUCCESS)
5208     {
5209       /* FIXME: '@' should be used here, but it's filtered out by generic code
5210          before we get to see it here. This may be subject to change.  */
5211       parse_operand_result result = parse_neon_alignment (&p, i);
5212       
5213       if (result != PARSE_OPERAND_SUCCESS)
5214         return result;
5215     }
5216
5217   if (skip_past_char (&p, ']') == FAIL)
5218     {
5219       inst.error = _("']' expected");
5220       return PARSE_OPERAND_FAIL;
5221     }
5222
5223   if (skip_past_char (&p, '!') == SUCCESS)
5224     inst.operands[i].writeback = 1;
5225
5226   else if (skip_past_comma (&p) == SUCCESS)
5227     {
5228       if (skip_past_char (&p, '{') == SUCCESS)
5229         {
5230           /* [Rn], {expr} - unindexed, with option */
5231           if (parse_immediate (&p, &inst.operands[i].imm,
5232                                0, 255, TRUE) == FAIL)
5233             return PARSE_OPERAND_FAIL;
5234
5235           if (skip_past_char (&p, '}') == FAIL)
5236             {
5237               inst.error = _("'}' expected at end of 'option' field");
5238               return PARSE_OPERAND_FAIL;
5239             }
5240           if (inst.operands[i].preind)
5241             {
5242               inst.error = _("cannot combine index with option");
5243               return PARSE_OPERAND_FAIL;
5244             }
5245           *str = p;
5246           return PARSE_OPERAND_SUCCESS;
5247         }
5248       else
5249         {
5250           inst.operands[i].postind = 1;
5251           inst.operands[i].writeback = 1;
5252
5253           if (inst.operands[i].preind)
5254             {
5255               inst.error = _("cannot combine pre- and post-indexing");
5256               return PARSE_OPERAND_FAIL;
5257             }
5258
5259           if (*p == '+') p++;
5260           else if (*p == '-') p++, inst.operands[i].negative = 1;
5261
5262           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5263             {
5264               /* We might be using the immediate for alignment already. If we
5265                  are, OR the register number into the low-order bits.  */
5266               if (inst.operands[i].immisalign)
5267                 inst.operands[i].imm |= reg;
5268               else
5269                 inst.operands[i].imm = reg;
5270               inst.operands[i].immisreg = 1;
5271
5272               if (skip_past_comma (&p) == SUCCESS)
5273                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5274                   return PARSE_OPERAND_FAIL;
5275             }
5276           else
5277             {
5278               if (inst.operands[i].negative)
5279                 {
5280                   inst.operands[i].negative = 0;
5281                   p--;
5282                 }
5283               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5284                 return PARSE_OPERAND_FAIL;
5285             }
5286         }
5287     }
5288
5289   /* If at this point neither .preind nor .postind is set, we have a
5290      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5291   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5292     {
5293       inst.operands[i].preind = 1;
5294       inst.reloc.exp.X_op = O_constant;
5295       inst.reloc.exp.X_add_number = 0;
5296     }
5297   *str = p;
5298   return PARSE_OPERAND_SUCCESS;
5299 }
5300
5301 static int
5302 parse_address (char **str, int i)
5303 {
5304   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5305          ? SUCCESS : FAIL;
5306 }
5307
5308 static parse_operand_result
5309 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5310 {
5311   return parse_address_main (str, i, 1, type);
5312 }
5313
5314 /* Parse an operand for a MOVW or MOVT instruction.  */
5315 static int
5316 parse_half (char **str)
5317 {
5318   char * p;
5319
5320   p = *str;
5321   skip_past_char (&p, '#');
5322   if (strncasecmp (p, ":lower16:", 9) == 0)
5323     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5324   else if (strncasecmp (p, ":upper16:", 9) == 0)
5325     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5326
5327   if (inst.reloc.type != BFD_RELOC_UNUSED)
5328     {
5329       p += 9;
5330       skip_whitespace (p);
5331     }
5332
5333   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5334     return FAIL;
5335
5336   if (inst.reloc.type == BFD_RELOC_UNUSED)
5337     {
5338       if (inst.reloc.exp.X_op != O_constant)
5339         {
5340           inst.error = _("constant expression expected");
5341           return FAIL;
5342         }
5343       if (inst.reloc.exp.X_add_number < 0
5344           || inst.reloc.exp.X_add_number > 0xffff)
5345         {
5346           inst.error = _("immediate value out of range");
5347           return FAIL;
5348         }
5349     }
5350   *str = p;
5351   return SUCCESS;
5352 }
5353
5354 /* Miscellaneous. */
5355
5356 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5357    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5358 static int
5359 parse_psr (char **str, bfd_boolean lhs)
5360 {
5361   char *p;
5362   unsigned long psr_field;
5363   const struct asm_psr *psr;
5364   char *start;
5365   bfd_boolean is_apsr = FALSE;
5366   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5367
5368   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5369      feature for ease of use and backwards compatibility.  */
5370   p = *str;
5371   if (strncasecmp (p, "SPSR", 4) == 0)
5372     {
5373       if (m_profile)
5374         goto unsupported_psr;
5375         
5376       psr_field = SPSR_BIT;
5377     }
5378   else if (strncasecmp (p, "CPSR", 4) == 0)
5379     {
5380       if (m_profile)
5381         goto unsupported_psr;
5382
5383       psr_field = 0;
5384     }
5385   else if (strncasecmp (p, "APSR", 4) == 0)
5386     {
5387       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5388          and ARMv7-R architecture CPUs.  */
5389       is_apsr = TRUE;
5390       psr_field = 0;
5391     }
5392   else if (m_profile)
5393     {
5394       start = p;
5395       do
5396         p++;
5397       while (ISALNUM (*p) || *p == '_');
5398
5399       if (strncasecmp (start, "iapsr", 5) == 0
5400           || strncasecmp (start, "eapsr", 5) == 0
5401           || strncasecmp (start, "xpsr", 4) == 0
5402           || strncasecmp (start, "psr", 3) == 0)
5403         p = start + strcspn (start, "rR") + 1;
5404
5405       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5406                                                   p - start);
5407
5408       if (!psr)
5409         return FAIL;
5410
5411       /* If APSR is being written, a bitfield may be specified.  Note that
5412          APSR itself is handled above.  */
5413       if (psr->field <= 3)
5414         {
5415           psr_field = psr->field;
5416           is_apsr = TRUE;
5417           goto check_suffix;
5418         }
5419
5420       *str = p;
5421       /* M-profile MSR instructions have the mask field set to "10", except
5422          *PSR variants which modify APSR, which may use a different mask (and
5423          have been handled already).  Do that by setting the PSR_f field
5424          here.  */
5425       return psr->field | (lhs ? PSR_f : 0);
5426     }
5427   else
5428     goto unsupported_psr;
5429
5430   p += 4;
5431 check_suffix:
5432   if (*p == '_')
5433     {
5434       /* A suffix follows.  */
5435       p++;
5436       start = p;
5437
5438       do
5439         p++;
5440       while (ISALNUM (*p) || *p == '_');
5441
5442       if (is_apsr)
5443         {
5444           /* APSR uses a notation for bits, rather than fields.  */
5445           unsigned int nzcvq_bits = 0;
5446           unsigned int g_bit = 0;
5447           char *bit;
5448           
5449           for (bit = start; bit != p; bit++)
5450             {
5451               switch (TOLOWER (*bit))
5452                 {
5453                 case 'n':
5454                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5455                   break;
5456
5457                 case 'z':
5458                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5459                   break;
5460
5461                 case 'c':
5462                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5463                   break;
5464
5465                 case 'v':
5466                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5467                   break;
5468                 
5469                 case 'q':
5470                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5471                   break;
5472                 
5473                 case 'g':
5474                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5475                   break;
5476                 
5477                 default:
5478                   inst.error = _("unexpected bit specified after APSR");
5479                   return FAIL;
5480                 }
5481             }
5482           
5483           if (nzcvq_bits == 0x1f)
5484             psr_field |= PSR_f;
5485           
5486           if (g_bit == 0x1)
5487             {
5488               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5489                 {
5490                   inst.error = _("selected processor does not "
5491                                  "support DSP extension");
5492                   return FAIL;
5493                 }
5494
5495               psr_field |= PSR_s;
5496             }
5497           
5498           if ((nzcvq_bits & 0x20) != 0
5499               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5500               || (g_bit & 0x2) != 0)
5501             {
5502               inst.error = _("bad bitmask specified after APSR");
5503               return FAIL;
5504             }
5505         }
5506       else
5507         {
5508           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
5509                                                       p - start);
5510           if (!psr)
5511             goto error;
5512
5513           psr_field |= psr->field;
5514         }
5515     }
5516   else
5517     {
5518       if (ISALNUM (*p))
5519         goto error;    /* Garbage after "[CS]PSR".  */
5520
5521       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
5522          is deprecated, but allow it anyway.  */
5523       if (is_apsr && lhs)
5524         {
5525           psr_field |= PSR_f;
5526           as_tsktsk (_("writing to APSR without specifying a bitmask is "
5527                        "deprecated"));
5528         }
5529       else if (!m_profile)
5530         /* These bits are never right for M-profile devices: don't set them
5531            (only code paths which read/write APSR reach here).  */
5532         psr_field |= (PSR_c | PSR_f);
5533     }
5534   *str = p;
5535   return psr_field;
5536
5537  unsupported_psr:
5538   inst.error = _("selected processor does not support requested special "
5539                  "purpose register");
5540   return FAIL;
5541
5542  error:
5543   inst.error = _("flag for {c}psr instruction expected");
5544   return FAIL;
5545 }
5546
5547 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
5548    value suitable for splatting into the AIF field of the instruction.  */
5549
5550 static int
5551 parse_cps_flags (char **str)
5552 {
5553   int val = 0;
5554   int saw_a_flag = 0;
5555   char *s = *str;
5556
5557   for (;;)
5558     switch (*s++)
5559       {
5560       case '\0': case ',':
5561         goto done;
5562
5563       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
5564       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
5565       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
5566
5567       default:
5568         inst.error = _("unrecognized CPS flag");
5569         return FAIL;
5570       }
5571
5572  done:
5573   if (saw_a_flag == 0)
5574     {
5575       inst.error = _("missing CPS flags");
5576       return FAIL;
5577     }
5578
5579   *str = s - 1;
5580   return val;
5581 }
5582
5583 /* Parse an endian specifier ("BE" or "LE", case insensitive);
5584    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
5585
5586 static int
5587 parse_endian_specifier (char **str)
5588 {
5589   int little_endian;
5590   char *s = *str;
5591
5592   if (strncasecmp (s, "BE", 2))
5593     little_endian = 0;
5594   else if (strncasecmp (s, "LE", 2))
5595     little_endian = 1;
5596   else
5597     {
5598       inst.error = _("valid endian specifiers are be or le");
5599       return FAIL;
5600     }
5601
5602   if (ISALNUM (s[2]) || s[2] == '_')
5603     {
5604       inst.error = _("valid endian specifiers are be or le");
5605       return FAIL;
5606     }
5607
5608   *str = s + 2;
5609   return little_endian;
5610 }
5611
5612 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
5613    value suitable for poking into the rotate field of an sxt or sxta
5614    instruction, or FAIL on error.  */
5615
5616 static int
5617 parse_ror (char **str)
5618 {
5619   int rot;
5620   char *s = *str;
5621
5622   if (strncasecmp (s, "ROR", 3) == 0)
5623     s += 3;
5624   else
5625     {
5626       inst.error = _("missing rotation field after comma");
5627       return FAIL;
5628     }
5629
5630   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
5631     return FAIL;
5632
5633   switch (rot)
5634     {
5635     case  0: *str = s; return 0x0;
5636     case  8: *str = s; return 0x1;
5637     case 16: *str = s; return 0x2;
5638     case 24: *str = s; return 0x3;
5639
5640     default:
5641       inst.error = _("rotation can only be 0, 8, 16, or 24");
5642       return FAIL;
5643     }
5644 }
5645
5646 /* Parse a conditional code (from conds[] below).  The value returned is in the
5647    range 0 .. 14, or FAIL.  */
5648 static int
5649 parse_cond (char **str)
5650 {
5651   char *q;
5652   const struct asm_cond *c;
5653   int n;
5654   /* Condition codes are always 2 characters, so matching up to
5655      3 characters is sufficient.  */
5656   char cond[3];
5657
5658   q = *str;
5659   n = 0;
5660   while (ISALPHA (*q) && n < 3)
5661     {
5662       cond[n] = TOLOWER (*q);
5663       q++;
5664       n++;
5665     }
5666
5667   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
5668   if (!c)
5669     {
5670       inst.error = _("condition required");
5671       return FAIL;
5672     }
5673
5674   *str = q;
5675   return c->value;
5676 }
5677
5678 /* Parse an option for a barrier instruction.  Returns the encoding for the
5679    option, or FAIL.  */
5680 static int
5681 parse_barrier (char **str)
5682 {
5683   char *p, *q;
5684   const struct asm_barrier_opt *o;
5685
5686   p = q = *str;
5687   while (ISALPHA (*q))
5688     q++;
5689
5690   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
5691                                                     q - p);
5692   if (!o)
5693     return FAIL;
5694
5695   *str = q;
5696   return o->value;
5697 }
5698
5699 /* Parse the operands of a table branch instruction.  Similar to a memory
5700    operand.  */
5701 static int
5702 parse_tb (char **str)
5703 {
5704   char * p = *str;
5705   int reg;
5706
5707   if (skip_past_char (&p, '[') == FAIL)
5708     {
5709       inst.error = _("'[' expected");
5710       return FAIL;
5711     }
5712
5713   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5714     {
5715       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5716       return FAIL;
5717     }
5718   inst.operands[0].reg = reg;
5719
5720   if (skip_past_comma (&p) == FAIL)
5721     {
5722       inst.error = _("',' expected");
5723       return FAIL;
5724     }
5725
5726   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5727     {
5728       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5729       return FAIL;
5730     }
5731   inst.operands[0].imm = reg;
5732
5733   if (skip_past_comma (&p) == SUCCESS)
5734     {
5735       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
5736         return FAIL;
5737       if (inst.reloc.exp.X_add_number != 1)
5738         {
5739           inst.error = _("invalid shift");
5740           return FAIL;
5741         }
5742       inst.operands[0].shifted = 1;
5743     }
5744
5745   if (skip_past_char (&p, ']') == FAIL)
5746     {
5747       inst.error = _("']' expected");
5748       return FAIL;
5749     }
5750   *str = p;
5751   return SUCCESS;
5752 }
5753
5754 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
5755    information on the types the operands can take and how they are encoded.
5756    Up to four operands may be read; this function handles setting the
5757    ".present" field for each read operand itself.
5758    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
5759    else returns FAIL.  */
5760
5761 static int
5762 parse_neon_mov (char **str, int *which_operand)
5763 {
5764   int i = *which_operand, val;
5765   enum arm_reg_type rtype;
5766   char *ptr = *str;
5767   struct neon_type_el optype;
5768
5769   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
5770     {
5771       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
5772       inst.operands[i].reg = val;
5773       inst.operands[i].isscalar = 1;
5774       inst.operands[i].vectype = optype;
5775       inst.operands[i++].present = 1;
5776
5777       if (skip_past_comma (&ptr) == FAIL)
5778         goto wanted_comma;
5779
5780       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5781         goto wanted_arm;
5782
5783       inst.operands[i].reg = val;
5784       inst.operands[i].isreg = 1;
5785       inst.operands[i].present = 1;
5786     }
5787   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
5788            != FAIL)
5789     {
5790       /* Cases 0, 1, 2, 3, 5 (D only).  */
5791       if (skip_past_comma (&ptr) == FAIL)
5792         goto wanted_comma;
5793
5794       inst.operands[i].reg = val;
5795       inst.operands[i].isreg = 1;
5796       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5797       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5798       inst.operands[i].isvec = 1;
5799       inst.operands[i].vectype = optype;
5800       inst.operands[i++].present = 1;
5801
5802       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5803         {
5804           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
5805              Case 13: VMOV <Sd>, <Rm>  */
5806           inst.operands[i].reg = val;
5807           inst.operands[i].isreg = 1;
5808           inst.operands[i].present = 1;
5809
5810           if (rtype == REG_TYPE_NQ)
5811             {
5812               first_error (_("can't use Neon quad register here"));
5813               return FAIL;
5814             }
5815           else if (rtype != REG_TYPE_VFS)
5816             {
5817               i++;
5818               if (skip_past_comma (&ptr) == FAIL)
5819                 goto wanted_comma;
5820               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5821                 goto wanted_arm;
5822               inst.operands[i].reg = val;
5823               inst.operands[i].isreg = 1;
5824               inst.operands[i].present = 1;
5825             }
5826         }
5827       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
5828                                            &optype)) != FAIL)
5829         {
5830           /* Case 0: VMOV<c><q> <Qd>, <Qm>
5831              Case 1: VMOV<c><q> <Dd>, <Dm>
5832              Case 8: VMOV.F32 <Sd>, <Sm>
5833              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
5834
5835           inst.operands[i].reg = val;
5836           inst.operands[i].isreg = 1;
5837           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5838           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5839           inst.operands[i].isvec = 1;
5840           inst.operands[i].vectype = optype;
5841           inst.operands[i].present = 1;
5842
5843           if (skip_past_comma (&ptr) == SUCCESS)
5844             {
5845               /* Case 15.  */
5846               i++;
5847
5848               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5849                 goto wanted_arm;
5850
5851               inst.operands[i].reg = val;
5852               inst.operands[i].isreg = 1;
5853               inst.operands[i++].present = 1;
5854
5855               if (skip_past_comma (&ptr) == FAIL)
5856                 goto wanted_comma;
5857
5858               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5859                 goto wanted_arm;
5860
5861               inst.operands[i].reg = val;
5862               inst.operands[i].isreg = 1;
5863               inst.operands[i++].present = 1;
5864             }
5865         }
5866       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
5867           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
5868              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
5869              Case 10: VMOV.F32 <Sd>, #<imm>
5870              Case 11: VMOV.F64 <Dd>, #<imm>  */
5871         inst.operands[i].immisfloat = 1;
5872       else if (parse_big_immediate (&ptr, i) == SUCCESS)
5873           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
5874              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
5875         ;
5876       else
5877         {
5878           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
5879           return FAIL;
5880         }
5881     }
5882   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5883     {
5884       /* Cases 6, 7.  */
5885       inst.operands[i].reg = val;
5886       inst.operands[i].isreg = 1;
5887       inst.operands[i++].present = 1;
5888
5889       if (skip_past_comma (&ptr) == FAIL)
5890         goto wanted_comma;
5891
5892       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
5893         {
5894           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
5895           inst.operands[i].reg = val;
5896           inst.operands[i].isscalar = 1;
5897           inst.operands[i].present = 1;
5898           inst.operands[i].vectype = optype;
5899         }
5900       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5901         {
5902           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
5903           inst.operands[i].reg = val;
5904           inst.operands[i].isreg = 1;
5905           inst.operands[i++].present = 1;
5906
5907           if (skip_past_comma (&ptr) == FAIL)
5908             goto wanted_comma;
5909
5910           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
5911               == FAIL)
5912             {
5913               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
5914               return FAIL;
5915             }
5916
5917           inst.operands[i].reg = val;
5918           inst.operands[i].isreg = 1;
5919           inst.operands[i].isvec = 1;
5920           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5921           inst.operands[i].vectype = optype;
5922           inst.operands[i].present = 1;
5923
5924           if (rtype == REG_TYPE_VFS)
5925             {
5926               /* Case 14.  */
5927               i++;
5928               if (skip_past_comma (&ptr) == FAIL)
5929                 goto wanted_comma;
5930               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
5931                                               &optype)) == FAIL)
5932                 {
5933                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
5934                   return FAIL;
5935                 }
5936               inst.operands[i].reg = val;
5937               inst.operands[i].isreg = 1;
5938               inst.operands[i].isvec = 1;
5939               inst.operands[i].issingle = 1;
5940               inst.operands[i].vectype = optype;
5941               inst.operands[i].present = 1;
5942             }
5943         }
5944       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
5945                != FAIL)
5946         {
5947           /* Case 13.  */
5948           inst.operands[i].reg = val;
5949           inst.operands[i].isreg = 1;
5950           inst.operands[i].isvec = 1;
5951           inst.operands[i].issingle = 1;
5952           inst.operands[i].vectype = optype;
5953           inst.operands[i++].present = 1;
5954         }
5955     }
5956   else
5957     {
5958       first_error (_("parse error"));
5959       return FAIL;
5960     }
5961
5962   /* Successfully parsed the operands. Update args.  */
5963   *which_operand = i;
5964   *str = ptr;
5965   return SUCCESS;
5966
5967  wanted_comma:
5968   first_error (_("expected comma"));
5969   return FAIL;
5970
5971  wanted_arm:
5972   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
5973   return FAIL;
5974 }
5975
5976 /* Use this macro when the operand constraints are different
5977    for ARM and THUMB (e.g. ldrd).  */
5978 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
5979         ((arm_operand) | ((thumb_operand) << 16))
5980
5981 /* Matcher codes for parse_operands.  */
5982 enum operand_parse_code
5983 {
5984   OP_stop,      /* end of line */
5985
5986   OP_RR,        /* ARM register */
5987   OP_RRnpc,     /* ARM register, not r15 */
5988   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
5989   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
5990   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback, 
5991                    optional trailing ! */
5992   OP_RRw,       /* ARM register, not r15, optional trailing ! */
5993   OP_RCP,       /* Coprocessor number */
5994   OP_RCN,       /* Coprocessor register */
5995   OP_RF,        /* FPA register */
5996   OP_RVS,       /* VFP single precision register */
5997   OP_RVD,       /* VFP double precision register (0..15) */
5998   OP_RND,       /* Neon double precision register (0..31) */
5999   OP_RNQ,       /* Neon quad precision register */
6000   OP_RVSD,      /* VFP single or double precision register */
6001   OP_RNDQ,      /* Neon double or quad precision register */
6002   OP_RNSDQ,     /* Neon single, double or quad precision register */
6003   OP_RNSC,      /* Neon scalar D[X] */
6004   OP_RVC,       /* VFP control register */
6005   OP_RMF,       /* Maverick F register */
6006   OP_RMD,       /* Maverick D register */
6007   OP_RMFX,      /* Maverick FX register */
6008   OP_RMDX,      /* Maverick DX register */
6009   OP_RMAX,      /* Maverick AX register */
6010   OP_RMDS,      /* Maverick DSPSC register */
6011   OP_RIWR,      /* iWMMXt wR register */
6012   OP_RIWC,      /* iWMMXt wC register */
6013   OP_RIWG,      /* iWMMXt wCG register */
6014   OP_RXA,       /* XScale accumulator register */
6015
6016   OP_REGLST,    /* ARM register list */
6017   OP_VRSLST,    /* VFP single-precision register list */
6018   OP_VRDLST,    /* VFP double-precision register list */
6019   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6020   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6021   OP_NSTRLST,   /* Neon element/structure list */
6022
6023   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6024   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6025   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6026   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6027   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6028   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6029   OP_VMOV,      /* Neon VMOV operands.  */
6030   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6031   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6032   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6033
6034   OP_I0,        /* immediate zero */
6035   OP_I7,        /* immediate value 0 .. 7 */
6036   OP_I15,       /*                 0 .. 15 */
6037   OP_I16,       /*                 1 .. 16 */
6038   OP_I16z,      /*                 0 .. 16 */
6039   OP_I31,       /*                 0 .. 31 */
6040   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6041   OP_I32,       /*                 1 .. 32 */
6042   OP_I32z,      /*                 0 .. 32 */
6043   OP_I63,       /*                 0 .. 63 */
6044   OP_I63s,      /*               -64 .. 63 */
6045   OP_I64,       /*                 1 .. 64 */
6046   OP_I64z,      /*                 0 .. 64 */
6047   OP_I255,      /*                 0 .. 255 */
6048
6049   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6050   OP_I7b,       /*                             0 .. 7 */
6051   OP_I15b,      /*                             0 .. 15 */
6052   OP_I31b,      /*                             0 .. 31 */
6053
6054   OP_SH,        /* shifter operand */
6055   OP_SHG,       /* shifter operand with possible group relocation */
6056   OP_ADDR,      /* Memory address expression (any mode) */
6057   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6058   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6059   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6060   OP_EXP,       /* arbitrary expression */
6061   OP_EXPi,      /* same, with optional immediate prefix */
6062   OP_EXPr,      /* same, with optional relocation suffix */
6063   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6064
6065   OP_CPSF,      /* CPS flags */
6066   OP_ENDI,      /* Endianness specifier */
6067   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6068   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6069   OP_COND,      /* conditional code */
6070   OP_TB,        /* Table branch.  */
6071
6072   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6073
6074   OP_RRnpc_I0,  /* ARM register or literal 0 */
6075   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
6076   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6077   OP_RF_IF,     /* FPA register or immediate */
6078   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6079   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6080
6081   /* Optional operands.  */
6082   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6083   OP_oI31b,      /*                             0 .. 31 */
6084   OP_oI32b,      /*                             1 .. 32 */
6085   OP_oIffffb,    /*                             0 .. 65535 */
6086   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6087
6088   OP_oRR,        /* ARM register */
6089   OP_oRRnpc,     /* ARM register, not the PC */
6090   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6091   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6092   OP_oRND,       /* Optional Neon double precision register */
6093   OP_oRNQ,       /* Optional Neon quad precision register */
6094   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6095   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6096   OP_oSHll,      /* LSL immediate */
6097   OP_oSHar,      /* ASR immediate */
6098   OP_oSHllar,    /* LSL or ASR immediate */
6099   OP_oROR,       /* ROR 0/8/16/24 */
6100   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6101
6102   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6103   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6104   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6105   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6106
6107   OP_FIRST_OPTIONAL = OP_oI7b
6108 };
6109
6110 /* Generic instruction operand parser.  This does no encoding and no
6111    semantic validation; it merely squirrels values away in the inst
6112    structure.  Returns SUCCESS or FAIL depending on whether the
6113    specified grammar matched.  */
6114 static int
6115 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6116 {
6117   unsigned const int *upat = pattern;
6118   char *backtrack_pos = 0;
6119   const char *backtrack_error = 0;
6120   int i, val, backtrack_index = 0;
6121   enum arm_reg_type rtype;
6122   parse_operand_result result;
6123   unsigned int op_parse_code;
6124
6125 #define po_char_or_fail(chr)                    \
6126   do                                            \
6127     {                                           \
6128       if (skip_past_char (&str, chr) == FAIL)   \
6129         goto bad_args;                          \
6130     }                                           \
6131   while (0)
6132
6133 #define po_reg_or_fail(regtype)                                 \
6134   do                                                            \
6135     {                                                           \
6136       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6137                                  & inst.operands[i].vectype);   \
6138       if (val == FAIL)                                          \
6139         {                                                       \
6140           first_error (_(reg_expected_msgs[regtype]));          \
6141           goto failure;                                         \
6142         }                                                       \
6143       inst.operands[i].reg = val;                               \
6144       inst.operands[i].isreg = 1;                               \
6145       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6146       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6147       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6148                              || rtype == REG_TYPE_VFD           \
6149                              || rtype == REG_TYPE_NQ);          \
6150     }                                                           \
6151   while (0)
6152
6153 #define po_reg_or_goto(regtype, label)                          \
6154   do                                                            \
6155     {                                                           \
6156       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6157                                  & inst.operands[i].vectype);   \
6158       if (val == FAIL)                                          \
6159         goto label;                                             \
6160                                                                 \
6161       inst.operands[i].reg = val;                               \
6162       inst.operands[i].isreg = 1;                               \
6163       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6164       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6165       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6166                              || rtype == REG_TYPE_VFD           \
6167                              || rtype == REG_TYPE_NQ);          \
6168     }                                                           \
6169   while (0)
6170
6171 #define po_imm_or_fail(min, max, popt)                          \
6172   do                                                            \
6173     {                                                           \
6174       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6175         goto failure;                                           \
6176       inst.operands[i].imm = val;                               \
6177     }                                                           \
6178   while (0)
6179
6180 #define po_scalar_or_goto(elsz, label)                                  \
6181   do                                                                    \
6182     {                                                                   \
6183       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6184       if (val == FAIL)                                                  \
6185         goto label;                                                     \
6186       inst.operands[i].reg = val;                                       \
6187       inst.operands[i].isscalar = 1;                                    \
6188     }                                                                   \
6189   while (0)
6190
6191 #define po_misc_or_fail(expr)                   \
6192   do                                            \
6193     {                                           \
6194       if (expr)                                 \
6195         goto failure;                           \
6196     }                                           \
6197   while (0)
6198
6199 #define po_misc_or_fail_no_backtrack(expr)              \
6200   do                                                    \
6201     {                                                   \
6202       result = expr;                                    \
6203       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6204         backtrack_pos = 0;                              \
6205       if (result != PARSE_OPERAND_SUCCESS)              \
6206         goto failure;                                   \
6207     }                                                   \
6208   while (0)
6209
6210 #define po_barrier_or_imm(str)                             \
6211   do                                                       \
6212     {                                                      \
6213       val = parse_barrier (&str);                          \
6214       if (val == FAIL)                                     \
6215         {                                                  \
6216           if (ISALPHA (*str))                              \
6217               goto failure;                                \
6218           else                                             \
6219               goto immediate;                              \
6220         }                                                  \
6221       else                                                 \
6222         {                                                  \
6223           if ((inst.instruction & 0xf0) == 0x60            \
6224               && val != 0xf)                               \
6225             {                                              \
6226                /* ISB can only take SY as an option.  */   \
6227                inst.error = _("invalid barrier type");     \
6228                goto failure;                               \
6229             }                                              \
6230         }                                                  \
6231     }                                                      \
6232   while (0)
6233
6234   skip_whitespace (str);
6235
6236   for (i = 0; upat[i] != OP_stop; i++)
6237     {
6238       op_parse_code = upat[i];
6239       if (op_parse_code >= 1<<16)
6240         op_parse_code = thumb ? (op_parse_code >> 16)
6241                                 : (op_parse_code & ((1<<16)-1));
6242
6243       if (op_parse_code >= OP_FIRST_OPTIONAL)
6244         {
6245           /* Remember where we are in case we need to backtrack.  */
6246           gas_assert (!backtrack_pos);
6247           backtrack_pos = str;
6248           backtrack_error = inst.error;
6249           backtrack_index = i;
6250         }
6251
6252       if (i > 0 && (i > 1 || inst.operands[0].present))
6253         po_char_or_fail (',');
6254
6255       switch (op_parse_code)
6256         {
6257           /* Registers */
6258         case OP_oRRnpc:
6259         case OP_oRRnpcsp:
6260         case OP_RRnpc:
6261         case OP_RRnpcsp:
6262         case OP_oRR:
6263         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6264         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6265         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6266         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6267         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6268         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6269         case OP_oRND:
6270         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6271         case OP_RVC:
6272           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6273           break;
6274           /* Also accept generic coprocessor regs for unknown registers.  */
6275           coproc_reg:
6276           po_reg_or_fail (REG_TYPE_CN);
6277           break;
6278         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6279         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6280         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6281         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6282         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6283         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6284         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6285         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6286         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6287         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6288         case OP_oRNQ:
6289         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6290         case OP_oRNDQ:
6291         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6292         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6293         case OP_oRNSDQ:
6294         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6295
6296         /* Neon scalar. Using an element size of 8 means that some invalid
6297            scalars are accepted here, so deal with those in later code.  */
6298         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6299
6300         case OP_RNDQ_I0:
6301           {
6302             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6303             break;
6304             try_imm0:
6305             po_imm_or_fail (0, 0, TRUE);
6306           }
6307           break;
6308
6309         case OP_RVSD_I0:
6310           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6311           break;
6312
6313         case OP_RR_RNSC:
6314           {
6315             po_scalar_or_goto (8, try_rr);
6316             break;
6317             try_rr:
6318             po_reg_or_fail (REG_TYPE_RN);
6319           }
6320           break;
6321
6322         case OP_RNSDQ_RNSC:
6323           {
6324             po_scalar_or_goto (8, try_nsdq);
6325             break;
6326             try_nsdq:
6327             po_reg_or_fail (REG_TYPE_NSDQ);
6328           }
6329           break;
6330
6331         case OP_RNDQ_RNSC:
6332           {
6333             po_scalar_or_goto (8, try_ndq);
6334             break;
6335             try_ndq:
6336             po_reg_or_fail (REG_TYPE_NDQ);
6337           }
6338           break;
6339
6340         case OP_RND_RNSC:
6341           {
6342             po_scalar_or_goto (8, try_vfd);
6343             break;
6344             try_vfd:
6345             po_reg_or_fail (REG_TYPE_VFD);
6346           }
6347           break;
6348
6349         case OP_VMOV:
6350           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6351              not careful then bad things might happen.  */
6352           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6353           break;
6354
6355         case OP_RNDQ_Ibig:
6356           {
6357             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6358             break;
6359             try_immbig:
6360             /* There's a possibility of getting a 64-bit immediate here, so
6361                we need special handling.  */
6362             if (parse_big_immediate (&str, i) == FAIL)
6363               {
6364                 inst.error = _("immediate value is out of range");
6365                 goto failure;
6366               }
6367           }
6368           break;
6369
6370         case OP_RNDQ_I63b:
6371           {
6372             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6373             break;
6374             try_shimm:
6375             po_imm_or_fail (0, 63, TRUE);
6376           }
6377           break;
6378
6379         case OP_RRnpcb:
6380           po_char_or_fail ('[');
6381           po_reg_or_fail  (REG_TYPE_RN);
6382           po_char_or_fail (']');
6383           break;
6384
6385         case OP_RRnpctw:
6386         case OP_RRw:
6387         case OP_oRRw:
6388           po_reg_or_fail (REG_TYPE_RN);
6389           if (skip_past_char (&str, '!') == SUCCESS)
6390             inst.operands[i].writeback = 1;
6391           break;
6392
6393           /* Immediates */
6394         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6395         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6396         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6397         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6398         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6399         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6400         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6401         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6402         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6403         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6404         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6405         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6406
6407         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6408         case OP_oI7b:
6409         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6410         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6411         case OP_oI31b:
6412         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6413         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6414         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6415
6416           /* Immediate variants */
6417         case OP_oI255c:
6418           po_char_or_fail ('{');
6419           po_imm_or_fail (0, 255, TRUE);
6420           po_char_or_fail ('}');
6421           break;
6422
6423         case OP_I31w:
6424           /* The expression parser chokes on a trailing !, so we have
6425              to find it first and zap it.  */
6426           {
6427             char *s = str;
6428             while (*s && *s != ',')
6429               s++;
6430             if (s[-1] == '!')
6431               {
6432                 s[-1] = '\0';
6433                 inst.operands[i].writeback = 1;
6434               }
6435             po_imm_or_fail (0, 31, TRUE);
6436             if (str == s - 1)
6437               str = s;
6438           }
6439           break;
6440
6441           /* Expressions */
6442         case OP_EXPi:   EXPi:
6443           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6444                                               GE_OPT_PREFIX));
6445           break;
6446
6447         case OP_EXP:
6448           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6449                                               GE_NO_PREFIX));
6450           break;
6451
6452         case OP_EXPr:   EXPr:
6453           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6454                                               GE_NO_PREFIX));
6455           if (inst.reloc.exp.X_op == O_symbol)
6456             {
6457               val = parse_reloc (&str);
6458               if (val == -1)
6459                 {
6460                   inst.error = _("unrecognized relocation suffix");
6461                   goto failure;
6462                 }
6463               else if (val != BFD_RELOC_UNUSED)
6464                 {
6465                   inst.operands[i].imm = val;
6466                   inst.operands[i].hasreloc = 1;
6467                 }
6468             }
6469           break;
6470
6471           /* Operand for MOVW or MOVT.  */
6472         case OP_HALF:
6473           po_misc_or_fail (parse_half (&str));
6474           break;
6475
6476           /* Register or expression.  */
6477         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
6478         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
6479
6480           /* Register or immediate.  */
6481         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
6482         I0:               po_imm_or_fail (0, 0, FALSE);       break;
6483
6484         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
6485         IF:
6486           if (!is_immediate_prefix (*str))
6487             goto bad_args;
6488           str++;
6489           val = parse_fpa_immediate (&str);
6490           if (val == FAIL)
6491             goto failure;
6492           /* FPA immediates are encoded as registers 8-15.
6493              parse_fpa_immediate has already applied the offset.  */
6494           inst.operands[i].reg = val;
6495           inst.operands[i].isreg = 1;
6496           break;
6497
6498         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
6499         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
6500
6501           /* Two kinds of register.  */
6502         case OP_RIWR_RIWC:
6503           {
6504             struct reg_entry *rege = arm_reg_parse_multi (&str);
6505             if (!rege
6506                 || (rege->type != REG_TYPE_MMXWR
6507                     && rege->type != REG_TYPE_MMXWC
6508                     && rege->type != REG_TYPE_MMXWCG))
6509               {
6510                 inst.error = _("iWMMXt data or control register expected");
6511                 goto failure;
6512               }
6513             inst.operands[i].reg = rege->number;
6514             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
6515           }
6516           break;
6517
6518         case OP_RIWC_RIWG:
6519           {
6520             struct reg_entry *rege = arm_reg_parse_multi (&str);
6521             if (!rege
6522                 || (rege->type != REG_TYPE_MMXWC
6523                     && rege->type != REG_TYPE_MMXWCG))
6524               {
6525                 inst.error = _("iWMMXt control register expected");
6526                 goto failure;
6527               }
6528             inst.operands[i].reg = rege->number;
6529             inst.operands[i].isreg = 1;
6530           }
6531           break;
6532
6533           /* Misc */
6534         case OP_CPSF:    val = parse_cps_flags (&str);          break;
6535         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
6536         case OP_oROR:    val = parse_ror (&str);                break;
6537         case OP_COND:    val = parse_cond (&str);               break;
6538         case OP_oBARRIER_I15:
6539           po_barrier_or_imm (str); break;
6540           immediate:
6541           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
6542             goto failure;
6543           break;
6544
6545         case OP_wPSR:    
6546         case OP_rPSR:
6547           po_reg_or_goto (REG_TYPE_RNB, try_psr);
6548           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
6549             {
6550               inst.error = _("Banked registers are not available with this "
6551                              "architecture.");
6552               goto failure;
6553             }
6554           break;
6555           try_psr:
6556           val = parse_psr (&str, op_parse_code == OP_wPSR);
6557           break;
6558
6559         case OP_APSR_RR:
6560           po_reg_or_goto (REG_TYPE_RN, try_apsr);
6561           break;
6562           try_apsr:
6563           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
6564              instruction).  */
6565           if (strncasecmp (str, "APSR_", 5) == 0)
6566             {
6567               unsigned found = 0;
6568               str += 5;
6569               while (found < 15)
6570                 switch (*str++)
6571                   {
6572                   case 'c': found = (found & 1) ? 16 : found | 1; break;
6573                   case 'n': found = (found & 2) ? 16 : found | 2; break;
6574                   case 'z': found = (found & 4) ? 16 : found | 4; break;
6575                   case 'v': found = (found & 8) ? 16 : found | 8; break;
6576                   default: found = 16;
6577                   }
6578               if (found != 15)
6579                 goto failure;
6580               inst.operands[i].isvec = 1;
6581               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
6582               inst.operands[i].reg = REG_PC;
6583             }
6584           else
6585             goto failure;
6586           break;
6587
6588         case OP_TB:
6589           po_misc_or_fail (parse_tb (&str));
6590           break;
6591
6592           /* Register lists.  */
6593         case OP_REGLST:
6594           val = parse_reg_list (&str);
6595           if (*str == '^')
6596             {
6597               inst.operands[1].writeback = 1;
6598               str++;
6599             }
6600           break;
6601
6602         case OP_VRSLST:
6603           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
6604           break;
6605
6606         case OP_VRDLST:
6607           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
6608           break;
6609
6610         case OP_VRSDLST:
6611           /* Allow Q registers too.  */
6612           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6613                                     REGLIST_NEON_D);
6614           if (val == FAIL)
6615             {
6616               inst.error = NULL;
6617               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6618                                         REGLIST_VFP_S);
6619               inst.operands[i].issingle = 1;
6620             }
6621           break;
6622
6623         case OP_NRDLST:
6624           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6625                                     REGLIST_NEON_D);
6626           break;
6627
6628         case OP_NSTRLST:
6629           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
6630                                            &inst.operands[i].vectype);
6631           break;
6632
6633           /* Addressing modes */
6634         case OP_ADDR:
6635           po_misc_or_fail (parse_address (&str, i));
6636           break;
6637
6638         case OP_ADDRGLDR:
6639           po_misc_or_fail_no_backtrack (
6640             parse_address_group_reloc (&str, i, GROUP_LDR));
6641           break;
6642
6643         case OP_ADDRGLDRS:
6644           po_misc_or_fail_no_backtrack (
6645             parse_address_group_reloc (&str, i, GROUP_LDRS));
6646           break;
6647
6648         case OP_ADDRGLDC:
6649           po_misc_or_fail_no_backtrack (
6650             parse_address_group_reloc (&str, i, GROUP_LDC));
6651           break;
6652
6653         case OP_SH:
6654           po_misc_or_fail (parse_shifter_operand (&str, i));
6655           break;
6656
6657         case OP_SHG:
6658           po_misc_or_fail_no_backtrack (
6659             parse_shifter_operand_group_reloc (&str, i));
6660           break;
6661
6662         case OP_oSHll:
6663           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
6664           break;
6665
6666         case OP_oSHar:
6667           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
6668           break;
6669
6670         case OP_oSHllar:
6671           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
6672           break;
6673
6674         default:
6675           as_fatal (_("unhandled operand code %d"), op_parse_code);
6676         }
6677
6678       /* Various value-based sanity checks and shared operations.  We
6679          do not signal immediate failures for the register constraints;
6680          this allows a syntax error to take precedence.  */
6681       switch (op_parse_code)
6682         {
6683         case OP_oRRnpc:
6684         case OP_RRnpc:
6685         case OP_RRnpcb:
6686         case OP_RRw:
6687         case OP_oRRw:
6688         case OP_RRnpc_I0:
6689           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
6690             inst.error = BAD_PC;
6691           break;
6692
6693         case OP_oRRnpcsp:
6694         case OP_RRnpcsp:
6695           if (inst.operands[i].isreg)
6696             {
6697               if (inst.operands[i].reg == REG_PC)
6698                 inst.error = BAD_PC;
6699               else if (inst.operands[i].reg == REG_SP)
6700                 inst.error = BAD_SP;
6701             }
6702           break;
6703
6704         case OP_RRnpctw:
6705           if (inst.operands[i].isreg 
6706               && inst.operands[i].reg == REG_PC 
6707               && (inst.operands[i].writeback || thumb))
6708             inst.error = BAD_PC;
6709           break;
6710
6711         case OP_CPSF:
6712         case OP_ENDI:
6713         case OP_oROR:
6714         case OP_wPSR:
6715         case OP_rPSR:
6716         case OP_COND:
6717         case OP_oBARRIER_I15:
6718         case OP_REGLST:
6719         case OP_VRSLST:
6720         case OP_VRDLST:
6721         case OP_VRSDLST:
6722         case OP_NRDLST:
6723         case OP_NSTRLST:
6724           if (val == FAIL)
6725             goto failure;
6726           inst.operands[i].imm = val;
6727           break;
6728
6729         default:
6730           break;
6731         }
6732
6733       /* If we get here, this operand was successfully parsed.  */
6734       inst.operands[i].present = 1;
6735       continue;
6736
6737     bad_args:
6738       inst.error = BAD_ARGS;
6739
6740     failure:
6741       if (!backtrack_pos)
6742         {
6743           /* The parse routine should already have set inst.error, but set a
6744              default here just in case.  */
6745           if (!inst.error)
6746             inst.error = _("syntax error");
6747           return FAIL;
6748         }
6749
6750       /* Do not backtrack over a trailing optional argument that
6751          absorbed some text.  We will only fail again, with the
6752          'garbage following instruction' error message, which is
6753          probably less helpful than the current one.  */
6754       if (backtrack_index == i && backtrack_pos != str
6755           && upat[i+1] == OP_stop)
6756         {
6757           if (!inst.error)
6758             inst.error = _("syntax error");
6759           return FAIL;
6760         }
6761
6762       /* Try again, skipping the optional argument at backtrack_pos.  */
6763       str = backtrack_pos;
6764       inst.error = backtrack_error;
6765       inst.operands[backtrack_index].present = 0;
6766       i = backtrack_index;
6767       backtrack_pos = 0;
6768     }
6769
6770   /* Check that we have parsed all the arguments.  */
6771   if (*str != '\0' && !inst.error)
6772     inst.error = _("garbage following instruction");
6773
6774   return inst.error ? FAIL : SUCCESS;
6775 }
6776
6777 #undef po_char_or_fail
6778 #undef po_reg_or_fail
6779 #undef po_reg_or_goto
6780 #undef po_imm_or_fail
6781 #undef po_scalar_or_fail
6782 #undef po_barrier_or_imm
6783
6784 /* Shorthand macro for instruction encoding functions issuing errors.  */
6785 #define constraint(expr, err)                   \
6786   do                                            \
6787     {                                           \
6788       if (expr)                                 \
6789         {                                       \
6790           inst.error = err;                     \
6791           return;                               \
6792         }                                       \
6793     }                                           \
6794   while (0)
6795
6796 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
6797    instructions are unpredictable if these registers are used.  This
6798    is the BadReg predicate in ARM's Thumb-2 documentation.  */
6799 #define reject_bad_reg(reg)                             \
6800   do                                                    \
6801    if (reg == REG_SP || reg == REG_PC)                  \
6802      {                                                  \
6803        inst.error = (reg == REG_SP) ? BAD_SP : BAD_PC;  \
6804        return;                                          \
6805      }                                                  \
6806   while (0)
6807
6808 /* If REG is R13 (the stack pointer), warn that its use is
6809    deprecated.  */
6810 #define warn_deprecated_sp(reg)                 \
6811   do                                            \
6812     if (warn_on_deprecated && reg == REG_SP)    \
6813        as_warn (_("use of r13 is deprecated")); \
6814   while (0)
6815
6816 /* Functions for operand encoding.  ARM, then Thumb.  */
6817
6818 #define rotate_left(v, n) (v << n | v >> (32 - n))
6819
6820 /* If VAL can be encoded in the immediate field of an ARM instruction,
6821    return the encoded form.  Otherwise, return FAIL.  */
6822
6823 static unsigned int
6824 encode_arm_immediate (unsigned int val)
6825 {
6826   unsigned int a, i;
6827
6828   for (i = 0; i < 32; i += 2)
6829     if ((a = rotate_left (val, i)) <= 0xff)
6830       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
6831
6832   return FAIL;
6833 }
6834
6835 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
6836    return the encoded form.  Otherwise, return FAIL.  */
6837 static unsigned int
6838 encode_thumb32_immediate (unsigned int val)
6839 {
6840   unsigned int a, i;
6841
6842   if (val <= 0xff)
6843     return val;
6844
6845   for (i = 1; i <= 24; i++)
6846     {
6847       a = val >> i;
6848       if ((val & ~(0xff << i)) == 0)
6849         return ((val >> i) & 0x7f) | ((32 - i) << 7);
6850     }
6851
6852   a = val & 0xff;
6853   if (val == ((a << 16) | a))
6854     return 0x100 | a;
6855   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
6856     return 0x300 | a;
6857
6858   a = val & 0xff00;
6859   if (val == ((a << 16) | a))
6860     return 0x200 | (a >> 8);
6861
6862   return FAIL;
6863 }
6864 /* Encode a VFP SP or DP register number into inst.instruction.  */
6865
6866 static void
6867 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
6868 {
6869   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
6870       && reg > 15)
6871     {
6872       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
6873         {
6874           if (thumb_mode)
6875             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
6876                                     fpu_vfp_ext_d32);
6877           else
6878             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
6879                                     fpu_vfp_ext_d32);
6880         }
6881       else
6882         {
6883           first_error (_("D register out of range for selected VFP version"));
6884           return;
6885         }
6886     }
6887
6888   switch (pos)
6889     {
6890     case VFP_REG_Sd:
6891       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
6892       break;
6893
6894     case VFP_REG_Sn:
6895       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
6896       break;
6897
6898     case VFP_REG_Sm:
6899       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
6900       break;
6901
6902     case VFP_REG_Dd:
6903       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
6904       break;
6905
6906     case VFP_REG_Dn:
6907       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
6908       break;
6909
6910     case VFP_REG_Dm:
6911       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
6912       break;
6913
6914     default:
6915       abort ();
6916     }
6917 }
6918
6919 /* Encode a <shift> in an ARM-format instruction.  The immediate,
6920    if any, is handled by md_apply_fix.   */
6921 static void
6922 encode_arm_shift (int i)
6923 {
6924   if (inst.operands[i].shift_kind == SHIFT_RRX)
6925     inst.instruction |= SHIFT_ROR << 5;
6926   else
6927     {
6928       inst.instruction |= inst.operands[i].shift_kind << 5;
6929       if (inst.operands[i].immisreg)
6930         {
6931           inst.instruction |= SHIFT_BY_REG;
6932           inst.instruction |= inst.operands[i].imm << 8;
6933         }
6934       else
6935         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
6936     }
6937 }
6938
6939 static void
6940 encode_arm_shifter_operand (int i)
6941 {
6942   if (inst.operands[i].isreg)
6943     {
6944       inst.instruction |= inst.operands[i].reg;
6945       encode_arm_shift (i);
6946     }
6947   else
6948     inst.instruction |= INST_IMMEDIATE;
6949 }
6950
6951 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
6952 static void
6953 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
6954 {
6955   gas_assert (inst.operands[i].isreg);
6956   inst.instruction |= inst.operands[i].reg << 16;
6957
6958   if (inst.operands[i].preind)
6959     {
6960       if (is_t)
6961         {
6962           inst.error = _("instruction does not accept preindexed addressing");
6963           return;
6964         }
6965       inst.instruction |= PRE_INDEX;
6966       if (inst.operands[i].writeback)
6967         inst.instruction |= WRITE_BACK;
6968
6969     }
6970   else if (inst.operands[i].postind)
6971     {
6972       gas_assert (inst.operands[i].writeback);
6973       if (is_t)
6974         inst.instruction |= WRITE_BACK;
6975     }
6976   else /* unindexed - only for coprocessor */
6977     {
6978       inst.error = _("instruction does not accept unindexed addressing");
6979       return;
6980     }
6981
6982   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
6983       && (((inst.instruction & 0x000f0000) >> 16)
6984           == ((inst.instruction & 0x0000f000) >> 12)))
6985     as_warn ((inst.instruction & LOAD_BIT)
6986              ? _("destination register same as write-back base")
6987              : _("source register same as write-back base"));
6988 }
6989
6990 /* inst.operands[i] was set up by parse_address.  Encode it into an
6991    ARM-format mode 2 load or store instruction.  If is_t is true,
6992    reject forms that cannot be used with a T instruction (i.e. not
6993    post-indexed).  */
6994 static void
6995 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
6996 {
6997   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
6998
6999   encode_arm_addr_mode_common (i, is_t);
7000
7001   if (inst.operands[i].immisreg)
7002     {
7003       constraint ((inst.operands[i].imm == REG_PC
7004                    || (is_pc && inst.operands[i].writeback)),
7005                   BAD_PC_ADDRESSING);
7006       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7007       inst.instruction |= inst.operands[i].imm;
7008       if (!inst.operands[i].negative)
7009         inst.instruction |= INDEX_UP;
7010       if (inst.operands[i].shifted)
7011         {
7012           if (inst.operands[i].shift_kind == SHIFT_RRX)
7013             inst.instruction |= SHIFT_ROR << 5;
7014           else
7015             {
7016               inst.instruction |= inst.operands[i].shift_kind << 5;
7017               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7018             }
7019         }
7020     }
7021   else /* immediate offset in inst.reloc */
7022     {
7023       if (is_pc && !inst.reloc.pc_rel)
7024         {
7025           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7026
7027           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7028              cannot use PC in addressing.
7029              PC cannot be used in writeback addressing, either.  */
7030           constraint ((is_t || inst.operands[i].writeback),
7031                       BAD_PC_ADDRESSING);
7032
7033           /* Use of PC in str is deprecated for ARMv7.  */
7034           if (warn_on_deprecated
7035               && !is_load
7036               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7037             as_warn (_("use of PC in this instruction is deprecated"));
7038         }
7039
7040       if (inst.reloc.type == BFD_RELOC_UNUSED)
7041         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7042     }
7043 }
7044
7045 /* inst.operands[i] was set up by parse_address.  Encode it into an
7046    ARM-format mode 3 load or store instruction.  Reject forms that
7047    cannot be used with such instructions.  If is_t is true, reject
7048    forms that cannot be used with a T instruction (i.e. not
7049    post-indexed).  */
7050 static void
7051 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7052 {
7053   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7054     {
7055       inst.error = _("instruction does not accept scaled register index");
7056       return;
7057     }
7058
7059   encode_arm_addr_mode_common (i, is_t);
7060
7061   if (inst.operands[i].immisreg)
7062     {
7063       constraint ((inst.operands[i].imm == REG_PC
7064                    || inst.operands[i].reg == REG_PC),
7065                   BAD_PC_ADDRESSING);
7066       inst.instruction |= inst.operands[i].imm;
7067       if (!inst.operands[i].negative)
7068         inst.instruction |= INDEX_UP;
7069     }
7070   else /* immediate offset in inst.reloc */
7071     {
7072       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7073                    && inst.operands[i].writeback),
7074                   BAD_PC_WRITEBACK);
7075       inst.instruction |= HWOFFSET_IMM;
7076       if (inst.reloc.type == BFD_RELOC_UNUSED)
7077         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7078     }
7079 }
7080
7081 /* inst.operands[i] was set up by parse_address.  Encode it into an
7082    ARM-format instruction.  Reject all forms which cannot be encoded
7083    into a coprocessor load/store instruction.  If wb_ok is false,
7084    reject use of writeback; if unind_ok is false, reject use of
7085    unindexed addressing.  If reloc_override is not 0, use it instead
7086    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
7087    (in which case it is preserved).  */
7088
7089 static int
7090 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
7091 {
7092   inst.instruction |= inst.operands[i].reg << 16;
7093
7094   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
7095
7096   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
7097     {
7098       gas_assert (!inst.operands[i].writeback);
7099       if (!unind_ok)
7100         {
7101           inst.error = _("instruction does not support unindexed addressing");
7102           return FAIL;
7103         }
7104       inst.instruction |= inst.operands[i].imm;
7105       inst.instruction |= INDEX_UP;
7106       return SUCCESS;
7107     }
7108
7109   if (inst.operands[i].preind)
7110     inst.instruction |= PRE_INDEX;
7111
7112   if (inst.operands[i].writeback)
7113     {
7114       if (inst.operands[i].reg == REG_PC)
7115         {
7116           inst.error = _("pc may not be used with write-back");
7117           return FAIL;
7118         }
7119       if (!wb_ok)
7120         {
7121           inst.error = _("instruction does not support writeback");
7122           return FAIL;
7123         }
7124       inst.instruction |= WRITE_BACK;
7125     }
7126
7127   if (reloc_override)
7128     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
7129   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
7130             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
7131            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
7132     {
7133       if (thumb_mode)
7134         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
7135       else
7136         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
7137     }
7138
7139   return SUCCESS;
7140 }
7141
7142 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7143    Determine whether it can be performed with a move instruction; if
7144    it can, convert inst.instruction to that move instruction and
7145    return TRUE; if it can't, convert inst.instruction to a literal-pool
7146    load and return FALSE.  If this is not a valid thing to do in the
7147    current context, set inst.error and return TRUE.
7148
7149    inst.operands[i] describes the destination register.  */
7150
7151 static bfd_boolean
7152 move_or_literal_pool (int i, bfd_boolean thumb_p, bfd_boolean mode_3)
7153 {
7154   unsigned long tbit;
7155
7156   if (thumb_p)
7157     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7158   else
7159     tbit = LOAD_BIT;
7160
7161   if ((inst.instruction & tbit) == 0)
7162     {
7163       inst.error = _("invalid pseudo operation");
7164       return TRUE;
7165     }
7166   if (inst.reloc.exp.X_op != O_constant && inst.reloc.exp.X_op != O_symbol)
7167     {
7168       inst.error = _("constant expression expected");
7169       return TRUE;
7170     }
7171   if (inst.reloc.exp.X_op == O_constant)
7172     {
7173       if (thumb_p)
7174         {
7175           if (!unified_syntax && (inst.reloc.exp.X_add_number & ~0xFF) == 0)
7176             {
7177               /* This can be done with a mov(1) instruction.  */
7178               inst.instruction  = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
7179               inst.instruction |= inst.reloc.exp.X_add_number;
7180               return TRUE;
7181             }
7182         }
7183       else
7184         {
7185           int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
7186           if (value != FAIL)
7187             {
7188               /* This can be done with a mov instruction.  */
7189               inst.instruction &= LITERAL_MASK;
7190               inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
7191               inst.instruction |= value & 0xfff;
7192               return TRUE;
7193             }
7194
7195           value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
7196           if (value != FAIL)
7197             {
7198               /* This can be done with a mvn instruction.  */
7199               inst.instruction &= LITERAL_MASK;
7200               inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
7201               inst.instruction |= value & 0xfff;
7202               return TRUE;
7203             }
7204         }
7205     }
7206
7207   if (add_to_lit_pool () == FAIL)
7208     {
7209       inst.error = _("literal pool insertion failed");
7210       return TRUE;
7211     }
7212   inst.operands[1].reg = REG_PC;
7213   inst.operands[1].isreg = 1;
7214   inst.operands[1].preind = 1;
7215   inst.reloc.pc_rel = 1;
7216   inst.reloc.type = (thumb_p
7217                      ? BFD_RELOC_ARM_THUMB_OFFSET
7218                      : (mode_3
7219                         ? BFD_RELOC_ARM_HWLITERAL
7220                         : BFD_RELOC_ARM_LITERAL));
7221   return FALSE;
7222 }
7223
7224 /* Functions for instruction encoding, sorted by sub-architecture.
7225    First some generics; their names are taken from the conventional
7226    bit positions for register arguments in ARM format instructions.  */
7227
7228 static void
7229 do_noargs (void)
7230 {
7231 }
7232
7233 static void
7234 do_rd (void)
7235 {
7236   inst.instruction |= inst.operands[0].reg << 12;
7237 }
7238
7239 static void
7240 do_rd_rm (void)
7241 {
7242   inst.instruction |= inst.operands[0].reg << 12;
7243   inst.instruction |= inst.operands[1].reg;
7244 }
7245
7246 static void
7247 do_rd_rn (void)
7248 {
7249   inst.instruction |= inst.operands[0].reg << 12;
7250   inst.instruction |= inst.operands[1].reg << 16;
7251 }
7252
7253 static void
7254 do_rn_rd (void)
7255 {
7256   inst.instruction |= inst.operands[0].reg << 16;
7257   inst.instruction |= inst.operands[1].reg << 12;
7258 }
7259
7260 static void
7261 do_rd_rm_rn (void)
7262 {
7263   unsigned Rn = inst.operands[2].reg;
7264   /* Enforce restrictions on SWP instruction.  */
7265   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
7266     {
7267       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
7268                   _("Rn must not overlap other operands"));
7269
7270       /* SWP{b} is deprecated for ARMv6* and ARMv7.  */
7271       if (warn_on_deprecated
7272           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
7273         as_warn (_("swp{b} use is deprecated for this architecture"));
7274
7275     }
7276   inst.instruction |= inst.operands[0].reg << 12;
7277   inst.instruction |= inst.operands[1].reg;
7278   inst.instruction |= Rn << 16;
7279 }
7280
7281 static void
7282 do_rd_rn_rm (void)
7283 {
7284   inst.instruction |= inst.operands[0].reg << 12;
7285   inst.instruction |= inst.operands[1].reg << 16;
7286   inst.instruction |= inst.operands[2].reg;
7287 }
7288
7289 static void
7290 do_rm_rd_rn (void)
7291 {
7292   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
7293   constraint (((inst.reloc.exp.X_op != O_constant
7294                 && inst.reloc.exp.X_op != O_illegal)
7295                || inst.reloc.exp.X_add_number != 0),
7296               BAD_ADDR_MODE);
7297   inst.instruction |= inst.operands[0].reg;
7298   inst.instruction |= inst.operands[1].reg << 12;
7299   inst.instruction |= inst.operands[2].reg << 16;
7300 }
7301
7302 static void
7303 do_imm0 (void)
7304 {
7305   inst.instruction |= inst.operands[0].imm;
7306 }
7307
7308 static void
7309 do_rd_cpaddr (void)
7310 {
7311   inst.instruction |= inst.operands[0].reg << 12;
7312   encode_arm_cp_address (1, TRUE, TRUE, 0);
7313 }
7314
7315 /* ARM instructions, in alphabetical order by function name (except
7316    that wrapper functions appear immediately after the function they
7317    wrap).  */
7318
7319 /* This is a pseudo-op of the form "adr rd, label" to be converted
7320    into a relative address of the form "add rd, pc, #label-.-8".  */
7321
7322 static void
7323 do_adr (void)
7324 {
7325   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
7326
7327   /* Frag hacking will turn this into a sub instruction if the offset turns
7328      out to be negative.  */
7329   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
7330   inst.reloc.pc_rel = 1;
7331   inst.reloc.exp.X_add_number -= 8;
7332 }
7333
7334 /* This is a pseudo-op of the form "adrl rd, label" to be converted
7335    into a relative address of the form:
7336    add rd, pc, #low(label-.-8)"
7337    add rd, rd, #high(label-.-8)"  */
7338
7339 static void
7340 do_adrl (void)
7341 {
7342   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
7343
7344   /* Frag hacking will turn this into a sub instruction if the offset turns
7345      out to be negative.  */
7346   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
7347   inst.reloc.pc_rel            = 1;
7348   inst.size                    = INSN_SIZE * 2;
7349   inst.reloc.exp.X_add_number -= 8;
7350 }
7351
7352 static void
7353 do_arit (void)
7354 {
7355   if (!inst.operands[1].present)
7356     inst.operands[1].reg = inst.operands[0].reg;
7357   inst.instruction |= inst.operands[0].reg << 12;
7358   inst.instruction |= inst.operands[1].reg << 16;
7359   encode_arm_shifter_operand (2);
7360 }
7361
7362 static void
7363 do_barrier (void)
7364 {
7365   if (inst.operands[0].present)
7366     {
7367       constraint ((inst.instruction & 0xf0) != 0x40
7368                   && inst.operands[0].imm > 0xf
7369                   && inst.operands[0].imm < 0x0,
7370                   _("bad barrier type"));
7371       inst.instruction |= inst.operands[0].imm;
7372     }
7373   else
7374     inst.instruction |= 0xf;
7375 }
7376
7377 static void
7378 do_bfc (void)
7379 {
7380   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
7381   constraint (msb > 32, _("bit-field extends past end of register"));
7382   /* The instruction encoding stores the LSB and MSB,
7383      not the LSB and width.  */
7384   inst.instruction |= inst.operands[0].reg << 12;
7385   inst.instruction |= inst.operands[1].imm << 7;
7386   inst.instruction |= (msb - 1) << 16;
7387 }
7388
7389 static void
7390 do_bfi (void)
7391 {
7392   unsigned int msb;
7393
7394   /* #0 in second position is alternative syntax for bfc, which is
7395      the same instruction but with REG_PC in the Rm field.  */
7396   if (!inst.operands[1].isreg)
7397     inst.operands[1].reg = REG_PC;
7398
7399   msb = inst.operands[2].imm + inst.operands[3].imm;
7400   constraint (msb > 32, _("bit-field extends past end of register"));
7401   /* The instruction encoding stores the LSB and MSB,
7402      not the LSB and width.  */
7403   inst.instruction |= inst.operands[0].reg << 12;
7404   inst.instruction |= inst.operands[1].reg;
7405   inst.instruction |= inst.operands[2].imm << 7;
7406   inst.instruction |= (msb - 1) << 16;
7407 }
7408
7409 static void
7410 do_bfx (void)
7411 {
7412   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
7413               _("bit-field extends past end of register"));
7414   inst.instruction |= inst.operands[0].reg << 12;
7415   inst.instruction |= inst.operands[1].reg;
7416   inst.instruction |= inst.operands[2].imm << 7;
7417   inst.instruction |= (inst.operands[3].imm - 1) << 16;
7418 }
7419
7420 /* ARM V5 breakpoint instruction (argument parse)
7421      BKPT <16 bit unsigned immediate>
7422      Instruction is not conditional.
7423         The bit pattern given in insns[] has the COND_ALWAYS condition,
7424         and it is an error if the caller tried to override that.  */
7425
7426 static void
7427 do_bkpt (void)
7428 {
7429   /* Top 12 of 16 bits to bits 19:8.  */
7430   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
7431
7432   /* Bottom 4 of 16 bits to bits 3:0.  */
7433   inst.instruction |= inst.operands[0].imm & 0xf;
7434 }
7435
7436 static void
7437 encode_branch (int default_reloc)
7438 {
7439   if (inst.operands[0].hasreloc)
7440     {
7441       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
7442                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
7443                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
7444       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
7445         ? BFD_RELOC_ARM_PLT32
7446         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
7447     }
7448   else
7449     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
7450   inst.reloc.pc_rel = 1;
7451 }
7452
7453 static void
7454 do_branch (void)
7455 {
7456 #ifdef OBJ_ELF
7457   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
7458     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
7459   else
7460 #endif
7461     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
7462 }
7463
7464 static void
7465 do_bl (void)
7466 {
7467 #ifdef OBJ_ELF
7468   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
7469     {
7470       if (inst.cond == COND_ALWAYS)
7471         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
7472       else
7473         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
7474     }
7475   else
7476 #endif
7477     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
7478 }
7479
7480 /* ARM V5 branch-link-exchange instruction (argument parse)
7481      BLX <target_addr>          ie BLX(1)
7482      BLX{<condition>} <Rm>      ie BLX(2)
7483    Unfortunately, there are two different opcodes for this mnemonic.
7484    So, the insns[].value is not used, and the code here zaps values
7485         into inst.instruction.
7486    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
7487
7488 static void
7489 do_blx (void)
7490 {
7491   if (inst.operands[0].isreg)
7492     {
7493       /* Arg is a register; the opcode provided by insns[] is correct.
7494          It is not illegal to do "blx pc", just useless.  */
7495       if (inst.operands[0].reg == REG_PC)
7496         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
7497
7498       inst.instruction |= inst.operands[0].reg;
7499     }
7500   else
7501     {
7502       /* Arg is an address; this instruction cannot be executed
7503          conditionally, and the opcode must be adjusted.
7504          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
7505          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
7506       constraint (inst.cond != COND_ALWAYS, BAD_COND);
7507       inst.instruction = 0xfa000000;
7508       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
7509     }
7510 }
7511
7512 static void
7513 do_bx (void)
7514 {
7515   bfd_boolean want_reloc;
7516
7517   if (inst.operands[0].reg == REG_PC)
7518     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
7519
7520   inst.instruction |= inst.operands[0].reg;
7521   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
7522      it is for ARMv4t or earlier.  */
7523   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
7524   if (object_arch && !ARM_CPU_HAS_FEATURE (*object_arch, arm_ext_v5))
7525       want_reloc = TRUE;
7526
7527 #ifdef OBJ_ELF
7528   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
7529 #endif
7530     want_reloc = FALSE;
7531
7532   if (want_reloc)
7533     inst.reloc.type = BFD_RELOC_ARM_V4BX;
7534 }
7535
7536
7537 /* ARM v5TEJ.  Jump to Jazelle code.  */
7538
7539 static void
7540 do_bxj (void)
7541 {
7542   if (inst.operands[0].reg == REG_PC)
7543     as_tsktsk (_("use of r15 in bxj is not really useful"));
7544
7545   inst.instruction |= inst.operands[0].reg;
7546 }
7547
7548 /* Co-processor data operation:
7549       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
7550       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
7551 static void
7552 do_cdp (void)
7553 {
7554   inst.instruction |= inst.operands[0].reg << 8;
7555   inst.instruction |= inst.operands[1].imm << 20;
7556   inst.instruction |= inst.operands[2].reg << 12;
7557   inst.instruction |= inst.operands[3].reg << 16;
7558   inst.instruction |= inst.operands[4].reg;
7559   inst.instruction |= inst.operands[5].imm << 5;
7560 }
7561
7562 static void
7563 do_cmp (void)
7564 {
7565   inst.instruction |= inst.operands[0].reg << 16;
7566   encode_arm_shifter_operand (1);
7567 }
7568
7569 /* Transfer between coprocessor and ARM registers.
7570    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
7571    MRC2
7572    MCR{cond}
7573    MCR2
7574
7575    No special properties.  */
7576
7577 static void
7578 do_co_reg (void)
7579 {
7580   unsigned Rd;
7581
7582   Rd = inst.operands[2].reg;
7583   if (thumb_mode)
7584     {
7585       if (inst.instruction == 0xee000010
7586           || inst.instruction == 0xfe000010)
7587         /* MCR, MCR2  */
7588         reject_bad_reg (Rd);
7589       else
7590         /* MRC, MRC2  */
7591         constraint (Rd == REG_SP, BAD_SP);
7592     }
7593   else
7594     {
7595       /* MCR */
7596       if (inst.instruction == 0xe000010)
7597         constraint (Rd == REG_PC, BAD_PC);
7598     }
7599
7600
7601   inst.instruction |= inst.operands[0].reg << 8;
7602   inst.instruction |= inst.operands[1].imm << 21;
7603   inst.instruction |= Rd << 12;
7604   inst.instruction |= inst.operands[3].reg << 16;
7605   inst.instruction |= inst.operands[4].reg;
7606   inst.instruction |= inst.operands[5].imm << 5;
7607 }
7608
7609 /* Transfer between coprocessor register and pair of ARM registers.
7610    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
7611    MCRR2
7612    MRRC{cond}
7613    MRRC2
7614
7615    Two XScale instructions are special cases of these:
7616
7617      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
7618      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
7619
7620    Result unpredictable if Rd or Rn is R15.  */
7621
7622 static void
7623 do_co_reg2c (void)
7624 {
7625   unsigned Rd, Rn;
7626
7627   Rd = inst.operands[2].reg;
7628   Rn = inst.operands[3].reg;
7629
7630   if (thumb_mode)
7631     {
7632       reject_bad_reg (Rd);
7633       reject_bad_reg (Rn);
7634     }
7635   else
7636     {
7637       constraint (Rd == REG_PC, BAD_PC);
7638       constraint (Rn == REG_PC, BAD_PC);
7639     }
7640
7641   inst.instruction |= inst.operands[0].reg << 8;
7642   inst.instruction |= inst.operands[1].imm << 4;
7643   inst.instruction |= Rd << 12;
7644   inst.instruction |= Rn << 16;
7645   inst.instruction |= inst.operands[4].reg;
7646 }
7647
7648 static void
7649 do_cpsi (void)
7650 {
7651   inst.instruction |= inst.operands[0].imm << 6;
7652   if (inst.operands[1].present)
7653     {
7654       inst.instruction |= CPSI_MMOD;
7655       inst.instruction |= inst.operands[1].imm;
7656     }
7657 }
7658
7659 static void
7660 do_dbg (void)
7661 {
7662   inst.instruction |= inst.operands[0].imm;
7663 }
7664
7665 static void
7666 do_div (void)
7667 {
7668   unsigned Rd, Rn, Rm;
7669
7670   Rd = inst.operands[0].reg;
7671   Rn = (inst.operands[1].present
7672         ? inst.operands[1].reg : Rd);
7673   Rm = inst.operands[2].reg;
7674
7675   constraint ((Rd == REG_PC), BAD_PC);
7676   constraint ((Rn == REG_PC), BAD_PC);
7677   constraint ((Rm == REG_PC), BAD_PC);
7678
7679   inst.instruction |= Rd << 16;
7680   inst.instruction |= Rn << 0;
7681   inst.instruction |= Rm << 8;
7682 }
7683
7684 static void
7685 do_it (void)
7686 {
7687   /* There is no IT instruction in ARM mode.  We
7688      process it to do the validation as if in
7689      thumb mode, just in case the code gets
7690      assembled for thumb using the unified syntax.  */
7691
7692   inst.size = 0;
7693   if (unified_syntax)
7694     {
7695       set_it_insn_type (IT_INSN);
7696       now_it.mask = (inst.instruction & 0xf) | 0x10;
7697       now_it.cc = inst.operands[0].imm;
7698     }
7699 }
7700
7701 static void
7702 do_ldmstm (void)
7703 {
7704   int base_reg = inst.operands[0].reg;
7705   int range = inst.operands[1].imm;
7706
7707   inst.instruction |= base_reg << 16;
7708   inst.instruction |= range;
7709
7710   if (inst.operands[1].writeback)
7711     inst.instruction |= LDM_TYPE_2_OR_3;
7712
7713   if (inst.operands[0].writeback)
7714     {
7715       inst.instruction |= WRITE_BACK;
7716       /* Check for unpredictable uses of writeback.  */
7717       if (inst.instruction & LOAD_BIT)
7718         {
7719           /* Not allowed in LDM type 2.  */
7720           if ((inst.instruction & LDM_TYPE_2_OR_3)
7721               && ((range & (1 << REG_PC)) == 0))
7722             as_warn (_("writeback of base register is UNPREDICTABLE"));
7723           /* Only allowed if base reg not in list for other types.  */
7724           else if (range & (1 << base_reg))
7725             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
7726         }
7727       else /* STM.  */
7728         {
7729           /* Not allowed for type 2.  */
7730           if (inst.instruction & LDM_TYPE_2_OR_3)
7731             as_warn (_("writeback of base register is UNPREDICTABLE"));
7732           /* Only allowed if base reg not in list, or first in list.  */
7733           else if ((range & (1 << base_reg))
7734                    && (range & ((1 << base_reg) - 1)))
7735             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
7736         }
7737     }
7738 }
7739
7740 /* ARMv5TE load-consecutive (argument parse)
7741    Mode is like LDRH.
7742
7743      LDRccD R, mode
7744      STRccD R, mode.  */
7745
7746 static void
7747 do_ldrd (void)
7748 {
7749   constraint (inst.operands[0].reg % 2 != 0,
7750               _("first destination register must be even"));
7751   constraint (inst.operands[1].present
7752               && inst.operands[1].reg != inst.operands[0].reg + 1,
7753               _("can only load two consecutive registers"));
7754   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
7755   constraint (!inst.operands[2].isreg, _("'[' expected"));
7756
7757   if (!inst.operands[1].present)
7758     inst.operands[1].reg = inst.operands[0].reg + 1;
7759
7760   if (inst.instruction & LOAD_BIT)
7761     {
7762       /* encode_arm_addr_mode_3 will diagnose overlap between the base
7763          register and the first register written; we have to diagnose
7764          overlap between the base and the second register written here.  */
7765
7766       if (inst.operands[2].reg == inst.operands[1].reg
7767           && (inst.operands[2].writeback || inst.operands[2].postind))
7768         as_warn (_("base register written back, and overlaps "
7769                    "second destination register"));
7770
7771       /* For an index-register load, the index register must not overlap the
7772          destination (even if not write-back).  */
7773       else if (inst.operands[2].immisreg
7774                && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
7775                    || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
7776         as_warn (_("index register overlaps destination register"));
7777     }
7778
7779   inst.instruction |= inst.operands[0].reg << 12;
7780   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
7781 }
7782
7783 static void
7784 do_ldrex (void)
7785 {
7786   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
7787               || inst.operands[1].postind || inst.operands[1].writeback
7788               || inst.operands[1].immisreg || inst.operands[1].shifted
7789               || inst.operands[1].negative
7790               /* This can arise if the programmer has written
7791                    strex rN, rM, foo
7792                  or if they have mistakenly used a register name as the last
7793                  operand,  eg:
7794                    strex rN, rM, rX
7795                  It is very difficult to distinguish between these two cases
7796                  because "rX" might actually be a label. ie the register
7797                  name has been occluded by a symbol of the same name. So we
7798                  just generate a general 'bad addressing mode' type error
7799                  message and leave it up to the programmer to discover the
7800                  true cause and fix their mistake.  */
7801               || (inst.operands[1].reg == REG_PC),
7802               BAD_ADDR_MODE);
7803
7804   constraint (inst.reloc.exp.X_op != O_constant
7805               || inst.reloc.exp.X_add_number != 0,
7806               _("offset must be zero in ARM encoding"));
7807
7808   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
7809
7810   inst.instruction |= inst.operands[0].reg << 12;
7811   inst.instruction |= inst.operands[1].reg << 16;
7812   inst.reloc.type = BFD_RELOC_UNUSED;
7813 }
7814
7815 static void
7816 do_ldrexd (void)
7817 {
7818   constraint (inst.operands[0].reg % 2 != 0,
7819               _("even register required"));
7820   constraint (inst.operands[1].present
7821               && inst.operands[1].reg != inst.operands[0].reg + 1,
7822               _("can only load two consecutive registers"));
7823   /* If op 1 were present and equal to PC, this function wouldn't
7824      have been called in the first place.  */
7825   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
7826
7827   inst.instruction |= inst.operands[0].reg << 12;
7828   inst.instruction |= inst.operands[2].reg << 16;
7829 }
7830
7831 static void
7832 do_ldst (void)
7833 {
7834   inst.instruction |= inst.operands[0].reg << 12;
7835   if (!inst.operands[1].isreg)
7836     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/FALSE))
7837       return;
7838   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
7839 }
7840
7841 static void
7842 do_ldstt (void)
7843 {
7844   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
7845      reject [Rn,...].  */
7846   if (inst.operands[1].preind)
7847     {
7848       constraint (inst.reloc.exp.X_op != O_constant
7849                   || inst.reloc.exp.X_add_number != 0,
7850                   _("this instruction requires a post-indexed address"));
7851
7852       inst.operands[1].preind = 0;
7853       inst.operands[1].postind = 1;
7854       inst.operands[1].writeback = 1;
7855     }
7856   inst.instruction |= inst.operands[0].reg << 12;
7857   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
7858 }
7859
7860 /* Halfword and signed-byte load/store operations.  */
7861
7862 static void
7863 do_ldstv4 (void)
7864 {
7865   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
7866   inst.instruction |= inst.operands[0].reg << 12;
7867   if (!inst.operands[1].isreg)
7868     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/TRUE))
7869       return;
7870   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
7871 }
7872
7873 static void
7874 do_ldsttv4 (void)
7875 {
7876   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
7877      reject [Rn,...].  */
7878   if (inst.operands[1].preind)
7879     {
7880       constraint (inst.reloc.exp.X_op != O_constant
7881                   || inst.reloc.exp.X_add_number != 0,
7882                   _("this instruction requires a post-indexed address"));
7883
7884       inst.operands[1].preind = 0;
7885       inst.operands[1].postind = 1;
7886       inst.operands[1].writeback = 1;
7887     }
7888   inst.instruction |= inst.operands[0].reg << 12;
7889   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
7890 }
7891
7892 /* Co-processor register load/store.
7893    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
7894 static void
7895 do_lstc (void)
7896 {
7897   inst.instruction |= inst.operands[0].reg << 8;
7898   inst.instruction |= inst.operands[1].reg << 12;
7899   encode_arm_cp_address (2, TRUE, TRUE, 0);
7900 }
7901
7902 static void
7903 do_mlas (void)
7904 {
7905   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
7906   if (inst.operands[0].reg == inst.operands[1].reg
7907       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
7908       && !(inst.instruction & 0x00400000))
7909     as_tsktsk (_("Rd and Rm should be different in mla"));
7910
7911   inst.instruction |= inst.operands[0].reg << 16;
7912   inst.instruction |= inst.operands[1].reg;
7913   inst.instruction |= inst.operands[2].reg << 8;
7914   inst.instruction |= inst.operands[3].reg << 12;
7915 }
7916
7917 static void
7918 do_mov (void)
7919 {
7920   inst.instruction |= inst.operands[0].reg << 12;
7921   encode_arm_shifter_operand (1);
7922 }
7923
7924 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
7925 static void
7926 do_mov16 (void)
7927 {
7928   bfd_vma imm;
7929   bfd_boolean top;
7930
7931   top = (inst.instruction & 0x00400000) != 0;
7932   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
7933               _(":lower16: not allowed this instruction"));
7934   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
7935               _(":upper16: not allowed instruction"));
7936   inst.instruction |= inst.operands[0].reg << 12;
7937   if (inst.reloc.type == BFD_RELOC_UNUSED)
7938     {
7939       imm = inst.reloc.exp.X_add_number;
7940       /* The value is in two pieces: 0:11, 16:19.  */
7941       inst.instruction |= (imm & 0x00000fff);
7942       inst.instruction |= (imm & 0x0000f000) << 4;
7943     }
7944 }
7945
7946 static void do_vfp_nsyn_opcode (const char *);
7947
7948 static int
7949 do_vfp_nsyn_mrs (void)
7950 {
7951   if (inst.operands[0].isvec)
7952     {
7953       if (inst.operands[1].reg != 1)
7954         first_error (_("operand 1 must be FPSCR"));
7955       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
7956       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
7957       do_vfp_nsyn_opcode ("fmstat");
7958     }
7959   else if (inst.operands[1].isvec)
7960     do_vfp_nsyn_opcode ("fmrx");
7961   else
7962     return FAIL;
7963
7964   return SUCCESS;
7965 }
7966
7967 static int
7968 do_vfp_nsyn_msr (void)
7969 {
7970   if (inst.operands[0].isvec)
7971     do_vfp_nsyn_opcode ("fmxr");
7972   else
7973     return FAIL;
7974
7975   return SUCCESS;
7976 }
7977
7978 static void
7979 do_vmrs (void)
7980 {
7981   unsigned Rt = inst.operands[0].reg;
7982   
7983   if (thumb_mode && inst.operands[0].reg == REG_SP)
7984     {
7985       inst.error = BAD_SP;
7986       return;
7987     }
7988
7989   /* APSR_ sets isvec. All other refs to PC are illegal.  */
7990   if (!inst.operands[0].isvec && inst.operands[0].reg == REG_PC)
7991     {
7992       inst.error = BAD_PC;
7993       return;
7994     }
7995
7996   if (inst.operands[1].reg != 1)
7997     first_error (_("operand 1 must be FPSCR"));
7998
7999   inst.instruction |= (Rt << 12);
8000 }
8001
8002 static void
8003 do_vmsr (void)
8004 {
8005   unsigned Rt = inst.operands[1].reg;
8006   
8007   if (thumb_mode)
8008     reject_bad_reg (Rt);
8009   else if (Rt == REG_PC)
8010     {
8011       inst.error = BAD_PC;
8012       return;
8013     }
8014
8015   if (inst.operands[0].reg != 1)
8016     first_error (_("operand 0 must be FPSCR"));
8017
8018   inst.instruction |= (Rt << 12);
8019 }
8020
8021 static void
8022 do_mrs (void)
8023 {
8024   unsigned br;
8025
8026   if (do_vfp_nsyn_mrs () == SUCCESS)
8027     return;
8028
8029   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8030   inst.instruction |= inst.operands[0].reg << 12;
8031
8032   if (inst.operands[1].isreg)
8033     {
8034       br = inst.operands[1].reg;
8035       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf000))
8036         as_bad (_("bad register for mrs"));
8037     }
8038   else
8039     {
8040       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
8041       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
8042                   != (PSR_c|PSR_f),
8043                   _("'APSR', 'CPSR' or 'SPSR' expected"));
8044       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
8045     }
8046
8047   inst.instruction |= br;
8048 }
8049
8050 /* Two possible forms:
8051       "{C|S}PSR_<field>, Rm",
8052       "{C|S}PSR_f, #expression".  */
8053
8054 static void
8055 do_msr (void)
8056 {
8057   if (do_vfp_nsyn_msr () == SUCCESS)
8058     return;
8059
8060   inst.instruction |= inst.operands[0].imm;
8061   if (inst.operands[1].isreg)
8062     inst.instruction |= inst.operands[1].reg;
8063   else
8064     {
8065       inst.instruction |= INST_IMMEDIATE;
8066       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8067       inst.reloc.pc_rel = 0;
8068     }
8069 }
8070
8071 static void
8072 do_mul (void)
8073 {
8074   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
8075
8076   if (!inst.operands[2].present)
8077     inst.operands[2].reg = inst.operands[0].reg;
8078   inst.instruction |= inst.operands[0].reg << 16;
8079   inst.instruction |= inst.operands[1].reg;
8080   inst.instruction |= inst.operands[2].reg << 8;
8081
8082   if (inst.operands[0].reg == inst.operands[1].reg
8083       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8084     as_tsktsk (_("Rd and Rm should be different in mul"));
8085 }
8086
8087 /* Long Multiply Parser
8088    UMULL RdLo, RdHi, Rm, Rs
8089    SMULL RdLo, RdHi, Rm, Rs
8090    UMLAL RdLo, RdHi, Rm, Rs
8091    SMLAL RdLo, RdHi, Rm, Rs.  */
8092
8093 static void
8094 do_mull (void)
8095 {
8096   inst.instruction |= inst.operands[0].reg << 12;
8097   inst.instruction |= inst.operands[1].reg << 16;
8098   inst.instruction |= inst.operands[2].reg;
8099   inst.instruction |= inst.operands[3].reg << 8;
8100
8101   /* rdhi and rdlo must be different.  */
8102   if (inst.operands[0].reg == inst.operands[1].reg)
8103     as_tsktsk (_("rdhi and rdlo must be different"));
8104
8105   /* rdhi, rdlo and rm must all be different before armv6.  */
8106   if ((inst.operands[0].reg == inst.operands[2].reg
8107       || inst.operands[1].reg == inst.operands[2].reg)
8108       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8109     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
8110 }
8111
8112 static void
8113 do_nop (void)
8114 {
8115   if (inst.operands[0].present
8116       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
8117     {
8118       /* Architectural NOP hints are CPSR sets with no bits selected.  */
8119       inst.instruction &= 0xf0000000;
8120       inst.instruction |= 0x0320f000;
8121       if (inst.operands[0].present)
8122         inst.instruction |= inst.operands[0].imm;
8123     }
8124 }
8125
8126 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
8127    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
8128    Condition defaults to COND_ALWAYS.
8129    Error if Rd, Rn or Rm are R15.  */
8130
8131 static void
8132 do_pkhbt (void)
8133 {
8134   inst.instruction |= inst.operands[0].reg << 12;
8135   inst.instruction |= inst.operands[1].reg << 16;
8136   inst.instruction |= inst.operands[2].reg;
8137   if (inst.operands[3].present)
8138     encode_arm_shift (3);
8139 }
8140
8141 /* ARM V6 PKHTB (Argument Parse).  */
8142
8143 static void
8144 do_pkhtb (void)
8145 {
8146   if (!inst.operands[3].present)
8147     {
8148       /* If the shift specifier is omitted, turn the instruction
8149          into pkhbt rd, rm, rn. */
8150       inst.instruction &= 0xfff00010;
8151       inst.instruction |= inst.operands[0].reg << 12;
8152       inst.instruction |= inst.operands[1].reg;
8153       inst.instruction |= inst.operands[2].reg << 16;
8154     }
8155   else
8156     {
8157       inst.instruction |= inst.operands[0].reg << 12;
8158       inst.instruction |= inst.operands[1].reg << 16;
8159       inst.instruction |= inst.operands[2].reg;
8160       encode_arm_shift (3);
8161     }
8162 }
8163
8164 /* ARMv5TE: Preload-Cache
8165    MP Extensions: Preload for write
8166
8167     PLD(W) <addr_mode>
8168
8169   Syntactically, like LDR with B=1, W=0, L=1.  */
8170
8171 static void
8172 do_pld (void)
8173 {
8174   constraint (!inst.operands[0].isreg,
8175               _("'[' expected after PLD mnemonic"));
8176   constraint (inst.operands[0].postind,
8177               _("post-indexed expression used in preload instruction"));
8178   constraint (inst.operands[0].writeback,
8179               _("writeback used in preload instruction"));
8180   constraint (!inst.operands[0].preind,
8181               _("unindexed addressing used in preload instruction"));
8182   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
8183 }
8184
8185 /* ARMv7: PLI <addr_mode>  */
8186 static void
8187 do_pli (void)
8188 {
8189   constraint (!inst.operands[0].isreg,
8190               _("'[' expected after PLI mnemonic"));
8191   constraint (inst.operands[0].postind,
8192               _("post-indexed expression used in preload instruction"));
8193   constraint (inst.operands[0].writeback,
8194               _("writeback used in preload instruction"));
8195   constraint (!inst.operands[0].preind,
8196               _("unindexed addressing used in preload instruction"));
8197   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
8198   inst.instruction &= ~PRE_INDEX;
8199 }
8200
8201 static void
8202 do_push_pop (void)
8203 {
8204   inst.operands[1] = inst.operands[0];
8205   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
8206   inst.operands[0].isreg = 1;
8207   inst.operands[0].writeback = 1;
8208   inst.operands[0].reg = REG_SP;
8209   do_ldmstm ();
8210 }
8211
8212 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
8213    word at the specified address and the following word
8214    respectively.
8215    Unconditionally executed.
8216    Error if Rn is R15.  */
8217
8218 static void
8219 do_rfe (void)
8220 {
8221   inst.instruction |= inst.operands[0].reg << 16;
8222   if (inst.operands[0].writeback)
8223     inst.instruction |= WRITE_BACK;
8224 }
8225
8226 /* ARM V6 ssat (argument parse).  */
8227
8228 static void
8229 do_ssat (void)
8230 {
8231   inst.instruction |= inst.operands[0].reg << 12;
8232   inst.instruction |= (inst.operands[1].imm - 1) << 16;
8233   inst.instruction |= inst.operands[2].reg;
8234
8235   if (inst.operands[3].present)
8236     encode_arm_shift (3);
8237 }
8238
8239 /* ARM V6 usat (argument parse).  */
8240
8241 static void
8242 do_usat (void)
8243 {
8244   inst.instruction |= inst.operands[0].reg << 12;
8245   inst.instruction |= inst.operands[1].imm << 16;
8246   inst.instruction |= inst.operands[2].reg;
8247
8248   if (inst.operands[3].present)
8249     encode_arm_shift (3);
8250 }
8251
8252 /* ARM V6 ssat16 (argument parse).  */
8253
8254 static void
8255 do_ssat16 (void)
8256 {
8257   inst.instruction |= inst.operands[0].reg << 12;
8258   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
8259   inst.instruction |= inst.operands[2].reg;
8260 }
8261
8262 static void
8263 do_usat16 (void)
8264 {
8265   inst.instruction |= inst.operands[0].reg << 12;
8266   inst.instruction |= inst.operands[1].imm << 16;
8267   inst.instruction |= inst.operands[2].reg;
8268 }
8269
8270 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
8271    preserving the other bits.
8272
8273    setend <endian_specifier>, where <endian_specifier> is either
8274    BE or LE.  */
8275
8276 static void
8277 do_setend (void)
8278 {
8279   if (inst.operands[0].imm)
8280     inst.instruction |= 0x200;
8281 }
8282
8283 static void
8284 do_shift (void)
8285 {
8286   unsigned int Rm = (inst.operands[1].present
8287                      ? inst.operands[1].reg
8288                      : inst.operands[0].reg);
8289
8290   inst.instruction |= inst.operands[0].reg << 12;
8291   inst.instruction |= Rm;
8292   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
8293     {
8294       inst.instruction |= inst.operands[2].reg << 8;
8295       inst.instruction |= SHIFT_BY_REG;
8296     }
8297   else
8298     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
8299 }
8300
8301 static void
8302 do_smc (void)
8303 {
8304   inst.reloc.type = BFD_RELOC_ARM_SMC;
8305   inst.reloc.pc_rel = 0;
8306 }
8307
8308 static void
8309 do_hvc (void)
8310 {
8311   inst.reloc.type = BFD_RELOC_ARM_HVC;
8312   inst.reloc.pc_rel = 0;
8313 }
8314
8315 static void
8316 do_swi (void)
8317 {
8318   inst.reloc.type = BFD_RELOC_ARM_SWI;
8319   inst.reloc.pc_rel = 0;
8320 }
8321
8322 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
8323    SMLAxy{cond} Rd,Rm,Rs,Rn
8324    SMLAWy{cond} Rd,Rm,Rs,Rn
8325    Error if any register is R15.  */
8326
8327 static void
8328 do_smla (void)
8329 {
8330   inst.instruction |= inst.operands[0].reg << 16;
8331   inst.instruction |= inst.operands[1].reg;
8332   inst.instruction |= inst.operands[2].reg << 8;
8333   inst.instruction |= inst.operands[3].reg << 12;
8334 }
8335
8336 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
8337    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
8338    Error if any register is R15.
8339    Warning if Rdlo == Rdhi.  */
8340
8341 static void
8342 do_smlal (void)
8343 {
8344   inst.instruction |= inst.operands[0].reg << 12;
8345   inst.instruction |= inst.operands[1].reg << 16;
8346   inst.instruction |= inst.operands[2].reg;
8347   inst.instruction |= inst.operands[3].reg << 8;
8348
8349   if (inst.operands[0].reg == inst.operands[1].reg)
8350     as_tsktsk (_("rdhi and rdlo must be different"));
8351 }
8352
8353 /* ARM V5E (El Segundo) signed-multiply (argument parse)
8354    SMULxy{cond} Rd,Rm,Rs
8355    Error if any register is R15.  */
8356
8357 static void
8358 do_smul (void)
8359 {
8360   inst.instruction |= inst.operands[0].reg << 16;
8361   inst.instruction |= inst.operands[1].reg;
8362   inst.instruction |= inst.operands[2].reg << 8;
8363 }
8364
8365 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
8366    the same for both ARM and Thumb-2.  */
8367
8368 static void
8369 do_srs (void)
8370 {
8371   int reg;
8372
8373   if (inst.operands[0].present)
8374     {
8375       reg = inst.operands[0].reg;
8376       constraint (reg != REG_SP, _("SRS base register must be r13"));
8377     }
8378   else
8379     reg = REG_SP;
8380
8381   inst.instruction |= reg << 16;
8382   inst.instruction |= inst.operands[1].imm;
8383   if (inst.operands[0].writeback || inst.operands[1].writeback)
8384     inst.instruction |= WRITE_BACK;
8385 }
8386
8387 /* ARM V6 strex (argument parse).  */
8388
8389 static void
8390 do_strex (void)
8391 {
8392   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
8393               || inst.operands[2].postind || inst.operands[2].writeback
8394               || inst.operands[2].immisreg || inst.operands[2].shifted
8395               || inst.operands[2].negative
8396               /* See comment in do_ldrex().  */
8397               || (inst.operands[2].reg == REG_PC),
8398               BAD_ADDR_MODE);
8399
8400   constraint (inst.operands[0].reg == inst.operands[1].reg
8401               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
8402
8403   constraint (inst.reloc.exp.X_op != O_constant
8404               || inst.reloc.exp.X_add_number != 0,
8405               _("offset must be zero in ARM encoding"));
8406
8407   inst.instruction |= inst.operands[0].reg << 12;
8408   inst.instruction |= inst.operands[1].reg;
8409   inst.instruction |= inst.operands[2].reg << 16;
8410   inst.reloc.type = BFD_RELOC_UNUSED;
8411 }
8412
8413 static void
8414 do_strexd (void)
8415 {
8416   constraint (inst.operands[1].reg % 2 != 0,
8417               _("even register required"));
8418   constraint (inst.operands[2].present
8419               && inst.operands[2].reg != inst.operands[1].reg + 1,
8420               _("can only store two consecutive registers"));
8421   /* If op 2 were present and equal to PC, this function wouldn't
8422      have been called in the first place.  */
8423   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
8424
8425   constraint (inst.operands[0].reg == inst.operands[1].reg
8426               || inst.operands[0].reg == inst.operands[1].reg + 1
8427               || inst.operands[0].reg == inst.operands[3].reg,
8428               BAD_OVERLAP);
8429
8430   inst.instruction |= inst.operands[0].reg << 12;
8431   inst.instruction |= inst.operands[1].reg;
8432   inst.instruction |= inst.operands[3].reg << 16;
8433 }
8434
8435 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
8436    extends it to 32-bits, and adds the result to a value in another
8437    register.  You can specify a rotation by 0, 8, 16, or 24 bits
8438    before extracting the 16-bit value.
8439    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
8440    Condition defaults to COND_ALWAYS.
8441    Error if any register uses R15.  */
8442
8443 static void
8444 do_sxtah (void)
8445 {
8446   inst.instruction |= inst.operands[0].reg << 12;
8447   inst.instruction |= inst.operands[1].reg << 16;
8448   inst.instruction |= inst.operands[2].reg;
8449   inst.instruction |= inst.operands[3].imm << 10;
8450 }
8451
8452 /* ARM V6 SXTH.
8453
8454    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
8455    Condition defaults to COND_ALWAYS.
8456    Error if any register uses R15.  */
8457
8458 static void
8459 do_sxth (void)
8460 {
8461   inst.instruction |= inst.operands[0].reg << 12;
8462   inst.instruction |= inst.operands[1].reg;
8463   inst.instruction |= inst.operands[2].imm << 10;
8464 }
8465 \f
8466 /* VFP instructions.  In a logical order: SP variant first, monad
8467    before dyad, arithmetic then move then load/store.  */
8468
8469 static void
8470 do_vfp_sp_monadic (void)
8471 {
8472   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8473   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
8474 }
8475
8476 static void
8477 do_vfp_sp_dyadic (void)
8478 {
8479   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8480   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
8481   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
8482 }
8483
8484 static void
8485 do_vfp_sp_compare_z (void)
8486 {
8487   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8488 }
8489
8490 static void
8491 do_vfp_dp_sp_cvt (void)
8492 {
8493   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8494   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
8495 }
8496
8497 static void
8498 do_vfp_sp_dp_cvt (void)
8499 {
8500   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8501   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
8502 }
8503
8504 static void
8505 do_vfp_reg_from_sp (void)
8506 {
8507   inst.instruction |= inst.operands[0].reg << 12;
8508   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
8509 }
8510
8511 static void
8512 do_vfp_reg2_from_sp2 (void)
8513 {
8514   constraint (inst.operands[2].imm != 2,
8515               _("only two consecutive VFP SP registers allowed here"));
8516   inst.instruction |= inst.operands[0].reg << 12;
8517   inst.instruction |= inst.operands[1].reg << 16;
8518   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
8519 }
8520
8521 static void
8522 do_vfp_sp_from_reg (void)
8523 {
8524   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
8525   inst.instruction |= inst.operands[1].reg << 12;
8526 }
8527
8528 static void
8529 do_vfp_sp2_from_reg2 (void)
8530 {
8531   constraint (inst.operands[0].imm != 2,
8532               _("only two consecutive VFP SP registers allowed here"));
8533   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
8534   inst.instruction |= inst.operands[1].reg << 12;
8535   inst.instruction |= inst.operands[2].reg << 16;
8536 }
8537
8538 static void
8539 do_vfp_sp_ldst (void)
8540 {
8541   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8542   encode_arm_cp_address (1, FALSE, TRUE, 0);
8543 }
8544
8545 static void
8546 do_vfp_dp_ldst (void)
8547 {
8548   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8549   encode_arm_cp_address (1, FALSE, TRUE, 0);
8550 }
8551
8552
8553 static void
8554 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
8555 {
8556   if (inst.operands[0].writeback)
8557     inst.instruction |= WRITE_BACK;
8558   else
8559     constraint (ldstm_type != VFP_LDSTMIA,
8560                 _("this addressing mode requires base-register writeback"));
8561   inst.instruction |= inst.operands[0].reg << 16;
8562   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
8563   inst.instruction |= inst.operands[1].imm;
8564 }
8565
8566 static void
8567 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
8568 {
8569   int count;
8570
8571   if (inst.operands[0].writeback)
8572     inst.instruction |= WRITE_BACK;
8573   else
8574     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
8575                 _("this addressing mode requires base-register writeback"));
8576
8577   inst.instruction |= inst.operands[0].reg << 16;
8578   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8579
8580   count = inst.operands[1].imm << 1;
8581   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
8582     count += 1;
8583
8584   inst.instruction |= count;
8585 }
8586
8587 static void
8588 do_vfp_sp_ldstmia (void)
8589 {
8590   vfp_sp_ldstm (VFP_LDSTMIA);
8591 }
8592
8593 static void
8594 do_vfp_sp_ldstmdb (void)
8595 {
8596   vfp_sp_ldstm (VFP_LDSTMDB);
8597 }
8598
8599 static void
8600 do_vfp_dp_ldstmia (void)
8601 {
8602   vfp_dp_ldstm (VFP_LDSTMIA);
8603 }
8604
8605 static void
8606 do_vfp_dp_ldstmdb (void)
8607 {
8608   vfp_dp_ldstm (VFP_LDSTMDB);
8609 }
8610
8611 static void
8612 do_vfp_xp_ldstmia (void)
8613 {
8614   vfp_dp_ldstm (VFP_LDSTMIAX);
8615 }
8616
8617 static void
8618 do_vfp_xp_ldstmdb (void)
8619 {
8620   vfp_dp_ldstm (VFP_LDSTMDBX);
8621 }
8622
8623 static void
8624 do_vfp_dp_rd_rm (void)
8625 {
8626   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8627   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
8628 }
8629
8630 static void
8631 do_vfp_dp_rn_rd (void)
8632 {
8633   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
8634   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8635 }
8636
8637 static void
8638 do_vfp_dp_rd_rn (void)
8639 {
8640   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8641   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
8642 }
8643
8644 static void
8645 do_vfp_dp_rd_rn_rm (void)
8646 {
8647   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8648   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
8649   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
8650 }
8651
8652 static void
8653 do_vfp_dp_rd (void)
8654 {
8655   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8656 }
8657
8658 static void
8659 do_vfp_dp_rm_rd_rn (void)
8660 {
8661   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
8662   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8663   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
8664 }
8665
8666 /* VFPv3 instructions.  */
8667 static void
8668 do_vfp_sp_const (void)
8669 {
8670   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8671   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
8672   inst.instruction |= (inst.operands[1].imm & 0x0f);
8673 }
8674
8675 static void
8676 do_vfp_dp_const (void)
8677 {
8678   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8679   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
8680   inst.instruction |= (inst.operands[1].imm & 0x0f);
8681 }
8682
8683 static void
8684 vfp_conv (int srcsize)
8685 {
8686   unsigned immbits = srcsize - inst.operands[1].imm;
8687   inst.instruction |= (immbits & 1) << 5;
8688   inst.instruction |= (immbits >> 1);
8689 }
8690
8691 static void
8692 do_vfp_sp_conv_16 (void)
8693 {
8694   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8695   vfp_conv (16);
8696 }
8697
8698 static void
8699 do_vfp_dp_conv_16 (void)
8700 {
8701   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8702   vfp_conv (16);
8703 }
8704
8705 static void
8706 do_vfp_sp_conv_32 (void)
8707 {
8708   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8709   vfp_conv (32);
8710 }
8711
8712 static void
8713 do_vfp_dp_conv_32 (void)
8714 {
8715   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8716   vfp_conv (32);
8717 }
8718 \f
8719 /* FPA instructions.  Also in a logical order.  */
8720
8721 static void
8722 do_fpa_cmp (void)
8723 {
8724   inst.instruction |= inst.operands[0].reg << 16;
8725   inst.instruction |= inst.operands[1].reg;
8726 }
8727
8728 static void
8729 do_fpa_ldmstm (void)
8730 {
8731   inst.instruction |= inst.operands[0].reg << 12;
8732   switch (inst.operands[1].imm)
8733     {
8734     case 1: inst.instruction |= CP_T_X;          break;
8735     case 2: inst.instruction |= CP_T_Y;          break;
8736     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
8737     case 4:                                      break;
8738     default: abort ();
8739     }
8740
8741   if (inst.instruction & (PRE_INDEX | INDEX_UP))
8742     {
8743       /* The instruction specified "ea" or "fd", so we can only accept
8744          [Rn]{!}.  The instruction does not really support stacking or
8745          unstacking, so we have to emulate these by setting appropriate
8746          bits and offsets.  */
8747       constraint (inst.reloc.exp.X_op != O_constant
8748                   || inst.reloc.exp.X_add_number != 0,
8749                   _("this instruction does not support indexing"));
8750
8751       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
8752         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
8753
8754       if (!(inst.instruction & INDEX_UP))
8755         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
8756
8757       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
8758         {
8759           inst.operands[2].preind = 0;
8760           inst.operands[2].postind = 1;
8761         }
8762     }
8763
8764   encode_arm_cp_address (2, TRUE, TRUE, 0);
8765 }
8766 \f
8767 /* iWMMXt instructions: strictly in alphabetical order.  */
8768
8769 static void
8770 do_iwmmxt_tandorc (void)
8771 {
8772   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
8773 }
8774
8775 static void
8776 do_iwmmxt_textrc (void)
8777 {
8778   inst.instruction |= inst.operands[0].reg << 12;
8779   inst.instruction |= inst.operands[1].imm;
8780 }
8781
8782 static void
8783 do_iwmmxt_textrm (void)
8784 {
8785   inst.instruction |= inst.operands[0].reg << 12;
8786   inst.instruction |= inst.operands[1].reg << 16;
8787   inst.instruction |= inst.operands[2].imm;
8788 }
8789
8790 static void
8791 do_iwmmxt_tinsr (void)
8792 {
8793   inst.instruction |= inst.operands[0].reg << 16;
8794   inst.instruction |= inst.operands[1].reg << 12;
8795   inst.instruction |= inst.operands[2].imm;
8796 }
8797
8798 static void
8799 do_iwmmxt_tmia (void)
8800 {
8801   inst.instruction |= inst.operands[0].reg << 5;
8802   inst.instruction |= inst.operands[1].reg;
8803   inst.instruction |= inst.operands[2].reg << 12;
8804 }
8805
8806 static void
8807 do_iwmmxt_waligni (void)
8808 {
8809   inst.instruction |= inst.operands[0].reg << 12;
8810   inst.instruction |= inst.operands[1].reg << 16;
8811   inst.instruction |= inst.operands[2].reg;
8812   inst.instruction |= inst.operands[3].imm << 20;
8813 }
8814
8815 static void
8816 do_iwmmxt_wmerge (void)
8817 {
8818   inst.instruction |= inst.operands[0].reg << 12;
8819   inst.instruction |= inst.operands[1].reg << 16;
8820   inst.instruction |= inst.operands[2].reg;
8821   inst.instruction |= inst.operands[3].imm << 21;
8822 }
8823
8824 static void
8825 do_iwmmxt_wmov (void)
8826 {
8827   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
8828   inst.instruction |= inst.operands[0].reg << 12;
8829   inst.instruction |= inst.operands[1].reg << 16;
8830   inst.instruction |= inst.operands[1].reg;
8831 }
8832
8833 static void
8834 do_iwmmxt_wldstbh (void)
8835 {
8836   int reloc;
8837   inst.instruction |= inst.operands[0].reg << 12;
8838   if (thumb_mode)
8839     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
8840   else
8841     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
8842   encode_arm_cp_address (1, TRUE, FALSE, reloc);
8843 }
8844
8845 static void
8846 do_iwmmxt_wldstw (void)
8847 {
8848   /* RIWR_RIWC clears .isreg for a control register.  */
8849   if (!inst.operands[0].isreg)
8850     {
8851       constraint (inst.cond != COND_ALWAYS, BAD_COND);
8852       inst.instruction |= 0xf0000000;
8853     }
8854
8855   inst.instruction |= inst.operands[0].reg << 12;
8856   encode_arm_cp_address (1, TRUE, TRUE, 0);
8857 }
8858
8859 static void
8860 do_iwmmxt_wldstd (void)
8861 {
8862   inst.instruction |= inst.operands[0].reg << 12;
8863   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
8864       && inst.operands[1].immisreg)
8865     {
8866       inst.instruction &= ~0x1a000ff;
8867       inst.instruction |= (0xf << 28);
8868       if (inst.operands[1].preind)
8869         inst.instruction |= PRE_INDEX;
8870       if (!inst.operands[1].negative)
8871         inst.instruction |= INDEX_UP;
8872       if (inst.operands[1].writeback)
8873         inst.instruction |= WRITE_BACK;
8874       inst.instruction |= inst.operands[1].reg << 16;
8875       inst.instruction |= inst.reloc.exp.X_add_number << 4;
8876       inst.instruction |= inst.operands[1].imm;
8877     }
8878   else
8879     encode_arm_cp_address (1, TRUE, FALSE, 0);
8880 }
8881
8882 static void
8883 do_iwmmxt_wshufh (void)
8884 {
8885   inst.instruction |= inst.operands[0].reg << 12;
8886   inst.instruction |= inst.operands[1].reg << 16;
8887   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
8888   inst.instruction |= (inst.operands[2].imm & 0x0f);
8889 }
8890
8891 static void
8892 do_iwmmxt_wzero (void)
8893 {
8894   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
8895   inst.instruction |= inst.operands[0].reg;
8896   inst.instruction |= inst.operands[0].reg << 12;
8897   inst.instruction |= inst.operands[0].reg << 16;
8898 }
8899
8900 static void
8901 do_iwmmxt_wrwrwr_or_imm5 (void)
8902 {
8903   if (inst.operands[2].isreg)
8904     do_rd_rn_rm ();
8905   else {
8906     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
8907                 _("immediate operand requires iWMMXt2"));
8908     do_rd_rn ();
8909     if (inst.operands[2].imm == 0)
8910       {
8911         switch ((inst.instruction >> 20) & 0xf)
8912           {
8913           case 4:
8914           case 5:
8915           case 6:
8916           case 7:
8917             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
8918             inst.operands[2].imm = 16;
8919             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
8920             break;
8921           case 8:
8922           case 9:
8923           case 10:
8924           case 11:
8925             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
8926             inst.operands[2].imm = 32;
8927             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
8928             break;
8929           case 12:
8930           case 13:
8931           case 14:
8932           case 15:
8933             {
8934               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
8935               unsigned long wrn;
8936               wrn = (inst.instruction >> 16) & 0xf;
8937               inst.instruction &= 0xff0fff0f;
8938               inst.instruction |= wrn;
8939               /* Bail out here; the instruction is now assembled.  */
8940               return;
8941             }
8942           }
8943       }
8944     /* Map 32 -> 0, etc.  */
8945     inst.operands[2].imm &= 0x1f;
8946     inst.instruction |= (0xf << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
8947   }
8948 }
8949 \f
8950 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
8951    operations first, then control, shift, and load/store.  */
8952
8953 /* Insns like "foo X,Y,Z".  */
8954
8955 static void
8956 do_mav_triple (void)
8957 {
8958   inst.instruction |= inst.operands[0].reg << 16;
8959   inst.instruction |= inst.operands[1].reg;
8960   inst.instruction |= inst.operands[2].reg << 12;
8961 }
8962
8963 /* Insns like "foo W,X,Y,Z".
8964     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
8965
8966 static void
8967 do_mav_quad (void)
8968 {
8969   inst.instruction |= inst.operands[0].reg << 5;
8970   inst.instruction |= inst.operands[1].reg << 12;
8971   inst.instruction |= inst.operands[2].reg << 16;
8972   inst.instruction |= inst.operands[3].reg;
8973 }
8974
8975 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
8976 static void
8977 do_mav_dspsc (void)
8978 {
8979   inst.instruction |= inst.operands[1].reg << 12;
8980 }
8981
8982 /* Maverick shift immediate instructions.
8983    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
8984    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
8985
8986 static void
8987 do_mav_shift (void)
8988 {
8989   int imm = inst.operands[2].imm;
8990
8991   inst.instruction |= inst.operands[0].reg << 12;
8992   inst.instruction |= inst.operands[1].reg << 16;
8993
8994   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
8995      Bits 5-7 of the insn should have bits 4-6 of the immediate.
8996      Bit 4 should be 0.  */
8997   imm = (imm & 0xf) | ((imm & 0x70) << 1);
8998
8999   inst.instruction |= imm;
9000 }
9001 \f
9002 /* XScale instructions.  Also sorted arithmetic before move.  */
9003
9004 /* Xscale multiply-accumulate (argument parse)
9005      MIAcc   acc0,Rm,Rs
9006      MIAPHcc acc0,Rm,Rs
9007      MIAxycc acc0,Rm,Rs.  */
9008
9009 static void
9010 do_xsc_mia (void)
9011 {
9012   inst.instruction |= inst.operands[1].reg;
9013   inst.instruction |= inst.operands[2].reg << 12;
9014 }
9015
9016 /* Xscale move-accumulator-register (argument parse)
9017
9018      MARcc   acc0,RdLo,RdHi.  */
9019
9020 static void
9021 do_xsc_mar (void)
9022 {
9023   inst.instruction |= inst.operands[1].reg << 12;
9024   inst.instruction |= inst.operands[2].reg << 16;
9025 }
9026
9027 /* Xscale move-register-accumulator (argument parse)
9028
9029      MRAcc   RdLo,RdHi,acc0.  */
9030
9031 static void
9032 do_xsc_mra (void)
9033 {
9034   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
9035   inst.instruction |= inst.operands[0].reg << 12;
9036   inst.instruction |= inst.operands[1].reg << 16;
9037 }
9038 \f
9039 /* Encoding functions relevant only to Thumb.  */
9040
9041 /* inst.operands[i] is a shifted-register operand; encode
9042    it into inst.instruction in the format used by Thumb32.  */
9043
9044 static void
9045 encode_thumb32_shifted_operand (int i)
9046 {
9047   unsigned int value = inst.reloc.exp.X_add_number;
9048   unsigned int shift = inst.operands[i].shift_kind;
9049
9050   constraint (inst.operands[i].immisreg,
9051               _("shift by register not allowed in thumb mode"));
9052   inst.instruction |= inst.operands[i].reg;
9053   if (shift == SHIFT_RRX)
9054     inst.instruction |= SHIFT_ROR << 4;
9055   else
9056     {
9057       constraint (inst.reloc.exp.X_op != O_constant,
9058                   _("expression too complex"));
9059
9060       constraint (value > 32
9061                   || (value == 32 && (shift == SHIFT_LSL
9062                                       || shift == SHIFT_ROR)),
9063                   _("shift expression is too large"));
9064
9065       if (value == 0)
9066         shift = SHIFT_LSL;
9067       else if (value == 32)
9068         value = 0;
9069
9070       inst.instruction |= shift << 4;
9071       inst.instruction |= (value & 0x1c) << 10;
9072       inst.instruction |= (value & 0x03) << 6;
9073     }
9074 }
9075
9076
9077 /* inst.operands[i] was set up by parse_address.  Encode it into a
9078    Thumb32 format load or store instruction.  Reject forms that cannot
9079    be used with such instructions.  If is_t is true, reject forms that
9080    cannot be used with a T instruction; if is_d is true, reject forms
9081    that cannot be used with a D instruction.  If it is a store insn,
9082    reject PC in Rn.  */
9083
9084 static void
9085 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
9086 {
9087   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
9088
9089   constraint (!inst.operands[i].isreg,
9090               _("Instruction does not support =N addresses"));
9091
9092   inst.instruction |= inst.operands[i].reg << 16;
9093   if (inst.operands[i].immisreg)
9094     {
9095       constraint (is_pc, BAD_PC_ADDRESSING);
9096       constraint (is_t || is_d, _("cannot use register index with this instruction"));
9097       constraint (inst.operands[i].negative,
9098                   _("Thumb does not support negative register indexing"));
9099       constraint (inst.operands[i].postind,
9100                   _("Thumb does not support register post-indexing"));
9101       constraint (inst.operands[i].writeback,
9102                   _("Thumb does not support register indexing with writeback"));
9103       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
9104                   _("Thumb supports only LSL in shifted register indexing"));
9105
9106       inst.instruction |= inst.operands[i].imm;
9107       if (inst.operands[i].shifted)
9108         {
9109           constraint (inst.reloc.exp.X_op != O_constant,
9110                       _("expression too complex"));
9111           constraint (inst.reloc.exp.X_add_number < 0
9112                       || inst.reloc.exp.X_add_number > 3,
9113                       _("shift out of range"));
9114           inst.instruction |= inst.reloc.exp.X_add_number << 4;
9115         }
9116       inst.reloc.type = BFD_RELOC_UNUSED;
9117     }
9118   else if (inst.operands[i].preind)
9119     {
9120       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
9121       constraint (is_t && inst.operands[i].writeback,
9122                   _("cannot use writeback with this instruction"));
9123       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0)
9124                   && !inst.reloc.pc_rel, BAD_PC_ADDRESSING);
9125
9126       if (is_d)
9127         {
9128           inst.instruction |= 0x01000000;
9129           if (inst.operands[i].writeback)
9130             inst.instruction |= 0x00200000;
9131         }
9132       else
9133         {
9134           inst.instruction |= 0x00000c00;
9135           if (inst.operands[i].writeback)
9136             inst.instruction |= 0x00000100;
9137         }
9138       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
9139     }
9140   else if (inst.operands[i].postind)
9141     {
9142       gas_assert (inst.operands[i].writeback);
9143       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
9144       constraint (is_t, _("cannot use post-indexing with this instruction"));
9145
9146       if (is_d)
9147         inst.instruction |= 0x00200000;
9148       else
9149         inst.instruction |= 0x00000900;
9150       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
9151     }
9152   else /* unindexed - only for coprocessor */
9153     inst.error = _("instruction does not accept unindexed addressing");
9154 }
9155
9156 /* Table of Thumb instructions which exist in both 16- and 32-bit
9157    encodings (the latter only in post-V6T2 cores).  The index is the
9158    value used in the insns table below.  When there is more than one
9159    possible 16-bit encoding for the instruction, this table always
9160    holds variant (1).
9161    Also contains several pseudo-instructions used during relaxation.  */
9162 #define T16_32_TAB                              \
9163   X(_adc,   4140, eb400000),                    \
9164   X(_adcs,  4140, eb500000),                    \
9165   X(_add,   1c00, eb000000),                    \
9166   X(_adds,  1c00, eb100000),                    \
9167   X(_addi,  0000, f1000000),                    \
9168   X(_addis, 0000, f1100000),                    \
9169   X(_add_pc,000f, f20f0000),                    \
9170   X(_add_sp,000d, f10d0000),                    \
9171   X(_adr,   000f, f20f0000),                    \
9172   X(_and,   4000, ea000000),                    \
9173   X(_ands,  4000, ea100000),                    \
9174   X(_asr,   1000, fa40f000),                    \
9175   X(_asrs,  1000, fa50f000),                    \
9176   X(_b,     e000, f000b000),                    \
9177   X(_bcond, d000, f0008000),                    \
9178   X(_bic,   4380, ea200000),                    \
9179   X(_bics,  4380, ea300000),                    \
9180   X(_cmn,   42c0, eb100f00),                    \
9181   X(_cmp,   2800, ebb00f00),                    \
9182   X(_cpsie, b660, f3af8400),                    \
9183   X(_cpsid, b670, f3af8600),                    \
9184   X(_cpy,   4600, ea4f0000),                    \
9185   X(_dec_sp,80dd, f1ad0d00),                    \
9186   X(_eor,   4040, ea800000),                    \
9187   X(_eors,  4040, ea900000),                    \
9188   X(_inc_sp,00dd, f10d0d00),                    \
9189   X(_ldmia, c800, e8900000),                    \
9190   X(_ldr,   6800, f8500000),                    \
9191   X(_ldrb,  7800, f8100000),                    \
9192   X(_ldrh,  8800, f8300000),                    \
9193   X(_ldrsb, 5600, f9100000),                    \
9194   X(_ldrsh, 5e00, f9300000),                    \
9195   X(_ldr_pc,4800, f85f0000),                    \
9196   X(_ldr_pc2,4800, f85f0000),                   \
9197   X(_ldr_sp,9800, f85d0000),                    \
9198   X(_lsl,   0000, fa00f000),                    \
9199   X(_lsls,  0000, fa10f000),                    \
9200   X(_lsr,   0800, fa20f000),                    \
9201   X(_lsrs,  0800, fa30f000),                    \
9202   X(_mov,   2000, ea4f0000),                    \
9203   X(_movs,  2000, ea5f0000),                    \
9204   X(_mul,   4340, fb00f000),                     \
9205   X(_muls,  4340, ffffffff), /* no 32b muls */  \
9206   X(_mvn,   43c0, ea6f0000),                    \
9207   X(_mvns,  43c0, ea7f0000),                    \
9208   X(_neg,   4240, f1c00000), /* rsb #0 */       \
9209   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
9210   X(_orr,   4300, ea400000),                    \
9211   X(_orrs,  4300, ea500000),                    \
9212   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
9213   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
9214   X(_rev,   ba00, fa90f080),                    \
9215   X(_rev16, ba40, fa90f090),                    \
9216   X(_revsh, bac0, fa90f0b0),                    \
9217   X(_ror,   41c0, fa60f000),                    \
9218   X(_rors,  41c0, fa70f000),                    \
9219   X(_sbc,   4180, eb600000),                    \
9220   X(_sbcs,  4180, eb700000),                    \
9221   X(_stmia, c000, e8800000),                    \
9222   X(_str,   6000, f8400000),                    \
9223   X(_strb,  7000, f8000000),                    \
9224   X(_strh,  8000, f8200000),                    \
9225   X(_str_sp,9000, f84d0000),                    \
9226   X(_sub,   1e00, eba00000),                    \
9227   X(_subs,  1e00, ebb00000),                    \
9228   X(_subi,  8000, f1a00000),                    \
9229   X(_subis, 8000, f1b00000),                    \
9230   X(_sxtb,  b240, fa4ff080),                    \
9231   X(_sxth,  b200, fa0ff080),                    \
9232   X(_tst,   4200, ea100f00),                    \
9233   X(_uxtb,  b2c0, fa5ff080),                    \
9234   X(_uxth,  b280, fa1ff080),                    \
9235   X(_nop,   bf00, f3af8000),                    \
9236   X(_yield, bf10, f3af8001),                    \
9237   X(_wfe,   bf20, f3af8002),                    \
9238   X(_wfi,   bf30, f3af8003),                    \
9239   X(_sev,   bf40, f3af8004),
9240
9241 /* To catch errors in encoding functions, the codes are all offset by
9242    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
9243    as 16-bit instructions.  */
9244 #define X(a,b,c) T_MNEM##a
9245 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
9246 #undef X
9247
9248 #define X(a,b,c) 0x##b
9249 static const unsigned short thumb_op16[] = { T16_32_TAB };
9250 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
9251 #undef X
9252
9253 #define X(a,b,c) 0x##c
9254 static const unsigned int thumb_op32[] = { T16_32_TAB };
9255 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
9256 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
9257 #undef X
9258 #undef T16_32_TAB
9259
9260 /* Thumb instruction encoders, in alphabetical order.  */
9261
9262 /* ADDW or SUBW.  */
9263
9264 static void
9265 do_t_add_sub_w (void)
9266 {
9267   int Rd, Rn;
9268
9269   Rd = inst.operands[0].reg;
9270   Rn = inst.operands[1].reg;
9271
9272   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
9273      is the SP-{plus,minus}-immediate form of the instruction.  */
9274   if (Rn == REG_SP)
9275     constraint (Rd == REG_PC, BAD_PC);
9276   else
9277     reject_bad_reg (Rd);
9278
9279   inst.instruction |= (Rn << 16) | (Rd << 8);
9280   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
9281 }
9282
9283 /* Parse an add or subtract instruction.  We get here with inst.instruction
9284    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
9285
9286 static void
9287 do_t_add_sub (void)
9288 {
9289   int Rd, Rs, Rn;
9290
9291   Rd = inst.operands[0].reg;
9292   Rs = (inst.operands[1].present
9293         ? inst.operands[1].reg    /* Rd, Rs, foo */
9294         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9295
9296   if (Rd == REG_PC)
9297     set_it_insn_type_last ();
9298
9299   if (unified_syntax)
9300     {
9301       bfd_boolean flags;
9302       bfd_boolean narrow;
9303       int opcode;
9304
9305       flags = (inst.instruction == T_MNEM_adds
9306                || inst.instruction == T_MNEM_subs);
9307       if (flags)
9308         narrow = !in_it_block ();
9309       else
9310         narrow = in_it_block ();
9311       if (!inst.operands[2].isreg)
9312         {
9313           int add;
9314
9315           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
9316
9317           add = (inst.instruction == T_MNEM_add
9318                  || inst.instruction == T_MNEM_adds);
9319           opcode = 0;
9320           if (inst.size_req != 4)
9321             {
9322               /* Attempt to use a narrow opcode, with relaxation if
9323                  appropriate.  */
9324               if (Rd == REG_SP && Rs == REG_SP && !flags)
9325                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
9326               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
9327                 opcode = T_MNEM_add_sp;
9328               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
9329                 opcode = T_MNEM_add_pc;
9330               else if (Rd <= 7 && Rs <= 7 && narrow)
9331                 {
9332                   if (flags)
9333                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
9334                   else
9335                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
9336                 }
9337               if (opcode)
9338                 {
9339                   inst.instruction = THUMB_OP16(opcode);
9340                   inst.instruction |= (Rd << 4) | Rs;
9341                   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9342                   if (inst.size_req != 2)
9343                     inst.relax = opcode;
9344                 }
9345               else
9346                 constraint (inst.size_req == 2, BAD_HIREG);
9347             }
9348           if (inst.size_req == 4
9349               || (inst.size_req != 2 && !opcode))
9350             {
9351               if (Rd == REG_PC)
9352                 {
9353                   constraint (add, BAD_PC);
9354                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
9355                              _("only SUBS PC, LR, #const allowed"));
9356                   constraint (inst.reloc.exp.X_op != O_constant,
9357                               _("expression too complex"));
9358                   constraint (inst.reloc.exp.X_add_number < 0
9359                               || inst.reloc.exp.X_add_number > 0xff,
9360                              _("immediate value out of range"));
9361                   inst.instruction = T2_SUBS_PC_LR
9362                                      | inst.reloc.exp.X_add_number;
9363                   inst.reloc.type = BFD_RELOC_UNUSED;
9364                   return;
9365                 }
9366               else if (Rs == REG_PC)
9367                 {
9368                   /* Always use addw/subw.  */
9369                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
9370                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
9371                 }
9372               else
9373                 {
9374                   inst.instruction = THUMB_OP32 (inst.instruction);
9375                   inst.instruction = (inst.instruction & 0xe1ffffff)
9376                                      | 0x10000000;
9377                   if (flags)
9378                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9379                   else
9380                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
9381                 }
9382               inst.instruction |= Rd << 8;
9383               inst.instruction |= Rs << 16;
9384             }
9385         }
9386       else
9387         {
9388           Rn = inst.operands[2].reg;
9389           /* See if we can do this with a 16-bit instruction.  */
9390           if (!inst.operands[2].shifted && inst.size_req != 4)
9391             {
9392               if (Rd > 7 || Rs > 7 || Rn > 7)
9393                 narrow = FALSE;
9394
9395               if (narrow)
9396                 {
9397                   inst.instruction = ((inst.instruction == T_MNEM_adds
9398                                        || inst.instruction == T_MNEM_add)
9399                                       ? T_OPCODE_ADD_R3
9400                                       : T_OPCODE_SUB_R3);
9401                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
9402                   return;
9403                 }
9404
9405               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
9406                 {
9407                   /* Thumb-1 cores (except v6-M) require at least one high
9408                      register in a narrow non flag setting add.  */
9409                   if (Rd > 7 || Rn > 7
9410                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
9411                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
9412                     {
9413                       if (Rd == Rn)
9414                         {
9415                           Rn = Rs;
9416                           Rs = Rd;
9417                         }
9418                       inst.instruction = T_OPCODE_ADD_HI;
9419                       inst.instruction |= (Rd & 8) << 4;
9420                       inst.instruction |= (Rd & 7);
9421                       inst.instruction |= Rn << 3;
9422                       return;
9423                     }
9424                 }
9425             }
9426
9427           constraint (Rd == REG_PC, BAD_PC);
9428           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
9429           constraint (Rs == REG_PC, BAD_PC);
9430           reject_bad_reg (Rn);
9431
9432           /* If we get here, it can't be done in 16 bits.  */
9433           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
9434                       _("shift must be constant"));
9435           inst.instruction = THUMB_OP32 (inst.instruction);
9436           inst.instruction |= Rd << 8;
9437           inst.instruction |= Rs << 16;
9438           encode_thumb32_shifted_operand (2);
9439         }
9440     }
9441   else
9442     {
9443       constraint (inst.instruction == T_MNEM_adds
9444                   || inst.instruction == T_MNEM_subs,
9445                   BAD_THUMB32);
9446
9447       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
9448         {
9449           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
9450                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
9451                       BAD_HIREG);
9452
9453           inst.instruction = (inst.instruction == T_MNEM_add
9454                               ? 0x0000 : 0x8000);
9455           inst.instruction |= (Rd << 4) | Rs;
9456           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9457           return;
9458         }
9459
9460       Rn = inst.operands[2].reg;
9461       constraint (inst.operands[2].shifted, _("unshifted register required"));
9462
9463       /* We now have Rd, Rs, and Rn set to registers.  */
9464       if (Rd > 7 || Rs > 7 || Rn > 7)
9465         {
9466           /* Can't do this for SUB.      */
9467           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
9468           inst.instruction = T_OPCODE_ADD_HI;
9469           inst.instruction |= (Rd & 8) << 4;
9470           inst.instruction |= (Rd & 7);
9471           if (Rs == Rd)
9472             inst.instruction |= Rn << 3;
9473           else if (Rn == Rd)
9474             inst.instruction |= Rs << 3;
9475           else
9476             constraint (1, _("dest must overlap one source register"));
9477         }
9478       else
9479         {
9480           inst.instruction = (inst.instruction == T_MNEM_add
9481                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
9482           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
9483         }
9484     }
9485 }
9486
9487 static void
9488 do_t_adr (void)
9489 {
9490   unsigned Rd;
9491
9492   Rd = inst.operands[0].reg;
9493   reject_bad_reg (Rd);
9494
9495   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
9496     {
9497       /* Defer to section relaxation.  */
9498       inst.relax = inst.instruction;
9499       inst.instruction = THUMB_OP16 (inst.instruction);
9500       inst.instruction |= Rd << 4;
9501     }
9502   else if (unified_syntax && inst.size_req != 2)
9503     {
9504       /* Generate a 32-bit opcode.  */
9505       inst.instruction = THUMB_OP32 (inst.instruction);
9506       inst.instruction |= Rd << 8;
9507       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
9508       inst.reloc.pc_rel = 1;
9509     }
9510   else
9511     {
9512       /* Generate a 16-bit opcode.  */
9513       inst.instruction = THUMB_OP16 (inst.instruction);
9514       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9515       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
9516       inst.reloc.pc_rel = 1;
9517
9518       inst.instruction |= Rd << 4;
9519     }
9520 }
9521
9522 /* Arithmetic instructions for which there is just one 16-bit
9523    instruction encoding, and it allows only two low registers.
9524    For maximal compatibility with ARM syntax, we allow three register
9525    operands even when Thumb-32 instructions are not available, as long
9526    as the first two are identical.  For instance, both "sbc r0,r1" and
9527    "sbc r0,r0,r1" are allowed.  */
9528 static void
9529 do_t_arit3 (void)
9530 {
9531   int Rd, Rs, Rn;
9532
9533   Rd = inst.operands[0].reg;
9534   Rs = (inst.operands[1].present
9535         ? inst.operands[1].reg    /* Rd, Rs, foo */
9536         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9537   Rn = inst.operands[2].reg;
9538
9539   reject_bad_reg (Rd);
9540   reject_bad_reg (Rs);
9541   if (inst.operands[2].isreg)
9542     reject_bad_reg (Rn);
9543
9544   if (unified_syntax)
9545     {
9546       if (!inst.operands[2].isreg)
9547         {
9548           /* For an immediate, we always generate a 32-bit opcode;
9549              section relaxation will shrink it later if possible.  */
9550           inst.instruction = THUMB_OP32 (inst.instruction);
9551           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9552           inst.instruction |= Rd << 8;
9553           inst.instruction |= Rs << 16;
9554           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9555         }
9556       else
9557         {
9558           bfd_boolean narrow;
9559
9560           /* See if we can do this with a 16-bit instruction.  */
9561           if (THUMB_SETS_FLAGS (inst.instruction))
9562             narrow = !in_it_block ();
9563           else
9564             narrow = in_it_block ();
9565
9566           if (Rd > 7 || Rn > 7 || Rs > 7)
9567             narrow = FALSE;
9568           if (inst.operands[2].shifted)
9569             narrow = FALSE;
9570           if (inst.size_req == 4)
9571             narrow = FALSE;
9572
9573           if (narrow
9574               && Rd == Rs)
9575             {
9576               inst.instruction = THUMB_OP16 (inst.instruction);
9577               inst.instruction |= Rd;
9578               inst.instruction |= Rn << 3;
9579               return;
9580             }
9581
9582           /* If we get here, it can't be done in 16 bits.  */
9583           constraint (inst.operands[2].shifted
9584                       && inst.operands[2].immisreg,
9585                       _("shift must be constant"));
9586           inst.instruction = THUMB_OP32 (inst.instruction);
9587           inst.instruction |= Rd << 8;
9588           inst.instruction |= Rs << 16;
9589           encode_thumb32_shifted_operand (2);
9590         }
9591     }
9592   else
9593     {
9594       /* On its face this is a lie - the instruction does set the
9595          flags.  However, the only supported mnemonic in this mode
9596          says it doesn't.  */
9597       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
9598
9599       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
9600                   _("unshifted register required"));
9601       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
9602       constraint (Rd != Rs,
9603                   _("dest and source1 must be the same register"));
9604
9605       inst.instruction = THUMB_OP16 (inst.instruction);
9606       inst.instruction |= Rd;
9607       inst.instruction |= Rn << 3;
9608     }
9609 }
9610
9611 /* Similarly, but for instructions where the arithmetic operation is
9612    commutative, so we can allow either of them to be different from
9613    the destination operand in a 16-bit instruction.  For instance, all
9614    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
9615    accepted.  */
9616 static void
9617 do_t_arit3c (void)
9618 {
9619   int Rd, Rs, Rn;
9620
9621   Rd = inst.operands[0].reg;
9622   Rs = (inst.operands[1].present
9623         ? inst.operands[1].reg    /* Rd, Rs, foo */
9624         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9625   Rn = inst.operands[2].reg;
9626
9627   reject_bad_reg (Rd);
9628   reject_bad_reg (Rs);
9629   if (inst.operands[2].isreg)
9630     reject_bad_reg (Rn);
9631
9632   if (unified_syntax)
9633     {
9634       if (!inst.operands[2].isreg)
9635         {
9636           /* For an immediate, we always generate a 32-bit opcode;
9637              section relaxation will shrink it later if possible.  */
9638           inst.instruction = THUMB_OP32 (inst.instruction);
9639           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9640           inst.instruction |= Rd << 8;
9641           inst.instruction |= Rs << 16;
9642           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9643         }
9644       else
9645         {
9646           bfd_boolean narrow;
9647
9648           /* See if we can do this with a 16-bit instruction.  */
9649           if (THUMB_SETS_FLAGS (inst.instruction))
9650             narrow = !in_it_block ();
9651           else
9652             narrow = in_it_block ();
9653
9654           if (Rd > 7 || Rn > 7 || Rs > 7)
9655             narrow = FALSE;
9656           if (inst.operands[2].shifted)
9657             narrow = FALSE;
9658           if (inst.size_req == 4)
9659             narrow = FALSE;
9660
9661           if (narrow)
9662             {
9663               if (Rd == Rs)
9664                 {
9665                   inst.instruction = THUMB_OP16 (inst.instruction);
9666                   inst.instruction |= Rd;
9667                   inst.instruction |= Rn << 3;
9668                   return;
9669                 }
9670               if (Rd == Rn)
9671                 {
9672                   inst.instruction = THUMB_OP16 (inst.instruction);
9673                   inst.instruction |= Rd;
9674                   inst.instruction |= Rs << 3;
9675                   return;
9676                 }
9677             }
9678
9679           /* If we get here, it can't be done in 16 bits.  */
9680           constraint (inst.operands[2].shifted
9681                       && inst.operands[2].immisreg,
9682                       _("shift must be constant"));
9683           inst.instruction = THUMB_OP32 (inst.instruction);
9684           inst.instruction |= Rd << 8;
9685           inst.instruction |= Rs << 16;
9686           encode_thumb32_shifted_operand (2);
9687         }
9688     }
9689   else
9690     {
9691       /* On its face this is a lie - the instruction does set the
9692          flags.  However, the only supported mnemonic in this mode
9693          says it doesn't.  */
9694       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
9695
9696       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
9697                   _("unshifted register required"));
9698       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
9699
9700       inst.instruction = THUMB_OP16 (inst.instruction);
9701       inst.instruction |= Rd;
9702
9703       if (Rd == Rs)
9704         inst.instruction |= Rn << 3;
9705       else if (Rd == Rn)
9706         inst.instruction |= Rs << 3;
9707       else
9708         constraint (1, _("dest must overlap one source register"));
9709     }
9710 }
9711
9712 static void
9713 do_t_barrier (void)
9714 {
9715   if (inst.operands[0].present)
9716     {
9717       constraint ((inst.instruction & 0xf0) != 0x40
9718                   && inst.operands[0].imm > 0xf
9719                   && inst.operands[0].imm < 0x0,
9720                   _("bad barrier type"));
9721       inst.instruction |= inst.operands[0].imm;
9722     }
9723   else
9724     inst.instruction |= 0xf;
9725 }
9726
9727 static void
9728 do_t_bfc (void)
9729 {
9730   unsigned Rd;
9731   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
9732   constraint (msb > 32, _("bit-field extends past end of register"));
9733   /* The instruction encoding stores the LSB and MSB,
9734      not the LSB and width.  */
9735   Rd = inst.operands[0].reg;
9736   reject_bad_reg (Rd);
9737   inst.instruction |= Rd << 8;
9738   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
9739   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
9740   inst.instruction |= msb - 1;
9741 }
9742
9743 static void
9744 do_t_bfi (void)
9745 {
9746   int Rd, Rn;
9747   unsigned int msb;
9748
9749   Rd = inst.operands[0].reg;
9750   reject_bad_reg (Rd);
9751
9752   /* #0 in second position is alternative syntax for bfc, which is
9753      the same instruction but with REG_PC in the Rm field.  */
9754   if (!inst.operands[1].isreg)
9755     Rn = REG_PC;
9756   else
9757     {
9758       Rn = inst.operands[1].reg;
9759       reject_bad_reg (Rn);
9760     }
9761
9762   msb = inst.operands[2].imm + inst.operands[3].imm;
9763   constraint (msb > 32, _("bit-field extends past end of register"));
9764   /* The instruction encoding stores the LSB and MSB,
9765      not the LSB and width.  */
9766   inst.instruction |= Rd << 8;
9767   inst.instruction |= Rn << 16;
9768   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
9769   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
9770   inst.instruction |= msb - 1;
9771 }
9772
9773 static void
9774 do_t_bfx (void)
9775 {
9776   unsigned Rd, Rn;
9777
9778   Rd = inst.operands[0].reg;
9779   Rn = inst.operands[1].reg;
9780
9781   reject_bad_reg (Rd);
9782   reject_bad_reg (Rn);
9783
9784   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
9785               _("bit-field extends past end of register"));
9786   inst.instruction |= Rd << 8;
9787   inst.instruction |= Rn << 16;
9788   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
9789   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
9790   inst.instruction |= inst.operands[3].imm - 1;
9791 }
9792
9793 /* ARM V5 Thumb BLX (argument parse)
9794         BLX <target_addr>       which is BLX(1)
9795         BLX <Rm>                which is BLX(2)
9796    Unfortunately, there are two different opcodes for this mnemonic.
9797    So, the insns[].value is not used, and the code here zaps values
9798         into inst.instruction.
9799
9800    ??? How to take advantage of the additional two bits of displacement
9801    available in Thumb32 mode?  Need new relocation?  */
9802
9803 static void
9804 do_t_blx (void)
9805 {
9806   set_it_insn_type_last ();
9807
9808   if (inst.operands[0].isreg)
9809     {
9810       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9811       /* We have a register, so this is BLX(2).  */
9812       inst.instruction |= inst.operands[0].reg << 3;
9813     }
9814   else
9815     {
9816       /* No register.  This must be BLX(1).  */
9817       inst.instruction = 0xf000e800;
9818       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
9819     }
9820 }
9821
9822 static void
9823 do_t_branch (void)
9824 {
9825   int opcode;
9826   int cond;
9827   int reloc;
9828
9829   cond = inst.cond;
9830   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
9831
9832   if (in_it_block ())
9833     {
9834       /* Conditional branches inside IT blocks are encoded as unconditional
9835          branches.  */
9836       cond = COND_ALWAYS;
9837     }
9838   else
9839     cond = inst.cond;
9840
9841   if (cond != COND_ALWAYS)
9842     opcode = T_MNEM_bcond;
9843   else
9844     opcode = inst.instruction;
9845
9846   if (unified_syntax
9847       && (inst.size_req == 4
9848           || (inst.size_req != 2
9849               && (inst.operands[0].hasreloc
9850                   || inst.reloc.exp.X_op == O_constant))))
9851     {
9852       inst.instruction = THUMB_OP32(opcode);
9853       if (cond == COND_ALWAYS)
9854         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
9855       else
9856         {
9857           gas_assert (cond != 0xF);
9858           inst.instruction |= cond << 22;
9859           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
9860         }
9861     }
9862   else
9863     {
9864       inst.instruction = THUMB_OP16(opcode);
9865       if (cond == COND_ALWAYS)
9866         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
9867       else
9868         {
9869           inst.instruction |= cond << 8;
9870           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
9871         }
9872       /* Allow section relaxation.  */
9873       if (unified_syntax && inst.size_req != 2)
9874         inst.relax = opcode;
9875     }
9876   inst.reloc.type = reloc;
9877   inst.reloc.pc_rel = 1;
9878 }
9879
9880 static void
9881 do_t_bkpt (void)
9882 {
9883   constraint (inst.cond != COND_ALWAYS,
9884               _("instruction is always unconditional"));
9885   if (inst.operands[0].present)
9886     {
9887       constraint (inst.operands[0].imm > 255,
9888                   _("immediate value out of range"));
9889       inst.instruction |= inst.operands[0].imm;
9890       set_it_insn_type (NEUTRAL_IT_INSN);
9891     }
9892 }
9893
9894 static void
9895 do_t_branch23 (void)
9896 {
9897   set_it_insn_type_last ();
9898   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
9899   
9900   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
9901      this file.  We used to simply ignore the PLT reloc type here --
9902      the branch encoding is now needed to deal with TLSCALL relocs.
9903      So if we see a PLT reloc now, put it back to how it used to be to
9904      keep the preexisting behaviour.  */
9905   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
9906     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
9907
9908 #if defined(OBJ_COFF)
9909   /* If the destination of the branch is a defined symbol which does not have
9910      the THUMB_FUNC attribute, then we must be calling a function which has
9911      the (interfacearm) attribute.  We look for the Thumb entry point to that
9912      function and change the branch to refer to that function instead.  */
9913   if (   inst.reloc.exp.X_op == O_symbol
9914       && inst.reloc.exp.X_add_symbol != NULL
9915       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
9916       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
9917     inst.reloc.exp.X_add_symbol =
9918       find_real_start (inst.reloc.exp.X_add_symbol);
9919 #endif
9920 }
9921
9922 static void
9923 do_t_bx (void)
9924 {
9925   set_it_insn_type_last ();
9926   inst.instruction |= inst.operands[0].reg << 3;
9927   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
9928      should cause the alignment to be checked once it is known.  This is
9929      because BX PC only works if the instruction is word aligned.  */
9930 }
9931
9932 static void
9933 do_t_bxj (void)
9934 {
9935   int Rm;
9936
9937   set_it_insn_type_last ();
9938   Rm = inst.operands[0].reg;
9939   reject_bad_reg (Rm);
9940   inst.instruction |= Rm << 16;
9941 }
9942
9943 static void
9944 do_t_clz (void)
9945 {
9946   unsigned Rd;
9947   unsigned Rm;
9948
9949   Rd = inst.operands[0].reg;
9950   Rm = inst.operands[1].reg;
9951
9952   reject_bad_reg (Rd);
9953   reject_bad_reg (Rm);
9954
9955   inst.instruction |= Rd << 8;
9956   inst.instruction |= Rm << 16;
9957   inst.instruction |= Rm;
9958 }
9959
9960 static void
9961 do_t_cps (void)
9962 {
9963   set_it_insn_type (OUTSIDE_IT_INSN);
9964   inst.instruction |= inst.operands[0].imm;
9965 }
9966
9967 static void
9968 do_t_cpsi (void)
9969 {
9970   set_it_insn_type (OUTSIDE_IT_INSN);
9971   if (unified_syntax
9972       && (inst.operands[1].present || inst.size_req == 4)
9973       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
9974     {
9975       unsigned int imod = (inst.instruction & 0x0030) >> 4;
9976       inst.instruction = 0xf3af8000;
9977       inst.instruction |= imod << 9;
9978       inst.instruction |= inst.operands[0].imm << 5;
9979       if (inst.operands[1].present)
9980         inst.instruction |= 0x100 | inst.operands[1].imm;
9981     }
9982   else
9983     {
9984       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
9985                   && (inst.operands[0].imm & 4),
9986                   _("selected processor does not support 'A' form "
9987                     "of this instruction"));
9988       constraint (inst.operands[1].present || inst.size_req == 4,
9989                   _("Thumb does not support the 2-argument "
9990                     "form of this instruction"));
9991       inst.instruction |= inst.operands[0].imm;
9992     }
9993 }
9994
9995 /* THUMB CPY instruction (argument parse).  */
9996
9997 static void
9998 do_t_cpy (void)
9999 {
10000   if (inst.size_req == 4)
10001     {
10002       inst.instruction = THUMB_OP32 (T_MNEM_mov);
10003       inst.instruction |= inst.operands[0].reg << 8;
10004       inst.instruction |= inst.operands[1].reg;
10005     }
10006   else
10007     {
10008       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
10009       inst.instruction |= (inst.operands[0].reg & 0x7);
10010       inst.instruction |= inst.operands[1].reg << 3;
10011     }
10012 }
10013
10014 static void
10015 do_t_cbz (void)
10016 {
10017   set_it_insn_type (OUTSIDE_IT_INSN);
10018   constraint (inst.operands[0].reg > 7, BAD_HIREG);
10019   inst.instruction |= inst.operands[0].reg;
10020   inst.reloc.pc_rel = 1;
10021   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
10022 }
10023
10024 static void
10025 do_t_dbg (void)
10026 {
10027   inst.instruction |= inst.operands[0].imm;
10028 }
10029
10030 static void
10031 do_t_div (void)
10032 {
10033   unsigned Rd, Rn, Rm;
10034
10035   Rd = inst.operands[0].reg;
10036   Rn = (inst.operands[1].present
10037         ? inst.operands[1].reg : Rd);
10038   Rm = inst.operands[2].reg;
10039
10040   reject_bad_reg (Rd);
10041   reject_bad_reg (Rn);
10042   reject_bad_reg (Rm);
10043
10044   inst.instruction |= Rd << 8;
10045   inst.instruction |= Rn << 16;
10046   inst.instruction |= Rm;
10047 }
10048
10049 static void
10050 do_t_hint (void)
10051 {
10052   if (unified_syntax && inst.size_req == 4)
10053     inst.instruction = THUMB_OP32 (inst.instruction);
10054   else
10055     inst.instruction = THUMB_OP16 (inst.instruction);
10056 }
10057
10058 static void
10059 do_t_it (void)
10060 {
10061   unsigned int cond = inst.operands[0].imm;
10062
10063   set_it_insn_type (IT_INSN);
10064   now_it.mask = (inst.instruction & 0xf) | 0x10;
10065   now_it.cc = cond;
10066
10067   /* If the condition is a negative condition, invert the mask.  */
10068   if ((cond & 0x1) == 0x0)
10069     {
10070       unsigned int mask = inst.instruction & 0x000f;
10071
10072       if ((mask & 0x7) == 0)
10073         /* no conversion needed */;
10074       else if ((mask & 0x3) == 0)
10075         mask ^= 0x8;
10076       else if ((mask & 0x1) == 0)
10077         mask ^= 0xC;
10078       else
10079         mask ^= 0xE;
10080
10081       inst.instruction &= 0xfff0;
10082       inst.instruction |= mask;
10083     }
10084
10085   inst.instruction |= cond << 4;
10086 }
10087
10088 /* Helper function used for both push/pop and ldm/stm.  */
10089 static void
10090 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
10091 {
10092   bfd_boolean load;
10093
10094   load = (inst.instruction & (1 << 20)) != 0;
10095
10096   if (mask & (1 << 13))
10097     inst.error =  _("SP not allowed in register list");
10098
10099   if ((mask & (1 << base)) != 0
10100       && writeback)
10101     inst.error = _("having the base register in the register list when "
10102                    "using write back is UNPREDICTABLE");
10103
10104   if (load)
10105     {
10106       if (mask & (1 << 15))
10107         {
10108           if (mask & (1 << 14))
10109             inst.error = _("LR and PC should not both be in register list");
10110           else
10111             set_it_insn_type_last ();
10112         }
10113     }
10114   else
10115     {
10116       if (mask & (1 << 15))
10117         inst.error = _("PC not allowed in register list");
10118     }
10119
10120   if ((mask & (mask - 1)) == 0)
10121     {
10122       /* Single register transfers implemented as str/ldr.  */
10123       if (writeback)
10124         {
10125           if (inst.instruction & (1 << 23))
10126             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
10127           else
10128             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
10129         }
10130       else
10131         {
10132           if (inst.instruction & (1 << 23))
10133             inst.instruction = 0x00800000; /* ia -> [base] */
10134           else
10135             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
10136         }
10137
10138       inst.instruction |= 0xf8400000;
10139       if (load)
10140         inst.instruction |= 0x00100000;
10141
10142       mask = ffs (mask) - 1;
10143       mask <<= 12;
10144     }
10145   else if (writeback)
10146     inst.instruction |= WRITE_BACK;
10147
10148   inst.instruction |= mask;
10149   inst.instruction |= base << 16;
10150 }
10151
10152 static void
10153 do_t_ldmstm (void)
10154 {
10155   /* This really doesn't seem worth it.  */
10156   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
10157               _("expression too complex"));
10158   constraint (inst.operands[1].writeback,
10159               _("Thumb load/store multiple does not support {reglist}^"));
10160
10161   if (unified_syntax)
10162     {
10163       bfd_boolean narrow;
10164       unsigned mask;
10165
10166       narrow = FALSE;
10167       /* See if we can use a 16-bit instruction.  */
10168       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
10169           && inst.size_req != 4
10170           && !(inst.operands[1].imm & ~0xff))
10171         {
10172           mask = 1 << inst.operands[0].reg;
10173
10174           if (inst.operands[0].reg <= 7)
10175             {
10176               if (inst.instruction == T_MNEM_stmia
10177                   ? inst.operands[0].writeback
10178                   : (inst.operands[0].writeback
10179                      == !(inst.operands[1].imm & mask)))
10180                 {
10181                   if (inst.instruction == T_MNEM_stmia
10182                       && (inst.operands[1].imm & mask)
10183                       && (inst.operands[1].imm & (mask - 1)))
10184                     as_warn (_("value stored for r%d is UNKNOWN"),
10185                              inst.operands[0].reg);
10186
10187                   inst.instruction = THUMB_OP16 (inst.instruction);
10188                   inst.instruction |= inst.operands[0].reg << 8;
10189                   inst.instruction |= inst.operands[1].imm;
10190                   narrow = TRUE;
10191                 }
10192               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
10193                 {
10194                   /* This means 1 register in reg list one of 3 situations:
10195                      1. Instruction is stmia, but without writeback.
10196                      2. lmdia without writeback, but with Rn not in
10197                         reglist.
10198                      3. ldmia with writeback, but with Rn in reglist.
10199                      Case 3 is UNPREDICTABLE behaviour, so we handle
10200                      case 1 and 2 which can be converted into a 16-bit
10201                      str or ldr. The SP cases are handled below.  */
10202                   unsigned long opcode;
10203                   /* First, record an error for Case 3.  */
10204                   if (inst.operands[1].imm & mask
10205                       && inst.operands[0].writeback)
10206                     inst.error = 
10207                         _("having the base register in the register list when "
10208                           "using write back is UNPREDICTABLE");
10209                     
10210                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str 
10211                                                              : T_MNEM_ldr);
10212                   inst.instruction = THUMB_OP16 (opcode);
10213                   inst.instruction |= inst.operands[0].reg << 3;
10214                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
10215                   narrow = TRUE;
10216                 }
10217             }
10218           else if (inst.operands[0] .reg == REG_SP)
10219             {
10220               if (inst.operands[0].writeback)
10221                 {
10222                   inst.instruction = 
10223                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
10224                                     ? T_MNEM_push : T_MNEM_pop);
10225                   inst.instruction |= inst.operands[1].imm;
10226                   narrow = TRUE;
10227                 }
10228               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
10229                 {
10230                   inst.instruction = 
10231                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
10232                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
10233                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
10234                   narrow = TRUE;
10235                 }
10236             }
10237         }
10238
10239       if (!narrow)
10240         {
10241           if (inst.instruction < 0xffff)
10242             inst.instruction = THUMB_OP32 (inst.instruction);
10243
10244           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
10245                                 inst.operands[0].writeback);
10246         }
10247     }
10248   else
10249     {
10250       constraint (inst.operands[0].reg > 7
10251                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
10252       constraint (inst.instruction != T_MNEM_ldmia
10253                   && inst.instruction != T_MNEM_stmia,
10254                   _("Thumb-2 instruction only valid in unified syntax"));
10255       if (inst.instruction == T_MNEM_stmia)
10256         {
10257           if (!inst.operands[0].writeback)
10258             as_warn (_("this instruction will write back the base register"));
10259           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
10260               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
10261             as_warn (_("value stored for r%d is UNKNOWN"),
10262                      inst.operands[0].reg);
10263         }
10264       else
10265         {
10266           if (!inst.operands[0].writeback
10267               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
10268             as_warn (_("this instruction will write back the base register"));
10269           else if (inst.operands[0].writeback
10270                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
10271             as_warn (_("this instruction will not write back the base register"));
10272         }
10273
10274       inst.instruction = THUMB_OP16 (inst.instruction);
10275       inst.instruction |= inst.operands[0].reg << 8;
10276       inst.instruction |= inst.operands[1].imm;
10277     }
10278 }
10279
10280 static void
10281 do_t_ldrex (void)
10282 {
10283   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
10284               || inst.operands[1].postind || inst.operands[1].writeback
10285               || inst.operands[1].immisreg || inst.operands[1].shifted
10286               || inst.operands[1].negative,
10287               BAD_ADDR_MODE);
10288
10289   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
10290
10291   inst.instruction |= inst.operands[0].reg << 12;
10292   inst.instruction |= inst.operands[1].reg << 16;
10293   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
10294 }
10295
10296 static void
10297 do_t_ldrexd (void)
10298 {
10299   if (!inst.operands[1].present)
10300     {
10301       constraint (inst.operands[0].reg == REG_LR,
10302                   _("r14 not allowed as first register "
10303                     "when second register is omitted"));
10304       inst.operands[1].reg = inst.operands[0].reg + 1;
10305     }
10306   constraint (inst.operands[0].reg == inst.operands[1].reg,
10307               BAD_OVERLAP);
10308
10309   inst.instruction |= inst.operands[0].reg << 12;
10310   inst.instruction |= inst.operands[1].reg << 8;
10311   inst.instruction |= inst.operands[2].reg << 16;
10312 }
10313
10314 static void
10315 do_t_ldst (void)
10316 {
10317   unsigned long opcode;
10318   int Rn;
10319
10320   if (inst.operands[0].isreg
10321       && !inst.operands[0].preind
10322       && inst.operands[0].reg == REG_PC)
10323     set_it_insn_type_last ();
10324
10325   opcode = inst.instruction;
10326   if (unified_syntax)
10327     {
10328       if (!inst.operands[1].isreg)
10329         {
10330           if (opcode <= 0xffff)
10331             inst.instruction = THUMB_OP32 (opcode);
10332           if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
10333             return;
10334         }
10335       if (inst.operands[1].isreg
10336           && !inst.operands[1].writeback
10337           && !inst.operands[1].shifted && !inst.operands[1].postind
10338           && !inst.operands[1].negative && inst.operands[0].reg <= 7
10339           && opcode <= 0xffff
10340           && inst.size_req != 4)
10341         {
10342           /* Insn may have a 16-bit form.  */
10343           Rn = inst.operands[1].reg;
10344           if (inst.operands[1].immisreg)
10345             {
10346               inst.instruction = THUMB_OP16 (opcode);
10347               /* [Rn, Rik] */
10348               if (Rn <= 7 && inst.operands[1].imm <= 7)
10349                 goto op16;
10350               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
10351                 reject_bad_reg (inst.operands[1].imm);
10352             }
10353           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
10354                     && opcode != T_MNEM_ldrsb)
10355                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
10356                    || (Rn == REG_SP && opcode == T_MNEM_str))
10357             {
10358               /* [Rn, #const] */
10359               if (Rn > 7)
10360                 {
10361                   if (Rn == REG_PC)
10362                     {
10363                       if (inst.reloc.pc_rel)
10364                         opcode = T_MNEM_ldr_pc2;
10365                       else
10366                         opcode = T_MNEM_ldr_pc;
10367                     }
10368                   else
10369                     {
10370                       if (opcode == T_MNEM_ldr)
10371                         opcode = T_MNEM_ldr_sp;
10372                       else
10373                         opcode = T_MNEM_str_sp;
10374                     }
10375                   inst.instruction = inst.operands[0].reg << 8;
10376                 }
10377               else
10378                 {
10379                   inst.instruction = inst.operands[0].reg;
10380                   inst.instruction |= inst.operands[1].reg << 3;
10381                 }
10382               inst.instruction |= THUMB_OP16 (opcode);
10383               if (inst.size_req == 2)
10384                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10385               else
10386                 inst.relax = opcode;
10387               return;
10388             }
10389         }
10390       /* Definitely a 32-bit variant.  */
10391
10392       /* Warning for Erratum 752419.  */
10393       if (opcode == T_MNEM_ldr
10394           && inst.operands[0].reg == REG_SP
10395           && inst.operands[1].writeback == 1
10396           && !inst.operands[1].immisreg)
10397         {
10398           if (no_cpu_selected ()
10399               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
10400                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
10401                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
10402             as_warn (_("This instruction may be unpredictable "
10403                        "if executed on M-profile cores "
10404                        "with interrupts enabled."));
10405         }
10406
10407       /* Do some validations regarding addressing modes.  */
10408       if (inst.operands[1].immisreg && opcode != T_MNEM_ldr
10409           && opcode != T_MNEM_str)
10410         reject_bad_reg (inst.operands[1].imm);
10411
10412       inst.instruction = THUMB_OP32 (opcode);
10413       inst.instruction |= inst.operands[0].reg << 12;
10414       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
10415       return;
10416     }
10417
10418   constraint (inst.operands[0].reg > 7, BAD_HIREG);
10419
10420   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
10421     {
10422       /* Only [Rn,Rm] is acceptable.  */
10423       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
10424       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
10425                   || inst.operands[1].postind || inst.operands[1].shifted
10426                   || inst.operands[1].negative,
10427                   _("Thumb does not support this addressing mode"));
10428       inst.instruction = THUMB_OP16 (inst.instruction);
10429       goto op16;
10430     }
10431
10432   inst.instruction = THUMB_OP16 (inst.instruction);
10433   if (!inst.operands[1].isreg)
10434     if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
10435       return;
10436
10437   constraint (!inst.operands[1].preind
10438               || inst.operands[1].shifted
10439               || inst.operands[1].writeback,
10440               _("Thumb does not support this addressing mode"));
10441   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
10442     {
10443       constraint (inst.instruction & 0x0600,
10444                   _("byte or halfword not valid for base register"));
10445       constraint (inst.operands[1].reg == REG_PC
10446                   && !(inst.instruction & THUMB_LOAD_BIT),
10447                   _("r15 based store not allowed"));
10448       constraint (inst.operands[1].immisreg,
10449                   _("invalid base register for register offset"));
10450
10451       if (inst.operands[1].reg == REG_PC)
10452         inst.instruction = T_OPCODE_LDR_PC;
10453       else if (inst.instruction & THUMB_LOAD_BIT)
10454         inst.instruction = T_OPCODE_LDR_SP;
10455       else
10456         inst.instruction = T_OPCODE_STR_SP;
10457
10458       inst.instruction |= inst.operands[0].reg << 8;
10459       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10460       return;
10461     }
10462
10463   constraint (inst.operands[1].reg > 7, BAD_HIREG);
10464   if (!inst.operands[1].immisreg)
10465     {
10466       /* Immediate offset.  */
10467       inst.instruction |= inst.operands[0].reg;
10468       inst.instruction |= inst.operands[1].reg << 3;
10469       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10470       return;
10471     }
10472
10473   /* Register offset.  */
10474   constraint (inst.operands[1].imm > 7, BAD_HIREG);
10475   constraint (inst.operands[1].negative,
10476               _("Thumb does not support this addressing mode"));
10477
10478  op16:
10479   switch (inst.instruction)
10480     {
10481     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
10482     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
10483     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
10484     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
10485     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
10486     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
10487     case 0x5600 /* ldrsb */:
10488     case 0x5e00 /* ldrsh */: break;
10489     default: abort ();
10490     }
10491
10492   inst.instruction |= inst.operands[0].reg;
10493   inst.instruction |= inst.operands[1].reg << 3;
10494   inst.instruction |= inst.operands[1].imm << 6;
10495 }
10496
10497 static void
10498 do_t_ldstd (void)
10499 {
10500   if (!inst.operands[1].present)
10501     {
10502       inst.operands[1].reg = inst.operands[0].reg + 1;
10503       constraint (inst.operands[0].reg == REG_LR,
10504                   _("r14 not allowed here"));
10505     }
10506   inst.instruction |= inst.operands[0].reg << 12;
10507   inst.instruction |= inst.operands[1].reg << 8;
10508   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
10509 }
10510
10511 static void
10512 do_t_ldstt (void)
10513 {
10514   inst.instruction |= inst.operands[0].reg << 12;
10515   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
10516 }
10517
10518 static void
10519 do_t_mla (void)
10520 {
10521   unsigned Rd, Rn, Rm, Ra;
10522
10523   Rd = inst.operands[0].reg;
10524   Rn = inst.operands[1].reg;
10525   Rm = inst.operands[2].reg;
10526   Ra = inst.operands[3].reg;
10527
10528   reject_bad_reg (Rd);
10529   reject_bad_reg (Rn);
10530   reject_bad_reg (Rm);
10531   reject_bad_reg (Ra);
10532
10533   inst.instruction |= Rd << 8;
10534   inst.instruction |= Rn << 16;
10535   inst.instruction |= Rm;
10536   inst.instruction |= Ra << 12;
10537 }
10538
10539 static void
10540 do_t_mlal (void)
10541 {
10542   unsigned RdLo, RdHi, Rn, Rm;
10543
10544   RdLo = inst.operands[0].reg;
10545   RdHi = inst.operands[1].reg;
10546   Rn = inst.operands[2].reg;
10547   Rm = inst.operands[3].reg;
10548
10549   reject_bad_reg (RdLo);
10550   reject_bad_reg (RdHi);
10551   reject_bad_reg (Rn);
10552   reject_bad_reg (Rm);
10553
10554   inst.instruction |= RdLo << 12;
10555   inst.instruction |= RdHi << 8;
10556   inst.instruction |= Rn << 16;
10557   inst.instruction |= Rm;
10558 }
10559
10560 static void
10561 do_t_mov_cmp (void)
10562 {
10563   unsigned Rn, Rm;
10564
10565   Rn = inst.operands[0].reg;
10566   Rm = inst.operands[1].reg;
10567
10568   if (Rn == REG_PC)
10569     set_it_insn_type_last ();
10570
10571   if (unified_syntax)
10572     {
10573       int r0off = (inst.instruction == T_MNEM_mov
10574                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
10575       unsigned long opcode;
10576       bfd_boolean narrow;
10577       bfd_boolean low_regs;
10578
10579       low_regs = (Rn <= 7 && Rm <= 7);
10580       opcode = inst.instruction;
10581       if (in_it_block ())
10582         narrow = opcode != T_MNEM_movs;
10583       else
10584         narrow = opcode != T_MNEM_movs || low_regs;
10585       if (inst.size_req == 4
10586           || inst.operands[1].shifted)
10587         narrow = FALSE;
10588
10589       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
10590       if (opcode == T_MNEM_movs && inst.operands[1].isreg
10591           && !inst.operands[1].shifted
10592           && Rn == REG_PC
10593           && Rm == REG_LR)
10594         {
10595           inst.instruction = T2_SUBS_PC_LR;
10596           return;
10597         }
10598
10599       if (opcode == T_MNEM_cmp)
10600         {
10601           constraint (Rn == REG_PC, BAD_PC);
10602           if (narrow)
10603             {
10604               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
10605                  but valid.  */
10606               warn_deprecated_sp (Rm);
10607               /* R15 was documented as a valid choice for Rm in ARMv6,
10608                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
10609                  tools reject R15, so we do too.  */
10610               constraint (Rm == REG_PC, BAD_PC);
10611             }
10612           else
10613             reject_bad_reg (Rm);
10614         }
10615       else if (opcode == T_MNEM_mov
10616                || opcode == T_MNEM_movs)
10617         {
10618           if (inst.operands[1].isreg)
10619             {
10620               if (opcode == T_MNEM_movs)
10621                 {
10622                   reject_bad_reg (Rn);
10623                   reject_bad_reg (Rm);
10624                 }
10625               else if (narrow)
10626                 {
10627                   /* This is mov.n.  */
10628                   if ((Rn == REG_SP || Rn == REG_PC)
10629                       && (Rm == REG_SP || Rm == REG_PC))
10630                     {
10631                       as_warn (_("Use of r%u as a source register is "
10632                                  "deprecated when r%u is the destination "
10633                                  "register."), Rm, Rn);
10634                     }
10635                 }
10636               else
10637                 {
10638                   /* This is mov.w.  */
10639                   constraint (Rn == REG_PC, BAD_PC);
10640                   constraint (Rm == REG_PC, BAD_PC);
10641                   constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
10642                 }
10643             }
10644           else
10645             reject_bad_reg (Rn);
10646         }
10647
10648       if (!inst.operands[1].isreg)
10649         {
10650           /* Immediate operand.  */
10651           if (!in_it_block () && opcode == T_MNEM_mov)
10652             narrow = 0;
10653           if (low_regs && narrow)
10654             {
10655               inst.instruction = THUMB_OP16 (opcode);
10656               inst.instruction |= Rn << 8;
10657               if (inst.size_req == 2)
10658                 inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
10659               else
10660                 inst.relax = opcode;
10661             }
10662           else
10663             {
10664               inst.instruction = THUMB_OP32 (inst.instruction);
10665               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10666               inst.instruction |= Rn << r0off;
10667               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10668             }
10669         }
10670       else if (inst.operands[1].shifted && inst.operands[1].immisreg
10671                && (inst.instruction == T_MNEM_mov
10672                    || inst.instruction == T_MNEM_movs))
10673         {
10674           /* Register shifts are encoded as separate shift instructions.  */
10675           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
10676
10677           if (in_it_block ())
10678             narrow = !flags;
10679           else
10680             narrow = flags;
10681
10682           if (inst.size_req == 4)
10683             narrow = FALSE;
10684
10685           if (!low_regs || inst.operands[1].imm > 7)
10686             narrow = FALSE;
10687
10688           if (Rn != Rm)
10689             narrow = FALSE;
10690
10691           switch (inst.operands[1].shift_kind)
10692             {
10693             case SHIFT_LSL:
10694               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
10695               break;
10696             case SHIFT_ASR:
10697               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
10698               break;
10699             case SHIFT_LSR:
10700               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
10701               break;
10702             case SHIFT_ROR:
10703               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
10704               break;
10705             default:
10706               abort ();
10707             }
10708
10709           inst.instruction = opcode;
10710           if (narrow)
10711             {
10712               inst.instruction |= Rn;
10713               inst.instruction |= inst.operands[1].imm << 3;
10714             }
10715           else
10716             {
10717               if (flags)
10718                 inst.instruction |= CONDS_BIT;
10719
10720               inst.instruction |= Rn << 8;
10721               inst.instruction |= Rm << 16;
10722               inst.instruction |= inst.operands[1].imm;
10723             }
10724         }
10725       else if (!narrow)
10726         {
10727           /* Some mov with immediate shift have narrow variants.
10728              Register shifts are handled above.  */
10729           if (low_regs && inst.operands[1].shifted
10730               && (inst.instruction == T_MNEM_mov
10731                   || inst.instruction == T_MNEM_movs))
10732             {
10733               if (in_it_block ())
10734                 narrow = (inst.instruction == T_MNEM_mov);
10735               else
10736                 narrow = (inst.instruction == T_MNEM_movs);
10737             }
10738
10739           if (narrow)
10740             {
10741               switch (inst.operands[1].shift_kind)
10742                 {
10743                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
10744                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
10745                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
10746                 default: narrow = FALSE; break;
10747                 }
10748             }
10749
10750           if (narrow)
10751             {
10752               inst.instruction |= Rn;
10753               inst.instruction |= Rm << 3;
10754               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
10755             }
10756           else
10757             {
10758               inst.instruction = THUMB_OP32 (inst.instruction);
10759               inst.instruction |= Rn << r0off;
10760               encode_thumb32_shifted_operand (1);
10761             }
10762         }
10763       else
10764         switch (inst.instruction)
10765           {
10766           case T_MNEM_mov:
10767             inst.instruction = T_OPCODE_MOV_HR;
10768             inst.instruction |= (Rn & 0x8) << 4;
10769             inst.instruction |= (Rn & 0x7);
10770             inst.instruction |= Rm << 3;
10771             break;
10772
10773           case T_MNEM_movs:
10774             /* We know we have low registers at this point.
10775                Generate LSLS Rd, Rs, #0.  */
10776             inst.instruction = T_OPCODE_LSL_I;
10777             inst.instruction |= Rn;
10778             inst.instruction |= Rm << 3;
10779             break;
10780
10781           case T_MNEM_cmp:
10782             if (low_regs)
10783               {
10784                 inst.instruction = T_OPCODE_CMP_LR;
10785                 inst.instruction |= Rn;
10786                 inst.instruction |= Rm << 3;
10787               }
10788             else
10789               {
10790                 inst.instruction = T_OPCODE_CMP_HR;
10791                 inst.instruction |= (Rn & 0x8) << 4;
10792                 inst.instruction |= (Rn & 0x7);
10793                 inst.instruction |= Rm << 3;
10794               }
10795             break;
10796           }
10797       return;
10798     }
10799
10800   inst.instruction = THUMB_OP16 (inst.instruction);
10801
10802   /* PR 10443: Do not silently ignore shifted operands.  */
10803   constraint (inst.operands[1].shifted,
10804               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
10805
10806   if (inst.operands[1].isreg)
10807     {
10808       if (Rn < 8 && Rm < 8)
10809         {
10810           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
10811              since a MOV instruction produces unpredictable results.  */
10812           if (inst.instruction == T_OPCODE_MOV_I8)
10813             inst.instruction = T_OPCODE_ADD_I3;
10814           else
10815             inst.instruction = T_OPCODE_CMP_LR;
10816
10817           inst.instruction |= Rn;
10818           inst.instruction |= Rm << 3;
10819         }
10820       else
10821         {
10822           if (inst.instruction == T_OPCODE_MOV_I8)
10823             inst.instruction = T_OPCODE_MOV_HR;
10824           else
10825             inst.instruction = T_OPCODE_CMP_HR;
10826           do_t_cpy ();
10827         }
10828     }
10829   else
10830     {
10831       constraint (Rn > 7,
10832                   _("only lo regs allowed with immediate"));
10833       inst.instruction |= Rn << 8;
10834       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
10835     }
10836 }
10837
10838 static void
10839 do_t_mov16 (void)
10840 {
10841   unsigned Rd;
10842   bfd_vma imm;
10843   bfd_boolean top;
10844
10845   top = (inst.instruction & 0x00800000) != 0;
10846   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
10847     {
10848       constraint (top, _(":lower16: not allowed this instruction"));
10849       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
10850     }
10851   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
10852     {
10853       constraint (!top, _(":upper16: not allowed this instruction"));
10854       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
10855     }
10856
10857   Rd = inst.operands[0].reg;
10858   reject_bad_reg (Rd);
10859
10860   inst.instruction |= Rd << 8;
10861   if (inst.reloc.type == BFD_RELOC_UNUSED)
10862     {
10863       imm = inst.reloc.exp.X_add_number;
10864       inst.instruction |= (imm & 0xf000) << 4;
10865       inst.instruction |= (imm & 0x0800) << 15;
10866       inst.instruction |= (imm & 0x0700) << 4;
10867       inst.instruction |= (imm & 0x00ff);
10868     }
10869 }
10870
10871 static void
10872 do_t_mvn_tst (void)
10873 {
10874   unsigned Rn, Rm;
10875
10876   Rn = inst.operands[0].reg;
10877   Rm = inst.operands[1].reg;
10878
10879   if (inst.instruction == T_MNEM_cmp
10880       || inst.instruction == T_MNEM_cmn)
10881     constraint (Rn == REG_PC, BAD_PC);
10882   else
10883     reject_bad_reg (Rn);
10884   reject_bad_reg (Rm);
10885
10886   if (unified_syntax)
10887     {
10888       int r0off = (inst.instruction == T_MNEM_mvn
10889                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
10890       bfd_boolean narrow;
10891
10892       if (inst.size_req == 4
10893           || inst.instruction > 0xffff
10894           || inst.operands[1].shifted
10895           || Rn > 7 || Rm > 7)
10896         narrow = FALSE;
10897       else if (inst.instruction == T_MNEM_cmn)
10898         narrow = TRUE;
10899       else if (THUMB_SETS_FLAGS (inst.instruction))
10900         narrow = !in_it_block ();
10901       else
10902         narrow = in_it_block ();
10903
10904       if (!inst.operands[1].isreg)
10905         {
10906           /* For an immediate, we always generate a 32-bit opcode;
10907              section relaxation will shrink it later if possible.  */
10908           if (inst.instruction < 0xffff)
10909             inst.instruction = THUMB_OP32 (inst.instruction);
10910           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10911           inst.instruction |= Rn << r0off;
10912           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10913         }
10914       else
10915         {
10916           /* See if we can do this with a 16-bit instruction.  */
10917           if (narrow)
10918             {
10919               inst.instruction = THUMB_OP16 (inst.instruction);
10920               inst.instruction |= Rn;
10921               inst.instruction |= Rm << 3;
10922             }
10923           else
10924             {
10925               constraint (inst.operands[1].shifted
10926                           && inst.operands[1].immisreg,
10927                           _("shift must be constant"));
10928               if (inst.instruction < 0xffff)
10929                 inst.instruction = THUMB_OP32 (inst.instruction);
10930               inst.instruction |= Rn << r0off;
10931               encode_thumb32_shifted_operand (1);
10932             }
10933         }
10934     }
10935   else
10936     {
10937       constraint (inst.instruction > 0xffff
10938                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
10939       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
10940                   _("unshifted register required"));
10941       constraint (Rn > 7 || Rm > 7,
10942                   BAD_HIREG);
10943
10944       inst.instruction = THUMB_OP16 (inst.instruction);
10945       inst.instruction |= Rn;
10946       inst.instruction |= Rm << 3;
10947     }
10948 }
10949
10950 static void
10951 do_t_mrs (void)
10952 {
10953   unsigned Rd;
10954
10955   if (do_vfp_nsyn_mrs () == SUCCESS)
10956     return;
10957
10958   Rd = inst.operands[0].reg;
10959   reject_bad_reg (Rd);
10960   inst.instruction |= Rd << 8;
10961
10962   if (inst.operands[1].isreg)
10963     {
10964       unsigned br = inst.operands[1].reg;
10965       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
10966         as_bad (_("bad register for mrs"));
10967
10968       inst.instruction |= br & (0xf << 16);
10969       inst.instruction |= (br & 0x300) >> 4;
10970       inst.instruction |= (br & SPSR_BIT) >> 2;
10971     }
10972   else
10973     {
10974       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
10975
10976       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
10977         constraint (flags != 0, _("selected processor does not support "
10978                     "requested special purpose register"));
10979       else
10980         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
10981            devices).  */
10982         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
10983                     _("'APSR', 'CPSR' or 'SPSR' expected"));
10984
10985       inst.instruction |= (flags & SPSR_BIT) >> 2;
10986       inst.instruction |= inst.operands[1].imm & 0xff;
10987       inst.instruction |= 0xf0000;
10988     }
10989 }
10990
10991 static void
10992 do_t_msr (void)
10993 {
10994   int flags;
10995   unsigned Rn;
10996
10997   if (do_vfp_nsyn_msr () == SUCCESS)
10998     return;
10999
11000   constraint (!inst.operands[1].isreg,
11001               _("Thumb encoding does not support an immediate here"));
11002
11003   if (inst.operands[0].isreg)
11004     flags = (int)(inst.operands[0].reg);
11005   else
11006     flags = inst.operands[0].imm;
11007
11008   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
11009     {
11010       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
11011
11012       constraint ((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
11013                    && (bits & ~(PSR_s | PSR_f)) != 0)
11014                   || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
11015                       && bits != PSR_f),
11016                   _("selected processor does not support requested special "
11017                     "purpose register"));
11018     }
11019   else
11020      constraint ((flags & 0xff) != 0, _("selected processor does not support "
11021                  "requested special purpose register"));
11022
11023   Rn = inst.operands[1].reg;
11024   reject_bad_reg (Rn);
11025
11026   inst.instruction |= (flags & SPSR_BIT) >> 2;
11027   inst.instruction |= (flags & 0xf0000) >> 8;
11028   inst.instruction |= (flags & 0x300) >> 4;
11029   inst.instruction |= (flags & 0xff);
11030   inst.instruction |= Rn << 16;
11031 }
11032
11033 static void
11034 do_t_mul (void)
11035 {
11036   bfd_boolean narrow;
11037   unsigned Rd, Rn, Rm;
11038
11039   if (!inst.operands[2].present)
11040     inst.operands[2].reg = inst.operands[0].reg;
11041
11042   Rd = inst.operands[0].reg;
11043   Rn = inst.operands[1].reg;
11044   Rm = inst.operands[2].reg;
11045
11046   if (unified_syntax)
11047     {
11048       if (inst.size_req == 4
11049           || (Rd != Rn
11050               && Rd != Rm)
11051           || Rn > 7
11052           || Rm > 7)
11053         narrow = FALSE;
11054       else if (inst.instruction == T_MNEM_muls)
11055         narrow = !in_it_block ();
11056       else
11057         narrow = in_it_block ();
11058     }
11059   else
11060     {
11061       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
11062       constraint (Rn > 7 || Rm > 7,
11063                   BAD_HIREG);
11064       narrow = TRUE;
11065     }
11066
11067   if (narrow)
11068     {
11069       /* 16-bit MULS/Conditional MUL.  */
11070       inst.instruction = THUMB_OP16 (inst.instruction);
11071       inst.instruction |= Rd;
11072
11073       if (Rd == Rn)
11074         inst.instruction |= Rm << 3;
11075       else if (Rd == Rm)
11076         inst.instruction |= Rn << 3;
11077       else
11078         constraint (1, _("dest must overlap one source register"));
11079     }
11080   else
11081     {
11082       constraint (inst.instruction != T_MNEM_mul,
11083                   _("Thumb-2 MUL must not set flags"));
11084       /* 32-bit MUL.  */
11085       inst.instruction = THUMB_OP32 (inst.instruction);
11086       inst.instruction |= Rd << 8;
11087       inst.instruction |= Rn << 16;
11088       inst.instruction |= Rm << 0;
11089
11090       reject_bad_reg (Rd);
11091       reject_bad_reg (Rn);
11092       reject_bad_reg (Rm);
11093     }
11094 }
11095
11096 static void
11097 do_t_mull (void)
11098 {
11099   unsigned RdLo, RdHi, Rn, Rm;
11100
11101   RdLo = inst.operands[0].reg;
11102   RdHi = inst.operands[1].reg;
11103   Rn = inst.operands[2].reg;
11104   Rm = inst.operands[3].reg;
11105
11106   reject_bad_reg (RdLo);
11107   reject_bad_reg (RdHi);
11108   reject_bad_reg (Rn);
11109   reject_bad_reg (Rm);
11110
11111   inst.instruction |= RdLo << 12;
11112   inst.instruction |= RdHi << 8;
11113   inst.instruction |= Rn << 16;
11114   inst.instruction |= Rm;
11115
11116  if (RdLo == RdHi)
11117     as_tsktsk (_("rdhi and rdlo must be different"));
11118 }
11119
11120 static void
11121 do_t_nop (void)
11122 {
11123   set_it_insn_type (NEUTRAL_IT_INSN);
11124
11125   if (unified_syntax)
11126     {
11127       if (inst.size_req == 4 || inst.operands[0].imm > 15)
11128         {
11129           inst.instruction = THUMB_OP32 (inst.instruction);
11130           inst.instruction |= inst.operands[0].imm;
11131         }
11132       else
11133         {
11134           /* PR9722: Check for Thumb2 availability before
11135              generating a thumb2 nop instruction.  */
11136           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
11137             {
11138               inst.instruction = THUMB_OP16 (inst.instruction);
11139               inst.instruction |= inst.operands[0].imm << 4;
11140             }
11141           else
11142             inst.instruction = 0x46c0;
11143         }
11144     }
11145   else
11146     {
11147       constraint (inst.operands[0].present,
11148                   _("Thumb does not support NOP with hints"));
11149       inst.instruction = 0x46c0;
11150     }
11151 }
11152
11153 static void
11154 do_t_neg (void)
11155 {
11156   if (unified_syntax)
11157     {
11158       bfd_boolean narrow;
11159
11160       if (THUMB_SETS_FLAGS (inst.instruction))
11161         narrow = !in_it_block ();
11162       else
11163         narrow = in_it_block ();
11164       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
11165         narrow = FALSE;
11166       if (inst.size_req == 4)
11167         narrow = FALSE;
11168
11169       if (!narrow)
11170         {
11171           inst.instruction = THUMB_OP32 (inst.instruction);
11172           inst.instruction |= inst.operands[0].reg << 8;
11173           inst.instruction |= inst.operands[1].reg << 16;
11174         }
11175       else
11176         {
11177           inst.instruction = THUMB_OP16 (inst.instruction);
11178           inst.instruction |= inst.operands[0].reg;
11179           inst.instruction |= inst.operands[1].reg << 3;
11180         }
11181     }
11182   else
11183     {
11184       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
11185                   BAD_HIREG);
11186       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11187
11188       inst.instruction = THUMB_OP16 (inst.instruction);
11189       inst.instruction |= inst.operands[0].reg;
11190       inst.instruction |= inst.operands[1].reg << 3;
11191     }
11192 }
11193
11194 static void
11195 do_t_orn (void)
11196 {
11197   unsigned Rd, Rn;
11198
11199   Rd = inst.operands[0].reg;
11200   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
11201
11202   reject_bad_reg (Rd);
11203   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
11204   reject_bad_reg (Rn);
11205
11206   inst.instruction |= Rd << 8;
11207   inst.instruction |= Rn << 16;
11208
11209   if (!inst.operands[2].isreg)
11210     {
11211       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11212       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11213     }
11214   else
11215     {
11216       unsigned Rm;
11217
11218       Rm = inst.operands[2].reg;
11219       reject_bad_reg (Rm);
11220
11221       constraint (inst.operands[2].shifted
11222                   && inst.operands[2].immisreg,
11223                   _("shift must be constant"));
11224       encode_thumb32_shifted_operand (2);
11225     }
11226 }
11227
11228 static void
11229 do_t_pkhbt (void)
11230 {
11231   unsigned Rd, Rn, Rm;
11232
11233   Rd = inst.operands[0].reg;
11234   Rn = inst.operands[1].reg;
11235   Rm = inst.operands[2].reg;
11236
11237   reject_bad_reg (Rd);
11238   reject_bad_reg (Rn);
11239   reject_bad_reg (Rm);
11240
11241   inst.instruction |= Rd << 8;
11242   inst.instruction |= Rn << 16;
11243   inst.instruction |= Rm;
11244   if (inst.operands[3].present)
11245     {
11246       unsigned int val = inst.reloc.exp.X_add_number;
11247       constraint (inst.reloc.exp.X_op != O_constant,
11248                   _("expression too complex"));
11249       inst.instruction |= (val & 0x1c) << 10;
11250       inst.instruction |= (val & 0x03) << 6;
11251     }
11252 }
11253
11254 static void
11255 do_t_pkhtb (void)
11256 {
11257   if (!inst.operands[3].present)
11258     {
11259       unsigned Rtmp;
11260
11261       inst.instruction &= ~0x00000020;
11262
11263       /* PR 10168.  Swap the Rm and Rn registers.  */
11264       Rtmp = inst.operands[1].reg;
11265       inst.operands[1].reg = inst.operands[2].reg;
11266       inst.operands[2].reg = Rtmp;
11267     }
11268   do_t_pkhbt ();
11269 }
11270
11271 static void
11272 do_t_pld (void)
11273 {
11274   if (inst.operands[0].immisreg)
11275     reject_bad_reg (inst.operands[0].imm);
11276
11277   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
11278 }
11279
11280 static void
11281 do_t_push_pop (void)
11282 {
11283   unsigned mask;
11284
11285   constraint (inst.operands[0].writeback,
11286               _("push/pop do not support {reglist}^"));
11287   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11288               _("expression too complex"));
11289
11290   mask = inst.operands[0].imm;
11291   if ((mask & ~0xff) == 0)
11292     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
11293   else if ((inst.instruction == T_MNEM_push
11294             && (mask & ~0xff) == 1 << REG_LR)
11295            || (inst.instruction == T_MNEM_pop
11296                && (mask & ~0xff) == 1 << REG_PC))
11297     {
11298       inst.instruction = THUMB_OP16 (inst.instruction);
11299       inst.instruction |= THUMB_PP_PC_LR;
11300       inst.instruction |= mask & 0xff;
11301     }
11302   else if (unified_syntax)
11303     {
11304       inst.instruction = THUMB_OP32 (inst.instruction);
11305       encode_thumb2_ldmstm (13, mask, TRUE);
11306     }
11307   else
11308     {
11309       inst.error = _("invalid register list to push/pop instruction");
11310       return;
11311     }
11312 }
11313
11314 static void
11315 do_t_rbit (void)
11316 {
11317   unsigned Rd, Rm;
11318
11319   Rd = inst.operands[0].reg;
11320   Rm = inst.operands[1].reg;
11321
11322   reject_bad_reg (Rd);
11323   reject_bad_reg (Rm);
11324
11325   inst.instruction |= Rd << 8;
11326   inst.instruction |= Rm << 16;
11327   inst.instruction |= Rm;
11328 }
11329
11330 static void
11331 do_t_rev (void)
11332 {
11333   unsigned Rd, Rm;
11334
11335   Rd = inst.operands[0].reg;
11336   Rm = inst.operands[1].reg;
11337
11338   reject_bad_reg (Rd);
11339   reject_bad_reg (Rm);
11340
11341   if (Rd <= 7 && Rm <= 7
11342       && inst.size_req != 4)
11343     {
11344       inst.instruction = THUMB_OP16 (inst.instruction);
11345       inst.instruction |= Rd;
11346       inst.instruction |= Rm << 3;
11347     }
11348   else if (unified_syntax)
11349     {
11350       inst.instruction = THUMB_OP32 (inst.instruction);
11351       inst.instruction |= Rd << 8;
11352       inst.instruction |= Rm << 16;
11353       inst.instruction |= Rm;
11354     }
11355   else
11356     inst.error = BAD_HIREG;
11357 }
11358
11359 static void
11360 do_t_rrx (void)
11361 {
11362   unsigned Rd, Rm;
11363
11364   Rd = inst.operands[0].reg;
11365   Rm = inst.operands[1].reg;
11366
11367   reject_bad_reg (Rd);
11368   reject_bad_reg (Rm);
11369
11370   inst.instruction |= Rd << 8;
11371   inst.instruction |= Rm;
11372 }
11373
11374 static void
11375 do_t_rsb (void)
11376 {
11377   unsigned Rd, Rs;
11378
11379   Rd = inst.operands[0].reg;
11380   Rs = (inst.operands[1].present
11381         ? inst.operands[1].reg    /* Rd, Rs, foo */
11382         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11383
11384   reject_bad_reg (Rd);
11385   reject_bad_reg (Rs);
11386   if (inst.operands[2].isreg)
11387     reject_bad_reg (inst.operands[2].reg);
11388
11389   inst.instruction |= Rd << 8;
11390   inst.instruction |= Rs << 16;
11391   if (!inst.operands[2].isreg)
11392     {
11393       bfd_boolean narrow;
11394
11395       if ((inst.instruction & 0x00100000) != 0)
11396         narrow = !in_it_block ();
11397       else
11398         narrow = in_it_block ();
11399
11400       if (Rd > 7 || Rs > 7)
11401         narrow = FALSE;
11402
11403       if (inst.size_req == 4 || !unified_syntax)
11404         narrow = FALSE;
11405
11406       if (inst.reloc.exp.X_op != O_constant
11407           || inst.reloc.exp.X_add_number != 0)
11408         narrow = FALSE;
11409
11410       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
11411          relaxation, but it doesn't seem worth the hassle.  */
11412       if (narrow)
11413         {
11414           inst.reloc.type = BFD_RELOC_UNUSED;
11415           inst.instruction = THUMB_OP16 (T_MNEM_negs);
11416           inst.instruction |= Rs << 3;
11417           inst.instruction |= Rd;
11418         }
11419       else
11420         {
11421           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11422           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11423         }
11424     }
11425   else
11426     encode_thumb32_shifted_operand (2);
11427 }
11428
11429 static void
11430 do_t_setend (void)
11431 {
11432   set_it_insn_type (OUTSIDE_IT_INSN);
11433   if (inst.operands[0].imm)
11434     inst.instruction |= 0x8;
11435 }
11436
11437 static void
11438 do_t_shift (void)
11439 {
11440   if (!inst.operands[1].present)
11441     inst.operands[1].reg = inst.operands[0].reg;
11442
11443   if (unified_syntax)
11444     {
11445       bfd_boolean narrow;
11446       int shift_kind;
11447
11448       switch (inst.instruction)
11449         {
11450         case T_MNEM_asr:
11451         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
11452         case T_MNEM_lsl:
11453         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
11454         case T_MNEM_lsr:
11455         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
11456         case T_MNEM_ror:
11457         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
11458         default: abort ();
11459         }
11460
11461       if (THUMB_SETS_FLAGS (inst.instruction))
11462         narrow = !in_it_block ();
11463       else
11464         narrow = in_it_block ();
11465       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
11466         narrow = FALSE;
11467       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
11468         narrow = FALSE;
11469       if (inst.operands[2].isreg
11470           && (inst.operands[1].reg != inst.operands[0].reg
11471               || inst.operands[2].reg > 7))
11472         narrow = FALSE;
11473       if (inst.size_req == 4)
11474         narrow = FALSE;
11475
11476       reject_bad_reg (inst.operands[0].reg);
11477       reject_bad_reg (inst.operands[1].reg);
11478
11479       if (!narrow)
11480         {
11481           if (inst.operands[2].isreg)
11482             {
11483               reject_bad_reg (inst.operands[2].reg);
11484               inst.instruction = THUMB_OP32 (inst.instruction);
11485               inst.instruction |= inst.operands[0].reg << 8;
11486               inst.instruction |= inst.operands[1].reg << 16;
11487               inst.instruction |= inst.operands[2].reg;
11488             }
11489           else
11490             {
11491               inst.operands[1].shifted = 1;
11492               inst.operands[1].shift_kind = shift_kind;
11493               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
11494                                              ? T_MNEM_movs : T_MNEM_mov);
11495               inst.instruction |= inst.operands[0].reg << 8;
11496               encode_thumb32_shifted_operand (1);
11497               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
11498               inst.reloc.type = BFD_RELOC_UNUSED;
11499             }
11500         }
11501       else
11502         {
11503           if (inst.operands[2].isreg)
11504             {
11505               switch (shift_kind)
11506                 {
11507                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
11508                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
11509                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
11510                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
11511                 default: abort ();
11512                 }
11513
11514               inst.instruction |= inst.operands[0].reg;
11515               inst.instruction |= inst.operands[2].reg << 3;
11516             }
11517           else
11518             {
11519               switch (shift_kind)
11520                 {
11521                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
11522                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
11523                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
11524                 default: abort ();
11525                 }
11526               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11527               inst.instruction |= inst.operands[0].reg;
11528               inst.instruction |= inst.operands[1].reg << 3;
11529             }
11530         }
11531     }
11532   else
11533     {
11534       constraint (inst.operands[0].reg > 7
11535                   || inst.operands[1].reg > 7, BAD_HIREG);
11536       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11537
11538       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
11539         {
11540           constraint (inst.operands[2].reg > 7, BAD_HIREG);
11541           constraint (inst.operands[0].reg != inst.operands[1].reg,
11542                       _("source1 and dest must be same register"));
11543
11544           switch (inst.instruction)
11545             {
11546             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
11547             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
11548             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
11549             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
11550             default: abort ();
11551             }
11552
11553           inst.instruction |= inst.operands[0].reg;
11554           inst.instruction |= inst.operands[2].reg << 3;
11555         }
11556       else
11557         {
11558           switch (inst.instruction)
11559             {
11560             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
11561             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
11562             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
11563             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
11564             default: abort ();
11565             }
11566           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11567           inst.instruction |= inst.operands[0].reg;
11568           inst.instruction |= inst.operands[1].reg << 3;
11569         }
11570     }
11571 }
11572
11573 static void
11574 do_t_simd (void)
11575 {
11576   unsigned Rd, Rn, Rm;
11577
11578   Rd = inst.operands[0].reg;
11579   Rn = inst.operands[1].reg;
11580   Rm = inst.operands[2].reg;
11581
11582   reject_bad_reg (Rd);
11583   reject_bad_reg (Rn);
11584   reject_bad_reg (Rm);
11585
11586   inst.instruction |= Rd << 8;
11587   inst.instruction |= Rn << 16;
11588   inst.instruction |= Rm;
11589 }
11590
11591 static void
11592 do_t_simd2 (void)
11593 {
11594   unsigned Rd, Rn, Rm;
11595
11596   Rd = inst.operands[0].reg;
11597   Rm = inst.operands[1].reg;
11598   Rn = inst.operands[2].reg;
11599
11600   reject_bad_reg (Rd);
11601   reject_bad_reg (Rn);
11602   reject_bad_reg (Rm);
11603
11604   inst.instruction |= Rd << 8;
11605   inst.instruction |= Rn << 16;
11606   inst.instruction |= Rm;
11607 }
11608
11609 static void
11610 do_t_smc (void)
11611 {
11612   unsigned int value = inst.reloc.exp.X_add_number;
11613   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
11614               _("SMC is not permitted on this architecture"));
11615   constraint (inst.reloc.exp.X_op != O_constant,
11616               _("expression too complex"));
11617   inst.reloc.type = BFD_RELOC_UNUSED;
11618   inst.instruction |= (value & 0xf000) >> 12;
11619   inst.instruction |= (value & 0x0ff0);
11620   inst.instruction |= (value & 0x000f) << 16;
11621 }
11622
11623 static void
11624 do_t_hvc (void)
11625 {
11626   unsigned int value = inst.reloc.exp.X_add_number;
11627
11628   inst.reloc.type = BFD_RELOC_UNUSED;
11629   inst.instruction |= (value & 0x0fff);
11630   inst.instruction |= (value & 0xf000) << 4;
11631 }
11632
11633 static void
11634 do_t_ssat_usat (int bias)
11635 {
11636   unsigned Rd, Rn;
11637
11638   Rd = inst.operands[0].reg;
11639   Rn = inst.operands[2].reg;
11640
11641   reject_bad_reg (Rd);
11642   reject_bad_reg (Rn);
11643
11644   inst.instruction |= Rd << 8;
11645   inst.instruction |= inst.operands[1].imm - bias;
11646   inst.instruction |= Rn << 16;
11647
11648   if (inst.operands[3].present)
11649     {
11650       offsetT shift_amount = inst.reloc.exp.X_add_number;
11651
11652       inst.reloc.type = BFD_RELOC_UNUSED;
11653
11654       constraint (inst.reloc.exp.X_op != O_constant,
11655                   _("expression too complex"));
11656
11657       if (shift_amount != 0)
11658         {
11659           constraint (shift_amount > 31,
11660                       _("shift expression is too large"));
11661
11662           if (inst.operands[3].shift_kind == SHIFT_ASR)
11663             inst.instruction |= 0x00200000;  /* sh bit.  */
11664
11665           inst.instruction |= (shift_amount & 0x1c) << 10;
11666           inst.instruction |= (shift_amount & 0x03) << 6;
11667         }
11668     }
11669 }
11670
11671 static void
11672 do_t_ssat (void)
11673 {
11674   do_t_ssat_usat (1);
11675 }
11676
11677 static void
11678 do_t_ssat16 (void)
11679 {
11680   unsigned Rd, Rn;
11681
11682   Rd = inst.operands[0].reg;
11683   Rn = inst.operands[2].reg;
11684
11685   reject_bad_reg (Rd);
11686   reject_bad_reg (Rn);
11687
11688   inst.instruction |= Rd << 8;
11689   inst.instruction |= inst.operands[1].imm - 1;
11690   inst.instruction |= Rn << 16;
11691 }
11692
11693 static void
11694 do_t_strex (void)
11695 {
11696   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
11697               || inst.operands[2].postind || inst.operands[2].writeback
11698               || inst.operands[2].immisreg || inst.operands[2].shifted
11699               || inst.operands[2].negative,
11700               BAD_ADDR_MODE);
11701
11702   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
11703
11704   inst.instruction |= inst.operands[0].reg << 8;
11705   inst.instruction |= inst.operands[1].reg << 12;
11706   inst.instruction |= inst.operands[2].reg << 16;
11707   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
11708 }
11709
11710 static void
11711 do_t_strexd (void)
11712 {
11713   if (!inst.operands[2].present)
11714     inst.operands[2].reg = inst.operands[1].reg + 1;
11715
11716   constraint (inst.operands[0].reg == inst.operands[1].reg
11717               || inst.operands[0].reg == inst.operands[2].reg
11718               || inst.operands[0].reg == inst.operands[3].reg,
11719               BAD_OVERLAP);
11720
11721   inst.instruction |= inst.operands[0].reg;
11722   inst.instruction |= inst.operands[1].reg << 12;
11723   inst.instruction |= inst.operands[2].reg << 8;
11724   inst.instruction |= inst.operands[3].reg << 16;
11725 }
11726
11727 static void
11728 do_t_sxtah (void)
11729 {
11730   unsigned Rd, Rn, Rm;
11731
11732   Rd = inst.operands[0].reg;
11733   Rn = inst.operands[1].reg;
11734   Rm = inst.operands[2].reg;
11735
11736   reject_bad_reg (Rd);
11737   reject_bad_reg (Rn);
11738   reject_bad_reg (Rm);
11739
11740   inst.instruction |= Rd << 8;
11741   inst.instruction |= Rn << 16;
11742   inst.instruction |= Rm;
11743   inst.instruction |= inst.operands[3].imm << 4;
11744 }
11745
11746 static void
11747 do_t_sxth (void)
11748 {
11749   unsigned Rd, Rm;
11750
11751   Rd = inst.operands[0].reg;
11752   Rm = inst.operands[1].reg;
11753
11754   reject_bad_reg (Rd);
11755   reject_bad_reg (Rm);
11756
11757   if (inst.instruction <= 0xffff
11758       && inst.size_req != 4
11759       && Rd <= 7 && Rm <= 7
11760       && (!inst.operands[2].present || inst.operands[2].imm == 0))
11761     {
11762       inst.instruction = THUMB_OP16 (inst.instruction);
11763       inst.instruction |= Rd;
11764       inst.instruction |= Rm << 3;
11765     }
11766   else if (unified_syntax)
11767     {
11768       if (inst.instruction <= 0xffff)
11769         inst.instruction = THUMB_OP32 (inst.instruction);
11770       inst.instruction |= Rd << 8;
11771       inst.instruction |= Rm;
11772       inst.instruction |= inst.operands[2].imm << 4;
11773     }
11774   else
11775     {
11776       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
11777                   _("Thumb encoding does not support rotation"));
11778       constraint (1, BAD_HIREG);
11779     }
11780 }
11781
11782 static void
11783 do_t_swi (void)
11784 {
11785   /* We have to do the following check manually as ARM_EXT_OS only applies
11786      to ARM_EXT_V6M.  */
11787   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6m))
11788     {
11789       if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_os)
11790           /* This only applies to the v6m howver, not later architectures.  */
11791           && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7))
11792         as_bad (_("SVC is not permitted on this architecture"));
11793       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, arm_ext_os);
11794     }
11795
11796   inst.reloc.type = BFD_RELOC_ARM_SWI;
11797 }
11798
11799 static void
11800 do_t_tb (void)
11801 {
11802   unsigned Rn, Rm;
11803   int half;
11804
11805   half = (inst.instruction & 0x10) != 0;
11806   set_it_insn_type_last ();
11807   constraint (inst.operands[0].immisreg,
11808               _("instruction requires register index"));
11809
11810   Rn = inst.operands[0].reg;
11811   Rm = inst.operands[0].imm;
11812
11813   constraint (Rn == REG_SP, BAD_SP);
11814   reject_bad_reg (Rm);
11815
11816   constraint (!half && inst.operands[0].shifted,
11817               _("instruction does not allow shifted index"));
11818   inst.instruction |= (Rn << 16) | Rm;
11819 }
11820
11821 static void
11822 do_t_usat (void)
11823 {
11824   do_t_ssat_usat (0);
11825 }
11826
11827 static void
11828 do_t_usat16 (void)
11829 {
11830   unsigned Rd, Rn;
11831
11832   Rd = inst.operands[0].reg;
11833   Rn = inst.operands[2].reg;
11834
11835   reject_bad_reg (Rd);
11836   reject_bad_reg (Rn);
11837
11838   inst.instruction |= Rd << 8;
11839   inst.instruction |= inst.operands[1].imm;
11840   inst.instruction |= Rn << 16;
11841 }
11842
11843 /* Neon instruction encoder helpers.  */
11844
11845 /* Encodings for the different types for various Neon opcodes.  */
11846
11847 /* An "invalid" code for the following tables.  */
11848 #define N_INV -1u
11849
11850 struct neon_tab_entry
11851 {
11852   unsigned integer;
11853   unsigned float_or_poly;
11854   unsigned scalar_or_imm;
11855 };
11856
11857 /* Map overloaded Neon opcodes to their respective encodings.  */
11858 #define NEON_ENC_TAB                                    \
11859   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
11860   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
11861   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
11862   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
11863   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
11864   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
11865   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
11866   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
11867   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
11868   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
11869   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
11870   /* Register variants of the following two instructions are encoded as
11871      vcge / vcgt with the operands reversed.  */        \
11872   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
11873   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
11874   X(vfma,       N_INV, 0x0000c10, N_INV),               \
11875   X(vfms,       N_INV, 0x0200c10, N_INV),               \
11876   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
11877   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
11878   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
11879   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
11880   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
11881   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
11882   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
11883   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
11884   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
11885   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
11886   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
11887   X(vshl,       0x0000400, N_INV,     0x0800510),       \
11888   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
11889   X(vand,       0x0000110, N_INV,     0x0800030),       \
11890   X(vbic,       0x0100110, N_INV,     0x0800030),       \
11891   X(veor,       0x1000110, N_INV,     N_INV),           \
11892   X(vorn,       0x0300110, N_INV,     0x0800010),       \
11893   X(vorr,       0x0200110, N_INV,     0x0800010),       \
11894   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
11895   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
11896   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
11897   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
11898   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
11899   X(vst1,       0x0000000, 0x0800000, N_INV),           \
11900   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
11901   X(vst2,       0x0000100, 0x0800100, N_INV),           \
11902   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
11903   X(vst3,       0x0000200, 0x0800200, N_INV),           \
11904   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
11905   X(vst4,       0x0000300, 0x0800300, N_INV),           \
11906   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
11907   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
11908   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
11909   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
11910   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
11911   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
11912   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
11913   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
11914   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
11915   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
11916   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
11917   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
11918   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV)
11919
11920 enum neon_opc
11921 {
11922 #define X(OPC,I,F,S) N_MNEM_##OPC
11923 NEON_ENC_TAB
11924 #undef X
11925 };
11926
11927 static const struct neon_tab_entry neon_enc_tab[] =
11928 {
11929 #define X(OPC,I,F,S) { (I), (F), (S) }
11930 NEON_ENC_TAB
11931 #undef X
11932 };
11933
11934 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
11935 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
11936 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
11937 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
11938 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
11939 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
11940 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
11941 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
11942 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
11943 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
11944 #define NEON_ENC_SINGLE_(X) \
11945   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
11946 #define NEON_ENC_DOUBLE_(X) \
11947   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
11948
11949 #define NEON_ENCODE(type, inst)                                 \
11950   do                                                            \
11951     {                                                           \
11952       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
11953       inst.is_neon = 1;                                         \
11954     }                                                           \
11955   while (0)
11956
11957 #define check_neon_suffixes                                             \
11958   do                                                                    \
11959     {                                                                   \
11960       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
11961         {                                                               \
11962           as_bad (_("invalid neon suffix for non neon instruction"));   \
11963           return;                                                       \
11964         }                                                               \
11965     }                                                                   \
11966   while (0)
11967
11968 /* Define shapes for instruction operands. The following mnemonic characters
11969    are used in this table:
11970
11971      F - VFP S<n> register
11972      D - Neon D<n> register
11973      Q - Neon Q<n> register
11974      I - Immediate
11975      S - Scalar
11976      R - ARM register
11977      L - D<n> register list
11978
11979    This table is used to generate various data:
11980      - enumerations of the form NS_DDR to be used as arguments to
11981        neon_select_shape.
11982      - a table classifying shapes into single, double, quad, mixed.
11983      - a table used to drive neon_select_shape.  */
11984
11985 #define NEON_SHAPE_DEF                  \
11986   X(3, (D, D, D), DOUBLE),              \
11987   X(3, (Q, Q, Q), QUAD),                \
11988   X(3, (D, D, I), DOUBLE),              \
11989   X(3, (Q, Q, I), QUAD),                \
11990   X(3, (D, D, S), DOUBLE),              \
11991   X(3, (Q, Q, S), QUAD),                \
11992   X(2, (D, D), DOUBLE),                 \
11993   X(2, (Q, Q), QUAD),                   \
11994   X(2, (D, S), DOUBLE),                 \
11995   X(2, (Q, S), QUAD),                   \
11996   X(2, (D, R), DOUBLE),                 \
11997   X(2, (Q, R), QUAD),                   \
11998   X(2, (D, I), DOUBLE),                 \
11999   X(2, (Q, I), QUAD),                   \
12000   X(3, (D, L, D), DOUBLE),              \
12001   X(2, (D, Q), MIXED),                  \
12002   X(2, (Q, D), MIXED),                  \
12003   X(3, (D, Q, I), MIXED),               \
12004   X(3, (Q, D, I), MIXED),               \
12005   X(3, (Q, D, D), MIXED),               \
12006   X(3, (D, Q, Q), MIXED),               \
12007   X(3, (Q, Q, D), MIXED),               \
12008   X(3, (Q, D, S), MIXED),               \
12009   X(3, (D, Q, S), MIXED),               \
12010   X(4, (D, D, D, I), DOUBLE),           \
12011   X(4, (Q, Q, Q, I), QUAD),             \
12012   X(2, (F, F), SINGLE),                 \
12013   X(3, (F, F, F), SINGLE),              \
12014   X(2, (F, I), SINGLE),                 \
12015   X(2, (F, D), MIXED),                  \
12016   X(2, (D, F), MIXED),                  \
12017   X(3, (F, F, I), MIXED),               \
12018   X(4, (R, R, F, F), SINGLE),           \
12019   X(4, (F, F, R, R), SINGLE),           \
12020   X(3, (D, R, R), DOUBLE),              \
12021   X(3, (R, R, D), DOUBLE),              \
12022   X(2, (S, R), SINGLE),                 \
12023   X(2, (R, S), SINGLE),                 \
12024   X(2, (F, R), SINGLE),                 \
12025   X(2, (R, F), SINGLE)
12026
12027 #define S2(A,B)         NS_##A##B
12028 #define S3(A,B,C)       NS_##A##B##C
12029 #define S4(A,B,C,D)     NS_##A##B##C##D
12030
12031 #define X(N, L, C) S##N L
12032
12033 enum neon_shape
12034 {
12035   NEON_SHAPE_DEF,
12036   NS_NULL
12037 };
12038
12039 #undef X
12040 #undef S2
12041 #undef S3
12042 #undef S4
12043
12044 enum neon_shape_class
12045 {
12046   SC_SINGLE,
12047   SC_DOUBLE,
12048   SC_QUAD,
12049   SC_MIXED
12050 };
12051
12052 #define X(N, L, C) SC_##C
12053
12054 static enum neon_shape_class neon_shape_class[] =
12055 {
12056   NEON_SHAPE_DEF
12057 };
12058
12059 #undef X
12060
12061 enum neon_shape_el
12062 {
12063   SE_F,
12064   SE_D,
12065   SE_Q,
12066   SE_I,
12067   SE_S,
12068   SE_R,
12069   SE_L
12070 };
12071
12072 /* Register widths of above.  */
12073 static unsigned neon_shape_el_size[] =
12074 {
12075   32,
12076   64,
12077   128,
12078   0,
12079   32,
12080   32,
12081   0
12082 };
12083
12084 struct neon_shape_info
12085 {
12086   unsigned els;
12087   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
12088 };
12089
12090 #define S2(A,B)         { SE_##A, SE_##B }
12091 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
12092 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
12093
12094 #define X(N, L, C) { N, S##N L }
12095
12096 static struct neon_shape_info neon_shape_tab[] =
12097 {
12098   NEON_SHAPE_DEF
12099 };
12100
12101 #undef X
12102 #undef S2
12103 #undef S3
12104 #undef S4
12105
12106 /* Bit masks used in type checking given instructions.
12107   'N_EQK' means the type must be the same as (or based on in some way) the key
12108    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
12109    set, various other bits can be set as well in order to modify the meaning of
12110    the type constraint.  */
12111
12112 enum neon_type_mask
12113 {
12114   N_S8   = 0x0000001,
12115   N_S16  = 0x0000002,
12116   N_S32  = 0x0000004,
12117   N_S64  = 0x0000008,
12118   N_U8   = 0x0000010,
12119   N_U16  = 0x0000020,
12120   N_U32  = 0x0000040,
12121   N_U64  = 0x0000080,
12122   N_I8   = 0x0000100,
12123   N_I16  = 0x0000200,
12124   N_I32  = 0x0000400,
12125   N_I64  = 0x0000800,
12126   N_8    = 0x0001000,
12127   N_16   = 0x0002000,
12128   N_32   = 0x0004000,
12129   N_64   = 0x0008000,
12130   N_P8   = 0x0010000,
12131   N_P16  = 0x0020000,
12132   N_F16  = 0x0040000,
12133   N_F32  = 0x0080000,
12134   N_F64  = 0x0100000,
12135   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
12136   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
12137   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
12138   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
12139   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
12140   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
12141   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
12142   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
12143   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
12144   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
12145   N_UTYP = 0,
12146   N_MAX_NONSPECIAL = N_F64
12147 };
12148
12149 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
12150
12151 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
12152 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
12153 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
12154 #define N_SUF_32   (N_SU_32 | N_F32)
12155 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
12156 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F32)
12157
12158 /* Pass this as the first type argument to neon_check_type to ignore types
12159    altogether.  */
12160 #define N_IGNORE_TYPE (N_KEY | N_EQK)
12161
12162 /* Select a "shape" for the current instruction (describing register types or
12163    sizes) from a list of alternatives. Return NS_NULL if the current instruction
12164    doesn't fit. For non-polymorphic shapes, checking is usually done as a
12165    function of operand parsing, so this function doesn't need to be called.
12166    Shapes should be listed in order of decreasing length.  */
12167
12168 static enum neon_shape
12169 neon_select_shape (enum neon_shape shape, ...)
12170 {
12171   va_list ap;
12172   enum neon_shape first_shape = shape;
12173
12174   /* Fix missing optional operands. FIXME: we don't know at this point how
12175      many arguments we should have, so this makes the assumption that we have
12176      > 1. This is true of all current Neon opcodes, I think, but may not be
12177      true in the future.  */
12178   if (!inst.operands[1].present)
12179     inst.operands[1] = inst.operands[0];
12180
12181   va_start (ap, shape);
12182
12183   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
12184     {
12185       unsigned j;
12186       int matches = 1;
12187
12188       for (j = 0; j < neon_shape_tab[shape].els; j++)
12189         {
12190           if (!inst.operands[j].present)
12191             {
12192               matches = 0;
12193               break;
12194             }
12195
12196           switch (neon_shape_tab[shape].el[j])
12197             {
12198             case SE_F:
12199               if (!(inst.operands[j].isreg
12200                     && inst.operands[j].isvec
12201                     && inst.operands[j].issingle
12202                     && !inst.operands[j].isquad))
12203                 matches = 0;
12204               break;
12205
12206             case SE_D:
12207               if (!(inst.operands[j].isreg
12208                     && inst.operands[j].isvec
12209                     && !inst.operands[j].isquad
12210                     && !inst.operands[j].issingle))
12211                 matches = 0;
12212               break;
12213
12214             case SE_R:
12215               if (!(inst.operands[j].isreg
12216                     && !inst.operands[j].isvec))
12217                 matches = 0;
12218               break;
12219
12220             case SE_Q:
12221               if (!(inst.operands[j].isreg
12222                     && inst.operands[j].isvec
12223                     && inst.operands[j].isquad
12224                     && !inst.operands[j].issingle))
12225                 matches = 0;
12226               break;
12227
12228             case SE_I:
12229               if (!(!inst.operands[j].isreg
12230                     && !inst.operands[j].isscalar))
12231                 matches = 0;
12232               break;
12233
12234             case SE_S:
12235               if (!(!inst.operands[j].isreg
12236                     && inst.operands[j].isscalar))
12237                 matches = 0;
12238               break;
12239
12240             case SE_L:
12241               break;
12242             }
12243           if (!matches)
12244             break;
12245         }
12246       if (matches)
12247         break;
12248     }
12249
12250   va_end (ap);
12251
12252   if (shape == NS_NULL && first_shape != NS_NULL)
12253     first_error (_("invalid instruction shape"));
12254
12255   return shape;
12256 }
12257
12258 /* True if SHAPE is predominantly a quadword operation (most of the time, this
12259    means the Q bit should be set).  */
12260
12261 static int
12262 neon_quad (enum neon_shape shape)
12263 {
12264   return neon_shape_class[shape] == SC_QUAD;
12265 }
12266
12267 static void
12268 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
12269                        unsigned *g_size)
12270 {
12271   /* Allow modification to be made to types which are constrained to be
12272      based on the key element, based on bits set alongside N_EQK.  */
12273   if ((typebits & N_EQK) != 0)
12274     {
12275       if ((typebits & N_HLF) != 0)
12276         *g_size /= 2;
12277       else if ((typebits & N_DBL) != 0)
12278         *g_size *= 2;
12279       if ((typebits & N_SGN) != 0)
12280         *g_type = NT_signed;
12281       else if ((typebits & N_UNS) != 0)
12282         *g_type = NT_unsigned;
12283       else if ((typebits & N_INT) != 0)
12284         *g_type = NT_integer;
12285       else if ((typebits & N_FLT) != 0)
12286         *g_type = NT_float;
12287       else if ((typebits & N_SIZ) != 0)
12288         *g_type = NT_untyped;
12289     }
12290 }
12291
12292 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
12293    operand type, i.e. the single type specified in a Neon instruction when it
12294    is the only one given.  */
12295
12296 static struct neon_type_el
12297 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
12298 {
12299   struct neon_type_el dest = *key;
12300
12301   gas_assert ((thisarg & N_EQK) != 0);
12302
12303   neon_modify_type_size (thisarg, &dest.type, &dest.size);
12304
12305   return dest;
12306 }
12307
12308 /* Convert Neon type and size into compact bitmask representation.  */
12309
12310 static enum neon_type_mask
12311 type_chk_of_el_type (enum neon_el_type type, unsigned size)
12312 {
12313   switch (type)
12314     {
12315     case NT_untyped:
12316       switch (size)
12317         {
12318         case 8:  return N_8;
12319         case 16: return N_16;
12320         case 32: return N_32;
12321         case 64: return N_64;
12322         default: ;
12323         }
12324       break;
12325
12326     case NT_integer:
12327       switch (size)
12328         {
12329         case 8:  return N_I8;
12330         case 16: return N_I16;
12331         case 32: return N_I32;
12332         case 64: return N_I64;
12333         default: ;
12334         }
12335       break;
12336
12337     case NT_float:
12338       switch (size)
12339         {
12340         case 16: return N_F16;
12341         case 32: return N_F32;
12342         case 64: return N_F64;
12343         default: ;
12344         }
12345       break;
12346
12347     case NT_poly:
12348       switch (size)
12349         {
12350         case 8:  return N_P8;
12351         case 16: return N_P16;
12352         default: ;
12353         }
12354       break;
12355
12356     case NT_signed:
12357       switch (size)
12358         {
12359         case 8:  return N_S8;
12360         case 16: return N_S16;
12361         case 32: return N_S32;
12362         case 64: return N_S64;
12363         default: ;
12364         }
12365       break;
12366
12367     case NT_unsigned:
12368       switch (size)
12369         {
12370         case 8:  return N_U8;
12371         case 16: return N_U16;
12372         case 32: return N_U32;
12373         case 64: return N_U64;
12374         default: ;
12375         }
12376       break;
12377
12378     default: ;
12379     }
12380
12381   return N_UTYP;
12382 }
12383
12384 /* Convert compact Neon bitmask type representation to a type and size. Only
12385    handles the case where a single bit is set in the mask.  */
12386
12387 static int
12388 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
12389                      enum neon_type_mask mask)
12390 {
12391   if ((mask & N_EQK) != 0)
12392     return FAIL;
12393
12394   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
12395     *size = 8;
12396   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_P16)) != 0)
12397     *size = 16;
12398   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
12399     *size = 32;
12400   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64)) != 0)
12401     *size = 64;
12402   else
12403     return FAIL;
12404
12405   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
12406     *type = NT_signed;
12407   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
12408     *type = NT_unsigned;
12409   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
12410     *type = NT_integer;
12411   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
12412     *type = NT_untyped;
12413   else if ((mask & (N_P8 | N_P16)) != 0)
12414     *type = NT_poly;
12415   else if ((mask & (N_F32 | N_F64)) != 0)
12416     *type = NT_float;
12417   else
12418     return FAIL;
12419
12420   return SUCCESS;
12421 }
12422
12423 /* Modify a bitmask of allowed types. This is only needed for type
12424    relaxation.  */
12425
12426 static unsigned
12427 modify_types_allowed (unsigned allowed, unsigned mods)
12428 {
12429   unsigned size;
12430   enum neon_el_type type;
12431   unsigned destmask;
12432   int i;
12433
12434   destmask = 0;
12435
12436   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
12437     {
12438       if (el_type_of_type_chk (&type, &size,
12439                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
12440         {
12441           neon_modify_type_size (mods, &type, &size);
12442           destmask |= type_chk_of_el_type (type, size);
12443         }
12444     }
12445
12446   return destmask;
12447 }
12448
12449 /* Check type and return type classification.
12450    The manual states (paraphrase): If one datatype is given, it indicates the
12451    type given in:
12452     - the second operand, if there is one
12453     - the operand, if there is no second operand
12454     - the result, if there are no operands.
12455    This isn't quite good enough though, so we use a concept of a "key" datatype
12456    which is set on a per-instruction basis, which is the one which matters when
12457    only one data type is written.
12458    Note: this function has side-effects (e.g. filling in missing operands). All
12459    Neon instructions should call it before performing bit encoding.  */
12460
12461 static struct neon_type_el
12462 neon_check_type (unsigned els, enum neon_shape ns, ...)
12463 {
12464   va_list ap;
12465   unsigned i, pass, key_el = 0;
12466   unsigned types[NEON_MAX_TYPE_ELS];
12467   enum neon_el_type k_type = NT_invtype;
12468   unsigned k_size = -1u;
12469   struct neon_type_el badtype = {NT_invtype, -1};
12470   unsigned key_allowed = 0;
12471
12472   /* Optional registers in Neon instructions are always (not) in operand 1.
12473      Fill in the missing operand here, if it was omitted.  */
12474   if (els > 1 && !inst.operands[1].present)
12475     inst.operands[1] = inst.operands[0];
12476
12477   /* Suck up all the varargs.  */
12478   va_start (ap, ns);
12479   for (i = 0; i < els; i++)
12480     {
12481       unsigned thisarg = va_arg (ap, unsigned);
12482       if (thisarg == N_IGNORE_TYPE)
12483         {
12484           va_end (ap);
12485           return badtype;
12486         }
12487       types[i] = thisarg;
12488       if ((thisarg & N_KEY) != 0)
12489         key_el = i;
12490     }
12491   va_end (ap);
12492
12493   if (inst.vectype.elems > 0)
12494     for (i = 0; i < els; i++)
12495       if (inst.operands[i].vectype.type != NT_invtype)
12496         {
12497           first_error (_("types specified in both the mnemonic and operands"));
12498           return badtype;
12499         }
12500
12501   /* Duplicate inst.vectype elements here as necessary.
12502      FIXME: No idea if this is exactly the same as the ARM assembler,
12503      particularly when an insn takes one register and one non-register
12504      operand. */
12505   if (inst.vectype.elems == 1 && els > 1)
12506     {
12507       unsigned j;
12508       inst.vectype.elems = els;
12509       inst.vectype.el[key_el] = inst.vectype.el[0];
12510       for (j = 0; j < els; j++)
12511         if (j != key_el)
12512           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
12513                                                   types[j]);
12514     }
12515   else if (inst.vectype.elems == 0 && els > 0)
12516     {
12517       unsigned j;
12518       /* No types were given after the mnemonic, so look for types specified
12519          after each operand. We allow some flexibility here; as long as the
12520          "key" operand has a type, we can infer the others.  */
12521       for (j = 0; j < els; j++)
12522         if (inst.operands[j].vectype.type != NT_invtype)
12523           inst.vectype.el[j] = inst.operands[j].vectype;
12524
12525       if (inst.operands[key_el].vectype.type != NT_invtype)
12526         {
12527           for (j = 0; j < els; j++)
12528             if (inst.operands[j].vectype.type == NT_invtype)
12529               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
12530                                                       types[j]);
12531         }
12532       else
12533         {
12534           first_error (_("operand types can't be inferred"));
12535           return badtype;
12536         }
12537     }
12538   else if (inst.vectype.elems != els)
12539     {
12540       first_error (_("type specifier has the wrong number of parts"));
12541       return badtype;
12542     }
12543
12544   for (pass = 0; pass < 2; pass++)
12545     {
12546       for (i = 0; i < els; i++)
12547         {
12548           unsigned thisarg = types[i];
12549           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
12550             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
12551           enum neon_el_type g_type = inst.vectype.el[i].type;
12552           unsigned g_size = inst.vectype.el[i].size;
12553
12554           /* Decay more-specific signed & unsigned types to sign-insensitive
12555              integer types if sign-specific variants are unavailable.  */
12556           if ((g_type == NT_signed || g_type == NT_unsigned)
12557               && (types_allowed & N_SU_ALL) == 0)
12558             g_type = NT_integer;
12559
12560           /* If only untyped args are allowed, decay any more specific types to
12561              them. Some instructions only care about signs for some element
12562              sizes, so handle that properly.  */
12563           if ((g_size == 8 && (types_allowed & N_8) != 0)
12564               || (g_size == 16 && (types_allowed & N_16) != 0)
12565               || (g_size == 32 && (types_allowed & N_32) != 0)
12566               || (g_size == 64 && (types_allowed & N_64) != 0))
12567             g_type = NT_untyped;
12568
12569           if (pass == 0)
12570             {
12571               if ((thisarg & N_KEY) != 0)
12572                 {
12573                   k_type = g_type;
12574                   k_size = g_size;
12575                   key_allowed = thisarg & ~N_KEY;
12576                 }
12577             }
12578           else
12579             {
12580               if ((thisarg & N_VFP) != 0)
12581                 {
12582                   enum neon_shape_el regshape;
12583                   unsigned regwidth, match;
12584
12585                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
12586                   if (ns == NS_NULL)
12587                     {
12588                       first_error (_("invalid instruction shape"));
12589                       return badtype;
12590                     }
12591                   regshape = neon_shape_tab[ns].el[i];
12592                   regwidth = neon_shape_el_size[regshape];
12593
12594                   /* In VFP mode, operands must match register widths. If we
12595                      have a key operand, use its width, else use the width of
12596                      the current operand.  */
12597                   if (k_size != -1u)
12598                     match = k_size;
12599                   else
12600                     match = g_size;
12601
12602                   if (regwidth != match)
12603                     {
12604                       first_error (_("operand size must match register width"));
12605                       return badtype;
12606                     }
12607                 }
12608
12609               if ((thisarg & N_EQK) == 0)
12610                 {
12611                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
12612
12613                   if ((given_type & types_allowed) == 0)
12614                     {
12615                       first_error (_("bad type in Neon instruction"));
12616                       return badtype;
12617                     }
12618                 }
12619               else
12620                 {
12621                   enum neon_el_type mod_k_type = k_type;
12622                   unsigned mod_k_size = k_size;
12623                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
12624                   if (g_type != mod_k_type || g_size != mod_k_size)
12625                     {
12626                       first_error (_("inconsistent types in Neon instruction"));
12627                       return badtype;
12628                     }
12629                 }
12630             }
12631         }
12632     }
12633
12634   return inst.vectype.el[key_el];
12635 }
12636
12637 /* Neon-style VFP instruction forwarding.  */
12638
12639 /* Thumb VFP instructions have 0xE in the condition field.  */
12640
12641 static void
12642 do_vfp_cond_or_thumb (void)
12643 {
12644   inst.is_neon = 1;
12645
12646   if (thumb_mode)
12647     inst.instruction |= 0xe0000000;
12648   else
12649     inst.instruction |= inst.cond << 28;
12650 }
12651
12652 /* Look up and encode a simple mnemonic, for use as a helper function for the
12653    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
12654    etc.  It is assumed that operand parsing has already been done, and that the
12655    operands are in the form expected by the given opcode (this isn't necessarily
12656    the same as the form in which they were parsed, hence some massaging must
12657    take place before this function is called).
12658    Checks current arch version against that in the looked-up opcode.  */
12659
12660 static void
12661 do_vfp_nsyn_opcode (const char *opname)
12662 {
12663   const struct asm_opcode *opcode;
12664
12665   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
12666
12667   if (!opcode)
12668     abort ();
12669
12670   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
12671                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
12672               _(BAD_FPU));
12673
12674   inst.is_neon = 1;
12675
12676   if (thumb_mode)
12677     {
12678       inst.instruction = opcode->tvalue;
12679       opcode->tencode ();
12680     }
12681   else
12682     {
12683       inst.instruction = (inst.cond << 28) | opcode->avalue;
12684       opcode->aencode ();
12685     }
12686 }
12687
12688 static void
12689 do_vfp_nsyn_add_sub (enum neon_shape rs)
12690 {
12691   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
12692
12693   if (rs == NS_FFF)
12694     {
12695       if (is_add)
12696         do_vfp_nsyn_opcode ("fadds");
12697       else
12698         do_vfp_nsyn_opcode ("fsubs");
12699     }
12700   else
12701     {
12702       if (is_add)
12703         do_vfp_nsyn_opcode ("faddd");
12704       else
12705         do_vfp_nsyn_opcode ("fsubd");
12706     }
12707 }
12708
12709 /* Check operand types to see if this is a VFP instruction, and if so call
12710    PFN ().  */
12711
12712 static int
12713 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
12714 {
12715   enum neon_shape rs;
12716   struct neon_type_el et;
12717
12718   switch (args)
12719     {
12720     case 2:
12721       rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
12722       et = neon_check_type (2, rs,
12723         N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12724       break;
12725
12726     case 3:
12727       rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
12728       et = neon_check_type (3, rs,
12729         N_EQK | N_VFP, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12730       break;
12731
12732     default:
12733       abort ();
12734     }
12735
12736   if (et.type != NT_invtype)
12737     {
12738       pfn (rs);
12739       return SUCCESS;
12740     }
12741
12742   inst.error = NULL;
12743   return FAIL;
12744 }
12745
12746 static void
12747 do_vfp_nsyn_mla_mls (enum neon_shape rs)
12748 {
12749   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
12750
12751   if (rs == NS_FFF)
12752     {
12753       if (is_mla)
12754         do_vfp_nsyn_opcode ("fmacs");
12755       else
12756         do_vfp_nsyn_opcode ("fnmacs");
12757     }
12758   else
12759     {
12760       if (is_mla)
12761         do_vfp_nsyn_opcode ("fmacd");
12762       else
12763         do_vfp_nsyn_opcode ("fnmacd");
12764     }
12765 }
12766
12767 static void
12768 do_vfp_nsyn_fma_fms (enum neon_shape rs)
12769 {
12770   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
12771
12772   if (rs == NS_FFF)
12773     {
12774       if (is_fma)
12775         do_vfp_nsyn_opcode ("ffmas");
12776       else
12777         do_vfp_nsyn_opcode ("ffnmas");
12778     }
12779   else
12780     {
12781       if (is_fma)
12782         do_vfp_nsyn_opcode ("ffmad");
12783       else
12784         do_vfp_nsyn_opcode ("ffnmad");
12785     }
12786 }
12787
12788 static void
12789 do_vfp_nsyn_mul (enum neon_shape rs)
12790 {
12791   if (rs == NS_FFF)
12792     do_vfp_nsyn_opcode ("fmuls");
12793   else
12794     do_vfp_nsyn_opcode ("fmuld");
12795 }
12796
12797 static void
12798 do_vfp_nsyn_abs_neg (enum neon_shape rs)
12799 {
12800   int is_neg = (inst.instruction & 0x80) != 0;
12801   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_VFP | N_KEY);
12802
12803   if (rs == NS_FF)
12804     {
12805       if (is_neg)
12806         do_vfp_nsyn_opcode ("fnegs");
12807       else
12808         do_vfp_nsyn_opcode ("fabss");
12809     }
12810   else
12811     {
12812       if (is_neg)
12813         do_vfp_nsyn_opcode ("fnegd");
12814       else
12815         do_vfp_nsyn_opcode ("fabsd");
12816     }
12817 }
12818
12819 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
12820    insns belong to Neon, and are handled elsewhere.  */
12821
12822 static void
12823 do_vfp_nsyn_ldm_stm (int is_dbmode)
12824 {
12825   int is_ldm = (inst.instruction & (1 << 20)) != 0;
12826   if (is_ldm)
12827     {
12828       if (is_dbmode)
12829         do_vfp_nsyn_opcode ("fldmdbs");
12830       else
12831         do_vfp_nsyn_opcode ("fldmias");
12832     }
12833   else
12834     {
12835       if (is_dbmode)
12836         do_vfp_nsyn_opcode ("fstmdbs");
12837       else
12838         do_vfp_nsyn_opcode ("fstmias");
12839     }
12840 }
12841
12842 static void
12843 do_vfp_nsyn_sqrt (void)
12844 {
12845   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
12846   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12847
12848   if (rs == NS_FF)
12849     do_vfp_nsyn_opcode ("fsqrts");
12850   else
12851     do_vfp_nsyn_opcode ("fsqrtd");
12852 }
12853
12854 static void
12855 do_vfp_nsyn_div (void)
12856 {
12857   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
12858   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
12859     N_F32 | N_F64 | N_KEY | N_VFP);
12860
12861   if (rs == NS_FFF)
12862     do_vfp_nsyn_opcode ("fdivs");
12863   else
12864     do_vfp_nsyn_opcode ("fdivd");
12865 }
12866
12867 static void
12868 do_vfp_nsyn_nmul (void)
12869 {
12870   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
12871   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
12872     N_F32 | N_F64 | N_KEY | N_VFP);
12873
12874   if (rs == NS_FFF)
12875     {
12876       NEON_ENCODE (SINGLE, inst);
12877       do_vfp_sp_dyadic ();
12878     }
12879   else
12880     {
12881       NEON_ENCODE (DOUBLE, inst);
12882       do_vfp_dp_rd_rn_rm ();
12883     }
12884   do_vfp_cond_or_thumb ();
12885 }
12886
12887 static void
12888 do_vfp_nsyn_cmp (void)
12889 {
12890   if (inst.operands[1].isreg)
12891     {
12892       enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
12893       neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12894
12895       if (rs == NS_FF)
12896         {
12897           NEON_ENCODE (SINGLE, inst);
12898           do_vfp_sp_monadic ();
12899         }
12900       else
12901         {
12902           NEON_ENCODE (DOUBLE, inst);
12903           do_vfp_dp_rd_rm ();
12904         }
12905     }
12906   else
12907     {
12908       enum neon_shape rs = neon_select_shape (NS_FI, NS_DI, NS_NULL);
12909       neon_check_type (2, rs, N_F32 | N_F64 | N_KEY | N_VFP, N_EQK);
12910
12911       switch (inst.instruction & 0x0fffffff)
12912         {
12913         case N_MNEM_vcmp:
12914           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
12915           break;
12916         case N_MNEM_vcmpe:
12917           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
12918           break;
12919         default:
12920           abort ();
12921         }
12922
12923       if (rs == NS_FI)
12924         {
12925           NEON_ENCODE (SINGLE, inst);
12926           do_vfp_sp_compare_z ();
12927         }
12928       else
12929         {
12930           NEON_ENCODE (DOUBLE, inst);
12931           do_vfp_dp_rd ();
12932         }
12933     }
12934   do_vfp_cond_or_thumb ();
12935 }
12936
12937 static void
12938 nsyn_insert_sp (void)
12939 {
12940   inst.operands[1] = inst.operands[0];
12941   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
12942   inst.operands[0].reg = REG_SP;
12943   inst.operands[0].isreg = 1;
12944   inst.operands[0].writeback = 1;
12945   inst.operands[0].present = 1;
12946 }
12947
12948 static void
12949 do_vfp_nsyn_push (void)
12950 {
12951   nsyn_insert_sp ();
12952   if (inst.operands[1].issingle)
12953     do_vfp_nsyn_opcode ("fstmdbs");
12954   else
12955     do_vfp_nsyn_opcode ("fstmdbd");
12956 }
12957
12958 static void
12959 do_vfp_nsyn_pop (void)
12960 {
12961   nsyn_insert_sp ();
12962   if (inst.operands[1].issingle)
12963     do_vfp_nsyn_opcode ("fldmias");
12964   else
12965     do_vfp_nsyn_opcode ("fldmiad");
12966 }
12967
12968 /* Fix up Neon data-processing instructions, ORing in the correct bits for
12969    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
12970
12971 static void
12972 neon_dp_fixup (struct arm_it* insn)
12973 {
12974   unsigned int i = insn->instruction;
12975   insn->is_neon = 1;
12976
12977   if (thumb_mode)
12978     {
12979       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
12980       if (i & (1 << 24))
12981         i |= 1 << 28;
12982
12983       i &= ~(1 << 24);
12984
12985       i |= 0xef000000;
12986     }
12987   else
12988     i |= 0xf2000000;
12989
12990   insn->instruction = i;
12991 }
12992
12993 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
12994    (0, 1, 2, 3).  */
12995
12996 static unsigned
12997 neon_logbits (unsigned x)
12998 {
12999   return ffs (x) - 4;
13000 }
13001
13002 #define LOW4(R) ((R) & 0xf)
13003 #define HI1(R) (((R) >> 4) & 1)
13004
13005 /* Encode insns with bit pattern:
13006
13007   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
13008   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
13009
13010   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
13011   different meaning for some instruction.  */
13012
13013 static void
13014 neon_three_same (int isquad, int ubit, int size)
13015 {
13016   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13017   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13018   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
13019   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
13020   inst.instruction |= LOW4 (inst.operands[2].reg);
13021   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
13022   inst.instruction |= (isquad != 0) << 6;
13023   inst.instruction |= (ubit != 0) << 24;
13024   if (size != -1)
13025     inst.instruction |= neon_logbits (size) << 20;
13026
13027   neon_dp_fixup (&inst);
13028 }
13029
13030 /* Encode instructions of the form:
13031
13032   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
13033   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
13034
13035   Don't write size if SIZE == -1.  */
13036
13037 static void
13038 neon_two_same (int qbit, int ubit, int size)
13039 {
13040   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13041   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13042   inst.instruction |= LOW4 (inst.operands[1].reg);
13043   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13044   inst.instruction |= (qbit != 0) << 6;
13045   inst.instruction |= (ubit != 0) << 24;
13046
13047   if (size != -1)
13048     inst.instruction |= neon_logbits (size) << 18;
13049
13050   neon_dp_fixup (&inst);
13051 }
13052
13053 /* Neon instruction encoders, in approximate order of appearance.  */
13054
13055 static void
13056 do_neon_dyadic_i_su (void)
13057 {
13058   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13059   struct neon_type_el et = neon_check_type (3, rs,
13060     N_EQK, N_EQK, N_SU_32 | N_KEY);
13061   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13062 }
13063
13064 static void
13065 do_neon_dyadic_i64_su (void)
13066 {
13067   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13068   struct neon_type_el et = neon_check_type (3, rs,
13069     N_EQK, N_EQK, N_SU_ALL | N_KEY);
13070   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13071 }
13072
13073 static void
13074 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
13075                 unsigned immbits)
13076 {
13077   unsigned size = et.size >> 3;
13078   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13079   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13080   inst.instruction |= LOW4 (inst.operands[1].reg);
13081   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13082   inst.instruction |= (isquad != 0) << 6;
13083   inst.instruction |= immbits << 16;
13084   inst.instruction |= (size >> 3) << 7;
13085   inst.instruction |= (size & 0x7) << 19;
13086   if (write_ubit)
13087     inst.instruction |= (uval != 0) << 24;
13088
13089   neon_dp_fixup (&inst);
13090 }
13091
13092 static void
13093 do_neon_shl_imm (void)
13094 {
13095   if (!inst.operands[2].isreg)
13096     {
13097       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13098       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
13099       NEON_ENCODE (IMMED, inst);
13100       neon_imm_shift (FALSE, 0, neon_quad (rs), et, inst.operands[2].imm);
13101     }
13102   else
13103     {
13104       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13105       struct neon_type_el et = neon_check_type (3, rs,
13106         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
13107       unsigned int tmp;
13108
13109       /* VSHL/VQSHL 3-register variants have syntax such as:
13110            vshl.xx Dd, Dm, Dn
13111          whereas other 3-register operations encoded by neon_three_same have
13112          syntax like:
13113            vadd.xx Dd, Dn, Dm
13114          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
13115          here.  */
13116       tmp = inst.operands[2].reg;
13117       inst.operands[2].reg = inst.operands[1].reg;
13118       inst.operands[1].reg = tmp;
13119       NEON_ENCODE (INTEGER, inst);
13120       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13121     }
13122 }
13123
13124 static void
13125 do_neon_qshl_imm (void)
13126 {
13127   if (!inst.operands[2].isreg)
13128     {
13129       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13130       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
13131
13132       NEON_ENCODE (IMMED, inst);
13133       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
13134                       inst.operands[2].imm);
13135     }
13136   else
13137     {
13138       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13139       struct neon_type_el et = neon_check_type (3, rs,
13140         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
13141       unsigned int tmp;
13142
13143       /* See note in do_neon_shl_imm.  */
13144       tmp = inst.operands[2].reg;
13145       inst.operands[2].reg = inst.operands[1].reg;
13146       inst.operands[1].reg = tmp;
13147       NEON_ENCODE (INTEGER, inst);
13148       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13149     }
13150 }
13151
13152 static void
13153 do_neon_rshl (void)
13154 {
13155   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13156   struct neon_type_el et = neon_check_type (3, rs,
13157     N_EQK, N_EQK, N_SU_ALL | N_KEY);
13158   unsigned int tmp;
13159
13160   tmp = inst.operands[2].reg;
13161   inst.operands[2].reg = inst.operands[1].reg;
13162   inst.operands[1].reg = tmp;
13163   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13164 }
13165
13166 static int
13167 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
13168 {
13169   /* Handle .I8 pseudo-instructions.  */
13170   if (size == 8)
13171     {
13172       /* Unfortunately, this will make everything apart from zero out-of-range.
13173          FIXME is this the intended semantics? There doesn't seem much point in
13174          accepting .I8 if so.  */
13175       immediate |= immediate << 8;
13176       size = 16;
13177     }
13178
13179   if (size >= 32)
13180     {
13181       if (immediate == (immediate & 0x000000ff))
13182         {
13183           *immbits = immediate;
13184           return 0x1;
13185         }
13186       else if (immediate == (immediate & 0x0000ff00))
13187         {
13188           *immbits = immediate >> 8;
13189           return 0x3;
13190         }
13191       else if (immediate == (immediate & 0x00ff0000))
13192         {
13193           *immbits = immediate >> 16;
13194           return 0x5;
13195         }
13196       else if (immediate == (immediate & 0xff000000))
13197         {
13198           *immbits = immediate >> 24;
13199           return 0x7;
13200         }
13201       if ((immediate & 0xffff) != (immediate >> 16))
13202         goto bad_immediate;
13203       immediate &= 0xffff;
13204     }
13205
13206   if (immediate == (immediate & 0x000000ff))
13207     {
13208       *immbits = immediate;
13209       return 0x9;
13210     }
13211   else if (immediate == (immediate & 0x0000ff00))
13212     {
13213       *immbits = immediate >> 8;
13214       return 0xb;
13215     }
13216
13217   bad_immediate:
13218   first_error (_("immediate value out of range"));
13219   return FAIL;
13220 }
13221
13222 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
13223    A, B, C, D.  */
13224
13225 static int
13226 neon_bits_same_in_bytes (unsigned imm)
13227 {
13228   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
13229          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
13230          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
13231          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
13232 }
13233
13234 /* For immediate of above form, return 0bABCD.  */
13235
13236 static unsigned
13237 neon_squash_bits (unsigned imm)
13238 {
13239   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
13240          | ((imm & 0x01000000) >> 21);
13241 }
13242
13243 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
13244
13245 static unsigned
13246 neon_qfloat_bits (unsigned imm)
13247 {
13248   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
13249 }
13250
13251 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
13252    the instruction. *OP is passed as the initial value of the op field, and
13253    may be set to a different value depending on the constant (i.e.
13254    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
13255    MVN).  If the immediate looks like a repeated pattern then also
13256    try smaller element sizes.  */
13257
13258 static int
13259 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
13260                          unsigned *immbits, int *op, int size,
13261                          enum neon_el_type type)
13262 {
13263   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
13264      float.  */
13265   if (type == NT_float && !float_p)
13266     return FAIL;
13267
13268   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
13269     {
13270       if (size != 32 || *op == 1)
13271         return FAIL;
13272       *immbits = neon_qfloat_bits (immlo);
13273       return 0xf;
13274     }
13275
13276   if (size == 64)
13277     {
13278       if (neon_bits_same_in_bytes (immhi)
13279           && neon_bits_same_in_bytes (immlo))
13280         {
13281           if (*op == 1)
13282             return FAIL;
13283           *immbits = (neon_squash_bits (immhi) << 4)
13284                      | neon_squash_bits (immlo);
13285           *op = 1;
13286           return 0xe;
13287         }
13288
13289       if (immhi != immlo)
13290         return FAIL;
13291     }
13292
13293   if (size >= 32)
13294     {
13295       if (immlo == (immlo & 0x000000ff))
13296         {
13297           *immbits = immlo;
13298           return 0x0;
13299         }
13300       else if (immlo == (immlo & 0x0000ff00))
13301         {
13302           *immbits = immlo >> 8;
13303           return 0x2;
13304         }
13305       else if (immlo == (immlo & 0x00ff0000))
13306         {
13307           *immbits = immlo >> 16;
13308           return 0x4;
13309         }
13310       else if (immlo == (immlo & 0xff000000))
13311         {
13312           *immbits = immlo >> 24;
13313           return 0x6;
13314         }
13315       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
13316         {
13317           *immbits = (immlo >> 8) & 0xff;
13318           return 0xc;
13319         }
13320       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
13321         {
13322           *immbits = (immlo >> 16) & 0xff;
13323           return 0xd;
13324         }
13325
13326       if ((immlo & 0xffff) != (immlo >> 16))
13327         return FAIL;
13328       immlo &= 0xffff;
13329     }
13330
13331   if (size >= 16)
13332     {
13333       if (immlo == (immlo & 0x000000ff))
13334         {
13335           *immbits = immlo;
13336           return 0x8;
13337         }
13338       else if (immlo == (immlo & 0x0000ff00))
13339         {
13340           *immbits = immlo >> 8;
13341           return 0xa;
13342         }
13343
13344       if ((immlo & 0xff) != (immlo >> 8))
13345         return FAIL;
13346       immlo &= 0xff;
13347     }
13348
13349   if (immlo == (immlo & 0x000000ff))
13350     {
13351       /* Don't allow MVN with 8-bit immediate.  */
13352       if (*op == 1)
13353         return FAIL;
13354       *immbits = immlo;
13355       return 0xe;
13356     }
13357
13358   return FAIL;
13359 }
13360
13361 /* Write immediate bits [7:0] to the following locations:
13362
13363   |28/24|23     19|18 16|15                    4|3     0|
13364   |  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|
13365
13366   This function is used by VMOV/VMVN/VORR/VBIC.  */
13367
13368 static void
13369 neon_write_immbits (unsigned immbits)
13370 {
13371   inst.instruction |= immbits & 0xf;
13372   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
13373   inst.instruction |= ((immbits >> 7) & 0x1) << 24;
13374 }
13375
13376 /* Invert low-order SIZE bits of XHI:XLO.  */
13377
13378 static void
13379 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
13380 {
13381   unsigned immlo = xlo ? *xlo : 0;
13382   unsigned immhi = xhi ? *xhi : 0;
13383
13384   switch (size)
13385     {
13386     case 8:
13387       immlo = (~immlo) & 0xff;
13388       break;
13389
13390     case 16:
13391       immlo = (~immlo) & 0xffff;
13392       break;
13393
13394     case 64:
13395       immhi = (~immhi) & 0xffffffff;
13396       /* fall through.  */
13397
13398     case 32:
13399       immlo = (~immlo) & 0xffffffff;
13400       break;
13401
13402     default:
13403       abort ();
13404     }
13405
13406   if (xlo)
13407     *xlo = immlo;
13408
13409   if (xhi)
13410     *xhi = immhi;
13411 }
13412
13413 static void
13414 do_neon_logic (void)
13415 {
13416   if (inst.operands[2].present && inst.operands[2].isreg)
13417     {
13418       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13419       neon_check_type (3, rs, N_IGNORE_TYPE);
13420       /* U bit and size field were set as part of the bitmask.  */
13421       NEON_ENCODE (INTEGER, inst);
13422       neon_three_same (neon_quad (rs), 0, -1);
13423     }
13424   else
13425     {
13426       const int three_ops_form = (inst.operands[2].present
13427                                   && !inst.operands[2].isreg);
13428       const int immoperand = (three_ops_form ? 2 : 1);
13429       enum neon_shape rs = (three_ops_form
13430                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
13431                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
13432       struct neon_type_el et = neon_check_type (2, rs,
13433         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
13434       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
13435       unsigned immbits;
13436       int cmode;
13437
13438       if (et.type == NT_invtype)
13439         return;
13440
13441       if (three_ops_form)
13442         constraint (inst.operands[0].reg != inst.operands[1].reg,
13443                     _("first and second operands shall be the same register"));
13444
13445       NEON_ENCODE (IMMED, inst);
13446
13447       immbits = inst.operands[immoperand].imm;
13448       if (et.size == 64)
13449         {
13450           /* .i64 is a pseudo-op, so the immediate must be a repeating
13451              pattern.  */
13452           if (immbits != (inst.operands[immoperand].regisimm ?
13453                           inst.operands[immoperand].reg : 0))
13454             {
13455               /* Set immbits to an invalid constant.  */
13456               immbits = 0xdeadbeef;
13457             }
13458         }
13459
13460       switch (opcode)
13461         {
13462         case N_MNEM_vbic:
13463           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13464           break;
13465
13466         case N_MNEM_vorr:
13467           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13468           break;
13469
13470         case N_MNEM_vand:
13471           /* Pseudo-instruction for VBIC.  */
13472           neon_invert_size (&immbits, 0, et.size);
13473           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13474           break;
13475
13476         case N_MNEM_vorn:
13477           /* Pseudo-instruction for VORR.  */
13478           neon_invert_size (&immbits, 0, et.size);
13479           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13480           break;
13481
13482         default:
13483           abort ();
13484         }
13485
13486       if (cmode == FAIL)
13487         return;
13488
13489       inst.instruction |= neon_quad (rs) << 6;
13490       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13491       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13492       inst.instruction |= cmode << 8;
13493       neon_write_immbits (immbits);
13494
13495       neon_dp_fixup (&inst);
13496     }
13497 }
13498
13499 static void
13500 do_neon_bitfield (void)
13501 {
13502   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13503   neon_check_type (3, rs, N_IGNORE_TYPE);
13504   neon_three_same (neon_quad (rs), 0, -1);
13505 }
13506
13507 static void
13508 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
13509                   unsigned destbits)
13510 {
13511   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13512   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
13513                                             types | N_KEY);
13514   if (et.type == NT_float)
13515     {
13516       NEON_ENCODE (FLOAT, inst);
13517       neon_three_same (neon_quad (rs), 0, -1);
13518     }
13519   else
13520     {
13521       NEON_ENCODE (INTEGER, inst);
13522       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
13523     }
13524 }
13525
13526 static void
13527 do_neon_dyadic_if_su (void)
13528 {
13529   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
13530 }
13531
13532 static void
13533 do_neon_dyadic_if_su_d (void)
13534 {
13535   /* This version only allow D registers, but that constraint is enforced during
13536      operand parsing so we don't need to do anything extra here.  */
13537   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
13538 }
13539
13540 static void
13541 do_neon_dyadic_if_i_d (void)
13542 {
13543   /* The "untyped" case can't happen. Do this to stop the "U" bit being
13544      affected if we specify unsigned args.  */
13545   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
13546 }
13547
13548 enum vfp_or_neon_is_neon_bits
13549 {
13550   NEON_CHECK_CC = 1,
13551   NEON_CHECK_ARCH = 2
13552 };
13553
13554 /* Call this function if an instruction which may have belonged to the VFP or
13555    Neon instruction sets, but turned out to be a Neon instruction (due to the
13556    operand types involved, etc.). We have to check and/or fix-up a couple of
13557    things:
13558
13559      - Make sure the user hasn't attempted to make a Neon instruction
13560        conditional.
13561      - Alter the value in the condition code field if necessary.
13562      - Make sure that the arch supports Neon instructions.
13563
13564    Which of these operations take place depends on bits from enum
13565    vfp_or_neon_is_neon_bits.
13566
13567    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
13568    current instruction's condition is COND_ALWAYS, the condition field is
13569    changed to inst.uncond_value. This is necessary because instructions shared
13570    between VFP and Neon may be conditional for the VFP variants only, and the
13571    unconditional Neon version must have, e.g., 0xF in the condition field.  */
13572
13573 static int
13574 vfp_or_neon_is_neon (unsigned check)
13575 {
13576   /* Conditions are always legal in Thumb mode (IT blocks).  */
13577   if (!thumb_mode && (check & NEON_CHECK_CC))
13578     {
13579       if (inst.cond != COND_ALWAYS)
13580         {
13581           first_error (_(BAD_COND));
13582           return FAIL;
13583         }
13584       if (inst.uncond_value != -1)
13585         inst.instruction |= inst.uncond_value << 28;
13586     }
13587
13588   if ((check & NEON_CHECK_ARCH)
13589       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
13590     {
13591       first_error (_(BAD_FPU));
13592       return FAIL;
13593     }
13594
13595   return SUCCESS;
13596 }
13597
13598 static void
13599 do_neon_addsub_if_i (void)
13600 {
13601   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
13602     return;
13603
13604   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13605     return;
13606
13607   /* The "untyped" case can't happen. Do this to stop the "U" bit being
13608      affected if we specify unsigned args.  */
13609   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
13610 }
13611
13612 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
13613    result to be:
13614      V<op> A,B     (A is operand 0, B is operand 2)
13615    to mean:
13616      V<op> A,B,A
13617    not:
13618      V<op> A,B,B
13619    so handle that case specially.  */
13620
13621 static void
13622 neon_exchange_operands (void)
13623 {
13624   void *scratch = alloca (sizeof (inst.operands[0]));
13625   if (inst.operands[1].present)
13626     {
13627       /* Swap operands[1] and operands[2].  */
13628       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
13629       inst.operands[1] = inst.operands[2];
13630       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
13631     }
13632   else
13633     {
13634       inst.operands[1] = inst.operands[2];
13635       inst.operands[2] = inst.operands[0];
13636     }
13637 }
13638
13639 static void
13640 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
13641 {
13642   if (inst.operands[2].isreg)
13643     {
13644       if (invert)
13645         neon_exchange_operands ();
13646       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
13647     }
13648   else
13649     {
13650       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13651       struct neon_type_el et = neon_check_type (2, rs,
13652         N_EQK | N_SIZ, immtypes | N_KEY);
13653
13654       NEON_ENCODE (IMMED, inst);
13655       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13656       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13657       inst.instruction |= LOW4 (inst.operands[1].reg);
13658       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13659       inst.instruction |= neon_quad (rs) << 6;
13660       inst.instruction |= (et.type == NT_float) << 10;
13661       inst.instruction |= neon_logbits (et.size) << 18;
13662
13663       neon_dp_fixup (&inst);
13664     }
13665 }
13666
13667 static void
13668 do_neon_cmp (void)
13669 {
13670   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, FALSE);
13671 }
13672
13673 static void
13674 do_neon_cmp_inv (void)
13675 {
13676   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, TRUE);
13677 }
13678
13679 static void
13680 do_neon_ceq (void)
13681 {
13682   neon_compare (N_IF_32, N_IF_32, FALSE);
13683 }
13684
13685 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
13686    scalars, which are encoded in 5 bits, M : Rm.
13687    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
13688    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
13689    index in M.  */
13690
13691 static unsigned
13692 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
13693 {
13694   unsigned regno = NEON_SCALAR_REG (scalar);
13695   unsigned elno = NEON_SCALAR_INDEX (scalar);
13696
13697   switch (elsize)
13698     {
13699     case 16:
13700       if (regno > 7 || elno > 3)
13701         goto bad_scalar;
13702       return regno | (elno << 3);
13703
13704     case 32:
13705       if (regno > 15 || elno > 1)
13706         goto bad_scalar;
13707       return regno | (elno << 4);
13708
13709     default:
13710     bad_scalar:
13711       first_error (_("scalar out of range for multiply instruction"));
13712     }
13713
13714   return 0;
13715 }
13716
13717 /* Encode multiply / multiply-accumulate scalar instructions.  */
13718
13719 static void
13720 neon_mul_mac (struct neon_type_el et, int ubit)
13721 {
13722   unsigned scalar;
13723
13724   /* Give a more helpful error message if we have an invalid type.  */
13725   if (et.type == NT_invtype)
13726     return;
13727
13728   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
13729   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13730   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13731   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
13732   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
13733   inst.instruction |= LOW4 (scalar);
13734   inst.instruction |= HI1 (scalar) << 5;
13735   inst.instruction |= (et.type == NT_float) << 8;
13736   inst.instruction |= neon_logbits (et.size) << 20;
13737   inst.instruction |= (ubit != 0) << 24;
13738
13739   neon_dp_fixup (&inst);
13740 }
13741
13742 static void
13743 do_neon_mac_maybe_scalar (void)
13744 {
13745   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
13746     return;
13747
13748   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13749     return;
13750
13751   if (inst.operands[2].isscalar)
13752     {
13753       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
13754       struct neon_type_el et = neon_check_type (3, rs,
13755         N_EQK, N_EQK, N_I16 | N_I32 | N_F32 | N_KEY);
13756       NEON_ENCODE (SCALAR, inst);
13757       neon_mul_mac (et, neon_quad (rs));
13758     }
13759   else
13760     {
13761       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
13762          affected if we specify unsigned args.  */
13763       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
13764     }
13765 }
13766
13767 static void
13768 do_neon_fmac (void)
13769 {
13770   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
13771     return;
13772
13773   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13774     return;
13775
13776   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
13777 }
13778
13779 static void
13780 do_neon_tst (void)
13781 {
13782   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13783   struct neon_type_el et = neon_check_type (3, rs,
13784     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
13785   neon_three_same (neon_quad (rs), 0, et.size);
13786 }
13787
13788 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
13789    same types as the MAC equivalents. The polynomial type for this instruction
13790    is encoded the same as the integer type.  */
13791
13792 static void
13793 do_neon_mul (void)
13794 {
13795   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
13796     return;
13797
13798   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13799     return;
13800
13801   if (inst.operands[2].isscalar)
13802     do_neon_mac_maybe_scalar ();
13803   else
13804     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F32 | N_P8, 0);
13805 }
13806
13807 static void
13808 do_neon_qdmulh (void)
13809 {
13810   if (inst.operands[2].isscalar)
13811     {
13812       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
13813       struct neon_type_el et = neon_check_type (3, rs,
13814         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
13815       NEON_ENCODE (SCALAR, inst);
13816       neon_mul_mac (et, neon_quad (rs));
13817     }
13818   else
13819     {
13820       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13821       struct neon_type_el et = neon_check_type (3, rs,
13822         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
13823       NEON_ENCODE (INTEGER, inst);
13824       /* The U bit (rounding) comes from bit mask.  */
13825       neon_three_same (neon_quad (rs), 0, et.size);
13826     }
13827 }
13828
13829 static void
13830 do_neon_fcmp_absolute (void)
13831 {
13832   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13833   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
13834   /* Size field comes from bit mask.  */
13835   neon_three_same (neon_quad (rs), 1, -1);
13836 }
13837
13838 static void
13839 do_neon_fcmp_absolute_inv (void)
13840 {
13841   neon_exchange_operands ();
13842   do_neon_fcmp_absolute ();
13843 }
13844
13845 static void
13846 do_neon_step (void)
13847 {
13848   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13849   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
13850   neon_three_same (neon_quad (rs), 0, -1);
13851 }
13852
13853 static void
13854 do_neon_abs_neg (void)
13855 {
13856   enum neon_shape rs;
13857   struct neon_type_el et;
13858
13859   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
13860     return;
13861
13862   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13863     return;
13864
13865   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13866   et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_F32 | N_KEY);
13867
13868   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13869   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13870   inst.instruction |= LOW4 (inst.operands[1].reg);
13871   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13872   inst.instruction |= neon_quad (rs) << 6;
13873   inst.instruction |= (et.type == NT_float) << 10;
13874   inst.instruction |= neon_logbits (et.size) << 18;
13875
13876   neon_dp_fixup (&inst);
13877 }
13878
13879 static void
13880 do_neon_sli (void)
13881 {
13882   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13883   struct neon_type_el et = neon_check_type (2, rs,
13884     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
13885   int imm = inst.operands[2].imm;
13886   constraint (imm < 0 || (unsigned)imm >= et.size,
13887               _("immediate out of range for insert"));
13888   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
13889 }
13890
13891 static void
13892 do_neon_sri (void)
13893 {
13894   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13895   struct neon_type_el et = neon_check_type (2, rs,
13896     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
13897   int imm = inst.operands[2].imm;
13898   constraint (imm < 1 || (unsigned)imm > et.size,
13899               _("immediate out of range for insert"));
13900   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
13901 }
13902
13903 static void
13904 do_neon_qshlu_imm (void)
13905 {
13906   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13907   struct neon_type_el et = neon_check_type (2, rs,
13908     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
13909   int imm = inst.operands[2].imm;
13910   constraint (imm < 0 || (unsigned)imm >= et.size,
13911               _("immediate out of range for shift"));
13912   /* Only encodes the 'U present' variant of the instruction.
13913      In this case, signed types have OP (bit 8) set to 0.
13914      Unsigned types have OP set to 1.  */
13915   inst.instruction |= (et.type == NT_unsigned) << 8;
13916   /* The rest of the bits are the same as other immediate shifts.  */
13917   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
13918 }
13919
13920 static void
13921 do_neon_qmovn (void)
13922 {
13923   struct neon_type_el et = neon_check_type (2, NS_DQ,
13924     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
13925   /* Saturating move where operands can be signed or unsigned, and the
13926      destination has the same signedness.  */
13927   NEON_ENCODE (INTEGER, inst);
13928   if (et.type == NT_unsigned)
13929     inst.instruction |= 0xc0;
13930   else
13931     inst.instruction |= 0x80;
13932   neon_two_same (0, 1, et.size / 2);
13933 }
13934
13935 static void
13936 do_neon_qmovun (void)
13937 {
13938   struct neon_type_el et = neon_check_type (2, NS_DQ,
13939     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
13940   /* Saturating move with unsigned results. Operands must be signed.  */
13941   NEON_ENCODE (INTEGER, inst);
13942   neon_two_same (0, 1, et.size / 2);
13943 }
13944
13945 static void
13946 do_neon_rshift_sat_narrow (void)
13947 {
13948   /* FIXME: Types for narrowing. If operands are signed, results can be signed
13949      or unsigned. If operands are unsigned, results must also be unsigned.  */
13950   struct neon_type_el et = neon_check_type (2, NS_DQI,
13951     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
13952   int imm = inst.operands[2].imm;
13953   /* This gets the bounds check, size encoding and immediate bits calculation
13954      right.  */
13955   et.size /= 2;
13956
13957   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
13958      VQMOVN.I<size> <Dd>, <Qm>.  */
13959   if (imm == 0)
13960     {
13961       inst.operands[2].present = 0;
13962       inst.instruction = N_MNEM_vqmovn;
13963       do_neon_qmovn ();
13964       return;
13965     }
13966
13967   constraint (imm < 1 || (unsigned)imm > et.size,
13968               _("immediate out of range"));
13969   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
13970 }
13971
13972 static void
13973 do_neon_rshift_sat_narrow_u (void)
13974 {
13975   /* FIXME: Types for narrowing. If operands are signed, results can be signed
13976      or unsigned. If operands are unsigned, results must also be unsigned.  */
13977   struct neon_type_el et = neon_check_type (2, NS_DQI,
13978     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
13979   int imm = inst.operands[2].imm;
13980   /* This gets the bounds check, size encoding and immediate bits calculation
13981      right.  */
13982   et.size /= 2;
13983
13984   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
13985      VQMOVUN.I<size> <Dd>, <Qm>.  */
13986   if (imm == 0)
13987     {
13988       inst.operands[2].present = 0;
13989       inst.instruction = N_MNEM_vqmovun;
13990       do_neon_qmovun ();
13991       return;
13992     }
13993
13994   constraint (imm < 1 || (unsigned)imm > et.size,
13995               _("immediate out of range"));
13996   /* FIXME: The manual is kind of unclear about what value U should have in
13997      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
13998      must be 1.  */
13999   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
14000 }
14001
14002 static void
14003 do_neon_movn (void)
14004 {
14005   struct neon_type_el et = neon_check_type (2, NS_DQ,
14006     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14007   NEON_ENCODE (INTEGER, inst);
14008   neon_two_same (0, 1, et.size / 2);
14009 }
14010
14011 static void
14012 do_neon_rshift_narrow (void)
14013 {
14014   struct neon_type_el et = neon_check_type (2, NS_DQI,
14015     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14016   int imm = inst.operands[2].imm;
14017   /* This gets the bounds check, size encoding and immediate bits calculation
14018      right.  */
14019   et.size /= 2;
14020
14021   /* If immediate is zero then we are a pseudo-instruction for
14022      VMOVN.I<size> <Dd>, <Qm>  */
14023   if (imm == 0)
14024     {
14025       inst.operands[2].present = 0;
14026       inst.instruction = N_MNEM_vmovn;
14027       do_neon_movn ();
14028       return;
14029     }
14030
14031   constraint (imm < 1 || (unsigned)imm > et.size,
14032               _("immediate out of range for narrowing operation"));
14033   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
14034 }
14035
14036 static void
14037 do_neon_shll (void)
14038 {
14039   /* FIXME: Type checking when lengthening.  */
14040   struct neon_type_el et = neon_check_type (2, NS_QDI,
14041     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
14042   unsigned imm = inst.operands[2].imm;
14043
14044   if (imm == et.size)
14045     {
14046       /* Maximum shift variant.  */
14047       NEON_ENCODE (INTEGER, inst);
14048       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14049       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14050       inst.instruction |= LOW4 (inst.operands[1].reg);
14051       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14052       inst.instruction |= neon_logbits (et.size) << 18;
14053
14054       neon_dp_fixup (&inst);
14055     }
14056   else
14057     {
14058       /* A more-specific type check for non-max versions.  */
14059       et = neon_check_type (2, NS_QDI,
14060         N_EQK | N_DBL, N_SU_32 | N_KEY);
14061       NEON_ENCODE (IMMED, inst);
14062       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
14063     }
14064 }
14065
14066 /* Check the various types for the VCVT instruction, and return which version
14067    the current instruction is.  */
14068
14069 static int
14070 neon_cvt_flavour (enum neon_shape rs)
14071 {
14072 #define CVT_VAR(C,X,Y)                                                  \
14073   et = neon_check_type (2, rs, whole_reg | (X), whole_reg | (Y));       \
14074   if (et.type != NT_invtype)                                            \
14075     {                                                                   \
14076       inst.error = NULL;                                                \
14077       return (C);                                                       \
14078     }
14079   struct neon_type_el et;
14080   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
14081                         || rs == NS_FF) ? N_VFP : 0;
14082   /* The instruction versions which take an immediate take one register
14083      argument, which is extended to the width of the full register. Thus the
14084      "source" and "destination" registers must have the same width.  Hack that
14085      here by making the size equal to the key (wider, in this case) operand.  */
14086   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
14087
14088   CVT_VAR (0, N_S32, N_F32);
14089   CVT_VAR (1, N_U32, N_F32);
14090   CVT_VAR (2, N_F32, N_S32);
14091   CVT_VAR (3, N_F32, N_U32);
14092   /* Half-precision conversions.  */
14093   CVT_VAR (4, N_F32, N_F16);
14094   CVT_VAR (5, N_F16, N_F32);
14095
14096   whole_reg = N_VFP;
14097
14098   /* VFP instructions.  */
14099   CVT_VAR (6, N_F32, N_F64);
14100   CVT_VAR (7, N_F64, N_F32);
14101   CVT_VAR (8, N_S32, N_F64 | key);
14102   CVT_VAR (9, N_U32, N_F64 | key);
14103   CVT_VAR (10, N_F64 | key, N_S32);
14104   CVT_VAR (11, N_F64 | key, N_U32);
14105   /* VFP instructions with bitshift.  */
14106   CVT_VAR (12, N_F32 | key, N_S16);
14107   CVT_VAR (13, N_F32 | key, N_U16);
14108   CVT_VAR (14, N_F64 | key, N_S16);
14109   CVT_VAR (15, N_F64 | key, N_U16);
14110   CVT_VAR (16, N_S16, N_F32 | key);
14111   CVT_VAR (17, N_U16, N_F32 | key);
14112   CVT_VAR (18, N_S16, N_F64 | key);
14113   CVT_VAR (19, N_U16, N_F64 | key);
14114
14115   return -1;
14116 #undef CVT_VAR
14117 }
14118
14119 /* Neon-syntax VFP conversions.  */
14120
14121 static void
14122 do_vfp_nsyn_cvt (enum neon_shape rs, int flavour)
14123 {
14124   const char *opname = 0;
14125
14126   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI)
14127     {
14128       /* Conversions with immediate bitshift.  */
14129       const char *enc[] =
14130         {
14131           "ftosls",
14132           "ftouls",
14133           "fsltos",
14134           "fultos",
14135           NULL,
14136           NULL,
14137           NULL,
14138           NULL,
14139           "ftosld",
14140           "ftould",
14141           "fsltod",
14142           "fultod",
14143           "fshtos",
14144           "fuhtos",
14145           "fshtod",
14146           "fuhtod",
14147           "ftoshs",
14148           "ftouhs",
14149           "ftoshd",
14150           "ftouhd"
14151         };
14152
14153       if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc))
14154         {
14155           opname = enc[flavour];
14156           constraint (inst.operands[0].reg != inst.operands[1].reg,
14157                       _("operands 0 and 1 must be the same register"));
14158           inst.operands[1] = inst.operands[2];
14159           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
14160         }
14161     }
14162   else
14163     {
14164       /* Conversions without bitshift.  */
14165       const char *enc[] =
14166         {
14167           "ftosis",
14168           "ftouis",
14169           "fsitos",
14170           "fuitos",
14171           "NULL",
14172           "NULL",
14173           "fcvtsd",
14174           "fcvtds",
14175           "ftosid",
14176           "ftouid",
14177           "fsitod",
14178           "fuitod"
14179         };
14180
14181       if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc))
14182         opname = enc[flavour];
14183     }
14184
14185   if (opname)
14186     do_vfp_nsyn_opcode (opname);
14187 }
14188
14189 static void
14190 do_vfp_nsyn_cvtz (void)
14191 {
14192   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_NULL);
14193   int flavour = neon_cvt_flavour (rs);
14194   const char *enc[] =
14195     {
14196       "ftosizs",
14197       "ftouizs",
14198       NULL,
14199       NULL,
14200       NULL,
14201       NULL,
14202       NULL,
14203       NULL,
14204       "ftosizd",
14205       "ftouizd"
14206     };
14207
14208   if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
14209     do_vfp_nsyn_opcode (enc[flavour]);
14210 }
14211
14212 static void
14213 do_neon_cvt_1 (bfd_boolean round_to_zero ATTRIBUTE_UNUSED)
14214 {
14215   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
14216     NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ, NS_NULL);
14217   int flavour = neon_cvt_flavour (rs);
14218
14219   /* PR11109: Handle round-to-zero for VCVT conversions.  */
14220   if (round_to_zero
14221       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
14222       && (flavour == 0 || flavour == 1 || flavour == 8 || flavour == 9)
14223       && (rs == NS_FD || rs == NS_FF))
14224     {
14225       do_vfp_nsyn_cvtz ();
14226       return;
14227     }
14228
14229   /* VFP rather than Neon conversions.  */
14230   if (flavour >= 6)
14231     {
14232       do_vfp_nsyn_cvt (rs, flavour);
14233       return;
14234     }
14235
14236   switch (rs)
14237     {
14238     case NS_DDI:
14239     case NS_QQI:
14240       {
14241         unsigned immbits;
14242         unsigned enctab[] = { 0x0000100, 0x1000100, 0x0, 0x1000000 };
14243
14244         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14245           return;
14246
14247         /* Fixed-point conversion with #0 immediate is encoded as an
14248            integer conversion.  */
14249         if (inst.operands[2].present && inst.operands[2].imm == 0)
14250           goto int_encode;
14251        immbits = 32 - inst.operands[2].imm;
14252         NEON_ENCODE (IMMED, inst);
14253         if (flavour != -1)
14254           inst.instruction |= enctab[flavour];
14255         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14256         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14257         inst.instruction |= LOW4 (inst.operands[1].reg);
14258         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14259         inst.instruction |= neon_quad (rs) << 6;
14260         inst.instruction |= 1 << 21;
14261         inst.instruction |= immbits << 16;
14262
14263         neon_dp_fixup (&inst);
14264       }
14265       break;
14266
14267     case NS_DD:
14268     case NS_QQ:
14269     int_encode:
14270       {
14271         unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080 };
14272
14273         NEON_ENCODE (INTEGER, inst);
14274
14275         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14276           return;
14277
14278         if (flavour != -1)
14279           inst.instruction |= enctab[flavour];
14280
14281         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14282         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14283         inst.instruction |= LOW4 (inst.operands[1].reg);
14284         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14285         inst.instruction |= neon_quad (rs) << 6;
14286         inst.instruction |= 2 << 18;
14287
14288         neon_dp_fixup (&inst);
14289       }
14290     break;
14291
14292     /* Half-precision conversions for Advanced SIMD -- neon.  */
14293     case NS_QD:
14294     case NS_DQ:
14295
14296       if ((rs == NS_DQ)
14297           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
14298           {
14299             as_bad (_("operand size must match register width"));
14300             break;
14301           }
14302
14303       if ((rs == NS_QD)
14304           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
14305           {
14306             as_bad (_("operand size must match register width"));
14307             break;
14308           }
14309
14310       if (rs == NS_DQ)
14311         inst.instruction = 0x3b60600;
14312       else
14313         inst.instruction = 0x3b60700;
14314
14315       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14316       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14317       inst.instruction |= LOW4 (inst.operands[1].reg);
14318       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14319       neon_dp_fixup (&inst);
14320       break;
14321
14322     default:
14323       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
14324       do_vfp_nsyn_cvt (rs, flavour);
14325     }
14326 }
14327
14328 static void
14329 do_neon_cvtr (void)
14330 {
14331   do_neon_cvt_1 (FALSE);
14332 }
14333
14334 static void
14335 do_neon_cvt (void)
14336 {
14337   do_neon_cvt_1 (TRUE);
14338 }
14339
14340 static void
14341 do_neon_cvtb (void)
14342 {
14343   inst.instruction = 0xeb20a40;
14344
14345   /* The sizes are attached to the mnemonic.  */
14346   if (inst.vectype.el[0].type != NT_invtype
14347       && inst.vectype.el[0].size == 16)
14348     inst.instruction |= 0x00010000;
14349
14350   /* Programmer's syntax: the sizes are attached to the operands.  */
14351   else if (inst.operands[0].vectype.type != NT_invtype
14352            && inst.operands[0].vectype.size == 16)
14353     inst.instruction |= 0x00010000;
14354
14355   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
14356   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
14357   do_vfp_cond_or_thumb ();
14358 }
14359
14360
14361 static void
14362 do_neon_cvtt (void)
14363 {
14364   do_neon_cvtb ();
14365   inst.instruction |= 0x80;
14366 }
14367
14368 static void
14369 neon_move_immediate (void)
14370 {
14371   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
14372   struct neon_type_el et = neon_check_type (2, rs,
14373     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14374   unsigned immlo, immhi = 0, immbits;
14375   int op, cmode, float_p;
14376
14377   constraint (et.type == NT_invtype,
14378               _("operand size must be specified for immediate VMOV"));
14379
14380   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
14381   op = (inst.instruction & (1 << 5)) != 0;
14382
14383   immlo = inst.operands[1].imm;
14384   if (inst.operands[1].regisimm)
14385     immhi = inst.operands[1].reg;
14386
14387   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
14388               _("immediate has bits set outside the operand size"));
14389
14390   float_p = inst.operands[1].immisfloat;
14391
14392   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
14393                                         et.size, et.type)) == FAIL)
14394     {
14395       /* Invert relevant bits only.  */
14396       neon_invert_size (&immlo, &immhi, et.size);
14397       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
14398          with one or the other; those cases are caught by
14399          neon_cmode_for_move_imm.  */
14400       op = !op;
14401       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
14402                                             &op, et.size, et.type)) == FAIL)
14403         {
14404           first_error (_("immediate out of range"));
14405           return;
14406         }
14407     }
14408
14409   inst.instruction &= ~(1 << 5);
14410   inst.instruction |= op << 5;
14411
14412   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14413   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14414   inst.instruction |= neon_quad (rs) << 6;
14415   inst.instruction |= cmode << 8;
14416
14417   neon_write_immbits (immbits);
14418 }
14419
14420 static void
14421 do_neon_mvn (void)
14422 {
14423   if (inst.operands[1].isreg)
14424     {
14425       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14426
14427       NEON_ENCODE (INTEGER, inst);
14428       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14429       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14430       inst.instruction |= LOW4 (inst.operands[1].reg);
14431       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14432       inst.instruction |= neon_quad (rs) << 6;
14433     }
14434   else
14435     {
14436       NEON_ENCODE (IMMED, inst);
14437       neon_move_immediate ();
14438     }
14439
14440   neon_dp_fixup (&inst);
14441 }
14442
14443 /* Encode instructions of form:
14444
14445   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
14446   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
14447
14448 static void
14449 neon_mixed_length (struct neon_type_el et, unsigned size)
14450 {
14451   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14452   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14453   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14454   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14455   inst.instruction |= LOW4 (inst.operands[2].reg);
14456   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14457   inst.instruction |= (et.type == NT_unsigned) << 24;
14458   inst.instruction |= neon_logbits (size) << 20;
14459
14460   neon_dp_fixup (&inst);
14461 }
14462
14463 static void
14464 do_neon_dyadic_long (void)
14465 {
14466   /* FIXME: Type checking for lengthening op.  */
14467   struct neon_type_el et = neon_check_type (3, NS_QDD,
14468     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
14469   neon_mixed_length (et, et.size);
14470 }
14471
14472 static void
14473 do_neon_abal (void)
14474 {
14475   struct neon_type_el et = neon_check_type (3, NS_QDD,
14476     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
14477   neon_mixed_length (et, et.size);
14478 }
14479
14480 static void
14481 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
14482 {
14483   if (inst.operands[2].isscalar)
14484     {
14485       struct neon_type_el et = neon_check_type (3, NS_QDS,
14486         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
14487       NEON_ENCODE (SCALAR, inst);
14488       neon_mul_mac (et, et.type == NT_unsigned);
14489     }
14490   else
14491     {
14492       struct neon_type_el et = neon_check_type (3, NS_QDD,
14493         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
14494       NEON_ENCODE (INTEGER, inst);
14495       neon_mixed_length (et, et.size);
14496     }
14497 }
14498
14499 static void
14500 do_neon_mac_maybe_scalar_long (void)
14501 {
14502   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
14503 }
14504
14505 static void
14506 do_neon_dyadic_wide (void)
14507 {
14508   struct neon_type_el et = neon_check_type (3, NS_QQD,
14509     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
14510   neon_mixed_length (et, et.size);
14511 }
14512
14513 static void
14514 do_neon_dyadic_narrow (void)
14515 {
14516   struct neon_type_el et = neon_check_type (3, NS_QDD,
14517     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
14518   /* Operand sign is unimportant, and the U bit is part of the opcode,
14519      so force the operand type to integer.  */
14520   et.type = NT_integer;
14521   neon_mixed_length (et, et.size / 2);
14522 }
14523
14524 static void
14525 do_neon_mul_sat_scalar_long (void)
14526 {
14527   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
14528 }
14529
14530 static void
14531 do_neon_vmull (void)
14532 {
14533   if (inst.operands[2].isscalar)
14534     do_neon_mac_maybe_scalar_long ();
14535   else
14536     {
14537       struct neon_type_el et = neon_check_type (3, NS_QDD,
14538         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_KEY);
14539       if (et.type == NT_poly)
14540         NEON_ENCODE (POLY, inst);
14541       else
14542         NEON_ENCODE (INTEGER, inst);
14543       /* For polynomial encoding, size field must be 0b00 and the U bit must be
14544          zero. Should be OK as-is.  */
14545       neon_mixed_length (et, et.size);
14546     }
14547 }
14548
14549 static void
14550 do_neon_ext (void)
14551 {
14552   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
14553   struct neon_type_el et = neon_check_type (3, rs,
14554     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14555   unsigned imm = (inst.operands[3].imm * et.size) / 8;
14556
14557   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
14558               _("shift out of range"));
14559   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14560   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14561   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14562   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14563   inst.instruction |= LOW4 (inst.operands[2].reg);
14564   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14565   inst.instruction |= neon_quad (rs) << 6;
14566   inst.instruction |= imm << 8;
14567
14568   neon_dp_fixup (&inst);
14569 }
14570
14571 static void
14572 do_neon_rev (void)
14573 {
14574   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14575   struct neon_type_el et = neon_check_type (2, rs,
14576     N_EQK, N_8 | N_16 | N_32 | N_KEY);
14577   unsigned op = (inst.instruction >> 7) & 3;
14578   /* N (width of reversed regions) is encoded as part of the bitmask. We
14579      extract it here to check the elements to be reversed are smaller.
14580      Otherwise we'd get a reserved instruction.  */
14581   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
14582   gas_assert (elsize != 0);
14583   constraint (et.size >= elsize,
14584               _("elements must be smaller than reversal region"));
14585   neon_two_same (neon_quad (rs), 1, et.size);
14586 }
14587
14588 static void
14589 do_neon_dup (void)
14590 {
14591   if (inst.operands[1].isscalar)
14592     {
14593       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
14594       struct neon_type_el et = neon_check_type (2, rs,
14595         N_EQK, N_8 | N_16 | N_32 | N_KEY);
14596       unsigned sizebits = et.size >> 3;
14597       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
14598       int logsize = neon_logbits (et.size);
14599       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
14600
14601       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
14602         return;
14603
14604       NEON_ENCODE (SCALAR, inst);
14605       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14606       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14607       inst.instruction |= LOW4 (dm);
14608       inst.instruction |= HI1 (dm) << 5;
14609       inst.instruction |= neon_quad (rs) << 6;
14610       inst.instruction |= x << 17;
14611       inst.instruction |= sizebits << 16;
14612
14613       neon_dp_fixup (&inst);
14614     }
14615   else
14616     {
14617       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
14618       struct neon_type_el et = neon_check_type (2, rs,
14619         N_8 | N_16 | N_32 | N_KEY, N_EQK);
14620       /* Duplicate ARM register to lanes of vector.  */
14621       NEON_ENCODE (ARMREG, inst);
14622       switch (et.size)
14623         {
14624         case 8:  inst.instruction |= 0x400000; break;
14625         case 16: inst.instruction |= 0x000020; break;
14626         case 32: inst.instruction |= 0x000000; break;
14627         default: break;
14628         }
14629       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
14630       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
14631       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
14632       inst.instruction |= neon_quad (rs) << 21;
14633       /* The encoding for this instruction is identical for the ARM and Thumb
14634          variants, except for the condition field.  */
14635       do_vfp_cond_or_thumb ();
14636     }
14637 }
14638
14639 /* VMOV has particularly many variations. It can be one of:
14640      0. VMOV<c><q> <Qd>, <Qm>
14641      1. VMOV<c><q> <Dd>, <Dm>
14642    (Register operations, which are VORR with Rm = Rn.)
14643      2. VMOV<c><q>.<dt> <Qd>, #<imm>
14644      3. VMOV<c><q>.<dt> <Dd>, #<imm>
14645    (Immediate loads.)
14646      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
14647    (ARM register to scalar.)
14648      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
14649    (Two ARM registers to vector.)
14650      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
14651    (Scalar to ARM register.)
14652      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
14653    (Vector to two ARM registers.)
14654      8. VMOV.F32 <Sd>, <Sm>
14655      9. VMOV.F64 <Dd>, <Dm>
14656    (VFP register moves.)
14657     10. VMOV.F32 <Sd>, #imm
14658     11. VMOV.F64 <Dd>, #imm
14659    (VFP float immediate load.)
14660     12. VMOV <Rd>, <Sm>
14661    (VFP single to ARM reg.)
14662     13. VMOV <Sd>, <Rm>
14663    (ARM reg to VFP single.)
14664     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
14665    (Two ARM regs to two VFP singles.)
14666     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
14667    (Two VFP singles to two ARM regs.)
14668
14669    These cases can be disambiguated using neon_select_shape, except cases 1/9
14670    and 3/11 which depend on the operand type too.
14671
14672    All the encoded bits are hardcoded by this function.
14673
14674    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
14675    Cases 5, 7 may be used with VFPv2 and above.
14676
14677    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
14678    can specify a type where it doesn't make sense to, and is ignored).  */
14679
14680 static void
14681 do_neon_mov (void)
14682 {
14683   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
14684     NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR, NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
14685     NS_NULL);
14686   struct neon_type_el et;
14687   const char *ldconst = 0;
14688
14689   switch (rs)
14690     {
14691     case NS_DD:  /* case 1/9.  */
14692       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
14693       /* It is not an error here if no type is given.  */
14694       inst.error = NULL;
14695       if (et.type == NT_float && et.size == 64)
14696         {
14697           do_vfp_nsyn_opcode ("fcpyd");
14698           break;
14699         }
14700       /* fall through.  */
14701
14702     case NS_QQ:  /* case 0/1.  */
14703       {
14704         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14705           return;
14706         /* The architecture manual I have doesn't explicitly state which
14707            value the U bit should have for register->register moves, but
14708            the equivalent VORR instruction has U = 0, so do that.  */
14709         inst.instruction = 0x0200110;
14710         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14711         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14712         inst.instruction |= LOW4 (inst.operands[1].reg);
14713         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14714         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14715         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14716         inst.instruction |= neon_quad (rs) << 6;
14717
14718         neon_dp_fixup (&inst);
14719       }
14720       break;
14721
14722     case NS_DI:  /* case 3/11.  */
14723       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
14724       inst.error = NULL;
14725       if (et.type == NT_float && et.size == 64)
14726         {
14727           /* case 11 (fconstd).  */
14728           ldconst = "fconstd";
14729           goto encode_fconstd;
14730         }
14731       /* fall through.  */
14732
14733     case NS_QI:  /* case 2/3.  */
14734       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14735         return;
14736       inst.instruction = 0x0800010;
14737       neon_move_immediate ();
14738       neon_dp_fixup (&inst);
14739       break;
14740
14741     case NS_SR:  /* case 4.  */
14742       {
14743         unsigned bcdebits = 0;
14744         int logsize;
14745         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
14746         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
14747
14748         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
14749         logsize = neon_logbits (et.size);
14750
14751         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
14752                     _(BAD_FPU));
14753         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
14754                     && et.size != 32, _(BAD_FPU));
14755         constraint (et.type == NT_invtype, _("bad type for scalar"));
14756         constraint (x >= 64 / et.size, _("scalar index out of range"));
14757
14758         switch (et.size)
14759           {
14760           case 8:  bcdebits = 0x8; break;
14761           case 16: bcdebits = 0x1; break;
14762           case 32: bcdebits = 0x0; break;
14763           default: ;
14764           }
14765
14766         bcdebits |= x << logsize;
14767
14768         inst.instruction = 0xe000b10;
14769         do_vfp_cond_or_thumb ();
14770         inst.instruction |= LOW4 (dn) << 16;
14771         inst.instruction |= HI1 (dn) << 7;
14772         inst.instruction |= inst.operands[1].reg << 12;
14773         inst.instruction |= (bcdebits & 3) << 5;
14774         inst.instruction |= (bcdebits >> 2) << 21;
14775       }
14776       break;
14777
14778     case NS_DRR:  /* case 5 (fmdrr).  */
14779       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
14780                   _(BAD_FPU));
14781
14782       inst.instruction = 0xc400b10;
14783       do_vfp_cond_or_thumb ();
14784       inst.instruction |= LOW4 (inst.operands[0].reg);
14785       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
14786       inst.instruction |= inst.operands[1].reg << 12;
14787       inst.instruction |= inst.operands[2].reg << 16;
14788       break;
14789
14790     case NS_RS:  /* case 6.  */
14791       {
14792         unsigned logsize;
14793         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
14794         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
14795         unsigned abcdebits = 0;
14796
14797         et = neon_check_type (2, NS_NULL,
14798                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
14799         logsize = neon_logbits (et.size);
14800
14801         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
14802                     _(BAD_FPU));
14803         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
14804                     && et.size != 32, _(BAD_FPU));
14805         constraint (et.type == NT_invtype, _("bad type for scalar"));
14806         constraint (x >= 64 / et.size, _("scalar index out of range"));
14807
14808         switch (et.size)
14809           {
14810           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
14811           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
14812           case 32: abcdebits = 0x00; break;
14813           default: ;
14814           }
14815
14816         abcdebits |= x << logsize;
14817         inst.instruction = 0xe100b10;
14818         do_vfp_cond_or_thumb ();
14819         inst.instruction |= LOW4 (dn) << 16;
14820         inst.instruction |= HI1 (dn) << 7;
14821         inst.instruction |= inst.operands[0].reg << 12;
14822         inst.instruction |= (abcdebits & 3) << 5;
14823         inst.instruction |= (abcdebits >> 2) << 21;
14824       }
14825       break;
14826
14827     case NS_RRD:  /* case 7 (fmrrd).  */
14828       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
14829                   _(BAD_FPU));
14830
14831       inst.instruction = 0xc500b10;
14832       do_vfp_cond_or_thumb ();
14833       inst.instruction |= inst.operands[0].reg << 12;
14834       inst.instruction |= inst.operands[1].reg << 16;
14835       inst.instruction |= LOW4 (inst.operands[2].reg);
14836       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14837       break;
14838
14839     case NS_FF:  /* case 8 (fcpys).  */
14840       do_vfp_nsyn_opcode ("fcpys");
14841       break;
14842
14843     case NS_FI:  /* case 10 (fconsts).  */
14844       ldconst = "fconsts";
14845       encode_fconstd:
14846       if (is_quarter_float (inst.operands[1].imm))
14847         {
14848           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
14849           do_vfp_nsyn_opcode (ldconst);
14850         }
14851       else
14852         first_error (_("immediate out of range"));
14853       break;
14854
14855     case NS_RF:  /* case 12 (fmrs).  */
14856       do_vfp_nsyn_opcode ("fmrs");
14857       break;
14858
14859     case NS_FR:  /* case 13 (fmsr).  */
14860       do_vfp_nsyn_opcode ("fmsr");
14861       break;
14862
14863     /* The encoders for the fmrrs and fmsrr instructions expect three operands
14864        (one of which is a list), but we have parsed four.  Do some fiddling to
14865        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
14866        expect.  */
14867     case NS_RRFF:  /* case 14 (fmrrs).  */
14868       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
14869                   _("VFP registers must be adjacent"));
14870       inst.operands[2].imm = 2;
14871       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
14872       do_vfp_nsyn_opcode ("fmrrs");
14873       break;
14874
14875     case NS_FFRR:  /* case 15 (fmsrr).  */
14876       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
14877                   _("VFP registers must be adjacent"));
14878       inst.operands[1] = inst.operands[2];
14879       inst.operands[2] = inst.operands[3];
14880       inst.operands[0].imm = 2;
14881       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
14882       do_vfp_nsyn_opcode ("fmsrr");
14883       break;
14884
14885     default:
14886       abort ();
14887     }
14888 }
14889
14890 static void
14891 do_neon_rshift_round_imm (void)
14892 {
14893   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14894   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
14895   int imm = inst.operands[2].imm;
14896
14897   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
14898   if (imm == 0)
14899     {
14900       inst.operands[2].present = 0;
14901       do_neon_mov ();
14902       return;
14903     }
14904
14905   constraint (imm < 1 || (unsigned)imm > et.size,
14906               _("immediate out of range for shift"));
14907   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
14908                   et.size - imm);
14909 }
14910
14911 static void
14912 do_neon_movl (void)
14913 {
14914   struct neon_type_el et = neon_check_type (2, NS_QD,
14915     N_EQK | N_DBL, N_SU_32 | N_KEY);
14916   unsigned sizebits = et.size >> 3;
14917   inst.instruction |= sizebits << 19;
14918   neon_two_same (0, et.type == NT_unsigned, -1);
14919 }
14920
14921 static void
14922 do_neon_trn (void)
14923 {
14924   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14925   struct neon_type_el et = neon_check_type (2, rs,
14926     N_EQK, N_8 | N_16 | N_32 | N_KEY);
14927   NEON_ENCODE (INTEGER, inst);
14928   neon_two_same (neon_quad (rs), 1, et.size);
14929 }
14930
14931 static void
14932 do_neon_zip_uzp (void)
14933 {
14934   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14935   struct neon_type_el et = neon_check_type (2, rs,
14936     N_EQK, N_8 | N_16 | N_32 | N_KEY);
14937   if (rs == NS_DD && et.size == 32)
14938     {
14939       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
14940       inst.instruction = N_MNEM_vtrn;
14941       do_neon_trn ();
14942       return;
14943     }
14944   neon_two_same (neon_quad (rs), 1, et.size);
14945 }
14946
14947 static void
14948 do_neon_sat_abs_neg (void)
14949 {
14950   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14951   struct neon_type_el et = neon_check_type (2, rs,
14952     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
14953   neon_two_same (neon_quad (rs), 1, et.size);
14954 }
14955
14956 static void
14957 do_neon_pair_long (void)
14958 {
14959   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14960   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
14961   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
14962   inst.instruction |= (et.type == NT_unsigned) << 7;
14963   neon_two_same (neon_quad (rs), 1, et.size);
14964 }
14965
14966 static void
14967 do_neon_recip_est (void)
14968 {
14969   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14970   struct neon_type_el et = neon_check_type (2, rs,
14971     N_EQK | N_FLT, N_F32 | N_U32 | N_KEY);
14972   inst.instruction |= (et.type == NT_float) << 8;
14973   neon_two_same (neon_quad (rs), 1, et.size);
14974 }
14975
14976 static void
14977 do_neon_cls (void)
14978 {
14979   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14980   struct neon_type_el et = neon_check_type (2, rs,
14981     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
14982   neon_two_same (neon_quad (rs), 1, et.size);
14983 }
14984
14985 static void
14986 do_neon_clz (void)
14987 {
14988   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14989   struct neon_type_el et = neon_check_type (2, rs,
14990     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
14991   neon_two_same (neon_quad (rs), 1, et.size);
14992 }
14993
14994 static void
14995 do_neon_cnt (void)
14996 {
14997   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14998   struct neon_type_el et = neon_check_type (2, rs,
14999     N_EQK | N_INT, N_8 | N_KEY);
15000   neon_two_same (neon_quad (rs), 1, et.size);
15001 }
15002
15003 static void
15004 do_neon_swp (void)
15005 {
15006   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15007   neon_two_same (neon_quad (rs), 1, -1);
15008 }
15009
15010 static void
15011 do_neon_tbl_tbx (void)
15012 {
15013   unsigned listlenbits;
15014   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
15015
15016   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
15017     {
15018       first_error (_("bad list length for table lookup"));
15019       return;
15020     }
15021
15022   listlenbits = inst.operands[1].imm - 1;
15023   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15024   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15025   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15026   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15027   inst.instruction |= LOW4 (inst.operands[2].reg);
15028   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15029   inst.instruction |= listlenbits << 8;
15030
15031   neon_dp_fixup (&inst);
15032 }
15033
15034 static void
15035 do_neon_ldm_stm (void)
15036 {
15037   /* P, U and L bits are part of bitmask.  */
15038   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
15039   unsigned offsetbits = inst.operands[1].imm * 2;
15040
15041   if (inst.operands[1].issingle)
15042     {
15043       do_vfp_nsyn_ldm_stm (is_dbmode);
15044       return;
15045     }
15046
15047   constraint (is_dbmode && !inst.operands[0].writeback,
15048               _("writeback (!) must be used for VLDMDB and VSTMDB"));
15049
15050   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
15051               _("register list must contain at least 1 and at most 16 "
15052                 "registers"));
15053
15054   inst.instruction |= inst.operands[0].reg << 16;
15055   inst.instruction |= inst.operands[0].writeback << 21;
15056   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
15057   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
15058
15059   inst.instruction |= offsetbits;
15060
15061   do_vfp_cond_or_thumb ();
15062 }
15063
15064 static void
15065 do_neon_ldr_str (void)
15066 {
15067   int is_ldr = (inst.instruction & (1 << 20)) != 0;
15068
15069   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
15070      And is UNPREDICTABLE in thumb mode.  */
15071   if (!is_ldr 
15072       && inst.operands[1].reg == REG_PC
15073       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
15074     {
15075       if (!thumb_mode && warn_on_deprecated)
15076         as_warn (_("Use of PC here is deprecated"));
15077       else
15078         inst.error = _("Use of PC here is UNPREDICTABLE");
15079     }
15080
15081   if (inst.operands[0].issingle)
15082     {
15083       if (is_ldr)
15084         do_vfp_nsyn_opcode ("flds");
15085       else
15086         do_vfp_nsyn_opcode ("fsts");
15087     }
15088   else
15089     {
15090       if (is_ldr)
15091         do_vfp_nsyn_opcode ("fldd");
15092       else
15093         do_vfp_nsyn_opcode ("fstd");
15094     }
15095 }
15096
15097 /* "interleave" version also handles non-interleaving register VLD1/VST1
15098    instructions.  */
15099
15100 static void
15101 do_neon_ld_st_interleave (void)
15102 {
15103   struct neon_type_el et = neon_check_type (1, NS_NULL,
15104                                             N_8 | N_16 | N_32 | N_64);
15105   unsigned alignbits = 0;
15106   unsigned idx;
15107   /* The bits in this table go:
15108      0: register stride of one (0) or two (1)
15109      1,2: register list length, minus one (1, 2, 3, 4).
15110      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
15111      We use -1 for invalid entries.  */
15112   const int typetable[] =
15113     {
15114       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
15115        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
15116        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
15117        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
15118     };
15119   int typebits;
15120
15121   if (et.type == NT_invtype)
15122     return;
15123
15124   if (inst.operands[1].immisalign)
15125     switch (inst.operands[1].imm >> 8)
15126       {
15127       case 64: alignbits = 1; break;
15128       case 128:
15129         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
15130             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
15131           goto bad_alignment;
15132         alignbits = 2;
15133         break;
15134       case 256:
15135         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
15136           goto bad_alignment;
15137         alignbits = 3;
15138         break;
15139       default:
15140       bad_alignment:
15141         first_error (_("bad alignment"));
15142         return;
15143       }
15144
15145   inst.instruction |= alignbits << 4;
15146   inst.instruction |= neon_logbits (et.size) << 6;
15147
15148   /* Bits [4:6] of the immediate in a list specifier encode register stride
15149      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
15150      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
15151      up the right value for "type" in a table based on this value and the given
15152      list style, then stick it back.  */
15153   idx = ((inst.operands[0].imm >> 4) & 7)
15154         | (((inst.instruction >> 8) & 3) << 3);
15155
15156   typebits = typetable[idx];
15157
15158   constraint (typebits == -1, _("bad list type for instruction"));
15159
15160   inst.instruction &= ~0xf00;
15161   inst.instruction |= typebits << 8;
15162 }
15163
15164 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
15165    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
15166    otherwise. The variable arguments are a list of pairs of legal (size, align)
15167    values, terminated with -1.  */
15168
15169 static int
15170 neon_alignment_bit (int size, int align, int *do_align, ...)
15171 {
15172   va_list ap;
15173   int result = FAIL, thissize, thisalign;
15174
15175   if (!inst.operands[1].immisalign)
15176     {
15177       *do_align = 0;
15178       return SUCCESS;
15179     }
15180
15181   va_start (ap, do_align);
15182
15183   do
15184     {
15185       thissize = va_arg (ap, int);
15186       if (thissize == -1)
15187         break;
15188       thisalign = va_arg (ap, int);
15189
15190       if (size == thissize && align == thisalign)
15191         result = SUCCESS;
15192     }
15193   while (result != SUCCESS);
15194
15195   va_end (ap);
15196
15197   if (result == SUCCESS)
15198     *do_align = 1;
15199   else
15200     first_error (_("unsupported alignment for instruction"));
15201
15202   return result;
15203 }
15204
15205 static void
15206 do_neon_ld_st_lane (void)
15207 {
15208   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
15209   int align_good, do_align = 0;
15210   int logsize = neon_logbits (et.size);
15211   int align = inst.operands[1].imm >> 8;
15212   int n = (inst.instruction >> 8) & 3;
15213   int max_el = 64 / et.size;
15214
15215   if (et.type == NT_invtype)
15216     return;
15217
15218   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
15219               _("bad list length"));
15220   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
15221               _("scalar index out of range"));
15222   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
15223               && et.size == 8,
15224               _("stride of 2 unavailable when element size is 8"));
15225
15226   switch (n)
15227     {
15228     case 0:  /* VLD1 / VST1.  */
15229       align_good = neon_alignment_bit (et.size, align, &do_align, 16, 16,
15230                                        32, 32, -1);
15231       if (align_good == FAIL)
15232         return;
15233       if (do_align)
15234         {
15235           unsigned alignbits = 0;
15236           switch (et.size)
15237             {
15238             case 16: alignbits = 0x1; break;
15239             case 32: alignbits = 0x3; break;
15240             default: ;
15241             }
15242           inst.instruction |= alignbits << 4;
15243         }
15244       break;
15245
15246     case 1:  /* VLD2 / VST2.  */
15247       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 16, 16, 32,
15248                                        32, 64, -1);
15249       if (align_good == FAIL)
15250         return;
15251       if (do_align)
15252         inst.instruction |= 1 << 4;
15253       break;
15254
15255     case 2:  /* VLD3 / VST3.  */
15256       constraint (inst.operands[1].immisalign,
15257                   _("can't use alignment with this instruction"));
15258       break;
15259
15260     case 3:  /* VLD4 / VST4.  */
15261       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
15262                                        16, 64, 32, 64, 32, 128, -1);
15263       if (align_good == FAIL)
15264         return;
15265       if (do_align)
15266         {
15267           unsigned alignbits = 0;
15268           switch (et.size)
15269             {
15270             case 8:  alignbits = 0x1; break;
15271             case 16: alignbits = 0x1; break;
15272             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
15273             default: ;
15274             }
15275           inst.instruction |= alignbits << 4;
15276         }
15277       break;
15278
15279     default: ;
15280     }
15281
15282   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
15283   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15284     inst.instruction |= 1 << (4 + logsize);
15285
15286   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
15287   inst.instruction |= logsize << 10;
15288 }
15289
15290 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
15291
15292 static void
15293 do_neon_ld_dup (void)
15294 {
15295   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
15296   int align_good, do_align = 0;
15297
15298   if (et.type == NT_invtype)
15299     return;
15300
15301   switch ((inst.instruction >> 8) & 3)
15302     {
15303     case 0:  /* VLD1.  */
15304       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
15305       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
15306                                        &do_align, 16, 16, 32, 32, -1);
15307       if (align_good == FAIL)
15308         return;
15309       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
15310         {
15311         case 1: break;
15312         case 2: inst.instruction |= 1 << 5; break;
15313         default: first_error (_("bad list length")); return;
15314         }
15315       inst.instruction |= neon_logbits (et.size) << 6;
15316       break;
15317
15318     case 1:  /* VLD2.  */
15319       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
15320                                        &do_align, 8, 16, 16, 32, 32, 64, -1);
15321       if (align_good == FAIL)
15322         return;
15323       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
15324                   _("bad list length"));
15325       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15326         inst.instruction |= 1 << 5;
15327       inst.instruction |= neon_logbits (et.size) << 6;
15328       break;
15329
15330     case 2:  /* VLD3.  */
15331       constraint (inst.operands[1].immisalign,
15332                   _("can't use alignment with this instruction"));
15333       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
15334                   _("bad list length"));
15335       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15336         inst.instruction |= 1 << 5;
15337       inst.instruction |= neon_logbits (et.size) << 6;
15338       break;
15339
15340     case 3:  /* VLD4.  */
15341       {
15342         int align = inst.operands[1].imm >> 8;
15343         align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
15344                                          16, 64, 32, 64, 32, 128, -1);
15345         if (align_good == FAIL)
15346           return;
15347         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
15348                     _("bad list length"));
15349         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15350           inst.instruction |= 1 << 5;
15351         if (et.size == 32 && align == 128)
15352           inst.instruction |= 0x3 << 6;
15353         else
15354           inst.instruction |= neon_logbits (et.size) << 6;
15355       }
15356       break;
15357
15358     default: ;
15359     }
15360
15361   inst.instruction |= do_align << 4;
15362 }
15363
15364 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
15365    apart from bits [11:4].  */
15366
15367 static void
15368 do_neon_ldx_stx (void)
15369 {
15370   if (inst.operands[1].isreg)
15371     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
15372
15373   switch (NEON_LANE (inst.operands[0].imm))
15374     {
15375     case NEON_INTERLEAVE_LANES:
15376       NEON_ENCODE (INTERLV, inst);
15377       do_neon_ld_st_interleave ();
15378       break;
15379
15380     case NEON_ALL_LANES:
15381       NEON_ENCODE (DUP, inst);
15382       do_neon_ld_dup ();
15383       break;
15384
15385     default:
15386       NEON_ENCODE (LANE, inst);
15387       do_neon_ld_st_lane ();
15388     }
15389
15390   /* L bit comes from bit mask.  */
15391   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15392   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15393   inst.instruction |= inst.operands[1].reg << 16;
15394
15395   if (inst.operands[1].postind)
15396     {
15397       int postreg = inst.operands[1].imm & 0xf;
15398       constraint (!inst.operands[1].immisreg,
15399                   _("post-index must be a register"));
15400       constraint (postreg == 0xd || postreg == 0xf,
15401                   _("bad register for post-index"));
15402       inst.instruction |= postreg;
15403     }
15404   else if (inst.operands[1].writeback)
15405     {
15406       inst.instruction |= 0xd;
15407     }
15408   else
15409     inst.instruction |= 0xf;
15410
15411   if (thumb_mode)
15412     inst.instruction |= 0xf9000000;
15413   else
15414     inst.instruction |= 0xf4000000;
15415 }
15416 \f
15417 /* Overall per-instruction processing.  */
15418
15419 /* We need to be able to fix up arbitrary expressions in some statements.
15420    This is so that we can handle symbols that are an arbitrary distance from
15421    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
15422    which returns part of an address in a form which will be valid for
15423    a data instruction.  We do this by pushing the expression into a symbol
15424    in the expr_section, and creating a fix for that.  */
15425
15426 static void
15427 fix_new_arm (fragS *       frag,
15428              int           where,
15429              short int     size,
15430              expressionS * exp,
15431              int           pc_rel,
15432              int           reloc)
15433 {
15434   fixS *           new_fix;
15435
15436   switch (exp->X_op)
15437     {
15438     case O_constant:
15439       if (pc_rel)
15440         {
15441           /* Create an absolute valued symbol, so we have something to
15442              refer to in the object file.  Unfortunately for us, gas's
15443              generic expression parsing will already have folded out
15444              any use of .set foo/.type foo %function that may have
15445              been used to set type information of the target location,
15446              that's being specified symbolically.  We have to presume
15447              the user knows what they are doing.  */
15448           char name[16 + 8];
15449           symbolS *symbol;
15450
15451           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
15452
15453           symbol = symbol_find_or_make (name);
15454           S_SET_SEGMENT (symbol, absolute_section);
15455           symbol_set_frag (symbol, &zero_address_frag);
15456           S_SET_VALUE (symbol, exp->X_add_number);
15457           exp->X_op = O_symbol;
15458           exp->X_add_symbol = symbol;
15459           exp->X_add_number = 0;
15460         }
15461       /* FALLTHROUGH */
15462     case O_symbol:
15463     case O_add:
15464     case O_subtract:
15465       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
15466                              (enum bfd_reloc_code_real) reloc);
15467       break;
15468
15469     default:
15470       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
15471                                   pc_rel, (enum bfd_reloc_code_real) reloc);
15472       break;
15473     }
15474
15475   /* Mark whether the fix is to a THUMB instruction, or an ARM
15476      instruction.  */
15477   new_fix->tc_fix_data = thumb_mode;
15478 }
15479
15480 /* Create a frg for an instruction requiring relaxation.  */
15481 static void
15482 output_relax_insn (void)
15483 {
15484   char * to;
15485   symbolS *sym;
15486   int offset;
15487
15488   /* The size of the instruction is unknown, so tie the debug info to the
15489      start of the instruction.  */
15490   dwarf2_emit_insn (0);
15491
15492   switch (inst.reloc.exp.X_op)
15493     {
15494     case O_symbol:
15495       sym = inst.reloc.exp.X_add_symbol;
15496       offset = inst.reloc.exp.X_add_number;
15497       break;
15498     case O_constant:
15499       sym = NULL;
15500       offset = inst.reloc.exp.X_add_number;
15501       break;
15502     default:
15503       sym = make_expr_symbol (&inst.reloc.exp);
15504       offset = 0;
15505       break;
15506   }
15507   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
15508                  inst.relax, sym, offset, NULL/*offset, opcode*/);
15509   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
15510 }
15511
15512 /* Write a 32-bit thumb instruction to buf.  */
15513 static void
15514 put_thumb32_insn (char * buf, unsigned long insn)
15515 {
15516   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
15517   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
15518 }
15519
15520 static void
15521 output_inst (const char * str)
15522 {
15523   char * to = NULL;
15524
15525   if (inst.error)
15526     {
15527       as_bad ("%s -- `%s'", inst.error, str);
15528       return;
15529     }
15530   if (inst.relax)
15531     {
15532       output_relax_insn ();
15533       return;
15534     }
15535   if (inst.size == 0)
15536     return;
15537
15538   to = frag_more (inst.size);
15539   /* PR 9814: Record the thumb mode into the current frag so that we know
15540      what type of NOP padding to use, if necessary.  We override any previous
15541      setting so that if the mode has changed then the NOPS that we use will
15542      match the encoding of the last instruction in the frag.  */
15543   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
15544
15545   if (thumb_mode && (inst.size > THUMB_SIZE))
15546     {
15547       gas_assert (inst.size == (2 * THUMB_SIZE));
15548       put_thumb32_insn (to, inst.instruction);
15549     }
15550   else if (inst.size > INSN_SIZE)
15551     {
15552       gas_assert (inst.size == (2 * INSN_SIZE));
15553       md_number_to_chars (to, inst.instruction, INSN_SIZE);
15554       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
15555     }
15556   else
15557     md_number_to_chars (to, inst.instruction, inst.size);
15558
15559   if (inst.reloc.type != BFD_RELOC_UNUSED)
15560     fix_new_arm (frag_now, to - frag_now->fr_literal,
15561                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
15562                  inst.reloc.type);
15563
15564   dwarf2_emit_insn (inst.size);
15565 }
15566
15567 static char *
15568 output_it_inst (int cond, int mask, char * to)
15569 {
15570   unsigned long instruction = 0xbf00;
15571
15572   mask &= 0xf;
15573   instruction |= mask;
15574   instruction |= cond << 4;
15575
15576   if (to == NULL)
15577     {
15578       to = frag_more (2);
15579 #ifdef OBJ_ELF
15580       dwarf2_emit_insn (2);
15581 #endif
15582     }
15583
15584   md_number_to_chars (to, instruction, 2);
15585
15586   return to;
15587 }
15588
15589 /* Tag values used in struct asm_opcode's tag field.  */
15590 enum opcode_tag
15591 {
15592   OT_unconditional,     /* Instruction cannot be conditionalized.
15593                            The ARM condition field is still 0xE.  */
15594   OT_unconditionalF,    /* Instruction cannot be conditionalized
15595                            and carries 0xF in its ARM condition field.  */
15596   OT_csuffix,           /* Instruction takes a conditional suffix.  */
15597   OT_csuffixF,          /* Some forms of the instruction take a conditional
15598                            suffix, others place 0xF where the condition field
15599                            would be.  */
15600   OT_cinfix3,           /* Instruction takes a conditional infix,
15601                            beginning at character index 3.  (In
15602                            unified mode, it becomes a suffix.)  */
15603   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
15604                             tsts, cmps, cmns, and teqs. */
15605   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
15606                            character index 3, even in unified mode.  Used for
15607                            legacy instructions where suffix and infix forms
15608                            may be ambiguous.  */
15609   OT_csuf_or_in3,       /* Instruction takes either a conditional
15610                            suffix or an infix at character index 3.  */
15611   OT_odd_infix_unc,     /* This is the unconditional variant of an
15612                            instruction that takes a conditional infix
15613                            at an unusual position.  In unified mode,
15614                            this variant will accept a suffix.  */
15615   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
15616                            are the conditional variants of instructions that
15617                            take conditional infixes in unusual positions.
15618                            The infix appears at character index
15619                            (tag - OT_odd_infix_0).  These are not accepted
15620                            in unified mode.  */
15621 };
15622
15623 /* Subroutine of md_assemble, responsible for looking up the primary
15624    opcode from the mnemonic the user wrote.  STR points to the
15625    beginning of the mnemonic.
15626
15627    This is not simply a hash table lookup, because of conditional
15628    variants.  Most instructions have conditional variants, which are
15629    expressed with a _conditional affix_ to the mnemonic.  If we were
15630    to encode each conditional variant as a literal string in the opcode
15631    table, it would have approximately 20,000 entries.
15632
15633    Most mnemonics take this affix as a suffix, and in unified syntax,
15634    'most' is upgraded to 'all'.  However, in the divided syntax, some
15635    instructions take the affix as an infix, notably the s-variants of
15636    the arithmetic instructions.  Of those instructions, all but six
15637    have the infix appear after the third character of the mnemonic.
15638
15639    Accordingly, the algorithm for looking up primary opcodes given
15640    an identifier is:
15641
15642    1. Look up the identifier in the opcode table.
15643       If we find a match, go to step U.
15644
15645    2. Look up the last two characters of the identifier in the
15646       conditions table.  If we find a match, look up the first N-2
15647       characters of the identifier in the opcode table.  If we
15648       find a match, go to step CE.
15649
15650    3. Look up the fourth and fifth characters of the identifier in
15651       the conditions table.  If we find a match, extract those
15652       characters from the identifier, and look up the remaining
15653       characters in the opcode table.  If we find a match, go
15654       to step CM.
15655
15656    4. Fail.
15657
15658    U. Examine the tag field of the opcode structure, in case this is
15659       one of the six instructions with its conditional infix in an
15660       unusual place.  If it is, the tag tells us where to find the
15661       infix; look it up in the conditions table and set inst.cond
15662       accordingly.  Otherwise, this is an unconditional instruction.
15663       Again set inst.cond accordingly.  Return the opcode structure.
15664
15665   CE. Examine the tag field to make sure this is an instruction that
15666       should receive a conditional suffix.  If it is not, fail.
15667       Otherwise, set inst.cond from the suffix we already looked up,
15668       and return the opcode structure.
15669
15670   CM. Examine the tag field to make sure this is an instruction that
15671       should receive a conditional infix after the third character.
15672       If it is not, fail.  Otherwise, undo the edits to the current
15673       line of input and proceed as for case CE.  */
15674
15675 static const struct asm_opcode *
15676 opcode_lookup (char **str)
15677 {
15678   char *end, *base;
15679   char *affix;
15680   const struct asm_opcode *opcode;
15681   const struct asm_cond *cond;
15682   char save[2];
15683
15684   /* Scan up to the end of the mnemonic, which must end in white space,
15685      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
15686   for (base = end = *str; *end != '\0'; end++)
15687     if (*end == ' ' || *end == '.')
15688       break;
15689
15690   if (end == base)
15691     return NULL;
15692
15693   /* Handle a possible width suffix and/or Neon type suffix.  */
15694   if (end[0] == '.')
15695     {
15696       int offset = 2;
15697
15698       /* The .w and .n suffixes are only valid if the unified syntax is in
15699          use.  */
15700       if (unified_syntax && end[1] == 'w')
15701         inst.size_req = 4;
15702       else if (unified_syntax && end[1] == 'n')
15703         inst.size_req = 2;
15704       else
15705         offset = 0;
15706
15707       inst.vectype.elems = 0;
15708
15709       *str = end + offset;
15710
15711       if (end[offset] == '.')
15712         {
15713           /* See if we have a Neon type suffix (possible in either unified or
15714              non-unified ARM syntax mode).  */
15715           if (parse_neon_type (&inst.vectype, str) == FAIL)
15716             return NULL;
15717         }
15718       else if (end[offset] != '\0' && end[offset] != ' ')
15719         return NULL;
15720     }
15721   else
15722     *str = end;
15723
15724   /* Look for unaffixed or special-case affixed mnemonic.  */
15725   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
15726                                                     end - base);
15727   if (opcode)
15728     {
15729       /* step U */
15730       if (opcode->tag < OT_odd_infix_0)
15731         {
15732           inst.cond = COND_ALWAYS;
15733           return opcode;
15734         }
15735
15736       if (warn_on_deprecated && unified_syntax)
15737         as_warn (_("conditional infixes are deprecated in unified syntax"));
15738       affix = base + (opcode->tag - OT_odd_infix_0);
15739       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
15740       gas_assert (cond);
15741
15742       inst.cond = cond->value;
15743       return opcode;
15744     }
15745
15746   /* Cannot have a conditional suffix on a mnemonic of less than two
15747      characters.  */
15748   if (end - base < 3)
15749     return NULL;
15750
15751   /* Look for suffixed mnemonic.  */
15752   affix = end - 2;
15753   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
15754   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
15755                                                     affix - base);
15756   if (opcode && cond)
15757     {
15758       /* step CE */
15759       switch (opcode->tag)
15760         {
15761         case OT_cinfix3_legacy:
15762           /* Ignore conditional suffixes matched on infix only mnemonics.  */
15763           break;
15764
15765         case OT_cinfix3:
15766         case OT_cinfix3_deprecated:
15767         case OT_odd_infix_unc:
15768           if (!unified_syntax)
15769             return 0;
15770           /* else fall through */
15771
15772         case OT_csuffix:
15773         case OT_csuffixF:
15774         case OT_csuf_or_in3:
15775           inst.cond = cond->value;
15776           return opcode;
15777
15778         case OT_unconditional:
15779         case OT_unconditionalF:
15780           if (thumb_mode)
15781             inst.cond = cond->value;
15782           else
15783             {
15784               /* Delayed diagnostic.  */
15785               inst.error = BAD_COND;
15786               inst.cond = COND_ALWAYS;
15787             }
15788           return opcode;
15789
15790         default:
15791           return NULL;
15792         }
15793     }
15794
15795   /* Cannot have a usual-position infix on a mnemonic of less than
15796      six characters (five would be a suffix).  */
15797   if (end - base < 6)
15798     return NULL;
15799
15800   /* Look for infixed mnemonic in the usual position.  */
15801   affix = base + 3;
15802   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
15803   if (!cond)
15804     return NULL;
15805
15806   memcpy (save, affix, 2);
15807   memmove (affix, affix + 2, (end - affix) - 2);
15808   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
15809                                                     (end - base) - 2);
15810   memmove (affix + 2, affix, (end - affix) - 2);
15811   memcpy (affix, save, 2);
15812
15813   if (opcode
15814       && (opcode->tag == OT_cinfix3
15815           || opcode->tag == OT_cinfix3_deprecated
15816           || opcode->tag == OT_csuf_or_in3
15817           || opcode->tag == OT_cinfix3_legacy))
15818     {
15819       /* Step CM.  */
15820       if (warn_on_deprecated && unified_syntax
15821           && (opcode->tag == OT_cinfix3
15822               || opcode->tag == OT_cinfix3_deprecated))
15823         as_warn (_("conditional infixes are deprecated in unified syntax"));
15824
15825       inst.cond = cond->value;
15826       return opcode;
15827     }
15828
15829   return NULL;
15830 }
15831
15832 /* This function generates an initial IT instruction, leaving its block
15833    virtually open for the new instructions. Eventually,
15834    the mask will be updated by now_it_add_mask () each time
15835    a new instruction needs to be included in the IT block.
15836    Finally, the block is closed with close_automatic_it_block ().
15837    The block closure can be requested either from md_assemble (),
15838    a tencode (), or due to a label hook.  */
15839
15840 static void
15841 new_automatic_it_block (int cond)
15842 {
15843   now_it.state = AUTOMATIC_IT_BLOCK;
15844   now_it.mask = 0x18;
15845   now_it.cc = cond;
15846   now_it.block_length = 1;
15847   mapping_state (MAP_THUMB);
15848   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
15849 }
15850
15851 /* Close an automatic IT block.
15852    See comments in new_automatic_it_block ().  */
15853
15854 static void
15855 close_automatic_it_block (void)
15856 {
15857   now_it.mask = 0x10;
15858   now_it.block_length = 0;
15859 }
15860
15861 /* Update the mask of the current automatically-generated IT
15862    instruction. See comments in new_automatic_it_block ().  */
15863
15864 static void
15865 now_it_add_mask (int cond)
15866 {
15867 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
15868 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
15869                                               | ((bitvalue) << (nbit)))
15870   const int resulting_bit = (cond & 1);
15871
15872   now_it.mask &= 0xf;
15873   now_it.mask = SET_BIT_VALUE (now_it.mask,
15874                                    resulting_bit,
15875                                   (5 - now_it.block_length));
15876   now_it.mask = SET_BIT_VALUE (now_it.mask,
15877                                    1,
15878                                    ((5 - now_it.block_length) - 1) );
15879   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
15880
15881 #undef CLEAR_BIT
15882 #undef SET_BIT_VALUE
15883 }
15884
15885 /* The IT blocks handling machinery is accessed through the these functions:
15886      it_fsm_pre_encode ()               from md_assemble ()
15887      set_it_insn_type ()                optional, from the tencode functions
15888      set_it_insn_type_last ()           ditto
15889      in_it_block ()                     ditto
15890      it_fsm_post_encode ()              from md_assemble ()
15891      force_automatic_it_block_close ()  from label habdling functions
15892
15893    Rationale:
15894      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
15895         initializing the IT insn type with a generic initial value depending
15896         on the inst.condition.
15897      2) During the tencode function, two things may happen:
15898         a) The tencode function overrides the IT insn type by
15899            calling either set_it_insn_type (type) or set_it_insn_type_last ().
15900         b) The tencode function queries the IT block state by
15901            calling in_it_block () (i.e. to determine narrow/not narrow mode).
15902
15903         Both set_it_insn_type and in_it_block run the internal FSM state
15904         handling function (handle_it_state), because: a) setting the IT insn
15905         type may incur in an invalid state (exiting the function),
15906         and b) querying the state requires the FSM to be updated.
15907         Specifically we want to avoid creating an IT block for conditional
15908         branches, so it_fsm_pre_encode is actually a guess and we can't
15909         determine whether an IT block is required until the tencode () routine
15910         has decided what type of instruction this actually it.
15911         Because of this, if set_it_insn_type and in_it_block have to be used,
15912         set_it_insn_type has to be called first.
15913
15914         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
15915         determines the insn IT type depending on the inst.cond code.
15916         When a tencode () routine encodes an instruction that can be
15917         either outside an IT block, or, in the case of being inside, has to be
15918         the last one, set_it_insn_type_last () will determine the proper
15919         IT instruction type based on the inst.cond code. Otherwise,
15920         set_it_insn_type can be called for overriding that logic or
15921         for covering other cases.
15922
15923         Calling handle_it_state () may not transition the IT block state to
15924         OUTSIDE_IT_BLOCK immediatelly, since the (current) state could be
15925         still queried. Instead, if the FSM determines that the state should
15926         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
15927         after the tencode () function: that's what it_fsm_post_encode () does.
15928
15929         Since in_it_block () calls the state handling function to get an
15930         updated state, an error may occur (due to invalid insns combination).
15931         In that case, inst.error is set.
15932         Therefore, inst.error has to be checked after the execution of
15933         the tencode () routine.
15934
15935      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
15936         any pending state change (if any) that didn't take place in
15937         handle_it_state () as explained above.  */
15938
15939 static void
15940 it_fsm_pre_encode (void)
15941 {
15942   if (inst.cond != COND_ALWAYS)
15943     inst.it_insn_type = INSIDE_IT_INSN;
15944   else
15945     inst.it_insn_type = OUTSIDE_IT_INSN;
15946
15947   now_it.state_handled = 0;
15948 }
15949
15950 /* IT state FSM handling function.  */
15951
15952 static int
15953 handle_it_state (void)
15954 {
15955   now_it.state_handled = 1;
15956
15957   switch (now_it.state)
15958     {
15959     case OUTSIDE_IT_BLOCK:
15960       switch (inst.it_insn_type)
15961         {
15962         case OUTSIDE_IT_INSN:
15963           break;
15964
15965         case INSIDE_IT_INSN:
15966         case INSIDE_IT_LAST_INSN:
15967           if (thumb_mode == 0)
15968             {
15969               if (unified_syntax
15970                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
15971                 as_tsktsk (_("Warning: conditional outside an IT block"\
15972                              " for Thumb."));
15973             }
15974           else
15975             {
15976               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
15977                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2))
15978                 {
15979                   /* Automatically generate the IT instruction.  */
15980                   new_automatic_it_block (inst.cond);
15981                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
15982                     close_automatic_it_block ();
15983                 }
15984               else
15985                 {
15986                   inst.error = BAD_OUT_IT;
15987                   return FAIL;
15988                 }
15989             }
15990           break;
15991
15992         case IF_INSIDE_IT_LAST_INSN:
15993         case NEUTRAL_IT_INSN:
15994           break;
15995
15996         case IT_INSN:
15997           now_it.state = MANUAL_IT_BLOCK;
15998           now_it.block_length = 0;
15999           break;
16000         }
16001       break;
16002
16003     case AUTOMATIC_IT_BLOCK:
16004       /* Three things may happen now:
16005          a) We should increment current it block size;
16006          b) We should close current it block (closing insn or 4 insns);
16007          c) We should close current it block and start a new one (due
16008          to incompatible conditions or
16009          4 insns-length block reached).  */
16010
16011       switch (inst.it_insn_type)
16012         {
16013         case OUTSIDE_IT_INSN:
16014           /* The closure of the block shall happen immediatelly,
16015              so any in_it_block () call reports the block as closed.  */
16016           force_automatic_it_block_close ();
16017           break;
16018
16019         case INSIDE_IT_INSN:
16020         case INSIDE_IT_LAST_INSN:
16021         case IF_INSIDE_IT_LAST_INSN:
16022           now_it.block_length++;
16023
16024           if (now_it.block_length > 4
16025               || !now_it_compatible (inst.cond))
16026             {
16027               force_automatic_it_block_close ();
16028               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
16029                 new_automatic_it_block (inst.cond);
16030             }
16031           else
16032             {
16033               now_it_add_mask (inst.cond);
16034             }
16035
16036           if (now_it.state == AUTOMATIC_IT_BLOCK
16037               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
16038                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
16039             close_automatic_it_block ();
16040           break;
16041
16042         case NEUTRAL_IT_INSN:
16043           now_it.block_length++;
16044
16045           if (now_it.block_length > 4)
16046             force_automatic_it_block_close ();
16047           else
16048             now_it_add_mask (now_it.cc & 1);
16049           break;
16050
16051         case IT_INSN:
16052           close_automatic_it_block ();
16053           now_it.state = MANUAL_IT_BLOCK;
16054           break;
16055         }
16056       break;
16057
16058     case MANUAL_IT_BLOCK:
16059       {
16060         /* Check conditional suffixes.  */
16061         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
16062         int is_last;
16063         now_it.mask <<= 1;
16064         now_it.mask &= 0x1f;
16065         is_last = (now_it.mask == 0x10);
16066
16067         switch (inst.it_insn_type)
16068           {
16069           case OUTSIDE_IT_INSN:
16070             inst.error = BAD_NOT_IT;
16071             return FAIL;
16072
16073           case INSIDE_IT_INSN:
16074             if (cond != inst.cond)
16075               {
16076                 inst.error = BAD_IT_COND;
16077                 return FAIL;
16078               }
16079             break;
16080
16081           case INSIDE_IT_LAST_INSN:
16082           case IF_INSIDE_IT_LAST_INSN:
16083             if (cond != inst.cond)
16084               {
16085                 inst.error = BAD_IT_COND;
16086                 return FAIL;
16087               }
16088             if (!is_last)
16089               {
16090                 inst.error = BAD_BRANCH;
16091                 return FAIL;
16092               }
16093             break;
16094
16095           case NEUTRAL_IT_INSN:
16096             /* The BKPT instruction is unconditional even in an IT block.  */
16097             break;
16098
16099           case IT_INSN:
16100             inst.error = BAD_IT_IT;
16101             return FAIL;
16102           }
16103       }
16104       break;
16105     }
16106
16107   return SUCCESS;
16108 }
16109
16110 static void
16111 it_fsm_post_encode (void)
16112 {
16113   int is_last;
16114
16115   if (!now_it.state_handled)
16116     handle_it_state ();
16117
16118   is_last = (now_it.mask == 0x10);
16119   if (is_last)
16120     {
16121       now_it.state = OUTSIDE_IT_BLOCK;
16122       now_it.mask = 0;
16123     }
16124 }
16125
16126 static void
16127 force_automatic_it_block_close (void)
16128 {
16129   if (now_it.state == AUTOMATIC_IT_BLOCK)
16130     {
16131       close_automatic_it_block ();
16132       now_it.state = OUTSIDE_IT_BLOCK;
16133       now_it.mask = 0;
16134     }
16135 }
16136
16137 static int
16138 in_it_block (void)
16139 {
16140   if (!now_it.state_handled)
16141     handle_it_state ();
16142
16143   return now_it.state != OUTSIDE_IT_BLOCK;
16144 }
16145
16146 void
16147 md_assemble (char *str)
16148 {
16149   char *p = str;
16150   const struct asm_opcode * opcode;
16151
16152   /* Align the previous label if needed.  */
16153   if (last_label_seen != NULL)
16154     {
16155       symbol_set_frag (last_label_seen, frag_now);
16156       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
16157       S_SET_SEGMENT (last_label_seen, now_seg);
16158     }
16159
16160   memset (&inst, '\0', sizeof (inst));
16161   inst.reloc.type = BFD_RELOC_UNUSED;
16162
16163   opcode = opcode_lookup (&p);
16164   if (!opcode)
16165     {
16166       /* It wasn't an instruction, but it might be a register alias of
16167          the form alias .req reg, or a Neon .dn/.qn directive.  */
16168       if (! create_register_alias (str, p)
16169           && ! create_neon_reg_alias (str, p))
16170         as_bad (_("bad instruction `%s'"), str);
16171
16172       return;
16173     }
16174
16175   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
16176     as_warn (_("s suffix on comparison instruction is deprecated"));
16177
16178   /* The value which unconditional instructions should have in place of the
16179      condition field.  */
16180   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
16181
16182   if (thumb_mode)
16183     {
16184       arm_feature_set variant;
16185
16186       variant = cpu_variant;
16187       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
16188       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
16189         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
16190       /* Check that this instruction is supported for this CPU.  */
16191       if (!opcode->tvariant
16192           || (thumb_mode == 1
16193               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
16194         {
16195           as_bad (_("selected processor does not support Thumb mode `%s'"), str);
16196           return;
16197         }
16198       if (inst.cond != COND_ALWAYS && !unified_syntax
16199           && opcode->tencode != do_t_branch)
16200         {
16201           as_bad (_("Thumb does not support conditional execution"));
16202           return;
16203         }
16204
16205       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2))
16206         {
16207           if (opcode->tencode != do_t_blx && opcode->tencode != do_t_branch23
16208               && !(ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_msr)
16209                    || ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_barrier)))
16210             {
16211               /* Two things are addressed here.
16212                  1) Implicit require narrow instructions on Thumb-1.
16213                     This avoids relaxation accidentally introducing Thumb-2
16214                      instructions.
16215                  2) Reject wide instructions in non Thumb-2 cores.  */
16216               if (inst.size_req == 0)
16217                 inst.size_req = 2;
16218               else if (inst.size_req == 4)
16219                 {
16220                   as_bad (_("selected processor does not support Thumb-2 mode `%s'"), str);
16221                   return;
16222                 }
16223             }
16224         }
16225
16226       inst.instruction = opcode->tvalue;
16227
16228       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
16229         {
16230           /* Prepare the it_insn_type for those encodings that don't set
16231              it.  */
16232           it_fsm_pre_encode ();
16233
16234           opcode->tencode ();
16235
16236           it_fsm_post_encode ();
16237         }
16238
16239       if (!(inst.error || inst.relax))
16240         {
16241           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
16242           inst.size = (inst.instruction > 0xffff ? 4 : 2);
16243           if (inst.size_req && inst.size_req != inst.size)
16244             {
16245               as_bad (_("cannot honor width suffix -- `%s'"), str);
16246               return;
16247             }
16248         }
16249
16250       /* Something has gone badly wrong if we try to relax a fixed size
16251          instruction.  */
16252       gas_assert (inst.size_req == 0 || !inst.relax);
16253
16254       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
16255                               *opcode->tvariant);
16256       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
16257          set those bits when Thumb-2 32-bit instructions are seen.  ie.
16258          anything other than bl/blx and v6-M instructions.
16259          This is overly pessimistic for relaxable instructions.  */
16260       if (((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
16261            || inst.relax)
16262           && !(ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
16263                || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier)))
16264         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
16265                                 arm_ext_v6t2);
16266
16267       check_neon_suffixes;
16268
16269       if (!inst.error)
16270         {
16271           mapping_state (MAP_THUMB);
16272         }
16273     }
16274   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
16275     {
16276       bfd_boolean is_bx;
16277
16278       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
16279       is_bx = (opcode->aencode == do_bx);
16280
16281       /* Check that this instruction is supported for this CPU.  */
16282       if (!(is_bx && fix_v4bx)
16283           && !(opcode->avariant &&
16284                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
16285         {
16286           as_bad (_("selected processor does not support ARM mode `%s'"), str);
16287           return;
16288         }
16289       if (inst.size_req)
16290         {
16291           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
16292           return;
16293         }
16294
16295       inst.instruction = opcode->avalue;
16296       if (opcode->tag == OT_unconditionalF)
16297         inst.instruction |= 0xF << 28;
16298       else
16299         inst.instruction |= inst.cond << 28;
16300       inst.size = INSN_SIZE;
16301       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
16302         {
16303           it_fsm_pre_encode ();
16304           opcode->aencode ();
16305           it_fsm_post_encode ();
16306         }
16307       /* Arm mode bx is marked as both v4T and v5 because it's still required
16308          on a hypothetical non-thumb v5 core.  */
16309       if (is_bx)
16310         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
16311       else
16312         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
16313                                 *opcode->avariant);
16314
16315       check_neon_suffixes;
16316
16317       if (!inst.error)
16318         {
16319           mapping_state (MAP_ARM);
16320         }
16321     }
16322   else
16323     {
16324       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
16325                 "-- `%s'"), str);
16326       return;
16327     }
16328   output_inst (str);
16329 }
16330
16331 static void
16332 check_it_blocks_finished (void)
16333 {
16334 #ifdef OBJ_ELF
16335   asection *sect;
16336
16337   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
16338     if (seg_info (sect)->tc_segment_info_data.current_it.state
16339         == MANUAL_IT_BLOCK)
16340       {
16341         as_warn (_("section '%s' finished with an open IT block."),
16342                  sect->name);
16343       }
16344 #else
16345   if (now_it.state == MANUAL_IT_BLOCK)
16346     as_warn (_("file finished with an open IT block."));
16347 #endif
16348 }
16349
16350 /* Various frobbings of labels and their addresses.  */
16351
16352 void
16353 arm_start_line_hook (void)
16354 {
16355   last_label_seen = NULL;
16356 }
16357
16358 void
16359 arm_frob_label (symbolS * sym)
16360 {
16361   last_label_seen = sym;
16362
16363   ARM_SET_THUMB (sym, thumb_mode);
16364
16365 #if defined OBJ_COFF || defined OBJ_ELF
16366   ARM_SET_INTERWORK (sym, support_interwork);
16367 #endif
16368
16369   force_automatic_it_block_close ();
16370
16371   /* Note - do not allow local symbols (.Lxxx) to be labelled
16372      as Thumb functions.  This is because these labels, whilst
16373      they exist inside Thumb code, are not the entry points for
16374      possible ARM->Thumb calls.  Also, these labels can be used
16375      as part of a computed goto or switch statement.  eg gcc
16376      can generate code that looks like this:
16377
16378                 ldr  r2, [pc, .Laaa]
16379                 lsl  r3, r3, #2
16380                 ldr  r2, [r3, r2]
16381                 mov  pc, r2
16382
16383        .Lbbb:  .word .Lxxx
16384        .Lccc:  .word .Lyyy
16385        ..etc...
16386        .Laaa:   .word Lbbb
16387
16388      The first instruction loads the address of the jump table.
16389      The second instruction converts a table index into a byte offset.
16390      The third instruction gets the jump address out of the table.
16391      The fourth instruction performs the jump.
16392
16393      If the address stored at .Laaa is that of a symbol which has the
16394      Thumb_Func bit set, then the linker will arrange for this address
16395      to have the bottom bit set, which in turn would mean that the
16396      address computation performed by the third instruction would end
16397      up with the bottom bit set.  Since the ARM is capable of unaligned
16398      word loads, the instruction would then load the incorrect address
16399      out of the jump table, and chaos would ensue.  */
16400   if (label_is_thumb_function_name
16401       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
16402       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
16403     {
16404       /* When the address of a Thumb function is taken the bottom
16405          bit of that address should be set.  This will allow
16406          interworking between Arm and Thumb functions to work
16407          correctly.  */
16408
16409       THUMB_SET_FUNC (sym, 1);
16410
16411       label_is_thumb_function_name = FALSE;
16412     }
16413
16414   dwarf2_emit_label (sym);
16415 }
16416
16417 bfd_boolean
16418 arm_data_in_code (void)
16419 {
16420   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
16421     {
16422       *input_line_pointer = '/';
16423       input_line_pointer += 5;
16424       *input_line_pointer = 0;
16425       return TRUE;
16426     }
16427
16428   return FALSE;
16429 }
16430
16431 char *
16432 arm_canonicalize_symbol_name (char * name)
16433 {
16434   int len;
16435
16436   if (thumb_mode && (len = strlen (name)) > 5
16437       && streq (name + len - 5, "/data"))
16438     *(name + len - 5) = 0;
16439
16440   return name;
16441 }
16442 \f
16443 /* Table of all register names defined by default.  The user can
16444    define additional names with .req.  Note that all register names
16445    should appear in both upper and lowercase variants.  Some registers
16446    also have mixed-case names.  */
16447
16448 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
16449 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
16450 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
16451 #define REGSET(p,t) \
16452   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
16453   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
16454   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
16455   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
16456 #define REGSETH(p,t) \
16457   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
16458   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
16459   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
16460   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
16461 #define REGSET2(p,t) \
16462   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
16463   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
16464   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
16465   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
16466 #define SPLRBANK(base,bank,t) \
16467   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
16468   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
16469   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
16470   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
16471   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
16472   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
16473
16474 static const struct reg_entry reg_names[] =
16475 {
16476   /* ARM integer registers.  */
16477   REGSET(r, RN), REGSET(R, RN),
16478
16479   /* ATPCS synonyms.  */
16480   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
16481   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
16482   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
16483
16484   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
16485   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
16486   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
16487
16488   /* Well-known aliases.  */
16489   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
16490   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
16491
16492   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
16493   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
16494
16495   /* Coprocessor numbers.  */
16496   REGSET(p, CP), REGSET(P, CP),
16497
16498   /* Coprocessor register numbers.  The "cr" variants are for backward
16499      compatibility.  */
16500   REGSET(c,  CN), REGSET(C, CN),
16501   REGSET(cr, CN), REGSET(CR, CN),
16502
16503   /* ARM banked registers.  */
16504   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
16505   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
16506   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
16507   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
16508   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
16509   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
16510   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
16511
16512   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
16513   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
16514   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
16515   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
16516   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
16517   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(SP_fiq,512|(13<<16),RNB),
16518   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
16519   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
16520
16521   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
16522   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
16523   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
16524   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
16525   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
16526   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
16527   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
16528   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB), 
16529   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
16530
16531   /* FPA registers.  */
16532   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
16533   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
16534
16535   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
16536   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
16537
16538   /* VFP SP registers.  */
16539   REGSET(s,VFS),  REGSET(S,VFS),
16540   REGSETH(s,VFS), REGSETH(S,VFS),
16541
16542   /* VFP DP Registers.  */
16543   REGSET(d,VFD),  REGSET(D,VFD),
16544   /* Extra Neon DP registers.  */
16545   REGSETH(d,VFD), REGSETH(D,VFD),
16546
16547   /* Neon QP registers.  */
16548   REGSET2(q,NQ),  REGSET2(Q,NQ),
16549
16550   /* VFP control registers.  */
16551   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
16552   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
16553   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
16554   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
16555   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
16556   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
16557
16558   /* Maverick DSP coprocessor registers.  */
16559   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
16560   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
16561
16562   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
16563   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
16564   REGDEF(dspsc,0,DSPSC),
16565
16566   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
16567   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
16568   REGDEF(DSPSC,0,DSPSC),
16569
16570   /* iWMMXt data registers - p0, c0-15.  */
16571   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
16572
16573   /* iWMMXt control registers - p1, c0-3.  */
16574   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
16575   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
16576   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
16577   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
16578
16579   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
16580   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
16581   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
16582   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
16583   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
16584
16585   /* XScale accumulator registers.  */
16586   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
16587 };
16588 #undef REGDEF
16589 #undef REGNUM
16590 #undef REGSET
16591
16592 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
16593    within psr_required_here.  */
16594 static const struct asm_psr psrs[] =
16595 {
16596   /* Backward compatibility notation.  Note that "all" is no longer
16597      truly all possible PSR bits.  */
16598   {"all",  PSR_c | PSR_f},
16599   {"flg",  PSR_f},
16600   {"ctl",  PSR_c},
16601
16602   /* Individual flags.  */
16603   {"f",    PSR_f},
16604   {"c",    PSR_c},
16605   {"x",    PSR_x},
16606   {"s",    PSR_s},
16607
16608   /* Combinations of flags.  */
16609   {"fs",   PSR_f | PSR_s},
16610   {"fx",   PSR_f | PSR_x},
16611   {"fc",   PSR_f | PSR_c},
16612   {"sf",   PSR_s | PSR_f},
16613   {"sx",   PSR_s | PSR_x},
16614   {"sc",   PSR_s | PSR_c},
16615   {"xf",   PSR_x | PSR_f},
16616   {"xs",   PSR_x | PSR_s},
16617   {"xc",   PSR_x | PSR_c},
16618   {"cf",   PSR_c | PSR_f},
16619   {"cs",   PSR_c | PSR_s},
16620   {"cx",   PSR_c | PSR_x},
16621   {"fsx",  PSR_f | PSR_s | PSR_x},
16622   {"fsc",  PSR_f | PSR_s | PSR_c},
16623   {"fxs",  PSR_f | PSR_x | PSR_s},
16624   {"fxc",  PSR_f | PSR_x | PSR_c},
16625   {"fcs",  PSR_f | PSR_c | PSR_s},
16626   {"fcx",  PSR_f | PSR_c | PSR_x},
16627   {"sfx",  PSR_s | PSR_f | PSR_x},
16628   {"sfc",  PSR_s | PSR_f | PSR_c},
16629   {"sxf",  PSR_s | PSR_x | PSR_f},
16630   {"sxc",  PSR_s | PSR_x | PSR_c},
16631   {"scf",  PSR_s | PSR_c | PSR_f},
16632   {"scx",  PSR_s | PSR_c | PSR_x},
16633   {"xfs",  PSR_x | PSR_f | PSR_s},
16634   {"xfc",  PSR_x | PSR_f | PSR_c},
16635   {"xsf",  PSR_x | PSR_s | PSR_f},
16636   {"xsc",  PSR_x | PSR_s | PSR_c},
16637   {"xcf",  PSR_x | PSR_c | PSR_f},
16638   {"xcs",  PSR_x | PSR_c | PSR_s},
16639   {"cfs",  PSR_c | PSR_f | PSR_s},
16640   {"cfx",  PSR_c | PSR_f | PSR_x},
16641   {"csf",  PSR_c | PSR_s | PSR_f},
16642   {"csx",  PSR_c | PSR_s | PSR_x},
16643   {"cxf",  PSR_c | PSR_x | PSR_f},
16644   {"cxs",  PSR_c | PSR_x | PSR_s},
16645   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
16646   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
16647   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
16648   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
16649   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
16650   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
16651   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
16652   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
16653   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
16654   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
16655   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
16656   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
16657   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
16658   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
16659   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
16660   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
16661   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
16662   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
16663   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
16664   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
16665   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
16666   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
16667   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
16668   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
16669 };
16670
16671 /* Table of V7M psr names.  */
16672 static const struct asm_psr v7m_psrs[] =
16673 {
16674   {"apsr",        0 }, {"APSR",         0 },
16675   {"iapsr",       1 }, {"IAPSR",        1 },
16676   {"eapsr",       2 }, {"EAPSR",        2 },
16677   {"psr",         3 }, {"PSR",          3 },
16678   {"xpsr",        3 }, {"XPSR",         3 }, {"xPSR",     3 },
16679   {"ipsr",        5 }, {"IPSR",         5 },
16680   {"epsr",        6 }, {"EPSR",         6 },
16681   {"iepsr",       7 }, {"IEPSR",        7 },
16682   {"msp",         8 }, {"MSP",          8 },
16683   {"psp",         9 }, {"PSP",          9 },
16684   {"primask",     16}, {"PRIMASK",      16},
16685   {"basepri",     17}, {"BASEPRI",      17},
16686   {"basepri_max", 18}, {"BASEPRI_MAX",  18},
16687   {"basepri_max", 18}, {"BASEPRI_MASK", 18}, /* Typo, preserved for backwards compatibility.  */
16688   {"faultmask",   19}, {"FAULTMASK",    19},
16689   {"control",     20}, {"CONTROL",      20}
16690 };
16691
16692 /* Table of all shift-in-operand names.  */
16693 static const struct asm_shift_name shift_names [] =
16694 {
16695   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
16696   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
16697   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
16698   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
16699   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
16700   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
16701 };
16702
16703 /* Table of all explicit relocation names.  */
16704 #ifdef OBJ_ELF
16705 static struct reloc_entry reloc_names[] =
16706 {
16707   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
16708   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
16709   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
16710   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
16711   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
16712   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
16713   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
16714   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
16715   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
16716   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
16717   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
16718   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
16719   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
16720         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
16721   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
16722         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
16723   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
16724         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ}
16725 };
16726 #endif
16727
16728 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
16729 static const struct asm_cond conds[] =
16730 {
16731   {"eq", 0x0},
16732   {"ne", 0x1},
16733   {"cs", 0x2}, {"hs", 0x2},
16734   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
16735   {"mi", 0x4},
16736   {"pl", 0x5},
16737   {"vs", 0x6},
16738   {"vc", 0x7},
16739   {"hi", 0x8},
16740   {"ls", 0x9},
16741   {"ge", 0xa},
16742   {"lt", 0xb},
16743   {"gt", 0xc},
16744   {"le", 0xd},
16745   {"al", 0xe}
16746 };
16747
16748 static struct asm_barrier_opt barrier_opt_names[] =
16749 {
16750   { "sy",    0xf }, { "SY",    0xf },
16751   { "un",    0x7 }, { "UN",    0x7 },
16752   { "st",    0xe }, { "ST",    0xe },
16753   { "unst",  0x6 }, { "UNST",  0x6 },
16754   { "ish",   0xb }, { "ISH",   0xb },
16755   { "sh",    0xb }, { "SH",    0xb },
16756   { "ishst", 0xa }, { "ISHST", 0xa },
16757   { "shst",  0xa }, { "SHST",  0xa },
16758   { "nsh",   0x7 }, { "NSH",   0x7 },
16759   { "nshst", 0x6 }, { "NSHST", 0x6 },
16760   { "osh",   0x3 }, { "OSH",   0x3 },
16761   { "oshst", 0x2 }, { "OSHST", 0x2 }
16762 };
16763
16764 /* Table of ARM-format instructions.    */
16765
16766 /* Macros for gluing together operand strings.  N.B. In all cases
16767    other than OPS0, the trailing OP_stop comes from default
16768    zero-initialization of the unspecified elements of the array.  */
16769 #define OPS0()            { OP_stop, }
16770 #define OPS1(a)           { OP_##a, }
16771 #define OPS2(a,b)         { OP_##a,OP_##b, }
16772 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
16773 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
16774 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
16775 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
16776
16777 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
16778    This is useful when mixing operands for ARM and THUMB, i.e. using the
16779    MIX_ARM_THUMB_OPERANDS macro.
16780    In order to use these macros, prefix the number of operands with _
16781    e.g. _3.  */
16782 #define OPS_1(a)           { a, }
16783 #define OPS_2(a,b)         { a,b, }
16784 #define OPS_3(a,b,c)       { a,b,c, }
16785 #define OPS_4(a,b,c,d)     { a,b,c,d, }
16786 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
16787 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
16788
16789 /* These macros abstract out the exact format of the mnemonic table and
16790    save some repeated characters.  */
16791
16792 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
16793 #define TxCE(mnem, op, top, nops, ops, ae, te) \
16794   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
16795     THUMB_VARIANT, do_##ae, do_##te }
16796
16797 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
16798    a T_MNEM_xyz enumerator.  */
16799 #define TCE(mnem, aop, top, nops, ops, ae, te) \
16800       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
16801 #define tCE(mnem, aop, top, nops, ops, ae, te) \
16802       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
16803
16804 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
16805    infix after the third character.  */
16806 #define TxC3(mnem, op, top, nops, ops, ae, te) \
16807   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
16808     THUMB_VARIANT, do_##ae, do_##te }
16809 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
16810   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
16811     THUMB_VARIANT, do_##ae, do_##te }
16812 #define TC3(mnem, aop, top, nops, ops, ae, te) \
16813       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
16814 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
16815       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
16816 #define tC3(mnem, aop, top, nops, ops, ae, te) \
16817       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
16818 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
16819       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
16820
16821 /* Mnemonic with a conditional infix in an unusual place.  Each and every variant has to
16822    appear in the condition table.  */
16823 #define TxCM_(m1, m2, m3, op, top, nops, ops, ae, te)   \
16824   { m1 #m2 m3, OPS##nops ops, sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
16825     0x##op, top, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##te }
16826
16827 #define TxCM(m1, m2, op, top, nops, ops, ae, te)        \
16828   TxCM_ (m1,   , m2, op, top, nops, ops, ae, te),       \
16829   TxCM_ (m1, eq, m2, op, top, nops, ops, ae, te),       \
16830   TxCM_ (m1, ne, m2, op, top, nops, ops, ae, te),       \
16831   TxCM_ (m1, cs, m2, op, top, nops, ops, ae, te),       \
16832   TxCM_ (m1, hs, m2, op, top, nops, ops, ae, te),       \
16833   TxCM_ (m1, cc, m2, op, top, nops, ops, ae, te),       \
16834   TxCM_ (m1, ul, m2, op, top, nops, ops, ae, te),       \
16835   TxCM_ (m1, lo, m2, op, top, nops, ops, ae, te),       \
16836   TxCM_ (m1, mi, m2, op, top, nops, ops, ae, te),       \
16837   TxCM_ (m1, pl, m2, op, top, nops, ops, ae, te),       \
16838   TxCM_ (m1, vs, m2, op, top, nops, ops, ae, te),       \
16839   TxCM_ (m1, vc, m2, op, top, nops, ops, ae, te),       \
16840   TxCM_ (m1, hi, m2, op, top, nops, ops, ae, te),       \
16841   TxCM_ (m1, ls, m2, op, top, nops, ops, ae, te),       \
16842   TxCM_ (m1, ge, m2, op, top, nops, ops, ae, te),       \
16843   TxCM_ (m1, lt, m2, op, top, nops, ops, ae, te),       \
16844   TxCM_ (m1, gt, m2, op, top, nops, ops, ae, te),       \
16845   TxCM_ (m1, le, m2, op, top, nops, ops, ae, te),       \
16846   TxCM_ (m1, al, m2, op, top, nops, ops, ae, te)
16847
16848 #define TCM(m1,m2, aop, top, nops, ops, ae, te)         \
16849       TxCM (m1,m2, aop, 0x##top, nops, ops, ae, te)
16850 #define tCM(m1,m2, aop, top, nops, ops, ae, te)         \
16851       TxCM (m1,m2, aop, T_MNEM##top, nops, ops, ae, te)
16852
16853 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
16854    field is still 0xE.  Many of the Thumb variants can be executed
16855    conditionally, so this is checked separately.  */
16856 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
16857   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
16858     THUMB_VARIANT, do_##ae, do_##te }
16859
16860 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
16861    condition code field.  */
16862 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
16863   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
16864     THUMB_VARIANT, do_##ae, do_##te }
16865
16866 /* ARM-only variants of all the above.  */
16867 #define CE(mnem,  op, nops, ops, ae)    \
16868   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
16869
16870 #define C3(mnem, op, nops, ops, ae)     \
16871   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
16872
16873 /* Legacy mnemonics that always have conditional infix after the third
16874    character.  */
16875 #define CL(mnem, op, nops, ops, ae)     \
16876   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
16877     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
16878
16879 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
16880 #define cCE(mnem,  op, nops, ops, ae)   \
16881   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
16882
16883 /* Legacy coprocessor instructions where conditional infix and conditional
16884    suffix are ambiguous.  For consistency this includes all FPA instructions,
16885    not just the potentially ambiguous ones.  */
16886 #define cCL(mnem, op, nops, ops, ae)    \
16887   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
16888     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
16889
16890 /* Coprocessor, takes either a suffix or a position-3 infix
16891    (for an FPA corner case). */
16892 #define C3E(mnem, op, nops, ops, ae) \
16893   { mnem, OPS##nops ops, OT_csuf_or_in3, \
16894     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
16895
16896 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
16897   { m1 #m2 m3, OPS##nops ops, \
16898     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
16899     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
16900
16901 #define CM(m1, m2, op, nops, ops, ae)   \
16902   xCM_ (m1,   , m2, op, nops, ops, ae), \
16903   xCM_ (m1, eq, m2, op, nops, ops, ae), \
16904   xCM_ (m1, ne, m2, op, nops, ops, ae), \
16905   xCM_ (m1, cs, m2, op, nops, ops, ae), \
16906   xCM_ (m1, hs, m2, op, nops, ops, ae), \
16907   xCM_ (m1, cc, m2, op, nops, ops, ae), \
16908   xCM_ (m1, ul, m2, op, nops, ops, ae), \
16909   xCM_ (m1, lo, m2, op, nops, ops, ae), \
16910   xCM_ (m1, mi, m2, op, nops, ops, ae), \
16911   xCM_ (m1, pl, m2, op, nops, ops, ae), \
16912   xCM_ (m1, vs, m2, op, nops, ops, ae), \
16913   xCM_ (m1, vc, m2, op, nops, ops, ae), \
16914   xCM_ (m1, hi, m2, op, nops, ops, ae), \
16915   xCM_ (m1, ls, m2, op, nops, ops, ae), \
16916   xCM_ (m1, ge, m2, op, nops, ops, ae), \
16917   xCM_ (m1, lt, m2, op, nops, ops, ae), \
16918   xCM_ (m1, gt, m2, op, nops, ops, ae), \
16919   xCM_ (m1, le, m2, op, nops, ops, ae), \
16920   xCM_ (m1, al, m2, op, nops, ops, ae)
16921
16922 #define UE(mnem, op, nops, ops, ae)     \
16923   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
16924
16925 #define UF(mnem, op, nops, ops, ae)     \
16926   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
16927
16928 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
16929    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
16930    use the same encoding function for each.  */
16931 #define NUF(mnem, op, nops, ops, enc)                                   \
16932   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
16933     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
16934
16935 /* Neon data processing, version which indirects through neon_enc_tab for
16936    the various overloaded versions of opcodes.  */
16937 #define nUF(mnem, op, nops, ops, enc)                                   \
16938   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
16939     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
16940
16941 /* Neon insn with conditional suffix for the ARM version, non-overloaded
16942    version.  */
16943 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
16944   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
16945     THUMB_VARIANT, do_##enc, do_##enc }
16946
16947 #define NCE(mnem, op, nops, ops, enc)                                   \
16948    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
16949
16950 #define NCEF(mnem, op, nops, ops, enc)                                  \
16951     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
16952
16953 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
16954 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
16955   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
16956     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
16957
16958 #define nCE(mnem, op, nops, ops, enc)                                   \
16959    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
16960
16961 #define nCEF(mnem, op, nops, ops, enc)                                  \
16962     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
16963
16964 #define do_0 0
16965
16966 static const struct asm_opcode insns[] =
16967 {
16968 #define ARM_VARIANT &arm_ext_v1 /* Core ARM Instructions.  */
16969 #define THUMB_VARIANT &arm_ext_v4t
16970  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
16971  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
16972  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
16973  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
16974  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
16975  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
16976  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
16977  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
16978  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
16979  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
16980  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
16981  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
16982  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
16983  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
16984  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
16985  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
16986
16987  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
16988     for setting PSR flag bits.  They are obsolete in V6 and do not
16989     have Thumb equivalents. */
16990  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
16991  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
16992   CL("tstp",    110f000,           2, (RR, SH),      cmp),
16993  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
16994  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
16995   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
16996  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
16997  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
16998   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
16999
17000  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
17001  tC3("movs",    1b00000, _movs,    2, (RR, SH),      mov,  t_mov_cmp),
17002  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
17003  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
17004
17005  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
17006  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
17007  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
17008                                                                 OP_RRnpc),
17009                                         OP_ADDRGLDR),ldst, t_ldst),
17010  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
17011
17012  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17013  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17014  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17015  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17016  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17017  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17018
17019  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
17020  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
17021  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
17022  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
17023
17024   /* Pseudo ops.  */
17025  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
17026   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
17027  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
17028
17029   /* Thumb-compatibility pseudo ops.  */
17030  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
17031  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
17032  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
17033  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
17034  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
17035  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
17036  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
17037  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
17038  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
17039  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
17040  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
17041  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
17042
17043  /* These may simplify to neg.  */
17044  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
17045  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
17046
17047 #undef  THUMB_VARIANT
17048 #define THUMB_VARIANT  & arm_ext_v6
17049
17050  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
17051
17052  /* V1 instructions with no Thumb analogue prior to V6T2.  */
17053 #undef  THUMB_VARIANT
17054 #define THUMB_VARIANT  & arm_ext_v6t2
17055
17056  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
17057  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
17058   CL("teqp",    130f000,           2, (RR, SH),      cmp),
17059
17060  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17061  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17062  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
17063  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17064
17065  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17066  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17067
17068  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17069  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17070
17071  /* V1 instructions with no Thumb analogue at all.  */
17072   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
17073   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
17074
17075   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
17076   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
17077   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
17078   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
17079   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
17080   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
17081   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
17082   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
17083
17084 #undef  ARM_VARIANT
17085 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
17086 #undef  THUMB_VARIANT
17087 #define THUMB_VARIANT  & arm_ext_v4t
17088
17089  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
17090  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
17091
17092 #undef  THUMB_VARIANT
17093 #define THUMB_VARIANT  & arm_ext_v6t2
17094
17095  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
17096   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
17097
17098   /* Generic coprocessor instructions.  */
17099  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
17100  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17101  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17102  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17103  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17104  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
17105  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
17106
17107 #undef  ARM_VARIANT
17108 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
17109
17110   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
17111   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
17112
17113 #undef  ARM_VARIANT
17114 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
17115 #undef  THUMB_VARIANT
17116 #define THUMB_VARIANT  & arm_ext_msr
17117
17118  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
17119  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
17120
17121 #undef  ARM_VARIANT
17122 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
17123 #undef  THUMB_VARIANT
17124 #define THUMB_VARIANT  & arm_ext_v6t2
17125
17126  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17127   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17128  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17129   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17130  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17131   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17132  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17133   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17134
17135 #undef  ARM_VARIANT
17136 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
17137 #undef  THUMB_VARIANT
17138 #define THUMB_VARIANT  & arm_ext_v4t
17139
17140  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17141  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17142  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17143  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17144  tCM("ld","sh", 01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17145  tCM("ld","sb", 01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17146
17147 #undef  ARM_VARIANT
17148 #define ARM_VARIANT  & arm_ext_v4t_5
17149
17150   /* ARM Architecture 4T.  */
17151   /* Note: bx (and blx) are required on V5, even if the processor does
17152      not support Thumb.  */
17153  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
17154
17155 #undef  ARM_VARIANT
17156 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
17157 #undef  THUMB_VARIANT
17158 #define THUMB_VARIANT  & arm_ext_v5t
17159
17160   /* Note: blx has 2 variants; the .value coded here is for
17161      BLX(2).  Only this variant has conditional execution.  */
17162  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
17163  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
17164
17165 #undef  THUMB_VARIANT
17166 #define THUMB_VARIANT  & arm_ext_v6t2
17167
17168  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
17169  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17170  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
17171  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17172  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
17173  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
17174  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
17175  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
17176
17177 #undef  ARM_VARIANT
17178 #define ARM_VARIANT  & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
17179 #undef THUMB_VARIANT
17180 #define THUMB_VARIANT &arm_ext_v5exp
17181
17182  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17183  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17184  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17185  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17186
17187  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17188  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17189
17190  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17191  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17192  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17193  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17194
17195  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17196  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17197  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17198  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17199
17200  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17201  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17202
17203  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17204  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17205  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17206  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17207
17208 #undef  ARM_VARIANT
17209 #define ARM_VARIANT  & arm_ext_v5e /*  ARM Architecture 5TE.  */
17210 #undef THUMB_VARIANT
17211 #define THUMB_VARIANT &arm_ext_v6t2
17212
17213  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
17214  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
17215      ldrd, t_ldstd),
17216  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
17217                                        ADDRGLDRS), ldrd, t_ldstd),
17218
17219  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17220  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17221
17222 #undef  ARM_VARIANT
17223 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
17224
17225  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
17226
17227 #undef  ARM_VARIANT
17228 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
17229 #undef  THUMB_VARIANT
17230 #define THUMB_VARIANT  & arm_ext_v6
17231
17232  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
17233  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
17234  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
17235  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
17236  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
17237  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17238  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17239  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17240  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17241  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
17242
17243 #undef  THUMB_VARIANT
17244 #define THUMB_VARIANT  & arm_ext_v6t2
17245
17246  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
17247  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
17248                                       strex,  t_strex),
17249  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17250  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17251
17252  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
17253  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
17254
17255 /*  ARM V6 not included in V7M.  */
17256 #undef  THUMB_VARIANT
17257 #define THUMB_VARIANT  & arm_ext_v6_notm
17258  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
17259   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
17260   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
17261  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
17262  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
17263   UF(rfefa,     9900a00,           1, (RRw),                       rfe),
17264   UF(rfeea,     8100a00,           1, (RRw),                       rfe),
17265  TUF("rfeed",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
17266  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
17267   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
17268   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
17269  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
17270
17271 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
17272 #undef  THUMB_VARIANT
17273 #define THUMB_VARIANT  & arm_ext_v6_dsp
17274  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
17275  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
17276  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
17277  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17278  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17279  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17280  /* Old name for QASX.  */
17281  TCE("qaddsubx",        6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17282  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17283  /* Old name for QSAX.  */
17284  TCE("qsubaddx",        6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17285  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17286  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17287  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17288  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17289  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17290  /* Old name for SASX.  */
17291  TCE("saddsubx",        6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17292  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17293  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17294  TCE("shasx",     6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17295  /* Old name for SHASX.  */
17296  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17297  TCE("shsax",      6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),    rd_rn_rm, t_simd),
17298  /* Old name for SHSAX.  */
17299  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17300  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17301  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17302  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17303  /* Old name for SSAX.  */
17304  TCE("ssubaddx",        6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17305  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17306  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17307  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17308  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17309  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17310  /* Old name for UASX.  */
17311  TCE("uaddsubx",        6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17312  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17313  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17314  TCE("uhasx",     6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17315  /* Old name for UHASX.  */
17316  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17317  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17318  /* Old name for UHSAX.  */
17319  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17320  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17321  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17322  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17323  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17324  TCE("uqasx",     6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17325  /* Old name for UQASX.  */
17326  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17327  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17328  /* Old name for UQSAX.  */
17329  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17330  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17331  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17332  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17333  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17334  /* Old name for USAX.  */
17335  TCE("usubaddx",        6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17336  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17337  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17338  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17339  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17340  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
17341  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17342  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17343  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17344  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
17345  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17346  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17347  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17348  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17349  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17350  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17351  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17352  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17353  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17354  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17355  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17356  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17357  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17358  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17359  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17360  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17361  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17362  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17363  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17364  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
17365  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
17366  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
17367  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
17368  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
17369
17370 #undef  ARM_VARIANT
17371 #define ARM_VARIANT   & arm_ext_v6k
17372 #undef  THUMB_VARIANT
17373 #define THUMB_VARIANT & arm_ext_v6k
17374
17375  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
17376  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
17377  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
17378  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
17379
17380 #undef  THUMB_VARIANT
17381 #define THUMB_VARIANT  & arm_ext_v6_notm
17382  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
17383                                       ldrexd, t_ldrexd),
17384  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
17385                                        RRnpcb), strexd, t_strexd),
17386
17387 #undef  THUMB_VARIANT
17388 #define THUMB_VARIANT  & arm_ext_v6t2
17389  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
17390      rd_rn,  rd_rn),
17391  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
17392      rd_rn,  rd_rn),
17393  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
17394      strex, rm_rd_rn),
17395  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
17396      strex, rm_rd_rn), 
17397  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
17398
17399 #undef  ARM_VARIANT
17400 #define ARM_VARIANT    & arm_ext_sec
17401 #undef THUMB_VARIANT
17402 #define THUMB_VARIANT  & arm_ext_sec
17403
17404  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
17405
17406 #undef  ARM_VARIANT
17407 #define ARM_VARIANT    & arm_ext_virt
17408 #undef  THUMB_VARIANT
17409 #define THUMB_VARIANT    & arm_ext_virt
17410
17411  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
17412  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
17413
17414 #undef  ARM_VARIANT
17415 #define ARM_VARIANT  & arm_ext_v6t2
17416 #undef  THUMB_VARIANT
17417 #define THUMB_VARIANT  & arm_ext_v6t2
17418
17419  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
17420  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
17421  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
17422  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
17423
17424  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
17425  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
17426  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
17427  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
17428
17429  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17430  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17431  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17432  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17433
17434  /* Thumb-only instructions.  */
17435 #undef ARM_VARIANT
17436 #define ARM_VARIANT NULL
17437   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
17438   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
17439
17440  /* ARM does not really have an IT instruction, so always allow it.
17441     The opcode is copied from Thumb in order to allow warnings in
17442     -mimplicit-it=[never | arm] modes.  */
17443 #undef  ARM_VARIANT
17444 #define ARM_VARIANT  & arm_ext_v1
17445
17446  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
17447  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
17448  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
17449  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
17450  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
17451  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
17452  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
17453  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
17454  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
17455  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
17456  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
17457  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
17458  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
17459  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
17460  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
17461  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
17462  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
17463  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
17464
17465  /* Thumb2 only instructions.  */
17466 #undef  ARM_VARIANT
17467 #define ARM_VARIANT  NULL
17468
17469  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
17470  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
17471  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
17472  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
17473  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
17474  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
17475
17476  /* Hardware division instructions.  */
17477 #undef  ARM_VARIANT
17478 #define ARM_VARIANT    & arm_ext_adiv
17479 #undef  THUMB_VARIANT
17480 #define THUMB_VARIANT  & arm_ext_div
17481
17482  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
17483  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
17484
17485  /* ARM V6M/V7 instructions.  */
17486 #undef  ARM_VARIANT
17487 #define ARM_VARIANT    & arm_ext_barrier
17488 #undef  THUMB_VARIANT
17489 #define THUMB_VARIANT  & arm_ext_barrier
17490
17491  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier,  t_barrier),
17492  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier,  t_barrier),
17493  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier,  t_barrier),
17494
17495  /* ARM V7 instructions.  */
17496 #undef  ARM_VARIANT
17497 #define ARM_VARIANT    & arm_ext_v7
17498 #undef  THUMB_VARIANT
17499 #define THUMB_VARIANT  & arm_ext_v7
17500
17501  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
17502  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
17503
17504 #undef ARM_VARIANT
17505 #define ARM_VARIANT    & arm_ext_mp
17506 #undef THUMB_VARIANT
17507 #define THUMB_VARIANT  & arm_ext_mp
17508
17509  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
17510
17511 #undef  ARM_VARIANT
17512 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
17513
17514  cCE("wfs",     e200110, 1, (RR),            rd),
17515  cCE("rfs",     e300110, 1, (RR),            rd),
17516  cCE("wfc",     e400110, 1, (RR),            rd),
17517  cCE("rfc",     e500110, 1, (RR),            rd),
17518
17519  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17520  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17521  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17522  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17523
17524  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17525  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17526  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17527  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17528
17529  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
17530  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
17531  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
17532  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
17533  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
17534  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
17535  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
17536  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
17537  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
17538  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
17539  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
17540  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
17541
17542  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
17543  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
17544  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
17545  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
17546  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
17547  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
17548  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
17549  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
17550  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
17551  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
17552  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
17553  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
17554
17555  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
17556  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
17557  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
17558  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
17559  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
17560  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
17561  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
17562  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
17563  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
17564  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
17565  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
17566  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
17567
17568  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
17569  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
17570  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
17571  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
17572  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
17573  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
17574  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
17575  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
17576  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
17577  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
17578  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
17579  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
17580
17581  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
17582  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
17583  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
17584  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
17585  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
17586  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
17587  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
17588  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
17589  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
17590  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
17591  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
17592  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
17593
17594  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
17595  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
17596  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
17597  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
17598  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
17599  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
17600  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
17601  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
17602  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
17603  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
17604  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
17605  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
17606
17607  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
17608  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
17609  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
17610  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
17611  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
17612  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
17613  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
17614  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
17615  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
17616  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
17617  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
17618  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
17619
17620  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
17621  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
17622  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
17623  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
17624  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
17625  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
17626  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
17627  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
17628  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
17629  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
17630  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
17631  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
17632
17633  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
17634  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
17635  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
17636  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
17637  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
17638  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
17639  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
17640  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
17641  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
17642  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
17643  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
17644  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
17645
17646  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
17647  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
17648  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
17649  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
17650  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
17651  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
17652  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
17653  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
17654  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
17655  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
17656  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
17657  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
17658
17659  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
17660  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
17661  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
17662  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
17663  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
17664  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
17665  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
17666  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
17667  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
17668  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
17669  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
17670  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
17671
17672  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
17673  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
17674  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
17675  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
17676  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
17677  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
17678  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
17679  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
17680  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
17681  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
17682  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
17683  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
17684
17685  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
17686  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
17687  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
17688  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
17689  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
17690  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
17691  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
17692  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
17693  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
17694  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
17695  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
17696  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
17697
17698  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
17699  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
17700  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
17701  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
17702  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
17703  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
17704  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
17705  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
17706  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
17707  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
17708  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
17709  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
17710
17711  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
17712  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
17713  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
17714  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
17715  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
17716  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
17717  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
17718  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
17719  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
17720  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
17721  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
17722  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
17723
17724  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
17725  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
17726  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
17727  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
17728  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
17729  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
17730  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
17731  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
17732  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
17733  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
17734  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
17735  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
17736
17737  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
17738  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
17739  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
17740  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
17741  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
17742  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17743  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17744  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17745  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
17746  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
17747  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
17748  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
17749
17750  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
17751  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
17752  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
17753  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
17754  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
17755  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17756  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17757  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17758  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
17759  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
17760  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
17761  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
17762
17763  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
17764  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
17765  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
17766  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
17767  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
17768  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17769  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17770  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17771  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
17772  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
17773  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
17774  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
17775
17776  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
17777  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
17778  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
17779  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
17780  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
17781  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17782  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17783  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17784  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
17785  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
17786  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
17787  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
17788
17789  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
17790  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
17791  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
17792  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
17793  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
17794  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17795  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17796  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17797  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
17798  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
17799  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
17800  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
17801
17802  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
17803  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
17804  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
17805  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
17806  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
17807  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17808  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17809  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17810  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
17811  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
17812  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
17813  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
17814
17815  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
17816  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
17817  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
17818  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
17819  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
17820  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17821  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17822  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17823  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
17824  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
17825  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
17826  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
17827
17828  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
17829  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
17830  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
17831  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
17832  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
17833  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17834  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17835  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17836  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
17837  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
17838  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
17839  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
17840
17841  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
17842  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
17843  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
17844  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
17845  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
17846  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17847  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17848  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17849  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
17850  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
17851  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
17852  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
17853
17854  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
17855  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
17856  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
17857  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
17858  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
17859  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17860  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17861  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17862  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
17863  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
17864  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
17865  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
17866
17867  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
17868  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
17869  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
17870  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
17871  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
17872  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17873  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17874  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17875  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
17876  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
17877  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
17878  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
17879
17880  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
17881  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
17882  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
17883  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
17884  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
17885  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17886  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17887  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17888  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
17889  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
17890  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
17891  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
17892
17893  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
17894  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
17895  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
17896  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
17897  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
17898  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17899  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17900  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17901  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
17902  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
17903  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
17904  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
17905
17906  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
17907  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
17908  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
17909  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
17910
17911  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
17912  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
17913  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
17914  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
17915  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
17916  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
17917  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
17918  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
17919  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
17920  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
17921  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
17922  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
17923
17924   /* The implementation of the FIX instruction is broken on some
17925      assemblers, in that it accepts a precision specifier as well as a
17926      rounding specifier, despite the fact that this is meaningless.
17927      To be more compatible, we accept it as well, though of course it
17928      does not set any bits.  */
17929  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
17930  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
17931  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
17932  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
17933  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
17934  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
17935  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
17936  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
17937  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
17938  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
17939  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
17940  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
17941  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
17942
17943   /* Instructions that were new with the real FPA, call them V2.  */
17944 #undef  ARM_VARIANT
17945 #define ARM_VARIANT  & fpu_fpa_ext_v2
17946
17947  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
17948  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
17949  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
17950  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
17951  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
17952  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
17953
17954 #undef  ARM_VARIANT
17955 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
17956
17957   /* Moves and type conversions.  */
17958  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
17959  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
17960  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
17961  cCE("fmstat",  ef1fa10, 0, (),               noargs),
17962  cCE("vmrs",    ef10a10, 2, (APSR_RR, RVC),   vmrs),
17963  cCE("vmsr",    ee10a10, 2, (RVC, RR),        vmsr),
17964  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
17965  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
17966  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
17967  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
17968  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
17969  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
17970  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
17971  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
17972
17973   /* Memory operations.  */
17974  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
17975  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
17976  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
17977  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
17978  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
17979  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
17980  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
17981  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
17982  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
17983  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
17984  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
17985  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
17986  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
17987  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
17988  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
17989  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
17990  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
17991  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
17992
17993   /* Monadic operations.  */
17994  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
17995  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
17996  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
17997
17998   /* Dyadic operations.  */
17999  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18000  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18001  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18002  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18003  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18004  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18005  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18006  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18007  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18008
18009   /* Comparisons.  */
18010  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
18011  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
18012  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18013  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
18014
18015  /* Double precision load/store are still present on single precision
18016     implementations.  */
18017  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
18018  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
18019  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18020  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18021  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18022  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18023  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18024  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18025  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18026  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18027
18028 #undef  ARM_VARIANT
18029 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
18030
18031   /* Moves and type conversions.  */
18032  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
18033  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
18034  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18035  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
18036  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
18037  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
18038  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
18039  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
18040  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
18041  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18042  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18043  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18044  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18045
18046   /* Monadic operations.  */
18047  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
18048  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
18049  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
18050
18051   /* Dyadic operations.  */
18052  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18053  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18054  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18055  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18056  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18057  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18058  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18059  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18060  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18061
18062   /* Comparisons.  */
18063  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
18064  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
18065  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
18066  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
18067
18068 #undef  ARM_VARIANT
18069 #define ARM_VARIANT  & fpu_vfp_ext_v2
18070
18071  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
18072  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
18073  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
18074  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
18075
18076 /* Instructions which may belong to either the Neon or VFP instruction sets.
18077    Individual encoder functions perform additional architecture checks.  */
18078 #undef  ARM_VARIANT
18079 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
18080 #undef  THUMB_VARIANT
18081 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
18082
18083   /* These mnemonics are unique to VFP.  */
18084  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
18085  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
18086  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18087  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18088  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18089  nCE(vcmp,      _vcmp,    2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
18090  nCE(vcmpe,     _vcmpe,   2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
18091  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
18092  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
18093  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
18094
18095   /* Mnemonics shared by Neon and VFP.  */
18096  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
18097  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
18098  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
18099
18100  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
18101  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
18102
18103  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
18104  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
18105
18106  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18107  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18108  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18109  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18110  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18111  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18112  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
18113  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
18114
18115  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32b), neon_cvt),
18116  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
18117  nCEF(vcvtb,    _vcvt,   2, (RVS, RVS), neon_cvtb),
18118  nCEF(vcvtt,    _vcvt,   2, (RVS, RVS), neon_cvtt),
18119
18120
18121   /* NOTE: All VMOV encoding is special-cased!  */
18122  NCE(vmov,      0,       1, (VMOV), neon_mov),
18123  NCE(vmovq,     0,       1, (VMOV), neon_mov),
18124
18125 #undef  THUMB_VARIANT
18126 #define THUMB_VARIANT  & fpu_neon_ext_v1
18127 #undef  ARM_VARIANT
18128 #define ARM_VARIANT    & fpu_neon_ext_v1
18129
18130   /* Data processing with three registers of the same length.  */
18131   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
18132  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
18133  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
18134  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
18135  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
18136  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
18137  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
18138  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
18139  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
18140   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
18141  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
18142  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
18143  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
18144  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
18145  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
18146  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
18147  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
18148  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
18149   /* If not immediate, fall back to neon_dyadic_i64_su.
18150      shl_imm should accept I8 I16 I32 I64,
18151      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
18152  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
18153  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
18154  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
18155  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
18156   /* Logic ops, types optional & ignored.  */
18157  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18158  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18159  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18160  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18161  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18162  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18163  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18164  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18165  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
18166  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
18167   /* Bitfield ops, untyped.  */
18168  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
18169  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
18170  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
18171  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
18172  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
18173  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
18174   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F32.  */
18175  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
18176  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
18177  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
18178  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
18179  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
18180  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
18181   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
18182      back to neon_dyadic_if_su.  */
18183  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
18184  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
18185  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
18186  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
18187  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
18188  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
18189  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
18190  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
18191   /* Comparison. Type I8 I16 I32 F32.  */
18192  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
18193  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
18194   /* As above, D registers only.  */
18195  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
18196  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
18197   /* Int and float variants, signedness unimportant.  */
18198  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
18199  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
18200  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
18201   /* Add/sub take types I8 I16 I32 I64 F32.  */
18202  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
18203  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
18204   /* vtst takes sizes 8, 16, 32.  */
18205  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
18206  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
18207   /* VMUL takes I8 I16 I32 F32 P8.  */
18208  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
18209   /* VQD{R}MULH takes S16 S32.  */
18210  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
18211  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
18212  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
18213  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
18214  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
18215  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
18216  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
18217  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
18218  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
18219  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
18220  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
18221  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
18222  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
18223  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
18224  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
18225  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
18226
18227   /* Two address, int/float. Types S8 S16 S32 F32.  */
18228  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
18229  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
18230
18231   /* Data processing with two registers and a shift amount.  */
18232   /* Right shifts, and variants with rounding.
18233      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
18234  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
18235  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
18236  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
18237  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
18238  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
18239  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
18240  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
18241  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
18242   /* Shift and insert. Sizes accepted 8 16 32 64.  */
18243  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
18244  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
18245  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
18246  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
18247   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
18248  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
18249  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
18250   /* Right shift immediate, saturating & narrowing, with rounding variants.
18251      Types accepted S16 S32 S64 U16 U32 U64.  */
18252  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
18253  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
18254   /* As above, unsigned. Types accepted S16 S32 S64.  */
18255  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
18256  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
18257   /* Right shift narrowing. Types accepted I16 I32 I64.  */
18258  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
18259  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
18260   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
18261  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
18262   /* CVT with optional immediate for fixed-point variant.  */
18263  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
18264
18265  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
18266  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
18267
18268   /* Data processing, three registers of different lengths.  */
18269   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
18270  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
18271  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
18272  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
18273  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
18274   /* If not scalar, fall back to neon_dyadic_long.
18275      Vector types as above, scalar types S16 S32 U16 U32.  */
18276  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
18277  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
18278   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
18279  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
18280  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
18281   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
18282  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18283  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18284  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18285  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18286   /* Saturating doubling multiplies. Types S16 S32.  */
18287  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
18288  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
18289  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
18290   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
18291      S16 S32 U16 U32.  */
18292  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
18293
18294   /* Extract. Size 8.  */
18295  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
18296  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
18297
18298   /* Two registers, miscellaneous.  */
18299   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
18300  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
18301  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
18302  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
18303  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
18304  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
18305  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
18306   /* Vector replicate. Sizes 8 16 32.  */
18307  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
18308  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
18309   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
18310  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
18311   /* VMOVN. Types I16 I32 I64.  */
18312  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
18313   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
18314  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
18315   /* VQMOVUN. Types S16 S32 S64.  */
18316  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
18317   /* VZIP / VUZP. Sizes 8 16 32.  */
18318  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
18319  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
18320  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
18321  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
18322   /* VQABS / VQNEG. Types S8 S16 S32.  */
18323  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
18324  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
18325  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
18326  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
18327   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
18328  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
18329  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
18330  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
18331  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
18332   /* Reciprocal estimates. Types U32 F32.  */
18333  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
18334  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
18335  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
18336  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
18337   /* VCLS. Types S8 S16 S32.  */
18338  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
18339  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
18340   /* VCLZ. Types I8 I16 I32.  */
18341  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
18342  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
18343   /* VCNT. Size 8.  */
18344  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
18345  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
18346   /* Two address, untyped.  */
18347  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
18348  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
18349   /* VTRN. Sizes 8 16 32.  */
18350  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
18351  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
18352
18353   /* Table lookup. Size 8.  */
18354  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
18355  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
18356
18357 #undef  THUMB_VARIANT
18358 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
18359 #undef  ARM_VARIANT
18360 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
18361
18362   /* Neon element/structure load/store.  */
18363  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18364  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18365  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18366  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18367  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18368  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18369  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18370  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18371
18372 #undef  THUMB_VARIANT
18373 #define THUMB_VARIANT &fpu_vfp_ext_v3xd
18374 #undef ARM_VARIANT
18375 #define ARM_VARIANT &fpu_vfp_ext_v3xd
18376  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
18377  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18378  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18379  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18380  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18381  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18382  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18383  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18384  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18385
18386 #undef THUMB_VARIANT
18387 #define THUMB_VARIANT  & fpu_vfp_ext_v3
18388 #undef  ARM_VARIANT
18389 #define ARM_VARIANT    & fpu_vfp_ext_v3
18390
18391  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
18392  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18393  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18394  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18395  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18396  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18397  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18398  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18399  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18400
18401 #undef ARM_VARIANT
18402 #define ARM_VARIANT &fpu_vfp_ext_fma
18403 #undef THUMB_VARIANT
18404 #define THUMB_VARIANT &fpu_vfp_ext_fma
18405  /* Mnemonics shared by Neon and VFP.  These are included in the
18406     VFP FMA variant; NEON and VFP FMA always includes the NEON
18407     FMA instructions.  */
18408  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
18409  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
18410  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
18411     the v form should always be used.  */
18412  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18413  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18414  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18415  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18416  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18417  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18418
18419 #undef THUMB_VARIANT
18420 #undef  ARM_VARIANT
18421 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
18422
18423  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18424  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18425  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18426  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18427  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18428  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18429  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
18430  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
18431
18432 #undef  ARM_VARIANT
18433 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
18434
18435  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
18436  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
18437  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
18438  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
18439  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
18440  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
18441  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
18442  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
18443  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
18444  cCE("textrmub",        e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18445  cCE("textrmuh",        e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18446  cCE("textrmuw",        e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18447  cCE("textrmsb",        e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18448  cCE("textrmsh",        e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18449  cCE("textrmsw",        e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18450  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
18451  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
18452  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
18453  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
18454  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
18455  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18456  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18457  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18458  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18459  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18460  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18461  cCE("tmovmskb",        e100030, 2, (RR, RIWR),             rd_rn),
18462  cCE("tmovmskh",        e500030, 2, (RR, RIWR),             rd_rn),
18463  cCE("tmovmskw",        e900030, 2, (RR, RIWR),             rd_rn),
18464  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
18465  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
18466  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
18467  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
18468  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
18469  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
18470  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
18471  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
18472  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18473  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18474  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18475  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18476  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18477  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18478  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18479  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18480  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18481  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
18482  cCE("walignr0",        e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18483  cCE("walignr1",        e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18484  cCE("walignr2",        ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18485  cCE("walignr3",        eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18486  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18487  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18488  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18489  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18490  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18491  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18492  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18493  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18494  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18495  cCE("wcmpgtub",        e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18496  cCE("wcmpgtuh",        e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18497  cCE("wcmpgtuw",        e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18498  cCE("wcmpgtsb",        e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18499  cCE("wcmpgtsh",        e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18500  cCE("wcmpgtsw",        eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18501  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18502  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18503  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
18504  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
18505  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18506  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18507  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18508  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18509  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18510  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18511  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18512  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18513  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18514  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18515  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18516  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18517  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18518  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18519  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18520  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18521  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18522  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18523  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
18524  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18525  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18526  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18527  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18528  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18529  cCE("wpackhss",        e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18530  cCE("wpackhus",        e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18531  cCE("wpackwss",        eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18532  cCE("wpackwus",        e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18533  cCE("wpackdss",        ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18534  cCE("wpackdus",        ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18535  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18536  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18537  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18538  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18539  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18540  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18541  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18542  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18543  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18544  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18545  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
18546  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18547  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18548  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18549  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18550  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18551  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18552  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18553  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18554  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18555  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18556  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18557  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18558  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18559  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18560  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18561  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18562  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18563  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18564  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18565  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18566  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
18567  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
18568  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18569  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18570  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18571  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18572  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18573  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18574  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18575  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18576  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18577  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
18578  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
18579  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
18580  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
18581  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
18582  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
18583  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18584  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18585  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18586  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
18587  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
18588  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
18589  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
18590  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
18591  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
18592  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18593  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18594  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18595  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18596  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
18597
18598 #undef  ARM_VARIANT
18599 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
18600
18601  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
18602  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
18603  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
18604  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
18605  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
18606  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
18607  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18608  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18609  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18610  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18611  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18612  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18613  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18614  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18615  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18616  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18617  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18618  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18619  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18620  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18621  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
18622  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18623  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18624  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18625  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18626  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18627  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18628  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18629  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18630  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18631  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18632  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18633  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18634  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18635  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18636  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18637  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18638  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18639  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18640  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18641  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18642  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18643  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18644  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18645  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18646  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18647  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18648  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18649  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18650  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18651  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18652  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18653  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18654  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18655  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18656  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18657  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18658
18659 #undef  ARM_VARIANT
18660 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
18661
18662  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
18663  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
18664  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
18665  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
18666  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
18667  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
18668  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
18669  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
18670  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
18671  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
18672  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
18673  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
18674  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
18675  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
18676  cCE("cfmv64lr",        e000510, 2, (RMDX, RR),               rn_rd),
18677  cCE("cfmvr64l",        e100510, 2, (RR, RMDX),               rd_rn),
18678  cCE("cfmv64hr",        e000530, 2, (RMDX, RR),               rn_rd),
18679  cCE("cfmvr64h",        e100530, 2, (RR, RMDX),               rd_rn),
18680  cCE("cfmval32",        e200440, 2, (RMAX, RMFX),             rd_rn),
18681  cCE("cfmv32al",        e100440, 2, (RMFX, RMAX),             rd_rn),
18682  cCE("cfmvam32",        e200460, 2, (RMAX, RMFX),             rd_rn),
18683  cCE("cfmv32am",        e100460, 2, (RMFX, RMAX),             rd_rn),
18684  cCE("cfmvah32",        e200480, 2, (RMAX, RMFX),             rd_rn),
18685  cCE("cfmv32ah",        e100480, 2, (RMFX, RMAX),             rd_rn),
18686  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
18687  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
18688  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
18689  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
18690  cCE("cfmvsc32",        e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
18691  cCE("cfmv32sc",        e1004e0, 2, (RMDX, RMDS),             rd),
18692  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
18693  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
18694  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
18695  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
18696  cCE("cfcvt32s",        e000480, 2, (RMF, RMFX),              rd_rn),
18697  cCE("cfcvt32d",        e0004a0, 2, (RMD, RMFX),              rd_rn),
18698  cCE("cfcvt64s",        e0004c0, 2, (RMF, RMDX),              rd_rn),
18699  cCE("cfcvt64d",        e0004e0, 2, (RMD, RMDX),              rd_rn),
18700  cCE("cfcvts32",        e100580, 2, (RMFX, RMF),              rd_rn),
18701  cCE("cfcvtd32",        e1005a0, 2, (RMFX, RMD),              rd_rn),
18702  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
18703  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
18704  cCE("cfrshl32",        e000550, 3, (RMFX, RMFX, RR),         mav_triple),
18705  cCE("cfrshl64",        e000570, 3, (RMDX, RMDX, RR),         mav_triple),
18706  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
18707  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
18708  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
18709  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
18710  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
18711  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
18712  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
18713  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
18714  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
18715  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
18716  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
18717  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
18718  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
18719  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
18720  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
18721  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
18722  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
18723  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
18724  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
18725  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
18726  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18727  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
18728  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18729  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
18730  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18731  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
18732  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18733  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18734  cCE("cfmadd32",        e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
18735  cCE("cfmsub32",        e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
18736  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
18737  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
18738 };
18739 #undef ARM_VARIANT
18740 #undef THUMB_VARIANT
18741 #undef TCE
18742 #undef TCM
18743 #undef TUE
18744 #undef TUF
18745 #undef TCC
18746 #undef cCE
18747 #undef cCL
18748 #undef C3E
18749 #undef CE
18750 #undef CM
18751 #undef UE
18752 #undef UF
18753 #undef UT
18754 #undef NUF
18755 #undef nUF
18756 #undef NCE
18757 #undef nCE
18758 #undef OPS0
18759 #undef OPS1
18760 #undef OPS2
18761 #undef OPS3
18762 #undef OPS4
18763 #undef OPS5
18764 #undef OPS6
18765 #undef do_0
18766 \f
18767 /* MD interface: bits in the object file.  */
18768
18769 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
18770    for use in the a.out file, and stores them in the array pointed to by buf.
18771    This knows about the endian-ness of the target machine and does
18772    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
18773    2 (short) and 4 (long)  Floating numbers are put out as a series of
18774    LITTLENUMS (shorts, here at least).  */
18775
18776 void
18777 md_number_to_chars (char * buf, valueT val, int n)
18778 {
18779   if (target_big_endian)
18780     number_to_chars_bigendian (buf, val, n);
18781   else
18782     number_to_chars_littleendian (buf, val, n);
18783 }
18784
18785 static valueT
18786 md_chars_to_number (char * buf, int n)
18787 {
18788   valueT result = 0;
18789   unsigned char * where = (unsigned char *) buf;
18790
18791   if (target_big_endian)
18792     {
18793       while (n--)
18794         {
18795           result <<= 8;
18796           result |= (*where++ & 255);
18797         }
18798     }
18799   else
18800     {
18801       while (n--)
18802         {
18803           result <<= 8;
18804           result |= (where[n] & 255);
18805         }
18806     }
18807
18808   return result;
18809 }
18810
18811 /* MD interface: Sections.  */
18812
18813 /* Estimate the size of a frag before relaxing.  Assume everything fits in
18814    2 bytes.  */
18815
18816 int
18817 md_estimate_size_before_relax (fragS * fragp,
18818                                segT    segtype ATTRIBUTE_UNUSED)
18819 {
18820   fragp->fr_var = 2;
18821   return 2;
18822 }
18823
18824 /* Convert a machine dependent frag.  */
18825
18826 void
18827 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
18828 {
18829   unsigned long insn;
18830   unsigned long old_op;
18831   char *buf;
18832   expressionS exp;
18833   fixS *fixp;
18834   int reloc_type;
18835   int pc_rel;
18836   int opcode;
18837
18838   buf = fragp->fr_literal + fragp->fr_fix;
18839
18840   old_op = bfd_get_16(abfd, buf);
18841   if (fragp->fr_symbol)
18842     {
18843       exp.X_op = O_symbol;
18844       exp.X_add_symbol = fragp->fr_symbol;
18845     }
18846   else
18847     {
18848       exp.X_op = O_constant;
18849     }
18850   exp.X_add_number = fragp->fr_offset;
18851   opcode = fragp->fr_subtype;
18852   switch (opcode)
18853     {
18854     case T_MNEM_ldr_pc:
18855     case T_MNEM_ldr_pc2:
18856     case T_MNEM_ldr_sp:
18857     case T_MNEM_str_sp:
18858     case T_MNEM_ldr:
18859     case T_MNEM_ldrb:
18860     case T_MNEM_ldrh:
18861     case T_MNEM_str:
18862     case T_MNEM_strb:
18863     case T_MNEM_strh:
18864       if (fragp->fr_var == 4)
18865         {
18866           insn = THUMB_OP32 (opcode);
18867           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
18868             {
18869               insn |= (old_op & 0x700) << 4;
18870             }
18871           else
18872             {
18873               insn |= (old_op & 7) << 12;
18874               insn |= (old_op & 0x38) << 13;
18875             }
18876           insn |= 0x00000c00;
18877           put_thumb32_insn (buf, insn);
18878           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
18879         }
18880       else
18881         {
18882           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
18883         }
18884       pc_rel = (opcode == T_MNEM_ldr_pc2);
18885       break;
18886     case T_MNEM_adr:
18887       if (fragp->fr_var == 4)
18888         {
18889           insn = THUMB_OP32 (opcode);
18890           insn |= (old_op & 0xf0) << 4;
18891           put_thumb32_insn (buf, insn);
18892           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
18893         }
18894       else
18895         {
18896           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
18897           exp.X_add_number -= 4;
18898         }
18899       pc_rel = 1;
18900       break;
18901     case T_MNEM_mov:
18902     case T_MNEM_movs:
18903     case T_MNEM_cmp:
18904     case T_MNEM_cmn:
18905       if (fragp->fr_var == 4)
18906         {
18907           int r0off = (opcode == T_MNEM_mov
18908                        || opcode == T_MNEM_movs) ? 0 : 8;
18909           insn = THUMB_OP32 (opcode);
18910           insn = (insn & 0xe1ffffff) | 0x10000000;
18911           insn |= (old_op & 0x700) << r0off;
18912           put_thumb32_insn (buf, insn);
18913           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
18914         }
18915       else
18916         {
18917           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
18918         }
18919       pc_rel = 0;
18920       break;
18921     case T_MNEM_b:
18922       if (fragp->fr_var == 4)
18923         {
18924           insn = THUMB_OP32(opcode);
18925           put_thumb32_insn (buf, insn);
18926           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
18927         }
18928       else
18929         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
18930       pc_rel = 1;
18931       break;
18932     case T_MNEM_bcond:
18933       if (fragp->fr_var == 4)
18934         {
18935           insn = THUMB_OP32(opcode);
18936           insn |= (old_op & 0xf00) << 14;
18937           put_thumb32_insn (buf, insn);
18938           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
18939         }
18940       else
18941         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
18942       pc_rel = 1;
18943       break;
18944     case T_MNEM_add_sp:
18945     case T_MNEM_add_pc:
18946     case T_MNEM_inc_sp:
18947     case T_MNEM_dec_sp:
18948       if (fragp->fr_var == 4)
18949         {
18950           /* ??? Choose between add and addw.  */
18951           insn = THUMB_OP32 (opcode);
18952           insn |= (old_op & 0xf0) << 4;
18953           put_thumb32_insn (buf, insn);
18954           if (opcode == T_MNEM_add_pc)
18955             reloc_type = BFD_RELOC_ARM_T32_IMM12;
18956           else
18957             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
18958         }
18959       else
18960         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
18961       pc_rel = 0;
18962       break;
18963
18964     case T_MNEM_addi:
18965     case T_MNEM_addis:
18966     case T_MNEM_subi:
18967     case T_MNEM_subis:
18968       if (fragp->fr_var == 4)
18969         {
18970           insn = THUMB_OP32 (opcode);
18971           insn |= (old_op & 0xf0) << 4;
18972           insn |= (old_op & 0xf) << 16;
18973           put_thumb32_insn (buf, insn);
18974           if (insn & (1 << 20))
18975             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
18976           else
18977             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
18978         }
18979       else
18980         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
18981       pc_rel = 0;
18982       break;
18983     default:
18984       abort ();
18985     }
18986   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
18987                       (enum bfd_reloc_code_real) reloc_type);
18988   fixp->fx_file = fragp->fr_file;
18989   fixp->fx_line = fragp->fr_line;
18990   fragp->fr_fix += fragp->fr_var;
18991 }
18992
18993 /* Return the size of a relaxable immediate operand instruction.
18994    SHIFT and SIZE specify the form of the allowable immediate.  */
18995 static int
18996 relax_immediate (fragS *fragp, int size, int shift)
18997 {
18998   offsetT offset;
18999   offsetT mask;
19000   offsetT low;
19001
19002   /* ??? Should be able to do better than this.  */
19003   if (fragp->fr_symbol)
19004     return 4;
19005
19006   low = (1 << shift) - 1;
19007   mask = (1 << (shift + size)) - (1 << shift);
19008   offset = fragp->fr_offset;
19009   /* Force misaligned offsets to 32-bit variant.  */
19010   if (offset & low)
19011     return 4;
19012   if (offset & ~mask)
19013     return 4;
19014   return 2;
19015 }
19016
19017 /* Get the address of a symbol during relaxation.  */
19018 static addressT
19019 relaxed_symbol_addr (fragS *fragp, long stretch)
19020 {
19021   fragS *sym_frag;
19022   addressT addr;
19023   symbolS *sym;
19024
19025   sym = fragp->fr_symbol;
19026   sym_frag = symbol_get_frag (sym);
19027   know (S_GET_SEGMENT (sym) != absolute_section
19028         || sym_frag == &zero_address_frag);
19029   addr = S_GET_VALUE (sym) + fragp->fr_offset;
19030
19031   /* If frag has yet to be reached on this pass, assume it will
19032      move by STRETCH just as we did.  If this is not so, it will
19033      be because some frag between grows, and that will force
19034      another pass.  */
19035
19036   if (stretch != 0
19037       && sym_frag->relax_marker != fragp->relax_marker)
19038     {
19039       fragS *f;
19040
19041       /* Adjust stretch for any alignment frag.  Note that if have
19042          been expanding the earlier code, the symbol may be
19043          defined in what appears to be an earlier frag.  FIXME:
19044          This doesn't handle the fr_subtype field, which specifies
19045          a maximum number of bytes to skip when doing an
19046          alignment.  */
19047       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
19048         {
19049           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
19050             {
19051               if (stretch < 0)
19052                 stretch = - ((- stretch)
19053                              & ~ ((1 << (int) f->fr_offset) - 1));
19054               else
19055                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
19056               if (stretch == 0)
19057                 break;
19058             }
19059         }
19060       if (f != NULL)
19061         addr += stretch;
19062     }
19063
19064   return addr;
19065 }
19066
19067 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
19068    load.  */
19069 static int
19070 relax_adr (fragS *fragp, asection *sec, long stretch)
19071 {
19072   addressT addr;
19073   offsetT val;
19074
19075   /* Assume worst case for symbols not known to be in the same section.  */
19076   if (fragp->fr_symbol == NULL
19077       || !S_IS_DEFINED (fragp->fr_symbol)
19078       || sec != S_GET_SEGMENT (fragp->fr_symbol)
19079       || S_IS_WEAK (fragp->fr_symbol))
19080     return 4;
19081
19082   val = relaxed_symbol_addr (fragp, stretch);
19083   addr = fragp->fr_address + fragp->fr_fix;
19084   addr = (addr + 4) & ~3;
19085   /* Force misaligned targets to 32-bit variant.  */
19086   if (val & 3)
19087     return 4;
19088   val -= addr;
19089   if (val < 0 || val > 1020)
19090     return 4;
19091   return 2;
19092 }
19093
19094 /* Return the size of a relaxable add/sub immediate instruction.  */
19095 static int
19096 relax_addsub (fragS *fragp, asection *sec)
19097 {
19098   char *buf;
19099   int op;
19100
19101   buf = fragp->fr_literal + fragp->fr_fix;
19102   op = bfd_get_16(sec->owner, buf);
19103   if ((op & 0xf) == ((op >> 4) & 0xf))
19104     return relax_immediate (fragp, 8, 0);
19105   else
19106     return relax_immediate (fragp, 3, 0);
19107 }
19108
19109
19110 /* Return the size of a relaxable branch instruction.  BITS is the
19111    size of the offset field in the narrow instruction.  */
19112
19113 static int
19114 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
19115 {
19116   addressT addr;
19117   offsetT val;
19118   offsetT limit;
19119
19120   /* Assume worst case for symbols not known to be in the same section.  */
19121   if (!S_IS_DEFINED (fragp->fr_symbol)
19122       || sec != S_GET_SEGMENT (fragp->fr_symbol)
19123       || S_IS_WEAK (fragp->fr_symbol))
19124     return 4;
19125
19126 #ifdef OBJ_ELF
19127   if (S_IS_DEFINED (fragp->fr_symbol)
19128       && ARM_IS_FUNC (fragp->fr_symbol))
19129       return 4;
19130
19131   /* PR 12532.  Global symbols with default visibility might
19132      be preempted, so do not relax relocations to them.  */
19133   if ((ELF_ST_VISIBILITY (S_GET_OTHER (fragp->fr_symbol)) == STV_DEFAULT)
19134       && (! S_IS_LOCAL (fragp->fr_symbol)))
19135     return 4;
19136 #endif
19137
19138   val = relaxed_symbol_addr (fragp, stretch);
19139   addr = fragp->fr_address + fragp->fr_fix + 4;
19140   val -= addr;
19141
19142   /* Offset is a signed value *2 */
19143   limit = 1 << bits;
19144   if (val >= limit || val < -limit)
19145     return 4;
19146   return 2;
19147 }
19148
19149
19150 /* Relax a machine dependent frag.  This returns the amount by which
19151    the current size of the frag should change.  */
19152
19153 int
19154 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
19155 {
19156   int oldsize;
19157   int newsize;
19158
19159   oldsize = fragp->fr_var;
19160   switch (fragp->fr_subtype)
19161     {
19162     case T_MNEM_ldr_pc2:
19163       newsize = relax_adr (fragp, sec, stretch);
19164       break;
19165     case T_MNEM_ldr_pc:
19166     case T_MNEM_ldr_sp:
19167     case T_MNEM_str_sp:
19168       newsize = relax_immediate (fragp, 8, 2);
19169       break;
19170     case T_MNEM_ldr:
19171     case T_MNEM_str:
19172       newsize = relax_immediate (fragp, 5, 2);
19173       break;
19174     case T_MNEM_ldrh:
19175     case T_MNEM_strh:
19176       newsize = relax_immediate (fragp, 5, 1);
19177       break;
19178     case T_MNEM_ldrb:
19179     case T_MNEM_strb:
19180       newsize = relax_immediate (fragp, 5, 0);
19181       break;
19182     case T_MNEM_adr:
19183       newsize = relax_adr (fragp, sec, stretch);
19184       break;
19185     case T_MNEM_mov:
19186     case T_MNEM_movs:
19187     case T_MNEM_cmp:
19188     case T_MNEM_cmn:
19189       newsize = relax_immediate (fragp, 8, 0);
19190       break;
19191     case T_MNEM_b:
19192       newsize = relax_branch (fragp, sec, 11, stretch);
19193       break;
19194     case T_MNEM_bcond:
19195       newsize = relax_branch (fragp, sec, 8, stretch);
19196       break;
19197     case T_MNEM_add_sp:
19198     case T_MNEM_add_pc:
19199       newsize = relax_immediate (fragp, 8, 2);
19200       break;
19201     case T_MNEM_inc_sp:
19202     case T_MNEM_dec_sp:
19203       newsize = relax_immediate (fragp, 7, 2);
19204       break;
19205     case T_MNEM_addi:
19206     case T_MNEM_addis:
19207     case T_MNEM_subi:
19208     case T_MNEM_subis:
19209       newsize = relax_addsub (fragp, sec);
19210       break;
19211     default:
19212       abort ();
19213     }
19214
19215   fragp->fr_var = newsize;
19216   /* Freeze wide instructions that are at or before the same location as
19217      in the previous pass.  This avoids infinite loops.
19218      Don't freeze them unconditionally because targets may be artificially
19219      misaligned by the expansion of preceding frags.  */
19220   if (stretch <= 0 && newsize > 2)
19221     {
19222       md_convert_frag (sec->owner, sec, fragp);
19223       frag_wane (fragp);
19224     }
19225
19226   return newsize - oldsize;
19227 }
19228
19229 /* Round up a section size to the appropriate boundary.  */
19230
19231 valueT
19232 md_section_align (segT   segment ATTRIBUTE_UNUSED,
19233                   valueT size)
19234 {
19235 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
19236   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
19237     {
19238       /* For a.out, force the section size to be aligned.  If we don't do
19239          this, BFD will align it for us, but it will not write out the
19240          final bytes of the section.  This may be a bug in BFD, but it is
19241          easier to fix it here since that is how the other a.out targets
19242          work.  */
19243       int align;
19244
19245       align = bfd_get_section_alignment (stdoutput, segment);
19246       size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
19247     }
19248 #endif
19249
19250   return size;
19251 }
19252
19253 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
19254    of an rs_align_code fragment.  */
19255
19256 void
19257 arm_handle_align (fragS * fragP)
19258 {
19259   static char const arm_noop[2][2][4] =
19260     {
19261       {  /* ARMv1 */
19262         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
19263         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
19264       },
19265       {  /* ARMv6k */
19266         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
19267         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
19268       },
19269     };
19270   static char const thumb_noop[2][2][2] =
19271     {
19272       {  /* Thumb-1 */
19273         {0xc0, 0x46},  /* LE */
19274         {0x46, 0xc0},  /* BE */
19275       },
19276       {  /* Thumb-2 */
19277         {0x00, 0xbf},  /* LE */
19278         {0xbf, 0x00}   /* BE */
19279       }
19280     };
19281   static char const wide_thumb_noop[2][4] =
19282     {  /* Wide Thumb-2 */
19283       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
19284       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
19285     };
19286
19287   unsigned bytes, fix, noop_size;
19288   char * p;
19289   const char * noop;
19290   const char *narrow_noop = NULL;
19291 #ifdef OBJ_ELF
19292   enum mstate state;
19293 #endif
19294
19295   if (fragP->fr_type != rs_align_code)
19296     return;
19297
19298   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
19299   p = fragP->fr_literal + fragP->fr_fix;
19300   fix = 0;
19301
19302   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
19303     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
19304
19305   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
19306
19307   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
19308     {
19309       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
19310         {
19311           narrow_noop = thumb_noop[1][target_big_endian];
19312           noop = wide_thumb_noop[target_big_endian];
19313         }
19314       else
19315         noop = thumb_noop[0][target_big_endian];
19316       noop_size = 2;
19317 #ifdef OBJ_ELF
19318       state = MAP_THUMB;
19319 #endif
19320     }
19321   else
19322     {
19323       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k) != 0]
19324                      [target_big_endian];
19325       noop_size = 4;
19326 #ifdef OBJ_ELF
19327       state = MAP_ARM;
19328 #endif
19329     }
19330
19331   fragP->fr_var = noop_size;
19332
19333   if (bytes & (noop_size - 1))
19334     {
19335       fix = bytes & (noop_size - 1);
19336 #ifdef OBJ_ELF
19337       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
19338 #endif
19339       memset (p, 0, fix);
19340       p += fix;
19341       bytes -= fix;
19342     }
19343
19344   if (narrow_noop)
19345     {
19346       if (bytes & noop_size)
19347         {
19348           /* Insert a narrow noop.  */
19349           memcpy (p, narrow_noop, noop_size);
19350           p += noop_size;
19351           bytes -= noop_size;
19352           fix += noop_size;
19353         }
19354
19355       /* Use wide noops for the remainder */
19356       noop_size = 4;
19357     }
19358
19359   while (bytes >= noop_size)
19360     {
19361       memcpy (p, noop, noop_size);
19362       p += noop_size;
19363       bytes -= noop_size;
19364       fix += noop_size;
19365     }
19366
19367   fragP->fr_fix += fix;
19368 }
19369
19370 /* Called from md_do_align.  Used to create an alignment
19371    frag in a code section.  */
19372
19373 void
19374 arm_frag_align_code (int n, int max)
19375 {
19376   char * p;
19377
19378   /* We assume that there will never be a requirement
19379      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
19380   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
19381     {
19382       char err_msg[128];
19383
19384       sprintf (err_msg, 
19385         _("alignments greater than %d bytes not supported in .text sections."),
19386         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
19387       as_fatal ("%s", err_msg);
19388     }
19389
19390   p = frag_var (rs_align_code,
19391                 MAX_MEM_FOR_RS_ALIGN_CODE,
19392                 1,
19393                 (relax_substateT) max,
19394                 (symbolS *) NULL,
19395                 (offsetT) n,
19396                 (char *) NULL);
19397   *p = 0;
19398 }
19399
19400 /* Perform target specific initialisation of a frag.
19401    Note - despite the name this initialisation is not done when the frag
19402    is created, but only when its type is assigned.  A frag can be created
19403    and used a long time before its type is set, so beware of assuming that
19404    this initialisationis performed first.  */
19405
19406 #ifndef OBJ_ELF
19407 void
19408 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
19409 {
19410   /* Record whether this frag is in an ARM or a THUMB area.  */
19411   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
19412 }
19413
19414 #else /* OBJ_ELF is defined.  */
19415 void
19416 arm_init_frag (fragS * fragP, int max_chars)
19417 {
19418   /* If the current ARM vs THUMB mode has not already
19419      been recorded into this frag then do so now.  */
19420   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
19421     {
19422       fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
19423
19424       /* Record a mapping symbol for alignment frags.  We will delete this
19425          later if the alignment ends up empty.  */
19426       switch (fragP->fr_type)
19427         {
19428           case rs_align:
19429           case rs_align_test:
19430           case rs_fill:
19431             mapping_state_2 (MAP_DATA, max_chars);
19432             break;
19433           case rs_align_code:
19434             mapping_state_2 (thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
19435             break;
19436           default:
19437             break;
19438         }
19439     }
19440 }
19441
19442 /* When we change sections we need to issue a new mapping symbol.  */
19443
19444 void
19445 arm_elf_change_section (void)
19446 {
19447   /* Link an unlinked unwind index table section to the .text section.  */
19448   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
19449       && elf_linked_to_section (now_seg) == NULL)
19450     elf_linked_to_section (now_seg) = text_section;
19451 }
19452
19453 int
19454 arm_elf_section_type (const char * str, size_t len)
19455 {
19456   if (len == 5 && strncmp (str, "exidx", 5) == 0)
19457     return SHT_ARM_EXIDX;
19458
19459   return -1;
19460 }
19461 \f
19462 /* Code to deal with unwinding tables.  */
19463
19464 static void add_unwind_adjustsp (offsetT);
19465
19466 /* Generate any deferred unwind frame offset.  */
19467
19468 static void
19469 flush_pending_unwind (void)
19470 {
19471   offsetT offset;
19472
19473   offset = unwind.pending_offset;
19474   unwind.pending_offset = 0;
19475   if (offset != 0)
19476     add_unwind_adjustsp (offset);
19477 }
19478
19479 /* Add an opcode to this list for this function.  Two-byte opcodes should
19480    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
19481    order.  */
19482
19483 static void
19484 add_unwind_opcode (valueT op, int length)
19485 {
19486   /* Add any deferred stack adjustment.  */
19487   if (unwind.pending_offset)
19488     flush_pending_unwind ();
19489
19490   unwind.sp_restored = 0;
19491
19492   if (unwind.opcode_count + length > unwind.opcode_alloc)
19493     {
19494       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
19495       if (unwind.opcodes)
19496         unwind.opcodes = (unsigned char *) xrealloc (unwind.opcodes,
19497                                                      unwind.opcode_alloc);
19498       else
19499         unwind.opcodes = (unsigned char *) xmalloc (unwind.opcode_alloc);
19500     }
19501   while (length > 0)
19502     {
19503       length--;
19504       unwind.opcodes[unwind.opcode_count] = op & 0xff;
19505       op >>= 8;
19506       unwind.opcode_count++;
19507     }
19508 }
19509
19510 /* Add unwind opcodes to adjust the stack pointer.  */
19511
19512 static void
19513 add_unwind_adjustsp (offsetT offset)
19514 {
19515   valueT op;
19516
19517   if (offset > 0x200)
19518     {
19519       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
19520       char bytes[5];
19521       int n;
19522       valueT o;
19523
19524       /* Long form: 0xb2, uleb128.  */
19525       /* This might not fit in a word so add the individual bytes,
19526          remembering the list is built in reverse order.  */
19527       o = (valueT) ((offset - 0x204) >> 2);
19528       if (o == 0)
19529         add_unwind_opcode (0, 1);
19530
19531       /* Calculate the uleb128 encoding of the offset.  */
19532       n = 0;
19533       while (o)
19534         {
19535           bytes[n] = o & 0x7f;
19536           o >>= 7;
19537           if (o)
19538             bytes[n] |= 0x80;
19539           n++;
19540         }
19541       /* Add the insn.  */
19542       for (; n; n--)
19543         add_unwind_opcode (bytes[n - 1], 1);
19544       add_unwind_opcode (0xb2, 1);
19545     }
19546   else if (offset > 0x100)
19547     {
19548       /* Two short opcodes.  */
19549       add_unwind_opcode (0x3f, 1);
19550       op = (offset - 0x104) >> 2;
19551       add_unwind_opcode (op, 1);
19552     }
19553   else if (offset > 0)
19554     {
19555       /* Short opcode.  */
19556       op = (offset - 4) >> 2;
19557       add_unwind_opcode (op, 1);
19558     }
19559   else if (offset < 0)
19560     {
19561       offset = -offset;
19562       while (offset > 0x100)
19563         {
19564           add_unwind_opcode (0x7f, 1);
19565           offset -= 0x100;
19566         }
19567       op = ((offset - 4) >> 2) | 0x40;
19568       add_unwind_opcode (op, 1);
19569     }
19570 }
19571
19572 /* Finish the list of unwind opcodes for this function.  */
19573 static void
19574 finish_unwind_opcodes (void)
19575 {
19576   valueT op;
19577
19578   if (unwind.fp_used)
19579     {
19580       /* Adjust sp as necessary.  */
19581       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
19582       flush_pending_unwind ();
19583
19584       /* After restoring sp from the frame pointer.  */
19585       op = 0x90 | unwind.fp_reg;
19586       add_unwind_opcode (op, 1);
19587     }
19588   else
19589     flush_pending_unwind ();
19590 }
19591
19592
19593 /* Start an exception table entry.  If idx is nonzero this is an index table
19594    entry.  */
19595
19596 static void
19597 start_unwind_section (const segT text_seg, int idx)
19598 {
19599   const char * text_name;
19600   const char * prefix;
19601   const char * prefix_once;
19602   const char * group_name;
19603   size_t prefix_len;
19604   size_t text_len;
19605   char * sec_name;
19606   size_t sec_name_len;
19607   int type;
19608   int flags;
19609   int linkonce;
19610
19611   if (idx)
19612     {
19613       prefix = ELF_STRING_ARM_unwind;
19614       prefix_once = ELF_STRING_ARM_unwind_once;
19615       type = SHT_ARM_EXIDX;
19616     }
19617   else
19618     {
19619       prefix = ELF_STRING_ARM_unwind_info;
19620       prefix_once = ELF_STRING_ARM_unwind_info_once;
19621       type = SHT_PROGBITS;
19622     }
19623
19624   text_name = segment_name (text_seg);
19625   if (streq (text_name, ".text"))
19626     text_name = "";
19627
19628   if (strncmp (text_name, ".gnu.linkonce.t.",
19629                strlen (".gnu.linkonce.t.")) == 0)
19630     {
19631       prefix = prefix_once;
19632       text_name += strlen (".gnu.linkonce.t.");
19633     }
19634
19635   prefix_len = strlen (prefix);
19636   text_len = strlen (text_name);
19637   sec_name_len = prefix_len + text_len;
19638   sec_name = (char *) xmalloc (sec_name_len + 1);
19639   memcpy (sec_name, prefix, prefix_len);
19640   memcpy (sec_name + prefix_len, text_name, text_len);
19641   sec_name[prefix_len + text_len] = '\0';
19642
19643   flags = SHF_ALLOC;
19644   linkonce = 0;
19645   group_name = 0;
19646
19647   /* Handle COMDAT group.  */
19648   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
19649     {
19650       group_name = elf_group_name (text_seg);
19651       if (group_name == NULL)
19652         {
19653           as_bad (_("Group section `%s' has no group signature"),
19654                   segment_name (text_seg));
19655           ignore_rest_of_line ();
19656           return;
19657         }
19658       flags |= SHF_GROUP;
19659       linkonce = 1;
19660     }
19661
19662   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
19663
19664   /* Set the section link for index tables.  */
19665   if (idx)
19666     elf_linked_to_section (now_seg) = text_seg;
19667 }
19668
19669
19670 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
19671    personality routine data.  Returns zero, or the index table value for
19672    and inline entry.  */
19673
19674 static valueT
19675 create_unwind_entry (int have_data)
19676 {
19677   int size;
19678   addressT where;
19679   char *ptr;
19680   /* The current word of data.  */
19681   valueT data;
19682   /* The number of bytes left in this word.  */
19683   int n;
19684
19685   finish_unwind_opcodes ();
19686
19687   /* Remember the current text section.  */
19688   unwind.saved_seg = now_seg;
19689   unwind.saved_subseg = now_subseg;
19690
19691   start_unwind_section (now_seg, 0);
19692
19693   if (unwind.personality_routine == NULL)
19694     {
19695       if (unwind.personality_index == -2)
19696         {
19697           if (have_data)
19698             as_bad (_("handlerdata in cantunwind frame"));
19699           return 1; /* EXIDX_CANTUNWIND.  */
19700         }
19701
19702       /* Use a default personality routine if none is specified.  */
19703       if (unwind.personality_index == -1)
19704         {
19705           if (unwind.opcode_count > 3)
19706             unwind.personality_index = 1;
19707           else
19708             unwind.personality_index = 0;
19709         }
19710
19711       /* Space for the personality routine entry.  */
19712       if (unwind.personality_index == 0)
19713         {
19714           if (unwind.opcode_count > 3)
19715             as_bad (_("too many unwind opcodes for personality routine 0"));
19716
19717           if (!have_data)
19718             {
19719               /* All the data is inline in the index table.  */
19720               data = 0x80;
19721               n = 3;
19722               while (unwind.opcode_count > 0)
19723                 {
19724                   unwind.opcode_count--;
19725                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
19726                   n--;
19727                 }
19728
19729               /* Pad with "finish" opcodes.  */
19730               while (n--)
19731                 data = (data << 8) | 0xb0;
19732
19733               return data;
19734             }
19735           size = 0;
19736         }
19737       else
19738         /* We get two opcodes "free" in the first word.  */
19739         size = unwind.opcode_count - 2;
19740     }
19741   else
19742     /* An extra byte is required for the opcode count.  */
19743     size = unwind.opcode_count + 1;
19744
19745   size = (size + 3) >> 2;
19746   if (size > 0xff)
19747     as_bad (_("too many unwind opcodes"));
19748
19749   frag_align (2, 0, 0);
19750   record_alignment (now_seg, 2);
19751   unwind.table_entry = expr_build_dot ();
19752
19753   /* Allocate the table entry.  */
19754   ptr = frag_more ((size << 2) + 4);
19755   where = frag_now_fix () - ((size << 2) + 4);
19756
19757   switch (unwind.personality_index)
19758     {
19759     case -1:
19760       /* ??? Should this be a PLT generating relocation?  */
19761       /* Custom personality routine.  */
19762       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
19763                BFD_RELOC_ARM_PREL31);
19764
19765       where += 4;
19766       ptr += 4;
19767
19768       /* Set the first byte to the number of additional words.  */
19769       data = size - 1;
19770       n = 3;
19771       break;
19772
19773     /* ABI defined personality routines.  */
19774     case 0:
19775       /* Three opcodes bytes are packed into the first word.  */
19776       data = 0x80;
19777       n = 3;
19778       break;
19779
19780     case 1:
19781     case 2:
19782       /* The size and first two opcode bytes go in the first word.  */
19783       data = ((0x80 + unwind.personality_index) << 8) | size;
19784       n = 2;
19785       break;
19786
19787     default:
19788       /* Should never happen.  */
19789       abort ();
19790     }
19791
19792   /* Pack the opcodes into words (MSB first), reversing the list at the same
19793      time.  */
19794   while (unwind.opcode_count > 0)
19795     {
19796       if (n == 0)
19797         {
19798           md_number_to_chars (ptr, data, 4);
19799           ptr += 4;
19800           n = 4;
19801           data = 0;
19802         }
19803       unwind.opcode_count--;
19804       n--;
19805       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
19806     }
19807
19808   /* Finish off the last word.  */
19809   if (n < 4)
19810     {
19811       /* Pad with "finish" opcodes.  */
19812       while (n--)
19813         data = (data << 8) | 0xb0;
19814
19815       md_number_to_chars (ptr, data, 4);
19816     }
19817
19818   if (!have_data)
19819     {
19820       /* Add an empty descriptor if there is no user-specified data.   */
19821       ptr = frag_more (4);
19822       md_number_to_chars (ptr, 0, 4);
19823     }
19824
19825   return 0;
19826 }
19827
19828
19829 /* Initialize the DWARF-2 unwind information for this procedure.  */
19830
19831 void
19832 tc_arm_frame_initial_instructions (void)
19833 {
19834   cfi_add_CFA_def_cfa (REG_SP, 0);
19835 }
19836 #endif /* OBJ_ELF */
19837
19838 /* Convert REGNAME to a DWARF-2 register number.  */
19839
19840 int
19841 tc_arm_regname_to_dw2regnum (char *regname)
19842 {
19843   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
19844
19845   if (reg == FAIL)
19846     return -1;
19847
19848   return reg;
19849 }
19850
19851 #ifdef TE_PE
19852 void
19853 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
19854 {
19855   expressionS exp;
19856
19857   exp.X_op = O_secrel;
19858   exp.X_add_symbol = symbol;
19859   exp.X_add_number = 0;
19860   emit_expr (&exp, size);
19861 }
19862 #endif
19863
19864 /* MD interface: Symbol and relocation handling.  */
19865
19866 /* Return the address within the segment that a PC-relative fixup is
19867    relative to.  For ARM, PC-relative fixups applied to instructions
19868    are generally relative to the location of the fixup plus 8 bytes.
19869    Thumb branches are offset by 4, and Thumb loads relative to PC
19870    require special handling.  */
19871
19872 long
19873 md_pcrel_from_section (fixS * fixP, segT seg)
19874 {
19875   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
19876
19877   /* If this is pc-relative and we are going to emit a relocation
19878      then we just want to put out any pipeline compensation that the linker
19879      will need.  Otherwise we want to use the calculated base.
19880      For WinCE we skip the bias for externals as well, since this
19881      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
19882   if (fixP->fx_pcrel
19883       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
19884           || (arm_force_relocation (fixP)
19885 #ifdef TE_WINCE
19886               && !S_IS_EXTERNAL (fixP->fx_addsy)
19887 #endif
19888               )))
19889     base = 0;
19890
19891
19892   switch (fixP->fx_r_type)
19893     {
19894       /* PC relative addressing on the Thumb is slightly odd as the
19895          bottom two bits of the PC are forced to zero for the
19896          calculation.  This happens *after* application of the
19897          pipeline offset.  However, Thumb adrl already adjusts for
19898          this, so we need not do it again.  */
19899     case BFD_RELOC_ARM_THUMB_ADD:
19900       return base & ~3;
19901
19902     case BFD_RELOC_ARM_THUMB_OFFSET:
19903     case BFD_RELOC_ARM_T32_OFFSET_IMM:
19904     case BFD_RELOC_ARM_T32_ADD_PC12:
19905     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
19906       return (base + 4) & ~3;
19907
19908       /* Thumb branches are simply offset by +4.  */
19909     case BFD_RELOC_THUMB_PCREL_BRANCH7:
19910     case BFD_RELOC_THUMB_PCREL_BRANCH9:
19911     case BFD_RELOC_THUMB_PCREL_BRANCH12:
19912     case BFD_RELOC_THUMB_PCREL_BRANCH20:
19913     case BFD_RELOC_THUMB_PCREL_BRANCH25:
19914       return base + 4;
19915
19916     case BFD_RELOC_THUMB_PCREL_BRANCH23:
19917       if (fixP->fx_addsy
19918           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
19919           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
19920           && ARM_IS_FUNC (fixP->fx_addsy)
19921           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
19922         base = fixP->fx_where + fixP->fx_frag->fr_address;
19923        return base + 4;
19924
19925       /* BLX is like branches above, but forces the low two bits of PC to
19926          zero.  */
19927     case BFD_RELOC_THUMB_PCREL_BLX:
19928       if (fixP->fx_addsy
19929           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
19930           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
19931           && THUMB_IS_FUNC (fixP->fx_addsy)
19932           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
19933         base = fixP->fx_where + fixP->fx_frag->fr_address;
19934       return (base + 4) & ~3;
19935
19936       /* ARM mode branches are offset by +8.  However, the Windows CE
19937          loader expects the relocation not to take this into account.  */
19938     case BFD_RELOC_ARM_PCREL_BLX:
19939       if (fixP->fx_addsy
19940           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
19941           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
19942           && ARM_IS_FUNC (fixP->fx_addsy)
19943           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
19944         base = fixP->fx_where + fixP->fx_frag->fr_address;
19945       return base + 8;
19946
19947     case BFD_RELOC_ARM_PCREL_CALL:
19948       if (fixP->fx_addsy
19949           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
19950           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
19951           && THUMB_IS_FUNC (fixP->fx_addsy)
19952           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
19953         base = fixP->fx_where + fixP->fx_frag->fr_address;
19954       return base + 8;
19955
19956     case BFD_RELOC_ARM_PCREL_BRANCH:
19957     case BFD_RELOC_ARM_PCREL_JUMP:
19958     case BFD_RELOC_ARM_PLT32:
19959 #ifdef TE_WINCE
19960       /* When handling fixups immediately, because we have already
19961          discovered the value of a symbol, or the address of the frag involved
19962          we must account for the offset by +8, as the OS loader will never see the reloc.
19963          see fixup_segment() in write.c
19964          The S_IS_EXTERNAL test handles the case of global symbols.
19965          Those need the calculated base, not just the pipe compensation the linker will need.  */
19966       if (fixP->fx_pcrel
19967           && fixP->fx_addsy != NULL
19968           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
19969           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
19970         return base + 8;
19971       return base;
19972 #else
19973       return base + 8;
19974 #endif
19975
19976
19977       /* ARM mode loads relative to PC are also offset by +8.  Unlike
19978          branches, the Windows CE loader *does* expect the relocation
19979          to take this into account.  */
19980     case BFD_RELOC_ARM_OFFSET_IMM:
19981     case BFD_RELOC_ARM_OFFSET_IMM8:
19982     case BFD_RELOC_ARM_HWLITERAL:
19983     case BFD_RELOC_ARM_LITERAL:
19984     case BFD_RELOC_ARM_CP_OFF_IMM:
19985       return base + 8;
19986
19987
19988       /* Other PC-relative relocations are un-offset.  */
19989     default:
19990       return base;
19991     }
19992 }
19993
19994 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
19995    Otherwise we have no need to default values of symbols.  */
19996
19997 symbolS *
19998 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
19999 {
20000 #ifdef OBJ_ELF
20001   if (name[0] == '_' && name[1] == 'G'
20002       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
20003     {
20004       if (!GOT_symbol)
20005         {
20006           if (symbol_find (name))
20007             as_bad (_("GOT already in the symbol table"));
20008
20009           GOT_symbol = symbol_new (name, undefined_section,
20010                                    (valueT) 0, & zero_address_frag);
20011         }
20012
20013       return GOT_symbol;
20014     }
20015 #endif
20016
20017   return NULL;
20018 }
20019
20020 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
20021    computed as two separate immediate values, added together.  We
20022    already know that this value cannot be computed by just one ARM
20023    instruction.  */
20024
20025 static unsigned int
20026 validate_immediate_twopart (unsigned int   val,
20027                             unsigned int * highpart)
20028 {
20029   unsigned int a;
20030   unsigned int i;
20031
20032   for (i = 0; i < 32; i += 2)
20033     if (((a = rotate_left (val, i)) & 0xff) != 0)
20034       {
20035         if (a & 0xff00)
20036           {
20037             if (a & ~ 0xffff)
20038               continue;
20039             * highpart = (a  >> 8) | ((i + 24) << 7);
20040           }
20041         else if (a & 0xff0000)
20042           {
20043             if (a & 0xff000000)
20044               continue;
20045             * highpart = (a >> 16) | ((i + 16) << 7);
20046           }
20047         else
20048           {
20049             gas_assert (a & 0xff000000);
20050             * highpart = (a >> 24) | ((i + 8) << 7);
20051           }
20052
20053         return (a & 0xff) | (i << 7);
20054       }
20055
20056   return FAIL;
20057 }
20058
20059 static int
20060 validate_offset_imm (unsigned int val, int hwse)
20061 {
20062   if ((hwse && val > 255) || val > 4095)
20063     return FAIL;
20064   return val;
20065 }
20066
20067 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
20068    negative immediate constant by altering the instruction.  A bit of
20069    a hack really.
20070         MOV <-> MVN
20071         AND <-> BIC
20072         ADC <-> SBC
20073         by inverting the second operand, and
20074         ADD <-> SUB
20075         CMP <-> CMN
20076         by negating the second operand.  */
20077
20078 static int
20079 negate_data_op (unsigned long * instruction,
20080                 unsigned long   value)
20081 {
20082   int op, new_inst;
20083   unsigned long negated, inverted;
20084
20085   negated = encode_arm_immediate (-value);
20086   inverted = encode_arm_immediate (~value);
20087
20088   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
20089   switch (op)
20090     {
20091       /* First negates.  */
20092     case OPCODE_SUB:             /* ADD <-> SUB  */
20093       new_inst = OPCODE_ADD;
20094       value = negated;
20095       break;
20096
20097     case OPCODE_ADD:
20098       new_inst = OPCODE_SUB;
20099       value = negated;
20100       break;
20101
20102     case OPCODE_CMP:             /* CMP <-> CMN  */
20103       new_inst = OPCODE_CMN;
20104       value = negated;
20105       break;
20106
20107     case OPCODE_CMN:
20108       new_inst = OPCODE_CMP;
20109       value = negated;
20110       break;
20111
20112       /* Now Inverted ops.  */
20113     case OPCODE_MOV:             /* MOV <-> MVN  */
20114       new_inst = OPCODE_MVN;
20115       value = inverted;
20116       break;
20117
20118     case OPCODE_MVN:
20119       new_inst = OPCODE_MOV;
20120       value = inverted;
20121       break;
20122
20123     case OPCODE_AND:             /* AND <-> BIC  */
20124       new_inst = OPCODE_BIC;
20125       value = inverted;
20126       break;
20127
20128     case OPCODE_BIC:
20129       new_inst = OPCODE_AND;
20130       value = inverted;
20131       break;
20132
20133     case OPCODE_ADC:              /* ADC <-> SBC  */
20134       new_inst = OPCODE_SBC;
20135       value = inverted;
20136       break;
20137
20138     case OPCODE_SBC:
20139       new_inst = OPCODE_ADC;
20140       value = inverted;
20141       break;
20142
20143       /* We cannot do anything.  */
20144     default:
20145       return FAIL;
20146     }
20147
20148   if (value == (unsigned) FAIL)
20149     return FAIL;
20150
20151   *instruction &= OPCODE_MASK;
20152   *instruction |= new_inst << DATA_OP_SHIFT;
20153   return value;
20154 }
20155
20156 /* Like negate_data_op, but for Thumb-2.   */
20157
20158 static unsigned int
20159 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
20160 {
20161   int op, new_inst;
20162   int rd;
20163   unsigned int negated, inverted;
20164
20165   negated = encode_thumb32_immediate (-value);
20166   inverted = encode_thumb32_immediate (~value);
20167
20168   rd = (*instruction >> 8) & 0xf;
20169   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
20170   switch (op)
20171     {
20172       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
20173     case T2_OPCODE_SUB:
20174       new_inst = T2_OPCODE_ADD;
20175       value = negated;
20176       break;
20177
20178     case T2_OPCODE_ADD:
20179       new_inst = T2_OPCODE_SUB;
20180       value = negated;
20181       break;
20182
20183       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
20184     case T2_OPCODE_ORR:
20185       new_inst = T2_OPCODE_ORN;
20186       value = inverted;
20187       break;
20188
20189     case T2_OPCODE_ORN:
20190       new_inst = T2_OPCODE_ORR;
20191       value = inverted;
20192       break;
20193
20194       /* AND <-> BIC.  TST has no inverted equivalent.  */
20195     case T2_OPCODE_AND:
20196       new_inst = T2_OPCODE_BIC;
20197       if (rd == 15)
20198         value = FAIL;
20199       else
20200         value = inverted;
20201       break;
20202
20203     case T2_OPCODE_BIC:
20204       new_inst = T2_OPCODE_AND;
20205       value = inverted;
20206       break;
20207
20208       /* ADC <-> SBC  */
20209     case T2_OPCODE_ADC:
20210       new_inst = T2_OPCODE_SBC;
20211       value = inverted;
20212       break;
20213
20214     case T2_OPCODE_SBC:
20215       new_inst = T2_OPCODE_ADC;
20216       value = inverted;
20217       break;
20218
20219       /* We cannot do anything.  */
20220     default:
20221       return FAIL;
20222     }
20223
20224   if (value == (unsigned int)FAIL)
20225     return FAIL;
20226
20227   *instruction &= T2_OPCODE_MASK;
20228   *instruction |= new_inst << T2_DATA_OP_SHIFT;
20229   return value;
20230 }
20231
20232 /* Read a 32-bit thumb instruction from buf.  */
20233 static unsigned long
20234 get_thumb32_insn (char * buf)
20235 {
20236   unsigned long insn;
20237   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
20238   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
20239
20240   return insn;
20241 }
20242
20243
20244 /* We usually want to set the low bit on the address of thumb function
20245    symbols.  In particular .word foo - . should have the low bit set.
20246    Generic code tries to fold the difference of two symbols to
20247    a constant.  Prevent this and force a relocation when the first symbols
20248    is a thumb function.  */
20249
20250 bfd_boolean
20251 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
20252 {
20253   if (op == O_subtract
20254       && l->X_op == O_symbol
20255       && r->X_op == O_symbol
20256       && THUMB_IS_FUNC (l->X_add_symbol))
20257     {
20258       l->X_op = O_subtract;
20259       l->X_op_symbol = r->X_add_symbol;
20260       l->X_add_number -= r->X_add_number;
20261       return TRUE;
20262     }
20263
20264   /* Process as normal.  */
20265   return FALSE;
20266 }
20267
20268 /* Encode Thumb2 unconditional branches and calls. The encoding
20269    for the 2 are identical for the immediate values.  */
20270
20271 static void
20272 encode_thumb2_b_bl_offset (char * buf, offsetT value)
20273 {
20274 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
20275   offsetT newval;
20276   offsetT newval2;
20277   addressT S, I1, I2, lo, hi;
20278
20279   S = (value >> 24) & 0x01;
20280   I1 = (value >> 23) & 0x01;
20281   I2 = (value >> 22) & 0x01;
20282   hi = (value >> 12) & 0x3ff;
20283   lo = (value >> 1) & 0x7ff; 
20284   newval   = md_chars_to_number (buf, THUMB_SIZE);
20285   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
20286   newval  |= (S << 10) | hi;
20287   newval2 &=  ~T2I1I2MASK;
20288   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
20289   md_number_to_chars (buf, newval, THUMB_SIZE);
20290   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
20291 }
20292
20293 void
20294 md_apply_fix (fixS *    fixP,
20295                valueT * valP,
20296                segT     seg)
20297 {
20298   offsetT        value = * valP;
20299   offsetT        newval;
20300   unsigned int   newimm;
20301   unsigned long  temp;
20302   int            sign;
20303   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
20304
20305   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
20306
20307   /* Note whether this will delete the relocation.  */
20308
20309   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
20310     fixP->fx_done = 1;
20311
20312   /* On a 64-bit host, silently truncate 'value' to 32 bits for
20313      consistency with the behaviour on 32-bit hosts.  Remember value
20314      for emit_reloc.  */
20315   value &= 0xffffffff;
20316   value ^= 0x80000000;
20317   value -= 0x80000000;
20318
20319   *valP = value;
20320   fixP->fx_addnumber = value;
20321
20322   /* Same treatment for fixP->fx_offset.  */
20323   fixP->fx_offset &= 0xffffffff;
20324   fixP->fx_offset ^= 0x80000000;
20325   fixP->fx_offset -= 0x80000000;
20326
20327   switch (fixP->fx_r_type)
20328     {
20329     case BFD_RELOC_NONE:
20330       /* This will need to go in the object file.  */
20331       fixP->fx_done = 0;
20332       break;
20333
20334     case BFD_RELOC_ARM_IMMEDIATE:
20335       /* We claim that this fixup has been processed here,
20336          even if in fact we generate an error because we do
20337          not have a reloc for it, so tc_gen_reloc will reject it.  */
20338       fixP->fx_done = 1;
20339
20340       if (fixP->fx_addsy)
20341         {
20342           const char *msg = 0;
20343
20344           if (! S_IS_DEFINED (fixP->fx_addsy))
20345             msg = _("undefined symbol %s used as an immediate value");
20346           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
20347             msg = _("symbol %s is in a different section");
20348           else if (S_IS_WEAK (fixP->fx_addsy))
20349             msg = _("symbol %s is weak and may be overridden later");
20350
20351           if (msg)
20352             {
20353               as_bad_where (fixP->fx_file, fixP->fx_line,
20354                             msg, S_GET_NAME (fixP->fx_addsy));
20355               break;
20356             }
20357         }
20358
20359       newimm = encode_arm_immediate (value);
20360       temp = md_chars_to_number (buf, INSN_SIZE);
20361
20362       /* If the instruction will fail, see if we can fix things up by
20363          changing the opcode.  */
20364       if (newimm == (unsigned int) FAIL
20365           && (newimm = negate_data_op (&temp, value)) == (unsigned int) FAIL)
20366         {
20367           as_bad_where (fixP->fx_file, fixP->fx_line,
20368                         _("invalid constant (%lx) after fixup"),
20369                         (unsigned long) value);
20370           break;
20371         }
20372
20373       newimm |= (temp & 0xfffff000);
20374       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
20375       break;
20376
20377     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
20378       {
20379         unsigned int highpart = 0;
20380         unsigned int newinsn  = 0xe1a00000; /* nop.  */
20381
20382         if (fixP->fx_addsy)
20383           {
20384             const char *msg = 0;
20385
20386             if (! S_IS_DEFINED (fixP->fx_addsy))
20387               msg = _("undefined symbol %s used as an immediate value");
20388             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
20389               msg = _("symbol %s is in a different section");
20390             else if (S_IS_WEAK (fixP->fx_addsy))
20391               msg = _("symbol %s is weak and may be overridden later");
20392
20393             if (msg)
20394               {
20395                 as_bad_where (fixP->fx_file, fixP->fx_line,
20396                               msg, S_GET_NAME (fixP->fx_addsy));
20397                 break;
20398               }
20399           }
20400         
20401         newimm = encode_arm_immediate (value);
20402         temp = md_chars_to_number (buf, INSN_SIZE);
20403
20404         /* If the instruction will fail, see if we can fix things up by
20405            changing the opcode.  */
20406         if (newimm == (unsigned int) FAIL
20407             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
20408           {
20409             /* No ?  OK - try using two ADD instructions to generate
20410                the value.  */
20411             newimm = validate_immediate_twopart (value, & highpart);
20412
20413             /* Yes - then make sure that the second instruction is
20414                also an add.  */
20415             if (newimm != (unsigned int) FAIL)
20416               newinsn = temp;
20417             /* Still No ?  Try using a negated value.  */
20418             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
20419               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
20420             /* Otherwise - give up.  */
20421             else
20422               {
20423                 as_bad_where (fixP->fx_file, fixP->fx_line,
20424                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
20425                               (long) value);
20426                 break;
20427               }
20428
20429             /* Replace the first operand in the 2nd instruction (which
20430                is the PC) with the destination register.  We have
20431                already added in the PC in the first instruction and we
20432                do not want to do it again.  */
20433             newinsn &= ~ 0xf0000;
20434             newinsn |= ((newinsn & 0x0f000) << 4);
20435           }
20436
20437         newimm |= (temp & 0xfffff000);
20438         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
20439
20440         highpart |= (newinsn & 0xfffff000);
20441         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
20442       }
20443       break;
20444
20445     case BFD_RELOC_ARM_OFFSET_IMM:
20446       if (!fixP->fx_done && seg->use_rela_p)
20447         value = 0;
20448
20449     case BFD_RELOC_ARM_LITERAL:
20450       sign = value >= 0;
20451
20452       if (value < 0)
20453         value = - value;
20454
20455       if (validate_offset_imm (value, 0) == FAIL)
20456         {
20457           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
20458             as_bad_where (fixP->fx_file, fixP->fx_line,
20459                           _("invalid literal constant: pool needs to be closer"));
20460           else
20461             as_bad_where (fixP->fx_file, fixP->fx_line,
20462                           _("bad immediate value for offset (%ld)"),
20463                           (long) value);
20464           break;
20465         }
20466
20467       newval = md_chars_to_number (buf, INSN_SIZE);
20468       newval &= 0xff7ff000;
20469       newval |= value | (sign ? INDEX_UP : 0);
20470       md_number_to_chars (buf, newval, INSN_SIZE);
20471       break;
20472
20473     case BFD_RELOC_ARM_OFFSET_IMM8:
20474     case BFD_RELOC_ARM_HWLITERAL:
20475       sign = value >= 0;
20476
20477       if (value < 0)
20478         value = - value;
20479
20480       if (validate_offset_imm (value, 1) == FAIL)
20481         {
20482           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
20483             as_bad_where (fixP->fx_file, fixP->fx_line,
20484                           _("invalid literal constant: pool needs to be closer"));
20485           else
20486             as_bad (_("bad immediate value for 8-bit offset (%ld)"),
20487                     (long) value);
20488           break;
20489         }
20490
20491       newval = md_chars_to_number (buf, INSN_SIZE);
20492       newval &= 0xff7ff0f0;
20493       newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
20494       md_number_to_chars (buf, newval, INSN_SIZE);
20495       break;
20496
20497     case BFD_RELOC_ARM_T32_OFFSET_U8:
20498       if (value < 0 || value > 1020 || value % 4 != 0)
20499         as_bad_where (fixP->fx_file, fixP->fx_line,
20500                       _("bad immediate value for offset (%ld)"), (long) value);
20501       value /= 4;
20502
20503       newval = md_chars_to_number (buf+2, THUMB_SIZE);
20504       newval |= value;
20505       md_number_to_chars (buf+2, newval, THUMB_SIZE);
20506       break;
20507
20508     case BFD_RELOC_ARM_T32_OFFSET_IMM:
20509       /* This is a complicated relocation used for all varieties of Thumb32
20510          load/store instruction with immediate offset:
20511
20512          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
20513                                                    *4, optional writeback(W)
20514                                                    (doubleword load/store)
20515
20516          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
20517          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
20518          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
20519          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
20520          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
20521
20522          Uppercase letters indicate bits that are already encoded at
20523          this point.  Lowercase letters are our problem.  For the
20524          second block of instructions, the secondary opcode nybble
20525          (bits 8..11) is present, and bit 23 is zero, even if this is
20526          a PC-relative operation.  */
20527       newval = md_chars_to_number (buf, THUMB_SIZE);
20528       newval <<= 16;
20529       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
20530
20531       if ((newval & 0xf0000000) == 0xe0000000)
20532         {
20533           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
20534           if (value >= 0)
20535             newval |= (1 << 23);
20536           else
20537             value = -value;
20538           if (value % 4 != 0)
20539             {
20540               as_bad_where (fixP->fx_file, fixP->fx_line,
20541                             _("offset not a multiple of 4"));
20542               break;
20543             }
20544           value /= 4;
20545           if (value > 0xff)
20546             {
20547               as_bad_where (fixP->fx_file, fixP->fx_line,
20548                             _("offset out of range"));
20549               break;
20550             }
20551           newval &= ~0xff;
20552         }
20553       else if ((newval & 0x000f0000) == 0x000f0000)
20554         {
20555           /* PC-relative, 12-bit offset.  */
20556           if (value >= 0)
20557             newval |= (1 << 23);
20558           else
20559             value = -value;
20560           if (value > 0xfff)
20561             {
20562               as_bad_where (fixP->fx_file, fixP->fx_line,
20563                             _("offset out of range"));
20564               break;
20565             }
20566           newval &= ~0xfff;
20567         }
20568       else if ((newval & 0x00000100) == 0x00000100)
20569         {
20570           /* Writeback: 8-bit, +/- offset.  */
20571           if (value >= 0)
20572             newval |= (1 << 9);
20573           else
20574             value = -value;
20575           if (value > 0xff)
20576             {
20577               as_bad_where (fixP->fx_file, fixP->fx_line,
20578                             _("offset out of range"));
20579               break;
20580             }
20581           newval &= ~0xff;
20582         }
20583       else if ((newval & 0x00000f00) == 0x00000e00)
20584         {
20585           /* T-instruction: positive 8-bit offset.  */
20586           if (value < 0 || value > 0xff)
20587             {
20588               as_bad_where (fixP->fx_file, fixP->fx_line,
20589                             _("offset out of range"));
20590               break;
20591             }
20592           newval &= ~0xff;
20593           newval |= value;
20594         }
20595       else
20596         {
20597           /* Positive 12-bit or negative 8-bit offset.  */
20598           int limit;
20599           if (value >= 0)
20600             {
20601               newval |= (1 << 23);
20602               limit = 0xfff;
20603             }
20604           else
20605             {
20606               value = -value;
20607               limit = 0xff;
20608             }
20609           if (value > limit)
20610             {
20611               as_bad_where (fixP->fx_file, fixP->fx_line,
20612                             _("offset out of range"));
20613               break;
20614             }
20615           newval &= ~limit;
20616         }
20617
20618       newval |= value;
20619       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
20620       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
20621       break;
20622
20623     case BFD_RELOC_ARM_SHIFT_IMM:
20624       newval = md_chars_to_number (buf, INSN_SIZE);
20625       if (((unsigned long) value) > 32
20626           || (value == 32
20627               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
20628         {
20629           as_bad_where (fixP->fx_file, fixP->fx_line,
20630                         _("shift expression is too large"));
20631           break;
20632         }
20633
20634       if (value == 0)
20635         /* Shifts of zero must be done as lsl.  */
20636         newval &= ~0x60;
20637       else if (value == 32)
20638         value = 0;
20639       newval &= 0xfffff07f;
20640       newval |= (value & 0x1f) << 7;
20641       md_number_to_chars (buf, newval, INSN_SIZE);
20642       break;
20643
20644     case BFD_RELOC_ARM_T32_IMMEDIATE:
20645     case BFD_RELOC_ARM_T32_ADD_IMM:
20646     case BFD_RELOC_ARM_T32_IMM12:
20647     case BFD_RELOC_ARM_T32_ADD_PC12:
20648       /* We claim that this fixup has been processed here,
20649          even if in fact we generate an error because we do
20650          not have a reloc for it, so tc_gen_reloc will reject it.  */
20651       fixP->fx_done = 1;
20652
20653       if (fixP->fx_addsy
20654           && ! S_IS_DEFINED (fixP->fx_addsy))
20655         {
20656           as_bad_where (fixP->fx_file, fixP->fx_line,
20657                         _("undefined symbol %s used as an immediate value"),
20658                         S_GET_NAME (fixP->fx_addsy));
20659           break;
20660         }
20661
20662       newval = md_chars_to_number (buf, THUMB_SIZE);
20663       newval <<= 16;
20664       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
20665
20666       newimm = FAIL;
20667       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
20668           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
20669         {
20670           newimm = encode_thumb32_immediate (value);
20671           if (newimm == (unsigned int) FAIL)
20672             newimm = thumb32_negate_data_op (&newval, value);
20673         }
20674       if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE
20675           && newimm == (unsigned int) FAIL)
20676         {
20677           /* Turn add/sum into addw/subw.  */
20678           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
20679             newval = (newval & 0xfeffffff) | 0x02000000;
20680           /* No flat 12-bit imm encoding for addsw/subsw.  */
20681           if ((newval & 0x00100000) == 0)
20682             {
20683               /* 12 bit immediate for addw/subw.  */
20684               if (value < 0)
20685                 {
20686                   value = -value;
20687                   newval ^= 0x00a00000;
20688                 }
20689               if (value > 0xfff)
20690                 newimm = (unsigned int) FAIL;
20691               else
20692                 newimm = value;
20693             }
20694         }
20695
20696       if (newimm == (unsigned int)FAIL)
20697         {
20698           as_bad_where (fixP->fx_file, fixP->fx_line,
20699                         _("invalid constant (%lx) after fixup"),
20700                         (unsigned long) value);
20701           break;
20702         }
20703
20704       newval |= (newimm & 0x800) << 15;
20705       newval |= (newimm & 0x700) << 4;
20706       newval |= (newimm & 0x0ff);
20707
20708       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
20709       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
20710       break;
20711
20712     case BFD_RELOC_ARM_SMC:
20713       if (((unsigned long) value) > 0xffff)
20714         as_bad_where (fixP->fx_file, fixP->fx_line,
20715                       _("invalid smc expression"));
20716       newval = md_chars_to_number (buf, INSN_SIZE);
20717       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
20718       md_number_to_chars (buf, newval, INSN_SIZE);
20719       break;
20720
20721     case BFD_RELOC_ARM_HVC:
20722       if (((unsigned long) value) > 0xffff)
20723         as_bad_where (fixP->fx_file, fixP->fx_line,
20724                       _("invalid hvc expression"));
20725       newval = md_chars_to_number (buf, INSN_SIZE);
20726       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
20727       md_number_to_chars (buf, newval, INSN_SIZE);
20728       break;
20729
20730     case BFD_RELOC_ARM_SWI:
20731       if (fixP->tc_fix_data != 0)
20732         {
20733           if (((unsigned long) value) > 0xff)
20734             as_bad_where (fixP->fx_file, fixP->fx_line,
20735                           _("invalid swi expression"));
20736           newval = md_chars_to_number (buf, THUMB_SIZE);
20737           newval |= value;
20738           md_number_to_chars (buf, newval, THUMB_SIZE);
20739         }
20740       else
20741         {
20742           if (((unsigned long) value) > 0x00ffffff)
20743             as_bad_where (fixP->fx_file, fixP->fx_line,
20744                           _("invalid swi expression"));
20745           newval = md_chars_to_number (buf, INSN_SIZE);
20746           newval |= value;
20747           md_number_to_chars (buf, newval, INSN_SIZE);
20748         }
20749       break;
20750
20751     case BFD_RELOC_ARM_MULTI:
20752       if (((unsigned long) value) > 0xffff)
20753         as_bad_where (fixP->fx_file, fixP->fx_line,
20754                       _("invalid expression in load/store multiple"));
20755       newval = value | md_chars_to_number (buf, INSN_SIZE);
20756       md_number_to_chars (buf, newval, INSN_SIZE);
20757       break;
20758
20759 #ifdef OBJ_ELF
20760     case BFD_RELOC_ARM_PCREL_CALL:
20761
20762       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
20763           && fixP->fx_addsy
20764           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20765           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20766           && THUMB_IS_FUNC (fixP->fx_addsy))
20767         /* Flip the bl to blx. This is a simple flip
20768            bit here because we generate PCREL_CALL for
20769            unconditional bls.  */
20770         {
20771           newval = md_chars_to_number (buf, INSN_SIZE);
20772           newval = newval | 0x10000000;
20773           md_number_to_chars (buf, newval, INSN_SIZE);
20774           temp = 1;
20775           fixP->fx_done = 1;
20776         }
20777       else
20778         temp = 3;
20779       goto arm_branch_common;
20780
20781     case BFD_RELOC_ARM_PCREL_JUMP:
20782       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
20783           && fixP->fx_addsy
20784           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20785           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20786           && THUMB_IS_FUNC (fixP->fx_addsy))
20787         {
20788           /* This would map to a bl<cond>, b<cond>,
20789              b<always> to a Thumb function. We
20790              need to force a relocation for this particular
20791              case.  */
20792           newval = md_chars_to_number (buf, INSN_SIZE);
20793           fixP->fx_done = 0;
20794         }
20795
20796     case BFD_RELOC_ARM_PLT32:
20797 #endif
20798     case BFD_RELOC_ARM_PCREL_BRANCH:
20799       temp = 3;
20800       goto arm_branch_common;
20801
20802     case BFD_RELOC_ARM_PCREL_BLX:
20803
20804       temp = 1;
20805       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
20806           && fixP->fx_addsy
20807           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20808           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20809           && ARM_IS_FUNC (fixP->fx_addsy))
20810         {
20811           /* Flip the blx to a bl and warn.  */
20812           const char *name = S_GET_NAME (fixP->fx_addsy);
20813           newval = 0xeb000000;
20814           as_warn_where (fixP->fx_file, fixP->fx_line,
20815                          _("blx to '%s' an ARM ISA state function changed to bl"),
20816                           name);
20817           md_number_to_chars (buf, newval, INSN_SIZE);
20818           temp = 3;
20819           fixP->fx_done = 1;
20820         }
20821
20822 #ifdef OBJ_ELF
20823        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
20824          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
20825 #endif
20826
20827     arm_branch_common:
20828       /* We are going to store value (shifted right by two) in the
20829          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
20830          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
20831          also be be clear.  */
20832       if (value & temp)
20833         as_bad_where (fixP->fx_file, fixP->fx_line,
20834                       _("misaligned branch destination"));
20835       if ((value & (offsetT)0xfe000000) != (offsetT)0
20836           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
20837         as_bad_where (fixP->fx_file, fixP->fx_line,
20838                       _("branch out of range"));
20839
20840       if (fixP->fx_done || !seg->use_rela_p)
20841         {
20842           newval = md_chars_to_number (buf, INSN_SIZE);
20843           newval |= (value >> 2) & 0x00ffffff;
20844           /* Set the H bit on BLX instructions.  */
20845           if (temp == 1)
20846             {
20847               if (value & 2)
20848                 newval |= 0x01000000;
20849               else
20850                 newval &= ~0x01000000;
20851             }
20852           md_number_to_chars (buf, newval, INSN_SIZE);
20853         }
20854       break;
20855
20856     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
20857       /* CBZ can only branch forward.  */
20858
20859       /* Attempts to use CBZ to branch to the next instruction
20860          (which, strictly speaking, are prohibited) will be turned into
20861          no-ops.
20862
20863          FIXME: It may be better to remove the instruction completely and
20864          perform relaxation.  */
20865       if (value == -2)
20866         {
20867           newval = md_chars_to_number (buf, THUMB_SIZE);
20868           newval = 0xbf00; /* NOP encoding T1 */
20869           md_number_to_chars (buf, newval, THUMB_SIZE);
20870         }
20871       else
20872         {
20873           if (value & ~0x7e)
20874             as_bad_where (fixP->fx_file, fixP->fx_line,
20875                           _("branch out of range"));
20876
20877           if (fixP->fx_done || !seg->use_rela_p)
20878             {
20879               newval = md_chars_to_number (buf, THUMB_SIZE);
20880               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
20881               md_number_to_chars (buf, newval, THUMB_SIZE);
20882             }
20883         }
20884       break;
20885
20886     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
20887       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
20888         as_bad_where (fixP->fx_file, fixP->fx_line,
20889                       _("branch out of range"));
20890
20891       if (fixP->fx_done || !seg->use_rela_p)
20892         {
20893           newval = md_chars_to_number (buf, THUMB_SIZE);
20894           newval |= (value & 0x1ff) >> 1;
20895           md_number_to_chars (buf, newval, THUMB_SIZE);
20896         }
20897       break;
20898
20899     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
20900       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
20901         as_bad_where (fixP->fx_file, fixP->fx_line,
20902                       _("branch out of range"));
20903
20904       if (fixP->fx_done || !seg->use_rela_p)
20905         {
20906           newval = md_chars_to_number (buf, THUMB_SIZE);
20907           newval |= (value & 0xfff) >> 1;
20908           md_number_to_chars (buf, newval, THUMB_SIZE);
20909         }
20910       break;
20911
20912     case BFD_RELOC_THUMB_PCREL_BRANCH20:
20913       if (fixP->fx_addsy
20914           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20915           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20916           && ARM_IS_FUNC (fixP->fx_addsy)
20917           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20918         {
20919           /* Force a relocation for a branch 20 bits wide.  */
20920           fixP->fx_done = 0;
20921         }
20922       if ((value & ~0x1fffff) && ((value & ~0x1fffff) != ~0x1fffff))
20923         as_bad_where (fixP->fx_file, fixP->fx_line,
20924                       _("conditional branch out of range"));
20925
20926       if (fixP->fx_done || !seg->use_rela_p)
20927         {
20928           offsetT newval2;
20929           addressT S, J1, J2, lo, hi;
20930
20931           S  = (value & 0x00100000) >> 20;
20932           J2 = (value & 0x00080000) >> 19;
20933           J1 = (value & 0x00040000) >> 18;
20934           hi = (value & 0x0003f000) >> 12;
20935           lo = (value & 0x00000ffe) >> 1;
20936
20937           newval   = md_chars_to_number (buf, THUMB_SIZE);
20938           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
20939           newval  |= (S << 10) | hi;
20940           newval2 |= (J1 << 13) | (J2 << 11) | lo;
20941           md_number_to_chars (buf, newval, THUMB_SIZE);
20942           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
20943         }
20944       break;
20945
20946     case BFD_RELOC_THUMB_PCREL_BLX:
20947
20948       /* If there is a blx from a thumb state function to
20949          another thumb function flip this to a bl and warn
20950          about it.  */
20951
20952       if (fixP->fx_addsy
20953           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20954           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20955           && THUMB_IS_FUNC (fixP->fx_addsy))
20956         {
20957           const char *name = S_GET_NAME (fixP->fx_addsy);
20958           as_warn_where (fixP->fx_file, fixP->fx_line,
20959                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
20960                          name);
20961           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
20962           newval = newval | 0x1000;
20963           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
20964           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
20965           fixP->fx_done = 1;
20966         }
20967
20968
20969       goto thumb_bl_common;
20970
20971     case BFD_RELOC_THUMB_PCREL_BRANCH23:
20972
20973       /* A bl from Thumb state ISA to an internal ARM state function
20974          is converted to a blx.  */
20975       if (fixP->fx_addsy
20976           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20977           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20978           && ARM_IS_FUNC (fixP->fx_addsy)
20979           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20980         {
20981           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
20982           newval = newval & ~0x1000;
20983           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
20984           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
20985           fixP->fx_done = 1;
20986         }
20987
20988     thumb_bl_common:
20989
20990 #ifdef OBJ_ELF
20991        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4 &&
20992            fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
20993          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
20994 #endif
20995
20996       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
20997         /* For a BLX instruction, make sure that the relocation is rounded up
20998            to a word boundary.  This follows the semantics of the instruction
20999            which specifies that bit 1 of the target address will come from bit
21000            1 of the base address.  */
21001         value = (value + 1) & ~ 1;
21002
21003
21004        if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
21005         {
21006           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2)))
21007             {
21008               as_bad_where (fixP->fx_file, fixP->fx_line,
21009                             _("branch out of range"));
21010             }
21011           else if ((value & ~0x1ffffff)
21012                    && ((value & ~0x1ffffff) != ~0x1ffffff))
21013               {
21014                 as_bad_where (fixP->fx_file, fixP->fx_line,
21015                             _("Thumb2 branch out of range"));
21016               }
21017         }
21018
21019       if (fixP->fx_done || !seg->use_rela_p)
21020         encode_thumb2_b_bl_offset (buf, value);
21021
21022       break;
21023
21024     case BFD_RELOC_THUMB_PCREL_BRANCH25:
21025       if ((value & ~0x1ffffff) && ((value & ~0x1ffffff) != ~0x1ffffff))
21026         as_bad_where (fixP->fx_file, fixP->fx_line,
21027                       _("branch out of range"));
21028
21029       if (fixP->fx_done || !seg->use_rela_p)
21030           encode_thumb2_b_bl_offset (buf, value);
21031
21032       break;
21033
21034     case BFD_RELOC_8:
21035       if (fixP->fx_done || !seg->use_rela_p)
21036         md_number_to_chars (buf, value, 1);
21037       break;
21038
21039     case BFD_RELOC_16:
21040       if (fixP->fx_done || !seg->use_rela_p)
21041         md_number_to_chars (buf, value, 2);
21042       break;
21043
21044 #ifdef OBJ_ELF
21045     case BFD_RELOC_ARM_TLS_CALL:
21046     case BFD_RELOC_ARM_THM_TLS_CALL:
21047     case BFD_RELOC_ARM_TLS_DESCSEQ:
21048     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
21049       S_SET_THREAD_LOCAL (fixP->fx_addsy);
21050       break;
21051
21052     case BFD_RELOC_ARM_TLS_GOTDESC:
21053     case BFD_RELOC_ARM_TLS_GD32:
21054     case BFD_RELOC_ARM_TLS_LE32:
21055     case BFD_RELOC_ARM_TLS_IE32:
21056     case BFD_RELOC_ARM_TLS_LDM32:
21057     case BFD_RELOC_ARM_TLS_LDO32:
21058       S_SET_THREAD_LOCAL (fixP->fx_addsy);
21059       /* fall through */
21060
21061     case BFD_RELOC_ARM_GOT32:
21062     case BFD_RELOC_ARM_GOTOFF:
21063       if (fixP->fx_done || !seg->use_rela_p)
21064         md_number_to_chars (buf, 0, 4);
21065       break;
21066
21067     case BFD_RELOC_ARM_GOT_PREL:
21068       if (fixP->fx_done || !seg->use_rela_p)
21069         md_number_to_chars (buf, value, 4);
21070       break;
21071
21072     case BFD_RELOC_ARM_TARGET2:
21073       /* TARGET2 is not partial-inplace, so we need to write the
21074          addend here for REL targets, because it won't be written out
21075          during reloc processing later.  */
21076       if (fixP->fx_done || !seg->use_rela_p)
21077         md_number_to_chars (buf, fixP->fx_offset, 4);
21078       break;
21079 #endif
21080
21081     case BFD_RELOC_RVA:
21082     case BFD_RELOC_32:
21083     case BFD_RELOC_ARM_TARGET1:
21084     case BFD_RELOC_ARM_ROSEGREL32:
21085     case BFD_RELOC_ARM_SBREL32:
21086     case BFD_RELOC_32_PCREL:
21087 #ifdef TE_PE
21088     case BFD_RELOC_32_SECREL:
21089 #endif
21090       if (fixP->fx_done || !seg->use_rela_p)
21091 #ifdef TE_WINCE
21092         /* For WinCE we only do this for pcrel fixups.  */
21093         if (fixP->fx_done || fixP->fx_pcrel)
21094 #endif
21095           md_number_to_chars (buf, value, 4);
21096       break;
21097
21098 #ifdef OBJ_ELF
21099     case BFD_RELOC_ARM_PREL31:
21100       if (fixP->fx_done || !seg->use_rela_p)
21101         {
21102           newval = md_chars_to_number (buf, 4) & 0x80000000;
21103           if ((value ^ (value >> 1)) & 0x40000000)
21104             {
21105               as_bad_where (fixP->fx_file, fixP->fx_line,
21106                             _("rel31 relocation overflow"));
21107             }
21108           newval |= value & 0x7fffffff;
21109           md_number_to_chars (buf, newval, 4);
21110         }
21111       break;
21112 #endif
21113
21114     case BFD_RELOC_ARM_CP_OFF_IMM:
21115     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
21116       if (value < -1023 || value > 1023 || (value & 3))
21117         as_bad_where (fixP->fx_file, fixP->fx_line,
21118                       _("co-processor offset out of range"));
21119     cp_off_common:
21120       sign = value >= 0;
21121       if (value < 0)
21122         value = -value;
21123       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
21124           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
21125         newval = md_chars_to_number (buf, INSN_SIZE);
21126       else
21127         newval = get_thumb32_insn (buf);
21128       newval &= 0xff7fff00;
21129       newval |= (value >> 2) | (sign ? INDEX_UP : 0);
21130       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
21131           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
21132         md_number_to_chars (buf, newval, INSN_SIZE);
21133       else
21134         put_thumb32_insn (buf, newval);
21135       break;
21136
21137     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
21138     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
21139       if (value < -255 || value > 255)
21140         as_bad_where (fixP->fx_file, fixP->fx_line,
21141                       _("co-processor offset out of range"));
21142       value *= 4;
21143       goto cp_off_common;
21144
21145     case BFD_RELOC_ARM_THUMB_OFFSET:
21146       newval = md_chars_to_number (buf, THUMB_SIZE);
21147       /* Exactly what ranges, and where the offset is inserted depends
21148          on the type of instruction, we can establish this from the
21149          top 4 bits.  */
21150       switch (newval >> 12)
21151         {
21152         case 4: /* PC load.  */
21153           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
21154              forced to zero for these loads; md_pcrel_from has already
21155              compensated for this.  */
21156           if (value & 3)
21157             as_bad_where (fixP->fx_file, fixP->fx_line,
21158                           _("invalid offset, target not word aligned (0x%08lX)"),
21159                           (((unsigned long) fixP->fx_frag->fr_address
21160                             + (unsigned long) fixP->fx_where) & ~3)
21161                           + (unsigned long) value);
21162
21163           if (value & ~0x3fc)
21164             as_bad_where (fixP->fx_file, fixP->fx_line,
21165                           _("invalid offset, value too big (0x%08lX)"),
21166                           (long) value);
21167
21168           newval |= value >> 2;
21169           break;
21170
21171         case 9: /* SP load/store.  */
21172           if (value & ~0x3fc)
21173             as_bad_where (fixP->fx_file, fixP->fx_line,
21174                           _("invalid offset, value too big (0x%08lX)"),
21175                           (long) value);
21176           newval |= value >> 2;
21177           break;
21178
21179         case 6: /* Word load/store.  */
21180           if (value & ~0x7c)
21181             as_bad_where (fixP->fx_file, fixP->fx_line,
21182                           _("invalid offset, value too big (0x%08lX)"),
21183                           (long) value);
21184           newval |= value << 4; /* 6 - 2.  */
21185           break;
21186
21187         case 7: /* Byte load/store.  */
21188           if (value & ~0x1f)
21189             as_bad_where (fixP->fx_file, fixP->fx_line,
21190                           _("invalid offset, value too big (0x%08lX)"),
21191                           (long) value);
21192           newval |= value << 6;
21193           break;
21194
21195         case 8: /* Halfword load/store.  */
21196           if (value & ~0x3e)
21197             as_bad_where (fixP->fx_file, fixP->fx_line,
21198                           _("invalid offset, value too big (0x%08lX)"),
21199                           (long) value);
21200           newval |= value << 5; /* 6 - 1.  */
21201           break;
21202
21203         default:
21204           as_bad_where (fixP->fx_file, fixP->fx_line,
21205                         "Unable to process relocation for thumb opcode: %lx",
21206                         (unsigned long) newval);
21207           break;
21208         }
21209       md_number_to_chars (buf, newval, THUMB_SIZE);
21210       break;
21211
21212     case BFD_RELOC_ARM_THUMB_ADD:
21213       /* This is a complicated relocation, since we use it for all of
21214          the following immediate relocations:
21215
21216             3bit ADD/SUB
21217             8bit ADD/SUB
21218             9bit ADD/SUB SP word-aligned
21219            10bit ADD PC/SP word-aligned
21220
21221          The type of instruction being processed is encoded in the
21222          instruction field:
21223
21224            0x8000  SUB
21225            0x00F0  Rd
21226            0x000F  Rs
21227       */
21228       newval = md_chars_to_number (buf, THUMB_SIZE);
21229       {
21230         int rd = (newval >> 4) & 0xf;
21231         int rs = newval & 0xf;
21232         int subtract = !!(newval & 0x8000);
21233
21234         /* Check for HI regs, only very restricted cases allowed:
21235            Adjusting SP, and using PC or SP to get an address.  */
21236         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
21237             || (rs > 7 && rs != REG_SP && rs != REG_PC))
21238           as_bad_where (fixP->fx_file, fixP->fx_line,
21239                         _("invalid Hi register with immediate"));
21240
21241         /* If value is negative, choose the opposite instruction.  */
21242         if (value < 0)
21243           {
21244             value = -value;
21245             subtract = !subtract;
21246             if (value < 0)
21247               as_bad_where (fixP->fx_file, fixP->fx_line,
21248                             _("immediate value out of range"));
21249           }
21250
21251         if (rd == REG_SP)
21252           {
21253             if (value & ~0x1fc)
21254               as_bad_where (fixP->fx_file, fixP->fx_line,
21255                             _("invalid immediate for stack address calculation"));
21256             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
21257             newval |= value >> 2;
21258           }
21259         else if (rs == REG_PC || rs == REG_SP)
21260           {
21261             if (subtract || value & ~0x3fc)
21262               as_bad_where (fixP->fx_file, fixP->fx_line,
21263                             _("invalid immediate for address calculation (value = 0x%08lX)"),
21264                             (unsigned long) value);
21265             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
21266             newval |= rd << 8;
21267             newval |= value >> 2;
21268           }
21269         else if (rs == rd)
21270           {
21271             if (value & ~0xff)
21272               as_bad_where (fixP->fx_file, fixP->fx_line,
21273                             _("immediate value out of range"));
21274             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
21275             newval |= (rd << 8) | value;
21276           }
21277         else
21278           {
21279             if (value & ~0x7)
21280               as_bad_where (fixP->fx_file, fixP->fx_line,
21281                             _("immediate value out of range"));
21282             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
21283             newval |= rd | (rs << 3) | (value << 6);
21284           }
21285       }
21286       md_number_to_chars (buf, newval, THUMB_SIZE);
21287       break;
21288
21289     case BFD_RELOC_ARM_THUMB_IMM:
21290       newval = md_chars_to_number (buf, THUMB_SIZE);
21291       if (value < 0 || value > 255)
21292         as_bad_where (fixP->fx_file, fixP->fx_line,
21293                       _("invalid immediate: %ld is out of range"),
21294                       (long) value);
21295       newval |= value;
21296       md_number_to_chars (buf, newval, THUMB_SIZE);
21297       break;
21298
21299     case BFD_RELOC_ARM_THUMB_SHIFT:
21300       /* 5bit shift value (0..32).  LSL cannot take 32.  */
21301       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
21302       temp = newval & 0xf800;
21303       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
21304         as_bad_where (fixP->fx_file, fixP->fx_line,
21305                       _("invalid shift value: %ld"), (long) value);
21306       /* Shifts of zero must be encoded as LSL.  */
21307       if (value == 0)
21308         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
21309       /* Shifts of 32 are encoded as zero.  */
21310       else if (value == 32)
21311         value = 0;
21312       newval |= value << 6;
21313       md_number_to_chars (buf, newval, THUMB_SIZE);
21314       break;
21315
21316     case BFD_RELOC_VTABLE_INHERIT:
21317     case BFD_RELOC_VTABLE_ENTRY:
21318       fixP->fx_done = 0;
21319       return;
21320
21321     case BFD_RELOC_ARM_MOVW:
21322     case BFD_RELOC_ARM_MOVT:
21323     case BFD_RELOC_ARM_THUMB_MOVW:
21324     case BFD_RELOC_ARM_THUMB_MOVT:
21325       if (fixP->fx_done || !seg->use_rela_p)
21326         {
21327           /* REL format relocations are limited to a 16-bit addend.  */
21328           if (!fixP->fx_done)
21329             {
21330               if (value < -0x8000 || value > 0x7fff)
21331                   as_bad_where (fixP->fx_file, fixP->fx_line,
21332                                 _("offset out of range"));
21333             }
21334           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
21335                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
21336             {
21337               value >>= 16;
21338             }
21339
21340           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
21341               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
21342             {
21343               newval = get_thumb32_insn (buf);
21344               newval &= 0xfbf08f00;
21345               newval |= (value & 0xf000) << 4;
21346               newval |= (value & 0x0800) << 15;
21347               newval |= (value & 0x0700) << 4;
21348               newval |= (value & 0x00ff);
21349               put_thumb32_insn (buf, newval);
21350             }
21351           else
21352             {
21353               newval = md_chars_to_number (buf, 4);
21354               newval &= 0xfff0f000;
21355               newval |= value & 0x0fff;
21356               newval |= (value & 0xf000) << 4;
21357               md_number_to_chars (buf, newval, 4);
21358             }
21359         }
21360       return;
21361
21362    case BFD_RELOC_ARM_ALU_PC_G0_NC:
21363    case BFD_RELOC_ARM_ALU_PC_G0:
21364    case BFD_RELOC_ARM_ALU_PC_G1_NC:
21365    case BFD_RELOC_ARM_ALU_PC_G1:
21366    case BFD_RELOC_ARM_ALU_PC_G2:
21367    case BFD_RELOC_ARM_ALU_SB_G0_NC:
21368    case BFD_RELOC_ARM_ALU_SB_G0:
21369    case BFD_RELOC_ARM_ALU_SB_G1_NC:
21370    case BFD_RELOC_ARM_ALU_SB_G1:
21371    case BFD_RELOC_ARM_ALU_SB_G2:
21372      gas_assert (!fixP->fx_done);
21373      if (!seg->use_rela_p)
21374        {
21375          bfd_vma insn;
21376          bfd_vma encoded_addend;
21377          bfd_vma addend_abs = abs (value);
21378
21379          /* Check that the absolute value of the addend can be
21380             expressed as an 8-bit constant plus a rotation.  */
21381          encoded_addend = encode_arm_immediate (addend_abs);
21382          if (encoded_addend == (unsigned int) FAIL)
21383            as_bad_where (fixP->fx_file, fixP->fx_line,
21384                          _("the offset 0x%08lX is not representable"),
21385                          (unsigned long) addend_abs);
21386
21387          /* Extract the instruction.  */
21388          insn = md_chars_to_number (buf, INSN_SIZE);
21389
21390          /* If the addend is positive, use an ADD instruction.
21391             Otherwise use a SUB.  Take care not to destroy the S bit.  */
21392          insn &= 0xff1fffff;
21393          if (value < 0)
21394            insn |= 1 << 22;
21395          else
21396            insn |= 1 << 23;
21397
21398          /* Place the encoded addend into the first 12 bits of the
21399             instruction.  */
21400          insn &= 0xfffff000;
21401          insn |= encoded_addend;
21402
21403          /* Update the instruction.  */
21404          md_number_to_chars (buf, insn, INSN_SIZE);
21405        }
21406      break;
21407
21408     case BFD_RELOC_ARM_LDR_PC_G0:
21409     case BFD_RELOC_ARM_LDR_PC_G1:
21410     case BFD_RELOC_ARM_LDR_PC_G2:
21411     case BFD_RELOC_ARM_LDR_SB_G0:
21412     case BFD_RELOC_ARM_LDR_SB_G1:
21413     case BFD_RELOC_ARM_LDR_SB_G2:
21414       gas_assert (!fixP->fx_done);
21415       if (!seg->use_rela_p)
21416         {
21417           bfd_vma insn;
21418           bfd_vma addend_abs = abs (value);
21419
21420           /* Check that the absolute value of the addend can be
21421              encoded in 12 bits.  */
21422           if (addend_abs >= 0x1000)
21423             as_bad_where (fixP->fx_file, fixP->fx_line,
21424                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
21425                           (unsigned long) addend_abs);
21426
21427           /* Extract the instruction.  */
21428           insn = md_chars_to_number (buf, INSN_SIZE);
21429
21430           /* If the addend is negative, clear bit 23 of the instruction.
21431              Otherwise set it.  */
21432           if (value < 0)
21433             insn &= ~(1 << 23);
21434           else
21435             insn |= 1 << 23;
21436
21437           /* Place the absolute value of the addend into the first 12 bits
21438              of the instruction.  */
21439           insn &= 0xfffff000;
21440           insn |= addend_abs;
21441
21442           /* Update the instruction.  */
21443           md_number_to_chars (buf, insn, INSN_SIZE);
21444         }
21445       break;
21446
21447     case BFD_RELOC_ARM_LDRS_PC_G0:
21448     case BFD_RELOC_ARM_LDRS_PC_G1:
21449     case BFD_RELOC_ARM_LDRS_PC_G2:
21450     case BFD_RELOC_ARM_LDRS_SB_G0:
21451     case BFD_RELOC_ARM_LDRS_SB_G1:
21452     case BFD_RELOC_ARM_LDRS_SB_G2:
21453       gas_assert (!fixP->fx_done);
21454       if (!seg->use_rela_p)
21455         {
21456           bfd_vma insn;
21457           bfd_vma addend_abs = abs (value);
21458
21459           /* Check that the absolute value of the addend can be
21460              encoded in 8 bits.  */
21461           if (addend_abs >= 0x100)
21462             as_bad_where (fixP->fx_file, fixP->fx_line,
21463                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
21464                           (unsigned long) addend_abs);
21465
21466           /* Extract the instruction.  */
21467           insn = md_chars_to_number (buf, INSN_SIZE);
21468
21469           /* If the addend is negative, clear bit 23 of the instruction.
21470              Otherwise set it.  */
21471           if (value < 0)
21472             insn &= ~(1 << 23);
21473           else
21474             insn |= 1 << 23;
21475
21476           /* Place the first four bits of the absolute value of the addend
21477              into the first 4 bits of the instruction, and the remaining
21478              four into bits 8 .. 11.  */
21479           insn &= 0xfffff0f0;
21480           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
21481
21482           /* Update the instruction.  */
21483           md_number_to_chars (buf, insn, INSN_SIZE);
21484         }
21485       break;
21486
21487     case BFD_RELOC_ARM_LDC_PC_G0:
21488     case BFD_RELOC_ARM_LDC_PC_G1:
21489     case BFD_RELOC_ARM_LDC_PC_G2:
21490     case BFD_RELOC_ARM_LDC_SB_G0:
21491     case BFD_RELOC_ARM_LDC_SB_G1:
21492     case BFD_RELOC_ARM_LDC_SB_G2:
21493       gas_assert (!fixP->fx_done);
21494       if (!seg->use_rela_p)
21495         {
21496           bfd_vma insn;
21497           bfd_vma addend_abs = abs (value);
21498
21499           /* Check that the absolute value of the addend is a multiple of
21500              four and, when divided by four, fits in 8 bits.  */
21501           if (addend_abs & 0x3)
21502             as_bad_where (fixP->fx_file, fixP->fx_line,
21503                           _("bad offset 0x%08lX (must be word-aligned)"),
21504                           (unsigned long) addend_abs);
21505
21506           if ((addend_abs >> 2) > 0xff)
21507             as_bad_where (fixP->fx_file, fixP->fx_line,
21508                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
21509                           (unsigned long) addend_abs);
21510
21511           /* Extract the instruction.  */
21512           insn = md_chars_to_number (buf, INSN_SIZE);
21513
21514           /* If the addend is negative, clear bit 23 of the instruction.
21515              Otherwise set it.  */
21516           if (value < 0)
21517             insn &= ~(1 << 23);
21518           else
21519             insn |= 1 << 23;
21520
21521           /* Place the addend (divided by four) into the first eight
21522              bits of the instruction.  */
21523           insn &= 0xfffffff0;
21524           insn |= addend_abs >> 2;
21525
21526           /* Update the instruction.  */
21527           md_number_to_chars (buf, insn, INSN_SIZE);
21528         }
21529       break;
21530
21531     case BFD_RELOC_ARM_V4BX:
21532       /* This will need to go in the object file.  */
21533       fixP->fx_done = 0;
21534       break;
21535
21536     case BFD_RELOC_UNUSED:
21537     default:
21538       as_bad_where (fixP->fx_file, fixP->fx_line,
21539                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
21540     }
21541 }
21542
21543 /* Translate internal representation of relocation info to BFD target
21544    format.  */
21545
21546 arelent *
21547 tc_gen_reloc (asection *section, fixS *fixp)
21548 {
21549   arelent * reloc;
21550   bfd_reloc_code_real_type code;
21551
21552   reloc = (arelent *) xmalloc (sizeof (arelent));
21553
21554   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
21555   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
21556   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
21557
21558   if (fixp->fx_pcrel)
21559     {
21560       if (section->use_rela_p)
21561         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
21562       else
21563         fixp->fx_offset = reloc->address;
21564     }
21565   reloc->addend = fixp->fx_offset;
21566
21567   switch (fixp->fx_r_type)
21568     {
21569     case BFD_RELOC_8:
21570       if (fixp->fx_pcrel)
21571         {
21572           code = BFD_RELOC_8_PCREL;
21573           break;
21574         }
21575
21576     case BFD_RELOC_16:
21577       if (fixp->fx_pcrel)
21578         {
21579           code = BFD_RELOC_16_PCREL;
21580           break;
21581         }
21582
21583     case BFD_RELOC_32:
21584       if (fixp->fx_pcrel)
21585         {
21586           code = BFD_RELOC_32_PCREL;
21587           break;
21588         }
21589
21590     case BFD_RELOC_ARM_MOVW:
21591       if (fixp->fx_pcrel)
21592         {
21593           code = BFD_RELOC_ARM_MOVW_PCREL;
21594           break;
21595         }
21596
21597     case BFD_RELOC_ARM_MOVT:
21598       if (fixp->fx_pcrel)
21599         {
21600           code = BFD_RELOC_ARM_MOVT_PCREL;
21601           break;
21602         }
21603
21604     case BFD_RELOC_ARM_THUMB_MOVW:
21605       if (fixp->fx_pcrel)
21606         {
21607           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
21608           break;
21609         }
21610
21611     case BFD_RELOC_ARM_THUMB_MOVT:
21612       if (fixp->fx_pcrel)
21613         {
21614           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
21615           break;
21616         }
21617
21618     case BFD_RELOC_NONE:
21619     case BFD_RELOC_ARM_PCREL_BRANCH:
21620     case BFD_RELOC_ARM_PCREL_BLX:
21621     case BFD_RELOC_RVA:
21622     case BFD_RELOC_THUMB_PCREL_BRANCH7:
21623     case BFD_RELOC_THUMB_PCREL_BRANCH9:
21624     case BFD_RELOC_THUMB_PCREL_BRANCH12:
21625     case BFD_RELOC_THUMB_PCREL_BRANCH20:
21626     case BFD_RELOC_THUMB_PCREL_BRANCH23:
21627     case BFD_RELOC_THUMB_PCREL_BRANCH25:
21628     case BFD_RELOC_VTABLE_ENTRY:
21629     case BFD_RELOC_VTABLE_INHERIT:
21630 #ifdef TE_PE
21631     case BFD_RELOC_32_SECREL:
21632 #endif
21633       code = fixp->fx_r_type;
21634       break;
21635
21636     case BFD_RELOC_THUMB_PCREL_BLX:
21637 #ifdef OBJ_ELF
21638       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
21639         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
21640       else
21641 #endif
21642         code = BFD_RELOC_THUMB_PCREL_BLX;
21643       break;
21644
21645     case BFD_RELOC_ARM_LITERAL:
21646     case BFD_RELOC_ARM_HWLITERAL:
21647       /* If this is called then the a literal has
21648          been referenced across a section boundary.  */
21649       as_bad_where (fixp->fx_file, fixp->fx_line,
21650                     _("literal referenced across section boundary"));
21651       return NULL;
21652
21653 #ifdef OBJ_ELF
21654     case BFD_RELOC_ARM_TLS_CALL:
21655     case BFD_RELOC_ARM_THM_TLS_CALL:
21656     case BFD_RELOC_ARM_TLS_DESCSEQ:
21657     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
21658     case BFD_RELOC_ARM_GOT32:
21659     case BFD_RELOC_ARM_GOTOFF:
21660     case BFD_RELOC_ARM_GOT_PREL:
21661     case BFD_RELOC_ARM_PLT32:
21662     case BFD_RELOC_ARM_TARGET1:
21663     case BFD_RELOC_ARM_ROSEGREL32:
21664     case BFD_RELOC_ARM_SBREL32:
21665     case BFD_RELOC_ARM_PREL31:
21666     case BFD_RELOC_ARM_TARGET2:
21667     case BFD_RELOC_ARM_TLS_LE32:
21668     case BFD_RELOC_ARM_TLS_LDO32:
21669     case BFD_RELOC_ARM_PCREL_CALL:
21670     case BFD_RELOC_ARM_PCREL_JUMP:
21671     case BFD_RELOC_ARM_ALU_PC_G0_NC:
21672     case BFD_RELOC_ARM_ALU_PC_G0:
21673     case BFD_RELOC_ARM_ALU_PC_G1_NC:
21674     case BFD_RELOC_ARM_ALU_PC_G1:
21675     case BFD_RELOC_ARM_ALU_PC_G2:
21676     case BFD_RELOC_ARM_LDR_PC_G0:
21677     case BFD_RELOC_ARM_LDR_PC_G1:
21678     case BFD_RELOC_ARM_LDR_PC_G2:
21679     case BFD_RELOC_ARM_LDRS_PC_G0:
21680     case BFD_RELOC_ARM_LDRS_PC_G1:
21681     case BFD_RELOC_ARM_LDRS_PC_G2:
21682     case BFD_RELOC_ARM_LDC_PC_G0:
21683     case BFD_RELOC_ARM_LDC_PC_G1:
21684     case BFD_RELOC_ARM_LDC_PC_G2:
21685     case BFD_RELOC_ARM_ALU_SB_G0_NC:
21686     case BFD_RELOC_ARM_ALU_SB_G0:
21687     case BFD_RELOC_ARM_ALU_SB_G1_NC:
21688     case BFD_RELOC_ARM_ALU_SB_G1:
21689     case BFD_RELOC_ARM_ALU_SB_G2:
21690     case BFD_RELOC_ARM_LDR_SB_G0:
21691     case BFD_RELOC_ARM_LDR_SB_G1:
21692     case BFD_RELOC_ARM_LDR_SB_G2:
21693     case BFD_RELOC_ARM_LDRS_SB_G0:
21694     case BFD_RELOC_ARM_LDRS_SB_G1:
21695     case BFD_RELOC_ARM_LDRS_SB_G2:
21696     case BFD_RELOC_ARM_LDC_SB_G0:
21697     case BFD_RELOC_ARM_LDC_SB_G1:
21698     case BFD_RELOC_ARM_LDC_SB_G2:
21699     case BFD_RELOC_ARM_V4BX:
21700       code = fixp->fx_r_type;
21701       break;
21702
21703     case BFD_RELOC_ARM_TLS_GOTDESC:
21704     case BFD_RELOC_ARM_TLS_GD32:
21705     case BFD_RELOC_ARM_TLS_IE32:
21706     case BFD_RELOC_ARM_TLS_LDM32:
21707       /* BFD will include the symbol's address in the addend.
21708          But we don't want that, so subtract it out again here.  */
21709       if (!S_IS_COMMON (fixp->fx_addsy))
21710         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
21711       code = fixp->fx_r_type;
21712       break;
21713 #endif
21714
21715     case BFD_RELOC_ARM_IMMEDIATE:
21716       as_bad_where (fixp->fx_file, fixp->fx_line,
21717                     _("internal relocation (type: IMMEDIATE) not fixed up"));
21718       return NULL;
21719
21720     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
21721       as_bad_where (fixp->fx_file, fixp->fx_line,
21722                     _("ADRL used for a symbol not defined in the same file"));
21723       return NULL;
21724
21725     case BFD_RELOC_ARM_OFFSET_IMM:
21726       if (section->use_rela_p)
21727         {
21728           code = fixp->fx_r_type;
21729           break;
21730         }
21731
21732       if (fixp->fx_addsy != NULL
21733           && !S_IS_DEFINED (fixp->fx_addsy)
21734           && S_IS_LOCAL (fixp->fx_addsy))
21735         {
21736           as_bad_where (fixp->fx_file, fixp->fx_line,
21737                         _("undefined local label `%s'"),
21738                         S_GET_NAME (fixp->fx_addsy));
21739           return NULL;
21740         }
21741
21742       as_bad_where (fixp->fx_file, fixp->fx_line,
21743                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
21744       return NULL;
21745
21746     default:
21747       {
21748         char * type;
21749
21750         switch (fixp->fx_r_type)
21751           {
21752           case BFD_RELOC_NONE:             type = "NONE";         break;
21753           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
21754           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
21755           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
21756           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
21757           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
21758           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
21759           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
21760           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
21761           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
21762           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
21763           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
21764           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
21765           default:                         type = _("<unknown>"); break;
21766           }
21767         as_bad_where (fixp->fx_file, fixp->fx_line,
21768                       _("cannot represent %s relocation in this object file format"),
21769                       type);
21770         return NULL;
21771       }
21772     }
21773
21774 #ifdef OBJ_ELF
21775   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
21776       && GOT_symbol
21777       && fixp->fx_addsy == GOT_symbol)
21778     {
21779       code = BFD_RELOC_ARM_GOTPC;
21780       reloc->addend = fixp->fx_offset = reloc->address;
21781     }
21782 #endif
21783
21784   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
21785
21786   if (reloc->howto == NULL)
21787     {
21788       as_bad_where (fixp->fx_file, fixp->fx_line,
21789                     _("cannot represent %s relocation in this object file format"),
21790                     bfd_get_reloc_code_name (code));
21791       return NULL;
21792     }
21793
21794   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
21795      vtable entry to be used in the relocation's section offset.  */
21796   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
21797     reloc->address = fixp->fx_offset;
21798
21799   return reloc;
21800 }
21801
21802 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
21803
21804 void
21805 cons_fix_new_arm (fragS *       frag,
21806                   int           where,
21807                   int           size,
21808                   expressionS * exp)
21809 {
21810   bfd_reloc_code_real_type type;
21811   int pcrel = 0;
21812
21813   /* Pick a reloc.
21814      FIXME: @@ Should look at CPU word size.  */
21815   switch (size)
21816     {
21817     case 1:
21818       type = BFD_RELOC_8;
21819       break;
21820     case 2:
21821       type = BFD_RELOC_16;
21822       break;
21823     case 4:
21824     default:
21825       type = BFD_RELOC_32;
21826       break;
21827     case 8:
21828       type = BFD_RELOC_64;
21829       break;
21830     }
21831
21832 #ifdef TE_PE
21833   if (exp->X_op == O_secrel)
21834   {
21835     exp->X_op = O_symbol;
21836     type = BFD_RELOC_32_SECREL;
21837   }
21838 #endif
21839
21840   fix_new_exp (frag, where, (int) size, exp, pcrel, type);
21841 }
21842
21843 #if defined (OBJ_COFF)
21844 void
21845 arm_validate_fix (fixS * fixP)
21846 {
21847   /* If the destination of the branch is a defined symbol which does not have
21848      the THUMB_FUNC attribute, then we must be calling a function which has
21849      the (interfacearm) attribute.  We look for the Thumb entry point to that
21850      function and change the branch to refer to that function instead.  */
21851   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
21852       && fixP->fx_addsy != NULL
21853       && S_IS_DEFINED (fixP->fx_addsy)
21854       && ! THUMB_IS_FUNC (fixP->fx_addsy))
21855     {
21856       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
21857     }
21858 }
21859 #endif
21860
21861
21862 int
21863 arm_force_relocation (struct fix * fixp)
21864 {
21865 #if defined (OBJ_COFF) && defined (TE_PE)
21866   if (fixp->fx_r_type == BFD_RELOC_RVA)
21867     return 1;
21868 #endif
21869
21870   /* In case we have a call or a branch to a function in ARM ISA mode from
21871      a thumb function or vice-versa force the relocation. These relocations
21872      are cleared off for some cores that might have blx and simple transformations
21873      are possible.  */
21874
21875 #ifdef OBJ_ELF
21876   switch (fixp->fx_r_type)
21877     {
21878     case BFD_RELOC_ARM_PCREL_JUMP:
21879     case BFD_RELOC_ARM_PCREL_CALL:
21880     case BFD_RELOC_THUMB_PCREL_BLX:
21881       if (THUMB_IS_FUNC (fixp->fx_addsy))
21882         return 1;
21883       break;
21884
21885     case BFD_RELOC_ARM_PCREL_BLX:
21886     case BFD_RELOC_THUMB_PCREL_BRANCH25:
21887     case BFD_RELOC_THUMB_PCREL_BRANCH20:
21888     case BFD_RELOC_THUMB_PCREL_BRANCH23:
21889       if (ARM_IS_FUNC (fixp->fx_addsy))
21890         return 1;
21891       break;
21892
21893     default:
21894       break;
21895     }
21896 #endif
21897
21898   /* Resolve these relocations even if the symbol is extern or weak.  */
21899   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
21900       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
21901       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
21902       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
21903       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
21904       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
21905       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12)
21906     return 0;
21907
21908   /* Always leave these relocations for the linker.  */
21909   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
21910        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
21911       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
21912     return 1;
21913
21914   /* Always generate relocations against function symbols.  */
21915   if (fixp->fx_r_type == BFD_RELOC_32
21916       && fixp->fx_addsy
21917       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
21918     return 1;
21919
21920   return generic_force_reloc (fixp);
21921 }
21922
21923 #if defined (OBJ_ELF) || defined (OBJ_COFF)
21924 /* Relocations against function names must be left unadjusted,
21925    so that the linker can use this information to generate interworking
21926    stubs.  The MIPS version of this function
21927    also prevents relocations that are mips-16 specific, but I do not
21928    know why it does this.
21929
21930    FIXME:
21931    There is one other problem that ought to be addressed here, but
21932    which currently is not:  Taking the address of a label (rather
21933    than a function) and then later jumping to that address.  Such
21934    addresses also ought to have their bottom bit set (assuming that
21935    they reside in Thumb code), but at the moment they will not.  */
21936
21937 bfd_boolean
21938 arm_fix_adjustable (fixS * fixP)
21939 {
21940   if (fixP->fx_addsy == NULL)
21941     return 1;
21942
21943   /* Preserve relocations against symbols with function type.  */
21944   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
21945     return FALSE;
21946
21947   if (THUMB_IS_FUNC (fixP->fx_addsy)
21948       && fixP->fx_subsy == NULL)
21949     return FALSE;
21950
21951   /* We need the symbol name for the VTABLE entries.  */
21952   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
21953       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
21954     return FALSE;
21955
21956   /* Don't allow symbols to be discarded on GOT related relocs.  */
21957   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
21958       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
21959       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
21960       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
21961       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
21962       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
21963       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
21964       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
21965       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
21966       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
21967       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
21968       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
21969       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
21970       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
21971     return FALSE;
21972
21973   /* Similarly for group relocations.  */
21974   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
21975        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
21976       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
21977     return FALSE;
21978
21979   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
21980   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
21981       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
21982       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
21983       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
21984       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
21985       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
21986       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
21987       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
21988     return FALSE;
21989
21990   return TRUE;
21991 }
21992 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
21993
21994 #ifdef OBJ_ELF
21995
21996 const char *
21997 elf32_arm_target_format (void)
21998 {
21999 #ifdef TE_SYMBIAN
22000   return (target_big_endian
22001           ? "elf32-bigarm-symbian"
22002           : "elf32-littlearm-symbian");
22003 #elif defined (TE_VXWORKS)
22004   return (target_big_endian
22005           ? "elf32-bigarm-vxworks"
22006           : "elf32-littlearm-vxworks");
22007 #else
22008   if (target_big_endian)
22009     return "elf32-bigarm";
22010   else
22011     return "elf32-littlearm";
22012 #endif
22013 }
22014
22015 void
22016 armelf_frob_symbol (symbolS * symp,
22017                     int *     puntp)
22018 {
22019   elf_frob_symbol (symp, puntp);
22020 }
22021 #endif
22022
22023 /* MD interface: Finalization.  */
22024
22025 void
22026 arm_cleanup (void)
22027 {
22028   literal_pool * pool;
22029
22030   /* Ensure that all the IT blocks are properly closed.  */
22031   check_it_blocks_finished ();
22032
22033   for (pool = list_of_pools; pool; pool = pool->next)
22034     {
22035       /* Put it at the end of the relevant section.  */
22036       subseg_set (pool->section, pool->sub_section);
22037 #ifdef OBJ_ELF
22038       arm_elf_change_section ();
22039 #endif
22040       s_ltorg (0);
22041     }
22042 }
22043
22044 #ifdef OBJ_ELF
22045 /* Remove any excess mapping symbols generated for alignment frags in
22046    SEC.  We may have created a mapping symbol before a zero byte
22047    alignment; remove it if there's a mapping symbol after the
22048    alignment.  */
22049 static void
22050 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
22051                        void *dummy ATTRIBUTE_UNUSED)
22052 {
22053   segment_info_type *seginfo = seg_info (sec);
22054   fragS *fragp;
22055
22056   if (seginfo == NULL || seginfo->frchainP == NULL)
22057     return;
22058
22059   for (fragp = seginfo->frchainP->frch_root;
22060        fragp != NULL;
22061        fragp = fragp->fr_next)
22062     {
22063       symbolS *sym = fragp->tc_frag_data.last_map;
22064       fragS *next = fragp->fr_next;
22065
22066       /* Variable-sized frags have been converted to fixed size by
22067          this point.  But if this was variable-sized to start with,
22068          there will be a fixed-size frag after it.  So don't handle
22069          next == NULL.  */
22070       if (sym == NULL || next == NULL)
22071         continue;
22072
22073       if (S_GET_VALUE (sym) < next->fr_address)
22074         /* Not at the end of this frag.  */
22075         continue;
22076       know (S_GET_VALUE (sym) == next->fr_address);
22077
22078       do
22079         {
22080           if (next->tc_frag_data.first_map != NULL)
22081             {
22082               /* Next frag starts with a mapping symbol.  Discard this
22083                  one.  */
22084               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
22085               break;
22086             }
22087
22088           if (next->fr_next == NULL)
22089             {
22090               /* This mapping symbol is at the end of the section.  Discard
22091                  it.  */
22092               know (next->fr_fix == 0 && next->fr_var == 0);
22093               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
22094               break;
22095             }
22096
22097           /* As long as we have empty frags without any mapping symbols,
22098              keep looking.  */
22099           /* If the next frag is non-empty and does not start with a
22100              mapping symbol, then this mapping symbol is required.  */
22101           if (next->fr_address != next->fr_next->fr_address)
22102             break;
22103
22104           next = next->fr_next;
22105         }
22106       while (next != NULL);
22107     }
22108 }
22109 #endif
22110
22111 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
22112    ARM ones.  */
22113
22114 void
22115 arm_adjust_symtab (void)
22116 {
22117 #ifdef OBJ_COFF
22118   symbolS * sym;
22119
22120   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
22121     {
22122       if (ARM_IS_THUMB (sym))
22123         {
22124           if (THUMB_IS_FUNC (sym))
22125             {
22126               /* Mark the symbol as a Thumb function.  */
22127               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
22128                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
22129                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
22130
22131               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
22132                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
22133               else
22134                 as_bad (_("%s: unexpected function type: %d"),
22135                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
22136             }
22137           else switch (S_GET_STORAGE_CLASS (sym))
22138             {
22139             case C_EXT:
22140               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
22141               break;
22142             case C_STAT:
22143               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
22144               break;
22145             case C_LABEL:
22146               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
22147               break;
22148             default:
22149               /* Do nothing.  */
22150               break;
22151             }
22152         }
22153
22154       if (ARM_IS_INTERWORK (sym))
22155         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
22156     }
22157 #endif
22158 #ifdef OBJ_ELF
22159   symbolS * sym;
22160   char      bind;
22161
22162   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
22163     {
22164       if (ARM_IS_THUMB (sym))
22165         {
22166           elf_symbol_type * elf_sym;
22167
22168           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
22169           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
22170
22171           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
22172                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
22173             {
22174               /* If it's a .thumb_func, declare it as so,
22175                  otherwise tag label as .code 16.  */
22176               if (THUMB_IS_FUNC (sym))
22177                 elf_sym->internal_elf_sym.st_target_internal
22178                   = ST_BRANCH_TO_THUMB;
22179               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
22180                 elf_sym->internal_elf_sym.st_info =
22181                   ELF_ST_INFO (bind, STT_ARM_16BIT);
22182             }
22183         }
22184     }
22185
22186   /* Remove any overlapping mapping symbols generated by alignment frags.  */
22187   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
22188   /* Now do generic ELF adjustments.  */
22189   elf_adjust_symtab ();
22190 #endif
22191 }
22192
22193 /* MD interface: Initialization.  */
22194
22195 static void
22196 set_constant_flonums (void)
22197 {
22198   int i;
22199
22200   for (i = 0; i < NUM_FLOAT_VALS; i++)
22201     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
22202       abort ();
22203 }
22204
22205 /* Auto-select Thumb mode if it's the only available instruction set for the
22206    given architecture.  */
22207
22208 static void
22209 autoselect_thumb_from_cpu_variant (void)
22210 {
22211   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
22212     opcode_select (16);
22213 }
22214
22215 void
22216 md_begin (void)
22217 {
22218   unsigned mach;
22219   unsigned int i;
22220
22221   if (   (arm_ops_hsh = hash_new ()) == NULL
22222       || (arm_cond_hsh = hash_new ()) == NULL
22223       || (arm_shift_hsh = hash_new ()) == NULL
22224       || (arm_psr_hsh = hash_new ()) == NULL
22225       || (arm_v7m_psr_hsh = hash_new ()) == NULL
22226       || (arm_reg_hsh = hash_new ()) == NULL
22227       || (arm_reloc_hsh = hash_new ()) == NULL
22228       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
22229     as_fatal (_("virtual memory exhausted"));
22230
22231   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
22232     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
22233   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
22234     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
22235   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
22236     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
22237   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
22238     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
22239   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
22240     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
22241                  (void *) (v7m_psrs + i));
22242   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
22243     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
22244   for (i = 0;
22245        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
22246        i++)
22247     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
22248                  (void *) (barrier_opt_names + i));
22249 #ifdef OBJ_ELF
22250   for (i = 0; i < sizeof (reloc_names) / sizeof (struct reloc_entry); i++)
22251     hash_insert (arm_reloc_hsh, reloc_names[i].name, (void *) (reloc_names + i));
22252 #endif
22253
22254   set_constant_flonums ();
22255
22256   /* Set the cpu variant based on the command-line options.  We prefer
22257      -mcpu= over -march= if both are set (as for GCC); and we prefer
22258      -mfpu= over any other way of setting the floating point unit.
22259      Use of legacy options with new options are faulted.  */
22260   if (legacy_cpu)
22261     {
22262       if (mcpu_cpu_opt || march_cpu_opt)
22263         as_bad (_("use of old and new-style options to set CPU type"));
22264
22265       mcpu_cpu_opt = legacy_cpu;
22266     }
22267   else if (!mcpu_cpu_opt)
22268     mcpu_cpu_opt = march_cpu_opt;
22269
22270   if (legacy_fpu)
22271     {
22272       if (mfpu_opt)
22273         as_bad (_("use of old and new-style options to set FPU type"));
22274
22275       mfpu_opt = legacy_fpu;
22276     }
22277   else if (!mfpu_opt)
22278     {
22279 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
22280         || defined (TE_NetBSD) || defined (TE_VXWORKS))
22281       /* Some environments specify a default FPU.  If they don't, infer it
22282          from the processor.  */
22283       if (mcpu_fpu_opt)
22284         mfpu_opt = mcpu_fpu_opt;
22285       else
22286         mfpu_opt = march_fpu_opt;
22287 #else
22288       mfpu_opt = &fpu_default;
22289 #endif
22290     }
22291
22292   if (!mfpu_opt)
22293     {
22294       if (mcpu_cpu_opt != NULL)
22295         mfpu_opt = &fpu_default;
22296       else if (mcpu_fpu_opt != NULL && ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
22297         mfpu_opt = &fpu_arch_vfp_v2;
22298       else
22299         mfpu_opt = &fpu_arch_fpa;
22300     }
22301
22302 #ifdef CPU_DEFAULT
22303   if (!mcpu_cpu_opt)
22304     {
22305       mcpu_cpu_opt = &cpu_default;
22306       selected_cpu = cpu_default;
22307     }
22308 #else
22309   if (mcpu_cpu_opt)
22310     selected_cpu = *mcpu_cpu_opt;
22311   else
22312     mcpu_cpu_opt = &arm_arch_any;
22313 #endif
22314
22315   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
22316
22317   autoselect_thumb_from_cpu_variant ();
22318
22319   arm_arch_used = thumb_arch_used = arm_arch_none;
22320
22321 #if defined OBJ_COFF || defined OBJ_ELF
22322   {
22323     unsigned int flags = 0;
22324
22325 #if defined OBJ_ELF
22326     flags = meabi_flags;
22327
22328     switch (meabi_flags)
22329       {
22330       case EF_ARM_EABI_UNKNOWN:
22331 #endif
22332         /* Set the flags in the private structure.  */
22333         if (uses_apcs_26)      flags |= F_APCS26;
22334         if (support_interwork) flags |= F_INTERWORK;
22335         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
22336         if (pic_code)          flags |= F_PIC;
22337         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
22338           flags |= F_SOFT_FLOAT;
22339
22340         switch (mfloat_abi_opt)
22341           {
22342           case ARM_FLOAT_ABI_SOFT:
22343           case ARM_FLOAT_ABI_SOFTFP:
22344             flags |= F_SOFT_FLOAT;
22345             break;
22346
22347           case ARM_FLOAT_ABI_HARD:
22348             if (flags & F_SOFT_FLOAT)
22349               as_bad (_("hard-float conflicts with specified fpu"));
22350             break;
22351           }
22352
22353         /* Using pure-endian doubles (even if soft-float).      */
22354         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
22355           flags |= F_VFP_FLOAT;
22356
22357 #if defined OBJ_ELF
22358         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
22359             flags |= EF_ARM_MAVERICK_FLOAT;
22360         break;
22361
22362       case EF_ARM_EABI_VER4:
22363       case EF_ARM_EABI_VER5:
22364         /* No additional flags to set.  */
22365         break;
22366
22367       default:
22368         abort ();
22369       }
22370 #endif
22371     bfd_set_private_flags (stdoutput, flags);
22372
22373     /* We have run out flags in the COFF header to encode the
22374        status of ATPCS support, so instead we create a dummy,
22375        empty, debug section called .arm.atpcs.  */
22376     if (atpcs)
22377       {
22378         asection * sec;
22379
22380         sec = bfd_make_section (stdoutput, ".arm.atpcs");
22381
22382         if (sec != NULL)
22383           {
22384             bfd_set_section_flags
22385               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
22386             bfd_set_section_size (stdoutput, sec, 0);
22387             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
22388           }
22389       }
22390   }
22391 #endif
22392
22393   /* Record the CPU type as well.  */
22394   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
22395     mach = bfd_mach_arm_iWMMXt2;
22396   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
22397     mach = bfd_mach_arm_iWMMXt;
22398   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
22399     mach = bfd_mach_arm_XScale;
22400   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
22401     mach = bfd_mach_arm_ep9312;
22402   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
22403     mach = bfd_mach_arm_5TE;
22404   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
22405     {
22406       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
22407         mach = bfd_mach_arm_5T;
22408       else
22409         mach = bfd_mach_arm_5;
22410     }
22411   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
22412     {
22413       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
22414         mach = bfd_mach_arm_4T;
22415       else
22416         mach = bfd_mach_arm_4;
22417     }
22418   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
22419     mach = bfd_mach_arm_3M;
22420   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
22421     mach = bfd_mach_arm_3;
22422   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
22423     mach = bfd_mach_arm_2a;
22424   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
22425     mach = bfd_mach_arm_2;
22426   else
22427     mach = bfd_mach_arm_unknown;
22428
22429   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
22430 }
22431
22432 /* Command line processing.  */
22433
22434 /* md_parse_option
22435       Invocation line includes a switch not recognized by the base assembler.
22436       See if it's a processor-specific option.
22437
22438       This routine is somewhat complicated by the need for backwards
22439       compatibility (since older releases of gcc can't be changed).
22440       The new options try to make the interface as compatible as
22441       possible with GCC.
22442
22443       New options (supported) are:
22444
22445               -mcpu=<cpu name>           Assemble for selected processor
22446               -march=<architecture name> Assemble for selected architecture
22447               -mfpu=<fpu architecture>   Assemble for selected FPU.
22448               -EB/-mbig-endian           Big-endian
22449               -EL/-mlittle-endian        Little-endian
22450               -k                         Generate PIC code
22451               -mthumb                    Start in Thumb mode
22452               -mthumb-interwork          Code supports ARM/Thumb interworking
22453
22454               -m[no-]warn-deprecated     Warn about deprecated features
22455
22456       For now we will also provide support for:
22457
22458               -mapcs-32                  32-bit Program counter
22459               -mapcs-26                  26-bit Program counter
22460               -macps-float               Floats passed in FP registers
22461               -mapcs-reentrant           Reentrant code
22462               -matpcs
22463       (sometime these will probably be replaced with -mapcs=<list of options>
22464       and -matpcs=<list of options>)
22465
22466       The remaining options are only supported for back-wards compatibility.
22467       Cpu variants, the arm part is optional:
22468               -m[arm]1                Currently not supported.
22469               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
22470               -m[arm]3                Arm 3 processor
22471               -m[arm]6[xx],           Arm 6 processors
22472               -m[arm]7[xx][t][[d]m]   Arm 7 processors
22473               -m[arm]8[10]            Arm 8 processors
22474               -m[arm]9[20][tdmi]      Arm 9 processors
22475               -mstrongarm[110[0]]     StrongARM processors
22476               -mxscale                XScale processors
22477               -m[arm]v[2345[t[e]]]    Arm architectures
22478               -mall                   All (except the ARM1)
22479       FP variants:
22480               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
22481               -mfpe-old               (No float load/store multiples)
22482               -mvfpxd                 VFP Single precision
22483               -mvfp                   All VFP
22484               -mno-fpu                Disable all floating point instructions
22485
22486       The following CPU names are recognized:
22487               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
22488               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
22489               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
22490               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
22491               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
22492               arm10t arm10e, arm1020t, arm1020e, arm10200e,
22493               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
22494
22495       */
22496
22497 const char * md_shortopts = "m:k";
22498
22499 #ifdef ARM_BI_ENDIAN
22500 #define OPTION_EB (OPTION_MD_BASE + 0)
22501 #define OPTION_EL (OPTION_MD_BASE + 1)
22502 #else
22503 #if TARGET_BYTES_BIG_ENDIAN
22504 #define OPTION_EB (OPTION_MD_BASE + 0)
22505 #else
22506 #define OPTION_EL (OPTION_MD_BASE + 1)
22507 #endif
22508 #endif
22509 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
22510
22511 struct option md_longopts[] =
22512 {
22513 #ifdef OPTION_EB
22514   {"EB", no_argument, NULL, OPTION_EB},
22515 #endif
22516 #ifdef OPTION_EL
22517   {"EL", no_argument, NULL, OPTION_EL},
22518 #endif
22519   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
22520   {NULL, no_argument, NULL, 0}
22521 };
22522
22523 size_t md_longopts_size = sizeof (md_longopts);
22524
22525 struct arm_option_table
22526 {
22527   char *option;         /* Option name to match.  */
22528   char *help;           /* Help information.  */
22529   int  *var;            /* Variable to change.  */
22530   int   value;          /* What to change it to.  */
22531   char *deprecated;     /* If non-null, print this message.  */
22532 };
22533
22534 struct arm_option_table arm_opts[] =
22535 {
22536   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
22537   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
22538   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
22539    &support_interwork, 1, NULL},
22540   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
22541   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
22542   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
22543    1, NULL},
22544   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
22545   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
22546   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
22547   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
22548    NULL},
22549
22550   /* These are recognized by the assembler, but have no affect on code.  */
22551   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
22552   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
22553
22554   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
22555   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
22556    &warn_on_deprecated, 0, NULL},
22557   {NULL, NULL, NULL, 0, NULL}
22558 };
22559
22560 struct arm_legacy_option_table
22561 {
22562   char *option;                         /* Option name to match.  */
22563   const arm_feature_set **var;          /* Variable to change.  */
22564   const arm_feature_set value;          /* What to change it to.  */
22565   char *deprecated;                     /* If non-null, print this message.  */
22566 };
22567
22568 const struct arm_legacy_option_table arm_legacy_opts[] =
22569 {
22570   /* DON'T add any new processors to this list -- we want the whole list
22571      to go away...  Add them to the processors table instead.  */
22572   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
22573   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
22574   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
22575   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
22576   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
22577   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
22578   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
22579   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
22580   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
22581   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
22582   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
22583   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
22584   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
22585   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
22586   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
22587   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
22588   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
22589   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
22590   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
22591   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
22592   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
22593   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
22594   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
22595   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
22596   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
22597   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
22598   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
22599   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
22600   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
22601   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
22602   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
22603   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
22604   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
22605   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
22606   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
22607   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
22608   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
22609   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
22610   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
22611   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
22612   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
22613   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
22614   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
22615   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
22616   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
22617   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
22618   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22619   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22620   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22621   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22622   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
22623   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
22624   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
22625   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
22626   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
22627   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
22628   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
22629   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
22630   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
22631   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
22632   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
22633   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
22634   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
22635   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
22636   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
22637   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
22638   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
22639   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
22640   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
22641   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
22642    N_("use -mcpu=strongarm110")},
22643   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
22644    N_("use -mcpu=strongarm1100")},
22645   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
22646    N_("use -mcpu=strongarm1110")},
22647   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
22648   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
22649   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
22650
22651   /* Architecture variants -- don't add any more to this list either.  */
22652   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
22653   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
22654   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
22655   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
22656   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
22657   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
22658   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
22659   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
22660   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
22661   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
22662   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
22663   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
22664   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
22665   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
22666   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
22667   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
22668   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
22669   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
22670
22671   /* Floating point variants -- don't add any more to this list either.  */
22672   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
22673   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
22674   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
22675   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
22676    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
22677
22678   {NULL, NULL, ARM_ARCH_NONE, NULL}
22679 };
22680
22681 struct arm_cpu_option_table
22682 {
22683   char *name;
22684   const arm_feature_set value;
22685   /* For some CPUs we assume an FPU unless the user explicitly sets
22686      -mfpu=...  */
22687   const arm_feature_set default_fpu;
22688   /* The canonical name of the CPU, or NULL to use NAME converted to upper
22689      case.  */
22690   const char *canonical_name;
22691 };
22692
22693 /* This list should, at a minimum, contain all the cpu names
22694    recognized by GCC.  */
22695 static const struct arm_cpu_option_table arm_cpus[] =
22696 {
22697   {"all",               ARM_ANY,         FPU_ARCH_FPA,    NULL},
22698   {"arm1",              ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL},
22699   {"arm2",              ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL},
22700   {"arm250",            ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
22701   {"arm3",              ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
22702   {"arm6",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22703   {"arm60",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22704   {"arm600",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22705   {"arm610",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22706   {"arm620",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22707   {"arm7",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22708   {"arm7m",             ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
22709   {"arm7d",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22710   {"arm7dm",            ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
22711   {"arm7di",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22712   {"arm7dmi",           ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
22713   {"arm70",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22714   {"arm700",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22715   {"arm700i",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22716   {"arm710",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22717   {"arm710t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22718   {"arm720",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22719   {"arm720t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22720   {"arm740t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22721   {"arm710c",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22722   {"arm7100",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22723   {"arm7500",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22724   {"arm7500fe",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22725   {"arm7t",             ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22726   {"arm7tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22727   {"arm7tdmi-s",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22728   {"arm8",              ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22729   {"arm810",            ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22730   {"strongarm",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22731   {"strongarm1",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22732   {"strongarm110",      ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22733   {"strongarm1100",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22734   {"strongarm1110",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22735   {"arm9",              ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22736   {"arm920",            ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"},
22737   {"arm920t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22738   {"arm922t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22739   {"arm940t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22740   {"arm9tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22741   {"fa526",             ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22742   {"fa626",             ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22743   /* For V5 or later processors we default to using VFP; but the user
22744      should really set the FPU type explicitly.  */
22745   {"arm9e-r0",          ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
22746   {"arm9e",             ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22747   {"arm926ej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
22748   {"arm926ejs",         ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
22749   {"arm926ej-s",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
22750   {"arm946e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
22751   {"arm946e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"},
22752   {"arm946e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22753   {"arm966e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
22754   {"arm966e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"},
22755   {"arm966e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22756   {"arm968e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22757   {"arm10t",            ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
22758   {"arm10tdmi",         ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
22759   {"arm10e",            ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22760   {"arm1020",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"},
22761   {"arm1020t",          ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
22762   {"arm1020e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22763   {"arm1022e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22764   {"arm1026ejs",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM1026EJ-S"},
22765   {"arm1026ej-s",       ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
22766   {"fa606te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22767   {"fa616te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22768   {"fa626te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22769   {"fmp626",            ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22770   {"fa726te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22771   {"arm1136js",         ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"},
22772   {"arm1136j-s",        ARM_ARCH_V6,     FPU_NONE,        NULL},
22773   {"arm1136jfs",        ARM_ARCH_V6,     FPU_ARCH_VFP_V2, "ARM1136JF-S"},
22774   {"arm1136jf-s",       ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL},
22775   {"mpcore",            ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, "MPCore"},
22776   {"mpcorenovfp",       ARM_ARCH_V6K,    FPU_NONE,        "MPCore"},
22777   {"arm1156t2-s",       ARM_ARCH_V6T2,   FPU_NONE,        NULL},
22778   {"arm1156t2f-s",      ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL},
22779   {"arm1176jz-s",       ARM_ARCH_V6ZK,   FPU_NONE,        NULL},
22780   {"arm1176jzf-s",      ARM_ARCH_V6ZK,   FPU_ARCH_VFP_V2, NULL},
22781   {"cortex-a5",         ARM_ARCH_V7A_MP_SEC, 
22782                                          FPU_NONE,        "Cortex-A5"},
22783   {"cortex-a8",         ARM_ARCH_V7A_SEC,
22784                                          ARM_FEATURE (0, FPU_VFP_V3
22785                                                         | FPU_NEON_EXT_V1),
22786                                                           "Cortex-A8"},
22787   {"cortex-a9",         ARM_ARCH_V7A_MP_SEC,
22788                                          ARM_FEATURE (0, FPU_VFP_V3
22789                                                         | FPU_NEON_EXT_V1),
22790                                                           "Cortex-A9"},
22791   {"cortex-a15",        ARM_ARCH_V7A_IDIV_MP_SEC_VIRT,
22792                                          FPU_ARCH_NEON_VFP_V4,
22793                                                           "Cortex-A15"},
22794   {"cortex-r4",         ARM_ARCH_V7R,    FPU_NONE,        "Cortex-R4"},
22795   {"cortex-r4f",        ARM_ARCH_V7R,    FPU_ARCH_VFP_V3D16,
22796                                                           "Cortex-R4F"},
22797   {"cortex-m4",         ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M4"},
22798   {"cortex-m3",         ARM_ARCH_V7M,    FPU_NONE,        "Cortex-M3"},
22799   {"cortex-m1",         ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M1"},
22800   {"cortex-m0",         ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0"},
22801   /* ??? XSCALE is really an architecture.  */
22802   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
22803   /* ??? iwmmxt is not a processor.  */
22804   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL},
22805   {"iwmmxt2",           ARM_ARCH_IWMMXT2,FPU_ARCH_VFP_V2, NULL},
22806   {"i80200",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
22807   /* Maverick */
22808   {"ep9312",    ARM_FEATURE (ARM_AEXT_V4T, ARM_CEXT_MAVERICK), FPU_ARCH_MAVERICK, "ARM920T"},
22809   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE, NULL}
22810 };
22811
22812 struct arm_arch_option_table
22813 {
22814   char *name;
22815   const arm_feature_set value;
22816   const arm_feature_set default_fpu;
22817 };
22818
22819 /* This list should, at a minimum, contain all the architecture names
22820    recognized by GCC.  */
22821 static const struct arm_arch_option_table arm_archs[] =
22822 {
22823   {"all",               ARM_ANY,         FPU_ARCH_FPA},
22824   {"armv1",             ARM_ARCH_V1,     FPU_ARCH_FPA},
22825   {"armv2",             ARM_ARCH_V2,     FPU_ARCH_FPA},
22826   {"armv2a",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
22827   {"armv2s",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
22828   {"armv3",             ARM_ARCH_V3,     FPU_ARCH_FPA},
22829   {"armv3m",            ARM_ARCH_V3M,    FPU_ARCH_FPA},
22830   {"armv4",             ARM_ARCH_V4,     FPU_ARCH_FPA},
22831   {"armv4xm",           ARM_ARCH_V4xM,   FPU_ARCH_FPA},
22832   {"armv4t",            ARM_ARCH_V4T,    FPU_ARCH_FPA},
22833   {"armv4txm",          ARM_ARCH_V4TxM,  FPU_ARCH_FPA},
22834   {"armv5",             ARM_ARCH_V5,     FPU_ARCH_VFP},
22835   {"armv5t",            ARM_ARCH_V5T,    FPU_ARCH_VFP},
22836   {"armv5txm",          ARM_ARCH_V5TxM,  FPU_ARCH_VFP},
22837   {"armv5te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP},
22838   {"armv5texp",         ARM_ARCH_V5TExP, FPU_ARCH_VFP},
22839   {"armv5tej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP},
22840   {"armv6",             ARM_ARCH_V6,     FPU_ARCH_VFP},
22841   {"armv6j",            ARM_ARCH_V6,     FPU_ARCH_VFP},
22842   {"armv6k",            ARM_ARCH_V6K,    FPU_ARCH_VFP},
22843   {"armv6z",            ARM_ARCH_V6Z,    FPU_ARCH_VFP},
22844   {"armv6zk",           ARM_ARCH_V6ZK,   FPU_ARCH_VFP},
22845   {"armv6t2",           ARM_ARCH_V6T2,   FPU_ARCH_VFP},
22846   {"armv6kt2",          ARM_ARCH_V6KT2,  FPU_ARCH_VFP},
22847   {"armv6zt2",          ARM_ARCH_V6ZT2,  FPU_ARCH_VFP},
22848   {"armv6zkt2",         ARM_ARCH_V6ZKT2, FPU_ARCH_VFP},
22849   {"armv6-m",           ARM_ARCH_V6M,    FPU_ARCH_VFP},
22850   {"armv6s-m",          ARM_ARCH_V6SM,   FPU_ARCH_VFP},
22851   {"armv7",             ARM_ARCH_V7,     FPU_ARCH_VFP},
22852   /* The official spelling of the ARMv7 profile variants is the dashed form.
22853      Accept the non-dashed form for compatibility with old toolchains.  */
22854   {"armv7a",            ARM_ARCH_V7A,    FPU_ARCH_VFP},
22855   {"armv7r",            ARM_ARCH_V7R,    FPU_ARCH_VFP},
22856   {"armv7m",            ARM_ARCH_V7M,    FPU_ARCH_VFP},
22857   {"armv7-a",           ARM_ARCH_V7A,    FPU_ARCH_VFP},
22858   {"armv7-r",           ARM_ARCH_V7R,    FPU_ARCH_VFP},
22859   {"armv7-m",           ARM_ARCH_V7M,    FPU_ARCH_VFP},
22860   {"armv7e-m",          ARM_ARCH_V7EM,   FPU_ARCH_VFP},
22861   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP},
22862   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP},
22863   {"iwmmxt2",           ARM_ARCH_IWMMXT2,FPU_ARCH_VFP},
22864   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE}
22865 };
22866
22867 /* ISA extensions in the co-processor and main instruction set space.  */
22868 struct arm_option_extension_value_table
22869 {
22870   char *name;
22871   const arm_feature_set value;
22872   const arm_feature_set allowed_archs;
22873 };
22874
22875 /* The following table must be in alphabetical order with a NULL last entry.
22876    */
22877 static const struct arm_option_extension_value_table arm_extensions[] =
22878 {
22879   {"idiv",      ARM_FEATURE (ARM_EXT_ADIV | ARM_EXT_DIV, 0),
22880                                    ARM_FEATURE (ARM_EXT_V7A, 0)},
22881   {"iwmmxt",    ARM_FEATURE (0, ARM_CEXT_IWMMXT),       ARM_ANY},
22882   {"iwmmxt2",   ARM_FEATURE (0, ARM_CEXT_IWMMXT2),      ARM_ANY},
22883   {"maverick",  ARM_FEATURE (0, ARM_CEXT_MAVERICK),     ARM_ANY},
22884   {"mp",        ARM_FEATURE (ARM_EXT_MP, 0),
22885                      ARM_FEATURE (ARM_EXT_V7A | ARM_EXT_V7R, 0)},
22886   {"os",        ARM_FEATURE (ARM_EXT_OS, 0),
22887                                    ARM_FEATURE (ARM_EXT_V6M, 0)},
22888   {"sec",       ARM_FEATURE (ARM_EXT_SEC, 0),
22889                      ARM_FEATURE (ARM_EXT_V6K | ARM_EXT_V7A, 0)},
22890   {"virt",      ARM_FEATURE (ARM_EXT_VIRT | ARM_EXT_ADIV | ARM_EXT_DIV, 0),
22891                                    ARM_FEATURE (ARM_EXT_V7A, 0)},
22892   {"xscale",    ARM_FEATURE (0, ARM_CEXT_XSCALE),       ARM_ANY},
22893   {NULL,        ARM_ARCH_NONE,                    ARM_ARCH_NONE}
22894 };
22895
22896 /* ISA floating-point and Advanced SIMD extensions.  */
22897 struct arm_option_fpu_value_table
22898 {
22899   char *name;
22900   const arm_feature_set value;
22901 };
22902
22903 /* This list should, at a minimum, contain all the fpu names
22904    recognized by GCC.  */
22905 static const struct arm_option_fpu_value_table arm_fpus[] =
22906 {
22907   {"softfpa",           FPU_NONE},
22908   {"fpe",               FPU_ARCH_FPE},
22909   {"fpe2",              FPU_ARCH_FPE},
22910   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
22911   {"fpa",               FPU_ARCH_FPA},
22912   {"fpa10",             FPU_ARCH_FPA},
22913   {"fpa11",             FPU_ARCH_FPA},
22914   {"arm7500fe",         FPU_ARCH_FPA},
22915   {"softvfp",           FPU_ARCH_VFP},
22916   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
22917   {"vfp",               FPU_ARCH_VFP_V2},
22918   {"vfp9",              FPU_ARCH_VFP_V2},
22919   {"vfp3",              FPU_ARCH_VFP_V3}, /* For backwards compatbility.  */
22920   {"vfp10",             FPU_ARCH_VFP_V2},
22921   {"vfp10-r0",          FPU_ARCH_VFP_V1},
22922   {"vfpxd",             FPU_ARCH_VFP_V1xD},
22923   {"vfpv2",             FPU_ARCH_VFP_V2},
22924   {"vfpv3",             FPU_ARCH_VFP_V3},
22925   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
22926   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
22927   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
22928   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
22929   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
22930   {"arm1020t",          FPU_ARCH_VFP_V1},
22931   {"arm1020e",          FPU_ARCH_VFP_V2},
22932   {"arm1136jfs",        FPU_ARCH_VFP_V2},
22933   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
22934   {"maverick",          FPU_ARCH_MAVERICK},
22935   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
22936   {"neon-fp16",         FPU_ARCH_NEON_FP16},
22937   {"vfpv4",             FPU_ARCH_VFP_V4},
22938   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
22939   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
22940   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
22941   {NULL,                ARM_ARCH_NONE}
22942 };
22943
22944 struct arm_option_value_table
22945 {
22946   char *name;
22947   long value;
22948 };
22949
22950 static const struct arm_option_value_table arm_float_abis[] =
22951 {
22952   {"hard",      ARM_FLOAT_ABI_HARD},
22953   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
22954   {"soft",      ARM_FLOAT_ABI_SOFT},
22955   {NULL,        0}
22956 };
22957
22958 #ifdef OBJ_ELF
22959 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
22960 static const struct arm_option_value_table arm_eabis[] =
22961 {
22962   {"gnu",       EF_ARM_EABI_UNKNOWN},
22963   {"4",         EF_ARM_EABI_VER4},
22964   {"5",         EF_ARM_EABI_VER5},
22965   {NULL,        0}
22966 };
22967 #endif
22968
22969 struct arm_long_option_table
22970 {
22971   char * option;                /* Substring to match.  */
22972   char * help;                  /* Help information.  */
22973   int (* func) (char * subopt); /* Function to decode sub-option.  */
22974   char * deprecated;            /* If non-null, print this message.  */
22975 };
22976
22977 static bfd_boolean
22978 arm_parse_extension (char * str, const arm_feature_set **opt_p)
22979 {
22980   arm_feature_set *ext_set = (arm_feature_set *)
22981       xmalloc (sizeof (arm_feature_set));
22982
22983   /* We insist on extensions being specified in alphabetical order, and with
22984      extensions being added before being removed.  We achieve this by having 
22985      the global ARM_EXTENSIONS table in alphabetical order, and using the 
22986      ADDING_VALUE variable to indicate whether we are adding an extension (1)
22987      or removing it (0) and only allowing it to change in the order 
22988      -1 -> 1 -> 0.  */
22989   const struct arm_option_extension_value_table * opt = NULL;
22990   int adding_value = -1;
22991
22992   /* Copy the feature set, so that we can modify it.  */
22993   *ext_set = **opt_p;
22994   *opt_p = ext_set;
22995
22996   while (str != NULL && *str != 0)
22997     {
22998       char * ext;
22999       size_t optlen;
23000
23001       if (*str != '+')
23002         {
23003           as_bad (_("invalid architectural extension"));
23004           return FALSE;
23005         }
23006
23007       str++;
23008       ext = strchr (str, '+');
23009
23010       if (ext != NULL)
23011         optlen = ext - str;
23012       else
23013         optlen = strlen (str);
23014
23015       if (optlen >= 2
23016           && strncmp (str, "no", 2) == 0)
23017         {
23018           if (adding_value != 0)
23019             {
23020               adding_value = 0;
23021               opt = arm_extensions;
23022             }
23023
23024           optlen -= 2;
23025           str += 2;
23026         }
23027       else if (optlen > 0)
23028         {
23029           if (adding_value == -1)
23030             {
23031               adding_value = 1;
23032               opt = arm_extensions;
23033             }
23034           else if (adding_value != 1)
23035             {
23036               as_bad (_("must specify extensions to add before specifying "
23037                         "those to remove"));
23038               return FALSE;
23039             }
23040         }
23041
23042       if (optlen == 0)
23043         {
23044           as_bad (_("missing architectural extension"));
23045           return FALSE;
23046         }
23047
23048       gas_assert (adding_value != -1);
23049       gas_assert (opt != NULL);
23050
23051       /* Scan over the options table trying to find an exact match. */
23052       for (; opt->name != NULL; opt++)
23053         if (strncmp (opt->name, str, optlen) == 0
23054             && strlen (opt->name) == optlen)
23055           {
23056             /* Check we can apply the extension to this architecture.  */
23057             if (!ARM_CPU_HAS_FEATURE (*ext_set, opt->allowed_archs))
23058               {
23059                 as_bad (_("extension does not apply to the base architecture"));
23060                 return FALSE;
23061               }
23062
23063             /* Add or remove the extension.  */
23064             if (adding_value)
23065               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->value);
23066             else
23067               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->value);
23068
23069             break;
23070           }
23071
23072       if (opt->name == NULL)
23073         {
23074           /* Did we fail to find an extension because it wasn't specified in
23075              alphabetical order, or because it does not exist?  */
23076
23077           for (opt = arm_extensions; opt->name != NULL; opt++)
23078             if (strncmp (opt->name, str, optlen) == 0)
23079               break;
23080
23081           if (opt->name == NULL)
23082             as_bad (_("unknown architectural extension `%s'"), str);
23083           else
23084             as_bad (_("architectural extensions must be specified in "
23085                       "alphabetical order"));
23086
23087           return FALSE;
23088         }
23089       else
23090         {
23091           /* We should skip the extension we've just matched the next time
23092              round.  */
23093           opt++;
23094         }
23095
23096       str = ext;
23097     };
23098
23099   return TRUE;
23100 }
23101
23102 static bfd_boolean
23103 arm_parse_cpu (char * str)
23104 {
23105   const struct arm_cpu_option_table * opt;
23106   char * ext = strchr (str, '+');
23107   int optlen;
23108
23109   if (ext != NULL)
23110     optlen = ext - str;
23111   else
23112     optlen = strlen (str);
23113
23114   if (optlen == 0)
23115     {
23116       as_bad (_("missing cpu name `%s'"), str);
23117       return FALSE;
23118     }
23119
23120   for (opt = arm_cpus; opt->name != NULL; opt++)
23121     if (strncmp (opt->name, str, optlen) == 0)
23122       {
23123         mcpu_cpu_opt = &opt->value;
23124         mcpu_fpu_opt = &opt->default_fpu;
23125         if (opt->canonical_name)
23126           strcpy (selected_cpu_name, opt->canonical_name);
23127         else
23128           {
23129             int i;
23130
23131             for (i = 0; i < optlen; i++)
23132               selected_cpu_name[i] = TOUPPER (opt->name[i]);
23133             selected_cpu_name[i] = 0;
23134           }
23135
23136         if (ext != NULL)
23137           return arm_parse_extension (ext, &mcpu_cpu_opt);
23138
23139         return TRUE;
23140       }
23141
23142   as_bad (_("unknown cpu `%s'"), str);
23143   return FALSE;
23144 }
23145
23146 static bfd_boolean
23147 arm_parse_arch (char * str)
23148 {
23149   const struct arm_arch_option_table *opt;
23150   char *ext = strchr (str, '+');
23151   int optlen;
23152
23153   if (ext != NULL)
23154     optlen = ext - str;
23155   else
23156     optlen = strlen (str);
23157
23158   if (optlen == 0)
23159     {
23160       as_bad (_("missing architecture name `%s'"), str);
23161       return FALSE;
23162     }
23163
23164   for (opt = arm_archs; opt->name != NULL; opt++)
23165     if (strncmp (opt->name, str, optlen) == 0)
23166       {
23167         march_cpu_opt = &opt->value;
23168         march_fpu_opt = &opt->default_fpu;
23169         strcpy (selected_cpu_name, opt->name);
23170
23171         if (ext != NULL)
23172           return arm_parse_extension (ext, &march_cpu_opt);
23173
23174         return TRUE;
23175       }
23176
23177   as_bad (_("unknown architecture `%s'\n"), str);
23178   return FALSE;
23179 }
23180
23181 static bfd_boolean
23182 arm_parse_fpu (char * str)
23183 {
23184   const struct arm_option_fpu_value_table * opt;
23185
23186   for (opt = arm_fpus; opt->name != NULL; opt++)
23187     if (streq (opt->name, str))
23188       {
23189         mfpu_opt = &opt->value;
23190         return TRUE;
23191       }
23192
23193   as_bad (_("unknown floating point format `%s'\n"), str);
23194   return FALSE;
23195 }
23196
23197 static bfd_boolean
23198 arm_parse_float_abi (char * str)
23199 {
23200   const struct arm_option_value_table * opt;
23201
23202   for (opt = arm_float_abis; opt->name != NULL; opt++)
23203     if (streq (opt->name, str))
23204       {
23205         mfloat_abi_opt = opt->value;
23206         return TRUE;
23207       }
23208
23209   as_bad (_("unknown floating point abi `%s'\n"), str);
23210   return FALSE;
23211 }
23212
23213 #ifdef OBJ_ELF
23214 static bfd_boolean
23215 arm_parse_eabi (char * str)
23216 {
23217   const struct arm_option_value_table *opt;
23218
23219   for (opt = arm_eabis; opt->name != NULL; opt++)
23220     if (streq (opt->name, str))
23221       {
23222         meabi_flags = opt->value;
23223         return TRUE;
23224       }
23225   as_bad (_("unknown EABI `%s'\n"), str);
23226   return FALSE;
23227 }
23228 #endif
23229
23230 static bfd_boolean
23231 arm_parse_it_mode (char * str)
23232 {
23233   bfd_boolean ret = TRUE;
23234
23235   if (streq ("arm", str))
23236     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
23237   else if (streq ("thumb", str))
23238     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
23239   else if (streq ("always", str))
23240     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
23241   else if (streq ("never", str))
23242     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
23243   else
23244     {
23245       as_bad (_("unknown implicit IT mode `%s', should be "\
23246                 "arm, thumb, always, or never."), str);
23247       ret = FALSE;
23248     }
23249
23250   return ret;
23251 }
23252
23253 struct arm_long_option_table arm_long_opts[] =
23254 {
23255   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
23256    arm_parse_cpu, NULL},
23257   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
23258    arm_parse_arch, NULL},
23259   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
23260    arm_parse_fpu, NULL},
23261   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
23262    arm_parse_float_abi, NULL},
23263 #ifdef OBJ_ELF
23264   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
23265    arm_parse_eabi, NULL},
23266 #endif
23267   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
23268    arm_parse_it_mode, NULL},
23269   {NULL, NULL, 0, NULL}
23270 };
23271
23272 int
23273 md_parse_option (int c, char * arg)
23274 {
23275   struct arm_option_table *opt;
23276   const struct arm_legacy_option_table *fopt;
23277   struct arm_long_option_table *lopt;
23278
23279   switch (c)
23280     {
23281 #ifdef OPTION_EB
23282     case OPTION_EB:
23283       target_big_endian = 1;
23284       break;
23285 #endif
23286
23287 #ifdef OPTION_EL
23288     case OPTION_EL:
23289       target_big_endian = 0;
23290       break;
23291 #endif
23292
23293     case OPTION_FIX_V4BX:
23294       fix_v4bx = TRUE;
23295       break;
23296
23297     case 'a':
23298       /* Listing option.  Just ignore these, we don't support additional
23299          ones.  */
23300       return 0;
23301
23302     default:
23303       for (opt = arm_opts; opt->option != NULL; opt++)
23304         {
23305           if (c == opt->option[0]
23306               && ((arg == NULL && opt->option[1] == 0)
23307                   || streq (arg, opt->option + 1)))
23308             {
23309               /* If the option is deprecated, tell the user.  */
23310               if (warn_on_deprecated && opt->deprecated != NULL)
23311                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
23312                            arg ? arg : "", _(opt->deprecated));
23313
23314               if (opt->var != NULL)
23315                 *opt->var = opt->value;
23316
23317               return 1;
23318             }
23319         }
23320
23321       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
23322         {
23323           if (c == fopt->option[0]
23324               && ((arg == NULL && fopt->option[1] == 0)
23325                   || streq (arg, fopt->option + 1)))
23326             {
23327               /* If the option is deprecated, tell the user.  */
23328               if (warn_on_deprecated && fopt->deprecated != NULL)
23329                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
23330                            arg ? arg : "", _(fopt->deprecated));
23331
23332               if (fopt->var != NULL)
23333                 *fopt->var = &fopt->value;
23334
23335               return 1;
23336             }
23337         }
23338
23339       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
23340         {
23341           /* These options are expected to have an argument.  */
23342           if (c == lopt->option[0]
23343               && arg != NULL
23344               && strncmp (arg, lopt->option + 1,
23345                           strlen (lopt->option + 1)) == 0)
23346             {
23347               /* If the option is deprecated, tell the user.  */
23348               if (warn_on_deprecated && lopt->deprecated != NULL)
23349                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
23350                            _(lopt->deprecated));
23351
23352               /* Call the sup-option parser.  */
23353               return lopt->func (arg + strlen (lopt->option) - 1);
23354             }
23355         }
23356
23357       return 0;
23358     }
23359
23360   return 1;
23361 }
23362
23363 void
23364 md_show_usage (FILE * fp)
23365 {
23366   struct arm_option_table *opt;
23367   struct arm_long_option_table *lopt;
23368
23369   fprintf (fp, _(" ARM-specific assembler options:\n"));
23370
23371   for (opt = arm_opts; opt->option != NULL; opt++)
23372     if (opt->help != NULL)
23373       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
23374
23375   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
23376     if (lopt->help != NULL)
23377       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
23378
23379 #ifdef OPTION_EB
23380   fprintf (fp, _("\
23381   -EB                     assemble code for a big-endian cpu\n"));
23382 #endif
23383
23384 #ifdef OPTION_EL
23385   fprintf (fp, _("\
23386   -EL                     assemble code for a little-endian cpu\n"));
23387 #endif
23388
23389   fprintf (fp, _("\
23390   --fix-v4bx              Allow BX in ARMv4 code\n"));
23391 }
23392
23393
23394 #ifdef OBJ_ELF
23395 typedef struct
23396 {
23397   int val;
23398   arm_feature_set flags;
23399 } cpu_arch_ver_table;
23400
23401 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
23402    least features first.  */
23403 static const cpu_arch_ver_table cpu_arch_ver[] =
23404 {
23405     {1, ARM_ARCH_V4},
23406     {2, ARM_ARCH_V4T},
23407     {3, ARM_ARCH_V5},
23408     {3, ARM_ARCH_V5T},
23409     {4, ARM_ARCH_V5TE},
23410     {5, ARM_ARCH_V5TEJ},
23411     {6, ARM_ARCH_V6},
23412     {9, ARM_ARCH_V6K},
23413     {7, ARM_ARCH_V6Z},
23414     {11, ARM_ARCH_V6M},
23415     {12, ARM_ARCH_V6SM},
23416     {8, ARM_ARCH_V6T2},
23417     {10, ARM_ARCH_V7A},
23418     {10, ARM_ARCH_V7R},
23419     {10, ARM_ARCH_V7M},
23420     {0, ARM_ARCH_NONE}
23421 };
23422
23423 /* Set an attribute if it has not already been set by the user.  */
23424 static void
23425 aeabi_set_attribute_int (int tag, int value)
23426 {
23427   if (tag < 1
23428       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
23429       || !attributes_set_explicitly[tag])
23430     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
23431 }
23432
23433 static void
23434 aeabi_set_attribute_string (int tag, const char *value)
23435 {
23436   if (tag < 1
23437       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
23438       || !attributes_set_explicitly[tag])
23439     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
23440 }
23441
23442 /* Set the public EABI object attributes.  */
23443 static void
23444 aeabi_set_public_attributes (void)
23445 {
23446   int arch;
23447   int virt_sec = 0;
23448   arm_feature_set flags;
23449   arm_feature_set tmp;
23450   const cpu_arch_ver_table *p;
23451
23452   /* Choose the architecture based on the capabilities of the requested cpu
23453      (if any) and/or the instructions actually used.  */
23454   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
23455   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
23456   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
23457   /*Allow the user to override the reported architecture.  */
23458   if (object_arch)
23459     {
23460       ARM_CLEAR_FEATURE (flags, flags, arm_arch_any);
23461       ARM_MERGE_FEATURE_SETS (flags, flags, *object_arch);
23462     }
23463
23464   /* We need to make sure that the attributes do not identify us as v6S-M
23465      when the only v6S-M feature in use is the Operating System Extensions.  */
23466   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_os))
23467       if (!ARM_CPU_HAS_FEATURE (flags, arm_arch_v6m_only))
23468         ARM_CLEAR_FEATURE (flags, flags, arm_ext_os);
23469
23470   tmp = flags;
23471   arch = 0;
23472   for (p = cpu_arch_ver; p->val; p++)
23473     {
23474       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
23475         {
23476           arch = p->val;
23477           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
23478         }
23479     }
23480
23481   /* The table lookup above finds the last architecture to contribute
23482      a new feature.  Unfortunately, Tag13 is a subset of the union of
23483      v6T2 and v7-M, so it is never seen as contributing a new feature.
23484      We can not search for the last entry which is entirely used,
23485      because if no CPU is specified we build up only those flags
23486      actually used.  Perhaps we should separate out the specified
23487      and implicit cases.  Avoid taking this path for -march=all by
23488      checking for contradictory v7-A / v7-M features.  */
23489   if (arch == 10
23490       && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
23491       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m)
23492       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v6_dsp))
23493     arch = 13;
23494
23495   /* Tag_CPU_name.  */
23496   if (selected_cpu_name[0])
23497     {
23498       char *q;
23499
23500       q = selected_cpu_name;
23501       if (strncmp (q, "armv", 4) == 0)
23502         {
23503           int i;
23504
23505           q += 4;
23506           for (i = 0; q[i]; i++)
23507             q[i] = TOUPPER (q[i]);
23508         }
23509       aeabi_set_attribute_string (Tag_CPU_name, q);
23510     }
23511
23512   /* Tag_CPU_arch.  */
23513   aeabi_set_attribute_int (Tag_CPU_arch, arch);
23514
23515   /* Tag_CPU_arch_profile.  */
23516   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a))
23517     aeabi_set_attribute_int (Tag_CPU_arch_profile, 'A');
23518   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
23519     aeabi_set_attribute_int (Tag_CPU_arch_profile, 'R');
23520   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_m))
23521     aeabi_set_attribute_int (Tag_CPU_arch_profile, 'M');
23522
23523   /* Tag_ARM_ISA_use.  */
23524   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
23525       || arch == 0)
23526     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
23527
23528   /* Tag_THUMB_ISA_use.  */
23529   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
23530       || arch == 0)
23531     aeabi_set_attribute_int (Tag_THUMB_ISA_use,
23532         ARM_CPU_HAS_FEATURE (flags, arm_arch_t2) ? 2 : 1);
23533
23534   /* Tag_VFP_arch.  */
23535   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
23536     aeabi_set_attribute_int (Tag_VFP_arch,
23537                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
23538                              ? 5 : 6);
23539   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
23540     aeabi_set_attribute_int (Tag_VFP_arch, 3);
23541   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
23542     aeabi_set_attribute_int (Tag_VFP_arch, 4);
23543   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
23544     aeabi_set_attribute_int (Tag_VFP_arch, 2);
23545   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
23546            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
23547     aeabi_set_attribute_int (Tag_VFP_arch, 1);
23548
23549   /* Tag_ABI_HardFP_use.  */
23550   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
23551       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
23552     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
23553
23554   /* Tag_WMMX_arch.  */
23555   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
23556     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
23557   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
23558     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
23559
23560   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
23561   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
23562     aeabi_set_attribute_int
23563       (Tag_Advanced_SIMD_arch, (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma)
23564                                 ? 2 : 1));
23565   
23566   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
23567   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16))
23568     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
23569
23570   /* Tag_DIV_use.  */
23571   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv))
23572     aeabi_set_attribute_int (Tag_DIV_use, 2);
23573   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_div))
23574     aeabi_set_attribute_int (Tag_DIV_use, 0);
23575   else
23576     aeabi_set_attribute_int (Tag_DIV_use, 1);
23577
23578   /* Tag_MP_extension_use.  */
23579   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
23580     aeabi_set_attribute_int (Tag_MPextension_use, 1);
23581
23582   /* Tag Virtualization_use.  */
23583   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
23584     virt_sec |= 1;
23585   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
23586     virt_sec |= 2;
23587   if (virt_sec != 0)
23588     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
23589 }
23590
23591 /* Add the default contents for the .ARM.attributes section.  */
23592 void
23593 arm_md_end (void)
23594 {
23595   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
23596     return;
23597
23598   aeabi_set_public_attributes ();
23599 }
23600 #endif /* OBJ_ELF */
23601
23602
23603 /* Parse a .cpu directive.  */
23604
23605 static void
23606 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
23607 {
23608   const struct arm_cpu_option_table *opt;
23609   char *name;
23610   char saved_char;
23611
23612   name = input_line_pointer;
23613   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23614     input_line_pointer++;
23615   saved_char = *input_line_pointer;
23616   *input_line_pointer = 0;
23617
23618   /* Skip the first "all" entry.  */
23619   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
23620     if (streq (opt->name, name))
23621       {
23622         mcpu_cpu_opt = &opt->value;
23623         selected_cpu = opt->value;
23624         if (opt->canonical_name)
23625           strcpy (selected_cpu_name, opt->canonical_name);
23626         else
23627           {
23628             int i;
23629             for (i = 0; opt->name[i]; i++)
23630               selected_cpu_name[i] = TOUPPER (opt->name[i]);
23631             selected_cpu_name[i] = 0;
23632           }
23633         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23634         *input_line_pointer = saved_char;
23635         demand_empty_rest_of_line ();
23636         return;
23637       }
23638   as_bad (_("unknown cpu `%s'"), name);
23639   *input_line_pointer = saved_char;
23640   ignore_rest_of_line ();
23641 }
23642
23643
23644 /* Parse a .arch directive.  */
23645
23646 static void
23647 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
23648 {
23649   const struct arm_arch_option_table *opt;
23650   char saved_char;
23651   char *name;
23652
23653   name = input_line_pointer;
23654   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23655     input_line_pointer++;
23656   saved_char = *input_line_pointer;
23657   *input_line_pointer = 0;
23658
23659   /* Skip the first "all" entry.  */
23660   for (opt = arm_archs + 1; opt->name != NULL; opt++)
23661     if (streq (opt->name, name))
23662       {
23663         mcpu_cpu_opt = &opt->value;
23664         selected_cpu = opt->value;
23665         strcpy (selected_cpu_name, opt->name);
23666         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23667         *input_line_pointer = saved_char;
23668         demand_empty_rest_of_line ();
23669         return;
23670       }
23671
23672   as_bad (_("unknown architecture `%s'\n"), name);
23673   *input_line_pointer = saved_char;
23674   ignore_rest_of_line ();
23675 }
23676
23677
23678 /* Parse a .object_arch directive.  */
23679
23680 static void
23681 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
23682 {
23683   const struct arm_arch_option_table *opt;
23684   char saved_char;
23685   char *name;
23686
23687   name = input_line_pointer;
23688   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23689     input_line_pointer++;
23690   saved_char = *input_line_pointer;
23691   *input_line_pointer = 0;
23692
23693   /* Skip the first "all" entry.  */
23694   for (opt = arm_archs + 1; opt->name != NULL; opt++)
23695     if (streq (opt->name, name))
23696       {
23697         object_arch = &opt->value;
23698         *input_line_pointer = saved_char;
23699         demand_empty_rest_of_line ();
23700         return;
23701       }
23702
23703   as_bad (_("unknown architecture `%s'\n"), name);
23704   *input_line_pointer = saved_char;
23705   ignore_rest_of_line ();
23706 }
23707
23708 /* Parse a .arch_extension directive.  */
23709
23710 static void
23711 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
23712 {
23713   const struct arm_option_extension_value_table *opt;
23714   char saved_char;
23715   char *name;
23716   int adding_value = 1;
23717
23718   name = input_line_pointer;
23719   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23720     input_line_pointer++;
23721   saved_char = *input_line_pointer;
23722   *input_line_pointer = 0;
23723
23724   if (strlen (name) >= 2
23725       && strncmp (name, "no", 2) == 0)
23726     {
23727       adding_value = 0;
23728       name += 2;
23729     }
23730
23731   for (opt = arm_extensions; opt->name != NULL; opt++)
23732     if (streq (opt->name, name))
23733       {
23734         if (!ARM_CPU_HAS_FEATURE (*mcpu_cpu_opt, opt->allowed_archs))
23735           {
23736             as_bad (_("architectural extension `%s' is not allowed for the "
23737                       "current base architecture"), name);
23738             break;
23739           }
23740
23741         if (adding_value)
23742           ARM_MERGE_FEATURE_SETS (selected_cpu, selected_cpu, opt->value);
23743         else
23744           ARM_CLEAR_FEATURE (selected_cpu, selected_cpu, opt->value);
23745
23746         mcpu_cpu_opt = &selected_cpu;
23747         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23748         *input_line_pointer = saved_char;
23749         demand_empty_rest_of_line ();
23750         return;
23751       }
23752
23753   if (opt->name == NULL)
23754     as_bad (_("unknown architecture `%s'\n"), name);
23755
23756   *input_line_pointer = saved_char;
23757   ignore_rest_of_line ();
23758 }
23759
23760 /* Parse a .fpu directive.  */
23761
23762 static void
23763 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
23764 {
23765   const struct arm_option_fpu_value_table *opt;
23766   char saved_char;
23767   char *name;
23768
23769   name = input_line_pointer;
23770   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23771     input_line_pointer++;
23772   saved_char = *input_line_pointer;
23773   *input_line_pointer = 0;
23774
23775   for (opt = arm_fpus; opt->name != NULL; opt++)
23776     if (streq (opt->name, name))
23777       {
23778         mfpu_opt = &opt->value;
23779         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23780         *input_line_pointer = saved_char;
23781         demand_empty_rest_of_line ();
23782         return;
23783       }
23784
23785   as_bad (_("unknown floating point format `%s'\n"), name);
23786   *input_line_pointer = saved_char;
23787   ignore_rest_of_line ();
23788 }
23789
23790 /* Copy symbol information.  */
23791
23792 void
23793 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
23794 {
23795   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
23796 }
23797
23798 #ifdef OBJ_ELF
23799 /* Given a symbolic attribute NAME, return the proper integer value.
23800    Returns -1 if the attribute is not known.  */
23801
23802 int
23803 arm_convert_symbolic_attribute (const char *name)
23804 {
23805   static const struct
23806   {
23807     const char * name;
23808     const int    tag;
23809   }
23810   attribute_table[] =
23811     {
23812       /* When you modify this table you should
23813          also modify the list in doc/c-arm.texi.  */
23814 #define T(tag) {#tag, tag}
23815       T (Tag_CPU_raw_name),
23816       T (Tag_CPU_name),
23817       T (Tag_CPU_arch),
23818       T (Tag_CPU_arch_profile),
23819       T (Tag_ARM_ISA_use),
23820       T (Tag_THUMB_ISA_use),
23821       T (Tag_FP_arch),
23822       T (Tag_VFP_arch),
23823       T (Tag_WMMX_arch),
23824       T (Tag_Advanced_SIMD_arch),
23825       T (Tag_PCS_config),
23826       T (Tag_ABI_PCS_R9_use),
23827       T (Tag_ABI_PCS_RW_data),
23828       T (Tag_ABI_PCS_RO_data),
23829       T (Tag_ABI_PCS_GOT_use),
23830       T (Tag_ABI_PCS_wchar_t),
23831       T (Tag_ABI_FP_rounding),
23832       T (Tag_ABI_FP_denormal),
23833       T (Tag_ABI_FP_exceptions),
23834       T (Tag_ABI_FP_user_exceptions),
23835       T (Tag_ABI_FP_number_model),
23836       T (Tag_ABI_align_needed),
23837       T (Tag_ABI_align8_needed),
23838       T (Tag_ABI_align_preserved),
23839       T (Tag_ABI_align8_preserved),
23840       T (Tag_ABI_enum_size),
23841       T (Tag_ABI_HardFP_use),
23842       T (Tag_ABI_VFP_args),
23843       T (Tag_ABI_WMMX_args),
23844       T (Tag_ABI_optimization_goals),
23845       T (Tag_ABI_FP_optimization_goals),
23846       T (Tag_compatibility),
23847       T (Tag_CPU_unaligned_access),
23848       T (Tag_FP_HP_extension),
23849       T (Tag_VFP_HP_extension),
23850       T (Tag_ABI_FP_16bit_format),
23851       T (Tag_MPextension_use),
23852       T (Tag_DIV_use),
23853       T (Tag_nodefaults),
23854       T (Tag_also_compatible_with),
23855       T (Tag_conformance),
23856       T (Tag_T2EE_use),
23857       T (Tag_Virtualization_use),
23858       /* We deliberately do not include Tag_MPextension_use_legacy.  */
23859 #undef T
23860     };
23861   unsigned int i;
23862
23863   if (name == NULL)
23864     return -1;
23865
23866   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
23867     if (streq (name, attribute_table[i].name))
23868       return attribute_table[i].tag;
23869
23870   return -1;
23871 }
23872
23873
23874 /* Apply sym value for relocations only in the case that
23875    they are for local symbols and you have the respective
23876    architectural feature for blx and simple switches.  */
23877 int
23878 arm_apply_sym_value (struct fix * fixP)
23879 {
23880   if (fixP->fx_addsy
23881       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23882       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
23883     {
23884       switch (fixP->fx_r_type)
23885         {
23886         case BFD_RELOC_ARM_PCREL_BLX:
23887         case BFD_RELOC_THUMB_PCREL_BRANCH23:
23888           if (ARM_IS_FUNC (fixP->fx_addsy))
23889             return 1;
23890           break;
23891
23892         case BFD_RELOC_ARM_PCREL_CALL:
23893         case BFD_RELOC_THUMB_PCREL_BLX:
23894           if (THUMB_IS_FUNC (fixP->fx_addsy))
23895               return 1;
23896           break;
23897
23898         default:
23899           break;
23900         }
23901
23902     }
23903   return 0;
23904 }
23905 #endif /* OBJ_ELF */