PR gas/12931
[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
2593   if (state == MAP_ARM || state == MAP_THUMB)
2594     /*  PR gas/12931
2595         All ARM instructions require 4-byte alignment.
2596         (Almost) all Thumb instructions require 2-byte alignment.
2597
2598         When emitting instructions into any section, mark the section
2599         appropriately.
2600
2601         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2602         but themselves require 2-byte alignment; this applies to some
2603         PC- relative forms.  However, these cases will invovle implicit
2604         literal pool generation or an explicit .align >=2, both of
2605         which will cause the section to me marked with sufficient
2606         alignment.  Thus, we don't handle those cases here.  */
2607     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2608
2609   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2610     /* This case will be evaluated later in the next else.  */
2611     return;
2612   else if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2613           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2614     {
2615       /* Only add the symbol if the offset is > 0:
2616          if we're at the first frag, check it's size > 0;
2617          if we're not at the first frag, then for sure
2618             the offset is > 0.  */
2619       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2620       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2621
2622       if (add_symbol)
2623         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2624     }
2625
2626   mapping_state_2 (state, 0);
2627 #undef TRANSITION
2628 }
2629
2630 /* Same as mapping_state, but MAX_CHARS bytes have already been
2631    allocated.  Put the mapping symbol that far back.  */
2632
2633 static void
2634 mapping_state_2 (enum mstate state, int max_chars)
2635 {
2636   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2637
2638   if (!SEG_NORMAL (now_seg))
2639     return;
2640
2641   if (mapstate == state)
2642     /* The mapping symbol has already been emitted.
2643        There is nothing else to do.  */
2644     return;
2645
2646   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2647   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2648 }
2649 #else
2650 #define mapping_state(x) ((void)0)
2651 #define mapping_state_2(x, y) ((void)0)
2652 #endif
2653
2654 /* Find the real, Thumb encoded start of a Thumb function.  */
2655
2656 #ifdef OBJ_COFF
2657 static symbolS *
2658 find_real_start (symbolS * symbolP)
2659 {
2660   char *       real_start;
2661   const char * name = S_GET_NAME (symbolP);
2662   symbolS *    new_target;
2663
2664   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2665 #define STUB_NAME ".real_start_of"
2666
2667   if (name == NULL)
2668     abort ();
2669
2670   /* The compiler may generate BL instructions to local labels because
2671      it needs to perform a branch to a far away location. These labels
2672      do not have a corresponding ".real_start_of" label.  We check
2673      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2674      the ".real_start_of" convention for nonlocal branches.  */
2675   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2676     return symbolP;
2677
2678   real_start = ACONCAT ((STUB_NAME, name, NULL));
2679   new_target = symbol_find (real_start);
2680
2681   if (new_target == NULL)
2682     {
2683       as_warn (_("Failed to find real start of function: %s\n"), name);
2684       new_target = symbolP;
2685     }
2686
2687   return new_target;
2688 }
2689 #endif
2690
2691 static void
2692 opcode_select (int width)
2693 {
2694   switch (width)
2695     {
2696     case 16:
2697       if (! thumb_mode)
2698         {
2699           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2700             as_bad (_("selected processor does not support THUMB opcodes"));
2701
2702           thumb_mode = 1;
2703           /* No need to force the alignment, since we will have been
2704              coming from ARM mode, which is word-aligned.  */
2705           record_alignment (now_seg, 1);
2706         }
2707       break;
2708
2709     case 32:
2710       if (thumb_mode)
2711         {
2712           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2713             as_bad (_("selected processor does not support ARM opcodes"));
2714
2715           thumb_mode = 0;
2716
2717           if (!need_pass_2)
2718             frag_align (2, 0, 0);
2719
2720           record_alignment (now_seg, 1);
2721         }
2722       break;
2723
2724     default:
2725       as_bad (_("invalid instruction size selected (%d)"), width);
2726     }
2727 }
2728
2729 static void
2730 s_arm (int ignore ATTRIBUTE_UNUSED)
2731 {
2732   opcode_select (32);
2733   demand_empty_rest_of_line ();
2734 }
2735
2736 static void
2737 s_thumb (int ignore ATTRIBUTE_UNUSED)
2738 {
2739   opcode_select (16);
2740   demand_empty_rest_of_line ();
2741 }
2742
2743 static void
2744 s_code (int unused ATTRIBUTE_UNUSED)
2745 {
2746   int temp;
2747
2748   temp = get_absolute_expression ();
2749   switch (temp)
2750     {
2751     case 16:
2752     case 32:
2753       opcode_select (temp);
2754       break;
2755
2756     default:
2757       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2758     }
2759 }
2760
2761 static void
2762 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2763 {
2764   /* If we are not already in thumb mode go into it, EVEN if
2765      the target processor does not support thumb instructions.
2766      This is used by gcc/config/arm/lib1funcs.asm for example
2767      to compile interworking support functions even if the
2768      target processor should not support interworking.  */
2769   if (! thumb_mode)
2770     {
2771       thumb_mode = 2;
2772       record_alignment (now_seg, 1);
2773     }
2774
2775   demand_empty_rest_of_line ();
2776 }
2777
2778 static void
2779 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2780 {
2781   s_thumb (0);
2782
2783   /* The following label is the name/address of the start of a Thumb function.
2784      We need to know this for the interworking support.  */
2785   label_is_thumb_function_name = TRUE;
2786 }
2787
2788 /* Perform a .set directive, but also mark the alias as
2789    being a thumb function.  */
2790
2791 static void
2792 s_thumb_set (int equiv)
2793 {
2794   /* XXX the following is a duplicate of the code for s_set() in read.c
2795      We cannot just call that code as we need to get at the symbol that
2796      is created.  */
2797   char *    name;
2798   char      delim;
2799   char *    end_name;
2800   symbolS * symbolP;
2801
2802   /* Especial apologies for the random logic:
2803      This just grew, and could be parsed much more simply!
2804      Dean - in haste.  */
2805   name      = input_line_pointer;
2806   delim     = get_symbol_end ();
2807   end_name  = input_line_pointer;
2808   *end_name = delim;
2809
2810   if (*input_line_pointer != ',')
2811     {
2812       *end_name = 0;
2813       as_bad (_("expected comma after name \"%s\""), name);
2814       *end_name = delim;
2815       ignore_rest_of_line ();
2816       return;
2817     }
2818
2819   input_line_pointer++;
2820   *end_name = 0;
2821
2822   if (name[0] == '.' && name[1] == '\0')
2823     {
2824       /* XXX - this should not happen to .thumb_set.  */
2825       abort ();
2826     }
2827
2828   if ((symbolP = symbol_find (name)) == NULL
2829       && (symbolP = md_undefined_symbol (name)) == NULL)
2830     {
2831 #ifndef NO_LISTING
2832       /* When doing symbol listings, play games with dummy fragments living
2833          outside the normal fragment chain to record the file and line info
2834          for this symbol.  */
2835       if (listing & LISTING_SYMBOLS)
2836         {
2837           extern struct list_info_struct * listing_tail;
2838           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2839
2840           memset (dummy_frag, 0, sizeof (fragS));
2841           dummy_frag->fr_type = rs_fill;
2842           dummy_frag->line = listing_tail;
2843           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2844           dummy_frag->fr_symbol = symbolP;
2845         }
2846       else
2847 #endif
2848         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2849
2850 #ifdef OBJ_COFF
2851       /* "set" symbols are local unless otherwise specified.  */
2852       SF_SET_LOCAL (symbolP);
2853 #endif /* OBJ_COFF  */
2854     }                           /* Make a new symbol.  */
2855
2856   symbol_table_insert (symbolP);
2857
2858   * end_name = delim;
2859
2860   if (equiv
2861       && S_IS_DEFINED (symbolP)
2862       && S_GET_SEGMENT (symbolP) != reg_section)
2863     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2864
2865   pseudo_set (symbolP);
2866
2867   demand_empty_rest_of_line ();
2868
2869   /* XXX Now we come to the Thumb specific bit of code.  */
2870
2871   THUMB_SET_FUNC (symbolP, 1);
2872   ARM_SET_THUMB (symbolP, 1);
2873 #if defined OBJ_ELF || defined OBJ_COFF
2874   ARM_SET_INTERWORK (symbolP, support_interwork);
2875 #endif
2876 }
2877
2878 /* Directives: Mode selection.  */
2879
2880 /* .syntax [unified|divided] - choose the new unified syntax
2881    (same for Arm and Thumb encoding, modulo slight differences in what
2882    can be represented) or the old divergent syntax for each mode.  */
2883 static void
2884 s_syntax (int unused ATTRIBUTE_UNUSED)
2885 {
2886   char *name, delim;
2887
2888   name = input_line_pointer;
2889   delim = get_symbol_end ();
2890
2891   if (!strcasecmp (name, "unified"))
2892     unified_syntax = TRUE;
2893   else if (!strcasecmp (name, "divided"))
2894     unified_syntax = FALSE;
2895   else
2896     {
2897       as_bad (_("unrecognized syntax mode \"%s\""), name);
2898       return;
2899     }
2900   *input_line_pointer = delim;
2901   demand_empty_rest_of_line ();
2902 }
2903
2904 /* Directives: sectioning and alignment.  */
2905
2906 /* Same as s_align_ptwo but align 0 => align 2.  */
2907
2908 static void
2909 s_align (int unused ATTRIBUTE_UNUSED)
2910 {
2911   int temp;
2912   bfd_boolean fill_p;
2913   long temp_fill;
2914   long max_alignment = 15;
2915
2916   temp = get_absolute_expression ();
2917   if (temp > max_alignment)
2918     as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
2919   else if (temp < 0)
2920     {
2921       as_bad (_("alignment negative. 0 assumed."));
2922       temp = 0;
2923     }
2924
2925   if (*input_line_pointer == ',')
2926     {
2927       input_line_pointer++;
2928       temp_fill = get_absolute_expression ();
2929       fill_p = TRUE;
2930     }
2931   else
2932     {
2933       fill_p = FALSE;
2934       temp_fill = 0;
2935     }
2936
2937   if (!temp)
2938     temp = 2;
2939
2940   /* Only make a frag if we HAVE to.  */
2941   if (temp && !need_pass_2)
2942     {
2943       if (!fill_p && subseg_text_p (now_seg))
2944         frag_align_code (temp, 0);
2945       else
2946         frag_align (temp, (int) temp_fill, 0);
2947     }
2948   demand_empty_rest_of_line ();
2949
2950   record_alignment (now_seg, temp);
2951 }
2952
2953 static void
2954 s_bss (int ignore ATTRIBUTE_UNUSED)
2955 {
2956   /* We don't support putting frags in the BSS segment, we fake it by
2957      marking in_bss, then looking at s_skip for clues.  */
2958   subseg_set (bss_section, 0);
2959   demand_empty_rest_of_line ();
2960
2961 #ifdef md_elf_section_change_hook
2962   md_elf_section_change_hook ();
2963 #endif
2964 }
2965
2966 static void
2967 s_even (int ignore ATTRIBUTE_UNUSED)
2968 {
2969   /* Never make frag if expect extra pass.  */
2970   if (!need_pass_2)
2971     frag_align (1, 0, 0);
2972
2973   record_alignment (now_seg, 1);
2974
2975   demand_empty_rest_of_line ();
2976 }
2977
2978 /* Directives: Literal pools.  */
2979
2980 static literal_pool *
2981 find_literal_pool (void)
2982 {
2983   literal_pool * pool;
2984
2985   for (pool = list_of_pools; pool != NULL; pool = pool->next)
2986     {
2987       if (pool->section == now_seg
2988           && pool->sub_section == now_subseg)
2989         break;
2990     }
2991
2992   return pool;
2993 }
2994
2995 static literal_pool *
2996 find_or_make_literal_pool (void)
2997 {
2998   /* Next literal pool ID number.  */
2999   static unsigned int latest_pool_num = 1;
3000   literal_pool *      pool;
3001
3002   pool = find_literal_pool ();
3003
3004   if (pool == NULL)
3005     {
3006       /* Create a new pool.  */
3007       pool = (literal_pool *) xmalloc (sizeof (* pool));
3008       if (! pool)
3009         return NULL;
3010
3011       pool->next_free_entry = 0;
3012       pool->section         = now_seg;
3013       pool->sub_section     = now_subseg;
3014       pool->next            = list_of_pools;
3015       pool->symbol          = NULL;
3016
3017       /* Add it to the list.  */
3018       list_of_pools = pool;
3019     }
3020
3021   /* New pools, and emptied pools, will have a NULL symbol.  */
3022   if (pool->symbol == NULL)
3023     {
3024       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3025                                     (valueT) 0, &zero_address_frag);
3026       pool->id = latest_pool_num ++;
3027     }
3028
3029   /* Done.  */
3030   return pool;
3031 }
3032
3033 /* Add the literal in the global 'inst'
3034    structure to the relevant literal pool.  */
3035
3036 static int
3037 add_to_lit_pool (void)
3038 {
3039   literal_pool * pool;
3040   unsigned int entry;
3041
3042   pool = find_or_make_literal_pool ();
3043
3044   /* Check if this literal value is already in the pool.  */
3045   for (entry = 0; entry < pool->next_free_entry; entry ++)
3046     {
3047       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3048           && (inst.reloc.exp.X_op == O_constant)
3049           && (pool->literals[entry].X_add_number
3050               == inst.reloc.exp.X_add_number)
3051           && (pool->literals[entry].X_unsigned
3052               == inst.reloc.exp.X_unsigned))
3053         break;
3054
3055       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3056           && (inst.reloc.exp.X_op == O_symbol)
3057           && (pool->literals[entry].X_add_number
3058               == inst.reloc.exp.X_add_number)
3059           && (pool->literals[entry].X_add_symbol
3060               == inst.reloc.exp.X_add_symbol)
3061           && (pool->literals[entry].X_op_symbol
3062               == inst.reloc.exp.X_op_symbol))
3063         break;
3064     }
3065
3066   /* Do we need to create a new entry?  */
3067   if (entry == pool->next_free_entry)
3068     {
3069       if (entry >= MAX_LITERAL_POOL_SIZE)
3070         {
3071           inst.error = _("literal pool overflow");
3072           return FAIL;
3073         }
3074
3075       pool->literals[entry] = inst.reloc.exp;
3076       pool->next_free_entry += 1;
3077     }
3078
3079   inst.reloc.exp.X_op         = O_symbol;
3080   inst.reloc.exp.X_add_number = ((int) entry) * 4;
3081   inst.reloc.exp.X_add_symbol = pool->symbol;
3082
3083   return SUCCESS;
3084 }
3085
3086 /* Can't use symbol_new here, so have to create a symbol and then at
3087    a later date assign it a value. Thats what these functions do.  */
3088
3089 static void
3090 symbol_locate (symbolS *    symbolP,
3091                const char * name,       /* It is copied, the caller can modify.  */
3092                segT         segment,    /* Segment identifier (SEG_<something>).  */
3093                valueT       valu,       /* Symbol value.  */
3094                fragS *      frag)       /* Associated fragment.  */
3095 {
3096   unsigned int name_length;
3097   char * preserved_copy_of_name;
3098
3099   name_length = strlen (name) + 1;   /* +1 for \0.  */
3100   obstack_grow (&notes, name, name_length);
3101   preserved_copy_of_name = (char *) obstack_finish (&notes);
3102
3103 #ifdef tc_canonicalize_symbol_name
3104   preserved_copy_of_name =
3105     tc_canonicalize_symbol_name (preserved_copy_of_name);
3106 #endif
3107
3108   S_SET_NAME (symbolP, preserved_copy_of_name);
3109
3110   S_SET_SEGMENT (symbolP, segment);
3111   S_SET_VALUE (symbolP, valu);
3112   symbol_clear_list_pointers (symbolP);
3113
3114   symbol_set_frag (symbolP, frag);
3115
3116   /* Link to end of symbol chain.  */
3117   {
3118     extern int symbol_table_frozen;
3119
3120     if (symbol_table_frozen)
3121       abort ();
3122   }
3123
3124   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3125
3126   obj_symbol_new_hook (symbolP);
3127
3128 #ifdef tc_symbol_new_hook
3129   tc_symbol_new_hook (symbolP);
3130 #endif
3131
3132 #ifdef DEBUG_SYMS
3133   verify_symbol_chain (symbol_rootP, symbol_lastP);
3134 #endif /* DEBUG_SYMS  */
3135 }
3136
3137
3138 static void
3139 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3140 {
3141   unsigned int entry;
3142   literal_pool * pool;
3143   char sym_name[20];
3144
3145   pool = find_literal_pool ();
3146   if (pool == NULL
3147       || pool->symbol == NULL
3148       || pool->next_free_entry == 0)
3149     return;
3150
3151   mapping_state (MAP_DATA);
3152
3153   /* Align pool as you have word accesses.
3154      Only make a frag if we have to.  */
3155   if (!need_pass_2)
3156     frag_align (2, 0, 0);
3157
3158   record_alignment (now_seg, 2);
3159
3160   sprintf (sym_name, "$$lit_\002%x", pool->id);
3161
3162   symbol_locate (pool->symbol, sym_name, now_seg,
3163                  (valueT) frag_now_fix (), frag_now);
3164   symbol_table_insert (pool->symbol);
3165
3166   ARM_SET_THUMB (pool->symbol, thumb_mode);
3167
3168 #if defined OBJ_COFF || defined OBJ_ELF
3169   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3170 #endif
3171
3172   for (entry = 0; entry < pool->next_free_entry; entry ++)
3173     /* First output the expression in the instruction to the pool.  */
3174     emit_expr (&(pool->literals[entry]), 4); /* .word  */
3175
3176   /* Mark the pool as empty.  */
3177   pool->next_free_entry = 0;
3178   pool->symbol = NULL;
3179 }
3180
3181 #ifdef OBJ_ELF
3182 /* Forward declarations for functions below, in the MD interface
3183    section.  */
3184 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3185 static valueT create_unwind_entry (int);
3186 static void start_unwind_section (const segT, int);
3187 static void add_unwind_opcode (valueT, int);
3188 static void flush_pending_unwind (void);
3189
3190 /* Directives: Data.  */
3191
3192 static void
3193 s_arm_elf_cons (int nbytes)
3194 {
3195   expressionS exp;
3196
3197 #ifdef md_flush_pending_output
3198   md_flush_pending_output ();
3199 #endif
3200
3201   if (is_it_end_of_statement ())
3202     {
3203       demand_empty_rest_of_line ();
3204       return;
3205     }
3206
3207 #ifdef md_cons_align
3208   md_cons_align (nbytes);
3209 #endif
3210
3211   mapping_state (MAP_DATA);
3212   do
3213     {
3214       int reloc;
3215       char *base = input_line_pointer;
3216
3217       expression (& exp);
3218
3219       if (exp.X_op != O_symbol)
3220         emit_expr (&exp, (unsigned int) nbytes);
3221       else
3222         {
3223           char *before_reloc = input_line_pointer;
3224           reloc = parse_reloc (&input_line_pointer);
3225           if (reloc == -1)
3226             {
3227               as_bad (_("unrecognized relocation suffix"));
3228               ignore_rest_of_line ();
3229               return;
3230             }
3231           else if (reloc == BFD_RELOC_UNUSED)
3232             emit_expr (&exp, (unsigned int) nbytes);
3233           else
3234             {
3235               reloc_howto_type *howto = (reloc_howto_type *)
3236                   bfd_reloc_type_lookup (stdoutput,
3237                                          (bfd_reloc_code_real_type) reloc);
3238               int size = bfd_get_reloc_size (howto);
3239
3240               if (reloc == BFD_RELOC_ARM_PLT32)
3241                 {
3242                   as_bad (_("(plt) is only valid on branch targets"));
3243                   reloc = BFD_RELOC_UNUSED;
3244                   size = 0;
3245                 }
3246
3247               if (size > nbytes)
3248                 as_bad (_("%s relocations do not fit in %d bytes"),
3249                         howto->name, nbytes);
3250               else
3251                 {
3252                   /* We've parsed an expression stopping at O_symbol.
3253                      But there may be more expression left now that we
3254                      have parsed the relocation marker.  Parse it again.
3255                      XXX Surely there is a cleaner way to do this.  */
3256                   char *p = input_line_pointer;
3257                   int offset;
3258                   char *save_buf = (char *) alloca (input_line_pointer - base);
3259                   memcpy (save_buf, base, input_line_pointer - base);
3260                   memmove (base + (input_line_pointer - before_reloc),
3261                            base, before_reloc - base);
3262
3263                   input_line_pointer = base + (input_line_pointer-before_reloc);
3264                   expression (&exp);
3265                   memcpy (base, save_buf, p - base);
3266
3267                   offset = nbytes - size;
3268                   p = frag_more ((int) nbytes);
3269                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3270                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3271                 }
3272             }
3273         }
3274     }
3275   while (*input_line_pointer++ == ',');
3276
3277   /* Put terminator back into stream.  */
3278   input_line_pointer --;
3279   demand_empty_rest_of_line ();
3280 }
3281
3282 /* Emit an expression containing a 32-bit thumb instruction.
3283    Implementation based on put_thumb32_insn.  */
3284
3285 static void
3286 emit_thumb32_expr (expressionS * exp)
3287 {
3288   expressionS exp_high = *exp;
3289
3290   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3291   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3292   exp->X_add_number &= 0xffff;
3293   emit_expr (exp, (unsigned int) THUMB_SIZE);
3294 }
3295
3296 /*  Guess the instruction size based on the opcode.  */
3297
3298 static int
3299 thumb_insn_size (int opcode)
3300 {
3301   if ((unsigned int) opcode < 0xe800u)
3302     return 2;
3303   else if ((unsigned int) opcode >= 0xe8000000u)
3304     return 4;
3305   else
3306     return 0;
3307 }
3308
3309 static bfd_boolean
3310 emit_insn (expressionS *exp, int nbytes)
3311 {
3312   int size = 0;
3313
3314   if (exp->X_op == O_constant)
3315     {
3316       size = nbytes;
3317
3318       if (size == 0)
3319         size = thumb_insn_size (exp->X_add_number);
3320
3321       if (size != 0)
3322         {
3323           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3324             {
3325               as_bad (_(".inst.n operand too big. "\
3326                         "Use .inst.w instead"));
3327               size = 0;
3328             }
3329           else
3330             {
3331               if (now_it.state == AUTOMATIC_IT_BLOCK)
3332                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3333               else
3334                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3335
3336               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3337                 emit_thumb32_expr (exp);
3338               else
3339                 emit_expr (exp, (unsigned int) size);
3340
3341               it_fsm_post_encode ();
3342             }
3343         }
3344       else
3345         as_bad (_("cannot determine Thumb instruction size. "   \
3346                   "Use .inst.n/.inst.w instead"));
3347     }
3348   else
3349     as_bad (_("constant expression required"));
3350
3351   return (size != 0);
3352 }
3353
3354 /* Like s_arm_elf_cons but do not use md_cons_align and
3355    set the mapping state to MAP_ARM/MAP_THUMB.  */
3356
3357 static void
3358 s_arm_elf_inst (int nbytes)
3359 {
3360   if (is_it_end_of_statement ())
3361     {
3362       demand_empty_rest_of_line ();
3363       return;
3364     }
3365
3366   /* Calling mapping_state () here will not change ARM/THUMB,
3367      but will ensure not to be in DATA state.  */
3368
3369   if (thumb_mode)
3370     mapping_state (MAP_THUMB);
3371   else
3372     {
3373       if (nbytes != 0)
3374         {
3375           as_bad (_("width suffixes are invalid in ARM mode"));
3376           ignore_rest_of_line ();
3377           return;
3378         }
3379
3380       nbytes = 4;
3381
3382       mapping_state (MAP_ARM);
3383     }
3384
3385   do
3386     {
3387       expressionS exp;
3388
3389       expression (& exp);
3390
3391       if (! emit_insn (& exp, nbytes))
3392         {
3393           ignore_rest_of_line ();
3394           return;
3395         }
3396     }
3397   while (*input_line_pointer++ == ',');
3398
3399   /* Put terminator back into stream.  */
3400   input_line_pointer --;
3401   demand_empty_rest_of_line ();
3402 }
3403
3404 /* Parse a .rel31 directive.  */
3405
3406 static void
3407 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3408 {
3409   expressionS exp;
3410   char *p;
3411   valueT highbit;
3412
3413   highbit = 0;
3414   if (*input_line_pointer == '1')
3415     highbit = 0x80000000;
3416   else if (*input_line_pointer != '0')
3417     as_bad (_("expected 0 or 1"));
3418
3419   input_line_pointer++;
3420   if (*input_line_pointer != ',')
3421     as_bad (_("missing comma"));
3422   input_line_pointer++;
3423
3424 #ifdef md_flush_pending_output
3425   md_flush_pending_output ();
3426 #endif
3427
3428 #ifdef md_cons_align
3429   md_cons_align (4);
3430 #endif
3431
3432   mapping_state (MAP_DATA);
3433
3434   expression (&exp);
3435
3436   p = frag_more (4);
3437   md_number_to_chars (p, highbit, 4);
3438   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3439                BFD_RELOC_ARM_PREL31);
3440
3441   demand_empty_rest_of_line ();
3442 }
3443
3444 /* Directives: AEABI stack-unwind tables.  */
3445
3446 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3447
3448 static void
3449 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3450 {
3451   demand_empty_rest_of_line ();
3452   if (unwind.proc_start)
3453     {
3454       as_bad (_("duplicate .fnstart directive"));
3455       return;
3456     }
3457
3458   /* Mark the start of the function.  */
3459   unwind.proc_start = expr_build_dot ();
3460
3461   /* Reset the rest of the unwind info.  */
3462   unwind.opcode_count = 0;
3463   unwind.table_entry = NULL;
3464   unwind.personality_routine = NULL;
3465   unwind.personality_index = -1;
3466   unwind.frame_size = 0;
3467   unwind.fp_offset = 0;
3468   unwind.fp_reg = REG_SP;
3469   unwind.fp_used = 0;
3470   unwind.sp_restored = 0;
3471 }
3472
3473
3474 /* Parse a handlerdata directive.  Creates the exception handling table entry
3475    for the function.  */
3476
3477 static void
3478 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3479 {
3480   demand_empty_rest_of_line ();
3481   if (!unwind.proc_start)
3482     as_bad (MISSING_FNSTART);
3483
3484   if (unwind.table_entry)
3485     as_bad (_("duplicate .handlerdata directive"));
3486
3487   create_unwind_entry (1);
3488 }
3489
3490 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3491
3492 static void
3493 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3494 {
3495   long where;
3496   char *ptr;
3497   valueT val;
3498   unsigned int marked_pr_dependency;
3499
3500   demand_empty_rest_of_line ();
3501
3502   if (!unwind.proc_start)
3503     {
3504       as_bad (_(".fnend directive without .fnstart"));
3505       return;
3506     }
3507
3508   /* Add eh table entry.  */
3509   if (unwind.table_entry == NULL)
3510     val = create_unwind_entry (0);
3511   else
3512     val = 0;
3513
3514   /* Add index table entry.  This is two words.  */
3515   start_unwind_section (unwind.saved_seg, 1);
3516   frag_align (2, 0, 0);
3517   record_alignment (now_seg, 2);
3518
3519   ptr = frag_more (8);
3520   where = frag_now_fix () - 8;
3521
3522   /* Self relative offset of the function start.  */
3523   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3524            BFD_RELOC_ARM_PREL31);
3525
3526   /* Indicate dependency on EHABI-defined personality routines to the
3527      linker, if it hasn't been done already.  */
3528   marked_pr_dependency
3529     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3530   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3531       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3532     {
3533       static const char *const name[] =
3534         {
3535           "__aeabi_unwind_cpp_pr0",
3536           "__aeabi_unwind_cpp_pr1",
3537           "__aeabi_unwind_cpp_pr2"
3538         };
3539       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3540       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3541       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3542         |= 1 << unwind.personality_index;
3543     }
3544
3545   if (val)
3546     /* Inline exception table entry.  */
3547     md_number_to_chars (ptr + 4, val, 4);
3548   else
3549     /* Self relative offset of the table entry.  */
3550     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3551              BFD_RELOC_ARM_PREL31);
3552
3553   /* Restore the original section.  */
3554   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3555
3556   unwind.proc_start = NULL;
3557 }
3558
3559
3560 /* Parse an unwind_cantunwind directive.  */
3561
3562 static void
3563 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3564 {
3565   demand_empty_rest_of_line ();
3566   if (!unwind.proc_start)
3567     as_bad (MISSING_FNSTART);
3568
3569   if (unwind.personality_routine || unwind.personality_index != -1)
3570     as_bad (_("personality routine specified for cantunwind frame"));
3571
3572   unwind.personality_index = -2;
3573 }
3574
3575
3576 /* Parse a personalityindex directive.  */
3577
3578 static void
3579 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3580 {
3581   expressionS exp;
3582
3583   if (!unwind.proc_start)
3584     as_bad (MISSING_FNSTART);
3585
3586   if (unwind.personality_routine || unwind.personality_index != -1)
3587     as_bad (_("duplicate .personalityindex directive"));
3588
3589   expression (&exp);
3590
3591   if (exp.X_op != O_constant
3592       || exp.X_add_number < 0 || exp.X_add_number > 15)
3593     {
3594       as_bad (_("bad personality routine number"));
3595       ignore_rest_of_line ();
3596       return;
3597     }
3598
3599   unwind.personality_index = exp.X_add_number;
3600
3601   demand_empty_rest_of_line ();
3602 }
3603
3604
3605 /* Parse a personality directive.  */
3606
3607 static void
3608 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3609 {
3610   char *name, *p, c;
3611
3612   if (!unwind.proc_start)
3613     as_bad (MISSING_FNSTART);
3614
3615   if (unwind.personality_routine || unwind.personality_index != -1)
3616     as_bad (_("duplicate .personality directive"));
3617
3618   name = input_line_pointer;
3619   c = get_symbol_end ();
3620   p = input_line_pointer;
3621   unwind.personality_routine = symbol_find_or_make (name);
3622   *p = c;
3623   demand_empty_rest_of_line ();
3624 }
3625
3626
3627 /* Parse a directive saving core registers.  */
3628
3629 static void
3630 s_arm_unwind_save_core (void)
3631 {
3632   valueT op;
3633   long range;
3634   int n;
3635
3636   range = parse_reg_list (&input_line_pointer);
3637   if (range == FAIL)
3638     {
3639       as_bad (_("expected register list"));
3640       ignore_rest_of_line ();
3641       return;
3642     }
3643
3644   demand_empty_rest_of_line ();
3645
3646   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3647      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3648      ip because it is clobbered by calls.  */
3649   if (unwind.sp_restored && unwind.fp_reg == 12
3650       && (range & 0x3000) == 0x1000)
3651     {
3652       unwind.opcode_count--;
3653       unwind.sp_restored = 0;
3654       range = (range | 0x2000) & ~0x1000;
3655       unwind.pending_offset = 0;
3656     }
3657
3658   /* Pop r4-r15.  */
3659   if (range & 0xfff0)
3660     {
3661       /* See if we can use the short opcodes.  These pop a block of up to 8
3662          registers starting with r4, plus maybe r14.  */
3663       for (n = 0; n < 8; n++)
3664         {
3665           /* Break at the first non-saved register.      */
3666           if ((range & (1 << (n + 4))) == 0)
3667             break;
3668         }
3669       /* See if there are any other bits set.  */
3670       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
3671         {
3672           /* Use the long form.  */
3673           op = 0x8000 | ((range >> 4) & 0xfff);
3674           add_unwind_opcode (op, 2);
3675         }
3676       else
3677         {
3678           /* Use the short form.  */
3679           if (range & 0x4000)
3680             op = 0xa8; /* Pop r14.      */
3681           else
3682             op = 0xa0; /* Do not pop r14.  */
3683           op |= (n - 1);
3684           add_unwind_opcode (op, 1);
3685         }
3686     }
3687
3688   /* Pop r0-r3.  */
3689   if (range & 0xf)
3690     {
3691       op = 0xb100 | (range & 0xf);
3692       add_unwind_opcode (op, 2);
3693     }
3694
3695   /* Record the number of bytes pushed.  */
3696   for (n = 0; n < 16; n++)
3697     {
3698       if (range & (1 << n))
3699         unwind.frame_size += 4;
3700     }
3701 }
3702
3703
3704 /* Parse a directive saving FPA registers.  */
3705
3706 static void
3707 s_arm_unwind_save_fpa (int reg)
3708 {
3709   expressionS exp;
3710   int num_regs;
3711   valueT op;
3712
3713   /* Get Number of registers to transfer.  */
3714   if (skip_past_comma (&input_line_pointer) != FAIL)
3715     expression (&exp);
3716   else
3717     exp.X_op = O_illegal;
3718
3719   if (exp.X_op != O_constant)
3720     {
3721       as_bad (_("expected , <constant>"));
3722       ignore_rest_of_line ();
3723       return;
3724     }
3725
3726   num_regs = exp.X_add_number;
3727
3728   if (num_regs < 1 || num_regs > 4)
3729     {
3730       as_bad (_("number of registers must be in the range [1:4]"));
3731       ignore_rest_of_line ();
3732       return;
3733     }
3734
3735   demand_empty_rest_of_line ();
3736
3737   if (reg == 4)
3738     {
3739       /* Short form.  */
3740       op = 0xb4 | (num_regs - 1);
3741       add_unwind_opcode (op, 1);
3742     }
3743   else
3744     {
3745       /* Long form.  */
3746       op = 0xc800 | (reg << 4) | (num_regs - 1);
3747       add_unwind_opcode (op, 2);
3748     }
3749   unwind.frame_size += num_regs * 12;
3750 }
3751
3752
3753 /* Parse a directive saving VFP registers for ARMv6 and above.  */
3754
3755 static void
3756 s_arm_unwind_save_vfp_armv6 (void)
3757 {
3758   int count;
3759   unsigned int start;
3760   valueT op;
3761   int num_vfpv3_regs = 0;
3762   int num_regs_below_16;
3763
3764   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
3765   if (count == FAIL)
3766     {
3767       as_bad (_("expected register list"));
3768       ignore_rest_of_line ();
3769       return;
3770     }
3771
3772   demand_empty_rest_of_line ();
3773
3774   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
3775      than FSTMX/FLDMX-style ones).  */
3776
3777   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
3778   if (start >= 16)
3779     num_vfpv3_regs = count;
3780   else if (start + count > 16)
3781     num_vfpv3_regs = start + count - 16;
3782
3783   if (num_vfpv3_regs > 0)
3784     {
3785       int start_offset = start > 16 ? start - 16 : 0;
3786       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
3787       add_unwind_opcode (op, 2);
3788     }
3789
3790   /* Generate opcode for registers numbered in the range 0 .. 15.  */
3791   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
3792   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
3793   if (num_regs_below_16 > 0)
3794     {
3795       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
3796       add_unwind_opcode (op, 2);
3797     }
3798
3799   unwind.frame_size += count * 8;
3800 }
3801
3802
3803 /* Parse a directive saving VFP registers for pre-ARMv6.  */
3804
3805 static void
3806 s_arm_unwind_save_vfp (void)
3807 {
3808   int count;
3809   unsigned int reg;
3810   valueT op;
3811
3812   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
3813   if (count == FAIL)
3814     {
3815       as_bad (_("expected register list"));
3816       ignore_rest_of_line ();
3817       return;
3818     }
3819
3820   demand_empty_rest_of_line ();
3821
3822   if (reg == 8)
3823     {
3824       /* Short form.  */
3825       op = 0xb8 | (count - 1);
3826       add_unwind_opcode (op, 1);
3827     }
3828   else
3829     {
3830       /* Long form.  */
3831       op = 0xb300 | (reg << 4) | (count - 1);
3832       add_unwind_opcode (op, 2);
3833     }
3834   unwind.frame_size += count * 8 + 4;
3835 }
3836
3837
3838 /* Parse a directive saving iWMMXt data registers.  */
3839
3840 static void
3841 s_arm_unwind_save_mmxwr (void)
3842 {
3843   int reg;
3844   int hi_reg;
3845   int i;
3846   unsigned mask = 0;
3847   valueT op;
3848
3849   if (*input_line_pointer == '{')
3850     input_line_pointer++;
3851
3852   do
3853     {
3854       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3855
3856       if (reg == FAIL)
3857         {
3858           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
3859           goto error;
3860         }
3861
3862       if (mask >> reg)
3863         as_tsktsk (_("register list not in ascending order"));
3864       mask |= 1 << reg;
3865
3866       if (*input_line_pointer == '-')
3867         {
3868           input_line_pointer++;
3869           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3870           if (hi_reg == FAIL)
3871             {
3872               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
3873               goto error;
3874             }
3875           else if (reg >= hi_reg)
3876             {
3877               as_bad (_("bad register range"));
3878               goto error;
3879             }
3880           for (; reg < hi_reg; reg++)
3881             mask |= 1 << reg;
3882         }
3883     }
3884   while (skip_past_comma (&input_line_pointer) != FAIL);
3885
3886   if (*input_line_pointer == '}')
3887     input_line_pointer++;
3888
3889   demand_empty_rest_of_line ();
3890
3891   /* Generate any deferred opcodes because we're going to be looking at
3892      the list.  */
3893   flush_pending_unwind ();
3894
3895   for (i = 0; i < 16; i++)
3896     {
3897       if (mask & (1 << i))
3898         unwind.frame_size += 8;
3899     }
3900
3901   /* Attempt to combine with a previous opcode.  We do this because gcc
3902      likes to output separate unwind directives for a single block of
3903      registers.  */
3904   if (unwind.opcode_count > 0)
3905     {
3906       i = unwind.opcodes[unwind.opcode_count - 1];
3907       if ((i & 0xf8) == 0xc0)
3908         {
3909           i &= 7;
3910           /* Only merge if the blocks are contiguous.  */
3911           if (i < 6)
3912             {
3913               if ((mask & 0xfe00) == (1 << 9))
3914                 {
3915                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
3916                   unwind.opcode_count--;
3917                 }
3918             }
3919           else if (i == 6 && unwind.opcode_count >= 2)
3920             {
3921               i = unwind.opcodes[unwind.opcode_count - 2];
3922               reg = i >> 4;
3923               i &= 0xf;
3924
3925               op = 0xffff << (reg - 1);
3926               if (reg > 0
3927                   && ((mask & op) == (1u << (reg - 1))))
3928                 {
3929                   op = (1 << (reg + i + 1)) - 1;
3930                   op &= ~((1 << reg) - 1);
3931                   mask |= op;
3932                   unwind.opcode_count -= 2;
3933                 }
3934             }
3935         }
3936     }
3937
3938   hi_reg = 15;
3939   /* We want to generate opcodes in the order the registers have been
3940      saved, ie. descending order.  */
3941   for (reg = 15; reg >= -1; reg--)
3942     {
3943       /* Save registers in blocks.  */
3944       if (reg < 0
3945           || !(mask & (1 << reg)))
3946         {
3947           /* We found an unsaved reg.  Generate opcodes to save the
3948              preceding block.   */
3949           if (reg != hi_reg)
3950             {
3951               if (reg == 9)
3952                 {
3953                   /* Short form.  */
3954                   op = 0xc0 | (hi_reg - 10);
3955                   add_unwind_opcode (op, 1);
3956                 }
3957               else
3958                 {
3959                   /* Long form.  */
3960                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
3961                   add_unwind_opcode (op, 2);
3962                 }
3963             }
3964           hi_reg = reg - 1;
3965         }
3966     }
3967
3968   return;
3969 error:
3970   ignore_rest_of_line ();
3971 }
3972
3973 static void
3974 s_arm_unwind_save_mmxwcg (void)
3975 {
3976   int reg;
3977   int hi_reg;
3978   unsigned mask = 0;
3979   valueT op;
3980
3981   if (*input_line_pointer == '{')
3982     input_line_pointer++;
3983
3984   do
3985     {
3986       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
3987
3988       if (reg == FAIL)
3989         {
3990           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
3991           goto error;
3992         }
3993
3994       reg -= 8;
3995       if (mask >> reg)
3996         as_tsktsk (_("register list not in ascending order"));
3997       mask |= 1 << reg;
3998
3999       if (*input_line_pointer == '-')
4000         {
4001           input_line_pointer++;
4002           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4003           if (hi_reg == FAIL)
4004             {
4005               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4006               goto error;
4007             }
4008           else if (reg >= hi_reg)
4009             {
4010               as_bad (_("bad register range"));
4011               goto error;
4012             }
4013           for (; reg < hi_reg; reg++)
4014             mask |= 1 << reg;
4015         }
4016     }
4017   while (skip_past_comma (&input_line_pointer) != FAIL);
4018
4019   if (*input_line_pointer == '}')
4020     input_line_pointer++;
4021
4022   demand_empty_rest_of_line ();
4023
4024   /* Generate any deferred opcodes because we're going to be looking at
4025      the list.  */
4026   flush_pending_unwind ();
4027
4028   for (reg = 0; reg < 16; reg++)
4029     {
4030       if (mask & (1 << reg))
4031         unwind.frame_size += 4;
4032     }
4033   op = 0xc700 | mask;
4034   add_unwind_opcode (op, 2);
4035   return;
4036 error:
4037   ignore_rest_of_line ();
4038 }
4039
4040
4041 /* Parse an unwind_save directive.
4042    If the argument is non-zero, this is a .vsave directive.  */
4043
4044 static void
4045 s_arm_unwind_save (int arch_v6)
4046 {
4047   char *peek;
4048   struct reg_entry *reg;
4049   bfd_boolean had_brace = FALSE;
4050
4051   if (!unwind.proc_start)
4052     as_bad (MISSING_FNSTART);
4053
4054   /* Figure out what sort of save we have.  */
4055   peek = input_line_pointer;
4056
4057   if (*peek == '{')
4058     {
4059       had_brace = TRUE;
4060       peek++;
4061     }
4062
4063   reg = arm_reg_parse_multi (&peek);
4064
4065   if (!reg)
4066     {
4067       as_bad (_("register expected"));
4068       ignore_rest_of_line ();
4069       return;
4070     }
4071
4072   switch (reg->type)
4073     {
4074     case REG_TYPE_FN:
4075       if (had_brace)
4076         {
4077           as_bad (_("FPA .unwind_save does not take a register list"));
4078           ignore_rest_of_line ();
4079           return;
4080         }
4081       input_line_pointer = peek;
4082       s_arm_unwind_save_fpa (reg->number);
4083       return;
4084
4085     case REG_TYPE_RN:     s_arm_unwind_save_core ();   return;
4086     case REG_TYPE_VFD:
4087       if (arch_v6)
4088         s_arm_unwind_save_vfp_armv6 ();
4089       else
4090         s_arm_unwind_save_vfp ();
4091       return;
4092     case REG_TYPE_MMXWR:  s_arm_unwind_save_mmxwr ();  return;
4093     case REG_TYPE_MMXWCG: s_arm_unwind_save_mmxwcg (); return;
4094
4095     default:
4096       as_bad (_(".unwind_save does not support this kind of register"));
4097       ignore_rest_of_line ();
4098     }
4099 }
4100
4101
4102 /* Parse an unwind_movsp directive.  */
4103
4104 static void
4105 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4106 {
4107   int reg;
4108   valueT op;
4109   int offset;
4110
4111   if (!unwind.proc_start)
4112     as_bad (MISSING_FNSTART);
4113
4114   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4115   if (reg == FAIL)
4116     {
4117       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4118       ignore_rest_of_line ();
4119       return;
4120     }
4121
4122   /* Optional constant.  */
4123   if (skip_past_comma (&input_line_pointer) != FAIL)
4124     {
4125       if (immediate_for_directive (&offset) == FAIL)
4126         return;
4127     }
4128   else
4129     offset = 0;
4130
4131   demand_empty_rest_of_line ();
4132
4133   if (reg == REG_SP || reg == REG_PC)
4134     {
4135       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4136       return;
4137     }
4138
4139   if (unwind.fp_reg != REG_SP)
4140     as_bad (_("unexpected .unwind_movsp directive"));
4141
4142   /* Generate opcode to restore the value.  */
4143   op = 0x90 | reg;
4144   add_unwind_opcode (op, 1);
4145
4146   /* Record the information for later.  */
4147   unwind.fp_reg = reg;
4148   unwind.fp_offset = unwind.frame_size - offset;
4149   unwind.sp_restored = 1;
4150 }
4151
4152 /* Parse an unwind_pad directive.  */
4153
4154 static void
4155 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4156 {
4157   int offset;
4158
4159   if (!unwind.proc_start)
4160     as_bad (MISSING_FNSTART);
4161
4162   if (immediate_for_directive (&offset) == FAIL)
4163     return;
4164
4165   if (offset & 3)
4166     {
4167       as_bad (_("stack increment must be multiple of 4"));
4168       ignore_rest_of_line ();
4169       return;
4170     }
4171
4172   /* Don't generate any opcodes, just record the details for later.  */
4173   unwind.frame_size += offset;
4174   unwind.pending_offset += offset;
4175
4176   demand_empty_rest_of_line ();
4177 }
4178
4179 /* Parse an unwind_setfp directive.  */
4180
4181 static void
4182 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4183 {
4184   int sp_reg;
4185   int fp_reg;
4186   int offset;
4187
4188   if (!unwind.proc_start)
4189     as_bad (MISSING_FNSTART);
4190
4191   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4192   if (skip_past_comma (&input_line_pointer) == FAIL)
4193     sp_reg = FAIL;
4194   else
4195     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4196
4197   if (fp_reg == FAIL || sp_reg == FAIL)
4198     {
4199       as_bad (_("expected <reg>, <reg>"));
4200       ignore_rest_of_line ();
4201       return;
4202     }
4203
4204   /* Optional constant.  */
4205   if (skip_past_comma (&input_line_pointer) != FAIL)
4206     {
4207       if (immediate_for_directive (&offset) == FAIL)
4208         return;
4209     }
4210   else
4211     offset = 0;
4212
4213   demand_empty_rest_of_line ();
4214
4215   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4216     {
4217       as_bad (_("register must be either sp or set by a previous"
4218                 "unwind_movsp directive"));
4219       return;
4220     }
4221
4222   /* Don't generate any opcodes, just record the information for later.  */
4223   unwind.fp_reg = fp_reg;
4224   unwind.fp_used = 1;
4225   if (sp_reg == REG_SP)
4226     unwind.fp_offset = unwind.frame_size - offset;
4227   else
4228     unwind.fp_offset -= offset;
4229 }
4230
4231 /* Parse an unwind_raw directive.  */
4232
4233 static void
4234 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4235 {
4236   expressionS exp;
4237   /* This is an arbitrary limit.         */
4238   unsigned char op[16];
4239   int count;
4240
4241   if (!unwind.proc_start)
4242     as_bad (MISSING_FNSTART);
4243
4244   expression (&exp);
4245   if (exp.X_op == O_constant
4246       && skip_past_comma (&input_line_pointer) != FAIL)
4247     {
4248       unwind.frame_size += exp.X_add_number;
4249       expression (&exp);
4250     }
4251   else
4252     exp.X_op = O_illegal;
4253
4254   if (exp.X_op != O_constant)
4255     {
4256       as_bad (_("expected <offset>, <opcode>"));
4257       ignore_rest_of_line ();
4258       return;
4259     }
4260
4261   count = 0;
4262
4263   /* Parse the opcode.  */
4264   for (;;)
4265     {
4266       if (count >= 16)
4267         {
4268           as_bad (_("unwind opcode too long"));
4269           ignore_rest_of_line ();
4270         }
4271       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4272         {
4273           as_bad (_("invalid unwind opcode"));
4274           ignore_rest_of_line ();
4275           return;
4276         }
4277       op[count++] = exp.X_add_number;
4278
4279       /* Parse the next byte.  */
4280       if (skip_past_comma (&input_line_pointer) == FAIL)
4281         break;
4282
4283       expression (&exp);
4284     }
4285
4286   /* Add the opcode bytes in reverse order.  */
4287   while (count--)
4288     add_unwind_opcode (op[count], 1);
4289
4290   demand_empty_rest_of_line ();
4291 }
4292
4293
4294 /* Parse a .eabi_attribute directive.  */
4295
4296 static void
4297 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4298 {
4299   int tag = s_vendor_attribute (OBJ_ATTR_PROC);
4300
4301   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4302     attributes_set_explicitly[tag] = 1;
4303 }
4304
4305 /* Emit a tls fix for the symbol.  */
4306
4307 static void
4308 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4309 {
4310   char *p;
4311   expressionS exp;
4312 #ifdef md_flush_pending_output
4313   md_flush_pending_output ();
4314 #endif
4315
4316 #ifdef md_cons_align
4317   md_cons_align (4);
4318 #endif
4319
4320   /* Since we're just labelling the code, there's no need to define a
4321      mapping symbol.  */
4322   expression (&exp);
4323   p = obstack_next_free (&frchain_now->frch_obstack);
4324   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4325                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4326                : BFD_RELOC_ARM_TLS_DESCSEQ);
4327 }
4328 #endif /* OBJ_ELF */
4329
4330 static void s_arm_arch (int);
4331 static void s_arm_object_arch (int);
4332 static void s_arm_cpu (int);
4333 static void s_arm_fpu (int);
4334 static void s_arm_arch_extension (int);
4335
4336 #ifdef TE_PE
4337
4338 static void
4339 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4340 {
4341   expressionS exp;
4342
4343   do
4344     {
4345       expression (&exp);
4346       if (exp.X_op == O_symbol)
4347         exp.X_op = O_secrel;
4348
4349       emit_expr (&exp, 4);
4350     }
4351   while (*input_line_pointer++ == ',');
4352
4353   input_line_pointer--;
4354   demand_empty_rest_of_line ();
4355 }
4356 #endif /* TE_PE */
4357
4358 /* This table describes all the machine specific pseudo-ops the assembler
4359    has to support.  The fields are:
4360      pseudo-op name without dot
4361      function to call to execute this pseudo-op
4362      Integer arg to pass to the function.  */
4363
4364 const pseudo_typeS md_pseudo_table[] =
4365 {
4366   /* Never called because '.req' does not start a line.  */
4367   { "req",         s_req,         0 },
4368   /* Following two are likewise never called.  */
4369   { "dn",          s_dn,          0 },
4370   { "qn",          s_qn,          0 },
4371   { "unreq",       s_unreq,       0 },
4372   { "bss",         s_bss,         0 },
4373   { "align",       s_align,       0 },
4374   { "arm",         s_arm,         0 },
4375   { "thumb",       s_thumb,       0 },
4376   { "code",        s_code,        0 },
4377   { "force_thumb", s_force_thumb, 0 },
4378   { "thumb_func",  s_thumb_func,  0 },
4379   { "thumb_set",   s_thumb_set,   0 },
4380   { "even",        s_even,        0 },
4381   { "ltorg",       s_ltorg,       0 },
4382   { "pool",        s_ltorg,       0 },
4383   { "syntax",      s_syntax,      0 },
4384   { "cpu",         s_arm_cpu,     0 },
4385   { "arch",        s_arm_arch,    0 },
4386   { "object_arch", s_arm_object_arch,   0 },
4387   { "fpu",         s_arm_fpu,     0 },
4388   { "arch_extension", s_arm_arch_extension, 0 },
4389 #ifdef OBJ_ELF
4390   { "word",             s_arm_elf_cons, 4 },
4391   { "long",             s_arm_elf_cons, 4 },
4392   { "inst.n",           s_arm_elf_inst, 2 },
4393   { "inst.w",           s_arm_elf_inst, 4 },
4394   { "inst",             s_arm_elf_inst, 0 },
4395   { "rel31",            s_arm_rel31,      0 },
4396   { "fnstart",          s_arm_unwind_fnstart,   0 },
4397   { "fnend",            s_arm_unwind_fnend,     0 },
4398   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4399   { "personality",      s_arm_unwind_personality, 0 },
4400   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4401   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4402   { "save",             s_arm_unwind_save,      0 },
4403   { "vsave",            s_arm_unwind_save,      1 },
4404   { "movsp",            s_arm_unwind_movsp,     0 },
4405   { "pad",              s_arm_unwind_pad,       0 },
4406   { "setfp",            s_arm_unwind_setfp,     0 },
4407   { "unwind_raw",       s_arm_unwind_raw,       0 },
4408   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4409   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4410 #else
4411   { "word",        cons, 4},
4412
4413   /* These are used for dwarf.  */
4414   {"2byte", cons, 2},
4415   {"4byte", cons, 4},
4416   {"8byte", cons, 8},
4417   /* These are used for dwarf2.  */
4418   { "file", (void (*) (int)) dwarf2_directive_file, 0 },
4419   { "loc",  dwarf2_directive_loc,  0 },
4420   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4421 #endif
4422   { "extend",      float_cons, 'x' },
4423   { "ldouble",     float_cons, 'x' },
4424   { "packed",      float_cons, 'p' },
4425 #ifdef TE_PE
4426   {"secrel32", pe_directive_secrel, 0},
4427 #endif
4428   { 0, 0, 0 }
4429 };
4430 \f
4431 /* Parser functions used exclusively in instruction operands.  */
4432
4433 /* Generic immediate-value read function for use in insn parsing.
4434    STR points to the beginning of the immediate (the leading #);
4435    VAL receives the value; if the value is outside [MIN, MAX]
4436    issue an error.  PREFIX_OPT is true if the immediate prefix is
4437    optional.  */
4438
4439 static int
4440 parse_immediate (char **str, int *val, int min, int max,
4441                  bfd_boolean prefix_opt)
4442 {
4443   expressionS exp;
4444   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4445   if (exp.X_op != O_constant)
4446     {
4447       inst.error = _("constant expression required");
4448       return FAIL;
4449     }
4450
4451   if (exp.X_add_number < min || exp.X_add_number > max)
4452     {
4453       inst.error = _("immediate value out of range");
4454       return FAIL;
4455     }
4456
4457   *val = exp.X_add_number;
4458   return SUCCESS;
4459 }
4460
4461 /* Less-generic immediate-value read function with the possibility of loading a
4462    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4463    instructions. Puts the result directly in inst.operands[i].  */
4464
4465 static int
4466 parse_big_immediate (char **str, int i)
4467 {
4468   expressionS exp;
4469   char *ptr = *str;
4470
4471   my_get_expression (&exp, &ptr, GE_OPT_PREFIX_BIG);
4472
4473   if (exp.X_op == O_constant)
4474     {
4475       inst.operands[i].imm = exp.X_add_number & 0xffffffff;
4476       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4477          O_constant.  We have to be careful not to break compilation for
4478          32-bit X_add_number, though.  */
4479       if ((exp.X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4480         {
4481           /* X >> 32 is illegal if sizeof (exp.X_add_number) == 4.  */
4482           inst.operands[i].reg = ((exp.X_add_number >> 16) >> 16) & 0xffffffff;
4483           inst.operands[i].regisimm = 1;
4484         }
4485     }
4486   else if (exp.X_op == O_big
4487            && LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 32)
4488     {
4489       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4490
4491       /* Bignums have their least significant bits in
4492          generic_bignum[0]. Make sure we put 32 bits in imm and
4493          32 bits in reg,  in a (hopefully) portable way.  */
4494       gas_assert (parts != 0);
4495
4496       /* Make sure that the number is not too big.
4497          PR 11972: Bignums can now be sign-extended to the
4498          size of a .octa so check that the out of range bits
4499          are all zero or all one.  */
4500       if (LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 64)
4501         {
4502           LITTLENUM_TYPE m = -1;
4503
4504           if (generic_bignum[parts * 2] != 0
4505               && generic_bignum[parts * 2] != m)
4506             return FAIL;
4507
4508           for (j = parts * 2 + 1; j < (unsigned) exp.X_add_number; j++)
4509             if (generic_bignum[j] != generic_bignum[j-1])
4510               return FAIL;
4511         }
4512
4513       inst.operands[i].imm = 0;
4514       for (j = 0; j < parts; j++, idx++)
4515         inst.operands[i].imm |= generic_bignum[idx]
4516                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4517       inst.operands[i].reg = 0;
4518       for (j = 0; j < parts; j++, idx++)
4519         inst.operands[i].reg |= generic_bignum[idx]
4520                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4521       inst.operands[i].regisimm = 1;
4522     }
4523   else
4524     return FAIL;
4525
4526   *str = ptr;
4527
4528   return SUCCESS;
4529 }
4530
4531 /* Returns the pseudo-register number of an FPA immediate constant,
4532    or FAIL if there isn't a valid constant here.  */
4533
4534 static int
4535 parse_fpa_immediate (char ** str)
4536 {
4537   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4538   char *         save_in;
4539   expressionS    exp;
4540   int            i;
4541   int            j;
4542
4543   /* First try and match exact strings, this is to guarantee
4544      that some formats will work even for cross assembly.  */
4545
4546   for (i = 0; fp_const[i]; i++)
4547     {
4548       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4549         {
4550           char *start = *str;
4551
4552           *str += strlen (fp_const[i]);
4553           if (is_end_of_line[(unsigned char) **str])
4554             return i + 8;
4555           *str = start;
4556         }
4557     }
4558
4559   /* Just because we didn't get a match doesn't mean that the constant
4560      isn't valid, just that it is in a format that we don't
4561      automatically recognize.  Try parsing it with the standard
4562      expression routines.  */
4563
4564   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4565
4566   /* Look for a raw floating point number.  */
4567   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4568       && is_end_of_line[(unsigned char) *save_in])
4569     {
4570       for (i = 0; i < NUM_FLOAT_VALS; i++)
4571         {
4572           for (j = 0; j < MAX_LITTLENUMS; j++)
4573             {
4574               if (words[j] != fp_values[i][j])
4575                 break;
4576             }
4577
4578           if (j == MAX_LITTLENUMS)
4579             {
4580               *str = save_in;
4581               return i + 8;
4582             }
4583         }
4584     }
4585
4586   /* Try and parse a more complex expression, this will probably fail
4587      unless the code uses a floating point prefix (eg "0f").  */
4588   save_in = input_line_pointer;
4589   input_line_pointer = *str;
4590   if (expression (&exp) == absolute_section
4591       && exp.X_op == O_big
4592       && exp.X_add_number < 0)
4593     {
4594       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4595          Ditto for 15.  */
4596       if (gen_to_words (words, 5, (long) 15) == 0)
4597         {
4598           for (i = 0; i < NUM_FLOAT_VALS; i++)
4599             {
4600               for (j = 0; j < MAX_LITTLENUMS; j++)
4601                 {
4602                   if (words[j] != fp_values[i][j])
4603                     break;
4604                 }
4605
4606               if (j == MAX_LITTLENUMS)
4607                 {
4608                   *str = input_line_pointer;
4609                   input_line_pointer = save_in;
4610                   return i + 8;
4611                 }
4612             }
4613         }
4614     }
4615
4616   *str = input_line_pointer;
4617   input_line_pointer = save_in;
4618   inst.error = _("invalid FPA immediate expression");
4619   return FAIL;
4620 }
4621
4622 /* Returns 1 if a number has "quarter-precision" float format
4623    0baBbbbbbc defgh000 00000000 00000000.  */
4624
4625 static int
4626 is_quarter_float (unsigned imm)
4627 {
4628   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4629   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4630 }
4631
4632 /* Parse an 8-bit "quarter-precision" floating point number of the form:
4633    0baBbbbbbc defgh000 00000000 00000000.
4634    The zero and minus-zero cases need special handling, since they can't be
4635    encoded in the "quarter-precision" float format, but can nonetheless be
4636    loaded as integer constants.  */
4637
4638 static unsigned
4639 parse_qfloat_immediate (char **ccp, int *immed)
4640 {
4641   char *str = *ccp;
4642   char *fpnum;
4643   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4644   int found_fpchar = 0;
4645
4646   skip_past_char (&str, '#');
4647
4648   /* We must not accidentally parse an integer as a floating-point number. Make
4649      sure that the value we parse is not an integer by checking for special
4650      characters '.' or 'e'.
4651      FIXME: This is a horrible hack, but doing better is tricky because type
4652      information isn't in a very usable state at parse time.  */
4653   fpnum = str;
4654   skip_whitespace (fpnum);
4655
4656   if (strncmp (fpnum, "0x", 2) == 0)
4657     return FAIL;
4658   else
4659     {
4660       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
4661         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
4662           {
4663             found_fpchar = 1;
4664             break;
4665           }
4666
4667       if (!found_fpchar)
4668         return FAIL;
4669     }
4670
4671   if ((str = atof_ieee (str, 's', words)) != NULL)
4672     {
4673       unsigned fpword = 0;
4674       int i;
4675
4676       /* Our FP word must be 32 bits (single-precision FP).  */
4677       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
4678         {
4679           fpword <<= LITTLENUM_NUMBER_OF_BITS;
4680           fpword |= words[i];
4681         }
4682
4683       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
4684         *immed = fpword;
4685       else
4686         return FAIL;
4687
4688       *ccp = str;
4689
4690       return SUCCESS;
4691     }
4692
4693   return FAIL;
4694 }
4695
4696 /* Shift operands.  */
4697 enum shift_kind
4698 {
4699   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
4700 };
4701
4702 struct asm_shift_name
4703 {
4704   const char      *name;
4705   enum shift_kind  kind;
4706 };
4707
4708 /* Third argument to parse_shift.  */
4709 enum parse_shift_mode
4710 {
4711   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
4712   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
4713   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
4714   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
4715   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
4716 };
4717
4718 /* Parse a <shift> specifier on an ARM data processing instruction.
4719    This has three forms:
4720
4721      (LSL|LSR|ASL|ASR|ROR) Rs
4722      (LSL|LSR|ASL|ASR|ROR) #imm
4723      RRX
4724
4725    Note that ASL is assimilated to LSL in the instruction encoding, and
4726    RRX to ROR #0 (which cannot be written as such).  */
4727
4728 static int
4729 parse_shift (char **str, int i, enum parse_shift_mode mode)
4730 {
4731   const struct asm_shift_name *shift_name;
4732   enum shift_kind shift;
4733   char *s = *str;
4734   char *p = s;
4735   int reg;
4736
4737   for (p = *str; ISALPHA (*p); p++)
4738     ;
4739
4740   if (p == *str)
4741     {
4742       inst.error = _("shift expression expected");
4743       return FAIL;
4744     }
4745
4746   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
4747                                                             p - *str);
4748
4749   if (shift_name == NULL)
4750     {
4751       inst.error = _("shift expression expected");
4752       return FAIL;
4753     }
4754
4755   shift = shift_name->kind;
4756
4757   switch (mode)
4758     {
4759     case NO_SHIFT_RESTRICT:
4760     case SHIFT_IMMEDIATE:   break;
4761
4762     case SHIFT_LSL_OR_ASR_IMMEDIATE:
4763       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
4764         {
4765           inst.error = _("'LSL' or 'ASR' required");
4766           return FAIL;
4767         }
4768       break;
4769
4770     case SHIFT_LSL_IMMEDIATE:
4771       if (shift != SHIFT_LSL)
4772         {
4773           inst.error = _("'LSL' required");
4774           return FAIL;
4775         }
4776       break;
4777
4778     case SHIFT_ASR_IMMEDIATE:
4779       if (shift != SHIFT_ASR)
4780         {
4781           inst.error = _("'ASR' required");
4782           return FAIL;
4783         }
4784       break;
4785
4786     default: abort ();
4787     }
4788
4789   if (shift != SHIFT_RRX)
4790     {
4791       /* Whitespace can appear here if the next thing is a bare digit.  */
4792       skip_whitespace (p);
4793
4794       if (mode == NO_SHIFT_RESTRICT
4795           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
4796         {
4797           inst.operands[i].imm = reg;
4798           inst.operands[i].immisreg = 1;
4799         }
4800       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
4801         return FAIL;
4802     }
4803   inst.operands[i].shift_kind = shift;
4804   inst.operands[i].shifted = 1;
4805   *str = p;
4806   return SUCCESS;
4807 }
4808
4809 /* Parse a <shifter_operand> for an ARM data processing instruction:
4810
4811       #<immediate>
4812       #<immediate>, <rotate>
4813       <Rm>
4814       <Rm>, <shift>
4815
4816    where <shift> is defined by parse_shift above, and <rotate> is a
4817    multiple of 2 between 0 and 30.  Validation of immediate operands
4818    is deferred to md_apply_fix.  */
4819
4820 static int
4821 parse_shifter_operand (char **str, int i)
4822 {
4823   int value;
4824   expressionS exp;
4825
4826   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
4827     {
4828       inst.operands[i].reg = value;
4829       inst.operands[i].isreg = 1;
4830
4831       /* parse_shift will override this if appropriate */
4832       inst.reloc.exp.X_op = O_constant;
4833       inst.reloc.exp.X_add_number = 0;
4834
4835       if (skip_past_comma (str) == FAIL)
4836         return SUCCESS;
4837
4838       /* Shift operation on register.  */
4839       return parse_shift (str, i, NO_SHIFT_RESTRICT);
4840     }
4841
4842   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
4843     return FAIL;
4844
4845   if (skip_past_comma (str) == SUCCESS)
4846     {
4847       /* #x, y -- ie explicit rotation by Y.  */
4848       if (my_get_expression (&exp, str, GE_NO_PREFIX))
4849         return FAIL;
4850
4851       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
4852         {
4853           inst.error = _("constant expression expected");
4854           return FAIL;
4855         }
4856
4857       value = exp.X_add_number;
4858       if (value < 0 || value > 30 || value % 2 != 0)
4859         {
4860           inst.error = _("invalid rotation");
4861           return FAIL;
4862         }
4863       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
4864         {
4865           inst.error = _("invalid constant");
4866           return FAIL;
4867         }
4868
4869       /* Convert to decoded value.  md_apply_fix will put it back.  */
4870       inst.reloc.exp.X_add_number
4871         = (((inst.reloc.exp.X_add_number << (32 - value))
4872             | (inst.reloc.exp.X_add_number >> value)) & 0xffffffff);
4873     }
4874
4875   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
4876   inst.reloc.pc_rel = 0;
4877   return SUCCESS;
4878 }
4879
4880 /* Group relocation information.  Each entry in the table contains the
4881    textual name of the relocation as may appear in assembler source
4882    and must end with a colon.
4883    Along with this textual name are the relocation codes to be used if
4884    the corresponding instruction is an ALU instruction (ADD or SUB only),
4885    an LDR, an LDRS, or an LDC.  */
4886
4887 struct group_reloc_table_entry
4888 {
4889   const char *name;
4890   int alu_code;
4891   int ldr_code;
4892   int ldrs_code;
4893   int ldc_code;
4894 };
4895
4896 typedef enum
4897 {
4898   /* Varieties of non-ALU group relocation.  */
4899
4900   GROUP_LDR,
4901   GROUP_LDRS,
4902   GROUP_LDC
4903 } group_reloc_type;
4904
4905 static struct group_reloc_table_entry group_reloc_table[] =
4906   { /* Program counter relative: */
4907     { "pc_g0_nc",
4908       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
4909       0,                                /* LDR */
4910       0,                                /* LDRS */
4911       0 },                              /* LDC */
4912     { "pc_g0",
4913       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
4914       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
4915       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
4916       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
4917     { "pc_g1_nc",
4918       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
4919       0,                                /* LDR */
4920       0,                                /* LDRS */
4921       0 },                              /* LDC */
4922     { "pc_g1",
4923       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
4924       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
4925       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
4926       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
4927     { "pc_g2",
4928       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
4929       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
4930       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
4931       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
4932     /* Section base relative */
4933     { "sb_g0_nc",
4934       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
4935       0,                                /* LDR */
4936       0,                                /* LDRS */
4937       0 },                              /* LDC */
4938     { "sb_g0",
4939       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
4940       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
4941       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
4942       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
4943     { "sb_g1_nc",
4944       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
4945       0,                                /* LDR */
4946       0,                                /* LDRS */
4947       0 },                              /* LDC */
4948     { "sb_g1",
4949       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
4950       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
4951       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
4952       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
4953     { "sb_g2",
4954       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
4955       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
4956       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
4957       BFD_RELOC_ARM_LDC_SB_G2 } };      /* LDC */
4958
4959 /* Given the address of a pointer pointing to the textual name of a group
4960    relocation as may appear in assembler source, attempt to find its details
4961    in group_reloc_table.  The pointer will be updated to the character after
4962    the trailing colon.  On failure, FAIL will be returned; SUCCESS
4963    otherwise.  On success, *entry will be updated to point at the relevant
4964    group_reloc_table entry. */
4965
4966 static int
4967 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
4968 {
4969   unsigned int i;
4970   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
4971     {
4972       int length = strlen (group_reloc_table[i].name);
4973
4974       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
4975           && (*str)[length] == ':')
4976         {
4977           *out = &group_reloc_table[i];
4978           *str += (length + 1);
4979           return SUCCESS;
4980         }
4981     }
4982
4983   return FAIL;
4984 }
4985
4986 /* Parse a <shifter_operand> for an ARM data processing instruction
4987    (as for parse_shifter_operand) where group relocations are allowed:
4988
4989       #<immediate>
4990       #<immediate>, <rotate>
4991       #:<group_reloc>:<expression>
4992       <Rm>
4993       <Rm>, <shift>
4994
4995    where <group_reloc> is one of the strings defined in group_reloc_table.
4996    The hashes are optional.
4997
4998    Everything else is as for parse_shifter_operand.  */
4999
5000 static parse_operand_result
5001 parse_shifter_operand_group_reloc (char **str, int i)
5002 {
5003   /* Determine if we have the sequence of characters #: or just :
5004      coming next.  If we do, then we check for a group relocation.
5005      If we don't, punt the whole lot to parse_shifter_operand.  */
5006
5007   if (((*str)[0] == '#' && (*str)[1] == ':')
5008       || (*str)[0] == ':')
5009     {
5010       struct group_reloc_table_entry *entry;
5011
5012       if ((*str)[0] == '#')
5013         (*str) += 2;
5014       else
5015         (*str)++;
5016
5017       /* Try to parse a group relocation.  Anything else is an error.  */
5018       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5019         {
5020           inst.error = _("unknown group relocation");
5021           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5022         }
5023
5024       /* We now have the group relocation table entry corresponding to
5025          the name in the assembler source.  Next, we parse the expression.  */
5026       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5027         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5028
5029       /* Record the relocation type (always the ALU variant here).  */
5030       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5031       gas_assert (inst.reloc.type != 0);
5032
5033       return PARSE_OPERAND_SUCCESS;
5034     }
5035   else
5036     return parse_shifter_operand (str, i) == SUCCESS
5037            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5038
5039   /* Never reached.  */
5040 }
5041
5042 /* Parse a Neon alignment expression.  Information is written to
5043    inst.operands[i].  We assume the initial ':' has been skipped.
5044    
5045    align        .imm = align << 8, .immisalign=1, .preind=0  */
5046 static parse_operand_result
5047 parse_neon_alignment (char **str, int i)
5048 {
5049   char *p = *str;
5050   expressionS exp;
5051
5052   my_get_expression (&exp, &p, GE_NO_PREFIX);
5053
5054   if (exp.X_op != O_constant)
5055     {
5056       inst.error = _("alignment must be constant");
5057       return PARSE_OPERAND_FAIL;
5058     }
5059
5060   inst.operands[i].imm = exp.X_add_number << 8;
5061   inst.operands[i].immisalign = 1;
5062   /* Alignments are not pre-indexes.  */
5063   inst.operands[i].preind = 0;
5064
5065   *str = p;
5066   return PARSE_OPERAND_SUCCESS;
5067 }
5068
5069 /* Parse all forms of an ARM address expression.  Information is written
5070    to inst.operands[i] and/or inst.reloc.
5071
5072    Preindexed addressing (.preind=1):
5073
5074    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5075    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5076    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5077                        .shift_kind=shift .reloc.exp=shift_imm
5078
5079    These three may have a trailing ! which causes .writeback to be set also.
5080
5081    Postindexed addressing (.postind=1, .writeback=1):
5082
5083    [Rn], #offset       .reg=Rn .reloc.exp=offset
5084    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5085    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5086                        .shift_kind=shift .reloc.exp=shift_imm
5087
5088    Unindexed addressing (.preind=0, .postind=0):
5089
5090    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5091
5092    Other:
5093
5094    [Rn]{!}             shorthand for [Rn,#0]{!}
5095    =immediate          .isreg=0 .reloc.exp=immediate
5096    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5097
5098   It is the caller's responsibility to check for addressing modes not
5099   supported by the instruction, and to set inst.reloc.type.  */
5100
5101 static parse_operand_result
5102 parse_address_main (char **str, int i, int group_relocations,
5103                     group_reloc_type group_type)
5104 {
5105   char *p = *str;
5106   int reg;
5107
5108   if (skip_past_char (&p, '[') == FAIL)
5109     {
5110       if (skip_past_char (&p, '=') == FAIL)
5111         {
5112           /* Bare address - translate to PC-relative offset.  */
5113           inst.reloc.pc_rel = 1;
5114           inst.operands[i].reg = REG_PC;
5115           inst.operands[i].isreg = 1;
5116           inst.operands[i].preind = 1;
5117         }
5118       /* Otherwise a load-constant pseudo op, no special treatment needed here.  */
5119
5120       if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5121         return PARSE_OPERAND_FAIL;
5122
5123       *str = p;
5124       return PARSE_OPERAND_SUCCESS;
5125     }
5126
5127   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5128     {
5129       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5130       return PARSE_OPERAND_FAIL;
5131     }
5132   inst.operands[i].reg = reg;
5133   inst.operands[i].isreg = 1;
5134
5135   if (skip_past_comma (&p) == SUCCESS)
5136     {
5137       inst.operands[i].preind = 1;
5138
5139       if (*p == '+') p++;
5140       else if (*p == '-') p++, inst.operands[i].negative = 1;
5141
5142       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5143         {
5144           inst.operands[i].imm = reg;
5145           inst.operands[i].immisreg = 1;
5146
5147           if (skip_past_comma (&p) == SUCCESS)
5148             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5149               return PARSE_OPERAND_FAIL;
5150         }
5151       else if (skip_past_char (&p, ':') == SUCCESS)
5152         {
5153           /* FIXME: '@' should be used here, but it's filtered out by generic
5154              code before we get to see it here. This may be subject to
5155              change.  */
5156           parse_operand_result result = parse_neon_alignment (&p, i);
5157           
5158           if (result != PARSE_OPERAND_SUCCESS)
5159             return result;
5160         }
5161       else
5162         {
5163           if (inst.operands[i].negative)
5164             {
5165               inst.operands[i].negative = 0;
5166               p--;
5167             }
5168
5169           if (group_relocations
5170               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5171             {
5172               struct group_reloc_table_entry *entry;
5173
5174               /* Skip over the #: or : sequence.  */
5175               if (*p == '#')
5176                 p += 2;
5177               else
5178                 p++;
5179
5180               /* Try to parse a group relocation.  Anything else is an
5181                  error.  */
5182               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5183                 {
5184                   inst.error = _("unknown group relocation");
5185                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5186                 }
5187
5188               /* We now have the group relocation table entry corresponding to
5189                  the name in the assembler source.  Next, we parse the
5190                  expression.  */
5191               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5192                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5193
5194               /* Record the relocation type.  */
5195               switch (group_type)
5196                 {
5197                   case GROUP_LDR:
5198                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5199                     break;
5200
5201                   case GROUP_LDRS:
5202                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5203                     break;
5204
5205                   case GROUP_LDC:
5206                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5207                     break;
5208
5209                   default:
5210                     gas_assert (0);
5211                 }
5212
5213               if (inst.reloc.type == 0)
5214                 {
5215                   inst.error = _("this group relocation is not allowed on this instruction");
5216                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5217                 }
5218             }
5219           else
5220             {
5221               char *q = p;
5222               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5223                 return PARSE_OPERAND_FAIL;
5224               /* If the offset is 0, find out if it's a +0 or -0.  */
5225               if (inst.reloc.exp.X_op == O_constant
5226                   && inst.reloc.exp.X_add_number == 0)
5227                 {
5228                   skip_whitespace (q);
5229                   if (*q == '#')
5230                     {
5231                       q++;
5232                       skip_whitespace (q);
5233                     }
5234                   if (*q == '-')
5235                     inst.operands[i].negative = 1;
5236                 }
5237             }
5238         }
5239     }
5240   else if (skip_past_char (&p, ':') == SUCCESS)
5241     {
5242       /* FIXME: '@' should be used here, but it's filtered out by generic code
5243          before we get to see it here. This may be subject to change.  */
5244       parse_operand_result result = parse_neon_alignment (&p, i);
5245       
5246       if (result != PARSE_OPERAND_SUCCESS)
5247         return result;
5248     }
5249
5250   if (skip_past_char (&p, ']') == FAIL)
5251     {
5252       inst.error = _("']' expected");
5253       return PARSE_OPERAND_FAIL;
5254     }
5255
5256   if (skip_past_char (&p, '!') == SUCCESS)
5257     inst.operands[i].writeback = 1;
5258
5259   else if (skip_past_comma (&p) == SUCCESS)
5260     {
5261       if (skip_past_char (&p, '{') == SUCCESS)
5262         {
5263           /* [Rn], {expr} - unindexed, with option */
5264           if (parse_immediate (&p, &inst.operands[i].imm,
5265                                0, 255, TRUE) == FAIL)
5266             return PARSE_OPERAND_FAIL;
5267
5268           if (skip_past_char (&p, '}') == FAIL)
5269             {
5270               inst.error = _("'}' expected at end of 'option' field");
5271               return PARSE_OPERAND_FAIL;
5272             }
5273           if (inst.operands[i].preind)
5274             {
5275               inst.error = _("cannot combine index with option");
5276               return PARSE_OPERAND_FAIL;
5277             }
5278           *str = p;
5279           return PARSE_OPERAND_SUCCESS;
5280         }
5281       else
5282         {
5283           inst.operands[i].postind = 1;
5284           inst.operands[i].writeback = 1;
5285
5286           if (inst.operands[i].preind)
5287             {
5288               inst.error = _("cannot combine pre- and post-indexing");
5289               return PARSE_OPERAND_FAIL;
5290             }
5291
5292           if (*p == '+') p++;
5293           else if (*p == '-') p++, inst.operands[i].negative = 1;
5294
5295           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5296             {
5297               /* We might be using the immediate for alignment already. If we
5298                  are, OR the register number into the low-order bits.  */
5299               if (inst.operands[i].immisalign)
5300                 inst.operands[i].imm |= reg;
5301               else
5302                 inst.operands[i].imm = reg;
5303               inst.operands[i].immisreg = 1;
5304
5305               if (skip_past_comma (&p) == SUCCESS)
5306                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5307                   return PARSE_OPERAND_FAIL;
5308             }
5309           else
5310             {
5311               char *q = p;
5312               if (inst.operands[i].negative)
5313                 {
5314                   inst.operands[i].negative = 0;
5315                   p--;
5316                 }
5317               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5318                 return PARSE_OPERAND_FAIL;
5319               /* If the offset is 0, find out if it's a +0 or -0.  */
5320               if (inst.reloc.exp.X_op == O_constant
5321                   && inst.reloc.exp.X_add_number == 0)
5322                 {
5323                   skip_whitespace (q);
5324                   if (*q == '#')
5325                     {
5326                       q++;
5327                       skip_whitespace (q);
5328                     }
5329                   if (*q == '-')
5330                     inst.operands[i].negative = 1;
5331                 }
5332             }
5333         }
5334     }
5335
5336   /* If at this point neither .preind nor .postind is set, we have a
5337      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5338   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5339     {
5340       inst.operands[i].preind = 1;
5341       inst.reloc.exp.X_op = O_constant;
5342       inst.reloc.exp.X_add_number = 0;
5343     }
5344   *str = p;
5345   return PARSE_OPERAND_SUCCESS;
5346 }
5347
5348 static int
5349 parse_address (char **str, int i)
5350 {
5351   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5352          ? SUCCESS : FAIL;
5353 }
5354
5355 static parse_operand_result
5356 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5357 {
5358   return parse_address_main (str, i, 1, type);
5359 }
5360
5361 /* Parse an operand for a MOVW or MOVT instruction.  */
5362 static int
5363 parse_half (char **str)
5364 {
5365   char * p;
5366
5367   p = *str;
5368   skip_past_char (&p, '#');
5369   if (strncasecmp (p, ":lower16:", 9) == 0)
5370     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5371   else if (strncasecmp (p, ":upper16:", 9) == 0)
5372     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5373
5374   if (inst.reloc.type != BFD_RELOC_UNUSED)
5375     {
5376       p += 9;
5377       skip_whitespace (p);
5378     }
5379
5380   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5381     return FAIL;
5382
5383   if (inst.reloc.type == BFD_RELOC_UNUSED)
5384     {
5385       if (inst.reloc.exp.X_op != O_constant)
5386         {
5387           inst.error = _("constant expression expected");
5388           return FAIL;
5389         }
5390       if (inst.reloc.exp.X_add_number < 0
5391           || inst.reloc.exp.X_add_number > 0xffff)
5392         {
5393           inst.error = _("immediate value out of range");
5394           return FAIL;
5395         }
5396     }
5397   *str = p;
5398   return SUCCESS;
5399 }
5400
5401 /* Miscellaneous. */
5402
5403 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5404    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5405 static int
5406 parse_psr (char **str, bfd_boolean lhs)
5407 {
5408   char *p;
5409   unsigned long psr_field;
5410   const struct asm_psr *psr;
5411   char *start;
5412   bfd_boolean is_apsr = FALSE;
5413   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5414
5415   /* PR gas/12698:  If the user has specified -march=all then m_profile will
5416      be TRUE, but we want to ignore it in this case as we are building for any
5417      CPU type, including non-m variants.  */
5418   if (selected_cpu.core == arm_arch_any.core)
5419     m_profile = FALSE;
5420
5421   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5422      feature for ease of use and backwards compatibility.  */
5423   p = *str;
5424   if (strncasecmp (p, "SPSR", 4) == 0)
5425     {
5426       if (m_profile)
5427         goto unsupported_psr;
5428         
5429       psr_field = SPSR_BIT;
5430     }
5431   else if (strncasecmp (p, "CPSR", 4) == 0)
5432     {
5433       if (m_profile)
5434         goto unsupported_psr;
5435
5436       psr_field = 0;
5437     }
5438   else if (strncasecmp (p, "APSR", 4) == 0)
5439     {
5440       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5441          and ARMv7-R architecture CPUs.  */
5442       is_apsr = TRUE;
5443       psr_field = 0;
5444     }
5445   else if (m_profile)
5446     {
5447       start = p;
5448       do
5449         p++;
5450       while (ISALNUM (*p) || *p == '_');
5451
5452       if (strncasecmp (start, "iapsr", 5) == 0
5453           || strncasecmp (start, "eapsr", 5) == 0
5454           || strncasecmp (start, "xpsr", 4) == 0
5455           || strncasecmp (start, "psr", 3) == 0)
5456         p = start + strcspn (start, "rR") + 1;
5457
5458       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5459                                                   p - start);
5460
5461       if (!psr)
5462         return FAIL;
5463
5464       /* If APSR is being written, a bitfield may be specified.  Note that
5465          APSR itself is handled above.  */
5466       if (psr->field <= 3)
5467         {
5468           psr_field = psr->field;
5469           is_apsr = TRUE;
5470           goto check_suffix;
5471         }
5472
5473       *str = p;
5474       /* M-profile MSR instructions have the mask field set to "10", except
5475          *PSR variants which modify APSR, which may use a different mask (and
5476          have been handled already).  Do that by setting the PSR_f field
5477          here.  */
5478       return psr->field | (lhs ? PSR_f : 0);
5479     }
5480   else
5481     goto unsupported_psr;
5482
5483   p += 4;
5484 check_suffix:
5485   if (*p == '_')
5486     {
5487       /* A suffix follows.  */
5488       p++;
5489       start = p;
5490
5491       do
5492         p++;
5493       while (ISALNUM (*p) || *p == '_');
5494
5495       if (is_apsr)
5496         {
5497           /* APSR uses a notation for bits, rather than fields.  */
5498           unsigned int nzcvq_bits = 0;
5499           unsigned int g_bit = 0;
5500           char *bit;
5501           
5502           for (bit = start; bit != p; bit++)
5503             {
5504               switch (TOLOWER (*bit))
5505                 {
5506                 case 'n':
5507                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5508                   break;
5509
5510                 case 'z':
5511                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5512                   break;
5513
5514                 case 'c':
5515                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5516                   break;
5517
5518                 case 'v':
5519                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5520                   break;
5521                 
5522                 case 'q':
5523                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5524                   break;
5525                 
5526                 case 'g':
5527                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5528                   break;
5529                 
5530                 default:
5531                   inst.error = _("unexpected bit specified after APSR");
5532                   return FAIL;
5533                 }
5534             }
5535           
5536           if (nzcvq_bits == 0x1f)
5537             psr_field |= PSR_f;
5538           
5539           if (g_bit == 0x1)
5540             {
5541               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5542                 {
5543                   inst.error = _("selected processor does not "
5544                                  "support DSP extension");
5545                   return FAIL;
5546                 }
5547
5548               psr_field |= PSR_s;
5549             }
5550           
5551           if ((nzcvq_bits & 0x20) != 0
5552               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5553               || (g_bit & 0x2) != 0)
5554             {
5555               inst.error = _("bad bitmask specified after APSR");
5556               return FAIL;
5557             }
5558         }
5559       else
5560         {
5561           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
5562                                                       p - start);
5563           if (!psr)
5564             goto error;
5565
5566           psr_field |= psr->field;
5567         }
5568     }
5569   else
5570     {
5571       if (ISALNUM (*p))
5572         goto error;    /* Garbage after "[CS]PSR".  */
5573
5574       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
5575          is deprecated, but allow it anyway.  */
5576       if (is_apsr && lhs)
5577         {
5578           psr_field |= PSR_f;
5579           as_tsktsk (_("writing to APSR without specifying a bitmask is "
5580                        "deprecated"));
5581         }
5582       else if (!m_profile)
5583         /* These bits are never right for M-profile devices: don't set them
5584            (only code paths which read/write APSR reach here).  */
5585         psr_field |= (PSR_c | PSR_f);
5586     }
5587   *str = p;
5588   return psr_field;
5589
5590  unsupported_psr:
5591   inst.error = _("selected processor does not support requested special "
5592                  "purpose register");
5593   return FAIL;
5594
5595  error:
5596   inst.error = _("flag for {c}psr instruction expected");
5597   return FAIL;
5598 }
5599
5600 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
5601    value suitable for splatting into the AIF field of the instruction.  */
5602
5603 static int
5604 parse_cps_flags (char **str)
5605 {
5606   int val = 0;
5607   int saw_a_flag = 0;
5608   char *s = *str;
5609
5610   for (;;)
5611     switch (*s++)
5612       {
5613       case '\0': case ',':
5614         goto done;
5615
5616       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
5617       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
5618       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
5619
5620       default:
5621         inst.error = _("unrecognized CPS flag");
5622         return FAIL;
5623       }
5624
5625  done:
5626   if (saw_a_flag == 0)
5627     {
5628       inst.error = _("missing CPS flags");
5629       return FAIL;
5630     }
5631
5632   *str = s - 1;
5633   return val;
5634 }
5635
5636 /* Parse an endian specifier ("BE" or "LE", case insensitive);
5637    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
5638
5639 static int
5640 parse_endian_specifier (char **str)
5641 {
5642   int little_endian;
5643   char *s = *str;
5644
5645   if (strncasecmp (s, "BE", 2))
5646     little_endian = 0;
5647   else if (strncasecmp (s, "LE", 2))
5648     little_endian = 1;
5649   else
5650     {
5651       inst.error = _("valid endian specifiers are be or le");
5652       return FAIL;
5653     }
5654
5655   if (ISALNUM (s[2]) || s[2] == '_')
5656     {
5657       inst.error = _("valid endian specifiers are be or le");
5658       return FAIL;
5659     }
5660
5661   *str = s + 2;
5662   return little_endian;
5663 }
5664
5665 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
5666    value suitable for poking into the rotate field of an sxt or sxta
5667    instruction, or FAIL on error.  */
5668
5669 static int
5670 parse_ror (char **str)
5671 {
5672   int rot;
5673   char *s = *str;
5674
5675   if (strncasecmp (s, "ROR", 3) == 0)
5676     s += 3;
5677   else
5678     {
5679       inst.error = _("missing rotation field after comma");
5680       return FAIL;
5681     }
5682
5683   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
5684     return FAIL;
5685
5686   switch (rot)
5687     {
5688     case  0: *str = s; return 0x0;
5689     case  8: *str = s; return 0x1;
5690     case 16: *str = s; return 0x2;
5691     case 24: *str = s; return 0x3;
5692
5693     default:
5694       inst.error = _("rotation can only be 0, 8, 16, or 24");
5695       return FAIL;
5696     }
5697 }
5698
5699 /* Parse a conditional code (from conds[] below).  The value returned is in the
5700    range 0 .. 14, or FAIL.  */
5701 static int
5702 parse_cond (char **str)
5703 {
5704   char *q;
5705   const struct asm_cond *c;
5706   int n;
5707   /* Condition codes are always 2 characters, so matching up to
5708      3 characters is sufficient.  */
5709   char cond[3];
5710
5711   q = *str;
5712   n = 0;
5713   while (ISALPHA (*q) && n < 3)
5714     {
5715       cond[n] = TOLOWER (*q);
5716       q++;
5717       n++;
5718     }
5719
5720   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
5721   if (!c)
5722     {
5723       inst.error = _("condition required");
5724       return FAIL;
5725     }
5726
5727   *str = q;
5728   return c->value;
5729 }
5730
5731 /* Parse an option for a barrier instruction.  Returns the encoding for the
5732    option, or FAIL.  */
5733 static int
5734 parse_barrier (char **str)
5735 {
5736   char *p, *q;
5737   const struct asm_barrier_opt *o;
5738
5739   p = q = *str;
5740   while (ISALPHA (*q))
5741     q++;
5742
5743   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
5744                                                     q - p);
5745   if (!o)
5746     return FAIL;
5747
5748   *str = q;
5749   return o->value;
5750 }
5751
5752 /* Parse the operands of a table branch instruction.  Similar to a memory
5753    operand.  */
5754 static int
5755 parse_tb (char **str)
5756 {
5757   char * p = *str;
5758   int reg;
5759
5760   if (skip_past_char (&p, '[') == FAIL)
5761     {
5762       inst.error = _("'[' expected");
5763       return FAIL;
5764     }
5765
5766   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5767     {
5768       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5769       return FAIL;
5770     }
5771   inst.operands[0].reg = reg;
5772
5773   if (skip_past_comma (&p) == FAIL)
5774     {
5775       inst.error = _("',' expected");
5776       return FAIL;
5777     }
5778
5779   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5780     {
5781       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5782       return FAIL;
5783     }
5784   inst.operands[0].imm = reg;
5785
5786   if (skip_past_comma (&p) == SUCCESS)
5787     {
5788       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
5789         return FAIL;
5790       if (inst.reloc.exp.X_add_number != 1)
5791         {
5792           inst.error = _("invalid shift");
5793           return FAIL;
5794         }
5795       inst.operands[0].shifted = 1;
5796     }
5797
5798   if (skip_past_char (&p, ']') == FAIL)
5799     {
5800       inst.error = _("']' expected");
5801       return FAIL;
5802     }
5803   *str = p;
5804   return SUCCESS;
5805 }
5806
5807 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
5808    information on the types the operands can take and how they are encoded.
5809    Up to four operands may be read; this function handles setting the
5810    ".present" field for each read operand itself.
5811    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
5812    else returns FAIL.  */
5813
5814 static int
5815 parse_neon_mov (char **str, int *which_operand)
5816 {
5817   int i = *which_operand, val;
5818   enum arm_reg_type rtype;
5819   char *ptr = *str;
5820   struct neon_type_el optype;
5821
5822   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
5823     {
5824       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
5825       inst.operands[i].reg = val;
5826       inst.operands[i].isscalar = 1;
5827       inst.operands[i].vectype = optype;
5828       inst.operands[i++].present = 1;
5829
5830       if (skip_past_comma (&ptr) == FAIL)
5831         goto wanted_comma;
5832
5833       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5834         goto wanted_arm;
5835
5836       inst.operands[i].reg = val;
5837       inst.operands[i].isreg = 1;
5838       inst.operands[i].present = 1;
5839     }
5840   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
5841            != FAIL)
5842     {
5843       /* Cases 0, 1, 2, 3, 5 (D only).  */
5844       if (skip_past_comma (&ptr) == FAIL)
5845         goto wanted_comma;
5846
5847       inst.operands[i].reg = val;
5848       inst.operands[i].isreg = 1;
5849       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5850       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5851       inst.operands[i].isvec = 1;
5852       inst.operands[i].vectype = optype;
5853       inst.operands[i++].present = 1;
5854
5855       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5856         {
5857           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
5858              Case 13: VMOV <Sd>, <Rm>  */
5859           inst.operands[i].reg = val;
5860           inst.operands[i].isreg = 1;
5861           inst.operands[i].present = 1;
5862
5863           if (rtype == REG_TYPE_NQ)
5864             {
5865               first_error (_("can't use Neon quad register here"));
5866               return FAIL;
5867             }
5868           else if (rtype != REG_TYPE_VFS)
5869             {
5870               i++;
5871               if (skip_past_comma (&ptr) == FAIL)
5872                 goto wanted_comma;
5873               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5874                 goto wanted_arm;
5875               inst.operands[i].reg = val;
5876               inst.operands[i].isreg = 1;
5877               inst.operands[i].present = 1;
5878             }
5879         }
5880       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
5881                                            &optype)) != FAIL)
5882         {
5883           /* Case 0: VMOV<c><q> <Qd>, <Qm>
5884              Case 1: VMOV<c><q> <Dd>, <Dm>
5885              Case 8: VMOV.F32 <Sd>, <Sm>
5886              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
5887
5888           inst.operands[i].reg = val;
5889           inst.operands[i].isreg = 1;
5890           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5891           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5892           inst.operands[i].isvec = 1;
5893           inst.operands[i].vectype = optype;
5894           inst.operands[i].present = 1;
5895
5896           if (skip_past_comma (&ptr) == SUCCESS)
5897             {
5898               /* Case 15.  */
5899               i++;
5900
5901               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5902                 goto wanted_arm;
5903
5904               inst.operands[i].reg = val;
5905               inst.operands[i].isreg = 1;
5906               inst.operands[i++].present = 1;
5907
5908               if (skip_past_comma (&ptr) == FAIL)
5909                 goto wanted_comma;
5910
5911               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5912                 goto wanted_arm;
5913
5914               inst.operands[i].reg = val;
5915               inst.operands[i].isreg = 1;
5916               inst.operands[i++].present = 1;
5917             }
5918         }
5919       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
5920           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
5921              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
5922              Case 10: VMOV.F32 <Sd>, #<imm>
5923              Case 11: VMOV.F64 <Dd>, #<imm>  */
5924         inst.operands[i].immisfloat = 1;
5925       else if (parse_big_immediate (&ptr, i) == SUCCESS)
5926           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
5927              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
5928         ;
5929       else
5930         {
5931           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
5932           return FAIL;
5933         }
5934     }
5935   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5936     {
5937       /* Cases 6, 7.  */
5938       inst.operands[i].reg = val;
5939       inst.operands[i].isreg = 1;
5940       inst.operands[i++].present = 1;
5941
5942       if (skip_past_comma (&ptr) == FAIL)
5943         goto wanted_comma;
5944
5945       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
5946         {
5947           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
5948           inst.operands[i].reg = val;
5949           inst.operands[i].isscalar = 1;
5950           inst.operands[i].present = 1;
5951           inst.operands[i].vectype = optype;
5952         }
5953       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5954         {
5955           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
5956           inst.operands[i].reg = val;
5957           inst.operands[i].isreg = 1;
5958           inst.operands[i++].present = 1;
5959
5960           if (skip_past_comma (&ptr) == FAIL)
5961             goto wanted_comma;
5962
5963           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
5964               == FAIL)
5965             {
5966               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
5967               return FAIL;
5968             }
5969
5970           inst.operands[i].reg = val;
5971           inst.operands[i].isreg = 1;
5972           inst.operands[i].isvec = 1;
5973           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5974           inst.operands[i].vectype = optype;
5975           inst.operands[i].present = 1;
5976
5977           if (rtype == REG_TYPE_VFS)
5978             {
5979               /* Case 14.  */
5980               i++;
5981               if (skip_past_comma (&ptr) == FAIL)
5982                 goto wanted_comma;
5983               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
5984                                               &optype)) == FAIL)
5985                 {
5986                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
5987                   return FAIL;
5988                 }
5989               inst.operands[i].reg = val;
5990               inst.operands[i].isreg = 1;
5991               inst.operands[i].isvec = 1;
5992               inst.operands[i].issingle = 1;
5993               inst.operands[i].vectype = optype;
5994               inst.operands[i].present = 1;
5995             }
5996         }
5997       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
5998                != FAIL)
5999         {
6000           /* Case 13.  */
6001           inst.operands[i].reg = val;
6002           inst.operands[i].isreg = 1;
6003           inst.operands[i].isvec = 1;
6004           inst.operands[i].issingle = 1;
6005           inst.operands[i].vectype = optype;
6006           inst.operands[i++].present = 1;
6007         }
6008     }
6009   else
6010     {
6011       first_error (_("parse error"));
6012       return FAIL;
6013     }
6014
6015   /* Successfully parsed the operands. Update args.  */
6016   *which_operand = i;
6017   *str = ptr;
6018   return SUCCESS;
6019
6020  wanted_comma:
6021   first_error (_("expected comma"));
6022   return FAIL;
6023
6024  wanted_arm:
6025   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6026   return FAIL;
6027 }
6028
6029 /* Use this macro when the operand constraints are different
6030    for ARM and THUMB (e.g. ldrd).  */
6031 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6032         ((arm_operand) | ((thumb_operand) << 16))
6033
6034 /* Matcher codes for parse_operands.  */
6035 enum operand_parse_code
6036 {
6037   OP_stop,      /* end of line */
6038
6039   OP_RR,        /* ARM register */
6040   OP_RRnpc,     /* ARM register, not r15 */
6041   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6042   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6043   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback, 
6044                    optional trailing ! */
6045   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6046   OP_RCP,       /* Coprocessor number */
6047   OP_RCN,       /* Coprocessor register */
6048   OP_RF,        /* FPA register */
6049   OP_RVS,       /* VFP single precision register */
6050   OP_RVD,       /* VFP double precision register (0..15) */
6051   OP_RND,       /* Neon double precision register (0..31) */
6052   OP_RNQ,       /* Neon quad precision register */
6053   OP_RVSD,      /* VFP single or double precision register */
6054   OP_RNDQ,      /* Neon double or quad precision register */
6055   OP_RNSDQ,     /* Neon single, double or quad precision register */
6056   OP_RNSC,      /* Neon scalar D[X] */
6057   OP_RVC,       /* VFP control register */
6058   OP_RMF,       /* Maverick F register */
6059   OP_RMD,       /* Maverick D register */
6060   OP_RMFX,      /* Maverick FX register */
6061   OP_RMDX,      /* Maverick DX register */
6062   OP_RMAX,      /* Maverick AX register */
6063   OP_RMDS,      /* Maverick DSPSC register */
6064   OP_RIWR,      /* iWMMXt wR register */
6065   OP_RIWC,      /* iWMMXt wC register */
6066   OP_RIWG,      /* iWMMXt wCG register */
6067   OP_RXA,       /* XScale accumulator register */
6068
6069   OP_REGLST,    /* ARM register list */
6070   OP_VRSLST,    /* VFP single-precision register list */
6071   OP_VRDLST,    /* VFP double-precision register list */
6072   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6073   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6074   OP_NSTRLST,   /* Neon element/structure list */
6075
6076   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6077   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6078   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6079   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6080   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6081   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6082   OP_VMOV,      /* Neon VMOV operands.  */
6083   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6084   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6085   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6086
6087   OP_I0,        /* immediate zero */
6088   OP_I7,        /* immediate value 0 .. 7 */
6089   OP_I15,       /*                 0 .. 15 */
6090   OP_I16,       /*                 1 .. 16 */
6091   OP_I16z,      /*                 0 .. 16 */
6092   OP_I31,       /*                 0 .. 31 */
6093   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6094   OP_I32,       /*                 1 .. 32 */
6095   OP_I32z,      /*                 0 .. 32 */
6096   OP_I63,       /*                 0 .. 63 */
6097   OP_I63s,      /*               -64 .. 63 */
6098   OP_I64,       /*                 1 .. 64 */
6099   OP_I64z,      /*                 0 .. 64 */
6100   OP_I255,      /*                 0 .. 255 */
6101
6102   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6103   OP_I7b,       /*                             0 .. 7 */
6104   OP_I15b,      /*                             0 .. 15 */
6105   OP_I31b,      /*                             0 .. 31 */
6106
6107   OP_SH,        /* shifter operand */
6108   OP_SHG,       /* shifter operand with possible group relocation */
6109   OP_ADDR,      /* Memory address expression (any mode) */
6110   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6111   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6112   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6113   OP_EXP,       /* arbitrary expression */
6114   OP_EXPi,      /* same, with optional immediate prefix */
6115   OP_EXPr,      /* same, with optional relocation suffix */
6116   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6117
6118   OP_CPSF,      /* CPS flags */
6119   OP_ENDI,      /* Endianness specifier */
6120   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6121   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6122   OP_COND,      /* conditional code */
6123   OP_TB,        /* Table branch.  */
6124
6125   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6126
6127   OP_RRnpc_I0,  /* ARM register or literal 0 */
6128   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
6129   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6130   OP_RF_IF,     /* FPA register or immediate */
6131   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6132   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6133
6134   /* Optional operands.  */
6135   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6136   OP_oI31b,      /*                             0 .. 31 */
6137   OP_oI32b,      /*                             1 .. 32 */
6138   OP_oI32z,      /*                             0 .. 32 */
6139   OP_oIffffb,    /*                             0 .. 65535 */
6140   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6141
6142   OP_oRR,        /* ARM register */
6143   OP_oRRnpc,     /* ARM register, not the PC */
6144   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6145   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6146   OP_oRND,       /* Optional Neon double precision register */
6147   OP_oRNQ,       /* Optional Neon quad precision register */
6148   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6149   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6150   OP_oSHll,      /* LSL immediate */
6151   OP_oSHar,      /* ASR immediate */
6152   OP_oSHllar,    /* LSL or ASR immediate */
6153   OP_oROR,       /* ROR 0/8/16/24 */
6154   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6155
6156   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6157   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6158   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6159   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6160
6161   OP_FIRST_OPTIONAL = OP_oI7b
6162 };
6163
6164 /* Generic instruction operand parser.  This does no encoding and no
6165    semantic validation; it merely squirrels values away in the inst
6166    structure.  Returns SUCCESS or FAIL depending on whether the
6167    specified grammar matched.  */
6168 static int
6169 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6170 {
6171   unsigned const int *upat = pattern;
6172   char *backtrack_pos = 0;
6173   const char *backtrack_error = 0;
6174   int i, val, backtrack_index = 0;
6175   enum arm_reg_type rtype;
6176   parse_operand_result result;
6177   unsigned int op_parse_code;
6178
6179 #define po_char_or_fail(chr)                    \
6180   do                                            \
6181     {                                           \
6182       if (skip_past_char (&str, chr) == FAIL)   \
6183         goto bad_args;                          \
6184     }                                           \
6185   while (0)
6186
6187 #define po_reg_or_fail(regtype)                                 \
6188   do                                                            \
6189     {                                                           \
6190       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6191                                  & inst.operands[i].vectype);   \
6192       if (val == FAIL)                                          \
6193         {                                                       \
6194           first_error (_(reg_expected_msgs[regtype]));          \
6195           goto failure;                                         \
6196         }                                                       \
6197       inst.operands[i].reg = val;                               \
6198       inst.operands[i].isreg = 1;                               \
6199       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6200       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6201       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6202                              || rtype == REG_TYPE_VFD           \
6203                              || rtype == REG_TYPE_NQ);          \
6204     }                                                           \
6205   while (0)
6206
6207 #define po_reg_or_goto(regtype, label)                          \
6208   do                                                            \
6209     {                                                           \
6210       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6211                                  & inst.operands[i].vectype);   \
6212       if (val == FAIL)                                          \
6213         goto label;                                             \
6214                                                                 \
6215       inst.operands[i].reg = val;                               \
6216       inst.operands[i].isreg = 1;                               \
6217       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6218       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6219       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6220                              || rtype == REG_TYPE_VFD           \
6221                              || rtype == REG_TYPE_NQ);          \
6222     }                                                           \
6223   while (0)
6224
6225 #define po_imm_or_fail(min, max, popt)                          \
6226   do                                                            \
6227     {                                                           \
6228       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6229         goto failure;                                           \
6230       inst.operands[i].imm = val;                               \
6231     }                                                           \
6232   while (0)
6233
6234 #define po_scalar_or_goto(elsz, label)                                  \
6235   do                                                                    \
6236     {                                                                   \
6237       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6238       if (val == FAIL)                                                  \
6239         goto label;                                                     \
6240       inst.operands[i].reg = val;                                       \
6241       inst.operands[i].isscalar = 1;                                    \
6242     }                                                                   \
6243   while (0)
6244
6245 #define po_misc_or_fail(expr)                   \
6246   do                                            \
6247     {                                           \
6248       if (expr)                                 \
6249         goto failure;                           \
6250     }                                           \
6251   while (0)
6252
6253 #define po_misc_or_fail_no_backtrack(expr)              \
6254   do                                                    \
6255     {                                                   \
6256       result = expr;                                    \
6257       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6258         backtrack_pos = 0;                              \
6259       if (result != PARSE_OPERAND_SUCCESS)              \
6260         goto failure;                                   \
6261     }                                                   \
6262   while (0)
6263
6264 #define po_barrier_or_imm(str)                             \
6265   do                                                       \
6266     {                                                      \
6267       val = parse_barrier (&str);                          \
6268       if (val == FAIL)                                     \
6269         {                                                  \
6270           if (ISALPHA (*str))                              \
6271               goto failure;                                \
6272           else                                             \
6273               goto immediate;                              \
6274         }                                                  \
6275       else                                                 \
6276         {                                                  \
6277           if ((inst.instruction & 0xf0) == 0x60            \
6278               && val != 0xf)                               \
6279             {                                              \
6280                /* ISB can only take SY as an option.  */   \
6281                inst.error = _("invalid barrier type");     \
6282                goto failure;                               \
6283             }                                              \
6284         }                                                  \
6285     }                                                      \
6286   while (0)
6287
6288   skip_whitespace (str);
6289
6290   for (i = 0; upat[i] != OP_stop; i++)
6291     {
6292       op_parse_code = upat[i];
6293       if (op_parse_code >= 1<<16)
6294         op_parse_code = thumb ? (op_parse_code >> 16)
6295                                 : (op_parse_code & ((1<<16)-1));
6296
6297       if (op_parse_code >= OP_FIRST_OPTIONAL)
6298         {
6299           /* Remember where we are in case we need to backtrack.  */
6300           gas_assert (!backtrack_pos);
6301           backtrack_pos = str;
6302           backtrack_error = inst.error;
6303           backtrack_index = i;
6304         }
6305
6306       if (i > 0 && (i > 1 || inst.operands[0].present))
6307         po_char_or_fail (',');
6308
6309       switch (op_parse_code)
6310         {
6311           /* Registers */
6312         case OP_oRRnpc:
6313         case OP_oRRnpcsp:
6314         case OP_RRnpc:
6315         case OP_RRnpcsp:
6316         case OP_oRR:
6317         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6318         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6319         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6320         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6321         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6322         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6323         case OP_oRND:
6324         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6325         case OP_RVC:
6326           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6327           break;
6328           /* Also accept generic coprocessor regs for unknown registers.  */
6329           coproc_reg:
6330           po_reg_or_fail (REG_TYPE_CN);
6331           break;
6332         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6333         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6334         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6335         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6336         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6337         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6338         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6339         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6340         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6341         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6342         case OP_oRNQ:
6343         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6344         case OP_oRNDQ:
6345         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6346         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6347         case OP_oRNSDQ:
6348         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6349
6350         /* Neon scalar. Using an element size of 8 means that some invalid
6351            scalars are accepted here, so deal with those in later code.  */
6352         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6353
6354         case OP_RNDQ_I0:
6355           {
6356             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6357             break;
6358             try_imm0:
6359             po_imm_or_fail (0, 0, TRUE);
6360           }
6361           break;
6362
6363         case OP_RVSD_I0:
6364           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6365           break;
6366
6367         case OP_RR_RNSC:
6368           {
6369             po_scalar_or_goto (8, try_rr);
6370             break;
6371             try_rr:
6372             po_reg_or_fail (REG_TYPE_RN);
6373           }
6374           break;
6375
6376         case OP_RNSDQ_RNSC:
6377           {
6378             po_scalar_or_goto (8, try_nsdq);
6379             break;
6380             try_nsdq:
6381             po_reg_or_fail (REG_TYPE_NSDQ);
6382           }
6383           break;
6384
6385         case OP_RNDQ_RNSC:
6386           {
6387             po_scalar_or_goto (8, try_ndq);
6388             break;
6389             try_ndq:
6390             po_reg_or_fail (REG_TYPE_NDQ);
6391           }
6392           break;
6393
6394         case OP_RND_RNSC:
6395           {
6396             po_scalar_or_goto (8, try_vfd);
6397             break;
6398             try_vfd:
6399             po_reg_or_fail (REG_TYPE_VFD);
6400           }
6401           break;
6402
6403         case OP_VMOV:
6404           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6405              not careful then bad things might happen.  */
6406           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6407           break;
6408
6409         case OP_RNDQ_Ibig:
6410           {
6411             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6412             break;
6413             try_immbig:
6414             /* There's a possibility of getting a 64-bit immediate here, so
6415                we need special handling.  */
6416             if (parse_big_immediate (&str, i) == FAIL)
6417               {
6418                 inst.error = _("immediate value is out of range");
6419                 goto failure;
6420               }
6421           }
6422           break;
6423
6424         case OP_RNDQ_I63b:
6425           {
6426             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6427             break;
6428             try_shimm:
6429             po_imm_or_fail (0, 63, TRUE);
6430           }
6431           break;
6432
6433         case OP_RRnpcb:
6434           po_char_or_fail ('[');
6435           po_reg_or_fail  (REG_TYPE_RN);
6436           po_char_or_fail (']');
6437           break;
6438
6439         case OP_RRnpctw:
6440         case OP_RRw:
6441         case OP_oRRw:
6442           po_reg_or_fail (REG_TYPE_RN);
6443           if (skip_past_char (&str, '!') == SUCCESS)
6444             inst.operands[i].writeback = 1;
6445           break;
6446
6447           /* Immediates */
6448         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6449         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6450         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6451         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6452         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6453         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6454         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6455         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6456         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6457         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6458         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6459         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6460
6461         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6462         case OP_oI7b:
6463         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6464         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6465         case OP_oI31b:
6466         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6467         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6468         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
6469         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6470
6471           /* Immediate variants */
6472         case OP_oI255c:
6473           po_char_or_fail ('{');
6474           po_imm_or_fail (0, 255, TRUE);
6475           po_char_or_fail ('}');
6476           break;
6477
6478         case OP_I31w:
6479           /* The expression parser chokes on a trailing !, so we have
6480              to find it first and zap it.  */
6481           {
6482             char *s = str;
6483             while (*s && *s != ',')
6484               s++;
6485             if (s[-1] == '!')
6486               {
6487                 s[-1] = '\0';
6488                 inst.operands[i].writeback = 1;
6489               }
6490             po_imm_or_fail (0, 31, TRUE);
6491             if (str == s - 1)
6492               str = s;
6493           }
6494           break;
6495
6496           /* Expressions */
6497         case OP_EXPi:   EXPi:
6498           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6499                                               GE_OPT_PREFIX));
6500           break;
6501
6502         case OP_EXP:
6503           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6504                                               GE_NO_PREFIX));
6505           break;
6506
6507         case OP_EXPr:   EXPr:
6508           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6509                                               GE_NO_PREFIX));
6510           if (inst.reloc.exp.X_op == O_symbol)
6511             {
6512               val = parse_reloc (&str);
6513               if (val == -1)
6514                 {
6515                   inst.error = _("unrecognized relocation suffix");
6516                   goto failure;
6517                 }
6518               else if (val != BFD_RELOC_UNUSED)
6519                 {
6520                   inst.operands[i].imm = val;
6521                   inst.operands[i].hasreloc = 1;
6522                 }
6523             }
6524           break;
6525
6526           /* Operand for MOVW or MOVT.  */
6527         case OP_HALF:
6528           po_misc_or_fail (parse_half (&str));
6529           break;
6530
6531           /* Register or expression.  */
6532         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
6533         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
6534
6535           /* Register or immediate.  */
6536         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
6537         I0:               po_imm_or_fail (0, 0, FALSE);       break;
6538
6539         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
6540         IF:
6541           if (!is_immediate_prefix (*str))
6542             goto bad_args;
6543           str++;
6544           val = parse_fpa_immediate (&str);
6545           if (val == FAIL)
6546             goto failure;
6547           /* FPA immediates are encoded as registers 8-15.
6548              parse_fpa_immediate has already applied the offset.  */
6549           inst.operands[i].reg = val;
6550           inst.operands[i].isreg = 1;
6551           break;
6552
6553         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
6554         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
6555
6556           /* Two kinds of register.  */
6557         case OP_RIWR_RIWC:
6558           {
6559             struct reg_entry *rege = arm_reg_parse_multi (&str);
6560             if (!rege
6561                 || (rege->type != REG_TYPE_MMXWR
6562                     && rege->type != REG_TYPE_MMXWC
6563                     && rege->type != REG_TYPE_MMXWCG))
6564               {
6565                 inst.error = _("iWMMXt data or control register expected");
6566                 goto failure;
6567               }
6568             inst.operands[i].reg = rege->number;
6569             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
6570           }
6571           break;
6572
6573         case OP_RIWC_RIWG:
6574           {
6575             struct reg_entry *rege = arm_reg_parse_multi (&str);
6576             if (!rege
6577                 || (rege->type != REG_TYPE_MMXWC
6578                     && rege->type != REG_TYPE_MMXWCG))
6579               {
6580                 inst.error = _("iWMMXt control register expected");
6581                 goto failure;
6582               }
6583             inst.operands[i].reg = rege->number;
6584             inst.operands[i].isreg = 1;
6585           }
6586           break;
6587
6588           /* Misc */
6589         case OP_CPSF:    val = parse_cps_flags (&str);          break;
6590         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
6591         case OP_oROR:    val = parse_ror (&str);                break;
6592         case OP_COND:    val = parse_cond (&str);               break;
6593         case OP_oBARRIER_I15:
6594           po_barrier_or_imm (str); break;
6595           immediate:
6596           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
6597             goto failure;
6598           break;
6599
6600         case OP_wPSR:    
6601         case OP_rPSR:
6602           po_reg_or_goto (REG_TYPE_RNB, try_psr);
6603           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
6604             {
6605               inst.error = _("Banked registers are not available with this "
6606                              "architecture.");
6607               goto failure;
6608             }
6609           break;
6610           try_psr:
6611           val = parse_psr (&str, op_parse_code == OP_wPSR);
6612           break;
6613
6614         case OP_APSR_RR:
6615           po_reg_or_goto (REG_TYPE_RN, try_apsr);
6616           break;
6617           try_apsr:
6618           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
6619              instruction).  */
6620           if (strncasecmp (str, "APSR_", 5) == 0)
6621             {
6622               unsigned found = 0;
6623               str += 5;
6624               while (found < 15)
6625                 switch (*str++)
6626                   {
6627                   case 'c': found = (found & 1) ? 16 : found | 1; break;
6628                   case 'n': found = (found & 2) ? 16 : found | 2; break;
6629                   case 'z': found = (found & 4) ? 16 : found | 4; break;
6630                   case 'v': found = (found & 8) ? 16 : found | 8; break;
6631                   default: found = 16;
6632                   }
6633               if (found != 15)
6634                 goto failure;
6635               inst.operands[i].isvec = 1;
6636               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
6637               inst.operands[i].reg = REG_PC;
6638             }
6639           else
6640             goto failure;
6641           break;
6642
6643         case OP_TB:
6644           po_misc_or_fail (parse_tb (&str));
6645           break;
6646
6647           /* Register lists.  */
6648         case OP_REGLST:
6649           val = parse_reg_list (&str);
6650           if (*str == '^')
6651             {
6652               inst.operands[1].writeback = 1;
6653               str++;
6654             }
6655           break;
6656
6657         case OP_VRSLST:
6658           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
6659           break;
6660
6661         case OP_VRDLST:
6662           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
6663           break;
6664
6665         case OP_VRSDLST:
6666           /* Allow Q registers too.  */
6667           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6668                                     REGLIST_NEON_D);
6669           if (val == FAIL)
6670             {
6671               inst.error = NULL;
6672               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6673                                         REGLIST_VFP_S);
6674               inst.operands[i].issingle = 1;
6675             }
6676           break;
6677
6678         case OP_NRDLST:
6679           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
6680                                     REGLIST_NEON_D);
6681           break;
6682
6683         case OP_NSTRLST:
6684           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
6685                                            &inst.operands[i].vectype);
6686           break;
6687
6688           /* Addressing modes */
6689         case OP_ADDR:
6690           po_misc_or_fail (parse_address (&str, i));
6691           break;
6692
6693         case OP_ADDRGLDR:
6694           po_misc_or_fail_no_backtrack (
6695             parse_address_group_reloc (&str, i, GROUP_LDR));
6696           break;
6697
6698         case OP_ADDRGLDRS:
6699           po_misc_or_fail_no_backtrack (
6700             parse_address_group_reloc (&str, i, GROUP_LDRS));
6701           break;
6702
6703         case OP_ADDRGLDC:
6704           po_misc_or_fail_no_backtrack (
6705             parse_address_group_reloc (&str, i, GROUP_LDC));
6706           break;
6707
6708         case OP_SH:
6709           po_misc_or_fail (parse_shifter_operand (&str, i));
6710           break;
6711
6712         case OP_SHG:
6713           po_misc_or_fail_no_backtrack (
6714             parse_shifter_operand_group_reloc (&str, i));
6715           break;
6716
6717         case OP_oSHll:
6718           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
6719           break;
6720
6721         case OP_oSHar:
6722           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
6723           break;
6724
6725         case OP_oSHllar:
6726           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
6727           break;
6728
6729         default:
6730           as_fatal (_("unhandled operand code %d"), op_parse_code);
6731         }
6732
6733       /* Various value-based sanity checks and shared operations.  We
6734          do not signal immediate failures for the register constraints;
6735          this allows a syntax error to take precedence.  */
6736       switch (op_parse_code)
6737         {
6738         case OP_oRRnpc:
6739         case OP_RRnpc:
6740         case OP_RRnpcb:
6741         case OP_RRw:
6742         case OP_oRRw:
6743         case OP_RRnpc_I0:
6744           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
6745             inst.error = BAD_PC;
6746           break;
6747
6748         case OP_oRRnpcsp:
6749         case OP_RRnpcsp:
6750           if (inst.operands[i].isreg)
6751             {
6752               if (inst.operands[i].reg == REG_PC)
6753                 inst.error = BAD_PC;
6754               else if (inst.operands[i].reg == REG_SP)
6755                 inst.error = BAD_SP;
6756             }
6757           break;
6758
6759         case OP_RRnpctw:
6760           if (inst.operands[i].isreg 
6761               && inst.operands[i].reg == REG_PC 
6762               && (inst.operands[i].writeback || thumb))
6763             inst.error = BAD_PC;
6764           break;
6765
6766         case OP_CPSF:
6767         case OP_ENDI:
6768         case OP_oROR:
6769         case OP_wPSR:
6770         case OP_rPSR:
6771         case OP_COND:
6772         case OP_oBARRIER_I15:
6773         case OP_REGLST:
6774         case OP_VRSLST:
6775         case OP_VRDLST:
6776         case OP_VRSDLST:
6777         case OP_NRDLST:
6778         case OP_NSTRLST:
6779           if (val == FAIL)
6780             goto failure;
6781           inst.operands[i].imm = val;
6782           break;
6783
6784         default:
6785           break;
6786         }
6787
6788       /* If we get here, this operand was successfully parsed.  */
6789       inst.operands[i].present = 1;
6790       continue;
6791
6792     bad_args:
6793       inst.error = BAD_ARGS;
6794
6795     failure:
6796       if (!backtrack_pos)
6797         {
6798           /* The parse routine should already have set inst.error, but set a
6799              default here just in case.  */
6800           if (!inst.error)
6801             inst.error = _("syntax error");
6802           return FAIL;
6803         }
6804
6805       /* Do not backtrack over a trailing optional argument that
6806          absorbed some text.  We will only fail again, with the
6807          'garbage following instruction' error message, which is
6808          probably less helpful than the current one.  */
6809       if (backtrack_index == i && backtrack_pos != str
6810           && upat[i+1] == OP_stop)
6811         {
6812           if (!inst.error)
6813             inst.error = _("syntax error");
6814           return FAIL;
6815         }
6816
6817       /* Try again, skipping the optional argument at backtrack_pos.  */
6818       str = backtrack_pos;
6819       inst.error = backtrack_error;
6820       inst.operands[backtrack_index].present = 0;
6821       i = backtrack_index;
6822       backtrack_pos = 0;
6823     }
6824
6825   /* Check that we have parsed all the arguments.  */
6826   if (*str != '\0' && !inst.error)
6827     inst.error = _("garbage following instruction");
6828
6829   return inst.error ? FAIL : SUCCESS;
6830 }
6831
6832 #undef po_char_or_fail
6833 #undef po_reg_or_fail
6834 #undef po_reg_or_goto
6835 #undef po_imm_or_fail
6836 #undef po_scalar_or_fail
6837 #undef po_barrier_or_imm
6838
6839 /* Shorthand macro for instruction encoding functions issuing errors.  */
6840 #define constraint(expr, err)                   \
6841   do                                            \
6842     {                                           \
6843       if (expr)                                 \
6844         {                                       \
6845           inst.error = err;                     \
6846           return;                               \
6847         }                                       \
6848     }                                           \
6849   while (0)
6850
6851 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
6852    instructions are unpredictable if these registers are used.  This
6853    is the BadReg predicate in ARM's Thumb-2 documentation.  */
6854 #define reject_bad_reg(reg)                             \
6855   do                                                    \
6856    if (reg == REG_SP || reg == REG_PC)                  \
6857      {                                                  \
6858        inst.error = (reg == REG_SP) ? BAD_SP : BAD_PC;  \
6859        return;                                          \
6860      }                                                  \
6861   while (0)
6862
6863 /* If REG is R13 (the stack pointer), warn that its use is
6864    deprecated.  */
6865 #define warn_deprecated_sp(reg)                 \
6866   do                                            \
6867     if (warn_on_deprecated && reg == REG_SP)    \
6868        as_warn (_("use of r13 is deprecated")); \
6869   while (0)
6870
6871 /* Functions for operand encoding.  ARM, then Thumb.  */
6872
6873 #define rotate_left(v, n) (v << n | v >> (32 - n))
6874
6875 /* If VAL can be encoded in the immediate field of an ARM instruction,
6876    return the encoded form.  Otherwise, return FAIL.  */
6877
6878 static unsigned int
6879 encode_arm_immediate (unsigned int val)
6880 {
6881   unsigned int a, i;
6882
6883   for (i = 0; i < 32; i += 2)
6884     if ((a = rotate_left (val, i)) <= 0xff)
6885       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
6886
6887   return FAIL;
6888 }
6889
6890 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
6891    return the encoded form.  Otherwise, return FAIL.  */
6892 static unsigned int
6893 encode_thumb32_immediate (unsigned int val)
6894 {
6895   unsigned int a, i;
6896
6897   if (val <= 0xff)
6898     return val;
6899
6900   for (i = 1; i <= 24; i++)
6901     {
6902       a = val >> i;
6903       if ((val & ~(0xff << i)) == 0)
6904         return ((val >> i) & 0x7f) | ((32 - i) << 7);
6905     }
6906
6907   a = val & 0xff;
6908   if (val == ((a << 16) | a))
6909     return 0x100 | a;
6910   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
6911     return 0x300 | a;
6912
6913   a = val & 0xff00;
6914   if (val == ((a << 16) | a))
6915     return 0x200 | (a >> 8);
6916
6917   return FAIL;
6918 }
6919 /* Encode a VFP SP or DP register number into inst.instruction.  */
6920
6921 static void
6922 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
6923 {
6924   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
6925       && reg > 15)
6926     {
6927       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
6928         {
6929           if (thumb_mode)
6930             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
6931                                     fpu_vfp_ext_d32);
6932           else
6933             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
6934                                     fpu_vfp_ext_d32);
6935         }
6936       else
6937         {
6938           first_error (_("D register out of range for selected VFP version"));
6939           return;
6940         }
6941     }
6942
6943   switch (pos)
6944     {
6945     case VFP_REG_Sd:
6946       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
6947       break;
6948
6949     case VFP_REG_Sn:
6950       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
6951       break;
6952
6953     case VFP_REG_Sm:
6954       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
6955       break;
6956
6957     case VFP_REG_Dd:
6958       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
6959       break;
6960
6961     case VFP_REG_Dn:
6962       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
6963       break;
6964
6965     case VFP_REG_Dm:
6966       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
6967       break;
6968
6969     default:
6970       abort ();
6971     }
6972 }
6973
6974 /* Encode a <shift> in an ARM-format instruction.  The immediate,
6975    if any, is handled by md_apply_fix.   */
6976 static void
6977 encode_arm_shift (int i)
6978 {
6979   if (inst.operands[i].shift_kind == SHIFT_RRX)
6980     inst.instruction |= SHIFT_ROR << 5;
6981   else
6982     {
6983       inst.instruction |= inst.operands[i].shift_kind << 5;
6984       if (inst.operands[i].immisreg)
6985         {
6986           inst.instruction |= SHIFT_BY_REG;
6987           inst.instruction |= inst.operands[i].imm << 8;
6988         }
6989       else
6990         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
6991     }
6992 }
6993
6994 static void
6995 encode_arm_shifter_operand (int i)
6996 {
6997   if (inst.operands[i].isreg)
6998     {
6999       inst.instruction |= inst.operands[i].reg;
7000       encode_arm_shift (i);
7001     }
7002   else
7003     inst.instruction |= INST_IMMEDIATE;
7004 }
7005
7006 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7007 static void
7008 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7009 {
7010   gas_assert (inst.operands[i].isreg);
7011   inst.instruction |= inst.operands[i].reg << 16;
7012
7013   if (inst.operands[i].preind)
7014     {
7015       if (is_t)
7016         {
7017           inst.error = _("instruction does not accept preindexed addressing");
7018           return;
7019         }
7020       inst.instruction |= PRE_INDEX;
7021       if (inst.operands[i].writeback)
7022         inst.instruction |= WRITE_BACK;
7023
7024     }
7025   else if (inst.operands[i].postind)
7026     {
7027       gas_assert (inst.operands[i].writeback);
7028       if (is_t)
7029         inst.instruction |= WRITE_BACK;
7030     }
7031   else /* unindexed - only for coprocessor */
7032     {
7033       inst.error = _("instruction does not accept unindexed addressing");
7034       return;
7035     }
7036
7037   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7038       && (((inst.instruction & 0x000f0000) >> 16)
7039           == ((inst.instruction & 0x0000f000) >> 12)))
7040     as_warn ((inst.instruction & LOAD_BIT)
7041              ? _("destination register same as write-back base")
7042              : _("source register same as write-back base"));
7043 }
7044
7045 /* inst.operands[i] was set up by parse_address.  Encode it into an
7046    ARM-format mode 2 load or store instruction.  If is_t is true,
7047    reject forms that cannot be used with a T instruction (i.e. not
7048    post-indexed).  */
7049 static void
7050 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7051 {
7052   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7053
7054   encode_arm_addr_mode_common (i, is_t);
7055
7056   if (inst.operands[i].immisreg)
7057     {
7058       constraint ((inst.operands[i].imm == REG_PC
7059                    || (is_pc && inst.operands[i].writeback)),
7060                   BAD_PC_ADDRESSING);
7061       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7062       inst.instruction |= inst.operands[i].imm;
7063       if (!inst.operands[i].negative)
7064         inst.instruction |= INDEX_UP;
7065       if (inst.operands[i].shifted)
7066         {
7067           if (inst.operands[i].shift_kind == SHIFT_RRX)
7068             inst.instruction |= SHIFT_ROR << 5;
7069           else
7070             {
7071               inst.instruction |= inst.operands[i].shift_kind << 5;
7072               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7073             }
7074         }
7075     }
7076   else /* immediate offset in inst.reloc */
7077     {
7078       if (is_pc && !inst.reloc.pc_rel)
7079         {
7080           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7081
7082           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7083              cannot use PC in addressing.
7084              PC cannot be used in writeback addressing, either.  */
7085           constraint ((is_t || inst.operands[i].writeback),
7086                       BAD_PC_ADDRESSING);
7087
7088           /* Use of PC in str is deprecated for ARMv7.  */
7089           if (warn_on_deprecated
7090               && !is_load
7091               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7092             as_warn (_("use of PC in this instruction is deprecated"));
7093         }
7094
7095       if (inst.reloc.type == BFD_RELOC_UNUSED)
7096         {
7097           /* Prefer + for zero encoded value.  */
7098           if (!inst.operands[i].negative)
7099             inst.instruction |= INDEX_UP;
7100           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7101         }
7102     }
7103 }
7104
7105 /* inst.operands[i] was set up by parse_address.  Encode it into an
7106    ARM-format mode 3 load or store instruction.  Reject forms that
7107    cannot be used with such instructions.  If is_t is true, reject
7108    forms that cannot be used with a T instruction (i.e. not
7109    post-indexed).  */
7110 static void
7111 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7112 {
7113   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7114     {
7115       inst.error = _("instruction does not accept scaled register index");
7116       return;
7117     }
7118
7119   encode_arm_addr_mode_common (i, is_t);
7120
7121   if (inst.operands[i].immisreg)
7122     {
7123       constraint ((inst.operands[i].imm == REG_PC
7124                    || inst.operands[i].reg == REG_PC),
7125                   BAD_PC_ADDRESSING);
7126       inst.instruction |= inst.operands[i].imm;
7127       if (!inst.operands[i].negative)
7128         inst.instruction |= INDEX_UP;
7129     }
7130   else /* immediate offset in inst.reloc */
7131     {
7132       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7133                    && inst.operands[i].writeback),
7134                   BAD_PC_WRITEBACK);
7135       inst.instruction |= HWOFFSET_IMM;
7136       if (inst.reloc.type == BFD_RELOC_UNUSED)
7137         {
7138           /* Prefer + for zero encoded value.  */
7139           if (!inst.operands[i].negative)
7140             inst.instruction |= INDEX_UP;
7141
7142           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7143         }
7144     }
7145 }
7146
7147 /* inst.operands[i] was set up by parse_address.  Encode it into an
7148    ARM-format instruction.  Reject all forms which cannot be encoded
7149    into a coprocessor load/store instruction.  If wb_ok is false,
7150    reject use of writeback; if unind_ok is false, reject use of
7151    unindexed addressing.  If reloc_override is not 0, use it instead
7152    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
7153    (in which case it is preserved).  */
7154
7155 static int
7156 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
7157 {
7158   inst.instruction |= inst.operands[i].reg << 16;
7159
7160   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
7161
7162   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
7163     {
7164       gas_assert (!inst.operands[i].writeback);
7165       if (!unind_ok)
7166         {
7167           inst.error = _("instruction does not support unindexed addressing");
7168           return FAIL;
7169         }
7170       inst.instruction |= inst.operands[i].imm;
7171       inst.instruction |= INDEX_UP;
7172       return SUCCESS;
7173     }
7174
7175   if (inst.operands[i].preind)
7176     inst.instruction |= PRE_INDEX;
7177
7178   if (inst.operands[i].writeback)
7179     {
7180       if (inst.operands[i].reg == REG_PC)
7181         {
7182           inst.error = _("pc may not be used with write-back");
7183           return FAIL;
7184         }
7185       if (!wb_ok)
7186         {
7187           inst.error = _("instruction does not support writeback");
7188           return FAIL;
7189         }
7190       inst.instruction |= WRITE_BACK;
7191     }
7192
7193   if (reloc_override)
7194     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
7195   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
7196             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
7197            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
7198     {
7199       if (thumb_mode)
7200         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
7201       else
7202         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
7203     }
7204
7205   /* Prefer + for zero encoded value.  */
7206   if (!inst.operands[i].negative)
7207     inst.instruction |= INDEX_UP;
7208
7209   return SUCCESS;
7210 }
7211
7212 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7213    Determine whether it can be performed with a move instruction; if
7214    it can, convert inst.instruction to that move instruction and
7215    return TRUE; if it can't, convert inst.instruction to a literal-pool
7216    load and return FALSE.  If this is not a valid thing to do in the
7217    current context, set inst.error and return TRUE.
7218
7219    inst.operands[i] describes the destination register.  */
7220
7221 static bfd_boolean
7222 move_or_literal_pool (int i, bfd_boolean thumb_p, bfd_boolean mode_3)
7223 {
7224   unsigned long tbit;
7225
7226   if (thumb_p)
7227     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7228   else
7229     tbit = LOAD_BIT;
7230
7231   if ((inst.instruction & tbit) == 0)
7232     {
7233       inst.error = _("invalid pseudo operation");
7234       return TRUE;
7235     }
7236   if (inst.reloc.exp.X_op != O_constant && inst.reloc.exp.X_op != O_symbol)
7237     {
7238       inst.error = _("constant expression expected");
7239       return TRUE;
7240     }
7241   if (inst.reloc.exp.X_op == O_constant)
7242     {
7243       if (thumb_p)
7244         {
7245           if (!unified_syntax && (inst.reloc.exp.X_add_number & ~0xFF) == 0)
7246             {
7247               /* This can be done with a mov(1) instruction.  */
7248               inst.instruction  = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
7249               inst.instruction |= inst.reloc.exp.X_add_number;
7250               return TRUE;
7251             }
7252         }
7253       else
7254         {
7255           int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
7256           if (value != FAIL)
7257             {
7258               /* This can be done with a mov instruction.  */
7259               inst.instruction &= LITERAL_MASK;
7260               inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
7261               inst.instruction |= value & 0xfff;
7262               return TRUE;
7263             }
7264
7265           value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
7266           if (value != FAIL)
7267             {
7268               /* This can be done with a mvn instruction.  */
7269               inst.instruction &= LITERAL_MASK;
7270               inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
7271               inst.instruction |= value & 0xfff;
7272               return TRUE;
7273             }
7274         }
7275     }
7276
7277   if (add_to_lit_pool () == FAIL)
7278     {
7279       inst.error = _("literal pool insertion failed");
7280       return TRUE;
7281     }
7282   inst.operands[1].reg = REG_PC;
7283   inst.operands[1].isreg = 1;
7284   inst.operands[1].preind = 1;
7285   inst.reloc.pc_rel = 1;
7286   inst.reloc.type = (thumb_p
7287                      ? BFD_RELOC_ARM_THUMB_OFFSET
7288                      : (mode_3
7289                         ? BFD_RELOC_ARM_HWLITERAL
7290                         : BFD_RELOC_ARM_LITERAL));
7291   return FALSE;
7292 }
7293
7294 /* Functions for instruction encoding, sorted by sub-architecture.
7295    First some generics; their names are taken from the conventional
7296    bit positions for register arguments in ARM format instructions.  */
7297
7298 static void
7299 do_noargs (void)
7300 {
7301 }
7302
7303 static void
7304 do_rd (void)
7305 {
7306   inst.instruction |= inst.operands[0].reg << 12;
7307 }
7308
7309 static void
7310 do_rd_rm (void)
7311 {
7312   inst.instruction |= inst.operands[0].reg << 12;
7313   inst.instruction |= inst.operands[1].reg;
7314 }
7315
7316 static void
7317 do_rd_rn (void)
7318 {
7319   inst.instruction |= inst.operands[0].reg << 12;
7320   inst.instruction |= inst.operands[1].reg << 16;
7321 }
7322
7323 static void
7324 do_rn_rd (void)
7325 {
7326   inst.instruction |= inst.operands[0].reg << 16;
7327   inst.instruction |= inst.operands[1].reg << 12;
7328 }
7329
7330 static void
7331 do_rd_rm_rn (void)
7332 {
7333   unsigned Rn = inst.operands[2].reg;
7334   /* Enforce restrictions on SWP instruction.  */
7335   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
7336     {
7337       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
7338                   _("Rn must not overlap other operands"));
7339
7340       /* SWP{b} is deprecated for ARMv6* and ARMv7.  */
7341       if (warn_on_deprecated
7342           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
7343         as_warn (_("swp{b} use is deprecated for this architecture"));
7344
7345     }
7346   inst.instruction |= inst.operands[0].reg << 12;
7347   inst.instruction |= inst.operands[1].reg;
7348   inst.instruction |= Rn << 16;
7349 }
7350
7351 static void
7352 do_rd_rn_rm (void)
7353 {
7354   inst.instruction |= inst.operands[0].reg << 12;
7355   inst.instruction |= inst.operands[1].reg << 16;
7356   inst.instruction |= inst.operands[2].reg;
7357 }
7358
7359 static void
7360 do_rm_rd_rn (void)
7361 {
7362   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
7363   constraint (((inst.reloc.exp.X_op != O_constant
7364                 && inst.reloc.exp.X_op != O_illegal)
7365                || inst.reloc.exp.X_add_number != 0),
7366               BAD_ADDR_MODE);
7367   inst.instruction |= inst.operands[0].reg;
7368   inst.instruction |= inst.operands[1].reg << 12;
7369   inst.instruction |= inst.operands[2].reg << 16;
7370 }
7371
7372 static void
7373 do_imm0 (void)
7374 {
7375   inst.instruction |= inst.operands[0].imm;
7376 }
7377
7378 static void
7379 do_rd_cpaddr (void)
7380 {
7381   inst.instruction |= inst.operands[0].reg << 12;
7382   encode_arm_cp_address (1, TRUE, TRUE, 0);
7383 }
7384
7385 /* ARM instructions, in alphabetical order by function name (except
7386    that wrapper functions appear immediately after the function they
7387    wrap).  */
7388
7389 /* This is a pseudo-op of the form "adr rd, label" to be converted
7390    into a relative address of the form "add rd, pc, #label-.-8".  */
7391
7392 static void
7393 do_adr (void)
7394 {
7395   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
7396
7397   /* Frag hacking will turn this into a sub instruction if the offset turns
7398      out to be negative.  */
7399   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
7400   inst.reloc.pc_rel = 1;
7401   inst.reloc.exp.X_add_number -= 8;
7402 }
7403
7404 /* This is a pseudo-op of the form "adrl rd, label" to be converted
7405    into a relative address of the form:
7406    add rd, pc, #low(label-.-8)"
7407    add rd, rd, #high(label-.-8)"  */
7408
7409 static void
7410 do_adrl (void)
7411 {
7412   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
7413
7414   /* Frag hacking will turn this into a sub instruction if the offset turns
7415      out to be negative.  */
7416   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
7417   inst.reloc.pc_rel            = 1;
7418   inst.size                    = INSN_SIZE * 2;
7419   inst.reloc.exp.X_add_number -= 8;
7420 }
7421
7422 static void
7423 do_arit (void)
7424 {
7425   if (!inst.operands[1].present)
7426     inst.operands[1].reg = inst.operands[0].reg;
7427   inst.instruction |= inst.operands[0].reg << 12;
7428   inst.instruction |= inst.operands[1].reg << 16;
7429   encode_arm_shifter_operand (2);
7430 }
7431
7432 static void
7433 do_barrier (void)
7434 {
7435   if (inst.operands[0].present)
7436     {
7437       constraint ((inst.instruction & 0xf0) != 0x40
7438                   && inst.operands[0].imm > 0xf
7439                   && inst.operands[0].imm < 0x0,
7440                   _("bad barrier type"));
7441       inst.instruction |= inst.operands[0].imm;
7442     }
7443   else
7444     inst.instruction |= 0xf;
7445 }
7446
7447 static void
7448 do_bfc (void)
7449 {
7450   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
7451   constraint (msb > 32, _("bit-field extends past end of register"));
7452   /* The instruction encoding stores the LSB and MSB,
7453      not the LSB and width.  */
7454   inst.instruction |= inst.operands[0].reg << 12;
7455   inst.instruction |= inst.operands[1].imm << 7;
7456   inst.instruction |= (msb - 1) << 16;
7457 }
7458
7459 static void
7460 do_bfi (void)
7461 {
7462   unsigned int msb;
7463
7464   /* #0 in second position is alternative syntax for bfc, which is
7465      the same instruction but with REG_PC in the Rm field.  */
7466   if (!inst.operands[1].isreg)
7467     inst.operands[1].reg = REG_PC;
7468
7469   msb = inst.operands[2].imm + inst.operands[3].imm;
7470   constraint (msb > 32, _("bit-field extends past end of register"));
7471   /* The instruction encoding stores the LSB and MSB,
7472      not the LSB and width.  */
7473   inst.instruction |= inst.operands[0].reg << 12;
7474   inst.instruction |= inst.operands[1].reg;
7475   inst.instruction |= inst.operands[2].imm << 7;
7476   inst.instruction |= (msb - 1) << 16;
7477 }
7478
7479 static void
7480 do_bfx (void)
7481 {
7482   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
7483               _("bit-field extends past end of register"));
7484   inst.instruction |= inst.operands[0].reg << 12;
7485   inst.instruction |= inst.operands[1].reg;
7486   inst.instruction |= inst.operands[2].imm << 7;
7487   inst.instruction |= (inst.operands[3].imm - 1) << 16;
7488 }
7489
7490 /* ARM V5 breakpoint instruction (argument parse)
7491      BKPT <16 bit unsigned immediate>
7492      Instruction is not conditional.
7493         The bit pattern given in insns[] has the COND_ALWAYS condition,
7494         and it is an error if the caller tried to override that.  */
7495
7496 static void
7497 do_bkpt (void)
7498 {
7499   /* Top 12 of 16 bits to bits 19:8.  */
7500   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
7501
7502   /* Bottom 4 of 16 bits to bits 3:0.  */
7503   inst.instruction |= inst.operands[0].imm & 0xf;
7504 }
7505
7506 static void
7507 encode_branch (int default_reloc)
7508 {
7509   if (inst.operands[0].hasreloc)
7510     {
7511       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
7512                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
7513                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
7514       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
7515         ? BFD_RELOC_ARM_PLT32
7516         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
7517     }
7518   else
7519     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
7520   inst.reloc.pc_rel = 1;
7521 }
7522
7523 static void
7524 do_branch (void)
7525 {
7526 #ifdef OBJ_ELF
7527   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
7528     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
7529   else
7530 #endif
7531     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
7532 }
7533
7534 static void
7535 do_bl (void)
7536 {
7537 #ifdef OBJ_ELF
7538   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
7539     {
7540       if (inst.cond == COND_ALWAYS)
7541         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
7542       else
7543         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
7544     }
7545   else
7546 #endif
7547     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
7548 }
7549
7550 /* ARM V5 branch-link-exchange instruction (argument parse)
7551      BLX <target_addr>          ie BLX(1)
7552      BLX{<condition>} <Rm>      ie BLX(2)
7553    Unfortunately, there are two different opcodes for this mnemonic.
7554    So, the insns[].value is not used, and the code here zaps values
7555         into inst.instruction.
7556    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
7557
7558 static void
7559 do_blx (void)
7560 {
7561   if (inst.operands[0].isreg)
7562     {
7563       /* Arg is a register; the opcode provided by insns[] is correct.
7564          It is not illegal to do "blx pc", just useless.  */
7565       if (inst.operands[0].reg == REG_PC)
7566         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
7567
7568       inst.instruction |= inst.operands[0].reg;
7569     }
7570   else
7571     {
7572       /* Arg is an address; this instruction cannot be executed
7573          conditionally, and the opcode must be adjusted.
7574          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
7575          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
7576       constraint (inst.cond != COND_ALWAYS, BAD_COND);
7577       inst.instruction = 0xfa000000;
7578       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
7579     }
7580 }
7581
7582 static void
7583 do_bx (void)
7584 {
7585   bfd_boolean want_reloc;
7586
7587   if (inst.operands[0].reg == REG_PC)
7588     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
7589
7590   inst.instruction |= inst.operands[0].reg;
7591   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
7592      it is for ARMv4t or earlier.  */
7593   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
7594   if (object_arch && !ARM_CPU_HAS_FEATURE (*object_arch, arm_ext_v5))
7595       want_reloc = TRUE;
7596
7597 #ifdef OBJ_ELF
7598   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
7599 #endif
7600     want_reloc = FALSE;
7601
7602   if (want_reloc)
7603     inst.reloc.type = BFD_RELOC_ARM_V4BX;
7604 }
7605
7606
7607 /* ARM v5TEJ.  Jump to Jazelle code.  */
7608
7609 static void
7610 do_bxj (void)
7611 {
7612   if (inst.operands[0].reg == REG_PC)
7613     as_tsktsk (_("use of r15 in bxj is not really useful"));
7614
7615   inst.instruction |= inst.operands[0].reg;
7616 }
7617
7618 /* Co-processor data operation:
7619       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
7620       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
7621 static void
7622 do_cdp (void)
7623 {
7624   inst.instruction |= inst.operands[0].reg << 8;
7625   inst.instruction |= inst.operands[1].imm << 20;
7626   inst.instruction |= inst.operands[2].reg << 12;
7627   inst.instruction |= inst.operands[3].reg << 16;
7628   inst.instruction |= inst.operands[4].reg;
7629   inst.instruction |= inst.operands[5].imm << 5;
7630 }
7631
7632 static void
7633 do_cmp (void)
7634 {
7635   inst.instruction |= inst.operands[0].reg << 16;
7636   encode_arm_shifter_operand (1);
7637 }
7638
7639 /* Transfer between coprocessor and ARM registers.
7640    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
7641    MRC2
7642    MCR{cond}
7643    MCR2
7644
7645    No special properties.  */
7646
7647 static void
7648 do_co_reg (void)
7649 {
7650   unsigned Rd;
7651
7652   Rd = inst.operands[2].reg;
7653   if (thumb_mode)
7654     {
7655       if (inst.instruction == 0xee000010
7656           || inst.instruction == 0xfe000010)
7657         /* MCR, MCR2  */
7658         reject_bad_reg (Rd);
7659       else
7660         /* MRC, MRC2  */
7661         constraint (Rd == REG_SP, BAD_SP);
7662     }
7663   else
7664     {
7665       /* MCR */
7666       if (inst.instruction == 0xe000010)
7667         constraint (Rd == REG_PC, BAD_PC);
7668     }
7669
7670
7671   inst.instruction |= inst.operands[0].reg << 8;
7672   inst.instruction |= inst.operands[1].imm << 21;
7673   inst.instruction |= Rd << 12;
7674   inst.instruction |= inst.operands[3].reg << 16;
7675   inst.instruction |= inst.operands[4].reg;
7676   inst.instruction |= inst.operands[5].imm << 5;
7677 }
7678
7679 /* Transfer between coprocessor register and pair of ARM registers.
7680    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
7681    MCRR2
7682    MRRC{cond}
7683    MRRC2
7684
7685    Two XScale instructions are special cases of these:
7686
7687      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
7688      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
7689
7690    Result unpredictable if Rd or Rn is R15.  */
7691
7692 static void
7693 do_co_reg2c (void)
7694 {
7695   unsigned Rd, Rn;
7696
7697   Rd = inst.operands[2].reg;
7698   Rn = inst.operands[3].reg;
7699
7700   if (thumb_mode)
7701     {
7702       reject_bad_reg (Rd);
7703       reject_bad_reg (Rn);
7704     }
7705   else
7706     {
7707       constraint (Rd == REG_PC, BAD_PC);
7708       constraint (Rn == REG_PC, BAD_PC);
7709     }
7710
7711   inst.instruction |= inst.operands[0].reg << 8;
7712   inst.instruction |= inst.operands[1].imm << 4;
7713   inst.instruction |= Rd << 12;
7714   inst.instruction |= Rn << 16;
7715   inst.instruction |= inst.operands[4].reg;
7716 }
7717
7718 static void
7719 do_cpsi (void)
7720 {
7721   inst.instruction |= inst.operands[0].imm << 6;
7722   if (inst.operands[1].present)
7723     {
7724       inst.instruction |= CPSI_MMOD;
7725       inst.instruction |= inst.operands[1].imm;
7726     }
7727 }
7728
7729 static void
7730 do_dbg (void)
7731 {
7732   inst.instruction |= inst.operands[0].imm;
7733 }
7734
7735 static void
7736 do_div (void)
7737 {
7738   unsigned Rd, Rn, Rm;
7739
7740   Rd = inst.operands[0].reg;
7741   Rn = (inst.operands[1].present
7742         ? inst.operands[1].reg : Rd);
7743   Rm = inst.operands[2].reg;
7744
7745   constraint ((Rd == REG_PC), BAD_PC);
7746   constraint ((Rn == REG_PC), BAD_PC);
7747   constraint ((Rm == REG_PC), BAD_PC);
7748
7749   inst.instruction |= Rd << 16;
7750   inst.instruction |= Rn << 0;
7751   inst.instruction |= Rm << 8;
7752 }
7753
7754 static void
7755 do_it (void)
7756 {
7757   /* There is no IT instruction in ARM mode.  We
7758      process it to do the validation as if in
7759      thumb mode, just in case the code gets
7760      assembled for thumb using the unified syntax.  */
7761
7762   inst.size = 0;
7763   if (unified_syntax)
7764     {
7765       set_it_insn_type (IT_INSN);
7766       now_it.mask = (inst.instruction & 0xf) | 0x10;
7767       now_it.cc = inst.operands[0].imm;
7768     }
7769 }
7770
7771 static void
7772 do_ldmstm (void)
7773 {
7774   int base_reg = inst.operands[0].reg;
7775   int range = inst.operands[1].imm;
7776
7777   inst.instruction |= base_reg << 16;
7778   inst.instruction |= range;
7779
7780   if (inst.operands[1].writeback)
7781     inst.instruction |= LDM_TYPE_2_OR_3;
7782
7783   if (inst.operands[0].writeback)
7784     {
7785       inst.instruction |= WRITE_BACK;
7786       /* Check for unpredictable uses of writeback.  */
7787       if (inst.instruction & LOAD_BIT)
7788         {
7789           /* Not allowed in LDM type 2.  */
7790           if ((inst.instruction & LDM_TYPE_2_OR_3)
7791               && ((range & (1 << REG_PC)) == 0))
7792             as_warn (_("writeback of base register is UNPREDICTABLE"));
7793           /* Only allowed if base reg not in list for other types.  */
7794           else if (range & (1 << base_reg))
7795             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
7796         }
7797       else /* STM.  */
7798         {
7799           /* Not allowed for type 2.  */
7800           if (inst.instruction & LDM_TYPE_2_OR_3)
7801             as_warn (_("writeback of base register is UNPREDICTABLE"));
7802           /* Only allowed if base reg not in list, or first in list.  */
7803           else if ((range & (1 << base_reg))
7804                    && (range & ((1 << base_reg) - 1)))
7805             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
7806         }
7807     }
7808 }
7809
7810 /* ARMv5TE load-consecutive (argument parse)
7811    Mode is like LDRH.
7812
7813      LDRccD R, mode
7814      STRccD R, mode.  */
7815
7816 static void
7817 do_ldrd (void)
7818 {
7819   constraint (inst.operands[0].reg % 2 != 0,
7820               _("first transfer register must be even"));
7821   constraint (inst.operands[1].present
7822               && inst.operands[1].reg != inst.operands[0].reg + 1,
7823               _("can only transfer two consecutive registers"));
7824   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
7825   constraint (!inst.operands[2].isreg, _("'[' expected"));
7826
7827   if (!inst.operands[1].present)
7828     inst.operands[1].reg = inst.operands[0].reg + 1;
7829
7830   /* encode_arm_addr_mode_3 will diagnose overlap between the base
7831      register and the first register written; we have to diagnose
7832      overlap between the base and the second register written here.  */
7833
7834   if (inst.operands[2].reg == inst.operands[1].reg
7835       && (inst.operands[2].writeback || inst.operands[2].postind))
7836     as_warn (_("base register written back, and overlaps "
7837                "second transfer register"));
7838
7839   if (!(inst.instruction & V4_STR_BIT))
7840     {
7841       /* For an index-register load, the index register must not overlap the
7842         destination (even if not write-back).  */
7843       if (inst.operands[2].immisreg
7844               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
7845               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
7846         as_warn (_("index register overlaps transfer register"));
7847     }
7848   inst.instruction |= inst.operands[0].reg << 12;
7849   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
7850 }
7851
7852 static void
7853 do_ldrex (void)
7854 {
7855   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
7856               || inst.operands[1].postind || inst.operands[1].writeback
7857               || inst.operands[1].immisreg || inst.operands[1].shifted
7858               || inst.operands[1].negative
7859               /* This can arise if the programmer has written
7860                    strex rN, rM, foo
7861                  or if they have mistakenly used a register name as the last
7862                  operand,  eg:
7863                    strex rN, rM, rX
7864                  It is very difficult to distinguish between these two cases
7865                  because "rX" might actually be a label. ie the register
7866                  name has been occluded by a symbol of the same name. So we
7867                  just generate a general 'bad addressing mode' type error
7868                  message and leave it up to the programmer to discover the
7869                  true cause and fix their mistake.  */
7870               || (inst.operands[1].reg == REG_PC),
7871               BAD_ADDR_MODE);
7872
7873   constraint (inst.reloc.exp.X_op != O_constant
7874               || inst.reloc.exp.X_add_number != 0,
7875               _("offset must be zero in ARM encoding"));
7876
7877   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
7878
7879   inst.instruction |= inst.operands[0].reg << 12;
7880   inst.instruction |= inst.operands[1].reg << 16;
7881   inst.reloc.type = BFD_RELOC_UNUSED;
7882 }
7883
7884 static void
7885 do_ldrexd (void)
7886 {
7887   constraint (inst.operands[0].reg % 2 != 0,
7888               _("even register required"));
7889   constraint (inst.operands[1].present
7890               && inst.operands[1].reg != inst.operands[0].reg + 1,
7891               _("can only load two consecutive registers"));
7892   /* If op 1 were present and equal to PC, this function wouldn't
7893      have been called in the first place.  */
7894   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
7895
7896   inst.instruction |= inst.operands[0].reg << 12;
7897   inst.instruction |= inst.operands[2].reg << 16;
7898 }
7899
7900 static void
7901 do_ldst (void)
7902 {
7903   inst.instruction |= inst.operands[0].reg << 12;
7904   if (!inst.operands[1].isreg)
7905     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/FALSE))
7906       return;
7907   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
7908 }
7909
7910 static void
7911 do_ldstt (void)
7912 {
7913   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
7914      reject [Rn,...].  */
7915   if (inst.operands[1].preind)
7916     {
7917       constraint (inst.reloc.exp.X_op != O_constant
7918                   || inst.reloc.exp.X_add_number != 0,
7919                   _("this instruction requires a post-indexed address"));
7920
7921       inst.operands[1].preind = 0;
7922       inst.operands[1].postind = 1;
7923       inst.operands[1].writeback = 1;
7924     }
7925   inst.instruction |= inst.operands[0].reg << 12;
7926   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
7927 }
7928
7929 /* Halfword and signed-byte load/store operations.  */
7930
7931 static void
7932 do_ldstv4 (void)
7933 {
7934   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
7935   inst.instruction |= inst.operands[0].reg << 12;
7936   if (!inst.operands[1].isreg)
7937     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/TRUE))
7938       return;
7939   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
7940 }
7941
7942 static void
7943 do_ldsttv4 (void)
7944 {
7945   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
7946      reject [Rn,...].  */
7947   if (inst.operands[1].preind)
7948     {
7949       constraint (inst.reloc.exp.X_op != O_constant
7950                   || inst.reloc.exp.X_add_number != 0,
7951                   _("this instruction requires a post-indexed address"));
7952
7953       inst.operands[1].preind = 0;
7954       inst.operands[1].postind = 1;
7955       inst.operands[1].writeback = 1;
7956     }
7957   inst.instruction |= inst.operands[0].reg << 12;
7958   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
7959 }
7960
7961 /* Co-processor register load/store.
7962    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
7963 static void
7964 do_lstc (void)
7965 {
7966   inst.instruction |= inst.operands[0].reg << 8;
7967   inst.instruction |= inst.operands[1].reg << 12;
7968   encode_arm_cp_address (2, TRUE, TRUE, 0);
7969 }
7970
7971 static void
7972 do_mlas (void)
7973 {
7974   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
7975   if (inst.operands[0].reg == inst.operands[1].reg
7976       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
7977       && !(inst.instruction & 0x00400000))
7978     as_tsktsk (_("Rd and Rm should be different in mla"));
7979
7980   inst.instruction |= inst.operands[0].reg << 16;
7981   inst.instruction |= inst.operands[1].reg;
7982   inst.instruction |= inst.operands[2].reg << 8;
7983   inst.instruction |= inst.operands[3].reg << 12;
7984 }
7985
7986 static void
7987 do_mov (void)
7988 {
7989   inst.instruction |= inst.operands[0].reg << 12;
7990   encode_arm_shifter_operand (1);
7991 }
7992
7993 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
7994 static void
7995 do_mov16 (void)
7996 {
7997   bfd_vma imm;
7998   bfd_boolean top;
7999
8000   top = (inst.instruction & 0x00400000) != 0;
8001   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
8002               _(":lower16: not allowed this instruction"));
8003   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
8004               _(":upper16: not allowed instruction"));
8005   inst.instruction |= inst.operands[0].reg << 12;
8006   if (inst.reloc.type == BFD_RELOC_UNUSED)
8007     {
8008       imm = inst.reloc.exp.X_add_number;
8009       /* The value is in two pieces: 0:11, 16:19.  */
8010       inst.instruction |= (imm & 0x00000fff);
8011       inst.instruction |= (imm & 0x0000f000) << 4;
8012     }
8013 }
8014
8015 static void do_vfp_nsyn_opcode (const char *);
8016
8017 static int
8018 do_vfp_nsyn_mrs (void)
8019 {
8020   if (inst.operands[0].isvec)
8021     {
8022       if (inst.operands[1].reg != 1)
8023         first_error (_("operand 1 must be FPSCR"));
8024       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
8025       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
8026       do_vfp_nsyn_opcode ("fmstat");
8027     }
8028   else if (inst.operands[1].isvec)
8029     do_vfp_nsyn_opcode ("fmrx");
8030   else
8031     return FAIL;
8032
8033   return SUCCESS;
8034 }
8035
8036 static int
8037 do_vfp_nsyn_msr (void)
8038 {
8039   if (inst.operands[0].isvec)
8040     do_vfp_nsyn_opcode ("fmxr");
8041   else
8042     return FAIL;
8043
8044   return SUCCESS;
8045 }
8046
8047 static void
8048 do_vmrs (void)
8049 {
8050   unsigned Rt = inst.operands[0].reg;
8051   
8052   if (thumb_mode && inst.operands[0].reg == REG_SP)
8053     {
8054       inst.error = BAD_SP;
8055       return;
8056     }
8057
8058   /* APSR_ sets isvec. All other refs to PC are illegal.  */
8059   if (!inst.operands[0].isvec && inst.operands[0].reg == REG_PC)
8060     {
8061       inst.error = BAD_PC;
8062       return;
8063     }
8064
8065   if (inst.operands[1].reg != 1)
8066     first_error (_("operand 1 must be FPSCR"));
8067
8068   inst.instruction |= (Rt << 12);
8069 }
8070
8071 static void
8072 do_vmsr (void)
8073 {
8074   unsigned Rt = inst.operands[1].reg;
8075   
8076   if (thumb_mode)
8077     reject_bad_reg (Rt);
8078   else if (Rt == REG_PC)
8079     {
8080       inst.error = BAD_PC;
8081       return;
8082     }
8083
8084   if (inst.operands[0].reg != 1)
8085     first_error (_("operand 0 must be FPSCR"));
8086
8087   inst.instruction |= (Rt << 12);
8088 }
8089
8090 static void
8091 do_mrs (void)
8092 {
8093   unsigned br;
8094
8095   if (do_vfp_nsyn_mrs () == SUCCESS)
8096     return;
8097
8098   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8099   inst.instruction |= inst.operands[0].reg << 12;
8100
8101   if (inst.operands[1].isreg)
8102     {
8103       br = inst.operands[1].reg;
8104       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf000))
8105         as_bad (_("bad register for mrs"));
8106     }
8107   else
8108     {
8109       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
8110       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
8111                   != (PSR_c|PSR_f),
8112                   _("'APSR', 'CPSR' or 'SPSR' expected"));
8113       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
8114     }
8115
8116   inst.instruction |= br;
8117 }
8118
8119 /* Two possible forms:
8120       "{C|S}PSR_<field>, Rm",
8121       "{C|S}PSR_f, #expression".  */
8122
8123 static void
8124 do_msr (void)
8125 {
8126   if (do_vfp_nsyn_msr () == SUCCESS)
8127     return;
8128
8129   inst.instruction |= inst.operands[0].imm;
8130   if (inst.operands[1].isreg)
8131     inst.instruction |= inst.operands[1].reg;
8132   else
8133     {
8134       inst.instruction |= INST_IMMEDIATE;
8135       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8136       inst.reloc.pc_rel = 0;
8137     }
8138 }
8139
8140 static void
8141 do_mul (void)
8142 {
8143   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
8144
8145   if (!inst.operands[2].present)
8146     inst.operands[2].reg = inst.operands[0].reg;
8147   inst.instruction |= inst.operands[0].reg << 16;
8148   inst.instruction |= inst.operands[1].reg;
8149   inst.instruction |= inst.operands[2].reg << 8;
8150
8151   if (inst.operands[0].reg == inst.operands[1].reg
8152       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8153     as_tsktsk (_("Rd and Rm should be different in mul"));
8154 }
8155
8156 /* Long Multiply Parser
8157    UMULL RdLo, RdHi, Rm, Rs
8158    SMULL RdLo, RdHi, Rm, Rs
8159    UMLAL RdLo, RdHi, Rm, Rs
8160    SMLAL RdLo, RdHi, Rm, Rs.  */
8161
8162 static void
8163 do_mull (void)
8164 {
8165   inst.instruction |= inst.operands[0].reg << 12;
8166   inst.instruction |= inst.operands[1].reg << 16;
8167   inst.instruction |= inst.operands[2].reg;
8168   inst.instruction |= inst.operands[3].reg << 8;
8169
8170   /* rdhi and rdlo must be different.  */
8171   if (inst.operands[0].reg == inst.operands[1].reg)
8172     as_tsktsk (_("rdhi and rdlo must be different"));
8173
8174   /* rdhi, rdlo and rm must all be different before armv6.  */
8175   if ((inst.operands[0].reg == inst.operands[2].reg
8176       || inst.operands[1].reg == inst.operands[2].reg)
8177       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
8178     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
8179 }
8180
8181 static void
8182 do_nop (void)
8183 {
8184   if (inst.operands[0].present
8185       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
8186     {
8187       /* Architectural NOP hints are CPSR sets with no bits selected.  */
8188       inst.instruction &= 0xf0000000;
8189       inst.instruction |= 0x0320f000;
8190       if (inst.operands[0].present)
8191         inst.instruction |= inst.operands[0].imm;
8192     }
8193 }
8194
8195 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
8196    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
8197    Condition defaults to COND_ALWAYS.
8198    Error if Rd, Rn or Rm are R15.  */
8199
8200 static void
8201 do_pkhbt (void)
8202 {
8203   inst.instruction |= inst.operands[0].reg << 12;
8204   inst.instruction |= inst.operands[1].reg << 16;
8205   inst.instruction |= inst.operands[2].reg;
8206   if (inst.operands[3].present)
8207     encode_arm_shift (3);
8208 }
8209
8210 /* ARM V6 PKHTB (Argument Parse).  */
8211
8212 static void
8213 do_pkhtb (void)
8214 {
8215   if (!inst.operands[3].present)
8216     {
8217       /* If the shift specifier is omitted, turn the instruction
8218          into pkhbt rd, rm, rn. */
8219       inst.instruction &= 0xfff00010;
8220       inst.instruction |= inst.operands[0].reg << 12;
8221       inst.instruction |= inst.operands[1].reg;
8222       inst.instruction |= inst.operands[2].reg << 16;
8223     }
8224   else
8225     {
8226       inst.instruction |= inst.operands[0].reg << 12;
8227       inst.instruction |= inst.operands[1].reg << 16;
8228       inst.instruction |= inst.operands[2].reg;
8229       encode_arm_shift (3);
8230     }
8231 }
8232
8233 /* ARMv5TE: Preload-Cache
8234    MP Extensions: Preload for write
8235
8236     PLD(W) <addr_mode>
8237
8238   Syntactically, like LDR with B=1, W=0, L=1.  */
8239
8240 static void
8241 do_pld (void)
8242 {
8243   constraint (!inst.operands[0].isreg,
8244               _("'[' expected after PLD mnemonic"));
8245   constraint (inst.operands[0].postind,
8246               _("post-indexed expression used in preload instruction"));
8247   constraint (inst.operands[0].writeback,
8248               _("writeback used in preload instruction"));
8249   constraint (!inst.operands[0].preind,
8250               _("unindexed addressing used in preload instruction"));
8251   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
8252 }
8253
8254 /* ARMv7: PLI <addr_mode>  */
8255 static void
8256 do_pli (void)
8257 {
8258   constraint (!inst.operands[0].isreg,
8259               _("'[' expected after PLI mnemonic"));
8260   constraint (inst.operands[0].postind,
8261               _("post-indexed expression used in preload instruction"));
8262   constraint (inst.operands[0].writeback,
8263               _("writeback used in preload instruction"));
8264   constraint (!inst.operands[0].preind,
8265               _("unindexed addressing used in preload instruction"));
8266   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
8267   inst.instruction &= ~PRE_INDEX;
8268 }
8269
8270 static void
8271 do_push_pop (void)
8272 {
8273   inst.operands[1] = inst.operands[0];
8274   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
8275   inst.operands[0].isreg = 1;
8276   inst.operands[0].writeback = 1;
8277   inst.operands[0].reg = REG_SP;
8278   do_ldmstm ();
8279 }
8280
8281 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
8282    word at the specified address and the following word
8283    respectively.
8284    Unconditionally executed.
8285    Error if Rn is R15.  */
8286
8287 static void
8288 do_rfe (void)
8289 {
8290   inst.instruction |= inst.operands[0].reg << 16;
8291   if (inst.operands[0].writeback)
8292     inst.instruction |= WRITE_BACK;
8293 }
8294
8295 /* ARM V6 ssat (argument parse).  */
8296
8297 static void
8298 do_ssat (void)
8299 {
8300   inst.instruction |= inst.operands[0].reg << 12;
8301   inst.instruction |= (inst.operands[1].imm - 1) << 16;
8302   inst.instruction |= inst.operands[2].reg;
8303
8304   if (inst.operands[3].present)
8305     encode_arm_shift (3);
8306 }
8307
8308 /* ARM V6 usat (argument parse).  */
8309
8310 static void
8311 do_usat (void)
8312 {
8313   inst.instruction |= inst.operands[0].reg << 12;
8314   inst.instruction |= inst.operands[1].imm << 16;
8315   inst.instruction |= inst.operands[2].reg;
8316
8317   if (inst.operands[3].present)
8318     encode_arm_shift (3);
8319 }
8320
8321 /* ARM V6 ssat16 (argument parse).  */
8322
8323 static void
8324 do_ssat16 (void)
8325 {
8326   inst.instruction |= inst.operands[0].reg << 12;
8327   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
8328   inst.instruction |= inst.operands[2].reg;
8329 }
8330
8331 static void
8332 do_usat16 (void)
8333 {
8334   inst.instruction |= inst.operands[0].reg << 12;
8335   inst.instruction |= inst.operands[1].imm << 16;
8336   inst.instruction |= inst.operands[2].reg;
8337 }
8338
8339 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
8340    preserving the other bits.
8341
8342    setend <endian_specifier>, where <endian_specifier> is either
8343    BE or LE.  */
8344
8345 static void
8346 do_setend (void)
8347 {
8348   if (inst.operands[0].imm)
8349     inst.instruction |= 0x200;
8350 }
8351
8352 static void
8353 do_shift (void)
8354 {
8355   unsigned int Rm = (inst.operands[1].present
8356                      ? inst.operands[1].reg
8357                      : inst.operands[0].reg);
8358
8359   inst.instruction |= inst.operands[0].reg << 12;
8360   inst.instruction |= Rm;
8361   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
8362     {
8363       inst.instruction |= inst.operands[2].reg << 8;
8364       inst.instruction |= SHIFT_BY_REG;
8365       /* PR 12854: Error on extraneous shifts.  */
8366       constraint (inst.operands[2].shifted,
8367                   _("extraneous shift as part of operand to shift insn"));
8368     }
8369   else
8370     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
8371 }
8372
8373 static void
8374 do_smc (void)
8375 {
8376   inst.reloc.type = BFD_RELOC_ARM_SMC;
8377   inst.reloc.pc_rel = 0;
8378 }
8379
8380 static void
8381 do_hvc (void)
8382 {
8383   inst.reloc.type = BFD_RELOC_ARM_HVC;
8384   inst.reloc.pc_rel = 0;
8385 }
8386
8387 static void
8388 do_swi (void)
8389 {
8390   inst.reloc.type = BFD_RELOC_ARM_SWI;
8391   inst.reloc.pc_rel = 0;
8392 }
8393
8394 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
8395    SMLAxy{cond} Rd,Rm,Rs,Rn
8396    SMLAWy{cond} Rd,Rm,Rs,Rn
8397    Error if any register is R15.  */
8398
8399 static void
8400 do_smla (void)
8401 {
8402   inst.instruction |= inst.operands[0].reg << 16;
8403   inst.instruction |= inst.operands[1].reg;
8404   inst.instruction |= inst.operands[2].reg << 8;
8405   inst.instruction |= inst.operands[3].reg << 12;
8406 }
8407
8408 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
8409    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
8410    Error if any register is R15.
8411    Warning if Rdlo == Rdhi.  */
8412
8413 static void
8414 do_smlal (void)
8415 {
8416   inst.instruction |= inst.operands[0].reg << 12;
8417   inst.instruction |= inst.operands[1].reg << 16;
8418   inst.instruction |= inst.operands[2].reg;
8419   inst.instruction |= inst.operands[3].reg << 8;
8420
8421   if (inst.operands[0].reg == inst.operands[1].reg)
8422     as_tsktsk (_("rdhi and rdlo must be different"));
8423 }
8424
8425 /* ARM V5E (El Segundo) signed-multiply (argument parse)
8426    SMULxy{cond} Rd,Rm,Rs
8427    Error if any register is R15.  */
8428
8429 static void
8430 do_smul (void)
8431 {
8432   inst.instruction |= inst.operands[0].reg << 16;
8433   inst.instruction |= inst.operands[1].reg;
8434   inst.instruction |= inst.operands[2].reg << 8;
8435 }
8436
8437 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
8438    the same for both ARM and Thumb-2.  */
8439
8440 static void
8441 do_srs (void)
8442 {
8443   int reg;
8444
8445   if (inst.operands[0].present)
8446     {
8447       reg = inst.operands[0].reg;
8448       constraint (reg != REG_SP, _("SRS base register must be r13"));
8449     }
8450   else
8451     reg = REG_SP;
8452
8453   inst.instruction |= reg << 16;
8454   inst.instruction |= inst.operands[1].imm;
8455   if (inst.operands[0].writeback || inst.operands[1].writeback)
8456     inst.instruction |= WRITE_BACK;
8457 }
8458
8459 /* ARM V6 strex (argument parse).  */
8460
8461 static void
8462 do_strex (void)
8463 {
8464   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
8465               || inst.operands[2].postind || inst.operands[2].writeback
8466               || inst.operands[2].immisreg || inst.operands[2].shifted
8467               || inst.operands[2].negative
8468               /* See comment in do_ldrex().  */
8469               || (inst.operands[2].reg == REG_PC),
8470               BAD_ADDR_MODE);
8471
8472   constraint (inst.operands[0].reg == inst.operands[1].reg
8473               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
8474
8475   constraint (inst.reloc.exp.X_op != O_constant
8476               || inst.reloc.exp.X_add_number != 0,
8477               _("offset must be zero in ARM encoding"));
8478
8479   inst.instruction |= inst.operands[0].reg << 12;
8480   inst.instruction |= inst.operands[1].reg;
8481   inst.instruction |= inst.operands[2].reg << 16;
8482   inst.reloc.type = BFD_RELOC_UNUSED;
8483 }
8484
8485 static void
8486 do_strexd (void)
8487 {
8488   constraint (inst.operands[1].reg % 2 != 0,
8489               _("even register required"));
8490   constraint (inst.operands[2].present
8491               && inst.operands[2].reg != inst.operands[1].reg + 1,
8492               _("can only store two consecutive registers"));
8493   /* If op 2 were present and equal to PC, this function wouldn't
8494      have been called in the first place.  */
8495   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
8496
8497   constraint (inst.operands[0].reg == inst.operands[1].reg
8498               || inst.operands[0].reg == inst.operands[1].reg + 1
8499               || inst.operands[0].reg == inst.operands[3].reg,
8500               BAD_OVERLAP);
8501
8502   inst.instruction |= inst.operands[0].reg << 12;
8503   inst.instruction |= inst.operands[1].reg;
8504   inst.instruction |= inst.operands[3].reg << 16;
8505 }
8506
8507 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
8508    extends it to 32-bits, and adds the result to a value in another
8509    register.  You can specify a rotation by 0, 8, 16, or 24 bits
8510    before extracting the 16-bit value.
8511    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
8512    Condition defaults to COND_ALWAYS.
8513    Error if any register uses R15.  */
8514
8515 static void
8516 do_sxtah (void)
8517 {
8518   inst.instruction |= inst.operands[0].reg << 12;
8519   inst.instruction |= inst.operands[1].reg << 16;
8520   inst.instruction |= inst.operands[2].reg;
8521   inst.instruction |= inst.operands[3].imm << 10;
8522 }
8523
8524 /* ARM V6 SXTH.
8525
8526    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
8527    Condition defaults to COND_ALWAYS.
8528    Error if any register uses R15.  */
8529
8530 static void
8531 do_sxth (void)
8532 {
8533   inst.instruction |= inst.operands[0].reg << 12;
8534   inst.instruction |= inst.operands[1].reg;
8535   inst.instruction |= inst.operands[2].imm << 10;
8536 }
8537 \f
8538 /* VFP instructions.  In a logical order: SP variant first, monad
8539    before dyad, arithmetic then move then load/store.  */
8540
8541 static void
8542 do_vfp_sp_monadic (void)
8543 {
8544   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8545   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
8546 }
8547
8548 static void
8549 do_vfp_sp_dyadic (void)
8550 {
8551   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8552   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
8553   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
8554 }
8555
8556 static void
8557 do_vfp_sp_compare_z (void)
8558 {
8559   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8560 }
8561
8562 static void
8563 do_vfp_dp_sp_cvt (void)
8564 {
8565   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8566   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
8567 }
8568
8569 static void
8570 do_vfp_sp_dp_cvt (void)
8571 {
8572   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8573   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
8574 }
8575
8576 static void
8577 do_vfp_reg_from_sp (void)
8578 {
8579   inst.instruction |= inst.operands[0].reg << 12;
8580   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
8581 }
8582
8583 static void
8584 do_vfp_reg2_from_sp2 (void)
8585 {
8586   constraint (inst.operands[2].imm != 2,
8587               _("only two consecutive VFP SP registers allowed here"));
8588   inst.instruction |= inst.operands[0].reg << 12;
8589   inst.instruction |= inst.operands[1].reg << 16;
8590   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
8591 }
8592
8593 static void
8594 do_vfp_sp_from_reg (void)
8595 {
8596   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
8597   inst.instruction |= inst.operands[1].reg << 12;
8598 }
8599
8600 static void
8601 do_vfp_sp2_from_reg2 (void)
8602 {
8603   constraint (inst.operands[0].imm != 2,
8604               _("only two consecutive VFP SP registers allowed here"));
8605   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
8606   inst.instruction |= inst.operands[1].reg << 12;
8607   inst.instruction |= inst.operands[2].reg << 16;
8608 }
8609
8610 static void
8611 do_vfp_sp_ldst (void)
8612 {
8613   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8614   encode_arm_cp_address (1, FALSE, TRUE, 0);
8615 }
8616
8617 static void
8618 do_vfp_dp_ldst (void)
8619 {
8620   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8621   encode_arm_cp_address (1, FALSE, TRUE, 0);
8622 }
8623
8624
8625 static void
8626 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
8627 {
8628   if (inst.operands[0].writeback)
8629     inst.instruction |= WRITE_BACK;
8630   else
8631     constraint (ldstm_type != VFP_LDSTMIA,
8632                 _("this addressing mode requires base-register writeback"));
8633   inst.instruction |= inst.operands[0].reg << 16;
8634   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
8635   inst.instruction |= inst.operands[1].imm;
8636 }
8637
8638 static void
8639 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
8640 {
8641   int count;
8642
8643   if (inst.operands[0].writeback)
8644     inst.instruction |= WRITE_BACK;
8645   else
8646     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
8647                 _("this addressing mode requires base-register writeback"));
8648
8649   inst.instruction |= inst.operands[0].reg << 16;
8650   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8651
8652   count = inst.operands[1].imm << 1;
8653   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
8654     count += 1;
8655
8656   inst.instruction |= count;
8657 }
8658
8659 static void
8660 do_vfp_sp_ldstmia (void)
8661 {
8662   vfp_sp_ldstm (VFP_LDSTMIA);
8663 }
8664
8665 static void
8666 do_vfp_sp_ldstmdb (void)
8667 {
8668   vfp_sp_ldstm (VFP_LDSTMDB);
8669 }
8670
8671 static void
8672 do_vfp_dp_ldstmia (void)
8673 {
8674   vfp_dp_ldstm (VFP_LDSTMIA);
8675 }
8676
8677 static void
8678 do_vfp_dp_ldstmdb (void)
8679 {
8680   vfp_dp_ldstm (VFP_LDSTMDB);
8681 }
8682
8683 static void
8684 do_vfp_xp_ldstmia (void)
8685 {
8686   vfp_dp_ldstm (VFP_LDSTMIAX);
8687 }
8688
8689 static void
8690 do_vfp_xp_ldstmdb (void)
8691 {
8692   vfp_dp_ldstm (VFP_LDSTMDBX);
8693 }
8694
8695 static void
8696 do_vfp_dp_rd_rm (void)
8697 {
8698   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8699   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
8700 }
8701
8702 static void
8703 do_vfp_dp_rn_rd (void)
8704 {
8705   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
8706   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8707 }
8708
8709 static void
8710 do_vfp_dp_rd_rn (void)
8711 {
8712   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8713   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
8714 }
8715
8716 static void
8717 do_vfp_dp_rd_rn_rm (void)
8718 {
8719   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8720   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
8721   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
8722 }
8723
8724 static void
8725 do_vfp_dp_rd (void)
8726 {
8727   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8728 }
8729
8730 static void
8731 do_vfp_dp_rm_rd_rn (void)
8732 {
8733   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
8734   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
8735   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
8736 }
8737
8738 /* VFPv3 instructions.  */
8739 static void
8740 do_vfp_sp_const (void)
8741 {
8742   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8743   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
8744   inst.instruction |= (inst.operands[1].imm & 0x0f);
8745 }
8746
8747 static void
8748 do_vfp_dp_const (void)
8749 {
8750   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8751   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
8752   inst.instruction |= (inst.operands[1].imm & 0x0f);
8753 }
8754
8755 static void
8756 vfp_conv (int srcsize)
8757 {
8758   int immbits = srcsize - inst.operands[1].imm;
8759
8760   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize)) 
8761     {  
8762       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
8763          i.e. immbits must be in range 0 - 16.  */
8764       inst.error = _("immediate value out of range, expected range [0, 16]");
8765       return;
8766     }
8767   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize)) 
8768     {
8769       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
8770          i.e. immbits must be in range 0 - 31.  */
8771       inst.error = _("immediate value out of range, expected range [1, 32]");
8772       return;
8773     }
8774
8775   inst.instruction |= (immbits & 1) << 5;
8776   inst.instruction |= (immbits >> 1);
8777 }
8778
8779 static void
8780 do_vfp_sp_conv_16 (void)
8781 {
8782   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8783   vfp_conv (16);
8784 }
8785
8786 static void
8787 do_vfp_dp_conv_16 (void)
8788 {
8789   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8790   vfp_conv (16);
8791 }
8792
8793 static void
8794 do_vfp_sp_conv_32 (void)
8795 {
8796   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
8797   vfp_conv (32);
8798 }
8799
8800 static void
8801 do_vfp_dp_conv_32 (void)
8802 {
8803   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
8804   vfp_conv (32);
8805 }
8806 \f
8807 /* FPA instructions.  Also in a logical order.  */
8808
8809 static void
8810 do_fpa_cmp (void)
8811 {
8812   inst.instruction |= inst.operands[0].reg << 16;
8813   inst.instruction |= inst.operands[1].reg;
8814 }
8815
8816 static void
8817 do_fpa_ldmstm (void)
8818 {
8819   inst.instruction |= inst.operands[0].reg << 12;
8820   switch (inst.operands[1].imm)
8821     {
8822     case 1: inst.instruction |= CP_T_X;          break;
8823     case 2: inst.instruction |= CP_T_Y;          break;
8824     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
8825     case 4:                                      break;
8826     default: abort ();
8827     }
8828
8829   if (inst.instruction & (PRE_INDEX | INDEX_UP))
8830     {
8831       /* The instruction specified "ea" or "fd", so we can only accept
8832          [Rn]{!}.  The instruction does not really support stacking or
8833          unstacking, so we have to emulate these by setting appropriate
8834          bits and offsets.  */
8835       constraint (inst.reloc.exp.X_op != O_constant
8836                   || inst.reloc.exp.X_add_number != 0,
8837                   _("this instruction does not support indexing"));
8838
8839       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
8840         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
8841
8842       if (!(inst.instruction & INDEX_UP))
8843         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
8844
8845       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
8846         {
8847           inst.operands[2].preind = 0;
8848           inst.operands[2].postind = 1;
8849         }
8850     }
8851
8852   encode_arm_cp_address (2, TRUE, TRUE, 0);
8853 }
8854 \f
8855 /* iWMMXt instructions: strictly in alphabetical order.  */
8856
8857 static void
8858 do_iwmmxt_tandorc (void)
8859 {
8860   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
8861 }
8862
8863 static void
8864 do_iwmmxt_textrc (void)
8865 {
8866   inst.instruction |= inst.operands[0].reg << 12;
8867   inst.instruction |= inst.operands[1].imm;
8868 }
8869
8870 static void
8871 do_iwmmxt_textrm (void)
8872 {
8873   inst.instruction |= inst.operands[0].reg << 12;
8874   inst.instruction |= inst.operands[1].reg << 16;
8875   inst.instruction |= inst.operands[2].imm;
8876 }
8877
8878 static void
8879 do_iwmmxt_tinsr (void)
8880 {
8881   inst.instruction |= inst.operands[0].reg << 16;
8882   inst.instruction |= inst.operands[1].reg << 12;
8883   inst.instruction |= inst.operands[2].imm;
8884 }
8885
8886 static void
8887 do_iwmmxt_tmia (void)
8888 {
8889   inst.instruction |= inst.operands[0].reg << 5;
8890   inst.instruction |= inst.operands[1].reg;
8891   inst.instruction |= inst.operands[2].reg << 12;
8892 }
8893
8894 static void
8895 do_iwmmxt_waligni (void)
8896 {
8897   inst.instruction |= inst.operands[0].reg << 12;
8898   inst.instruction |= inst.operands[1].reg << 16;
8899   inst.instruction |= inst.operands[2].reg;
8900   inst.instruction |= inst.operands[3].imm << 20;
8901 }
8902
8903 static void
8904 do_iwmmxt_wmerge (void)
8905 {
8906   inst.instruction |= inst.operands[0].reg << 12;
8907   inst.instruction |= inst.operands[1].reg << 16;
8908   inst.instruction |= inst.operands[2].reg;
8909   inst.instruction |= inst.operands[3].imm << 21;
8910 }
8911
8912 static void
8913 do_iwmmxt_wmov (void)
8914 {
8915   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
8916   inst.instruction |= inst.operands[0].reg << 12;
8917   inst.instruction |= inst.operands[1].reg << 16;
8918   inst.instruction |= inst.operands[1].reg;
8919 }
8920
8921 static void
8922 do_iwmmxt_wldstbh (void)
8923 {
8924   int reloc;
8925   inst.instruction |= inst.operands[0].reg << 12;
8926   if (thumb_mode)
8927     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
8928   else
8929     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
8930   encode_arm_cp_address (1, TRUE, FALSE, reloc);
8931 }
8932
8933 static void
8934 do_iwmmxt_wldstw (void)
8935 {
8936   /* RIWR_RIWC clears .isreg for a control register.  */
8937   if (!inst.operands[0].isreg)
8938     {
8939       constraint (inst.cond != COND_ALWAYS, BAD_COND);
8940       inst.instruction |= 0xf0000000;
8941     }
8942
8943   inst.instruction |= inst.operands[0].reg << 12;
8944   encode_arm_cp_address (1, TRUE, TRUE, 0);
8945 }
8946
8947 static void
8948 do_iwmmxt_wldstd (void)
8949 {
8950   inst.instruction |= inst.operands[0].reg << 12;
8951   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
8952       && inst.operands[1].immisreg)
8953     {
8954       inst.instruction &= ~0x1a000ff;
8955       inst.instruction |= (0xf << 28);
8956       if (inst.operands[1].preind)
8957         inst.instruction |= PRE_INDEX;
8958       if (!inst.operands[1].negative)
8959         inst.instruction |= INDEX_UP;
8960       if (inst.operands[1].writeback)
8961         inst.instruction |= WRITE_BACK;
8962       inst.instruction |= inst.operands[1].reg << 16;
8963       inst.instruction |= inst.reloc.exp.X_add_number << 4;
8964       inst.instruction |= inst.operands[1].imm;
8965     }
8966   else
8967     encode_arm_cp_address (1, TRUE, FALSE, 0);
8968 }
8969
8970 static void
8971 do_iwmmxt_wshufh (void)
8972 {
8973   inst.instruction |= inst.operands[0].reg << 12;
8974   inst.instruction |= inst.operands[1].reg << 16;
8975   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
8976   inst.instruction |= (inst.operands[2].imm & 0x0f);
8977 }
8978
8979 static void
8980 do_iwmmxt_wzero (void)
8981 {
8982   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
8983   inst.instruction |= inst.operands[0].reg;
8984   inst.instruction |= inst.operands[0].reg << 12;
8985   inst.instruction |= inst.operands[0].reg << 16;
8986 }
8987
8988 static void
8989 do_iwmmxt_wrwrwr_or_imm5 (void)
8990 {
8991   if (inst.operands[2].isreg)
8992     do_rd_rn_rm ();
8993   else {
8994     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
8995                 _("immediate operand requires iWMMXt2"));
8996     do_rd_rn ();
8997     if (inst.operands[2].imm == 0)
8998       {
8999         switch ((inst.instruction >> 20) & 0xf)
9000           {
9001           case 4:
9002           case 5:
9003           case 6:
9004           case 7:
9005             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
9006             inst.operands[2].imm = 16;
9007             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
9008             break;
9009           case 8:
9010           case 9:
9011           case 10:
9012           case 11:
9013             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
9014             inst.operands[2].imm = 32;
9015             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
9016             break;
9017           case 12:
9018           case 13:
9019           case 14:
9020           case 15:
9021             {
9022               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
9023               unsigned long wrn;
9024               wrn = (inst.instruction >> 16) & 0xf;
9025               inst.instruction &= 0xff0fff0f;
9026               inst.instruction |= wrn;
9027               /* Bail out here; the instruction is now assembled.  */
9028               return;
9029             }
9030           }
9031       }
9032     /* Map 32 -> 0, etc.  */
9033     inst.operands[2].imm &= 0x1f;
9034     inst.instruction |= (0xf << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
9035   }
9036 }
9037 \f
9038 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
9039    operations first, then control, shift, and load/store.  */
9040
9041 /* Insns like "foo X,Y,Z".  */
9042
9043 static void
9044 do_mav_triple (void)
9045 {
9046   inst.instruction |= inst.operands[0].reg << 16;
9047   inst.instruction |= inst.operands[1].reg;
9048   inst.instruction |= inst.operands[2].reg << 12;
9049 }
9050
9051 /* Insns like "foo W,X,Y,Z".
9052     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
9053
9054 static void
9055 do_mav_quad (void)
9056 {
9057   inst.instruction |= inst.operands[0].reg << 5;
9058   inst.instruction |= inst.operands[1].reg << 12;
9059   inst.instruction |= inst.operands[2].reg << 16;
9060   inst.instruction |= inst.operands[3].reg;
9061 }
9062
9063 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
9064 static void
9065 do_mav_dspsc (void)
9066 {
9067   inst.instruction |= inst.operands[1].reg << 12;
9068 }
9069
9070 /* Maverick shift immediate instructions.
9071    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
9072    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
9073
9074 static void
9075 do_mav_shift (void)
9076 {
9077   int imm = inst.operands[2].imm;
9078
9079   inst.instruction |= inst.operands[0].reg << 12;
9080   inst.instruction |= inst.operands[1].reg << 16;
9081
9082   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
9083      Bits 5-7 of the insn should have bits 4-6 of the immediate.
9084      Bit 4 should be 0.  */
9085   imm = (imm & 0xf) | ((imm & 0x70) << 1);
9086
9087   inst.instruction |= imm;
9088 }
9089 \f
9090 /* XScale instructions.  Also sorted arithmetic before move.  */
9091
9092 /* Xscale multiply-accumulate (argument parse)
9093      MIAcc   acc0,Rm,Rs
9094      MIAPHcc acc0,Rm,Rs
9095      MIAxycc acc0,Rm,Rs.  */
9096
9097 static void
9098 do_xsc_mia (void)
9099 {
9100   inst.instruction |= inst.operands[1].reg;
9101   inst.instruction |= inst.operands[2].reg << 12;
9102 }
9103
9104 /* Xscale move-accumulator-register (argument parse)
9105
9106      MARcc   acc0,RdLo,RdHi.  */
9107
9108 static void
9109 do_xsc_mar (void)
9110 {
9111   inst.instruction |= inst.operands[1].reg << 12;
9112   inst.instruction |= inst.operands[2].reg << 16;
9113 }
9114
9115 /* Xscale move-register-accumulator (argument parse)
9116
9117      MRAcc   RdLo,RdHi,acc0.  */
9118
9119 static void
9120 do_xsc_mra (void)
9121 {
9122   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
9123   inst.instruction |= inst.operands[0].reg << 12;
9124   inst.instruction |= inst.operands[1].reg << 16;
9125 }
9126 \f
9127 /* Encoding functions relevant only to Thumb.  */
9128
9129 /* inst.operands[i] is a shifted-register operand; encode
9130    it into inst.instruction in the format used by Thumb32.  */
9131
9132 static void
9133 encode_thumb32_shifted_operand (int i)
9134 {
9135   unsigned int value = inst.reloc.exp.X_add_number;
9136   unsigned int shift = inst.operands[i].shift_kind;
9137
9138   constraint (inst.operands[i].immisreg,
9139               _("shift by register not allowed in thumb mode"));
9140   inst.instruction |= inst.operands[i].reg;
9141   if (shift == SHIFT_RRX)
9142     inst.instruction |= SHIFT_ROR << 4;
9143   else
9144     {
9145       constraint (inst.reloc.exp.X_op != O_constant,
9146                   _("expression too complex"));
9147
9148       constraint (value > 32
9149                   || (value == 32 && (shift == SHIFT_LSL
9150                                       || shift == SHIFT_ROR)),
9151                   _("shift expression is too large"));
9152
9153       if (value == 0)
9154         shift = SHIFT_LSL;
9155       else if (value == 32)
9156         value = 0;
9157
9158       inst.instruction |= shift << 4;
9159       inst.instruction |= (value & 0x1c) << 10;
9160       inst.instruction |= (value & 0x03) << 6;
9161     }
9162 }
9163
9164
9165 /* inst.operands[i] was set up by parse_address.  Encode it into a
9166    Thumb32 format load or store instruction.  Reject forms that cannot
9167    be used with such instructions.  If is_t is true, reject forms that
9168    cannot be used with a T instruction; if is_d is true, reject forms
9169    that cannot be used with a D instruction.  If it is a store insn,
9170    reject PC in Rn.  */
9171
9172 static void
9173 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
9174 {
9175   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
9176
9177   constraint (!inst.operands[i].isreg,
9178               _("Instruction does not support =N addresses"));
9179
9180   inst.instruction |= inst.operands[i].reg << 16;
9181   if (inst.operands[i].immisreg)
9182     {
9183       constraint (is_pc, BAD_PC_ADDRESSING);
9184       constraint (is_t || is_d, _("cannot use register index with this instruction"));
9185       constraint (inst.operands[i].negative,
9186                   _("Thumb does not support negative register indexing"));
9187       constraint (inst.operands[i].postind,
9188                   _("Thumb does not support register post-indexing"));
9189       constraint (inst.operands[i].writeback,
9190                   _("Thumb does not support register indexing with writeback"));
9191       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
9192                   _("Thumb supports only LSL in shifted register indexing"));
9193
9194       inst.instruction |= inst.operands[i].imm;
9195       if (inst.operands[i].shifted)
9196         {
9197           constraint (inst.reloc.exp.X_op != O_constant,
9198                       _("expression too complex"));
9199           constraint (inst.reloc.exp.X_add_number < 0
9200                       || inst.reloc.exp.X_add_number > 3,
9201                       _("shift out of range"));
9202           inst.instruction |= inst.reloc.exp.X_add_number << 4;
9203         }
9204       inst.reloc.type = BFD_RELOC_UNUSED;
9205     }
9206   else if (inst.operands[i].preind)
9207     {
9208       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
9209       constraint (is_t && inst.operands[i].writeback,
9210                   _("cannot use writeback with this instruction"));
9211       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0)
9212                   && !inst.reloc.pc_rel, BAD_PC_ADDRESSING);
9213
9214       if (is_d)
9215         {
9216           inst.instruction |= 0x01000000;
9217           if (inst.operands[i].writeback)
9218             inst.instruction |= 0x00200000;
9219         }
9220       else
9221         {
9222           inst.instruction |= 0x00000c00;
9223           if (inst.operands[i].writeback)
9224             inst.instruction |= 0x00000100;
9225         }
9226       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
9227     }
9228   else if (inst.operands[i].postind)
9229     {
9230       gas_assert (inst.operands[i].writeback);
9231       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
9232       constraint (is_t, _("cannot use post-indexing with this instruction"));
9233
9234       if (is_d)
9235         inst.instruction |= 0x00200000;
9236       else
9237         inst.instruction |= 0x00000900;
9238       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
9239     }
9240   else /* unindexed - only for coprocessor */
9241     inst.error = _("instruction does not accept unindexed addressing");
9242 }
9243
9244 /* Table of Thumb instructions which exist in both 16- and 32-bit
9245    encodings (the latter only in post-V6T2 cores).  The index is the
9246    value used in the insns table below.  When there is more than one
9247    possible 16-bit encoding for the instruction, this table always
9248    holds variant (1).
9249    Also contains several pseudo-instructions used during relaxation.  */
9250 #define T16_32_TAB                              \
9251   X(_adc,   4140, eb400000),                    \
9252   X(_adcs,  4140, eb500000),                    \
9253   X(_add,   1c00, eb000000),                    \
9254   X(_adds,  1c00, eb100000),                    \
9255   X(_addi,  0000, f1000000),                    \
9256   X(_addis, 0000, f1100000),                    \
9257   X(_add_pc,000f, f20f0000),                    \
9258   X(_add_sp,000d, f10d0000),                    \
9259   X(_adr,   000f, f20f0000),                    \
9260   X(_and,   4000, ea000000),                    \
9261   X(_ands,  4000, ea100000),                    \
9262   X(_asr,   1000, fa40f000),                    \
9263   X(_asrs,  1000, fa50f000),                    \
9264   X(_b,     e000, f000b000),                    \
9265   X(_bcond, d000, f0008000),                    \
9266   X(_bic,   4380, ea200000),                    \
9267   X(_bics,  4380, ea300000),                    \
9268   X(_cmn,   42c0, eb100f00),                    \
9269   X(_cmp,   2800, ebb00f00),                    \
9270   X(_cpsie, b660, f3af8400),                    \
9271   X(_cpsid, b670, f3af8600),                    \
9272   X(_cpy,   4600, ea4f0000),                    \
9273   X(_dec_sp,80dd, f1ad0d00),                    \
9274   X(_eor,   4040, ea800000),                    \
9275   X(_eors,  4040, ea900000),                    \
9276   X(_inc_sp,00dd, f10d0d00),                    \
9277   X(_ldmia, c800, e8900000),                    \
9278   X(_ldr,   6800, f8500000),                    \
9279   X(_ldrb,  7800, f8100000),                    \
9280   X(_ldrh,  8800, f8300000),                    \
9281   X(_ldrsb, 5600, f9100000),                    \
9282   X(_ldrsh, 5e00, f9300000),                    \
9283   X(_ldr_pc,4800, f85f0000),                    \
9284   X(_ldr_pc2,4800, f85f0000),                   \
9285   X(_ldr_sp,9800, f85d0000),                    \
9286   X(_lsl,   0000, fa00f000),                    \
9287   X(_lsls,  0000, fa10f000),                    \
9288   X(_lsr,   0800, fa20f000),                    \
9289   X(_lsrs,  0800, fa30f000),                    \
9290   X(_mov,   2000, ea4f0000),                    \
9291   X(_movs,  2000, ea5f0000),                    \
9292   X(_mul,   4340, fb00f000),                     \
9293   X(_muls,  4340, ffffffff), /* no 32b muls */  \
9294   X(_mvn,   43c0, ea6f0000),                    \
9295   X(_mvns,  43c0, ea7f0000),                    \
9296   X(_neg,   4240, f1c00000), /* rsb #0 */       \
9297   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
9298   X(_orr,   4300, ea400000),                    \
9299   X(_orrs,  4300, ea500000),                    \
9300   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
9301   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
9302   X(_rev,   ba00, fa90f080),                    \
9303   X(_rev16, ba40, fa90f090),                    \
9304   X(_revsh, bac0, fa90f0b0),                    \
9305   X(_ror,   41c0, fa60f000),                    \
9306   X(_rors,  41c0, fa70f000),                    \
9307   X(_sbc,   4180, eb600000),                    \
9308   X(_sbcs,  4180, eb700000),                    \
9309   X(_stmia, c000, e8800000),                    \
9310   X(_str,   6000, f8400000),                    \
9311   X(_strb,  7000, f8000000),                    \
9312   X(_strh,  8000, f8200000),                    \
9313   X(_str_sp,9000, f84d0000),                    \
9314   X(_sub,   1e00, eba00000),                    \
9315   X(_subs,  1e00, ebb00000),                    \
9316   X(_subi,  8000, f1a00000),                    \
9317   X(_subis, 8000, f1b00000),                    \
9318   X(_sxtb,  b240, fa4ff080),                    \
9319   X(_sxth,  b200, fa0ff080),                    \
9320   X(_tst,   4200, ea100f00),                    \
9321   X(_uxtb,  b2c0, fa5ff080),                    \
9322   X(_uxth,  b280, fa1ff080),                    \
9323   X(_nop,   bf00, f3af8000),                    \
9324   X(_yield, bf10, f3af8001),                    \
9325   X(_wfe,   bf20, f3af8002),                    \
9326   X(_wfi,   bf30, f3af8003),                    \
9327   X(_sev,   bf40, f3af8004),
9328
9329 /* To catch errors in encoding functions, the codes are all offset by
9330    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
9331    as 16-bit instructions.  */
9332 #define X(a,b,c) T_MNEM##a
9333 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
9334 #undef X
9335
9336 #define X(a,b,c) 0x##b
9337 static const unsigned short thumb_op16[] = { T16_32_TAB };
9338 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
9339 #undef X
9340
9341 #define X(a,b,c) 0x##c
9342 static const unsigned int thumb_op32[] = { T16_32_TAB };
9343 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
9344 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
9345 #undef X
9346 #undef T16_32_TAB
9347
9348 /* Thumb instruction encoders, in alphabetical order.  */
9349
9350 /* ADDW or SUBW.  */
9351
9352 static void
9353 do_t_add_sub_w (void)
9354 {
9355   int Rd, Rn;
9356
9357   Rd = inst.operands[0].reg;
9358   Rn = inst.operands[1].reg;
9359
9360   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
9361      is the SP-{plus,minus}-immediate form of the instruction.  */
9362   if (Rn == REG_SP)
9363     constraint (Rd == REG_PC, BAD_PC);
9364   else
9365     reject_bad_reg (Rd);
9366
9367   inst.instruction |= (Rn << 16) | (Rd << 8);
9368   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
9369 }
9370
9371 /* Parse an add or subtract instruction.  We get here with inst.instruction
9372    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
9373
9374 static void
9375 do_t_add_sub (void)
9376 {
9377   int Rd, Rs, Rn;
9378
9379   Rd = inst.operands[0].reg;
9380   Rs = (inst.operands[1].present
9381         ? inst.operands[1].reg    /* Rd, Rs, foo */
9382         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9383
9384   if (Rd == REG_PC)
9385     set_it_insn_type_last ();
9386
9387   if (unified_syntax)
9388     {
9389       bfd_boolean flags;
9390       bfd_boolean narrow;
9391       int opcode;
9392
9393       flags = (inst.instruction == T_MNEM_adds
9394                || inst.instruction == T_MNEM_subs);
9395       if (flags)
9396         narrow = !in_it_block ();
9397       else
9398         narrow = in_it_block ();
9399       if (!inst.operands[2].isreg)
9400         {
9401           int add;
9402
9403           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
9404
9405           add = (inst.instruction == T_MNEM_add
9406                  || inst.instruction == T_MNEM_adds);
9407           opcode = 0;
9408           if (inst.size_req != 4)
9409             {
9410               /* Attempt to use a narrow opcode, with relaxation if
9411                  appropriate.  */
9412               if (Rd == REG_SP && Rs == REG_SP && !flags)
9413                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
9414               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
9415                 opcode = T_MNEM_add_sp;
9416               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
9417                 opcode = T_MNEM_add_pc;
9418               else if (Rd <= 7 && Rs <= 7 && narrow)
9419                 {
9420                   if (flags)
9421                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
9422                   else
9423                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
9424                 }
9425               if (opcode)
9426                 {
9427                   inst.instruction = THUMB_OP16(opcode);
9428                   inst.instruction |= (Rd << 4) | Rs;
9429                   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9430                   if (inst.size_req != 2)
9431                     inst.relax = opcode;
9432                 }
9433               else
9434                 constraint (inst.size_req == 2, BAD_HIREG);
9435             }
9436           if (inst.size_req == 4
9437               || (inst.size_req != 2 && !opcode))
9438             {
9439               if (Rd == REG_PC)
9440                 {
9441                   constraint (add, BAD_PC);
9442                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
9443                              _("only SUBS PC, LR, #const allowed"));
9444                   constraint (inst.reloc.exp.X_op != O_constant,
9445                               _("expression too complex"));
9446                   constraint (inst.reloc.exp.X_add_number < 0
9447                               || inst.reloc.exp.X_add_number > 0xff,
9448                              _("immediate value out of range"));
9449                   inst.instruction = T2_SUBS_PC_LR
9450                                      | inst.reloc.exp.X_add_number;
9451                   inst.reloc.type = BFD_RELOC_UNUSED;
9452                   return;
9453                 }
9454               else if (Rs == REG_PC)
9455                 {
9456                   /* Always use addw/subw.  */
9457                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
9458                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
9459                 }
9460               else
9461                 {
9462                   inst.instruction = THUMB_OP32 (inst.instruction);
9463                   inst.instruction = (inst.instruction & 0xe1ffffff)
9464                                      | 0x10000000;
9465                   if (flags)
9466                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9467                   else
9468                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
9469                 }
9470               inst.instruction |= Rd << 8;
9471               inst.instruction |= Rs << 16;
9472             }
9473         }
9474       else
9475         {
9476           Rn = inst.operands[2].reg;
9477           /* See if we can do this with a 16-bit instruction.  */
9478           if (!inst.operands[2].shifted && inst.size_req != 4)
9479             {
9480               if (Rd > 7 || Rs > 7 || Rn > 7)
9481                 narrow = FALSE;
9482
9483               if (narrow)
9484                 {
9485                   inst.instruction = ((inst.instruction == T_MNEM_adds
9486                                        || inst.instruction == T_MNEM_add)
9487                                       ? T_OPCODE_ADD_R3
9488                                       : T_OPCODE_SUB_R3);
9489                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
9490                   return;
9491                 }
9492
9493               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
9494                 {
9495                   /* Thumb-1 cores (except v6-M) require at least one high
9496                      register in a narrow non flag setting add.  */
9497                   if (Rd > 7 || Rn > 7
9498                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
9499                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
9500                     {
9501                       if (Rd == Rn)
9502                         {
9503                           Rn = Rs;
9504                           Rs = Rd;
9505                         }
9506                       inst.instruction = T_OPCODE_ADD_HI;
9507                       inst.instruction |= (Rd & 8) << 4;
9508                       inst.instruction |= (Rd & 7);
9509                       inst.instruction |= Rn << 3;
9510                       return;
9511                     }
9512                 }
9513             }
9514
9515           constraint (Rd == REG_PC, BAD_PC);
9516           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
9517           constraint (Rs == REG_PC, BAD_PC);
9518           reject_bad_reg (Rn);
9519
9520           /* If we get here, it can't be done in 16 bits.  */
9521           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
9522                       _("shift must be constant"));
9523           inst.instruction = THUMB_OP32 (inst.instruction);
9524           inst.instruction |= Rd << 8;
9525           inst.instruction |= Rs << 16;
9526           encode_thumb32_shifted_operand (2);
9527         }
9528     }
9529   else
9530     {
9531       constraint (inst.instruction == T_MNEM_adds
9532                   || inst.instruction == T_MNEM_subs,
9533                   BAD_THUMB32);
9534
9535       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
9536         {
9537           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
9538                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
9539                       BAD_HIREG);
9540
9541           inst.instruction = (inst.instruction == T_MNEM_add
9542                               ? 0x0000 : 0x8000);
9543           inst.instruction |= (Rd << 4) | Rs;
9544           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9545           return;
9546         }
9547
9548       Rn = inst.operands[2].reg;
9549       constraint (inst.operands[2].shifted, _("unshifted register required"));
9550
9551       /* We now have Rd, Rs, and Rn set to registers.  */
9552       if (Rd > 7 || Rs > 7 || Rn > 7)
9553         {
9554           /* Can't do this for SUB.      */
9555           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
9556           inst.instruction = T_OPCODE_ADD_HI;
9557           inst.instruction |= (Rd & 8) << 4;
9558           inst.instruction |= (Rd & 7);
9559           if (Rs == Rd)
9560             inst.instruction |= Rn << 3;
9561           else if (Rn == Rd)
9562             inst.instruction |= Rs << 3;
9563           else
9564             constraint (1, _("dest must overlap one source register"));
9565         }
9566       else
9567         {
9568           inst.instruction = (inst.instruction == T_MNEM_add
9569                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
9570           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
9571         }
9572     }
9573 }
9574
9575 static void
9576 do_t_adr (void)
9577 {
9578   unsigned Rd;
9579
9580   Rd = inst.operands[0].reg;
9581   reject_bad_reg (Rd);
9582
9583   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
9584     {
9585       /* Defer to section relaxation.  */
9586       inst.relax = inst.instruction;
9587       inst.instruction = THUMB_OP16 (inst.instruction);
9588       inst.instruction |= Rd << 4;
9589     }
9590   else if (unified_syntax && inst.size_req != 2)
9591     {
9592       /* Generate a 32-bit opcode.  */
9593       inst.instruction = THUMB_OP32 (inst.instruction);
9594       inst.instruction |= Rd << 8;
9595       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
9596       inst.reloc.pc_rel = 1;
9597     }
9598   else
9599     {
9600       /* Generate a 16-bit opcode.  */
9601       inst.instruction = THUMB_OP16 (inst.instruction);
9602       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
9603       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
9604       inst.reloc.pc_rel = 1;
9605
9606       inst.instruction |= Rd << 4;
9607     }
9608 }
9609
9610 /* Arithmetic instructions for which there is just one 16-bit
9611    instruction encoding, and it allows only two low registers.
9612    For maximal compatibility with ARM syntax, we allow three register
9613    operands even when Thumb-32 instructions are not available, as long
9614    as the first two are identical.  For instance, both "sbc r0,r1" and
9615    "sbc r0,r0,r1" are allowed.  */
9616 static void
9617 do_t_arit3 (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               && Rd == Rs)
9663             {
9664               inst.instruction = THUMB_OP16 (inst.instruction);
9665               inst.instruction |= Rd;
9666               inst.instruction |= Rn << 3;
9667               return;
9668             }
9669
9670           /* If we get here, it can't be done in 16 bits.  */
9671           constraint (inst.operands[2].shifted
9672                       && inst.operands[2].immisreg,
9673                       _("shift must be constant"));
9674           inst.instruction = THUMB_OP32 (inst.instruction);
9675           inst.instruction |= Rd << 8;
9676           inst.instruction |= Rs << 16;
9677           encode_thumb32_shifted_operand (2);
9678         }
9679     }
9680   else
9681     {
9682       /* On its face this is a lie - the instruction does set the
9683          flags.  However, the only supported mnemonic in this mode
9684          says it doesn't.  */
9685       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
9686
9687       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
9688                   _("unshifted register required"));
9689       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
9690       constraint (Rd != Rs,
9691                   _("dest and source1 must be the same register"));
9692
9693       inst.instruction = THUMB_OP16 (inst.instruction);
9694       inst.instruction |= Rd;
9695       inst.instruction |= Rn << 3;
9696     }
9697 }
9698
9699 /* Similarly, but for instructions where the arithmetic operation is
9700    commutative, so we can allow either of them to be different from
9701    the destination operand in a 16-bit instruction.  For instance, all
9702    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
9703    accepted.  */
9704 static void
9705 do_t_arit3c (void)
9706 {
9707   int Rd, Rs, Rn;
9708
9709   Rd = inst.operands[0].reg;
9710   Rs = (inst.operands[1].present
9711         ? inst.operands[1].reg    /* Rd, Rs, foo */
9712         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9713   Rn = inst.operands[2].reg;
9714
9715   reject_bad_reg (Rd);
9716   reject_bad_reg (Rs);
9717   if (inst.operands[2].isreg)
9718     reject_bad_reg (Rn);
9719
9720   if (unified_syntax)
9721     {
9722       if (!inst.operands[2].isreg)
9723         {
9724           /* For an immediate, we always generate a 32-bit opcode;
9725              section relaxation will shrink it later if possible.  */
9726           inst.instruction = THUMB_OP32 (inst.instruction);
9727           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9728           inst.instruction |= Rd << 8;
9729           inst.instruction |= Rs << 16;
9730           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9731         }
9732       else
9733         {
9734           bfd_boolean narrow;
9735
9736           /* See if we can do this with a 16-bit instruction.  */
9737           if (THUMB_SETS_FLAGS (inst.instruction))
9738             narrow = !in_it_block ();
9739           else
9740             narrow = in_it_block ();
9741
9742           if (Rd > 7 || Rn > 7 || Rs > 7)
9743             narrow = FALSE;
9744           if (inst.operands[2].shifted)
9745             narrow = FALSE;
9746           if (inst.size_req == 4)
9747             narrow = FALSE;
9748
9749           if (narrow)
9750             {
9751               if (Rd == Rs)
9752                 {
9753                   inst.instruction = THUMB_OP16 (inst.instruction);
9754                   inst.instruction |= Rd;
9755                   inst.instruction |= Rn << 3;
9756                   return;
9757                 }
9758               if (Rd == Rn)
9759                 {
9760                   inst.instruction = THUMB_OP16 (inst.instruction);
9761                   inst.instruction |= Rd;
9762                   inst.instruction |= Rs << 3;
9763                   return;
9764                 }
9765             }
9766
9767           /* If we get here, it can't be done in 16 bits.  */
9768           constraint (inst.operands[2].shifted
9769                       && inst.operands[2].immisreg,
9770                       _("shift must be constant"));
9771           inst.instruction = THUMB_OP32 (inst.instruction);
9772           inst.instruction |= Rd << 8;
9773           inst.instruction |= Rs << 16;
9774           encode_thumb32_shifted_operand (2);
9775         }
9776     }
9777   else
9778     {
9779       /* On its face this is a lie - the instruction does set the
9780          flags.  However, the only supported mnemonic in this mode
9781          says it doesn't.  */
9782       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
9783
9784       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
9785                   _("unshifted register required"));
9786       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
9787
9788       inst.instruction = THUMB_OP16 (inst.instruction);
9789       inst.instruction |= Rd;
9790
9791       if (Rd == Rs)
9792         inst.instruction |= Rn << 3;
9793       else if (Rd == Rn)
9794         inst.instruction |= Rs << 3;
9795       else
9796         constraint (1, _("dest must overlap one source register"));
9797     }
9798 }
9799
9800 static void
9801 do_t_barrier (void)
9802 {
9803   if (inst.operands[0].present)
9804     {
9805       constraint ((inst.instruction & 0xf0) != 0x40
9806                   && inst.operands[0].imm > 0xf
9807                   && inst.operands[0].imm < 0x0,
9808                   _("bad barrier type"));
9809       inst.instruction |= inst.operands[0].imm;
9810     }
9811   else
9812     inst.instruction |= 0xf;
9813 }
9814
9815 static void
9816 do_t_bfc (void)
9817 {
9818   unsigned Rd;
9819   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
9820   constraint (msb > 32, _("bit-field extends past end of register"));
9821   /* The instruction encoding stores the LSB and MSB,
9822      not the LSB and width.  */
9823   Rd = inst.operands[0].reg;
9824   reject_bad_reg (Rd);
9825   inst.instruction |= Rd << 8;
9826   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
9827   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
9828   inst.instruction |= msb - 1;
9829 }
9830
9831 static void
9832 do_t_bfi (void)
9833 {
9834   int Rd, Rn;
9835   unsigned int msb;
9836
9837   Rd = inst.operands[0].reg;
9838   reject_bad_reg (Rd);
9839
9840   /* #0 in second position is alternative syntax for bfc, which is
9841      the same instruction but with REG_PC in the Rm field.  */
9842   if (!inst.operands[1].isreg)
9843     Rn = REG_PC;
9844   else
9845     {
9846       Rn = inst.operands[1].reg;
9847       reject_bad_reg (Rn);
9848     }
9849
9850   msb = inst.operands[2].imm + inst.operands[3].imm;
9851   constraint (msb > 32, _("bit-field extends past end of register"));
9852   /* The instruction encoding stores the LSB and MSB,
9853      not the LSB and width.  */
9854   inst.instruction |= Rd << 8;
9855   inst.instruction |= Rn << 16;
9856   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
9857   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
9858   inst.instruction |= msb - 1;
9859 }
9860
9861 static void
9862 do_t_bfx (void)
9863 {
9864   unsigned Rd, Rn;
9865
9866   Rd = inst.operands[0].reg;
9867   Rn = inst.operands[1].reg;
9868
9869   reject_bad_reg (Rd);
9870   reject_bad_reg (Rn);
9871
9872   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
9873               _("bit-field extends past end of register"));
9874   inst.instruction |= Rd << 8;
9875   inst.instruction |= Rn << 16;
9876   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
9877   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
9878   inst.instruction |= inst.operands[3].imm - 1;
9879 }
9880
9881 /* ARM V5 Thumb BLX (argument parse)
9882         BLX <target_addr>       which is BLX(1)
9883         BLX <Rm>                which is BLX(2)
9884    Unfortunately, there are two different opcodes for this mnemonic.
9885    So, the insns[].value is not used, and the code here zaps values
9886         into inst.instruction.
9887
9888    ??? How to take advantage of the additional two bits of displacement
9889    available in Thumb32 mode?  Need new relocation?  */
9890
9891 static void
9892 do_t_blx (void)
9893 {
9894   set_it_insn_type_last ();
9895
9896   if (inst.operands[0].isreg)
9897     {
9898       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9899       /* We have a register, so this is BLX(2).  */
9900       inst.instruction |= inst.operands[0].reg << 3;
9901     }
9902   else
9903     {
9904       /* No register.  This must be BLX(1).  */
9905       inst.instruction = 0xf000e800;
9906       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
9907     }
9908 }
9909
9910 static void
9911 do_t_branch (void)
9912 {
9913   int opcode;
9914   int cond;
9915   int reloc;
9916
9917   cond = inst.cond;
9918   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
9919
9920   if (in_it_block ())
9921     {
9922       /* Conditional branches inside IT blocks are encoded as unconditional
9923          branches.  */
9924       cond = COND_ALWAYS;
9925     }
9926   else
9927     cond = inst.cond;
9928
9929   if (cond != COND_ALWAYS)
9930     opcode = T_MNEM_bcond;
9931   else
9932     opcode = inst.instruction;
9933
9934   if (unified_syntax
9935       && (inst.size_req == 4
9936           || (inst.size_req != 2
9937               && (inst.operands[0].hasreloc
9938                   || inst.reloc.exp.X_op == O_constant))))
9939     {
9940       inst.instruction = THUMB_OP32(opcode);
9941       if (cond == COND_ALWAYS)
9942         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
9943       else
9944         {
9945           gas_assert (cond != 0xF);
9946           inst.instruction |= cond << 22;
9947           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
9948         }
9949     }
9950   else
9951     {
9952       inst.instruction = THUMB_OP16(opcode);
9953       if (cond == COND_ALWAYS)
9954         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
9955       else
9956         {
9957           inst.instruction |= cond << 8;
9958           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
9959         }
9960       /* Allow section relaxation.  */
9961       if (unified_syntax && inst.size_req != 2)
9962         inst.relax = opcode;
9963     }
9964   inst.reloc.type = reloc;
9965   inst.reloc.pc_rel = 1;
9966 }
9967
9968 static void
9969 do_t_bkpt (void)
9970 {
9971   constraint (inst.cond != COND_ALWAYS,
9972               _("instruction is always unconditional"));
9973   if (inst.operands[0].present)
9974     {
9975       constraint (inst.operands[0].imm > 255,
9976                   _("immediate value out of range"));
9977       inst.instruction |= inst.operands[0].imm;
9978       set_it_insn_type (NEUTRAL_IT_INSN);
9979     }
9980 }
9981
9982 static void
9983 do_t_branch23 (void)
9984 {
9985   set_it_insn_type_last ();
9986   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
9987   
9988   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
9989      this file.  We used to simply ignore the PLT reloc type here --
9990      the branch encoding is now needed to deal with TLSCALL relocs.
9991      So if we see a PLT reloc now, put it back to how it used to be to
9992      keep the preexisting behaviour.  */
9993   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
9994     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
9995
9996 #if defined(OBJ_COFF)
9997   /* If the destination of the branch is a defined symbol which does not have
9998      the THUMB_FUNC attribute, then we must be calling a function which has
9999      the (interfacearm) attribute.  We look for the Thumb entry point to that
10000      function and change the branch to refer to that function instead.  */
10001   if (   inst.reloc.exp.X_op == O_symbol
10002       && inst.reloc.exp.X_add_symbol != NULL
10003       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
10004       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
10005     inst.reloc.exp.X_add_symbol =
10006       find_real_start (inst.reloc.exp.X_add_symbol);
10007 #endif
10008 }
10009
10010 static void
10011 do_t_bx (void)
10012 {
10013   set_it_insn_type_last ();
10014   inst.instruction |= inst.operands[0].reg << 3;
10015   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
10016      should cause the alignment to be checked once it is known.  This is
10017      because BX PC only works if the instruction is word aligned.  */
10018 }
10019
10020 static void
10021 do_t_bxj (void)
10022 {
10023   int Rm;
10024
10025   set_it_insn_type_last ();
10026   Rm = inst.operands[0].reg;
10027   reject_bad_reg (Rm);
10028   inst.instruction |= Rm << 16;
10029 }
10030
10031 static void
10032 do_t_clz (void)
10033 {
10034   unsigned Rd;
10035   unsigned Rm;
10036
10037   Rd = inst.operands[0].reg;
10038   Rm = inst.operands[1].reg;
10039
10040   reject_bad_reg (Rd);
10041   reject_bad_reg (Rm);
10042
10043   inst.instruction |= Rd << 8;
10044   inst.instruction |= Rm << 16;
10045   inst.instruction |= Rm;
10046 }
10047
10048 static void
10049 do_t_cps (void)
10050 {
10051   set_it_insn_type (OUTSIDE_IT_INSN);
10052   inst.instruction |= inst.operands[0].imm;
10053 }
10054
10055 static void
10056 do_t_cpsi (void)
10057 {
10058   set_it_insn_type (OUTSIDE_IT_INSN);
10059   if (unified_syntax
10060       && (inst.operands[1].present || inst.size_req == 4)
10061       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
10062     {
10063       unsigned int imod = (inst.instruction & 0x0030) >> 4;
10064       inst.instruction = 0xf3af8000;
10065       inst.instruction |= imod << 9;
10066       inst.instruction |= inst.operands[0].imm << 5;
10067       if (inst.operands[1].present)
10068         inst.instruction |= 0x100 | inst.operands[1].imm;
10069     }
10070   else
10071     {
10072       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
10073                   && (inst.operands[0].imm & 4),
10074                   _("selected processor does not support 'A' form "
10075                     "of this instruction"));
10076       constraint (inst.operands[1].present || inst.size_req == 4,
10077                   _("Thumb does not support the 2-argument "
10078                     "form of this instruction"));
10079       inst.instruction |= inst.operands[0].imm;
10080     }
10081 }
10082
10083 /* THUMB CPY instruction (argument parse).  */
10084
10085 static void
10086 do_t_cpy (void)
10087 {
10088   if (inst.size_req == 4)
10089     {
10090       inst.instruction = THUMB_OP32 (T_MNEM_mov);
10091       inst.instruction |= inst.operands[0].reg << 8;
10092       inst.instruction |= inst.operands[1].reg;
10093     }
10094   else
10095     {
10096       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
10097       inst.instruction |= (inst.operands[0].reg & 0x7);
10098       inst.instruction |= inst.operands[1].reg << 3;
10099     }
10100 }
10101
10102 static void
10103 do_t_cbz (void)
10104 {
10105   set_it_insn_type (OUTSIDE_IT_INSN);
10106   constraint (inst.operands[0].reg > 7, BAD_HIREG);
10107   inst.instruction |= inst.operands[0].reg;
10108   inst.reloc.pc_rel = 1;
10109   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
10110 }
10111
10112 static void
10113 do_t_dbg (void)
10114 {
10115   inst.instruction |= inst.operands[0].imm;
10116 }
10117
10118 static void
10119 do_t_div (void)
10120 {
10121   unsigned Rd, Rn, Rm;
10122
10123   Rd = inst.operands[0].reg;
10124   Rn = (inst.operands[1].present
10125         ? inst.operands[1].reg : Rd);
10126   Rm = inst.operands[2].reg;
10127
10128   reject_bad_reg (Rd);
10129   reject_bad_reg (Rn);
10130   reject_bad_reg (Rm);
10131
10132   inst.instruction |= Rd << 8;
10133   inst.instruction |= Rn << 16;
10134   inst.instruction |= Rm;
10135 }
10136
10137 static void
10138 do_t_hint (void)
10139 {
10140   if (unified_syntax && inst.size_req == 4)
10141     inst.instruction = THUMB_OP32 (inst.instruction);
10142   else
10143     inst.instruction = THUMB_OP16 (inst.instruction);
10144 }
10145
10146 static void
10147 do_t_it (void)
10148 {
10149   unsigned int cond = inst.operands[0].imm;
10150
10151   set_it_insn_type (IT_INSN);
10152   now_it.mask = (inst.instruction & 0xf) | 0x10;
10153   now_it.cc = cond;
10154
10155   /* If the condition is a negative condition, invert the mask.  */
10156   if ((cond & 0x1) == 0x0)
10157     {
10158       unsigned int mask = inst.instruction & 0x000f;
10159
10160       if ((mask & 0x7) == 0)
10161         /* no conversion needed */;
10162       else if ((mask & 0x3) == 0)
10163         mask ^= 0x8;
10164       else if ((mask & 0x1) == 0)
10165         mask ^= 0xC;
10166       else
10167         mask ^= 0xE;
10168
10169       inst.instruction &= 0xfff0;
10170       inst.instruction |= mask;
10171     }
10172
10173   inst.instruction |= cond << 4;
10174 }
10175
10176 /* Helper function used for both push/pop and ldm/stm.  */
10177 static void
10178 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
10179 {
10180   bfd_boolean load;
10181
10182   load = (inst.instruction & (1 << 20)) != 0;
10183
10184   if (mask & (1 << 13))
10185     inst.error =  _("SP not allowed in register list");
10186
10187   if ((mask & (1 << base)) != 0
10188       && writeback)
10189     inst.error = _("having the base register in the register list when "
10190                    "using write back is UNPREDICTABLE");
10191
10192   if (load)
10193     {
10194       if (mask & (1 << 15))
10195         {
10196           if (mask & (1 << 14))
10197             inst.error = _("LR and PC should not both be in register list");
10198           else
10199             set_it_insn_type_last ();
10200         }
10201     }
10202   else
10203     {
10204       if (mask & (1 << 15))
10205         inst.error = _("PC not allowed in register list");
10206     }
10207
10208   if ((mask & (mask - 1)) == 0)
10209     {
10210       /* Single register transfers implemented as str/ldr.  */
10211       if (writeback)
10212         {
10213           if (inst.instruction & (1 << 23))
10214             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
10215           else
10216             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
10217         }
10218       else
10219         {
10220           if (inst.instruction & (1 << 23))
10221             inst.instruction = 0x00800000; /* ia -> [base] */
10222           else
10223             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
10224         }
10225
10226       inst.instruction |= 0xf8400000;
10227       if (load)
10228         inst.instruction |= 0x00100000;
10229
10230       mask = ffs (mask) - 1;
10231       mask <<= 12;
10232     }
10233   else if (writeback)
10234     inst.instruction |= WRITE_BACK;
10235
10236   inst.instruction |= mask;
10237   inst.instruction |= base << 16;
10238 }
10239
10240 static void
10241 do_t_ldmstm (void)
10242 {
10243   /* This really doesn't seem worth it.  */
10244   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
10245               _("expression too complex"));
10246   constraint (inst.operands[1].writeback,
10247               _("Thumb load/store multiple does not support {reglist}^"));
10248
10249   if (unified_syntax)
10250     {
10251       bfd_boolean narrow;
10252       unsigned mask;
10253
10254       narrow = FALSE;
10255       /* See if we can use a 16-bit instruction.  */
10256       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
10257           && inst.size_req != 4
10258           && !(inst.operands[1].imm & ~0xff))
10259         {
10260           mask = 1 << inst.operands[0].reg;
10261
10262           if (inst.operands[0].reg <= 7)
10263             {
10264               if (inst.instruction == T_MNEM_stmia
10265                   ? inst.operands[0].writeback
10266                   : (inst.operands[0].writeback
10267                      == !(inst.operands[1].imm & mask)))
10268                 {
10269                   if (inst.instruction == T_MNEM_stmia
10270                       && (inst.operands[1].imm & mask)
10271                       && (inst.operands[1].imm & (mask - 1)))
10272                     as_warn (_("value stored for r%d is UNKNOWN"),
10273                              inst.operands[0].reg);
10274
10275                   inst.instruction = THUMB_OP16 (inst.instruction);
10276                   inst.instruction |= inst.operands[0].reg << 8;
10277                   inst.instruction |= inst.operands[1].imm;
10278                   narrow = TRUE;
10279                 }
10280               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
10281                 {
10282                   /* This means 1 register in reg list one of 3 situations:
10283                      1. Instruction is stmia, but without writeback.
10284                      2. lmdia without writeback, but with Rn not in
10285                         reglist.
10286                      3. ldmia with writeback, but with Rn in reglist.
10287                      Case 3 is UNPREDICTABLE behaviour, so we handle
10288                      case 1 and 2 which can be converted into a 16-bit
10289                      str or ldr. The SP cases are handled below.  */
10290                   unsigned long opcode;
10291                   /* First, record an error for Case 3.  */
10292                   if (inst.operands[1].imm & mask
10293                       && inst.operands[0].writeback)
10294                     inst.error = 
10295                         _("having the base register in the register list when "
10296                           "using write back is UNPREDICTABLE");
10297                     
10298                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str 
10299                                                              : T_MNEM_ldr);
10300                   inst.instruction = THUMB_OP16 (opcode);
10301                   inst.instruction |= inst.operands[0].reg << 3;
10302                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
10303                   narrow = TRUE;
10304                 }
10305             }
10306           else if (inst.operands[0] .reg == REG_SP)
10307             {
10308               if (inst.operands[0].writeback)
10309                 {
10310                   inst.instruction = 
10311                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
10312                                     ? T_MNEM_push : T_MNEM_pop);
10313                   inst.instruction |= inst.operands[1].imm;
10314                   narrow = TRUE;
10315                 }
10316               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
10317                 {
10318                   inst.instruction = 
10319                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
10320                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
10321                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
10322                   narrow = TRUE;
10323                 }
10324             }
10325         }
10326
10327       if (!narrow)
10328         {
10329           if (inst.instruction < 0xffff)
10330             inst.instruction = THUMB_OP32 (inst.instruction);
10331
10332           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
10333                                 inst.operands[0].writeback);
10334         }
10335     }
10336   else
10337     {
10338       constraint (inst.operands[0].reg > 7
10339                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
10340       constraint (inst.instruction != T_MNEM_ldmia
10341                   && inst.instruction != T_MNEM_stmia,
10342                   _("Thumb-2 instruction only valid in unified syntax"));
10343       if (inst.instruction == T_MNEM_stmia)
10344         {
10345           if (!inst.operands[0].writeback)
10346             as_warn (_("this instruction will write back the base register"));
10347           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
10348               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
10349             as_warn (_("value stored for r%d is UNKNOWN"),
10350                      inst.operands[0].reg);
10351         }
10352       else
10353         {
10354           if (!inst.operands[0].writeback
10355               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
10356             as_warn (_("this instruction will write back the base register"));
10357           else if (inst.operands[0].writeback
10358                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
10359             as_warn (_("this instruction will not write back the base register"));
10360         }
10361
10362       inst.instruction = THUMB_OP16 (inst.instruction);
10363       inst.instruction |= inst.operands[0].reg << 8;
10364       inst.instruction |= inst.operands[1].imm;
10365     }
10366 }
10367
10368 static void
10369 do_t_ldrex (void)
10370 {
10371   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
10372               || inst.operands[1].postind || inst.operands[1].writeback
10373               || inst.operands[1].immisreg || inst.operands[1].shifted
10374               || inst.operands[1].negative,
10375               BAD_ADDR_MODE);
10376
10377   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
10378
10379   inst.instruction |= inst.operands[0].reg << 12;
10380   inst.instruction |= inst.operands[1].reg << 16;
10381   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
10382 }
10383
10384 static void
10385 do_t_ldrexd (void)
10386 {
10387   if (!inst.operands[1].present)
10388     {
10389       constraint (inst.operands[0].reg == REG_LR,
10390                   _("r14 not allowed as first register "
10391                     "when second register is omitted"));
10392       inst.operands[1].reg = inst.operands[0].reg + 1;
10393     }
10394   constraint (inst.operands[0].reg == inst.operands[1].reg,
10395               BAD_OVERLAP);
10396
10397   inst.instruction |= inst.operands[0].reg << 12;
10398   inst.instruction |= inst.operands[1].reg << 8;
10399   inst.instruction |= inst.operands[2].reg << 16;
10400 }
10401
10402 static void
10403 do_t_ldst (void)
10404 {
10405   unsigned long opcode;
10406   int Rn;
10407
10408   if (inst.operands[0].isreg
10409       && !inst.operands[0].preind
10410       && inst.operands[0].reg == REG_PC)
10411     set_it_insn_type_last ();
10412
10413   opcode = inst.instruction;
10414   if (unified_syntax)
10415     {
10416       if (!inst.operands[1].isreg)
10417         {
10418           if (opcode <= 0xffff)
10419             inst.instruction = THUMB_OP32 (opcode);
10420           if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
10421             return;
10422         }
10423       if (inst.operands[1].isreg
10424           && !inst.operands[1].writeback
10425           && !inst.operands[1].shifted && !inst.operands[1].postind
10426           && !inst.operands[1].negative && inst.operands[0].reg <= 7
10427           && opcode <= 0xffff
10428           && inst.size_req != 4)
10429         {
10430           /* Insn may have a 16-bit form.  */
10431           Rn = inst.operands[1].reg;
10432           if (inst.operands[1].immisreg)
10433             {
10434               inst.instruction = THUMB_OP16 (opcode);
10435               /* [Rn, Rik] */
10436               if (Rn <= 7 && inst.operands[1].imm <= 7)
10437                 goto op16;
10438               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
10439                 reject_bad_reg (inst.operands[1].imm);
10440             }
10441           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
10442                     && opcode != T_MNEM_ldrsb)
10443                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
10444                    || (Rn == REG_SP && opcode == T_MNEM_str))
10445             {
10446               /* [Rn, #const] */
10447               if (Rn > 7)
10448                 {
10449                   if (Rn == REG_PC)
10450                     {
10451                       if (inst.reloc.pc_rel)
10452                         opcode = T_MNEM_ldr_pc2;
10453                       else
10454                         opcode = T_MNEM_ldr_pc;
10455                     }
10456                   else
10457                     {
10458                       if (opcode == T_MNEM_ldr)
10459                         opcode = T_MNEM_ldr_sp;
10460                       else
10461                         opcode = T_MNEM_str_sp;
10462                     }
10463                   inst.instruction = inst.operands[0].reg << 8;
10464                 }
10465               else
10466                 {
10467                   inst.instruction = inst.operands[0].reg;
10468                   inst.instruction |= inst.operands[1].reg << 3;
10469                 }
10470               inst.instruction |= THUMB_OP16 (opcode);
10471               if (inst.size_req == 2)
10472                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10473               else
10474                 inst.relax = opcode;
10475               return;
10476             }
10477         }
10478       /* Definitely a 32-bit variant.  */
10479
10480       /* Warning for Erratum 752419.  */
10481       if (opcode == T_MNEM_ldr
10482           && inst.operands[0].reg == REG_SP
10483           && inst.operands[1].writeback == 1
10484           && !inst.operands[1].immisreg)
10485         {
10486           if (no_cpu_selected ()
10487               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
10488                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
10489                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
10490             as_warn (_("This instruction may be unpredictable "
10491                        "if executed on M-profile cores "
10492                        "with interrupts enabled."));
10493         }
10494
10495       /* Do some validations regarding addressing modes.  */
10496       if (inst.operands[1].immisreg && opcode != T_MNEM_ldr
10497           && opcode != T_MNEM_str)
10498         reject_bad_reg (inst.operands[1].imm);
10499
10500       inst.instruction = THUMB_OP32 (opcode);
10501       inst.instruction |= inst.operands[0].reg << 12;
10502       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
10503       return;
10504     }
10505
10506   constraint (inst.operands[0].reg > 7, BAD_HIREG);
10507
10508   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
10509     {
10510       /* Only [Rn,Rm] is acceptable.  */
10511       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
10512       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
10513                   || inst.operands[1].postind || inst.operands[1].shifted
10514                   || inst.operands[1].negative,
10515                   _("Thumb does not support this addressing mode"));
10516       inst.instruction = THUMB_OP16 (inst.instruction);
10517       goto op16;
10518     }
10519
10520   inst.instruction = THUMB_OP16 (inst.instruction);
10521   if (!inst.operands[1].isreg)
10522     if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
10523       return;
10524
10525   constraint (!inst.operands[1].preind
10526               || inst.operands[1].shifted
10527               || inst.operands[1].writeback,
10528               _("Thumb does not support this addressing mode"));
10529   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
10530     {
10531       constraint (inst.instruction & 0x0600,
10532                   _("byte or halfword not valid for base register"));
10533       constraint (inst.operands[1].reg == REG_PC
10534                   && !(inst.instruction & THUMB_LOAD_BIT),
10535                   _("r15 based store not allowed"));
10536       constraint (inst.operands[1].immisreg,
10537                   _("invalid base register for register offset"));
10538
10539       if (inst.operands[1].reg == REG_PC)
10540         inst.instruction = T_OPCODE_LDR_PC;
10541       else if (inst.instruction & THUMB_LOAD_BIT)
10542         inst.instruction = T_OPCODE_LDR_SP;
10543       else
10544         inst.instruction = T_OPCODE_STR_SP;
10545
10546       inst.instruction |= inst.operands[0].reg << 8;
10547       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10548       return;
10549     }
10550
10551   constraint (inst.operands[1].reg > 7, BAD_HIREG);
10552   if (!inst.operands[1].immisreg)
10553     {
10554       /* Immediate offset.  */
10555       inst.instruction |= inst.operands[0].reg;
10556       inst.instruction |= inst.operands[1].reg << 3;
10557       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
10558       return;
10559     }
10560
10561   /* Register offset.  */
10562   constraint (inst.operands[1].imm > 7, BAD_HIREG);
10563   constraint (inst.operands[1].negative,
10564               _("Thumb does not support this addressing mode"));
10565
10566  op16:
10567   switch (inst.instruction)
10568     {
10569     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
10570     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
10571     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
10572     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
10573     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
10574     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
10575     case 0x5600 /* ldrsb */:
10576     case 0x5e00 /* ldrsh */: break;
10577     default: abort ();
10578     }
10579
10580   inst.instruction |= inst.operands[0].reg;
10581   inst.instruction |= inst.operands[1].reg << 3;
10582   inst.instruction |= inst.operands[1].imm << 6;
10583 }
10584
10585 static void
10586 do_t_ldstd (void)
10587 {
10588   if (!inst.operands[1].present)
10589     {
10590       inst.operands[1].reg = inst.operands[0].reg + 1;
10591       constraint (inst.operands[0].reg == REG_LR,
10592                   _("r14 not allowed here"));
10593     }
10594   inst.instruction |= inst.operands[0].reg << 12;
10595   inst.instruction |= inst.operands[1].reg << 8;
10596   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
10597 }
10598
10599 static void
10600 do_t_ldstt (void)
10601 {
10602   inst.instruction |= inst.operands[0].reg << 12;
10603   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
10604 }
10605
10606 static void
10607 do_t_mla (void)
10608 {
10609   unsigned Rd, Rn, Rm, Ra;
10610
10611   Rd = inst.operands[0].reg;
10612   Rn = inst.operands[1].reg;
10613   Rm = inst.operands[2].reg;
10614   Ra = inst.operands[3].reg;
10615
10616   reject_bad_reg (Rd);
10617   reject_bad_reg (Rn);
10618   reject_bad_reg (Rm);
10619   reject_bad_reg (Ra);
10620
10621   inst.instruction |= Rd << 8;
10622   inst.instruction |= Rn << 16;
10623   inst.instruction |= Rm;
10624   inst.instruction |= Ra << 12;
10625 }
10626
10627 static void
10628 do_t_mlal (void)
10629 {
10630   unsigned RdLo, RdHi, Rn, Rm;
10631
10632   RdLo = inst.operands[0].reg;
10633   RdHi = inst.operands[1].reg;
10634   Rn = inst.operands[2].reg;
10635   Rm = inst.operands[3].reg;
10636
10637   reject_bad_reg (RdLo);
10638   reject_bad_reg (RdHi);
10639   reject_bad_reg (Rn);
10640   reject_bad_reg (Rm);
10641
10642   inst.instruction |= RdLo << 12;
10643   inst.instruction |= RdHi << 8;
10644   inst.instruction |= Rn << 16;
10645   inst.instruction |= Rm;
10646 }
10647
10648 static void
10649 do_t_mov_cmp (void)
10650 {
10651   unsigned Rn, Rm;
10652
10653   Rn = inst.operands[0].reg;
10654   Rm = inst.operands[1].reg;
10655
10656   if (Rn == REG_PC)
10657     set_it_insn_type_last ();
10658
10659   if (unified_syntax)
10660     {
10661       int r0off = (inst.instruction == T_MNEM_mov
10662                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
10663       unsigned long opcode;
10664       bfd_boolean narrow;
10665       bfd_boolean low_regs;
10666
10667       low_regs = (Rn <= 7 && Rm <= 7);
10668       opcode = inst.instruction;
10669       if (in_it_block ())
10670         narrow = opcode != T_MNEM_movs;
10671       else
10672         narrow = opcode != T_MNEM_movs || low_regs;
10673       if (inst.size_req == 4
10674           || inst.operands[1].shifted)
10675         narrow = FALSE;
10676
10677       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
10678       if (opcode == T_MNEM_movs && inst.operands[1].isreg
10679           && !inst.operands[1].shifted
10680           && Rn == REG_PC
10681           && Rm == REG_LR)
10682         {
10683           inst.instruction = T2_SUBS_PC_LR;
10684           return;
10685         }
10686
10687       if (opcode == T_MNEM_cmp)
10688         {
10689           constraint (Rn == REG_PC, BAD_PC);
10690           if (narrow)
10691             {
10692               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
10693                  but valid.  */
10694               warn_deprecated_sp (Rm);
10695               /* R15 was documented as a valid choice for Rm in ARMv6,
10696                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
10697                  tools reject R15, so we do too.  */
10698               constraint (Rm == REG_PC, BAD_PC);
10699             }
10700           else
10701             reject_bad_reg (Rm);
10702         }
10703       else if (opcode == T_MNEM_mov
10704                || opcode == T_MNEM_movs)
10705         {
10706           if (inst.operands[1].isreg)
10707             {
10708               if (opcode == T_MNEM_movs)
10709                 {
10710                   reject_bad_reg (Rn);
10711                   reject_bad_reg (Rm);
10712                 }
10713               else if (narrow)
10714                 {
10715                   /* This is mov.n.  */
10716                   if ((Rn == REG_SP || Rn == REG_PC)
10717                       && (Rm == REG_SP || Rm == REG_PC))
10718                     {
10719                       as_warn (_("Use of r%u as a source register is "
10720                                  "deprecated when r%u is the destination "
10721                                  "register."), Rm, Rn);
10722                     }
10723                 }
10724               else
10725                 {
10726                   /* This is mov.w.  */
10727                   constraint (Rn == REG_PC, BAD_PC);
10728                   constraint (Rm == REG_PC, BAD_PC);
10729                   constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
10730                 }
10731             }
10732           else
10733             reject_bad_reg (Rn);
10734         }
10735
10736       if (!inst.operands[1].isreg)
10737         {
10738           /* Immediate operand.  */
10739           if (!in_it_block () && opcode == T_MNEM_mov)
10740             narrow = 0;
10741           if (low_regs && narrow)
10742             {
10743               inst.instruction = THUMB_OP16 (opcode);
10744               inst.instruction |= Rn << 8;
10745               if (inst.size_req == 2)
10746                 inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
10747               else
10748                 inst.relax = opcode;
10749             }
10750           else
10751             {
10752               inst.instruction = THUMB_OP32 (inst.instruction);
10753               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10754               inst.instruction |= Rn << r0off;
10755               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10756             }
10757         }
10758       else if (inst.operands[1].shifted && inst.operands[1].immisreg
10759                && (inst.instruction == T_MNEM_mov
10760                    || inst.instruction == T_MNEM_movs))
10761         {
10762           /* Register shifts are encoded as separate shift instructions.  */
10763           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
10764
10765           if (in_it_block ())
10766             narrow = !flags;
10767           else
10768             narrow = flags;
10769
10770           if (inst.size_req == 4)
10771             narrow = FALSE;
10772
10773           if (!low_regs || inst.operands[1].imm > 7)
10774             narrow = FALSE;
10775
10776           if (Rn != Rm)
10777             narrow = FALSE;
10778
10779           switch (inst.operands[1].shift_kind)
10780             {
10781             case SHIFT_LSL:
10782               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
10783               break;
10784             case SHIFT_ASR:
10785               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
10786               break;
10787             case SHIFT_LSR:
10788               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
10789               break;
10790             case SHIFT_ROR:
10791               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
10792               break;
10793             default:
10794               abort ();
10795             }
10796
10797           inst.instruction = opcode;
10798           if (narrow)
10799             {
10800               inst.instruction |= Rn;
10801               inst.instruction |= inst.operands[1].imm << 3;
10802             }
10803           else
10804             {
10805               if (flags)
10806                 inst.instruction |= CONDS_BIT;
10807
10808               inst.instruction |= Rn << 8;
10809               inst.instruction |= Rm << 16;
10810               inst.instruction |= inst.operands[1].imm;
10811             }
10812         }
10813       else if (!narrow)
10814         {
10815           /* Some mov with immediate shift have narrow variants.
10816              Register shifts are handled above.  */
10817           if (low_regs && inst.operands[1].shifted
10818               && (inst.instruction == T_MNEM_mov
10819                   || inst.instruction == T_MNEM_movs))
10820             {
10821               if (in_it_block ())
10822                 narrow = (inst.instruction == T_MNEM_mov);
10823               else
10824                 narrow = (inst.instruction == T_MNEM_movs);
10825             }
10826
10827           if (narrow)
10828             {
10829               switch (inst.operands[1].shift_kind)
10830                 {
10831                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
10832                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
10833                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
10834                 default: narrow = FALSE; break;
10835                 }
10836             }
10837
10838           if (narrow)
10839             {
10840               inst.instruction |= Rn;
10841               inst.instruction |= Rm << 3;
10842               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
10843             }
10844           else
10845             {
10846               inst.instruction = THUMB_OP32 (inst.instruction);
10847               inst.instruction |= Rn << r0off;
10848               encode_thumb32_shifted_operand (1);
10849             }
10850         }
10851       else
10852         switch (inst.instruction)
10853           {
10854           case T_MNEM_mov:
10855             inst.instruction = T_OPCODE_MOV_HR;
10856             inst.instruction |= (Rn & 0x8) << 4;
10857             inst.instruction |= (Rn & 0x7);
10858             inst.instruction |= Rm << 3;
10859             break;
10860
10861           case T_MNEM_movs:
10862             /* We know we have low registers at this point.
10863                Generate LSLS Rd, Rs, #0.  */
10864             inst.instruction = T_OPCODE_LSL_I;
10865             inst.instruction |= Rn;
10866             inst.instruction |= Rm << 3;
10867             break;
10868
10869           case T_MNEM_cmp:
10870             if (low_regs)
10871               {
10872                 inst.instruction = T_OPCODE_CMP_LR;
10873                 inst.instruction |= Rn;
10874                 inst.instruction |= Rm << 3;
10875               }
10876             else
10877               {
10878                 inst.instruction = T_OPCODE_CMP_HR;
10879                 inst.instruction |= (Rn & 0x8) << 4;
10880                 inst.instruction |= (Rn & 0x7);
10881                 inst.instruction |= Rm << 3;
10882               }
10883             break;
10884           }
10885       return;
10886     }
10887
10888   inst.instruction = THUMB_OP16 (inst.instruction);
10889
10890   /* PR 10443: Do not silently ignore shifted operands.  */
10891   constraint (inst.operands[1].shifted,
10892               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
10893
10894   if (inst.operands[1].isreg)
10895     {
10896       if (Rn < 8 && Rm < 8)
10897         {
10898           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
10899              since a MOV instruction produces unpredictable results.  */
10900           if (inst.instruction == T_OPCODE_MOV_I8)
10901             inst.instruction = T_OPCODE_ADD_I3;
10902           else
10903             inst.instruction = T_OPCODE_CMP_LR;
10904
10905           inst.instruction |= Rn;
10906           inst.instruction |= Rm << 3;
10907         }
10908       else
10909         {
10910           if (inst.instruction == T_OPCODE_MOV_I8)
10911             inst.instruction = T_OPCODE_MOV_HR;
10912           else
10913             inst.instruction = T_OPCODE_CMP_HR;
10914           do_t_cpy ();
10915         }
10916     }
10917   else
10918     {
10919       constraint (Rn > 7,
10920                   _("only lo regs allowed with immediate"));
10921       inst.instruction |= Rn << 8;
10922       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
10923     }
10924 }
10925
10926 static void
10927 do_t_mov16 (void)
10928 {
10929   unsigned Rd;
10930   bfd_vma imm;
10931   bfd_boolean top;
10932
10933   top = (inst.instruction & 0x00800000) != 0;
10934   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
10935     {
10936       constraint (top, _(":lower16: not allowed this instruction"));
10937       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
10938     }
10939   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
10940     {
10941       constraint (!top, _(":upper16: not allowed this instruction"));
10942       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
10943     }
10944
10945   Rd = inst.operands[0].reg;
10946   reject_bad_reg (Rd);
10947
10948   inst.instruction |= Rd << 8;
10949   if (inst.reloc.type == BFD_RELOC_UNUSED)
10950     {
10951       imm = inst.reloc.exp.X_add_number;
10952       inst.instruction |= (imm & 0xf000) << 4;
10953       inst.instruction |= (imm & 0x0800) << 15;
10954       inst.instruction |= (imm & 0x0700) << 4;
10955       inst.instruction |= (imm & 0x00ff);
10956     }
10957 }
10958
10959 static void
10960 do_t_mvn_tst (void)
10961 {
10962   unsigned Rn, Rm;
10963
10964   Rn = inst.operands[0].reg;
10965   Rm = inst.operands[1].reg;
10966
10967   if (inst.instruction == T_MNEM_cmp
10968       || inst.instruction == T_MNEM_cmn)
10969     constraint (Rn == REG_PC, BAD_PC);
10970   else
10971     reject_bad_reg (Rn);
10972   reject_bad_reg (Rm);
10973
10974   if (unified_syntax)
10975     {
10976       int r0off = (inst.instruction == T_MNEM_mvn
10977                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
10978       bfd_boolean narrow;
10979
10980       if (inst.size_req == 4
10981           || inst.instruction > 0xffff
10982           || inst.operands[1].shifted
10983           || Rn > 7 || Rm > 7)
10984         narrow = FALSE;
10985       else if (inst.instruction == T_MNEM_cmn)
10986         narrow = TRUE;
10987       else if (THUMB_SETS_FLAGS (inst.instruction))
10988         narrow = !in_it_block ();
10989       else
10990         narrow = in_it_block ();
10991
10992       if (!inst.operands[1].isreg)
10993         {
10994           /* For an immediate, we always generate a 32-bit opcode;
10995              section relaxation will shrink it later if possible.  */
10996           if (inst.instruction < 0xffff)
10997             inst.instruction = THUMB_OP32 (inst.instruction);
10998           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10999           inst.instruction |= Rn << r0off;
11000           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11001         }
11002       else
11003         {
11004           /* See if we can do this with a 16-bit instruction.  */
11005           if (narrow)
11006             {
11007               inst.instruction = THUMB_OP16 (inst.instruction);
11008               inst.instruction |= Rn;
11009               inst.instruction |= Rm << 3;
11010             }
11011           else
11012             {
11013               constraint (inst.operands[1].shifted
11014                           && inst.operands[1].immisreg,
11015                           _("shift must be constant"));
11016               if (inst.instruction < 0xffff)
11017                 inst.instruction = THUMB_OP32 (inst.instruction);
11018               inst.instruction |= Rn << r0off;
11019               encode_thumb32_shifted_operand (1);
11020             }
11021         }
11022     }
11023   else
11024     {
11025       constraint (inst.instruction > 0xffff
11026                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
11027       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
11028                   _("unshifted register required"));
11029       constraint (Rn > 7 || Rm > 7,
11030                   BAD_HIREG);
11031
11032       inst.instruction = THUMB_OP16 (inst.instruction);
11033       inst.instruction |= Rn;
11034       inst.instruction |= Rm << 3;
11035     }
11036 }
11037
11038 static void
11039 do_t_mrs (void)
11040 {
11041   unsigned Rd;
11042
11043   if (do_vfp_nsyn_mrs () == SUCCESS)
11044     return;
11045
11046   Rd = inst.operands[0].reg;
11047   reject_bad_reg (Rd);
11048   inst.instruction |= Rd << 8;
11049
11050   if (inst.operands[1].isreg)
11051     {
11052       unsigned br = inst.operands[1].reg;
11053       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
11054         as_bad (_("bad register for mrs"));
11055
11056       inst.instruction |= br & (0xf << 16);
11057       inst.instruction |= (br & 0x300) >> 4;
11058       inst.instruction |= (br & SPSR_BIT) >> 2;
11059     }
11060   else
11061     {
11062       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
11063
11064       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
11065         constraint (flags != 0, _("selected processor does not support "
11066                     "requested special purpose register"));
11067       else
11068         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
11069            devices).  */
11070         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
11071                     _("'APSR', 'CPSR' or 'SPSR' expected"));
11072
11073       inst.instruction |= (flags & SPSR_BIT) >> 2;
11074       inst.instruction |= inst.operands[1].imm & 0xff;
11075       inst.instruction |= 0xf0000;
11076     }
11077 }
11078
11079 static void
11080 do_t_msr (void)
11081 {
11082   int flags;
11083   unsigned Rn;
11084
11085   if (do_vfp_nsyn_msr () == SUCCESS)
11086     return;
11087
11088   constraint (!inst.operands[1].isreg,
11089               _("Thumb encoding does not support an immediate here"));
11090
11091   if (inst.operands[0].isreg)
11092     flags = (int)(inst.operands[0].reg);
11093   else
11094     flags = inst.operands[0].imm;
11095
11096   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
11097     {
11098       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
11099
11100       constraint ((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
11101                    && (bits & ~(PSR_s | PSR_f)) != 0)
11102                   || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
11103                       && bits != PSR_f),
11104                   _("selected processor does not support requested special "
11105                     "purpose register"));
11106     }
11107   else
11108      constraint ((flags & 0xff) != 0, _("selected processor does not support "
11109                  "requested special purpose register"));
11110
11111   Rn = inst.operands[1].reg;
11112   reject_bad_reg (Rn);
11113
11114   inst.instruction |= (flags & SPSR_BIT) >> 2;
11115   inst.instruction |= (flags & 0xf0000) >> 8;
11116   inst.instruction |= (flags & 0x300) >> 4;
11117   inst.instruction |= (flags & 0xff);
11118   inst.instruction |= Rn << 16;
11119 }
11120
11121 static void
11122 do_t_mul (void)
11123 {
11124   bfd_boolean narrow;
11125   unsigned Rd, Rn, Rm;
11126
11127   if (!inst.operands[2].present)
11128     inst.operands[2].reg = inst.operands[0].reg;
11129
11130   Rd = inst.operands[0].reg;
11131   Rn = inst.operands[1].reg;
11132   Rm = inst.operands[2].reg;
11133
11134   if (unified_syntax)
11135     {
11136       if (inst.size_req == 4
11137           || (Rd != Rn
11138               && Rd != Rm)
11139           || Rn > 7
11140           || Rm > 7)
11141         narrow = FALSE;
11142       else if (inst.instruction == T_MNEM_muls)
11143         narrow = !in_it_block ();
11144       else
11145         narrow = in_it_block ();
11146     }
11147   else
11148     {
11149       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
11150       constraint (Rn > 7 || Rm > 7,
11151                   BAD_HIREG);
11152       narrow = TRUE;
11153     }
11154
11155   if (narrow)
11156     {
11157       /* 16-bit MULS/Conditional MUL.  */
11158       inst.instruction = THUMB_OP16 (inst.instruction);
11159       inst.instruction |= Rd;
11160
11161       if (Rd == Rn)
11162         inst.instruction |= Rm << 3;
11163       else if (Rd == Rm)
11164         inst.instruction |= Rn << 3;
11165       else
11166         constraint (1, _("dest must overlap one source register"));
11167     }
11168   else
11169     {
11170       constraint (inst.instruction != T_MNEM_mul,
11171                   _("Thumb-2 MUL must not set flags"));
11172       /* 32-bit MUL.  */
11173       inst.instruction = THUMB_OP32 (inst.instruction);
11174       inst.instruction |= Rd << 8;
11175       inst.instruction |= Rn << 16;
11176       inst.instruction |= Rm << 0;
11177
11178       reject_bad_reg (Rd);
11179       reject_bad_reg (Rn);
11180       reject_bad_reg (Rm);
11181     }
11182 }
11183
11184 static void
11185 do_t_mull (void)
11186 {
11187   unsigned RdLo, RdHi, Rn, Rm;
11188
11189   RdLo = inst.operands[0].reg;
11190   RdHi = inst.operands[1].reg;
11191   Rn = inst.operands[2].reg;
11192   Rm = inst.operands[3].reg;
11193
11194   reject_bad_reg (RdLo);
11195   reject_bad_reg (RdHi);
11196   reject_bad_reg (Rn);
11197   reject_bad_reg (Rm);
11198
11199   inst.instruction |= RdLo << 12;
11200   inst.instruction |= RdHi << 8;
11201   inst.instruction |= Rn << 16;
11202   inst.instruction |= Rm;
11203
11204  if (RdLo == RdHi)
11205     as_tsktsk (_("rdhi and rdlo must be different"));
11206 }
11207
11208 static void
11209 do_t_nop (void)
11210 {
11211   set_it_insn_type (NEUTRAL_IT_INSN);
11212
11213   if (unified_syntax)
11214     {
11215       if (inst.size_req == 4 || inst.operands[0].imm > 15)
11216         {
11217           inst.instruction = THUMB_OP32 (inst.instruction);
11218           inst.instruction |= inst.operands[0].imm;
11219         }
11220       else
11221         {
11222           /* PR9722: Check for Thumb2 availability before
11223              generating a thumb2 nop instruction.  */
11224           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
11225             {
11226               inst.instruction = THUMB_OP16 (inst.instruction);
11227               inst.instruction |= inst.operands[0].imm << 4;
11228             }
11229           else
11230             inst.instruction = 0x46c0;
11231         }
11232     }
11233   else
11234     {
11235       constraint (inst.operands[0].present,
11236                   _("Thumb does not support NOP with hints"));
11237       inst.instruction = 0x46c0;
11238     }
11239 }
11240
11241 static void
11242 do_t_neg (void)
11243 {
11244   if (unified_syntax)
11245     {
11246       bfd_boolean narrow;
11247
11248       if (THUMB_SETS_FLAGS (inst.instruction))
11249         narrow = !in_it_block ();
11250       else
11251         narrow = in_it_block ();
11252       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
11253         narrow = FALSE;
11254       if (inst.size_req == 4)
11255         narrow = FALSE;
11256
11257       if (!narrow)
11258         {
11259           inst.instruction = THUMB_OP32 (inst.instruction);
11260           inst.instruction |= inst.operands[0].reg << 8;
11261           inst.instruction |= inst.operands[1].reg << 16;
11262         }
11263       else
11264         {
11265           inst.instruction = THUMB_OP16 (inst.instruction);
11266           inst.instruction |= inst.operands[0].reg;
11267           inst.instruction |= inst.operands[1].reg << 3;
11268         }
11269     }
11270   else
11271     {
11272       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
11273                   BAD_HIREG);
11274       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11275
11276       inst.instruction = THUMB_OP16 (inst.instruction);
11277       inst.instruction |= inst.operands[0].reg;
11278       inst.instruction |= inst.operands[1].reg << 3;
11279     }
11280 }
11281
11282 static void
11283 do_t_orn (void)
11284 {
11285   unsigned Rd, Rn;
11286
11287   Rd = inst.operands[0].reg;
11288   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
11289
11290   reject_bad_reg (Rd);
11291   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
11292   reject_bad_reg (Rn);
11293
11294   inst.instruction |= Rd << 8;
11295   inst.instruction |= Rn << 16;
11296
11297   if (!inst.operands[2].isreg)
11298     {
11299       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11300       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11301     }
11302   else
11303     {
11304       unsigned Rm;
11305
11306       Rm = inst.operands[2].reg;
11307       reject_bad_reg (Rm);
11308
11309       constraint (inst.operands[2].shifted
11310                   && inst.operands[2].immisreg,
11311                   _("shift must be constant"));
11312       encode_thumb32_shifted_operand (2);
11313     }
11314 }
11315
11316 static void
11317 do_t_pkhbt (void)
11318 {
11319   unsigned Rd, Rn, Rm;
11320
11321   Rd = inst.operands[0].reg;
11322   Rn = inst.operands[1].reg;
11323   Rm = inst.operands[2].reg;
11324
11325   reject_bad_reg (Rd);
11326   reject_bad_reg (Rn);
11327   reject_bad_reg (Rm);
11328
11329   inst.instruction |= Rd << 8;
11330   inst.instruction |= Rn << 16;
11331   inst.instruction |= Rm;
11332   if (inst.operands[3].present)
11333     {
11334       unsigned int val = inst.reloc.exp.X_add_number;
11335       constraint (inst.reloc.exp.X_op != O_constant,
11336                   _("expression too complex"));
11337       inst.instruction |= (val & 0x1c) << 10;
11338       inst.instruction |= (val & 0x03) << 6;
11339     }
11340 }
11341
11342 static void
11343 do_t_pkhtb (void)
11344 {
11345   if (!inst.operands[3].present)
11346     {
11347       unsigned Rtmp;
11348
11349       inst.instruction &= ~0x00000020;
11350
11351       /* PR 10168.  Swap the Rm and Rn registers.  */
11352       Rtmp = inst.operands[1].reg;
11353       inst.operands[1].reg = inst.operands[2].reg;
11354       inst.operands[2].reg = Rtmp;
11355     }
11356   do_t_pkhbt ();
11357 }
11358
11359 static void
11360 do_t_pld (void)
11361 {
11362   if (inst.operands[0].immisreg)
11363     reject_bad_reg (inst.operands[0].imm);
11364
11365   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
11366 }
11367
11368 static void
11369 do_t_push_pop (void)
11370 {
11371   unsigned mask;
11372
11373   constraint (inst.operands[0].writeback,
11374               _("push/pop do not support {reglist}^"));
11375   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11376               _("expression too complex"));
11377
11378   mask = inst.operands[0].imm;
11379   if ((mask & ~0xff) == 0)
11380     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
11381   else if ((inst.instruction == T_MNEM_push
11382             && (mask & ~0xff) == 1 << REG_LR)
11383            || (inst.instruction == T_MNEM_pop
11384                && (mask & ~0xff) == 1 << REG_PC))
11385     {
11386       inst.instruction = THUMB_OP16 (inst.instruction);
11387       inst.instruction |= THUMB_PP_PC_LR;
11388       inst.instruction |= mask & 0xff;
11389     }
11390   else if (unified_syntax)
11391     {
11392       inst.instruction = THUMB_OP32 (inst.instruction);
11393       encode_thumb2_ldmstm (13, mask, TRUE);
11394     }
11395   else
11396     {
11397       inst.error = _("invalid register list to push/pop instruction");
11398       return;
11399     }
11400 }
11401
11402 static void
11403 do_t_rbit (void)
11404 {
11405   unsigned Rd, Rm;
11406
11407   Rd = inst.operands[0].reg;
11408   Rm = inst.operands[1].reg;
11409
11410   reject_bad_reg (Rd);
11411   reject_bad_reg (Rm);
11412
11413   inst.instruction |= Rd << 8;
11414   inst.instruction |= Rm << 16;
11415   inst.instruction |= Rm;
11416 }
11417
11418 static void
11419 do_t_rev (void)
11420 {
11421   unsigned Rd, Rm;
11422
11423   Rd = inst.operands[0].reg;
11424   Rm = inst.operands[1].reg;
11425
11426   reject_bad_reg (Rd);
11427   reject_bad_reg (Rm);
11428
11429   if (Rd <= 7 && Rm <= 7
11430       && inst.size_req != 4)
11431     {
11432       inst.instruction = THUMB_OP16 (inst.instruction);
11433       inst.instruction |= Rd;
11434       inst.instruction |= Rm << 3;
11435     }
11436   else if (unified_syntax)
11437     {
11438       inst.instruction = THUMB_OP32 (inst.instruction);
11439       inst.instruction |= Rd << 8;
11440       inst.instruction |= Rm << 16;
11441       inst.instruction |= Rm;
11442     }
11443   else
11444     inst.error = BAD_HIREG;
11445 }
11446
11447 static void
11448 do_t_rrx (void)
11449 {
11450   unsigned Rd, Rm;
11451
11452   Rd = inst.operands[0].reg;
11453   Rm = inst.operands[1].reg;
11454
11455   reject_bad_reg (Rd);
11456   reject_bad_reg (Rm);
11457
11458   inst.instruction |= Rd << 8;
11459   inst.instruction |= Rm;
11460 }
11461
11462 static void
11463 do_t_rsb (void)
11464 {
11465   unsigned Rd, Rs;
11466
11467   Rd = inst.operands[0].reg;
11468   Rs = (inst.operands[1].present
11469         ? inst.operands[1].reg    /* Rd, Rs, foo */
11470         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11471
11472   reject_bad_reg (Rd);
11473   reject_bad_reg (Rs);
11474   if (inst.operands[2].isreg)
11475     reject_bad_reg (inst.operands[2].reg);
11476
11477   inst.instruction |= Rd << 8;
11478   inst.instruction |= Rs << 16;
11479   if (!inst.operands[2].isreg)
11480     {
11481       bfd_boolean narrow;
11482
11483       if ((inst.instruction & 0x00100000) != 0)
11484         narrow = !in_it_block ();
11485       else
11486         narrow = in_it_block ();
11487
11488       if (Rd > 7 || Rs > 7)
11489         narrow = FALSE;
11490
11491       if (inst.size_req == 4 || !unified_syntax)
11492         narrow = FALSE;
11493
11494       if (inst.reloc.exp.X_op != O_constant
11495           || inst.reloc.exp.X_add_number != 0)
11496         narrow = FALSE;
11497
11498       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
11499          relaxation, but it doesn't seem worth the hassle.  */
11500       if (narrow)
11501         {
11502           inst.reloc.type = BFD_RELOC_UNUSED;
11503           inst.instruction = THUMB_OP16 (T_MNEM_negs);
11504           inst.instruction |= Rs << 3;
11505           inst.instruction |= Rd;
11506         }
11507       else
11508         {
11509           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11510           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11511         }
11512     }
11513   else
11514     encode_thumb32_shifted_operand (2);
11515 }
11516
11517 static void
11518 do_t_setend (void)
11519 {
11520   set_it_insn_type (OUTSIDE_IT_INSN);
11521   if (inst.operands[0].imm)
11522     inst.instruction |= 0x8;
11523 }
11524
11525 static void
11526 do_t_shift (void)
11527 {
11528   if (!inst.operands[1].present)
11529     inst.operands[1].reg = inst.operands[0].reg;
11530
11531   if (unified_syntax)
11532     {
11533       bfd_boolean narrow;
11534       int shift_kind;
11535
11536       switch (inst.instruction)
11537         {
11538         case T_MNEM_asr:
11539         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
11540         case T_MNEM_lsl:
11541         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
11542         case T_MNEM_lsr:
11543         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
11544         case T_MNEM_ror:
11545         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
11546         default: abort ();
11547         }
11548
11549       if (THUMB_SETS_FLAGS (inst.instruction))
11550         narrow = !in_it_block ();
11551       else
11552         narrow = in_it_block ();
11553       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
11554         narrow = FALSE;
11555       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
11556         narrow = FALSE;
11557       if (inst.operands[2].isreg
11558           && (inst.operands[1].reg != inst.operands[0].reg
11559               || inst.operands[2].reg > 7))
11560         narrow = FALSE;
11561       if (inst.size_req == 4)
11562         narrow = FALSE;
11563
11564       reject_bad_reg (inst.operands[0].reg);
11565       reject_bad_reg (inst.operands[1].reg);
11566
11567       if (!narrow)
11568         {
11569           if (inst.operands[2].isreg)
11570             {
11571               reject_bad_reg (inst.operands[2].reg);
11572               inst.instruction = THUMB_OP32 (inst.instruction);
11573               inst.instruction |= inst.operands[0].reg << 8;
11574               inst.instruction |= inst.operands[1].reg << 16;
11575               inst.instruction |= inst.operands[2].reg;
11576
11577               /* PR 12854: Error on extraneous shifts.  */
11578               constraint (inst.operands[2].shifted,
11579                           _("extraneous shift as part of operand to shift insn"));
11580             }
11581           else
11582             {
11583               inst.operands[1].shifted = 1;
11584               inst.operands[1].shift_kind = shift_kind;
11585               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
11586                                              ? T_MNEM_movs : T_MNEM_mov);
11587               inst.instruction |= inst.operands[0].reg << 8;
11588               encode_thumb32_shifted_operand (1);
11589               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
11590               inst.reloc.type = BFD_RELOC_UNUSED;
11591             }
11592         }
11593       else
11594         {
11595           if (inst.operands[2].isreg)
11596             {
11597               switch (shift_kind)
11598                 {
11599                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
11600                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
11601                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
11602                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
11603                 default: abort ();
11604                 }
11605
11606               inst.instruction |= inst.operands[0].reg;
11607               inst.instruction |= inst.operands[2].reg << 3;
11608
11609               /* PR 12854: Error on extraneous shifts.  */
11610               constraint (inst.operands[2].shifted,
11611                           _("extraneous shift as part of operand to shift insn"));
11612             }
11613           else
11614             {
11615               switch (shift_kind)
11616                 {
11617                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
11618                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
11619                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
11620                 default: abort ();
11621                 }
11622               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11623               inst.instruction |= inst.operands[0].reg;
11624               inst.instruction |= inst.operands[1].reg << 3;
11625             }
11626         }
11627     }
11628   else
11629     {
11630       constraint (inst.operands[0].reg > 7
11631                   || inst.operands[1].reg > 7, BAD_HIREG);
11632       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11633
11634       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
11635         {
11636           constraint (inst.operands[2].reg > 7, BAD_HIREG);
11637           constraint (inst.operands[0].reg != inst.operands[1].reg,
11638                       _("source1 and dest must be same register"));
11639
11640           switch (inst.instruction)
11641             {
11642             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
11643             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
11644             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
11645             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
11646             default: abort ();
11647             }
11648
11649           inst.instruction |= inst.operands[0].reg;
11650           inst.instruction |= inst.operands[2].reg << 3;
11651
11652           /* PR 12854: Error on extraneous shifts.  */
11653           constraint (inst.operands[2].shifted,
11654                       _("extraneous shift as part of operand to shift insn"));
11655         }
11656       else
11657         {
11658           switch (inst.instruction)
11659             {
11660             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
11661             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
11662             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
11663             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
11664             default: abort ();
11665             }
11666           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11667           inst.instruction |= inst.operands[0].reg;
11668           inst.instruction |= inst.operands[1].reg << 3;
11669         }
11670     }
11671 }
11672
11673 static void
11674 do_t_simd (void)
11675 {
11676   unsigned Rd, Rn, Rm;
11677
11678   Rd = inst.operands[0].reg;
11679   Rn = inst.operands[1].reg;
11680   Rm = inst.operands[2].reg;
11681
11682   reject_bad_reg (Rd);
11683   reject_bad_reg (Rn);
11684   reject_bad_reg (Rm);
11685
11686   inst.instruction |= Rd << 8;
11687   inst.instruction |= Rn << 16;
11688   inst.instruction |= Rm;
11689 }
11690
11691 static void
11692 do_t_simd2 (void)
11693 {
11694   unsigned Rd, Rn, Rm;
11695
11696   Rd = inst.operands[0].reg;
11697   Rm = inst.operands[1].reg;
11698   Rn = inst.operands[2].reg;
11699
11700   reject_bad_reg (Rd);
11701   reject_bad_reg (Rn);
11702   reject_bad_reg (Rm);
11703
11704   inst.instruction |= Rd << 8;
11705   inst.instruction |= Rn << 16;
11706   inst.instruction |= Rm;
11707 }
11708
11709 static void
11710 do_t_smc (void)
11711 {
11712   unsigned int value = inst.reloc.exp.X_add_number;
11713   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
11714               _("SMC is not permitted on this architecture"));
11715   constraint (inst.reloc.exp.X_op != O_constant,
11716               _("expression too complex"));
11717   inst.reloc.type = BFD_RELOC_UNUSED;
11718   inst.instruction |= (value & 0xf000) >> 12;
11719   inst.instruction |= (value & 0x0ff0);
11720   inst.instruction |= (value & 0x000f) << 16;
11721 }
11722
11723 static void
11724 do_t_hvc (void)
11725 {
11726   unsigned int value = inst.reloc.exp.X_add_number;
11727
11728   inst.reloc.type = BFD_RELOC_UNUSED;
11729   inst.instruction |= (value & 0x0fff);
11730   inst.instruction |= (value & 0xf000) << 4;
11731 }
11732
11733 static void
11734 do_t_ssat_usat (int bias)
11735 {
11736   unsigned Rd, Rn;
11737
11738   Rd = inst.operands[0].reg;
11739   Rn = inst.operands[2].reg;
11740
11741   reject_bad_reg (Rd);
11742   reject_bad_reg (Rn);
11743
11744   inst.instruction |= Rd << 8;
11745   inst.instruction |= inst.operands[1].imm - bias;
11746   inst.instruction |= Rn << 16;
11747
11748   if (inst.operands[3].present)
11749     {
11750       offsetT shift_amount = inst.reloc.exp.X_add_number;
11751
11752       inst.reloc.type = BFD_RELOC_UNUSED;
11753
11754       constraint (inst.reloc.exp.X_op != O_constant,
11755                   _("expression too complex"));
11756
11757       if (shift_amount != 0)
11758         {
11759           constraint (shift_amount > 31,
11760                       _("shift expression is too large"));
11761
11762           if (inst.operands[3].shift_kind == SHIFT_ASR)
11763             inst.instruction |= 0x00200000;  /* sh bit.  */
11764
11765           inst.instruction |= (shift_amount & 0x1c) << 10;
11766           inst.instruction |= (shift_amount & 0x03) << 6;
11767         }
11768     }
11769 }
11770
11771 static void
11772 do_t_ssat (void)
11773 {
11774   do_t_ssat_usat (1);
11775 }
11776
11777 static void
11778 do_t_ssat16 (void)
11779 {
11780   unsigned Rd, Rn;
11781
11782   Rd = inst.operands[0].reg;
11783   Rn = inst.operands[2].reg;
11784
11785   reject_bad_reg (Rd);
11786   reject_bad_reg (Rn);
11787
11788   inst.instruction |= Rd << 8;
11789   inst.instruction |= inst.operands[1].imm - 1;
11790   inst.instruction |= Rn << 16;
11791 }
11792
11793 static void
11794 do_t_strex (void)
11795 {
11796   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
11797               || inst.operands[2].postind || inst.operands[2].writeback
11798               || inst.operands[2].immisreg || inst.operands[2].shifted
11799               || inst.operands[2].negative,
11800               BAD_ADDR_MODE);
11801
11802   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
11803
11804   inst.instruction |= inst.operands[0].reg << 8;
11805   inst.instruction |= inst.operands[1].reg << 12;
11806   inst.instruction |= inst.operands[2].reg << 16;
11807   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
11808 }
11809
11810 static void
11811 do_t_strexd (void)
11812 {
11813   if (!inst.operands[2].present)
11814     inst.operands[2].reg = inst.operands[1].reg + 1;
11815
11816   constraint (inst.operands[0].reg == inst.operands[1].reg
11817               || inst.operands[0].reg == inst.operands[2].reg
11818               || inst.operands[0].reg == inst.operands[3].reg,
11819               BAD_OVERLAP);
11820
11821   inst.instruction |= inst.operands[0].reg;
11822   inst.instruction |= inst.operands[1].reg << 12;
11823   inst.instruction |= inst.operands[2].reg << 8;
11824   inst.instruction |= inst.operands[3].reg << 16;
11825 }
11826
11827 static void
11828 do_t_sxtah (void)
11829 {
11830   unsigned Rd, Rn, Rm;
11831
11832   Rd = inst.operands[0].reg;
11833   Rn = inst.operands[1].reg;
11834   Rm = inst.operands[2].reg;
11835
11836   reject_bad_reg (Rd);
11837   reject_bad_reg (Rn);
11838   reject_bad_reg (Rm);
11839
11840   inst.instruction |= Rd << 8;
11841   inst.instruction |= Rn << 16;
11842   inst.instruction |= Rm;
11843   inst.instruction |= inst.operands[3].imm << 4;
11844 }
11845
11846 static void
11847 do_t_sxth (void)
11848 {
11849   unsigned Rd, Rm;
11850
11851   Rd = inst.operands[0].reg;
11852   Rm = inst.operands[1].reg;
11853
11854   reject_bad_reg (Rd);
11855   reject_bad_reg (Rm);
11856
11857   if (inst.instruction <= 0xffff
11858       && inst.size_req != 4
11859       && Rd <= 7 && Rm <= 7
11860       && (!inst.operands[2].present || inst.operands[2].imm == 0))
11861     {
11862       inst.instruction = THUMB_OP16 (inst.instruction);
11863       inst.instruction |= Rd;
11864       inst.instruction |= Rm << 3;
11865     }
11866   else if (unified_syntax)
11867     {
11868       if (inst.instruction <= 0xffff)
11869         inst.instruction = THUMB_OP32 (inst.instruction);
11870       inst.instruction |= Rd << 8;
11871       inst.instruction |= Rm;
11872       inst.instruction |= inst.operands[2].imm << 4;
11873     }
11874   else
11875     {
11876       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
11877                   _("Thumb encoding does not support rotation"));
11878       constraint (1, BAD_HIREG);
11879     }
11880 }
11881
11882 static void
11883 do_t_swi (void)
11884 {
11885   /* We have to do the following check manually as ARM_EXT_OS only applies
11886      to ARM_EXT_V6M.  */
11887   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6m))
11888     {
11889       if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_os)
11890           /* This only applies to the v6m howver, not later architectures.  */
11891           && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7))
11892         as_bad (_("SVC is not permitted on this architecture"));
11893       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, arm_ext_os);
11894     }
11895
11896   inst.reloc.type = BFD_RELOC_ARM_SWI;
11897 }
11898
11899 static void
11900 do_t_tb (void)
11901 {
11902   unsigned Rn, Rm;
11903   int half;
11904
11905   half = (inst.instruction & 0x10) != 0;
11906   set_it_insn_type_last ();
11907   constraint (inst.operands[0].immisreg,
11908               _("instruction requires register index"));
11909
11910   Rn = inst.operands[0].reg;
11911   Rm = inst.operands[0].imm;
11912
11913   constraint (Rn == REG_SP, BAD_SP);
11914   reject_bad_reg (Rm);
11915
11916   constraint (!half && inst.operands[0].shifted,
11917               _("instruction does not allow shifted index"));
11918   inst.instruction |= (Rn << 16) | Rm;
11919 }
11920
11921 static void
11922 do_t_usat (void)
11923 {
11924   do_t_ssat_usat (0);
11925 }
11926
11927 static void
11928 do_t_usat16 (void)
11929 {
11930   unsigned Rd, Rn;
11931
11932   Rd = inst.operands[0].reg;
11933   Rn = inst.operands[2].reg;
11934
11935   reject_bad_reg (Rd);
11936   reject_bad_reg (Rn);
11937
11938   inst.instruction |= Rd << 8;
11939   inst.instruction |= inst.operands[1].imm;
11940   inst.instruction |= Rn << 16;
11941 }
11942
11943 /* Neon instruction encoder helpers.  */
11944
11945 /* Encodings for the different types for various Neon opcodes.  */
11946
11947 /* An "invalid" code for the following tables.  */
11948 #define N_INV -1u
11949
11950 struct neon_tab_entry
11951 {
11952   unsigned integer;
11953   unsigned float_or_poly;
11954   unsigned scalar_or_imm;
11955 };
11956
11957 /* Map overloaded Neon opcodes to their respective encodings.  */
11958 #define NEON_ENC_TAB                                    \
11959   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
11960   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
11961   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
11962   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
11963   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
11964   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
11965   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
11966   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
11967   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
11968   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
11969   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
11970   /* Register variants of the following two instructions are encoded as
11971      vcge / vcgt with the operands reversed.  */        \
11972   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
11973   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
11974   X(vfma,       N_INV, 0x0000c10, N_INV),               \
11975   X(vfms,       N_INV, 0x0200c10, N_INV),               \
11976   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
11977   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
11978   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
11979   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
11980   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
11981   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
11982   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
11983   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
11984   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
11985   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
11986   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
11987   X(vshl,       0x0000400, N_INV,     0x0800510),       \
11988   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
11989   X(vand,       0x0000110, N_INV,     0x0800030),       \
11990   X(vbic,       0x0100110, N_INV,     0x0800030),       \
11991   X(veor,       0x1000110, N_INV,     N_INV),           \
11992   X(vorn,       0x0300110, N_INV,     0x0800010),       \
11993   X(vorr,       0x0200110, N_INV,     0x0800010),       \
11994   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
11995   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
11996   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
11997   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
11998   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
11999   X(vst1,       0x0000000, 0x0800000, N_INV),           \
12000   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
12001   X(vst2,       0x0000100, 0x0800100, N_INV),           \
12002   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
12003   X(vst3,       0x0000200, 0x0800200, N_INV),           \
12004   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
12005   X(vst4,       0x0000300, 0x0800300, N_INV),           \
12006   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
12007   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
12008   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
12009   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
12010   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
12011   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
12012   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
12013   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
12014   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
12015   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
12016   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
12017   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
12018   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV)
12019
12020 enum neon_opc
12021 {
12022 #define X(OPC,I,F,S) N_MNEM_##OPC
12023 NEON_ENC_TAB
12024 #undef X
12025 };
12026
12027 static const struct neon_tab_entry neon_enc_tab[] =
12028 {
12029 #define X(OPC,I,F,S) { (I), (F), (S) }
12030 NEON_ENC_TAB
12031 #undef X
12032 };
12033
12034 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
12035 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
12036 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
12037 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12038 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12039 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12040 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12041 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
12042 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
12043 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
12044 #define NEON_ENC_SINGLE_(X) \
12045   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
12046 #define NEON_ENC_DOUBLE_(X) \
12047   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
12048
12049 #define NEON_ENCODE(type, inst)                                 \
12050   do                                                            \
12051     {                                                           \
12052       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
12053       inst.is_neon = 1;                                         \
12054     }                                                           \
12055   while (0)
12056
12057 #define check_neon_suffixes                                             \
12058   do                                                                    \
12059     {                                                                   \
12060       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
12061         {                                                               \
12062           as_bad (_("invalid neon suffix for non neon instruction"));   \
12063           return;                                                       \
12064         }                                                               \
12065     }                                                                   \
12066   while (0)
12067
12068 /* Define shapes for instruction operands. The following mnemonic characters
12069    are used in this table:
12070
12071      F - VFP S<n> register
12072      D - Neon D<n> register
12073      Q - Neon Q<n> register
12074      I - Immediate
12075      S - Scalar
12076      R - ARM register
12077      L - D<n> register list
12078
12079    This table is used to generate various data:
12080      - enumerations of the form NS_DDR to be used as arguments to
12081        neon_select_shape.
12082      - a table classifying shapes into single, double, quad, mixed.
12083      - a table used to drive neon_select_shape.  */
12084
12085 #define NEON_SHAPE_DEF                  \
12086   X(3, (D, D, D), DOUBLE),              \
12087   X(3, (Q, Q, Q), QUAD),                \
12088   X(3, (D, D, I), DOUBLE),              \
12089   X(3, (Q, Q, I), QUAD),                \
12090   X(3, (D, D, S), DOUBLE),              \
12091   X(3, (Q, Q, S), QUAD),                \
12092   X(2, (D, D), DOUBLE),                 \
12093   X(2, (Q, Q), QUAD),                   \
12094   X(2, (D, S), DOUBLE),                 \
12095   X(2, (Q, S), QUAD),                   \
12096   X(2, (D, R), DOUBLE),                 \
12097   X(2, (Q, R), QUAD),                   \
12098   X(2, (D, I), DOUBLE),                 \
12099   X(2, (Q, I), QUAD),                   \
12100   X(3, (D, L, D), DOUBLE),              \
12101   X(2, (D, Q), MIXED),                  \
12102   X(2, (Q, D), MIXED),                  \
12103   X(3, (D, Q, I), MIXED),               \
12104   X(3, (Q, D, I), MIXED),               \
12105   X(3, (Q, D, D), MIXED),               \
12106   X(3, (D, Q, Q), MIXED),               \
12107   X(3, (Q, Q, D), MIXED),               \
12108   X(3, (Q, D, S), MIXED),               \
12109   X(3, (D, Q, S), MIXED),               \
12110   X(4, (D, D, D, I), DOUBLE),           \
12111   X(4, (Q, Q, Q, I), QUAD),             \
12112   X(2, (F, F), SINGLE),                 \
12113   X(3, (F, F, F), SINGLE),              \
12114   X(2, (F, I), SINGLE),                 \
12115   X(2, (F, D), MIXED),                  \
12116   X(2, (D, F), MIXED),                  \
12117   X(3, (F, F, I), MIXED),               \
12118   X(4, (R, R, F, F), SINGLE),           \
12119   X(4, (F, F, R, R), SINGLE),           \
12120   X(3, (D, R, R), DOUBLE),              \
12121   X(3, (R, R, D), DOUBLE),              \
12122   X(2, (S, R), SINGLE),                 \
12123   X(2, (R, S), SINGLE),                 \
12124   X(2, (F, R), SINGLE),                 \
12125   X(2, (R, F), SINGLE)
12126
12127 #define S2(A,B)         NS_##A##B
12128 #define S3(A,B,C)       NS_##A##B##C
12129 #define S4(A,B,C,D)     NS_##A##B##C##D
12130
12131 #define X(N, L, C) S##N L
12132
12133 enum neon_shape
12134 {
12135   NEON_SHAPE_DEF,
12136   NS_NULL
12137 };
12138
12139 #undef X
12140 #undef S2
12141 #undef S3
12142 #undef S4
12143
12144 enum neon_shape_class
12145 {
12146   SC_SINGLE,
12147   SC_DOUBLE,
12148   SC_QUAD,
12149   SC_MIXED
12150 };
12151
12152 #define X(N, L, C) SC_##C
12153
12154 static enum neon_shape_class neon_shape_class[] =
12155 {
12156   NEON_SHAPE_DEF
12157 };
12158
12159 #undef X
12160
12161 enum neon_shape_el
12162 {
12163   SE_F,
12164   SE_D,
12165   SE_Q,
12166   SE_I,
12167   SE_S,
12168   SE_R,
12169   SE_L
12170 };
12171
12172 /* Register widths of above.  */
12173 static unsigned neon_shape_el_size[] =
12174 {
12175   32,
12176   64,
12177   128,
12178   0,
12179   32,
12180   32,
12181   0
12182 };
12183
12184 struct neon_shape_info
12185 {
12186   unsigned els;
12187   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
12188 };
12189
12190 #define S2(A,B)         { SE_##A, SE_##B }
12191 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
12192 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
12193
12194 #define X(N, L, C) { N, S##N L }
12195
12196 static struct neon_shape_info neon_shape_tab[] =
12197 {
12198   NEON_SHAPE_DEF
12199 };
12200
12201 #undef X
12202 #undef S2
12203 #undef S3
12204 #undef S4
12205
12206 /* Bit masks used in type checking given instructions.
12207   'N_EQK' means the type must be the same as (or based on in some way) the key
12208    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
12209    set, various other bits can be set as well in order to modify the meaning of
12210    the type constraint.  */
12211
12212 enum neon_type_mask
12213 {
12214   N_S8   = 0x0000001,
12215   N_S16  = 0x0000002,
12216   N_S32  = 0x0000004,
12217   N_S64  = 0x0000008,
12218   N_U8   = 0x0000010,
12219   N_U16  = 0x0000020,
12220   N_U32  = 0x0000040,
12221   N_U64  = 0x0000080,
12222   N_I8   = 0x0000100,
12223   N_I16  = 0x0000200,
12224   N_I32  = 0x0000400,
12225   N_I64  = 0x0000800,
12226   N_8    = 0x0001000,
12227   N_16   = 0x0002000,
12228   N_32   = 0x0004000,
12229   N_64   = 0x0008000,
12230   N_P8   = 0x0010000,
12231   N_P16  = 0x0020000,
12232   N_F16  = 0x0040000,
12233   N_F32  = 0x0080000,
12234   N_F64  = 0x0100000,
12235   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
12236   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
12237   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
12238   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
12239   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
12240   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
12241   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
12242   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
12243   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
12244   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
12245   N_UTYP = 0,
12246   N_MAX_NONSPECIAL = N_F64
12247 };
12248
12249 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
12250
12251 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
12252 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
12253 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
12254 #define N_SUF_32   (N_SU_32 | N_F32)
12255 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
12256 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F32)
12257
12258 /* Pass this as the first type argument to neon_check_type to ignore types
12259    altogether.  */
12260 #define N_IGNORE_TYPE (N_KEY | N_EQK)
12261
12262 /* Select a "shape" for the current instruction (describing register types or
12263    sizes) from a list of alternatives. Return NS_NULL if the current instruction
12264    doesn't fit. For non-polymorphic shapes, checking is usually done as a
12265    function of operand parsing, so this function doesn't need to be called.
12266    Shapes should be listed in order of decreasing length.  */
12267
12268 static enum neon_shape
12269 neon_select_shape (enum neon_shape shape, ...)
12270 {
12271   va_list ap;
12272   enum neon_shape first_shape = shape;
12273
12274   /* Fix missing optional operands. FIXME: we don't know at this point how
12275      many arguments we should have, so this makes the assumption that we have
12276      > 1. This is true of all current Neon opcodes, I think, but may not be
12277      true in the future.  */
12278   if (!inst.operands[1].present)
12279     inst.operands[1] = inst.operands[0];
12280
12281   va_start (ap, shape);
12282
12283   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
12284     {
12285       unsigned j;
12286       int matches = 1;
12287
12288       for (j = 0; j < neon_shape_tab[shape].els; j++)
12289         {
12290           if (!inst.operands[j].present)
12291             {
12292               matches = 0;
12293               break;
12294             }
12295
12296           switch (neon_shape_tab[shape].el[j])
12297             {
12298             case SE_F:
12299               if (!(inst.operands[j].isreg
12300                     && inst.operands[j].isvec
12301                     && inst.operands[j].issingle
12302                     && !inst.operands[j].isquad))
12303                 matches = 0;
12304               break;
12305
12306             case SE_D:
12307               if (!(inst.operands[j].isreg
12308                     && inst.operands[j].isvec
12309                     && !inst.operands[j].isquad
12310                     && !inst.operands[j].issingle))
12311                 matches = 0;
12312               break;
12313
12314             case SE_R:
12315               if (!(inst.operands[j].isreg
12316                     && !inst.operands[j].isvec))
12317                 matches = 0;
12318               break;
12319
12320             case SE_Q:
12321               if (!(inst.operands[j].isreg
12322                     && inst.operands[j].isvec
12323                     && inst.operands[j].isquad
12324                     && !inst.operands[j].issingle))
12325                 matches = 0;
12326               break;
12327
12328             case SE_I:
12329               if (!(!inst.operands[j].isreg
12330                     && !inst.operands[j].isscalar))
12331                 matches = 0;
12332               break;
12333
12334             case SE_S:
12335               if (!(!inst.operands[j].isreg
12336                     && inst.operands[j].isscalar))
12337                 matches = 0;
12338               break;
12339
12340             case SE_L:
12341               break;
12342             }
12343           if (!matches)
12344             break;
12345         }
12346       if (matches)
12347         break;
12348     }
12349
12350   va_end (ap);
12351
12352   if (shape == NS_NULL && first_shape != NS_NULL)
12353     first_error (_("invalid instruction shape"));
12354
12355   return shape;
12356 }
12357
12358 /* True if SHAPE is predominantly a quadword operation (most of the time, this
12359    means the Q bit should be set).  */
12360
12361 static int
12362 neon_quad (enum neon_shape shape)
12363 {
12364   return neon_shape_class[shape] == SC_QUAD;
12365 }
12366
12367 static void
12368 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
12369                        unsigned *g_size)
12370 {
12371   /* Allow modification to be made to types which are constrained to be
12372      based on the key element, based on bits set alongside N_EQK.  */
12373   if ((typebits & N_EQK) != 0)
12374     {
12375       if ((typebits & N_HLF) != 0)
12376         *g_size /= 2;
12377       else if ((typebits & N_DBL) != 0)
12378         *g_size *= 2;
12379       if ((typebits & N_SGN) != 0)
12380         *g_type = NT_signed;
12381       else if ((typebits & N_UNS) != 0)
12382         *g_type = NT_unsigned;
12383       else if ((typebits & N_INT) != 0)
12384         *g_type = NT_integer;
12385       else if ((typebits & N_FLT) != 0)
12386         *g_type = NT_float;
12387       else if ((typebits & N_SIZ) != 0)
12388         *g_type = NT_untyped;
12389     }
12390 }
12391
12392 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
12393    operand type, i.e. the single type specified in a Neon instruction when it
12394    is the only one given.  */
12395
12396 static struct neon_type_el
12397 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
12398 {
12399   struct neon_type_el dest = *key;
12400
12401   gas_assert ((thisarg & N_EQK) != 0);
12402
12403   neon_modify_type_size (thisarg, &dest.type, &dest.size);
12404
12405   return dest;
12406 }
12407
12408 /* Convert Neon type and size into compact bitmask representation.  */
12409
12410 static enum neon_type_mask
12411 type_chk_of_el_type (enum neon_el_type type, unsigned size)
12412 {
12413   switch (type)
12414     {
12415     case NT_untyped:
12416       switch (size)
12417         {
12418         case 8:  return N_8;
12419         case 16: return N_16;
12420         case 32: return N_32;
12421         case 64: return N_64;
12422         default: ;
12423         }
12424       break;
12425
12426     case NT_integer:
12427       switch (size)
12428         {
12429         case 8:  return N_I8;
12430         case 16: return N_I16;
12431         case 32: return N_I32;
12432         case 64: return N_I64;
12433         default: ;
12434         }
12435       break;
12436
12437     case NT_float:
12438       switch (size)
12439         {
12440         case 16: return N_F16;
12441         case 32: return N_F32;
12442         case 64: return N_F64;
12443         default: ;
12444         }
12445       break;
12446
12447     case NT_poly:
12448       switch (size)
12449         {
12450         case 8:  return N_P8;
12451         case 16: return N_P16;
12452         default: ;
12453         }
12454       break;
12455
12456     case NT_signed:
12457       switch (size)
12458         {
12459         case 8:  return N_S8;
12460         case 16: return N_S16;
12461         case 32: return N_S32;
12462         case 64: return N_S64;
12463         default: ;
12464         }
12465       break;
12466
12467     case NT_unsigned:
12468       switch (size)
12469         {
12470         case 8:  return N_U8;
12471         case 16: return N_U16;
12472         case 32: return N_U32;
12473         case 64: return N_U64;
12474         default: ;
12475         }
12476       break;
12477
12478     default: ;
12479     }
12480
12481   return N_UTYP;
12482 }
12483
12484 /* Convert compact Neon bitmask type representation to a type and size. Only
12485    handles the case where a single bit is set in the mask.  */
12486
12487 static int
12488 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
12489                      enum neon_type_mask mask)
12490 {
12491   if ((mask & N_EQK) != 0)
12492     return FAIL;
12493
12494   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
12495     *size = 8;
12496   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_P16)) != 0)
12497     *size = 16;
12498   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
12499     *size = 32;
12500   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64)) != 0)
12501     *size = 64;
12502   else
12503     return FAIL;
12504
12505   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
12506     *type = NT_signed;
12507   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
12508     *type = NT_unsigned;
12509   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
12510     *type = NT_integer;
12511   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
12512     *type = NT_untyped;
12513   else if ((mask & (N_P8 | N_P16)) != 0)
12514     *type = NT_poly;
12515   else if ((mask & (N_F32 | N_F64)) != 0)
12516     *type = NT_float;
12517   else
12518     return FAIL;
12519
12520   return SUCCESS;
12521 }
12522
12523 /* Modify a bitmask of allowed types. This is only needed for type
12524    relaxation.  */
12525
12526 static unsigned
12527 modify_types_allowed (unsigned allowed, unsigned mods)
12528 {
12529   unsigned size;
12530   enum neon_el_type type;
12531   unsigned destmask;
12532   int i;
12533
12534   destmask = 0;
12535
12536   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
12537     {
12538       if (el_type_of_type_chk (&type, &size,
12539                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
12540         {
12541           neon_modify_type_size (mods, &type, &size);
12542           destmask |= type_chk_of_el_type (type, size);
12543         }
12544     }
12545
12546   return destmask;
12547 }
12548
12549 /* Check type and return type classification.
12550    The manual states (paraphrase): If one datatype is given, it indicates the
12551    type given in:
12552     - the second operand, if there is one
12553     - the operand, if there is no second operand
12554     - the result, if there are no operands.
12555    This isn't quite good enough though, so we use a concept of a "key" datatype
12556    which is set on a per-instruction basis, which is the one which matters when
12557    only one data type is written.
12558    Note: this function has side-effects (e.g. filling in missing operands). All
12559    Neon instructions should call it before performing bit encoding.  */
12560
12561 static struct neon_type_el
12562 neon_check_type (unsigned els, enum neon_shape ns, ...)
12563 {
12564   va_list ap;
12565   unsigned i, pass, key_el = 0;
12566   unsigned types[NEON_MAX_TYPE_ELS];
12567   enum neon_el_type k_type = NT_invtype;
12568   unsigned k_size = -1u;
12569   struct neon_type_el badtype = {NT_invtype, -1};
12570   unsigned key_allowed = 0;
12571
12572   /* Optional registers in Neon instructions are always (not) in operand 1.
12573      Fill in the missing operand here, if it was omitted.  */
12574   if (els > 1 && !inst.operands[1].present)
12575     inst.operands[1] = inst.operands[0];
12576
12577   /* Suck up all the varargs.  */
12578   va_start (ap, ns);
12579   for (i = 0; i < els; i++)
12580     {
12581       unsigned thisarg = va_arg (ap, unsigned);
12582       if (thisarg == N_IGNORE_TYPE)
12583         {
12584           va_end (ap);
12585           return badtype;
12586         }
12587       types[i] = thisarg;
12588       if ((thisarg & N_KEY) != 0)
12589         key_el = i;
12590     }
12591   va_end (ap);
12592
12593   if (inst.vectype.elems > 0)
12594     for (i = 0; i < els; i++)
12595       if (inst.operands[i].vectype.type != NT_invtype)
12596         {
12597           first_error (_("types specified in both the mnemonic and operands"));
12598           return badtype;
12599         }
12600
12601   /* Duplicate inst.vectype elements here as necessary.
12602      FIXME: No idea if this is exactly the same as the ARM assembler,
12603      particularly when an insn takes one register and one non-register
12604      operand. */
12605   if (inst.vectype.elems == 1 && els > 1)
12606     {
12607       unsigned j;
12608       inst.vectype.elems = els;
12609       inst.vectype.el[key_el] = inst.vectype.el[0];
12610       for (j = 0; j < els; j++)
12611         if (j != key_el)
12612           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
12613                                                   types[j]);
12614     }
12615   else if (inst.vectype.elems == 0 && els > 0)
12616     {
12617       unsigned j;
12618       /* No types were given after the mnemonic, so look for types specified
12619          after each operand. We allow some flexibility here; as long as the
12620          "key" operand has a type, we can infer the others.  */
12621       for (j = 0; j < els; j++)
12622         if (inst.operands[j].vectype.type != NT_invtype)
12623           inst.vectype.el[j] = inst.operands[j].vectype;
12624
12625       if (inst.operands[key_el].vectype.type != NT_invtype)
12626         {
12627           for (j = 0; j < els; j++)
12628             if (inst.operands[j].vectype.type == NT_invtype)
12629               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
12630                                                       types[j]);
12631         }
12632       else
12633         {
12634           first_error (_("operand types can't be inferred"));
12635           return badtype;
12636         }
12637     }
12638   else if (inst.vectype.elems != els)
12639     {
12640       first_error (_("type specifier has the wrong number of parts"));
12641       return badtype;
12642     }
12643
12644   for (pass = 0; pass < 2; pass++)
12645     {
12646       for (i = 0; i < els; i++)
12647         {
12648           unsigned thisarg = types[i];
12649           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
12650             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
12651           enum neon_el_type g_type = inst.vectype.el[i].type;
12652           unsigned g_size = inst.vectype.el[i].size;
12653
12654           /* Decay more-specific signed & unsigned types to sign-insensitive
12655              integer types if sign-specific variants are unavailable.  */
12656           if ((g_type == NT_signed || g_type == NT_unsigned)
12657               && (types_allowed & N_SU_ALL) == 0)
12658             g_type = NT_integer;
12659
12660           /* If only untyped args are allowed, decay any more specific types to
12661              them. Some instructions only care about signs for some element
12662              sizes, so handle that properly.  */
12663           if ((g_size == 8 && (types_allowed & N_8) != 0)
12664               || (g_size == 16 && (types_allowed & N_16) != 0)
12665               || (g_size == 32 && (types_allowed & N_32) != 0)
12666               || (g_size == 64 && (types_allowed & N_64) != 0))
12667             g_type = NT_untyped;
12668
12669           if (pass == 0)
12670             {
12671               if ((thisarg & N_KEY) != 0)
12672                 {
12673                   k_type = g_type;
12674                   k_size = g_size;
12675                   key_allowed = thisarg & ~N_KEY;
12676                 }
12677             }
12678           else
12679             {
12680               if ((thisarg & N_VFP) != 0)
12681                 {
12682                   enum neon_shape_el regshape;
12683                   unsigned regwidth, match;
12684
12685                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
12686                   if (ns == NS_NULL)
12687                     {
12688                       first_error (_("invalid instruction shape"));
12689                       return badtype;
12690                     }
12691                   regshape = neon_shape_tab[ns].el[i];
12692                   regwidth = neon_shape_el_size[regshape];
12693
12694                   /* In VFP mode, operands must match register widths. If we
12695                      have a key operand, use its width, else use the width of
12696                      the current operand.  */
12697                   if (k_size != -1u)
12698                     match = k_size;
12699                   else
12700                     match = g_size;
12701
12702                   if (regwidth != match)
12703                     {
12704                       first_error (_("operand size must match register width"));
12705                       return badtype;
12706                     }
12707                 }
12708
12709               if ((thisarg & N_EQK) == 0)
12710                 {
12711                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
12712
12713                   if ((given_type & types_allowed) == 0)
12714                     {
12715                       first_error (_("bad type in Neon instruction"));
12716                       return badtype;
12717                     }
12718                 }
12719               else
12720                 {
12721                   enum neon_el_type mod_k_type = k_type;
12722                   unsigned mod_k_size = k_size;
12723                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
12724                   if (g_type != mod_k_type || g_size != mod_k_size)
12725                     {
12726                       first_error (_("inconsistent types in Neon instruction"));
12727                       return badtype;
12728                     }
12729                 }
12730             }
12731         }
12732     }
12733
12734   return inst.vectype.el[key_el];
12735 }
12736
12737 /* Neon-style VFP instruction forwarding.  */
12738
12739 /* Thumb VFP instructions have 0xE in the condition field.  */
12740
12741 static void
12742 do_vfp_cond_or_thumb (void)
12743 {
12744   inst.is_neon = 1;
12745
12746   if (thumb_mode)
12747     inst.instruction |= 0xe0000000;
12748   else
12749     inst.instruction |= inst.cond << 28;
12750 }
12751
12752 /* Look up and encode a simple mnemonic, for use as a helper function for the
12753    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
12754    etc.  It is assumed that operand parsing has already been done, and that the
12755    operands are in the form expected by the given opcode (this isn't necessarily
12756    the same as the form in which they were parsed, hence some massaging must
12757    take place before this function is called).
12758    Checks current arch version against that in the looked-up opcode.  */
12759
12760 static void
12761 do_vfp_nsyn_opcode (const char *opname)
12762 {
12763   const struct asm_opcode *opcode;
12764
12765   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
12766
12767   if (!opcode)
12768     abort ();
12769
12770   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
12771                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
12772               _(BAD_FPU));
12773
12774   inst.is_neon = 1;
12775
12776   if (thumb_mode)
12777     {
12778       inst.instruction = opcode->tvalue;
12779       opcode->tencode ();
12780     }
12781   else
12782     {
12783       inst.instruction = (inst.cond << 28) | opcode->avalue;
12784       opcode->aencode ();
12785     }
12786 }
12787
12788 static void
12789 do_vfp_nsyn_add_sub (enum neon_shape rs)
12790 {
12791   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
12792
12793   if (rs == NS_FFF)
12794     {
12795       if (is_add)
12796         do_vfp_nsyn_opcode ("fadds");
12797       else
12798         do_vfp_nsyn_opcode ("fsubs");
12799     }
12800   else
12801     {
12802       if (is_add)
12803         do_vfp_nsyn_opcode ("faddd");
12804       else
12805         do_vfp_nsyn_opcode ("fsubd");
12806     }
12807 }
12808
12809 /* Check operand types to see if this is a VFP instruction, and if so call
12810    PFN ().  */
12811
12812 static int
12813 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
12814 {
12815   enum neon_shape rs;
12816   struct neon_type_el et;
12817
12818   switch (args)
12819     {
12820     case 2:
12821       rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
12822       et = neon_check_type (2, rs,
12823         N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12824       break;
12825
12826     case 3:
12827       rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
12828       et = neon_check_type (3, rs,
12829         N_EQK | N_VFP, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12830       break;
12831
12832     default:
12833       abort ();
12834     }
12835
12836   if (et.type != NT_invtype)
12837     {
12838       pfn (rs);
12839       return SUCCESS;
12840     }
12841
12842   inst.error = NULL;
12843   return FAIL;
12844 }
12845
12846 static void
12847 do_vfp_nsyn_mla_mls (enum neon_shape rs)
12848 {
12849   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
12850
12851   if (rs == NS_FFF)
12852     {
12853       if (is_mla)
12854         do_vfp_nsyn_opcode ("fmacs");
12855       else
12856         do_vfp_nsyn_opcode ("fnmacs");
12857     }
12858   else
12859     {
12860       if (is_mla)
12861         do_vfp_nsyn_opcode ("fmacd");
12862       else
12863         do_vfp_nsyn_opcode ("fnmacd");
12864     }
12865 }
12866
12867 static void
12868 do_vfp_nsyn_fma_fms (enum neon_shape rs)
12869 {
12870   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
12871
12872   if (rs == NS_FFF)
12873     {
12874       if (is_fma)
12875         do_vfp_nsyn_opcode ("ffmas");
12876       else
12877         do_vfp_nsyn_opcode ("ffnmas");
12878     }
12879   else
12880     {
12881       if (is_fma)
12882         do_vfp_nsyn_opcode ("ffmad");
12883       else
12884         do_vfp_nsyn_opcode ("ffnmad");
12885     }
12886 }
12887
12888 static void
12889 do_vfp_nsyn_mul (enum neon_shape rs)
12890 {
12891   if (rs == NS_FFF)
12892     do_vfp_nsyn_opcode ("fmuls");
12893   else
12894     do_vfp_nsyn_opcode ("fmuld");
12895 }
12896
12897 static void
12898 do_vfp_nsyn_abs_neg (enum neon_shape rs)
12899 {
12900   int is_neg = (inst.instruction & 0x80) != 0;
12901   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_VFP | N_KEY);
12902
12903   if (rs == NS_FF)
12904     {
12905       if (is_neg)
12906         do_vfp_nsyn_opcode ("fnegs");
12907       else
12908         do_vfp_nsyn_opcode ("fabss");
12909     }
12910   else
12911     {
12912       if (is_neg)
12913         do_vfp_nsyn_opcode ("fnegd");
12914       else
12915         do_vfp_nsyn_opcode ("fabsd");
12916     }
12917 }
12918
12919 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
12920    insns belong to Neon, and are handled elsewhere.  */
12921
12922 static void
12923 do_vfp_nsyn_ldm_stm (int is_dbmode)
12924 {
12925   int is_ldm = (inst.instruction & (1 << 20)) != 0;
12926   if (is_ldm)
12927     {
12928       if (is_dbmode)
12929         do_vfp_nsyn_opcode ("fldmdbs");
12930       else
12931         do_vfp_nsyn_opcode ("fldmias");
12932     }
12933   else
12934     {
12935       if (is_dbmode)
12936         do_vfp_nsyn_opcode ("fstmdbs");
12937       else
12938         do_vfp_nsyn_opcode ("fstmias");
12939     }
12940 }
12941
12942 static void
12943 do_vfp_nsyn_sqrt (void)
12944 {
12945   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
12946   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12947
12948   if (rs == NS_FF)
12949     do_vfp_nsyn_opcode ("fsqrts");
12950   else
12951     do_vfp_nsyn_opcode ("fsqrtd");
12952 }
12953
12954 static void
12955 do_vfp_nsyn_div (void)
12956 {
12957   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
12958   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
12959     N_F32 | N_F64 | N_KEY | N_VFP);
12960
12961   if (rs == NS_FFF)
12962     do_vfp_nsyn_opcode ("fdivs");
12963   else
12964     do_vfp_nsyn_opcode ("fdivd");
12965 }
12966
12967 static void
12968 do_vfp_nsyn_nmul (void)
12969 {
12970   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
12971   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
12972     N_F32 | N_F64 | N_KEY | N_VFP);
12973
12974   if (rs == NS_FFF)
12975     {
12976       NEON_ENCODE (SINGLE, inst);
12977       do_vfp_sp_dyadic ();
12978     }
12979   else
12980     {
12981       NEON_ENCODE (DOUBLE, inst);
12982       do_vfp_dp_rd_rn_rm ();
12983     }
12984   do_vfp_cond_or_thumb ();
12985 }
12986
12987 static void
12988 do_vfp_nsyn_cmp (void)
12989 {
12990   if (inst.operands[1].isreg)
12991     {
12992       enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
12993       neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
12994
12995       if (rs == NS_FF)
12996         {
12997           NEON_ENCODE (SINGLE, inst);
12998           do_vfp_sp_monadic ();
12999         }
13000       else
13001         {
13002           NEON_ENCODE (DOUBLE, inst);
13003           do_vfp_dp_rd_rm ();
13004         }
13005     }
13006   else
13007     {
13008       enum neon_shape rs = neon_select_shape (NS_FI, NS_DI, NS_NULL);
13009       neon_check_type (2, rs, N_F32 | N_F64 | N_KEY | N_VFP, N_EQK);
13010
13011       switch (inst.instruction & 0x0fffffff)
13012         {
13013         case N_MNEM_vcmp:
13014           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
13015           break;
13016         case N_MNEM_vcmpe:
13017           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
13018           break;
13019         default:
13020           abort ();
13021         }
13022
13023       if (rs == NS_FI)
13024         {
13025           NEON_ENCODE (SINGLE, inst);
13026           do_vfp_sp_compare_z ();
13027         }
13028       else
13029         {
13030           NEON_ENCODE (DOUBLE, inst);
13031           do_vfp_dp_rd ();
13032         }
13033     }
13034   do_vfp_cond_or_thumb ();
13035 }
13036
13037 static void
13038 nsyn_insert_sp (void)
13039 {
13040   inst.operands[1] = inst.operands[0];
13041   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
13042   inst.operands[0].reg = REG_SP;
13043   inst.operands[0].isreg = 1;
13044   inst.operands[0].writeback = 1;
13045   inst.operands[0].present = 1;
13046 }
13047
13048 static void
13049 do_vfp_nsyn_push (void)
13050 {
13051   nsyn_insert_sp ();
13052   if (inst.operands[1].issingle)
13053     do_vfp_nsyn_opcode ("fstmdbs");
13054   else
13055     do_vfp_nsyn_opcode ("fstmdbd");
13056 }
13057
13058 static void
13059 do_vfp_nsyn_pop (void)
13060 {
13061   nsyn_insert_sp ();
13062   if (inst.operands[1].issingle)
13063     do_vfp_nsyn_opcode ("fldmias");
13064   else
13065     do_vfp_nsyn_opcode ("fldmiad");
13066 }
13067
13068 /* Fix up Neon data-processing instructions, ORing in the correct bits for
13069    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
13070
13071 static void
13072 neon_dp_fixup (struct arm_it* insn)
13073 {
13074   unsigned int i = insn->instruction;
13075   insn->is_neon = 1;
13076
13077   if (thumb_mode)
13078     {
13079       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
13080       if (i & (1 << 24))
13081         i |= 1 << 28;
13082
13083       i &= ~(1 << 24);
13084
13085       i |= 0xef000000;
13086     }
13087   else
13088     i |= 0xf2000000;
13089
13090   insn->instruction = i;
13091 }
13092
13093 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
13094    (0, 1, 2, 3).  */
13095
13096 static unsigned
13097 neon_logbits (unsigned x)
13098 {
13099   return ffs (x) - 4;
13100 }
13101
13102 #define LOW4(R) ((R) & 0xf)
13103 #define HI1(R) (((R) >> 4) & 1)
13104
13105 /* Encode insns with bit pattern:
13106
13107   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
13108   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
13109
13110   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
13111   different meaning for some instruction.  */
13112
13113 static void
13114 neon_three_same (int isquad, int ubit, int size)
13115 {
13116   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13117   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13118   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
13119   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
13120   inst.instruction |= LOW4 (inst.operands[2].reg);
13121   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
13122   inst.instruction |= (isquad != 0) << 6;
13123   inst.instruction |= (ubit != 0) << 24;
13124   if (size != -1)
13125     inst.instruction |= neon_logbits (size) << 20;
13126
13127   neon_dp_fixup (&inst);
13128 }
13129
13130 /* Encode instructions of the form:
13131
13132   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
13133   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
13134
13135   Don't write size if SIZE == -1.  */
13136
13137 static void
13138 neon_two_same (int qbit, int ubit, int size)
13139 {
13140   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13141   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13142   inst.instruction |= LOW4 (inst.operands[1].reg);
13143   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13144   inst.instruction |= (qbit != 0) << 6;
13145   inst.instruction |= (ubit != 0) << 24;
13146
13147   if (size != -1)
13148     inst.instruction |= neon_logbits (size) << 18;
13149
13150   neon_dp_fixup (&inst);
13151 }
13152
13153 /* Neon instruction encoders, in approximate order of appearance.  */
13154
13155 static void
13156 do_neon_dyadic_i_su (void)
13157 {
13158   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13159   struct neon_type_el et = neon_check_type (3, rs,
13160     N_EQK, N_EQK, N_SU_32 | N_KEY);
13161   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13162 }
13163
13164 static void
13165 do_neon_dyadic_i64_su (void)
13166 {
13167   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13168   struct neon_type_el et = neon_check_type (3, rs,
13169     N_EQK, N_EQK, N_SU_ALL | N_KEY);
13170   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13171 }
13172
13173 static void
13174 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
13175                 unsigned immbits)
13176 {
13177   unsigned size = et.size >> 3;
13178   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13179   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13180   inst.instruction |= LOW4 (inst.operands[1].reg);
13181   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13182   inst.instruction |= (isquad != 0) << 6;
13183   inst.instruction |= immbits << 16;
13184   inst.instruction |= (size >> 3) << 7;
13185   inst.instruction |= (size & 0x7) << 19;
13186   if (write_ubit)
13187     inst.instruction |= (uval != 0) << 24;
13188
13189   neon_dp_fixup (&inst);
13190 }
13191
13192 static void
13193 do_neon_shl_imm (void)
13194 {
13195   if (!inst.operands[2].isreg)
13196     {
13197       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13198       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
13199       NEON_ENCODE (IMMED, inst);
13200       neon_imm_shift (FALSE, 0, neon_quad (rs), et, inst.operands[2].imm);
13201     }
13202   else
13203     {
13204       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13205       struct neon_type_el et = neon_check_type (3, rs,
13206         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
13207       unsigned int tmp;
13208
13209       /* VSHL/VQSHL 3-register variants have syntax such as:
13210            vshl.xx Dd, Dm, Dn
13211          whereas other 3-register operations encoded by neon_three_same have
13212          syntax like:
13213            vadd.xx Dd, Dn, Dm
13214          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
13215          here.  */
13216       tmp = inst.operands[2].reg;
13217       inst.operands[2].reg = inst.operands[1].reg;
13218       inst.operands[1].reg = tmp;
13219       NEON_ENCODE (INTEGER, inst);
13220       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13221     }
13222 }
13223
13224 static void
13225 do_neon_qshl_imm (void)
13226 {
13227   if (!inst.operands[2].isreg)
13228     {
13229       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13230       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
13231
13232       NEON_ENCODE (IMMED, inst);
13233       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
13234                       inst.operands[2].imm);
13235     }
13236   else
13237     {
13238       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13239       struct neon_type_el et = neon_check_type (3, rs,
13240         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
13241       unsigned int tmp;
13242
13243       /* See note in do_neon_shl_imm.  */
13244       tmp = inst.operands[2].reg;
13245       inst.operands[2].reg = inst.operands[1].reg;
13246       inst.operands[1].reg = tmp;
13247       NEON_ENCODE (INTEGER, inst);
13248       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13249     }
13250 }
13251
13252 static void
13253 do_neon_rshl (void)
13254 {
13255   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13256   struct neon_type_el et = neon_check_type (3, rs,
13257     N_EQK, N_EQK, N_SU_ALL | N_KEY);
13258   unsigned int tmp;
13259
13260   tmp = inst.operands[2].reg;
13261   inst.operands[2].reg = inst.operands[1].reg;
13262   inst.operands[1].reg = tmp;
13263   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
13264 }
13265
13266 static int
13267 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
13268 {
13269   /* Handle .I8 pseudo-instructions.  */
13270   if (size == 8)
13271     {
13272       /* Unfortunately, this will make everything apart from zero out-of-range.
13273          FIXME is this the intended semantics? There doesn't seem much point in
13274          accepting .I8 if so.  */
13275       immediate |= immediate << 8;
13276       size = 16;
13277     }
13278
13279   if (size >= 32)
13280     {
13281       if (immediate == (immediate & 0x000000ff))
13282         {
13283           *immbits = immediate;
13284           return 0x1;
13285         }
13286       else if (immediate == (immediate & 0x0000ff00))
13287         {
13288           *immbits = immediate >> 8;
13289           return 0x3;
13290         }
13291       else if (immediate == (immediate & 0x00ff0000))
13292         {
13293           *immbits = immediate >> 16;
13294           return 0x5;
13295         }
13296       else if (immediate == (immediate & 0xff000000))
13297         {
13298           *immbits = immediate >> 24;
13299           return 0x7;
13300         }
13301       if ((immediate & 0xffff) != (immediate >> 16))
13302         goto bad_immediate;
13303       immediate &= 0xffff;
13304     }
13305
13306   if (immediate == (immediate & 0x000000ff))
13307     {
13308       *immbits = immediate;
13309       return 0x9;
13310     }
13311   else if (immediate == (immediate & 0x0000ff00))
13312     {
13313       *immbits = immediate >> 8;
13314       return 0xb;
13315     }
13316
13317   bad_immediate:
13318   first_error (_("immediate value out of range"));
13319   return FAIL;
13320 }
13321
13322 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
13323    A, B, C, D.  */
13324
13325 static int
13326 neon_bits_same_in_bytes (unsigned imm)
13327 {
13328   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
13329          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
13330          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
13331          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
13332 }
13333
13334 /* For immediate of above form, return 0bABCD.  */
13335
13336 static unsigned
13337 neon_squash_bits (unsigned imm)
13338 {
13339   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
13340          | ((imm & 0x01000000) >> 21);
13341 }
13342
13343 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
13344
13345 static unsigned
13346 neon_qfloat_bits (unsigned imm)
13347 {
13348   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
13349 }
13350
13351 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
13352    the instruction. *OP is passed as the initial value of the op field, and
13353    may be set to a different value depending on the constant (i.e.
13354    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
13355    MVN).  If the immediate looks like a repeated pattern then also
13356    try smaller element sizes.  */
13357
13358 static int
13359 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
13360                          unsigned *immbits, int *op, int size,
13361                          enum neon_el_type type)
13362 {
13363   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
13364      float.  */
13365   if (type == NT_float && !float_p)
13366     return FAIL;
13367
13368   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
13369     {
13370       if (size != 32 || *op == 1)
13371         return FAIL;
13372       *immbits = neon_qfloat_bits (immlo);
13373       return 0xf;
13374     }
13375
13376   if (size == 64)
13377     {
13378       if (neon_bits_same_in_bytes (immhi)
13379           && neon_bits_same_in_bytes (immlo))
13380         {
13381           if (*op == 1)
13382             return FAIL;
13383           *immbits = (neon_squash_bits (immhi) << 4)
13384                      | neon_squash_bits (immlo);
13385           *op = 1;
13386           return 0xe;
13387         }
13388
13389       if (immhi != immlo)
13390         return FAIL;
13391     }
13392
13393   if (size >= 32)
13394     {
13395       if (immlo == (immlo & 0x000000ff))
13396         {
13397           *immbits = immlo;
13398           return 0x0;
13399         }
13400       else if (immlo == (immlo & 0x0000ff00))
13401         {
13402           *immbits = immlo >> 8;
13403           return 0x2;
13404         }
13405       else if (immlo == (immlo & 0x00ff0000))
13406         {
13407           *immbits = immlo >> 16;
13408           return 0x4;
13409         }
13410       else if (immlo == (immlo & 0xff000000))
13411         {
13412           *immbits = immlo >> 24;
13413           return 0x6;
13414         }
13415       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
13416         {
13417           *immbits = (immlo >> 8) & 0xff;
13418           return 0xc;
13419         }
13420       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
13421         {
13422           *immbits = (immlo >> 16) & 0xff;
13423           return 0xd;
13424         }
13425
13426       if ((immlo & 0xffff) != (immlo >> 16))
13427         return FAIL;
13428       immlo &= 0xffff;
13429     }
13430
13431   if (size >= 16)
13432     {
13433       if (immlo == (immlo & 0x000000ff))
13434         {
13435           *immbits = immlo;
13436           return 0x8;
13437         }
13438       else if (immlo == (immlo & 0x0000ff00))
13439         {
13440           *immbits = immlo >> 8;
13441           return 0xa;
13442         }
13443
13444       if ((immlo & 0xff) != (immlo >> 8))
13445         return FAIL;
13446       immlo &= 0xff;
13447     }
13448
13449   if (immlo == (immlo & 0x000000ff))
13450     {
13451       /* Don't allow MVN with 8-bit immediate.  */
13452       if (*op == 1)
13453         return FAIL;
13454       *immbits = immlo;
13455       return 0xe;
13456     }
13457
13458   return FAIL;
13459 }
13460
13461 /* Write immediate bits [7:0] to the following locations:
13462
13463   |28/24|23     19|18 16|15                    4|3     0|
13464   |  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|
13465
13466   This function is used by VMOV/VMVN/VORR/VBIC.  */
13467
13468 static void
13469 neon_write_immbits (unsigned immbits)
13470 {
13471   inst.instruction |= immbits & 0xf;
13472   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
13473   inst.instruction |= ((immbits >> 7) & 0x1) << 24;
13474 }
13475
13476 /* Invert low-order SIZE bits of XHI:XLO.  */
13477
13478 static void
13479 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
13480 {
13481   unsigned immlo = xlo ? *xlo : 0;
13482   unsigned immhi = xhi ? *xhi : 0;
13483
13484   switch (size)
13485     {
13486     case 8:
13487       immlo = (~immlo) & 0xff;
13488       break;
13489
13490     case 16:
13491       immlo = (~immlo) & 0xffff;
13492       break;
13493
13494     case 64:
13495       immhi = (~immhi) & 0xffffffff;
13496       /* fall through.  */
13497
13498     case 32:
13499       immlo = (~immlo) & 0xffffffff;
13500       break;
13501
13502     default:
13503       abort ();
13504     }
13505
13506   if (xlo)
13507     *xlo = immlo;
13508
13509   if (xhi)
13510     *xhi = immhi;
13511 }
13512
13513 static void
13514 do_neon_logic (void)
13515 {
13516   if (inst.operands[2].present && inst.operands[2].isreg)
13517     {
13518       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13519       neon_check_type (3, rs, N_IGNORE_TYPE);
13520       /* U bit and size field were set as part of the bitmask.  */
13521       NEON_ENCODE (INTEGER, inst);
13522       neon_three_same (neon_quad (rs), 0, -1);
13523     }
13524   else
13525     {
13526       const int three_ops_form = (inst.operands[2].present
13527                                   && !inst.operands[2].isreg);
13528       const int immoperand = (three_ops_form ? 2 : 1);
13529       enum neon_shape rs = (three_ops_form
13530                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
13531                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
13532       struct neon_type_el et = neon_check_type (2, rs,
13533         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
13534       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
13535       unsigned immbits;
13536       int cmode;
13537
13538       if (et.type == NT_invtype)
13539         return;
13540
13541       if (three_ops_form)
13542         constraint (inst.operands[0].reg != inst.operands[1].reg,
13543                     _("first and second operands shall be the same register"));
13544
13545       NEON_ENCODE (IMMED, inst);
13546
13547       immbits = inst.operands[immoperand].imm;
13548       if (et.size == 64)
13549         {
13550           /* .i64 is a pseudo-op, so the immediate must be a repeating
13551              pattern.  */
13552           if (immbits != (inst.operands[immoperand].regisimm ?
13553                           inst.operands[immoperand].reg : 0))
13554             {
13555               /* Set immbits to an invalid constant.  */
13556               immbits = 0xdeadbeef;
13557             }
13558         }
13559
13560       switch (opcode)
13561         {
13562         case N_MNEM_vbic:
13563           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13564           break;
13565
13566         case N_MNEM_vorr:
13567           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13568           break;
13569
13570         case N_MNEM_vand:
13571           /* Pseudo-instruction for VBIC.  */
13572           neon_invert_size (&immbits, 0, et.size);
13573           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13574           break;
13575
13576         case N_MNEM_vorn:
13577           /* Pseudo-instruction for VORR.  */
13578           neon_invert_size (&immbits, 0, et.size);
13579           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
13580           break;
13581
13582         default:
13583           abort ();
13584         }
13585
13586       if (cmode == FAIL)
13587         return;
13588
13589       inst.instruction |= neon_quad (rs) << 6;
13590       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13591       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13592       inst.instruction |= cmode << 8;
13593       neon_write_immbits (immbits);
13594
13595       neon_dp_fixup (&inst);
13596     }
13597 }
13598
13599 static void
13600 do_neon_bitfield (void)
13601 {
13602   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13603   neon_check_type (3, rs, N_IGNORE_TYPE);
13604   neon_three_same (neon_quad (rs), 0, -1);
13605 }
13606
13607 static void
13608 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
13609                   unsigned destbits)
13610 {
13611   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13612   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
13613                                             types | N_KEY);
13614   if (et.type == NT_float)
13615     {
13616       NEON_ENCODE (FLOAT, inst);
13617       neon_three_same (neon_quad (rs), 0, -1);
13618     }
13619   else
13620     {
13621       NEON_ENCODE (INTEGER, inst);
13622       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
13623     }
13624 }
13625
13626 static void
13627 do_neon_dyadic_if_su (void)
13628 {
13629   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
13630 }
13631
13632 static void
13633 do_neon_dyadic_if_su_d (void)
13634 {
13635   /* This version only allow D registers, but that constraint is enforced during
13636      operand parsing so we don't need to do anything extra here.  */
13637   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
13638 }
13639
13640 static void
13641 do_neon_dyadic_if_i_d (void)
13642 {
13643   /* The "untyped" case can't happen. Do this to stop the "U" bit being
13644      affected if we specify unsigned args.  */
13645   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
13646 }
13647
13648 enum vfp_or_neon_is_neon_bits
13649 {
13650   NEON_CHECK_CC = 1,
13651   NEON_CHECK_ARCH = 2
13652 };
13653
13654 /* Call this function if an instruction which may have belonged to the VFP or
13655    Neon instruction sets, but turned out to be a Neon instruction (due to the
13656    operand types involved, etc.). We have to check and/or fix-up a couple of
13657    things:
13658
13659      - Make sure the user hasn't attempted to make a Neon instruction
13660        conditional.
13661      - Alter the value in the condition code field if necessary.
13662      - Make sure that the arch supports Neon instructions.
13663
13664    Which of these operations take place depends on bits from enum
13665    vfp_or_neon_is_neon_bits.
13666
13667    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
13668    current instruction's condition is COND_ALWAYS, the condition field is
13669    changed to inst.uncond_value. This is necessary because instructions shared
13670    between VFP and Neon may be conditional for the VFP variants only, and the
13671    unconditional Neon version must have, e.g., 0xF in the condition field.  */
13672
13673 static int
13674 vfp_or_neon_is_neon (unsigned check)
13675 {
13676   /* Conditions are always legal in Thumb mode (IT blocks).  */
13677   if (!thumb_mode && (check & NEON_CHECK_CC))
13678     {
13679       if (inst.cond != COND_ALWAYS)
13680         {
13681           first_error (_(BAD_COND));
13682           return FAIL;
13683         }
13684       if (inst.uncond_value != -1)
13685         inst.instruction |= inst.uncond_value << 28;
13686     }
13687
13688   if ((check & NEON_CHECK_ARCH)
13689       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
13690     {
13691       first_error (_(BAD_FPU));
13692       return FAIL;
13693     }
13694
13695   return SUCCESS;
13696 }
13697
13698 static void
13699 do_neon_addsub_if_i (void)
13700 {
13701   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
13702     return;
13703
13704   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13705     return;
13706
13707   /* The "untyped" case can't happen. Do this to stop the "U" bit being
13708      affected if we specify unsigned args.  */
13709   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
13710 }
13711
13712 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
13713    result to be:
13714      V<op> A,B     (A is operand 0, B is operand 2)
13715    to mean:
13716      V<op> A,B,A
13717    not:
13718      V<op> A,B,B
13719    so handle that case specially.  */
13720
13721 static void
13722 neon_exchange_operands (void)
13723 {
13724   void *scratch = alloca (sizeof (inst.operands[0]));
13725   if (inst.operands[1].present)
13726     {
13727       /* Swap operands[1] and operands[2].  */
13728       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
13729       inst.operands[1] = inst.operands[2];
13730       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
13731     }
13732   else
13733     {
13734       inst.operands[1] = inst.operands[2];
13735       inst.operands[2] = inst.operands[0];
13736     }
13737 }
13738
13739 static void
13740 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
13741 {
13742   if (inst.operands[2].isreg)
13743     {
13744       if (invert)
13745         neon_exchange_operands ();
13746       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
13747     }
13748   else
13749     {
13750       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13751       struct neon_type_el et = neon_check_type (2, rs,
13752         N_EQK | N_SIZ, immtypes | N_KEY);
13753
13754       NEON_ENCODE (IMMED, inst);
13755       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13756       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13757       inst.instruction |= LOW4 (inst.operands[1].reg);
13758       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13759       inst.instruction |= neon_quad (rs) << 6;
13760       inst.instruction |= (et.type == NT_float) << 10;
13761       inst.instruction |= neon_logbits (et.size) << 18;
13762
13763       neon_dp_fixup (&inst);
13764     }
13765 }
13766
13767 static void
13768 do_neon_cmp (void)
13769 {
13770   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, FALSE);
13771 }
13772
13773 static void
13774 do_neon_cmp_inv (void)
13775 {
13776   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, TRUE);
13777 }
13778
13779 static void
13780 do_neon_ceq (void)
13781 {
13782   neon_compare (N_IF_32, N_IF_32, FALSE);
13783 }
13784
13785 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
13786    scalars, which are encoded in 5 bits, M : Rm.
13787    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
13788    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
13789    index in M.  */
13790
13791 static unsigned
13792 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
13793 {
13794   unsigned regno = NEON_SCALAR_REG (scalar);
13795   unsigned elno = NEON_SCALAR_INDEX (scalar);
13796
13797   switch (elsize)
13798     {
13799     case 16:
13800       if (regno > 7 || elno > 3)
13801         goto bad_scalar;
13802       return regno | (elno << 3);
13803
13804     case 32:
13805       if (regno > 15 || elno > 1)
13806         goto bad_scalar;
13807       return regno | (elno << 4);
13808
13809     default:
13810     bad_scalar:
13811       first_error (_("scalar out of range for multiply instruction"));
13812     }
13813
13814   return 0;
13815 }
13816
13817 /* Encode multiply / multiply-accumulate scalar instructions.  */
13818
13819 static void
13820 neon_mul_mac (struct neon_type_el et, int ubit)
13821 {
13822   unsigned scalar;
13823
13824   /* Give a more helpful error message if we have an invalid type.  */
13825   if (et.type == NT_invtype)
13826     return;
13827
13828   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
13829   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13830   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13831   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
13832   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
13833   inst.instruction |= LOW4 (scalar);
13834   inst.instruction |= HI1 (scalar) << 5;
13835   inst.instruction |= (et.type == NT_float) << 8;
13836   inst.instruction |= neon_logbits (et.size) << 20;
13837   inst.instruction |= (ubit != 0) << 24;
13838
13839   neon_dp_fixup (&inst);
13840 }
13841
13842 static void
13843 do_neon_mac_maybe_scalar (void)
13844 {
13845   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
13846     return;
13847
13848   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13849     return;
13850
13851   if (inst.operands[2].isscalar)
13852     {
13853       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
13854       struct neon_type_el et = neon_check_type (3, rs,
13855         N_EQK, N_EQK, N_I16 | N_I32 | N_F32 | N_KEY);
13856       NEON_ENCODE (SCALAR, inst);
13857       neon_mul_mac (et, neon_quad (rs));
13858     }
13859   else
13860     {
13861       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
13862          affected if we specify unsigned args.  */
13863       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
13864     }
13865 }
13866
13867 static void
13868 do_neon_fmac (void)
13869 {
13870   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
13871     return;
13872
13873   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13874     return;
13875
13876   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
13877 }
13878
13879 static void
13880 do_neon_tst (void)
13881 {
13882   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13883   struct neon_type_el et = neon_check_type (3, rs,
13884     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
13885   neon_three_same (neon_quad (rs), 0, et.size);
13886 }
13887
13888 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
13889    same types as the MAC equivalents. The polynomial type for this instruction
13890    is encoded the same as the integer type.  */
13891
13892 static void
13893 do_neon_mul (void)
13894 {
13895   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
13896     return;
13897
13898   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13899     return;
13900
13901   if (inst.operands[2].isscalar)
13902     do_neon_mac_maybe_scalar ();
13903   else
13904     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F32 | N_P8, 0);
13905 }
13906
13907 static void
13908 do_neon_qdmulh (void)
13909 {
13910   if (inst.operands[2].isscalar)
13911     {
13912       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
13913       struct neon_type_el et = neon_check_type (3, rs,
13914         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
13915       NEON_ENCODE (SCALAR, inst);
13916       neon_mul_mac (et, neon_quad (rs));
13917     }
13918   else
13919     {
13920       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13921       struct neon_type_el et = neon_check_type (3, rs,
13922         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
13923       NEON_ENCODE (INTEGER, inst);
13924       /* The U bit (rounding) comes from bit mask.  */
13925       neon_three_same (neon_quad (rs), 0, et.size);
13926     }
13927 }
13928
13929 static void
13930 do_neon_fcmp_absolute (void)
13931 {
13932   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13933   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
13934   /* Size field comes from bit mask.  */
13935   neon_three_same (neon_quad (rs), 1, -1);
13936 }
13937
13938 static void
13939 do_neon_fcmp_absolute_inv (void)
13940 {
13941   neon_exchange_operands ();
13942   do_neon_fcmp_absolute ();
13943 }
13944
13945 static void
13946 do_neon_step (void)
13947 {
13948   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
13949   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
13950   neon_three_same (neon_quad (rs), 0, -1);
13951 }
13952
13953 static void
13954 do_neon_abs_neg (void)
13955 {
13956   enum neon_shape rs;
13957   struct neon_type_el et;
13958
13959   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
13960     return;
13961
13962   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
13963     return;
13964
13965   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13966   et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_F32 | N_KEY);
13967
13968   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13969   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13970   inst.instruction |= LOW4 (inst.operands[1].reg);
13971   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
13972   inst.instruction |= neon_quad (rs) << 6;
13973   inst.instruction |= (et.type == NT_float) << 10;
13974   inst.instruction |= neon_logbits (et.size) << 18;
13975
13976   neon_dp_fixup (&inst);
13977 }
13978
13979 static void
13980 do_neon_sli (void)
13981 {
13982   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13983   struct neon_type_el et = neon_check_type (2, rs,
13984     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
13985   int imm = inst.operands[2].imm;
13986   constraint (imm < 0 || (unsigned)imm >= et.size,
13987               _("immediate out of range for insert"));
13988   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
13989 }
13990
13991 static void
13992 do_neon_sri (void)
13993 {
13994   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13995   struct neon_type_el et = neon_check_type (2, rs,
13996     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
13997   int imm = inst.operands[2].imm;
13998   constraint (imm < 1 || (unsigned)imm > et.size,
13999               _("immediate out of range for insert"));
14000   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
14001 }
14002
14003 static void
14004 do_neon_qshlu_imm (void)
14005 {
14006   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14007   struct neon_type_el et = neon_check_type (2, rs,
14008     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
14009   int imm = inst.operands[2].imm;
14010   constraint (imm < 0 || (unsigned)imm >= et.size,
14011               _("immediate out of range for shift"));
14012   /* Only encodes the 'U present' variant of the instruction.
14013      In this case, signed types have OP (bit 8) set to 0.
14014      Unsigned types have OP set to 1.  */
14015   inst.instruction |= (et.type == NT_unsigned) << 8;
14016   /* The rest of the bits are the same as other immediate shifts.  */
14017   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14018 }
14019
14020 static void
14021 do_neon_qmovn (void)
14022 {
14023   struct neon_type_el et = neon_check_type (2, NS_DQ,
14024     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14025   /* Saturating move where operands can be signed or unsigned, and the
14026      destination has the same signedness.  */
14027   NEON_ENCODE (INTEGER, inst);
14028   if (et.type == NT_unsigned)
14029     inst.instruction |= 0xc0;
14030   else
14031     inst.instruction |= 0x80;
14032   neon_two_same (0, 1, et.size / 2);
14033 }
14034
14035 static void
14036 do_neon_qmovun (void)
14037 {
14038   struct neon_type_el et = neon_check_type (2, NS_DQ,
14039     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
14040   /* Saturating move with unsigned results. Operands must be signed.  */
14041   NEON_ENCODE (INTEGER, inst);
14042   neon_two_same (0, 1, et.size / 2);
14043 }
14044
14045 static void
14046 do_neon_rshift_sat_narrow (void)
14047 {
14048   /* FIXME: Types for narrowing. If operands are signed, results can be signed
14049      or unsigned. If operands are unsigned, results must also be unsigned.  */
14050   struct neon_type_el et = neon_check_type (2, NS_DQI,
14051     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14052   int imm = inst.operands[2].imm;
14053   /* This gets the bounds check, size encoding and immediate bits calculation
14054      right.  */
14055   et.size /= 2;
14056
14057   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
14058      VQMOVN.I<size> <Dd>, <Qm>.  */
14059   if (imm == 0)
14060     {
14061       inst.operands[2].present = 0;
14062       inst.instruction = N_MNEM_vqmovn;
14063       do_neon_qmovn ();
14064       return;
14065     }
14066
14067   constraint (imm < 1 || (unsigned)imm > et.size,
14068               _("immediate out of range"));
14069   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
14070 }
14071
14072 static void
14073 do_neon_rshift_sat_narrow_u (void)
14074 {
14075   /* FIXME: Types for narrowing. If operands are signed, results can be signed
14076      or unsigned. If operands are unsigned, results must also be unsigned.  */
14077   struct neon_type_el et = neon_check_type (2, NS_DQI,
14078     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
14079   int imm = inst.operands[2].imm;
14080   /* This gets the bounds check, size encoding and immediate bits calculation
14081      right.  */
14082   et.size /= 2;
14083
14084   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
14085      VQMOVUN.I<size> <Dd>, <Qm>.  */
14086   if (imm == 0)
14087     {
14088       inst.operands[2].present = 0;
14089       inst.instruction = N_MNEM_vqmovun;
14090       do_neon_qmovun ();
14091       return;
14092     }
14093
14094   constraint (imm < 1 || (unsigned)imm > et.size,
14095               _("immediate out of range"));
14096   /* FIXME: The manual is kind of unclear about what value U should have in
14097      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
14098      must be 1.  */
14099   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
14100 }
14101
14102 static void
14103 do_neon_movn (void)
14104 {
14105   struct neon_type_el et = neon_check_type (2, NS_DQ,
14106     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14107   NEON_ENCODE (INTEGER, inst);
14108   neon_two_same (0, 1, et.size / 2);
14109 }
14110
14111 static void
14112 do_neon_rshift_narrow (void)
14113 {
14114   struct neon_type_el et = neon_check_type (2, NS_DQI,
14115     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
14116   int imm = inst.operands[2].imm;
14117   /* This gets the bounds check, size encoding and immediate bits calculation
14118      right.  */
14119   et.size /= 2;
14120
14121   /* If immediate is zero then we are a pseudo-instruction for
14122      VMOVN.I<size> <Dd>, <Qm>  */
14123   if (imm == 0)
14124     {
14125       inst.operands[2].present = 0;
14126       inst.instruction = N_MNEM_vmovn;
14127       do_neon_movn ();
14128       return;
14129     }
14130
14131   constraint (imm < 1 || (unsigned)imm > et.size,
14132               _("immediate out of range for narrowing operation"));
14133   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
14134 }
14135
14136 static void
14137 do_neon_shll (void)
14138 {
14139   /* FIXME: Type checking when lengthening.  */
14140   struct neon_type_el et = neon_check_type (2, NS_QDI,
14141     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
14142   unsigned imm = inst.operands[2].imm;
14143
14144   if (imm == et.size)
14145     {
14146       /* Maximum shift variant.  */
14147       NEON_ENCODE (INTEGER, inst);
14148       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14149       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14150       inst.instruction |= LOW4 (inst.operands[1].reg);
14151       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14152       inst.instruction |= neon_logbits (et.size) << 18;
14153
14154       neon_dp_fixup (&inst);
14155     }
14156   else
14157     {
14158       /* A more-specific type check for non-max versions.  */
14159       et = neon_check_type (2, NS_QDI,
14160         N_EQK | N_DBL, N_SU_32 | N_KEY);
14161       NEON_ENCODE (IMMED, inst);
14162       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
14163     }
14164 }
14165
14166 /* Check the various types for the VCVT instruction, and return which version
14167    the current instruction is.  */
14168
14169 static int
14170 neon_cvt_flavour (enum neon_shape rs)
14171 {
14172 #define CVT_VAR(C,X,Y)                                                  \
14173   et = neon_check_type (2, rs, whole_reg | (X), whole_reg | (Y));       \
14174   if (et.type != NT_invtype)                                            \
14175     {                                                                   \
14176       inst.error = NULL;                                                \
14177       return (C);                                                       \
14178     }
14179   struct neon_type_el et;
14180   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
14181                         || rs == NS_FF) ? N_VFP : 0;
14182   /* The instruction versions which take an immediate take one register
14183      argument, which is extended to the width of the full register. Thus the
14184      "source" and "destination" registers must have the same width.  Hack that
14185      here by making the size equal to the key (wider, in this case) operand.  */
14186   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
14187
14188   CVT_VAR (0, N_S32, N_F32);
14189   CVT_VAR (1, N_U32, N_F32);
14190   CVT_VAR (2, N_F32, N_S32);
14191   CVT_VAR (3, N_F32, N_U32);
14192   /* Half-precision conversions.  */
14193   CVT_VAR (4, N_F32, N_F16);
14194   CVT_VAR (5, N_F16, N_F32);
14195
14196   whole_reg = N_VFP;
14197
14198   /* VFP instructions.  */
14199   CVT_VAR (6, N_F32, N_F64);
14200   CVT_VAR (7, N_F64, N_F32);
14201   CVT_VAR (8, N_S32, N_F64 | key);
14202   CVT_VAR (9, N_U32, N_F64 | key);
14203   CVT_VAR (10, N_F64 | key, N_S32);
14204   CVT_VAR (11, N_F64 | key, N_U32);
14205   /* VFP instructions with bitshift.  */
14206   CVT_VAR (12, N_F32 | key, N_S16);
14207   CVT_VAR (13, N_F32 | key, N_U16);
14208   CVT_VAR (14, N_F64 | key, N_S16);
14209   CVT_VAR (15, N_F64 | key, N_U16);
14210   CVT_VAR (16, N_S16, N_F32 | key);
14211   CVT_VAR (17, N_U16, N_F32 | key);
14212   CVT_VAR (18, N_S16, N_F64 | key);
14213   CVT_VAR (19, N_U16, N_F64 | key);
14214
14215   return -1;
14216 #undef CVT_VAR
14217 }
14218
14219 /* Neon-syntax VFP conversions.  */
14220
14221 static void
14222 do_vfp_nsyn_cvt (enum neon_shape rs, int flavour)
14223 {
14224   const char *opname = 0;
14225
14226   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI)
14227     {
14228       /* Conversions with immediate bitshift.  */
14229       const char *enc[] =
14230         {
14231           "ftosls",
14232           "ftouls",
14233           "fsltos",
14234           "fultos",
14235           NULL,
14236           NULL,
14237           NULL,
14238           NULL,
14239           "ftosld",
14240           "ftould",
14241           "fsltod",
14242           "fultod",
14243           "fshtos",
14244           "fuhtos",
14245           "fshtod",
14246           "fuhtod",
14247           "ftoshs",
14248           "ftouhs",
14249           "ftoshd",
14250           "ftouhd"
14251         };
14252
14253       if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc))
14254         {
14255           opname = enc[flavour];
14256           constraint (inst.operands[0].reg != inst.operands[1].reg,
14257                       _("operands 0 and 1 must be the same register"));
14258           inst.operands[1] = inst.operands[2];
14259           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
14260         }
14261     }
14262   else
14263     {
14264       /* Conversions without bitshift.  */
14265       const char *enc[] =
14266         {
14267           "ftosis",
14268           "ftouis",
14269           "fsitos",
14270           "fuitos",
14271           "NULL",
14272           "NULL",
14273           "fcvtsd",
14274           "fcvtds",
14275           "ftosid",
14276           "ftouid",
14277           "fsitod",
14278           "fuitod"
14279         };
14280
14281       if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc))
14282         opname = enc[flavour];
14283     }
14284
14285   if (opname)
14286     do_vfp_nsyn_opcode (opname);
14287 }
14288
14289 static void
14290 do_vfp_nsyn_cvtz (void)
14291 {
14292   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_NULL);
14293   int flavour = neon_cvt_flavour (rs);
14294   const char *enc[] =
14295     {
14296       "ftosizs",
14297       "ftouizs",
14298       NULL,
14299       NULL,
14300       NULL,
14301       NULL,
14302       NULL,
14303       NULL,
14304       "ftosizd",
14305       "ftouizd"
14306     };
14307
14308   if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
14309     do_vfp_nsyn_opcode (enc[flavour]);
14310 }
14311
14312 static void
14313 do_neon_cvt_1 (bfd_boolean round_to_zero ATTRIBUTE_UNUSED)
14314 {
14315   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
14316     NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ, NS_NULL);
14317   int flavour = neon_cvt_flavour (rs);
14318
14319   /* PR11109: Handle round-to-zero for VCVT conversions.  */
14320   if (round_to_zero
14321       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
14322       && (flavour == 0 || flavour == 1 || flavour == 8 || flavour == 9)
14323       && (rs == NS_FD || rs == NS_FF))
14324     {
14325       do_vfp_nsyn_cvtz ();
14326       return;
14327     }
14328
14329   /* VFP rather than Neon conversions.  */
14330   if (flavour >= 6)
14331     {
14332       do_vfp_nsyn_cvt (rs, flavour);
14333       return;
14334     }
14335
14336   switch (rs)
14337     {
14338     case NS_DDI:
14339     case NS_QQI:
14340       {
14341         unsigned immbits;
14342         unsigned enctab[] = { 0x0000100, 0x1000100, 0x0, 0x1000000 };
14343
14344         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14345           return;
14346
14347         /* Fixed-point conversion with #0 immediate is encoded as an
14348            integer conversion.  */
14349         if (inst.operands[2].present && inst.operands[2].imm == 0)
14350           goto int_encode;
14351        immbits = 32 - inst.operands[2].imm;
14352         NEON_ENCODE (IMMED, inst);
14353         if (flavour != -1)
14354           inst.instruction |= enctab[flavour];
14355         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14356         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14357         inst.instruction |= LOW4 (inst.operands[1].reg);
14358         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14359         inst.instruction |= neon_quad (rs) << 6;
14360         inst.instruction |= 1 << 21;
14361         inst.instruction |= immbits << 16;
14362
14363         neon_dp_fixup (&inst);
14364       }
14365       break;
14366
14367     case NS_DD:
14368     case NS_QQ:
14369     int_encode:
14370       {
14371         unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080 };
14372
14373         NEON_ENCODE (INTEGER, inst);
14374
14375         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14376           return;
14377
14378         if (flavour != -1)
14379           inst.instruction |= enctab[flavour];
14380
14381         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14382         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14383         inst.instruction |= LOW4 (inst.operands[1].reg);
14384         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14385         inst.instruction |= neon_quad (rs) << 6;
14386         inst.instruction |= 2 << 18;
14387
14388         neon_dp_fixup (&inst);
14389       }
14390     break;
14391
14392     /* Half-precision conversions for Advanced SIMD -- neon.  */
14393     case NS_QD:
14394     case NS_DQ:
14395
14396       if ((rs == NS_DQ)
14397           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
14398           {
14399             as_bad (_("operand size must match register width"));
14400             break;
14401           }
14402
14403       if ((rs == NS_QD)
14404           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
14405           {
14406             as_bad (_("operand size must match register width"));
14407             break;
14408           }
14409
14410       if (rs == NS_DQ)
14411         inst.instruction = 0x3b60600;
14412       else
14413         inst.instruction = 0x3b60700;
14414
14415       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14416       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14417       inst.instruction |= LOW4 (inst.operands[1].reg);
14418       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14419       neon_dp_fixup (&inst);
14420       break;
14421
14422     default:
14423       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
14424       do_vfp_nsyn_cvt (rs, flavour);
14425     }
14426 }
14427
14428 static void
14429 do_neon_cvtr (void)
14430 {
14431   do_neon_cvt_1 (FALSE);
14432 }
14433
14434 static void
14435 do_neon_cvt (void)
14436 {
14437   do_neon_cvt_1 (TRUE);
14438 }
14439
14440 static void
14441 do_neon_cvtb (void)
14442 {
14443   inst.instruction = 0xeb20a40;
14444
14445   /* The sizes are attached to the mnemonic.  */
14446   if (inst.vectype.el[0].type != NT_invtype
14447       && inst.vectype.el[0].size == 16)
14448     inst.instruction |= 0x00010000;
14449
14450   /* Programmer's syntax: the sizes are attached to the operands.  */
14451   else if (inst.operands[0].vectype.type != NT_invtype
14452            && inst.operands[0].vectype.size == 16)
14453     inst.instruction |= 0x00010000;
14454
14455   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
14456   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
14457   do_vfp_cond_or_thumb ();
14458 }
14459
14460
14461 static void
14462 do_neon_cvtt (void)
14463 {
14464   do_neon_cvtb ();
14465   inst.instruction |= 0x80;
14466 }
14467
14468 static void
14469 neon_move_immediate (void)
14470 {
14471   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
14472   struct neon_type_el et = neon_check_type (2, rs,
14473     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14474   unsigned immlo, immhi = 0, immbits;
14475   int op, cmode, float_p;
14476
14477   constraint (et.type == NT_invtype,
14478               _("operand size must be specified for immediate VMOV"));
14479
14480   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
14481   op = (inst.instruction & (1 << 5)) != 0;
14482
14483   immlo = inst.operands[1].imm;
14484   if (inst.operands[1].regisimm)
14485     immhi = inst.operands[1].reg;
14486
14487   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
14488               _("immediate has bits set outside the operand size"));
14489
14490   float_p = inst.operands[1].immisfloat;
14491
14492   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
14493                                         et.size, et.type)) == FAIL)
14494     {
14495       /* Invert relevant bits only.  */
14496       neon_invert_size (&immlo, &immhi, et.size);
14497       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
14498          with one or the other; those cases are caught by
14499          neon_cmode_for_move_imm.  */
14500       op = !op;
14501       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
14502                                             &op, et.size, et.type)) == FAIL)
14503         {
14504           first_error (_("immediate out of range"));
14505           return;
14506         }
14507     }
14508
14509   inst.instruction &= ~(1 << 5);
14510   inst.instruction |= op << 5;
14511
14512   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14513   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14514   inst.instruction |= neon_quad (rs) << 6;
14515   inst.instruction |= cmode << 8;
14516
14517   neon_write_immbits (immbits);
14518 }
14519
14520 static void
14521 do_neon_mvn (void)
14522 {
14523   if (inst.operands[1].isreg)
14524     {
14525       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14526
14527       NEON_ENCODE (INTEGER, inst);
14528       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14529       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14530       inst.instruction |= LOW4 (inst.operands[1].reg);
14531       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14532       inst.instruction |= neon_quad (rs) << 6;
14533     }
14534   else
14535     {
14536       NEON_ENCODE (IMMED, inst);
14537       neon_move_immediate ();
14538     }
14539
14540   neon_dp_fixup (&inst);
14541 }
14542
14543 /* Encode instructions of form:
14544
14545   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
14546   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
14547
14548 static void
14549 neon_mixed_length (struct neon_type_el et, unsigned size)
14550 {
14551   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14552   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14553   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14554   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14555   inst.instruction |= LOW4 (inst.operands[2].reg);
14556   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14557   inst.instruction |= (et.type == NT_unsigned) << 24;
14558   inst.instruction |= neon_logbits (size) << 20;
14559
14560   neon_dp_fixup (&inst);
14561 }
14562
14563 static void
14564 do_neon_dyadic_long (void)
14565 {
14566   /* FIXME: Type checking for lengthening op.  */
14567   struct neon_type_el et = neon_check_type (3, NS_QDD,
14568     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
14569   neon_mixed_length (et, et.size);
14570 }
14571
14572 static void
14573 do_neon_abal (void)
14574 {
14575   struct neon_type_el et = neon_check_type (3, NS_QDD,
14576     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
14577   neon_mixed_length (et, et.size);
14578 }
14579
14580 static void
14581 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
14582 {
14583   if (inst.operands[2].isscalar)
14584     {
14585       struct neon_type_el et = neon_check_type (3, NS_QDS,
14586         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
14587       NEON_ENCODE (SCALAR, inst);
14588       neon_mul_mac (et, et.type == NT_unsigned);
14589     }
14590   else
14591     {
14592       struct neon_type_el et = neon_check_type (3, NS_QDD,
14593         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
14594       NEON_ENCODE (INTEGER, inst);
14595       neon_mixed_length (et, et.size);
14596     }
14597 }
14598
14599 static void
14600 do_neon_mac_maybe_scalar_long (void)
14601 {
14602   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
14603 }
14604
14605 static void
14606 do_neon_dyadic_wide (void)
14607 {
14608   struct neon_type_el et = neon_check_type (3, NS_QQD,
14609     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
14610   neon_mixed_length (et, et.size);
14611 }
14612
14613 static void
14614 do_neon_dyadic_narrow (void)
14615 {
14616   struct neon_type_el et = neon_check_type (3, NS_QDD,
14617     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
14618   /* Operand sign is unimportant, and the U bit is part of the opcode,
14619      so force the operand type to integer.  */
14620   et.type = NT_integer;
14621   neon_mixed_length (et, et.size / 2);
14622 }
14623
14624 static void
14625 do_neon_mul_sat_scalar_long (void)
14626 {
14627   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
14628 }
14629
14630 static void
14631 do_neon_vmull (void)
14632 {
14633   if (inst.operands[2].isscalar)
14634     do_neon_mac_maybe_scalar_long ();
14635   else
14636     {
14637       struct neon_type_el et = neon_check_type (3, NS_QDD,
14638         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_KEY);
14639       if (et.type == NT_poly)
14640         NEON_ENCODE (POLY, inst);
14641       else
14642         NEON_ENCODE (INTEGER, inst);
14643       /* For polynomial encoding, size field must be 0b00 and the U bit must be
14644          zero. Should be OK as-is.  */
14645       neon_mixed_length (et, et.size);
14646     }
14647 }
14648
14649 static void
14650 do_neon_ext (void)
14651 {
14652   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
14653   struct neon_type_el et = neon_check_type (3, rs,
14654     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14655   unsigned imm = (inst.operands[3].imm * et.size) / 8;
14656
14657   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
14658               _("shift out of range"));
14659   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14660   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14661   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14662   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14663   inst.instruction |= LOW4 (inst.operands[2].reg);
14664   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14665   inst.instruction |= neon_quad (rs) << 6;
14666   inst.instruction |= imm << 8;
14667
14668   neon_dp_fixup (&inst);
14669 }
14670
14671 static void
14672 do_neon_rev (void)
14673 {
14674   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14675   struct neon_type_el et = neon_check_type (2, rs,
14676     N_EQK, N_8 | N_16 | N_32 | N_KEY);
14677   unsigned op = (inst.instruction >> 7) & 3;
14678   /* N (width of reversed regions) is encoded as part of the bitmask. We
14679      extract it here to check the elements to be reversed are smaller.
14680      Otherwise we'd get a reserved instruction.  */
14681   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
14682   gas_assert (elsize != 0);
14683   constraint (et.size >= elsize,
14684               _("elements must be smaller than reversal region"));
14685   neon_two_same (neon_quad (rs), 1, et.size);
14686 }
14687
14688 static void
14689 do_neon_dup (void)
14690 {
14691   if (inst.operands[1].isscalar)
14692     {
14693       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
14694       struct neon_type_el et = neon_check_type (2, rs,
14695         N_EQK, N_8 | N_16 | N_32 | N_KEY);
14696       unsigned sizebits = et.size >> 3;
14697       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
14698       int logsize = neon_logbits (et.size);
14699       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
14700
14701       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
14702         return;
14703
14704       NEON_ENCODE (SCALAR, inst);
14705       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14706       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14707       inst.instruction |= LOW4 (dm);
14708       inst.instruction |= HI1 (dm) << 5;
14709       inst.instruction |= neon_quad (rs) << 6;
14710       inst.instruction |= x << 17;
14711       inst.instruction |= sizebits << 16;
14712
14713       neon_dp_fixup (&inst);
14714     }
14715   else
14716     {
14717       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
14718       struct neon_type_el et = neon_check_type (2, rs,
14719         N_8 | N_16 | N_32 | N_KEY, N_EQK);
14720       /* Duplicate ARM register to lanes of vector.  */
14721       NEON_ENCODE (ARMREG, inst);
14722       switch (et.size)
14723         {
14724         case 8:  inst.instruction |= 0x400000; break;
14725         case 16: inst.instruction |= 0x000020; break;
14726         case 32: inst.instruction |= 0x000000; break;
14727         default: break;
14728         }
14729       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
14730       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
14731       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
14732       inst.instruction |= neon_quad (rs) << 21;
14733       /* The encoding for this instruction is identical for the ARM and Thumb
14734          variants, except for the condition field.  */
14735       do_vfp_cond_or_thumb ();
14736     }
14737 }
14738
14739 /* VMOV has particularly many variations. It can be one of:
14740      0. VMOV<c><q> <Qd>, <Qm>
14741      1. VMOV<c><q> <Dd>, <Dm>
14742    (Register operations, which are VORR with Rm = Rn.)
14743      2. VMOV<c><q>.<dt> <Qd>, #<imm>
14744      3. VMOV<c><q>.<dt> <Dd>, #<imm>
14745    (Immediate loads.)
14746      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
14747    (ARM register to scalar.)
14748      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
14749    (Two ARM registers to vector.)
14750      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
14751    (Scalar to ARM register.)
14752      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
14753    (Vector to two ARM registers.)
14754      8. VMOV.F32 <Sd>, <Sm>
14755      9. VMOV.F64 <Dd>, <Dm>
14756    (VFP register moves.)
14757     10. VMOV.F32 <Sd>, #imm
14758     11. VMOV.F64 <Dd>, #imm
14759    (VFP float immediate load.)
14760     12. VMOV <Rd>, <Sm>
14761    (VFP single to ARM reg.)
14762     13. VMOV <Sd>, <Rm>
14763    (ARM reg to VFP single.)
14764     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
14765    (Two ARM regs to two VFP singles.)
14766     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
14767    (Two VFP singles to two ARM regs.)
14768
14769    These cases can be disambiguated using neon_select_shape, except cases 1/9
14770    and 3/11 which depend on the operand type too.
14771
14772    All the encoded bits are hardcoded by this function.
14773
14774    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
14775    Cases 5, 7 may be used with VFPv2 and above.
14776
14777    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
14778    can specify a type where it doesn't make sense to, and is ignored).  */
14779
14780 static void
14781 do_neon_mov (void)
14782 {
14783   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
14784     NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR, NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
14785     NS_NULL);
14786   struct neon_type_el et;
14787   const char *ldconst = 0;
14788
14789   switch (rs)
14790     {
14791     case NS_DD:  /* case 1/9.  */
14792       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
14793       /* It is not an error here if no type is given.  */
14794       inst.error = NULL;
14795       if (et.type == NT_float && et.size == 64)
14796         {
14797           do_vfp_nsyn_opcode ("fcpyd");
14798           break;
14799         }
14800       /* fall through.  */
14801
14802     case NS_QQ:  /* case 0/1.  */
14803       {
14804         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14805           return;
14806         /* The architecture manual I have doesn't explicitly state which
14807            value the U bit should have for register->register moves, but
14808            the equivalent VORR instruction has U = 0, so do that.  */
14809         inst.instruction = 0x0200110;
14810         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14811         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14812         inst.instruction |= LOW4 (inst.operands[1].reg);
14813         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14814         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14815         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14816         inst.instruction |= neon_quad (rs) << 6;
14817
14818         neon_dp_fixup (&inst);
14819       }
14820       break;
14821
14822     case NS_DI:  /* case 3/11.  */
14823       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
14824       inst.error = NULL;
14825       if (et.type == NT_float && et.size == 64)
14826         {
14827           /* case 11 (fconstd).  */
14828           ldconst = "fconstd";
14829           goto encode_fconstd;
14830         }
14831       /* fall through.  */
14832
14833     case NS_QI:  /* case 2/3.  */
14834       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14835         return;
14836       inst.instruction = 0x0800010;
14837       neon_move_immediate ();
14838       neon_dp_fixup (&inst);
14839       break;
14840
14841     case NS_SR:  /* case 4.  */
14842       {
14843         unsigned bcdebits = 0;
14844         int logsize;
14845         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
14846         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
14847
14848         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
14849         logsize = neon_logbits (et.size);
14850
14851         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
14852                     _(BAD_FPU));
14853         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
14854                     && et.size != 32, _(BAD_FPU));
14855         constraint (et.type == NT_invtype, _("bad type for scalar"));
14856         constraint (x >= 64 / et.size, _("scalar index out of range"));
14857
14858         switch (et.size)
14859           {
14860           case 8:  bcdebits = 0x8; break;
14861           case 16: bcdebits = 0x1; break;
14862           case 32: bcdebits = 0x0; break;
14863           default: ;
14864           }
14865
14866         bcdebits |= x << logsize;
14867
14868         inst.instruction = 0xe000b10;
14869         do_vfp_cond_or_thumb ();
14870         inst.instruction |= LOW4 (dn) << 16;
14871         inst.instruction |= HI1 (dn) << 7;
14872         inst.instruction |= inst.operands[1].reg << 12;
14873         inst.instruction |= (bcdebits & 3) << 5;
14874         inst.instruction |= (bcdebits >> 2) << 21;
14875       }
14876       break;
14877
14878     case NS_DRR:  /* case 5 (fmdrr).  */
14879       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
14880                   _(BAD_FPU));
14881
14882       inst.instruction = 0xc400b10;
14883       do_vfp_cond_or_thumb ();
14884       inst.instruction |= LOW4 (inst.operands[0].reg);
14885       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
14886       inst.instruction |= inst.operands[1].reg << 12;
14887       inst.instruction |= inst.operands[2].reg << 16;
14888       break;
14889
14890     case NS_RS:  /* case 6.  */
14891       {
14892         unsigned logsize;
14893         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
14894         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
14895         unsigned abcdebits = 0;
14896
14897         et = neon_check_type (2, NS_NULL,
14898                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
14899         logsize = neon_logbits (et.size);
14900
14901         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
14902                     _(BAD_FPU));
14903         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
14904                     && et.size != 32, _(BAD_FPU));
14905         constraint (et.type == NT_invtype, _("bad type for scalar"));
14906         constraint (x >= 64 / et.size, _("scalar index out of range"));
14907
14908         switch (et.size)
14909           {
14910           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
14911           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
14912           case 32: abcdebits = 0x00; break;
14913           default: ;
14914           }
14915
14916         abcdebits |= x << logsize;
14917         inst.instruction = 0xe100b10;
14918         do_vfp_cond_or_thumb ();
14919         inst.instruction |= LOW4 (dn) << 16;
14920         inst.instruction |= HI1 (dn) << 7;
14921         inst.instruction |= inst.operands[0].reg << 12;
14922         inst.instruction |= (abcdebits & 3) << 5;
14923         inst.instruction |= (abcdebits >> 2) << 21;
14924       }
14925       break;
14926
14927     case NS_RRD:  /* case 7 (fmrrd).  */
14928       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
14929                   _(BAD_FPU));
14930
14931       inst.instruction = 0xc500b10;
14932       do_vfp_cond_or_thumb ();
14933       inst.instruction |= inst.operands[0].reg << 12;
14934       inst.instruction |= inst.operands[1].reg << 16;
14935       inst.instruction |= LOW4 (inst.operands[2].reg);
14936       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14937       break;
14938
14939     case NS_FF:  /* case 8 (fcpys).  */
14940       do_vfp_nsyn_opcode ("fcpys");
14941       break;
14942
14943     case NS_FI:  /* case 10 (fconsts).  */
14944       ldconst = "fconsts";
14945       encode_fconstd:
14946       if (is_quarter_float (inst.operands[1].imm))
14947         {
14948           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
14949           do_vfp_nsyn_opcode (ldconst);
14950         }
14951       else
14952         first_error (_("immediate out of range"));
14953       break;
14954
14955     case NS_RF:  /* case 12 (fmrs).  */
14956       do_vfp_nsyn_opcode ("fmrs");
14957       break;
14958
14959     case NS_FR:  /* case 13 (fmsr).  */
14960       do_vfp_nsyn_opcode ("fmsr");
14961       break;
14962
14963     /* The encoders for the fmrrs and fmsrr instructions expect three operands
14964        (one of which is a list), but we have parsed four.  Do some fiddling to
14965        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
14966        expect.  */
14967     case NS_RRFF:  /* case 14 (fmrrs).  */
14968       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
14969                   _("VFP registers must be adjacent"));
14970       inst.operands[2].imm = 2;
14971       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
14972       do_vfp_nsyn_opcode ("fmrrs");
14973       break;
14974
14975     case NS_FFRR:  /* case 15 (fmsrr).  */
14976       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
14977                   _("VFP registers must be adjacent"));
14978       inst.operands[1] = inst.operands[2];
14979       inst.operands[2] = inst.operands[3];
14980       inst.operands[0].imm = 2;
14981       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
14982       do_vfp_nsyn_opcode ("fmsrr");
14983       break;
14984
14985     default:
14986       abort ();
14987     }
14988 }
14989
14990 static void
14991 do_neon_rshift_round_imm (void)
14992 {
14993   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14994   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
14995   int imm = inst.operands[2].imm;
14996
14997   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
14998   if (imm == 0)
14999     {
15000       inst.operands[2].present = 0;
15001       do_neon_mov ();
15002       return;
15003     }
15004
15005   constraint (imm < 1 || (unsigned)imm > et.size,
15006               _("immediate out of range for shift"));
15007   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
15008                   et.size - imm);
15009 }
15010
15011 static void
15012 do_neon_movl (void)
15013 {
15014   struct neon_type_el et = neon_check_type (2, NS_QD,
15015     N_EQK | N_DBL, N_SU_32 | N_KEY);
15016   unsigned sizebits = et.size >> 3;
15017   inst.instruction |= sizebits << 19;
15018   neon_two_same (0, et.type == NT_unsigned, -1);
15019 }
15020
15021 static void
15022 do_neon_trn (void)
15023 {
15024   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15025   struct neon_type_el et = neon_check_type (2, rs,
15026     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15027   NEON_ENCODE (INTEGER, inst);
15028   neon_two_same (neon_quad (rs), 1, et.size);
15029 }
15030
15031 static void
15032 do_neon_zip_uzp (void)
15033 {
15034   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15035   struct neon_type_el et = neon_check_type (2, rs,
15036     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15037   if (rs == NS_DD && et.size == 32)
15038     {
15039       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
15040       inst.instruction = N_MNEM_vtrn;
15041       do_neon_trn ();
15042       return;
15043     }
15044   neon_two_same (neon_quad (rs), 1, et.size);
15045 }
15046
15047 static void
15048 do_neon_sat_abs_neg (void)
15049 {
15050   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15051   struct neon_type_el et = neon_check_type (2, rs,
15052     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
15053   neon_two_same (neon_quad (rs), 1, et.size);
15054 }
15055
15056 static void
15057 do_neon_pair_long (void)
15058 {
15059   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15060   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
15061   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
15062   inst.instruction |= (et.type == NT_unsigned) << 7;
15063   neon_two_same (neon_quad (rs), 1, et.size);
15064 }
15065
15066 static void
15067 do_neon_recip_est (void)
15068 {
15069   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15070   struct neon_type_el et = neon_check_type (2, rs,
15071     N_EQK | N_FLT, N_F32 | N_U32 | N_KEY);
15072   inst.instruction |= (et.type == NT_float) << 8;
15073   neon_two_same (neon_quad (rs), 1, et.size);
15074 }
15075
15076 static void
15077 do_neon_cls (void)
15078 {
15079   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15080   struct neon_type_el et = neon_check_type (2, rs,
15081     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
15082   neon_two_same (neon_quad (rs), 1, et.size);
15083 }
15084
15085 static void
15086 do_neon_clz (void)
15087 {
15088   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15089   struct neon_type_el et = neon_check_type (2, rs,
15090     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
15091   neon_two_same (neon_quad (rs), 1, et.size);
15092 }
15093
15094 static void
15095 do_neon_cnt (void)
15096 {
15097   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15098   struct neon_type_el et = neon_check_type (2, rs,
15099     N_EQK | N_INT, N_8 | N_KEY);
15100   neon_two_same (neon_quad (rs), 1, et.size);
15101 }
15102
15103 static void
15104 do_neon_swp (void)
15105 {
15106   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15107   neon_two_same (neon_quad (rs), 1, -1);
15108 }
15109
15110 static void
15111 do_neon_tbl_tbx (void)
15112 {
15113   unsigned listlenbits;
15114   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
15115
15116   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
15117     {
15118       first_error (_("bad list length for table lookup"));
15119       return;
15120     }
15121
15122   listlenbits = inst.operands[1].imm - 1;
15123   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15124   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15125   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15126   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15127   inst.instruction |= LOW4 (inst.operands[2].reg);
15128   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15129   inst.instruction |= listlenbits << 8;
15130
15131   neon_dp_fixup (&inst);
15132 }
15133
15134 static void
15135 do_neon_ldm_stm (void)
15136 {
15137   /* P, U and L bits are part of bitmask.  */
15138   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
15139   unsigned offsetbits = inst.operands[1].imm * 2;
15140
15141   if (inst.operands[1].issingle)
15142     {
15143       do_vfp_nsyn_ldm_stm (is_dbmode);
15144       return;
15145     }
15146
15147   constraint (is_dbmode && !inst.operands[0].writeback,
15148               _("writeback (!) must be used for VLDMDB and VSTMDB"));
15149
15150   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
15151               _("register list must contain at least 1 and at most 16 "
15152                 "registers"));
15153
15154   inst.instruction |= inst.operands[0].reg << 16;
15155   inst.instruction |= inst.operands[0].writeback << 21;
15156   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
15157   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
15158
15159   inst.instruction |= offsetbits;
15160
15161   do_vfp_cond_or_thumb ();
15162 }
15163
15164 static void
15165 do_neon_ldr_str (void)
15166 {
15167   int is_ldr = (inst.instruction & (1 << 20)) != 0;
15168
15169   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
15170      And is UNPREDICTABLE in thumb mode.  */
15171   if (!is_ldr 
15172       && inst.operands[1].reg == REG_PC
15173       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
15174     {
15175       if (!thumb_mode && warn_on_deprecated)
15176         as_warn (_("Use of PC here is deprecated"));
15177       else
15178         inst.error = _("Use of PC here is UNPREDICTABLE");
15179     }
15180
15181   if (inst.operands[0].issingle)
15182     {
15183       if (is_ldr)
15184         do_vfp_nsyn_opcode ("flds");
15185       else
15186         do_vfp_nsyn_opcode ("fsts");
15187     }
15188   else
15189     {
15190       if (is_ldr)
15191         do_vfp_nsyn_opcode ("fldd");
15192       else
15193         do_vfp_nsyn_opcode ("fstd");
15194     }
15195 }
15196
15197 /* "interleave" version also handles non-interleaving register VLD1/VST1
15198    instructions.  */
15199
15200 static void
15201 do_neon_ld_st_interleave (void)
15202 {
15203   struct neon_type_el et = neon_check_type (1, NS_NULL,
15204                                             N_8 | N_16 | N_32 | N_64);
15205   unsigned alignbits = 0;
15206   unsigned idx;
15207   /* The bits in this table go:
15208      0: register stride of one (0) or two (1)
15209      1,2: register list length, minus one (1, 2, 3, 4).
15210      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
15211      We use -1 for invalid entries.  */
15212   const int typetable[] =
15213     {
15214       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
15215        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
15216        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
15217        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
15218     };
15219   int typebits;
15220
15221   if (et.type == NT_invtype)
15222     return;
15223
15224   if (inst.operands[1].immisalign)
15225     switch (inst.operands[1].imm >> 8)
15226       {
15227       case 64: alignbits = 1; break;
15228       case 128:
15229         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
15230             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
15231           goto bad_alignment;
15232         alignbits = 2;
15233         break;
15234       case 256:
15235         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
15236           goto bad_alignment;
15237         alignbits = 3;
15238         break;
15239       default:
15240       bad_alignment:
15241         first_error (_("bad alignment"));
15242         return;
15243       }
15244
15245   inst.instruction |= alignbits << 4;
15246   inst.instruction |= neon_logbits (et.size) << 6;
15247
15248   /* Bits [4:6] of the immediate in a list specifier encode register stride
15249      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
15250      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
15251      up the right value for "type" in a table based on this value and the given
15252      list style, then stick it back.  */
15253   idx = ((inst.operands[0].imm >> 4) & 7)
15254         | (((inst.instruction >> 8) & 3) << 3);
15255
15256   typebits = typetable[idx];
15257
15258   constraint (typebits == -1, _("bad list type for instruction"));
15259
15260   inst.instruction &= ~0xf00;
15261   inst.instruction |= typebits << 8;
15262 }
15263
15264 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
15265    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
15266    otherwise. The variable arguments are a list of pairs of legal (size, align)
15267    values, terminated with -1.  */
15268
15269 static int
15270 neon_alignment_bit (int size, int align, int *do_align, ...)
15271 {
15272   va_list ap;
15273   int result = FAIL, thissize, thisalign;
15274
15275   if (!inst.operands[1].immisalign)
15276     {
15277       *do_align = 0;
15278       return SUCCESS;
15279     }
15280
15281   va_start (ap, do_align);
15282
15283   do
15284     {
15285       thissize = va_arg (ap, int);
15286       if (thissize == -1)
15287         break;
15288       thisalign = va_arg (ap, int);
15289
15290       if (size == thissize && align == thisalign)
15291         result = SUCCESS;
15292     }
15293   while (result != SUCCESS);
15294
15295   va_end (ap);
15296
15297   if (result == SUCCESS)
15298     *do_align = 1;
15299   else
15300     first_error (_("unsupported alignment for instruction"));
15301
15302   return result;
15303 }
15304
15305 static void
15306 do_neon_ld_st_lane (void)
15307 {
15308   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
15309   int align_good, do_align = 0;
15310   int logsize = neon_logbits (et.size);
15311   int align = inst.operands[1].imm >> 8;
15312   int n = (inst.instruction >> 8) & 3;
15313   int max_el = 64 / et.size;
15314
15315   if (et.type == NT_invtype)
15316     return;
15317
15318   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
15319               _("bad list length"));
15320   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
15321               _("scalar index out of range"));
15322   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
15323               && et.size == 8,
15324               _("stride of 2 unavailable when element size is 8"));
15325
15326   switch (n)
15327     {
15328     case 0:  /* VLD1 / VST1.  */
15329       align_good = neon_alignment_bit (et.size, align, &do_align, 16, 16,
15330                                        32, 32, -1);
15331       if (align_good == FAIL)
15332         return;
15333       if (do_align)
15334         {
15335           unsigned alignbits = 0;
15336           switch (et.size)
15337             {
15338             case 16: alignbits = 0x1; break;
15339             case 32: alignbits = 0x3; break;
15340             default: ;
15341             }
15342           inst.instruction |= alignbits << 4;
15343         }
15344       break;
15345
15346     case 1:  /* VLD2 / VST2.  */
15347       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 16, 16, 32,
15348                                        32, 64, -1);
15349       if (align_good == FAIL)
15350         return;
15351       if (do_align)
15352         inst.instruction |= 1 << 4;
15353       break;
15354
15355     case 2:  /* VLD3 / VST3.  */
15356       constraint (inst.operands[1].immisalign,
15357                   _("can't use alignment with this instruction"));
15358       break;
15359
15360     case 3:  /* VLD4 / VST4.  */
15361       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
15362                                        16, 64, 32, 64, 32, 128, -1);
15363       if (align_good == FAIL)
15364         return;
15365       if (do_align)
15366         {
15367           unsigned alignbits = 0;
15368           switch (et.size)
15369             {
15370             case 8:  alignbits = 0x1; break;
15371             case 16: alignbits = 0x1; break;
15372             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
15373             default: ;
15374             }
15375           inst.instruction |= alignbits << 4;
15376         }
15377       break;
15378
15379     default: ;
15380     }
15381
15382   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
15383   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15384     inst.instruction |= 1 << (4 + logsize);
15385
15386   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
15387   inst.instruction |= logsize << 10;
15388 }
15389
15390 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
15391
15392 static void
15393 do_neon_ld_dup (void)
15394 {
15395   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
15396   int align_good, do_align = 0;
15397
15398   if (et.type == NT_invtype)
15399     return;
15400
15401   switch ((inst.instruction >> 8) & 3)
15402     {
15403     case 0:  /* VLD1.  */
15404       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
15405       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
15406                                        &do_align, 16, 16, 32, 32, -1);
15407       if (align_good == FAIL)
15408         return;
15409       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
15410         {
15411         case 1: break;
15412         case 2: inst.instruction |= 1 << 5; break;
15413         default: first_error (_("bad list length")); return;
15414         }
15415       inst.instruction |= neon_logbits (et.size) << 6;
15416       break;
15417
15418     case 1:  /* VLD2.  */
15419       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
15420                                        &do_align, 8, 16, 16, 32, 32, 64, -1);
15421       if (align_good == FAIL)
15422         return;
15423       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
15424                   _("bad list length"));
15425       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15426         inst.instruction |= 1 << 5;
15427       inst.instruction |= neon_logbits (et.size) << 6;
15428       break;
15429
15430     case 2:  /* VLD3.  */
15431       constraint (inst.operands[1].immisalign,
15432                   _("can't use alignment with this instruction"));
15433       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
15434                   _("bad list length"));
15435       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15436         inst.instruction |= 1 << 5;
15437       inst.instruction |= neon_logbits (et.size) << 6;
15438       break;
15439
15440     case 3:  /* VLD4.  */
15441       {
15442         int align = inst.operands[1].imm >> 8;
15443         align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
15444                                          16, 64, 32, 64, 32, 128, -1);
15445         if (align_good == FAIL)
15446           return;
15447         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
15448                     _("bad list length"));
15449         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
15450           inst.instruction |= 1 << 5;
15451         if (et.size == 32 && align == 128)
15452           inst.instruction |= 0x3 << 6;
15453         else
15454           inst.instruction |= neon_logbits (et.size) << 6;
15455       }
15456       break;
15457
15458     default: ;
15459     }
15460
15461   inst.instruction |= do_align << 4;
15462 }
15463
15464 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
15465    apart from bits [11:4].  */
15466
15467 static void
15468 do_neon_ldx_stx (void)
15469 {
15470   if (inst.operands[1].isreg)
15471     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
15472
15473   switch (NEON_LANE (inst.operands[0].imm))
15474     {
15475     case NEON_INTERLEAVE_LANES:
15476       NEON_ENCODE (INTERLV, inst);
15477       do_neon_ld_st_interleave ();
15478       break;
15479
15480     case NEON_ALL_LANES:
15481       NEON_ENCODE (DUP, inst);
15482       do_neon_ld_dup ();
15483       break;
15484
15485     default:
15486       NEON_ENCODE (LANE, inst);
15487       do_neon_ld_st_lane ();
15488     }
15489
15490   /* L bit comes from bit mask.  */
15491   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15492   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15493   inst.instruction |= inst.operands[1].reg << 16;
15494
15495   if (inst.operands[1].postind)
15496     {
15497       int postreg = inst.operands[1].imm & 0xf;
15498       constraint (!inst.operands[1].immisreg,
15499                   _("post-index must be a register"));
15500       constraint (postreg == 0xd || postreg == 0xf,
15501                   _("bad register for post-index"));
15502       inst.instruction |= postreg;
15503     }
15504   else if (inst.operands[1].writeback)
15505     {
15506       inst.instruction |= 0xd;
15507     }
15508   else
15509     inst.instruction |= 0xf;
15510
15511   if (thumb_mode)
15512     inst.instruction |= 0xf9000000;
15513   else
15514     inst.instruction |= 0xf4000000;
15515 }
15516 \f
15517 /* Overall per-instruction processing.  */
15518
15519 /* We need to be able to fix up arbitrary expressions in some statements.
15520    This is so that we can handle symbols that are an arbitrary distance from
15521    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
15522    which returns part of an address in a form which will be valid for
15523    a data instruction.  We do this by pushing the expression into a symbol
15524    in the expr_section, and creating a fix for that.  */
15525
15526 static void
15527 fix_new_arm (fragS *       frag,
15528              int           where,
15529              short int     size,
15530              expressionS * exp,
15531              int           pc_rel,
15532              int           reloc)
15533 {
15534   fixS *           new_fix;
15535
15536   switch (exp->X_op)
15537     {
15538     case O_constant:
15539       if (pc_rel)
15540         {
15541           /* Create an absolute valued symbol, so we have something to
15542              refer to in the object file.  Unfortunately for us, gas's
15543              generic expression parsing will already have folded out
15544              any use of .set foo/.type foo %function that may have
15545              been used to set type information of the target location,
15546              that's being specified symbolically.  We have to presume
15547              the user knows what they are doing.  */
15548           char name[16 + 8];
15549           symbolS *symbol;
15550
15551           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
15552
15553           symbol = symbol_find_or_make (name);
15554           S_SET_SEGMENT (symbol, absolute_section);
15555           symbol_set_frag (symbol, &zero_address_frag);
15556           S_SET_VALUE (symbol, exp->X_add_number);
15557           exp->X_op = O_symbol;
15558           exp->X_add_symbol = symbol;
15559           exp->X_add_number = 0;
15560         }
15561       /* FALLTHROUGH */
15562     case O_symbol:
15563     case O_add:
15564     case O_subtract:
15565       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
15566                              (enum bfd_reloc_code_real) reloc);
15567       break;
15568
15569     default:
15570       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
15571                                   pc_rel, (enum bfd_reloc_code_real) reloc);
15572       break;
15573     }
15574
15575   /* Mark whether the fix is to a THUMB instruction, or an ARM
15576      instruction.  */
15577   new_fix->tc_fix_data = thumb_mode;
15578 }
15579
15580 /* Create a frg for an instruction requiring relaxation.  */
15581 static void
15582 output_relax_insn (void)
15583 {
15584   char * to;
15585   symbolS *sym;
15586   int offset;
15587
15588   /* The size of the instruction is unknown, so tie the debug info to the
15589      start of the instruction.  */
15590   dwarf2_emit_insn (0);
15591
15592   switch (inst.reloc.exp.X_op)
15593     {
15594     case O_symbol:
15595       sym = inst.reloc.exp.X_add_symbol;
15596       offset = inst.reloc.exp.X_add_number;
15597       break;
15598     case O_constant:
15599       sym = NULL;
15600       offset = inst.reloc.exp.X_add_number;
15601       break;
15602     default:
15603       sym = make_expr_symbol (&inst.reloc.exp);
15604       offset = 0;
15605       break;
15606   }
15607   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
15608                  inst.relax, sym, offset, NULL/*offset, opcode*/);
15609   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
15610 }
15611
15612 /* Write a 32-bit thumb instruction to buf.  */
15613 static void
15614 put_thumb32_insn (char * buf, unsigned long insn)
15615 {
15616   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
15617   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
15618 }
15619
15620 static void
15621 output_inst (const char * str)
15622 {
15623   char * to = NULL;
15624
15625   if (inst.error)
15626     {
15627       as_bad ("%s -- `%s'", inst.error, str);
15628       return;
15629     }
15630   if (inst.relax)
15631     {
15632       output_relax_insn ();
15633       return;
15634     }
15635   if (inst.size == 0)
15636     return;
15637
15638   to = frag_more (inst.size);
15639   /* PR 9814: Record the thumb mode into the current frag so that we know
15640      what type of NOP padding to use, if necessary.  We override any previous
15641      setting so that if the mode has changed then the NOPS that we use will
15642      match the encoding of the last instruction in the frag.  */
15643   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
15644
15645   if (thumb_mode && (inst.size > THUMB_SIZE))
15646     {
15647       gas_assert (inst.size == (2 * THUMB_SIZE));
15648       put_thumb32_insn (to, inst.instruction);
15649     }
15650   else if (inst.size > INSN_SIZE)
15651     {
15652       gas_assert (inst.size == (2 * INSN_SIZE));
15653       md_number_to_chars (to, inst.instruction, INSN_SIZE);
15654       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
15655     }
15656   else
15657     md_number_to_chars (to, inst.instruction, inst.size);
15658
15659   if (inst.reloc.type != BFD_RELOC_UNUSED)
15660     fix_new_arm (frag_now, to - frag_now->fr_literal,
15661                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
15662                  inst.reloc.type);
15663
15664   dwarf2_emit_insn (inst.size);
15665 }
15666
15667 static char *
15668 output_it_inst (int cond, int mask, char * to)
15669 {
15670   unsigned long instruction = 0xbf00;
15671
15672   mask &= 0xf;
15673   instruction |= mask;
15674   instruction |= cond << 4;
15675
15676   if (to == NULL)
15677     {
15678       to = frag_more (2);
15679 #ifdef OBJ_ELF
15680       dwarf2_emit_insn (2);
15681 #endif
15682     }
15683
15684   md_number_to_chars (to, instruction, 2);
15685
15686   return to;
15687 }
15688
15689 /* Tag values used in struct asm_opcode's tag field.  */
15690 enum opcode_tag
15691 {
15692   OT_unconditional,     /* Instruction cannot be conditionalized.
15693                            The ARM condition field is still 0xE.  */
15694   OT_unconditionalF,    /* Instruction cannot be conditionalized
15695                            and carries 0xF in its ARM condition field.  */
15696   OT_csuffix,           /* Instruction takes a conditional suffix.  */
15697   OT_csuffixF,          /* Some forms of the instruction take a conditional
15698                            suffix, others place 0xF where the condition field
15699                            would be.  */
15700   OT_cinfix3,           /* Instruction takes a conditional infix,
15701                            beginning at character index 3.  (In
15702                            unified mode, it becomes a suffix.)  */
15703   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
15704                             tsts, cmps, cmns, and teqs. */
15705   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
15706                            character index 3, even in unified mode.  Used for
15707                            legacy instructions where suffix and infix forms
15708                            may be ambiguous.  */
15709   OT_csuf_or_in3,       /* Instruction takes either a conditional
15710                            suffix or an infix at character index 3.  */
15711   OT_odd_infix_unc,     /* This is the unconditional variant of an
15712                            instruction that takes a conditional infix
15713                            at an unusual position.  In unified mode,
15714                            this variant will accept a suffix.  */
15715   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
15716                            are the conditional variants of instructions that
15717                            take conditional infixes in unusual positions.
15718                            The infix appears at character index
15719                            (tag - OT_odd_infix_0).  These are not accepted
15720                            in unified mode.  */
15721 };
15722
15723 /* Subroutine of md_assemble, responsible for looking up the primary
15724    opcode from the mnemonic the user wrote.  STR points to the
15725    beginning of the mnemonic.
15726
15727    This is not simply a hash table lookup, because of conditional
15728    variants.  Most instructions have conditional variants, which are
15729    expressed with a _conditional affix_ to the mnemonic.  If we were
15730    to encode each conditional variant as a literal string in the opcode
15731    table, it would have approximately 20,000 entries.
15732
15733    Most mnemonics take this affix as a suffix, and in unified syntax,
15734    'most' is upgraded to 'all'.  However, in the divided syntax, some
15735    instructions take the affix as an infix, notably the s-variants of
15736    the arithmetic instructions.  Of those instructions, all but six
15737    have the infix appear after the third character of the mnemonic.
15738
15739    Accordingly, the algorithm for looking up primary opcodes given
15740    an identifier is:
15741
15742    1. Look up the identifier in the opcode table.
15743       If we find a match, go to step U.
15744
15745    2. Look up the last two characters of the identifier in the
15746       conditions table.  If we find a match, look up the first N-2
15747       characters of the identifier in the opcode table.  If we
15748       find a match, go to step CE.
15749
15750    3. Look up the fourth and fifth characters of the identifier in
15751       the conditions table.  If we find a match, extract those
15752       characters from the identifier, and look up the remaining
15753       characters in the opcode table.  If we find a match, go
15754       to step CM.
15755
15756    4. Fail.
15757
15758    U. Examine the tag field of the opcode structure, in case this is
15759       one of the six instructions with its conditional infix in an
15760       unusual place.  If it is, the tag tells us where to find the
15761       infix; look it up in the conditions table and set inst.cond
15762       accordingly.  Otherwise, this is an unconditional instruction.
15763       Again set inst.cond accordingly.  Return the opcode structure.
15764
15765   CE. Examine the tag field to make sure this is an instruction that
15766       should receive a conditional suffix.  If it is not, fail.
15767       Otherwise, set inst.cond from the suffix we already looked up,
15768       and return the opcode structure.
15769
15770   CM. Examine the tag field to make sure this is an instruction that
15771       should receive a conditional infix after the third character.
15772       If it is not, fail.  Otherwise, undo the edits to the current
15773       line of input and proceed as for case CE.  */
15774
15775 static const struct asm_opcode *
15776 opcode_lookup (char **str)
15777 {
15778   char *end, *base;
15779   char *affix;
15780   const struct asm_opcode *opcode;
15781   const struct asm_cond *cond;
15782   char save[2];
15783
15784   /* Scan up to the end of the mnemonic, which must end in white space,
15785      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
15786   for (base = end = *str; *end != '\0'; end++)
15787     if (*end == ' ' || *end == '.')
15788       break;
15789
15790   if (end == base)
15791     return NULL;
15792
15793   /* Handle a possible width suffix and/or Neon type suffix.  */
15794   if (end[0] == '.')
15795     {
15796       int offset = 2;
15797
15798       /* The .w and .n suffixes are only valid if the unified syntax is in
15799          use.  */
15800       if (unified_syntax && end[1] == 'w')
15801         inst.size_req = 4;
15802       else if (unified_syntax && end[1] == 'n')
15803         inst.size_req = 2;
15804       else
15805         offset = 0;
15806
15807       inst.vectype.elems = 0;
15808
15809       *str = end + offset;
15810
15811       if (end[offset] == '.')
15812         {
15813           /* See if we have a Neon type suffix (possible in either unified or
15814              non-unified ARM syntax mode).  */
15815           if (parse_neon_type (&inst.vectype, str) == FAIL)
15816             return NULL;
15817         }
15818       else if (end[offset] != '\0' && end[offset] != ' ')
15819         return NULL;
15820     }
15821   else
15822     *str = end;
15823
15824   /* Look for unaffixed or special-case affixed mnemonic.  */
15825   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
15826                                                     end - base);
15827   if (opcode)
15828     {
15829       /* step U */
15830       if (opcode->tag < OT_odd_infix_0)
15831         {
15832           inst.cond = COND_ALWAYS;
15833           return opcode;
15834         }
15835
15836       if (warn_on_deprecated && unified_syntax)
15837         as_warn (_("conditional infixes are deprecated in unified syntax"));
15838       affix = base + (opcode->tag - OT_odd_infix_0);
15839       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
15840       gas_assert (cond);
15841
15842       inst.cond = cond->value;
15843       return opcode;
15844     }
15845
15846   /* Cannot have a conditional suffix on a mnemonic of less than two
15847      characters.  */
15848   if (end - base < 3)
15849     return NULL;
15850
15851   /* Look for suffixed mnemonic.  */
15852   affix = end - 2;
15853   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
15854   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
15855                                                     affix - base);
15856   if (opcode && cond)
15857     {
15858       /* step CE */
15859       switch (opcode->tag)
15860         {
15861         case OT_cinfix3_legacy:
15862           /* Ignore conditional suffixes matched on infix only mnemonics.  */
15863           break;
15864
15865         case OT_cinfix3:
15866         case OT_cinfix3_deprecated:
15867         case OT_odd_infix_unc:
15868           if (!unified_syntax)
15869             return 0;
15870           /* else fall through */
15871
15872         case OT_csuffix:
15873         case OT_csuffixF:
15874         case OT_csuf_or_in3:
15875           inst.cond = cond->value;
15876           return opcode;
15877
15878         case OT_unconditional:
15879         case OT_unconditionalF:
15880           if (thumb_mode)
15881             inst.cond = cond->value;
15882           else
15883             {
15884               /* Delayed diagnostic.  */
15885               inst.error = BAD_COND;
15886               inst.cond = COND_ALWAYS;
15887             }
15888           return opcode;
15889
15890         default:
15891           return NULL;
15892         }
15893     }
15894
15895   /* Cannot have a usual-position infix on a mnemonic of less than
15896      six characters (five would be a suffix).  */
15897   if (end - base < 6)
15898     return NULL;
15899
15900   /* Look for infixed mnemonic in the usual position.  */
15901   affix = base + 3;
15902   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
15903   if (!cond)
15904     return NULL;
15905
15906   memcpy (save, affix, 2);
15907   memmove (affix, affix + 2, (end - affix) - 2);
15908   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
15909                                                     (end - base) - 2);
15910   memmove (affix + 2, affix, (end - affix) - 2);
15911   memcpy (affix, save, 2);
15912
15913   if (opcode
15914       && (opcode->tag == OT_cinfix3
15915           || opcode->tag == OT_cinfix3_deprecated
15916           || opcode->tag == OT_csuf_or_in3
15917           || opcode->tag == OT_cinfix3_legacy))
15918     {
15919       /* Step CM.  */
15920       if (warn_on_deprecated && unified_syntax
15921           && (opcode->tag == OT_cinfix3
15922               || opcode->tag == OT_cinfix3_deprecated))
15923         as_warn (_("conditional infixes are deprecated in unified syntax"));
15924
15925       inst.cond = cond->value;
15926       return opcode;
15927     }
15928
15929   return NULL;
15930 }
15931
15932 /* This function generates an initial IT instruction, leaving its block
15933    virtually open for the new instructions. Eventually,
15934    the mask will be updated by now_it_add_mask () each time
15935    a new instruction needs to be included in the IT block.
15936    Finally, the block is closed with close_automatic_it_block ().
15937    The block closure can be requested either from md_assemble (),
15938    a tencode (), or due to a label hook.  */
15939
15940 static void
15941 new_automatic_it_block (int cond)
15942 {
15943   now_it.state = AUTOMATIC_IT_BLOCK;
15944   now_it.mask = 0x18;
15945   now_it.cc = cond;
15946   now_it.block_length = 1;
15947   mapping_state (MAP_THUMB);
15948   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
15949 }
15950
15951 /* Close an automatic IT block.
15952    See comments in new_automatic_it_block ().  */
15953
15954 static void
15955 close_automatic_it_block (void)
15956 {
15957   now_it.mask = 0x10;
15958   now_it.block_length = 0;
15959 }
15960
15961 /* Update the mask of the current automatically-generated IT
15962    instruction. See comments in new_automatic_it_block ().  */
15963
15964 static void
15965 now_it_add_mask (int cond)
15966 {
15967 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
15968 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
15969                                               | ((bitvalue) << (nbit)))
15970   const int resulting_bit = (cond & 1);
15971
15972   now_it.mask &= 0xf;
15973   now_it.mask = SET_BIT_VALUE (now_it.mask,
15974                                    resulting_bit,
15975                                   (5 - now_it.block_length));
15976   now_it.mask = SET_BIT_VALUE (now_it.mask,
15977                                    1,
15978                                    ((5 - now_it.block_length) - 1) );
15979   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
15980
15981 #undef CLEAR_BIT
15982 #undef SET_BIT_VALUE
15983 }
15984
15985 /* The IT blocks handling machinery is accessed through the these functions:
15986      it_fsm_pre_encode ()               from md_assemble ()
15987      set_it_insn_type ()                optional, from the tencode functions
15988      set_it_insn_type_last ()           ditto
15989      in_it_block ()                     ditto
15990      it_fsm_post_encode ()              from md_assemble ()
15991      force_automatic_it_block_close ()  from label habdling functions
15992
15993    Rationale:
15994      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
15995         initializing the IT insn type with a generic initial value depending
15996         on the inst.condition.
15997      2) During the tencode function, two things may happen:
15998         a) The tencode function overrides the IT insn type by
15999            calling either set_it_insn_type (type) or set_it_insn_type_last ().
16000         b) The tencode function queries the IT block state by
16001            calling in_it_block () (i.e. to determine narrow/not narrow mode).
16002
16003         Both set_it_insn_type and in_it_block run the internal FSM state
16004         handling function (handle_it_state), because: a) setting the IT insn
16005         type may incur in an invalid state (exiting the function),
16006         and b) querying the state requires the FSM to be updated.
16007         Specifically we want to avoid creating an IT block for conditional
16008         branches, so it_fsm_pre_encode is actually a guess and we can't
16009         determine whether an IT block is required until the tencode () routine
16010         has decided what type of instruction this actually it.
16011         Because of this, if set_it_insn_type and in_it_block have to be used,
16012         set_it_insn_type has to be called first.
16013
16014         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
16015         determines the insn IT type depending on the inst.cond code.
16016         When a tencode () routine encodes an instruction that can be
16017         either outside an IT block, or, in the case of being inside, has to be
16018         the last one, set_it_insn_type_last () will determine the proper
16019         IT instruction type based on the inst.cond code. Otherwise,
16020         set_it_insn_type can be called for overriding that logic or
16021         for covering other cases.
16022
16023         Calling handle_it_state () may not transition the IT block state to
16024         OUTSIDE_IT_BLOCK immediatelly, since the (current) state could be
16025         still queried. Instead, if the FSM determines that the state should
16026         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
16027         after the tencode () function: that's what it_fsm_post_encode () does.
16028
16029         Since in_it_block () calls the state handling function to get an
16030         updated state, an error may occur (due to invalid insns combination).
16031         In that case, inst.error is set.
16032         Therefore, inst.error has to be checked after the execution of
16033         the tencode () routine.
16034
16035      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
16036         any pending state change (if any) that didn't take place in
16037         handle_it_state () as explained above.  */
16038
16039 static void
16040 it_fsm_pre_encode (void)
16041 {
16042   if (inst.cond != COND_ALWAYS)
16043     inst.it_insn_type = INSIDE_IT_INSN;
16044   else
16045     inst.it_insn_type = OUTSIDE_IT_INSN;
16046
16047   now_it.state_handled = 0;
16048 }
16049
16050 /* IT state FSM handling function.  */
16051
16052 static int
16053 handle_it_state (void)
16054 {
16055   now_it.state_handled = 1;
16056
16057   switch (now_it.state)
16058     {
16059     case OUTSIDE_IT_BLOCK:
16060       switch (inst.it_insn_type)
16061         {
16062         case OUTSIDE_IT_INSN:
16063           break;
16064
16065         case INSIDE_IT_INSN:
16066         case INSIDE_IT_LAST_INSN:
16067           if (thumb_mode == 0)
16068             {
16069               if (unified_syntax
16070                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
16071                 as_tsktsk (_("Warning: conditional outside an IT block"\
16072                              " for Thumb."));
16073             }
16074           else
16075             {
16076               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
16077                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2))
16078                 {
16079                   /* Automatically generate the IT instruction.  */
16080                   new_automatic_it_block (inst.cond);
16081                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
16082                     close_automatic_it_block ();
16083                 }
16084               else
16085                 {
16086                   inst.error = BAD_OUT_IT;
16087                   return FAIL;
16088                 }
16089             }
16090           break;
16091
16092         case IF_INSIDE_IT_LAST_INSN:
16093         case NEUTRAL_IT_INSN:
16094           break;
16095
16096         case IT_INSN:
16097           now_it.state = MANUAL_IT_BLOCK;
16098           now_it.block_length = 0;
16099           break;
16100         }
16101       break;
16102
16103     case AUTOMATIC_IT_BLOCK:
16104       /* Three things may happen now:
16105          a) We should increment current it block size;
16106          b) We should close current it block (closing insn or 4 insns);
16107          c) We should close current it block and start a new one (due
16108          to incompatible conditions or
16109          4 insns-length block reached).  */
16110
16111       switch (inst.it_insn_type)
16112         {
16113         case OUTSIDE_IT_INSN:
16114           /* The closure of the block shall happen immediatelly,
16115              so any in_it_block () call reports the block as closed.  */
16116           force_automatic_it_block_close ();
16117           break;
16118
16119         case INSIDE_IT_INSN:
16120         case INSIDE_IT_LAST_INSN:
16121         case IF_INSIDE_IT_LAST_INSN:
16122           now_it.block_length++;
16123
16124           if (now_it.block_length > 4
16125               || !now_it_compatible (inst.cond))
16126             {
16127               force_automatic_it_block_close ();
16128               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
16129                 new_automatic_it_block (inst.cond);
16130             }
16131           else
16132             {
16133               now_it_add_mask (inst.cond);
16134             }
16135
16136           if (now_it.state == AUTOMATIC_IT_BLOCK
16137               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
16138                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
16139             close_automatic_it_block ();
16140           break;
16141
16142         case NEUTRAL_IT_INSN:
16143           now_it.block_length++;
16144
16145           if (now_it.block_length > 4)
16146             force_automatic_it_block_close ();
16147           else
16148             now_it_add_mask (now_it.cc & 1);
16149           break;
16150
16151         case IT_INSN:
16152           close_automatic_it_block ();
16153           now_it.state = MANUAL_IT_BLOCK;
16154           break;
16155         }
16156       break;
16157
16158     case MANUAL_IT_BLOCK:
16159       {
16160         /* Check conditional suffixes.  */
16161         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
16162         int is_last;
16163         now_it.mask <<= 1;
16164         now_it.mask &= 0x1f;
16165         is_last = (now_it.mask == 0x10);
16166
16167         switch (inst.it_insn_type)
16168           {
16169           case OUTSIDE_IT_INSN:
16170             inst.error = BAD_NOT_IT;
16171             return FAIL;
16172
16173           case INSIDE_IT_INSN:
16174             if (cond != inst.cond)
16175               {
16176                 inst.error = BAD_IT_COND;
16177                 return FAIL;
16178               }
16179             break;
16180
16181           case INSIDE_IT_LAST_INSN:
16182           case IF_INSIDE_IT_LAST_INSN:
16183             if (cond != inst.cond)
16184               {
16185                 inst.error = BAD_IT_COND;
16186                 return FAIL;
16187               }
16188             if (!is_last)
16189               {
16190                 inst.error = BAD_BRANCH;
16191                 return FAIL;
16192               }
16193             break;
16194
16195           case NEUTRAL_IT_INSN:
16196             /* The BKPT instruction is unconditional even in an IT block.  */
16197             break;
16198
16199           case IT_INSN:
16200             inst.error = BAD_IT_IT;
16201             return FAIL;
16202           }
16203       }
16204       break;
16205     }
16206
16207   return SUCCESS;
16208 }
16209
16210 static void
16211 it_fsm_post_encode (void)
16212 {
16213   int is_last;
16214
16215   if (!now_it.state_handled)
16216     handle_it_state ();
16217
16218   is_last = (now_it.mask == 0x10);
16219   if (is_last)
16220     {
16221       now_it.state = OUTSIDE_IT_BLOCK;
16222       now_it.mask = 0;
16223     }
16224 }
16225
16226 static void
16227 force_automatic_it_block_close (void)
16228 {
16229   if (now_it.state == AUTOMATIC_IT_BLOCK)
16230     {
16231       close_automatic_it_block ();
16232       now_it.state = OUTSIDE_IT_BLOCK;
16233       now_it.mask = 0;
16234     }
16235 }
16236
16237 static int
16238 in_it_block (void)
16239 {
16240   if (!now_it.state_handled)
16241     handle_it_state ();
16242
16243   return now_it.state != OUTSIDE_IT_BLOCK;
16244 }
16245
16246 void
16247 md_assemble (char *str)
16248 {
16249   char *p = str;
16250   const struct asm_opcode * opcode;
16251
16252   /* Align the previous label if needed.  */
16253   if (last_label_seen != NULL)
16254     {
16255       symbol_set_frag (last_label_seen, frag_now);
16256       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
16257       S_SET_SEGMENT (last_label_seen, now_seg);
16258     }
16259
16260   memset (&inst, '\0', sizeof (inst));
16261   inst.reloc.type = BFD_RELOC_UNUSED;
16262
16263   opcode = opcode_lookup (&p);
16264   if (!opcode)
16265     {
16266       /* It wasn't an instruction, but it might be a register alias of
16267          the form alias .req reg, or a Neon .dn/.qn directive.  */
16268       if (! create_register_alias (str, p)
16269           && ! create_neon_reg_alias (str, p))
16270         as_bad (_("bad instruction `%s'"), str);
16271
16272       return;
16273     }
16274
16275   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
16276     as_warn (_("s suffix on comparison instruction is deprecated"));
16277
16278   /* The value which unconditional instructions should have in place of the
16279      condition field.  */
16280   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
16281
16282   if (thumb_mode)
16283     {
16284       arm_feature_set variant;
16285
16286       variant = cpu_variant;
16287       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
16288       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
16289         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
16290       /* Check that this instruction is supported for this CPU.  */
16291       if (!opcode->tvariant
16292           || (thumb_mode == 1
16293               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
16294         {
16295           as_bad (_("selected processor does not support Thumb mode `%s'"), str);
16296           return;
16297         }
16298       if (inst.cond != COND_ALWAYS && !unified_syntax
16299           && opcode->tencode != do_t_branch)
16300         {
16301           as_bad (_("Thumb does not support conditional execution"));
16302           return;
16303         }
16304
16305       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2))
16306         {
16307           if (opcode->tencode != do_t_blx && opcode->tencode != do_t_branch23
16308               && !(ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_msr)
16309                    || ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_barrier)))
16310             {
16311               /* Two things are addressed here.
16312                  1) Implicit require narrow instructions on Thumb-1.
16313                     This avoids relaxation accidentally introducing Thumb-2
16314                      instructions.
16315                  2) Reject wide instructions in non Thumb-2 cores.  */
16316               if (inst.size_req == 0)
16317                 inst.size_req = 2;
16318               else if (inst.size_req == 4)
16319                 {
16320                   as_bad (_("selected processor does not support Thumb-2 mode `%s'"), str);
16321                   return;
16322                 }
16323             }
16324         }
16325
16326       inst.instruction = opcode->tvalue;
16327
16328       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
16329         {
16330           /* Prepare the it_insn_type for those encodings that don't set
16331              it.  */
16332           it_fsm_pre_encode ();
16333
16334           opcode->tencode ();
16335
16336           it_fsm_post_encode ();
16337         }
16338
16339       if (!(inst.error || inst.relax))
16340         {
16341           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
16342           inst.size = (inst.instruction > 0xffff ? 4 : 2);
16343           if (inst.size_req && inst.size_req != inst.size)
16344             {
16345               as_bad (_("cannot honor width suffix -- `%s'"), str);
16346               return;
16347             }
16348         }
16349
16350       /* Something has gone badly wrong if we try to relax a fixed size
16351          instruction.  */
16352       gas_assert (inst.size_req == 0 || !inst.relax);
16353
16354       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
16355                               *opcode->tvariant);
16356       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
16357          set those bits when Thumb-2 32-bit instructions are seen.  ie.
16358          anything other than bl/blx and v6-M instructions.
16359          This is overly pessimistic for relaxable instructions.  */
16360       if (((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
16361            || inst.relax)
16362           && !(ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
16363                || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier)))
16364         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
16365                                 arm_ext_v6t2);
16366
16367       check_neon_suffixes;
16368
16369       if (!inst.error)
16370         {
16371           mapping_state (MAP_THUMB);
16372         }
16373     }
16374   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
16375     {
16376       bfd_boolean is_bx;
16377
16378       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
16379       is_bx = (opcode->aencode == do_bx);
16380
16381       /* Check that this instruction is supported for this CPU.  */
16382       if (!(is_bx && fix_v4bx)
16383           && !(opcode->avariant &&
16384                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
16385         {
16386           as_bad (_("selected processor does not support ARM mode `%s'"), str);
16387           return;
16388         }
16389       if (inst.size_req)
16390         {
16391           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
16392           return;
16393         }
16394
16395       inst.instruction = opcode->avalue;
16396       if (opcode->tag == OT_unconditionalF)
16397         inst.instruction |= 0xF << 28;
16398       else
16399         inst.instruction |= inst.cond << 28;
16400       inst.size = INSN_SIZE;
16401       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
16402         {
16403           it_fsm_pre_encode ();
16404           opcode->aencode ();
16405           it_fsm_post_encode ();
16406         }
16407       /* Arm mode bx is marked as both v4T and v5 because it's still required
16408          on a hypothetical non-thumb v5 core.  */
16409       if (is_bx)
16410         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
16411       else
16412         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
16413                                 *opcode->avariant);
16414
16415       check_neon_suffixes;
16416
16417       if (!inst.error)
16418         {
16419           mapping_state (MAP_ARM);
16420         }
16421     }
16422   else
16423     {
16424       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
16425                 "-- `%s'"), str);
16426       return;
16427     }
16428   output_inst (str);
16429 }
16430
16431 static void
16432 check_it_blocks_finished (void)
16433 {
16434 #ifdef OBJ_ELF
16435   asection *sect;
16436
16437   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
16438     if (seg_info (sect)->tc_segment_info_data.current_it.state
16439         == MANUAL_IT_BLOCK)
16440       {
16441         as_warn (_("section '%s' finished with an open IT block."),
16442                  sect->name);
16443       }
16444 #else
16445   if (now_it.state == MANUAL_IT_BLOCK)
16446     as_warn (_("file finished with an open IT block."));
16447 #endif
16448 }
16449
16450 /* Various frobbings of labels and their addresses.  */
16451
16452 void
16453 arm_start_line_hook (void)
16454 {
16455   last_label_seen = NULL;
16456 }
16457
16458 void
16459 arm_frob_label (symbolS * sym)
16460 {
16461   last_label_seen = sym;
16462
16463   ARM_SET_THUMB (sym, thumb_mode);
16464
16465 #if defined OBJ_COFF || defined OBJ_ELF
16466   ARM_SET_INTERWORK (sym, support_interwork);
16467 #endif
16468
16469   force_automatic_it_block_close ();
16470
16471   /* Note - do not allow local symbols (.Lxxx) to be labelled
16472      as Thumb functions.  This is because these labels, whilst
16473      they exist inside Thumb code, are not the entry points for
16474      possible ARM->Thumb calls.  Also, these labels can be used
16475      as part of a computed goto or switch statement.  eg gcc
16476      can generate code that looks like this:
16477
16478                 ldr  r2, [pc, .Laaa]
16479                 lsl  r3, r3, #2
16480                 ldr  r2, [r3, r2]
16481                 mov  pc, r2
16482
16483        .Lbbb:  .word .Lxxx
16484        .Lccc:  .word .Lyyy
16485        ..etc...
16486        .Laaa:   .word Lbbb
16487
16488      The first instruction loads the address of the jump table.
16489      The second instruction converts a table index into a byte offset.
16490      The third instruction gets the jump address out of the table.
16491      The fourth instruction performs the jump.
16492
16493      If the address stored at .Laaa is that of a symbol which has the
16494      Thumb_Func bit set, then the linker will arrange for this address
16495      to have the bottom bit set, which in turn would mean that the
16496      address computation performed by the third instruction would end
16497      up with the bottom bit set.  Since the ARM is capable of unaligned
16498      word loads, the instruction would then load the incorrect address
16499      out of the jump table, and chaos would ensue.  */
16500   if (label_is_thumb_function_name
16501       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
16502       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
16503     {
16504       /* When the address of a Thumb function is taken the bottom
16505          bit of that address should be set.  This will allow
16506          interworking between Arm and Thumb functions to work
16507          correctly.  */
16508
16509       THUMB_SET_FUNC (sym, 1);
16510
16511       label_is_thumb_function_name = FALSE;
16512     }
16513
16514   dwarf2_emit_label (sym);
16515 }
16516
16517 bfd_boolean
16518 arm_data_in_code (void)
16519 {
16520   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
16521     {
16522       *input_line_pointer = '/';
16523       input_line_pointer += 5;
16524       *input_line_pointer = 0;
16525       return TRUE;
16526     }
16527
16528   return FALSE;
16529 }
16530
16531 char *
16532 arm_canonicalize_symbol_name (char * name)
16533 {
16534   int len;
16535
16536   if (thumb_mode && (len = strlen (name)) > 5
16537       && streq (name + len - 5, "/data"))
16538     *(name + len - 5) = 0;
16539
16540   return name;
16541 }
16542 \f
16543 /* Table of all register names defined by default.  The user can
16544    define additional names with .req.  Note that all register names
16545    should appear in both upper and lowercase variants.  Some registers
16546    also have mixed-case names.  */
16547
16548 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
16549 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
16550 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
16551 #define REGSET(p,t) \
16552   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
16553   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
16554   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
16555   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
16556 #define REGSETH(p,t) \
16557   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
16558   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
16559   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
16560   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
16561 #define REGSET2(p,t) \
16562   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
16563   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
16564   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
16565   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
16566 #define SPLRBANK(base,bank,t) \
16567   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
16568   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
16569   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
16570   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
16571   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
16572   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
16573
16574 static const struct reg_entry reg_names[] =
16575 {
16576   /* ARM integer registers.  */
16577   REGSET(r, RN), REGSET(R, RN),
16578
16579   /* ATPCS synonyms.  */
16580   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
16581   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
16582   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
16583
16584   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
16585   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
16586   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
16587
16588   /* Well-known aliases.  */
16589   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
16590   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
16591
16592   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
16593   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
16594
16595   /* Coprocessor numbers.  */
16596   REGSET(p, CP), REGSET(P, CP),
16597
16598   /* Coprocessor register numbers.  The "cr" variants are for backward
16599      compatibility.  */
16600   REGSET(c,  CN), REGSET(C, CN),
16601   REGSET(cr, CN), REGSET(CR, CN),
16602
16603   /* ARM banked registers.  */
16604   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
16605   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
16606   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
16607   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
16608   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
16609   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
16610   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
16611
16612   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
16613   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
16614   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
16615   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
16616   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
16617   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(SP_fiq,512|(13<<16),RNB),
16618   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
16619   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
16620
16621   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
16622   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
16623   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
16624   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
16625   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
16626   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
16627   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
16628   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB), 
16629   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
16630
16631   /* FPA registers.  */
16632   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
16633   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
16634
16635   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
16636   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
16637
16638   /* VFP SP registers.  */
16639   REGSET(s,VFS),  REGSET(S,VFS),
16640   REGSETH(s,VFS), REGSETH(S,VFS),
16641
16642   /* VFP DP Registers.  */
16643   REGSET(d,VFD),  REGSET(D,VFD),
16644   /* Extra Neon DP registers.  */
16645   REGSETH(d,VFD), REGSETH(D,VFD),
16646
16647   /* Neon QP registers.  */
16648   REGSET2(q,NQ),  REGSET2(Q,NQ),
16649
16650   /* VFP control registers.  */
16651   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
16652   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
16653   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
16654   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
16655   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
16656   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
16657
16658   /* Maverick DSP coprocessor registers.  */
16659   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
16660   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
16661
16662   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
16663   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
16664   REGDEF(dspsc,0,DSPSC),
16665
16666   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
16667   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
16668   REGDEF(DSPSC,0,DSPSC),
16669
16670   /* iWMMXt data registers - p0, c0-15.  */
16671   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
16672
16673   /* iWMMXt control registers - p1, c0-3.  */
16674   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
16675   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
16676   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
16677   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
16678
16679   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
16680   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
16681   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
16682   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
16683   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
16684
16685   /* XScale accumulator registers.  */
16686   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
16687 };
16688 #undef REGDEF
16689 #undef REGNUM
16690 #undef REGSET
16691
16692 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
16693    within psr_required_here.  */
16694 static const struct asm_psr psrs[] =
16695 {
16696   /* Backward compatibility notation.  Note that "all" is no longer
16697      truly all possible PSR bits.  */
16698   {"all",  PSR_c | PSR_f},
16699   {"flg",  PSR_f},
16700   {"ctl",  PSR_c},
16701
16702   /* Individual flags.  */
16703   {"f",    PSR_f},
16704   {"c",    PSR_c},
16705   {"x",    PSR_x},
16706   {"s",    PSR_s},
16707
16708   /* Combinations of flags.  */
16709   {"fs",   PSR_f | PSR_s},
16710   {"fx",   PSR_f | PSR_x},
16711   {"fc",   PSR_f | PSR_c},
16712   {"sf",   PSR_s | PSR_f},
16713   {"sx",   PSR_s | PSR_x},
16714   {"sc",   PSR_s | PSR_c},
16715   {"xf",   PSR_x | PSR_f},
16716   {"xs",   PSR_x | PSR_s},
16717   {"xc",   PSR_x | PSR_c},
16718   {"cf",   PSR_c | PSR_f},
16719   {"cs",   PSR_c | PSR_s},
16720   {"cx",   PSR_c | PSR_x},
16721   {"fsx",  PSR_f | PSR_s | PSR_x},
16722   {"fsc",  PSR_f | PSR_s | PSR_c},
16723   {"fxs",  PSR_f | PSR_x | PSR_s},
16724   {"fxc",  PSR_f | PSR_x | PSR_c},
16725   {"fcs",  PSR_f | PSR_c | PSR_s},
16726   {"fcx",  PSR_f | PSR_c | PSR_x},
16727   {"sfx",  PSR_s | PSR_f | PSR_x},
16728   {"sfc",  PSR_s | PSR_f | PSR_c},
16729   {"sxf",  PSR_s | PSR_x | PSR_f},
16730   {"sxc",  PSR_s | PSR_x | PSR_c},
16731   {"scf",  PSR_s | PSR_c | PSR_f},
16732   {"scx",  PSR_s | PSR_c | PSR_x},
16733   {"xfs",  PSR_x | PSR_f | PSR_s},
16734   {"xfc",  PSR_x | PSR_f | PSR_c},
16735   {"xsf",  PSR_x | PSR_s | PSR_f},
16736   {"xsc",  PSR_x | PSR_s | PSR_c},
16737   {"xcf",  PSR_x | PSR_c | PSR_f},
16738   {"xcs",  PSR_x | PSR_c | PSR_s},
16739   {"cfs",  PSR_c | PSR_f | PSR_s},
16740   {"cfx",  PSR_c | PSR_f | PSR_x},
16741   {"csf",  PSR_c | PSR_s | PSR_f},
16742   {"csx",  PSR_c | PSR_s | PSR_x},
16743   {"cxf",  PSR_c | PSR_x | PSR_f},
16744   {"cxs",  PSR_c | PSR_x | PSR_s},
16745   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
16746   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
16747   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
16748   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
16749   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
16750   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
16751   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
16752   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
16753   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
16754   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
16755   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
16756   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
16757   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
16758   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
16759   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
16760   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
16761   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
16762   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
16763   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
16764   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
16765   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
16766   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
16767   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
16768   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
16769 };
16770
16771 /* Table of V7M psr names.  */
16772 static const struct asm_psr v7m_psrs[] =
16773 {
16774   {"apsr",        0 }, {"APSR",         0 },
16775   {"iapsr",       1 }, {"IAPSR",        1 },
16776   {"eapsr",       2 }, {"EAPSR",        2 },
16777   {"psr",         3 }, {"PSR",          3 },
16778   {"xpsr",        3 }, {"XPSR",         3 }, {"xPSR",     3 },
16779   {"ipsr",        5 }, {"IPSR",         5 },
16780   {"epsr",        6 }, {"EPSR",         6 },
16781   {"iepsr",       7 }, {"IEPSR",        7 },
16782   {"msp",         8 }, {"MSP",          8 },
16783   {"psp",         9 }, {"PSP",          9 },
16784   {"primask",     16}, {"PRIMASK",      16},
16785   {"basepri",     17}, {"BASEPRI",      17},
16786   {"basepri_max", 18}, {"BASEPRI_MAX",  18},
16787   {"basepri_max", 18}, {"BASEPRI_MASK", 18}, /* Typo, preserved for backwards compatibility.  */
16788   {"faultmask",   19}, {"FAULTMASK",    19},
16789   {"control",     20}, {"CONTROL",      20}
16790 };
16791
16792 /* Table of all shift-in-operand names.  */
16793 static const struct asm_shift_name shift_names [] =
16794 {
16795   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
16796   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
16797   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
16798   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
16799   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
16800   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
16801 };
16802
16803 /* Table of all explicit relocation names.  */
16804 #ifdef OBJ_ELF
16805 static struct reloc_entry reloc_names[] =
16806 {
16807   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
16808   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
16809   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
16810   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
16811   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
16812   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
16813   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
16814   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
16815   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
16816   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
16817   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
16818   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
16819   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
16820         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
16821   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
16822         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
16823   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
16824         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ}
16825 };
16826 #endif
16827
16828 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
16829 static const struct asm_cond conds[] =
16830 {
16831   {"eq", 0x0},
16832   {"ne", 0x1},
16833   {"cs", 0x2}, {"hs", 0x2},
16834   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
16835   {"mi", 0x4},
16836   {"pl", 0x5},
16837   {"vs", 0x6},
16838   {"vc", 0x7},
16839   {"hi", 0x8},
16840   {"ls", 0x9},
16841   {"ge", 0xa},
16842   {"lt", 0xb},
16843   {"gt", 0xc},
16844   {"le", 0xd},
16845   {"al", 0xe}
16846 };
16847
16848 static struct asm_barrier_opt barrier_opt_names[] =
16849 {
16850   { "sy",    0xf }, { "SY",    0xf },
16851   { "un",    0x7 }, { "UN",    0x7 },
16852   { "st",    0xe }, { "ST",    0xe },
16853   { "unst",  0x6 }, { "UNST",  0x6 },
16854   { "ish",   0xb }, { "ISH",   0xb },
16855   { "sh",    0xb }, { "SH",    0xb },
16856   { "ishst", 0xa }, { "ISHST", 0xa },
16857   { "shst",  0xa }, { "SHST",  0xa },
16858   { "nsh",   0x7 }, { "NSH",   0x7 },
16859   { "nshst", 0x6 }, { "NSHST", 0x6 },
16860   { "osh",   0x3 }, { "OSH",   0x3 },
16861   { "oshst", 0x2 }, { "OSHST", 0x2 }
16862 };
16863
16864 /* Table of ARM-format instructions.    */
16865
16866 /* Macros for gluing together operand strings.  N.B. In all cases
16867    other than OPS0, the trailing OP_stop comes from default
16868    zero-initialization of the unspecified elements of the array.  */
16869 #define OPS0()            { OP_stop, }
16870 #define OPS1(a)           { OP_##a, }
16871 #define OPS2(a,b)         { OP_##a,OP_##b, }
16872 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
16873 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
16874 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
16875 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
16876
16877 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
16878    This is useful when mixing operands for ARM and THUMB, i.e. using the
16879    MIX_ARM_THUMB_OPERANDS macro.
16880    In order to use these macros, prefix the number of operands with _
16881    e.g. _3.  */
16882 #define OPS_1(a)           { a, }
16883 #define OPS_2(a,b)         { a,b, }
16884 #define OPS_3(a,b,c)       { a,b,c, }
16885 #define OPS_4(a,b,c,d)     { a,b,c,d, }
16886 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
16887 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
16888
16889 /* These macros abstract out the exact format of the mnemonic table and
16890    save some repeated characters.  */
16891
16892 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
16893 #define TxCE(mnem, op, top, nops, ops, ae, te) \
16894   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
16895     THUMB_VARIANT, do_##ae, do_##te }
16896
16897 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
16898    a T_MNEM_xyz enumerator.  */
16899 #define TCE(mnem, aop, top, nops, ops, ae, te) \
16900       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
16901 #define tCE(mnem, aop, top, nops, ops, ae, te) \
16902       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
16903
16904 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
16905    infix after the third character.  */
16906 #define TxC3(mnem, op, top, nops, ops, ae, te) \
16907   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
16908     THUMB_VARIANT, do_##ae, do_##te }
16909 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
16910   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
16911     THUMB_VARIANT, do_##ae, do_##te }
16912 #define TC3(mnem, aop, top, nops, ops, ae, te) \
16913       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
16914 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
16915       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
16916 #define tC3(mnem, aop, top, nops, ops, ae, te) \
16917       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
16918 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
16919       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
16920
16921 /* Mnemonic with a conditional infix in an unusual place.  Each and every variant has to
16922    appear in the condition table.  */
16923 #define TxCM_(m1, m2, m3, op, top, nops, ops, ae, te)   \
16924   { m1 #m2 m3, OPS##nops ops, sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
16925     0x##op, top, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##te }
16926
16927 #define TxCM(m1, m2, op, top, nops, ops, ae, te)        \
16928   TxCM_ (m1,   , m2, op, top, nops, ops, ae, te),       \
16929   TxCM_ (m1, eq, m2, op, top, nops, ops, ae, te),       \
16930   TxCM_ (m1, ne, m2, op, top, nops, ops, ae, te),       \
16931   TxCM_ (m1, cs, m2, op, top, nops, ops, ae, te),       \
16932   TxCM_ (m1, hs, m2, op, top, nops, ops, ae, te),       \
16933   TxCM_ (m1, cc, m2, op, top, nops, ops, ae, te),       \
16934   TxCM_ (m1, ul, m2, op, top, nops, ops, ae, te),       \
16935   TxCM_ (m1, lo, m2, op, top, nops, ops, ae, te),       \
16936   TxCM_ (m1, mi, m2, op, top, nops, ops, ae, te),       \
16937   TxCM_ (m1, pl, m2, op, top, nops, ops, ae, te),       \
16938   TxCM_ (m1, vs, m2, op, top, nops, ops, ae, te),       \
16939   TxCM_ (m1, vc, m2, op, top, nops, ops, ae, te),       \
16940   TxCM_ (m1, hi, m2, op, top, nops, ops, ae, te),       \
16941   TxCM_ (m1, ls, m2, op, top, nops, ops, ae, te),       \
16942   TxCM_ (m1, ge, m2, op, top, nops, ops, ae, te),       \
16943   TxCM_ (m1, lt, m2, op, top, nops, ops, ae, te),       \
16944   TxCM_ (m1, gt, m2, op, top, nops, ops, ae, te),       \
16945   TxCM_ (m1, le, m2, op, top, nops, ops, ae, te),       \
16946   TxCM_ (m1, al, m2, op, top, nops, ops, ae, te)
16947
16948 #define TCM(m1,m2, aop, top, nops, ops, ae, te)         \
16949       TxCM (m1,m2, aop, 0x##top, nops, ops, ae, te)
16950 #define tCM(m1,m2, aop, top, nops, ops, ae, te)         \
16951       TxCM (m1,m2, aop, T_MNEM##top, nops, ops, ae, te)
16952
16953 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
16954    field is still 0xE.  Many of the Thumb variants can be executed
16955    conditionally, so this is checked separately.  */
16956 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
16957   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
16958     THUMB_VARIANT, do_##ae, do_##te }
16959
16960 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
16961    condition code field.  */
16962 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
16963   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
16964     THUMB_VARIANT, do_##ae, do_##te }
16965
16966 /* ARM-only variants of all the above.  */
16967 #define CE(mnem,  op, nops, ops, ae)    \
16968   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
16969
16970 #define C3(mnem, op, nops, ops, ae)     \
16971   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
16972
16973 /* Legacy mnemonics that always have conditional infix after the third
16974    character.  */
16975 #define CL(mnem, op, nops, ops, ae)     \
16976   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
16977     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
16978
16979 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
16980 #define cCE(mnem,  op, nops, ops, ae)   \
16981   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
16982
16983 /* Legacy coprocessor instructions where conditional infix and conditional
16984    suffix are ambiguous.  For consistency this includes all FPA instructions,
16985    not just the potentially ambiguous ones.  */
16986 #define cCL(mnem, op, nops, ops, ae)    \
16987   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
16988     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
16989
16990 /* Coprocessor, takes either a suffix or a position-3 infix
16991    (for an FPA corner case). */
16992 #define C3E(mnem, op, nops, ops, ae) \
16993   { mnem, OPS##nops ops, OT_csuf_or_in3, \
16994     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
16995
16996 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
16997   { m1 #m2 m3, OPS##nops ops, \
16998     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
16999     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
17000
17001 #define CM(m1, m2, op, nops, ops, ae)   \
17002   xCM_ (m1,   , m2, op, nops, ops, ae), \
17003   xCM_ (m1, eq, m2, op, nops, ops, ae), \
17004   xCM_ (m1, ne, m2, op, nops, ops, ae), \
17005   xCM_ (m1, cs, m2, op, nops, ops, ae), \
17006   xCM_ (m1, hs, m2, op, nops, ops, ae), \
17007   xCM_ (m1, cc, m2, op, nops, ops, ae), \
17008   xCM_ (m1, ul, m2, op, nops, ops, ae), \
17009   xCM_ (m1, lo, m2, op, nops, ops, ae), \
17010   xCM_ (m1, mi, m2, op, nops, ops, ae), \
17011   xCM_ (m1, pl, m2, op, nops, ops, ae), \
17012   xCM_ (m1, vs, m2, op, nops, ops, ae), \
17013   xCM_ (m1, vc, m2, op, nops, ops, ae), \
17014   xCM_ (m1, hi, m2, op, nops, ops, ae), \
17015   xCM_ (m1, ls, m2, op, nops, ops, ae), \
17016   xCM_ (m1, ge, m2, op, nops, ops, ae), \
17017   xCM_ (m1, lt, m2, op, nops, ops, ae), \
17018   xCM_ (m1, gt, m2, op, nops, ops, ae), \
17019   xCM_ (m1, le, m2, op, nops, ops, ae), \
17020   xCM_ (m1, al, m2, op, nops, ops, ae)
17021
17022 #define UE(mnem, op, nops, ops, ae)     \
17023   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
17024
17025 #define UF(mnem, op, nops, ops, ae)     \
17026   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
17027
17028 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
17029    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
17030    use the same encoding function for each.  */
17031 #define NUF(mnem, op, nops, ops, enc)                                   \
17032   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
17033     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
17034
17035 /* Neon data processing, version which indirects through neon_enc_tab for
17036    the various overloaded versions of opcodes.  */
17037 #define nUF(mnem, op, nops, ops, enc)                                   \
17038   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
17039     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
17040
17041 /* Neon insn with conditional suffix for the ARM version, non-overloaded
17042    version.  */
17043 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
17044   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
17045     THUMB_VARIANT, do_##enc, do_##enc }
17046
17047 #define NCE(mnem, op, nops, ops, enc)                                   \
17048    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
17049
17050 #define NCEF(mnem, op, nops, ops, enc)                                  \
17051     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
17052
17053 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
17054 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
17055   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
17056     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
17057
17058 #define nCE(mnem, op, nops, ops, enc)                                   \
17059    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
17060
17061 #define nCEF(mnem, op, nops, ops, enc)                                  \
17062     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
17063
17064 #define do_0 0
17065
17066 static const struct asm_opcode insns[] =
17067 {
17068 #define ARM_VARIANT &arm_ext_v1 /* Core ARM Instructions.  */
17069 #define THUMB_VARIANT &arm_ext_v4t
17070  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
17071  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
17072  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
17073  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
17074  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
17075  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
17076  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
17077  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
17078  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
17079  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
17080  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
17081  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
17082  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
17083  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
17084  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
17085  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
17086
17087  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
17088     for setting PSR flag bits.  They are obsolete in V6 and do not
17089     have Thumb equivalents. */
17090  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
17091  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
17092   CL("tstp",    110f000,           2, (RR, SH),      cmp),
17093  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
17094  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
17095   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
17096  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
17097  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
17098   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
17099
17100  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
17101  tC3("movs",    1b00000, _movs,    2, (RR, SH),      mov,  t_mov_cmp),
17102  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
17103  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
17104
17105  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
17106  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
17107  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
17108                                                                 OP_RRnpc),
17109                                         OP_ADDRGLDR),ldst, t_ldst),
17110  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
17111
17112  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17113  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17114  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17115  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17116  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17117  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
17118
17119  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
17120  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
17121  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
17122  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
17123
17124   /* Pseudo ops.  */
17125  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
17126   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
17127  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
17128
17129   /* Thumb-compatibility pseudo ops.  */
17130  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
17131  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
17132  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
17133  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
17134  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
17135  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
17136  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
17137  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
17138  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
17139  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
17140  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
17141  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
17142
17143  /* These may simplify to neg.  */
17144  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
17145  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
17146
17147 #undef  THUMB_VARIANT
17148 #define THUMB_VARIANT  & arm_ext_v6
17149
17150  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
17151
17152  /* V1 instructions with no Thumb analogue prior to V6T2.  */
17153 #undef  THUMB_VARIANT
17154 #define THUMB_VARIANT  & arm_ext_v6t2
17155
17156  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
17157  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
17158   CL("teqp",    130f000,           2, (RR, SH),      cmp),
17159
17160  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17161  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17162  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
17163  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
17164
17165  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17166  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17167
17168  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17169  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
17170
17171  /* V1 instructions with no Thumb analogue at all.  */
17172   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
17173   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
17174
17175   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
17176   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
17177   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
17178   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
17179   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
17180   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
17181   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
17182   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
17183
17184 #undef  ARM_VARIANT
17185 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
17186 #undef  THUMB_VARIANT
17187 #define THUMB_VARIANT  & arm_ext_v4t
17188
17189  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
17190  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
17191
17192 #undef  THUMB_VARIANT
17193 #define THUMB_VARIANT  & arm_ext_v6t2
17194
17195  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
17196   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
17197
17198   /* Generic coprocessor instructions.  */
17199  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
17200  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17201  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17202  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17203  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17204  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
17205  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
17206
17207 #undef  ARM_VARIANT
17208 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
17209
17210   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
17211   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
17212
17213 #undef  ARM_VARIANT
17214 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
17215 #undef  THUMB_VARIANT
17216 #define THUMB_VARIANT  & arm_ext_msr
17217
17218  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
17219  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
17220
17221 #undef  ARM_VARIANT
17222 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
17223 #undef  THUMB_VARIANT
17224 #define THUMB_VARIANT  & arm_ext_v6t2
17225
17226  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17227   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17228  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17229   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17230  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17231   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17232  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
17233   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
17234
17235 #undef  ARM_VARIANT
17236 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
17237 #undef  THUMB_VARIANT
17238 #define THUMB_VARIANT  & arm_ext_v4t
17239
17240  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17241  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17242  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17243  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17244  tCM("ld","sh", 01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17245  tCM("ld","sb", 01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
17246
17247 #undef  ARM_VARIANT
17248 #define ARM_VARIANT  & arm_ext_v4t_5
17249
17250   /* ARM Architecture 4T.  */
17251   /* Note: bx (and blx) are required on V5, even if the processor does
17252      not support Thumb.  */
17253  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
17254
17255 #undef  ARM_VARIANT
17256 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
17257 #undef  THUMB_VARIANT
17258 #define THUMB_VARIANT  & arm_ext_v5t
17259
17260   /* Note: blx has 2 variants; the .value coded here is for
17261      BLX(2).  Only this variant has conditional execution.  */
17262  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
17263  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
17264
17265 #undef  THUMB_VARIANT
17266 #define THUMB_VARIANT  & arm_ext_v6t2
17267
17268  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
17269  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17270  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
17271  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
17272  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
17273  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
17274  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
17275  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
17276
17277 #undef  ARM_VARIANT
17278 #define ARM_VARIANT  & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
17279 #undef THUMB_VARIANT
17280 #define THUMB_VARIANT &arm_ext_v5exp
17281
17282  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17283  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17284  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17285  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17286
17287  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17288  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
17289
17290  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17291  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17292  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17293  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
17294
17295  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17296  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17297  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17298  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17299
17300  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17301  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
17302
17303  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17304  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17305  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17306  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
17307
17308 #undef  ARM_VARIANT
17309 #define ARM_VARIANT  & arm_ext_v5e /*  ARM Architecture 5TE.  */
17310 #undef THUMB_VARIANT
17311 #define THUMB_VARIANT &arm_ext_v6t2
17312
17313  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
17314  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
17315      ldrd, t_ldstd),
17316  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
17317                                        ADDRGLDRS), ldrd, t_ldstd),
17318
17319  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17320  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17321
17322 #undef  ARM_VARIANT
17323 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
17324
17325  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
17326
17327 #undef  ARM_VARIANT
17328 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
17329 #undef  THUMB_VARIANT
17330 #define THUMB_VARIANT  & arm_ext_v6
17331
17332  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
17333  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
17334  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
17335  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
17336  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
17337  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17338  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17339  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17340  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
17341  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
17342
17343 #undef  THUMB_VARIANT
17344 #define THUMB_VARIANT  & arm_ext_v6t2
17345
17346  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
17347  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
17348                                       strex,  t_strex),
17349  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17350  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
17351
17352  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
17353  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
17354
17355 /*  ARM V6 not included in V7M.  */
17356 #undef  THUMB_VARIANT
17357 #define THUMB_VARIANT  & arm_ext_v6_notm
17358  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
17359   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
17360   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
17361  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
17362  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
17363   UF(rfefa,     9900a00,           1, (RRw),                       rfe),
17364   UF(rfeea,     8100a00,           1, (RRw),                       rfe),
17365  TUF("rfeed",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
17366  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
17367   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
17368   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
17369  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
17370
17371 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
17372 #undef  THUMB_VARIANT
17373 #define THUMB_VARIANT  & arm_ext_v6_dsp
17374  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
17375  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
17376  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
17377  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17378  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17379  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17380  /* Old name for QASX.  */
17381  TCE("qaddsubx",        6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17382  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17383  /* Old name for QSAX.  */
17384  TCE("qsubaddx",        6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17385  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17386  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17387  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17388  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17389  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17390  /* Old name for SASX.  */
17391  TCE("saddsubx",        6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17392  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17393  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17394  TCE("shasx",     6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17395  /* Old name for SHASX.  */
17396  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17397  TCE("shsax",      6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),    rd_rn_rm, t_simd),
17398  /* Old name for SHSAX.  */
17399  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17400  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17401  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17402  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17403  /* Old name for SSAX.  */
17404  TCE("ssubaddx",        6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17405  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17406  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17407  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17408  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17409  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17410  /* Old name for UASX.  */
17411  TCE("uaddsubx",        6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17412  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17413  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17414  TCE("uhasx",     6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17415  /* Old name for UHASX.  */
17416  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17417  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17418  /* Old name for UHSAX.  */
17419  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17420  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17421  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17422  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17423  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17424  TCE("uqasx",     6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17425  /* Old name for UQASX.  */
17426  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17427  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17428  /* Old name for UQSAX.  */
17429  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
17430  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17431  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17432  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17433  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17434  /* Old name for USAX.  */
17435  TCE("usubaddx",        6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17436  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17437  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17438  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17439  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17440  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
17441  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17442  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17443  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
17444  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
17445  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
17446  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17447  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17448  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17449  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17450  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17451  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17452  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17453  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
17454  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17455  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17456  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17457  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
17458  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17459  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17460  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17461  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17462  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17463  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
17464  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
17465  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
17466  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
17467  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
17468  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
17469
17470 #undef  ARM_VARIANT
17471 #define ARM_VARIANT   & arm_ext_v6k
17472 #undef  THUMB_VARIANT
17473 #define THUMB_VARIANT & arm_ext_v6k
17474
17475  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
17476  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
17477  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
17478  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
17479
17480 #undef  THUMB_VARIANT
17481 #define THUMB_VARIANT  & arm_ext_v6_notm
17482  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
17483                                       ldrexd, t_ldrexd),
17484  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
17485                                        RRnpcb), strexd, t_strexd),
17486
17487 #undef  THUMB_VARIANT
17488 #define THUMB_VARIANT  & arm_ext_v6t2
17489  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
17490      rd_rn,  rd_rn),
17491  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
17492      rd_rn,  rd_rn),
17493  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
17494      strex, rm_rd_rn),
17495  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
17496      strex, rm_rd_rn), 
17497  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
17498
17499 #undef  ARM_VARIANT
17500 #define ARM_VARIANT    & arm_ext_sec
17501 #undef THUMB_VARIANT
17502 #define THUMB_VARIANT  & arm_ext_sec
17503
17504  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
17505
17506 #undef  ARM_VARIANT
17507 #define ARM_VARIANT    & arm_ext_virt
17508 #undef  THUMB_VARIANT
17509 #define THUMB_VARIANT    & arm_ext_virt
17510
17511  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
17512  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
17513
17514 #undef  ARM_VARIANT
17515 #define ARM_VARIANT  & arm_ext_v6t2
17516 #undef  THUMB_VARIANT
17517 #define THUMB_VARIANT  & arm_ext_v6t2
17518
17519  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
17520  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
17521  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
17522  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
17523
17524  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
17525  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
17526  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
17527  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
17528
17529  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17530  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17531  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17532  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
17533
17534  /* Thumb-only instructions.  */
17535 #undef ARM_VARIANT
17536 #define ARM_VARIANT NULL
17537   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
17538   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
17539
17540  /* ARM does not really have an IT instruction, so always allow it.
17541     The opcode is copied from Thumb in order to allow warnings in
17542     -mimplicit-it=[never | arm] modes.  */
17543 #undef  ARM_VARIANT
17544 #define ARM_VARIANT  & arm_ext_v1
17545
17546  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
17547  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
17548  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
17549  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
17550  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
17551  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
17552  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
17553  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
17554  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
17555  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
17556  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
17557  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
17558  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
17559  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
17560  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
17561  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
17562  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
17563  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
17564
17565  /* Thumb2 only instructions.  */
17566 #undef  ARM_VARIANT
17567 #define ARM_VARIANT  NULL
17568
17569  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
17570  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
17571  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
17572  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
17573  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
17574  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
17575
17576  /* Hardware division instructions.  */
17577 #undef  ARM_VARIANT
17578 #define ARM_VARIANT    & arm_ext_adiv
17579 #undef  THUMB_VARIANT
17580 #define THUMB_VARIANT  & arm_ext_div
17581
17582  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
17583  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
17584
17585  /* ARM V6M/V7 instructions.  */
17586 #undef  ARM_VARIANT
17587 #define ARM_VARIANT    & arm_ext_barrier
17588 #undef  THUMB_VARIANT
17589 #define THUMB_VARIANT  & arm_ext_barrier
17590
17591  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier,  t_barrier),
17592  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier,  t_barrier),
17593  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier,  t_barrier),
17594
17595  /* ARM V7 instructions.  */
17596 #undef  ARM_VARIANT
17597 #define ARM_VARIANT    & arm_ext_v7
17598 #undef  THUMB_VARIANT
17599 #define THUMB_VARIANT  & arm_ext_v7
17600
17601  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
17602  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
17603
17604 #undef ARM_VARIANT
17605 #define ARM_VARIANT    & arm_ext_mp
17606 #undef THUMB_VARIANT
17607 #define THUMB_VARIANT  & arm_ext_mp
17608
17609  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
17610
17611 #undef  ARM_VARIANT
17612 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
17613
17614  cCE("wfs",     e200110, 1, (RR),            rd),
17615  cCE("rfs",     e300110, 1, (RR),            rd),
17616  cCE("wfc",     e400110, 1, (RR),            rd),
17617  cCE("rfc",     e500110, 1, (RR),            rd),
17618
17619  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17620  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17621  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17622  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17623
17624  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17625  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17626  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17627  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
17628
17629  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
17630  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
17631  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
17632  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
17633  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
17634  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
17635  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
17636  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
17637  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
17638  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
17639  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
17640  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
17641
17642  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
17643  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
17644  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
17645  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
17646  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
17647  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
17648  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
17649  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
17650  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
17651  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
17652  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
17653  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
17654
17655  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
17656  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
17657  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
17658  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
17659  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
17660  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
17661  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
17662  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
17663  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
17664  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
17665  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
17666  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
17667
17668  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
17669  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
17670  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
17671  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
17672  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
17673  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
17674  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
17675  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
17676  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
17677  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
17678  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
17679  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
17680
17681  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
17682  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
17683  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
17684  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
17685  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
17686  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
17687  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
17688  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
17689  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
17690  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
17691  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
17692  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
17693
17694  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
17695  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
17696  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
17697  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
17698  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
17699  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
17700  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
17701  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
17702  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
17703  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
17704  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
17705  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
17706
17707  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
17708  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
17709  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
17710  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
17711  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
17712  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
17713  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
17714  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
17715  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
17716  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
17717  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
17718  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
17719
17720  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
17721  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
17722  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
17723  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
17724  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
17725  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
17726  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
17727  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
17728  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
17729  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
17730  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
17731  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
17732
17733  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
17734  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
17735  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
17736  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
17737  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
17738  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
17739  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
17740  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
17741  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
17742  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
17743  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
17744  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
17745
17746  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
17747  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
17748  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
17749  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
17750  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
17751  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
17752  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
17753  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
17754  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
17755  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
17756  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
17757  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
17758
17759  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
17760  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
17761  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
17762  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
17763  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
17764  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
17765  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
17766  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
17767  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
17768  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
17769  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
17770  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
17771
17772  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
17773  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
17774  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
17775  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
17776  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
17777  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
17778  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
17779  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
17780  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
17781  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
17782  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
17783  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
17784
17785  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
17786  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
17787  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
17788  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
17789  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
17790  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
17791  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
17792  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
17793  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
17794  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
17795  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
17796  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
17797
17798  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
17799  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
17800  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
17801  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
17802  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
17803  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
17804  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
17805  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
17806  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
17807  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
17808  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
17809  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
17810
17811  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
17812  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
17813  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
17814  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
17815  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
17816  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
17817  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
17818  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
17819  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
17820  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
17821  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
17822  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
17823
17824  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
17825  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
17826  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
17827  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
17828  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
17829  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
17830  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
17831  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
17832  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
17833  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
17834  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
17835  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
17836
17837  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
17838  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
17839  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
17840  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
17841  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
17842  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17843  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17844  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17845  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
17846  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
17847  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
17848  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
17849
17850  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
17851  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
17852  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
17853  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
17854  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
17855  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17856  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17857  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17858  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
17859  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
17860  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
17861  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
17862
17863  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
17864  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
17865  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
17866  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
17867  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
17868  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17869  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17870  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17871  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
17872  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
17873  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
17874  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
17875
17876  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
17877  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
17878  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
17879  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
17880  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
17881  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17882  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17883  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17884  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
17885  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
17886  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
17887  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
17888
17889  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
17890  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
17891  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
17892  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
17893  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
17894  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17895  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17896  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17897  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
17898  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
17899  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
17900  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
17901
17902  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
17903  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
17904  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
17905  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
17906  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
17907  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17908  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17909  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17910  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
17911  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
17912  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
17913  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
17914
17915  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
17916  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
17917  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
17918  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
17919  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
17920  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17921  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17922  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17923  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
17924  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
17925  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
17926  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
17927
17928  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
17929  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
17930  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
17931  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
17932  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
17933  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17934  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17935  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17936  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
17937  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
17938  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
17939  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
17940
17941  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
17942  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
17943  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
17944  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
17945  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
17946  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17947  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17948  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17949  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
17950  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
17951  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
17952  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
17953
17954  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
17955  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
17956  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
17957  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
17958  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
17959  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17960  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17961  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17962  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
17963  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
17964  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
17965  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
17966
17967  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
17968  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
17969  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
17970  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
17971  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
17972  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17973  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17974  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17975  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
17976  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
17977  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
17978  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
17979
17980  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
17981  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
17982  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
17983  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
17984  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
17985  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17986  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
17987  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
17988  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
17989  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
17990  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
17991  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
17992
17993  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
17994  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
17995  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
17996  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
17997  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
17998  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
17999  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
18000  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
18001  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
18002  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
18003  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
18004  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
18005
18006  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
18007  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
18008  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
18009  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
18010
18011  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
18012  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
18013  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
18014  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
18015  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
18016  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
18017  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
18018  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
18019  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
18020  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
18021  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
18022  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
18023
18024   /* The implementation of the FIX instruction is broken on some
18025      assemblers, in that it accepts a precision specifier as well as a
18026      rounding specifier, despite the fact that this is meaningless.
18027      To be more compatible, we accept it as well, though of course it
18028      does not set any bits.  */
18029  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
18030  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
18031  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
18032  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
18033  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
18034  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
18035  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
18036  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
18037  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
18038  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
18039  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
18040  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
18041  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
18042
18043   /* Instructions that were new with the real FPA, call them V2.  */
18044 #undef  ARM_VARIANT
18045 #define ARM_VARIANT  & fpu_fpa_ext_v2
18046
18047  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18048  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18049  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18050  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18051  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18052  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
18053
18054 #undef  ARM_VARIANT
18055 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
18056
18057   /* Moves and type conversions.  */
18058  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
18059  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
18060  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
18061  cCE("fmstat",  ef1fa10, 0, (),               noargs),
18062  cCE("vmrs",    ef10a10, 2, (APSR_RR, RVC),   vmrs),
18063  cCE("vmsr",    ee10a10, 2, (RVC, RR),        vmsr),
18064  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18065  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
18066  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
18067  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18068  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
18069  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18070  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
18071  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
18072
18073   /* Memory operations.  */
18074  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
18075  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
18076  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
18077  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
18078  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
18079  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
18080  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
18081  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
18082  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
18083  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
18084  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
18085  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
18086  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
18087  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
18088  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
18089  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
18090  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
18091  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
18092
18093   /* Monadic operations.  */
18094  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18095  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
18096  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18097
18098   /* Dyadic operations.  */
18099  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18100  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18101  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18102  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18103  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18104  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18105  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18106  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18107  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18108
18109   /* Comparisons.  */
18110  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
18111  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
18112  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
18113  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
18114
18115  /* Double precision load/store are still present on single precision
18116     implementations.  */
18117  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
18118  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
18119  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18120  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18121  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18122  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18123  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18124  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
18125  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18126  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
18127
18128 #undef  ARM_VARIANT
18129 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
18130
18131   /* Moves and type conversions.  */
18132  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
18133  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
18134  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18135  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
18136  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
18137  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
18138  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
18139  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
18140  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
18141  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18142  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18143  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18144  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
18145
18146   /* Monadic operations.  */
18147  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
18148  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
18149  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
18150
18151   /* Dyadic operations.  */
18152  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18153  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18154  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18155  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18156  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18157  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18158  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18159  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18160  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18161
18162   /* Comparisons.  */
18163  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
18164  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
18165  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
18166  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
18167
18168 #undef  ARM_VARIANT
18169 #define ARM_VARIANT  & fpu_vfp_ext_v2
18170
18171  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
18172  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
18173  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
18174  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
18175
18176 /* Instructions which may belong to either the Neon or VFP instruction sets.
18177    Individual encoder functions perform additional architecture checks.  */
18178 #undef  ARM_VARIANT
18179 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
18180 #undef  THUMB_VARIANT
18181 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
18182
18183   /* These mnemonics are unique to VFP.  */
18184  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
18185  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
18186  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18187  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18188  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18189  nCE(vcmp,      _vcmp,    2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
18190  nCE(vcmpe,     _vcmpe,   2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
18191  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
18192  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
18193  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
18194
18195   /* Mnemonics shared by Neon and VFP.  */
18196  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
18197  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
18198  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
18199
18200  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
18201  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
18202
18203  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
18204  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
18205
18206  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18207  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18208  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18209  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18210  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18211  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
18212  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
18213  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
18214
18215  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
18216  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
18217  nCEF(vcvtb,    _vcvt,   2, (RVS, RVS), neon_cvtb),
18218  nCEF(vcvtt,    _vcvt,   2, (RVS, RVS), neon_cvtt),
18219
18220
18221   /* NOTE: All VMOV encoding is special-cased!  */
18222  NCE(vmov,      0,       1, (VMOV), neon_mov),
18223  NCE(vmovq,     0,       1, (VMOV), neon_mov),
18224
18225 #undef  THUMB_VARIANT
18226 #define THUMB_VARIANT  & fpu_neon_ext_v1
18227 #undef  ARM_VARIANT
18228 #define ARM_VARIANT    & fpu_neon_ext_v1
18229
18230   /* Data processing with three registers of the same length.  */
18231   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
18232  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
18233  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
18234  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
18235  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
18236  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
18237  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
18238  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
18239  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
18240   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
18241  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
18242  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
18243  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
18244  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
18245  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
18246  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
18247  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
18248  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
18249   /* If not immediate, fall back to neon_dyadic_i64_su.
18250      shl_imm should accept I8 I16 I32 I64,
18251      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
18252  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
18253  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
18254  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
18255  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
18256   /* Logic ops, types optional & ignored.  */
18257  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18258  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18259  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18260  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18261  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18262  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18263  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
18264  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
18265  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
18266  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
18267   /* Bitfield ops, untyped.  */
18268  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
18269  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
18270  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
18271  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
18272  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
18273  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
18274   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F32.  */
18275  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
18276  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
18277  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
18278  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
18279  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
18280  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
18281   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
18282      back to neon_dyadic_if_su.  */
18283  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
18284  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
18285  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
18286  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
18287  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
18288  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
18289  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
18290  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
18291   /* Comparison. Type I8 I16 I32 F32.  */
18292  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
18293  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
18294   /* As above, D registers only.  */
18295  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
18296  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
18297   /* Int and float variants, signedness unimportant.  */
18298  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
18299  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
18300  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
18301   /* Add/sub take types I8 I16 I32 I64 F32.  */
18302  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
18303  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
18304   /* vtst takes sizes 8, 16, 32.  */
18305  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
18306  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
18307   /* VMUL takes I8 I16 I32 F32 P8.  */
18308  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
18309   /* VQD{R}MULH takes S16 S32.  */
18310  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
18311  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
18312  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
18313  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
18314  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
18315  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
18316  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
18317  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
18318  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
18319  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
18320  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
18321  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
18322  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
18323  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
18324  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
18325  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
18326
18327   /* Two address, int/float. Types S8 S16 S32 F32.  */
18328  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
18329  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
18330
18331   /* Data processing with two registers and a shift amount.  */
18332   /* Right shifts, and variants with rounding.
18333      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
18334  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
18335  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
18336  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
18337  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
18338  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
18339  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
18340  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
18341  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
18342   /* Shift and insert. Sizes accepted 8 16 32 64.  */
18343  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
18344  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
18345  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
18346  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
18347   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
18348  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
18349  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
18350   /* Right shift immediate, saturating & narrowing, with rounding variants.
18351      Types accepted S16 S32 S64 U16 U32 U64.  */
18352  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
18353  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
18354   /* As above, unsigned. Types accepted S16 S32 S64.  */
18355  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
18356  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
18357   /* Right shift narrowing. Types accepted I16 I32 I64.  */
18358  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
18359  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
18360   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
18361  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
18362   /* CVT with optional immediate for fixed-point variant.  */
18363  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
18364
18365  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
18366  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
18367
18368   /* Data processing, three registers of different lengths.  */
18369   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
18370  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
18371  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
18372  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
18373  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
18374   /* If not scalar, fall back to neon_dyadic_long.
18375      Vector types as above, scalar types S16 S32 U16 U32.  */
18376  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
18377  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
18378   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
18379  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
18380  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
18381   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
18382  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18383  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18384  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18385  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
18386   /* Saturating doubling multiplies. Types S16 S32.  */
18387  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
18388  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
18389  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
18390   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
18391      S16 S32 U16 U32.  */
18392  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
18393
18394   /* Extract. Size 8.  */
18395  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
18396  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
18397
18398   /* Two registers, miscellaneous.  */
18399   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
18400  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
18401  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
18402  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
18403  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
18404  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
18405  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
18406   /* Vector replicate. Sizes 8 16 32.  */
18407  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
18408  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
18409   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
18410  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
18411   /* VMOVN. Types I16 I32 I64.  */
18412  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
18413   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
18414  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
18415   /* VQMOVUN. Types S16 S32 S64.  */
18416  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
18417   /* VZIP / VUZP. Sizes 8 16 32.  */
18418  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
18419  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
18420  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
18421  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
18422   /* VQABS / VQNEG. Types S8 S16 S32.  */
18423  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
18424  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
18425  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
18426  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
18427   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
18428  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
18429  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
18430  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
18431  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
18432   /* Reciprocal estimates. Types U32 F32.  */
18433  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
18434  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
18435  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
18436  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
18437   /* VCLS. Types S8 S16 S32.  */
18438  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
18439  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
18440   /* VCLZ. Types I8 I16 I32.  */
18441  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
18442  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
18443   /* VCNT. Size 8.  */
18444  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
18445  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
18446   /* Two address, untyped.  */
18447  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
18448  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
18449   /* VTRN. Sizes 8 16 32.  */
18450  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
18451  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
18452
18453   /* Table lookup. Size 8.  */
18454  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
18455  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
18456
18457 #undef  THUMB_VARIANT
18458 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
18459 #undef  ARM_VARIANT
18460 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
18461
18462   /* Neon element/structure load/store.  */
18463  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18464  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18465  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18466  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18467  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18468  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18469  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18470  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
18471
18472 #undef  THUMB_VARIANT
18473 #define THUMB_VARIANT &fpu_vfp_ext_v3xd
18474 #undef ARM_VARIANT
18475 #define ARM_VARIANT &fpu_vfp_ext_v3xd
18476  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
18477  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18478  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18479  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18480  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18481  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18482  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18483  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
18484  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
18485
18486 #undef THUMB_VARIANT
18487 #define THUMB_VARIANT  & fpu_vfp_ext_v3
18488 #undef  ARM_VARIANT
18489 #define ARM_VARIANT    & fpu_vfp_ext_v3
18490
18491  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
18492  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18493  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18494  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18495  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18496  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18497  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18498  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
18499  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
18500
18501 #undef ARM_VARIANT
18502 #define ARM_VARIANT &fpu_vfp_ext_fma
18503 #undef THUMB_VARIANT
18504 #define THUMB_VARIANT &fpu_vfp_ext_fma
18505  /* Mnemonics shared by Neon and VFP.  These are included in the
18506     VFP FMA variant; NEON and VFP FMA always includes the NEON
18507     FMA instructions.  */
18508  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
18509  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
18510  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
18511     the v form should always be used.  */
18512  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18513  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
18514  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18515  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
18516  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18517  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
18518
18519 #undef THUMB_VARIANT
18520 #undef  ARM_VARIANT
18521 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
18522
18523  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18524  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18525  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18526  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18527  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18528  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
18529  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
18530  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
18531
18532 #undef  ARM_VARIANT
18533 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
18534
18535  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
18536  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
18537  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
18538  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
18539  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
18540  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
18541  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
18542  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
18543  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
18544  cCE("textrmub",        e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18545  cCE("textrmuh",        e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18546  cCE("textrmuw",        e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18547  cCE("textrmsb",        e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18548  cCE("textrmsh",        e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18549  cCE("textrmsw",        e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
18550  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
18551  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
18552  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
18553  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
18554  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
18555  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18556  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18557  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18558  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18559  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18560  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
18561  cCE("tmovmskb",        e100030, 2, (RR, RIWR),             rd_rn),
18562  cCE("tmovmskh",        e500030, 2, (RR, RIWR),             rd_rn),
18563  cCE("tmovmskw",        e900030, 2, (RR, RIWR),             rd_rn),
18564  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
18565  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
18566  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
18567  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
18568  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
18569  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
18570  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
18571  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
18572  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18573  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18574  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18575  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18576  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18577  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18578  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18579  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18580  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18581  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
18582  cCE("walignr0",        e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18583  cCE("walignr1",        e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18584  cCE("walignr2",        ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18585  cCE("walignr3",        eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18586  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18587  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18588  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18589  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18590  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18591  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18592  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18593  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18594  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18595  cCE("wcmpgtub",        e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18596  cCE("wcmpgtuh",        e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18597  cCE("wcmpgtuw",        e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18598  cCE("wcmpgtsb",        e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18599  cCE("wcmpgtsh",        e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18600  cCE("wcmpgtsw",        eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18601  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18602  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18603  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
18604  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
18605  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18606  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18607  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18608  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18609  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18610  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18611  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18612  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18613  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18614  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18615  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18616  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18617  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18618  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18619  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18620  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18621  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18622  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18623  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
18624  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18625  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18626  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18627  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18628  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18629  cCE("wpackhss",        e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18630  cCE("wpackhus",        e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18631  cCE("wpackwss",        eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18632  cCE("wpackwus",        e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18633  cCE("wpackdss",        ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18634  cCE("wpackdus",        ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18635  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18636  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18637  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18638  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18639  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18640  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18641  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18642  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18643  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18644  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18645  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
18646  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18647  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18648  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18649  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18650  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18651  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18652  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18653  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18654  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18655  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18656  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18657  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18658  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18659  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18660  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18661  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18662  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
18663  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
18664  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18665  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
18666  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
18667  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
18668  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18669  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18670  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18671  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18672  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18673  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18674  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18675  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18676  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18677  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
18678  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
18679  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
18680  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
18681  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
18682  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
18683  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18684  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18685  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18686  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
18687  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
18688  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
18689  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
18690  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
18691  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
18692  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18693  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18694  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
18695  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18696  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
18697
18698 #undef  ARM_VARIANT
18699 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
18700
18701  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
18702  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
18703  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
18704  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
18705  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
18706  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
18707  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18708  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18709  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18710  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18711  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18712  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18713  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18714  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18715  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18716  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18717  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18718  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18719  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18720  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18721  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
18722  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18723  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18724  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18725  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18726  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18727  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18728  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18729  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18730  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18731  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18732  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18733  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18734  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18735  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18736  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18737  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18738  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18739  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18740  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18741  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18742  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18743  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18744  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18745  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18746  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18747  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18748  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18749  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18750  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18751  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18752  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18753  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18754  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18755  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18756  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18757  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
18758
18759 #undef  ARM_VARIANT
18760 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
18761
18762  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
18763  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
18764  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
18765  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
18766  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
18767  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
18768  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
18769  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
18770  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
18771  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
18772  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
18773  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
18774  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
18775  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
18776  cCE("cfmv64lr",        e000510, 2, (RMDX, RR),               rn_rd),
18777  cCE("cfmvr64l",        e100510, 2, (RR, RMDX),               rd_rn),
18778  cCE("cfmv64hr",        e000530, 2, (RMDX, RR),               rn_rd),
18779  cCE("cfmvr64h",        e100530, 2, (RR, RMDX),               rd_rn),
18780  cCE("cfmval32",        e200440, 2, (RMAX, RMFX),             rd_rn),
18781  cCE("cfmv32al",        e100440, 2, (RMFX, RMAX),             rd_rn),
18782  cCE("cfmvam32",        e200460, 2, (RMAX, RMFX),             rd_rn),
18783  cCE("cfmv32am",        e100460, 2, (RMFX, RMAX),             rd_rn),
18784  cCE("cfmvah32",        e200480, 2, (RMAX, RMFX),             rd_rn),
18785  cCE("cfmv32ah",        e100480, 2, (RMFX, RMAX),             rd_rn),
18786  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
18787  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
18788  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
18789  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
18790  cCE("cfmvsc32",        e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
18791  cCE("cfmv32sc",        e1004e0, 2, (RMDX, RMDS),             rd),
18792  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
18793  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
18794  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
18795  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
18796  cCE("cfcvt32s",        e000480, 2, (RMF, RMFX),              rd_rn),
18797  cCE("cfcvt32d",        e0004a0, 2, (RMD, RMFX),              rd_rn),
18798  cCE("cfcvt64s",        e0004c0, 2, (RMF, RMDX),              rd_rn),
18799  cCE("cfcvt64d",        e0004e0, 2, (RMD, RMDX),              rd_rn),
18800  cCE("cfcvts32",        e100580, 2, (RMFX, RMF),              rd_rn),
18801  cCE("cfcvtd32",        e1005a0, 2, (RMFX, RMD),              rd_rn),
18802  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
18803  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
18804  cCE("cfrshl32",        e000550, 3, (RMFX, RMFX, RR),         mav_triple),
18805  cCE("cfrshl64",        e000570, 3, (RMDX, RMDX, RR),         mav_triple),
18806  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
18807  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
18808  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
18809  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
18810  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
18811  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
18812  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
18813  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
18814  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
18815  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
18816  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
18817  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
18818  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
18819  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
18820  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
18821  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
18822  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
18823  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
18824  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
18825  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
18826  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18827  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
18828  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18829  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
18830  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18831  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
18832  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18833  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
18834  cCE("cfmadd32",        e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
18835  cCE("cfmsub32",        e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
18836  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
18837  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
18838 };
18839 #undef ARM_VARIANT
18840 #undef THUMB_VARIANT
18841 #undef TCE
18842 #undef TCM
18843 #undef TUE
18844 #undef TUF
18845 #undef TCC
18846 #undef cCE
18847 #undef cCL
18848 #undef C3E
18849 #undef CE
18850 #undef CM
18851 #undef UE
18852 #undef UF
18853 #undef UT
18854 #undef NUF
18855 #undef nUF
18856 #undef NCE
18857 #undef nCE
18858 #undef OPS0
18859 #undef OPS1
18860 #undef OPS2
18861 #undef OPS3
18862 #undef OPS4
18863 #undef OPS5
18864 #undef OPS6
18865 #undef do_0
18866 \f
18867 /* MD interface: bits in the object file.  */
18868
18869 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
18870    for use in the a.out file, and stores them in the array pointed to by buf.
18871    This knows about the endian-ness of the target machine and does
18872    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
18873    2 (short) and 4 (long)  Floating numbers are put out as a series of
18874    LITTLENUMS (shorts, here at least).  */
18875
18876 void
18877 md_number_to_chars (char * buf, valueT val, int n)
18878 {
18879   if (target_big_endian)
18880     number_to_chars_bigendian (buf, val, n);
18881   else
18882     number_to_chars_littleendian (buf, val, n);
18883 }
18884
18885 static valueT
18886 md_chars_to_number (char * buf, int n)
18887 {
18888   valueT result = 0;
18889   unsigned char * where = (unsigned char *) buf;
18890
18891   if (target_big_endian)
18892     {
18893       while (n--)
18894         {
18895           result <<= 8;
18896           result |= (*where++ & 255);
18897         }
18898     }
18899   else
18900     {
18901       while (n--)
18902         {
18903           result <<= 8;
18904           result |= (where[n] & 255);
18905         }
18906     }
18907
18908   return result;
18909 }
18910
18911 /* MD interface: Sections.  */
18912
18913 /* Estimate the size of a frag before relaxing.  Assume everything fits in
18914    2 bytes.  */
18915
18916 int
18917 md_estimate_size_before_relax (fragS * fragp,
18918                                segT    segtype ATTRIBUTE_UNUSED)
18919 {
18920   fragp->fr_var = 2;
18921   return 2;
18922 }
18923
18924 /* Convert a machine dependent frag.  */
18925
18926 void
18927 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
18928 {
18929   unsigned long insn;
18930   unsigned long old_op;
18931   char *buf;
18932   expressionS exp;
18933   fixS *fixp;
18934   int reloc_type;
18935   int pc_rel;
18936   int opcode;
18937
18938   buf = fragp->fr_literal + fragp->fr_fix;
18939
18940   old_op = bfd_get_16(abfd, buf);
18941   if (fragp->fr_symbol)
18942     {
18943       exp.X_op = O_symbol;
18944       exp.X_add_symbol = fragp->fr_symbol;
18945     }
18946   else
18947     {
18948       exp.X_op = O_constant;
18949     }
18950   exp.X_add_number = fragp->fr_offset;
18951   opcode = fragp->fr_subtype;
18952   switch (opcode)
18953     {
18954     case T_MNEM_ldr_pc:
18955     case T_MNEM_ldr_pc2:
18956     case T_MNEM_ldr_sp:
18957     case T_MNEM_str_sp:
18958     case T_MNEM_ldr:
18959     case T_MNEM_ldrb:
18960     case T_MNEM_ldrh:
18961     case T_MNEM_str:
18962     case T_MNEM_strb:
18963     case T_MNEM_strh:
18964       if (fragp->fr_var == 4)
18965         {
18966           insn = THUMB_OP32 (opcode);
18967           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
18968             {
18969               insn |= (old_op & 0x700) << 4;
18970             }
18971           else
18972             {
18973               insn |= (old_op & 7) << 12;
18974               insn |= (old_op & 0x38) << 13;
18975             }
18976           insn |= 0x00000c00;
18977           put_thumb32_insn (buf, insn);
18978           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
18979         }
18980       else
18981         {
18982           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
18983         }
18984       pc_rel = (opcode == T_MNEM_ldr_pc2);
18985       break;
18986     case T_MNEM_adr:
18987       if (fragp->fr_var == 4)
18988         {
18989           insn = THUMB_OP32 (opcode);
18990           insn |= (old_op & 0xf0) << 4;
18991           put_thumb32_insn (buf, insn);
18992           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
18993         }
18994       else
18995         {
18996           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
18997           exp.X_add_number -= 4;
18998         }
18999       pc_rel = 1;
19000       break;
19001     case T_MNEM_mov:
19002     case T_MNEM_movs:
19003     case T_MNEM_cmp:
19004     case T_MNEM_cmn:
19005       if (fragp->fr_var == 4)
19006         {
19007           int r0off = (opcode == T_MNEM_mov
19008                        || opcode == T_MNEM_movs) ? 0 : 8;
19009           insn = THUMB_OP32 (opcode);
19010           insn = (insn & 0xe1ffffff) | 0x10000000;
19011           insn |= (old_op & 0x700) << r0off;
19012           put_thumb32_insn (buf, insn);
19013           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
19014         }
19015       else
19016         {
19017           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
19018         }
19019       pc_rel = 0;
19020       break;
19021     case T_MNEM_b:
19022       if (fragp->fr_var == 4)
19023         {
19024           insn = THUMB_OP32(opcode);
19025           put_thumb32_insn (buf, insn);
19026           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
19027         }
19028       else
19029         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
19030       pc_rel = 1;
19031       break;
19032     case T_MNEM_bcond:
19033       if (fragp->fr_var == 4)
19034         {
19035           insn = THUMB_OP32(opcode);
19036           insn |= (old_op & 0xf00) << 14;
19037           put_thumb32_insn (buf, insn);
19038           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
19039         }
19040       else
19041         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
19042       pc_rel = 1;
19043       break;
19044     case T_MNEM_add_sp:
19045     case T_MNEM_add_pc:
19046     case T_MNEM_inc_sp:
19047     case T_MNEM_dec_sp:
19048       if (fragp->fr_var == 4)
19049         {
19050           /* ??? Choose between add and addw.  */
19051           insn = THUMB_OP32 (opcode);
19052           insn |= (old_op & 0xf0) << 4;
19053           put_thumb32_insn (buf, insn);
19054           if (opcode == T_MNEM_add_pc)
19055             reloc_type = BFD_RELOC_ARM_T32_IMM12;
19056           else
19057             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
19058         }
19059       else
19060         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
19061       pc_rel = 0;
19062       break;
19063
19064     case T_MNEM_addi:
19065     case T_MNEM_addis:
19066     case T_MNEM_subi:
19067     case T_MNEM_subis:
19068       if (fragp->fr_var == 4)
19069         {
19070           insn = THUMB_OP32 (opcode);
19071           insn |= (old_op & 0xf0) << 4;
19072           insn |= (old_op & 0xf) << 16;
19073           put_thumb32_insn (buf, insn);
19074           if (insn & (1 << 20))
19075             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
19076           else
19077             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
19078         }
19079       else
19080         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
19081       pc_rel = 0;
19082       break;
19083     default:
19084       abort ();
19085     }
19086   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
19087                       (enum bfd_reloc_code_real) reloc_type);
19088   fixp->fx_file = fragp->fr_file;
19089   fixp->fx_line = fragp->fr_line;
19090   fragp->fr_fix += fragp->fr_var;
19091 }
19092
19093 /* Return the size of a relaxable immediate operand instruction.
19094    SHIFT and SIZE specify the form of the allowable immediate.  */
19095 static int
19096 relax_immediate (fragS *fragp, int size, int shift)
19097 {
19098   offsetT offset;
19099   offsetT mask;
19100   offsetT low;
19101
19102   /* ??? Should be able to do better than this.  */
19103   if (fragp->fr_symbol)
19104     return 4;
19105
19106   low = (1 << shift) - 1;
19107   mask = (1 << (shift + size)) - (1 << shift);
19108   offset = fragp->fr_offset;
19109   /* Force misaligned offsets to 32-bit variant.  */
19110   if (offset & low)
19111     return 4;
19112   if (offset & ~mask)
19113     return 4;
19114   return 2;
19115 }
19116
19117 /* Get the address of a symbol during relaxation.  */
19118 static addressT
19119 relaxed_symbol_addr (fragS *fragp, long stretch)
19120 {
19121   fragS *sym_frag;
19122   addressT addr;
19123   symbolS *sym;
19124
19125   sym = fragp->fr_symbol;
19126   sym_frag = symbol_get_frag (sym);
19127   know (S_GET_SEGMENT (sym) != absolute_section
19128         || sym_frag == &zero_address_frag);
19129   addr = S_GET_VALUE (sym) + fragp->fr_offset;
19130
19131   /* If frag has yet to be reached on this pass, assume it will
19132      move by STRETCH just as we did.  If this is not so, it will
19133      be because some frag between grows, and that will force
19134      another pass.  */
19135
19136   if (stretch != 0
19137       && sym_frag->relax_marker != fragp->relax_marker)
19138     {
19139       fragS *f;
19140
19141       /* Adjust stretch for any alignment frag.  Note that if have
19142          been expanding the earlier code, the symbol may be
19143          defined in what appears to be an earlier frag.  FIXME:
19144          This doesn't handle the fr_subtype field, which specifies
19145          a maximum number of bytes to skip when doing an
19146          alignment.  */
19147       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
19148         {
19149           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
19150             {
19151               if (stretch < 0)
19152                 stretch = - ((- stretch)
19153                              & ~ ((1 << (int) f->fr_offset) - 1));
19154               else
19155                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
19156               if (stretch == 0)
19157                 break;
19158             }
19159         }
19160       if (f != NULL)
19161         addr += stretch;
19162     }
19163
19164   return addr;
19165 }
19166
19167 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
19168    load.  */
19169 static int
19170 relax_adr (fragS *fragp, asection *sec, long stretch)
19171 {
19172   addressT addr;
19173   offsetT val;
19174
19175   /* Assume worst case for symbols not known to be in the same section.  */
19176   if (fragp->fr_symbol == NULL
19177       || !S_IS_DEFINED (fragp->fr_symbol)
19178       || sec != S_GET_SEGMENT (fragp->fr_symbol)
19179       || S_IS_WEAK (fragp->fr_symbol))
19180     return 4;
19181
19182   val = relaxed_symbol_addr (fragp, stretch);
19183   addr = fragp->fr_address + fragp->fr_fix;
19184   addr = (addr + 4) & ~3;
19185   /* Force misaligned targets to 32-bit variant.  */
19186   if (val & 3)
19187     return 4;
19188   val -= addr;
19189   if (val < 0 || val > 1020)
19190     return 4;
19191   return 2;
19192 }
19193
19194 /* Return the size of a relaxable add/sub immediate instruction.  */
19195 static int
19196 relax_addsub (fragS *fragp, asection *sec)
19197 {
19198   char *buf;
19199   int op;
19200
19201   buf = fragp->fr_literal + fragp->fr_fix;
19202   op = bfd_get_16(sec->owner, buf);
19203   if ((op & 0xf) == ((op >> 4) & 0xf))
19204     return relax_immediate (fragp, 8, 0);
19205   else
19206     return relax_immediate (fragp, 3, 0);
19207 }
19208
19209
19210 /* Return the size of a relaxable branch instruction.  BITS is the
19211    size of the offset field in the narrow instruction.  */
19212
19213 static int
19214 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
19215 {
19216   addressT addr;
19217   offsetT val;
19218   offsetT limit;
19219
19220   /* Assume worst case for symbols not known to be in the same section.  */
19221   if (!S_IS_DEFINED (fragp->fr_symbol)
19222       || sec != S_GET_SEGMENT (fragp->fr_symbol)
19223       || S_IS_WEAK (fragp->fr_symbol))
19224     return 4;
19225
19226 #ifdef OBJ_ELF
19227   if (S_IS_DEFINED (fragp->fr_symbol)
19228       && ARM_IS_FUNC (fragp->fr_symbol))
19229       return 4;
19230
19231   /* PR 12532.  Global symbols with default visibility might
19232      be preempted, so do not relax relocations to them.  */
19233   if ((ELF_ST_VISIBILITY (S_GET_OTHER (fragp->fr_symbol)) == STV_DEFAULT)
19234       && (! S_IS_LOCAL (fragp->fr_symbol)))
19235     return 4;
19236 #endif
19237
19238   val = relaxed_symbol_addr (fragp, stretch);
19239   addr = fragp->fr_address + fragp->fr_fix + 4;
19240   val -= addr;
19241
19242   /* Offset is a signed value *2 */
19243   limit = 1 << bits;
19244   if (val >= limit || val < -limit)
19245     return 4;
19246   return 2;
19247 }
19248
19249
19250 /* Relax a machine dependent frag.  This returns the amount by which
19251    the current size of the frag should change.  */
19252
19253 int
19254 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
19255 {
19256   int oldsize;
19257   int newsize;
19258
19259   oldsize = fragp->fr_var;
19260   switch (fragp->fr_subtype)
19261     {
19262     case T_MNEM_ldr_pc2:
19263       newsize = relax_adr (fragp, sec, stretch);
19264       break;
19265     case T_MNEM_ldr_pc:
19266     case T_MNEM_ldr_sp:
19267     case T_MNEM_str_sp:
19268       newsize = relax_immediate (fragp, 8, 2);
19269       break;
19270     case T_MNEM_ldr:
19271     case T_MNEM_str:
19272       newsize = relax_immediate (fragp, 5, 2);
19273       break;
19274     case T_MNEM_ldrh:
19275     case T_MNEM_strh:
19276       newsize = relax_immediate (fragp, 5, 1);
19277       break;
19278     case T_MNEM_ldrb:
19279     case T_MNEM_strb:
19280       newsize = relax_immediate (fragp, 5, 0);
19281       break;
19282     case T_MNEM_adr:
19283       newsize = relax_adr (fragp, sec, stretch);
19284       break;
19285     case T_MNEM_mov:
19286     case T_MNEM_movs:
19287     case T_MNEM_cmp:
19288     case T_MNEM_cmn:
19289       newsize = relax_immediate (fragp, 8, 0);
19290       break;
19291     case T_MNEM_b:
19292       newsize = relax_branch (fragp, sec, 11, stretch);
19293       break;
19294     case T_MNEM_bcond:
19295       newsize = relax_branch (fragp, sec, 8, stretch);
19296       break;
19297     case T_MNEM_add_sp:
19298     case T_MNEM_add_pc:
19299       newsize = relax_immediate (fragp, 8, 2);
19300       break;
19301     case T_MNEM_inc_sp:
19302     case T_MNEM_dec_sp:
19303       newsize = relax_immediate (fragp, 7, 2);
19304       break;
19305     case T_MNEM_addi:
19306     case T_MNEM_addis:
19307     case T_MNEM_subi:
19308     case T_MNEM_subis:
19309       newsize = relax_addsub (fragp, sec);
19310       break;
19311     default:
19312       abort ();
19313     }
19314
19315   fragp->fr_var = newsize;
19316   /* Freeze wide instructions that are at or before the same location as
19317      in the previous pass.  This avoids infinite loops.
19318      Don't freeze them unconditionally because targets may be artificially
19319      misaligned by the expansion of preceding frags.  */
19320   if (stretch <= 0 && newsize > 2)
19321     {
19322       md_convert_frag (sec->owner, sec, fragp);
19323       frag_wane (fragp);
19324     }
19325
19326   return newsize - oldsize;
19327 }
19328
19329 /* Round up a section size to the appropriate boundary.  */
19330
19331 valueT
19332 md_section_align (segT   segment ATTRIBUTE_UNUSED,
19333                   valueT size)
19334 {
19335 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
19336   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
19337     {
19338       /* For a.out, force the section size to be aligned.  If we don't do
19339          this, BFD will align it for us, but it will not write out the
19340          final bytes of the section.  This may be a bug in BFD, but it is
19341          easier to fix it here since that is how the other a.out targets
19342          work.  */
19343       int align;
19344
19345       align = bfd_get_section_alignment (stdoutput, segment);
19346       size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
19347     }
19348 #endif
19349
19350   return size;
19351 }
19352
19353 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
19354    of an rs_align_code fragment.  */
19355
19356 void
19357 arm_handle_align (fragS * fragP)
19358 {
19359   static char const arm_noop[2][2][4] =
19360     {
19361       {  /* ARMv1 */
19362         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
19363         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
19364       },
19365       {  /* ARMv6k */
19366         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
19367         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
19368       },
19369     };
19370   static char const thumb_noop[2][2][2] =
19371     {
19372       {  /* Thumb-1 */
19373         {0xc0, 0x46},  /* LE */
19374         {0x46, 0xc0},  /* BE */
19375       },
19376       {  /* Thumb-2 */
19377         {0x00, 0xbf},  /* LE */
19378         {0xbf, 0x00}   /* BE */
19379       }
19380     };
19381   static char const wide_thumb_noop[2][4] =
19382     {  /* Wide Thumb-2 */
19383       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
19384       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
19385     };
19386
19387   unsigned bytes, fix, noop_size;
19388   char * p;
19389   const char * noop;
19390   const char *narrow_noop = NULL;
19391 #ifdef OBJ_ELF
19392   enum mstate state;
19393 #endif
19394
19395   if (fragP->fr_type != rs_align_code)
19396     return;
19397
19398   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
19399   p = fragP->fr_literal + fragP->fr_fix;
19400   fix = 0;
19401
19402   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
19403     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
19404
19405   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
19406
19407   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
19408     {
19409       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
19410         {
19411           narrow_noop = thumb_noop[1][target_big_endian];
19412           noop = wide_thumb_noop[target_big_endian];
19413         }
19414       else
19415         noop = thumb_noop[0][target_big_endian];
19416       noop_size = 2;
19417 #ifdef OBJ_ELF
19418       state = MAP_THUMB;
19419 #endif
19420     }
19421   else
19422     {
19423       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k) != 0]
19424                      [target_big_endian];
19425       noop_size = 4;
19426 #ifdef OBJ_ELF
19427       state = MAP_ARM;
19428 #endif
19429     }
19430
19431   fragP->fr_var = noop_size;
19432
19433   if (bytes & (noop_size - 1))
19434     {
19435       fix = bytes & (noop_size - 1);
19436 #ifdef OBJ_ELF
19437       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
19438 #endif
19439       memset (p, 0, fix);
19440       p += fix;
19441       bytes -= fix;
19442     }
19443
19444   if (narrow_noop)
19445     {
19446       if (bytes & noop_size)
19447         {
19448           /* Insert a narrow noop.  */
19449           memcpy (p, narrow_noop, noop_size);
19450           p += noop_size;
19451           bytes -= noop_size;
19452           fix += noop_size;
19453         }
19454
19455       /* Use wide noops for the remainder */
19456       noop_size = 4;
19457     }
19458
19459   while (bytes >= noop_size)
19460     {
19461       memcpy (p, noop, noop_size);
19462       p += noop_size;
19463       bytes -= noop_size;
19464       fix += noop_size;
19465     }
19466
19467   fragP->fr_fix += fix;
19468 }
19469
19470 /* Called from md_do_align.  Used to create an alignment
19471    frag in a code section.  */
19472
19473 void
19474 arm_frag_align_code (int n, int max)
19475 {
19476   char * p;
19477
19478   /* We assume that there will never be a requirement
19479      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
19480   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
19481     {
19482       char err_msg[128];
19483
19484       sprintf (err_msg, 
19485         _("alignments greater than %d bytes not supported in .text sections."),
19486         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
19487       as_fatal ("%s", err_msg);
19488     }
19489
19490   p = frag_var (rs_align_code,
19491                 MAX_MEM_FOR_RS_ALIGN_CODE,
19492                 1,
19493                 (relax_substateT) max,
19494                 (symbolS *) NULL,
19495                 (offsetT) n,
19496                 (char *) NULL);
19497   *p = 0;
19498 }
19499
19500 /* Perform target specific initialisation of a frag.
19501    Note - despite the name this initialisation is not done when the frag
19502    is created, but only when its type is assigned.  A frag can be created
19503    and used a long time before its type is set, so beware of assuming that
19504    this initialisationis performed first.  */
19505
19506 #ifndef OBJ_ELF
19507 void
19508 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
19509 {
19510   /* Record whether this frag is in an ARM or a THUMB area.  */
19511   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
19512 }
19513
19514 #else /* OBJ_ELF is defined.  */
19515 void
19516 arm_init_frag (fragS * fragP, int max_chars)
19517 {
19518   /* If the current ARM vs THUMB mode has not already
19519      been recorded into this frag then do so now.  */
19520   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
19521     {
19522       fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
19523
19524       /* Record a mapping symbol for alignment frags.  We will delete this
19525          later if the alignment ends up empty.  */
19526       switch (fragP->fr_type)
19527         {
19528           case rs_align:
19529           case rs_align_test:
19530           case rs_fill:
19531             mapping_state_2 (MAP_DATA, max_chars);
19532             break;
19533           case rs_align_code:
19534             mapping_state_2 (thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
19535             break;
19536           default:
19537             break;
19538         }
19539     }
19540 }
19541
19542 /* When we change sections we need to issue a new mapping symbol.  */
19543
19544 void
19545 arm_elf_change_section (void)
19546 {
19547   /* Link an unlinked unwind index table section to the .text section.  */
19548   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
19549       && elf_linked_to_section (now_seg) == NULL)
19550     elf_linked_to_section (now_seg) = text_section;
19551 }
19552
19553 int
19554 arm_elf_section_type (const char * str, size_t len)
19555 {
19556   if (len == 5 && strncmp (str, "exidx", 5) == 0)
19557     return SHT_ARM_EXIDX;
19558
19559   return -1;
19560 }
19561 \f
19562 /* Code to deal with unwinding tables.  */
19563
19564 static void add_unwind_adjustsp (offsetT);
19565
19566 /* Generate any deferred unwind frame offset.  */
19567
19568 static void
19569 flush_pending_unwind (void)
19570 {
19571   offsetT offset;
19572
19573   offset = unwind.pending_offset;
19574   unwind.pending_offset = 0;
19575   if (offset != 0)
19576     add_unwind_adjustsp (offset);
19577 }
19578
19579 /* Add an opcode to this list for this function.  Two-byte opcodes should
19580    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
19581    order.  */
19582
19583 static void
19584 add_unwind_opcode (valueT op, int length)
19585 {
19586   /* Add any deferred stack adjustment.  */
19587   if (unwind.pending_offset)
19588     flush_pending_unwind ();
19589
19590   unwind.sp_restored = 0;
19591
19592   if (unwind.opcode_count + length > unwind.opcode_alloc)
19593     {
19594       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
19595       if (unwind.opcodes)
19596         unwind.opcodes = (unsigned char *) xrealloc (unwind.opcodes,
19597                                                      unwind.opcode_alloc);
19598       else
19599         unwind.opcodes = (unsigned char *) xmalloc (unwind.opcode_alloc);
19600     }
19601   while (length > 0)
19602     {
19603       length--;
19604       unwind.opcodes[unwind.opcode_count] = op & 0xff;
19605       op >>= 8;
19606       unwind.opcode_count++;
19607     }
19608 }
19609
19610 /* Add unwind opcodes to adjust the stack pointer.  */
19611
19612 static void
19613 add_unwind_adjustsp (offsetT offset)
19614 {
19615   valueT op;
19616
19617   if (offset > 0x200)
19618     {
19619       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
19620       char bytes[5];
19621       int n;
19622       valueT o;
19623
19624       /* Long form: 0xb2, uleb128.  */
19625       /* This might not fit in a word so add the individual bytes,
19626          remembering the list is built in reverse order.  */
19627       o = (valueT) ((offset - 0x204) >> 2);
19628       if (o == 0)
19629         add_unwind_opcode (0, 1);
19630
19631       /* Calculate the uleb128 encoding of the offset.  */
19632       n = 0;
19633       while (o)
19634         {
19635           bytes[n] = o & 0x7f;
19636           o >>= 7;
19637           if (o)
19638             bytes[n] |= 0x80;
19639           n++;
19640         }
19641       /* Add the insn.  */
19642       for (; n; n--)
19643         add_unwind_opcode (bytes[n - 1], 1);
19644       add_unwind_opcode (0xb2, 1);
19645     }
19646   else if (offset > 0x100)
19647     {
19648       /* Two short opcodes.  */
19649       add_unwind_opcode (0x3f, 1);
19650       op = (offset - 0x104) >> 2;
19651       add_unwind_opcode (op, 1);
19652     }
19653   else if (offset > 0)
19654     {
19655       /* Short opcode.  */
19656       op = (offset - 4) >> 2;
19657       add_unwind_opcode (op, 1);
19658     }
19659   else if (offset < 0)
19660     {
19661       offset = -offset;
19662       while (offset > 0x100)
19663         {
19664           add_unwind_opcode (0x7f, 1);
19665           offset -= 0x100;
19666         }
19667       op = ((offset - 4) >> 2) | 0x40;
19668       add_unwind_opcode (op, 1);
19669     }
19670 }
19671
19672 /* Finish the list of unwind opcodes for this function.  */
19673 static void
19674 finish_unwind_opcodes (void)
19675 {
19676   valueT op;
19677
19678   if (unwind.fp_used)
19679     {
19680       /* Adjust sp as necessary.  */
19681       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
19682       flush_pending_unwind ();
19683
19684       /* After restoring sp from the frame pointer.  */
19685       op = 0x90 | unwind.fp_reg;
19686       add_unwind_opcode (op, 1);
19687     }
19688   else
19689     flush_pending_unwind ();
19690 }
19691
19692
19693 /* Start an exception table entry.  If idx is nonzero this is an index table
19694    entry.  */
19695
19696 static void
19697 start_unwind_section (const segT text_seg, int idx)
19698 {
19699   const char * text_name;
19700   const char * prefix;
19701   const char * prefix_once;
19702   const char * group_name;
19703   size_t prefix_len;
19704   size_t text_len;
19705   char * sec_name;
19706   size_t sec_name_len;
19707   int type;
19708   int flags;
19709   int linkonce;
19710
19711   if (idx)
19712     {
19713       prefix = ELF_STRING_ARM_unwind;
19714       prefix_once = ELF_STRING_ARM_unwind_once;
19715       type = SHT_ARM_EXIDX;
19716     }
19717   else
19718     {
19719       prefix = ELF_STRING_ARM_unwind_info;
19720       prefix_once = ELF_STRING_ARM_unwind_info_once;
19721       type = SHT_PROGBITS;
19722     }
19723
19724   text_name = segment_name (text_seg);
19725   if (streq (text_name, ".text"))
19726     text_name = "";
19727
19728   if (strncmp (text_name, ".gnu.linkonce.t.",
19729                strlen (".gnu.linkonce.t.")) == 0)
19730     {
19731       prefix = prefix_once;
19732       text_name += strlen (".gnu.linkonce.t.");
19733     }
19734
19735   prefix_len = strlen (prefix);
19736   text_len = strlen (text_name);
19737   sec_name_len = prefix_len + text_len;
19738   sec_name = (char *) xmalloc (sec_name_len + 1);
19739   memcpy (sec_name, prefix, prefix_len);
19740   memcpy (sec_name + prefix_len, text_name, text_len);
19741   sec_name[prefix_len + text_len] = '\0';
19742
19743   flags = SHF_ALLOC;
19744   linkonce = 0;
19745   group_name = 0;
19746
19747   /* Handle COMDAT group.  */
19748   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
19749     {
19750       group_name = elf_group_name (text_seg);
19751       if (group_name == NULL)
19752         {
19753           as_bad (_("Group section `%s' has no group signature"),
19754                   segment_name (text_seg));
19755           ignore_rest_of_line ();
19756           return;
19757         }
19758       flags |= SHF_GROUP;
19759       linkonce = 1;
19760     }
19761
19762   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
19763
19764   /* Set the section link for index tables.  */
19765   if (idx)
19766     elf_linked_to_section (now_seg) = text_seg;
19767 }
19768
19769
19770 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
19771    personality routine data.  Returns zero, or the index table value for
19772    and inline entry.  */
19773
19774 static valueT
19775 create_unwind_entry (int have_data)
19776 {
19777   int size;
19778   addressT where;
19779   char *ptr;
19780   /* The current word of data.  */
19781   valueT data;
19782   /* The number of bytes left in this word.  */
19783   int n;
19784
19785   finish_unwind_opcodes ();
19786
19787   /* Remember the current text section.  */
19788   unwind.saved_seg = now_seg;
19789   unwind.saved_subseg = now_subseg;
19790
19791   start_unwind_section (now_seg, 0);
19792
19793   if (unwind.personality_routine == NULL)
19794     {
19795       if (unwind.personality_index == -2)
19796         {
19797           if (have_data)
19798             as_bad (_("handlerdata in cantunwind frame"));
19799           return 1; /* EXIDX_CANTUNWIND.  */
19800         }
19801
19802       /* Use a default personality routine if none is specified.  */
19803       if (unwind.personality_index == -1)
19804         {
19805           if (unwind.opcode_count > 3)
19806             unwind.personality_index = 1;
19807           else
19808             unwind.personality_index = 0;
19809         }
19810
19811       /* Space for the personality routine entry.  */
19812       if (unwind.personality_index == 0)
19813         {
19814           if (unwind.opcode_count > 3)
19815             as_bad (_("too many unwind opcodes for personality routine 0"));
19816
19817           if (!have_data)
19818             {
19819               /* All the data is inline in the index table.  */
19820               data = 0x80;
19821               n = 3;
19822               while (unwind.opcode_count > 0)
19823                 {
19824                   unwind.opcode_count--;
19825                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
19826                   n--;
19827                 }
19828
19829               /* Pad with "finish" opcodes.  */
19830               while (n--)
19831                 data = (data << 8) | 0xb0;
19832
19833               return data;
19834             }
19835           size = 0;
19836         }
19837       else
19838         /* We get two opcodes "free" in the first word.  */
19839         size = unwind.opcode_count - 2;
19840     }
19841   else
19842     /* An extra byte is required for the opcode count.  */
19843     size = unwind.opcode_count + 1;
19844
19845   size = (size + 3) >> 2;
19846   if (size > 0xff)
19847     as_bad (_("too many unwind opcodes"));
19848
19849   frag_align (2, 0, 0);
19850   record_alignment (now_seg, 2);
19851   unwind.table_entry = expr_build_dot ();
19852
19853   /* Allocate the table entry.  */
19854   ptr = frag_more ((size << 2) + 4);
19855   where = frag_now_fix () - ((size << 2) + 4);
19856
19857   switch (unwind.personality_index)
19858     {
19859     case -1:
19860       /* ??? Should this be a PLT generating relocation?  */
19861       /* Custom personality routine.  */
19862       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
19863                BFD_RELOC_ARM_PREL31);
19864
19865       where += 4;
19866       ptr += 4;
19867
19868       /* Set the first byte to the number of additional words.  */
19869       data = size - 1;
19870       n = 3;
19871       break;
19872
19873     /* ABI defined personality routines.  */
19874     case 0:
19875       /* Three opcodes bytes are packed into the first word.  */
19876       data = 0x80;
19877       n = 3;
19878       break;
19879
19880     case 1:
19881     case 2:
19882       /* The size and first two opcode bytes go in the first word.  */
19883       data = ((0x80 + unwind.personality_index) << 8) | size;
19884       n = 2;
19885       break;
19886
19887     default:
19888       /* Should never happen.  */
19889       abort ();
19890     }
19891
19892   /* Pack the opcodes into words (MSB first), reversing the list at the same
19893      time.  */
19894   while (unwind.opcode_count > 0)
19895     {
19896       if (n == 0)
19897         {
19898           md_number_to_chars (ptr, data, 4);
19899           ptr += 4;
19900           n = 4;
19901           data = 0;
19902         }
19903       unwind.opcode_count--;
19904       n--;
19905       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
19906     }
19907
19908   /* Finish off the last word.  */
19909   if (n < 4)
19910     {
19911       /* Pad with "finish" opcodes.  */
19912       while (n--)
19913         data = (data << 8) | 0xb0;
19914
19915       md_number_to_chars (ptr, data, 4);
19916     }
19917
19918   if (!have_data)
19919     {
19920       /* Add an empty descriptor if there is no user-specified data.   */
19921       ptr = frag_more (4);
19922       md_number_to_chars (ptr, 0, 4);
19923     }
19924
19925   return 0;
19926 }
19927
19928
19929 /* Initialize the DWARF-2 unwind information for this procedure.  */
19930
19931 void
19932 tc_arm_frame_initial_instructions (void)
19933 {
19934   cfi_add_CFA_def_cfa (REG_SP, 0);
19935 }
19936 #endif /* OBJ_ELF */
19937
19938 /* Convert REGNAME to a DWARF-2 register number.  */
19939
19940 int
19941 tc_arm_regname_to_dw2regnum (char *regname)
19942 {
19943   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
19944
19945   if (reg == FAIL)
19946     return -1;
19947
19948   return reg;
19949 }
19950
19951 #ifdef TE_PE
19952 void
19953 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
19954 {
19955   expressionS exp;
19956
19957   exp.X_op = O_secrel;
19958   exp.X_add_symbol = symbol;
19959   exp.X_add_number = 0;
19960   emit_expr (&exp, size);
19961 }
19962 #endif
19963
19964 /* MD interface: Symbol and relocation handling.  */
19965
19966 /* Return the address within the segment that a PC-relative fixup is
19967    relative to.  For ARM, PC-relative fixups applied to instructions
19968    are generally relative to the location of the fixup plus 8 bytes.
19969    Thumb branches are offset by 4, and Thumb loads relative to PC
19970    require special handling.  */
19971
19972 long
19973 md_pcrel_from_section (fixS * fixP, segT seg)
19974 {
19975   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
19976
19977   /* If this is pc-relative and we are going to emit a relocation
19978      then we just want to put out any pipeline compensation that the linker
19979      will need.  Otherwise we want to use the calculated base.
19980      For WinCE we skip the bias for externals as well, since this
19981      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
19982   if (fixP->fx_pcrel
19983       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
19984           || (arm_force_relocation (fixP)
19985 #ifdef TE_WINCE
19986               && !S_IS_EXTERNAL (fixP->fx_addsy)
19987 #endif
19988               )))
19989     base = 0;
19990
19991
19992   switch (fixP->fx_r_type)
19993     {
19994       /* PC relative addressing on the Thumb is slightly odd as the
19995          bottom two bits of the PC are forced to zero for the
19996          calculation.  This happens *after* application of the
19997          pipeline offset.  However, Thumb adrl already adjusts for
19998          this, so we need not do it again.  */
19999     case BFD_RELOC_ARM_THUMB_ADD:
20000       return base & ~3;
20001
20002     case BFD_RELOC_ARM_THUMB_OFFSET:
20003     case BFD_RELOC_ARM_T32_OFFSET_IMM:
20004     case BFD_RELOC_ARM_T32_ADD_PC12:
20005     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
20006       return (base + 4) & ~3;
20007
20008       /* Thumb branches are simply offset by +4.  */
20009     case BFD_RELOC_THUMB_PCREL_BRANCH7:
20010     case BFD_RELOC_THUMB_PCREL_BRANCH9:
20011     case BFD_RELOC_THUMB_PCREL_BRANCH12:
20012     case BFD_RELOC_THUMB_PCREL_BRANCH20:
20013     case BFD_RELOC_THUMB_PCREL_BRANCH25:
20014       return base + 4;
20015
20016     case BFD_RELOC_THUMB_PCREL_BRANCH23:
20017       if (fixP->fx_addsy
20018           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20019           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20020           && ARM_IS_FUNC (fixP->fx_addsy)
20021           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20022         base = fixP->fx_where + fixP->fx_frag->fr_address;
20023        return base + 4;
20024
20025       /* BLX is like branches above, but forces the low two bits of PC to
20026          zero.  */
20027     case BFD_RELOC_THUMB_PCREL_BLX:
20028       if (fixP->fx_addsy
20029           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20030           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20031           && THUMB_IS_FUNC (fixP->fx_addsy)
20032           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20033         base = fixP->fx_where + fixP->fx_frag->fr_address;
20034       return (base + 4) & ~3;
20035
20036       /* ARM mode branches are offset by +8.  However, the Windows CE
20037          loader expects the relocation not to take this into account.  */
20038     case BFD_RELOC_ARM_PCREL_BLX:
20039       if (fixP->fx_addsy
20040           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20041           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20042           && ARM_IS_FUNC (fixP->fx_addsy)
20043           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20044         base = fixP->fx_where + fixP->fx_frag->fr_address;
20045       return base + 8;
20046
20047     case BFD_RELOC_ARM_PCREL_CALL:
20048       if (fixP->fx_addsy
20049           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20050           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20051           && THUMB_IS_FUNC (fixP->fx_addsy)
20052           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
20053         base = fixP->fx_where + fixP->fx_frag->fr_address;
20054       return base + 8;
20055
20056     case BFD_RELOC_ARM_PCREL_BRANCH:
20057     case BFD_RELOC_ARM_PCREL_JUMP:
20058     case BFD_RELOC_ARM_PLT32:
20059 #ifdef TE_WINCE
20060       /* When handling fixups immediately, because we have already
20061          discovered the value of a symbol, or the address of the frag involved
20062          we must account for the offset by +8, as the OS loader will never see the reloc.
20063          see fixup_segment() in write.c
20064          The S_IS_EXTERNAL test handles the case of global symbols.
20065          Those need the calculated base, not just the pipe compensation the linker will need.  */
20066       if (fixP->fx_pcrel
20067           && fixP->fx_addsy != NULL
20068           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20069           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
20070         return base + 8;
20071       return base;
20072 #else
20073       return base + 8;
20074 #endif
20075
20076
20077       /* ARM mode loads relative to PC are also offset by +8.  Unlike
20078          branches, the Windows CE loader *does* expect the relocation
20079          to take this into account.  */
20080     case BFD_RELOC_ARM_OFFSET_IMM:
20081     case BFD_RELOC_ARM_OFFSET_IMM8:
20082     case BFD_RELOC_ARM_HWLITERAL:
20083     case BFD_RELOC_ARM_LITERAL:
20084     case BFD_RELOC_ARM_CP_OFF_IMM:
20085       return base + 8;
20086
20087
20088       /* Other PC-relative relocations are un-offset.  */
20089     default:
20090       return base;
20091     }
20092 }
20093
20094 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
20095    Otherwise we have no need to default values of symbols.  */
20096
20097 symbolS *
20098 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
20099 {
20100 #ifdef OBJ_ELF
20101   if (name[0] == '_' && name[1] == 'G'
20102       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
20103     {
20104       if (!GOT_symbol)
20105         {
20106           if (symbol_find (name))
20107             as_bad (_("GOT already in the symbol table"));
20108
20109           GOT_symbol = symbol_new (name, undefined_section,
20110                                    (valueT) 0, & zero_address_frag);
20111         }
20112
20113       return GOT_symbol;
20114     }
20115 #endif
20116
20117   return NULL;
20118 }
20119
20120 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
20121    computed as two separate immediate values, added together.  We
20122    already know that this value cannot be computed by just one ARM
20123    instruction.  */
20124
20125 static unsigned int
20126 validate_immediate_twopart (unsigned int   val,
20127                             unsigned int * highpart)
20128 {
20129   unsigned int a;
20130   unsigned int i;
20131
20132   for (i = 0; i < 32; i += 2)
20133     if (((a = rotate_left (val, i)) & 0xff) != 0)
20134       {
20135         if (a & 0xff00)
20136           {
20137             if (a & ~ 0xffff)
20138               continue;
20139             * highpart = (a  >> 8) | ((i + 24) << 7);
20140           }
20141         else if (a & 0xff0000)
20142           {
20143             if (a & 0xff000000)
20144               continue;
20145             * highpart = (a >> 16) | ((i + 16) << 7);
20146           }
20147         else
20148           {
20149             gas_assert (a & 0xff000000);
20150             * highpart = (a >> 24) | ((i + 8) << 7);
20151           }
20152
20153         return (a & 0xff) | (i << 7);
20154       }
20155
20156   return FAIL;
20157 }
20158
20159 static int
20160 validate_offset_imm (unsigned int val, int hwse)
20161 {
20162   if ((hwse && val > 255) || val > 4095)
20163     return FAIL;
20164   return val;
20165 }
20166
20167 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
20168    negative immediate constant by altering the instruction.  A bit of
20169    a hack really.
20170         MOV <-> MVN
20171         AND <-> BIC
20172         ADC <-> SBC
20173         by inverting the second operand, and
20174         ADD <-> SUB
20175         CMP <-> CMN
20176         by negating the second operand.  */
20177
20178 static int
20179 negate_data_op (unsigned long * instruction,
20180                 unsigned long   value)
20181 {
20182   int op, new_inst;
20183   unsigned long negated, inverted;
20184
20185   negated = encode_arm_immediate (-value);
20186   inverted = encode_arm_immediate (~value);
20187
20188   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
20189   switch (op)
20190     {
20191       /* First negates.  */
20192     case OPCODE_SUB:             /* ADD <-> SUB  */
20193       new_inst = OPCODE_ADD;
20194       value = negated;
20195       break;
20196
20197     case OPCODE_ADD:
20198       new_inst = OPCODE_SUB;
20199       value = negated;
20200       break;
20201
20202     case OPCODE_CMP:             /* CMP <-> CMN  */
20203       new_inst = OPCODE_CMN;
20204       value = negated;
20205       break;
20206
20207     case OPCODE_CMN:
20208       new_inst = OPCODE_CMP;
20209       value = negated;
20210       break;
20211
20212       /* Now Inverted ops.  */
20213     case OPCODE_MOV:             /* MOV <-> MVN  */
20214       new_inst = OPCODE_MVN;
20215       value = inverted;
20216       break;
20217
20218     case OPCODE_MVN:
20219       new_inst = OPCODE_MOV;
20220       value = inverted;
20221       break;
20222
20223     case OPCODE_AND:             /* AND <-> BIC  */
20224       new_inst = OPCODE_BIC;
20225       value = inverted;
20226       break;
20227
20228     case OPCODE_BIC:
20229       new_inst = OPCODE_AND;
20230       value = inverted;
20231       break;
20232
20233     case OPCODE_ADC:              /* ADC <-> SBC  */
20234       new_inst = OPCODE_SBC;
20235       value = inverted;
20236       break;
20237
20238     case OPCODE_SBC:
20239       new_inst = OPCODE_ADC;
20240       value = inverted;
20241       break;
20242
20243       /* We cannot do anything.  */
20244     default:
20245       return FAIL;
20246     }
20247
20248   if (value == (unsigned) FAIL)
20249     return FAIL;
20250
20251   *instruction &= OPCODE_MASK;
20252   *instruction |= new_inst << DATA_OP_SHIFT;
20253   return value;
20254 }
20255
20256 /* Like negate_data_op, but for Thumb-2.   */
20257
20258 static unsigned int
20259 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
20260 {
20261   int op, new_inst;
20262   int rd;
20263   unsigned int negated, inverted;
20264
20265   negated = encode_thumb32_immediate (-value);
20266   inverted = encode_thumb32_immediate (~value);
20267
20268   rd = (*instruction >> 8) & 0xf;
20269   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
20270   switch (op)
20271     {
20272       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
20273     case T2_OPCODE_SUB:
20274       new_inst = T2_OPCODE_ADD;
20275       value = negated;
20276       break;
20277
20278     case T2_OPCODE_ADD:
20279       new_inst = T2_OPCODE_SUB;
20280       value = negated;
20281       break;
20282
20283       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
20284     case T2_OPCODE_ORR:
20285       new_inst = T2_OPCODE_ORN;
20286       value = inverted;
20287       break;
20288
20289     case T2_OPCODE_ORN:
20290       new_inst = T2_OPCODE_ORR;
20291       value = inverted;
20292       break;
20293
20294       /* AND <-> BIC.  TST has no inverted equivalent.  */
20295     case T2_OPCODE_AND:
20296       new_inst = T2_OPCODE_BIC;
20297       if (rd == 15)
20298         value = FAIL;
20299       else
20300         value = inverted;
20301       break;
20302
20303     case T2_OPCODE_BIC:
20304       new_inst = T2_OPCODE_AND;
20305       value = inverted;
20306       break;
20307
20308       /* ADC <-> SBC  */
20309     case T2_OPCODE_ADC:
20310       new_inst = T2_OPCODE_SBC;
20311       value = inverted;
20312       break;
20313
20314     case T2_OPCODE_SBC:
20315       new_inst = T2_OPCODE_ADC;
20316       value = inverted;
20317       break;
20318
20319       /* We cannot do anything.  */
20320     default:
20321       return FAIL;
20322     }
20323
20324   if (value == (unsigned int)FAIL)
20325     return FAIL;
20326
20327   *instruction &= T2_OPCODE_MASK;
20328   *instruction |= new_inst << T2_DATA_OP_SHIFT;
20329   return value;
20330 }
20331
20332 /* Read a 32-bit thumb instruction from buf.  */
20333 static unsigned long
20334 get_thumb32_insn (char * buf)
20335 {
20336   unsigned long insn;
20337   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
20338   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
20339
20340   return insn;
20341 }
20342
20343
20344 /* We usually want to set the low bit on the address of thumb function
20345    symbols.  In particular .word foo - . should have the low bit set.
20346    Generic code tries to fold the difference of two symbols to
20347    a constant.  Prevent this and force a relocation when the first symbols
20348    is a thumb function.  */
20349
20350 bfd_boolean
20351 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
20352 {
20353   if (op == O_subtract
20354       && l->X_op == O_symbol
20355       && r->X_op == O_symbol
20356       && THUMB_IS_FUNC (l->X_add_symbol))
20357     {
20358       l->X_op = O_subtract;
20359       l->X_op_symbol = r->X_add_symbol;
20360       l->X_add_number -= r->X_add_number;
20361       return TRUE;
20362     }
20363
20364   /* Process as normal.  */
20365   return FALSE;
20366 }
20367
20368 /* Encode Thumb2 unconditional branches and calls. The encoding
20369    for the 2 are identical for the immediate values.  */
20370
20371 static void
20372 encode_thumb2_b_bl_offset (char * buf, offsetT value)
20373 {
20374 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
20375   offsetT newval;
20376   offsetT newval2;
20377   addressT S, I1, I2, lo, hi;
20378
20379   S = (value >> 24) & 0x01;
20380   I1 = (value >> 23) & 0x01;
20381   I2 = (value >> 22) & 0x01;
20382   hi = (value >> 12) & 0x3ff;
20383   lo = (value >> 1) & 0x7ff; 
20384   newval   = md_chars_to_number (buf, THUMB_SIZE);
20385   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
20386   newval  |= (S << 10) | hi;
20387   newval2 &=  ~T2I1I2MASK;
20388   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
20389   md_number_to_chars (buf, newval, THUMB_SIZE);
20390   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
20391 }
20392
20393 void
20394 md_apply_fix (fixS *    fixP,
20395                valueT * valP,
20396                segT     seg)
20397 {
20398   offsetT        value = * valP;
20399   offsetT        newval;
20400   unsigned int   newimm;
20401   unsigned long  temp;
20402   int            sign;
20403   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
20404
20405   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
20406
20407   /* Note whether this will delete the relocation.  */
20408
20409   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
20410     fixP->fx_done = 1;
20411
20412   /* On a 64-bit host, silently truncate 'value' to 32 bits for
20413      consistency with the behaviour on 32-bit hosts.  Remember value
20414      for emit_reloc.  */
20415   value &= 0xffffffff;
20416   value ^= 0x80000000;
20417   value -= 0x80000000;
20418
20419   *valP = value;
20420   fixP->fx_addnumber = value;
20421
20422   /* Same treatment for fixP->fx_offset.  */
20423   fixP->fx_offset &= 0xffffffff;
20424   fixP->fx_offset ^= 0x80000000;
20425   fixP->fx_offset -= 0x80000000;
20426
20427   switch (fixP->fx_r_type)
20428     {
20429     case BFD_RELOC_NONE:
20430       /* This will need to go in the object file.  */
20431       fixP->fx_done = 0;
20432       break;
20433
20434     case BFD_RELOC_ARM_IMMEDIATE:
20435       /* We claim that this fixup has been processed here,
20436          even if in fact we generate an error because we do
20437          not have a reloc for it, so tc_gen_reloc will reject it.  */
20438       fixP->fx_done = 1;
20439
20440       if (fixP->fx_addsy)
20441         {
20442           const char *msg = 0;
20443
20444           if (! S_IS_DEFINED (fixP->fx_addsy))
20445             msg = _("undefined symbol %s used as an immediate value");
20446           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
20447             msg = _("symbol %s is in a different section");
20448           else if (S_IS_WEAK (fixP->fx_addsy))
20449             msg = _("symbol %s is weak and may be overridden later");
20450
20451           if (msg)
20452             {
20453               as_bad_where (fixP->fx_file, fixP->fx_line,
20454                             msg, S_GET_NAME (fixP->fx_addsy));
20455               break;
20456             }
20457         }
20458
20459       newimm = encode_arm_immediate (value);
20460       temp = md_chars_to_number (buf, INSN_SIZE);
20461
20462       /* If the instruction will fail, see if we can fix things up by
20463          changing the opcode.  */
20464       if (newimm == (unsigned int) FAIL
20465           && (newimm = negate_data_op (&temp, value)) == (unsigned int) FAIL)
20466         {
20467           as_bad_where (fixP->fx_file, fixP->fx_line,
20468                         _("invalid constant (%lx) after fixup"),
20469                         (unsigned long) value);
20470           break;
20471         }
20472
20473       newimm |= (temp & 0xfffff000);
20474       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
20475       break;
20476
20477     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
20478       {
20479         unsigned int highpart = 0;
20480         unsigned int newinsn  = 0xe1a00000; /* nop.  */
20481
20482         if (fixP->fx_addsy)
20483           {
20484             const char *msg = 0;
20485
20486             if (! S_IS_DEFINED (fixP->fx_addsy))
20487               msg = _("undefined symbol %s used as an immediate value");
20488             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
20489               msg = _("symbol %s is in a different section");
20490             else if (S_IS_WEAK (fixP->fx_addsy))
20491               msg = _("symbol %s is weak and may be overridden later");
20492
20493             if (msg)
20494               {
20495                 as_bad_where (fixP->fx_file, fixP->fx_line,
20496                               msg, S_GET_NAME (fixP->fx_addsy));
20497                 break;
20498               }
20499           }
20500         
20501         newimm = encode_arm_immediate (value);
20502         temp = md_chars_to_number (buf, INSN_SIZE);
20503
20504         /* If the instruction will fail, see if we can fix things up by
20505            changing the opcode.  */
20506         if (newimm == (unsigned int) FAIL
20507             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
20508           {
20509             /* No ?  OK - try using two ADD instructions to generate
20510                the value.  */
20511             newimm = validate_immediate_twopart (value, & highpart);
20512
20513             /* Yes - then make sure that the second instruction is
20514                also an add.  */
20515             if (newimm != (unsigned int) FAIL)
20516               newinsn = temp;
20517             /* Still No ?  Try using a negated value.  */
20518             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
20519               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
20520             /* Otherwise - give up.  */
20521             else
20522               {
20523                 as_bad_where (fixP->fx_file, fixP->fx_line,
20524                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
20525                               (long) value);
20526                 break;
20527               }
20528
20529             /* Replace the first operand in the 2nd instruction (which
20530                is the PC) with the destination register.  We have
20531                already added in the PC in the first instruction and we
20532                do not want to do it again.  */
20533             newinsn &= ~ 0xf0000;
20534             newinsn |= ((newinsn & 0x0f000) << 4);
20535           }
20536
20537         newimm |= (temp & 0xfffff000);
20538         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
20539
20540         highpart |= (newinsn & 0xfffff000);
20541         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
20542       }
20543       break;
20544
20545     case BFD_RELOC_ARM_OFFSET_IMM:
20546       if (!fixP->fx_done && seg->use_rela_p)
20547         value = 0;
20548
20549     case BFD_RELOC_ARM_LITERAL:
20550       sign = value > 0;
20551
20552       if (value < 0)
20553         value = - value;
20554
20555       if (validate_offset_imm (value, 0) == FAIL)
20556         {
20557           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
20558             as_bad_where (fixP->fx_file, fixP->fx_line,
20559                           _("invalid literal constant: pool needs to be closer"));
20560           else
20561             as_bad_where (fixP->fx_file, fixP->fx_line,
20562                           _("bad immediate value for offset (%ld)"),
20563                           (long) value);
20564           break;
20565         }
20566
20567       newval = md_chars_to_number (buf, INSN_SIZE);
20568       if (value == 0)
20569         newval &= 0xfffff000;
20570       else
20571         {
20572           newval &= 0xff7ff000;
20573           newval |= value | (sign ? INDEX_UP : 0);
20574         }
20575       md_number_to_chars (buf, newval, INSN_SIZE);
20576       break;
20577
20578     case BFD_RELOC_ARM_OFFSET_IMM8:
20579     case BFD_RELOC_ARM_HWLITERAL:
20580       sign = value > 0;
20581
20582       if (value < 0)
20583         value = - value;
20584
20585       if (validate_offset_imm (value, 1) == FAIL)
20586         {
20587           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
20588             as_bad_where (fixP->fx_file, fixP->fx_line,
20589                           _("invalid literal constant: pool needs to be closer"));
20590           else
20591             as_bad (_("bad immediate value for 8-bit offset (%ld)"),
20592                     (long) value);
20593           break;
20594         }
20595
20596       newval = md_chars_to_number (buf, INSN_SIZE);
20597       if (value == 0)
20598         newval &= 0xfffff0f0;
20599       else
20600         {
20601           newval &= 0xff7ff0f0;
20602           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
20603         }
20604       md_number_to_chars (buf, newval, INSN_SIZE);
20605       break;
20606
20607     case BFD_RELOC_ARM_T32_OFFSET_U8:
20608       if (value < 0 || value > 1020 || value % 4 != 0)
20609         as_bad_where (fixP->fx_file, fixP->fx_line,
20610                       _("bad immediate value for offset (%ld)"), (long) value);
20611       value /= 4;
20612
20613       newval = md_chars_to_number (buf+2, THUMB_SIZE);
20614       newval |= value;
20615       md_number_to_chars (buf+2, newval, THUMB_SIZE);
20616       break;
20617
20618     case BFD_RELOC_ARM_T32_OFFSET_IMM:
20619       /* This is a complicated relocation used for all varieties of Thumb32
20620          load/store instruction with immediate offset:
20621
20622          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
20623                                                    *4, optional writeback(W)
20624                                                    (doubleword load/store)
20625
20626          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
20627          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
20628          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
20629          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
20630          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
20631
20632          Uppercase letters indicate bits that are already encoded at
20633          this point.  Lowercase letters are our problem.  For the
20634          second block of instructions, the secondary opcode nybble
20635          (bits 8..11) is present, and bit 23 is zero, even if this is
20636          a PC-relative operation.  */
20637       newval = md_chars_to_number (buf, THUMB_SIZE);
20638       newval <<= 16;
20639       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
20640
20641       if ((newval & 0xf0000000) == 0xe0000000)
20642         {
20643           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
20644           if (value >= 0)
20645             newval |= (1 << 23);
20646           else
20647             value = -value;
20648           if (value % 4 != 0)
20649             {
20650               as_bad_where (fixP->fx_file, fixP->fx_line,
20651                             _("offset not a multiple of 4"));
20652               break;
20653             }
20654           value /= 4;
20655           if (value > 0xff)
20656             {
20657               as_bad_where (fixP->fx_file, fixP->fx_line,
20658                             _("offset out of range"));
20659               break;
20660             }
20661           newval &= ~0xff;
20662         }
20663       else if ((newval & 0x000f0000) == 0x000f0000)
20664         {
20665           /* PC-relative, 12-bit offset.  */
20666           if (value >= 0)
20667             newval |= (1 << 23);
20668           else
20669             value = -value;
20670           if (value > 0xfff)
20671             {
20672               as_bad_where (fixP->fx_file, fixP->fx_line,
20673                             _("offset out of range"));
20674               break;
20675             }
20676           newval &= ~0xfff;
20677         }
20678       else if ((newval & 0x00000100) == 0x00000100)
20679         {
20680           /* Writeback: 8-bit, +/- offset.  */
20681           if (value >= 0)
20682             newval |= (1 << 9);
20683           else
20684             value = -value;
20685           if (value > 0xff)
20686             {
20687               as_bad_where (fixP->fx_file, fixP->fx_line,
20688                             _("offset out of range"));
20689               break;
20690             }
20691           newval &= ~0xff;
20692         }
20693       else if ((newval & 0x00000f00) == 0x00000e00)
20694         {
20695           /* T-instruction: positive 8-bit offset.  */
20696           if (value < 0 || value > 0xff)
20697             {
20698               as_bad_where (fixP->fx_file, fixP->fx_line,
20699                             _("offset out of range"));
20700               break;
20701             }
20702           newval &= ~0xff;
20703           newval |= value;
20704         }
20705       else
20706         {
20707           /* Positive 12-bit or negative 8-bit offset.  */
20708           int limit;
20709           if (value >= 0)
20710             {
20711               newval |= (1 << 23);
20712               limit = 0xfff;
20713             }
20714           else
20715             {
20716               value = -value;
20717               limit = 0xff;
20718             }
20719           if (value > limit)
20720             {
20721               as_bad_where (fixP->fx_file, fixP->fx_line,
20722                             _("offset out of range"));
20723               break;
20724             }
20725           newval &= ~limit;
20726         }
20727
20728       newval |= value;
20729       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
20730       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
20731       break;
20732
20733     case BFD_RELOC_ARM_SHIFT_IMM:
20734       newval = md_chars_to_number (buf, INSN_SIZE);
20735       if (((unsigned long) value) > 32
20736           || (value == 32
20737               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
20738         {
20739           as_bad_where (fixP->fx_file, fixP->fx_line,
20740                         _("shift expression is too large"));
20741           break;
20742         }
20743
20744       if (value == 0)
20745         /* Shifts of zero must be done as lsl.  */
20746         newval &= ~0x60;
20747       else if (value == 32)
20748         value = 0;
20749       newval &= 0xfffff07f;
20750       newval |= (value & 0x1f) << 7;
20751       md_number_to_chars (buf, newval, INSN_SIZE);
20752       break;
20753
20754     case BFD_RELOC_ARM_T32_IMMEDIATE:
20755     case BFD_RELOC_ARM_T32_ADD_IMM:
20756     case BFD_RELOC_ARM_T32_IMM12:
20757     case BFD_RELOC_ARM_T32_ADD_PC12:
20758       /* We claim that this fixup has been processed here,
20759          even if in fact we generate an error because we do
20760          not have a reloc for it, so tc_gen_reloc will reject it.  */
20761       fixP->fx_done = 1;
20762
20763       if (fixP->fx_addsy
20764           && ! S_IS_DEFINED (fixP->fx_addsy))
20765         {
20766           as_bad_where (fixP->fx_file, fixP->fx_line,
20767                         _("undefined symbol %s used as an immediate value"),
20768                         S_GET_NAME (fixP->fx_addsy));
20769           break;
20770         }
20771
20772       newval = md_chars_to_number (buf, THUMB_SIZE);
20773       newval <<= 16;
20774       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
20775
20776       newimm = FAIL;
20777       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
20778           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
20779         {
20780           newimm = encode_thumb32_immediate (value);
20781           if (newimm == (unsigned int) FAIL)
20782             newimm = thumb32_negate_data_op (&newval, value);
20783         }
20784       if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE
20785           && newimm == (unsigned int) FAIL)
20786         {
20787           /* Turn add/sum into addw/subw.  */
20788           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
20789             newval = (newval & 0xfeffffff) | 0x02000000;
20790           /* No flat 12-bit imm encoding for addsw/subsw.  */
20791           if ((newval & 0x00100000) == 0)
20792             {
20793               /* 12 bit immediate for addw/subw.  */
20794               if (value < 0)
20795                 {
20796                   value = -value;
20797                   newval ^= 0x00a00000;
20798                 }
20799               if (value > 0xfff)
20800                 newimm = (unsigned int) FAIL;
20801               else
20802                 newimm = value;
20803             }
20804         }
20805
20806       if (newimm == (unsigned int)FAIL)
20807         {
20808           as_bad_where (fixP->fx_file, fixP->fx_line,
20809                         _("invalid constant (%lx) after fixup"),
20810                         (unsigned long) value);
20811           break;
20812         }
20813
20814       newval |= (newimm & 0x800) << 15;
20815       newval |= (newimm & 0x700) << 4;
20816       newval |= (newimm & 0x0ff);
20817
20818       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
20819       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
20820       break;
20821
20822     case BFD_RELOC_ARM_SMC:
20823       if (((unsigned long) value) > 0xffff)
20824         as_bad_where (fixP->fx_file, fixP->fx_line,
20825                       _("invalid smc expression"));
20826       newval = md_chars_to_number (buf, INSN_SIZE);
20827       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
20828       md_number_to_chars (buf, newval, INSN_SIZE);
20829       break;
20830
20831     case BFD_RELOC_ARM_HVC:
20832       if (((unsigned long) value) > 0xffff)
20833         as_bad_where (fixP->fx_file, fixP->fx_line,
20834                       _("invalid hvc expression"));
20835       newval = md_chars_to_number (buf, INSN_SIZE);
20836       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
20837       md_number_to_chars (buf, newval, INSN_SIZE);
20838       break;
20839
20840     case BFD_RELOC_ARM_SWI:
20841       if (fixP->tc_fix_data != 0)
20842         {
20843           if (((unsigned long) value) > 0xff)
20844             as_bad_where (fixP->fx_file, fixP->fx_line,
20845                           _("invalid swi expression"));
20846           newval = md_chars_to_number (buf, THUMB_SIZE);
20847           newval |= value;
20848           md_number_to_chars (buf, newval, THUMB_SIZE);
20849         }
20850       else
20851         {
20852           if (((unsigned long) value) > 0x00ffffff)
20853             as_bad_where (fixP->fx_file, fixP->fx_line,
20854                           _("invalid swi expression"));
20855           newval = md_chars_to_number (buf, INSN_SIZE);
20856           newval |= value;
20857           md_number_to_chars (buf, newval, INSN_SIZE);
20858         }
20859       break;
20860
20861     case BFD_RELOC_ARM_MULTI:
20862       if (((unsigned long) value) > 0xffff)
20863         as_bad_where (fixP->fx_file, fixP->fx_line,
20864                       _("invalid expression in load/store multiple"));
20865       newval = value | md_chars_to_number (buf, INSN_SIZE);
20866       md_number_to_chars (buf, newval, INSN_SIZE);
20867       break;
20868
20869 #ifdef OBJ_ELF
20870     case BFD_RELOC_ARM_PCREL_CALL:
20871
20872       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
20873           && fixP->fx_addsy
20874           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20875           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20876           && THUMB_IS_FUNC (fixP->fx_addsy))
20877         /* Flip the bl to blx. This is a simple flip
20878            bit here because we generate PCREL_CALL for
20879            unconditional bls.  */
20880         {
20881           newval = md_chars_to_number (buf, INSN_SIZE);
20882           newval = newval | 0x10000000;
20883           md_number_to_chars (buf, newval, INSN_SIZE);
20884           temp = 1;
20885           fixP->fx_done = 1;
20886         }
20887       else
20888         temp = 3;
20889       goto arm_branch_common;
20890
20891     case BFD_RELOC_ARM_PCREL_JUMP:
20892       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
20893           && fixP->fx_addsy
20894           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20895           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20896           && THUMB_IS_FUNC (fixP->fx_addsy))
20897         {
20898           /* This would map to a bl<cond>, b<cond>,
20899              b<always> to a Thumb function. We
20900              need to force a relocation for this particular
20901              case.  */
20902           newval = md_chars_to_number (buf, INSN_SIZE);
20903           fixP->fx_done = 0;
20904         }
20905
20906     case BFD_RELOC_ARM_PLT32:
20907 #endif
20908     case BFD_RELOC_ARM_PCREL_BRANCH:
20909       temp = 3;
20910       goto arm_branch_common;
20911
20912     case BFD_RELOC_ARM_PCREL_BLX:
20913
20914       temp = 1;
20915       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
20916           && fixP->fx_addsy
20917           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
20918           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
20919           && ARM_IS_FUNC (fixP->fx_addsy))
20920         {
20921           /* Flip the blx to a bl and warn.  */
20922           const char *name = S_GET_NAME (fixP->fx_addsy);
20923           newval = 0xeb000000;
20924           as_warn_where (fixP->fx_file, fixP->fx_line,
20925                          _("blx to '%s' an ARM ISA state function changed to bl"),
20926                           name);
20927           md_number_to_chars (buf, newval, INSN_SIZE);
20928           temp = 3;
20929           fixP->fx_done = 1;
20930         }
20931
20932 #ifdef OBJ_ELF
20933        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
20934          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
20935 #endif
20936
20937     arm_branch_common:
20938       /* We are going to store value (shifted right by two) in the
20939          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
20940          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
20941          also be be clear.  */
20942       if (value & temp)
20943         as_bad_where (fixP->fx_file, fixP->fx_line,
20944                       _("misaligned branch destination"));
20945       if ((value & (offsetT)0xfe000000) != (offsetT)0
20946           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
20947         as_bad_where (fixP->fx_file, fixP->fx_line,
20948                       _("branch out of range"));
20949
20950       if (fixP->fx_done || !seg->use_rela_p)
20951         {
20952           newval = md_chars_to_number (buf, INSN_SIZE);
20953           newval |= (value >> 2) & 0x00ffffff;
20954           /* Set the H bit on BLX instructions.  */
20955           if (temp == 1)
20956             {
20957               if (value & 2)
20958                 newval |= 0x01000000;
20959               else
20960                 newval &= ~0x01000000;
20961             }
20962           md_number_to_chars (buf, newval, INSN_SIZE);
20963         }
20964       break;
20965
20966     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
20967       /* CBZ can only branch forward.  */
20968
20969       /* Attempts to use CBZ to branch to the next instruction
20970          (which, strictly speaking, are prohibited) will be turned into
20971          no-ops.
20972
20973          FIXME: It may be better to remove the instruction completely and
20974          perform relaxation.  */
20975       if (value == -2)
20976         {
20977           newval = md_chars_to_number (buf, THUMB_SIZE);
20978           newval = 0xbf00; /* NOP encoding T1 */
20979           md_number_to_chars (buf, newval, THUMB_SIZE);
20980         }
20981       else
20982         {
20983           if (value & ~0x7e)
20984             as_bad_where (fixP->fx_file, fixP->fx_line,
20985                           _("branch out of range"));
20986
20987           if (fixP->fx_done || !seg->use_rela_p)
20988             {
20989               newval = md_chars_to_number (buf, THUMB_SIZE);
20990               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
20991               md_number_to_chars (buf, newval, THUMB_SIZE);
20992             }
20993         }
20994       break;
20995
20996     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
20997       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
20998         as_bad_where (fixP->fx_file, fixP->fx_line,
20999                       _("branch out of range"));
21000
21001       if (fixP->fx_done || !seg->use_rela_p)
21002         {
21003           newval = md_chars_to_number (buf, THUMB_SIZE);
21004           newval |= (value & 0x1ff) >> 1;
21005           md_number_to_chars (buf, newval, THUMB_SIZE);
21006         }
21007       break;
21008
21009     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
21010       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
21011         as_bad_where (fixP->fx_file, fixP->fx_line,
21012                       _("branch out of range"));
21013
21014       if (fixP->fx_done || !seg->use_rela_p)
21015         {
21016           newval = md_chars_to_number (buf, THUMB_SIZE);
21017           newval |= (value & 0xfff) >> 1;
21018           md_number_to_chars (buf, newval, THUMB_SIZE);
21019         }
21020       break;
21021
21022     case BFD_RELOC_THUMB_PCREL_BRANCH20:
21023       if (fixP->fx_addsy
21024           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21025           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21026           && ARM_IS_FUNC (fixP->fx_addsy)
21027           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21028         {
21029           /* Force a relocation for a branch 20 bits wide.  */
21030           fixP->fx_done = 0;
21031         }
21032       if ((value & ~0x1fffff) && ((value & ~0x1fffff) != ~0x1fffff))
21033         as_bad_where (fixP->fx_file, fixP->fx_line,
21034                       _("conditional branch out of range"));
21035
21036       if (fixP->fx_done || !seg->use_rela_p)
21037         {
21038           offsetT newval2;
21039           addressT S, J1, J2, lo, hi;
21040
21041           S  = (value & 0x00100000) >> 20;
21042           J2 = (value & 0x00080000) >> 19;
21043           J1 = (value & 0x00040000) >> 18;
21044           hi = (value & 0x0003f000) >> 12;
21045           lo = (value & 0x00000ffe) >> 1;
21046
21047           newval   = md_chars_to_number (buf, THUMB_SIZE);
21048           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
21049           newval  |= (S << 10) | hi;
21050           newval2 |= (J1 << 13) | (J2 << 11) | lo;
21051           md_number_to_chars (buf, newval, THUMB_SIZE);
21052           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
21053         }
21054       break;
21055
21056     case BFD_RELOC_THUMB_PCREL_BLX:
21057
21058       /* If there is a blx from a thumb state function to
21059          another thumb function flip this to a bl and warn
21060          about it.  */
21061
21062       if (fixP->fx_addsy
21063           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21064           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21065           && THUMB_IS_FUNC (fixP->fx_addsy))
21066         {
21067           const char *name = S_GET_NAME (fixP->fx_addsy);
21068           as_warn_where (fixP->fx_file, fixP->fx_line,
21069                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
21070                          name);
21071           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
21072           newval = newval | 0x1000;
21073           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
21074           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
21075           fixP->fx_done = 1;
21076         }
21077
21078
21079       goto thumb_bl_common;
21080
21081     case BFD_RELOC_THUMB_PCREL_BRANCH23:
21082
21083       /* A bl from Thumb state ISA to an internal ARM state function
21084          is converted to a blx.  */
21085       if (fixP->fx_addsy
21086           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21087           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21088           && ARM_IS_FUNC (fixP->fx_addsy)
21089           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21090         {
21091           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
21092           newval = newval & ~0x1000;
21093           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
21094           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
21095           fixP->fx_done = 1;
21096         }
21097
21098     thumb_bl_common:
21099
21100 #ifdef OBJ_ELF
21101        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4 &&
21102            fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
21103          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
21104 #endif
21105
21106       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
21107         /* For a BLX instruction, make sure that the relocation is rounded up
21108            to a word boundary.  This follows the semantics of the instruction
21109            which specifies that bit 1 of the target address will come from bit
21110            1 of the base address.  */
21111         value = (value + 1) & ~ 1;
21112
21113
21114        if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
21115         {
21116           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2)))
21117             {
21118               as_bad_where (fixP->fx_file, fixP->fx_line,
21119                             _("branch out of range"));
21120             }
21121           else if ((value & ~0x1ffffff)
21122                    && ((value & ~0x1ffffff) != ~0x1ffffff))
21123               {
21124                 as_bad_where (fixP->fx_file, fixP->fx_line,
21125                             _("Thumb2 branch out of range"));
21126               }
21127         }
21128
21129       if (fixP->fx_done || !seg->use_rela_p)
21130         encode_thumb2_b_bl_offset (buf, value);
21131
21132       break;
21133
21134     case BFD_RELOC_THUMB_PCREL_BRANCH25:
21135       if ((value & ~0x1ffffff) && ((value & ~0x1ffffff) != ~0x1ffffff))
21136         as_bad_where (fixP->fx_file, fixP->fx_line,
21137                       _("branch out of range"));
21138
21139       if (fixP->fx_done || !seg->use_rela_p)
21140           encode_thumb2_b_bl_offset (buf, value);
21141
21142       break;
21143
21144     case BFD_RELOC_8:
21145       if (fixP->fx_done || !seg->use_rela_p)
21146         md_number_to_chars (buf, value, 1);
21147       break;
21148
21149     case BFD_RELOC_16:
21150       if (fixP->fx_done || !seg->use_rela_p)
21151         md_number_to_chars (buf, value, 2);
21152       break;
21153
21154 #ifdef OBJ_ELF
21155     case BFD_RELOC_ARM_TLS_CALL:
21156     case BFD_RELOC_ARM_THM_TLS_CALL:
21157     case BFD_RELOC_ARM_TLS_DESCSEQ:
21158     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
21159       S_SET_THREAD_LOCAL (fixP->fx_addsy);
21160       break;
21161
21162     case BFD_RELOC_ARM_TLS_GOTDESC:
21163     case BFD_RELOC_ARM_TLS_GD32:
21164     case BFD_RELOC_ARM_TLS_LE32:
21165     case BFD_RELOC_ARM_TLS_IE32:
21166     case BFD_RELOC_ARM_TLS_LDM32:
21167     case BFD_RELOC_ARM_TLS_LDO32:
21168       S_SET_THREAD_LOCAL (fixP->fx_addsy);
21169       /* fall through */
21170
21171     case BFD_RELOC_ARM_GOT32:
21172     case BFD_RELOC_ARM_GOTOFF:
21173       if (fixP->fx_done || !seg->use_rela_p)
21174         md_number_to_chars (buf, 0, 4);
21175       break;
21176
21177     case BFD_RELOC_ARM_GOT_PREL:
21178       if (fixP->fx_done || !seg->use_rela_p)
21179         md_number_to_chars (buf, value, 4);
21180       break;
21181
21182     case BFD_RELOC_ARM_TARGET2:
21183       /* TARGET2 is not partial-inplace, so we need to write the
21184          addend here for REL targets, because it won't be written out
21185          during reloc processing later.  */
21186       if (fixP->fx_done || !seg->use_rela_p)
21187         md_number_to_chars (buf, fixP->fx_offset, 4);
21188       break;
21189 #endif
21190
21191     case BFD_RELOC_RVA:
21192     case BFD_RELOC_32:
21193     case BFD_RELOC_ARM_TARGET1:
21194     case BFD_RELOC_ARM_ROSEGREL32:
21195     case BFD_RELOC_ARM_SBREL32:
21196     case BFD_RELOC_32_PCREL:
21197 #ifdef TE_PE
21198     case BFD_RELOC_32_SECREL:
21199 #endif
21200       if (fixP->fx_done || !seg->use_rela_p)
21201 #ifdef TE_WINCE
21202         /* For WinCE we only do this for pcrel fixups.  */
21203         if (fixP->fx_done || fixP->fx_pcrel)
21204 #endif
21205           md_number_to_chars (buf, value, 4);
21206       break;
21207
21208 #ifdef OBJ_ELF
21209     case BFD_RELOC_ARM_PREL31:
21210       if (fixP->fx_done || !seg->use_rela_p)
21211         {
21212           newval = md_chars_to_number (buf, 4) & 0x80000000;
21213           if ((value ^ (value >> 1)) & 0x40000000)
21214             {
21215               as_bad_where (fixP->fx_file, fixP->fx_line,
21216                             _("rel31 relocation overflow"));
21217             }
21218           newval |= value & 0x7fffffff;
21219           md_number_to_chars (buf, newval, 4);
21220         }
21221       break;
21222 #endif
21223
21224     case BFD_RELOC_ARM_CP_OFF_IMM:
21225     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
21226       if (value < -1023 || value > 1023 || (value & 3))
21227         as_bad_where (fixP->fx_file, fixP->fx_line,
21228                       _("co-processor offset out of range"));
21229     cp_off_common:
21230       sign = value > 0;
21231       if (value < 0)
21232         value = -value;
21233       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
21234           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
21235         newval = md_chars_to_number (buf, INSN_SIZE);
21236       else
21237         newval = get_thumb32_insn (buf);
21238       if (value == 0)
21239         newval &= 0xffffff00;
21240       else
21241         {
21242           newval &= 0xff7fff00;
21243           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
21244         }
21245       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
21246           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
21247         md_number_to_chars (buf, newval, INSN_SIZE);
21248       else
21249         put_thumb32_insn (buf, newval);
21250       break;
21251
21252     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
21253     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
21254       if (value < -255 || value > 255)
21255         as_bad_where (fixP->fx_file, fixP->fx_line,
21256                       _("co-processor offset out of range"));
21257       value *= 4;
21258       goto cp_off_common;
21259
21260     case BFD_RELOC_ARM_THUMB_OFFSET:
21261       newval = md_chars_to_number (buf, THUMB_SIZE);
21262       /* Exactly what ranges, and where the offset is inserted depends
21263          on the type of instruction, we can establish this from the
21264          top 4 bits.  */
21265       switch (newval >> 12)
21266         {
21267         case 4: /* PC load.  */
21268           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
21269              forced to zero for these loads; md_pcrel_from has already
21270              compensated for this.  */
21271           if (value & 3)
21272             as_bad_where (fixP->fx_file, fixP->fx_line,
21273                           _("invalid offset, target not word aligned (0x%08lX)"),
21274                           (((unsigned long) fixP->fx_frag->fr_address
21275                             + (unsigned long) fixP->fx_where) & ~3)
21276                           + (unsigned long) value);
21277
21278           if (value & ~0x3fc)
21279             as_bad_where (fixP->fx_file, fixP->fx_line,
21280                           _("invalid offset, value too big (0x%08lX)"),
21281                           (long) value);
21282
21283           newval |= value >> 2;
21284           break;
21285
21286         case 9: /* SP load/store.  */
21287           if (value & ~0x3fc)
21288             as_bad_where (fixP->fx_file, fixP->fx_line,
21289                           _("invalid offset, value too big (0x%08lX)"),
21290                           (long) value);
21291           newval |= value >> 2;
21292           break;
21293
21294         case 6: /* Word load/store.  */
21295           if (value & ~0x7c)
21296             as_bad_where (fixP->fx_file, fixP->fx_line,
21297                           _("invalid offset, value too big (0x%08lX)"),
21298                           (long) value);
21299           newval |= value << 4; /* 6 - 2.  */
21300           break;
21301
21302         case 7: /* Byte load/store.  */
21303           if (value & ~0x1f)
21304             as_bad_where (fixP->fx_file, fixP->fx_line,
21305                           _("invalid offset, value too big (0x%08lX)"),
21306                           (long) value);
21307           newval |= value << 6;
21308           break;
21309
21310         case 8: /* Halfword load/store.  */
21311           if (value & ~0x3e)
21312             as_bad_where (fixP->fx_file, fixP->fx_line,
21313                           _("invalid offset, value too big (0x%08lX)"),
21314                           (long) value);
21315           newval |= value << 5; /* 6 - 1.  */
21316           break;
21317
21318         default:
21319           as_bad_where (fixP->fx_file, fixP->fx_line,
21320                         "Unable to process relocation for thumb opcode: %lx",
21321                         (unsigned long) newval);
21322           break;
21323         }
21324       md_number_to_chars (buf, newval, THUMB_SIZE);
21325       break;
21326
21327     case BFD_RELOC_ARM_THUMB_ADD:
21328       /* This is a complicated relocation, since we use it for all of
21329          the following immediate relocations:
21330
21331             3bit ADD/SUB
21332             8bit ADD/SUB
21333             9bit ADD/SUB SP word-aligned
21334            10bit ADD PC/SP word-aligned
21335
21336          The type of instruction being processed is encoded in the
21337          instruction field:
21338
21339            0x8000  SUB
21340            0x00F0  Rd
21341            0x000F  Rs
21342       */
21343       newval = md_chars_to_number (buf, THUMB_SIZE);
21344       {
21345         int rd = (newval >> 4) & 0xf;
21346         int rs = newval & 0xf;
21347         int subtract = !!(newval & 0x8000);
21348
21349         /* Check for HI regs, only very restricted cases allowed:
21350            Adjusting SP, and using PC or SP to get an address.  */
21351         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
21352             || (rs > 7 && rs != REG_SP && rs != REG_PC))
21353           as_bad_where (fixP->fx_file, fixP->fx_line,
21354                         _("invalid Hi register with immediate"));
21355
21356         /* If value is negative, choose the opposite instruction.  */
21357         if (value < 0)
21358           {
21359             value = -value;
21360             subtract = !subtract;
21361             if (value < 0)
21362               as_bad_where (fixP->fx_file, fixP->fx_line,
21363                             _("immediate value out of range"));
21364           }
21365
21366         if (rd == REG_SP)
21367           {
21368             if (value & ~0x1fc)
21369               as_bad_where (fixP->fx_file, fixP->fx_line,
21370                             _("invalid immediate for stack address calculation"));
21371             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
21372             newval |= value >> 2;
21373           }
21374         else if (rs == REG_PC || rs == REG_SP)
21375           {
21376             if (subtract || value & ~0x3fc)
21377               as_bad_where (fixP->fx_file, fixP->fx_line,
21378                             _("invalid immediate for address calculation (value = 0x%08lX)"),
21379                             (unsigned long) value);
21380             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
21381             newval |= rd << 8;
21382             newval |= value >> 2;
21383           }
21384         else if (rs == rd)
21385           {
21386             if (value & ~0xff)
21387               as_bad_where (fixP->fx_file, fixP->fx_line,
21388                             _("immediate value out of range"));
21389             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
21390             newval |= (rd << 8) | value;
21391           }
21392         else
21393           {
21394             if (value & ~0x7)
21395               as_bad_where (fixP->fx_file, fixP->fx_line,
21396                             _("immediate value out of range"));
21397             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
21398             newval |= rd | (rs << 3) | (value << 6);
21399           }
21400       }
21401       md_number_to_chars (buf, newval, THUMB_SIZE);
21402       break;
21403
21404     case BFD_RELOC_ARM_THUMB_IMM:
21405       newval = md_chars_to_number (buf, THUMB_SIZE);
21406       if (value < 0 || value > 255)
21407         as_bad_where (fixP->fx_file, fixP->fx_line,
21408                       _("invalid immediate: %ld is out of range"),
21409                       (long) value);
21410       newval |= value;
21411       md_number_to_chars (buf, newval, THUMB_SIZE);
21412       break;
21413
21414     case BFD_RELOC_ARM_THUMB_SHIFT:
21415       /* 5bit shift value (0..32).  LSL cannot take 32.  */
21416       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
21417       temp = newval & 0xf800;
21418       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
21419         as_bad_where (fixP->fx_file, fixP->fx_line,
21420                       _("invalid shift value: %ld"), (long) value);
21421       /* Shifts of zero must be encoded as LSL.  */
21422       if (value == 0)
21423         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
21424       /* Shifts of 32 are encoded as zero.  */
21425       else if (value == 32)
21426         value = 0;
21427       newval |= value << 6;
21428       md_number_to_chars (buf, newval, THUMB_SIZE);
21429       break;
21430
21431     case BFD_RELOC_VTABLE_INHERIT:
21432     case BFD_RELOC_VTABLE_ENTRY:
21433       fixP->fx_done = 0;
21434       return;
21435
21436     case BFD_RELOC_ARM_MOVW:
21437     case BFD_RELOC_ARM_MOVT:
21438     case BFD_RELOC_ARM_THUMB_MOVW:
21439     case BFD_RELOC_ARM_THUMB_MOVT:
21440       if (fixP->fx_done || !seg->use_rela_p)
21441         {
21442           /* REL format relocations are limited to a 16-bit addend.  */
21443           if (!fixP->fx_done)
21444             {
21445               if (value < -0x8000 || value > 0x7fff)
21446                   as_bad_where (fixP->fx_file, fixP->fx_line,
21447                                 _("offset out of range"));
21448             }
21449           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
21450                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
21451             {
21452               value >>= 16;
21453             }
21454
21455           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
21456               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
21457             {
21458               newval = get_thumb32_insn (buf);
21459               newval &= 0xfbf08f00;
21460               newval |= (value & 0xf000) << 4;
21461               newval |= (value & 0x0800) << 15;
21462               newval |= (value & 0x0700) << 4;
21463               newval |= (value & 0x00ff);
21464               put_thumb32_insn (buf, newval);
21465             }
21466           else
21467             {
21468               newval = md_chars_to_number (buf, 4);
21469               newval &= 0xfff0f000;
21470               newval |= value & 0x0fff;
21471               newval |= (value & 0xf000) << 4;
21472               md_number_to_chars (buf, newval, 4);
21473             }
21474         }
21475       return;
21476
21477    case BFD_RELOC_ARM_ALU_PC_G0_NC:
21478    case BFD_RELOC_ARM_ALU_PC_G0:
21479    case BFD_RELOC_ARM_ALU_PC_G1_NC:
21480    case BFD_RELOC_ARM_ALU_PC_G1:
21481    case BFD_RELOC_ARM_ALU_PC_G2:
21482    case BFD_RELOC_ARM_ALU_SB_G0_NC:
21483    case BFD_RELOC_ARM_ALU_SB_G0:
21484    case BFD_RELOC_ARM_ALU_SB_G1_NC:
21485    case BFD_RELOC_ARM_ALU_SB_G1:
21486    case BFD_RELOC_ARM_ALU_SB_G2:
21487      gas_assert (!fixP->fx_done);
21488      if (!seg->use_rela_p)
21489        {
21490          bfd_vma insn;
21491          bfd_vma encoded_addend;
21492          bfd_vma addend_abs = abs (value);
21493
21494          /* Check that the absolute value of the addend can be
21495             expressed as an 8-bit constant plus a rotation.  */
21496          encoded_addend = encode_arm_immediate (addend_abs);
21497          if (encoded_addend == (unsigned int) FAIL)
21498            as_bad_where (fixP->fx_file, fixP->fx_line,
21499                          _("the offset 0x%08lX is not representable"),
21500                          (unsigned long) addend_abs);
21501
21502          /* Extract the instruction.  */
21503          insn = md_chars_to_number (buf, INSN_SIZE);
21504
21505          /* If the addend is positive, use an ADD instruction.
21506             Otherwise use a SUB.  Take care not to destroy the S bit.  */
21507          insn &= 0xff1fffff;
21508          if (value < 0)
21509            insn |= 1 << 22;
21510          else
21511            insn |= 1 << 23;
21512
21513          /* Place the encoded addend into the first 12 bits of the
21514             instruction.  */
21515          insn &= 0xfffff000;
21516          insn |= encoded_addend;
21517
21518          /* Update the instruction.  */
21519          md_number_to_chars (buf, insn, INSN_SIZE);
21520        }
21521      break;
21522
21523     case BFD_RELOC_ARM_LDR_PC_G0:
21524     case BFD_RELOC_ARM_LDR_PC_G1:
21525     case BFD_RELOC_ARM_LDR_PC_G2:
21526     case BFD_RELOC_ARM_LDR_SB_G0:
21527     case BFD_RELOC_ARM_LDR_SB_G1:
21528     case BFD_RELOC_ARM_LDR_SB_G2:
21529       gas_assert (!fixP->fx_done);
21530       if (!seg->use_rela_p)
21531         {
21532           bfd_vma insn;
21533           bfd_vma addend_abs = abs (value);
21534
21535           /* Check that the absolute value of the addend can be
21536              encoded in 12 bits.  */
21537           if (addend_abs >= 0x1000)
21538             as_bad_where (fixP->fx_file, fixP->fx_line,
21539                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
21540                           (unsigned long) addend_abs);
21541
21542           /* Extract the instruction.  */
21543           insn = md_chars_to_number (buf, INSN_SIZE);
21544
21545           /* If the addend is negative, clear bit 23 of the instruction.
21546              Otherwise set it.  */
21547           if (value < 0)
21548             insn &= ~(1 << 23);
21549           else
21550             insn |= 1 << 23;
21551
21552           /* Place the absolute value of the addend into the first 12 bits
21553              of the instruction.  */
21554           insn &= 0xfffff000;
21555           insn |= addend_abs;
21556
21557           /* Update the instruction.  */
21558           md_number_to_chars (buf, insn, INSN_SIZE);
21559         }
21560       break;
21561
21562     case BFD_RELOC_ARM_LDRS_PC_G0:
21563     case BFD_RELOC_ARM_LDRS_PC_G1:
21564     case BFD_RELOC_ARM_LDRS_PC_G2:
21565     case BFD_RELOC_ARM_LDRS_SB_G0:
21566     case BFD_RELOC_ARM_LDRS_SB_G1:
21567     case BFD_RELOC_ARM_LDRS_SB_G2:
21568       gas_assert (!fixP->fx_done);
21569       if (!seg->use_rela_p)
21570         {
21571           bfd_vma insn;
21572           bfd_vma addend_abs = abs (value);
21573
21574           /* Check that the absolute value of the addend can be
21575              encoded in 8 bits.  */
21576           if (addend_abs >= 0x100)
21577             as_bad_where (fixP->fx_file, fixP->fx_line,
21578                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
21579                           (unsigned long) addend_abs);
21580
21581           /* Extract the instruction.  */
21582           insn = md_chars_to_number (buf, INSN_SIZE);
21583
21584           /* If the addend is negative, clear bit 23 of the instruction.
21585              Otherwise set it.  */
21586           if (value < 0)
21587             insn &= ~(1 << 23);
21588           else
21589             insn |= 1 << 23;
21590
21591           /* Place the first four bits of the absolute value of the addend
21592              into the first 4 bits of the instruction, and the remaining
21593              four into bits 8 .. 11.  */
21594           insn &= 0xfffff0f0;
21595           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
21596
21597           /* Update the instruction.  */
21598           md_number_to_chars (buf, insn, INSN_SIZE);
21599         }
21600       break;
21601
21602     case BFD_RELOC_ARM_LDC_PC_G0:
21603     case BFD_RELOC_ARM_LDC_PC_G1:
21604     case BFD_RELOC_ARM_LDC_PC_G2:
21605     case BFD_RELOC_ARM_LDC_SB_G0:
21606     case BFD_RELOC_ARM_LDC_SB_G1:
21607     case BFD_RELOC_ARM_LDC_SB_G2:
21608       gas_assert (!fixP->fx_done);
21609       if (!seg->use_rela_p)
21610         {
21611           bfd_vma insn;
21612           bfd_vma addend_abs = abs (value);
21613
21614           /* Check that the absolute value of the addend is a multiple of
21615              four and, when divided by four, fits in 8 bits.  */
21616           if (addend_abs & 0x3)
21617             as_bad_where (fixP->fx_file, fixP->fx_line,
21618                           _("bad offset 0x%08lX (must be word-aligned)"),
21619                           (unsigned long) addend_abs);
21620
21621           if ((addend_abs >> 2) > 0xff)
21622             as_bad_where (fixP->fx_file, fixP->fx_line,
21623                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
21624                           (unsigned long) addend_abs);
21625
21626           /* Extract the instruction.  */
21627           insn = md_chars_to_number (buf, INSN_SIZE);
21628
21629           /* If the addend is negative, clear bit 23 of the instruction.
21630              Otherwise set it.  */
21631           if (value < 0)
21632             insn &= ~(1 << 23);
21633           else
21634             insn |= 1 << 23;
21635
21636           /* Place the addend (divided by four) into the first eight
21637              bits of the instruction.  */
21638           insn &= 0xfffffff0;
21639           insn |= addend_abs >> 2;
21640
21641           /* Update the instruction.  */
21642           md_number_to_chars (buf, insn, INSN_SIZE);
21643         }
21644       break;
21645
21646     case BFD_RELOC_ARM_V4BX:
21647       /* This will need to go in the object file.  */
21648       fixP->fx_done = 0;
21649       break;
21650
21651     case BFD_RELOC_UNUSED:
21652     default:
21653       as_bad_where (fixP->fx_file, fixP->fx_line,
21654                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
21655     }
21656 }
21657
21658 /* Translate internal representation of relocation info to BFD target
21659    format.  */
21660
21661 arelent *
21662 tc_gen_reloc (asection *section, fixS *fixp)
21663 {
21664   arelent * reloc;
21665   bfd_reloc_code_real_type code;
21666
21667   reloc = (arelent *) xmalloc (sizeof (arelent));
21668
21669   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
21670   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
21671   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
21672
21673   if (fixp->fx_pcrel)
21674     {
21675       if (section->use_rela_p)
21676         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
21677       else
21678         fixp->fx_offset = reloc->address;
21679     }
21680   reloc->addend = fixp->fx_offset;
21681
21682   switch (fixp->fx_r_type)
21683     {
21684     case BFD_RELOC_8:
21685       if (fixp->fx_pcrel)
21686         {
21687           code = BFD_RELOC_8_PCREL;
21688           break;
21689         }
21690
21691     case BFD_RELOC_16:
21692       if (fixp->fx_pcrel)
21693         {
21694           code = BFD_RELOC_16_PCREL;
21695           break;
21696         }
21697
21698     case BFD_RELOC_32:
21699       if (fixp->fx_pcrel)
21700         {
21701           code = BFD_RELOC_32_PCREL;
21702           break;
21703         }
21704
21705     case BFD_RELOC_ARM_MOVW:
21706       if (fixp->fx_pcrel)
21707         {
21708           code = BFD_RELOC_ARM_MOVW_PCREL;
21709           break;
21710         }
21711
21712     case BFD_RELOC_ARM_MOVT:
21713       if (fixp->fx_pcrel)
21714         {
21715           code = BFD_RELOC_ARM_MOVT_PCREL;
21716           break;
21717         }
21718
21719     case BFD_RELOC_ARM_THUMB_MOVW:
21720       if (fixp->fx_pcrel)
21721         {
21722           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
21723           break;
21724         }
21725
21726     case BFD_RELOC_ARM_THUMB_MOVT:
21727       if (fixp->fx_pcrel)
21728         {
21729           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
21730           break;
21731         }
21732
21733     case BFD_RELOC_NONE:
21734     case BFD_RELOC_ARM_PCREL_BRANCH:
21735     case BFD_RELOC_ARM_PCREL_BLX:
21736     case BFD_RELOC_RVA:
21737     case BFD_RELOC_THUMB_PCREL_BRANCH7:
21738     case BFD_RELOC_THUMB_PCREL_BRANCH9:
21739     case BFD_RELOC_THUMB_PCREL_BRANCH12:
21740     case BFD_RELOC_THUMB_PCREL_BRANCH20:
21741     case BFD_RELOC_THUMB_PCREL_BRANCH23:
21742     case BFD_RELOC_THUMB_PCREL_BRANCH25:
21743     case BFD_RELOC_VTABLE_ENTRY:
21744     case BFD_RELOC_VTABLE_INHERIT:
21745 #ifdef TE_PE
21746     case BFD_RELOC_32_SECREL:
21747 #endif
21748       code = fixp->fx_r_type;
21749       break;
21750
21751     case BFD_RELOC_THUMB_PCREL_BLX:
21752 #ifdef OBJ_ELF
21753       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
21754         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
21755       else
21756 #endif
21757         code = BFD_RELOC_THUMB_PCREL_BLX;
21758       break;
21759
21760     case BFD_RELOC_ARM_LITERAL:
21761     case BFD_RELOC_ARM_HWLITERAL:
21762       /* If this is called then the a literal has
21763          been referenced across a section boundary.  */
21764       as_bad_where (fixp->fx_file, fixp->fx_line,
21765                     _("literal referenced across section boundary"));
21766       return NULL;
21767
21768 #ifdef OBJ_ELF
21769     case BFD_RELOC_ARM_TLS_CALL:
21770     case BFD_RELOC_ARM_THM_TLS_CALL:
21771     case BFD_RELOC_ARM_TLS_DESCSEQ:
21772     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
21773     case BFD_RELOC_ARM_GOT32:
21774     case BFD_RELOC_ARM_GOTOFF:
21775     case BFD_RELOC_ARM_GOT_PREL:
21776     case BFD_RELOC_ARM_PLT32:
21777     case BFD_RELOC_ARM_TARGET1:
21778     case BFD_RELOC_ARM_ROSEGREL32:
21779     case BFD_RELOC_ARM_SBREL32:
21780     case BFD_RELOC_ARM_PREL31:
21781     case BFD_RELOC_ARM_TARGET2:
21782     case BFD_RELOC_ARM_TLS_LE32:
21783     case BFD_RELOC_ARM_TLS_LDO32:
21784     case BFD_RELOC_ARM_PCREL_CALL:
21785     case BFD_RELOC_ARM_PCREL_JUMP:
21786     case BFD_RELOC_ARM_ALU_PC_G0_NC:
21787     case BFD_RELOC_ARM_ALU_PC_G0:
21788     case BFD_RELOC_ARM_ALU_PC_G1_NC:
21789     case BFD_RELOC_ARM_ALU_PC_G1:
21790     case BFD_RELOC_ARM_ALU_PC_G2:
21791     case BFD_RELOC_ARM_LDR_PC_G0:
21792     case BFD_RELOC_ARM_LDR_PC_G1:
21793     case BFD_RELOC_ARM_LDR_PC_G2:
21794     case BFD_RELOC_ARM_LDRS_PC_G0:
21795     case BFD_RELOC_ARM_LDRS_PC_G1:
21796     case BFD_RELOC_ARM_LDRS_PC_G2:
21797     case BFD_RELOC_ARM_LDC_PC_G0:
21798     case BFD_RELOC_ARM_LDC_PC_G1:
21799     case BFD_RELOC_ARM_LDC_PC_G2:
21800     case BFD_RELOC_ARM_ALU_SB_G0_NC:
21801     case BFD_RELOC_ARM_ALU_SB_G0:
21802     case BFD_RELOC_ARM_ALU_SB_G1_NC:
21803     case BFD_RELOC_ARM_ALU_SB_G1:
21804     case BFD_RELOC_ARM_ALU_SB_G2:
21805     case BFD_RELOC_ARM_LDR_SB_G0:
21806     case BFD_RELOC_ARM_LDR_SB_G1:
21807     case BFD_RELOC_ARM_LDR_SB_G2:
21808     case BFD_RELOC_ARM_LDRS_SB_G0:
21809     case BFD_RELOC_ARM_LDRS_SB_G1:
21810     case BFD_RELOC_ARM_LDRS_SB_G2:
21811     case BFD_RELOC_ARM_LDC_SB_G0:
21812     case BFD_RELOC_ARM_LDC_SB_G1:
21813     case BFD_RELOC_ARM_LDC_SB_G2:
21814     case BFD_RELOC_ARM_V4BX:
21815       code = fixp->fx_r_type;
21816       break;
21817
21818     case BFD_RELOC_ARM_TLS_GOTDESC:
21819     case BFD_RELOC_ARM_TLS_GD32:
21820     case BFD_RELOC_ARM_TLS_IE32:
21821     case BFD_RELOC_ARM_TLS_LDM32:
21822       /* BFD will include the symbol's address in the addend.
21823          But we don't want that, so subtract it out again here.  */
21824       if (!S_IS_COMMON (fixp->fx_addsy))
21825         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
21826       code = fixp->fx_r_type;
21827       break;
21828 #endif
21829
21830     case BFD_RELOC_ARM_IMMEDIATE:
21831       as_bad_where (fixp->fx_file, fixp->fx_line,
21832                     _("internal relocation (type: IMMEDIATE) not fixed up"));
21833       return NULL;
21834
21835     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
21836       as_bad_where (fixp->fx_file, fixp->fx_line,
21837                     _("ADRL used for a symbol not defined in the same file"));
21838       return NULL;
21839
21840     case BFD_RELOC_ARM_OFFSET_IMM:
21841       if (section->use_rela_p)
21842         {
21843           code = fixp->fx_r_type;
21844           break;
21845         }
21846
21847       if (fixp->fx_addsy != NULL
21848           && !S_IS_DEFINED (fixp->fx_addsy)
21849           && S_IS_LOCAL (fixp->fx_addsy))
21850         {
21851           as_bad_where (fixp->fx_file, fixp->fx_line,
21852                         _("undefined local label `%s'"),
21853                         S_GET_NAME (fixp->fx_addsy));
21854           return NULL;
21855         }
21856
21857       as_bad_where (fixp->fx_file, fixp->fx_line,
21858                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
21859       return NULL;
21860
21861     default:
21862       {
21863         char * type;
21864
21865         switch (fixp->fx_r_type)
21866           {
21867           case BFD_RELOC_NONE:             type = "NONE";         break;
21868           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
21869           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
21870           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
21871           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
21872           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
21873           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
21874           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
21875           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
21876           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
21877           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
21878           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
21879           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
21880           default:                         type = _("<unknown>"); break;
21881           }
21882         as_bad_where (fixp->fx_file, fixp->fx_line,
21883                       _("cannot represent %s relocation in this object file format"),
21884                       type);
21885         return NULL;
21886       }
21887     }
21888
21889 #ifdef OBJ_ELF
21890   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
21891       && GOT_symbol
21892       && fixp->fx_addsy == GOT_symbol)
21893     {
21894       code = BFD_RELOC_ARM_GOTPC;
21895       reloc->addend = fixp->fx_offset = reloc->address;
21896     }
21897 #endif
21898
21899   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
21900
21901   if (reloc->howto == NULL)
21902     {
21903       as_bad_where (fixp->fx_file, fixp->fx_line,
21904                     _("cannot represent %s relocation in this object file format"),
21905                     bfd_get_reloc_code_name (code));
21906       return NULL;
21907     }
21908
21909   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
21910      vtable entry to be used in the relocation's section offset.  */
21911   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
21912     reloc->address = fixp->fx_offset;
21913
21914   return reloc;
21915 }
21916
21917 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
21918
21919 void
21920 cons_fix_new_arm (fragS *       frag,
21921                   int           where,
21922                   int           size,
21923                   expressionS * exp)
21924 {
21925   bfd_reloc_code_real_type type;
21926   int pcrel = 0;
21927
21928   /* Pick a reloc.
21929      FIXME: @@ Should look at CPU word size.  */
21930   switch (size)
21931     {
21932     case 1:
21933       type = BFD_RELOC_8;
21934       break;
21935     case 2:
21936       type = BFD_RELOC_16;
21937       break;
21938     case 4:
21939     default:
21940       type = BFD_RELOC_32;
21941       break;
21942     case 8:
21943       type = BFD_RELOC_64;
21944       break;
21945     }
21946
21947 #ifdef TE_PE
21948   if (exp->X_op == O_secrel)
21949   {
21950     exp->X_op = O_symbol;
21951     type = BFD_RELOC_32_SECREL;
21952   }
21953 #endif
21954
21955   fix_new_exp (frag, where, (int) size, exp, pcrel, type);
21956 }
21957
21958 #if defined (OBJ_COFF)
21959 void
21960 arm_validate_fix (fixS * fixP)
21961 {
21962   /* If the destination of the branch is a defined symbol which does not have
21963      the THUMB_FUNC attribute, then we must be calling a function which has
21964      the (interfacearm) attribute.  We look for the Thumb entry point to that
21965      function and change the branch to refer to that function instead.  */
21966   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
21967       && fixP->fx_addsy != NULL
21968       && S_IS_DEFINED (fixP->fx_addsy)
21969       && ! THUMB_IS_FUNC (fixP->fx_addsy))
21970     {
21971       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
21972     }
21973 }
21974 #endif
21975
21976
21977 int
21978 arm_force_relocation (struct fix * fixp)
21979 {
21980 #if defined (OBJ_COFF) && defined (TE_PE)
21981   if (fixp->fx_r_type == BFD_RELOC_RVA)
21982     return 1;
21983 #endif
21984
21985   /* In case we have a call or a branch to a function in ARM ISA mode from
21986      a thumb function or vice-versa force the relocation. These relocations
21987      are cleared off for some cores that might have blx and simple transformations
21988      are possible.  */
21989
21990 #ifdef OBJ_ELF
21991   switch (fixp->fx_r_type)
21992     {
21993     case BFD_RELOC_ARM_PCREL_JUMP:
21994     case BFD_RELOC_ARM_PCREL_CALL:
21995     case BFD_RELOC_THUMB_PCREL_BLX:
21996       if (THUMB_IS_FUNC (fixp->fx_addsy))
21997         return 1;
21998       break;
21999
22000     case BFD_RELOC_ARM_PCREL_BLX:
22001     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22002     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22003     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22004       if (ARM_IS_FUNC (fixp->fx_addsy))
22005         return 1;
22006       break;
22007
22008     default:
22009       break;
22010     }
22011 #endif
22012
22013   /* Resolve these relocations even if the symbol is extern or weak.
22014      Technically this is probably wrong due to symbol preemption.
22015      In practice these relocations do not have enough range to be useful
22016      at dynamic link time, and some code (e.g. in the Linux kernel)
22017      expects these references to be resolved.  */
22018   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
22019       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
22020       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
22021       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
22022       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
22023       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
22024       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
22025       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
22026       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
22027       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
22028       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
22029       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
22030       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
22031       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
22032     return 0;
22033
22034   /* Always leave these relocations for the linker.  */
22035   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
22036        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
22037       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
22038     return 1;
22039
22040   /* Always generate relocations against function symbols.  */
22041   if (fixp->fx_r_type == BFD_RELOC_32
22042       && fixp->fx_addsy
22043       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
22044     return 1;
22045
22046   return generic_force_reloc (fixp);
22047 }
22048
22049 #if defined (OBJ_ELF) || defined (OBJ_COFF)
22050 /* Relocations against function names must be left unadjusted,
22051    so that the linker can use this information to generate interworking
22052    stubs.  The MIPS version of this function
22053    also prevents relocations that are mips-16 specific, but I do not
22054    know why it does this.
22055
22056    FIXME:
22057    There is one other problem that ought to be addressed here, but
22058    which currently is not:  Taking the address of a label (rather
22059    than a function) and then later jumping to that address.  Such
22060    addresses also ought to have their bottom bit set (assuming that
22061    they reside in Thumb code), but at the moment they will not.  */
22062
22063 bfd_boolean
22064 arm_fix_adjustable (fixS * fixP)
22065 {
22066   if (fixP->fx_addsy == NULL)
22067     return 1;
22068
22069   /* Preserve relocations against symbols with function type.  */
22070   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
22071     return FALSE;
22072
22073   if (THUMB_IS_FUNC (fixP->fx_addsy)
22074       && fixP->fx_subsy == NULL)
22075     return FALSE;
22076
22077   /* We need the symbol name for the VTABLE entries.  */
22078   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
22079       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
22080     return FALSE;
22081
22082   /* Don't allow symbols to be discarded on GOT related relocs.  */
22083   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
22084       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
22085       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
22086       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
22087       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
22088       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
22089       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
22090       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
22091       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
22092       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
22093       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
22094       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
22095       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
22096       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
22097     return FALSE;
22098
22099   /* Similarly for group relocations.  */
22100   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
22101        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
22102       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
22103     return FALSE;
22104
22105   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
22106   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
22107       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
22108       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
22109       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
22110       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
22111       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
22112       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
22113       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
22114     return FALSE;
22115
22116   return TRUE;
22117 }
22118 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
22119
22120 #ifdef OBJ_ELF
22121
22122 const char *
22123 elf32_arm_target_format (void)
22124 {
22125 #ifdef TE_SYMBIAN
22126   return (target_big_endian
22127           ? "elf32-bigarm-symbian"
22128           : "elf32-littlearm-symbian");
22129 #elif defined (TE_VXWORKS)
22130   return (target_big_endian
22131           ? "elf32-bigarm-vxworks"
22132           : "elf32-littlearm-vxworks");
22133 #else
22134   if (target_big_endian)
22135     return "elf32-bigarm";
22136   else
22137     return "elf32-littlearm";
22138 #endif
22139 }
22140
22141 void
22142 armelf_frob_symbol (symbolS * symp,
22143                     int *     puntp)
22144 {
22145   elf_frob_symbol (symp, puntp);
22146 }
22147 #endif
22148
22149 /* MD interface: Finalization.  */
22150
22151 void
22152 arm_cleanup (void)
22153 {
22154   literal_pool * pool;
22155
22156   /* Ensure that all the IT blocks are properly closed.  */
22157   check_it_blocks_finished ();
22158
22159   for (pool = list_of_pools; pool; pool = pool->next)
22160     {
22161       /* Put it at the end of the relevant section.  */
22162       subseg_set (pool->section, pool->sub_section);
22163 #ifdef OBJ_ELF
22164       arm_elf_change_section ();
22165 #endif
22166       s_ltorg (0);
22167     }
22168 }
22169
22170 #ifdef OBJ_ELF
22171 /* Remove any excess mapping symbols generated for alignment frags in
22172    SEC.  We may have created a mapping symbol before a zero byte
22173    alignment; remove it if there's a mapping symbol after the
22174    alignment.  */
22175 static void
22176 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
22177                        void *dummy ATTRIBUTE_UNUSED)
22178 {
22179   segment_info_type *seginfo = seg_info (sec);
22180   fragS *fragp;
22181
22182   if (seginfo == NULL || seginfo->frchainP == NULL)
22183     return;
22184
22185   for (fragp = seginfo->frchainP->frch_root;
22186        fragp != NULL;
22187        fragp = fragp->fr_next)
22188     {
22189       symbolS *sym = fragp->tc_frag_data.last_map;
22190       fragS *next = fragp->fr_next;
22191
22192       /* Variable-sized frags have been converted to fixed size by
22193          this point.  But if this was variable-sized to start with,
22194          there will be a fixed-size frag after it.  So don't handle
22195          next == NULL.  */
22196       if (sym == NULL || next == NULL)
22197         continue;
22198
22199       if (S_GET_VALUE (sym) < next->fr_address)
22200         /* Not at the end of this frag.  */
22201         continue;
22202       know (S_GET_VALUE (sym) == next->fr_address);
22203
22204       do
22205         {
22206           if (next->tc_frag_data.first_map != NULL)
22207             {
22208               /* Next frag starts with a mapping symbol.  Discard this
22209                  one.  */
22210               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
22211               break;
22212             }
22213
22214           if (next->fr_next == NULL)
22215             {
22216               /* This mapping symbol is at the end of the section.  Discard
22217                  it.  */
22218               know (next->fr_fix == 0 && next->fr_var == 0);
22219               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
22220               break;
22221             }
22222
22223           /* As long as we have empty frags without any mapping symbols,
22224              keep looking.  */
22225           /* If the next frag is non-empty and does not start with a
22226              mapping symbol, then this mapping symbol is required.  */
22227           if (next->fr_address != next->fr_next->fr_address)
22228             break;
22229
22230           next = next->fr_next;
22231         }
22232       while (next != NULL);
22233     }
22234 }
22235 #endif
22236
22237 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
22238    ARM ones.  */
22239
22240 void
22241 arm_adjust_symtab (void)
22242 {
22243 #ifdef OBJ_COFF
22244   symbolS * sym;
22245
22246   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
22247     {
22248       if (ARM_IS_THUMB (sym))
22249         {
22250           if (THUMB_IS_FUNC (sym))
22251             {
22252               /* Mark the symbol as a Thumb function.  */
22253               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
22254                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
22255                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
22256
22257               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
22258                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
22259               else
22260                 as_bad (_("%s: unexpected function type: %d"),
22261                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
22262             }
22263           else switch (S_GET_STORAGE_CLASS (sym))
22264             {
22265             case C_EXT:
22266               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
22267               break;
22268             case C_STAT:
22269               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
22270               break;
22271             case C_LABEL:
22272               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
22273               break;
22274             default:
22275               /* Do nothing.  */
22276               break;
22277             }
22278         }
22279
22280       if (ARM_IS_INTERWORK (sym))
22281         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
22282     }
22283 #endif
22284 #ifdef OBJ_ELF
22285   symbolS * sym;
22286   char      bind;
22287
22288   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
22289     {
22290       if (ARM_IS_THUMB (sym))
22291         {
22292           elf_symbol_type * elf_sym;
22293
22294           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
22295           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
22296
22297           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
22298                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
22299             {
22300               /* If it's a .thumb_func, declare it as so,
22301                  otherwise tag label as .code 16.  */
22302               if (THUMB_IS_FUNC (sym))
22303                 elf_sym->internal_elf_sym.st_target_internal
22304                   = ST_BRANCH_TO_THUMB;
22305               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
22306                 elf_sym->internal_elf_sym.st_info =
22307                   ELF_ST_INFO (bind, STT_ARM_16BIT);
22308             }
22309         }
22310     }
22311
22312   /* Remove any overlapping mapping symbols generated by alignment frags.  */
22313   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
22314   /* Now do generic ELF adjustments.  */
22315   elf_adjust_symtab ();
22316 #endif
22317 }
22318
22319 /* MD interface: Initialization.  */
22320
22321 static void
22322 set_constant_flonums (void)
22323 {
22324   int i;
22325
22326   for (i = 0; i < NUM_FLOAT_VALS; i++)
22327     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
22328       abort ();
22329 }
22330
22331 /* Auto-select Thumb mode if it's the only available instruction set for the
22332    given architecture.  */
22333
22334 static void
22335 autoselect_thumb_from_cpu_variant (void)
22336 {
22337   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
22338     opcode_select (16);
22339 }
22340
22341 void
22342 md_begin (void)
22343 {
22344   unsigned mach;
22345   unsigned int i;
22346
22347   if (   (arm_ops_hsh = hash_new ()) == NULL
22348       || (arm_cond_hsh = hash_new ()) == NULL
22349       || (arm_shift_hsh = hash_new ()) == NULL
22350       || (arm_psr_hsh = hash_new ()) == NULL
22351       || (arm_v7m_psr_hsh = hash_new ()) == NULL
22352       || (arm_reg_hsh = hash_new ()) == NULL
22353       || (arm_reloc_hsh = hash_new ()) == NULL
22354       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
22355     as_fatal (_("virtual memory exhausted"));
22356
22357   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
22358     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
22359   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
22360     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
22361   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
22362     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
22363   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
22364     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
22365   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
22366     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
22367                  (void *) (v7m_psrs + i));
22368   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
22369     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
22370   for (i = 0;
22371        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
22372        i++)
22373     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
22374                  (void *) (barrier_opt_names + i));
22375 #ifdef OBJ_ELF
22376   for (i = 0; i < sizeof (reloc_names) / sizeof (struct reloc_entry); i++)
22377     hash_insert (arm_reloc_hsh, reloc_names[i].name, (void *) (reloc_names + i));
22378 #endif
22379
22380   set_constant_flonums ();
22381
22382   /* Set the cpu variant based on the command-line options.  We prefer
22383      -mcpu= over -march= if both are set (as for GCC); and we prefer
22384      -mfpu= over any other way of setting the floating point unit.
22385      Use of legacy options with new options are faulted.  */
22386   if (legacy_cpu)
22387     {
22388       if (mcpu_cpu_opt || march_cpu_opt)
22389         as_bad (_("use of old and new-style options to set CPU type"));
22390
22391       mcpu_cpu_opt = legacy_cpu;
22392     }
22393   else if (!mcpu_cpu_opt)
22394     mcpu_cpu_opt = march_cpu_opt;
22395
22396   if (legacy_fpu)
22397     {
22398       if (mfpu_opt)
22399         as_bad (_("use of old and new-style options to set FPU type"));
22400
22401       mfpu_opt = legacy_fpu;
22402     }
22403   else if (!mfpu_opt)
22404     {
22405 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
22406         || defined (TE_NetBSD) || defined (TE_VXWORKS))
22407       /* Some environments specify a default FPU.  If they don't, infer it
22408          from the processor.  */
22409       if (mcpu_fpu_opt)
22410         mfpu_opt = mcpu_fpu_opt;
22411       else
22412         mfpu_opt = march_fpu_opt;
22413 #else
22414       mfpu_opt = &fpu_default;
22415 #endif
22416     }
22417
22418   if (!mfpu_opt)
22419     {
22420       if (mcpu_cpu_opt != NULL)
22421         mfpu_opt = &fpu_default;
22422       else if (mcpu_fpu_opt != NULL && ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
22423         mfpu_opt = &fpu_arch_vfp_v2;
22424       else
22425         mfpu_opt = &fpu_arch_fpa;
22426     }
22427
22428 #ifdef CPU_DEFAULT
22429   if (!mcpu_cpu_opt)
22430     {
22431       mcpu_cpu_opt = &cpu_default;
22432       selected_cpu = cpu_default;
22433     }
22434 #else
22435   if (mcpu_cpu_opt)
22436     selected_cpu = *mcpu_cpu_opt;
22437   else
22438     mcpu_cpu_opt = &arm_arch_any;
22439 #endif
22440
22441   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
22442
22443   autoselect_thumb_from_cpu_variant ();
22444
22445   arm_arch_used = thumb_arch_used = arm_arch_none;
22446
22447 #if defined OBJ_COFF || defined OBJ_ELF
22448   {
22449     unsigned int flags = 0;
22450
22451 #if defined OBJ_ELF
22452     flags = meabi_flags;
22453
22454     switch (meabi_flags)
22455       {
22456       case EF_ARM_EABI_UNKNOWN:
22457 #endif
22458         /* Set the flags in the private structure.  */
22459         if (uses_apcs_26)      flags |= F_APCS26;
22460         if (support_interwork) flags |= F_INTERWORK;
22461         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
22462         if (pic_code)          flags |= F_PIC;
22463         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
22464           flags |= F_SOFT_FLOAT;
22465
22466         switch (mfloat_abi_opt)
22467           {
22468           case ARM_FLOAT_ABI_SOFT:
22469           case ARM_FLOAT_ABI_SOFTFP:
22470             flags |= F_SOFT_FLOAT;
22471             break;
22472
22473           case ARM_FLOAT_ABI_HARD:
22474             if (flags & F_SOFT_FLOAT)
22475               as_bad (_("hard-float conflicts with specified fpu"));
22476             break;
22477           }
22478
22479         /* Using pure-endian doubles (even if soft-float).      */
22480         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
22481           flags |= F_VFP_FLOAT;
22482
22483 #if defined OBJ_ELF
22484         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
22485             flags |= EF_ARM_MAVERICK_FLOAT;
22486         break;
22487
22488       case EF_ARM_EABI_VER4:
22489       case EF_ARM_EABI_VER5:
22490         /* No additional flags to set.  */
22491         break;
22492
22493       default:
22494         abort ();
22495       }
22496 #endif
22497     bfd_set_private_flags (stdoutput, flags);
22498
22499     /* We have run out flags in the COFF header to encode the
22500        status of ATPCS support, so instead we create a dummy,
22501        empty, debug section called .arm.atpcs.  */
22502     if (atpcs)
22503       {
22504         asection * sec;
22505
22506         sec = bfd_make_section (stdoutput, ".arm.atpcs");
22507
22508         if (sec != NULL)
22509           {
22510             bfd_set_section_flags
22511               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
22512             bfd_set_section_size (stdoutput, sec, 0);
22513             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
22514           }
22515       }
22516   }
22517 #endif
22518
22519   /* Record the CPU type as well.  */
22520   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
22521     mach = bfd_mach_arm_iWMMXt2;
22522   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
22523     mach = bfd_mach_arm_iWMMXt;
22524   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
22525     mach = bfd_mach_arm_XScale;
22526   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
22527     mach = bfd_mach_arm_ep9312;
22528   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
22529     mach = bfd_mach_arm_5TE;
22530   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
22531     {
22532       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
22533         mach = bfd_mach_arm_5T;
22534       else
22535         mach = bfd_mach_arm_5;
22536     }
22537   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
22538     {
22539       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
22540         mach = bfd_mach_arm_4T;
22541       else
22542         mach = bfd_mach_arm_4;
22543     }
22544   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
22545     mach = bfd_mach_arm_3M;
22546   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
22547     mach = bfd_mach_arm_3;
22548   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
22549     mach = bfd_mach_arm_2a;
22550   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
22551     mach = bfd_mach_arm_2;
22552   else
22553     mach = bfd_mach_arm_unknown;
22554
22555   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
22556 }
22557
22558 /* Command line processing.  */
22559
22560 /* md_parse_option
22561       Invocation line includes a switch not recognized by the base assembler.
22562       See if it's a processor-specific option.
22563
22564       This routine is somewhat complicated by the need for backwards
22565       compatibility (since older releases of gcc can't be changed).
22566       The new options try to make the interface as compatible as
22567       possible with GCC.
22568
22569       New options (supported) are:
22570
22571               -mcpu=<cpu name>           Assemble for selected processor
22572               -march=<architecture name> Assemble for selected architecture
22573               -mfpu=<fpu architecture>   Assemble for selected FPU.
22574               -EB/-mbig-endian           Big-endian
22575               -EL/-mlittle-endian        Little-endian
22576               -k                         Generate PIC code
22577               -mthumb                    Start in Thumb mode
22578               -mthumb-interwork          Code supports ARM/Thumb interworking
22579
22580               -m[no-]warn-deprecated     Warn about deprecated features
22581
22582       For now we will also provide support for:
22583
22584               -mapcs-32                  32-bit Program counter
22585               -mapcs-26                  26-bit Program counter
22586               -macps-float               Floats passed in FP registers
22587               -mapcs-reentrant           Reentrant code
22588               -matpcs
22589       (sometime these will probably be replaced with -mapcs=<list of options>
22590       and -matpcs=<list of options>)
22591
22592       The remaining options are only supported for back-wards compatibility.
22593       Cpu variants, the arm part is optional:
22594               -m[arm]1                Currently not supported.
22595               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
22596               -m[arm]3                Arm 3 processor
22597               -m[arm]6[xx],           Arm 6 processors
22598               -m[arm]7[xx][t][[d]m]   Arm 7 processors
22599               -m[arm]8[10]            Arm 8 processors
22600               -m[arm]9[20][tdmi]      Arm 9 processors
22601               -mstrongarm[110[0]]     StrongARM processors
22602               -mxscale                XScale processors
22603               -m[arm]v[2345[t[e]]]    Arm architectures
22604               -mall                   All (except the ARM1)
22605       FP variants:
22606               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
22607               -mfpe-old               (No float load/store multiples)
22608               -mvfpxd                 VFP Single precision
22609               -mvfp                   All VFP
22610               -mno-fpu                Disable all floating point instructions
22611
22612       The following CPU names are recognized:
22613               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
22614               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
22615               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
22616               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
22617               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
22618               arm10t arm10e, arm1020t, arm1020e, arm10200e,
22619               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
22620
22621       */
22622
22623 const char * md_shortopts = "m:k";
22624
22625 #ifdef ARM_BI_ENDIAN
22626 #define OPTION_EB (OPTION_MD_BASE + 0)
22627 #define OPTION_EL (OPTION_MD_BASE + 1)
22628 #else
22629 #if TARGET_BYTES_BIG_ENDIAN
22630 #define OPTION_EB (OPTION_MD_BASE + 0)
22631 #else
22632 #define OPTION_EL (OPTION_MD_BASE + 1)
22633 #endif
22634 #endif
22635 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
22636
22637 struct option md_longopts[] =
22638 {
22639 #ifdef OPTION_EB
22640   {"EB", no_argument, NULL, OPTION_EB},
22641 #endif
22642 #ifdef OPTION_EL
22643   {"EL", no_argument, NULL, OPTION_EL},
22644 #endif
22645   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
22646   {NULL, no_argument, NULL, 0}
22647 };
22648
22649 size_t md_longopts_size = sizeof (md_longopts);
22650
22651 struct arm_option_table
22652 {
22653   char *option;         /* Option name to match.  */
22654   char *help;           /* Help information.  */
22655   int  *var;            /* Variable to change.  */
22656   int   value;          /* What to change it to.  */
22657   char *deprecated;     /* If non-null, print this message.  */
22658 };
22659
22660 struct arm_option_table arm_opts[] =
22661 {
22662   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
22663   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
22664   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
22665    &support_interwork, 1, NULL},
22666   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
22667   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
22668   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
22669    1, NULL},
22670   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
22671   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
22672   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
22673   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
22674    NULL},
22675
22676   /* These are recognized by the assembler, but have no affect on code.  */
22677   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
22678   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
22679
22680   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
22681   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
22682    &warn_on_deprecated, 0, NULL},
22683   {NULL, NULL, NULL, 0, NULL}
22684 };
22685
22686 struct arm_legacy_option_table
22687 {
22688   char *option;                         /* Option name to match.  */
22689   const arm_feature_set **var;          /* Variable to change.  */
22690   const arm_feature_set value;          /* What to change it to.  */
22691   char *deprecated;                     /* If non-null, print this message.  */
22692 };
22693
22694 const struct arm_legacy_option_table arm_legacy_opts[] =
22695 {
22696   /* DON'T add any new processors to this list -- we want the whole list
22697      to go away...  Add them to the processors table instead.  */
22698   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
22699   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
22700   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
22701   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
22702   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
22703   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
22704   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
22705   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
22706   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
22707   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
22708   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
22709   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
22710   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
22711   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
22712   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
22713   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
22714   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
22715   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
22716   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
22717   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
22718   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
22719   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
22720   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
22721   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
22722   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
22723   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
22724   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
22725   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
22726   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
22727   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
22728   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
22729   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
22730   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
22731   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
22732   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
22733   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
22734   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
22735   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
22736   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
22737   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
22738   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
22739   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
22740   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
22741   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
22742   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
22743   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
22744   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22745   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22746   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22747   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
22748   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
22749   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
22750   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
22751   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
22752   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
22753   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
22754   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
22755   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
22756   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
22757   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
22758   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
22759   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
22760   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
22761   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
22762   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
22763   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
22764   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
22765   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
22766   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
22767   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
22768    N_("use -mcpu=strongarm110")},
22769   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
22770    N_("use -mcpu=strongarm1100")},
22771   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
22772    N_("use -mcpu=strongarm1110")},
22773   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
22774   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
22775   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
22776
22777   /* Architecture variants -- don't add any more to this list either.  */
22778   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
22779   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
22780   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
22781   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
22782   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
22783   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
22784   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
22785   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
22786   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
22787   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
22788   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
22789   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
22790   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
22791   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
22792   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
22793   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
22794   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
22795   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
22796
22797   /* Floating point variants -- don't add any more to this list either.  */
22798   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
22799   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
22800   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
22801   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
22802    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
22803
22804   {NULL, NULL, ARM_ARCH_NONE, NULL}
22805 };
22806
22807 struct arm_cpu_option_table
22808 {
22809   char *name;
22810   const arm_feature_set value;
22811   /* For some CPUs we assume an FPU unless the user explicitly sets
22812      -mfpu=...  */
22813   const arm_feature_set default_fpu;
22814   /* The canonical name of the CPU, or NULL to use NAME converted to upper
22815      case.  */
22816   const char *canonical_name;
22817 };
22818
22819 /* This list should, at a minimum, contain all the cpu names
22820    recognized by GCC.  */
22821 static const struct arm_cpu_option_table arm_cpus[] =
22822 {
22823   {"all",               ARM_ANY,         FPU_ARCH_FPA,    NULL},
22824   {"arm1",              ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL},
22825   {"arm2",              ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL},
22826   {"arm250",            ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
22827   {"arm3",              ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
22828   {"arm6",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22829   {"arm60",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22830   {"arm600",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22831   {"arm610",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22832   {"arm620",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22833   {"arm7",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22834   {"arm7m",             ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
22835   {"arm7d",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22836   {"arm7dm",            ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
22837   {"arm7di",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22838   {"arm7dmi",           ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
22839   {"arm70",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22840   {"arm700",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22841   {"arm700i",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22842   {"arm710",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22843   {"arm710t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22844   {"arm720",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22845   {"arm720t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22846   {"arm740t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22847   {"arm710c",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22848   {"arm7100",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22849   {"arm7500",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22850   {"arm7500fe",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
22851   {"arm7t",             ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22852   {"arm7tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22853   {"arm7tdmi-s",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22854   {"arm8",              ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22855   {"arm810",            ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22856   {"strongarm",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22857   {"strongarm1",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22858   {"strongarm110",      ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22859   {"strongarm1100",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22860   {"strongarm1110",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22861   {"arm9",              ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22862   {"arm920",            ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"},
22863   {"arm920t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22864   {"arm922t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22865   {"arm940t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22866   {"arm9tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
22867   {"fa526",             ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22868   {"fa626",             ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
22869   /* For V5 or later processors we default to using VFP; but the user
22870      should really set the FPU type explicitly.  */
22871   {"arm9e-r0",          ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
22872   {"arm9e",             ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22873   {"arm926ej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
22874   {"arm926ejs",         ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
22875   {"arm926ej-s",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
22876   {"arm946e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
22877   {"arm946e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"},
22878   {"arm946e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22879   {"arm966e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
22880   {"arm966e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"},
22881   {"arm966e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22882   {"arm968e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22883   {"arm10t",            ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
22884   {"arm10tdmi",         ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
22885   {"arm10e",            ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22886   {"arm1020",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"},
22887   {"arm1020t",          ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
22888   {"arm1020e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22889   {"arm1022e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22890   {"arm1026ejs",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM1026EJ-S"},
22891   {"arm1026ej-s",       ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
22892   {"fa606te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22893   {"fa616te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22894   {"fa626te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22895   {"fmp626",            ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22896   {"fa726te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
22897   {"arm1136js",         ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"},
22898   {"arm1136j-s",        ARM_ARCH_V6,     FPU_NONE,        NULL},
22899   {"arm1136jfs",        ARM_ARCH_V6,     FPU_ARCH_VFP_V2, "ARM1136JF-S"},
22900   {"arm1136jf-s",       ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL},
22901   {"mpcore",            ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, "MPCore"},
22902   {"mpcorenovfp",       ARM_ARCH_V6K,    FPU_NONE,        "MPCore"},
22903   {"arm1156t2-s",       ARM_ARCH_V6T2,   FPU_NONE,        NULL},
22904   {"arm1156t2f-s",      ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL},
22905   {"arm1176jz-s",       ARM_ARCH_V6ZK,   FPU_NONE,        NULL},
22906   {"arm1176jzf-s",      ARM_ARCH_V6ZK,   FPU_ARCH_VFP_V2, NULL},
22907   {"cortex-a5",         ARM_ARCH_V7A_MP_SEC, 
22908                                          FPU_NONE,        "Cortex-A5"},
22909   {"cortex-a8",         ARM_ARCH_V7A_SEC,
22910                                          ARM_FEATURE (0, FPU_VFP_V3
22911                                                         | FPU_NEON_EXT_V1),
22912                                                           "Cortex-A8"},
22913   {"cortex-a9",         ARM_ARCH_V7A_MP_SEC,
22914                                          ARM_FEATURE (0, FPU_VFP_V3
22915                                                         | FPU_NEON_EXT_V1),
22916                                                           "Cortex-A9"},
22917   {"cortex-a15",        ARM_ARCH_V7A_IDIV_MP_SEC_VIRT,
22918                                          FPU_ARCH_NEON_VFP_V4,
22919                                                           "Cortex-A15"},
22920   {"cortex-r4",         ARM_ARCH_V7R,    FPU_NONE,        "Cortex-R4"},
22921   {"cortex-r4f",        ARM_ARCH_V7R,    FPU_ARCH_VFP_V3D16,
22922                                                           "Cortex-R4F"},
22923   {"cortex-r5",         ARM_ARCH_V7R_IDIV,
22924                                          FPU_NONE,        "Cortex-R5"},
22925   {"cortex-m4",         ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M4"},
22926   {"cortex-m3",         ARM_ARCH_V7M,    FPU_NONE,        "Cortex-M3"},
22927   {"cortex-m1",         ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M1"},
22928   {"cortex-m0",         ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0"},
22929   /* ??? XSCALE is really an architecture.  */
22930   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
22931   /* ??? iwmmxt is not a processor.  */
22932   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL},
22933   {"iwmmxt2",           ARM_ARCH_IWMMXT2,FPU_ARCH_VFP_V2, NULL},
22934   {"i80200",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
22935   /* Maverick */
22936   {"ep9312",    ARM_FEATURE (ARM_AEXT_V4T, ARM_CEXT_MAVERICK), FPU_ARCH_MAVERICK, "ARM920T"},
22937   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE, NULL}
22938 };
22939
22940 struct arm_arch_option_table
22941 {
22942   char *name;
22943   const arm_feature_set value;
22944   const arm_feature_set default_fpu;
22945 };
22946
22947 /* This list should, at a minimum, contain all the architecture names
22948    recognized by GCC.  */
22949 static const struct arm_arch_option_table arm_archs[] =
22950 {
22951   {"all",               ARM_ANY,         FPU_ARCH_FPA},
22952   {"armv1",             ARM_ARCH_V1,     FPU_ARCH_FPA},
22953   {"armv2",             ARM_ARCH_V2,     FPU_ARCH_FPA},
22954   {"armv2a",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
22955   {"armv2s",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
22956   {"armv3",             ARM_ARCH_V3,     FPU_ARCH_FPA},
22957   {"armv3m",            ARM_ARCH_V3M,    FPU_ARCH_FPA},
22958   {"armv4",             ARM_ARCH_V4,     FPU_ARCH_FPA},
22959   {"armv4xm",           ARM_ARCH_V4xM,   FPU_ARCH_FPA},
22960   {"armv4t",            ARM_ARCH_V4T,    FPU_ARCH_FPA},
22961   {"armv4txm",          ARM_ARCH_V4TxM,  FPU_ARCH_FPA},
22962   {"armv5",             ARM_ARCH_V5,     FPU_ARCH_VFP},
22963   {"armv5t",            ARM_ARCH_V5T,    FPU_ARCH_VFP},
22964   {"armv5txm",          ARM_ARCH_V5TxM,  FPU_ARCH_VFP},
22965   {"armv5te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP},
22966   {"armv5texp",         ARM_ARCH_V5TExP, FPU_ARCH_VFP},
22967   {"armv5tej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP},
22968   {"armv6",             ARM_ARCH_V6,     FPU_ARCH_VFP},
22969   {"armv6j",            ARM_ARCH_V6,     FPU_ARCH_VFP},
22970   {"armv6k",            ARM_ARCH_V6K,    FPU_ARCH_VFP},
22971   {"armv6z",            ARM_ARCH_V6Z,    FPU_ARCH_VFP},
22972   {"armv6zk",           ARM_ARCH_V6ZK,   FPU_ARCH_VFP},
22973   {"armv6t2",           ARM_ARCH_V6T2,   FPU_ARCH_VFP},
22974   {"armv6kt2",          ARM_ARCH_V6KT2,  FPU_ARCH_VFP},
22975   {"armv6zt2",          ARM_ARCH_V6ZT2,  FPU_ARCH_VFP},
22976   {"armv6zkt2",         ARM_ARCH_V6ZKT2, FPU_ARCH_VFP},
22977   {"armv6-m",           ARM_ARCH_V6M,    FPU_ARCH_VFP},
22978   {"armv6s-m",          ARM_ARCH_V6SM,   FPU_ARCH_VFP},
22979   {"armv7",             ARM_ARCH_V7,     FPU_ARCH_VFP},
22980   /* The official spelling of the ARMv7 profile variants is the dashed form.
22981      Accept the non-dashed form for compatibility with old toolchains.  */
22982   {"armv7a",            ARM_ARCH_V7A,    FPU_ARCH_VFP},
22983   {"armv7r",            ARM_ARCH_V7R,    FPU_ARCH_VFP},
22984   {"armv7m",            ARM_ARCH_V7M,    FPU_ARCH_VFP},
22985   {"armv7-a",           ARM_ARCH_V7A,    FPU_ARCH_VFP},
22986   {"armv7-r",           ARM_ARCH_V7R,    FPU_ARCH_VFP},
22987   {"armv7-m",           ARM_ARCH_V7M,    FPU_ARCH_VFP},
22988   {"armv7e-m",          ARM_ARCH_V7EM,   FPU_ARCH_VFP},
22989   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP},
22990   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP},
22991   {"iwmmxt2",           ARM_ARCH_IWMMXT2,FPU_ARCH_VFP},
22992   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE}
22993 };
22994
22995 /* ISA extensions in the co-processor and main instruction set space.  */
22996 struct arm_option_extension_value_table
22997 {
22998   char *name;
22999   const arm_feature_set value;
23000   const arm_feature_set allowed_archs;
23001 };
23002
23003 /* The following table must be in alphabetical order with a NULL last entry.
23004    */
23005 static const struct arm_option_extension_value_table arm_extensions[] =
23006 {
23007   {"idiv",      ARM_FEATURE (ARM_EXT_ADIV | ARM_EXT_DIV, 0),
23008                                    ARM_FEATURE (ARM_EXT_V7A | ARM_EXT_V7R, 0)},
23009   {"iwmmxt",    ARM_FEATURE (0, ARM_CEXT_IWMMXT),       ARM_ANY},
23010   {"iwmmxt2",   ARM_FEATURE (0, ARM_CEXT_IWMMXT2),      ARM_ANY},
23011   {"maverick",  ARM_FEATURE (0, ARM_CEXT_MAVERICK),     ARM_ANY},
23012   {"mp",        ARM_FEATURE (ARM_EXT_MP, 0),
23013                      ARM_FEATURE (ARM_EXT_V7A | ARM_EXT_V7R, 0)},
23014   {"os",        ARM_FEATURE (ARM_EXT_OS, 0),
23015                                    ARM_FEATURE (ARM_EXT_V6M, 0)},
23016   {"sec",       ARM_FEATURE (ARM_EXT_SEC, 0),
23017                      ARM_FEATURE (ARM_EXT_V6K | ARM_EXT_V7A, 0)},
23018   {"virt",      ARM_FEATURE (ARM_EXT_VIRT | ARM_EXT_ADIV | ARM_EXT_DIV, 0),
23019                                    ARM_FEATURE (ARM_EXT_V7A, 0)},
23020   {"xscale",    ARM_FEATURE (0, ARM_CEXT_XSCALE),       ARM_ANY},
23021   {NULL,        ARM_ARCH_NONE,                    ARM_ARCH_NONE}
23022 };
23023
23024 /* ISA floating-point and Advanced SIMD extensions.  */
23025 struct arm_option_fpu_value_table
23026 {
23027   char *name;
23028   const arm_feature_set value;
23029 };
23030
23031 /* This list should, at a minimum, contain all the fpu names
23032    recognized by GCC.  */
23033 static const struct arm_option_fpu_value_table arm_fpus[] =
23034 {
23035   {"softfpa",           FPU_NONE},
23036   {"fpe",               FPU_ARCH_FPE},
23037   {"fpe2",              FPU_ARCH_FPE},
23038   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
23039   {"fpa",               FPU_ARCH_FPA},
23040   {"fpa10",             FPU_ARCH_FPA},
23041   {"fpa11",             FPU_ARCH_FPA},
23042   {"arm7500fe",         FPU_ARCH_FPA},
23043   {"softvfp",           FPU_ARCH_VFP},
23044   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
23045   {"vfp",               FPU_ARCH_VFP_V2},
23046   {"vfp9",              FPU_ARCH_VFP_V2},
23047   {"vfp3",              FPU_ARCH_VFP_V3}, /* For backwards compatbility.  */
23048   {"vfp10",             FPU_ARCH_VFP_V2},
23049   {"vfp10-r0",          FPU_ARCH_VFP_V1},
23050   {"vfpxd",             FPU_ARCH_VFP_V1xD},
23051   {"vfpv2",             FPU_ARCH_VFP_V2},
23052   {"vfpv3",             FPU_ARCH_VFP_V3},
23053   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
23054   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
23055   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
23056   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
23057   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
23058   {"arm1020t",          FPU_ARCH_VFP_V1},
23059   {"arm1020e",          FPU_ARCH_VFP_V2},
23060   {"arm1136jfs",        FPU_ARCH_VFP_V2},
23061   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
23062   {"maverick",          FPU_ARCH_MAVERICK},
23063   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
23064   {"neon-fp16",         FPU_ARCH_NEON_FP16},
23065   {"vfpv4",             FPU_ARCH_VFP_V4},
23066   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
23067   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
23068   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
23069   {NULL,                ARM_ARCH_NONE}
23070 };
23071
23072 struct arm_option_value_table
23073 {
23074   char *name;
23075   long value;
23076 };
23077
23078 static const struct arm_option_value_table arm_float_abis[] =
23079 {
23080   {"hard",      ARM_FLOAT_ABI_HARD},
23081   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
23082   {"soft",      ARM_FLOAT_ABI_SOFT},
23083   {NULL,        0}
23084 };
23085
23086 #ifdef OBJ_ELF
23087 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
23088 static const struct arm_option_value_table arm_eabis[] =
23089 {
23090   {"gnu",       EF_ARM_EABI_UNKNOWN},
23091   {"4",         EF_ARM_EABI_VER4},
23092   {"5",         EF_ARM_EABI_VER5},
23093   {NULL,        0}
23094 };
23095 #endif
23096
23097 struct arm_long_option_table
23098 {
23099   char * option;                /* Substring to match.  */
23100   char * help;                  /* Help information.  */
23101   int (* func) (char * subopt); /* Function to decode sub-option.  */
23102   char * deprecated;            /* If non-null, print this message.  */
23103 };
23104
23105 static bfd_boolean
23106 arm_parse_extension (char * str, const arm_feature_set **opt_p)
23107 {
23108   arm_feature_set *ext_set = (arm_feature_set *)
23109       xmalloc (sizeof (arm_feature_set));
23110
23111   /* We insist on extensions being specified in alphabetical order, and with
23112      extensions being added before being removed.  We achieve this by having 
23113      the global ARM_EXTENSIONS table in alphabetical order, and using the 
23114      ADDING_VALUE variable to indicate whether we are adding an extension (1)
23115      or removing it (0) and only allowing it to change in the order 
23116      -1 -> 1 -> 0.  */
23117   const struct arm_option_extension_value_table * opt = NULL;
23118   int adding_value = -1;
23119
23120   /* Copy the feature set, so that we can modify it.  */
23121   *ext_set = **opt_p;
23122   *opt_p = ext_set;
23123
23124   while (str != NULL && *str != 0)
23125     {
23126       char * ext;
23127       size_t optlen;
23128
23129       if (*str != '+')
23130         {
23131           as_bad (_("invalid architectural extension"));
23132           return FALSE;
23133         }
23134
23135       str++;
23136       ext = strchr (str, '+');
23137
23138       if (ext != NULL)
23139         optlen = ext - str;
23140       else
23141         optlen = strlen (str);
23142
23143       if (optlen >= 2
23144           && strncmp (str, "no", 2) == 0)
23145         {
23146           if (adding_value != 0)
23147             {
23148               adding_value = 0;
23149               opt = arm_extensions;
23150             }
23151
23152           optlen -= 2;
23153           str += 2;
23154         }
23155       else if (optlen > 0)
23156         {
23157           if (adding_value == -1)
23158             {
23159               adding_value = 1;
23160               opt = arm_extensions;
23161             }
23162           else if (adding_value != 1)
23163             {
23164               as_bad (_("must specify extensions to add before specifying "
23165                         "those to remove"));
23166               return FALSE;
23167             }
23168         }
23169
23170       if (optlen == 0)
23171         {
23172           as_bad (_("missing architectural extension"));
23173           return FALSE;
23174         }
23175
23176       gas_assert (adding_value != -1);
23177       gas_assert (opt != NULL);
23178
23179       /* Scan over the options table trying to find an exact match. */
23180       for (; opt->name != NULL; opt++)
23181         if (strncmp (opt->name, str, optlen) == 0
23182             && strlen (opt->name) == optlen)
23183           {
23184             /* Check we can apply the extension to this architecture.  */
23185             if (!ARM_CPU_HAS_FEATURE (*ext_set, opt->allowed_archs))
23186               {
23187                 as_bad (_("extension does not apply to the base architecture"));
23188                 return FALSE;
23189               }
23190
23191             /* Add or remove the extension.  */
23192             if (adding_value)
23193               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->value);
23194             else
23195               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->value);
23196
23197             break;
23198           }
23199
23200       if (opt->name == NULL)
23201         {
23202           /* Did we fail to find an extension because it wasn't specified in
23203              alphabetical order, or because it does not exist?  */
23204
23205           for (opt = arm_extensions; opt->name != NULL; opt++)
23206             if (strncmp (opt->name, str, optlen) == 0)
23207               break;
23208
23209           if (opt->name == NULL)
23210             as_bad (_("unknown architectural extension `%s'"), str);
23211           else
23212             as_bad (_("architectural extensions must be specified in "
23213                       "alphabetical order"));
23214
23215           return FALSE;
23216         }
23217       else
23218         {
23219           /* We should skip the extension we've just matched the next time
23220              round.  */
23221           opt++;
23222         }
23223
23224       str = ext;
23225     };
23226
23227   return TRUE;
23228 }
23229
23230 static bfd_boolean
23231 arm_parse_cpu (char * str)
23232 {
23233   const struct arm_cpu_option_table * opt;
23234   char * ext = strchr (str, '+');
23235   int optlen;
23236
23237   if (ext != NULL)
23238     optlen = ext - str;
23239   else
23240     optlen = strlen (str);
23241
23242   if (optlen == 0)
23243     {
23244       as_bad (_("missing cpu name `%s'"), str);
23245       return FALSE;
23246     }
23247
23248   for (opt = arm_cpus; opt->name != NULL; opt++)
23249     if (strncmp (opt->name, str, optlen) == 0)
23250       {
23251         mcpu_cpu_opt = &opt->value;
23252         mcpu_fpu_opt = &opt->default_fpu;
23253         if (opt->canonical_name)
23254           strcpy (selected_cpu_name, opt->canonical_name);
23255         else
23256           {
23257             int i;
23258
23259             for (i = 0; i < optlen; i++)
23260               selected_cpu_name[i] = TOUPPER (opt->name[i]);
23261             selected_cpu_name[i] = 0;
23262           }
23263
23264         if (ext != NULL)
23265           return arm_parse_extension (ext, &mcpu_cpu_opt);
23266
23267         return TRUE;
23268       }
23269
23270   as_bad (_("unknown cpu `%s'"), str);
23271   return FALSE;
23272 }
23273
23274 static bfd_boolean
23275 arm_parse_arch (char * str)
23276 {
23277   const struct arm_arch_option_table *opt;
23278   char *ext = strchr (str, '+');
23279   int optlen;
23280
23281   if (ext != NULL)
23282     optlen = ext - str;
23283   else
23284     optlen = strlen (str);
23285
23286   if (optlen == 0)
23287     {
23288       as_bad (_("missing architecture name `%s'"), str);
23289       return FALSE;
23290     }
23291
23292   for (opt = arm_archs; opt->name != NULL; opt++)
23293     if (strncmp (opt->name, str, optlen) == 0)
23294       {
23295         march_cpu_opt = &opt->value;
23296         march_fpu_opt = &opt->default_fpu;
23297         strcpy (selected_cpu_name, opt->name);
23298
23299         if (ext != NULL)
23300           return arm_parse_extension (ext, &march_cpu_opt);
23301
23302         return TRUE;
23303       }
23304
23305   as_bad (_("unknown architecture `%s'\n"), str);
23306   return FALSE;
23307 }
23308
23309 static bfd_boolean
23310 arm_parse_fpu (char * str)
23311 {
23312   const struct arm_option_fpu_value_table * opt;
23313
23314   for (opt = arm_fpus; opt->name != NULL; opt++)
23315     if (streq (opt->name, str))
23316       {
23317         mfpu_opt = &opt->value;
23318         return TRUE;
23319       }
23320
23321   as_bad (_("unknown floating point format `%s'\n"), str);
23322   return FALSE;
23323 }
23324
23325 static bfd_boolean
23326 arm_parse_float_abi (char * str)
23327 {
23328   const struct arm_option_value_table * opt;
23329
23330   for (opt = arm_float_abis; opt->name != NULL; opt++)
23331     if (streq (opt->name, str))
23332       {
23333         mfloat_abi_opt = opt->value;
23334         return TRUE;
23335       }
23336
23337   as_bad (_("unknown floating point abi `%s'\n"), str);
23338   return FALSE;
23339 }
23340
23341 #ifdef OBJ_ELF
23342 static bfd_boolean
23343 arm_parse_eabi (char * str)
23344 {
23345   const struct arm_option_value_table *opt;
23346
23347   for (opt = arm_eabis; opt->name != NULL; opt++)
23348     if (streq (opt->name, str))
23349       {
23350         meabi_flags = opt->value;
23351         return TRUE;
23352       }
23353   as_bad (_("unknown EABI `%s'\n"), str);
23354   return FALSE;
23355 }
23356 #endif
23357
23358 static bfd_boolean
23359 arm_parse_it_mode (char * str)
23360 {
23361   bfd_boolean ret = TRUE;
23362
23363   if (streq ("arm", str))
23364     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
23365   else if (streq ("thumb", str))
23366     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
23367   else if (streq ("always", str))
23368     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
23369   else if (streq ("never", str))
23370     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
23371   else
23372     {
23373       as_bad (_("unknown implicit IT mode `%s', should be "\
23374                 "arm, thumb, always, or never."), str);
23375       ret = FALSE;
23376     }
23377
23378   return ret;
23379 }
23380
23381 struct arm_long_option_table arm_long_opts[] =
23382 {
23383   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
23384    arm_parse_cpu, NULL},
23385   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
23386    arm_parse_arch, NULL},
23387   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
23388    arm_parse_fpu, NULL},
23389   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
23390    arm_parse_float_abi, NULL},
23391 #ifdef OBJ_ELF
23392   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
23393    arm_parse_eabi, NULL},
23394 #endif
23395   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
23396    arm_parse_it_mode, NULL},
23397   {NULL, NULL, 0, NULL}
23398 };
23399
23400 int
23401 md_parse_option (int c, char * arg)
23402 {
23403   struct arm_option_table *opt;
23404   const struct arm_legacy_option_table *fopt;
23405   struct arm_long_option_table *lopt;
23406
23407   switch (c)
23408     {
23409 #ifdef OPTION_EB
23410     case OPTION_EB:
23411       target_big_endian = 1;
23412       break;
23413 #endif
23414
23415 #ifdef OPTION_EL
23416     case OPTION_EL:
23417       target_big_endian = 0;
23418       break;
23419 #endif
23420
23421     case OPTION_FIX_V4BX:
23422       fix_v4bx = TRUE;
23423       break;
23424
23425     case 'a':
23426       /* Listing option.  Just ignore these, we don't support additional
23427          ones.  */
23428       return 0;
23429
23430     default:
23431       for (opt = arm_opts; opt->option != NULL; opt++)
23432         {
23433           if (c == opt->option[0]
23434               && ((arg == NULL && opt->option[1] == 0)
23435                   || streq (arg, opt->option + 1)))
23436             {
23437               /* If the option is deprecated, tell the user.  */
23438               if (warn_on_deprecated && opt->deprecated != NULL)
23439                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
23440                            arg ? arg : "", _(opt->deprecated));
23441
23442               if (opt->var != NULL)
23443                 *opt->var = opt->value;
23444
23445               return 1;
23446             }
23447         }
23448
23449       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
23450         {
23451           if (c == fopt->option[0]
23452               && ((arg == NULL && fopt->option[1] == 0)
23453                   || streq (arg, fopt->option + 1)))
23454             {
23455               /* If the option is deprecated, tell the user.  */
23456               if (warn_on_deprecated && fopt->deprecated != NULL)
23457                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
23458                            arg ? arg : "", _(fopt->deprecated));
23459
23460               if (fopt->var != NULL)
23461                 *fopt->var = &fopt->value;
23462
23463               return 1;
23464             }
23465         }
23466
23467       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
23468         {
23469           /* These options are expected to have an argument.  */
23470           if (c == lopt->option[0]
23471               && arg != NULL
23472               && strncmp (arg, lopt->option + 1,
23473                           strlen (lopt->option + 1)) == 0)
23474             {
23475               /* If the option is deprecated, tell the user.  */
23476               if (warn_on_deprecated && lopt->deprecated != NULL)
23477                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
23478                            _(lopt->deprecated));
23479
23480               /* Call the sup-option parser.  */
23481               return lopt->func (arg + strlen (lopt->option) - 1);
23482             }
23483         }
23484
23485       return 0;
23486     }
23487
23488   return 1;
23489 }
23490
23491 void
23492 md_show_usage (FILE * fp)
23493 {
23494   struct arm_option_table *opt;
23495   struct arm_long_option_table *lopt;
23496
23497   fprintf (fp, _(" ARM-specific assembler options:\n"));
23498
23499   for (opt = arm_opts; opt->option != NULL; opt++)
23500     if (opt->help != NULL)
23501       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
23502
23503   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
23504     if (lopt->help != NULL)
23505       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
23506
23507 #ifdef OPTION_EB
23508   fprintf (fp, _("\
23509   -EB                     assemble code for a big-endian cpu\n"));
23510 #endif
23511
23512 #ifdef OPTION_EL
23513   fprintf (fp, _("\
23514   -EL                     assemble code for a little-endian cpu\n"));
23515 #endif
23516
23517   fprintf (fp, _("\
23518   --fix-v4bx              Allow BX in ARMv4 code\n"));
23519 }
23520
23521
23522 #ifdef OBJ_ELF
23523 typedef struct
23524 {
23525   int val;
23526   arm_feature_set flags;
23527 } cpu_arch_ver_table;
23528
23529 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
23530    least features first.  */
23531 static const cpu_arch_ver_table cpu_arch_ver[] =
23532 {
23533     {1, ARM_ARCH_V4},
23534     {2, ARM_ARCH_V4T},
23535     {3, ARM_ARCH_V5},
23536     {3, ARM_ARCH_V5T},
23537     {4, ARM_ARCH_V5TE},
23538     {5, ARM_ARCH_V5TEJ},
23539     {6, ARM_ARCH_V6},
23540     {9, ARM_ARCH_V6K},
23541     {7, ARM_ARCH_V6Z},
23542     {11, ARM_ARCH_V6M},
23543     {12, ARM_ARCH_V6SM},
23544     {8, ARM_ARCH_V6T2},
23545     {10, ARM_ARCH_V7A},
23546     {10, ARM_ARCH_V7R},
23547     {10, ARM_ARCH_V7M},
23548     {0, ARM_ARCH_NONE}
23549 };
23550
23551 /* Set an attribute if it has not already been set by the user.  */
23552 static void
23553 aeabi_set_attribute_int (int tag, int value)
23554 {
23555   if (tag < 1
23556       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
23557       || !attributes_set_explicitly[tag])
23558     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
23559 }
23560
23561 static void
23562 aeabi_set_attribute_string (int tag, const char *value)
23563 {
23564   if (tag < 1
23565       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
23566       || !attributes_set_explicitly[tag])
23567     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
23568 }
23569
23570 /* Set the public EABI object attributes.  */
23571 static void
23572 aeabi_set_public_attributes (void)
23573 {
23574   int arch;
23575   int virt_sec = 0;
23576   arm_feature_set flags;
23577   arm_feature_set tmp;
23578   const cpu_arch_ver_table *p;
23579
23580   /* Choose the architecture based on the capabilities of the requested cpu
23581      (if any) and/or the instructions actually used.  */
23582   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
23583   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
23584   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
23585   /*Allow the user to override the reported architecture.  */
23586   if (object_arch)
23587     {
23588       ARM_CLEAR_FEATURE (flags, flags, arm_arch_any);
23589       ARM_MERGE_FEATURE_SETS (flags, flags, *object_arch);
23590     }
23591
23592   /* We need to make sure that the attributes do not identify us as v6S-M
23593      when the only v6S-M feature in use is the Operating System Extensions.  */
23594   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_os))
23595       if (!ARM_CPU_HAS_FEATURE (flags, arm_arch_v6m_only))
23596         ARM_CLEAR_FEATURE (flags, flags, arm_ext_os);
23597
23598   tmp = flags;
23599   arch = 0;
23600   for (p = cpu_arch_ver; p->val; p++)
23601     {
23602       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
23603         {
23604           arch = p->val;
23605           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
23606         }
23607     }
23608
23609   /* The table lookup above finds the last architecture to contribute
23610      a new feature.  Unfortunately, Tag13 is a subset of the union of
23611      v6T2 and v7-M, so it is never seen as contributing a new feature.
23612      We can not search for the last entry which is entirely used,
23613      because if no CPU is specified we build up only those flags
23614      actually used.  Perhaps we should separate out the specified
23615      and implicit cases.  Avoid taking this path for -march=all by
23616      checking for contradictory v7-A / v7-M features.  */
23617   if (arch == 10
23618       && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
23619       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m)
23620       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v6_dsp))
23621     arch = 13;
23622
23623   /* Tag_CPU_name.  */
23624   if (selected_cpu_name[0])
23625     {
23626       char *q;
23627
23628       q = selected_cpu_name;
23629       if (strncmp (q, "armv", 4) == 0)
23630         {
23631           int i;
23632
23633           q += 4;
23634           for (i = 0; q[i]; i++)
23635             q[i] = TOUPPER (q[i]);
23636         }
23637       aeabi_set_attribute_string (Tag_CPU_name, q);
23638     }
23639
23640   /* Tag_CPU_arch.  */
23641   aeabi_set_attribute_int (Tag_CPU_arch, arch);
23642
23643   /* Tag_CPU_arch_profile.  */
23644   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a))
23645     aeabi_set_attribute_int (Tag_CPU_arch_profile, 'A');
23646   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
23647     aeabi_set_attribute_int (Tag_CPU_arch_profile, 'R');
23648   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_m))
23649     aeabi_set_attribute_int (Tag_CPU_arch_profile, 'M');
23650
23651   /* Tag_ARM_ISA_use.  */
23652   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
23653       || arch == 0)
23654     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
23655
23656   /* Tag_THUMB_ISA_use.  */
23657   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
23658       || arch == 0)
23659     aeabi_set_attribute_int (Tag_THUMB_ISA_use,
23660         ARM_CPU_HAS_FEATURE (flags, arm_arch_t2) ? 2 : 1);
23661
23662   /* Tag_VFP_arch.  */
23663   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
23664     aeabi_set_attribute_int (Tag_VFP_arch,
23665                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
23666                              ? 5 : 6);
23667   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
23668     aeabi_set_attribute_int (Tag_VFP_arch, 3);
23669   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
23670     aeabi_set_attribute_int (Tag_VFP_arch, 4);
23671   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
23672     aeabi_set_attribute_int (Tag_VFP_arch, 2);
23673   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
23674            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
23675     aeabi_set_attribute_int (Tag_VFP_arch, 1);
23676
23677   /* Tag_ABI_HardFP_use.  */
23678   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
23679       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
23680     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
23681
23682   /* Tag_WMMX_arch.  */
23683   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
23684     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
23685   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
23686     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
23687
23688   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
23689   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
23690     aeabi_set_attribute_int
23691       (Tag_Advanced_SIMD_arch, (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma)
23692                                 ? 2 : 1));
23693   
23694   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
23695   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16))
23696     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
23697
23698   /* Tag_DIV_use.  */
23699   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv))
23700     aeabi_set_attribute_int (Tag_DIV_use, 2);
23701   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_div))
23702     aeabi_set_attribute_int (Tag_DIV_use, 0);
23703   else
23704     aeabi_set_attribute_int (Tag_DIV_use, 1);
23705
23706   /* Tag_MP_extension_use.  */
23707   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
23708     aeabi_set_attribute_int (Tag_MPextension_use, 1);
23709
23710   /* Tag Virtualization_use.  */
23711   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
23712     virt_sec |= 1;
23713   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
23714     virt_sec |= 2;
23715   if (virt_sec != 0)
23716     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
23717 }
23718
23719 /* Add the default contents for the .ARM.attributes section.  */
23720 void
23721 arm_md_end (void)
23722 {
23723   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
23724     return;
23725
23726   aeabi_set_public_attributes ();
23727 }
23728 #endif /* OBJ_ELF */
23729
23730
23731 /* Parse a .cpu directive.  */
23732
23733 static void
23734 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
23735 {
23736   const struct arm_cpu_option_table *opt;
23737   char *name;
23738   char saved_char;
23739
23740   name = input_line_pointer;
23741   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23742     input_line_pointer++;
23743   saved_char = *input_line_pointer;
23744   *input_line_pointer = 0;
23745
23746   /* Skip the first "all" entry.  */
23747   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
23748     if (streq (opt->name, name))
23749       {
23750         mcpu_cpu_opt = &opt->value;
23751         selected_cpu = opt->value;
23752         if (opt->canonical_name)
23753           strcpy (selected_cpu_name, opt->canonical_name);
23754         else
23755           {
23756             int i;
23757             for (i = 0; opt->name[i]; i++)
23758               selected_cpu_name[i] = TOUPPER (opt->name[i]);
23759             selected_cpu_name[i] = 0;
23760           }
23761         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23762         *input_line_pointer = saved_char;
23763         demand_empty_rest_of_line ();
23764         return;
23765       }
23766   as_bad (_("unknown cpu `%s'"), name);
23767   *input_line_pointer = saved_char;
23768   ignore_rest_of_line ();
23769 }
23770
23771
23772 /* Parse a .arch directive.  */
23773
23774 static void
23775 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
23776 {
23777   const struct arm_arch_option_table *opt;
23778   char saved_char;
23779   char *name;
23780
23781   name = input_line_pointer;
23782   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23783     input_line_pointer++;
23784   saved_char = *input_line_pointer;
23785   *input_line_pointer = 0;
23786
23787   /* Skip the first "all" entry.  */
23788   for (opt = arm_archs + 1; opt->name != NULL; opt++)
23789     if (streq (opt->name, name))
23790       {
23791         mcpu_cpu_opt = &opt->value;
23792         selected_cpu = opt->value;
23793         strcpy (selected_cpu_name, opt->name);
23794         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23795         *input_line_pointer = saved_char;
23796         demand_empty_rest_of_line ();
23797         return;
23798       }
23799
23800   as_bad (_("unknown architecture `%s'\n"), name);
23801   *input_line_pointer = saved_char;
23802   ignore_rest_of_line ();
23803 }
23804
23805
23806 /* Parse a .object_arch directive.  */
23807
23808 static void
23809 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
23810 {
23811   const struct arm_arch_option_table *opt;
23812   char saved_char;
23813   char *name;
23814
23815   name = input_line_pointer;
23816   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23817     input_line_pointer++;
23818   saved_char = *input_line_pointer;
23819   *input_line_pointer = 0;
23820
23821   /* Skip the first "all" entry.  */
23822   for (opt = arm_archs + 1; opt->name != NULL; opt++)
23823     if (streq (opt->name, name))
23824       {
23825         object_arch = &opt->value;
23826         *input_line_pointer = saved_char;
23827         demand_empty_rest_of_line ();
23828         return;
23829       }
23830
23831   as_bad (_("unknown architecture `%s'\n"), name);
23832   *input_line_pointer = saved_char;
23833   ignore_rest_of_line ();
23834 }
23835
23836 /* Parse a .arch_extension directive.  */
23837
23838 static void
23839 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
23840 {
23841   const struct arm_option_extension_value_table *opt;
23842   char saved_char;
23843   char *name;
23844   int adding_value = 1;
23845
23846   name = input_line_pointer;
23847   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23848     input_line_pointer++;
23849   saved_char = *input_line_pointer;
23850   *input_line_pointer = 0;
23851
23852   if (strlen (name) >= 2
23853       && strncmp (name, "no", 2) == 0)
23854     {
23855       adding_value = 0;
23856       name += 2;
23857     }
23858
23859   for (opt = arm_extensions; opt->name != NULL; opt++)
23860     if (streq (opt->name, name))
23861       {
23862         if (!ARM_CPU_HAS_FEATURE (*mcpu_cpu_opt, opt->allowed_archs))
23863           {
23864             as_bad (_("architectural extension `%s' is not allowed for the "
23865                       "current base architecture"), name);
23866             break;
23867           }
23868
23869         if (adding_value)
23870           ARM_MERGE_FEATURE_SETS (selected_cpu, selected_cpu, opt->value);
23871         else
23872           ARM_CLEAR_FEATURE (selected_cpu, selected_cpu, opt->value);
23873
23874         mcpu_cpu_opt = &selected_cpu;
23875         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23876         *input_line_pointer = saved_char;
23877         demand_empty_rest_of_line ();
23878         return;
23879       }
23880
23881   if (opt->name == NULL)
23882     as_bad (_("unknown architecture `%s'\n"), name);
23883
23884   *input_line_pointer = saved_char;
23885   ignore_rest_of_line ();
23886 }
23887
23888 /* Parse a .fpu directive.  */
23889
23890 static void
23891 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
23892 {
23893   const struct arm_option_fpu_value_table *opt;
23894   char saved_char;
23895   char *name;
23896
23897   name = input_line_pointer;
23898   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
23899     input_line_pointer++;
23900   saved_char = *input_line_pointer;
23901   *input_line_pointer = 0;
23902
23903   for (opt = arm_fpus; opt->name != NULL; opt++)
23904     if (streq (opt->name, name))
23905       {
23906         mfpu_opt = &opt->value;
23907         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
23908         *input_line_pointer = saved_char;
23909         demand_empty_rest_of_line ();
23910         return;
23911       }
23912
23913   as_bad (_("unknown floating point format `%s'\n"), name);
23914   *input_line_pointer = saved_char;
23915   ignore_rest_of_line ();
23916 }
23917
23918 /* Copy symbol information.  */
23919
23920 void
23921 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
23922 {
23923   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
23924 }
23925
23926 #ifdef OBJ_ELF
23927 /* Given a symbolic attribute NAME, return the proper integer value.
23928    Returns -1 if the attribute is not known.  */
23929
23930 int
23931 arm_convert_symbolic_attribute (const char *name)
23932 {
23933   static const struct
23934   {
23935     const char * name;
23936     const int    tag;
23937   }
23938   attribute_table[] =
23939     {
23940       /* When you modify this table you should
23941          also modify the list in doc/c-arm.texi.  */
23942 #define T(tag) {#tag, tag}
23943       T (Tag_CPU_raw_name),
23944       T (Tag_CPU_name),
23945       T (Tag_CPU_arch),
23946       T (Tag_CPU_arch_profile),
23947       T (Tag_ARM_ISA_use),
23948       T (Tag_THUMB_ISA_use),
23949       T (Tag_FP_arch),
23950       T (Tag_VFP_arch),
23951       T (Tag_WMMX_arch),
23952       T (Tag_Advanced_SIMD_arch),
23953       T (Tag_PCS_config),
23954       T (Tag_ABI_PCS_R9_use),
23955       T (Tag_ABI_PCS_RW_data),
23956       T (Tag_ABI_PCS_RO_data),
23957       T (Tag_ABI_PCS_GOT_use),
23958       T (Tag_ABI_PCS_wchar_t),
23959       T (Tag_ABI_FP_rounding),
23960       T (Tag_ABI_FP_denormal),
23961       T (Tag_ABI_FP_exceptions),
23962       T (Tag_ABI_FP_user_exceptions),
23963       T (Tag_ABI_FP_number_model),
23964       T (Tag_ABI_align_needed),
23965       T (Tag_ABI_align8_needed),
23966       T (Tag_ABI_align_preserved),
23967       T (Tag_ABI_align8_preserved),
23968       T (Tag_ABI_enum_size),
23969       T (Tag_ABI_HardFP_use),
23970       T (Tag_ABI_VFP_args),
23971       T (Tag_ABI_WMMX_args),
23972       T (Tag_ABI_optimization_goals),
23973       T (Tag_ABI_FP_optimization_goals),
23974       T (Tag_compatibility),
23975       T (Tag_CPU_unaligned_access),
23976       T (Tag_FP_HP_extension),
23977       T (Tag_VFP_HP_extension),
23978       T (Tag_ABI_FP_16bit_format),
23979       T (Tag_MPextension_use),
23980       T (Tag_DIV_use),
23981       T (Tag_nodefaults),
23982       T (Tag_also_compatible_with),
23983       T (Tag_conformance),
23984       T (Tag_T2EE_use),
23985       T (Tag_Virtualization_use),
23986       /* We deliberately do not include Tag_MPextension_use_legacy.  */
23987 #undef T
23988     };
23989   unsigned int i;
23990
23991   if (name == NULL)
23992     return -1;
23993
23994   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
23995     if (streq (name, attribute_table[i].name))
23996       return attribute_table[i].tag;
23997
23998   return -1;
23999 }
24000
24001
24002 /* Apply sym value for relocations only in the case that
24003    they are for local symbols and you have the respective
24004    architectural feature for blx and simple switches.  */
24005 int
24006 arm_apply_sym_value (struct fix * fixP)
24007 {
24008   if (fixP->fx_addsy
24009       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
24010       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
24011     {
24012       switch (fixP->fx_r_type)
24013         {
24014         case BFD_RELOC_ARM_PCREL_BLX:
24015         case BFD_RELOC_THUMB_PCREL_BRANCH23:
24016           if (ARM_IS_FUNC (fixP->fx_addsy))
24017             return 1;
24018           break;
24019
24020         case BFD_RELOC_ARM_PCREL_CALL:
24021         case BFD_RELOC_THUMB_PCREL_BLX:
24022           if (THUMB_IS_FUNC (fixP->fx_addsy))
24023               return 1;
24024           break;
24025
24026         default:
24027           break;
24028         }
24029
24030     }
24031   return 0;
24032 }
24033 #endif /* OBJ_ELF */