b2541feb7ae1f98b5c64257dc1641cf0891c3c28
[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
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 2, 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 <string.h>
29 #include <limits.h>
30 #define  NO_RELOC 0
31 #include "as.h"
32 #include "safe-ctype.h"
33
34 /* Need TARGET_CPU.  */
35 #include "config.h"
36 #include "subsegs.h"
37 #include "obstack.h"
38 #include "symbols.h"
39 #include "listing.h"
40
41 #include "opcode/arm.h"
42
43 #ifdef OBJ_ELF
44 #include "elf/arm.h"
45 #include "dwarf2dbg.h"
46 #include "dw2gencfi.h"
47 #endif
48
49 /* XXX Set this to 1 after the next binutils release.  */
50 #define WARN_DEPRECATED 0
51
52 #ifdef OBJ_ELF
53 /* Must be at least the size of the largest unwind opcode (currently two).  */
54 #define ARM_OPCODE_CHUNK_SIZE 8
55
56 /* This structure holds the unwinding state.  */
57
58 static struct
59 {
60   symbolS *       proc_start;
61   symbolS *       table_entry;
62   symbolS *       personality_routine;
63   int             personality_index;
64   /* The segment containing the function.  */
65   segT            saved_seg;
66   subsegT         saved_subseg;
67   /* Opcodes generated from this function.  */
68   unsigned char * opcodes;
69   int             opcode_count;
70   int             opcode_alloc;
71   /* The number of bytes pushed to the stack.  */
72   offsetT         frame_size;
73   /* We don't add stack adjustment opcodes immediately so that we can merge
74      multiple adjustments.  We can also omit the final adjustment
75      when using a frame pointer.  */
76   offsetT         pending_offset;
77   /* These two fields are set by both unwind_movsp and unwind_setfp.  They
78      hold the reg+offset to use when restoring sp from a frame pointer.  */
79   offsetT         fp_offset;
80   int             fp_reg;
81   /* Nonzero if an unwind_setfp directive has been seen.  */
82   unsigned        fp_used:1;
83   /* Nonzero if the last opcode restores sp from fp_reg.  */
84   unsigned        sp_restored:1;
85 } unwind;
86
87 /* Bit N indicates that an R_ARM_NONE relocation has been output for
88    __aeabi_unwind_cpp_prN already if set. This enables dependencies to be
89    emitted only once per section, to save unnecessary bloat.  */
90 static unsigned int marked_pr_dependency = 0;
91
92 #endif /* OBJ_ELF */
93
94 enum arm_float_abi
95 {
96   ARM_FLOAT_ABI_HARD,
97   ARM_FLOAT_ABI_SOFTFP,
98   ARM_FLOAT_ABI_SOFT
99 };
100
101 /* Types of processor to assemble for.  */
102 #ifndef CPU_DEFAULT
103 #if defined __XSCALE__
104 #define CPU_DEFAULT     ARM_ARCH_XSCALE
105 #else
106 #if defined __thumb__
107 #define CPU_DEFAULT     ARM_ARCH_V5T
108 #endif
109 #endif
110 #endif
111
112 #ifndef FPU_DEFAULT
113 # ifdef TE_LINUX
114 #  define FPU_DEFAULT FPU_ARCH_FPA
115 # elif defined (TE_NetBSD)
116 #  ifdef OBJ_ELF
117 #   define FPU_DEFAULT FPU_ARCH_VFP     /* Soft-float, but VFP order.  */
118 #  else
119     /* Legacy a.out format.  */
120 #   define FPU_DEFAULT FPU_ARCH_FPA     /* Soft-float, but FPA order.  */
121 #  endif
122 # elif defined (TE_VXWORKS)
123 #  define FPU_DEFAULT FPU_ARCH_VFP      /* Soft-float, VFP order.  */
124 # else
125    /* For backwards compatibility, default to FPA.  */
126 #  define FPU_DEFAULT FPU_ARCH_FPA
127 # endif
128 #endif /* ifndef FPU_DEFAULT */
129
130 #define streq(a, b)           (strcmp (a, b) == 0)
131
132 static arm_feature_set cpu_variant;
133 static arm_feature_set arm_arch_used;
134 static arm_feature_set thumb_arch_used;
135
136 /* Flags stored in private area of BFD structure.  */
137 static int uses_apcs_26      = FALSE;
138 static int atpcs             = FALSE;
139 static int support_interwork = FALSE;
140 static int uses_apcs_float   = FALSE;
141 static int pic_code          = FALSE;
142
143 /* Variables that we set while parsing command-line options.  Once all
144    options have been read we re-process these values to set the real
145    assembly flags.  */
146 static const arm_feature_set *legacy_cpu = NULL;
147 static const arm_feature_set *legacy_fpu = NULL;
148
149 static const arm_feature_set *mcpu_cpu_opt = NULL;
150 static const arm_feature_set *mcpu_fpu_opt = NULL;
151 static const arm_feature_set *march_cpu_opt = NULL;
152 static const arm_feature_set *march_fpu_opt = NULL;
153 static const arm_feature_set *mfpu_opt = NULL;
154
155 /* Constants for known architecture features.  */
156 static const arm_feature_set fpu_default = FPU_DEFAULT;
157 static const arm_feature_set fpu_arch_vfp_v1 = FPU_ARCH_VFP_V1;
158 static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
159 static const arm_feature_set fpu_arch_vfp_v3 = FPU_ARCH_VFP_V3;
160 static const arm_feature_set fpu_arch_neon_v1 = FPU_ARCH_NEON_V1;
161 static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
162 static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
163 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
164 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
165
166 #ifdef CPU_DEFAULT
167 static const arm_feature_set cpu_default = CPU_DEFAULT;
168 #endif
169
170 static const arm_feature_set arm_ext_v1 = ARM_FEATURE (ARM_EXT_V1, 0);
171 static const arm_feature_set arm_ext_v2 = ARM_FEATURE (ARM_EXT_V1, 0);
172 static const arm_feature_set arm_ext_v2s = ARM_FEATURE (ARM_EXT_V2S, 0);
173 static const arm_feature_set arm_ext_v3 = ARM_FEATURE (ARM_EXT_V3, 0);
174 static const arm_feature_set arm_ext_v3m = ARM_FEATURE (ARM_EXT_V3M, 0);
175 static const arm_feature_set arm_ext_v4 = ARM_FEATURE (ARM_EXT_V4, 0);
176 static const arm_feature_set arm_ext_v4t = ARM_FEATURE (ARM_EXT_V4T, 0);
177 static const arm_feature_set arm_ext_v5 = ARM_FEATURE (ARM_EXT_V5, 0);
178 static const arm_feature_set arm_ext_v4t_5 =
179   ARM_FEATURE (ARM_EXT_V4T | ARM_EXT_V5, 0);
180 static const arm_feature_set arm_ext_v5t = ARM_FEATURE (ARM_EXT_V5T, 0);
181 static const arm_feature_set arm_ext_v5e = ARM_FEATURE (ARM_EXT_V5E, 0);
182 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE (ARM_EXT_V5ExP, 0);
183 static const arm_feature_set arm_ext_v5j = ARM_FEATURE (ARM_EXT_V5J, 0);
184 static const arm_feature_set arm_ext_v6 = ARM_FEATURE (ARM_EXT_V6, 0);
185 static const arm_feature_set arm_ext_v6k = ARM_FEATURE (ARM_EXT_V6K, 0);
186 static const arm_feature_set arm_ext_v6z = ARM_FEATURE (ARM_EXT_V6Z, 0);
187 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE (ARM_EXT_V6T2, 0);
188 static const arm_feature_set arm_ext_v6_notm = ARM_FEATURE (ARM_EXT_V6_NOTM, 0);
189 static const arm_feature_set arm_ext_div = ARM_FEATURE (ARM_EXT_DIV, 0);
190 static const arm_feature_set arm_ext_v7 = ARM_FEATURE (ARM_EXT_V7, 0);
191 static const arm_feature_set arm_ext_v7a = ARM_FEATURE (ARM_EXT_V7A, 0);
192 static const arm_feature_set arm_ext_v7r = ARM_FEATURE (ARM_EXT_V7R, 0);
193 static const arm_feature_set arm_ext_v7m = ARM_FEATURE (ARM_EXT_V7M, 0);
194
195 static const arm_feature_set arm_arch_any = ARM_ANY;
196 static const arm_feature_set arm_arch_full = ARM_FEATURE (-1, -1);
197 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
198 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
199
200 static const arm_feature_set arm_cext_iwmmxt =
201   ARM_FEATURE (0, ARM_CEXT_IWMMXT);
202 static const arm_feature_set arm_cext_xscale =
203   ARM_FEATURE (0, ARM_CEXT_XSCALE);
204 static const arm_feature_set arm_cext_maverick =
205   ARM_FEATURE (0, ARM_CEXT_MAVERICK);
206 static const arm_feature_set fpu_fpa_ext_v1 = ARM_FEATURE (0, FPU_FPA_EXT_V1);
207 static const arm_feature_set fpu_fpa_ext_v2 = ARM_FEATURE (0, FPU_FPA_EXT_V2);
208 static const arm_feature_set fpu_vfp_ext_v1xd =
209   ARM_FEATURE (0, FPU_VFP_EXT_V1xD);
210 static const arm_feature_set fpu_vfp_ext_v1 = ARM_FEATURE (0, FPU_VFP_EXT_V1);
211 static const arm_feature_set fpu_vfp_ext_v2 = ARM_FEATURE (0, FPU_VFP_EXT_V2);
212 static const arm_feature_set fpu_vfp_ext_v3 = ARM_FEATURE (0, FPU_VFP_EXT_V3);
213 static const arm_feature_set fpu_neon_ext_v1 = ARM_FEATURE (0, FPU_NEON_EXT_V1);
214 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
215   ARM_FEATURE (0, FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
216
217 static int mfloat_abi_opt = -1;
218 /* Record user cpu selection for object attributes.  */
219 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
220 /* Must be long enough to hold any of the names in arm_cpus.  */
221 static char selected_cpu_name[16];
222 #ifdef OBJ_ELF
223 # ifdef EABI_DEFAULT
224 static int meabi_flags = EABI_DEFAULT;
225 # else
226 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
227 # endif
228 #endif
229
230 #ifdef OBJ_ELF
231 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
232 symbolS * GOT_symbol;
233 #endif
234
235 /* 0: assemble for ARM,
236    1: assemble for Thumb,
237    2: assemble for Thumb even though target CPU does not support thumb
238       instructions.  */
239 static int thumb_mode = 0;
240
241 /* If unified_syntax is true, we are processing the new unified
242    ARM/Thumb syntax.  Important differences from the old ARM mode:
243
244      - Immediate operands do not require a # prefix.
245      - Conditional affixes always appear at the end of the
246        instruction.  (For backward compatibility, those instructions
247        that formerly had them in the middle, continue to accept them
248        there.)
249      - The IT instruction may appear, and if it does is validated
250        against subsequent conditional affixes.  It does not generate
251        machine code.
252
253    Important differences from the old Thumb mode:
254
255      - Immediate operands do not require a # prefix.
256      - Most of the V6T2 instructions are only available in unified mode.
257      - The .N and .W suffixes are recognized and honored (it is an error
258        if they cannot be honored).
259      - All instructions set the flags if and only if they have an 's' affix.
260      - Conditional affixes may be used.  They are validated against
261        preceding IT instructions.  Unlike ARM mode, you cannot use a
262        conditional affix except in the scope of an IT instruction.  */
263
264 static bfd_boolean unified_syntax = FALSE;
265
266 enum neon_el_type
267 {
268   NT_untyped,
269   NT_integer,
270   NT_float,
271   NT_poly,
272   NT_signed,
273   NT_unsigned,
274   NT_invtype
275 };
276
277 struct neon_type_el
278 {
279   enum neon_el_type type;
280   unsigned size;
281 };
282
283 #define NEON_MAX_TYPE_ELS 4
284
285 struct neon_type
286 {
287   struct neon_type_el el[NEON_MAX_TYPE_ELS];
288   unsigned elems;
289 };
290
291 struct arm_it
292 {
293   const char *  error;
294   unsigned long instruction;
295   int           size;
296   int           size_req;
297   int           cond;
298   struct neon_type vectype;
299   /* Set to the opcode if the instruction needs relaxation.
300      Zero if the instruction is not relaxed.  */
301   unsigned long relax;
302   struct
303   {
304     bfd_reloc_code_real_type type;
305     expressionS              exp;
306     int                      pc_rel;
307   } reloc;
308
309   struct
310   {
311     unsigned reg;
312     signed int imm;
313     unsigned present    : 1;  /* Operand present.  */
314     unsigned isreg      : 1;  /* Operand was a register.  */
315     unsigned immisreg   : 1;  /* .imm field is a second register.  */
316     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
317     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
318     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
319        instructions. This allows us to disambiguate ARM <-> vector insns.  */
320     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
321     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
322     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
323     unsigned writeback  : 1;  /* Operand has trailing !  */
324     unsigned preind     : 1;  /* Preindexed address.  */
325     unsigned postind    : 1;  /* Postindexed address.  */
326     unsigned negative   : 1;  /* Index register was negated.  */
327     unsigned shifted    : 1;  /* Shift applied to operation.  */
328     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
329   } operands[6];
330 };
331
332 static struct arm_it inst;
333
334 #define NUM_FLOAT_VALS 8
335
336 const char * fp_const[] =
337 {
338   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
339 };
340
341 /* Number of littlenums required to hold an extended precision number.  */
342 #define MAX_LITTLENUMS 6
343
344 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
345
346 #define FAIL    (-1)
347 #define SUCCESS (0)
348
349 #define SUFF_S 1
350 #define SUFF_D 2
351 #define SUFF_E 3
352 #define SUFF_P 4
353
354 #define CP_T_X   0x00008000
355 #define CP_T_Y   0x00400000
356
357 #define CONDS_BIT        0x00100000
358 #define LOAD_BIT         0x00100000
359
360 #define DOUBLE_LOAD_FLAG 0x00000001
361
362 struct asm_cond
363 {
364   const char *  template;
365   unsigned long value;
366 };
367
368 #define COND_ALWAYS 0xE
369
370 struct asm_psr
371 {
372   const char *template;
373   unsigned long field;
374 };
375
376 struct asm_barrier_opt
377 {
378   const char *template;
379   unsigned long value;
380 };
381
382 /* The bit that distinguishes CPSR and SPSR.  */
383 #define SPSR_BIT   (1 << 22)
384
385 /* The individual PSR flag bits.  */
386 #define PSR_c   (1 << 16)
387 #define PSR_x   (1 << 17)
388 #define PSR_s   (1 << 18)
389 #define PSR_f   (1 << 19)
390
391 struct reloc_entry
392 {
393   char *name;
394   bfd_reloc_code_real_type reloc;
395 };
396
397 enum vfp_reg_pos
398 {
399   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
400   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
401 };
402
403 enum vfp_ldstm_type
404 {
405   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
406 };
407
408 /* ARM register categories.  This includes coprocessor numbers and various
409    architecture extensions' registers.  */
410 enum arm_reg_type
411 {
412   REG_TYPE_RN,
413   REG_TYPE_CP,
414   REG_TYPE_CN,
415   REG_TYPE_FN,
416   REG_TYPE_VFS,
417   REG_TYPE_VFD,
418   REG_TYPE_NQ,
419   REG_TYPE_NDQ,
420   REG_TYPE_VFC,
421   REG_TYPE_MVF,
422   REG_TYPE_MVD,
423   REG_TYPE_MVFX,
424   REG_TYPE_MVDX,
425   REG_TYPE_MVAX,
426   REG_TYPE_DSPSC,
427   REG_TYPE_MMXWR,
428   REG_TYPE_MMXWC,
429   REG_TYPE_MMXWCG,
430   REG_TYPE_XSCALE,
431 };
432
433 /* Structure for a hash table entry for a register.  */
434 struct reg_entry
435 {
436   const char   *name;
437   unsigned char number;
438   unsigned char type;
439   unsigned char builtin;
440 };
441
442 /* Diagnostics used when we don't get a register of the expected type.  */
443 const char *const reg_expected_msgs[] =
444 {
445   N_("ARM register expected"),
446   N_("bad or missing co-processor number"),
447   N_("co-processor register expected"),
448   N_("FPA register expected"),
449   N_("VFP single precision register expected"),
450   N_("VFP/Neon double precision register expected"),
451   N_("Neon quad precision register expected"),
452   N_("Neon double or quad precision register expected"),
453   N_("VFP system register expected"),
454   N_("Maverick MVF register expected"),
455   N_("Maverick MVD register expected"),
456   N_("Maverick MVFX register expected"),
457   N_("Maverick MVDX register expected"),
458   N_("Maverick MVAX register expected"),
459   N_("Maverick DSPSC register expected"),
460   N_("iWMMXt data register expected"),
461   N_("iWMMXt control register expected"),
462   N_("iWMMXt scalar register expected"),
463   N_("XScale accumulator register expected"),
464 };
465
466 /* Some well known registers that we refer to directly elsewhere.  */
467 #define REG_SP  13
468 #define REG_LR  14
469 #define REG_PC  15
470
471 /* ARM instructions take 4bytes in the object file, Thumb instructions
472    take 2:  */
473 #define INSN_SIZE       4
474
475 struct asm_opcode
476 {
477   /* Basic string to match.  */
478   const char *template;
479
480   /* Parameters to instruction.  */
481   unsigned char operands[8];
482
483   /* Conditional tag - see opcode_lookup.  */
484   unsigned int tag : 4;
485
486   /* Basic instruction code.  */
487   unsigned int avalue : 28;
488
489   /* Thumb-format instruction code.  */
490   unsigned int tvalue;
491
492   /* Which architecture variant provides this instruction.  */
493   const arm_feature_set *avariant;
494   const arm_feature_set *tvariant;
495
496   /* Function to call to encode instruction in ARM format.  */
497   void (* aencode) (void);
498
499   /* Function to call to encode instruction in Thumb format.  */
500   void (* tencode) (void);
501 };
502
503 /* Defines for various bits that we will want to toggle.  */
504 #define INST_IMMEDIATE  0x02000000
505 #define OFFSET_REG      0x02000000
506 #define HWOFFSET_IMM    0x00400000
507 #define SHIFT_BY_REG    0x00000010
508 #define PRE_INDEX       0x01000000
509 #define INDEX_UP        0x00800000
510 #define WRITE_BACK      0x00200000
511 #define LDM_TYPE_2_OR_3 0x00400000
512
513 #define LITERAL_MASK    0xf000f000
514 #define OPCODE_MASK     0xfe1fffff
515 #define V4_STR_BIT      0x00000020
516
517 #define DATA_OP_SHIFT   21
518
519 #define T2_OPCODE_MASK  0xfe1fffff
520 #define T2_DATA_OP_SHIFT 21
521
522 /* Codes to distinguish the arithmetic instructions.  */
523 #define OPCODE_AND      0
524 #define OPCODE_EOR      1
525 #define OPCODE_SUB      2
526 #define OPCODE_RSB      3
527 #define OPCODE_ADD      4
528 #define OPCODE_ADC      5
529 #define OPCODE_SBC      6
530 #define OPCODE_RSC      7
531 #define OPCODE_TST      8
532 #define OPCODE_TEQ      9
533 #define OPCODE_CMP      10
534 #define OPCODE_CMN      11
535 #define OPCODE_ORR      12
536 #define OPCODE_MOV      13
537 #define OPCODE_BIC      14
538 #define OPCODE_MVN      15
539
540 #define T2_OPCODE_AND   0
541 #define T2_OPCODE_BIC   1
542 #define T2_OPCODE_ORR   2
543 #define T2_OPCODE_ORN   3
544 #define T2_OPCODE_EOR   4
545 #define T2_OPCODE_ADD   8
546 #define T2_OPCODE_ADC   10
547 #define T2_OPCODE_SBC   11
548 #define T2_OPCODE_SUB   13
549 #define T2_OPCODE_RSB   14
550
551 #define T_OPCODE_MUL 0x4340
552 #define T_OPCODE_TST 0x4200
553 #define T_OPCODE_CMN 0x42c0
554 #define T_OPCODE_NEG 0x4240
555 #define T_OPCODE_MVN 0x43c0
556
557 #define T_OPCODE_ADD_R3 0x1800
558 #define T_OPCODE_SUB_R3 0x1a00
559 #define T_OPCODE_ADD_HI 0x4400
560 #define T_OPCODE_ADD_ST 0xb000
561 #define T_OPCODE_SUB_ST 0xb080
562 #define T_OPCODE_ADD_SP 0xa800
563 #define T_OPCODE_ADD_PC 0xa000
564 #define T_OPCODE_ADD_I8 0x3000
565 #define T_OPCODE_SUB_I8 0x3800
566 #define T_OPCODE_ADD_I3 0x1c00
567 #define T_OPCODE_SUB_I3 0x1e00
568
569 #define T_OPCODE_ASR_R  0x4100
570 #define T_OPCODE_LSL_R  0x4080
571 #define T_OPCODE_LSR_R  0x40c0
572 #define T_OPCODE_ROR_R  0x41c0
573 #define T_OPCODE_ASR_I  0x1000
574 #define T_OPCODE_LSL_I  0x0000
575 #define T_OPCODE_LSR_I  0x0800
576
577 #define T_OPCODE_MOV_I8 0x2000
578 #define T_OPCODE_CMP_I8 0x2800
579 #define T_OPCODE_CMP_LR 0x4280
580 #define T_OPCODE_MOV_HR 0x4600
581 #define T_OPCODE_CMP_HR 0x4500
582
583 #define T_OPCODE_LDR_PC 0x4800
584 #define T_OPCODE_LDR_SP 0x9800
585 #define T_OPCODE_STR_SP 0x9000
586 #define T_OPCODE_LDR_IW 0x6800
587 #define T_OPCODE_STR_IW 0x6000
588 #define T_OPCODE_LDR_IH 0x8800
589 #define T_OPCODE_STR_IH 0x8000
590 #define T_OPCODE_LDR_IB 0x7800
591 #define T_OPCODE_STR_IB 0x7000
592 #define T_OPCODE_LDR_RW 0x5800
593 #define T_OPCODE_STR_RW 0x5000
594 #define T_OPCODE_LDR_RH 0x5a00
595 #define T_OPCODE_STR_RH 0x5200
596 #define T_OPCODE_LDR_RB 0x5c00
597 #define T_OPCODE_STR_RB 0x5400
598
599 #define T_OPCODE_PUSH   0xb400
600 #define T_OPCODE_POP    0xbc00
601
602 #define T_OPCODE_BRANCH 0xe000
603
604 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
605 #define THUMB_PP_PC_LR 0x0100
606 #define THUMB_LOAD_BIT 0x0800
607 #define THUMB2_LOAD_BIT 0x00100000
608
609 #define BAD_ARGS        _("bad arguments to instruction")
610 #define BAD_PC          _("r15 not allowed here")
611 #define BAD_COND        _("instruction cannot be conditional")
612 #define BAD_OVERLAP     _("registers may not be the same")
613 #define BAD_HIREG       _("lo register required")
614 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
615 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
616 #define BAD_BRANCH      _("branch must be last instruction in IT block")
617 #define BAD_NOT_IT      _("instruction not allowed in IT block")
618
619 static struct hash_control *arm_ops_hsh;
620 static struct hash_control *arm_cond_hsh;
621 static struct hash_control *arm_shift_hsh;
622 static struct hash_control *arm_psr_hsh;
623 static struct hash_control *arm_v7m_psr_hsh;
624 static struct hash_control *arm_reg_hsh;
625 static struct hash_control *arm_reloc_hsh;
626 static struct hash_control *arm_barrier_opt_hsh;
627
628 /* Stuff needed to resolve the label ambiguity
629    As:
630      ...
631      label:   <insn>
632    may differ from:
633      ...
634      label:
635               <insn>
636 */
637
638 symbolS *  last_label_seen;
639 static int label_is_thumb_function_name = FALSE;
640 \f
641 /* Literal pool structure.  Held on a per-section
642    and per-sub-section basis.  */
643
644 #define MAX_LITERAL_POOL_SIZE 1024
645 typedef struct literal_pool
646 {
647   expressionS    literals [MAX_LITERAL_POOL_SIZE];
648   unsigned int   next_free_entry;
649   unsigned int   id;
650   symbolS *      symbol;
651   segT           section;
652   subsegT        sub_section;
653   struct literal_pool * next;
654 } literal_pool;
655
656 /* Pointer to a linked list of literal pools.  */
657 literal_pool * list_of_pools = NULL;
658
659 /* State variables for IT block handling.  */
660 static bfd_boolean current_it_mask = 0;
661 static int current_cc;
662
663 \f
664 /* Pure syntax.  */
665
666 /* This array holds the chars that always start a comment.  If the
667    pre-processor is disabled, these aren't very useful.  */
668 const char comment_chars[] = "@";
669
670 /* This array holds the chars that only start a comment at the beginning of
671    a line.  If the line seems to have the form '# 123 filename'
672    .line and .file directives will appear in the pre-processed output.  */
673 /* Note that input_file.c hand checks for '#' at the beginning of the
674    first line of the input file.  This is because the compiler outputs
675    #NO_APP at the beginning of its output.  */
676 /* Also note that comments like this one will always work.  */
677 const char line_comment_chars[] = "#";
678
679 const char line_separator_chars[] = ";";
680
681 /* Chars that can be used to separate mant
682    from exp in floating point numbers.  */
683 const char EXP_CHARS[] = "eE";
684
685 /* Chars that mean this number is a floating point constant.  */
686 /* As in 0f12.456  */
687 /* or    0d1.2345e12  */
688
689 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
690
691 /* Prefix characters that indicate the start of an immediate
692    value.  */
693 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
694
695 /* Separator character handling.  */
696
697 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
698
699 static inline int
700 skip_past_char (char ** str, char c)
701 {
702   if (**str == c)
703     {
704       (*str)++;
705       return SUCCESS;
706     }
707   else
708     return FAIL;
709 }
710 #define skip_past_comma(str) skip_past_char (str, ',')
711
712 /* Arithmetic expressions (possibly involving symbols).  */
713
714 /* Return TRUE if anything in the expression is a bignum.  */
715
716 static int
717 walk_no_bignums (symbolS * sp)
718 {
719   if (symbol_get_value_expression (sp)->X_op == O_big)
720     return 1;
721
722   if (symbol_get_value_expression (sp)->X_add_symbol)
723     {
724       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
725               || (symbol_get_value_expression (sp)->X_op_symbol
726                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
727     }
728
729   return 0;
730 }
731
732 static int in_my_get_expression = 0;
733
734 /* Third argument to my_get_expression.  */
735 #define GE_NO_PREFIX 0
736 #define GE_IMM_PREFIX 1
737 #define GE_OPT_PREFIX 2
738 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
739    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
740 #define GE_OPT_PREFIX_BIG 3
741
742 static int
743 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
744 {
745   char * save_in;
746   segT   seg;
747
748   /* In unified syntax, all prefixes are optional.  */
749   if (unified_syntax)
750     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
751                   : GE_OPT_PREFIX;
752
753   switch (prefix_mode)
754     {
755     case GE_NO_PREFIX: break;
756     case GE_IMM_PREFIX:
757       if (!is_immediate_prefix (**str))
758         {
759           inst.error = _("immediate expression requires a # prefix");
760           return FAIL;
761         }
762       (*str)++;
763       break;
764     case GE_OPT_PREFIX:
765     case GE_OPT_PREFIX_BIG:
766       if (is_immediate_prefix (**str))
767         (*str)++;
768       break;
769     default: abort ();
770     }
771
772   memset (ep, 0, sizeof (expressionS));
773
774   save_in = input_line_pointer;
775   input_line_pointer = *str;
776   in_my_get_expression = 1;
777   seg = expression (ep);
778   in_my_get_expression = 0;
779
780   if (ep->X_op == O_illegal)
781     {
782       /* We found a bad expression in md_operand().  */
783       *str = input_line_pointer;
784       input_line_pointer = save_in;
785       if (inst.error == NULL)
786         inst.error = _("bad expression");
787       return 1;
788     }
789
790 #ifdef OBJ_AOUT
791   if (seg != absolute_section
792       && seg != text_section
793       && seg != data_section
794       && seg != bss_section
795       && seg != undefined_section)
796     {
797       inst.error = _("bad segment");
798       *str = input_line_pointer;
799       input_line_pointer = save_in;
800       return 1;
801     }
802 #endif
803
804   /* Get rid of any bignums now, so that we don't generate an error for which
805      we can't establish a line number later on.  Big numbers are never valid
806      in instructions, which is where this routine is always called.  */
807   if (prefix_mode != GE_OPT_PREFIX_BIG
808       && (ep->X_op == O_big
809           || (ep->X_add_symbol
810               && (walk_no_bignums (ep->X_add_symbol)
811                   || (ep->X_op_symbol
812                       && walk_no_bignums (ep->X_op_symbol))))))
813     {
814       inst.error = _("invalid constant");
815       *str = input_line_pointer;
816       input_line_pointer = save_in;
817       return 1;
818     }
819
820   *str = input_line_pointer;
821   input_line_pointer = save_in;
822   return 0;
823 }
824
825 /* Turn a string in input_line_pointer into a floating point constant
826    of type TYPE, and store the appropriate bytes in *LITP.  The number
827    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
828    returned, or NULL on OK.
829
830    Note that fp constants aren't represent in the normal way on the ARM.
831    In big endian mode, things are as expected.  However, in little endian
832    mode fp constants are big-endian word-wise, and little-endian byte-wise
833    within the words.  For example, (double) 1.1 in big endian mode is
834    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
835    the byte sequence 99 99 f1 3f 9a 99 99 99.
836
837    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
838
839 char *
840 md_atof (int type, char * litP, int * sizeP)
841 {
842   int prec;
843   LITTLENUM_TYPE words[MAX_LITTLENUMS];
844   char *t;
845   int i;
846
847   switch (type)
848     {
849     case 'f':
850     case 'F':
851     case 's':
852     case 'S':
853       prec = 2;
854       break;
855
856     case 'd':
857     case 'D':
858     case 'r':
859     case 'R':
860       prec = 4;
861       break;
862
863     case 'x':
864     case 'X':
865       prec = 6;
866       break;
867
868     case 'p':
869     case 'P':
870       prec = 6;
871       break;
872
873     default:
874       *sizeP = 0;
875       return _("bad call to MD_ATOF()");
876     }
877
878   t = atof_ieee (input_line_pointer, type, words);
879   if (t)
880     input_line_pointer = t;
881   *sizeP = prec * 2;
882
883   if (target_big_endian)
884     {
885       for (i = 0; i < prec; i++)
886         {
887           md_number_to_chars (litP, (valueT) words[i], 2);
888           litP += 2;
889         }
890     }
891   else
892     {
893       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
894         for (i = prec - 1; i >= 0; i--)
895           {
896             md_number_to_chars (litP, (valueT) words[i], 2);
897             litP += 2;
898           }
899       else
900         /* For a 4 byte float the order of elements in `words' is 1 0.
901            For an 8 byte float the order is 1 0 3 2.  */
902         for (i = 0; i < prec; i += 2)
903           {
904             md_number_to_chars (litP, (valueT) words[i + 1], 2);
905             md_number_to_chars (litP + 2, (valueT) words[i], 2);
906             litP += 4;
907           }
908     }
909
910   return 0;
911 }
912
913 /* We handle all bad expressions here, so that we can report the faulty
914    instruction in the error message.  */
915 void
916 md_operand (expressionS * expr)
917 {
918   if (in_my_get_expression)
919     expr->X_op = O_illegal;
920 }
921
922 /* Immediate values.  */
923
924 /* Generic immediate-value read function for use in directives.
925    Accepts anything that 'expression' can fold to a constant.
926    *val receives the number.  */
927 #ifdef OBJ_ELF
928 static int
929 immediate_for_directive (int *val)
930 {
931   expressionS exp;
932   exp.X_op = O_illegal;
933
934   if (is_immediate_prefix (*input_line_pointer))
935     {
936       input_line_pointer++;
937       expression (&exp);
938     }
939
940   if (exp.X_op != O_constant)
941     {
942       as_bad (_("expected #constant"));
943       ignore_rest_of_line ();
944       return FAIL;
945     }
946   *val = exp.X_add_number;
947   return SUCCESS;
948 }
949 #endif
950
951 /* Register parsing.  */
952
953 /* Generic register parser.  CCP points to what should be the
954    beginning of a register name.  If it is indeed a valid register
955    name, advance CCP over it and return the reg_entry structure;
956    otherwise return NULL.  Does not issue diagnostics.  */
957
958 static struct reg_entry *
959 arm_reg_parse_multi (char **ccp)
960 {
961   char *start = *ccp;
962   char *p;
963   struct reg_entry *reg;
964
965 #ifdef REGISTER_PREFIX
966   if (*start != REGISTER_PREFIX)
967     return NULL;
968   start++;
969 #endif
970 #ifdef OPTIONAL_REGISTER_PREFIX
971   if (*start == OPTIONAL_REGISTER_PREFIX)
972     start++;
973 #endif
974
975   p = start;
976   if (!ISALPHA (*p) || !is_name_beginner (*p))
977     return NULL;
978
979   do
980     p++;
981   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
982
983   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
984
985   if (!reg)
986     return NULL;
987
988   *ccp = p;
989   return reg;
990 }
991
992 /* As above, but the register must be of type TYPE, and the return
993    value is the register number or FAIL.
994    If RTYPE is non-zero, return the (possibly restricted) type of the
995    register (e.g. Neon double or quad reg when either has been requested).  */
996
997 static int
998 arm_reg_parse (char **ccp, enum arm_reg_type type, enum arm_reg_type *rtype)
999 {
1000   char *start = *ccp;
1001   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1002
1003   /* Undo polymorphism for Neon D and Q registers.  */
1004   if (reg && type == REG_TYPE_NDQ
1005       && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1006     type = reg->type;
1007
1008   if (rtype)
1009     *rtype = type;
1010
1011   if (reg && reg->type == type)
1012     return reg->number;
1013
1014   /* Alternative syntaxes are accepted for a few register classes.  */
1015   switch (type)
1016     {
1017     case REG_TYPE_MVF:
1018     case REG_TYPE_MVD:
1019     case REG_TYPE_MVFX:
1020     case REG_TYPE_MVDX:
1021       /* Generic coprocessor register names are allowed for these.  */
1022       if (reg && reg->type == REG_TYPE_CN)
1023         return reg->number;
1024       break;
1025
1026     case REG_TYPE_CP:
1027       /* For backward compatibility, a bare number is valid here.  */
1028       {
1029         unsigned long processor = strtoul (start, ccp, 10);
1030         if (*ccp != start && processor <= 15)
1031           return processor;
1032       }
1033
1034     case REG_TYPE_MMXWC:
1035       /* WC includes WCG.  ??? I'm not sure this is true for all
1036          instructions that take WC registers.  */
1037       if (reg && reg->type == REG_TYPE_MMXWCG)
1038         return reg->number;
1039       break;
1040
1041     default:
1042       break;
1043     }
1044
1045   *ccp = start;
1046   return FAIL;
1047 }
1048
1049 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1050    have enough information to be able to do a good job bounds-checking. So, we
1051    just do easy checks here, and do further checks later.  */
1052
1053 static int
1054 parse_scalar (char **ccp, int elsize)
1055 {
1056   int regno, elno;
1057   char *str = *ccp;
1058   expressionS exp;
1059   
1060   if ((regno = arm_reg_parse (&str, REG_TYPE_VFD, NULL)) == FAIL)
1061     return FAIL;
1062   
1063   if (skip_past_char (&str, '[') == FAIL)
1064     return FAIL;
1065   
1066   my_get_expression (&exp, &str, GE_NO_PREFIX);
1067   if (exp.X_op != O_constant)
1068     {
1069       inst.error = _("constant expression required");
1070       return FAIL;
1071     }
1072   elno = exp.X_add_number;
1073   
1074   if (elno >= 64 / elsize)
1075     {
1076       inst.error = _("scalar index out of range");
1077       return FAIL;
1078     }
1079   
1080   if (skip_past_char (&str, ']') == FAIL)
1081     return FAIL;
1082   
1083   /* Parsed scalar successfully. Skip over it.  */
1084   *ccp = str;
1085   
1086   return (regno * 8) + elno;
1087 }
1088
1089 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1090 static long
1091 parse_reg_list (char ** strp)
1092 {
1093   char * str = * strp;
1094   long   range = 0;
1095   int    another_range;
1096
1097   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1098   do
1099     {
1100       another_range = 0;
1101
1102       if (*str == '{')
1103         {
1104           int in_range = 0;
1105           int cur_reg = -1;
1106
1107           str++;
1108           do
1109             {
1110               int reg;
1111
1112               if ((reg = arm_reg_parse (&str, REG_TYPE_RN, NULL)) == FAIL)
1113                 {
1114                   inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
1115                   return FAIL;
1116                 }
1117
1118               if (in_range)
1119                 {
1120                   int i;
1121
1122                   if (reg <= cur_reg)
1123                     {
1124                       inst.error = _("bad range in register list");
1125                       return FAIL;
1126                     }
1127
1128                   for (i = cur_reg + 1; i < reg; i++)
1129                     {
1130                       if (range & (1 << i))
1131                         as_tsktsk
1132                           (_("Warning: duplicated register (r%d) in register list"),
1133                            i);
1134                       else
1135                         range |= 1 << i;
1136                     }
1137                   in_range = 0;
1138                 }
1139
1140               if (range & (1 << reg))
1141                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1142                            reg);
1143               else if (reg <= cur_reg)
1144                 as_tsktsk (_("Warning: register range not in ascending order"));
1145
1146               range |= 1 << reg;
1147               cur_reg = reg;
1148             }
1149           while (skip_past_comma (&str) != FAIL
1150                  || (in_range = 1, *str++ == '-'));
1151           str--;
1152
1153           if (*str++ != '}')
1154             {
1155               inst.error = _("missing `}'");
1156               return FAIL;
1157             }
1158         }
1159       else
1160         {
1161           expressionS expr;
1162
1163           if (my_get_expression (&expr, &str, GE_NO_PREFIX))
1164             return FAIL;
1165
1166           if (expr.X_op == O_constant)
1167             {
1168               if (expr.X_add_number
1169                   != (expr.X_add_number & 0x0000ffff))
1170                 {
1171                   inst.error = _("invalid register mask");
1172                   return FAIL;
1173                 }
1174
1175               if ((range & expr.X_add_number) != 0)
1176                 {
1177                   int regno = range & expr.X_add_number;
1178
1179                   regno &= -regno;
1180                   regno = (1 << regno) - 1;
1181                   as_tsktsk
1182                     (_("Warning: duplicated register (r%d) in register list"),
1183                      regno);
1184                 }
1185
1186               range |= expr.X_add_number;
1187             }
1188           else
1189             {
1190               if (inst.reloc.type != 0)
1191                 {
1192                   inst.error = _("expression too complex");
1193                   return FAIL;
1194                 }
1195
1196               memcpy (&inst.reloc.exp, &expr, sizeof (expressionS));
1197               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1198               inst.reloc.pc_rel = 0;
1199             }
1200         }
1201
1202       if (*str == '|' || *str == '+')
1203         {
1204           str++;
1205           another_range = 1;
1206         }
1207     }
1208   while (another_range);
1209
1210   *strp = str;
1211   return range;
1212 }
1213
1214 /* Types of registers in a list.  */
1215
1216 enum reg_list_els
1217 {
1218   REGLIST_VFP_S,
1219   REGLIST_VFP_D,
1220   REGLIST_NEON_D
1221 };
1222
1223 /* Parse a VFP register list.  If the string is invalid return FAIL.
1224    Otherwise return the number of registers, and set PBASE to the first
1225    register.  Parses registers of type ETYPE.
1226    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1227      - Q registers can be used to specify pairs of D registers
1228      - { } can be omitted from around a singleton register list
1229          FIXME: This is not implemented, as it would require backtracking in
1230          some cases, e.g.:
1231            vtbl.8 d3,d4,d5
1232          This could be done (the meaning isn't really ambiguous), but doesn't
1233          fit in well with the current parsing framework.
1234      - 32 D registers may be used (also true for VFPv3).  */
1235
1236 static int
1237 parse_vfp_reg_list (char **str, unsigned int *pbase, enum reg_list_els etype)
1238 {
1239   int base_reg;
1240   int new_base;
1241   enum arm_reg_type regtype = 0;
1242   int max_regs = 0;
1243   int count = 0;
1244   int warned = 0;
1245   unsigned long mask = 0;
1246   int i;
1247
1248   if (**str != '{')
1249     {
1250       inst.error = _("expecting {");
1251       return FAIL;
1252     }
1253
1254   (*str)++;
1255
1256   switch (etype)
1257     {
1258     case REGLIST_VFP_S:
1259       regtype = REG_TYPE_VFS;
1260       max_regs = 32;
1261       break;
1262     
1263     case REGLIST_VFP_D:
1264       regtype = REG_TYPE_VFD;
1265       /* VFPv3 allows 32 D registers.  */
1266       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
1267         {
1268           max_regs = 32;
1269           if (thumb_mode)
1270             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1271                                     fpu_vfp_ext_v3);
1272           else
1273             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1274                                     fpu_vfp_ext_v3);
1275         }
1276       else
1277         max_regs = 16;
1278       break;
1279     
1280     case REGLIST_NEON_D:
1281       regtype = REG_TYPE_NDQ;
1282       max_regs = 32;
1283       break;
1284     }
1285
1286   base_reg = max_regs;
1287
1288   do
1289     {
1290       int setmask = 1, addregs = 1;
1291       new_base = arm_reg_parse (str, regtype, &regtype);
1292       if (new_base == FAIL)
1293         {
1294           inst.error = gettext (reg_expected_msgs[regtype]);
1295           return FAIL;
1296         }
1297
1298       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1299       if (regtype == REG_TYPE_NQ)
1300         {
1301           setmask = 3;
1302           addregs = 2;
1303         }
1304
1305       if (new_base < base_reg)
1306         base_reg = new_base;
1307
1308       if (mask & (setmask << new_base))
1309         {
1310           inst.error = _("invalid register list");
1311           return FAIL;
1312         }
1313
1314       if ((mask >> new_base) != 0 && ! warned)
1315         {
1316           as_tsktsk (_("register list not in ascending order"));
1317           warned = 1;
1318         }
1319
1320       mask |= setmask << new_base;
1321       count += addregs;
1322
1323       if (**str == '-') /* We have the start of a range expression */
1324         {
1325           int high_range;
1326
1327           (*str)++;
1328
1329           if ((high_range = arm_reg_parse (str, regtype, NULL)) == FAIL)
1330             {
1331               inst.error = gettext (reg_expected_msgs[regtype]);
1332               return FAIL;
1333             }
1334
1335           if (regtype == REG_TYPE_NQ)
1336             high_range = high_range + 1;
1337
1338           if (high_range <= new_base)
1339             {
1340               inst.error = _("register range not in ascending order");
1341               return FAIL;
1342             }
1343
1344           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1345             {
1346               if (mask & (setmask << new_base))
1347                 {
1348                   inst.error = _("invalid register list");
1349                   return FAIL;
1350                 }
1351
1352               mask |= setmask << new_base;
1353               count += addregs;
1354             }
1355         }
1356     }
1357   while (skip_past_comma (str) != FAIL);
1358
1359   (*str)++;
1360
1361   /* Sanity check -- should have raised a parse error above.  */
1362   if (count == 0 || count > max_regs)
1363     abort ();
1364
1365   *pbase = base_reg;
1366
1367   /* Final test -- the registers must be consecutive.  */
1368   mask >>= base_reg;
1369   for (i = 0; i < count; i++)
1370     {
1371       if ((mask & (1u << i)) == 0)
1372         {
1373           inst.error = _("non-contiguous register range");
1374           return FAIL;
1375         }
1376     }
1377
1378   return count;
1379 }
1380
1381 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
1382    The base register is put in *PBASE.
1383    The lane (or one of the #defined constants below) is placed in bits [3:0] of
1384    the return value.
1385    The register stride (minus one) is put in bit 4 of the return value.
1386    Bits [6:5] encode the list length (minus one).  */
1387
1388 #define NEON_ALL_LANES          15
1389 #define NEON_INTERLEAVE_LANES   14
1390 #define NEON_LANE(X)            ((X) & 0xf)
1391 #define NEON_REG_STRIDE(X)      (((X) & (1 << 4)) ? 2 : 1)
1392 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
1393
1394 static int
1395 parse_neon_el_struct_list (char **str, unsigned *pbase)
1396 {
1397   char *ptr = *str;
1398   int base_reg = -1;
1399   int reg_incr = -1;
1400   int count = 0;
1401   int lane = -1;
1402   int leading_brace = 0;
1403   enum arm_reg_type rtype = REG_TYPE_NDQ;
1404   int addregs = 1;
1405   const char *const incr_error = "register stride must be 1 or 2";
1406   const char *const type_error = "mismatched element/structure types in list";
1407   
1408   if (skip_past_char (&ptr, '{') == SUCCESS)
1409     leading_brace = 1;
1410   
1411   do
1412     {
1413       int getreg = arm_reg_parse (&ptr, rtype, &rtype);
1414       if (getreg == FAIL)
1415         {
1416           inst.error = _(reg_expected_msgs[rtype]);
1417           return FAIL;
1418         }
1419       
1420       if (base_reg == -1)
1421         {
1422           base_reg = getreg;
1423           if (rtype == REG_TYPE_NQ)
1424             {
1425               reg_incr = 1;
1426               addregs = 2;
1427             }
1428         }
1429       else if (reg_incr == -1)
1430         {
1431           reg_incr = getreg - base_reg;
1432           if (reg_incr < 1 || reg_incr > 2)
1433             {
1434               inst.error = _(incr_error);
1435               return FAIL;
1436             }
1437         }
1438       else if (getreg != base_reg + reg_incr * count)
1439         {
1440           inst.error = _(incr_error);
1441           return FAIL;
1442         }
1443       
1444       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
1445          modes.  */
1446       if (ptr[0] == '-')
1447         {
1448           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
1449           if (lane == -1)
1450             lane = NEON_INTERLEAVE_LANES;
1451           else if (lane != NEON_INTERLEAVE_LANES)
1452             {
1453               inst.error = _(type_error);
1454               return FAIL;
1455             }
1456           if (reg_incr == -1)
1457             reg_incr = 1;
1458           else if (reg_incr != 1)
1459             {
1460               inst.error = _("don't use Rn-Rm syntax with non-unit stride");
1461               return FAIL;
1462             }
1463           ptr++;
1464           hireg = arm_reg_parse (&ptr, rtype, NULL);
1465           if (hireg == FAIL)
1466             {
1467               inst.error = _(reg_expected_msgs[rtype]);
1468               return FAIL;
1469             }
1470           count += hireg + dregs - getreg;
1471           continue;
1472         }
1473       
1474       /* If we're using Q registers, we can't use [] or [n] syntax.  */
1475       if (rtype == REG_TYPE_NQ)
1476         {
1477           count += 2;
1478           continue;
1479         }
1480       
1481       if (skip_past_char (&ptr, '[') == SUCCESS)
1482         {
1483           if (skip_past_char (&ptr, ']') == SUCCESS)
1484             {
1485               if (lane == -1)
1486                 lane = NEON_ALL_LANES;
1487               else if (lane != NEON_ALL_LANES)
1488                 {
1489                   inst.error = _(type_error);
1490                   return FAIL;
1491                 }
1492             }
1493           else
1494             {
1495               expressionS exp;
1496               my_get_expression (&exp, &ptr, GE_NO_PREFIX);
1497               if (exp.X_op != O_constant)
1498                 {
1499                   inst.error = _("constant expression required");
1500                   return FAIL;
1501                 }
1502               if (lane == -1)
1503                 lane = exp.X_add_number;
1504               else if (lane != exp.X_add_number)
1505                 {
1506                   inst.error = _(type_error);
1507                   return FAIL;
1508                 }
1509               
1510               if (skip_past_char (&ptr, ']') == FAIL)
1511                 {
1512                   inst.error = _("expected ]");
1513                   return FAIL;
1514                 }
1515             }
1516         }
1517       else if (lane == -1)
1518         lane = NEON_INTERLEAVE_LANES;
1519       else if (lane != NEON_INTERLEAVE_LANES)
1520         {
1521           inst.error = _(type_error);
1522           return FAIL;
1523         }
1524       count++;
1525     }
1526   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
1527   
1528   /* No lane set by [x]. We must be interleaving structures.  */
1529   if (lane == -1)
1530     lane = NEON_INTERLEAVE_LANES;
1531   
1532   /* Sanity check.  */
1533   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
1534       || (count > 1 && reg_incr == -1))
1535     {
1536       inst.error = _("error parsing element/structure list");
1537       return FAIL;
1538     }
1539
1540   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
1541     {
1542       inst.error = _("expected }");
1543       return FAIL;
1544     }
1545   
1546   if (reg_incr == -1)
1547     reg_incr = 1;
1548
1549   *pbase = base_reg;
1550   *str = ptr;
1551   
1552   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
1553 }
1554
1555 /* Parse an explicit relocation suffix on an expression.  This is
1556    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
1557    arm_reloc_hsh contains no entries, so this function can only
1558    succeed if there is no () after the word.  Returns -1 on error,
1559    BFD_RELOC_UNUSED if there wasn't any suffix.  */
1560 static int
1561 parse_reloc (char **str)
1562 {
1563   struct reloc_entry *r;
1564   char *p, *q;
1565
1566   if (**str != '(')
1567     return BFD_RELOC_UNUSED;
1568
1569   p = *str + 1;
1570   q = p;
1571
1572   while (*q && *q != ')' && *q != ',')
1573     q++;
1574   if (*q != ')')
1575     return -1;
1576
1577   if ((r = hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
1578     return -1;
1579
1580   *str = q + 1;
1581   return r->reloc;
1582 }
1583
1584 /* Directives: register aliases.  */
1585
1586 static void
1587 insert_reg_alias (char *str, int number, int type)
1588 {
1589   struct reg_entry *new;
1590   const char *name;
1591
1592   if ((new = hash_find (arm_reg_hsh, str)) != 0)
1593     {
1594       if (new->builtin)
1595         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
1596
1597       /* Only warn about a redefinition if it's not defined as the
1598          same register.  */
1599       else if (new->number != number || new->type != type)
1600         as_warn (_("ignoring redefinition of register alias '%s'"), str);
1601
1602       return;
1603     }
1604
1605   name = xstrdup (str);
1606   new = xmalloc (sizeof (struct reg_entry));
1607
1608   new->name = name;
1609   new->number = number;
1610   new->type = type;
1611   new->builtin = FALSE;
1612
1613   if (hash_insert (arm_reg_hsh, name, (PTR) new))
1614     abort ();
1615 }
1616
1617 /* Look for the .req directive.  This is of the form:
1618
1619         new_register_name .req existing_register_name
1620
1621    If we find one, or if it looks sufficiently like one that we want to
1622    handle any error here, return non-zero.  Otherwise return zero.  */
1623
1624 static int
1625 create_register_alias (char * newname, char *p)
1626 {
1627   struct reg_entry *old;
1628   char *oldname, *nbuf;
1629   size_t nlen;
1630
1631   /* The input scrubber ensures that whitespace after the mnemonic is
1632      collapsed to single spaces.  */
1633   oldname = p;
1634   if (strncmp (oldname, " .req ", 6) != 0)
1635     return 0;
1636
1637   oldname += 6;
1638   if (*oldname == '\0')
1639     return 0;
1640
1641   old = hash_find (arm_reg_hsh, oldname);
1642   if (!old)
1643     {
1644       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
1645       return 1;
1646     }
1647
1648   /* If TC_CASE_SENSITIVE is defined, then newname already points to
1649      the desired alias name, and p points to its end.  If not, then
1650      the desired alias name is in the global original_case_string.  */
1651 #ifdef TC_CASE_SENSITIVE
1652   nlen = p - newname;
1653 #else
1654   newname = original_case_string;
1655   nlen = strlen (newname);
1656 #endif
1657
1658   nbuf = alloca (nlen + 1);
1659   memcpy (nbuf, newname, nlen);
1660   nbuf[nlen] = '\0';
1661
1662   /* Create aliases under the new name as stated; an all-lowercase
1663      version of the new name; and an all-uppercase version of the new
1664      name.  */
1665   insert_reg_alias (nbuf, old->number, old->type);
1666
1667   for (p = nbuf; *p; p++)
1668     *p = TOUPPER (*p);
1669
1670   if (strncmp (nbuf, newname, nlen))
1671     insert_reg_alias (nbuf, old->number, old->type);
1672
1673   for (p = nbuf; *p; p++)
1674     *p = TOLOWER (*p);
1675
1676   if (strncmp (nbuf, newname, nlen))
1677     insert_reg_alias (nbuf, old->number, old->type);
1678
1679   return 1;
1680 }
1681
1682 /* Should never be called, as .req goes between the alias and the
1683    register name, not at the beginning of the line.  */
1684 static void
1685 s_req (int a ATTRIBUTE_UNUSED)
1686 {
1687   as_bad (_("invalid syntax for .req directive"));
1688 }
1689
1690 /* The .unreq directive deletes an alias which was previously defined
1691    by .req.  For example:
1692
1693        my_alias .req r11
1694        .unreq my_alias    */
1695
1696 static void
1697 s_unreq (int a ATTRIBUTE_UNUSED)
1698 {
1699   char * name;
1700   char saved_char;
1701
1702   name = input_line_pointer;
1703
1704   while (*input_line_pointer != 0
1705          && *input_line_pointer != ' '
1706          && *input_line_pointer != '\n')
1707     ++input_line_pointer;
1708
1709   saved_char = *input_line_pointer;
1710   *input_line_pointer = 0;
1711
1712   if (!*name)
1713     as_bad (_("invalid syntax for .unreq directive"));
1714   else
1715     {
1716       struct reg_entry *reg = hash_find (arm_reg_hsh, name);
1717
1718       if (!reg)
1719         as_bad (_("unknown register alias '%s'"), name);
1720       else if (reg->builtin)
1721         as_warn (_("ignoring attempt to undefine built-in register '%s'"),
1722                  name);
1723       else
1724         {
1725           hash_delete (arm_reg_hsh, name);
1726           free ((char *) reg->name);
1727           free (reg);
1728         }
1729     }
1730
1731   *input_line_pointer = saved_char;
1732   demand_empty_rest_of_line ();
1733 }
1734
1735 /* Directives: Instruction set selection.  */
1736
1737 #ifdef OBJ_ELF
1738 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
1739    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
1740    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
1741    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
1742
1743 static enum mstate mapstate = MAP_UNDEFINED;
1744
1745 static void
1746 mapping_state (enum mstate state)
1747 {
1748   symbolS * symbolP;
1749   const char * symname;
1750   int type;
1751
1752   if (mapstate == state)
1753     /* The mapping symbol has already been emitted.
1754        There is nothing else to do.  */
1755     return;
1756
1757   mapstate = state;
1758
1759   switch (state)
1760     {
1761     case MAP_DATA:
1762       symname = "$d";
1763       type = BSF_NO_FLAGS;
1764       break;
1765     case MAP_ARM:
1766       symname = "$a";
1767       type = BSF_NO_FLAGS;
1768       break;
1769     case MAP_THUMB:
1770       symname = "$t";
1771       type = BSF_NO_FLAGS;
1772       break;
1773     case MAP_UNDEFINED:
1774       return;
1775     default:
1776       abort ();
1777     }
1778
1779   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
1780
1781   symbolP = symbol_new (symname, now_seg, (valueT) frag_now_fix (), frag_now);
1782   symbol_table_insert (symbolP);
1783   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
1784
1785   switch (state)
1786     {
1787     case MAP_ARM:
1788       THUMB_SET_FUNC (symbolP, 0);
1789       ARM_SET_THUMB (symbolP, 0);
1790       ARM_SET_INTERWORK (symbolP, support_interwork);
1791       break;
1792
1793     case MAP_THUMB:
1794       THUMB_SET_FUNC (symbolP, 1);
1795       ARM_SET_THUMB (symbolP, 1);
1796       ARM_SET_INTERWORK (symbolP, support_interwork);
1797       break;
1798
1799     case MAP_DATA:
1800     default:
1801       return;
1802     }
1803 }
1804 #else
1805 #define mapping_state(x) /* nothing */
1806 #endif
1807
1808 /* Find the real, Thumb encoded start of a Thumb function.  */
1809
1810 static symbolS *
1811 find_real_start (symbolS * symbolP)
1812 {
1813   char *       real_start;
1814   const char * name = S_GET_NAME (symbolP);
1815   symbolS *    new_target;
1816
1817   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
1818 #define STUB_NAME ".real_start_of"
1819
1820   if (name == NULL)
1821     abort ();
1822
1823   /* The compiler may generate BL instructions to local labels because
1824      it needs to perform a branch to a far away location. These labels
1825      do not have a corresponding ".real_start_of" label.  We check
1826      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
1827      the ".real_start_of" convention for nonlocal branches.  */
1828   if (S_IS_LOCAL (symbolP) || name[0] == '.')
1829     return symbolP;
1830
1831   real_start = ACONCAT ((STUB_NAME, name, NULL));
1832   new_target = symbol_find (real_start);
1833
1834   if (new_target == NULL)
1835     {
1836       as_warn ("Failed to find real start of function: %s\n", name);
1837       new_target = symbolP;
1838     }
1839
1840   return new_target;
1841 }
1842
1843 static void
1844 opcode_select (int width)
1845 {
1846   switch (width)
1847     {
1848     case 16:
1849       if (! thumb_mode)
1850         {
1851           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
1852             as_bad (_("selected processor does not support THUMB opcodes"));
1853
1854           thumb_mode = 1;
1855           /* No need to force the alignment, since we will have been
1856              coming from ARM mode, which is word-aligned.  */
1857           record_alignment (now_seg, 1);
1858         }
1859       mapping_state (MAP_THUMB);
1860       break;
1861
1862     case 32:
1863       if (thumb_mode)
1864         {
1865           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
1866             as_bad (_("selected processor does not support ARM opcodes"));
1867
1868           thumb_mode = 0;
1869
1870           if (!need_pass_2)
1871             frag_align (2, 0, 0);
1872
1873           record_alignment (now_seg, 1);
1874         }
1875       mapping_state (MAP_ARM);
1876       break;
1877
1878     default:
1879       as_bad (_("invalid instruction size selected (%d)"), width);
1880     }
1881 }
1882
1883 static void
1884 s_arm (int ignore ATTRIBUTE_UNUSED)
1885 {
1886   opcode_select (32);
1887   demand_empty_rest_of_line ();
1888 }
1889
1890 static void
1891 s_thumb (int ignore ATTRIBUTE_UNUSED)
1892 {
1893   opcode_select (16);
1894   demand_empty_rest_of_line ();
1895 }
1896
1897 static void
1898 s_code (int unused ATTRIBUTE_UNUSED)
1899 {
1900   int temp;
1901
1902   temp = get_absolute_expression ();
1903   switch (temp)
1904     {
1905     case 16:
1906     case 32:
1907       opcode_select (temp);
1908       break;
1909
1910     default:
1911       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
1912     }
1913 }
1914
1915 static void
1916 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
1917 {
1918   /* If we are not already in thumb mode go into it, EVEN if
1919      the target processor does not support thumb instructions.
1920      This is used by gcc/config/arm/lib1funcs.asm for example
1921      to compile interworking support functions even if the
1922      target processor should not support interworking.  */
1923   if (! thumb_mode)
1924     {
1925       thumb_mode = 2;
1926       record_alignment (now_seg, 1);
1927     }
1928
1929   demand_empty_rest_of_line ();
1930 }
1931
1932 static void
1933 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
1934 {
1935   s_thumb (0);
1936
1937   /* The following label is the name/address of the start of a Thumb function.
1938      We need to know this for the interworking support.  */
1939   label_is_thumb_function_name = TRUE;
1940 }
1941
1942 /* Perform a .set directive, but also mark the alias as
1943    being a thumb function.  */
1944
1945 static void
1946 s_thumb_set (int equiv)
1947 {
1948   /* XXX the following is a duplicate of the code for s_set() in read.c
1949      We cannot just call that code as we need to get at the symbol that
1950      is created.  */
1951   char *    name;
1952   char      delim;
1953   char *    end_name;
1954   symbolS * symbolP;
1955
1956   /* Especial apologies for the random logic:
1957      This just grew, and could be parsed much more simply!
1958      Dean - in haste.  */
1959   name      = input_line_pointer;
1960   delim     = get_symbol_end ();
1961   end_name  = input_line_pointer;
1962   *end_name = delim;
1963
1964   if (*input_line_pointer != ',')
1965     {
1966       *end_name = 0;
1967       as_bad (_("expected comma after name \"%s\""), name);
1968       *end_name = delim;
1969       ignore_rest_of_line ();
1970       return;
1971     }
1972
1973   input_line_pointer++;
1974   *end_name = 0;
1975
1976   if (name[0] == '.' && name[1] == '\0')
1977     {
1978       /* XXX - this should not happen to .thumb_set.  */
1979       abort ();
1980     }
1981
1982   if ((symbolP = symbol_find (name)) == NULL
1983       && (symbolP = md_undefined_symbol (name)) == NULL)
1984     {
1985 #ifndef NO_LISTING
1986       /* When doing symbol listings, play games with dummy fragments living
1987          outside the normal fragment chain to record the file and line info
1988          for this symbol.  */
1989       if (listing & LISTING_SYMBOLS)
1990         {
1991           extern struct list_info_struct * listing_tail;
1992           fragS * dummy_frag = xmalloc (sizeof (fragS));
1993
1994           memset (dummy_frag, 0, sizeof (fragS));
1995           dummy_frag->fr_type = rs_fill;
1996           dummy_frag->line = listing_tail;
1997           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
1998           dummy_frag->fr_symbol = symbolP;
1999         }
2000       else
2001 #endif
2002         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2003
2004 #ifdef OBJ_COFF
2005       /* "set" symbols are local unless otherwise specified.  */
2006       SF_SET_LOCAL (symbolP);
2007 #endif /* OBJ_COFF  */
2008     }                           /* Make a new symbol.  */
2009
2010   symbol_table_insert (symbolP);
2011
2012   * end_name = delim;
2013
2014   if (equiv
2015       && S_IS_DEFINED (symbolP)
2016       && S_GET_SEGMENT (symbolP) != reg_section)
2017     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2018
2019   pseudo_set (symbolP);
2020
2021   demand_empty_rest_of_line ();
2022
2023   /* XXX Now we come to the Thumb specific bit of code.  */
2024
2025   THUMB_SET_FUNC (symbolP, 1);
2026   ARM_SET_THUMB (symbolP, 1);
2027 #if defined OBJ_ELF || defined OBJ_COFF
2028   ARM_SET_INTERWORK (symbolP, support_interwork);
2029 #endif
2030 }
2031
2032 /* Directives: Mode selection.  */
2033
2034 /* .syntax [unified|divided] - choose the new unified syntax
2035    (same for Arm and Thumb encoding, modulo slight differences in what
2036    can be represented) or the old divergent syntax for each mode.  */
2037 static void
2038 s_syntax (int unused ATTRIBUTE_UNUSED)
2039 {
2040   char *name, delim;
2041
2042   name = input_line_pointer;
2043   delim = get_symbol_end ();
2044
2045   if (!strcasecmp (name, "unified"))
2046     unified_syntax = TRUE;
2047   else if (!strcasecmp (name, "divided"))
2048     unified_syntax = FALSE;
2049   else
2050     {
2051       as_bad (_("unrecognized syntax mode \"%s\""), name);
2052       return;
2053     }
2054   *input_line_pointer = delim;
2055   demand_empty_rest_of_line ();
2056 }
2057
2058 /* Directives: sectioning and alignment.  */
2059
2060 /* Same as s_align_ptwo but align 0 => align 2.  */
2061
2062 static void
2063 s_align (int unused ATTRIBUTE_UNUSED)
2064 {
2065   int temp;
2066   long temp_fill;
2067   long max_alignment = 15;
2068
2069   temp = get_absolute_expression ();
2070   if (temp > max_alignment)
2071     as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
2072   else if (temp < 0)
2073     {
2074       as_bad (_("alignment negative. 0 assumed."));
2075       temp = 0;
2076     }
2077
2078   if (*input_line_pointer == ',')
2079     {
2080       input_line_pointer++;
2081       temp_fill = get_absolute_expression ();
2082     }
2083   else
2084     temp_fill = 0;
2085
2086   if (!temp)
2087     temp = 2;
2088
2089   /* Only make a frag if we HAVE to.  */
2090   if (temp && !need_pass_2)
2091     frag_align (temp, (int) temp_fill, 0);
2092   demand_empty_rest_of_line ();
2093
2094   record_alignment (now_seg, temp);
2095 }
2096
2097 static void
2098 s_bss (int ignore ATTRIBUTE_UNUSED)
2099 {
2100   /* We don't support putting frags in the BSS segment, we fake it by
2101      marking in_bss, then looking at s_skip for clues.  */
2102   subseg_set (bss_section, 0);
2103   demand_empty_rest_of_line ();
2104   mapping_state (MAP_DATA);
2105 }
2106
2107 static void
2108 s_even (int ignore ATTRIBUTE_UNUSED)
2109 {
2110   /* Never make frag if expect extra pass.  */
2111   if (!need_pass_2)
2112     frag_align (1, 0, 0);
2113
2114   record_alignment (now_seg, 1);
2115
2116   demand_empty_rest_of_line ();
2117 }
2118
2119 /* Directives: Literal pools.  */
2120
2121 static literal_pool *
2122 find_literal_pool (void)
2123 {
2124   literal_pool * pool;
2125
2126   for (pool = list_of_pools; pool != NULL; pool = pool->next)
2127     {
2128       if (pool->section == now_seg
2129           && pool->sub_section == now_subseg)
2130         break;
2131     }
2132
2133   return pool;
2134 }
2135
2136 static literal_pool *
2137 find_or_make_literal_pool (void)
2138 {
2139   /* Next literal pool ID number.  */
2140   static unsigned int latest_pool_num = 1;
2141   literal_pool *      pool;
2142
2143   pool = find_literal_pool ();
2144
2145   if (pool == NULL)
2146     {
2147       /* Create a new pool.  */
2148       pool = xmalloc (sizeof (* pool));
2149       if (! pool)
2150         return NULL;
2151
2152       pool->next_free_entry = 0;
2153       pool->section         = now_seg;
2154       pool->sub_section     = now_subseg;
2155       pool->next            = list_of_pools;
2156       pool->symbol          = NULL;
2157
2158       /* Add it to the list.  */
2159       list_of_pools = pool;
2160     }
2161
2162   /* New pools, and emptied pools, will have a NULL symbol.  */
2163   if (pool->symbol == NULL)
2164     {
2165       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
2166                                     (valueT) 0, &zero_address_frag);
2167       pool->id = latest_pool_num ++;
2168     }
2169
2170   /* Done.  */
2171   return pool;
2172 }
2173
2174 /* Add the literal in the global 'inst'
2175    structure to the relevent literal pool.  */
2176
2177 static int
2178 add_to_lit_pool (void)
2179 {
2180   literal_pool * pool;
2181   unsigned int entry;
2182
2183   pool = find_or_make_literal_pool ();
2184
2185   /* Check if this literal value is already in the pool.  */
2186   for (entry = 0; entry < pool->next_free_entry; entry ++)
2187     {
2188       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
2189           && (inst.reloc.exp.X_op == O_constant)
2190           && (pool->literals[entry].X_add_number
2191               == inst.reloc.exp.X_add_number)
2192           && (pool->literals[entry].X_unsigned
2193               == inst.reloc.exp.X_unsigned))
2194         break;
2195
2196       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
2197           && (inst.reloc.exp.X_op == O_symbol)
2198           && (pool->literals[entry].X_add_number
2199               == inst.reloc.exp.X_add_number)
2200           && (pool->literals[entry].X_add_symbol
2201               == inst.reloc.exp.X_add_symbol)
2202           && (pool->literals[entry].X_op_symbol
2203               == inst.reloc.exp.X_op_symbol))
2204         break;
2205     }
2206
2207   /* Do we need to create a new entry?  */
2208   if (entry == pool->next_free_entry)
2209     {
2210       if (entry >= MAX_LITERAL_POOL_SIZE)
2211         {
2212           inst.error = _("literal pool overflow");
2213           return FAIL;
2214         }
2215
2216       pool->literals[entry] = inst.reloc.exp;
2217       pool->next_free_entry += 1;
2218     }
2219
2220   inst.reloc.exp.X_op         = O_symbol;
2221   inst.reloc.exp.X_add_number = ((int) entry) * 4;
2222   inst.reloc.exp.X_add_symbol = pool->symbol;
2223
2224   return SUCCESS;
2225 }
2226
2227 /* Can't use symbol_new here, so have to create a symbol and then at
2228    a later date assign it a value. Thats what these functions do.  */
2229
2230 static void
2231 symbol_locate (symbolS *    symbolP,
2232                const char * name,       /* It is copied, the caller can modify.  */
2233                segT         segment,    /* Segment identifier (SEG_<something>).  */
2234                valueT       valu,       /* Symbol value.  */
2235                fragS *      frag)       /* Associated fragment.  */
2236 {
2237   unsigned int name_length;
2238   char * preserved_copy_of_name;
2239
2240   name_length = strlen (name) + 1;   /* +1 for \0.  */
2241   obstack_grow (&notes, name, name_length);
2242   preserved_copy_of_name = obstack_finish (&notes);
2243
2244 #ifdef tc_canonicalize_symbol_name
2245   preserved_copy_of_name =
2246     tc_canonicalize_symbol_name (preserved_copy_of_name);
2247 #endif
2248
2249   S_SET_NAME (symbolP, preserved_copy_of_name);
2250
2251   S_SET_SEGMENT (symbolP, segment);
2252   S_SET_VALUE (symbolP, valu);
2253   symbol_clear_list_pointers (symbolP);
2254
2255   symbol_set_frag (symbolP, frag);
2256
2257   /* Link to end of symbol chain.  */
2258   {
2259     extern int symbol_table_frozen;
2260
2261     if (symbol_table_frozen)
2262       abort ();
2263   }
2264
2265   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
2266
2267   obj_symbol_new_hook (symbolP);
2268
2269 #ifdef tc_symbol_new_hook
2270   tc_symbol_new_hook (symbolP);
2271 #endif
2272
2273 #ifdef DEBUG_SYMS
2274   verify_symbol_chain (symbol_rootP, symbol_lastP);
2275 #endif /* DEBUG_SYMS  */
2276 }
2277
2278
2279 static void
2280 s_ltorg (int ignored ATTRIBUTE_UNUSED)
2281 {
2282   unsigned int entry;
2283   literal_pool * pool;
2284   char sym_name[20];
2285
2286   pool = find_literal_pool ();
2287   if (pool == NULL
2288       || pool->symbol == NULL
2289       || pool->next_free_entry == 0)
2290     return;
2291
2292   mapping_state (MAP_DATA);
2293
2294   /* Align pool as you have word accesses.
2295      Only make a frag if we have to.  */
2296   if (!need_pass_2)
2297     frag_align (2, 0, 0);
2298
2299   record_alignment (now_seg, 2);
2300
2301   sprintf (sym_name, "$$lit_\002%x", pool->id);
2302
2303   symbol_locate (pool->symbol, sym_name, now_seg,
2304                  (valueT) frag_now_fix (), frag_now);
2305   symbol_table_insert (pool->symbol);
2306
2307   ARM_SET_THUMB (pool->symbol, thumb_mode);
2308
2309 #if defined OBJ_COFF || defined OBJ_ELF
2310   ARM_SET_INTERWORK (pool->symbol, support_interwork);
2311 #endif
2312
2313   for (entry = 0; entry < pool->next_free_entry; entry ++)
2314     /* First output the expression in the instruction to the pool.  */
2315     emit_expr (&(pool->literals[entry]), 4); /* .word  */
2316
2317   /* Mark the pool as empty.  */
2318   pool->next_free_entry = 0;
2319   pool->symbol = NULL;
2320 }
2321
2322 #ifdef OBJ_ELF
2323 /* Forward declarations for functions below, in the MD interface
2324    section.  */
2325 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
2326 static valueT create_unwind_entry (int);
2327 static void start_unwind_section (const segT, int);
2328 static void add_unwind_opcode (valueT, int);
2329 static void flush_pending_unwind (void);
2330
2331 /* Directives: Data.  */
2332
2333 static void
2334 s_arm_elf_cons (int nbytes)
2335 {
2336   expressionS exp;
2337
2338 #ifdef md_flush_pending_output
2339   md_flush_pending_output ();
2340 #endif
2341
2342   if (is_it_end_of_statement ())
2343     {
2344       demand_empty_rest_of_line ();
2345       return;
2346     }
2347
2348 #ifdef md_cons_align
2349   md_cons_align (nbytes);
2350 #endif
2351
2352   mapping_state (MAP_DATA);
2353   do
2354     {
2355       int reloc;
2356       char *base = input_line_pointer;
2357
2358       expression (& exp);
2359
2360       if (exp.X_op != O_symbol)
2361         emit_expr (&exp, (unsigned int) nbytes);
2362       else
2363         {
2364           char *before_reloc = input_line_pointer;
2365           reloc = parse_reloc (&input_line_pointer);
2366           if (reloc == -1)
2367             {
2368               as_bad (_("unrecognized relocation suffix"));
2369               ignore_rest_of_line ();
2370               return;
2371             }
2372           else if (reloc == BFD_RELOC_UNUSED)
2373             emit_expr (&exp, (unsigned int) nbytes);
2374           else
2375             {
2376               reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, reloc);
2377               int size = bfd_get_reloc_size (howto);
2378
2379               if (reloc == BFD_RELOC_ARM_PLT32)
2380                 {
2381                   as_bad (_("(plt) is only valid on branch targets"));
2382                   reloc = BFD_RELOC_UNUSED;
2383                   size = 0;
2384                 }
2385
2386               if (size > nbytes)
2387                 as_bad (_("%s relocations do not fit in %d bytes"),
2388                         howto->name, nbytes);
2389               else
2390                 {
2391                   /* We've parsed an expression stopping at O_symbol.
2392                      But there may be more expression left now that we
2393                      have parsed the relocation marker.  Parse it again.
2394                      XXX Surely there is a cleaner way to do this.  */
2395                   char *p = input_line_pointer;
2396                   int offset;
2397                   char *save_buf = alloca (input_line_pointer - base);
2398                   memcpy (save_buf, base, input_line_pointer - base);
2399                   memmove (base + (input_line_pointer - before_reloc),
2400                            base, before_reloc - base);
2401
2402                   input_line_pointer = base + (input_line_pointer-before_reloc);
2403                   expression (&exp);
2404                   memcpy (base, save_buf, p - base);
2405
2406                   offset = nbytes - size;
2407                   p = frag_more ((int) nbytes);
2408                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
2409                                size, &exp, 0, reloc);
2410                 }
2411             }
2412         }
2413     }
2414   while (*input_line_pointer++ == ',');
2415
2416   /* Put terminator back into stream.  */
2417   input_line_pointer --;
2418   demand_empty_rest_of_line ();
2419 }
2420
2421
2422 /* Parse a .rel31 directive.  */
2423
2424 static void
2425 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
2426 {
2427   expressionS exp;
2428   char *p;
2429   valueT highbit;
2430
2431   highbit = 0;
2432   if (*input_line_pointer == '1')
2433     highbit = 0x80000000;
2434   else if (*input_line_pointer != '0')
2435     as_bad (_("expected 0 or 1"));
2436
2437   input_line_pointer++;
2438   if (*input_line_pointer != ',')
2439     as_bad (_("missing comma"));
2440   input_line_pointer++;
2441
2442 #ifdef md_flush_pending_output
2443   md_flush_pending_output ();
2444 #endif
2445
2446 #ifdef md_cons_align
2447   md_cons_align (4);
2448 #endif
2449
2450   mapping_state (MAP_DATA);
2451
2452   expression (&exp);
2453
2454   p = frag_more (4);
2455   md_number_to_chars (p, highbit, 4);
2456   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
2457                BFD_RELOC_ARM_PREL31);
2458
2459   demand_empty_rest_of_line ();
2460 }
2461
2462 /* Directives: AEABI stack-unwind tables.  */
2463
2464 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
2465
2466 static void
2467 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
2468 {
2469   demand_empty_rest_of_line ();
2470   /* Mark the start of the function.  */
2471   unwind.proc_start = expr_build_dot ();
2472
2473   /* Reset the rest of the unwind info.  */
2474   unwind.opcode_count = 0;
2475   unwind.table_entry = NULL;
2476   unwind.personality_routine = NULL;
2477   unwind.personality_index = -1;
2478   unwind.frame_size = 0;
2479   unwind.fp_offset = 0;
2480   unwind.fp_reg = 13;
2481   unwind.fp_used = 0;
2482   unwind.sp_restored = 0;
2483 }
2484
2485
2486 /* Parse a handlerdata directive.  Creates the exception handling table entry
2487    for the function.  */
2488
2489 static void
2490 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
2491 {
2492   demand_empty_rest_of_line ();
2493   if (unwind.table_entry)
2494     as_bad (_("dupicate .handlerdata directive"));
2495
2496   create_unwind_entry (1);
2497 }
2498
2499 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
2500
2501 static void
2502 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
2503 {
2504   long where;
2505   char *ptr;
2506   valueT val;
2507
2508   demand_empty_rest_of_line ();
2509
2510   /* Add eh table entry.  */
2511   if (unwind.table_entry == NULL)
2512     val = create_unwind_entry (0);
2513   else
2514     val = 0;
2515
2516   /* Add index table entry.  This is two words.  */
2517   start_unwind_section (unwind.saved_seg, 1);
2518   frag_align (2, 0, 0);
2519   record_alignment (now_seg, 2);
2520
2521   ptr = frag_more (8);
2522   where = frag_now_fix () - 8;
2523
2524   /* Self relative offset of the function start.  */
2525   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
2526            BFD_RELOC_ARM_PREL31);
2527
2528   /* Indicate dependency on EHABI-defined personality routines to the
2529      linker, if it hasn't been done already.  */
2530   if (unwind.personality_index >= 0 && unwind.personality_index < 3
2531       && !(marked_pr_dependency & (1 << unwind.personality_index)))
2532     {
2533       static const char *const name[] = {
2534         "__aeabi_unwind_cpp_pr0",
2535         "__aeabi_unwind_cpp_pr1",
2536         "__aeabi_unwind_cpp_pr2"
2537       };
2538       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
2539       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
2540       marked_pr_dependency |= 1 << unwind.personality_index;
2541       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
2542         = marked_pr_dependency;
2543     }
2544
2545   if (val)
2546     /* Inline exception table entry.  */
2547     md_number_to_chars (ptr + 4, val, 4);
2548   else
2549     /* Self relative offset of the table entry.  */
2550     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
2551              BFD_RELOC_ARM_PREL31);
2552
2553   /* Restore the original section.  */
2554   subseg_set (unwind.saved_seg, unwind.saved_subseg);
2555 }
2556
2557
2558 /* Parse an unwind_cantunwind directive.  */
2559
2560 static void
2561 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
2562 {
2563   demand_empty_rest_of_line ();
2564   if (unwind.personality_routine || unwind.personality_index != -1)
2565     as_bad (_("personality routine specified for cantunwind frame"));
2566
2567   unwind.personality_index = -2;
2568 }
2569
2570
2571 /* Parse a personalityindex directive.  */
2572
2573 static void
2574 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
2575 {
2576   expressionS exp;
2577
2578   if (unwind.personality_routine || unwind.personality_index != -1)
2579     as_bad (_("duplicate .personalityindex directive"));
2580
2581   expression (&exp);
2582
2583   if (exp.X_op != O_constant
2584       || exp.X_add_number < 0 || exp.X_add_number > 15)
2585     {
2586       as_bad (_("bad personality routine number"));
2587       ignore_rest_of_line ();
2588       return;
2589     }
2590
2591   unwind.personality_index = exp.X_add_number;
2592
2593   demand_empty_rest_of_line ();
2594 }
2595
2596
2597 /* Parse a personality directive.  */
2598
2599 static void
2600 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
2601 {
2602   char *name, *p, c;
2603
2604   if (unwind.personality_routine || unwind.personality_index != -1)
2605     as_bad (_("duplicate .personality directive"));
2606
2607   name = input_line_pointer;
2608   c = get_symbol_end ();
2609   p = input_line_pointer;
2610   unwind.personality_routine = symbol_find_or_make (name);
2611   *p = c;
2612   demand_empty_rest_of_line ();
2613 }
2614
2615
2616 /* Parse a directive saving core registers.  */
2617
2618 static void
2619 s_arm_unwind_save_core (void)
2620 {
2621   valueT op;
2622   long range;
2623   int n;
2624
2625   range = parse_reg_list (&input_line_pointer);
2626   if (range == FAIL)
2627     {
2628       as_bad (_("expected register list"));
2629       ignore_rest_of_line ();
2630       return;
2631     }
2632
2633   demand_empty_rest_of_line ();
2634
2635   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
2636      into .unwind_save {..., sp...}.  We aren't bothered about the value of
2637      ip because it is clobbered by calls.  */
2638   if (unwind.sp_restored && unwind.fp_reg == 12
2639       && (range & 0x3000) == 0x1000)
2640     {
2641       unwind.opcode_count--;
2642       unwind.sp_restored = 0;
2643       range = (range | 0x2000) & ~0x1000;
2644       unwind.pending_offset = 0;
2645     }
2646
2647   /* Pop r4-r15.  */
2648   if (range & 0xfff0)
2649     {
2650       /* See if we can use the short opcodes.  These pop a block of up to 8
2651          registers starting with r4, plus maybe r14.  */
2652       for (n = 0; n < 8; n++)
2653         {
2654           /* Break at the first non-saved register.      */
2655           if ((range & (1 << (n + 4))) == 0)
2656             break;
2657         }
2658       /* See if there are any other bits set.  */
2659       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
2660         {
2661           /* Use the long form.  */
2662           op = 0x8000 | ((range >> 4) & 0xfff);
2663           add_unwind_opcode (op, 2);
2664         }
2665       else
2666         {
2667           /* Use the short form.  */
2668           if (range & 0x4000)
2669             op = 0xa8; /* Pop r14.      */
2670           else
2671             op = 0xa0; /* Do not pop r14.  */
2672           op |= (n - 1);
2673           add_unwind_opcode (op, 1);
2674         }
2675     }
2676
2677   /* Pop r0-r3.  */
2678   if (range & 0xf)
2679     {
2680       op = 0xb100 | (range & 0xf);
2681       add_unwind_opcode (op, 2);
2682     }
2683
2684   /* Record the number of bytes pushed.  */
2685   for (n = 0; n < 16; n++)
2686     {
2687       if (range & (1 << n))
2688         unwind.frame_size += 4;
2689     }
2690 }
2691
2692
2693 /* Parse a directive saving FPA registers.  */
2694
2695 static void
2696 s_arm_unwind_save_fpa (int reg)
2697 {
2698   expressionS exp;
2699   int num_regs;
2700   valueT op;
2701
2702   /* Get Number of registers to transfer.  */
2703   if (skip_past_comma (&input_line_pointer) != FAIL)
2704     expression (&exp);
2705   else
2706     exp.X_op = O_illegal;
2707
2708   if (exp.X_op != O_constant)
2709     {
2710       as_bad (_("expected , <constant>"));
2711       ignore_rest_of_line ();
2712       return;
2713     }
2714
2715   num_regs = exp.X_add_number;
2716
2717   if (num_regs < 1 || num_regs > 4)
2718     {
2719       as_bad (_("number of registers must be in the range [1:4]"));
2720       ignore_rest_of_line ();
2721       return;
2722     }
2723
2724   demand_empty_rest_of_line ();
2725
2726   if (reg == 4)
2727     {
2728       /* Short form.  */
2729       op = 0xb4 | (num_regs - 1);
2730       add_unwind_opcode (op, 1);
2731     }
2732   else
2733     {
2734       /* Long form.  */
2735       op = 0xc800 | (reg << 4) | (num_regs - 1);
2736       add_unwind_opcode (op, 2);
2737     }
2738   unwind.frame_size += num_regs * 12;
2739 }
2740
2741
2742 /* Parse a directive saving VFP registers.  */
2743
2744 static void
2745 s_arm_unwind_save_vfp (void)
2746 {
2747   int count;
2748   unsigned int reg;
2749   valueT op;
2750
2751   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
2752   if (count == FAIL)
2753     {
2754       as_bad (_("expected register list"));
2755       ignore_rest_of_line ();
2756       return;
2757     }
2758
2759   demand_empty_rest_of_line ();
2760
2761   if (reg == 8)
2762     {
2763       /* Short form.  */
2764       op = 0xb8 | (count - 1);
2765       add_unwind_opcode (op, 1);
2766     }
2767   else
2768     {
2769       /* Long form.  */
2770       op = 0xb300 | (reg << 4) | (count - 1);
2771       add_unwind_opcode (op, 2);
2772     }
2773   unwind.frame_size += count * 8 + 4;
2774 }
2775
2776
2777 /* Parse a directive saving iWMMXt data registers.  */
2778
2779 static void
2780 s_arm_unwind_save_mmxwr (void)
2781 {
2782   int reg;
2783   int hi_reg;
2784   int i;
2785   unsigned mask = 0;
2786   valueT op;
2787
2788   if (*input_line_pointer == '{')
2789     input_line_pointer++;
2790
2791   do
2792     {
2793       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR, NULL);
2794
2795       if (reg == FAIL)
2796         {
2797           as_bad (_(reg_expected_msgs[REG_TYPE_MMXWR]));
2798           goto error;
2799         }
2800
2801       if (mask >> reg)
2802         as_tsktsk (_("register list not in ascending order"));
2803       mask |= 1 << reg;
2804
2805       if (*input_line_pointer == '-')
2806         {
2807           input_line_pointer++;
2808           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR, NULL);
2809           if (hi_reg == FAIL)
2810             {
2811               as_bad (_(reg_expected_msgs[REG_TYPE_MMXWR]));
2812               goto error;
2813             }
2814           else if (reg >= hi_reg)
2815             {
2816               as_bad (_("bad register range"));
2817               goto error;
2818             }
2819           for (; reg < hi_reg; reg++)
2820             mask |= 1 << reg;
2821         }
2822     }
2823   while (skip_past_comma (&input_line_pointer) != FAIL);
2824
2825   if (*input_line_pointer == '}')
2826     input_line_pointer++;
2827
2828   demand_empty_rest_of_line ();
2829
2830   /* Generate any deferred opcodes becuuse we're going to be looking at
2831      the list.  */
2832   flush_pending_unwind ();
2833
2834   for (i = 0; i < 16; i++)
2835     {
2836       if (mask & (1 << i))
2837         unwind.frame_size += 8;
2838     }
2839
2840   /* Attempt to combine with a previous opcode.  We do this because gcc
2841      likes to output separate unwind directives for a single block of
2842      registers.  */
2843   if (unwind.opcode_count > 0)
2844     {
2845       i = unwind.opcodes[unwind.opcode_count - 1];
2846       if ((i & 0xf8) == 0xc0)
2847         {
2848           i &= 7;
2849           /* Only merge if the blocks are contiguous.  */
2850           if (i < 6)
2851             {
2852               if ((mask & 0xfe00) == (1 << 9))
2853                 {
2854                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
2855                   unwind.opcode_count--;
2856                 }
2857             }
2858           else if (i == 6 && unwind.opcode_count >= 2)
2859             {
2860               i = unwind.opcodes[unwind.opcode_count - 2];
2861               reg = i >> 4;
2862               i &= 0xf;
2863
2864               op = 0xffff << (reg - 1);
2865               if (reg > 0
2866                   || ((mask & op) == (1u << (reg - 1))))
2867                 {
2868                   op = (1 << (reg + i + 1)) - 1;
2869                   op &= ~((1 << reg) - 1);
2870                   mask |= op;
2871                   unwind.opcode_count -= 2;
2872                 }
2873             }
2874         }
2875     }
2876
2877   hi_reg = 15;
2878   /* We want to generate opcodes in the order the registers have been
2879      saved, ie. descending order.  */
2880   for (reg = 15; reg >= -1; reg--)
2881     {
2882       /* Save registers in blocks.  */
2883       if (reg < 0
2884           || !(mask & (1 << reg)))
2885         {
2886           /* We found an unsaved reg.  Generate opcodes to save the
2887              preceeding block.  */
2888           if (reg != hi_reg)
2889             {
2890               if (reg == 9)
2891                 {
2892                   /* Short form.  */
2893                   op = 0xc0 | (hi_reg - 10);
2894                   add_unwind_opcode (op, 1);
2895                 }
2896               else
2897                 {
2898                   /* Long form.  */
2899                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
2900                   add_unwind_opcode (op, 2);
2901                 }
2902             }
2903           hi_reg = reg - 1;
2904         }
2905     }
2906
2907   return;
2908 error:
2909   ignore_rest_of_line ();
2910 }
2911
2912 static void
2913 s_arm_unwind_save_mmxwcg (void)
2914 {
2915   int reg;
2916   int hi_reg;
2917   unsigned mask = 0;
2918   valueT op;
2919
2920   if (*input_line_pointer == '{')
2921     input_line_pointer++;
2922
2923   do
2924     {
2925       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG, NULL);
2926
2927       if (reg == FAIL)
2928         {
2929           as_bad (_(reg_expected_msgs[REG_TYPE_MMXWCG]));
2930           goto error;
2931         }
2932
2933       reg -= 8;
2934       if (mask >> reg)
2935         as_tsktsk (_("register list not in ascending order"));
2936       mask |= 1 << reg;
2937
2938       if (*input_line_pointer == '-')
2939         {
2940           input_line_pointer++;
2941           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG, NULL);
2942           if (hi_reg == FAIL)
2943             {
2944               as_bad (_(reg_expected_msgs[REG_TYPE_MMXWCG]));
2945               goto error;
2946             }
2947           else if (reg >= hi_reg)
2948             {
2949               as_bad (_("bad register range"));
2950               goto error;
2951             }
2952           for (; reg < hi_reg; reg++)
2953             mask |= 1 << reg;
2954         }
2955     }
2956   while (skip_past_comma (&input_line_pointer) != FAIL);
2957
2958   if (*input_line_pointer == '}')
2959     input_line_pointer++;
2960
2961   demand_empty_rest_of_line ();
2962
2963   /* Generate any deferred opcodes becuuse we're going to be looking at
2964      the list.  */
2965   flush_pending_unwind ();
2966
2967   for (reg = 0; reg < 16; reg++)
2968     {
2969       if (mask & (1 << reg))
2970         unwind.frame_size += 4;
2971     }
2972   op = 0xc700 | mask;
2973   add_unwind_opcode (op, 2);
2974   return;
2975 error:
2976   ignore_rest_of_line ();
2977 }
2978
2979
2980 /* Parse an unwind_save directive.  */
2981
2982 static void
2983 s_arm_unwind_save (int ignored ATTRIBUTE_UNUSED)
2984 {
2985   char *peek;
2986   struct reg_entry *reg;
2987   bfd_boolean had_brace = FALSE;
2988
2989   /* Figure out what sort of save we have.  */
2990   peek = input_line_pointer;
2991
2992   if (*peek == '{')
2993     {
2994       had_brace = TRUE;
2995       peek++;
2996     }
2997
2998   reg = arm_reg_parse_multi (&peek);
2999
3000   if (!reg)
3001     {
3002       as_bad (_("register expected"));
3003       ignore_rest_of_line ();
3004       return;
3005     }
3006
3007   switch (reg->type)
3008     {
3009     case REG_TYPE_FN:
3010       if (had_brace)
3011         {
3012           as_bad (_("FPA .unwind_save does not take a register list"));
3013           ignore_rest_of_line ();
3014           return;
3015         }
3016       s_arm_unwind_save_fpa (reg->number);
3017       return;
3018
3019     case REG_TYPE_RN:     s_arm_unwind_save_core ();   return;
3020     case REG_TYPE_VFD:     s_arm_unwind_save_vfp ();    return;
3021     case REG_TYPE_MMXWR:  s_arm_unwind_save_mmxwr ();  return;
3022     case REG_TYPE_MMXWCG: s_arm_unwind_save_mmxwcg (); return;
3023
3024     default:
3025       as_bad (_(".unwind_save does not support this kind of register"));
3026       ignore_rest_of_line ();
3027     }
3028 }
3029
3030
3031 /* Parse an unwind_movsp directive.  */
3032
3033 static void
3034 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
3035 {
3036   int reg;
3037   valueT op;
3038
3039   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN, NULL);
3040   if (reg == FAIL)
3041     {
3042       as_bad (_(reg_expected_msgs[REG_TYPE_RN]));
3043       ignore_rest_of_line ();
3044       return;
3045     }
3046   demand_empty_rest_of_line ();
3047
3048   if (reg == REG_SP || reg == REG_PC)
3049     {
3050       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
3051       return;
3052     }
3053
3054   if (unwind.fp_reg != REG_SP)
3055     as_bad (_("unexpected .unwind_movsp directive"));
3056
3057   /* Generate opcode to restore the value.  */
3058   op = 0x90 | reg;
3059   add_unwind_opcode (op, 1);
3060
3061   /* Record the information for later.  */
3062   unwind.fp_reg = reg;
3063   unwind.fp_offset = unwind.frame_size;
3064   unwind.sp_restored = 1;
3065 }
3066
3067 /* Parse an unwind_pad directive.  */
3068
3069 static void
3070 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
3071 {
3072   int offset;
3073
3074   if (immediate_for_directive (&offset) == FAIL)
3075     return;
3076
3077   if (offset & 3)
3078     {
3079       as_bad (_("stack increment must be multiple of 4"));
3080       ignore_rest_of_line ();
3081       return;
3082     }
3083
3084   /* Don't generate any opcodes, just record the details for later.  */
3085   unwind.frame_size += offset;
3086   unwind.pending_offset += offset;
3087
3088   demand_empty_rest_of_line ();
3089 }
3090
3091 /* Parse an unwind_setfp directive.  */
3092
3093 static void
3094 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
3095 {
3096   int sp_reg;
3097   int fp_reg;
3098   int offset;
3099
3100   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN, NULL);
3101   if (skip_past_comma (&input_line_pointer) == FAIL)
3102     sp_reg = FAIL;
3103   else
3104     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN, NULL);
3105
3106   if (fp_reg == FAIL || sp_reg == FAIL)
3107     {
3108       as_bad (_("expected <reg>, <reg>"));
3109       ignore_rest_of_line ();
3110       return;
3111     }
3112
3113   /* Optional constant.  */
3114   if (skip_past_comma (&input_line_pointer) != FAIL)
3115     {
3116       if (immediate_for_directive (&offset) == FAIL)
3117         return;
3118     }
3119   else
3120     offset = 0;
3121
3122   demand_empty_rest_of_line ();
3123
3124   if (sp_reg != 13 && sp_reg != unwind.fp_reg)
3125     {
3126       as_bad (_("register must be either sp or set by a previous"
3127                 "unwind_movsp directive"));
3128       return;
3129     }
3130
3131   /* Don't generate any opcodes, just record the information for later.  */
3132   unwind.fp_reg = fp_reg;
3133   unwind.fp_used = 1;
3134   if (sp_reg == 13)
3135     unwind.fp_offset = unwind.frame_size - offset;
3136   else
3137     unwind.fp_offset -= offset;
3138 }
3139
3140 /* Parse an unwind_raw directive.  */
3141
3142 static void
3143 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
3144 {
3145   expressionS exp;
3146   /* This is an arbitary limit.  */
3147   unsigned char op[16];
3148   int count;
3149
3150   expression (&exp);
3151   if (exp.X_op == O_constant
3152       && skip_past_comma (&input_line_pointer) != FAIL)
3153     {
3154       unwind.frame_size += exp.X_add_number;
3155       expression (&exp);
3156     }
3157   else
3158     exp.X_op = O_illegal;
3159
3160   if (exp.X_op != O_constant)
3161     {
3162       as_bad (_("expected <offset>, <opcode>"));
3163       ignore_rest_of_line ();
3164       return;
3165     }
3166
3167   count = 0;
3168
3169   /* Parse the opcode.  */
3170   for (;;)
3171     {
3172       if (count >= 16)
3173         {
3174           as_bad (_("unwind opcode too long"));
3175           ignore_rest_of_line ();
3176         }
3177       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
3178         {
3179           as_bad (_("invalid unwind opcode"));
3180           ignore_rest_of_line ();
3181           return;
3182         }
3183       op[count++] = exp.X_add_number;
3184
3185       /* Parse the next byte.  */
3186       if (skip_past_comma (&input_line_pointer) == FAIL)
3187         break;
3188
3189       expression (&exp);
3190     }
3191
3192   /* Add the opcode bytes in reverse order.  */
3193   while (count--)
3194     add_unwind_opcode (op[count], 1);
3195
3196   demand_empty_rest_of_line ();
3197 }
3198
3199
3200 /* Parse a .eabi_attribute directive.  */
3201
3202 static void
3203 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
3204 {
3205   expressionS exp;
3206   bfd_boolean is_string;
3207   int tag;
3208   unsigned int i = 0;
3209   char *s = NULL;
3210   char saved_char;
3211
3212   expression (& exp);
3213   if (exp.X_op != O_constant)
3214     goto bad;
3215
3216   tag = exp.X_add_number;
3217   if (tag == 4 || tag == 5 || tag == 32 || (tag > 32 && (tag & 1) != 0))
3218     is_string = 1;
3219   else
3220     is_string = 0;
3221
3222   if (skip_past_comma (&input_line_pointer) == FAIL)
3223     goto bad;
3224   if (tag == 32 || !is_string)
3225     {
3226       expression (& exp);
3227       if (exp.X_op != O_constant)
3228         {
3229           as_bad (_("expected numeric constant"));
3230           ignore_rest_of_line ();
3231           return;
3232         }
3233       i = exp.X_add_number;
3234     }
3235   if (tag == Tag_compatibility
3236       && skip_past_comma (&input_line_pointer) == FAIL)
3237     {
3238       as_bad (_("expected comma"));
3239       ignore_rest_of_line ();
3240       return;
3241     }
3242   if (is_string)
3243     {
3244       skip_whitespace(input_line_pointer);
3245       if (*input_line_pointer != '"')
3246         goto bad_string;
3247       input_line_pointer++;
3248       s = input_line_pointer;
3249       while (*input_line_pointer && *input_line_pointer != '"')
3250         input_line_pointer++;
3251       if (*input_line_pointer != '"')
3252         goto bad_string;
3253       saved_char = *input_line_pointer;
3254       *input_line_pointer = 0;
3255     }
3256   else
3257     {
3258       s = NULL;
3259       saved_char = 0;
3260     }
3261   
3262   if (tag == Tag_compatibility)
3263     elf32_arm_add_eabi_attr_compat (stdoutput, i, s);
3264   else if (is_string)
3265     elf32_arm_add_eabi_attr_string (stdoutput, tag, s);
3266   else
3267     elf32_arm_add_eabi_attr_int (stdoutput, tag, i);
3268
3269   if (s)
3270     {
3271       *input_line_pointer = saved_char;
3272       input_line_pointer++;
3273     }
3274   demand_empty_rest_of_line ();
3275   return;
3276 bad_string:
3277   as_bad (_("bad string constant"));
3278   ignore_rest_of_line ();
3279   return;
3280 bad:
3281   as_bad (_("expected <tag> , <value>"));
3282   ignore_rest_of_line ();
3283 }
3284
3285 static void s_arm_arch (int);
3286 static void s_arm_cpu (int);
3287 static void s_arm_fpu (int);
3288 #endif /* OBJ_ELF */
3289
3290 /* This table describes all the machine specific pseudo-ops the assembler
3291    has to support.  The fields are:
3292      pseudo-op name without dot
3293      function to call to execute this pseudo-op
3294      Integer arg to pass to the function.  */
3295
3296 const pseudo_typeS md_pseudo_table[] =
3297 {
3298   /* Never called because '.req' does not start a line.  */
3299   { "req",         s_req,         0 },
3300   { "unreq",       s_unreq,       0 },
3301   { "bss",         s_bss,         0 },
3302   { "align",       s_align,       0 },
3303   { "arm",         s_arm,         0 },
3304   { "thumb",       s_thumb,       0 },
3305   { "code",        s_code,        0 },
3306   { "force_thumb", s_force_thumb, 0 },
3307   { "thumb_func",  s_thumb_func,  0 },
3308   { "thumb_set",   s_thumb_set,   0 },
3309   { "even",        s_even,        0 },
3310   { "ltorg",       s_ltorg,       0 },
3311   { "pool",        s_ltorg,       0 },
3312   { "syntax",      s_syntax,      0 },
3313 #ifdef OBJ_ELF
3314   { "word",        s_arm_elf_cons, 4 },
3315   { "long",        s_arm_elf_cons, 4 },
3316   { "rel31",       s_arm_rel31,   0 },
3317   { "fnstart",          s_arm_unwind_fnstart,   0 },
3318   { "fnend",            s_arm_unwind_fnend,     0 },
3319   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
3320   { "personality",      s_arm_unwind_personality, 0 },
3321   { "personalityindex", s_arm_unwind_personalityindex, 0 },
3322   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
3323   { "save",             s_arm_unwind_save,      0 },
3324   { "movsp",            s_arm_unwind_movsp,     0 },
3325   { "pad",              s_arm_unwind_pad,       0 },
3326   { "setfp",            s_arm_unwind_setfp,     0 },
3327   { "unwind_raw",       s_arm_unwind_raw,       0 },
3328   { "cpu",              s_arm_cpu,              0 },
3329   { "arch",             s_arm_arch,             0 },
3330   { "fpu",              s_arm_fpu,              0 },
3331   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
3332 #else
3333   { "word",        cons, 4},
3334 #endif
3335   { "extend",      float_cons, 'x' },
3336   { "ldouble",     float_cons, 'x' },
3337   { "packed",      float_cons, 'p' },
3338   { 0, 0, 0 }
3339 };
3340 \f
3341 /* Parser functions used exclusively in instruction operands.  */
3342
3343 /* Generic immediate-value read function for use in insn parsing.
3344    STR points to the beginning of the immediate (the leading #);
3345    VAL receives the value; if the value is outside [MIN, MAX]
3346    issue an error.  PREFIX_OPT is true if the immediate prefix is
3347    optional.  */
3348
3349 static int
3350 parse_immediate (char **str, int *val, int min, int max,
3351                  bfd_boolean prefix_opt)
3352 {
3353   expressionS exp;
3354   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
3355   if (exp.X_op != O_constant)
3356     {
3357       inst.error = _("constant expression required");
3358       return FAIL;
3359     }
3360
3361   if (exp.X_add_number < min || exp.X_add_number > max)
3362     {
3363       inst.error = _("immediate value out of range");
3364       return FAIL;
3365     }
3366
3367   *val = exp.X_add_number;
3368   return SUCCESS;
3369 }
3370
3371 /* Less-generic immediate-value read function with the possibility of loading a
3372    big (64-bit) immediate, as required by Neon VMOV and VMVN immediate
3373    instructions. Puts the result directly in inst.operands[i].  */
3374
3375 static int
3376 parse_big_immediate (char **str, int i)
3377 {
3378   expressionS exp;
3379   char *ptr = *str;
3380
3381   my_get_expression (&exp, &ptr, GE_OPT_PREFIX_BIG);
3382
3383   if (exp.X_op == O_constant)
3384     inst.operands[i].imm = exp.X_add_number;
3385   else if (exp.X_op == O_big
3386            && LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 32
3387            && LITTLENUM_NUMBER_OF_BITS * exp.X_add_number <= 64)
3388     {
3389       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
3390       /* Bignums have their least significant bits in
3391          generic_bignum[0]. Make sure we put 32 bits in imm and
3392          32 bits in reg,  in a (hopefully) portable way.  */
3393       assert (parts != 0);
3394       inst.operands[i].imm = 0;
3395       for (j = 0; j < parts; j++, idx++)
3396         inst.operands[i].imm |= generic_bignum[idx]
3397                                 << (LITTLENUM_NUMBER_OF_BITS * j);
3398       inst.operands[i].reg = 0;
3399       for (j = 0; j < parts; j++, idx++)
3400         inst.operands[i].reg |= generic_bignum[idx]
3401                                 << (LITTLENUM_NUMBER_OF_BITS * j);
3402       inst.operands[i].regisimm = 1;
3403     }
3404   else
3405     return FAIL;
3406   
3407   *str = ptr;
3408
3409   return SUCCESS;
3410 }
3411
3412 /* Returns the pseudo-register number of an FPA immediate constant,
3413    or FAIL if there isn't a valid constant here.  */
3414
3415 static int
3416 parse_fpa_immediate (char ** str)
3417 {
3418   LITTLENUM_TYPE words[MAX_LITTLENUMS];
3419   char *         save_in;
3420   expressionS    exp;
3421   int            i;
3422   int            j;
3423
3424   /* First try and match exact strings, this is to guarantee
3425      that some formats will work even for cross assembly.  */
3426
3427   for (i = 0; fp_const[i]; i++)
3428     {
3429       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
3430         {
3431           char *start = *str;
3432
3433           *str += strlen (fp_const[i]);
3434           if (is_end_of_line[(unsigned char) **str])
3435             return i + 8;
3436           *str = start;
3437         }
3438     }
3439
3440   /* Just because we didn't get a match doesn't mean that the constant
3441      isn't valid, just that it is in a format that we don't
3442      automatically recognize.  Try parsing it with the standard
3443      expression routines.  */
3444
3445   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
3446
3447   /* Look for a raw floating point number.  */
3448   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
3449       && is_end_of_line[(unsigned char) *save_in])
3450     {
3451       for (i = 0; i < NUM_FLOAT_VALS; i++)
3452         {
3453           for (j = 0; j < MAX_LITTLENUMS; j++)
3454             {
3455               if (words[j] != fp_values[i][j])
3456                 break;
3457             }
3458
3459           if (j == MAX_LITTLENUMS)
3460             {
3461               *str = save_in;
3462               return i + 8;
3463             }
3464         }
3465     }
3466
3467   /* Try and parse a more complex expression, this will probably fail
3468      unless the code uses a floating point prefix (eg "0f").  */
3469   save_in = input_line_pointer;
3470   input_line_pointer = *str;
3471   if (expression (&exp) == absolute_section
3472       && exp.X_op == O_big
3473       && exp.X_add_number < 0)
3474     {
3475       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
3476          Ditto for 15.  */
3477       if (gen_to_words (words, 5, (long) 15) == 0)
3478         {
3479           for (i = 0; i < NUM_FLOAT_VALS; i++)
3480             {
3481               for (j = 0; j < MAX_LITTLENUMS; j++)
3482                 {
3483                   if (words[j] != fp_values[i][j])
3484                     break;
3485                 }
3486
3487               if (j == MAX_LITTLENUMS)
3488                 {
3489                   *str = input_line_pointer;
3490                   input_line_pointer = save_in;
3491                   return i + 8;
3492                 }
3493             }
3494         }
3495     }
3496
3497   *str = input_line_pointer;
3498   input_line_pointer = save_in;
3499   inst.error = _("invalid FPA immediate expression");
3500   return FAIL;
3501 }
3502
3503 /* Shift operands.  */
3504 enum shift_kind
3505 {
3506   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
3507 };
3508
3509 struct asm_shift_name
3510 {
3511   const char      *name;
3512   enum shift_kind  kind;
3513 };
3514
3515 /* Third argument to parse_shift.  */
3516 enum parse_shift_mode
3517 {
3518   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
3519   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
3520   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
3521   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
3522   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
3523 };
3524
3525 /* Parse a <shift> specifier on an ARM data processing instruction.
3526    This has three forms:
3527
3528      (LSL|LSR|ASL|ASR|ROR) Rs
3529      (LSL|LSR|ASL|ASR|ROR) #imm
3530      RRX
3531
3532    Note that ASL is assimilated to LSL in the instruction encoding, and
3533    RRX to ROR #0 (which cannot be written as such).  */
3534
3535 static int
3536 parse_shift (char **str, int i, enum parse_shift_mode mode)
3537 {
3538   const struct asm_shift_name *shift_name;
3539   enum shift_kind shift;
3540   char *s = *str;
3541   char *p = s;
3542   int reg;
3543
3544   for (p = *str; ISALPHA (*p); p++)
3545     ;
3546
3547   if (p == *str)
3548     {
3549       inst.error = _("shift expression expected");
3550       return FAIL;
3551     }
3552
3553   shift_name = hash_find_n (arm_shift_hsh, *str, p - *str);
3554
3555   if (shift_name == NULL)
3556     {
3557       inst.error = _("shift expression expected");
3558       return FAIL;
3559     }
3560
3561   shift = shift_name->kind;
3562
3563   switch (mode)
3564     {
3565     case NO_SHIFT_RESTRICT:
3566     case SHIFT_IMMEDIATE:   break;
3567
3568     case SHIFT_LSL_OR_ASR_IMMEDIATE:
3569       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
3570         {
3571           inst.error = _("'LSL' or 'ASR' required");
3572           return FAIL;
3573         }
3574       break;
3575
3576     case SHIFT_LSL_IMMEDIATE:
3577       if (shift != SHIFT_LSL)
3578         {
3579           inst.error = _("'LSL' required");
3580           return FAIL;
3581         }
3582       break;
3583
3584     case SHIFT_ASR_IMMEDIATE:
3585       if (shift != SHIFT_ASR)
3586         {
3587           inst.error = _("'ASR' required");
3588           return FAIL;
3589         }
3590       break;
3591
3592     default: abort ();
3593     }
3594
3595   if (shift != SHIFT_RRX)
3596     {
3597       /* Whitespace can appear here if the next thing is a bare digit.  */
3598       skip_whitespace (p);
3599
3600       if (mode == NO_SHIFT_RESTRICT
3601           && (reg = arm_reg_parse (&p, REG_TYPE_RN, NULL)) != FAIL)
3602         {
3603           inst.operands[i].imm = reg;
3604           inst.operands[i].immisreg = 1;
3605         }
3606       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3607         return FAIL;
3608     }
3609   inst.operands[i].shift_kind = shift;
3610   inst.operands[i].shifted = 1;
3611   *str = p;
3612   return SUCCESS;
3613 }
3614
3615 /* Parse a <shifter_operand> for an ARM data processing instruction:
3616
3617       #<immediate>
3618       #<immediate>, <rotate>
3619       <Rm>
3620       <Rm>, <shift>
3621
3622    where <shift> is defined by parse_shift above, and <rotate> is a
3623    multiple of 2 between 0 and 30.  Validation of immediate operands
3624    is deferred to md_apply_fix.  */
3625
3626 static int
3627 parse_shifter_operand (char **str, int i)
3628 {
3629   int value;
3630   expressionS expr;
3631
3632   if ((value = arm_reg_parse (str, REG_TYPE_RN, NULL)) != FAIL)
3633     {
3634       inst.operands[i].reg = value;
3635       inst.operands[i].isreg = 1;
3636
3637       /* parse_shift will override this if appropriate */
3638       inst.reloc.exp.X_op = O_constant;
3639       inst.reloc.exp.X_add_number = 0;
3640
3641       if (skip_past_comma (str) == FAIL)
3642         return SUCCESS;
3643
3644       /* Shift operation on register.  */
3645       return parse_shift (str, i, NO_SHIFT_RESTRICT);
3646     }
3647
3648   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
3649     return FAIL;
3650
3651   if (skip_past_comma (str) == SUCCESS)
3652     {
3653       /* #x, y -- ie explicit rotation by Y.  */
3654       if (my_get_expression (&expr, str, GE_NO_PREFIX))
3655         return FAIL;
3656
3657       if (expr.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
3658         {
3659           inst.error = _("constant expression expected");
3660           return FAIL;
3661         }
3662
3663       value = expr.X_add_number;
3664       if (value < 0 || value > 30 || value % 2 != 0)
3665         {
3666           inst.error = _("invalid rotation");
3667           return FAIL;
3668         }
3669       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
3670         {
3671           inst.error = _("invalid constant");
3672           return FAIL;
3673         }
3674
3675       /* Convert to decoded value.  md_apply_fix will put it back.  */
3676       inst.reloc.exp.X_add_number
3677         = (((inst.reloc.exp.X_add_number << (32 - value))
3678             | (inst.reloc.exp.X_add_number >> value)) & 0xffffffff);
3679     }
3680
3681   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
3682   inst.reloc.pc_rel = 0;
3683   return SUCCESS;
3684 }
3685
3686 /* Parse all forms of an ARM address expression.  Information is written
3687    to inst.operands[i] and/or inst.reloc.
3688
3689    Preindexed addressing (.preind=1):
3690
3691    [Rn, #offset]       .reg=Rn .reloc.exp=offset
3692    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3693    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3694                        .shift_kind=shift .reloc.exp=shift_imm
3695
3696    These three may have a trailing ! which causes .writeback to be set also.
3697
3698    Postindexed addressing (.postind=1, .writeback=1):
3699
3700    [Rn], #offset       .reg=Rn .reloc.exp=offset
3701    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3702    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3703                        .shift_kind=shift .reloc.exp=shift_imm
3704
3705    Unindexed addressing (.preind=0, .postind=0):
3706
3707    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
3708
3709    Other:
3710
3711    [Rn]{!}             shorthand for [Rn,#0]{!}
3712    =immediate          .isreg=0 .reloc.exp=immediate
3713    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
3714
3715   It is the caller's responsibility to check for addressing modes not
3716   supported by the instruction, and to set inst.reloc.type.  */
3717
3718 static int
3719 parse_address (char **str, int i)
3720 {
3721   char *p = *str;
3722   int reg;
3723
3724   if (skip_past_char (&p, '[') == FAIL)
3725     {
3726       if (skip_past_char (&p, '=') == FAIL)
3727         {
3728           /* bare address - translate to PC-relative offset */
3729           inst.reloc.pc_rel = 1;
3730           inst.operands[i].reg = REG_PC;
3731           inst.operands[i].isreg = 1;
3732           inst.operands[i].preind = 1;
3733         }
3734       /* else a load-constant pseudo op, no special treatment needed here */
3735
3736       if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
3737         return FAIL;
3738
3739       *str = p;
3740       return SUCCESS;
3741     }
3742
3743   if ((reg = arm_reg_parse (&p, REG_TYPE_RN, NULL)) == FAIL)
3744     {
3745       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3746       return FAIL;
3747     }
3748   inst.operands[i].reg = reg;
3749   inst.operands[i].isreg = 1;
3750
3751   if (skip_past_comma (&p) == SUCCESS)
3752     {
3753       inst.operands[i].preind = 1;
3754
3755       if (*p == '+') p++;
3756       else if (*p == '-') p++, inst.operands[i].negative = 1;
3757
3758       if ((reg = arm_reg_parse (&p, REG_TYPE_RN, NULL)) != FAIL)
3759         {
3760           inst.operands[i].imm = reg;
3761           inst.operands[i].immisreg = 1;
3762
3763           if (skip_past_comma (&p) == SUCCESS)
3764             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
3765               return FAIL;
3766         }
3767       else if (skip_past_char (&p, ':') == SUCCESS)
3768         {
3769           /* FIXME: '@' should be used here, but it's filtered out by generic
3770              code before we get to see it here. This may be subject to
3771              change.  */
3772           expressionS exp;
3773           my_get_expression (&exp, &p, GE_NO_PREFIX);
3774           if (exp.X_op != O_constant)
3775             {
3776               inst.error = _("alignment must be constant");
3777               return FAIL;
3778             }
3779           inst.operands[i].imm = exp.X_add_number << 8;
3780           inst.operands[i].immisalign = 1;
3781           /* Alignments are not pre-indexes.  */
3782           inst.operands[i].preind = 0;
3783         }
3784       else
3785         {
3786           if (inst.operands[i].negative)
3787             {
3788               inst.operands[i].negative = 0;
3789               p--;
3790             }
3791           if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3792             return FAIL;
3793         }
3794     }
3795
3796   if (skip_past_char (&p, ']') == FAIL)
3797     {
3798       inst.error = _("']' expected");
3799       return FAIL;
3800     }
3801
3802   if (skip_past_char (&p, '!') == SUCCESS)
3803     inst.operands[i].writeback = 1;
3804
3805   else if (skip_past_comma (&p) == SUCCESS)
3806     {
3807       if (skip_past_char (&p, '{') == SUCCESS)
3808         {
3809           /* [Rn], {expr} - unindexed, with option */
3810           if (parse_immediate (&p, &inst.operands[i].imm,
3811                                0, 255, TRUE) == FAIL)
3812             return FAIL;
3813
3814           if (skip_past_char (&p, '}') == FAIL)
3815             {
3816               inst.error = _("'}' expected at end of 'option' field");
3817               return FAIL;
3818             }
3819           if (inst.operands[i].preind)
3820             {
3821               inst.error = _("cannot combine index with option");
3822               return FAIL;
3823             }
3824           *str = p;
3825           return SUCCESS;
3826         }
3827       else
3828         {
3829           inst.operands[i].postind = 1;
3830           inst.operands[i].writeback = 1;
3831
3832           if (inst.operands[i].preind)
3833             {
3834               inst.error = _("cannot combine pre- and post-indexing");
3835               return FAIL;
3836             }
3837
3838           if (*p == '+') p++;
3839           else if (*p == '-') p++, inst.operands[i].negative = 1;
3840
3841           if ((reg = arm_reg_parse (&p, REG_TYPE_RN, NULL)) != FAIL)
3842             {
3843               /* We might be using the immediate for alignment already. If we
3844                  are, OR the register number into the low-order bits.  */
3845               if (inst.operands[i].immisalign)
3846                 inst.operands[i].imm |= reg;
3847               else
3848                 inst.operands[i].imm = reg;
3849               inst.operands[i].immisreg = 1;
3850
3851               if (skip_past_comma (&p) == SUCCESS)
3852                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
3853                   return FAIL;
3854             }
3855           else
3856             {
3857               if (inst.operands[i].negative)
3858                 {
3859                   inst.operands[i].negative = 0;
3860                   p--;
3861                 }
3862               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3863                 return FAIL;
3864             }
3865         }
3866     }
3867
3868   /* If at this point neither .preind nor .postind is set, we have a
3869      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
3870   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
3871     {
3872       inst.operands[i].preind = 1;
3873       inst.reloc.exp.X_op = O_constant;
3874       inst.reloc.exp.X_add_number = 0;
3875     }
3876   *str = p;
3877   return SUCCESS;
3878 }
3879
3880 /* Miscellaneous. */
3881
3882 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
3883    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
3884 static int
3885 parse_psr (char **str)
3886 {
3887   char *p;
3888   unsigned long psr_field;
3889   const struct asm_psr *psr;
3890   char *start;
3891
3892   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
3893      feature for ease of use and backwards compatibility.  */
3894   p = *str;
3895   if (strncasecmp (p, "SPSR", 4) == 0)
3896     psr_field = SPSR_BIT;
3897   else if (strncasecmp (p, "CPSR", 4) == 0)
3898     psr_field = 0;
3899   else
3900     {
3901       start = p;
3902       do
3903         p++;
3904       while (ISALNUM (*p) || *p == '_');
3905
3906       psr = hash_find_n (arm_v7m_psr_hsh, start, p - start);
3907       if (!psr)
3908         return FAIL;
3909
3910       *str = p;
3911       return psr->field;
3912     }
3913
3914   p += 4;
3915   if (*p == '_')
3916     {
3917       /* A suffix follows.  */
3918       p++;
3919       start = p;
3920
3921       do
3922         p++;
3923       while (ISALNUM (*p) || *p == '_');
3924
3925       psr = hash_find_n (arm_psr_hsh, start, p - start);
3926       if (!psr)
3927         goto error;
3928
3929       psr_field |= psr->field;
3930     }
3931   else
3932     {
3933       if (ISALNUM (*p))
3934         goto error;    /* Garbage after "[CS]PSR".  */
3935
3936       psr_field |= (PSR_c | PSR_f);
3937     }
3938   *str = p;
3939   return psr_field;
3940
3941  error:
3942   inst.error = _("flag for {c}psr instruction expected");
3943   return FAIL;
3944 }
3945
3946 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
3947    value suitable for splatting into the AIF field of the instruction.  */
3948
3949 static int
3950 parse_cps_flags (char **str)
3951 {
3952   int val = 0;
3953   int saw_a_flag = 0;
3954   char *s = *str;
3955
3956   for (;;)
3957     switch (*s++)
3958       {
3959       case '\0': case ',':
3960         goto done;
3961
3962       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
3963       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
3964       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
3965
3966       default:
3967         inst.error = _("unrecognized CPS flag");
3968         return FAIL;
3969       }
3970
3971  done:
3972   if (saw_a_flag == 0)
3973     {
3974       inst.error = _("missing CPS flags");
3975       return FAIL;
3976     }
3977
3978   *str = s - 1;
3979   return val;
3980 }
3981
3982 /* Parse an endian specifier ("BE" or "LE", case insensitive);
3983    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
3984
3985 static int
3986 parse_endian_specifier (char **str)
3987 {
3988   int little_endian;
3989   char *s = *str;
3990
3991   if (strncasecmp (s, "BE", 2))
3992     little_endian = 0;
3993   else if (strncasecmp (s, "LE", 2))
3994     little_endian = 1;
3995   else
3996     {
3997       inst.error = _("valid endian specifiers are be or le");
3998       return FAIL;
3999     }
4000
4001   if (ISALNUM (s[2]) || s[2] == '_')
4002     {
4003       inst.error = _("valid endian specifiers are be or le");
4004       return FAIL;
4005     }
4006
4007   *str = s + 2;
4008   return little_endian;
4009 }
4010
4011 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
4012    value suitable for poking into the rotate field of an sxt or sxta
4013    instruction, or FAIL on error.  */
4014
4015 static int
4016 parse_ror (char **str)
4017 {
4018   int rot;
4019   char *s = *str;
4020
4021   if (strncasecmp (s, "ROR", 3) == 0)
4022     s += 3;
4023   else
4024     {
4025       inst.error = _("missing rotation field after comma");
4026       return FAIL;
4027     }
4028
4029   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
4030     return FAIL;
4031
4032   switch (rot)
4033     {
4034     case  0: *str = s; return 0x0;
4035     case  8: *str = s; return 0x1;
4036     case 16: *str = s; return 0x2;
4037     case 24: *str = s; return 0x3;
4038
4039     default:
4040       inst.error = _("rotation can only be 0, 8, 16, or 24");
4041       return FAIL;
4042     }
4043 }
4044
4045 /* Parse a conditional code (from conds[] below).  The value returned is in the
4046    range 0 .. 14, or FAIL.  */
4047 static int
4048 parse_cond (char **str)
4049 {
4050   char *p, *q;
4051   const struct asm_cond *c;
4052
4053   p = q = *str;
4054   while (ISALPHA (*q))
4055     q++;
4056
4057   c = hash_find_n (arm_cond_hsh, p, q - p);
4058   if (!c)
4059     {
4060       inst.error = _("condition required");
4061       return FAIL;
4062     }
4063
4064   *str = q;
4065   return c->value;
4066 }
4067
4068 /* Parse an option for a barrier instruction.  Returns the encoding for the
4069    option, or FAIL.  */
4070 static int
4071 parse_barrier (char **str)
4072 {
4073   char *p, *q;
4074   const struct asm_barrier_opt *o;
4075
4076   p = q = *str;
4077   while (ISALPHA (*q))
4078     q++;
4079
4080   o = hash_find_n (arm_barrier_opt_hsh, p, q - p);
4081   if (!o)
4082     return FAIL;
4083
4084   *str = q;
4085   return o->value;
4086 }
4087
4088 /* Parse the operands of a table branch instruction.  Similar to a memory
4089    operand.  */
4090 static int
4091 parse_tb (char **str)
4092 {
4093   char * p = *str;
4094   int reg;
4095
4096   if (skip_past_char (&p, '[') == FAIL)
4097     {
4098       inst.error = _("'[' expected");
4099       return FAIL;
4100     }
4101
4102   if ((reg = arm_reg_parse (&p, REG_TYPE_RN, NULL)) == FAIL)
4103     {
4104       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
4105       return FAIL;
4106     }
4107   inst.operands[0].reg = reg;
4108
4109   if (skip_past_comma (&p) == FAIL)
4110     {
4111       inst.error = _("',' expected");
4112       return FAIL;
4113     }
4114   
4115   if ((reg = arm_reg_parse (&p, REG_TYPE_RN, NULL)) == FAIL)
4116     {
4117       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
4118       return FAIL;
4119     }
4120   inst.operands[0].imm = reg;
4121
4122   if (skip_past_comma (&p) == SUCCESS)
4123     {
4124       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
4125         return FAIL;
4126       if (inst.reloc.exp.X_add_number != 1)
4127         {
4128           inst.error = _("invalid shift");
4129           return FAIL;
4130         }
4131       inst.operands[0].shifted = 1;
4132     }
4133
4134   if (skip_past_char (&p, ']') == FAIL)
4135     {
4136       inst.error = _("']' expected");
4137       return FAIL;
4138     }
4139   *str = p;
4140   return SUCCESS;
4141 }
4142
4143 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
4144    information on the types the operands can take and how they are encoded.
4145    Note particularly the abuse of ".regisimm" to signify a Neon register.
4146    Up to three operands may be read; this function handles setting the
4147    ".present" field for each operand itself.
4148    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
4149    else returns FAIL.  */
4150
4151 static int
4152 parse_neon_mov (char **str, int *which_operand)
4153 {
4154   int i = *which_operand, val;
4155   enum arm_reg_type rtype;
4156   char *ptr = *str;
4157   
4158   if ((val = parse_scalar (&ptr, 8)) != FAIL)
4159     {
4160       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
4161       inst.operands[i].reg = val;
4162       inst.operands[i].isscalar = 1;
4163       inst.operands[i++].present = 1;
4164
4165       if (skip_past_comma (&ptr) == FAIL)
4166         goto wanted_comma;
4167       
4168       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN, NULL)) == FAIL)
4169         goto wanted_arm;
4170       
4171       inst.operands[i].reg = val;
4172       inst.operands[i].isreg = 1;
4173       inst.operands[i].present = 1;
4174     }
4175   else if ((val = arm_reg_parse (&ptr, REG_TYPE_NDQ, &rtype)) != FAIL)
4176     {
4177       /* Cases 0, 1, 2, 3, 5 (D only).  */
4178       if (skip_past_comma (&ptr) == FAIL)
4179         goto wanted_comma;
4180       
4181       inst.operands[i].reg = val;
4182       inst.operands[i].isreg = 1;
4183       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
4184       inst.operands[i++].present = 1;
4185
4186       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN, NULL)) != FAIL)
4187         {
4188           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.  */
4189           inst.operands[i-1].regisimm = 1;
4190           inst.operands[i].reg = val;
4191           inst.operands[i].isreg = 1;
4192           inst.operands[i++].present = 1;
4193
4194           if (rtype == REG_TYPE_NQ)
4195             {
4196               inst.error = _("can't use Neon quad register here");
4197               return FAIL;
4198             }
4199           if (skip_past_comma (&ptr) == FAIL)
4200             goto wanted_comma;
4201           if ((val = arm_reg_parse (&ptr, REG_TYPE_RN, NULL)) == FAIL)
4202             goto wanted_arm;
4203           inst.operands[i].reg = val;
4204           inst.operands[i].isreg = 1;
4205           inst.operands[i].present = 1;
4206         }
4207       else if (parse_big_immediate (&ptr, i) == SUCCESS)
4208         {
4209           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
4210              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
4211           if (!thumb_mode && (inst.instruction & 0xf0000000) != 0xe0000000)
4212             goto bad_cond;
4213         }
4214       else if ((val = arm_reg_parse (&ptr, REG_TYPE_NDQ, &rtype)) != FAIL)
4215         {
4216           /* Case 0: VMOV<c><q> <Qd>, <Qm>
4217              Case 1: VMOV<c><q> <Dd>, <Dm>  */
4218           if (!thumb_mode && (inst.instruction & 0xf0000000) != 0xe0000000)
4219             goto bad_cond;
4220
4221           inst.operands[i].reg = val;
4222           inst.operands[i].isreg = 1;
4223           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
4224           inst.operands[i].present = 1;
4225         }
4226       else
4227         {
4228           inst.error = _("expected <Rm> or <Dm> or <Qm> operand");
4229           return FAIL;
4230         }
4231     }
4232   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN, NULL)) != FAIL)
4233     {
4234       /* Cases 6, 7.  */
4235       inst.operands[i].reg = val;
4236       inst.operands[i].isreg = 1;
4237       inst.operands[i++].present = 1;
4238       
4239       if (skip_past_comma (&ptr) == FAIL)
4240         goto wanted_comma;
4241       
4242       if ((val = parse_scalar (&ptr, 8)) != FAIL)
4243         {
4244           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
4245           inst.operands[i].reg = val;
4246           inst.operands[i].isscalar = 1;
4247           inst.operands[i].present = 1;
4248         }
4249       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN, NULL)) != FAIL)
4250         {
4251           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
4252           inst.operands[i].reg = val;
4253           inst.operands[i].isreg = 1;
4254           inst.operands[i++].present = 1;
4255           
4256           if (skip_past_comma (&ptr) == FAIL)
4257             goto wanted_comma;
4258           
4259           if ((val = arm_reg_parse (&ptr, REG_TYPE_VFD, NULL)) == FAIL)
4260             {
4261               inst.error = _(reg_expected_msgs[REG_TYPE_VFD]);
4262               return FAIL;
4263             }
4264
4265           inst.operands[i].reg = val;
4266           inst.operands[i].isreg = 1;
4267           inst.operands[i].regisimm = 1;
4268           inst.operands[i].present = 1;
4269         }
4270     }
4271   else
4272     {
4273       inst.error = _("parse error");
4274       return FAIL;
4275     }
4276
4277   /* Successfully parsed the operands. Update args.  */
4278   *which_operand = i;
4279   *str = ptr;
4280   return SUCCESS;
4281
4282   wanted_comma:
4283   inst.error = _("expected comma");
4284   return FAIL;
4285   
4286   wanted_arm:
4287   inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
4288   return FAIL;
4289
4290   bad_cond:
4291   inst.error = _("instruction cannot be conditionalized");
4292   return FAIL;
4293 }
4294
4295 /* Matcher codes for parse_operands.  */
4296 enum operand_parse_code
4297 {
4298   OP_stop,      /* end of line */
4299
4300   OP_RR,        /* ARM register */
4301   OP_RRnpc,     /* ARM register, not r15 */
4302   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
4303   OP_RRw,       /* ARM register, not r15, optional trailing ! */
4304   OP_RCP,       /* Coprocessor number */
4305   OP_RCN,       /* Coprocessor register */
4306   OP_RF,        /* FPA register */
4307   OP_RVS,       /* VFP single precision register */
4308   OP_RVD,       /* VFP double precision register (0..15) */
4309   OP_RND,       /* Neon double precision register (0..31) */
4310   OP_RNQ,       /* Neon quad precision register */
4311   OP_RNDQ,      /* Neon double or quad precision register */
4312   OP_RNSC,      /* Neon scalar D[X] */
4313   OP_RVC,       /* VFP control register */
4314   OP_RMF,       /* Maverick F register */
4315   OP_RMD,       /* Maverick D register */
4316   OP_RMFX,      /* Maverick FX register */
4317   OP_RMDX,      /* Maverick DX register */
4318   OP_RMAX,      /* Maverick AX register */
4319   OP_RMDS,      /* Maverick DSPSC register */
4320   OP_RIWR,      /* iWMMXt wR register */
4321   OP_RIWC,      /* iWMMXt wC register */
4322   OP_RIWG,      /* iWMMXt wCG register */
4323   OP_RXA,       /* XScale accumulator register */
4324
4325   OP_REGLST,    /* ARM register list */
4326   OP_VRSLST,    /* VFP single-precision register list */
4327   OP_VRDLST,    /* VFP double-precision register list */
4328   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
4329   OP_NSTRLST,   /* Neon element/structure list */
4330
4331   OP_NILO,      /* Neon immediate/logic operands 2 or 2+3. (VBIC, VORR...)  */
4332   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
4333   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
4334   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
4335   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
4336   OP_VMOV,      /* Neon VMOV operands.  */
4337   OP_RNDQ_IMVNb,/* Neon D or Q reg, or immediate good for VMVN.  */
4338   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
4339
4340   OP_I0,        /* immediate zero */
4341   OP_I7,        /* immediate value 0 .. 7 */
4342   OP_I15,       /*                 0 .. 15 */
4343   OP_I16,       /*                 1 .. 16 */
4344   OP_I16z,      /*                 0 .. 16 */
4345   OP_I31,       /*                 0 .. 31 */
4346   OP_I31w,      /*                 0 .. 31, optional trailing ! */
4347   OP_I32,       /*                 1 .. 32 */
4348   OP_I32z,      /*                 0 .. 32 */
4349   OP_I63,       /*                 0 .. 63 */
4350   OP_I63s,      /*               -64 .. 63 */
4351   OP_I64,       /*                 1 .. 64 */
4352   OP_I64z,      /*                 0 .. 64 */
4353   OP_I255,      /*                 0 .. 255 */
4354   OP_Iffff,     /*                 0 .. 65535 */
4355
4356   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
4357   OP_I7b,       /*                             0 .. 7 */
4358   OP_I15b,      /*                             0 .. 15 */
4359   OP_I31b,      /*                             0 .. 31 */
4360
4361   OP_SH,        /* shifter operand */
4362   OP_ADDR,      /* Memory address expression (any mode) */
4363   OP_EXP,       /* arbitrary expression */
4364   OP_EXPi,      /* same, with optional immediate prefix */
4365   OP_EXPr,      /* same, with optional relocation suffix */
4366
4367   OP_CPSF,      /* CPS flags */
4368   OP_ENDI,      /* Endianness specifier */
4369   OP_PSR,       /* CPSR/SPSR mask for msr */
4370   OP_COND,      /* conditional code */
4371   OP_TB,        /* Table branch.  */
4372
4373   OP_RRnpc_I0,  /* ARM register or literal 0 */
4374   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
4375   OP_RR_EXi,    /* ARM register or expression with imm prefix */
4376   OP_RF_IF,     /* FPA register or immediate */
4377   OP_RIWR_RIWC, /* iWMMXt R or C reg */
4378
4379   /* Optional operands.  */
4380   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
4381   OP_oI31b,      /*                             0 .. 31 */
4382   OP_oI32b,      /*                             1 .. 32 */
4383   OP_oIffffb,    /*                             0 .. 65535 */
4384   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
4385
4386   OP_oRR,        /* ARM register */
4387   OP_oRRnpc,     /* ARM register, not the PC */
4388   OP_oRND,       /* Optional Neon double precision register */
4389   OP_oRNQ,       /* Optional Neon quad precision register */
4390   OP_oRNDQ,      /* Optional Neon double or quad precision register */
4391   OP_oSHll,      /* LSL immediate */
4392   OP_oSHar,      /* ASR immediate */
4393   OP_oSHllar,    /* LSL or ASR immediate */
4394   OP_oROR,       /* ROR 0/8/16/24 */
4395   OP_oBARRIER,   /* Option argument for a barrier instruction.  */
4396
4397   OP_FIRST_OPTIONAL = OP_oI7b
4398 };
4399
4400 /* Generic instruction operand parser.  This does no encoding and no
4401    semantic validation; it merely squirrels values away in the inst
4402    structure.  Returns SUCCESS or FAIL depending on whether the
4403    specified grammar matched.  */
4404 static int
4405 parse_operands (char *str, const unsigned char *pattern)
4406 {
4407   unsigned const char *upat = pattern;
4408   char *backtrack_pos = 0;
4409   const char *backtrack_error = 0;
4410   int i, val, backtrack_index = 0;
4411   enum arm_reg_type rtype;
4412
4413 #define po_char_or_fail(chr) do {               \
4414   if (skip_past_char (&str, chr) == FAIL)       \
4415     goto bad_args;                              \
4416 } while (0)
4417
4418 #define po_reg_or_fail(regtype) do {                    \
4419   val = arm_reg_parse (&str, regtype, &rtype);          \
4420   if (val == FAIL)                                      \
4421     {                                                   \
4422       inst.error = _(reg_expected_msgs[regtype]);       \
4423       goto failure;                                     \
4424     }                                                   \
4425   inst.operands[i].reg = val;                           \
4426   inst.operands[i].isreg = 1;                           \
4427   inst.operands[i].isquad = (rtype == REG_TYPE_NQ);     \
4428 } while (0)
4429
4430 #define po_reg_or_goto(regtype, label) do {             \
4431   val = arm_reg_parse (&str, regtype, &rtype);          \
4432   if (val == FAIL)                                      \
4433     goto label;                                         \
4434                                                         \
4435   inst.operands[i].reg = val;                           \
4436   inst.operands[i].isreg = 1;                           \
4437   inst.operands[i].isquad = (rtype == REG_TYPE_NQ);     \
4438 } while (0)
4439
4440 #define po_imm_or_fail(min, max, popt) do {                     \
4441   if (parse_immediate (&str, &val, min, max, popt) == FAIL)     \
4442     goto failure;                                               \
4443   inst.operands[i].imm = val;                                   \
4444 } while (0)
4445
4446 #define po_scalar_or_goto(elsz, label) do {     \
4447   val = parse_scalar (&str, elsz);              \
4448   if (val == FAIL)                              \
4449     goto label;                                 \
4450   inst.operands[i].reg = val;                   \
4451   inst.operands[i].isscalar = 1;                \
4452 } while (0)
4453
4454 #define po_misc_or_fail(expr) do {              \
4455   if (expr)                                     \
4456     goto failure;                               \
4457 } while (0)
4458
4459   skip_whitespace (str);
4460
4461   for (i = 0; upat[i] != OP_stop; i++)
4462     {
4463       if (upat[i] >= OP_FIRST_OPTIONAL)
4464         {
4465           /* Remember where we are in case we need to backtrack.  */
4466           assert (!backtrack_pos);
4467           backtrack_pos = str;
4468           backtrack_error = inst.error;
4469           backtrack_index = i;
4470         }
4471
4472       if (i > 0)
4473         po_char_or_fail (',');
4474
4475       switch (upat[i])
4476         {
4477           /* Registers */
4478         case OP_oRRnpc:
4479         case OP_RRnpc:
4480         case OP_oRR:
4481         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
4482         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
4483         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
4484         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
4485         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
4486         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
4487         case OP_oRND:
4488         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
4489         case OP_RVC:   po_reg_or_fail (REG_TYPE_VFC);     break;
4490         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
4491         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
4492         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
4493         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
4494         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
4495         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
4496         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
4497         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
4498         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
4499         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
4500         case OP_oRNQ:
4501         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
4502         case OP_oRNDQ:
4503         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
4504
4505         /* Neon scalar. Using an element size of 8 means that some invalid
4506            scalars are accepted here, so deal with those in later code.  */
4507         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
4508
4509         /* WARNING: We can expand to two operands here. This has the potential
4510            to totally confuse the backtracking mechanism! It will be OK at
4511            least as long as we don't try to use optional args as well,
4512            though.  */
4513         case OP_NILO:
4514           {
4515             po_reg_or_goto (REG_TYPE_NDQ, try_imm);
4516             i++;
4517             skip_past_comma (&str);
4518             po_reg_or_goto (REG_TYPE_NDQ, one_reg_only);
4519             break;
4520             one_reg_only:
4521             /* Optional register operand was omitted. Unfortunately, it's in
4522                operands[i-1] and we need it to be in inst.operands[i]. Fix that
4523                here (this is a bit grotty).  */
4524             inst.operands[i] = inst.operands[i-1];
4525             inst.operands[i-1].present = 0;
4526             break;
4527             try_imm:
4528             /* Immediate gets verified properly later, so accept any now.  */
4529             po_imm_or_fail (INT_MIN, INT_MAX, TRUE);
4530           }
4531           break;
4532
4533         case OP_RNDQ_I0:
4534           {
4535             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
4536             break;
4537             try_imm0:
4538             po_imm_or_fail (0, 0, TRUE);
4539           }
4540           break;
4541
4542         case OP_RR_RNSC:
4543           {
4544             po_scalar_or_goto (8, try_rr);
4545             break;
4546             try_rr:
4547             po_reg_or_fail (REG_TYPE_RN);
4548           }
4549           break;
4550
4551         case OP_RNDQ_RNSC:
4552           {
4553             po_scalar_or_goto (8, try_ndq);
4554             break;
4555             try_ndq:
4556             po_reg_or_fail (REG_TYPE_NDQ);
4557           }
4558           break;
4559
4560         case OP_RND_RNSC:
4561           {
4562             po_scalar_or_goto (8, try_vfd);
4563             break;
4564             try_vfd:
4565             po_reg_or_fail (REG_TYPE_VFD);
4566           }
4567           break;
4568
4569         case OP_VMOV:
4570           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
4571              not careful then bad things might happen.  */
4572           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
4573           break;
4574
4575         case OP_RNDQ_IMVNb:
4576           {
4577             po_reg_or_goto (REG_TYPE_NDQ, try_mvnimm);
4578             break;
4579             try_mvnimm:
4580             /* There's a possibility of getting a 64-bit immediate here, so
4581                we need special handling.  */
4582             if (parse_big_immediate (&str, i) == FAIL)
4583               {
4584                 inst.error = _("immediate value is out of range");
4585                 goto failure;
4586               }
4587           }
4588           break;
4589
4590         case OP_RNDQ_I63b:
4591           {
4592             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
4593             break;
4594             try_shimm:
4595             po_imm_or_fail (0, 63, TRUE);
4596           }
4597           break;
4598
4599         case OP_RRnpcb:
4600           po_char_or_fail ('[');
4601           po_reg_or_fail  (REG_TYPE_RN);
4602           po_char_or_fail (']');
4603           break;
4604
4605         case OP_RRw:
4606           po_reg_or_fail (REG_TYPE_RN);
4607           if (skip_past_char (&str, '!') == SUCCESS)
4608             inst.operands[i].writeback = 1;
4609           break;
4610
4611           /* Immediates */
4612         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
4613         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
4614         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
4615         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
4616         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
4617         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
4618         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
4619         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
4620         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
4621         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
4622         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
4623         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
4624         case OP_Iffff:   po_imm_or_fail (  0, 0xffff, FALSE);   break;
4625
4626         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
4627         case OP_oI7b:
4628         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
4629         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
4630         case OP_oI31b:
4631         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
4632         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
4633         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
4634
4635           /* Immediate variants */
4636         case OP_oI255c:
4637           po_char_or_fail ('{');
4638           po_imm_or_fail (0, 255, TRUE);
4639           po_char_or_fail ('}');
4640           break;
4641
4642         case OP_I31w:
4643           /* The expression parser chokes on a trailing !, so we have
4644              to find it first and zap it.  */
4645           {
4646             char *s = str;
4647             while (*s && *s != ',')
4648               s++;
4649             if (s[-1] == '!')
4650               {
4651                 s[-1] = '\0';
4652                 inst.operands[i].writeback = 1;
4653               }
4654             po_imm_or_fail (0, 31, TRUE);
4655             if (str == s - 1)
4656               str = s;
4657           }
4658           break;
4659
4660           /* Expressions */
4661         case OP_EXPi:   EXPi:
4662           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
4663                                               GE_OPT_PREFIX));
4664           break;
4665
4666         case OP_EXP:
4667           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
4668                                               GE_NO_PREFIX));
4669           break;
4670
4671         case OP_EXPr:   EXPr:
4672           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
4673                                               GE_NO_PREFIX));
4674           if (inst.reloc.exp.X_op == O_symbol)
4675             {
4676               val = parse_reloc (&str);
4677               if (val == -1)
4678                 {
4679                   inst.error = _("unrecognized relocation suffix");
4680                   goto failure;
4681                 }
4682               else if (val != BFD_RELOC_UNUSED)
4683                 {
4684                   inst.operands[i].imm = val;
4685                   inst.operands[i].hasreloc = 1;
4686                 }
4687             }
4688           break;
4689
4690           /* Register or expression */
4691         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
4692         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
4693
4694           /* Register or immediate */
4695         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
4696         I0:               po_imm_or_fail (0, 0, FALSE);       break;
4697
4698         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
4699         IF:
4700           if (!is_immediate_prefix (*str))
4701             goto bad_args;
4702           str++;
4703           val = parse_fpa_immediate (&str);
4704           if (val == FAIL)
4705             goto failure;
4706           /* FPA immediates are encoded as registers 8-15.
4707              parse_fpa_immediate has already applied the offset.  */
4708           inst.operands[i].reg = val;
4709           inst.operands[i].isreg = 1;
4710           break;
4711
4712           /* Two kinds of register */
4713         case OP_RIWR_RIWC:
4714           {
4715             struct reg_entry *rege = arm_reg_parse_multi (&str);
4716             if (rege->type != REG_TYPE_MMXWR
4717                 && rege->type != REG_TYPE_MMXWC
4718                 && rege->type != REG_TYPE_MMXWCG)
4719               {
4720                 inst.error = _("iWMMXt data or control register expected");
4721                 goto failure;
4722               }
4723             inst.operands[i].reg = rege->number;
4724             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
4725           }
4726           break;
4727
4728           /* Misc */
4729         case OP_CPSF:    val = parse_cps_flags (&str);          break;
4730         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
4731         case OP_oROR:    val = parse_ror (&str);                break;
4732         case OP_PSR:     val = parse_psr (&str);                break;
4733         case OP_COND:    val = parse_cond (&str);               break;
4734         case OP_oBARRIER:val = parse_barrier (&str);            break;
4735
4736         case OP_TB:
4737           po_misc_or_fail (parse_tb (&str));
4738           break;
4739
4740           /* Register lists */
4741         case OP_REGLST:
4742           val = parse_reg_list (&str);
4743           if (*str == '^')
4744             {
4745               inst.operands[1].writeback = 1;
4746               str++;
4747             }
4748           break;
4749
4750         case OP_VRSLST:
4751           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
4752           break;
4753
4754         case OP_VRDLST:
4755           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
4756           break;
4757
4758         case OP_NRDLST:
4759           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
4760                                     REGLIST_NEON_D);
4761           break;
4762
4763         case OP_NSTRLST:
4764           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg);
4765           break;
4766
4767           /* Addressing modes */
4768         case OP_ADDR:
4769           po_misc_or_fail (parse_address (&str, i));
4770           break;
4771
4772         case OP_SH:
4773           po_misc_or_fail (parse_shifter_operand (&str, i));
4774           break;
4775
4776         case OP_oSHll:
4777           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
4778           break;
4779
4780         case OP_oSHar:
4781           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
4782           break;
4783
4784         case OP_oSHllar:
4785           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
4786           break;
4787
4788         default:
4789           as_fatal ("unhandled operand code %d", upat[i]);
4790         }
4791
4792       /* Various value-based sanity checks and shared operations.  We
4793          do not signal immediate failures for the register constraints;
4794          this allows a syntax error to take precedence.  */
4795       switch (upat[i])
4796         {
4797         case OP_oRRnpc:
4798         case OP_RRnpc:
4799         case OP_RRnpcb:
4800         case OP_RRw:
4801         case OP_RRnpc_I0:
4802           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
4803             inst.error = BAD_PC;
4804           break;
4805
4806         case OP_CPSF:
4807         case OP_ENDI:
4808         case OP_oROR:
4809         case OP_PSR:
4810         case OP_COND:
4811         case OP_oBARRIER:
4812         case OP_REGLST:
4813         case OP_VRSLST:
4814         case OP_VRDLST:
4815         case OP_NRDLST:
4816         case OP_NSTRLST:
4817           if (val == FAIL)
4818             goto failure;
4819           inst.operands[i].imm = val;
4820           break;
4821
4822         default:
4823           break;
4824         }
4825
4826       /* If we get here, this operand was successfully parsed.  */
4827       inst.operands[i].present = 1;
4828       continue;
4829
4830     bad_args:
4831       inst.error = BAD_ARGS;
4832
4833     failure:
4834       if (!backtrack_pos)
4835         {
4836           /* The parse routine should already have set inst.error, but set a
4837              defaut here just in case.  */
4838           if (!inst.error)
4839             inst.error = _("syntax error");
4840           return FAIL;
4841         }
4842
4843       /* Do not backtrack over a trailing optional argument that
4844          absorbed some text.  We will only fail again, with the
4845          'garbage following instruction' error message, which is
4846          probably less helpful than the current one.  */
4847       if (backtrack_index == i && backtrack_pos != str
4848           && upat[i+1] == OP_stop)
4849         {
4850           if (!inst.error)
4851             inst.error = _("syntax error");
4852           return FAIL;
4853         }
4854
4855       /* Try again, skipping the optional argument at backtrack_pos.  */
4856       str = backtrack_pos;
4857       inst.error = backtrack_error;
4858       inst.operands[backtrack_index].present = 0;
4859       i = backtrack_index;
4860       backtrack_pos = 0;
4861     }
4862
4863   /* Check that we have parsed all the arguments.  */
4864   if (*str != '\0' && !inst.error)
4865     inst.error = _("garbage following instruction");
4866
4867   return inst.error ? FAIL : SUCCESS;
4868 }
4869
4870 #undef po_char_or_fail
4871 #undef po_reg_or_fail
4872 #undef po_reg_or_goto
4873 #undef po_imm_or_fail
4874 #undef po_scalar_or_fail
4875 \f
4876 /* Shorthand macro for instruction encoding functions issuing errors.  */
4877 #define constraint(expr, err) do {              \
4878   if (expr)                                     \
4879     {                                           \
4880       inst.error = err;                         \
4881       return;                                   \
4882     }                                           \
4883 } while (0)
4884
4885 /* Functions for operand encoding.  ARM, then Thumb.  */
4886
4887 #define rotate_left(v, n) (v << n | v >> (32 - n))
4888
4889 /* If VAL can be encoded in the immediate field of an ARM instruction,
4890    return the encoded form.  Otherwise, return FAIL.  */
4891
4892 static unsigned int
4893 encode_arm_immediate (unsigned int val)
4894 {
4895   unsigned int a, i;
4896
4897   for (i = 0; i < 32; i += 2)
4898     if ((a = rotate_left (val, i)) <= 0xff)
4899       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
4900
4901   return FAIL;
4902 }
4903
4904 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
4905    return the encoded form.  Otherwise, return FAIL.  */
4906 static unsigned int
4907 encode_thumb32_immediate (unsigned int val)
4908 {
4909   unsigned int a, i;
4910
4911   if (val <= 0xff)
4912     return val;
4913
4914   for (i = 1; i <= 24; i++)
4915     {
4916       a = val >> i;
4917       if ((val & ~(0xff << i)) == 0)
4918         return ((val >> i) & 0x7f) | ((32 - i) << 7);
4919     }
4920
4921   a = val & 0xff;
4922   if (val == ((a << 16) | a))
4923     return 0x100 | a;
4924   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
4925     return 0x300 | a;
4926
4927   a = val & 0xff00;
4928   if (val == ((a << 16) | a))
4929     return 0x200 | (a >> 8);
4930
4931   return FAIL;
4932 }
4933 /* Encode a VFP SP or DP register number into inst.instruction.  */
4934
4935 static void
4936 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
4937 {
4938   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
4939       && reg > 15)
4940     {
4941       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
4942         {
4943           if (thumb_mode)
4944             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
4945                                     fpu_vfp_ext_v3);
4946           else
4947             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
4948                                     fpu_vfp_ext_v3);
4949         }
4950       else
4951         {
4952           inst.error = _("D register out of range for selected VFP version");
4953           return;
4954         }
4955     }
4956
4957   switch (pos)
4958     {
4959     case VFP_REG_Sd:
4960       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
4961       break;
4962
4963     case VFP_REG_Sn:
4964       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
4965       break;
4966
4967     case VFP_REG_Sm:
4968       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
4969       break;
4970
4971     case VFP_REG_Dd:
4972       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
4973       break;
4974     
4975     case VFP_REG_Dn:
4976       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
4977       break;
4978     
4979     case VFP_REG_Dm:
4980       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
4981       break;
4982
4983     default:
4984       abort ();
4985     }
4986 }
4987
4988 /* Encode a <shift> in an ARM-format instruction.  The immediate,
4989    if any, is handled by md_apply_fix.   */
4990 static void
4991 encode_arm_shift (int i)
4992 {
4993   if (inst.operands[i].shift_kind == SHIFT_RRX)
4994     inst.instruction |= SHIFT_ROR << 5;
4995   else
4996     {
4997       inst.instruction |= inst.operands[i].shift_kind << 5;
4998       if (inst.operands[i].immisreg)
4999         {
5000           inst.instruction |= SHIFT_BY_REG;
5001           inst.instruction |= inst.operands[i].imm << 8;
5002         }
5003       else
5004         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
5005     }
5006 }
5007
5008 static void
5009 encode_arm_shifter_operand (int i)
5010 {
5011   if (inst.operands[i].isreg)
5012     {
5013       inst.instruction |= inst.operands[i].reg;
5014       encode_arm_shift (i);
5015     }
5016   else
5017     inst.instruction |= INST_IMMEDIATE;
5018 }
5019
5020 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
5021 static void
5022 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
5023 {
5024   assert (inst.operands[i].isreg);
5025   inst.instruction |= inst.operands[i].reg << 16;
5026
5027   if (inst.operands[i].preind)
5028     {
5029       if (is_t)
5030         {
5031           inst.error = _("instruction does not accept preindexed addressing");
5032           return;
5033         }
5034       inst.instruction |= PRE_INDEX;
5035       if (inst.operands[i].writeback)
5036         inst.instruction |= WRITE_BACK;
5037
5038     }
5039   else if (inst.operands[i].postind)
5040     {
5041       assert (inst.operands[i].writeback);
5042       if (is_t)
5043         inst.instruction |= WRITE_BACK;
5044     }
5045   else /* unindexed - only for coprocessor */
5046     {
5047       inst.error = _("instruction does not accept unindexed addressing");
5048       return;
5049     }
5050
5051   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
5052       && (((inst.instruction & 0x000f0000) >> 16)
5053           == ((inst.instruction & 0x0000f000) >> 12)))
5054     as_warn ((inst.instruction & LOAD_BIT)
5055              ? _("destination register same as write-back base")
5056              : _("source register same as write-back base"));
5057 }
5058
5059 /* inst.operands[i] was set up by parse_address.  Encode it into an
5060    ARM-format mode 2 load or store instruction.  If is_t is true,
5061    reject forms that cannot be used with a T instruction (i.e. not
5062    post-indexed).  */
5063 static void
5064 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
5065 {
5066   encode_arm_addr_mode_common (i, is_t);
5067
5068   if (inst.operands[i].immisreg)
5069     {
5070       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
5071       inst.instruction |= inst.operands[i].imm;
5072       if (!inst.operands[i].negative)
5073         inst.instruction |= INDEX_UP;
5074       if (inst.operands[i].shifted)
5075         {
5076           if (inst.operands[i].shift_kind == SHIFT_RRX)
5077             inst.instruction |= SHIFT_ROR << 5;
5078           else
5079             {
5080               inst.instruction |= inst.operands[i].shift_kind << 5;
5081               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
5082             }
5083         }
5084     }
5085   else /* immediate offset in inst.reloc */
5086     {
5087       if (inst.reloc.type == BFD_RELOC_UNUSED)
5088         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
5089     }
5090 }
5091
5092 /* inst.operands[i] was set up by parse_address.  Encode it into an
5093    ARM-format mode 3 load or store instruction.  Reject forms that
5094    cannot be used with such instructions.  If is_t is true, reject
5095    forms that cannot be used with a T instruction (i.e. not
5096    post-indexed).  */
5097 static void
5098 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
5099 {
5100   if (inst.operands[i].immisreg && inst.operands[i].shifted)
5101     {
5102       inst.error = _("instruction does not accept scaled register index");
5103       return;
5104     }
5105
5106   encode_arm_addr_mode_common (i, is_t);
5107
5108   if (inst.operands[i].immisreg)
5109     {
5110       inst.instruction |= inst.operands[i].imm;
5111       if (!inst.operands[i].negative)
5112         inst.instruction |= INDEX_UP;
5113     }
5114   else /* immediate offset in inst.reloc */
5115     {
5116       inst.instruction |= HWOFFSET_IMM;
5117       if (inst.reloc.type == BFD_RELOC_UNUSED)
5118         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
5119     }
5120 }
5121
5122 /* inst.operands[i] was set up by parse_address.  Encode it into an
5123    ARM-format instruction.  Reject all forms which cannot be encoded
5124    into a coprocessor load/store instruction.  If wb_ok is false,
5125    reject use of writeback; if unind_ok is false, reject use of
5126    unindexed addressing.  If reloc_override is not 0, use it instead
5127    of BFD_ARM_CP_OFF_IMM.  */
5128
5129 static int
5130 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
5131 {
5132   inst.instruction |= inst.operands[i].reg << 16;
5133
5134   assert (!(inst.operands[i].preind && inst.operands[i].postind));
5135
5136   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
5137     {
5138       assert (!inst.operands[i].writeback);
5139       if (!unind_ok)
5140         {
5141           inst.error = _("instruction does not support unindexed addressing");
5142           return FAIL;
5143         }
5144       inst.instruction |= inst.operands[i].imm;
5145       inst.instruction |= INDEX_UP;
5146       return SUCCESS;
5147     }
5148
5149   if (inst.operands[i].preind)
5150     inst.instruction |= PRE_INDEX;
5151
5152   if (inst.operands[i].writeback)
5153     {
5154       if (inst.operands[i].reg == REG_PC)
5155         {
5156           inst.error = _("pc may not be used with write-back");
5157           return FAIL;
5158         }
5159       if (!wb_ok)
5160         {
5161           inst.error = _("instruction does not support writeback");
5162           return FAIL;
5163         }
5164       inst.instruction |= WRITE_BACK;
5165     }
5166
5167   if (reloc_override)
5168     inst.reloc.type = reloc_override;
5169   else if (thumb_mode)
5170     inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
5171   else
5172     inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
5173   return SUCCESS;
5174 }
5175
5176 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
5177    Determine whether it can be performed with a move instruction; if
5178    it can, convert inst.instruction to that move instruction and
5179    return 1; if it can't, convert inst.instruction to a literal-pool
5180    load and return 0.  If this is not a valid thing to do in the
5181    current context, set inst.error and return 1.
5182
5183    inst.operands[i] describes the destination register.  */
5184
5185 static int
5186 move_or_literal_pool (int i, bfd_boolean thumb_p, bfd_boolean mode_3)
5187 {
5188   unsigned long tbit;
5189
5190   if (thumb_p)
5191     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
5192   else
5193     tbit = LOAD_BIT;
5194
5195   if ((inst.instruction & tbit) == 0)
5196     {
5197       inst.error = _("invalid pseudo operation");
5198       return 1;
5199     }
5200   if (inst.reloc.exp.X_op != O_constant && inst.reloc.exp.X_op != O_symbol)
5201     {
5202       inst.error = _("constant expression expected");
5203       return 1;
5204     }
5205   if (inst.reloc.exp.X_op == O_constant)
5206     {
5207       if (thumb_p)
5208         {
5209           if (!unified_syntax && (inst.reloc.exp.X_add_number & ~0xFF) == 0)
5210             {
5211               /* This can be done with a mov(1) instruction.  */
5212               inst.instruction  = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
5213               inst.instruction |= inst.reloc.exp.X_add_number;
5214               return 1;
5215             }
5216         }
5217       else
5218         {
5219           int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
5220           if (value != FAIL)
5221             {
5222               /* This can be done with a mov instruction.  */
5223               inst.instruction &= LITERAL_MASK;
5224               inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
5225               inst.instruction |= value & 0xfff;
5226               return 1;
5227             }
5228
5229           value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
5230           if (value != FAIL)
5231             {
5232               /* This can be done with a mvn instruction.  */
5233               inst.instruction &= LITERAL_MASK;
5234               inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
5235               inst.instruction |= value & 0xfff;
5236               return 1;
5237             }
5238         }
5239     }
5240
5241   if (add_to_lit_pool () == FAIL)
5242     {
5243       inst.error = _("literal pool insertion failed");
5244       return 1;
5245     }
5246   inst.operands[1].reg = REG_PC;
5247   inst.operands[1].isreg = 1;
5248   inst.operands[1].preind = 1;
5249   inst.reloc.pc_rel = 1;
5250   inst.reloc.type = (thumb_p
5251                      ? BFD_RELOC_ARM_THUMB_OFFSET
5252                      : (mode_3
5253                         ? BFD_RELOC_ARM_HWLITERAL
5254                         : BFD_RELOC_ARM_LITERAL));
5255   return 0;
5256 }
5257
5258 /* Functions for instruction encoding, sorted by subarchitecture.
5259    First some generics; their names are taken from the conventional
5260    bit positions for register arguments in ARM format instructions.  */
5261
5262 static void
5263 do_noargs (void)
5264 {
5265 }
5266
5267 static void
5268 do_rd (void)
5269 {
5270   inst.instruction |= inst.operands[0].reg << 12;
5271 }
5272
5273 static void
5274 do_rd_rm (void)
5275 {
5276   inst.instruction |= inst.operands[0].reg << 12;
5277   inst.instruction |= inst.operands[1].reg;
5278 }
5279
5280 static void
5281 do_rd_rn (void)
5282 {
5283   inst.instruction |= inst.operands[0].reg << 12;
5284   inst.instruction |= inst.operands[1].reg << 16;
5285 }
5286
5287 static void
5288 do_rn_rd (void)
5289 {
5290   inst.instruction |= inst.operands[0].reg << 16;
5291   inst.instruction |= inst.operands[1].reg << 12;
5292 }
5293
5294 static void
5295 do_rd_rm_rn (void)
5296 {
5297   unsigned Rn = inst.operands[2].reg;
5298   /* Enforce resutrictions on SWP instruction.  */
5299   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
5300     constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
5301                 _("Rn must not overlap other operands"));
5302   inst.instruction |= inst.operands[0].reg << 12;
5303   inst.instruction |= inst.operands[1].reg;
5304   inst.instruction |= Rn << 16;
5305 }
5306
5307 static void
5308 do_rd_rn_rm (void)
5309 {
5310   inst.instruction |= inst.operands[0].reg << 12;
5311   inst.instruction |= inst.operands[1].reg << 16;
5312   inst.instruction |= inst.operands[2].reg;
5313 }
5314
5315 static void
5316 do_rm_rd_rn (void)
5317 {
5318   inst.instruction |= inst.operands[0].reg;
5319   inst.instruction |= inst.operands[1].reg << 12;
5320   inst.instruction |= inst.operands[2].reg << 16;
5321 }
5322
5323 static void
5324 do_imm0 (void)
5325 {
5326   inst.instruction |= inst.operands[0].imm;
5327 }
5328
5329 static void
5330 do_rd_cpaddr (void)
5331 {
5332   inst.instruction |= inst.operands[0].reg << 12;
5333   encode_arm_cp_address (1, TRUE, TRUE, 0);
5334 }
5335
5336 /* ARM instructions, in alphabetical order by function name (except
5337    that wrapper functions appear immediately after the function they
5338    wrap).  */
5339
5340 /* This is a pseudo-op of the form "adr rd, label" to be converted
5341    into a relative address of the form "add rd, pc, #label-.-8".  */
5342
5343 static void
5344 do_adr (void)
5345 {
5346   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
5347
5348   /* Frag hacking will turn this into a sub instruction if the offset turns
5349      out to be negative.  */
5350   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5351   inst.reloc.pc_rel = 1;
5352   inst.reloc.exp.X_add_number -= 8;
5353 }
5354
5355 /* This is a pseudo-op of the form "adrl rd, label" to be converted
5356    into a relative address of the form:
5357    add rd, pc, #low(label-.-8)"
5358    add rd, rd, #high(label-.-8)"  */
5359
5360 static void
5361 do_adrl (void)
5362 {
5363   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
5364
5365   /* Frag hacking will turn this into a sub instruction if the offset turns
5366      out to be negative.  */
5367   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
5368   inst.reloc.pc_rel            = 1;
5369   inst.size                    = INSN_SIZE * 2;
5370   inst.reloc.exp.X_add_number -= 8;
5371 }
5372
5373 static void
5374 do_arit (void)
5375 {
5376   if (!inst.operands[1].present)
5377     inst.operands[1].reg = inst.operands[0].reg;
5378   inst.instruction |= inst.operands[0].reg << 12;
5379   inst.instruction |= inst.operands[1].reg << 16;
5380   encode_arm_shifter_operand (2);
5381 }
5382
5383 static void
5384 do_barrier (void)
5385 {
5386   if (inst.operands[0].present)
5387     {
5388       constraint ((inst.instruction & 0xf0) != 0x40
5389                   && inst.operands[0].imm != 0xf,
5390                   "bad barrier type");
5391       inst.instruction |= inst.operands[0].imm;
5392     }
5393   else
5394     inst.instruction |= 0xf;
5395 }
5396
5397 static void
5398 do_bfc (void)
5399 {
5400   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
5401   constraint (msb > 32, _("bit-field extends past end of register"));
5402   /* The instruction encoding stores the LSB and MSB,
5403      not the LSB and width.  */
5404   inst.instruction |= inst.operands[0].reg << 12;
5405   inst.instruction |= inst.operands[1].imm << 7;
5406   inst.instruction |= (msb - 1) << 16;
5407 }
5408
5409 static void
5410 do_bfi (void)
5411 {
5412   unsigned int msb;
5413
5414   /* #0 in second position is alternative syntax for bfc, which is
5415      the same instruction but with REG_PC in the Rm field.  */
5416   if (!inst.operands[1].isreg)
5417     inst.operands[1].reg = REG_PC;
5418
5419   msb = inst.operands[2].imm + inst.operands[3].imm;
5420   constraint (msb > 32, _("bit-field extends past end of register"));
5421   /* The instruction encoding stores the LSB and MSB,
5422      not the LSB and width.  */
5423   inst.instruction |= inst.operands[0].reg << 12;
5424   inst.instruction |= inst.operands[1].reg;
5425   inst.instruction |= inst.operands[2].imm << 7;
5426   inst.instruction |= (msb - 1) << 16;
5427 }
5428
5429 static void
5430 do_bfx (void)
5431 {
5432   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
5433               _("bit-field extends past end of register"));
5434   inst.instruction |= inst.operands[0].reg << 12;
5435   inst.instruction |= inst.operands[1].reg;
5436   inst.instruction |= inst.operands[2].imm << 7;
5437   inst.instruction |= (inst.operands[3].imm - 1) << 16;
5438 }
5439
5440 /* ARM V5 breakpoint instruction (argument parse)
5441      BKPT <16 bit unsigned immediate>
5442      Instruction is not conditional.
5443         The bit pattern given in insns[] has the COND_ALWAYS condition,
5444         and it is an error if the caller tried to override that.  */
5445
5446 static void
5447 do_bkpt (void)
5448 {
5449   /* Top 12 of 16 bits to bits 19:8.  */
5450   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
5451
5452   /* Bottom 4 of 16 bits to bits 3:0.  */
5453   inst.instruction |= inst.operands[0].imm & 0xf;
5454 }
5455
5456 static void
5457 encode_branch (int default_reloc)
5458 {
5459   if (inst.operands[0].hasreloc)
5460     {
5461       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32,
5462                   _("the only suffix valid here is '(plt)'"));
5463       inst.reloc.type   = BFD_RELOC_ARM_PLT32;
5464     }
5465   else
5466     {
5467       inst.reloc.type = default_reloc;
5468     }
5469   inst.reloc.pc_rel = 1;
5470 }
5471
5472 static void
5473 do_branch (void)
5474 {
5475 #ifdef OBJ_ELF
5476   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
5477     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
5478   else
5479 #endif
5480     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
5481 }
5482
5483 static void
5484 do_bl (void)
5485 {
5486 #ifdef OBJ_ELF
5487   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
5488     {
5489       if (inst.cond == COND_ALWAYS)
5490         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
5491       else
5492         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
5493     }
5494   else
5495 #endif
5496     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
5497 }
5498
5499 /* ARM V5 branch-link-exchange instruction (argument parse)
5500      BLX <target_addr>          ie BLX(1)
5501      BLX{<condition>} <Rm>      ie BLX(2)
5502    Unfortunately, there are two different opcodes for this mnemonic.
5503    So, the insns[].value is not used, and the code here zaps values
5504         into inst.instruction.
5505    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
5506
5507 static void
5508 do_blx (void)
5509 {
5510   if (inst.operands[0].isreg)
5511     {
5512       /* Arg is a register; the opcode provided by insns[] is correct.
5513          It is not illegal to do "blx pc", just useless.  */
5514       if (inst.operands[0].reg == REG_PC)
5515         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
5516
5517       inst.instruction |= inst.operands[0].reg;
5518     }
5519   else
5520     {
5521       /* Arg is an address; this instruction cannot be executed
5522          conditionally, and the opcode must be adjusted.  */
5523       constraint (inst.cond != COND_ALWAYS, BAD_COND);
5524       inst.instruction = 0xfa000000;
5525 #ifdef OBJ_ELF
5526       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
5527         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
5528       else
5529 #endif
5530         encode_branch (BFD_RELOC_ARM_PCREL_BLX);
5531     }
5532 }
5533
5534 static void
5535 do_bx (void)
5536 {
5537   if (inst.operands[0].reg == REG_PC)
5538     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
5539
5540   inst.instruction |= inst.operands[0].reg;
5541 }
5542
5543
5544 /* ARM v5TEJ.  Jump to Jazelle code.  */
5545
5546 static void
5547 do_bxj (void)
5548 {
5549   if (inst.operands[0].reg == REG_PC)
5550     as_tsktsk (_("use of r15 in bxj is not really useful"));
5551
5552   inst.instruction |= inst.operands[0].reg;
5553 }
5554
5555 /* Co-processor data operation:
5556       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
5557       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
5558 static void
5559 do_cdp (void)
5560 {
5561   inst.instruction |= inst.operands[0].reg << 8;
5562   inst.instruction |= inst.operands[1].imm << 20;
5563   inst.instruction |= inst.operands[2].reg << 12;
5564   inst.instruction |= inst.operands[3].reg << 16;
5565   inst.instruction |= inst.operands[4].reg;
5566   inst.instruction |= inst.operands[5].imm << 5;
5567 }
5568
5569 static void
5570 do_cmp (void)
5571 {
5572   inst.instruction |= inst.operands[0].reg << 16;
5573   encode_arm_shifter_operand (1);
5574 }
5575
5576 /* Transfer between coprocessor and ARM registers.
5577    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
5578    MRC2
5579    MCR{cond}
5580    MCR2
5581
5582    No special properties.  */
5583
5584 static void
5585 do_co_reg (void)
5586 {
5587   inst.instruction |= inst.operands[0].reg << 8;
5588   inst.instruction |= inst.operands[1].imm << 21;
5589   inst.instruction |= inst.operands[2].reg << 12;
5590   inst.instruction |= inst.operands[3].reg << 16;
5591   inst.instruction |= inst.operands[4].reg;
5592   inst.instruction |= inst.operands[5].imm << 5;
5593 }
5594
5595 /* Transfer between coprocessor register and pair of ARM registers.
5596    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
5597    MCRR2
5598    MRRC{cond}
5599    MRRC2
5600
5601    Two XScale instructions are special cases of these:
5602
5603      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
5604      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
5605
5606    Result unpredicatable if Rd or Rn is R15.  */
5607
5608 static void
5609 do_co_reg2c (void)
5610 {
5611   inst.instruction |= inst.operands[0].reg << 8;
5612   inst.instruction |= inst.operands[1].imm << 4;
5613   inst.instruction |= inst.operands[2].reg << 12;
5614   inst.instruction |= inst.operands[3].reg << 16;
5615   inst.instruction |= inst.operands[4].reg;
5616 }
5617
5618 static void
5619 do_cpsi (void)
5620 {
5621   inst.instruction |= inst.operands[0].imm << 6;
5622   inst.instruction |= inst.operands[1].imm;
5623 }
5624
5625 static void
5626 do_dbg (void)
5627 {
5628   inst.instruction |= inst.operands[0].imm;
5629 }
5630
5631 static void
5632 do_it (void)
5633 {
5634   /* There is no IT instruction in ARM mode.  We
5635      process it but do not generate code for it.  */
5636   inst.size = 0;
5637 }
5638
5639 static void
5640 do_ldmstm (void)
5641 {
5642   int base_reg = inst.operands[0].reg;
5643   int range = inst.operands[1].imm;
5644
5645   inst.instruction |= base_reg << 16;
5646   inst.instruction |= range;
5647
5648   if (inst.operands[1].writeback)
5649     inst.instruction |= LDM_TYPE_2_OR_3;
5650
5651   if (inst.operands[0].writeback)
5652     {
5653       inst.instruction |= WRITE_BACK;
5654       /* Check for unpredictable uses of writeback.  */
5655       if (inst.instruction & LOAD_BIT)
5656         {
5657           /* Not allowed in LDM type 2.  */
5658           if ((inst.instruction & LDM_TYPE_2_OR_3)
5659               && ((range & (1 << REG_PC)) == 0))
5660             as_warn (_("writeback of base register is UNPREDICTABLE"));
5661           /* Only allowed if base reg not in list for other types.  */
5662           else if (range & (1 << base_reg))
5663             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
5664         }
5665       else /* STM.  */
5666         {
5667           /* Not allowed for type 2.  */
5668           if (inst.instruction & LDM_TYPE_2_OR_3)
5669             as_warn (_("writeback of base register is UNPREDICTABLE"));
5670           /* Only allowed if base reg not in list, or first in list.  */
5671           else if ((range & (1 << base_reg))
5672                    && (range & ((1 << base_reg) - 1)))
5673             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
5674         }
5675     }
5676 }
5677
5678 /* ARMv5TE load-consecutive (argument parse)
5679    Mode is like LDRH.
5680
5681      LDRccD R, mode
5682      STRccD R, mode.  */
5683
5684 static void
5685 do_ldrd (void)
5686 {
5687   constraint (inst.operands[0].reg % 2 != 0,
5688               _("first destination register must be even"));
5689   constraint (inst.operands[1].present
5690               && inst.operands[1].reg != inst.operands[0].reg + 1,
5691               _("can only load two consecutive registers"));
5692   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
5693   constraint (!inst.operands[2].isreg, _("'[' expected"));
5694
5695   if (!inst.operands[1].present)
5696     inst.operands[1].reg = inst.operands[0].reg + 1;
5697   
5698   if (inst.instruction & LOAD_BIT)
5699     {
5700       /* encode_arm_addr_mode_3 will diagnose overlap between the base
5701          register and the first register written; we have to diagnose
5702          overlap between the base and the second register written here.  */
5703
5704       if (inst.operands[2].reg == inst.operands[1].reg
5705           && (inst.operands[2].writeback || inst.operands[2].postind))
5706         as_warn (_("base register written back, and overlaps "
5707                    "second destination register"));
5708
5709       /* For an index-register load, the index register must not overlap the
5710          destination (even if not write-back).  */
5711       else if (inst.operands[2].immisreg
5712                && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
5713                    || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
5714         as_warn (_("index register overlaps destination register"));
5715     }
5716
5717   inst.instruction |= inst.operands[0].reg << 12;
5718   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
5719 }
5720
5721 static void
5722 do_ldrex (void)
5723 {
5724   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
5725               || inst.operands[1].postind || inst.operands[1].writeback
5726               || inst.operands[1].immisreg || inst.operands[1].shifted
5727               || inst.operands[1].negative
5728               /* This can arise if the programmer has written
5729                    strex rN, rM, foo
5730                  or if they have mistakenly used a register name as the last
5731                  operand,  eg:
5732                    strex rN, rM, rX
5733                  It is very difficult to distinguish between these two cases
5734                  because "rX" might actually be a label. ie the register
5735                  name has been occluded by a symbol of the same name. So we
5736                  just generate a general 'bad addressing mode' type error
5737                  message and leave it up to the programmer to discover the
5738                  true cause and fix their mistake.  */
5739               || (inst.operands[1].reg == REG_PC),
5740               BAD_ADDR_MODE);
5741
5742   constraint (inst.reloc.exp.X_op != O_constant
5743               || inst.reloc.exp.X_add_number != 0,
5744               _("offset must be zero in ARM encoding"));
5745
5746   inst.instruction |= inst.operands[0].reg << 12;
5747   inst.instruction |= inst.operands[1].reg << 16;
5748   inst.reloc.type = BFD_RELOC_UNUSED;
5749 }
5750
5751 static void
5752 do_ldrexd (void)
5753 {
5754   constraint (inst.operands[0].reg % 2 != 0,
5755               _("even register required"));
5756   constraint (inst.operands[1].present
5757               && inst.operands[1].reg != inst.operands[0].reg + 1,
5758               _("can only load two consecutive registers"));
5759   /* If op 1 were present and equal to PC, this function wouldn't
5760      have been called in the first place.  */
5761   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
5762
5763   inst.instruction |= inst.operands[0].reg << 12;
5764   inst.instruction |= inst.operands[2].reg << 16;
5765 }
5766
5767 static void
5768 do_ldst (void)
5769 {
5770   inst.instruction |= inst.operands[0].reg << 12;
5771   if (!inst.operands[1].isreg)
5772     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/FALSE))
5773       return;
5774   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
5775 }
5776
5777 static void
5778 do_ldstt (void)
5779 {
5780   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
5781      reject [Rn,...].  */
5782   if (inst.operands[1].preind)
5783     {
5784       constraint (inst.reloc.exp.X_op != O_constant ||
5785                   inst.reloc.exp.X_add_number != 0,
5786                   _("this instruction requires a post-indexed address"));
5787
5788       inst.operands[1].preind = 0;
5789       inst.operands[1].postind = 1;
5790       inst.operands[1].writeback = 1;
5791     }
5792   inst.instruction |= inst.operands[0].reg << 12;
5793   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
5794 }
5795
5796 /* Halfword and signed-byte load/store operations.  */
5797
5798 static void
5799 do_ldstv4 (void)
5800 {
5801   inst.instruction |= inst.operands[0].reg << 12;
5802   if (!inst.operands[1].isreg)
5803     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/TRUE))
5804       return;
5805   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
5806 }
5807
5808 static void
5809 do_ldsttv4 (void)
5810 {
5811   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
5812      reject [Rn,...].  */
5813   if (inst.operands[1].preind)
5814     {
5815       constraint (inst.reloc.exp.X_op != O_constant ||
5816                   inst.reloc.exp.X_add_number != 0,
5817                   _("this instruction requires a post-indexed address"));
5818
5819       inst.operands[1].preind = 0;
5820       inst.operands[1].postind = 1;
5821       inst.operands[1].writeback = 1;
5822     }
5823   inst.instruction |= inst.operands[0].reg << 12;
5824   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
5825 }
5826
5827 /* Co-processor register load/store.
5828    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
5829 static void
5830 do_lstc (void)
5831 {
5832   inst.instruction |= inst.operands[0].reg << 8;
5833   inst.instruction |= inst.operands[1].reg << 12;
5834   encode_arm_cp_address (2, TRUE, TRUE, 0);
5835 }
5836
5837 static void
5838 do_mlas (void)
5839 {
5840   /* This restriction does not apply to mls (nor to mla in v6, but
5841      that's hard to detect at present).  */
5842   if (inst.operands[0].reg == inst.operands[1].reg
5843       && !(inst.instruction & 0x00400000))
5844     as_tsktsk (_("rd and rm should be different in mla"));
5845
5846   inst.instruction |= inst.operands[0].reg << 16;
5847   inst.instruction |= inst.operands[1].reg;
5848   inst.instruction |= inst.operands[2].reg << 8;
5849   inst.instruction |= inst.operands[3].reg << 12;
5850
5851 }
5852
5853 static void
5854 do_mov (void)
5855 {
5856   inst.instruction |= inst.operands[0].reg << 12;
5857   encode_arm_shifter_operand (1);
5858 }
5859
5860 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
5861 static void
5862 do_mov16 (void)
5863 {
5864   inst.instruction |= inst.operands[0].reg << 12;
5865   /* The value is in two pieces: 0:11, 16:19.  */
5866   inst.instruction |= (inst.operands[1].imm & 0x00000fff);
5867   inst.instruction |= (inst.operands[1].imm & 0x0000f000) << 4;
5868 }
5869
5870 static void
5871 do_mrs (void)
5872 {
5873   /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
5874   constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
5875               != (PSR_c|PSR_f),
5876               _("'CPSR' or 'SPSR' expected"));
5877   inst.instruction |= inst.operands[0].reg << 12;
5878   inst.instruction |= (inst.operands[1].imm & SPSR_BIT);
5879 }
5880
5881 /* Two possible forms:
5882       "{C|S}PSR_<field>, Rm",
5883       "{C|S}PSR_f, #expression".  */
5884
5885 static void
5886 do_msr (void)
5887 {
5888   inst.instruction |= inst.operands[0].imm;
5889   if (inst.operands[1].isreg)
5890     inst.instruction |= inst.operands[1].reg;
5891   else
5892     {
5893       inst.instruction |= INST_IMMEDIATE;
5894       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5895       inst.reloc.pc_rel = 0;
5896     }
5897 }
5898
5899 static void
5900 do_mul (void)
5901 {
5902   if (!inst.operands[2].present)
5903     inst.operands[2].reg = inst.operands[0].reg;
5904   inst.instruction |= inst.operands[0].reg << 16;
5905   inst.instruction |= inst.operands[1].reg;
5906   inst.instruction |= inst.operands[2].reg << 8;
5907
5908   if (inst.operands[0].reg == inst.operands[1].reg)
5909     as_tsktsk (_("rd and rm should be different in mul"));
5910 }
5911
5912 /* Long Multiply Parser
5913    UMULL RdLo, RdHi, Rm, Rs
5914    SMULL RdLo, RdHi, Rm, Rs
5915    UMLAL RdLo, RdHi, Rm, Rs
5916    SMLAL RdLo, RdHi, Rm, Rs.  */
5917
5918 static void
5919 do_mull (void)
5920 {
5921   inst.instruction |= inst.operands[0].reg << 12;
5922   inst.instruction |= inst.operands[1].reg << 16;
5923   inst.instruction |= inst.operands[2].reg;
5924   inst.instruction |= inst.operands[3].reg << 8;
5925
5926   /* rdhi, rdlo and rm must all be different.  */
5927   if (inst.operands[0].reg == inst.operands[1].reg
5928       || inst.operands[0].reg == inst.operands[2].reg
5929       || inst.operands[1].reg == inst.operands[2].reg)
5930     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
5931 }
5932
5933 static void
5934 do_nop (void)
5935 {
5936   if (inst.operands[0].present)
5937     {
5938       /* Architectural NOP hints are CPSR sets with no bits selected.  */
5939       inst.instruction &= 0xf0000000;
5940       inst.instruction |= 0x0320f000 + inst.operands[0].imm;
5941     }
5942 }
5943
5944 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
5945    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
5946    Condition defaults to COND_ALWAYS.
5947    Error if Rd, Rn or Rm are R15.  */
5948
5949 static void
5950 do_pkhbt (void)
5951 {
5952   inst.instruction |= inst.operands[0].reg << 12;
5953   inst.instruction |= inst.operands[1].reg << 16;
5954   inst.instruction |= inst.operands[2].reg;
5955   if (inst.operands[3].present)
5956     encode_arm_shift (3);
5957 }
5958
5959 /* ARM V6 PKHTB (Argument Parse).  */
5960
5961 static void
5962 do_pkhtb (void)
5963 {
5964   if (!inst.operands[3].present)
5965     {
5966       /* If the shift specifier is omitted, turn the instruction
5967          into pkhbt rd, rm, rn. */
5968       inst.instruction &= 0xfff00010;
5969       inst.instruction |= inst.operands[0].reg << 12;
5970       inst.instruction |= inst.operands[1].reg;
5971       inst.instruction |= inst.operands[2].reg << 16;
5972     }
5973   else
5974     {
5975       inst.instruction |= inst.operands[0].reg << 12;
5976       inst.instruction |= inst.operands[1].reg << 16;
5977       inst.instruction |= inst.operands[2].reg;
5978       encode_arm_shift (3);
5979     }
5980 }
5981
5982 /* ARMv5TE: Preload-Cache
5983
5984     PLD <addr_mode>
5985
5986   Syntactically, like LDR with B=1, W=0, L=1.  */
5987
5988 static void
5989 do_pld (void)
5990 {
5991   constraint (!inst.operands[0].isreg,
5992               _("'[' expected after PLD mnemonic"));
5993   constraint (inst.operands[0].postind,
5994               _("post-indexed expression used in preload instruction"));
5995   constraint (inst.operands[0].writeback,
5996               _("writeback used in preload instruction"));
5997   constraint (!inst.operands[0].preind,
5998               _("unindexed addressing used in preload instruction"));
5999   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
6000 }
6001
6002 /* ARMv7: PLI <addr_mode>  */
6003 static void
6004 do_pli (void)
6005 {
6006   constraint (!inst.operands[0].isreg,
6007               _("'[' expected after PLI mnemonic"));
6008   constraint (inst.operands[0].postind,
6009               _("post-indexed expression used in preload instruction"));
6010   constraint (inst.operands[0].writeback,
6011               _("writeback used in preload instruction"));
6012   constraint (!inst.operands[0].preind,
6013               _("unindexed addressing used in preload instruction"));
6014   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
6015   inst.instruction &= ~PRE_INDEX;
6016 }
6017
6018 static void
6019 do_push_pop (void)
6020 {
6021   inst.operands[1] = inst.operands[0];
6022   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
6023   inst.operands[0].isreg = 1;
6024   inst.operands[0].writeback = 1;
6025   inst.operands[0].reg = REG_SP;
6026   do_ldmstm ();
6027 }
6028
6029 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
6030    word at the specified address and the following word
6031    respectively.
6032    Unconditionally executed.
6033    Error if Rn is R15.  */
6034
6035 static void
6036 do_rfe (void)
6037 {
6038   inst.instruction |= inst.operands[0].reg << 16;
6039   if (inst.operands[0].writeback)
6040     inst.instruction |= WRITE_BACK;
6041 }
6042
6043 /* ARM V6 ssat (argument parse).  */
6044
6045 static void
6046 do_ssat (void)
6047 {
6048   inst.instruction |= inst.operands[0].reg << 12;
6049   inst.instruction |= (inst.operands[1].imm - 1) << 16;
6050   inst.instruction |= inst.operands[2].reg;
6051
6052   if (inst.operands[3].present)
6053     encode_arm_shift (3);
6054 }
6055
6056 /* ARM V6 usat (argument parse).  */
6057
6058 static void
6059 do_usat (void)
6060 {
6061   inst.instruction |= inst.operands[0].reg << 12;
6062   inst.instruction |= inst.operands[1].imm << 16;
6063   inst.instruction |= inst.operands[2].reg;
6064
6065   if (inst.operands[3].present)
6066     encode_arm_shift (3);
6067 }
6068
6069 /* ARM V6 ssat16 (argument parse).  */
6070
6071 static void
6072 do_ssat16 (void)
6073 {
6074   inst.instruction |= inst.operands[0].reg << 12;
6075   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
6076   inst.instruction |= inst.operands[2].reg;
6077 }
6078
6079 static void
6080 do_usat16 (void)
6081 {
6082   inst.instruction |= inst.operands[0].reg << 12;
6083   inst.instruction |= inst.operands[1].imm << 16;
6084   inst.instruction |= inst.operands[2].reg;
6085 }
6086
6087 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
6088    preserving the other bits.
6089
6090    setend <endian_specifier>, where <endian_specifier> is either
6091    BE or LE.  */
6092
6093 static void
6094 do_setend (void)
6095 {
6096   if (inst.operands[0].imm)
6097     inst.instruction |= 0x200;
6098 }
6099
6100 static void
6101 do_shift (void)
6102 {
6103   unsigned int Rm = (inst.operands[1].present
6104                      ? inst.operands[1].reg
6105                      : inst.operands[0].reg);
6106
6107   inst.instruction |= inst.operands[0].reg << 12;
6108   inst.instruction |= Rm;
6109   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
6110     {
6111       inst.instruction |= inst.operands[2].reg << 8;
6112       inst.instruction |= SHIFT_BY_REG;
6113     }
6114   else
6115     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
6116 }
6117
6118 static void
6119 do_smc (void)
6120 {
6121   inst.reloc.type = BFD_RELOC_ARM_SMC;
6122   inst.reloc.pc_rel = 0;
6123 }
6124
6125 static void
6126 do_swi (void)
6127 {
6128   inst.reloc.type = BFD_RELOC_ARM_SWI;
6129   inst.reloc.pc_rel = 0;
6130 }
6131
6132 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
6133    SMLAxy{cond} Rd,Rm,Rs,Rn
6134    SMLAWy{cond} Rd,Rm,Rs,Rn
6135    Error if any register is R15.  */
6136
6137 static void
6138 do_smla (void)
6139 {
6140   inst.instruction |= inst.operands[0].reg << 16;
6141   inst.instruction |= inst.operands[1].reg;
6142   inst.instruction |= inst.operands[2].reg << 8;
6143   inst.instruction |= inst.operands[3].reg << 12;
6144 }
6145
6146 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
6147    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
6148    Error if any register is R15.
6149    Warning if Rdlo == Rdhi.  */
6150
6151 static void
6152 do_smlal (void)
6153 {
6154   inst.instruction |= inst.operands[0].reg << 12;
6155   inst.instruction |= inst.operands[1].reg << 16;
6156   inst.instruction |= inst.operands[2].reg;
6157   inst.instruction |= inst.operands[3].reg << 8;
6158
6159   if (inst.operands[0].reg == inst.operands[1].reg)
6160     as_tsktsk (_("rdhi and rdlo must be different"));
6161 }
6162
6163 /* ARM V5E (El Segundo) signed-multiply (argument parse)
6164    SMULxy{cond} Rd,Rm,Rs
6165    Error if any register is R15.  */
6166
6167 static void
6168 do_smul (void)
6169 {
6170   inst.instruction |= inst.operands[0].reg << 16;
6171   inst.instruction |= inst.operands[1].reg;
6172   inst.instruction |= inst.operands[2].reg << 8;
6173 }
6174
6175 /* ARM V6 srs (argument parse).  */
6176
6177 static void
6178 do_srs (void)
6179 {
6180   inst.instruction |= inst.operands[0].imm;
6181   if (inst.operands[0].writeback)
6182     inst.instruction |= WRITE_BACK;
6183 }
6184
6185 /* ARM V6 strex (argument parse).  */
6186
6187 static void
6188 do_strex (void)
6189 {
6190   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
6191               || inst.operands[2].postind || inst.operands[2].writeback
6192               || inst.operands[2].immisreg || inst.operands[2].shifted
6193               || inst.operands[2].negative
6194               /* See comment in do_ldrex().  */
6195               || (inst.operands[2].reg == REG_PC),
6196               BAD_ADDR_MODE);
6197
6198   constraint (inst.operands[0].reg == inst.operands[1].reg
6199               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
6200
6201   constraint (inst.reloc.exp.X_op != O_constant
6202               || inst.reloc.exp.X_add_number != 0,
6203               _("offset must be zero in ARM encoding"));
6204
6205   inst.instruction |= inst.operands[0].reg << 12;
6206   inst.instruction |= inst.operands[1].reg;
6207   inst.instruction |= inst.operands[2].reg << 16;
6208   inst.reloc.type = BFD_RELOC_UNUSED;
6209 }
6210
6211 static void
6212 do_strexd (void)
6213 {
6214   constraint (inst.operands[1].reg % 2 != 0,
6215               _("even register required"));
6216   constraint (inst.operands[2].present
6217               && inst.operands[2].reg != inst.operands[1].reg + 1,
6218               _("can only store two consecutive registers"));
6219   /* If op 2 were present and equal to PC, this function wouldn't
6220      have been called in the first place.  */
6221   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
6222
6223   constraint (inst.operands[0].reg == inst.operands[1].reg
6224               || inst.operands[0].reg == inst.operands[1].reg + 1
6225               || inst.operands[0].reg == inst.operands[3].reg,
6226               BAD_OVERLAP);
6227
6228   inst.instruction |= inst.operands[0].reg << 12;
6229   inst.instruction |= inst.operands[1].reg;
6230   inst.instruction |= inst.operands[3].reg << 16;
6231 }
6232
6233 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
6234    extends it to 32-bits, and adds the result to a value in another
6235    register.  You can specify a rotation by 0, 8, 16, or 24 bits
6236    before extracting the 16-bit value.
6237    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
6238    Condition defaults to COND_ALWAYS.
6239    Error if any register uses R15.  */
6240
6241 static void
6242 do_sxtah (void)
6243 {
6244   inst.instruction |= inst.operands[0].reg << 12;
6245   inst.instruction |= inst.operands[1].reg << 16;
6246   inst.instruction |= inst.operands[2].reg;
6247   inst.instruction |= inst.operands[3].imm << 10;
6248 }
6249
6250 /* ARM V6 SXTH.
6251
6252    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
6253    Condition defaults to COND_ALWAYS.
6254    Error if any register uses R15.  */
6255
6256 static void
6257 do_sxth (void)
6258 {
6259   inst.instruction |= inst.operands[0].reg << 12;
6260   inst.instruction |= inst.operands[1].reg;
6261   inst.instruction |= inst.operands[2].imm << 10;
6262 }
6263 \f
6264 /* VFP instructions.  In a logical order: SP variant first, monad
6265    before dyad, arithmetic then move then load/store.  */
6266
6267 static void
6268 do_vfp_sp_monadic (void)
6269 {
6270   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
6271   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
6272 }
6273
6274 static void
6275 do_vfp_sp_dyadic (void)
6276 {
6277   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
6278   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
6279   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
6280 }
6281
6282 static void
6283 do_vfp_sp_compare_z (void)
6284 {
6285   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
6286 }
6287
6288 static void
6289 do_vfp_dp_sp_cvt (void)
6290 {
6291   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
6292   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
6293 }
6294
6295 static void
6296 do_vfp_sp_dp_cvt (void)
6297 {
6298   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
6299   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
6300 }
6301
6302 static void
6303 do_vfp_reg_from_sp (void)
6304 {
6305   inst.instruction |= inst.operands[0].reg << 12;
6306   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
6307 }
6308
6309 static void
6310 do_vfp_reg2_from_sp2 (void)
6311 {
6312   constraint (inst.operands[2].imm != 2,
6313               _("only two consecutive VFP SP registers allowed here"));
6314   inst.instruction |= inst.operands[0].reg << 12;
6315   inst.instruction |= inst.operands[1].reg << 16;
6316   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
6317 }
6318
6319 static void
6320 do_vfp_sp_from_reg (void)
6321 {
6322   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
6323   inst.instruction |= inst.operands[1].reg << 12;
6324 }
6325
6326 static void
6327 do_vfp_sp2_from_reg2 (void)
6328 {
6329   constraint (inst.operands[0].imm != 2,
6330               _("only two consecutive VFP SP registers allowed here"));
6331   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
6332   inst.instruction |= inst.operands[1].reg << 12;
6333   inst.instruction |= inst.operands[2].reg << 16;
6334 }
6335
6336 static void
6337 do_vfp_sp_ldst (void)
6338 {
6339   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
6340   encode_arm_cp_address (1, FALSE, TRUE, 0);
6341 }
6342
6343 static void
6344 do_vfp_dp_ldst (void)
6345 {
6346   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
6347   encode_arm_cp_address (1, FALSE, TRUE, 0);
6348 }
6349
6350
6351 static void
6352 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
6353 {
6354   if (inst.operands[0].writeback)
6355     inst.instruction |= WRITE_BACK;
6356   else
6357     constraint (ldstm_type != VFP_LDSTMIA,
6358                 _("this addressing mode requires base-register writeback"));
6359   inst.instruction |= inst.operands[0].reg << 16;
6360   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
6361   inst.instruction |= inst.operands[1].imm;
6362 }
6363
6364 static void
6365 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
6366 {
6367   int count;
6368
6369   if (inst.operands[0].writeback)
6370     inst.instruction |= WRITE_BACK;
6371   else
6372     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
6373                 _("this addressing mode requires base-register writeback"));
6374
6375   inst.instruction |= inst.operands[0].reg << 16;
6376   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
6377
6378   count = inst.operands[1].imm << 1;
6379   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
6380     count += 1;
6381
6382   inst.instruction |= count;
6383 }
6384
6385 static void
6386 do_vfp_sp_ldstmia (void)
6387 {
6388   vfp_sp_ldstm (VFP_LDSTMIA);
6389 }
6390
6391 static void
6392 do_vfp_sp_ldstmdb (void)
6393 {
6394   vfp_sp_ldstm (VFP_LDSTMDB);
6395 }
6396
6397 static void
6398 do_vfp_dp_ldstmia (void)
6399 {
6400   vfp_dp_ldstm (VFP_LDSTMIA);
6401 }
6402
6403 static void
6404 do_vfp_dp_ldstmdb (void)
6405 {
6406   vfp_dp_ldstm (VFP_LDSTMDB);
6407 }
6408
6409 static void
6410 do_vfp_xp_ldstmia (void)
6411 {
6412   vfp_dp_ldstm (VFP_LDSTMIAX);
6413 }
6414
6415 static void
6416 do_vfp_xp_ldstmdb (void)
6417 {
6418   vfp_dp_ldstm (VFP_LDSTMDBX);
6419 }
6420
6421 static void
6422 do_vfp_dp_rd_rm (void)
6423 {
6424   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
6425   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
6426 }
6427
6428 static void
6429 do_vfp_dp_rn_rd (void)
6430 {
6431   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
6432   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
6433 }
6434
6435 static void
6436 do_vfp_dp_rd_rn (void)
6437 {
6438   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
6439   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
6440 }
6441
6442 static void
6443 do_vfp_dp_rd_rn_rm (void)
6444 {
6445   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
6446   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
6447   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
6448 }
6449
6450 static void
6451 do_vfp_dp_rd (void)
6452 {
6453   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
6454 }
6455
6456 static void
6457 do_vfp_dp_rm_rd_rn (void)
6458 {
6459   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
6460   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
6461   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
6462 }
6463
6464 /* VFPv3 instructions.  */
6465 static void
6466 do_vfp_sp_const (void)
6467 {
6468   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
6469   inst.instruction |= (inst.operands[1].imm & 15) << 16;
6470   inst.instruction |= (inst.operands[1].imm >> 4);
6471 }
6472
6473 static void
6474 do_vfp_dp_const (void)
6475 {
6476   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
6477   inst.instruction |= (inst.operands[1].imm & 15) << 16;
6478   inst.instruction |= (inst.operands[1].imm >> 4);
6479 }
6480
6481 static void
6482 vfp_conv (int srcsize)
6483 {
6484   unsigned immbits = srcsize - inst.operands[1].imm;
6485   inst.instruction |= (immbits & 1) << 5;
6486   inst.instruction |= (immbits >> 1);
6487 }
6488
6489 static void
6490 do_vfp_sp_conv_16 (void)
6491 {
6492   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
6493   vfp_conv (16);
6494 }
6495
6496 static void
6497 do_vfp_dp_conv_16 (void)
6498 {
6499   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
6500   vfp_conv (16);
6501 }
6502
6503 static void
6504 do_vfp_sp_conv_32 (void)
6505 {
6506   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
6507   vfp_conv (32);
6508 }
6509
6510 static void
6511 do_vfp_dp_conv_32 (void)
6512 {
6513   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
6514   vfp_conv (32);
6515 }
6516
6517 \f
6518 /* FPA instructions.  Also in a logical order.  */
6519
6520 static void
6521 do_fpa_cmp (void)
6522 {
6523   inst.instruction |= inst.operands[0].reg << 16;
6524   inst.instruction |= inst.operands[1].reg;
6525 }
6526
6527 static void
6528 do_fpa_ldmstm (void)
6529 {
6530   inst.instruction |= inst.operands[0].reg << 12;
6531   switch (inst.operands[1].imm)
6532     {
6533     case 1: inst.instruction |= CP_T_X;          break;
6534     case 2: inst.instruction |= CP_T_Y;          break;
6535     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
6536     case 4:                                      break;
6537     default: abort ();
6538     }
6539
6540   if (inst.instruction & (PRE_INDEX | INDEX_UP))
6541     {
6542       /* The instruction specified "ea" or "fd", so we can only accept
6543          [Rn]{!}.  The instruction does not really support stacking or
6544          unstacking, so we have to emulate these by setting appropriate
6545          bits and offsets.  */
6546       constraint (inst.reloc.exp.X_op != O_constant
6547                   || inst.reloc.exp.X_add_number != 0,
6548                   _("this instruction does not support indexing"));
6549
6550       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
6551         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
6552
6553       if (!(inst.instruction & INDEX_UP))
6554         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
6555
6556       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
6557         {
6558           inst.operands[2].preind = 0;
6559           inst.operands[2].postind = 1;
6560         }
6561     }
6562
6563   encode_arm_cp_address (2, TRUE, TRUE, 0);
6564 }
6565 \f
6566 /* iWMMXt instructions: strictly in alphabetical order.  */
6567
6568 static void
6569 do_iwmmxt_tandorc (void)
6570 {
6571   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
6572 }
6573
6574 static void
6575 do_iwmmxt_textrc (void)
6576 {
6577   inst.instruction |= inst.operands[0].reg << 12;
6578   inst.instruction |= inst.operands[1].imm;
6579 }
6580
6581 static void
6582 do_iwmmxt_textrm (void)
6583 {
6584   inst.instruction |= inst.operands[0].reg << 12;
6585   inst.instruction |= inst.operands[1].reg << 16;
6586   inst.instruction |= inst.operands[2].imm;
6587 }
6588
6589 static void
6590 do_iwmmxt_tinsr (void)
6591 {
6592   inst.instruction |= inst.operands[0].reg << 16;
6593   inst.instruction |= inst.operands[1].reg << 12;
6594   inst.instruction |= inst.operands[2].imm;
6595 }
6596
6597 static void
6598 do_iwmmxt_tmia (void)
6599 {
6600   inst.instruction |= inst.operands[0].reg << 5;
6601   inst.instruction |= inst.operands[1].reg;
6602   inst.instruction |= inst.operands[2].reg << 12;
6603 }
6604
6605 static void
6606 do_iwmmxt_waligni (void)
6607 {
6608   inst.instruction |= inst.operands[0].reg << 12;
6609   inst.instruction |= inst.operands[1].reg << 16;
6610   inst.instruction |= inst.operands[2].reg;
6611   inst.instruction |= inst.operands[3].imm << 20;
6612 }
6613
6614 static void
6615 do_iwmmxt_wmov (void)
6616 {
6617   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
6618   inst.instruction |= inst.operands[0].reg << 12;
6619   inst.instruction |= inst.operands[1].reg << 16;
6620   inst.instruction |= inst.operands[1].reg;
6621 }
6622
6623 static void
6624 do_iwmmxt_wldstbh (void)
6625 {
6626   int reloc;
6627   inst.instruction |= inst.operands[0].reg << 12;
6628   inst.reloc.exp.X_add_number *= 4;
6629   if (thumb_mode)
6630     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
6631   else
6632     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
6633   encode_arm_cp_address (1, TRUE, FALSE, reloc);
6634 }
6635
6636 static void
6637 do_iwmmxt_wldstw (void)
6638 {
6639   /* RIWR_RIWC clears .isreg for a control register.  */
6640   if (!inst.operands[0].isreg)
6641     {
6642       constraint (inst.cond != COND_ALWAYS, BAD_COND);
6643       inst.instruction |= 0xf0000000;
6644     }
6645
6646   inst.instruction |= inst.operands[0].reg << 12;
6647   encode_arm_cp_address (1, TRUE, TRUE, 0);
6648 }
6649
6650 static void
6651 do_iwmmxt_wldstd (void)
6652 {
6653   inst.instruction |= inst.operands[0].reg << 12;
6654   encode_arm_cp_address (1, TRUE, FALSE, 0);
6655 }
6656
6657 static void
6658 do_iwmmxt_wshufh (void)
6659 {
6660   inst.instruction |= inst.operands[0].reg << 12;
6661   inst.instruction |= inst.operands[1].reg << 16;
6662   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
6663   inst.instruction |= (inst.operands[2].imm & 0x0f);
6664 }
6665
6666 static void
6667 do_iwmmxt_wzero (void)
6668 {
6669   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
6670   inst.instruction |= inst.operands[0].reg;
6671   inst.instruction |= inst.operands[0].reg << 12;
6672   inst.instruction |= inst.operands[0].reg << 16;
6673 }
6674 \f
6675 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
6676    operations first, then control, shift, and load/store.  */
6677
6678 /* Insns like "foo X,Y,Z".  */
6679
6680 static void
6681 do_mav_triple (void)
6682 {
6683   inst.instruction |= inst.operands[0].reg << 16;
6684   inst.instruction |= inst.operands[1].reg;
6685   inst.instruction |= inst.operands[2].reg << 12;
6686 }
6687
6688 /* Insns like "foo W,X,Y,Z".
6689     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
6690
6691 static void
6692 do_mav_quad (void)
6693 {
6694   inst.instruction |= inst.operands[0].reg << 5;
6695   inst.instruction |= inst.operands[1].reg << 12;
6696   inst.instruction |= inst.operands[2].reg << 16;
6697   inst.instruction |= inst.operands[3].reg;
6698 }
6699
6700 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
6701 static void
6702 do_mav_dspsc (void)
6703 {
6704   inst.instruction |= inst.operands[1].reg << 12;
6705 }
6706
6707 /* Maverick shift immediate instructions.
6708    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
6709    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
6710
6711 static void
6712 do_mav_shift (void)
6713 {
6714   int imm = inst.operands[2].imm;
6715
6716   inst.instruction |= inst.operands[0].reg << 12;
6717   inst.instruction |= inst.operands[1].reg << 16;
6718
6719   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
6720      Bits 5-7 of the insn should have bits 4-6 of the immediate.
6721      Bit 4 should be 0.  */
6722   imm = (imm & 0xf) | ((imm & 0x70) << 1);
6723
6724   inst.instruction |= imm;
6725 }
6726 \f
6727 /* XScale instructions.  Also sorted arithmetic before move.  */
6728
6729 /* Xscale multiply-accumulate (argument parse)
6730      MIAcc   acc0,Rm,Rs
6731      MIAPHcc acc0,Rm,Rs
6732      MIAxycc acc0,Rm,Rs.  */
6733
6734 static void
6735 do_xsc_mia (void)
6736 {
6737   inst.instruction |= inst.operands[1].reg;
6738   inst.instruction |= inst.operands[2].reg << 12;
6739 }
6740
6741 /* Xscale move-accumulator-register (argument parse)
6742
6743      MARcc   acc0,RdLo,RdHi.  */
6744
6745 static void
6746 do_xsc_mar (void)
6747 {
6748   inst.instruction |= inst.operands[1].reg << 12;
6749   inst.instruction |= inst.operands[2].reg << 16;
6750 }
6751
6752 /* Xscale move-register-accumulator (argument parse)
6753
6754      MRAcc   RdLo,RdHi,acc0.  */
6755
6756 static void
6757 do_xsc_mra (void)
6758 {
6759   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
6760   inst.instruction |= inst.operands[0].reg << 12;
6761   inst.instruction |= inst.operands[1].reg << 16;
6762 }
6763 \f
6764 /* Encoding functions relevant only to Thumb.  */
6765
6766 /* inst.operands[i] is a shifted-register operand; encode
6767    it into inst.instruction in the format used by Thumb32.  */
6768
6769 static void
6770 encode_thumb32_shifted_operand (int i)
6771 {
6772   unsigned int value = inst.reloc.exp.X_add_number;
6773   unsigned int shift = inst.operands[i].shift_kind;
6774
6775   constraint (inst.operands[i].immisreg,
6776               _("shift by register not allowed in thumb mode"));
6777   inst.instruction |= inst.operands[i].reg;
6778   if (shift == SHIFT_RRX)
6779     inst.instruction |= SHIFT_ROR << 4;
6780   else
6781     {
6782       constraint (inst.reloc.exp.X_op != O_constant,
6783                   _("expression too complex"));
6784
6785       constraint (value > 32
6786                   || (value == 32 && (shift == SHIFT_LSL
6787                                       || shift == SHIFT_ROR)),
6788                   _("shift expression is too large"));
6789
6790       if (value == 0)
6791         shift = SHIFT_LSL;
6792       else if (value == 32)
6793         value = 0;
6794
6795       inst.instruction |= shift << 4;
6796       inst.instruction |= (value & 0x1c) << 10;
6797       inst.instruction |= (value & 0x03) << 6;
6798     }
6799 }
6800
6801
6802 /* inst.operands[i] was set up by parse_address.  Encode it into a
6803    Thumb32 format load or store instruction.  Reject forms that cannot
6804    be used with such instructions.  If is_t is true, reject forms that
6805    cannot be used with a T instruction; if is_d is true, reject forms
6806    that cannot be used with a D instruction.  */
6807
6808 static void
6809 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
6810 {
6811   bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
6812
6813   constraint (!inst.operands[i].isreg,
6814               _("Instruction does not support =N addresses"));
6815
6816   inst.instruction |= inst.operands[i].reg << 16;
6817   if (inst.operands[i].immisreg)
6818     {
6819       constraint (is_pc, _("cannot use register index with PC-relative addressing"));
6820       constraint (is_t || is_d, _("cannot use register index with this instruction"));
6821       constraint (inst.operands[i].negative,
6822                   _("Thumb does not support negative register indexing"));
6823       constraint (inst.operands[i].postind,
6824                   _("Thumb does not support register post-indexing"));
6825       constraint (inst.operands[i].writeback,
6826                   _("Thumb does not support register indexing with writeback"));
6827       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
6828                   _("Thumb supports only LSL in shifted register indexing"));
6829
6830       inst.instruction |= inst.operands[i].imm;
6831       if (inst.operands[i].shifted)
6832         {
6833           constraint (inst.reloc.exp.X_op != O_constant,
6834                       _("expression too complex"));
6835           constraint (inst.reloc.exp.X_add_number < 0
6836                       || inst.reloc.exp.X_add_number > 3,
6837                       _("shift out of range"));
6838           inst.instruction |= inst.reloc.exp.X_add_number << 4;
6839         }
6840       inst.reloc.type = BFD_RELOC_UNUSED;
6841     }
6842   else if (inst.operands[i].preind)
6843     {
6844       constraint (is_pc && inst.operands[i].writeback,
6845                   _("cannot use writeback with PC-relative addressing"));
6846       constraint (is_t && inst.operands[i].writeback,
6847                   _("cannot use writeback with this instruction"));
6848
6849       if (is_d)
6850         {
6851           inst.instruction |= 0x01000000;
6852           if (inst.operands[i].writeback)
6853             inst.instruction |= 0x00200000;
6854         }
6855       else
6856         {
6857           inst.instruction |= 0x00000c00;
6858           if (inst.operands[i].writeback)
6859             inst.instruction |= 0x00000100;
6860         }
6861       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
6862     }
6863   else if (inst.operands[i].postind)
6864     {
6865       assert (inst.operands[i].writeback);
6866       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
6867       constraint (is_t, _("cannot use post-indexing with this instruction"));
6868
6869       if (is_d)
6870         inst.instruction |= 0x00200000;
6871       else
6872         inst.instruction |= 0x00000900;
6873       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
6874     }
6875   else /* unindexed - only for coprocessor */
6876     inst.error = _("instruction does not accept unindexed addressing");
6877 }
6878
6879 /* Table of Thumb instructions which exist in both 16- and 32-bit
6880    encodings (the latter only in post-V6T2 cores).  The index is the
6881    value used in the insns table below.  When there is more than one
6882    possible 16-bit encoding for the instruction, this table always
6883    holds variant (1).
6884    Also contains several pseudo-instructions used during relaxation.  */
6885 #define T16_32_TAB                              \
6886   X(adc,   4140, eb400000),                     \
6887   X(adcs,  4140, eb500000),                     \
6888   X(add,   1c00, eb000000),                     \
6889   X(adds,  1c00, eb100000),                     \
6890   X(addi,  0000, f1000000),                     \
6891   X(addis, 0000, f1100000),                     \
6892   X(add_pc,000f, f20f0000),                     \
6893   X(add_sp,000d, f10d0000),                     \
6894   X(adr,   000f, f20f0000),                     \
6895   X(and,   4000, ea000000),                     \
6896   X(ands,  4000, ea100000),                     \
6897   X(asr,   1000, fa40f000),                     \
6898   X(asrs,  1000, fa50f000),                     \
6899   X(b,     e000, f000b000),                     \
6900   X(bcond, d000, f0008000),                     \
6901   X(bic,   4380, ea200000),                     \
6902   X(bics,  4380, ea300000),                     \
6903   X(cmn,   42c0, eb100f00),                     \
6904   X(cmp,   2800, ebb00f00),                     \
6905   X(cpsie, b660, f3af8400),                     \
6906   X(cpsid, b670, f3af8600),                     \
6907   X(cpy,   4600, ea4f0000),                     \
6908   X(dec_sp,80dd, f1bd0d00),                     \
6909   X(eor,   4040, ea800000),                     \
6910   X(eors,  4040, ea900000),                     \
6911   X(inc_sp,00dd, f10d0d00),                     \
6912   X(ldmia, c800, e8900000),                     \
6913   X(ldr,   6800, f8500000),                     \
6914   X(ldrb,  7800, f8100000),                     \
6915   X(ldrh,  8800, f8300000),                     \
6916   X(ldrsb, 5600, f9100000),                     \
6917   X(ldrsh, 5e00, f9300000),                     \
6918   X(ldr_pc,4800, f85f0000),                     \
6919   X(ldr_pc2,4800, f85f0000),                    \
6920   X(ldr_sp,9800, f85d0000),                     \
6921   X(lsl,   0000, fa00f000),                     \
6922   X(lsls,  0000, fa10f000),                     \
6923   X(lsr,   0800, fa20f000),                     \
6924   X(lsrs,  0800, fa30f000),                     \
6925   X(mov,   2000, ea4f0000),                     \
6926   X(movs,  2000, ea5f0000),                     \
6927   X(mul,   4340, fb00f000),                     \
6928   X(muls,  4340, ffffffff), /* no 32b muls */   \
6929   X(mvn,   43c0, ea6f0000),                     \
6930   X(mvns,  43c0, ea7f0000),                     \
6931   X(neg,   4240, f1c00000), /* rsb #0 */        \
6932   X(negs,  4240, f1d00000), /* rsbs #0 */       \
6933   X(orr,   4300, ea400000),                     \
6934   X(orrs,  4300, ea500000),                     \
6935   X(pop,   bc00, e8bd0000), /* ldmia sp!,... */ \
6936   X(push,  b400, e92d0000), /* stmdb sp!,... */ \
6937   X(rev,   ba00, fa90f080),                     \
6938   X(rev16, ba40, fa90f090),                     \
6939   X(revsh, bac0, fa90f0b0),                     \
6940   X(ror,   41c0, fa60f000),                     \
6941   X(rors,  41c0, fa70f000),                     \
6942   X(sbc,   4180, eb600000),                     \
6943   X(sbcs,  4180, eb700000),                     \
6944   X(stmia, c000, e8800000),                     \
6945   X(str,   6000, f8400000),                     \
6946   X(strb,  7000, f8000000),                     \
6947   X(strh,  8000, f8200000),                     \
6948   X(str_sp,9000, f84d0000),                     \
6949   X(sub,   1e00, eba00000),                     \
6950   X(subs,  1e00, ebb00000),                     \
6951   X(subi,  8000, f1a00000),                     \
6952   X(subis, 8000, f1b00000),                     \
6953   X(sxtb,  b240, fa4ff080),                     \
6954   X(sxth,  b200, fa0ff080),                     \
6955   X(tst,   4200, ea100f00),                     \
6956   X(uxtb,  b2c0, fa5ff080),                     \
6957   X(uxth,  b280, fa1ff080),                     \
6958   X(nop,   bf00, f3af8000),                     \
6959   X(yield, bf10, f3af8001),                     \
6960   X(wfe,   bf20, f3af8002),                     \
6961   X(wfi,   bf30, f3af8003),                     \
6962   X(sev,   bf40, f3af9004), /* typo, 8004? */
6963
6964 /* To catch errors in encoding functions, the codes are all offset by
6965    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
6966    as 16-bit instructions.  */
6967 #define X(a,b,c) T_MNEM_##a
6968 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
6969 #undef X
6970
6971 #define X(a,b,c) 0x##b
6972 static const unsigned short thumb_op16[] = { T16_32_TAB };
6973 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
6974 #undef X
6975
6976 #define X(a,b,c) 0x##c
6977 static const unsigned int thumb_op32[] = { T16_32_TAB };
6978 #define THUMB_OP32(n) (thumb_op32[(n) - (T16_32_OFFSET + 1)])
6979 #define THUMB_SETS_FLAGS(n) (THUMB_OP32 (n) & 0x00100000)
6980 #undef X
6981 #undef T16_32_TAB
6982
6983 /* Thumb instruction encoders, in alphabetical order.  */
6984
6985 /* ADDW or SUBW.  */
6986 static void
6987 do_t_add_sub_w (void)
6988 {
6989   int Rd, Rn;
6990
6991   Rd = inst.operands[0].reg;
6992   Rn = inst.operands[1].reg;
6993
6994   constraint (Rd == 15, _("PC not allowed as destination"));
6995   inst.instruction |= (Rn << 16) | (Rd << 8);
6996   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
6997 }
6998
6999 /* Parse an add or subtract instruction.  We get here with inst.instruction
7000    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
7001
7002 static void
7003 do_t_add_sub (void)
7004 {
7005   int Rd, Rs, Rn;
7006
7007   Rd = inst.operands[0].reg;
7008   Rs = (inst.operands[1].present
7009         ? inst.operands[1].reg    /* Rd, Rs, foo */
7010         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
7011
7012   if (unified_syntax)
7013     {
7014       bfd_boolean flags;
7015       bfd_boolean narrow;
7016       int opcode;
7017
7018       flags = (inst.instruction == T_MNEM_adds
7019                || inst.instruction == T_MNEM_subs);
7020       if (flags)
7021         narrow = (current_it_mask == 0);
7022       else
7023         narrow = (current_it_mask != 0);
7024       if (!inst.operands[2].isreg)
7025         {
7026           opcode = 0;
7027           if (inst.size_req != 4)
7028             {
7029               int add;
7030
7031               add = (inst.instruction == T_MNEM_add
7032                      || inst.instruction == T_MNEM_adds);
7033               /* Attempt to use a narrow opcode, with relaxation if
7034                  appropriate.  */
7035               if (Rd == REG_SP && Rs == REG_SP && !flags)
7036                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
7037               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
7038                 opcode = T_MNEM_add_sp;
7039               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
7040                 opcode = T_MNEM_add_pc;
7041               else if (Rd <= 7 && Rs <= 7 && narrow)
7042                 {
7043                   if (flags)
7044                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
7045                   else
7046                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
7047                 }
7048               if (opcode)
7049                 {
7050                   inst.instruction = THUMB_OP16(opcode);
7051                   inst.instruction |= (Rd << 4) | Rs;
7052                   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
7053                   if (inst.size_req != 2)
7054                     inst.relax = opcode;
7055                 }
7056               else
7057                 constraint (inst.size_req == 2, BAD_HIREG);
7058             }
7059           if (inst.size_req == 4
7060               || (inst.size_req != 2 && !opcode))
7061             {
7062               /* ??? Convert large immediates to addw/subw.  */
7063               inst.instruction = THUMB_OP32 (inst.instruction);
7064               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7065               inst.instruction |= inst.operands[0].reg << 8;
7066               inst.instruction |= inst.operands[1].reg << 16;
7067               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7068             }
7069         }
7070       else
7071         {
7072           Rn = inst.operands[2].reg;
7073           /* See if we can do this with a 16-bit instruction.  */
7074           if (!inst.operands[2].shifted && inst.size_req != 4)
7075             {
7076               if (Rd > 7 || Rs > 7 || Rn > 7)
7077                 narrow = FALSE;
7078
7079               if (narrow)
7080                 {
7081                   inst.instruction = ((inst.instruction == T_MNEM_adds
7082                                        || inst.instruction == T_MNEM_add)
7083                                       ? T_OPCODE_ADD_R3
7084                                       : T_OPCODE_SUB_R3);
7085                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
7086                   return;
7087                 }
7088
7089               if (inst.instruction == T_MNEM_add)
7090                 {
7091                   if (Rd == Rs)
7092                     {
7093                       inst.instruction = T_OPCODE_ADD_HI;
7094                       inst.instruction |= (Rd & 8) << 4;
7095                       inst.instruction |= (Rd & 7);
7096                       inst.instruction |= Rn << 3;
7097                       return;
7098                     }
7099                   /* ... because addition is commutative! */
7100                   else if (Rd == Rn)
7101                     {
7102                       inst.instruction = T_OPCODE_ADD_HI;
7103                       inst.instruction |= (Rd & 8) << 4;
7104                       inst.instruction |= (Rd & 7);
7105                       inst.instruction |= Rs << 3;
7106                       return;
7107                     }
7108                 }
7109             }
7110           /* If we get here, it can't be done in 16 bits.  */
7111           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
7112                       _("shift must be constant"));
7113           inst.instruction = THUMB_OP32 (inst.instruction);
7114           inst.instruction |= Rd << 8;
7115           inst.instruction |= Rs << 16;
7116           encode_thumb32_shifted_operand (2);
7117         }
7118     }
7119   else
7120     {
7121       constraint (inst.instruction == T_MNEM_adds
7122                   || inst.instruction == T_MNEM_subs,
7123                   BAD_THUMB32);
7124
7125       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
7126         {
7127           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
7128                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
7129                       BAD_HIREG);
7130
7131           inst.instruction = (inst.instruction == T_MNEM_add
7132                               ? 0x0000 : 0x8000);
7133           inst.instruction |= (Rd << 4) | Rs;
7134           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
7135           return;
7136         }
7137
7138       Rn = inst.operands[2].reg;
7139       constraint (inst.operands[2].shifted, _("unshifted register required"));
7140
7141       /* We now have Rd, Rs, and Rn set to registers.  */
7142       if (Rd > 7 || Rs > 7 || Rn > 7)
7143         {
7144           /* Can't do this for SUB.      */
7145           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
7146           inst.instruction = T_OPCODE_ADD_HI;
7147           inst.instruction |= (Rd & 8) << 4;
7148           inst.instruction |= (Rd & 7);
7149           if (Rs == Rd)
7150             inst.instruction |= Rn << 3;
7151           else if (Rn == Rd)
7152             inst.instruction |= Rs << 3;
7153           else
7154             constraint (1, _("dest must overlap one source register"));
7155         }
7156       else
7157         {
7158           inst.instruction = (inst.instruction == T_MNEM_add
7159                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
7160           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
7161         }
7162     }
7163 }
7164
7165 static void
7166 do_t_adr (void)
7167 {
7168   if (unified_syntax && inst.size_req == 0 && inst.operands[0].reg <= 7)
7169     {
7170       /* Defer to section relaxation.  */
7171       inst.relax = inst.instruction;
7172       inst.instruction = THUMB_OP16 (inst.instruction);
7173       inst.instruction |= inst.operands[0].reg << 4;
7174     }
7175   else if (unified_syntax && inst.size_req != 2)
7176     {
7177       /* Generate a 32-bit opcode.  */
7178       inst.instruction = THUMB_OP32 (inst.instruction);
7179       inst.instruction |= inst.operands[0].reg << 8;
7180       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
7181       inst.reloc.pc_rel = 1;
7182     }
7183   else
7184     {
7185       /* Generate a 16-bit opcode.  */
7186       inst.instruction = THUMB_OP16 (inst.instruction);
7187       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
7188       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
7189       inst.reloc.pc_rel = 1;
7190
7191       inst.instruction |= inst.operands[0].reg << 4;
7192     }
7193 }
7194
7195 /* Arithmetic instructions for which there is just one 16-bit
7196    instruction encoding, and it allows only two low registers.
7197    For maximal compatibility with ARM syntax, we allow three register
7198    operands even when Thumb-32 instructions are not available, as long
7199    as the first two are identical.  For instance, both "sbc r0,r1" and
7200    "sbc r0,r0,r1" are allowed.  */
7201 static void
7202 do_t_arit3 (void)
7203 {
7204   int Rd, Rs, Rn;
7205
7206   Rd = inst.operands[0].reg;
7207   Rs = (inst.operands[1].present
7208         ? inst.operands[1].reg    /* Rd, Rs, foo */
7209         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
7210   Rn = inst.operands[2].reg;
7211
7212   if (unified_syntax)
7213     {
7214       if (!inst.operands[2].isreg)
7215         {
7216           /* For an immediate, we always generate a 32-bit opcode;
7217              section relaxation will shrink it later if possible.  */
7218           inst.instruction = THUMB_OP32 (inst.instruction);
7219           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7220           inst.instruction |= Rd << 8;
7221           inst.instruction |= Rs << 16;
7222           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7223         }
7224       else
7225         {
7226           bfd_boolean narrow;
7227
7228           /* See if we can do this with a 16-bit instruction.  */
7229           if (THUMB_SETS_FLAGS (inst.instruction))
7230             narrow = current_it_mask == 0;
7231           else
7232             narrow = current_it_mask != 0;
7233
7234           if (Rd > 7 || Rn > 7 || Rs > 7)
7235             narrow = FALSE;
7236           if (inst.operands[2].shifted)
7237             narrow = FALSE;
7238           if (inst.size_req == 4)
7239             narrow = FALSE;
7240
7241           if (narrow
7242               && Rd == Rs)
7243             {
7244               inst.instruction = THUMB_OP16 (inst.instruction);
7245               inst.instruction |= Rd;
7246               inst.instruction |= Rn << 3;
7247               return;
7248             }
7249
7250           /* If we get here, it can't be done in 16 bits.  */
7251           constraint (inst.operands[2].shifted
7252                       && inst.operands[2].immisreg,
7253                       _("shift must be constant"));
7254           inst.instruction = THUMB_OP32 (inst.instruction);
7255           inst.instruction |= Rd << 8;
7256           inst.instruction |= Rs << 16;
7257           encode_thumb32_shifted_operand (2);
7258         }
7259     }
7260   else
7261     {
7262       /* On its face this is a lie - the instruction does set the
7263          flags.  However, the only supported mnemonic in this mode
7264          says it doesn't.  */
7265       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
7266
7267       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
7268                   _("unshifted register required"));
7269       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
7270       constraint (Rd != Rs,
7271                   _("dest and source1 must be the same register"));
7272
7273       inst.instruction = THUMB_OP16 (inst.instruction);
7274       inst.instruction |= Rd;
7275       inst.instruction |= Rn << 3;
7276     }
7277 }
7278
7279 /* Similarly, but for instructions where the arithmetic operation is
7280    commutative, so we can allow either of them to be different from
7281    the destination operand in a 16-bit instruction.  For instance, all
7282    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
7283    accepted.  */
7284 static void
7285 do_t_arit3c (void)
7286 {
7287   int Rd, Rs, Rn;
7288
7289   Rd = inst.operands[0].reg;
7290   Rs = (inst.operands[1].present
7291         ? inst.operands[1].reg    /* Rd, Rs, foo */
7292         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
7293   Rn = inst.operands[2].reg;
7294
7295   if (unified_syntax)
7296     {
7297       if (!inst.operands[2].isreg)
7298         {
7299           /* For an immediate, we always generate a 32-bit opcode;
7300              section relaxation will shrink it later if possible.  */
7301           inst.instruction = THUMB_OP32 (inst.instruction);
7302           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7303           inst.instruction |= Rd << 8;
7304           inst.instruction |= Rs << 16;
7305           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7306         }
7307       else
7308         {
7309           bfd_boolean narrow;
7310
7311           /* See if we can do this with a 16-bit instruction.  */
7312           if (THUMB_SETS_FLAGS (inst.instruction))
7313             narrow = current_it_mask == 0;
7314           else
7315             narrow = current_it_mask != 0;
7316
7317           if (Rd > 7 || Rn > 7 || Rs > 7)
7318             narrow = FALSE;
7319           if (inst.operands[2].shifted)
7320             narrow = FALSE;
7321           if (inst.size_req == 4)
7322             narrow = FALSE;
7323
7324           if (narrow)
7325             {
7326               if (Rd == Rs)
7327                 {
7328                   inst.instruction = THUMB_OP16 (inst.instruction);
7329                   inst.instruction |= Rd;
7330                   inst.instruction |= Rn << 3;
7331                   return;
7332                 }
7333               if (Rd == Rn)
7334                 {
7335                   inst.instruction = THUMB_OP16 (inst.instruction);
7336                   inst.instruction |= Rd;
7337                   inst.instruction |= Rs << 3;
7338                   return;
7339                 }
7340             }
7341
7342           /* If we get here, it can't be done in 16 bits.  */
7343           constraint (inst.operands[2].shifted
7344                       && inst.operands[2].immisreg,
7345                       _("shift must be constant"));
7346           inst.instruction = THUMB_OP32 (inst.instruction);
7347           inst.instruction |= Rd << 8;
7348           inst.instruction |= Rs << 16;
7349           encode_thumb32_shifted_operand (2);
7350         }
7351     }
7352   else
7353     {
7354       /* On its face this is a lie - the instruction does set the
7355          flags.  However, the only supported mnemonic in this mode
7356          says it doesn't.  */
7357       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
7358
7359       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
7360                   _("unshifted register required"));
7361       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
7362
7363       inst.instruction = THUMB_OP16 (inst.instruction);
7364       inst.instruction |= Rd;
7365
7366       if (Rd == Rs)
7367         inst.instruction |= Rn << 3;
7368       else if (Rd == Rn)
7369         inst.instruction |= Rs << 3;
7370       else
7371         constraint (1, _("dest must overlap one source register"));
7372     }
7373 }
7374
7375 static void
7376 do_t_barrier (void)
7377 {
7378   if (inst.operands[0].present)
7379     {
7380       constraint ((inst.instruction & 0xf0) != 0x40
7381                   && inst.operands[0].imm != 0xf,
7382                   "bad barrier type");
7383       inst.instruction |= inst.operands[0].imm;
7384     }
7385   else
7386     inst.instruction |= 0xf;
7387 }
7388
7389 static void
7390 do_t_bfc (void)
7391 {
7392   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
7393   constraint (msb > 32, _("bit-field extends past end of register"));
7394   /* The instruction encoding stores the LSB and MSB,
7395      not the LSB and width.  */
7396   inst.instruction |= inst.operands[0].reg << 8;
7397   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
7398   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
7399   inst.instruction |= msb - 1;
7400 }
7401
7402 static void
7403 do_t_bfi (void)
7404 {
7405   unsigned int msb;
7406
7407   /* #0 in second position is alternative syntax for bfc, which is
7408      the same instruction but with REG_PC in the Rm field.  */
7409   if (!inst.operands[1].isreg)
7410     inst.operands[1].reg = REG_PC;
7411
7412   msb = inst.operands[2].imm + inst.operands[3].imm;
7413   constraint (msb > 32, _("bit-field extends past end of register"));
7414   /* The instruction encoding stores the LSB and MSB,
7415      not the LSB and width.  */
7416   inst.instruction |= inst.operands[0].reg << 8;
7417   inst.instruction |= inst.operands[1].reg << 16;
7418   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
7419   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
7420   inst.instruction |= msb - 1;
7421 }
7422
7423 static void
7424 do_t_bfx (void)
7425 {
7426   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
7427               _("bit-field extends past end of register"));
7428   inst.instruction |= inst.operands[0].reg << 8;
7429   inst.instruction |= inst.operands[1].reg << 16;
7430   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
7431   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
7432   inst.instruction |= inst.operands[3].imm - 1;
7433 }
7434
7435 /* ARM V5 Thumb BLX (argument parse)
7436         BLX <target_addr>       which is BLX(1)
7437         BLX <Rm>                which is BLX(2)
7438    Unfortunately, there are two different opcodes for this mnemonic.
7439    So, the insns[].value is not used, and the code here zaps values
7440         into inst.instruction.
7441
7442    ??? How to take advantage of the additional two bits of displacement
7443    available in Thumb32 mode?  Need new relocation?  */
7444
7445 static void
7446 do_t_blx (void)
7447 {
7448   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
7449   if (inst.operands[0].isreg)
7450     /* We have a register, so this is BLX(2).  */
7451     inst.instruction |= inst.operands[0].reg << 3;
7452   else
7453     {
7454       /* No register.  This must be BLX(1).  */
7455       inst.instruction = 0xf000e800;
7456 #ifdef OBJ_ELF
7457       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
7458         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
7459       else
7460 #endif
7461         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BLX;
7462       inst.reloc.pc_rel = 1;
7463     }
7464 }
7465
7466 static void
7467 do_t_branch (void)
7468 {
7469   int opcode;
7470   int cond;
7471
7472   if (current_it_mask)
7473     {
7474       /* Conditional branches inside IT blocks are encoded as unconditional
7475          branches.  */
7476       cond = COND_ALWAYS;
7477       /* A branch must be the last instruction in an IT block.  */
7478       constraint (current_it_mask != 0x10, BAD_BRANCH);
7479     }
7480   else
7481     cond = inst.cond;
7482
7483   if (cond != COND_ALWAYS)
7484     opcode = T_MNEM_bcond;
7485   else
7486     opcode = inst.instruction;
7487
7488   if (unified_syntax && inst.size_req == 4)
7489     {
7490       inst.instruction = THUMB_OP32(opcode);
7491       if (cond == COND_ALWAYS)
7492         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH25;
7493       else
7494         {
7495           assert (cond != 0xF);
7496           inst.instruction |= cond << 22;
7497           inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH20;
7498         }
7499     }
7500   else
7501     {
7502       inst.instruction = THUMB_OP16(opcode);
7503       if (cond == COND_ALWAYS)
7504         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH12;
7505       else
7506         {
7507           inst.instruction |= cond << 8;
7508           inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH9;
7509         }
7510       /* Allow section relaxation.  */
7511       if (unified_syntax && inst.size_req != 2)
7512         inst.relax = opcode;
7513     }
7514
7515   inst.reloc.pc_rel = 1;
7516 }
7517
7518 static void
7519 do_t_bkpt (void)
7520 {
7521   constraint (inst.cond != COND_ALWAYS,
7522               _("instruction is always unconditional"));
7523   if (inst.operands[0].present)
7524     {
7525       constraint (inst.operands[0].imm > 255,
7526                   _("immediate value out of range"));
7527       inst.instruction |= inst.operands[0].imm;
7528     }
7529 }
7530
7531 static void
7532 do_t_branch23 (void)
7533 {
7534   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
7535   inst.reloc.type   = BFD_RELOC_THUMB_PCREL_BRANCH23;
7536   inst.reloc.pc_rel = 1;
7537
7538   /* If the destination of the branch is a defined symbol which does not have
7539      the THUMB_FUNC attribute, then we must be calling a function which has
7540      the (interfacearm) attribute.  We look for the Thumb entry point to that
7541      function and change the branch to refer to that function instead.  */
7542   if (   inst.reloc.exp.X_op == O_symbol
7543       && inst.reloc.exp.X_add_symbol != NULL
7544       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
7545       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
7546     inst.reloc.exp.X_add_symbol =
7547       find_real_start (inst.reloc.exp.X_add_symbol);
7548 }
7549
7550 static void
7551 do_t_bx (void)
7552 {
7553   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
7554   inst.instruction |= inst.operands[0].reg << 3;
7555   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
7556      should cause the alignment to be checked once it is known.  This is
7557      because BX PC only works if the instruction is word aligned.  */
7558 }
7559
7560 static void
7561 do_t_bxj (void)
7562 {
7563   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
7564   if (inst.operands[0].reg == REG_PC)
7565     as_tsktsk (_("use of r15 in bxj is not really useful"));
7566
7567   inst.instruction |= inst.operands[0].reg << 16;
7568 }
7569
7570 static void
7571 do_t_clz (void)
7572 {
7573   inst.instruction |= inst.operands[0].reg << 8;
7574   inst.instruction |= inst.operands[1].reg << 16;
7575   inst.instruction |= inst.operands[1].reg;
7576 }
7577
7578 static void
7579 do_t_cps (void)
7580 {
7581   constraint (current_it_mask, BAD_NOT_IT);
7582   inst.instruction |= inst.operands[0].imm;
7583 }
7584
7585 static void
7586 do_t_cpsi (void)
7587 {
7588   constraint (current_it_mask, BAD_NOT_IT);
7589   if (unified_syntax
7590       && (inst.operands[1].present || inst.size_req == 4)
7591       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
7592     {
7593       unsigned int imod = (inst.instruction & 0x0030) >> 4;
7594       inst.instruction = 0xf3af8000;
7595       inst.instruction |= imod << 9;
7596       inst.instruction |= inst.operands[0].imm << 5;
7597       if (inst.operands[1].present)
7598         inst.instruction |= 0x100 | inst.operands[1].imm;
7599     }
7600   else
7601     {
7602       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
7603                   && (inst.operands[0].imm & 4),
7604                   _("selected processor does not support 'A' form "
7605                     "of this instruction"));
7606       constraint (inst.operands[1].present || inst.size_req == 4,
7607                   _("Thumb does not support the 2-argument "
7608                     "form of this instruction"));
7609       inst.instruction |= inst.operands[0].imm;
7610     }
7611 }
7612
7613 /* THUMB CPY instruction (argument parse).  */
7614
7615 static void
7616 do_t_cpy (void)
7617 {
7618   if (inst.size_req == 4)
7619     {
7620       inst.instruction = THUMB_OP32 (T_MNEM_mov);
7621       inst.instruction |= inst.operands[0].reg << 8;
7622       inst.instruction |= inst.operands[1].reg;
7623     }
7624   else
7625     {
7626       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
7627       inst.instruction |= (inst.operands[0].reg & 0x7);
7628       inst.instruction |= inst.operands[1].reg << 3;
7629     }
7630 }
7631
7632 static void
7633 do_t_czb (void)
7634 {
7635   constraint (current_it_mask, BAD_NOT_IT);
7636   constraint (inst.operands[0].reg > 7, BAD_HIREG);
7637   inst.instruction |= inst.operands[0].reg;
7638   inst.reloc.pc_rel = 1;
7639   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
7640 }
7641
7642 static void
7643 do_t_dbg (void)
7644 {
7645   inst.instruction |= inst.operands[0].imm;
7646 }
7647
7648 static void
7649 do_t_div (void)
7650 {
7651   if (!inst.operands[1].present)
7652     inst.operands[1].reg = inst.operands[0].reg;
7653   inst.instruction |= inst.operands[0].reg << 8;
7654   inst.instruction |= inst.operands[1].reg << 16;
7655   inst.instruction |= inst.operands[2].reg;
7656 }
7657
7658 static void
7659 do_t_hint (void)
7660 {
7661   if (unified_syntax && inst.size_req == 4)
7662     inst.instruction = THUMB_OP32 (inst.instruction);
7663   else
7664     inst.instruction = THUMB_OP16 (inst.instruction);
7665 }
7666
7667 static void
7668 do_t_it (void)
7669 {
7670   unsigned int cond = inst.operands[0].imm;
7671
7672   constraint (current_it_mask, BAD_NOT_IT);
7673   current_it_mask = (inst.instruction & 0xf) | 0x10;
7674   current_cc = cond;
7675
7676   /* If the condition is a negative condition, invert the mask.  */
7677   if ((cond & 0x1) == 0x0)
7678     {
7679       unsigned int mask = inst.instruction & 0x000f;
7680
7681       if ((mask & 0x7) == 0)
7682         /* no conversion needed */;
7683       else if ((mask & 0x3) == 0)
7684         mask ^= 0x8;
7685       else if ((mask & 0x1) == 0)
7686         mask ^= 0xC;
7687       else
7688         mask ^= 0xE;
7689
7690       inst.instruction &= 0xfff0;
7691       inst.instruction |= mask;
7692     }
7693
7694   inst.instruction |= cond << 4;
7695 }
7696
7697 static void
7698 do_t_ldmstm (void)
7699 {
7700   /* This really doesn't seem worth it.  */
7701   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
7702               _("expression too complex"));
7703   constraint (inst.operands[1].writeback,
7704               _("Thumb load/store multiple does not support {reglist}^"));
7705
7706   if (unified_syntax)
7707     {
7708       /* See if we can use a 16-bit instruction.  */
7709       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
7710           && inst.size_req != 4
7711           && inst.operands[0].reg <= 7
7712           && !(inst.operands[1].imm & ~0xff)
7713           && (inst.instruction == T_MNEM_stmia
7714               ? inst.operands[0].writeback
7715               : (inst.operands[0].writeback
7716                  == !(inst.operands[1].imm & (1 << inst.operands[0].reg)))))
7717         {
7718           if (inst.instruction == T_MNEM_stmia
7719               && (inst.operands[1].imm & (1 << inst.operands[0].reg))
7720               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
7721             as_warn (_("value stored for r%d is UNPREDICTABLE"),
7722                      inst.operands[0].reg);
7723
7724           inst.instruction = THUMB_OP16 (inst.instruction);
7725           inst.instruction |= inst.operands[0].reg << 8;
7726           inst.instruction |= inst.operands[1].imm;
7727         }
7728       else
7729         {
7730           if (inst.operands[1].imm & (1 << 13))
7731             as_warn (_("SP should not be in register list"));
7732           if (inst.instruction == T_MNEM_stmia)
7733             {
7734               if (inst.operands[1].imm & (1 << 15))
7735                 as_warn (_("PC should not be in register list"));
7736               if (inst.operands[1].imm & (1 << inst.operands[0].reg))
7737                 as_warn (_("value stored for r%d is UNPREDICTABLE"),
7738                          inst.operands[0].reg);
7739             }
7740           else
7741             {
7742               if (inst.operands[1].imm & (1 << 14)
7743                   && inst.operands[1].imm & (1 << 15))
7744                 as_warn (_("LR and PC should not both be in register list"));
7745               if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
7746                   && inst.operands[0].writeback)
7747                 as_warn (_("base register should not be in register list "
7748                            "when written back"));
7749             }
7750           if (inst.instruction < 0xffff)
7751             inst.instruction = THUMB_OP32 (inst.instruction);
7752           inst.instruction |= inst.operands[0].reg << 16;
7753           inst.instruction |= inst.operands[1].imm;
7754           if (inst.operands[0].writeback)
7755             inst.instruction |= WRITE_BACK;
7756         }
7757     }
7758   else
7759     {
7760       constraint (inst.operands[0].reg > 7
7761                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
7762       if (inst.instruction == T_MNEM_stmia)
7763         {
7764           if (!inst.operands[0].writeback)
7765             as_warn (_("this instruction will write back the base register"));
7766           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
7767               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
7768             as_warn (_("value stored for r%d is UNPREDICTABLE"),
7769                      inst.operands[0].reg);
7770         }
7771       else
7772         {
7773           if (!inst.operands[0].writeback
7774               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
7775             as_warn (_("this instruction will write back the base register"));
7776           else if (inst.operands[0].writeback
7777                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
7778             as_warn (_("this instruction will not write back the base register"));
7779         }
7780
7781       inst.instruction = THUMB_OP16 (inst.instruction);
7782       inst.instruction |= inst.operands[0].reg << 8;
7783       inst.instruction |= inst.operands[1].imm;
7784     }
7785 }
7786
7787 static void
7788 do_t_ldrex (void)
7789 {
7790   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
7791               || inst.operands[1].postind || inst.operands[1].writeback
7792               || inst.operands[1].immisreg || inst.operands[1].shifted
7793               || inst.operands[1].negative,
7794               BAD_ADDR_MODE);
7795
7796   inst.instruction |= inst.operands[0].reg << 12;
7797   inst.instruction |= inst.operands[1].reg << 16;
7798   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
7799 }
7800
7801 static void
7802 do_t_ldrexd (void)
7803 {
7804   if (!inst.operands[1].present)
7805     {
7806       constraint (inst.operands[0].reg == REG_LR,
7807                   _("r14 not allowed as first register "
7808                     "when second register is omitted"));
7809       inst.operands[1].reg = inst.operands[0].reg + 1;
7810     }
7811   constraint (inst.operands[0].reg == inst.operands[1].reg,
7812               BAD_OVERLAP);
7813
7814   inst.instruction |= inst.operands[0].reg << 12;
7815   inst.instruction |= inst.operands[1].reg << 8;
7816   inst.instruction |= inst.operands[2].reg << 16;
7817 }
7818
7819 static void
7820 do_t_ldst (void)
7821 {
7822   unsigned long opcode;
7823   int Rn;
7824
7825   opcode = inst.instruction;
7826   if (unified_syntax)
7827     {
7828       if (!inst.operands[1].isreg)
7829         {
7830           if (opcode <= 0xffff)
7831             inst.instruction = THUMB_OP32 (opcode);
7832           if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
7833             return;
7834         }
7835       if (inst.operands[1].isreg
7836           && !inst.operands[1].writeback
7837           && !inst.operands[1].shifted && !inst.operands[1].postind
7838           && !inst.operands[1].negative && inst.operands[0].reg <= 7
7839           && opcode <= 0xffff
7840           && inst.size_req != 4)
7841         {
7842           /* Insn may have a 16-bit form.  */
7843           Rn = inst.operands[1].reg;
7844           if (inst.operands[1].immisreg)
7845             {
7846               inst.instruction = THUMB_OP16 (opcode);
7847               /* [Rn, Ri] */
7848               if (Rn <= 7 && inst.operands[1].imm <= 7)
7849                 goto op16;
7850             }
7851           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
7852                     && opcode != T_MNEM_ldrsb)
7853                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
7854                    || (Rn == REG_SP && opcode == T_MNEM_str))
7855             {
7856               /* [Rn, #const] */
7857               if (Rn > 7)
7858                 {
7859                   if (Rn == REG_PC)
7860                     {
7861                       if (inst.reloc.pc_rel)
7862                         opcode = T_MNEM_ldr_pc2;
7863                       else
7864                         opcode = T_MNEM_ldr_pc;
7865                     }
7866                   else
7867                     {
7868                       if (opcode == T_MNEM_ldr)
7869                         opcode = T_MNEM_ldr_sp;
7870                       else
7871                         opcode = T_MNEM_str_sp;
7872                     }
7873                   inst.instruction = inst.operands[0].reg << 8;
7874                 }
7875               else
7876                 {
7877                   inst.instruction = inst.operands[0].reg;
7878                   inst.instruction |= inst.operands[1].reg << 3;
7879                 }
7880               inst.instruction |= THUMB_OP16 (opcode);
7881               if (inst.size_req == 2)
7882                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
7883               else
7884                 inst.relax = opcode;
7885               return;
7886             }
7887         }
7888       /* Definitely a 32-bit variant.  */
7889       inst.instruction = THUMB_OP32 (opcode);
7890       inst.instruction |= inst.operands[0].reg << 12;
7891       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
7892       return;
7893     }
7894
7895   constraint (inst.operands[0].reg > 7, BAD_HIREG);
7896
7897   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
7898     {
7899       /* Only [Rn,Rm] is acceptable.  */
7900       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
7901       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
7902                   || inst.operands[1].postind || inst.operands[1].shifted
7903                   || inst.operands[1].negative,
7904                   _("Thumb does not support this addressing mode"));
7905       inst.instruction = THUMB_OP16 (inst.instruction);
7906       goto op16;
7907     }
7908      
7909   inst.instruction = THUMB_OP16 (inst.instruction);
7910   if (!inst.operands[1].isreg)
7911     if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
7912       return;
7913
7914   constraint (!inst.operands[1].preind
7915               || inst.operands[1].shifted
7916               || inst.operands[1].writeback,
7917               _("Thumb does not support this addressing mode"));
7918   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
7919     {
7920       constraint (inst.instruction & 0x0600,
7921                   _("byte or halfword not valid for base register"));
7922       constraint (inst.operands[1].reg == REG_PC
7923                   && !(inst.instruction & THUMB_LOAD_BIT),
7924                   _("r15 based store not allowed"));
7925       constraint (inst.operands[1].immisreg,
7926                   _("invalid base register for register offset"));
7927
7928       if (inst.operands[1].reg == REG_PC)
7929         inst.instruction = T_OPCODE_LDR_PC;
7930       else if (inst.instruction & THUMB_LOAD_BIT)
7931         inst.instruction = T_OPCODE_LDR_SP;
7932       else
7933         inst.instruction = T_OPCODE_STR_SP;
7934
7935       inst.instruction |= inst.operands[0].reg << 8;
7936       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
7937       return;
7938     }
7939
7940   constraint (inst.operands[1].reg > 7, BAD_HIREG);
7941   if (!inst.operands[1].immisreg)
7942     {
7943       /* Immediate offset.  */
7944       inst.instruction |= inst.operands[0].reg;
7945       inst.instruction |= inst.operands[1].reg << 3;
7946       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
7947       return;
7948     }
7949
7950   /* Register offset.  */
7951   constraint (inst.operands[1].imm > 7, BAD_HIREG);
7952   constraint (inst.operands[1].negative,
7953               _("Thumb does not support this addressing mode"));
7954
7955  op16:
7956   switch (inst.instruction)
7957     {
7958     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
7959     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
7960     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
7961     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
7962     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
7963     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
7964     case 0x5600 /* ldrsb */:
7965     case 0x5e00 /* ldrsh */: break;
7966     default: abort ();
7967     }
7968
7969   inst.instruction |= inst.operands[0].reg;
7970   inst.instruction |= inst.operands[1].reg << 3;
7971   inst.instruction |= inst.operands[1].imm << 6;
7972 }
7973
7974 static void
7975 do_t_ldstd (void)
7976 {
7977   if (!inst.operands[1].present)
7978     {
7979       inst.operands[1].reg = inst.operands[0].reg + 1;
7980       constraint (inst.operands[0].reg == REG_LR,
7981                   _("r14 not allowed here"));
7982     }
7983   inst.instruction |= inst.operands[0].reg << 12;
7984   inst.instruction |= inst.operands[1].reg << 8;
7985   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
7986                             
7987 }
7988
7989 static void
7990 do_t_ldstt (void)
7991 {
7992   inst.instruction |= inst.operands[0].reg << 12;
7993   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
7994 }
7995
7996 static void
7997 do_t_mla (void)
7998 {
7999   inst.instruction |= inst.operands[0].reg << 8;
8000   inst.instruction |= inst.operands[1].reg << 16;
8001   inst.instruction |= inst.operands[2].reg;
8002   inst.instruction |= inst.operands[3].reg << 12;
8003 }
8004
8005 static void
8006 do_t_mlal (void)
8007 {
8008   inst.instruction |= inst.operands[0].reg << 12;
8009   inst.instruction |= inst.operands[1].reg << 8;
8010   inst.instruction |= inst.operands[2].reg << 16;
8011   inst.instruction |= inst.operands[3].reg;
8012 }
8013
8014 static void
8015 do_t_mov_cmp (void)
8016 {
8017   if (unified_syntax)
8018     {
8019       int r0off = (inst.instruction == T_MNEM_mov
8020                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
8021       unsigned long opcode;
8022       bfd_boolean narrow;
8023       bfd_boolean low_regs;
8024
8025       low_regs = (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7);
8026       opcode = inst.instruction;
8027       if (current_it_mask)
8028         narrow = opcode != T_MNEM_movs;
8029       else
8030         narrow = opcode != T_MNEM_movs || low_regs;
8031       if (inst.size_req == 4
8032           || inst.operands[1].shifted)
8033         narrow = FALSE;
8034
8035       if (!inst.operands[1].isreg)
8036         {
8037           /* Immediate operand.  */
8038           if (current_it_mask == 0 && opcode == T_MNEM_mov)
8039             narrow = 0;
8040           if (low_regs && narrow)
8041             {
8042               inst.instruction = THUMB_OP16 (opcode);
8043               inst.instruction |= inst.operands[0].reg << 8;
8044               if (inst.size_req == 2)
8045                 inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
8046               else
8047                 inst.relax = opcode;
8048             }
8049           else
8050             {
8051               inst.instruction = THUMB_OP32 (inst.instruction);
8052               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
8053               inst.instruction |= inst.operands[0].reg << r0off;
8054               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
8055             }
8056         }
8057       else if (!narrow)
8058         {
8059           inst.instruction = THUMB_OP32 (inst.instruction);
8060           inst.instruction |= inst.operands[0].reg << r0off;
8061           encode_thumb32_shifted_operand (1);
8062         }
8063       else
8064         switch (inst.instruction)
8065           {
8066           case T_MNEM_mov:
8067             inst.instruction = T_OPCODE_MOV_HR;
8068             inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
8069             inst.instruction |= (inst.operands[0].reg & 0x7);
8070             inst.instruction |= inst.operands[1].reg << 3;
8071             break;
8072
8073           case T_MNEM_movs:
8074             /* We know we have low registers at this point.
8075                Generate ADD Rd, Rs, #0.  */
8076             inst.instruction = T_OPCODE_ADD_I3;
8077             inst.instruction |= inst.operands[0].reg;
8078             inst.instruction |= inst.operands[1].reg << 3;
8079             break;
8080
8081           case T_MNEM_cmp:
8082             if (low_regs)
8083               {
8084                 inst.instruction = T_OPCODE_CMP_LR;
8085                 inst.instruction |= inst.operands[0].reg;
8086                 inst.instruction |= inst.operands[1].reg << 3;
8087               }
8088             else
8089               {
8090                 inst.instruction = T_OPCODE_CMP_HR;
8091                 inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
8092                 inst.instruction |= (inst.operands[0].reg & 0x7);
8093                 inst.instruction |= inst.operands[1].reg << 3;
8094               }
8095             break;
8096           }
8097       return;
8098     }
8099
8100   inst.instruction = THUMB_OP16 (inst.instruction);
8101   if (inst.operands[1].isreg)
8102     {
8103       if (inst.operands[0].reg < 8 && inst.operands[1].reg < 8)
8104         {
8105           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
8106              since a MOV instruction produces unpredictable results.  */
8107           if (inst.instruction == T_OPCODE_MOV_I8)
8108             inst.instruction = T_OPCODE_ADD_I3;
8109           else
8110             inst.instruction = T_OPCODE_CMP_LR;
8111
8112           inst.instruction |= inst.operands[0].reg;
8113           inst.instruction |= inst.operands[1].reg << 3;
8114         }
8115       else
8116         {
8117           if (inst.instruction == T_OPCODE_MOV_I8)
8118             inst.instruction = T_OPCODE_MOV_HR;
8119           else
8120             inst.instruction = T_OPCODE_CMP_HR;
8121           do_t_cpy ();
8122         }
8123     }
8124   else
8125     {
8126       constraint (inst.operands[0].reg > 7,
8127                   _("only lo regs allowed with immediate"));
8128       inst.instruction |= inst.operands[0].reg << 8;
8129       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
8130     }
8131 }
8132
8133 static void
8134 do_t_mov16 (void)
8135 {
8136   inst.instruction |= inst.operands[0].reg << 8;
8137   inst.instruction |= (inst.operands[1].imm & 0xf000) << 4;
8138   inst.instruction |= (inst.operands[1].imm & 0x0800) << 15;
8139   inst.instruction |= (inst.operands[1].imm & 0x0700) << 4;
8140   inst.instruction |= (inst.operands[1].imm & 0x00ff);
8141 }
8142
8143 static void
8144 do_t_mvn_tst (void)
8145 {
8146   if (unified_syntax)
8147     {
8148       int r0off = (inst.instruction == T_MNEM_mvn
8149                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
8150       bfd_boolean narrow;
8151
8152       if (inst.size_req == 4
8153           || inst.instruction > 0xffff
8154           || inst.operands[1].shifted
8155           || inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
8156         narrow = FALSE;
8157       else if (inst.instruction == T_MNEM_cmn)
8158         narrow = TRUE;
8159       else if (THUMB_SETS_FLAGS (inst.instruction))
8160         narrow = (current_it_mask == 0);
8161       else
8162         narrow = (current_it_mask != 0);
8163
8164       if (!inst.operands[1].isreg)
8165         {
8166           /* For an immediate, we always generate a 32-bit opcode;
8167              section relaxation will shrink it later if possible.  */
8168           if (inst.instruction < 0xffff)
8169             inst.instruction = THUMB_OP32 (inst.instruction);
8170           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
8171           inst.instruction |= inst.operands[0].reg << r0off;
8172           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
8173         }
8174       else
8175         {
8176           /* See if we can do this with a 16-bit instruction.  */
8177           if (narrow)
8178             {
8179               inst.instruction = THUMB_OP16 (inst.instruction);
8180               inst.instruction |= inst.operands[0].reg;
8181               inst.instruction |= inst.operands[1].reg << 3;
8182             }
8183           else
8184             {
8185               constraint (inst.operands[1].shifted
8186                           && inst.operands[1].immisreg,
8187                           _("shift must be constant"));
8188               if (inst.instruction < 0xffff)
8189                 inst.instruction = THUMB_OP32 (inst.instruction);
8190               inst.instruction |= inst.operands[0].reg << r0off;
8191               encode_thumb32_shifted_operand (1);
8192             }
8193         }
8194     }
8195   else
8196     {
8197       constraint (inst.instruction > 0xffff
8198                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
8199       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
8200                   _("unshifted register required"));
8201       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
8202                   BAD_HIREG);
8203
8204       inst.instruction = THUMB_OP16 (inst.instruction);
8205       inst.instruction |= inst.operands[0].reg;
8206       inst.instruction |= inst.operands[1].reg << 3;
8207     }
8208 }
8209
8210 static void
8211 do_t_mrs (void)
8212 {
8213   int flags;
8214   flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
8215   if (flags == 0)
8216     {
8217       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7m),
8218                   _("selected processor does not support "
8219                     "requested special purpose register"));
8220     }
8221   else
8222     {
8223       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1),
8224                   _("selected processor does not support "
8225                     "requested special purpose register %x"));
8226       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
8227       constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
8228                   _("'CPSR' or 'SPSR' expected"));
8229     }
8230     
8231   inst.instruction |= inst.operands[0].reg << 8;
8232   inst.instruction |= (flags & SPSR_BIT) >> 2;
8233   inst.instruction |= inst.operands[1].imm & 0xff;
8234 }
8235
8236 static void
8237 do_t_msr (void)
8238 {
8239   int flags;
8240
8241   constraint (!inst.operands[1].isreg,
8242               _("Thumb encoding does not support an immediate here"));
8243   flags = inst.operands[0].imm;
8244   if (flags & ~0xff)
8245     {
8246       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1),
8247                   _("selected processor does not support "
8248                     "requested special purpose register"));
8249     }
8250   else
8251     {
8252       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7m),
8253                   _("selected processor does not support "
8254                     "requested special purpose register"));
8255       flags |= PSR_f;
8256     }
8257   inst.instruction |= (flags & SPSR_BIT) >> 2;
8258   inst.instruction |= (flags & ~SPSR_BIT) >> 8;
8259   inst.instruction |= (flags & 0xff);
8260   inst.instruction |= inst.operands[1].reg << 16;
8261 }
8262
8263 static void
8264 do_t_mul (void)
8265 {
8266   if (!inst.operands[2].present)
8267     inst.operands[2].reg = inst.operands[0].reg;
8268
8269   /* There is no 32-bit MULS and no 16-bit MUL. */
8270   if (unified_syntax && inst.instruction == T_MNEM_mul)
8271     {
8272       inst.instruction = THUMB_OP32 (inst.instruction);
8273       inst.instruction |= inst.operands[0].reg << 8;
8274       inst.instruction |= inst.operands[1].reg << 16;
8275       inst.instruction |= inst.operands[2].reg << 0;
8276     }
8277   else
8278     {
8279       constraint (!unified_syntax
8280                   && inst.instruction == T_MNEM_muls, BAD_THUMB32);
8281       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
8282                   BAD_HIREG);
8283
8284       inst.instruction = THUMB_OP16 (inst.instruction);
8285       inst.instruction |= inst.operands[0].reg;
8286
8287       if (inst.operands[0].reg == inst.operands[1].reg)
8288         inst.instruction |= inst.operands[2].reg << 3;
8289       else if (inst.operands[0].reg == inst.operands[2].reg)
8290         inst.instruction |= inst.operands[1].reg << 3;
8291       else
8292         constraint (1, _("dest must overlap one source register"));
8293     }
8294 }
8295
8296 static void
8297 do_t_mull (void)
8298 {
8299   inst.instruction |= inst.operands[0].reg << 12;
8300   inst.instruction |= inst.operands[1].reg << 8;
8301   inst.instruction |= inst.operands[2].reg << 16;
8302   inst.instruction |= inst.operands[3].reg;
8303
8304   if (inst.operands[0].reg == inst.operands[1].reg)
8305     as_tsktsk (_("rdhi and rdlo must be different"));
8306 }
8307
8308 static void
8309 do_t_nop (void)
8310 {
8311   if (unified_syntax)
8312     {
8313       if (inst.size_req == 4 || inst.operands[0].imm > 15)
8314         {
8315           inst.instruction = THUMB_OP32 (inst.instruction);
8316           inst.instruction |= inst.operands[0].imm;
8317         }
8318       else
8319         {
8320           inst.instruction = THUMB_OP16 (inst.instruction);
8321           inst.instruction |= inst.operands[0].imm << 4;
8322         }
8323     }
8324   else
8325     {
8326       constraint (inst.operands[0].present,
8327                   _("Thumb does not support NOP with hints"));
8328       inst.instruction = 0x46c0;
8329     }
8330 }
8331
8332 static void
8333 do_t_neg (void)
8334 {
8335   if (unified_syntax)
8336     {
8337       bfd_boolean narrow;
8338
8339       if (THUMB_SETS_FLAGS (inst.instruction))
8340         narrow = (current_it_mask == 0);
8341       else
8342         narrow = (current_it_mask != 0);
8343       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
8344         narrow = FALSE;
8345       if (inst.size_req == 4)
8346         narrow = FALSE;
8347
8348       if (!narrow)
8349         {
8350           inst.instruction = THUMB_OP32 (inst.instruction);
8351           inst.instruction |= inst.operands[0].reg << 8;
8352           inst.instruction |= inst.operands[1].reg << 16;
8353         }
8354       else
8355         {
8356           inst.instruction = THUMB_OP16 (inst.instruction);
8357           inst.instruction |= inst.operands[0].reg;
8358           inst.instruction |= inst.operands[1].reg << 3;
8359         }
8360     }
8361   else
8362     {
8363       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
8364                   BAD_HIREG);
8365       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
8366
8367       inst.instruction = THUMB_OP16 (inst.instruction);
8368       inst.instruction |= inst.operands[0].reg;
8369       inst.instruction |= inst.operands[1].reg << 3;
8370     }
8371 }
8372
8373 static void
8374 do_t_pkhbt (void)
8375 {
8376   inst.instruction |= inst.operands[0].reg << 8;
8377   inst.instruction |= inst.operands[1].reg << 16;
8378   inst.instruction |= inst.operands[2].reg;
8379   if (inst.operands[3].present)
8380     {
8381       unsigned int val = inst.reloc.exp.X_add_number;
8382       constraint (inst.reloc.exp.X_op != O_constant,
8383                   _("expression too complex"));
8384       inst.instruction |= (val & 0x1c) << 10;
8385       inst.instruction |= (val & 0x03) << 6;
8386     }
8387 }
8388
8389 static void
8390 do_t_pkhtb (void)
8391 {
8392   if (!inst.operands[3].present)
8393     inst.instruction &= ~0x00000020;
8394   do_t_pkhbt ();
8395 }
8396
8397 static void
8398 do_t_pld (void)
8399 {
8400   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
8401 }
8402
8403 static void
8404 do_t_push_pop (void)
8405 {
8406   unsigned mask;
8407   
8408   constraint (inst.operands[0].writeback,
8409               _("push/pop do not support {reglist}^"));
8410   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
8411               _("expression too complex"));
8412
8413   mask = inst.operands[0].imm;
8414   if ((mask & ~0xff) == 0)
8415     inst.instruction = THUMB_OP16 (inst.instruction);
8416   else if ((inst.instruction == T_MNEM_push
8417             && (mask & ~0xff) == 1 << REG_LR)
8418            || (inst.instruction == T_MNEM_pop
8419                && (mask & ~0xff) == 1 << REG_PC))
8420     {
8421       inst.instruction = THUMB_OP16 (inst.instruction);
8422       inst.instruction |= THUMB_PP_PC_LR;
8423       mask &= 0xff;
8424     }
8425   else if (unified_syntax)
8426     {
8427       if (mask & (1 << 13))
8428         inst.error =  _("SP not allowed in register list");
8429       if (inst.instruction == T_MNEM_push)
8430         {
8431           if (mask & (1 << 15))
8432             inst.error = _("PC not allowed in register list");
8433         }
8434       else
8435         {
8436           if (mask & (1 << 14)
8437               && mask & (1 << 15))
8438             inst.error = _("LR and PC should not both be in register list");
8439         }
8440       if ((mask & (mask - 1)) == 0)
8441         {
8442           /* Single register push/pop implemented as str/ldr.  */
8443           if (inst.instruction == T_MNEM_push)
8444             inst.instruction = 0xf84d0d04; /* str reg, [sp, #-4]! */
8445           else
8446             inst.instruction = 0xf85d0b04; /* ldr reg, [sp], #4 */
8447           mask = ffs(mask) - 1;
8448           mask <<= 12;
8449         }
8450       else
8451         inst.instruction = THUMB_OP32 (inst.instruction);
8452     }
8453   else
8454     {
8455       inst.error = _("invalid register list to push/pop instruction");
8456       return;
8457     }
8458
8459   inst.instruction |= mask;
8460 }
8461
8462 static void
8463 do_t_rbit (void)
8464 {
8465   inst.instruction |= inst.operands[0].reg << 8;
8466   inst.instruction |= inst.operands[1].reg << 16;
8467 }
8468
8469 static void
8470 do_t_rev (void)
8471 {
8472   if (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
8473       && inst.size_req != 4)
8474     {
8475       inst.instruction = THUMB_OP16 (inst.instruction);
8476       inst.instruction |= inst.operands[0].reg;
8477       inst.instruction |= inst.operands[1].reg << 3;
8478     }
8479   else if (unified_syntax)
8480     {
8481       inst.instruction = THUMB_OP32 (inst.instruction);
8482       inst.instruction |= inst.operands[0].reg << 8;
8483       inst.instruction |= inst.operands[1].reg << 16;
8484       inst.instruction |= inst.operands[1].reg;
8485     }
8486   else
8487     inst.error = BAD_HIREG;
8488 }
8489
8490 static void
8491 do_t_rsb (void)
8492 {
8493   int Rd, Rs;
8494
8495   Rd = inst.operands[0].reg;
8496   Rs = (inst.operands[1].present
8497         ? inst.operands[1].reg    /* Rd, Rs, foo */
8498         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
8499
8500   inst.instruction |= Rd << 8;
8501   inst.instruction |= Rs << 16;
8502   if (!inst.operands[2].isreg)
8503     {
8504       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
8505       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
8506     }
8507   else
8508     encode_thumb32_shifted_operand (2);
8509 }
8510
8511 static void
8512 do_t_setend (void)
8513 {
8514   constraint (current_it_mask, BAD_NOT_IT);
8515   if (inst.operands[0].imm)
8516     inst.instruction |= 0x8;
8517 }
8518
8519 static void
8520 do_t_shift (void)
8521 {
8522   if (!inst.operands[1].present)
8523     inst.operands[1].reg = inst.operands[0].reg;
8524
8525   if (unified_syntax)
8526     {
8527       bfd_boolean narrow;
8528       int shift_kind;
8529
8530       switch (inst.instruction)
8531         {
8532         case T_MNEM_asr:
8533         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
8534         case T_MNEM_lsl:
8535         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
8536         case T_MNEM_lsr:
8537         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
8538         case T_MNEM_ror:
8539         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
8540         default: abort ();
8541         }
8542
8543       if (THUMB_SETS_FLAGS (inst.instruction))
8544         narrow = (current_it_mask == 0);
8545       else
8546         narrow = (current_it_mask != 0);
8547       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
8548         narrow = FALSE;
8549       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
8550         narrow = FALSE;
8551       if (inst.operands[2].isreg
8552           && (inst.operands[1].reg != inst.operands[0].reg
8553               || inst.operands[2].reg > 7))
8554         narrow = FALSE;
8555       if (inst.size_req == 4)
8556         narrow = FALSE;
8557
8558       if (!narrow)
8559         {
8560           if (inst.operands[2].isreg)
8561             {
8562               inst.instruction = THUMB_OP32 (inst.instruction);
8563               inst.instruction |= inst.operands[0].reg << 8;
8564               inst.instruction |= inst.operands[1].reg << 16;
8565               inst.instruction |= inst.operands[2].reg;
8566             }
8567           else
8568             {
8569               inst.operands[1].shifted = 1;
8570               inst.operands[1].shift_kind = shift_kind;
8571               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
8572                                              ? T_MNEM_movs : T_MNEM_mov);
8573               inst.instruction |= inst.operands[0].reg << 8;
8574               encode_thumb32_shifted_operand (1);
8575               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
8576               inst.reloc.type = BFD_RELOC_UNUSED;
8577             }
8578         }
8579       else
8580         {
8581           if (inst.operands[2].isreg)
8582             {
8583               switch (shift_kind)
8584                 {
8585                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
8586                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
8587                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
8588                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
8589                 default: abort ();
8590                 }
8591           
8592               inst.instruction |= inst.operands[0].reg;
8593               inst.instruction |= inst.operands[2].reg << 3;
8594             }
8595           else
8596             {
8597               switch (shift_kind)
8598                 {
8599                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
8600                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
8601                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
8602                 default: abort ();
8603                 }
8604               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
8605               inst.instruction |= inst.operands[0].reg;
8606               inst.instruction |= inst.operands[1].reg << 3;
8607             }
8608         }
8609     }
8610   else
8611     {
8612       constraint (inst.operands[0].reg > 7
8613                   || inst.operands[1].reg > 7, BAD_HIREG);
8614       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
8615
8616       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
8617         {
8618           constraint (inst.operands[2].reg > 7, BAD_HIREG);
8619           constraint (inst.operands[0].reg != inst.operands[1].reg,
8620                       _("source1 and dest must be same register"));
8621
8622           switch (inst.instruction)
8623             {
8624             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
8625             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
8626             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
8627             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
8628             default: abort ();
8629             }
8630           
8631           inst.instruction |= inst.operands[0].reg;
8632           inst.instruction |= inst.operands[2].reg << 3;
8633         }
8634       else
8635         {
8636           switch (inst.instruction)
8637             {
8638             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
8639             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
8640             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
8641             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
8642             default: abort ();
8643             }
8644           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
8645           inst.instruction |= inst.operands[0].reg;
8646           inst.instruction |= inst.operands[1].reg << 3;
8647         }
8648     }
8649 }
8650
8651 static void
8652 do_t_simd (void)
8653 {
8654   inst.instruction |= inst.operands[0].reg << 8;
8655   inst.instruction |= inst.operands[1].reg << 16;
8656   inst.instruction |= inst.operands[2].reg;
8657 }
8658
8659 static void
8660 do_t_smc (void)
8661 {
8662   unsigned int value = inst.reloc.exp.X_add_number;
8663   constraint (inst.reloc.exp.X_op != O_constant,
8664               _("expression too complex"));
8665   inst.reloc.type = BFD_RELOC_UNUSED;
8666   inst.instruction |= (value & 0xf000) >> 12;
8667   inst.instruction |= (value & 0x0ff0);
8668   inst.instruction |= (value & 0x000f) << 16;
8669 }
8670
8671 static void
8672 do_t_ssat (void)
8673 {
8674   inst.instruction |= inst.operands[0].reg << 8;
8675   inst.instruction |= inst.operands[1].imm - 1;
8676   inst.instruction |= inst.operands[2].reg << 16;
8677
8678   if (inst.operands[3].present)
8679     {
8680       constraint (inst.reloc.exp.X_op != O_constant,
8681                   _("expression too complex"));
8682
8683       if (inst.reloc.exp.X_add_number != 0)
8684         {
8685           if (inst.operands[3].shift_kind == SHIFT_ASR)
8686             inst.instruction |= 0x00200000;  /* sh bit */
8687           inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
8688           inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
8689         }
8690       inst.reloc.type = BFD_RELOC_UNUSED;
8691     }
8692 }
8693
8694 static void
8695 do_t_ssat16 (void)
8696 {
8697   inst.instruction |= inst.operands[0].reg << 8;
8698   inst.instruction |= inst.operands[1].imm - 1;
8699   inst.instruction |= inst.operands[2].reg << 16;
8700 }
8701
8702 static void
8703 do_t_strex (void)
8704 {
8705   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
8706               || inst.operands[2].postind || inst.operands[2].writeback
8707               || inst.operands[2].immisreg || inst.operands[2].shifted
8708               || inst.operands[2].negative,
8709               BAD_ADDR_MODE);
8710
8711   inst.instruction |= inst.operands[0].reg << 8;
8712   inst.instruction |= inst.operands[1].reg << 12;
8713   inst.instruction |= inst.operands[2].reg << 16;
8714   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
8715 }
8716
8717 static void
8718 do_t_strexd (void)
8719 {
8720   if (!inst.operands[2].present)
8721     inst.operands[2].reg = inst.operands[1].reg + 1;
8722
8723   constraint (inst.operands[0].reg == inst.operands[1].reg
8724               || inst.operands[0].reg == inst.operands[2].reg
8725               || inst.operands[0].reg == inst.operands[3].reg
8726               || inst.operands[1].reg == inst.operands[2].reg,
8727               BAD_OVERLAP);
8728
8729   inst.instruction |= inst.operands[0].reg;
8730   inst.instruction |= inst.operands[1].reg << 12;
8731   inst.instruction |= inst.operands[2].reg << 8;
8732   inst.instruction |= inst.operands[3].reg << 16;
8733 }
8734
8735 static void
8736 do_t_sxtah (void)
8737 {
8738   inst.instruction |= inst.operands[0].reg << 8;
8739   inst.instruction |= inst.operands[1].reg << 16;
8740   inst.instruction |= inst.operands[2].reg;
8741   inst.instruction |= inst.operands[3].imm << 4;
8742 }
8743
8744 static void
8745 do_t_sxth (void)
8746 {
8747   if (inst.instruction <= 0xffff && inst.size_req != 4
8748       && inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
8749       && (!inst.operands[2].present || inst.operands[2].imm == 0))
8750     {
8751       inst.instruction = THUMB_OP16 (inst.instruction);
8752       inst.instruction |= inst.operands[0].reg;
8753       inst.instruction |= inst.operands[1].reg << 3;
8754     }
8755   else if (unified_syntax)
8756     {
8757       if (inst.instruction <= 0xffff)
8758         inst.instruction = THUMB_OP32 (inst.instruction);
8759       inst.instruction |= inst.operands[0].reg << 8;
8760       inst.instruction |= inst.operands[1].reg;
8761       inst.instruction |= inst.operands[2].imm << 4;
8762     }
8763   else
8764     {
8765       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
8766                   _("Thumb encoding does not support rotation"));
8767       constraint (1, BAD_HIREG);
8768     }
8769 }
8770
8771 static void
8772 do_t_swi (void)
8773 {
8774   inst.reloc.type = BFD_RELOC_ARM_SWI;
8775 }
8776
8777 static void
8778 do_t_tb (void)
8779 {
8780   int half;
8781
8782   half = (inst.instruction & 0x10) != 0;
8783   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
8784   constraint (inst.operands[0].immisreg,
8785               _("instruction requires register index"));
8786   constraint (inst.operands[0].imm == 15,
8787               _("PC is not a valid index register"));
8788   constraint (!half && inst.operands[0].shifted,
8789               _("instruction does not allow shifted index"));
8790   inst.instruction |= (inst.operands[0].reg << 16) | inst.operands[0].imm;
8791 }
8792
8793 static void
8794 do_t_usat (void)
8795 {
8796   inst.instruction |= inst.operands[0].reg << 8;
8797   inst.instruction |= inst.operands[1].imm;
8798   inst.instruction |= inst.operands[2].reg << 16;
8799
8800   if (inst.operands[3].present)
8801     {
8802       constraint (inst.reloc.exp.X_op != O_constant,
8803                   _("expression too complex"));
8804       if (inst.reloc.exp.X_add_number != 0)
8805         {
8806           if (inst.operands[3].shift_kind == SHIFT_ASR)
8807             inst.instruction |= 0x00200000;  /* sh bit */
8808
8809           inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
8810           inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
8811         }
8812       inst.reloc.type = BFD_RELOC_UNUSED;
8813     }
8814 }
8815
8816 static void
8817 do_t_usat16 (void)
8818 {
8819   inst.instruction |= inst.operands[0].reg << 8;
8820   inst.instruction |= inst.operands[1].imm;
8821   inst.instruction |= inst.operands[2].reg << 16;
8822 }
8823
8824 /* Neon instruction encoder helpers.  */
8825   
8826 /* Encodings for the different types for various Neon opcodes.  */
8827
8828 /* An "invalid" code for the following tables.  */
8829 #define N_INV -1u
8830
8831 struct neon_tab_entry
8832 {
8833   unsigned integer;
8834   unsigned float_or_poly;
8835   unsigned scalar_or_imm;
8836 };
8837   
8838 /* Map overloaded Neon opcodes to their respective encodings.  */
8839 #define NEON_ENC_TAB                                    \
8840   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
8841   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
8842   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
8843   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
8844   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
8845   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
8846   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
8847   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
8848   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
8849   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
8850   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
8851   /* Register variants of the following two instructions are encoded as
8852      vcge / vcgt with the operands reversed. */         \
8853   X(vclt,       0x0000310, 0x1000e00, 0x1b10200),       \
8854   X(vcle,       0x0000300, 0x1200e00, 0x1b10180),       \
8855   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
8856   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
8857   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
8858   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
8859   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
8860   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
8861   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
8862   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
8863   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
8864   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
8865   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
8866   X(vshl,       0x0000400, N_INV,     0x0800510),       \
8867   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
8868   X(vand,       0x0000110, N_INV,     0x0800030),       \
8869   X(vbic,       0x0100110, N_INV,     0x0800030),       \
8870   X(veor,       0x1000110, N_INV,     N_INV),           \
8871   X(vorn,       0x0300110, N_INV,     0x0800010),       \
8872   X(vorr,       0x0200110, N_INV,     0x0800010),       \
8873   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
8874   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
8875   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
8876   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
8877   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
8878   X(vst1,       0x0000000, 0x0800000, N_INV),           \
8879   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
8880   X(vst2,       0x0000100, 0x0800100, N_INV),           \
8881   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
8882   X(vst3,       0x0000200, 0x0800200, N_INV),           \
8883   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
8884   X(vst4,       0x0000300, 0x0800300, N_INV),           \
8885   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
8886   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
8887   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
8888   X(vqmovun,    0x1b20240, N_INV,     N_INV)
8889
8890 enum neon_opc
8891 {
8892 #define X(OPC,I,F,S) N_MNEM_##OPC
8893 NEON_ENC_TAB
8894 #undef X
8895 };
8896
8897 static const struct neon_tab_entry neon_enc_tab[] =
8898 {
8899 #define X(OPC,I,F,S) { (I), (F), (S) }
8900 NEON_ENC_TAB
8901 #undef X
8902 };
8903
8904 #define NEON_ENC_INTEGER(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
8905 #define NEON_ENC_ARMREG(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
8906 #define NEON_ENC_POLY(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
8907 #define NEON_ENC_FLOAT(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
8908 #define NEON_ENC_SCALAR(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
8909 #define NEON_ENC_IMMED(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
8910 #define NEON_ENC_INTERLV(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
8911 #define NEON_ENC_LANE(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
8912 #define NEON_ENC_DUP(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
8913
8914 /* Shapes for instruction operands. Some (e.g. NS_DDD_QQQ) represent multiple
8915    shapes which an instruction can accept. The following mnemonic characters
8916    are used in the tag names for this enumeration:
8917
8918      D - Neon D<n> register
8919      Q - Neon Q<n> register
8920      I - Immediate
8921      S - Scalar
8922      R - ARM register
8923      L - D<n> register list
8924 */
8925
8926 enum neon_shape
8927 {
8928   NS_DDD_QQQ,
8929   NS_DDD,
8930   NS_QQQ,
8931   NS_DDI_QQI,
8932   NS_DDI,
8933   NS_QQI,
8934   NS_DDS_QQS,
8935   NS_DDS,
8936   NS_QQS,
8937   NS_DD_QQ,
8938   NS_DD,
8939   NS_QQ,
8940   NS_DS_QS,
8941   NS_DS,
8942   NS_QS,
8943   NS_DR_QR,
8944   NS_DR,
8945   NS_QR,
8946   NS_DI_QI,
8947   NS_DI,
8948   NS_QI,
8949   NS_DLD,
8950   NS_DQ,
8951   NS_QD,
8952   NS_DQI,
8953   NS_QDI,
8954   NS_QDD,
8955   NS_QDS,
8956   NS_QQD,
8957   NS_DQQ,
8958   NS_DDDI_QQQI,
8959   NS_DDDI,
8960   NS_QQQI,
8961   NS_IGNORE
8962 };
8963
8964 /* Bit masks used in type checking given instructions.
8965   'N_EQK' means the type must be the same as (or based on in some way) the key
8966    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
8967    set, various other bits can be set as well in order to modify the meaning of
8968    the type constraint.  */
8969
8970 enum neon_type_mask
8971 {
8972   N_S8   = 0x000001,
8973   N_S16  = 0x000002,
8974   N_S32  = 0x000004,
8975   N_S64  = 0x000008,
8976   N_U8   = 0x000010,
8977   N_U16  = 0x000020,
8978   N_U32  = 0x000040,
8979   N_U64  = 0x000080,
8980   N_I8   = 0x000100,
8981   N_I16  = 0x000200,
8982   N_I32  = 0x000400,
8983   N_I64  = 0x000800,
8984   N_8    = 0x001000,
8985   N_16   = 0x002000,
8986   N_32   = 0x004000,
8987   N_64   = 0x008000,
8988   N_P8   = 0x010000,
8989   N_P16  = 0x020000,
8990   N_F32  = 0x040000,
8991   N_KEY  = 0x080000, /* key element (main type specifier).  */
8992   N_EQK  = 0x100000, /* given operand has the same type & size as the key.  */
8993   N_DBL  = 0x000001, /* if N_EQK, this operand is twice the size.  */
8994   N_HLF  = 0x000002, /* if N_EQK, this operand is half the size.  */
8995   N_SGN  = 0x000004, /* if N_EQK, this operand is forced to be signed.  */
8996   N_UNS  = 0x000008, /* if N_EQK, this operand is forced to be unsigned.  */
8997   N_INT  = 0x000010, /* if N_EQK, this operand is forced to be integer.  */
8998   N_FLT  = 0x000020, /* if N_EQK, this operand is forced to be float.  */
8999   N_UTYP = 0,
9000   N_MAX_NONSPECIAL = N_F32
9001 };
9002
9003 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
9004 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
9005 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
9006 #define N_SUF_32   (N_SU_32 | N_F32)
9007 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
9008 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F32)
9009
9010 /* Pass this as the first type argument to neon_check_type to ignore types
9011    altogether.  */
9012 #define N_IGNORE_TYPE (N_KEY | N_EQK)
9013
9014 /* Check the shape of a Neon instruction (sizes of registers). Returns the more
9015    specific shape when there are two alternatives. For non-polymorphic shapes,
9016    checking is done during operand parsing, so is not implemented here.  */
9017
9018 static enum neon_shape
9019 neon_check_shape (enum neon_shape req)
9020 {
9021 #define RR(X) (inst.operands[(X)].isreg)
9022 #define RD(X) (inst.operands[(X)].isreg && !inst.operands[(X)].isquad)
9023 #define RQ(X) (inst.operands[(X)].isreg && inst.operands[(X)].isquad)
9024 #define IM(X) (!inst.operands[(X)].isreg && !inst.operands[(X)].isscalar)
9025 #define SC(X) (!inst.operands[(X)].isreg && inst.operands[(X)].isscalar)
9026
9027   /* Fix missing optional operands. FIXME: we don't know at this point how
9028      many arguments we should have, so this makes the assumption that we have
9029      > 1. This is true of all current Neon opcodes, I think, but may not be
9030      true in the future.  */
9031   if (!inst.operands[1].present)
9032     inst.operands[1] = inst.operands[0];
9033
9034   switch (req)
9035       {
9036     case NS_DDD_QQQ:
9037       {
9038         if (RD(0) && RD(1) && RD(2))
9039           return NS_DDD;
9040         else if (RQ(0) && RQ(1) && RQ(1))
9041           return NS_QQQ;
9042         else
9043           inst.error = _("expected <Qd>, <Qn>, <Qm> or <Dd>, <Dn>, <Dm> "
9044                          "operands");
9045       }
9046       break;
9047     
9048     case NS_DDI_QQI:
9049       {
9050         if (RD(0) && RD(1) && IM(2))
9051           return NS_DDI;
9052         else if (RQ(0) && RQ(1) && IM(2))
9053           return NS_QQI;
9054         else
9055           inst.error = _("expected <Qd>, <Qn>, #<imm> or <Dd>, <Dn>, #<imm> "
9056                          "operands");
9057       }
9058         break;
9059   
9060     case NS_DDDI_QQQI:
9061       {
9062         if (RD(0) && RD(1) && RD(2) && IM(3))
9063           return NS_DDDI;
9064         if (RQ(0) && RQ(1) && RQ(2) && IM(3))
9065           return NS_QQQI;
9066         else
9067           inst.error = _("expected <Qd>, <Qn>, <Qm>, #<imm> or "
9068                          "<Dd>, <Dn>, <Dm>, #<imm> operands");
9069       }
9070         break;
9071   
9072     case NS_DDS_QQS:
9073       {
9074         if (RD(0) && RD(1) && SC(2))
9075           return NS_DDS;
9076         else if (RQ(0) && RQ(1) && SC(2))
9077           return NS_QQS;
9078         else
9079           inst.error = _("expected <Qd>, <Qn>, <Dm[x]> or <Dd>, <Dn>, <Dm[x]> "
9080                          "operands");
9081       }
9082       break;
9083   
9084     case NS_DD_QQ:
9085       {
9086         if (RD(0) && RD(1))
9087           return NS_DD;
9088         else if (RQ(0) && RQ(1))
9089           return NS_QQ;
9090         else
9091           inst.error = _("expected <Qd>, <Qm> or <Dd>, <Dm> operands");
9092       }
9093       break;
9094   
9095     case NS_DS_QS:
9096       {
9097         if (RD(0) && SC(1))
9098           return NS_DS;
9099         else if (RQ(0) && SC(1))
9100           return NS_QS;
9101         else
9102           inst.error = _("expected <Qd>, <Dm[x]> or <Dd>, <Dm[x]> operands");
9103       }
9104       break;
9105
9106     case NS_DR_QR:
9107       {
9108         if (RD(0) && RR(1))
9109           return NS_DR;
9110         else if (RQ(0) && RR(1))
9111           return NS_QR;
9112         else
9113           inst.error = _("expected <Qd>, <Rm> or <Dd>, <Rm> operands");
9114       }
9115       break;
9116
9117     case NS_DI_QI:
9118       {
9119         if (RD(0) && IM(1))
9120           return NS_DI;
9121         else if (RQ(0) && IM(1))
9122           return NS_QI;
9123         else
9124           inst.error = _("expected <Qd>, #<imm> or <Dd>, #<imm> operands");
9125       }
9126       break;
9127    
9128     default:
9129       abort ();
9130     }
9131
9132   return req;
9133 #undef RR
9134 #undef RD
9135 #undef RQ
9136 #undef IM
9137 #undef SC
9138 }
9139   
9140 static void
9141 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
9142                        unsigned *g_size)
9143 {
9144   /* Allow modification to be made to types which are constrained to be
9145      based on the key element, based on bits set alongside N_EQK.  */
9146   if ((typebits & N_EQK) != 0)
9147     {
9148       if ((typebits & N_HLF) != 0)
9149         *g_size /= 2;
9150       else if ((typebits & N_DBL) != 0)
9151         *g_size *= 2;
9152       if ((typebits & N_SGN) != 0)
9153         *g_type = NT_signed;
9154       else if ((typebits & N_UNS) != 0)
9155         *g_type = NT_unsigned;
9156       else if ((typebits & N_INT) != 0)
9157         *g_type = NT_integer;
9158       else if ((typebits & N_FLT) != 0)
9159         *g_type = NT_float;
9160     }
9161 }
9162   
9163 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
9164    operand type, i.e. the single type specified in a Neon instruction when it
9165    is the only one given.  */
9166
9167 static struct neon_type_el
9168 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
9169 {
9170   struct neon_type_el dest = *key;
9171   
9172   assert ((thisarg & N_EQK) != 0);
9173   
9174   neon_modify_type_size (thisarg, &dest.type, &dest.size);
9175
9176   return dest;
9177 }
9178
9179 /* Convert Neon type and size into compact bitmask representation.  */
9180
9181 static enum neon_type_mask
9182 type_chk_of_el_type (enum neon_el_type type, unsigned size)
9183 {
9184   switch (type)
9185     {
9186     case NT_untyped:
9187       switch (size)
9188         {
9189         case 8:  return N_8;
9190         case 16: return N_16;
9191         case 32: return N_32;
9192         case 64: return N_64;
9193         default: ;
9194         }
9195       break;
9196
9197     case NT_integer:
9198       switch (size)
9199         {
9200         case 8:  return N_I8;
9201         case 16: return N_I16;
9202         case 32: return N_I32;
9203         case 64: return N_I64;
9204         default: ;
9205         }
9206       break;
9207
9208     case NT_float:
9209       if (size == 32)
9210         return N_F32;
9211       break;
9212
9213     case NT_poly:
9214       switch (size)
9215         {
9216         case 8:  return N_P8;
9217         case 16: return N_P16;
9218         default: ;
9219         }
9220       break;
9221
9222     case NT_signed:
9223       switch (size)
9224         {
9225         case 8:  return N_S8;
9226         case 16: return N_S16;
9227         case 32: return N_S32;
9228         case 64: return N_S64;
9229         default: ;
9230         }
9231       break;
9232
9233     case NT_unsigned:
9234       switch (size)
9235         {
9236         case 8:  return N_U8;
9237         case 16: return N_U16;
9238         case 32: return N_U32;
9239         case 64: return N_U64;
9240         default: ;
9241         }
9242       break;
9243
9244     default: ;
9245     }
9246   
9247   return N_UTYP;
9248 }
9249
9250 /* Convert compact Neon bitmask type representation to a type and size. Only
9251    handles the case where a single bit is set in the mask.  */
9252
9253 static void
9254 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
9255                      enum neon_type_mask mask)
9256 {
9257   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
9258     *size = 8;
9259   if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_P16)) != 0)
9260     *size = 16;
9261   if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
9262     *size = 32;
9263   if ((mask & (N_S64 | N_U64 | N_I64 | N_64)) != 0)
9264     *size = 64;
9265   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
9266     *type = NT_signed;
9267   if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
9268     *type = NT_unsigned;
9269   if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
9270     *type = NT_integer;
9271   if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
9272     *type = NT_untyped;
9273   if ((mask & (N_P8 | N_P16)) != 0)
9274     *type = NT_poly;
9275   if ((mask & N_F32) != 0)
9276     *type = NT_float;
9277 }
9278
9279 /* Modify a bitmask of allowed types. This is only needed for type
9280    relaxation.  */
9281
9282 static unsigned
9283 modify_types_allowed (unsigned allowed, unsigned mods)
9284 {
9285   unsigned size;
9286   enum neon_el_type type;
9287   unsigned destmask;
9288   int i;
9289   
9290   destmask = 0;
9291   
9292   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
9293     {
9294       el_type_of_type_chk (&type, &size, allowed & i);
9295       neon_modify_type_size (mods, &type, &size);
9296       destmask |= type_chk_of_el_type (type, size);
9297     }
9298   
9299   return destmask;
9300 }
9301
9302 /* Check type and return type classification.
9303    The manual states (paraphrase): If one datatype is given, it indicates the
9304    type given in:
9305     - the second operand, if there is one
9306     - the operand, if there is no second operand
9307     - the result, if there are no operands.
9308    This isn't quite good enough though, so we use a concept of a "key" datatype
9309    which is set on a per-instruction basis, which is the one which matters when
9310    only one data type is written.
9311    Note: this function has side-effects (e.g. filling in missing operands). All
9312    Neon instructions should call it before performing bit encoding.
9313 */
9314
9315 static struct neon_type_el
9316 neon_check_type (unsigned els, enum neon_shape ns, ...)
9317 {
9318   va_list ap;
9319   unsigned i, pass, key_el = 0;
9320   unsigned types[NEON_MAX_TYPE_ELS];
9321   enum neon_el_type k_type = NT_invtype;
9322   unsigned k_size = -1u;
9323   struct neon_type_el badtype = {NT_invtype, -1};
9324   unsigned key_allowed = 0;
9325
9326   /* Optional registers in Neon instructions are always (not) in operand 1.
9327      Fill in the missing operand here, if it was omitted.  */
9328   if (els > 1 && !inst.operands[1].present)
9329     inst.operands[1] = inst.operands[0];
9330
9331   /* Suck up all the varargs.  */
9332   va_start (ap, ns);
9333   for (i = 0; i < els; i++)
9334     {
9335       unsigned thisarg = va_arg (ap, unsigned);
9336       if (thisarg == N_IGNORE_TYPE)
9337         {
9338           va_end (ap);
9339           return badtype;
9340         }
9341       types[i] = thisarg;
9342       if ((thisarg & N_KEY) != 0)
9343         key_el = i;
9344     }
9345   va_end (ap);
9346
9347   /* Duplicate inst.vectype elements here as necessary.
9348      FIXME: No idea if this is exactly the same as the ARM assembler,
9349      particularly when an insn takes one register and one non-register
9350      operand. */
9351   if (inst.vectype.elems == 1 && els > 1)
9352     {
9353       unsigned j;
9354       inst.vectype.elems = els;
9355       inst.vectype.el[key_el] = inst.vectype.el[0];
9356       for (j = 0; j < els; j++)
9357         {
9358           if (j != key_el)
9359             inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
9360                                                     types[j]);
9361         }
9362     }
9363   else if (inst.vectype.elems != els)
9364     {
9365       inst.error = _("type specifier has the wrong number of parts");
9366       return badtype;
9367     }
9368
9369   for (pass = 0; pass < 2; pass++)
9370     {
9371       for (i = 0; i < els; i++)
9372         {
9373           unsigned thisarg = types[i];
9374           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
9375             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
9376           enum neon_el_type g_type = inst.vectype.el[i].type;
9377           unsigned g_size = inst.vectype.el[i].size;
9378
9379           /* Decay more-specific signed & unsigned types to sign-insensitive
9380              integer types if sign-specific variants are unavailable.  */
9381           if ((g_type == NT_signed || g_type == NT_unsigned)
9382               && (types_allowed & N_SU_ALL) == 0)
9383             g_type = NT_integer;
9384
9385           /* If only untyped args are allowed, decay any more specific types to
9386              them. Some instructions only care about signs for some element
9387              sizes, so handle that properly.  */
9388           if ((g_size == 8 && (types_allowed & N_8) != 0)
9389               || (g_size == 16 && (types_allowed & N_16) != 0)
9390               || (g_size == 32 && (types_allowed & N_32) != 0)
9391               || (g_size == 64 && (types_allowed & N_64) != 0))
9392             g_type = NT_untyped;
9393
9394           if (pass == 0)
9395             {
9396               if ((thisarg & N_KEY) != 0)
9397                 {
9398                   k_type = g_type;
9399                   k_size = g_size;
9400                   key_allowed = thisarg & ~N_KEY;
9401                 }
9402             }
9403           else
9404             {
9405               if ((thisarg & N_EQK) == 0)
9406                 {
9407                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
9408
9409                   if ((given_type & types_allowed) == 0)
9410                     {
9411                       inst.error = _("bad type in Neon instruction");
9412                       return badtype;
9413                     }
9414                 }
9415               else
9416                 {
9417                   enum neon_el_type mod_k_type = k_type;
9418                   unsigned mod_k_size = k_size;
9419                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
9420                   if (g_type != mod_k_type || g_size != mod_k_size)
9421                     {
9422                       inst.error = _("inconsistent types in Neon instruction");
9423                       return badtype;
9424                     }
9425                 }
9426             }
9427         }
9428     }
9429
9430   return inst.vectype.el[key_el];
9431 }
9432
9433 /* Fix up Neon data-processing instructions, ORing in the correct bits for
9434    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
9435
9436 static unsigned
9437 neon_dp_fixup (unsigned i)
9438 {
9439   if (thumb_mode)
9440     {
9441       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
9442       if (i & (1 << 24))
9443         i |= 1 << 28;
9444       
9445       i &= ~(1 << 24);
9446       
9447       i |= 0xef000000;
9448     }
9449   else
9450     i |= 0xf2000000;
9451   
9452   return i;
9453 }
9454
9455 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
9456    (0, 1, 2, 3).  */
9457
9458 static unsigned
9459 neon_logbits (unsigned x)
9460 {
9461   return ffs (x) - 4;
9462 }
9463
9464 #define LOW4(R) ((R) & 0xf)
9465 #define HI1(R) (((R) >> 4) & 1)
9466
9467 /* Encode insns with bit pattern:
9468
9469   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
9470   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
9471   
9472   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
9473   different meaning for some instruction.  */
9474
9475 static void
9476 neon_three_same (int first_optional, int isquad, int ubit, int size)
9477 {
9478   /* FIXME optional argument handling.  */
9479   if (first_optional && !inst.operands[0].present)
9480     inst.operands[0].reg = inst.operands[1].reg;
9481
9482   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
9483   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
9484   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
9485   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
9486   inst.instruction |= LOW4 (inst.operands[2].reg);
9487   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
9488   inst.instruction |= (isquad != 0) << 6;
9489   inst.instruction |= (ubit != 0) << 24;
9490   if (size != -1)
9491     inst.instruction |= neon_logbits (size) << 20;
9492   
9493   inst.instruction = neon_dp_fixup (inst.instruction);
9494 }
9495
9496 /* Encode instructions of the form:
9497
9498   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
9499   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
9500
9501   Don't write size if SIZE == -1.  */
9502
9503 static void
9504 neon_two_same (int qbit, int ubit, int size)
9505 {
9506   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
9507   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
9508   inst.instruction |= LOW4 (inst.operands[1].reg);
9509   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
9510   inst.instruction |= (qbit != 0) << 6;
9511   inst.instruction |= (ubit != 0) << 24;
9512
9513   if (size != -1)
9514     inst.instruction |= neon_logbits (size) << 18;
9515
9516   inst.instruction = neon_dp_fixup (inst.instruction);
9517 }
9518
9519 /* Neon instruction encoders, in approximate order of appearance.  */
9520
9521 static void
9522 do_neon_dyadic_i_su (void)
9523 {
9524   enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
9525   struct neon_type_el et = neon_check_type (3, rs,
9526     N_EQK, N_EQK, N_SU_32 | N_KEY);
9527   neon_three_same (TRUE, rs == NS_QQQ, et.type == NT_unsigned, et.size);
9528 }
9529
9530 static void
9531 do_neon_dyadic_i64_su (void)
9532 {
9533   enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
9534   struct neon_type_el et = neon_check_type (3, rs,
9535     N_EQK, N_EQK, N_SU_ALL | N_KEY);
9536   neon_three_same (TRUE, rs == NS_QQQ, et.type == NT_unsigned, et.size);
9537 }
9538
9539 static void
9540 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
9541                 unsigned immbits)
9542 {
9543   unsigned size = et.size >> 3;
9544   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
9545   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
9546   inst.instruction |= LOW4 (inst.operands[1].reg);
9547   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
9548   inst.instruction |= (isquad != 0) << 6;
9549   inst.instruction |= immbits << 16;
9550   inst.instruction |= (size >> 3) << 7;
9551   inst.instruction |= (size & 0x7) << 19;
9552   if (write_ubit)
9553     inst.instruction |= (uval != 0) << 24;
9554
9555   inst.instruction = neon_dp_fixup (inst.instruction);
9556 }
9557
9558 static void
9559 do_neon_shl_imm (void)
9560 {
9561   if (!inst.operands[2].isreg)
9562     {
9563       enum neon_shape rs = neon_check_shape (NS_DDI_QQI);
9564       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
9565       inst.instruction = NEON_ENC_IMMED (inst.instruction);
9566       neon_imm_shift (FALSE, 0, rs == NS_QQI, et, inst.operands[2].imm);
9567     }
9568   else
9569     {
9570       enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
9571       struct neon_type_el et = neon_check_type (3, rs,
9572         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
9573       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
9574       neon_three_same (TRUE, rs == NS_QQQ, et.type == NT_unsigned, et.size);
9575     }
9576 }
9577
9578 static void
9579 do_neon_qshl_imm (void)
9580 {
9581   if (!inst.operands[2].isreg)
9582     {
9583       enum neon_shape rs = neon_check_shape (NS_DDI_QQI);
9584       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
9585       inst.instruction = NEON_ENC_IMMED (inst.instruction);
9586       neon_imm_shift (TRUE, et.type == NT_unsigned, rs == NS_QQI, et,
9587                       inst.operands[2].imm);
9588     }
9589   else
9590     {
9591       enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
9592       struct neon_type_el et = neon_check_type (3, rs,
9593         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
9594       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
9595       neon_three_same (TRUE, rs == NS_QQQ, et.type == NT_unsigned, et.size);
9596     }
9597 }
9598
9599 static int
9600 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
9601 {
9602   /* Handle .I8 and .I64 as pseudo-instructions.  */
9603   switch (size)
9604     {
9605     case 8:
9606       /* Unfortunately, this will make everything apart from zero out-of-range.
9607          FIXME is this the intended semantics? There doesn't seem much point in
9608          accepting .I8 if so.  */
9609       immediate |= immediate << 8;
9610       size = 16;
9611       break;
9612     case 64:
9613       /* Similarly, anything other than zero will be replicated in bits [63:32],
9614          which probably isn't want we want if we specified .I64.  */
9615       if (immediate != 0)
9616         goto bad_immediate;
9617       size = 32;
9618       break;
9619     default: ;
9620     }
9621
9622   if (immediate == (immediate & 0x000000ff))
9623     {
9624       *immbits = immediate;
9625       return (size == 16) ? 0x9 : 0x1;
9626     }
9627   else if (immediate == (immediate & 0x0000ff00))
9628     {
9629       *immbits = immediate >> 8;
9630       return (size == 16) ? 0xb : 0x3;
9631     }
9632   else if (immediate == (immediate & 0x00ff0000))
9633     {
9634       *immbits = immediate >> 16;
9635       return 0x5;
9636     }
9637   else if (immediate == (immediate & 0xff000000))
9638     {
9639       *immbits = immediate >> 24;
9640       return 0x7;
9641     }
9642
9643   bad_immediate:
9644   inst.error = _("immediate value out of range");
9645   return FAIL;
9646 }
9647
9648 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
9649    A, B, C, D.  */
9650
9651 static int
9652 neon_bits_same_in_bytes (unsigned imm)
9653 {
9654   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
9655          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
9656          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
9657          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
9658 }
9659
9660 /* For immediate of above form, return 0bABCD.  */
9661
9662 static unsigned
9663 neon_squash_bits (unsigned imm)
9664 {
9665   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
9666          | ((imm & 0x01000000) >> 21);
9667 }
9668
9669 /* Returns 1 if a number has "quarter-precision" float format
9670    0baBbbbbbc defgh000 00000000 00000000.  */
9671
9672 static int
9673 neon_is_quarter_float (unsigned imm)
9674 {
9675   int b = (imm & 0x20000000) != 0;
9676   int bs = (b << 25) | (b << 26) | (b << 27) | (b << 28) | (b << 29)
9677            | ((!b) << 30);
9678   return (imm & 0x81ffffff) == (imm & 0x81f80000)
9679          && ((imm & 0x7e000000) ^ bs) == 0;
9680 }
9681
9682 /* Compress above representation to 0b...000 abcdefgh.  */
9683
9684 static unsigned
9685 neon_qfloat_bits (unsigned imm)
9686 {
9687   return ((imm >> 19) & 0x7f) | (imm >> 24);
9688 }
9689
9690 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
9691    the instruction. *OP is passed as the initial value of the op field, and
9692    may be set to a different value depending on the constant (i.e.
9693    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
9694    MVN).  */
9695
9696 static int
9697 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, unsigned *immbits,
9698                          int *op, int size)
9699 {
9700   if (size == 64 && neon_bits_same_in_bytes (immhi)
9701       && neon_bits_same_in_bytes (immlo))
9702     {
9703       /* Check this one first so we don't have to bother with immhi in later
9704          tests.  */
9705       if (*op == 1)
9706         return FAIL;
9707       *immbits = (neon_squash_bits (immhi) << 4) | neon_squash_bits (immlo);
9708       *op = 1;
9709       return 0xe;
9710     }
9711   else if (immhi != 0)
9712     return FAIL;
9713   else if (immlo == (immlo & 0x000000ff))
9714     {
9715       /* 64-bit case was already handled. Don't allow MVN with 8-bit
9716          immediate.  */
9717       if ((size != 8 && size != 16 && size != 32)
9718           || (size == 8 && *op == 1))
9719         return FAIL;
9720       *immbits = immlo;
9721       return (size == 8) ? 0xe : (size == 16) ? 0x8 : 0x0;
9722     }
9723   else if (immlo == (immlo & 0x0000ff00))
9724     {
9725       if (size != 16 && size != 32)
9726         return FAIL;
9727       *immbits = immlo >> 8;
9728       return (size == 16) ? 0xa : 0x2;
9729     }
9730   else if (immlo == (immlo & 0x00ff0000))
9731     {
9732       if (size != 32)
9733         return FAIL;
9734       *immbits = immlo >> 16;
9735       return 0x4;
9736     }
9737   else if (immlo == (immlo & 0xff000000))
9738     {
9739       if (size != 32)
9740         return FAIL;
9741       *immbits = immlo >> 24;
9742       return 0x6;
9743     }
9744   else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
9745     {
9746       if (size != 32)
9747         return FAIL;
9748       *immbits = (immlo >> 8) & 0xff;
9749       return 0xc;
9750     }
9751   else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
9752     {
9753       if (size != 32)
9754         return FAIL;
9755       *immbits = (immlo >> 16) & 0xff;
9756       return 0xd;
9757     }
9758   else if (neon_is_quarter_float (immlo))
9759     {
9760       if (size != 32 || *op == 1)
9761         return FAIL;
9762       *immbits = neon_qfloat_bits (immlo);
9763       return 0xf;
9764     }
9765
9766   return FAIL;
9767 }
9768
9769 /* Write immediate bits [7:0] to the following locations:
9770
9771   |28/24|23     19|18 16|15                    4|3     0|
9772   |  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|
9773
9774   This function is used by VMOV/VMVN/VORR/VBIC.  */
9775
9776 static void
9777 neon_write_immbits (unsigned immbits)
9778 {
9779   inst.instruction |= immbits & 0xf;
9780   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
9781   inst.instruction |= ((immbits >> 7) & 0x1) << 24;
9782 }
9783
9784 /* Invert low-order SIZE bits of XHI:XLO.  */
9785
9786 static void
9787 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
9788 {
9789   unsigned immlo = xlo ? *xlo : 0;
9790   unsigned immhi = xhi ? *xhi : 0;
9791
9792   switch (size)
9793     {
9794     case 8:
9795       immlo = (~immlo) & 0xff;
9796       break;
9797
9798     case 16:
9799       immlo = (~immlo) & 0xffff;
9800       break;
9801
9802     case 64:
9803       immhi = (~immhi) & 0xffffffff;
9804       /* fall through.  */
9805
9806     case 32:
9807       immlo = (~immlo) & 0xffffffff;
9808       break;
9809
9810     default:
9811       abort ();
9812     }
9813
9814   if (xlo)
9815     *xlo = immlo;
9816
9817   if (xhi)
9818     *xhi = immhi;
9819 }
9820
9821 static void
9822 do_neon_logic (void)
9823 {
9824   if (inst.operands[2].present && inst.operands[2].isreg)
9825     {
9826       enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
9827       neon_check_type (3, rs, N_IGNORE_TYPE);
9828       /* U bit and size field were set as part of the bitmask.  */
9829       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
9830       neon_three_same (TRUE, rs == NS_QQQ, 0, -1);
9831     }
9832   else
9833     {
9834       enum neon_shape rs = neon_check_shape (NS_DI_QI);
9835       struct neon_type_el et = neon_check_type (1, rs, N_I8 | N_I16 | N_I32
9836                                                 | N_I64 | N_F32);
9837       enum neon_opc opcode = inst.instruction & 0x0fffffff;
9838       unsigned immbits;
9839       int cmode;
9840       
9841       if (et.type == NT_invtype)
9842         return;
9843       
9844       inst.instruction = NEON_ENC_IMMED (inst.instruction);
9845
9846       switch (opcode)
9847         {
9848         case N_MNEM_vbic:
9849           cmode = neon_cmode_for_logic_imm (inst.operands[1].imm, &immbits,
9850                                             et.size);
9851           break;
9852         
9853         case N_MNEM_vorr:
9854           cmode = neon_cmode_for_logic_imm (inst.operands[1].imm, &immbits,
9855                                             et.size);
9856           break;
9857         
9858         case N_MNEM_vand:
9859           /* Pseudo-instruction for VBIC.  */
9860           immbits = inst.operands[1].imm;
9861           neon_invert_size (&immbits, 0, et.size);
9862           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
9863           break;
9864         
9865         case N_MNEM_vorn:
9866           /* Pseudo-instruction for VORR.  */
9867           immbits = inst.operands[1].imm;
9868           neon_invert_size (&immbits, 0, et.size);
9869           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
9870           break;
9871         
9872         default:
9873           abort ();
9874         }
9875
9876       if (cmode == FAIL)
9877         return;
9878
9879       inst.instruction |= (rs == NS_QI) << 6;
9880       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
9881       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
9882       inst.instruction |= cmode << 8;
9883       neon_write_immbits (immbits);
9884       
9885       inst.instruction = neon_dp_fixup (inst.instruction);
9886     }
9887 }
9888
9889 static void
9890 do_neon_bitfield (void)
9891 {
9892   enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
9893   /* FIXME: Check that no type was given.  */
9894   neon_three_same (FALSE, rs == NS_QQQ, 0, -1);
9895 }
9896
9897 static void
9898 neon_dyadic (enum neon_el_type ubit_meaning, unsigned types)
9899 {
9900   enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
9901   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK, types | N_KEY);
9902   if (et.type == NT_float)
9903     {
9904       inst.instruction = NEON_ENC_FLOAT (inst.instruction);
9905       neon_three_same (TRUE, rs == NS_QQQ, 0, -1);
9906     }
9907   else
9908     {
9909       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
9910       neon_three_same (TRUE, rs == NS_QQQ, et.type == ubit_meaning, et.size);
9911     }
9912 }
9913
9914 static void
9915 do_neon_dyadic_if_su (void)
9916 {
9917   neon_dyadic (NT_unsigned, N_SUF_32);
9918 }
9919
9920 static void
9921 do_neon_dyadic_if_su_d (void)
9922 {
9923   /* This version only allow D registers, but that constraint is enforced during
9924      operand parsing so we don't need to do anything extra here.  */
9925   neon_dyadic (NT_unsigned, N_SUF_32);
9926 }
9927
9928 static void
9929 do_neon_dyadic_if_i (void)
9930 {
9931   neon_dyadic (NT_unsigned, N_IF_32);
9932 }
9933
9934 static void
9935 do_neon_dyadic_if_i_d (void)
9936 {
9937   neon_dyadic (NT_unsigned, N_IF_32);
9938 }
9939
9940 static void
9941 do_neon_addsub_if_i (void)
9942 {
9943   /* The "untyped" case can't happen. Do this to stop the "U" bit being
9944      affected if we specify unsigned args.  */
9945   neon_dyadic (NT_untyped, N_IF_32 | N_I64);
9946 }
9947
9948 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
9949    result to be:
9950      V<op> A,B     (A is operand 0, B is operand 2)
9951    to mean:
9952      V<op> A,B,A
9953    not:
9954      V<op> A,B,B
9955    so handle that case specially.  */
9956
9957 static void
9958 neon_exchange_operands (void)
9959 {
9960   void *scratch = alloca (sizeof (inst.operands[0]));
9961   if (inst.operands[1].present)
9962     {
9963       /* Swap operands[1] and operands[2].  */
9964       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
9965       inst.operands[1] = inst.operands[2];
9966       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
9967     }
9968   else
9969     {
9970       inst.operands[1] = inst.operands[2];
9971       inst.operands[2] = inst.operands[0];
9972     }
9973 }
9974
9975 static void
9976 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
9977 {
9978   if (inst.operands[2].isreg)
9979     {
9980       if (invert)
9981         neon_exchange_operands ();
9982       neon_dyadic (NT_unsigned, regtypes);
9983     }
9984   else
9985     {
9986       enum neon_shape rs = neon_check_shape (NS_DDI_QQI);
9987       struct neon_type_el et = neon_check_type (2, rs, N_EQK, immtypes | N_KEY);
9988
9989       inst.instruction = NEON_ENC_IMMED (inst.instruction);
9990       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
9991       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
9992       inst.instruction |= LOW4 (inst.operands[1].reg);
9993       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
9994       inst.instruction |= (rs == NS_QQI) << 6;
9995       inst.instruction |= (et.type == NT_float) << 10;
9996       inst.instruction |= neon_logbits (et.size) << 18;
9997       
9998       inst.instruction = neon_dp_fixup (inst.instruction);
9999     }
10000 }
10001
10002 static void
10003 do_neon_cmp (void)
10004 {
10005   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, FALSE);
10006 }
10007
10008 static void
10009 do_neon_cmp_inv (void)
10010 {
10011   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, TRUE);
10012 }
10013
10014 static void
10015 do_neon_ceq (void)
10016 {
10017   neon_compare (N_IF_32, N_IF_32, FALSE);
10018 }
10019
10020 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
10021    scalars, which are encoded in 5 bits, M : Rm.
10022    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
10023    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
10024    index in M.  */
10025
10026 static unsigned
10027 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
10028 {
10029   unsigned regno = scalar >> 3;
10030   unsigned elno = scalar & 7;
10031
10032   switch (elsize)
10033     {
10034     case 16:
10035       if (regno > 7 || elno > 3)
10036         goto bad_scalar;
10037       return regno | (elno << 3);
10038     
10039     case 32:
10040       if (regno > 15 || elno > 1)
10041         goto bad_scalar;
10042       return regno | (elno << 4);
10043
10044     default:
10045     bad_scalar:
10046       as_bad (_("Scalar out of range for multiply instruction"));
10047     }
10048
10049   return 0;
10050 }
10051
10052 /* Encode multiply / multiply-accumulate scalar instructions.  */
10053
10054 static void
10055 neon_mul_mac (struct neon_type_el et, int ubit)
10056 {
10057   unsigned scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
10058   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10059   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10060   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
10061   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
10062   inst.instruction |= LOW4 (scalar);
10063   inst.instruction |= HI1 (scalar) << 5;
10064   inst.instruction |= (et.type == NT_float) << 8;
10065   inst.instruction |= neon_logbits (et.size) << 20;
10066   inst.instruction |= (ubit != 0) << 24;
10067
10068   inst.instruction = neon_dp_fixup (inst.instruction);
10069 }
10070
10071 static void
10072 do_neon_mac_maybe_scalar (void)
10073 {
10074   if (inst.operands[2].isscalar)
10075     {
10076       enum neon_shape rs = neon_check_shape (NS_DDS_QQS);
10077       struct neon_type_el et = neon_check_type (3, rs,
10078         N_EQK, N_EQK, N_I16 | N_I32 | N_F32 | N_KEY);
10079       inst.instruction = NEON_ENC_SCALAR (inst.instruction);
10080       neon_mul_mac (et, rs == NS_QQS);
10081     }
10082   else
10083     do_neon_dyadic_if_i ();
10084 }
10085
10086 static void
10087 do_neon_tst (void)
10088 {
10089   enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
10090   struct neon_type_el et = neon_check_type (3, rs,
10091     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
10092   neon_three_same (TRUE, rs == NS_QQQ, 0, et.size);
10093 }
10094
10095 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
10096    same types as the MAC equivalents. The polynomial type for this instruction
10097    is encoded the same as the integer type.  */
10098
10099 static void
10100 do_neon_mul (void)
10101 {
10102   if (inst.operands[2].isscalar)
10103     do_neon_mac_maybe_scalar ();
10104   else
10105     neon_dyadic (NT_poly, N_I8 | N_I16 | N_I32 | N_F32 | N_P8);
10106 }
10107
10108 static void
10109 do_neon_qdmulh (void)
10110 {
10111   if (inst.operands[2].isscalar)
10112     {
10113       enum neon_shape rs = neon_check_shape (NS_DDS_QQS);
10114       struct neon_type_el et = neon_check_type (3, rs,
10115         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
10116       inst.instruction = NEON_ENC_SCALAR (inst.instruction);
10117       neon_mul_mac (et, rs == NS_QQS);
10118     }
10119   else
10120     {
10121       enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
10122       struct neon_type_el et = neon_check_type (3, rs,
10123         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
10124       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10125       /* The U bit (rounding) comes from bit mask.  */
10126       neon_three_same (TRUE, rs == NS_QQQ, 0, et.size);
10127     }
10128 }
10129
10130 static void
10131 do_neon_fcmp_absolute (void)
10132 {
10133   enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
10134   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
10135   /* Size field comes from bit mask.  */
10136   neon_three_same (TRUE, rs == NS_QQQ, 1, -1);
10137 }
10138
10139 static void
10140 do_neon_fcmp_absolute_inv (void)
10141 {
10142   neon_exchange_operands ();
10143   do_neon_fcmp_absolute ();
10144 }
10145
10146 static void
10147 do_neon_step (void)
10148 {
10149   enum neon_shape rs = neon_check_shape (NS_DDD_QQQ);
10150   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
10151   neon_three_same (TRUE, rs == NS_QQQ, 0, -1);
10152 }
10153
10154 static void
10155 do_neon_abs_neg (void)
10156 {
10157   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10158   struct neon_type_el et = neon_check_type (3, rs,
10159     N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_F32 | N_KEY);
10160   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10161   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10162   inst.instruction |= LOW4 (inst.operands[1].reg);
10163   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
10164   inst.instruction |= (rs == NS_QQ) << 6;
10165   inst.instruction |= (et.type == NT_float) << 10;
10166   inst.instruction |= neon_logbits (et.size) << 18;
10167   
10168   inst.instruction = neon_dp_fixup (inst.instruction);
10169 }
10170
10171 static void
10172 do_neon_sli (void)
10173 {
10174   enum neon_shape rs = neon_check_shape (NS_DDI_QQI);
10175   struct neon_type_el et = neon_check_type (2, rs,
10176     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
10177   int imm = inst.operands[2].imm;
10178   constraint (imm < 0 || (unsigned)imm >= et.size,
10179               _("immediate out of range for insert"));
10180   neon_imm_shift (FALSE, 0, rs == NS_QQI, et, imm);
10181 }
10182
10183 static void
10184 do_neon_sri (void)
10185 {
10186   enum neon_shape rs = neon_check_shape (NS_DDI_QQI);
10187   struct neon_type_el et = neon_check_type (2, rs,
10188     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
10189   int imm = inst.operands[2].imm;
10190   constraint (imm < 1 || (unsigned)imm > et.size,
10191               _("immediate out of range for insert"));
10192   neon_imm_shift (FALSE, 0, rs == NS_QQI, et, et.size - imm);
10193 }
10194
10195 static void
10196 do_neon_qshlu_imm (void)
10197 {
10198   enum neon_shape rs = neon_check_shape (NS_DDI_QQI);
10199   struct neon_type_el et = neon_check_type (2, rs,
10200     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
10201   int imm = inst.operands[2].imm;
10202   constraint (imm < 0 || (unsigned)imm >= et.size,
10203               _("immediate out of range for shift"));
10204   /* Only encodes the 'U present' variant of the instruction.
10205      In this case, signed types have OP (bit 8) set to 0.
10206      Unsigned types have OP set to 1.  */
10207   inst.instruction |= (et.type == NT_unsigned) << 8;
10208   /* The rest of the bits are the same as other immediate shifts.  */
10209   neon_imm_shift (FALSE, 0, rs == NS_QQI, et, imm);
10210 }
10211
10212 static void
10213 do_neon_qmovn (void)
10214 {
10215   struct neon_type_el et = neon_check_type (2, NS_DQ,
10216     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
10217   /* Saturating move where operands can be signed or unsigned, and the
10218      destination has the same signedness.  */
10219   inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10220   if (et.type == NT_unsigned)
10221     inst.instruction |= 0xc0;
10222   else
10223     inst.instruction |= 0x80;
10224   neon_two_same (0, 1, et.size / 2);
10225 }
10226
10227 static void
10228 do_neon_qmovun (void)
10229 {
10230   struct neon_type_el et = neon_check_type (2, NS_DQ,
10231     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
10232   /* Saturating move with unsigned results. Operands must be signed.  */
10233   inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10234   neon_two_same (0, 1, et.size / 2);
10235 }
10236
10237 static void
10238 do_neon_rshift_sat_narrow (void)
10239 {
10240   /* FIXME: Types for narrowing. If operands are signed, results can be signed
10241      or unsigned. If operands are unsigned, results must also be unsigned.  */
10242   struct neon_type_el et = neon_check_type (2, NS_DQI,
10243     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
10244   int imm = inst.operands[2].imm;
10245   /* This gets the bounds check, size encoding and immediate bits calculation
10246      right.  */
10247   et.size /= 2;
10248   
10249   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
10250      VQMOVN.I<size> <Dd>, <Qm>.  */
10251   if (imm == 0)
10252     {
10253       inst.operands[2].present = 0;
10254       inst.instruction = N_MNEM_vqmovn;
10255       do_neon_qmovn ();
10256       return;
10257     }
10258   
10259   constraint (imm < 1 || (unsigned)imm > et.size,
10260               _("immediate out of range"));
10261   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
10262 }
10263
10264 static void
10265 do_neon_rshift_sat_narrow_u (void)
10266 {
10267   /* FIXME: Types for narrowing. If operands are signed, results can be signed
10268      or unsigned. If operands are unsigned, results must also be unsigned.  */
10269   struct neon_type_el et = neon_check_type (2, NS_DQI,
10270     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
10271   int imm = inst.operands[2].imm;
10272   /* This gets the bounds check, size encoding and immediate bits calculation
10273      right.  */
10274   et.size /= 2;
10275
10276   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
10277      VQMOVUN.I<size> <Dd>, <Qm>.  */
10278   if (imm == 0)
10279     {
10280       inst.operands[2].present = 0;
10281       inst.instruction = N_MNEM_vqmovun;
10282       do_neon_qmovun ();
10283       return;
10284     }
10285
10286   constraint (imm < 1 || (unsigned)imm > et.size,
10287               _("immediate out of range"));
10288   /* FIXME: The manual is kind of unclear about what value U should have in
10289      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
10290      must be 1.  */
10291   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
10292 }
10293
10294 static void
10295 do_neon_movn (void)
10296 {
10297   struct neon_type_el et = neon_check_type (2, NS_DQ,
10298     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
10299   inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10300   neon_two_same (0, 1, et.size / 2);
10301 }
10302
10303 static void
10304 do_neon_rshift_narrow (void)
10305 {
10306   struct neon_type_el et = neon_check_type (2, NS_DQI,
10307     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
10308   int imm = inst.operands[2].imm;
10309   /* This gets the bounds check, size encoding and immediate bits calculation
10310      right.  */
10311   et.size /= 2;
10312   
10313   /* If immediate is zero then we are a pseudo-instruction for
10314      VMOVN.I<size> <Dd>, <Qm>  */
10315   if (imm == 0)
10316     {
10317       inst.operands[2].present = 0;
10318       inst.instruction = N_MNEM_vmovn;
10319       do_neon_movn ();
10320       return;
10321     }
10322   
10323   constraint (imm < 1 || (unsigned)imm > et.size,
10324               _("immediate out of range for narrowing operation"));
10325   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
10326 }
10327
10328 static void
10329 do_neon_shll (void)
10330 {
10331   /* FIXME: Type checking when lengthening.  */
10332   struct neon_type_el et = neon_check_type (2, NS_QDI,
10333     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
10334   unsigned imm = inst.operands[2].imm;
10335
10336   if (imm == et.size)
10337     {
10338       /* Maximum shift variant.  */
10339       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10340       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10341       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10342       inst.instruction |= LOW4 (inst.operands[1].reg);
10343       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
10344       inst.instruction |= neon_logbits (et.size) << 18;
10345       
10346       inst.instruction = neon_dp_fixup (inst.instruction);
10347     }
10348   else
10349     {
10350       /* A more-specific type check for non-max versions.  */
10351       et = neon_check_type (2, NS_QDI,
10352         N_EQK | N_DBL, N_SU_32 | N_KEY);
10353       inst.instruction = NEON_ENC_IMMED (inst.instruction);
10354       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
10355     }
10356 }
10357
10358 /* Check the various types for the VCVT instruction, and return the one that
10359    the current instruction is.  */
10360
10361 static int
10362 neon_cvt_flavour (enum neon_shape rs)
10363 {
10364 #define CVT_VAR(C,X,Y)                          \
10365   et = neon_check_type (2, rs, (X), (Y));       \
10366   if (et.type != NT_invtype)                    \
10367     {                                           \
10368       inst.error = NULL;                        \
10369       return (C);                               \
10370     }
10371   struct neon_type_el et;
10372   
10373   CVT_VAR (0, N_S32, N_F32);
10374   CVT_VAR (1, N_U32, N_F32);
10375   CVT_VAR (2, N_F32, N_S32);
10376   CVT_VAR (3, N_F32, N_U32);
10377   
10378   return -1;
10379 #undef CVT_VAR
10380 }
10381
10382 static void
10383 do_neon_cvt (void)
10384 {
10385   /* Fixed-point conversion with #0 immediate is encoded as an integer
10386      conversion.  */
10387   if (inst.operands[2].present && inst.operands[2].imm != 0)
10388     {
10389       enum neon_shape rs = neon_check_shape (NS_DDI_QQI);
10390       int flavour = neon_cvt_flavour (rs);
10391       unsigned immbits = 32 - inst.operands[2].imm;
10392       unsigned enctab[] = { 0x0000100, 0x1000100, 0x0, 0x1000000 };
10393       inst.instruction = NEON_ENC_IMMED (inst.instruction);
10394       if (flavour != -1)
10395         inst.instruction |= enctab[flavour];
10396       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10397       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10398       inst.instruction |= LOW4 (inst.operands[1].reg);
10399       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
10400       inst.instruction |= (rs == NS_QQI) << 6;
10401       inst.instruction |= 1 << 21;
10402       inst.instruction |= immbits << 16;
10403     }
10404   else
10405     {
10406       enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10407       int flavour = neon_cvt_flavour (rs);
10408       unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080 };
10409       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10410       if (flavour != -1)
10411         inst.instruction |= enctab[flavour];
10412       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10413       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10414       inst.instruction |= LOW4 (inst.operands[1].reg);
10415       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
10416       inst.instruction |= (rs == NS_QQ) << 6;
10417       inst.instruction |= 2 << 18;
10418     }
10419   inst.instruction = neon_dp_fixup (inst.instruction);
10420 }
10421
10422 static void
10423 neon_move_immediate (void)
10424 {
10425   enum neon_shape rs = neon_check_shape (NS_DI_QI);
10426   struct neon_type_el et = neon_check_type (1, rs,
10427     N_I8 | N_I16 | N_I32 | N_I64 | N_F32);
10428   unsigned immlo, immhi = 0, immbits;
10429   int op, cmode;
10430
10431   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
10432   op = (inst.instruction & (1 << 5)) != 0;
10433
10434   immlo = inst.operands[1].imm;
10435   if (inst.operands[1].regisimm)
10436     immhi = inst.operands[1].reg;
10437
10438   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
10439               _("immediate has bits set outside the operand size"));
10440
10441   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, &immbits, &op,
10442                                         et.size)) == FAIL)
10443     {
10444       /* Invert relevant bits only.  */
10445       neon_invert_size (&immlo, &immhi, et.size);
10446       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
10447          with one or the other; those cases are caught by
10448          neon_cmode_for_move_imm.  */
10449       op = !op;
10450       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, &immbits, &op,
10451                                             et.size)) == FAIL)
10452         {
10453           inst.error = _("immediate out of range");
10454           return;
10455         }
10456     }
10457
10458   inst.instruction &= ~(1 << 5);
10459   inst.instruction |= op << 5;
10460
10461   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10462   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10463   inst.instruction |= (rs == NS_QI) << 6;
10464   inst.instruction |= cmode << 8;
10465
10466   neon_write_immbits (immbits);
10467 }
10468
10469 static void
10470 do_neon_mvn (void)
10471 {
10472   if (inst.operands[1].isreg)
10473     {
10474       enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10475       
10476       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10477       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10478       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10479       inst.instruction |= LOW4 (inst.operands[1].reg);
10480       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
10481       inst.instruction |= (rs == NS_QQ) << 6;
10482     }
10483   else
10484     {
10485       inst.instruction = NEON_ENC_IMMED (inst.instruction);
10486       neon_move_immediate ();
10487     }
10488
10489   inst.instruction = neon_dp_fixup (inst.instruction);
10490 }
10491
10492 /* Encode instructions of form:
10493
10494   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
10495   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |
10496
10497 */
10498
10499 static void
10500 neon_mixed_length (struct neon_type_el et, unsigned size)
10501 {
10502   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10503   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10504   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
10505   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
10506   inst.instruction |= LOW4 (inst.operands[2].reg);
10507   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
10508   inst.instruction |= (et.type == NT_unsigned) << 24;
10509   inst.instruction |= neon_logbits (size) << 20;
10510   
10511   inst.instruction = neon_dp_fixup (inst.instruction);
10512 }
10513
10514 static void
10515 do_neon_dyadic_long (void)
10516 {
10517   /* FIXME: Type checking for lengthening op.  */
10518   struct neon_type_el et = neon_check_type (3, NS_QDD,
10519     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
10520   neon_mixed_length (et, et.size);
10521 }
10522
10523 static void
10524 do_neon_abal (void)
10525 {
10526   struct neon_type_el et = neon_check_type (3, NS_QDD,
10527     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
10528   neon_mixed_length (et, et.size);
10529 }
10530
10531 static void
10532 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
10533 {
10534   if (inst.operands[2].isscalar)
10535     {
10536       struct neon_type_el et = neon_check_type (2, NS_QDS,
10537         N_EQK | N_DBL, regtypes | N_KEY);
10538       inst.instruction = NEON_ENC_SCALAR (inst.instruction);
10539       neon_mul_mac (et, et.type == NT_unsigned);
10540     }
10541   else
10542     {
10543       struct neon_type_el et = neon_check_type (3, NS_QDD,
10544         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
10545       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10546       neon_mixed_length (et, et.size);
10547     }
10548 }
10549
10550 static void
10551 do_neon_mac_maybe_scalar_long (void)
10552 {
10553   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
10554 }
10555
10556 static void
10557 do_neon_dyadic_wide (void)
10558 {
10559   struct neon_type_el et = neon_check_type (3, NS_QQD,
10560     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
10561   neon_mixed_length (et, et.size);
10562 }
10563
10564 static void
10565 do_neon_dyadic_narrow (void)
10566 {
10567   struct neon_type_el et = neon_check_type (3, NS_QDD,
10568     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
10569   neon_mixed_length (et, et.size / 2);
10570 }
10571
10572 static void
10573 do_neon_mul_sat_scalar_long (void)
10574 {
10575   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
10576 }
10577
10578 static void
10579 do_neon_vmull (void)
10580 {
10581   if (inst.operands[2].isscalar)
10582     do_neon_mac_maybe_scalar_long ();
10583   else
10584     {
10585       struct neon_type_el et = neon_check_type (3, NS_QDD,
10586         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_KEY);
10587       if (et.type == NT_poly)
10588         inst.instruction = NEON_ENC_POLY (inst.instruction);
10589       else
10590         inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10591       /* For polynomial encoding, size field must be 0b00 and the U bit must be
10592          zero. Should be OK as-is.  */
10593       neon_mixed_length (et, et.size);
10594     }
10595 }
10596
10597 static void
10598 do_neon_ext (void)
10599 {
10600   enum neon_shape rs = neon_check_shape (NS_DDDI_QQQI);
10601   struct neon_type_el et = neon_check_type (3, rs,
10602     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
10603   unsigned imm = (inst.operands[3].imm * et.size) / 8;
10604   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10605   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10606   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
10607   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
10608   inst.instruction |= LOW4 (inst.operands[2].reg);
10609   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
10610   inst.instruction |= (rs == NS_QQQI) << 6;
10611   inst.instruction |= imm << 8;
10612   
10613   inst.instruction = neon_dp_fixup (inst.instruction);
10614 }
10615
10616 static void
10617 do_neon_rev (void)
10618 {
10619   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10620   struct neon_type_el et = neon_check_type (2, rs,
10621     N_EQK, N_8 | N_16 | N_32 | N_KEY);
10622   unsigned op = (inst.instruction >> 7) & 3;
10623   /* N (width of reversed regions) is encoded as part of the bitmask. We
10624      extract it here to check the elements to be reversed are smaller.
10625      Otherwise we'd get a reserved instruction.  */
10626   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
10627   assert (elsize != 0);
10628   constraint (et.size >= elsize,
10629               _("elements must be smaller than reversal region"));
10630   neon_two_same (rs == NS_QQ, 1, et.size);
10631 }
10632
10633 static void
10634 do_neon_dup (void)
10635 {
10636   if (inst.operands[1].isscalar)
10637     {
10638       enum neon_shape rs = neon_check_shape (NS_DS_QS);
10639       struct neon_type_el et = neon_check_type (1, rs, N_8 | N_16 | N_32);
10640       unsigned sizebits = et.size >> 3;
10641       unsigned dm = inst.operands[1].reg >> 3;
10642       int logsize = neon_logbits (et.size);
10643       unsigned x = (inst.operands[1].reg & 7) << logsize;
10644       inst.instruction = NEON_ENC_SCALAR (inst.instruction);
10645       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10646       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10647       inst.instruction |= LOW4 (dm);
10648       inst.instruction |= HI1 (dm) << 5;
10649       inst.instruction |= (rs == NS_QS) << 6;
10650       inst.instruction |= x << 17;
10651       inst.instruction |= sizebits << 16;
10652       
10653       inst.instruction = neon_dp_fixup (inst.instruction);
10654     }
10655   else
10656     {
10657       enum neon_shape rs = neon_check_shape (NS_DR_QR);
10658       struct neon_type_el et = neon_check_type (1, rs, N_8 | N_16 | N_32);
10659       unsigned save_cond = inst.instruction & 0xf0000000;
10660       /* Duplicate ARM register to lanes of vector.  */
10661       inst.instruction = NEON_ENC_ARMREG (inst.instruction);
10662       switch (et.size)
10663         {
10664         case 8:  inst.instruction |= 0x400000; break;
10665         case 16: inst.instruction |= 0x000020; break;
10666         case 32: inst.instruction |= 0x000000; break;
10667         default: break;
10668         }
10669       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
10670       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
10671       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
10672       inst.instruction |= (rs == NS_QR) << 21;
10673       /* The encoding for this instruction is identical for the ARM and Thumb
10674          variants, except for the condition field.  */
10675       if (thumb_mode)
10676         inst.instruction |= 0xe0000000;
10677       else
10678         inst.instruction |= save_cond;
10679     }
10680 }
10681
10682 /* VMOV has particularly many variations. It can be one of:
10683      0. VMOV<c><q> <Qd>, <Qm>
10684      1. VMOV<c><q> <Dd>, <Dm>
10685    (Register operations, which are VORR with Rm = Rn.)
10686      2. VMOV<c><q>.<dt> <Qd>, #<imm>
10687      3. VMOV<c><q>.<dt> <Dd>, #<imm>
10688    (Immediate loads.)
10689      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
10690    (ARM register to scalar.)
10691      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
10692    (Two ARM registers to vector.)
10693      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
10694    (Scalar to ARM register.)
10695      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
10696    (Vector to two ARM registers.)
10697   
10698    We should have just enough information to be able to disambiguate most of
10699    these, apart from "Two ARM registers to vector" and "Vector to two ARM
10700    registers" cases. For these, abuse the .regisimm operand field to signify a
10701    Neon register.
10702    
10703    All the encoded bits are hardcoded by this function.
10704    
10705    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
10706    can specify a type where it doesn't make sense to, and is ignored).
10707 */
10708
10709 static void
10710 do_neon_mov (void)
10711 {
10712   int nargs = inst.operands[0].present + inst.operands[1].present
10713               + inst.operands[2].present;
10714   unsigned save_cond = thumb_mode ? 0xe0000000 : inst.instruction & 0xf0000000;
10715
10716   switch (nargs)
10717     {
10718     case 2:
10719       /* Cases 0, 1, 2, 3, 4, 6.  */
10720       if (inst.operands[1].isscalar)
10721         {
10722           /* Case 6.  */
10723           struct neon_type_el et = neon_check_type (1, NS_IGNORE,
10724             N_S8 | N_S16 | N_U8 | N_U16 | N_32);
10725           unsigned logsize = neon_logbits (et.size);
10726           unsigned dn = inst.operands[1].reg >> 3;
10727           unsigned x = inst.operands[1].reg & 7;
10728           unsigned abcdebits = 0;
10729
10730           constraint (x >= 64 / et.size, _("scalar index out of range"));
10731
10732           switch (et.size)
10733             {
10734             case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
10735             case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
10736             case 32: abcdebits = 0x00; break;
10737             default: ;
10738             }
10739
10740           abcdebits |= x << logsize;
10741           inst.instruction = save_cond;
10742           inst.instruction |= 0xe100b10;
10743           inst.instruction |= LOW4 (dn) << 16;
10744           inst.instruction |= HI1 (dn) << 7;
10745           inst.instruction |= inst.operands[0].reg << 12;
10746           inst.instruction |= (abcdebits & 3) << 5;
10747           inst.instruction |= (abcdebits >> 2) << 21;
10748         }
10749       else if (inst.operands[1].isreg)
10750         {
10751           /* Cases 0, 1, 4.  */
10752           if (inst.operands[0].isscalar)
10753             {
10754               /* Case 4.  */
10755               unsigned bcdebits = 0;
10756               struct neon_type_el et = neon_check_type (1, NS_IGNORE,
10757                                                         N_8 | N_16 | N_32);
10758               int logsize = neon_logbits (et.size);
10759               unsigned dn = inst.operands[0].reg >> 3;
10760               unsigned x = inst.operands[0].reg & 7;
10761
10762               constraint (x >= 64 / et.size, _("scalar index out of range"));
10763
10764               switch (et.size)
10765                 {
10766                 case 8:  bcdebits = 0x8; break;
10767                 case 16: bcdebits = 0x1; break;
10768                 case 32: bcdebits = 0x0; break;
10769                 default: ;
10770                 }
10771
10772               bcdebits |= x << logsize;
10773               inst.instruction = save_cond;
10774               inst.instruction |= 0xe000b10;
10775               inst.instruction |= LOW4 (dn) << 16;
10776               inst.instruction |= HI1 (dn) << 7;
10777               inst.instruction |= inst.operands[1].reg << 12;
10778               inst.instruction |= (bcdebits & 3) << 5;
10779               inst.instruction |= (bcdebits >> 2) << 21;
10780             }
10781           else
10782             {
10783               /* Cases 0, 1.  */
10784               enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10785               /* The architecture manual I have doesn't explicitly state which
10786                  value the U bit should have for register->register moves, but
10787                  the equivalent VORR instruction has U = 0, so do that.  */
10788               inst.instruction = 0x0200110;
10789               inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10790               inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10791               inst.instruction |= LOW4 (inst.operands[1].reg);
10792               inst.instruction |= HI1 (inst.operands[1].reg) << 5;
10793               inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
10794               inst.instruction |= HI1 (inst.operands[1].reg) << 7;
10795               inst.instruction |= (rs == NS_QQ) << 6;
10796               
10797               inst.instruction = neon_dp_fixup (inst.instruction);
10798             }
10799         }
10800       else
10801         {
10802           /* Cases 2, 3.  */
10803           inst.instruction = 0x0800010;
10804           neon_move_immediate ();
10805           inst.instruction = neon_dp_fixup (inst.instruction);
10806         }
10807       break;
10808     
10809     case 3:
10810       /* Cases 5, 7.  */
10811       if (inst.operands[0].regisimm)
10812         {
10813           /* Case 5.  */
10814           inst.instruction = save_cond;
10815           inst.instruction |= 0xc400b10;
10816           inst.instruction |= LOW4 (inst.operands[0].reg);
10817           inst.instruction |= HI1 (inst.operands[0].reg) << 5;
10818           inst.instruction |= inst.operands[1].reg << 12;
10819           inst.instruction |= inst.operands[2].reg << 16;
10820         }
10821       else
10822         {
10823           /* Case 7.  */
10824           inst.instruction = save_cond;
10825           inst.instruction |= 0xc500b10;
10826           inst.instruction |= inst.operands[0].reg << 12;
10827           inst.instruction |= inst.operands[1].reg << 16;
10828           inst.instruction |= LOW4 (inst.operands[2].reg);
10829           inst.instruction |= HI1 (inst.operands[2].reg) << 5;
10830         }
10831       break;
10832     
10833     default:
10834       abort ();
10835     }
10836 }
10837
10838 static void
10839 do_neon_rshift_round_imm (void)
10840 {
10841   enum neon_shape rs = neon_check_shape (NS_DDI_QQI);
10842   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
10843   int imm = inst.operands[2].imm;
10844
10845   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
10846   if (imm == 0)
10847     {
10848       inst.operands[2].present = 0;
10849       do_neon_mov ();
10850       return;
10851     }
10852
10853   constraint (imm < 1 || (unsigned)imm > et.size,
10854               _("immediate out of range for shift"));
10855   neon_imm_shift (TRUE, et.type == NT_unsigned, rs == NS_QQI, et,
10856                   et.size - imm);
10857 }
10858
10859 static void
10860 do_neon_movl (void)
10861 {
10862   struct neon_type_el et = neon_check_type (2, NS_QD,
10863     N_EQK | N_DBL, N_SU_32 | N_KEY);
10864   unsigned sizebits = et.size >> 3;
10865   inst.instruction |= sizebits << 19;
10866   neon_two_same (0, et.type == NT_unsigned, -1);
10867 }
10868
10869 static void
10870 do_neon_trn (void)
10871 {
10872   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10873   struct neon_type_el et = neon_check_type (2, rs,
10874     N_EQK, N_8 | N_16 | N_32 | N_KEY);
10875   inst.instruction = NEON_ENC_INTEGER (inst.instruction);
10876   neon_two_same (rs == NS_QQ, 1, et.size);
10877 }
10878
10879 static void
10880 do_neon_zip_uzp (void)
10881 {
10882   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10883   struct neon_type_el et = neon_check_type (2, rs,
10884     N_EQK, N_8 | N_16 | N_32 | N_KEY);
10885   if (rs == NS_DD && et.size == 32)
10886     {
10887       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
10888       inst.instruction = N_MNEM_vtrn;
10889       do_neon_trn ();
10890       return;
10891     }
10892   neon_two_same (rs == NS_QQ, 1, et.size);
10893 }
10894
10895 static void
10896 do_neon_sat_abs_neg (void)
10897 {
10898   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10899   struct neon_type_el et = neon_check_type (2, rs,
10900     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
10901   neon_two_same (rs == NS_QQ, 1, et.size);
10902 }
10903
10904 static void
10905 do_neon_pair_long (void)
10906 {
10907   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10908   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
10909   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
10910   inst.instruction |= (et.type == NT_unsigned) << 7;
10911   neon_two_same (rs == NS_QQ, 1, et.size);
10912 }
10913
10914 static void
10915 do_neon_recip_est (void)
10916 {
10917   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10918   struct neon_type_el et = neon_check_type (2, rs,
10919     N_EQK | N_FLT, N_F32 | N_U32 | N_KEY);
10920   inst.instruction |= (et.type == NT_float) << 8;
10921   neon_two_same (rs == NS_QQ, 1, et.size);
10922 }
10923
10924 static void
10925 do_neon_cls (void)
10926 {
10927   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10928   struct neon_type_el et = neon_check_type (2, rs,
10929     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
10930   neon_two_same (rs == NS_QQ, 1, et.size);
10931 }
10932
10933 static void
10934 do_neon_clz (void)
10935 {
10936   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10937   struct neon_type_el et = neon_check_type (2, rs,
10938     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
10939   neon_two_same (rs == NS_QQ, 1, et.size);
10940 }
10941
10942 static void
10943 do_neon_cnt (void)
10944 {
10945   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10946   struct neon_type_el et = neon_check_type (2, rs,
10947     N_EQK | N_INT, N_8 | N_KEY);
10948   neon_two_same (rs == NS_QQ, 1, et.size);
10949 }
10950
10951 static void
10952 do_neon_swp (void)
10953 {
10954   enum neon_shape rs = neon_check_shape (NS_DD_QQ);
10955   neon_two_same (rs == NS_QQ, 1, -1);
10956 }
10957
10958 static void
10959 do_neon_tbl_tbx (void)
10960 {
10961   unsigned listlenbits;
10962   neon_check_type (1, NS_DLD, N_8);
10963   
10964   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
10965     {
10966       inst.error = _("bad list length for table lookup");
10967       return;
10968     }
10969   
10970   listlenbits = inst.operands[1].imm - 1;
10971   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
10972   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
10973   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
10974   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
10975   inst.instruction |= LOW4 (inst.operands[2].reg);
10976   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
10977   inst.instruction |= listlenbits << 8;
10978   
10979   inst.instruction = neon_dp_fixup (inst.instruction);
10980 }
10981
10982 static void
10983 do_neon_ldm_stm (void)
10984 {
10985   /* P, U and L bits are part of bitmask.  */
10986   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
10987   unsigned offsetbits = inst.operands[1].imm * 2;
10988
10989   constraint (is_dbmode && !inst.operands[0].writeback,
10990               _("writeback (!) must be used for VLDMDB and VSTMDB"));
10991
10992   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
10993               _("register list must contain at least 1 and at most 16 "
10994                 "registers"));
10995
10996   inst.instruction |= inst.operands[0].reg << 16;
10997   inst.instruction |= inst.operands[0].writeback << 21;
10998   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
10999   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
11000
11001   inst.instruction |= offsetbits;
11002   
11003   if (thumb_mode)
11004     inst.instruction |= 0xe0000000;
11005 }
11006
11007 static void
11008 do_neon_ldr_str (void)
11009 {
11010   unsigned offsetbits;
11011   int offset_up = 1;
11012   int is_ldr = (inst.instruction & (1 << 20)) != 0;
11013   
11014   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
11015   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
11016   
11017   constraint (inst.reloc.pc_rel && !is_ldr,
11018               _("PC-relative addressing unavailable with VSTR"));
11019   
11020   constraint (!inst.reloc.pc_rel && inst.reloc.exp.X_op != O_constant,
11021               _("Immediate value must be a constant"));
11022   
11023   if (inst.reloc.exp.X_add_number < 0)
11024     {
11025       offset_up = 0;
11026       offsetbits = -inst.reloc.exp.X_add_number / 4;
11027     }
11028   else
11029     offsetbits = inst.reloc.exp.X_add_number / 4;
11030   
11031   /* FIXME: Does this catch everything?  */
11032   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
11033               || inst.operands[1].postind || inst.operands[1].writeback
11034               || inst.operands[1].immisreg || inst.operands[1].shifted,
11035               BAD_ADDR_MODE);
11036   constraint ((inst.operands[1].imm & 3) != 0,
11037               _("Offset must be a multiple of 4"));
11038   constraint (offsetbits != (offsetbits & 0xff),
11039               _("Immediate offset out of range"));
11040
11041   inst.instruction |= inst.operands[1].reg << 16;
11042   inst.instruction |= offsetbits & 0xff;
11043   inst.instruction |= offset_up << 23;
11044   
11045   if (thumb_mode)
11046     inst.instruction |= 0xe0000000;
11047
11048   if (inst.reloc.pc_rel)
11049     {
11050       if (thumb_mode)
11051         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
11052       else
11053         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
11054     }
11055   else
11056     inst.reloc.type = BFD_RELOC_UNUSED;
11057 }
11058
11059 /* "interleave" version also handles non-interleaving register VLD1/VST1
11060    instructions.  */
11061
11062 static void
11063 do_neon_ld_st_interleave (void)
11064 {
11065   struct neon_type_el et = neon_check_type (1, NS_IGNORE,
11066                                             N_8 | N_16 | N_32 | N_64);
11067   unsigned alignbits = 0;
11068   unsigned idx;
11069   /* The bits in this table go:
11070      0: register stride of one (0) or two (1)
11071      1,2: register list length, minus one (1, 2, 3, 4).
11072      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
11073      We use -1 for invalid entries.  */
11074   const int typetable[] =
11075     {
11076       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
11077        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
11078        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
11079        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
11080     };
11081   int typebits;
11082
11083   if (inst.operands[1].immisalign)
11084     switch (inst.operands[1].imm >> 8)
11085       {
11086       case 64: alignbits = 1; break;
11087       case 128:
11088         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) == 3)
11089           goto bad_alignment;
11090         alignbits = 2;
11091         break;
11092       case 256:
11093         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) == 3)
11094           goto bad_alignment;
11095         alignbits = 3;
11096         break;
11097       default:
11098       bad_alignment:
11099         inst.error = _("bad alignment");
11100         return;
11101       }
11102
11103   inst.instruction |= alignbits << 4;
11104   inst.instruction |= neon_logbits (et.size) << 6;
11105
11106   /* Bits [4:6] of the immediate in a list specifier encode register stride
11107      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
11108      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
11109      up the right value for "type" in a table based on this value and the given
11110      list style, then stick it back.  */
11111   idx = ((inst.operands[0].imm >> 4) & 7)
11112         | (((inst.instruction >> 8) & 3) << 3);
11113
11114   typebits = typetable[idx];
11115   
11116   constraint (typebits == -1, _("bad list type for instruction"));
11117
11118   inst.instruction &= ~0xf00;
11119   inst.instruction |= typebits << 8;
11120 }
11121
11122 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
11123    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
11124    otherwise. The variable arguments are a list of pairs of legal (size, align)
11125    values, terminated with -1.  */
11126
11127 static int
11128 neon_alignment_bit (int size, int align, int *do_align, ...)
11129 {
11130   va_list ap;
11131   int result = FAIL, thissize, thisalign;
11132     
11133   if (!inst.operands[1].immisalign)
11134     {
11135       *do_align = 0;
11136       return SUCCESS;
11137     }
11138       
11139   va_start (ap, do_align);
11140
11141   do
11142     {
11143       thissize = va_arg (ap, int);
11144       if (thissize == -1)
11145         break;
11146       thisalign = va_arg (ap, int);
11147
11148       if (size == thissize && align == thisalign)
11149         result = SUCCESS;
11150     }
11151   while (result != SUCCESS);
11152
11153   va_end (ap);
11154
11155   if (result == SUCCESS)
11156     *do_align = 1;
11157   else
11158     inst.error = _("unsupported alignment for instruction");
11159     
11160   return result;
11161 }
11162
11163 static void
11164 do_neon_ld_st_lane (void)
11165 {
11166   struct neon_type_el et = neon_check_type (1, NS_IGNORE, N_8 | N_16 | N_32);
11167   int align_good, do_align = 0;
11168   int logsize = neon_logbits (et.size);
11169   int align = inst.operands[1].imm >> 8;
11170   int n = (inst.instruction >> 8) & 3;
11171   int max_el = 64 / et.size;
11172   
11173   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
11174               _("bad list length"));
11175   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
11176               _("scalar index out of range"));
11177   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
11178               && et.size == 8,
11179               _("stride of 2 unavailable when element size is 8"));
11180   
11181   switch (n)
11182     {
11183     case 0:  /* VLD1 / VST1.  */
11184       align_good = neon_alignment_bit (et.size, align, &do_align, 16, 16,
11185                                        32, 32, -1);
11186       if (align_good == FAIL)
11187         return;
11188       if (do_align)
11189         {
11190           unsigned alignbits = 0;
11191           switch (et.size)
11192             {
11193             case 16: alignbits = 0x1; break;
11194             case 32: alignbits = 0x3; break;
11195             default: ;
11196             }
11197           inst.instruction |= alignbits << 4;
11198         }
11199       break;
11200
11201     case 1:  /* VLD2 / VST2.  */
11202       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 16, 16, 32,
11203                                        32, 64, -1);
11204       if (align_good == FAIL)
11205         return;
11206       if (do_align)
11207         inst.instruction |= 1 << 4;
11208       break;
11209
11210     case 2:  /* VLD3 / VST3.  */
11211       constraint (inst.operands[1].immisalign,
11212                   _("can't use alignment with this instruction"));
11213       break;
11214
11215     case 3:  /* VLD4 / VST4.  */
11216       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
11217                                        16, 64, 32, 64, 32, 128, -1);
11218       if (align_good == FAIL)
11219         return;
11220       if (do_align)
11221         {
11222           unsigned alignbits = 0;
11223           switch (et.size)
11224             {
11225             case 8:  alignbits = 0x1; break;
11226             case 16: alignbits = 0x1; break;
11227             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
11228             default: ;
11229             }
11230           inst.instruction |= alignbits << 4;
11231         }
11232       break;
11233
11234     default: ;
11235     }
11236
11237   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
11238   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
11239     inst.instruction |= 1 << (4 + logsize);
11240       
11241   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
11242   inst.instruction |= logsize << 10;
11243 }
11244
11245 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
11246
11247 static void
11248 do_neon_ld_dup (void)
11249 {
11250   struct neon_type_el et = neon_check_type (1, NS_IGNORE, N_8 | N_16 | N_32);
11251   int align_good, do_align = 0;
11252
11253   switch ((inst.instruction >> 8) & 3)
11254     {
11255     case 0:  /* VLD1.  */
11256       assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
11257       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
11258                                        &do_align, 16, 16, 32, 32, -1);
11259       if (align_good == FAIL)
11260         return;
11261       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
11262         {
11263         case 1: break;
11264         case 2: inst.instruction |= 1 << 5; break;
11265         default: inst.error = _("bad list length"); return;
11266         }
11267       inst.instruction |= neon_logbits (et.size) << 6;
11268       break;
11269
11270     case 1:  /* VLD2.  */
11271       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
11272                                        &do_align, 8, 16, 16, 32, 32, 64, -1);
11273       if (align_good == FAIL)
11274         return;
11275       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
11276                   _("bad list length"));
11277       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
11278         inst.instruction |= 1 << 5;
11279       inst.instruction |= neon_logbits (et.size) << 6;
11280       break;
11281
11282     case 2:  /* VLD3.  */
11283       constraint (inst.operands[1].immisalign,
11284                   _("can't use alignment with this instruction"));
11285       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
11286                   _("bad list length"));
11287       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
11288         inst.instruction |= 1 << 5;
11289       inst.instruction |= neon_logbits (et.size) << 6;
11290       break;
11291
11292     case 3:  /* VLD4.  */
11293       {
11294         int align = inst.operands[1].imm >> 8;
11295         align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
11296                                          16, 64, 32, 64, 32, 128, -1);
11297         if (align_good == FAIL)
11298           return;
11299         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
11300                     _("bad list length"));
11301         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
11302           inst.instruction |= 1 << 5;
11303         if (et.size == 32 && align == 128)
11304           inst.instruction |= 0x3 << 6;
11305         else
11306           inst.instruction |= neon_logbits (et.size) << 6;
11307       }
11308       break;
11309
11310     default: ;
11311     }
11312
11313   inst.instruction |= do_align << 4;
11314 }
11315
11316 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
11317    apart from bits [11:4].  */
11318
11319 static void
11320 do_neon_ldx_stx (void)
11321 {
11322   switch (NEON_LANE (inst.operands[0].imm))
11323     {
11324     case NEON_INTERLEAVE_LANES:
11325       inst.instruction = NEON_ENC_INTERLV (inst.instruction);
11326       do_neon_ld_st_interleave ();
11327       break;
11328     
11329     case NEON_ALL_LANES:
11330       inst.instruction = NEON_ENC_DUP (inst.instruction);
11331       do_neon_ld_dup ();
11332       break;
11333     
11334     default:
11335       inst.instruction = NEON_ENC_LANE (inst.instruction);
11336       do_neon_ld_st_lane ();
11337     }
11338
11339   /* L bit comes from bit mask.  */
11340   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
11341   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
11342   inst.instruction |= inst.operands[1].reg << 16;
11343   
11344   if (inst.operands[1].postind)
11345     {
11346       int postreg = inst.operands[1].imm & 0xf;
11347       constraint (!inst.operands[1].immisreg,
11348                   _("post-index must be a register"));
11349       constraint (postreg == 0xd || postreg == 0xf,
11350                   _("bad register for post-index"));
11351       inst.instruction |= postreg;
11352     }
11353   else if (inst.operands[1].writeback)
11354     {
11355       inst.instruction |= 0xd;
11356     }
11357   else
11358     inst.instruction |= 0xf; 
11359   
11360   if (thumb_mode)
11361     inst.instruction |= 0xf9000000;
11362   else
11363     inst.instruction |= 0xf4000000;
11364 }
11365
11366 \f
11367 /* Overall per-instruction processing.  */
11368
11369 /* We need to be able to fix up arbitrary expressions in some statements.
11370    This is so that we can handle symbols that are an arbitrary distance from
11371    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
11372    which returns part of an address in a form which will be valid for
11373    a data instruction.  We do this by pushing the expression into a symbol
11374    in the expr_section, and creating a fix for that.  */
11375
11376 static void
11377 fix_new_arm (fragS *       frag,
11378              int           where,
11379              short int     size,
11380              expressionS * exp,
11381              int           pc_rel,
11382              int           reloc)
11383 {
11384   fixS *           new_fix;
11385
11386   switch (exp->X_op)
11387     {
11388     case O_constant:
11389     case O_symbol:
11390     case O_add:
11391     case O_subtract:
11392       new_fix = fix_new_exp (frag, where, size, exp, pc_rel, reloc);
11393       break;
11394
11395     default:
11396       new_fix = fix_new (frag, where, size, make_expr_symbol (exp), 0,
11397                          pc_rel, reloc);
11398       break;
11399     }
11400
11401   /* Mark whether the fix is to a THUMB instruction, or an ARM
11402      instruction.  */
11403   new_fix->tc_fix_data = thumb_mode;
11404 }
11405
11406 /* Create a frg for an instruction requiring relaxation.  */
11407 static void
11408 output_relax_insn (void)
11409 {
11410   char * to;
11411   symbolS *sym;
11412   int offset;
11413
11414 #ifdef OBJ_ELF
11415   /* The size of the instruction is unknown, so tie the debug info to the
11416      start of the instruction.  */
11417   dwarf2_emit_insn (0);
11418 #endif
11419
11420   switch (inst.reloc.exp.X_op)
11421     {
11422     case O_symbol:
11423       sym = inst.reloc.exp.X_add_symbol;
11424       offset = inst.reloc.exp.X_add_number;
11425       break;
11426     case O_constant:
11427       sym = NULL;
11428       offset = inst.reloc.exp.X_add_number;
11429       break;
11430     default:
11431       sym = make_expr_symbol (&inst.reloc.exp);
11432       offset = 0;
11433       break;
11434   }
11435   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
11436                  inst.relax, sym, offset, NULL/*offset, opcode*/);
11437   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
11438 }
11439
11440 /* Write a 32-bit thumb instruction to buf.  */
11441 static void
11442 put_thumb32_insn (char * buf, unsigned long insn)
11443 {
11444   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
11445   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
11446 }
11447
11448 static void
11449 output_inst (const char * str)
11450 {
11451   char * to = NULL;
11452
11453   if (inst.error)
11454     {
11455       as_bad ("%s -- `%s'", inst.error, str);
11456       return;
11457     }
11458   if (inst.relax) {
11459       output_relax_insn();
11460       return;
11461   }
11462   if (inst.size == 0)
11463     return;
11464
11465   to = frag_more (inst.size);
11466
11467   if (thumb_mode && (inst.size > THUMB_SIZE))
11468     {
11469       assert (inst.size == (2 * THUMB_SIZE));
11470       put_thumb32_insn (to, inst.instruction);
11471     }
11472   else if (inst.size > INSN_SIZE)
11473     {
11474       assert (inst.size == (2 * INSN_SIZE));
11475       md_number_to_chars (to, inst.instruction, INSN_SIZE);
11476       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
11477     }
11478   else
11479     md_number_to_chars (to, inst.instruction, inst.size);
11480
11481   if (inst.reloc.type != BFD_RELOC_UNUSED)
11482     fix_new_arm (frag_now, to - frag_now->fr_literal,
11483                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
11484                  inst.reloc.type);
11485
11486 #ifdef OBJ_ELF
11487   dwarf2_emit_insn (inst.size);
11488 #endif
11489 }
11490
11491 /* Parse a Neon type specifier. *STR should point at the leading '.'
11492    character. Does no verification at this stage that the type fits the opcode
11493    properly. E.g.,
11494
11495      .i32.i32.s16
11496      .s32.f32
11497      .u16
11498
11499    Can all be legally parsed by this function.
11500
11501    Fills in neon_type struct pointer with parsed information, and updates STR
11502    to point after the parsed type specifier. Returns TRUE if this was a legal
11503    type, FALSE if not.  */
11504
11505 static bfd_boolean
11506 parse_neon_type (struct neon_type *type, char **str)
11507 {
11508   char *ptr = *str;
11509
11510   if (type)
11511     type->elems = 0;
11512
11513   while (type->elems < NEON_MAX_TYPE_ELS)
11514     {
11515       enum neon_el_type thistype = NT_untyped;
11516       unsigned thissize = -1u;
11517
11518       if (*ptr != '.')
11519         break;
11520
11521       ptr++;
11522
11523       /* Just a size without an explicit type.  */
11524       if (ISDIGIT (*ptr))
11525         goto parsesize;
11526
11527       switch (*ptr)
11528         {
11529         case 'i': thistype = NT_integer; break;
11530         case 'f': thistype = NT_float; break;
11531         case 'p': thistype = NT_poly; break;
11532         case 's': thistype = NT_signed; break;
11533         case 'u': thistype = NT_unsigned; break;
11534         default:
11535           as_bad (_("Unexpected character `%c' in type specifier"), *ptr);
11536           return 0;
11537         }
11538
11539       ptr++;
11540
11541       /* .f is an abbreviation for .f32.  */
11542       if (thistype == NT_float && !ISDIGIT (*ptr))
11543         thissize = 32;
11544       else
11545         {
11546         parsesize:
11547           thissize = strtoul (ptr, &ptr, 10);
11548
11549           if (thissize != 8 && thissize != 16 && thissize != 32
11550               && thissize != 64)
11551             {
11552               as_bad (_("Bad size %d in type specifier"), thissize);
11553               return 0;
11554             }
11555         }
11556
11557       if (type)
11558         {
11559           type->el[type->elems].type = thistype;
11560           type->el[type->elems].size = thissize;
11561           type->elems++;
11562         }
11563     }
11564
11565   *str = ptr;
11566
11567   return 1;
11568 }
11569
11570 /* Tag values used in struct asm_opcode's tag field.  */
11571 enum opcode_tag
11572 {
11573   OT_unconditional,     /* Instruction cannot be conditionalized.
11574                            The ARM condition field is still 0xE.  */
11575   OT_unconditionalF,    /* Instruction cannot be conditionalized
11576                            and carries 0xF in its ARM condition field.  */
11577   OT_csuffix,           /* Instruction takes a conditional suffix.  */
11578   OT_cinfix3,           /* Instruction takes a conditional infix,
11579                            beginning at character index 3.  (In
11580                            unified mode, it becomes a suffix.)  */
11581   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
11582                            character index 3, even in unified mode.  Used for
11583                            legacy instructions where suffix and infix forms
11584                            may be ambiguous.  */
11585   OT_csuf_or_in3,       /* Instruction takes either a conditional
11586                            suffix or an infix at character index 3.  */
11587   OT_odd_infix_unc,     /* This is the unconditional variant of an
11588                            instruction that takes a conditional infix
11589                            at an unusual position.  In unified mode,
11590                            this variant will accept a suffix.  */
11591   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
11592                            are the conditional variants of instructions that
11593                            take conditional infixes in unusual positions.
11594                            The infix appears at character index
11595                            (tag - OT_odd_infix_0).  These are not accepted
11596                            in unified mode.  */
11597 };
11598
11599 /* Subroutine of md_assemble, responsible for looking up the primary
11600    opcode from the mnemonic the user wrote.  STR points to the
11601    beginning of the mnemonic.
11602
11603    This is not simply a hash table lookup, because of conditional
11604    variants.  Most instructions have conditional variants, which are
11605    expressed with a _conditional affix_ to the mnemonic.  If we were
11606    to encode each conditional variant as a literal string in the opcode
11607    table, it would have approximately 20,000 entries.
11608
11609    Most mnemonics take this affix as a suffix, and in unified syntax,
11610    'most' is upgraded to 'all'.  However, in the divided syntax, some
11611    instructions take the affix as an infix, notably the s-variants of
11612    the arithmetic instructions.  Of those instructions, all but six
11613    have the infix appear after the third character of the mnemonic.
11614
11615    Accordingly, the algorithm for looking up primary opcodes given
11616    an identifier is:
11617
11618    1. Look up the identifier in the opcode table.
11619       If we find a match, go to step U.
11620
11621    2. Look up the last two characters of the identifier in the
11622       conditions table.  If we find a match, look up the first N-2
11623       characters of the identifier in the opcode table.  If we
11624       find a match, go to step CE.
11625
11626    3. Look up the fourth and fifth characters of the identifier in
11627       the conditions table.  If we find a match, extract those
11628       characters from the identifier, and look up the remaining
11629       characters in the opcode table.  If we find a match, go
11630       to step CM.
11631
11632    4. Fail.
11633
11634    U. Examine the tag field of the opcode structure, in case this is
11635       one of the six instructions with its conditional infix in an
11636       unusual place.  If it is, the tag tells us where to find the
11637       infix; look it up in the conditions table and set inst.cond
11638       accordingly.  Otherwise, this is an unconditional instruction.
11639       Again set inst.cond accordingly.  Return the opcode structure.
11640
11641   CE. Examine the tag field to make sure this is an instruction that
11642       should receive a conditional suffix.  If it is not, fail.
11643       Otherwise, set inst.cond from the suffix we already looked up,
11644       and return the opcode structure.
11645
11646   CM. Examine the tag field to make sure this is an instruction that
11647       should receive a conditional infix after the third character.
11648       If it is not, fail.  Otherwise, undo the edits to the current
11649       line of input and proceed as for case CE.  */
11650
11651 static const struct asm_opcode *
11652 opcode_lookup (char **str)
11653 {
11654   char *end, *base;
11655   char *affix;
11656   const struct asm_opcode *opcode;
11657   const struct asm_cond *cond;
11658   char save[2];
11659
11660   /* Scan up to the end of the mnemonic, which must end in white space,
11661      '.' (in unified mode only), or end of string.  */
11662   for (base = end = *str; *end != '\0'; end++)
11663     if (*end == ' ' || (unified_syntax && *end == '.'))
11664       break;
11665
11666   if (end == base)
11667     return 0;
11668
11669   /* Handle a possible width suffix and/or Neon type suffix.  */
11670   if (end[0] == '.')
11671     {
11672       int offset = 2;
11673       
11674       if (end[1] == 'w')
11675         inst.size_req = 4;
11676       else if (end[1] == 'n')
11677         inst.size_req = 2;
11678       else
11679         offset = 0;
11680
11681       inst.vectype.elems = 0;
11682
11683       *str = end + offset;
11684
11685       if (end[offset] == '.')      
11686         {
11687           /* See if we have a Neon type suffix.  */
11688           if (!parse_neon_type (&inst.vectype, str))
11689             return 0;
11690         }
11691       else if (end[offset] != '\0' && end[offset] != ' ')
11692         return 0;
11693     }
11694   else
11695     *str = end;
11696
11697   /* Look for unaffixed or special-case affixed mnemonic.  */
11698   opcode = hash_find_n (arm_ops_hsh, base, end - base);
11699   if (opcode)
11700     {
11701       /* step U */
11702       if (opcode->tag < OT_odd_infix_0)
11703         {
11704           inst.cond = COND_ALWAYS;
11705           return opcode;
11706         }
11707
11708       if (unified_syntax)
11709         as_warn (_("conditional infixes are deprecated in unified syntax"));
11710       affix = base + (opcode->tag - OT_odd_infix_0);
11711       cond = hash_find_n (arm_cond_hsh, affix, 2);
11712       assert (cond);
11713
11714       inst.cond = cond->value;
11715       return opcode;
11716     }
11717
11718   /* Cannot have a conditional suffix on a mnemonic of less than two
11719      characters.  */
11720   if (end - base < 3)
11721     return 0;
11722
11723   /* Look for suffixed mnemonic.  */
11724   affix = end - 2;
11725   cond = hash_find_n (arm_cond_hsh, affix, 2);
11726   opcode = hash_find_n (arm_ops_hsh, base, affix - base);
11727   if (opcode && cond)
11728     {
11729       /* step CE */
11730       switch (opcode->tag)
11731         {
11732         case OT_cinfix3_legacy:
11733           /* Ignore conditional suffixes matched on infix only mnemonics.  */
11734           break;
11735
11736         case OT_cinfix3:
11737         case OT_odd_infix_unc:
11738           if (!unified_syntax)
11739             return 0;
11740           /* else fall through */
11741
11742         case OT_csuffix:
11743         case OT_csuf_or_in3:
11744           inst.cond = cond->value;
11745           return opcode;
11746
11747         case OT_unconditional:
11748         case OT_unconditionalF:
11749           if (thumb_mode)
11750             {
11751               inst.cond = cond->value;
11752             }
11753           else
11754             {
11755               /* delayed diagnostic */
11756               inst.error = BAD_COND;
11757               inst.cond = COND_ALWAYS;
11758             }
11759           return opcode;
11760
11761         default:
11762           return 0;
11763         }
11764     }
11765
11766   /* Cannot have a usual-position infix on a mnemonic of less than
11767      six characters (five would be a suffix).  */
11768   if (end - base < 6)
11769     return 0;
11770
11771   /* Look for infixed mnemonic in the usual position.  */
11772   affix = base + 3;
11773   cond = hash_find_n (arm_cond_hsh, affix, 2);
11774   if (!cond)
11775     return 0;
11776
11777   memcpy (save, affix, 2);
11778   memmove (affix, affix + 2, (end - affix) - 2);
11779   opcode = hash_find_n (arm_ops_hsh, base, (end - base) - 2);
11780   memmove (affix + 2, affix, (end - affix) - 2);
11781   memcpy (affix, save, 2);
11782
11783   if (opcode && (opcode->tag == OT_cinfix3 || opcode->tag == OT_csuf_or_in3
11784                  || opcode->tag == OT_cinfix3_legacy))
11785     {
11786       /* step CM */
11787       if (unified_syntax && opcode->tag == OT_cinfix3)
11788         as_warn (_("conditional infixes are deprecated in unified syntax"));
11789
11790       inst.cond = cond->value;
11791       return opcode;
11792     }
11793
11794   return 0;
11795 }
11796
11797 void
11798 md_assemble (char *str)
11799 {
11800   char *p = str;
11801   const struct asm_opcode * opcode;
11802
11803   /* Align the previous label if needed.  */
11804   if (last_label_seen != NULL)
11805     {
11806       symbol_set_frag (last_label_seen, frag_now);
11807       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
11808       S_SET_SEGMENT (last_label_seen, now_seg);
11809     }
11810
11811   memset (&inst, '\0', sizeof (inst));
11812   inst.reloc.type = BFD_RELOC_UNUSED;
11813
11814   opcode = opcode_lookup (&p);
11815   if (!opcode)
11816     {
11817       /* It wasn't an instruction, but it might be a register alias of
11818          the form alias .req reg.  */
11819       if (!create_register_alias (str, p))
11820         as_bad (_("bad instruction `%s'"), str);
11821
11822       return;
11823     }
11824
11825   if (thumb_mode)
11826     {
11827       arm_feature_set variant;
11828
11829       variant = cpu_variant;
11830       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
11831       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
11832         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
11833       /* Check that this instruction is supported for this CPU.  */
11834       if (!opcode->tvariant
11835           || (thumb_mode == 1
11836               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
11837         {
11838           as_bad (_("selected processor does not support `%s'"), str);
11839           return;
11840         }
11841       if (inst.cond != COND_ALWAYS && !unified_syntax
11842           && opcode->tencode != do_t_branch)
11843         {
11844           as_bad (_("Thumb does not support conditional execution"));
11845           return;
11846         }
11847
11848       /* Check conditional suffixes.  */
11849       if (current_it_mask)
11850         {
11851           int cond;
11852           cond = current_cc ^ ((current_it_mask >> 4) & 1) ^ 1;
11853           current_it_mask <<= 1;
11854           current_it_mask &= 0x1f;
11855           /* The BKPT instruction is unconditional even in an IT block.  */
11856           if (!inst.error
11857               && cond != inst.cond && opcode->tencode != do_t_bkpt)
11858             {
11859               as_bad (_("incorrect condition in IT block"));
11860               return;
11861             }
11862         }
11863       else if (inst.cond != COND_ALWAYS && opcode->tencode != do_t_branch)
11864         {
11865           as_bad (_("thumb conditional instrunction not in IT block"));
11866           return;
11867         }
11868
11869       mapping_state (MAP_THUMB);
11870       inst.instruction = opcode->tvalue;
11871
11872       if (!parse_operands (p, opcode->operands))
11873         opcode->tencode ();
11874
11875       /* Clear current_it_mask at the end of an IT block.  */
11876       if (current_it_mask == 0x10)
11877         current_it_mask = 0;
11878
11879       if (!(inst.error || inst.relax))
11880         {
11881           assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
11882           inst.size = (inst.instruction > 0xffff ? 4 : 2);
11883           if (inst.size_req && inst.size_req != inst.size)
11884             {
11885               as_bad (_("cannot honor width suffix -- `%s'"), str);
11886               return;
11887             }
11888         }
11889       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
11890                               *opcode->tvariant);
11891       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
11892          set those bits when Thumb-2 32-bit instuctions are seen.  ie.
11893          anything other than bl/blx.
11894          This is overly pessimistic for relaxable instructions.  */
11895       if ((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
11896           || inst.relax)
11897         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
11898                                 arm_ext_v6t2);
11899     }
11900   else
11901     {
11902       /* Check that this instruction is supported for this CPU.  */
11903       if (!opcode->avariant ||
11904           !ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant))
11905         {
11906           as_bad (_("selected processor does not support `%s'"), str);
11907           return;
11908         }
11909       if (inst.size_req)
11910         {
11911           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
11912           return;
11913         }
11914
11915       mapping_state (MAP_ARM);
11916       inst.instruction = opcode->avalue;
11917       if (opcode->tag == OT_unconditionalF)
11918         inst.instruction |= 0xF << 28;
11919       else
11920         inst.instruction |= inst.cond << 28;
11921       inst.size = INSN_SIZE;
11922       if (!parse_operands (p, opcode->operands))
11923         opcode->aencode ();
11924       /* Arm mode bx is marked as both v4T and v5 because it's still required
11925          on a hypothetical non-thumb v5 core.  */
11926       if (ARM_CPU_HAS_FEATURE (*opcode->avariant, arm_ext_v4t)
11927           || ARM_CPU_HAS_FEATURE (*opcode->avariant, arm_ext_v5))
11928         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
11929       else
11930         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
11931                                 *opcode->avariant);
11932     }
11933   output_inst (str);
11934 }
11935
11936 /* Various frobbings of labels and their addresses.  */
11937
11938 void
11939 arm_start_line_hook (void)
11940 {
11941   last_label_seen = NULL;
11942 }
11943
11944 void
11945 arm_frob_label (symbolS * sym)
11946 {
11947   last_label_seen = sym;
11948
11949   ARM_SET_THUMB (sym, thumb_mode);
11950
11951 #if defined OBJ_COFF || defined OBJ_ELF
11952   ARM_SET_INTERWORK (sym, support_interwork);
11953 #endif
11954
11955   /* Note - do not allow local symbols (.Lxxx) to be labeled
11956      as Thumb functions.  This is because these labels, whilst
11957      they exist inside Thumb code, are not the entry points for
11958      possible ARM->Thumb calls.  Also, these labels can be used
11959      as part of a computed goto or switch statement.  eg gcc
11960      can generate code that looks like this:
11961
11962                 ldr  r2, [pc, .Laaa]
11963                 lsl  r3, r3, #2
11964                 ldr  r2, [r3, r2]
11965                 mov  pc, r2
11966
11967        .Lbbb:  .word .Lxxx
11968        .Lccc:  .word .Lyyy
11969        ..etc...
11970        .Laaa:   .word Lbbb
11971
11972      The first instruction loads the address of the jump table.
11973      The second instruction converts a table index into a byte offset.
11974      The third instruction gets the jump address out of the table.
11975      The fourth instruction performs the jump.
11976
11977      If the address stored at .Laaa is that of a symbol which has the
11978      Thumb_Func bit set, then the linker will arrange for this address
11979      to have the bottom bit set, which in turn would mean that the
11980      address computation performed by the third instruction would end
11981      up with the bottom bit set.  Since the ARM is capable of unaligned
11982      word loads, the instruction would then load the incorrect address
11983      out of the jump table, and chaos would ensue.  */
11984   if (label_is_thumb_function_name
11985       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
11986       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
11987     {
11988       /* When the address of a Thumb function is taken the bottom
11989          bit of that address should be set.  This will allow
11990          interworking between Arm and Thumb functions to work
11991          correctly.  */
11992
11993       THUMB_SET_FUNC (sym, 1);
11994
11995       label_is_thumb_function_name = FALSE;
11996     }
11997
11998 #ifdef OBJ_ELF
11999   dwarf2_emit_label (sym);
12000 #endif
12001 }
12002
12003 int
12004 arm_data_in_code (void)
12005 {
12006   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
12007     {
12008       *input_line_pointer = '/';
12009       input_line_pointer += 5;
12010       *input_line_pointer = 0;
12011       return 1;
12012     }
12013
12014   return 0;
12015 }
12016
12017 char *
12018 arm_canonicalize_symbol_name (char * name)
12019 {
12020   int len;
12021
12022   if (thumb_mode && (len = strlen (name)) > 5
12023       && streq (name + len - 5, "/data"))
12024     *(name + len - 5) = 0;
12025
12026   return name;
12027 }
12028 \f
12029 /* Table of all register names defined by default.  The user can
12030    define additional names with .req.  Note that all register names
12031    should appear in both upper and lowercase variants.  Some registers
12032    also have mixed-case names.  */
12033
12034 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE }
12035 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
12036 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
12037 #define REGSET(p,t) \
12038   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
12039   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
12040   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
12041   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
12042 #define REGSETH(p,t) \
12043   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
12044   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
12045   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
12046   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
12047 #define REGSET2(p,t) \
12048   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
12049   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
12050   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
12051   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
12052
12053 static const struct reg_entry reg_names[] =
12054 {
12055   /* ARM integer registers.  */
12056   REGSET(r, RN), REGSET(R, RN),
12057
12058   /* ATPCS synonyms.  */
12059   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
12060   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
12061   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
12062
12063   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
12064   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
12065   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
12066
12067   /* Well-known aliases.  */
12068   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
12069   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
12070
12071   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
12072   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
12073
12074   /* Coprocessor numbers.  */
12075   REGSET(p, CP), REGSET(P, CP),
12076
12077   /* Coprocessor register numbers.  The "cr" variants are for backward
12078      compatibility.  */
12079   REGSET(c,  CN), REGSET(C, CN),
12080   REGSET(cr, CN), REGSET(CR, CN),
12081
12082   /* FPA registers.  */
12083   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
12084   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
12085
12086   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
12087   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
12088
12089   /* VFP SP registers.  */
12090   REGSET(s,VFS),  REGSET(S,VFS),
12091   REGSETH(s,VFS), REGSETH(S,VFS),
12092
12093   /* VFP DP Registers.  */
12094   REGSET(d,VFD),  REGSET(D,VFD),
12095   /* Extra Neon DP registers.  */
12096   REGSETH(d,VFD), REGSETH(D,VFD),
12097
12098   /* Neon QP registers.  */
12099   REGSET2(q,NQ),  REGSET2(Q,NQ),
12100
12101   /* VFP control registers.  */
12102   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
12103   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
12104
12105   /* Maverick DSP coprocessor registers.  */
12106   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
12107   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
12108
12109   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
12110   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
12111   REGDEF(dspsc,0,DSPSC),
12112
12113   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
12114   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
12115   REGDEF(DSPSC,0,DSPSC),
12116
12117   /* iWMMXt data registers - p0, c0-15.  */
12118   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
12119
12120   /* iWMMXt control registers - p1, c0-3.  */
12121   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
12122   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
12123   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
12124   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
12125
12126   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
12127   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
12128   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
12129   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
12130   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
12131
12132   /* XScale accumulator registers.  */
12133   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
12134 };
12135 #undef REGDEF
12136 #undef REGNUM
12137 #undef REGSET
12138
12139 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
12140    within psr_required_here.  */
12141 static const struct asm_psr psrs[] =
12142 {
12143   /* Backward compatibility notation.  Note that "all" is no longer
12144      truly all possible PSR bits.  */
12145   {"all",  PSR_c | PSR_f},
12146   {"flg",  PSR_f},
12147   {"ctl",  PSR_c},
12148
12149   /* Individual flags.  */
12150   {"f",    PSR_f},
12151   {"c",    PSR_c},
12152   {"x",    PSR_x},
12153   {"s",    PSR_s},
12154   /* Combinations of flags.  */
12155   {"fs",   PSR_f | PSR_s},
12156   {"fx",   PSR_f | PSR_x},
12157   {"fc",   PSR_f | PSR_c},
12158   {"sf",   PSR_s | PSR_f},
12159   {"sx",   PSR_s | PSR_x},
12160   {"sc",   PSR_s | PSR_c},
12161   {"xf",   PSR_x | PSR_f},
12162   {"xs",   PSR_x | PSR_s},
12163   {"xc",   PSR_x | PSR_c},
12164   {"cf",   PSR_c | PSR_f},
12165   {"cs",   PSR_c | PSR_s},
12166   {"cx",   PSR_c | PSR_x},
12167   {"fsx",  PSR_f | PSR_s | PSR_x},
12168   {"fsc",  PSR_f | PSR_s | PSR_c},
12169   {"fxs",  PSR_f | PSR_x | PSR_s},
12170   {"fxc",  PSR_f | PSR_x | PSR_c},
12171   {"fcs",  PSR_f | PSR_c | PSR_s},
12172   {"fcx",  PSR_f | PSR_c | PSR_x},
12173   {"sfx",  PSR_s | PSR_f | PSR_x},
12174   {"sfc",  PSR_s | PSR_f | PSR_c},
12175   {"sxf",  PSR_s | PSR_x | PSR_f},
12176   {"sxc",  PSR_s | PSR_x | PSR_c},
12177   {"scf",  PSR_s | PSR_c | PSR_f},
12178   {"scx",  PSR_s | PSR_c | PSR_x},
12179   {"xfs",  PSR_x | PSR_f | PSR_s},
12180   {"xfc",  PSR_x | PSR_f | PSR_c},
12181   {"xsf",  PSR_x | PSR_s | PSR_f},
12182   {"xsc",  PSR_x | PSR_s | PSR_c},
12183   {"xcf",  PSR_x | PSR_c | PSR_f},
12184   {"xcs",  PSR_x | PSR_c | PSR_s},
12185   {"cfs",  PSR_c | PSR_f | PSR_s},
12186   {"cfx",  PSR_c | PSR_f | PSR_x},
12187   {"csf",  PSR_c | PSR_s | PSR_f},
12188   {"csx",  PSR_c | PSR_s | PSR_x},
12189   {"cxf",  PSR_c | PSR_x | PSR_f},
12190   {"cxs",  PSR_c | PSR_x | PSR_s},
12191   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
12192   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
12193   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
12194   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
12195   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
12196   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
12197   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
12198   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
12199   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
12200   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
12201   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
12202   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
12203   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
12204   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
12205   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
12206   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
12207   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
12208   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
12209   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
12210   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
12211   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
12212   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
12213   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
12214   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
12215 };
12216
12217 /* Table of V7M psr names.  */
12218 static const struct asm_psr v7m_psrs[] =
12219 {
12220   {"apsr",      0 },
12221   {"iapsr",     1 },
12222   {"eapsr",     2 },
12223   {"psr",       3 },
12224   {"ipsr",      5 },
12225   {"epsr",      6 },
12226   {"iepsr",     7 },
12227   {"msp",       8 },
12228   {"psp",       9 },
12229   {"primask",   16},
12230   {"basepri",   17},
12231   {"basepri_max", 18},
12232   {"faultmask", 19},
12233   {"control",   20}
12234 };
12235
12236 /* Table of all shift-in-operand names.  */
12237 static const struct asm_shift_name shift_names [] =
12238 {
12239   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
12240   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
12241   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
12242   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
12243   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
12244   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
12245 };
12246
12247 /* Table of all explicit relocation names.  */
12248 #ifdef OBJ_ELF
12249 static struct reloc_entry reloc_names[] =
12250 {
12251   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
12252   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
12253   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
12254   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
12255   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
12256   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
12257   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
12258   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
12259   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
12260   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
12261   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32}
12262 };
12263 #endif
12264
12265 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
12266 static const struct asm_cond conds[] =
12267 {
12268   {"eq", 0x0},
12269   {"ne", 0x1},
12270   {"cs", 0x2}, {"hs", 0x2},
12271   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
12272   {"mi", 0x4},
12273   {"pl", 0x5},
12274   {"vs", 0x6},
12275   {"vc", 0x7},
12276   {"hi", 0x8},
12277   {"ls", 0x9},
12278   {"ge", 0xa},
12279   {"lt", 0xb},
12280   {"gt", 0xc},
12281   {"le", 0xd},
12282   {"al", 0xe}
12283 };
12284
12285 static struct asm_barrier_opt barrier_opt_names[] =
12286 {
12287   { "sy",   0xf },
12288   { "un",   0x7 },
12289   { "st",   0xe },
12290   { "unst", 0x6 }
12291 };
12292
12293 /* Table of ARM-format instructions.    */
12294
12295 /* Macros for gluing together operand strings.  N.B. In all cases
12296    other than OPS0, the trailing OP_stop comes from default
12297    zero-initialization of the unspecified elements of the array.  */
12298 #define OPS0()            { OP_stop, }
12299 #define OPS1(a)           { OP_##a, }
12300 #define OPS2(a,b)         { OP_##a,OP_##b, }
12301 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
12302 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
12303 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
12304 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
12305
12306 /* These macros abstract out the exact format of the mnemonic table and
12307    save some repeated characters.  */
12308
12309 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
12310 #define TxCE(mnem, op, top, nops, ops, ae, te) \
12311   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
12312     THUMB_VARIANT, do_##ae, do_##te }
12313
12314 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
12315    a T_MNEM_xyz enumerator.  */
12316 #define TCE(mnem, aop, top, nops, ops, ae, te) \
12317        TxCE(mnem, aop, 0x##top, nops, ops, ae, te)
12318 #define tCE(mnem, aop, top, nops, ops, ae, te) \
12319        TxCE(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
12320
12321 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
12322    infix after the third character.  */
12323 #define TxC3(mnem, op, top, nops, ops, ae, te) \
12324   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
12325     THUMB_VARIANT, do_##ae, do_##te }
12326 #define TC3(mnem, aop, top, nops, ops, ae, te) \
12327        TxC3(mnem, aop, 0x##top, nops, ops, ae, te)
12328 #define tC3(mnem, aop, top, nops, ops, ae, te) \
12329        TxC3(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
12330
12331 /* Mnemonic with a conditional infix in an unusual place.  Each and every variant has to
12332    appear in the condition table.  */
12333 #define TxCM_(m1, m2, m3, op, top, nops, ops, ae, te)   \
12334   { #m1 #m2 #m3, OPS##nops ops, sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
12335     0x##op, top, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##te }
12336
12337 #define TxCM(m1, m2, op, top, nops, ops, ae, te)        \
12338   TxCM_(m1,   , m2, op, top, nops, ops, ae, te),        \
12339   TxCM_(m1, eq, m2, op, top, nops, ops, ae, te),        \
12340   TxCM_(m1, ne, m2, op, top, nops, ops, ae, te),        \
12341   TxCM_(m1, cs, m2, op, top, nops, ops, ae, te),        \
12342   TxCM_(m1, hs, m2, op, top, nops, ops, ae, te),        \
12343   TxCM_(m1, cc, m2, op, top, nops, ops, ae, te),        \
12344   TxCM_(m1, ul, m2, op, top, nops, ops, ae, te),        \
12345   TxCM_(m1, lo, m2, op, top, nops, ops, ae, te),        \
12346   TxCM_(m1, mi, m2, op, top, nops, ops, ae, te),        \
12347   TxCM_(m1, pl, m2, op, top, nops, ops, ae, te),        \
12348   TxCM_(m1, vs, m2, op, top, nops, ops, ae, te),        \
12349   TxCM_(m1, vc, m2, op, top, nops, ops, ae, te),        \
12350   TxCM_(m1, hi, m2, op, top, nops, ops, ae, te),        \
12351   TxCM_(m1, ls, m2, op, top, nops, ops, ae, te),        \
12352   TxCM_(m1, ge, m2, op, top, nops, ops, ae, te),        \
12353   TxCM_(m1, lt, m2, op, top, nops, ops, ae, te),        \
12354   TxCM_(m1, gt, m2, op, top, nops, ops, ae, te),        \
12355   TxCM_(m1, le, m2, op, top, nops, ops, ae, te),        \
12356   TxCM_(m1, al, m2, op, top, nops, ops, ae, te)
12357
12358 #define TCM(m1,m2, aop, top, nops, ops, ae, te)         \
12359        TxCM(m1,m2, aop, 0x##top, nops, ops, ae, te)
12360 #define tCM(m1,m2, aop, top, nops, ops, ae, te)                 \
12361        TxCM(m1,m2, aop, T_MNEM_##top, nops, ops, ae, te)
12362
12363 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
12364    field is still 0xE.  Many of the Thumb variants can be executed
12365    conditionally, so this is checked separately.  */
12366 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
12367   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
12368     THUMB_VARIANT, do_##ae, do_##te }
12369
12370 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
12371    condition code field.  */
12372 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
12373   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
12374     THUMB_VARIANT, do_##ae, do_##te }
12375
12376 /* ARM-only variants of all the above.  */
12377 #define CE(mnem,  op, nops, ops, ae)    \
12378   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
12379
12380 #define C3(mnem, op, nops, ops, ae)     \
12381   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
12382
12383 /* Legacy mnemonics that always have conditional infix after the third
12384    character.  */
12385 #define CL(mnem, op, nops, ops, ae)     \
12386   { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
12387     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
12388
12389 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
12390 #define cCE(mnem,  op, nops, ops, ae)   \
12391   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
12392
12393 /* Legacy coprocessor instructions where conditional infix and conditional
12394    suffix are ambiguous.  For consistency this includes all FPA instructions,
12395    not just the potentially ambiguous ones.  */
12396 #define cCL(mnem, op, nops, ops, ae)    \
12397   { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
12398     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
12399
12400 /* Coprocessor, takes either a suffix or a position-3 infix
12401    (for an FPA corner case). */
12402 #define C3E(mnem, op, nops, ops, ae) \
12403   { #mnem, OPS##nops ops, OT_csuf_or_in3, \
12404     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
12405
12406 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
12407   { #m1 #m2 #m3, OPS##nops ops, \
12408     sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
12409     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
12410
12411 #define CM(m1, m2, op, nops, ops, ae)   \
12412   xCM_(m1,   , m2, op, nops, ops, ae),  \
12413   xCM_(m1, eq, m2, op, nops, ops, ae),  \
12414   xCM_(m1, ne, m2, op, nops, ops, ae),  \
12415   xCM_(m1, cs, m2, op, nops, ops, ae),  \
12416   xCM_(m1, hs, m2, op, nops, ops, ae),  \
12417   xCM_(m1, cc, m2, op, nops, ops, ae),  \
12418   xCM_(m1, ul, m2, op, nops, ops, ae),  \
12419   xCM_(m1, lo, m2, op, nops, ops, ae),  \
12420   xCM_(m1, mi, m2, op, nops, ops, ae),  \
12421   xCM_(m1, pl, m2, op, nops, ops, ae),  \
12422   xCM_(m1, vs, m2, op, nops, ops, ae),  \
12423   xCM_(m1, vc, m2, op, nops, ops, ae),  \
12424   xCM_(m1, hi, m2, op, nops, ops, ae),  \
12425   xCM_(m1, ls, m2, op, nops, ops, ae),  \
12426   xCM_(m1, ge, m2, op, nops, ops, ae),  \
12427   xCM_(m1, lt, m2, op, nops, ops, ae),  \
12428   xCM_(m1, gt, m2, op, nops, ops, ae),  \
12429   xCM_(m1, le, m2, op, nops, ops, ae),  \
12430   xCM_(m1, al, m2, op, nops, ops, ae)
12431
12432 #define UE(mnem, op, nops, ops, ae)     \
12433   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
12434
12435 #define UF(mnem, op, nops, ops, ae)     \
12436   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
12437
12438 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
12439    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
12440    use the same encoding function for each.  */
12441 #define NUF(mnem, op, nops, ops, enc)                                   \
12442   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
12443     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
12444
12445 /* Neon data processing, version which indirects through neon_enc_tab for
12446    the various overloaded versions of opcodes.  */
12447 #define nUF(mnem, op, nops, ops, enc)                                   \
12448   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM_##op, N_MNEM_##op,  \
12449     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
12450
12451 /* Neon insn with conditional suffix for the ARM version, non-overloaded
12452    version.  */
12453 #define NCE(mnem, op, nops, ops, enc)                                   \
12454   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x##op, ARM_VARIANT,      \
12455     THUMB_VARIANT, do_##enc, do_##enc }
12456
12457 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
12458 #define nCE(mnem, op, nops, ops, enc)                                   \
12459   { #mnem, OPS##nops ops, OT_csuffix, N_MNEM_##op, N_MNEM_##op,         \
12460     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
12461
12462 #define do_0 0
12463
12464 /* Thumb-only, unconditional.  */
12465 #define UT(mnem,  op, nops, ops, te) TUE(mnem,  0, op, nops, ops, 0, te)
12466
12467 static const struct asm_opcode insns[] =
12468 {
12469 #define ARM_VARIANT &arm_ext_v1 /* Core ARM Instructions.  */
12470 #define THUMB_VARIANT &arm_ext_v4t
12471  tCE(and,       0000000, and,      3, (RR, oRR, SH), arit, t_arit3c),
12472  tC3(ands,      0100000, ands,     3, (RR, oRR, SH), arit, t_arit3c),
12473  tCE(eor,       0200000, eor,      3, (RR, oRR, SH), arit, t_arit3c),
12474  tC3(eors,      0300000, eors,     3, (RR, oRR, SH), arit, t_arit3c),
12475  tCE(sub,       0400000, sub,      3, (RR, oRR, SH), arit, t_add_sub),
12476  tC3(subs,      0500000, subs,     3, (RR, oRR, SH), arit, t_add_sub),
12477  tCE(add,       0800000, add,      3, (RR, oRR, SH), arit, t_add_sub),
12478  tC3(adds,      0900000, adds,     3, (RR, oRR, SH), arit, t_add_sub),
12479  tCE(adc,       0a00000, adc,      3, (RR, oRR, SH), arit, t_arit3c),
12480  tC3(adcs,      0b00000, adcs,     3, (RR, oRR, SH), arit, t_arit3c),
12481  tCE(sbc,       0c00000, sbc,      3, (RR, oRR, SH), arit, t_arit3),
12482  tC3(sbcs,      0d00000, sbcs,     3, (RR, oRR, SH), arit, t_arit3),
12483  tCE(orr,       1800000, orr,      3, (RR, oRR, SH), arit, t_arit3c),
12484  tC3(orrs,      1900000, orrs,     3, (RR, oRR, SH), arit, t_arit3c),
12485  tCE(bic,       1c00000, bic,      3, (RR, oRR, SH), arit, t_arit3),
12486  tC3(bics,      1d00000, bics,     3, (RR, oRR, SH), arit, t_arit3),
12487
12488  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
12489     for setting PSR flag bits.  They are obsolete in V6 and do not
12490     have Thumb equivalents. */
12491  tCE(tst,       1100000, tst,      2, (RR, SH),      cmp,  t_mvn_tst),
12492  tC3(tsts,      1100000, tst,      2, (RR, SH),      cmp,  t_mvn_tst),
12493   CL(tstp,      110f000,           2, (RR, SH),      cmp),
12494  tCE(cmp,       1500000, cmp,      2, (RR, SH),      cmp,  t_mov_cmp),
12495  tC3(cmps,      1500000, cmp,      2, (RR, SH),      cmp,  t_mov_cmp),
12496   CL(cmpp,      150f000,           2, (RR, SH),      cmp),
12497  tCE(cmn,       1700000, cmn,      2, (RR, SH),      cmp,  t_mvn_tst),
12498  tC3(cmns,      1700000, cmn,      2, (RR, SH),      cmp,  t_mvn_tst),
12499   CL(cmnp,      170f000,           2, (RR, SH),      cmp),
12500
12501  tCE(mov,       1a00000, mov,      2, (RR, SH),      mov,  t_mov_cmp),
12502  tC3(movs,      1b00000, movs,     2, (RR, SH),      mov,  t_mov_cmp),
12503  tCE(mvn,       1e00000, mvn,      2, (RR, SH),      mov,  t_mvn_tst),
12504  tC3(mvns,      1f00000, mvns,     2, (RR, SH),      mov,  t_mvn_tst),
12505
12506  tCE(ldr,       4100000, ldr,      2, (RR, ADDR),    ldst, t_ldst),
12507  tC3(ldrb,      4500000, ldrb,     2, (RR, ADDR),    ldst, t_ldst),
12508  tCE(str,       4000000, str,      2, (RR, ADDR),    ldst, t_ldst),
12509  tC3(strb,      4400000, strb,     2, (RR, ADDR),    ldst, t_ldst),
12510
12511  tCE(stm,       8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
12512  tC3(stmia,     8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
12513  tC3(stmea,     8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
12514  tCE(ldm,       8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
12515  tC3(ldmia,     8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
12516  tC3(ldmfd,     8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
12517
12518  TCE(swi,       f000000, df00,     1, (EXPi),        swi, t_swi),
12519  TCE(svc,       f000000, df00,     1, (EXPi),        swi, t_swi),
12520  tCE(b,         a000000, b,        1, (EXPr),        branch, t_branch),
12521  TCE(bl,        b000000, f000f800, 1, (EXPr),        bl, t_branch23),
12522
12523   /* Pseudo ops.  */
12524  tCE(adr,       28f0000, adr,      2, (RR, EXP),     adr,  t_adr),
12525   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
12526  tCE(nop,       1a00000, nop,      1, (oI255c),      nop,  t_nop),
12527
12528   /* Thumb-compatibility pseudo ops.  */
12529  tCE(lsl,       1a00000, lsl,      3, (RR, oRR, SH), shift, t_shift),
12530  tC3(lsls,      1b00000, lsls,     3, (RR, oRR, SH), shift, t_shift),
12531  tCE(lsr,       1a00020, lsr,      3, (RR, oRR, SH), shift, t_shift),
12532  tC3(lsrs,      1b00020, lsrs,     3, (RR, oRR, SH), shift, t_shift),
12533  tCE(asr,       1a00040, asr,      3, (RR, oRR, SH), shift, t_shift),
12534  tC3(asrs,      1b00040, asrs,     3, (RR, oRR, SH), shift, t_shift),
12535  tCE(ror,       1a00060, ror,      3, (RR, oRR, SH), shift, t_shift),
12536  tC3(rors,      1b00060, rors,     3, (RR, oRR, SH), shift, t_shift),
12537  tCE(neg,       2600000, neg,      2, (RR, RR),      rd_rn, t_neg),
12538  tC3(negs,      2700000, negs,     2, (RR, RR),      rd_rn, t_neg),
12539  tCE(push,      92d0000, push,     1, (REGLST),      push_pop, t_push_pop),
12540  tCE(pop,       8bd0000, pop,      1, (REGLST),      push_pop, t_push_pop),
12541
12542 #undef THUMB_VARIANT
12543 #define THUMB_VARIANT &arm_ext_v6
12544  TCE(cpy,       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
12545
12546  /* V1 instructions with no Thumb analogue prior to V6T2.  */
12547 #undef THUMB_VARIANT
12548 #define THUMB_VARIANT &arm_ext_v6t2
12549  TCE(rsb,       0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
12550  TC3(rsbs,      0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
12551  TCE(teq,       1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
12552  TC3(teqs,      1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
12553   CL(teqp,      130f000,           2, (RR, SH),      cmp),
12554
12555  TC3(ldrt,      4300000, f8500e00, 2, (RR, ADDR),    ldstt, t_ldstt),
12556  TC3(ldrbt,     4700000, f8100e00, 2, (RR, ADDR),    ldstt, t_ldstt),
12557  TC3(strt,      4200000, f8400e00, 2, (RR, ADDR),    ldstt, t_ldstt),
12558  TC3(strbt,     4600000, f8000e00, 2, (RR, ADDR),    ldstt, t_ldstt),
12559
12560  TC3(stmdb,     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
12561  TC3(stmfd,     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
12562
12563  TC3(ldmdb,     9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
12564  TC3(ldmea,     9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
12565
12566  /* V1 instructions with no Thumb analogue at all.  */
12567   CE(rsc,       0e00000,           3, (RR, oRR, SH), arit),
12568   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
12569
12570   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
12571   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
12572   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
12573   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
12574   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
12575   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
12576   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
12577   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
12578
12579 #undef ARM_VARIANT
12580 #define ARM_VARIANT &arm_ext_v2 /* ARM 2 - multiplies.  */
12581 #undef THUMB_VARIANT
12582 #define THUMB_VARIANT &arm_ext_v4t
12583  tCE(mul,       0000090, mul,      3, (RRnpc, RRnpc, oRR), mul, t_mul),
12584  tC3(muls,      0100090, muls,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
12585
12586 #undef THUMB_VARIANT
12587 #define THUMB_VARIANT &arm_ext_v6t2
12588  TCE(mla,       0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
12589   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
12590
12591   /* Generic coprocessor instructions.  */
12592  TCE(cdp,       e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
12593  TCE(ldc,       c100000, ec100000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
12594  TC3(ldcl,      c500000, ec500000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
12595  TCE(stc,       c000000, ec000000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
12596  TC3(stcl,      c400000, ec400000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
12597  TCE(mcr,       e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
12598  TCE(mrc,       e100010, ee100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
12599
12600 #undef ARM_VARIANT
12601 #define ARM_VARIANT &arm_ext_v2s /* ARM 3 - swp instructions.  */
12602   CE(swp,       1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
12603   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
12604
12605 #undef ARM_VARIANT
12606 #define ARM_VARIANT &arm_ext_v3 /* ARM 6 Status register instructions.  */
12607  TCE(mrs,       10f0000, f3ef8000, 2, (RR, PSR),     mrs, t_mrs),
12608  TCE(msr,       120f000, f3808000, 2, (PSR, RR_EXi), msr, t_msr),
12609
12610 #undef ARM_VARIANT
12611 #define ARM_VARIANT &arm_ext_v3m         /* ARM 7M long multiplies.  */
12612  TCE(smull,     0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
12613   CM(smull,s,   0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
12614  TCE(umull,     0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
12615   CM(umull,s,   0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
12616  TCE(smlal,     0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
12617   CM(smlal,s,   0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
12618  TCE(umlal,     0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
12619   CM(umlal,s,   0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
12620
12621 #undef ARM_VARIANT
12622 #define ARM_VARIANT &arm_ext_v4 /* ARM Architecture 4.  */
12623 #undef THUMB_VARIANT
12624 #define THUMB_VARIANT &arm_ext_v4t
12625  tC3(ldrh,      01000b0, ldrh,     2, (RR, ADDR), ldstv4, t_ldst),
12626  tC3(strh,      00000b0, strh,     2, (RR, ADDR), ldstv4, t_ldst),
12627  tC3(ldrsh,     01000f0, ldrsh,    2, (RR, ADDR), ldstv4, t_ldst),
12628  tC3(ldrsb,     01000d0, ldrsb,    2, (RR, ADDR), ldstv4, t_ldst),
12629  tCM(ld,sh,     01000f0, ldrsh,    2, (RR, ADDR), ldstv4, t_ldst),
12630  tCM(ld,sb,     01000d0, ldrsb,    2, (RR, ADDR), ldstv4, t_ldst),
12631
12632 #undef ARM_VARIANT
12633 #define ARM_VARIANT &arm_ext_v4t_5
12634   /* ARM Architecture 4T.  */
12635   /* Note: bx (and blx) are required on V5, even if the processor does
12636      not support Thumb.  */
12637  TCE(bx,        12fff10, 4700, 1, (RR), bx, t_bx),
12638
12639 #undef ARM_VARIANT
12640 #define ARM_VARIANT &arm_ext_v5 /*  ARM Architecture 5T.         */
12641 #undef THUMB_VARIANT
12642 #define THUMB_VARIANT &arm_ext_v5t
12643   /* Note: blx has 2 variants; the .value coded here is for
12644      BLX(2).  Only this variant has conditional execution.  */
12645  TCE(blx,       12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
12646  TUE(bkpt,      1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
12647
12648 #undef THUMB_VARIANT
12649 #define THUMB_VARIANT &arm_ext_v6t2
12650  TCE(clz,       16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
12651  TUF(ldc2,      c100000, fc100000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
12652  TUF(ldc2l,     c500000, fc500000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
12653  TUF(stc2,      c000000, fc000000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
12654  TUF(stc2l,     c400000, fc400000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
12655  TUF(cdp2,      e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
12656  TUF(mcr2,      e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
12657  TUF(mrc2,      e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
12658
12659 #undef ARM_VARIANT
12660 #define ARM_VARIANT &arm_ext_v5exp /*  ARM Architecture 5TExP.  */
12661  TCE(smlabb,    1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
12662  TCE(smlatb,    10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
12663  TCE(smlabt,    10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
12664  TCE(smlatt,    10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
12665
12666  TCE(smlawb,    1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
12667  TCE(smlawt,    12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
12668
12669  TCE(smlalbb,   1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
12670  TCE(smlaltb,   14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
12671  TCE(smlalbt,   14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
12672  TCE(smlaltt,   14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
12673
12674  TCE(smulbb,    1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
12675  TCE(smultb,    16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
12676  TCE(smulbt,    16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
12677  TCE(smultt,    16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
12678
12679  TCE(smulwb,    12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
12680  TCE(smulwt,    12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
12681
12682  TCE(qadd,      1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
12683  TCE(qdadd,     1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
12684  TCE(qsub,      1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
12685  TCE(qdsub,     1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
12686
12687 #undef ARM_VARIANT
12688 #define ARM_VARIANT &arm_ext_v5e /*  ARM Architecture 5TE.  */
12689  TUF(pld,       450f000, f810f000, 1, (ADDR),                pld,  t_pld),
12690  TC3(ldrd,      00000d0, e9500000, 3, (RRnpc, oRRnpc, ADDR), ldrd, t_ldstd),
12691  TC3(strd,      00000f0, e9400000, 3, (RRnpc, oRRnpc, ADDR), ldrd, t_ldstd),
12692
12693  TCE(mcrr,      c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
12694  TCE(mrrc,      c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
12695
12696 #undef ARM_VARIANT
12697 #define ARM_VARIANT &arm_ext_v5j /*  ARM Architecture 5TEJ.  */
12698  TCE(bxj,       12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
12699
12700 #undef ARM_VARIANT
12701 #define ARM_VARIANT &arm_ext_v6 /*  ARM V6.  */
12702 #undef THUMB_VARIANT
12703 #define THUMB_VARIANT &arm_ext_v6
12704  TUF(cpsie,     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
12705  TUF(cpsid,     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
12706  tCE(rev,       6bf0f30, rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
12707  tCE(rev16,     6bf0fb0, rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
12708  tCE(revsh,     6ff0fb0, revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
12709  tCE(sxth,      6bf0070, sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
12710  tCE(uxth,      6ff0070, uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
12711  tCE(sxtb,      6af0070, sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
12712  tCE(uxtb,      6ef0070, uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
12713  TUF(setend,    1010000, b650,     1, (ENDI),                     setend, t_setend),
12714
12715 #undef THUMB_VARIANT
12716 #define THUMB_VARIANT &arm_ext_v6t2
12717  TCE(ldrex,     1900f9f, e8500f00, 2, (RRnpc, ADDR),              ldrex, t_ldrex),
12718  TUF(mcrr2,     c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
12719  TUF(mrrc2,     c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
12720
12721  TCE(ssat,      6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
12722  TCE(usat,      6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
12723
12724 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
12725 #undef THUMB_VARIANT
12726 #define THUMB_VARIANT &arm_ext_v6_notm
12727  TUF(cps,       1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
12728  TCE(pkhbt,     6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
12729  TCE(pkhtb,     6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
12730  TCE(qadd16,    6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12731  TCE(qadd8,     6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12732  TCE(qaddsubx,  6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12733  TCE(qsub16,    6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12734  TCE(qsub8,     6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12735  TCE(qsubaddx,  6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12736  TCE(sadd16,    6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12737  TCE(sadd8,     6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12738  TCE(saddsubx,  6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12739  TCE(shadd16,   6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12740  TCE(shadd8,    6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12741  TCE(shaddsubx, 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12742  TCE(shsub16,   6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12743  TCE(shsub8,    6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12744  TCE(shsubaddx, 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12745  TCE(ssub16,    6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12746  TCE(ssub8,     6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12747  TCE(ssubaddx,  6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12748  TCE(uadd16,    6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12749  TCE(uadd8,     6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12750  TCE(uaddsubx,  6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12751  TCE(uhadd16,   6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12752  TCE(uhadd8,    6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12753  TCE(uhaddsubx, 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12754  TCE(uhsub16,   6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12755  TCE(uhsub8,    6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12756  TCE(uhsubaddx, 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12757  TCE(uqadd16,   6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12758  TCE(uqadd8,    6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12759  TCE(uqaddsubx, 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12760  TCE(uqsub16,   6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12761  TCE(uqsub8,    6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12762  TCE(uqsubaddx, 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12763  TCE(usub16,    6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12764  TCE(usub8,     6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12765  TCE(usubaddx,  6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12766  TUF(rfeia,     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
12767   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
12768   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
12769  TUF(rfedb,     9100a00, e810c000, 1, (RRw),                       rfe, rfe),
12770  TUF(rfefd,     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
12771   UF(rfefa,     9900a00,           1, (RRw),                       rfe),
12772   UF(rfeea,     8100a00,           1, (RRw),                       rfe),
12773  TUF(rfeed,     9100a00, e810c000, 1, (RRw),                       rfe, rfe),
12774  TCE(sxtah,     6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
12775  TCE(sxtab16,   6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
12776  TCE(sxtab,     6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
12777  TCE(sxtb16,    68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
12778  TCE(uxtah,     6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
12779  TCE(uxtab16,   6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
12780  TCE(uxtab,     6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
12781  TCE(uxtb16,    6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
12782  TCE(sel,       6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
12783  TCE(smlad,     7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
12784  TCE(smladx,    7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
12785  TCE(smlald,    7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
12786  TCE(smlaldx,   7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
12787  TCE(smlsd,     7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
12788  TCE(smlsdx,    7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
12789  TCE(smlsld,    7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
12790  TCE(smlsldx,   7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
12791  TCE(smmla,     7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
12792  TCE(smmlar,    7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
12793  TCE(smmls,     75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
12794  TCE(smmlsr,    75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
12795  TCE(smmul,     750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
12796  TCE(smmulr,    750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
12797  TCE(smuad,     700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
12798  TCE(smuadx,    700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
12799  TCE(smusd,     700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
12800  TCE(smusdx,    700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
12801  TUF(srsia,     8cd0500, e980c000, 1, (I31w),                      srs,  srs),
12802   UF(srsib,     9cd0500,           1, (I31w),                      srs),
12803   UF(srsda,     84d0500,           1, (I31w),                      srs),
12804  TUF(srsdb,     94d0500, e800c000, 1, (I31w),                      srs,  srs),
12805  TCE(ssat16,    6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
12806  TCE(strex,     1800f90, e8400000, 3, (RRnpc, RRnpc, ADDR),        strex,  t_strex),
12807  TCE(umaal,     0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
12808  TCE(usad8,     780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
12809  TCE(usada8,    7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
12810  TCE(usat16,    6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
12811
12812 #undef ARM_VARIANT
12813 #define ARM_VARIANT &arm_ext_v6k
12814 #undef THUMB_VARIANT
12815 #define THUMB_VARIANT &arm_ext_v6k
12816  tCE(yield,     320f001, yield,    0, (), noargs, t_hint),
12817  tCE(wfe,       320f002, wfe,      0, (), noargs, t_hint),
12818  tCE(wfi,       320f003, wfi,      0, (), noargs, t_hint),
12819  tCE(sev,       320f004, sev,      0, (), noargs, t_hint),
12820
12821 #undef THUMB_VARIANT
12822 #define THUMB_VARIANT &arm_ext_v6_notm
12823  TCE(ldrexd,    1b00f9f, e8d0007f, 3, (RRnpc, oRRnpc, RRnpcb),        ldrexd, t_ldrexd),
12824  TCE(strexd,    1a00f90, e8c00070, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb), strexd, t_strexd),
12825
12826 #undef THUMB_VARIANT
12827 #define THUMB_VARIANT &arm_ext_v6t2
12828  TCE(ldrexb,    1d00f9f, e8d00f4f, 2, (RRnpc, RRnpcb),                rd_rn,  rd_rn),
12829  TCE(ldrexh,    1f00f9f, e8d00f5f, 2, (RRnpc, RRnpcb),                rd_rn,  rd_rn),
12830  TCE(strexb,    1c00f90, e8c00f40, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
12831  TCE(strexh,    1e00f90, e8c00f50, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
12832  TUF(clrex,     57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
12833
12834 #undef ARM_VARIANT
12835 #define ARM_VARIANT &arm_ext_v6z
12836  TCE(smc,       1600070, f7f08000, 1, (EXPi), smc, t_smc),
12837
12838 #undef ARM_VARIANT
12839 #define ARM_VARIANT &arm_ext_v6t2
12840  TCE(bfc,       7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
12841  TCE(bfi,       7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
12842  TCE(sbfx,      7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
12843  TCE(ubfx,      7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
12844
12845  TCE(mls,       0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
12846  TCE(movw,      3000000, f2400000, 2, (RRnpc, Iffff),               mov16, t_mov16),
12847  TCE(movt,      3400000, f2c00000, 2, (RRnpc, Iffff),               mov16, t_mov16),
12848  TCE(rbit,      3ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
12849
12850  TC3(ldrht,     03000b0, f8300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
12851  TC3(ldrsht,    03000f0, f9300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
12852  TC3(ldrsbt,    03000d0, f9100e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
12853  TC3(strht,     02000b0, f8200e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
12854
12855   UT(cbnz,      b900,    2, (RR, EXP), t_czb),
12856   UT(cbz,       b100,    2, (RR, EXP), t_czb),
12857  /* ARM does not really have an IT instruction.  */
12858  TUE(it,        0, bf08, 1, (COND),    it, t_it),
12859  TUE(itt,       0, bf0c, 1, (COND),    it, t_it),
12860  TUE(ite,       0, bf04, 1, (COND),    it, t_it),
12861  TUE(ittt,      0, bf0e, 1, (COND),    it, t_it),
12862  TUE(itet,      0, bf06, 1, (COND),    it, t_it),
12863  TUE(itte,      0, bf0a, 1, (COND),    it, t_it),
12864  TUE(itee,      0, bf02, 1, (COND),    it, t_it),
12865  TUE(itttt,     0, bf0f, 1, (COND),    it, t_it),
12866  TUE(itett,     0, bf07, 1, (COND),    it, t_it),
12867  TUE(ittet,     0, bf0b, 1, (COND),    it, t_it),
12868  TUE(iteet,     0, bf03, 1, (COND),    it, t_it),
12869  TUE(ittte,     0, bf0d, 1, (COND),    it, t_it),
12870  TUE(itete,     0, bf05, 1, (COND),    it, t_it),
12871  TUE(ittee,     0, bf09, 1, (COND),    it, t_it),
12872  TUE(iteee,     0, bf01, 1, (COND),    it, t_it),
12873
12874  /* Thumb2 only instructions.  */
12875 #undef ARM_VARIANT
12876 #define ARM_VARIANT NULL
12877
12878  TCE(addw,      0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
12879  TCE(subw,      0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
12880  TCE(tbb,       0, e8d0f000, 1, (TB), 0, t_tb),
12881  TCE(tbh,       0, e8d0f010, 1, (TB), 0, t_tb),
12882
12883  /* Thumb-2 hardware division instructions (R and M profiles only).  */
12884 #undef THUMB_VARIANT
12885 #define THUMB_VARIANT &arm_ext_div
12886  TCE(sdiv,      0, fb90f0f0, 3, (RR, oRR, RR), 0, t_div),
12887  TCE(udiv,      0, fbb0f0f0, 3, (RR, oRR, RR), 0, t_div),
12888
12889  /* ARM V7 instructions.  */
12890 #undef ARM_VARIANT
12891 #define ARM_VARIANT &arm_ext_v7
12892 #undef THUMB_VARIANT
12893 #define THUMB_VARIANT &arm_ext_v7
12894  TUF(pli,       450f000, f910f000, 1, (ADDR),     pli,      t_pld),
12895  TCE(dbg,       320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
12896  TUF(dmb,       57ff050, f3bf8f50, 1, (oBARRIER), barrier,  t_barrier),
12897  TUF(dsb,       57ff040, f3bf8f40, 1, (oBARRIER), barrier,  t_barrier),
12898  TUF(isb,       57ff060, f3bf8f60, 1, (oBARRIER), barrier,  t_barrier),
12899
12900 #undef ARM_VARIANT
12901 #define ARM_VARIANT &fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
12902  cCE(wfs,       e200110, 1, (RR),            rd),
12903  cCE(rfs,       e300110, 1, (RR),            rd),
12904  cCE(wfc,       e400110, 1, (RR),            rd),
12905  cCE(rfc,       e500110, 1, (RR),            rd),
12906
12907  cCL(ldfs,      c100100, 2, (RF, ADDR),      rd_cpaddr),
12908  cCL(ldfd,      c108100, 2, (RF, ADDR),      rd_cpaddr),
12909  cCL(ldfe,      c500100, 2, (RF, ADDR),      rd_cpaddr),
12910  cCL(ldfp,      c508100, 2, (RF, ADDR),      rd_cpaddr),
12911
12912  cCL(stfs,      c000100, 2, (RF, ADDR),      rd_cpaddr),
12913  cCL(stfd,      c008100, 2, (RF, ADDR),      rd_cpaddr),
12914  cCL(stfe,      c400100, 2, (RF, ADDR),      rd_cpaddr),
12915  cCL(stfp,      c408100, 2, (RF, ADDR),      rd_cpaddr),
12916
12917  cCL(mvfs,      e008100, 2, (RF, RF_IF),     rd_rm),
12918  cCL(mvfsp,     e008120, 2, (RF, RF_IF),     rd_rm),
12919  cCL(mvfsm,     e008140, 2, (RF, RF_IF),     rd_rm),
12920  cCL(mvfsz,     e008160, 2, (RF, RF_IF),     rd_rm),
12921  cCL(mvfd,      e008180, 2, (RF, RF_IF),     rd_rm),
12922  cCL(mvfdp,     e0081a0, 2, (RF, RF_IF),     rd_rm),
12923  cCL(mvfdm,     e0081c0, 2, (RF, RF_IF),     rd_rm),
12924  cCL(mvfdz,     e0081e0, 2, (RF, RF_IF),     rd_rm),
12925  cCL(mvfe,      e088100, 2, (RF, RF_IF),     rd_rm),
12926  cCL(mvfep,     e088120, 2, (RF, RF_IF),     rd_rm),
12927  cCL(mvfem,     e088140, 2, (RF, RF_IF),     rd_rm),
12928  cCL(mvfez,     e088160, 2, (RF, RF_IF),     rd_rm),
12929
12930  cCL(mnfs,      e108100, 2, (RF, RF_IF),     rd_rm),
12931  cCL(mnfsp,     e108120, 2, (RF, RF_IF),     rd_rm),
12932  cCL(mnfsm,     e108140, 2, (RF, RF_IF),     rd_rm),
12933  cCL(mnfsz,     e108160, 2, (RF, RF_IF),     rd_rm),
12934  cCL(mnfd,      e108180, 2, (RF, RF_IF),     rd_rm),
12935  cCL(mnfdp,     e1081a0, 2, (RF, RF_IF),     rd_rm),
12936  cCL(mnfdm,     e1081c0, 2, (RF, RF_IF),     rd_rm),
12937  cCL(mnfdz,     e1081e0, 2, (RF, RF_IF),     rd_rm),
12938  cCL(mnfe,      e188100, 2, (RF, RF_IF),     rd_rm),
12939  cCL(mnfep,     e188120, 2, (RF, RF_IF),     rd_rm),
12940  cCL(mnfem,     e188140, 2, (RF, RF_IF),     rd_rm),
12941  cCL(mnfez,     e188160, 2, (RF, RF_IF),     rd_rm),
12942
12943  cCL(abss,      e208100, 2, (RF, RF_IF),     rd_rm),
12944  cCL(abssp,     e208120, 2, (RF, RF_IF),     rd_rm),
12945  cCL(abssm,     e208140, 2, (RF, RF_IF),     rd_rm),
12946  cCL(abssz,     e208160, 2, (RF, RF_IF),     rd_rm),
12947  cCL(absd,      e208180, 2, (RF, RF_IF),     rd_rm),
12948  cCL(absdp,     e2081a0, 2, (RF, RF_IF),     rd_rm),
12949  cCL(absdm,     e2081c0, 2, (RF, RF_IF),     rd_rm),
12950  cCL(absdz,     e2081e0, 2, (RF, RF_IF),     rd_rm),
12951  cCL(abse,      e288100, 2, (RF, RF_IF),     rd_rm),
12952  cCL(absep,     e288120, 2, (RF, RF_IF),     rd_rm),
12953  cCL(absem,     e288140, 2, (RF, RF_IF),     rd_rm),
12954  cCL(absez,     e288160, 2, (RF, RF_IF),     rd_rm),
12955
12956  cCL(rnds,      e308100, 2, (RF, RF_IF),     rd_rm),
12957  cCL(rndsp,     e308120, 2, (RF, RF_IF),     rd_rm),
12958  cCL(rndsm,     e308140, 2, (RF, RF_IF),     rd_rm),
12959  cCL(rndsz,     e308160, 2, (RF, RF_IF),     rd_rm),
12960  cCL(rndd,      e308180, 2, (RF, RF_IF),     rd_rm),
12961  cCL(rnddp,     e3081a0, 2, (RF, RF_IF),     rd_rm),
12962  cCL(rnddm,     e3081c0, 2, (RF, RF_IF),     rd_rm),
12963  cCL(rnddz,     e3081e0, 2, (RF, RF_IF),     rd_rm),
12964  cCL(rnde,      e388100, 2, (RF, RF_IF),     rd_rm),
12965  cCL(rndep,     e388120, 2, (RF, RF_IF),     rd_rm),
12966  cCL(rndem,     e388140, 2, (RF, RF_IF),     rd_rm),
12967  cCL(rndez,     e388160, 2, (RF, RF_IF),     rd_rm),
12968
12969  cCL(sqts,      e408100, 2, (RF, RF_IF),     rd_rm),
12970  cCL(sqtsp,     e408120, 2, (RF, RF_IF),     rd_rm),
12971  cCL(sqtsm,     e408140, 2, (RF, RF_IF),     rd_rm),
12972  cCL(sqtsz,     e408160, 2, (RF, RF_IF),     rd_rm),
12973  cCL(sqtd,      e408180, 2, (RF, RF_IF),     rd_rm),
12974  cCL(sqtdp,     e4081a0, 2, (RF, RF_IF),     rd_rm),
12975  cCL(sqtdm,     e4081c0, 2, (RF, RF_IF),     rd_rm),
12976  cCL(sqtdz,     e4081e0, 2, (RF, RF_IF),     rd_rm),
12977  cCL(sqte,      e488100, 2, (RF, RF_IF),     rd_rm),
12978  cCL(sqtep,     e488120, 2, (RF, RF_IF),     rd_rm),
12979  cCL(sqtem,     e488140, 2, (RF, RF_IF),     rd_rm),
12980  cCL(sqtez,     e488160, 2, (RF, RF_IF),     rd_rm),
12981
12982  cCL(logs,      e508100, 2, (RF, RF_IF),     rd_rm),
12983  cCL(logsp,     e508120, 2, (RF, RF_IF),     rd_rm),
12984  cCL(logsm,     e508140, 2, (RF, RF_IF),     rd_rm),
12985  cCL(logsz,     e508160, 2, (RF, RF_IF),     rd_rm),
12986  cCL(logd,      e508180, 2, (RF, RF_IF),     rd_rm),
12987  cCL(logdp,     e5081a0, 2, (RF, RF_IF),     rd_rm),
12988  cCL(logdm,     e5081c0, 2, (RF, RF_IF),     rd_rm),
12989  cCL(logdz,     e5081e0, 2, (RF, RF_IF),     rd_rm),
12990  cCL(loge,      e588100, 2, (RF, RF_IF),     rd_rm),
12991  cCL(logep,     e588120, 2, (RF, RF_IF),     rd_rm),
12992  cCL(logem,     e588140, 2, (RF, RF_IF),     rd_rm),
12993  cCL(logez,     e588160, 2, (RF, RF_IF),     rd_rm),
12994
12995  cCL(lgns,      e608100, 2, (RF, RF_IF),     rd_rm),
12996  cCL(lgnsp,     e608120, 2, (RF, RF_IF),     rd_rm),
12997  cCL(lgnsm,     e608140, 2, (RF, RF_IF),     rd_rm),
12998  cCL(lgnsz,     e608160, 2, (RF, RF_IF),     rd_rm),
12999  cCL(lgnd,      e608180, 2, (RF, RF_IF),     rd_rm),
13000  cCL(lgndp,     e6081a0, 2, (RF, RF_IF),     rd_rm),
13001  cCL(lgndm,     e6081c0, 2, (RF, RF_IF),     rd_rm),
13002  cCL(lgndz,     e6081e0, 2, (RF, RF_IF),     rd_rm),
13003  cCL(lgne,      e688100, 2, (RF, RF_IF),     rd_rm),
13004  cCL(lgnep,     e688120, 2, (RF, RF_IF),     rd_rm),
13005  cCL(lgnem,     e688140, 2, (RF, RF_IF),     rd_rm),
13006  cCL(lgnez,     e688160, 2, (RF, RF_IF),     rd_rm),
13007
13008  cCL(exps,      e708100, 2, (RF, RF_IF),     rd_rm),
13009  cCL(expsp,     e708120, 2, (RF, RF_IF),     rd_rm),
13010  cCL(expsm,     e708140, 2, (RF, RF_IF),     rd_rm),
13011  cCL(expsz,     e708160, 2, (RF, RF_IF),     rd_rm),
13012  cCL(expd,      e708180, 2, (RF, RF_IF),     rd_rm),
13013  cCL(expdp,     e7081a0, 2, (RF, RF_IF),     rd_rm),
13014  cCL(expdm,     e7081c0, 2, (RF, RF_IF),     rd_rm),
13015  cCL(expdz,     e7081e0, 2, (RF, RF_IF),     rd_rm),
13016  cCL(expe,      e788100, 2, (RF, RF_IF),     rd_rm),
13017  cCL(expep,     e788120, 2, (RF, RF_IF),     rd_rm),
13018  cCL(expem,     e788140, 2, (RF, RF_IF),     rd_rm),
13019  cCL(expdz,     e788160, 2, (RF, RF_IF),     rd_rm),
13020
13021  cCL(sins,      e808100, 2, (RF, RF_IF),     rd_rm),
13022  cCL(sinsp,     e808120, 2, (RF, RF_IF),     rd_rm),
13023  cCL(sinsm,     e808140, 2, (RF, RF_IF),     rd_rm),
13024  cCL(sinsz,     e808160, 2, (RF, RF_IF),     rd_rm),
13025  cCL(sind,      e808180, 2, (RF, RF_IF),     rd_rm),
13026  cCL(sindp,     e8081a0, 2, (RF, RF_IF),     rd_rm),
13027  cCL(sindm,     e8081c0, 2, (RF, RF_IF),     rd_rm),
13028  cCL(sindz,     e8081e0, 2, (RF, RF_IF),     rd_rm),
13029  cCL(sine,      e888100, 2, (RF, RF_IF),     rd_rm),
13030  cCL(sinep,     e888120, 2, (RF, RF_IF),     rd_rm),
13031  cCL(sinem,     e888140, 2, (RF, RF_IF),     rd_rm),
13032  cCL(sinez,     e888160, 2, (RF, RF_IF),     rd_rm),
13033
13034  cCL(coss,      e908100, 2, (RF, RF_IF),     rd_rm),
13035  cCL(cossp,     e908120, 2, (RF, RF_IF),     rd_rm),
13036  cCL(cossm,     e908140, 2, (RF, RF_IF),     rd_rm),
13037  cCL(cossz,     e908160, 2, (RF, RF_IF),     rd_rm),
13038  cCL(cosd,      e908180, 2, (RF, RF_IF),     rd_rm),
13039  cCL(cosdp,     e9081a0, 2, (RF, RF_IF),     rd_rm),
13040  cCL(cosdm,     e9081c0, 2, (RF, RF_IF),     rd_rm),
13041  cCL(cosdz,     e9081e0, 2, (RF, RF_IF),     rd_rm),
13042  cCL(cose,      e988100, 2, (RF, RF_IF),     rd_rm),
13043  cCL(cosep,     e988120, 2, (RF, RF_IF),     rd_rm),
13044  cCL(cosem,     e988140, 2, (RF, RF_IF),     rd_rm),
13045  cCL(cosez,     e988160, 2, (RF, RF_IF),     rd_rm),
13046
13047  cCL(tans,      ea08100, 2, (RF, RF_IF),     rd_rm),
13048  cCL(tansp,     ea08120, 2, (RF, RF_IF),     rd_rm),
13049  cCL(tansm,     ea08140, 2, (RF, RF_IF),     rd_rm),
13050  cCL(tansz,     ea08160, 2, (RF, RF_IF),     rd_rm),
13051  cCL(tand,      ea08180, 2, (RF, RF_IF),     rd_rm),
13052  cCL(tandp,     ea081a0, 2, (RF, RF_IF),     rd_rm),
13053  cCL(tandm,     ea081c0, 2, (RF, RF_IF),     rd_rm),
13054  cCL(tandz,     ea081e0, 2, (RF, RF_IF),     rd_rm),
13055  cCL(tane,      ea88100, 2, (RF, RF_IF),     rd_rm),
13056  cCL(tanep,     ea88120, 2, (RF, RF_IF),     rd_rm),
13057  cCL(tanem,     ea88140, 2, (RF, RF_IF),     rd_rm),
13058  cCL(tanez,     ea88160, 2, (RF, RF_IF),     rd_rm),
13059
13060  cCL(asns,      eb08100, 2, (RF, RF_IF),     rd_rm),
13061  cCL(asnsp,     eb08120, 2, (RF, RF_IF),     rd_rm),
13062  cCL(asnsm,     eb08140, 2, (RF, RF_IF),     rd_rm),
13063  cCL(asnsz,     eb08160, 2, (RF, RF_IF),     rd_rm),
13064  cCL(asnd,      eb08180, 2, (RF, RF_IF),     rd_rm),
13065  cCL(asndp,     eb081a0, 2, (RF, RF_IF),     rd_rm),
13066  cCL(asndm,     eb081c0, 2, (RF, RF_IF),     rd_rm),
13067  cCL(asndz,     eb081e0, 2, (RF, RF_IF),     rd_rm),
13068  cCL(asne,      eb88100, 2, (RF, RF_IF),     rd_rm),
13069  cCL(asnep,     eb88120, 2, (RF, RF_IF),     rd_rm),
13070  cCL(asnem,     eb88140, 2, (RF, RF_IF),     rd_rm),
13071  cCL(asnez,     eb88160, 2, (RF, RF_IF),     rd_rm),
13072
13073  cCL(acss,      ec08100, 2, (RF, RF_IF),     rd_rm),
13074  cCL(acssp,     ec08120, 2, (RF, RF_IF),     rd_rm),
13075  cCL(acssm,     ec08140, 2, (RF, RF_IF),     rd_rm),
13076  cCL(acssz,     ec08160, 2, (RF, RF_IF),     rd_rm),
13077  cCL(acsd,      ec08180, 2, (RF, RF_IF),     rd_rm),
13078  cCL(acsdp,     ec081a0, 2, (RF, RF_IF),     rd_rm),
13079  cCL(acsdm,     ec081c0, 2, (RF, RF_IF),     rd_rm),
13080  cCL(acsdz,     ec081e0, 2, (RF, RF_IF),     rd_rm),
13081  cCL(acse,      ec88100, 2, (RF, RF_IF),     rd_rm),
13082  cCL(acsep,     ec88120, 2, (RF, RF_IF),     rd_rm),
13083  cCL(acsem,     ec88140, 2, (RF, RF_IF),     rd_rm),
13084  cCL(acsez,     ec88160, 2, (RF, RF_IF),     rd_rm),
13085
13086  cCL(atns,      ed08100, 2, (RF, RF_IF),     rd_rm),
13087  cCL(atnsp,     ed08120, 2, (RF, RF_IF),     rd_rm),
13088  cCL(atnsm,     ed08140, 2, (RF, RF_IF),     rd_rm),
13089  cCL(atnsz,     ed08160, 2, (RF, RF_IF),     rd_rm),
13090  cCL(atnd,      ed08180, 2, (RF, RF_IF),     rd_rm),
13091  cCL(atndp,     ed081a0, 2, (RF, RF_IF),     rd_rm),
13092  cCL(atndm,     ed081c0, 2, (RF, RF_IF),     rd_rm),
13093  cCL(atndz,     ed081e0, 2, (RF, RF_IF),     rd_rm),
13094  cCL(atne,      ed88100, 2, (RF, RF_IF),     rd_rm),
13095  cCL(atnep,     ed88120, 2, (RF, RF_IF),     rd_rm),
13096  cCL(atnem,     ed88140, 2, (RF, RF_IF),     rd_rm),
13097  cCL(atnez,     ed88160, 2, (RF, RF_IF),     rd_rm),
13098
13099  cCL(urds,      ee08100, 2, (RF, RF_IF),     rd_rm),
13100  cCL(urdsp,     ee08120, 2, (RF, RF_IF),     rd_rm),
13101  cCL(urdsm,     ee08140, 2, (RF, RF_IF),     rd_rm),
13102  cCL(urdsz,     ee08160, 2, (RF, RF_IF),     rd_rm),
13103  cCL(urdd,      ee08180, 2, (RF, RF_IF),     rd_rm),
13104  cCL(urddp,     ee081a0, 2, (RF, RF_IF),     rd_rm),
13105  cCL(urddm,     ee081c0, 2, (RF, RF_IF),     rd_rm),
13106  cCL(urddz,     ee081e0, 2, (RF, RF_IF),     rd_rm),
13107  cCL(urde,      ee88100, 2, (RF, RF_IF),     rd_rm),
13108  cCL(urdep,     ee88120, 2, (RF, RF_IF),     rd_rm),
13109  cCL(urdem,     ee88140, 2, (RF, RF_IF),     rd_rm),
13110  cCL(urdez,     ee88160, 2, (RF, RF_IF),     rd_rm),
13111
13112  cCL(nrms,      ef08100, 2, (RF, RF_IF),     rd_rm),
13113  cCL(nrmsp,     ef08120, 2, (RF, RF_IF),     rd_rm),
13114  cCL(nrmsm,     ef08140, 2, (RF, RF_IF),     rd_rm),
13115  cCL(nrmsz,     ef08160, 2, (RF, RF_IF),     rd_rm),
13116  cCL(nrmd,      ef08180, 2, (RF, RF_IF),     rd_rm),
13117  cCL(nrmdp,     ef081a0, 2, (RF, RF_IF),     rd_rm),
13118  cCL(nrmdm,     ef081c0, 2, (RF, RF_IF),     rd_rm),
13119  cCL(nrmdz,     ef081e0, 2, (RF, RF_IF),     rd_rm),
13120  cCL(nrme,      ef88100, 2, (RF, RF_IF),     rd_rm),
13121  cCL(nrmep,     ef88120, 2, (RF, RF_IF),     rd_rm),
13122  cCL(nrmem,     ef88140, 2, (RF, RF_IF),     rd_rm),
13123  cCL(nrmez,     ef88160, 2, (RF, RF_IF),     rd_rm),
13124
13125  cCL(adfs,      e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
13126  cCL(adfsp,     e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
13127  cCL(adfsm,     e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
13128  cCL(adfsz,     e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
13129  cCL(adfd,      e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
13130  cCL(adfdp,     e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13131  cCL(adfdm,     e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13132  cCL(adfdz,     e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13133  cCL(adfe,      e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
13134  cCL(adfep,     e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
13135  cCL(adfem,     e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
13136  cCL(adfez,     e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
13137
13138  cCL(sufs,      e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
13139  cCL(sufsp,     e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
13140  cCL(sufsm,     e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
13141  cCL(sufsz,     e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
13142  cCL(sufd,      e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
13143  cCL(sufdp,     e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13144  cCL(sufdm,     e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13145  cCL(sufdz,     e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13146  cCL(sufe,      e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
13147  cCL(sufep,     e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
13148  cCL(sufem,     e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
13149  cCL(sufez,     e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
13150
13151  cCL(rsfs,      e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
13152  cCL(rsfsp,     e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
13153  cCL(rsfsm,     e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
13154  cCL(rsfsz,     e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
13155  cCL(rsfd,      e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
13156  cCL(rsfdp,     e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13157  cCL(rsfdm,     e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13158  cCL(rsfdz,     e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13159  cCL(rsfe,      e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
13160  cCL(rsfep,     e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
13161  cCL(rsfem,     e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
13162  cCL(rsfez,     e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
13163
13164  cCL(mufs,      e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
13165  cCL(mufsp,     e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
13166  cCL(mufsm,     e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
13167  cCL(mufsz,     e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
13168  cCL(mufd,      e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
13169  cCL(mufdp,     e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13170  cCL(mufdm,     e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13171  cCL(mufdz,     e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13172  cCL(mufe,      e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
13173  cCL(mufep,     e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
13174  cCL(mufem,     e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
13175  cCL(mufez,     e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
13176
13177  cCL(dvfs,      e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
13178  cCL(dvfsp,     e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
13179  cCL(dvfsm,     e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
13180  cCL(dvfsz,     e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
13181  cCL(dvfd,      e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
13182  cCL(dvfdp,     e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13183  cCL(dvfdm,     e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13184  cCL(dvfdz,     e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13185  cCL(dvfe,      e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
13186  cCL(dvfep,     e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
13187  cCL(dvfem,     e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
13188  cCL(dvfez,     e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
13189
13190  cCL(rdfs,      e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
13191  cCL(rdfsp,     e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
13192  cCL(rdfsm,     e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
13193  cCL(rdfsz,     e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
13194  cCL(rdfd,      e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
13195  cCL(rdfdp,     e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13196  cCL(rdfdm,     e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13197  cCL(rdfdz,     e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13198  cCL(rdfe,      e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
13199  cCL(rdfep,     e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
13200  cCL(rdfem,     e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
13201  cCL(rdfez,     e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
13202
13203  cCL(pows,      e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
13204  cCL(powsp,     e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
13205  cCL(powsm,     e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
13206  cCL(powsz,     e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
13207  cCL(powd,      e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
13208  cCL(powdp,     e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13209  cCL(powdm,     e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13210  cCL(powdz,     e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13211  cCL(powe,      e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
13212  cCL(powep,     e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
13213  cCL(powem,     e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
13214  cCL(powez,     e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
13215
13216  cCL(rpws,      e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
13217  cCL(rpwsp,     e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
13218  cCL(rpwsm,     e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
13219  cCL(rpwsz,     e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
13220  cCL(rpwd,      e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
13221  cCL(rpwdp,     e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13222  cCL(rpwdm,     e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13223  cCL(rpwdz,     e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13224  cCL(rpwe,      e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
13225  cCL(rpwep,     e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
13226  cCL(rpwem,     e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
13227  cCL(rpwez,     e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
13228
13229  cCL(rmfs,      e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
13230  cCL(rmfsp,     e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
13231  cCL(rmfsm,     e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
13232  cCL(rmfsz,     e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
13233  cCL(rmfd,      e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
13234  cCL(rmfdp,     e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13235  cCL(rmfdm,     e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13236  cCL(rmfdz,     e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13237  cCL(rmfe,      e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
13238  cCL(rmfep,     e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
13239  cCL(rmfem,     e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
13240  cCL(rmfez,     e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
13241
13242  cCL(fmls,      e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
13243  cCL(fmlsp,     e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
13244  cCL(fmlsm,     e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
13245  cCL(fmlsz,     e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
13246  cCL(fmld,      e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
13247  cCL(fmldp,     e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13248  cCL(fmldm,     e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13249  cCL(fmldz,     e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13250  cCL(fmle,      e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
13251  cCL(fmlep,     e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
13252  cCL(fmlem,     e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
13253  cCL(fmlez,     e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
13254
13255  cCL(fdvs,      ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
13256  cCL(fdvsp,     ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
13257  cCL(fdvsm,     ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
13258  cCL(fdvsz,     ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
13259  cCL(fdvd,      ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
13260  cCL(fdvdp,     ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13261  cCL(fdvdm,     ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13262  cCL(fdvdz,     ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13263  cCL(fdve,      ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
13264  cCL(fdvep,     ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
13265  cCL(fdvem,     ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
13266  cCL(fdvez,     ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
13267
13268  cCL(frds,      eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
13269  cCL(frdsp,     eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
13270  cCL(frdsm,     eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
13271  cCL(frdsz,     eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
13272  cCL(frdd,      eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
13273  cCL(frddp,     eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13274  cCL(frddm,     eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13275  cCL(frddz,     eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13276  cCL(frde,      eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
13277  cCL(frdep,     eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
13278  cCL(frdem,     eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
13279  cCL(frdez,     eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
13280
13281  cCL(pols,      ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
13282  cCL(polsp,     ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
13283  cCL(polsm,     ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
13284  cCL(polsz,     ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
13285  cCL(pold,      ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
13286  cCL(poldp,     ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
13287  cCL(poldm,     ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
13288  cCL(poldz,     ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
13289  cCL(pole,      ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
13290  cCL(polep,     ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
13291  cCL(polem,     ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
13292  cCL(polez,     ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
13293
13294  cCE(cmf,       e90f110, 2, (RF, RF_IF),     fpa_cmp),
13295  C3E(cmfe,      ed0f110, 2, (RF, RF_IF),     fpa_cmp),
13296  cCE(cnf,       eb0f110, 2, (RF, RF_IF),     fpa_cmp),
13297  C3E(cnfe,      ef0f110, 2, (RF, RF_IF),     fpa_cmp),
13298
13299  cCL(flts,      e000110, 2, (RF, RR),        rn_rd),
13300  cCL(fltsp,     e000130, 2, (RF, RR),        rn_rd),
13301  cCL(fltsm,     e000150, 2, (RF, RR),        rn_rd),
13302  cCL(fltsz,     e000170, 2, (RF, RR),        rn_rd),
13303  cCL(fltd,      e000190, 2, (RF, RR),        rn_rd),
13304  cCL(fltdp,     e0001b0, 2, (RF, RR),        rn_rd),
13305  cCL(fltdm,     e0001d0, 2, (RF, RR),        rn_rd),
13306  cCL(fltdz,     e0001f0, 2, (RF, RR),        rn_rd),
13307  cCL(flte,      e080110, 2, (RF, RR),        rn_rd),
13308  cCL(fltep,     e080130, 2, (RF, RR),        rn_rd),
13309  cCL(fltem,     e080150, 2, (RF, RR),        rn_rd),
13310  cCL(fltez,     e080170, 2, (RF, RR),        rn_rd),
13311
13312   /* The implementation of the FIX instruction is broken on some
13313      assemblers, in that it accepts a precision specifier as well as a
13314      rounding specifier, despite the fact that this is meaningless.
13315      To be more compatible, we accept it as well, though of course it
13316      does not set any bits.  */
13317  cCE(fix,       e100110, 2, (RR, RF),        rd_rm),
13318  cCL(fixp,      e100130, 2, (RR, RF),        rd_rm),
13319  cCL(fixm,      e100150, 2, (RR, RF),        rd_rm),
13320  cCL(fixz,      e100170, 2, (RR, RF),        rd_rm),
13321  cCL(fixsp,     e100130, 2, (RR, RF),        rd_rm),
13322  cCL(fixsm,     e100150, 2, (RR, RF),        rd_rm),
13323  cCL(fixsz,     e100170, 2, (RR, RF),        rd_rm),
13324  cCL(fixdp,     e100130, 2, (RR, RF),        rd_rm),
13325  cCL(fixdm,     e100150, 2, (RR, RF),        rd_rm),
13326  cCL(fixdz,     e100170, 2, (RR, RF),        rd_rm),
13327  cCL(fixep,     e100130, 2, (RR, RF),        rd_rm),
13328  cCL(fixem,     e100150, 2, (RR, RF),        rd_rm),
13329  cCL(fixez,     e100170, 2, (RR, RF),        rd_rm),
13330
13331   /* Instructions that were new with the real FPA, call them V2.  */
13332 #undef ARM_VARIANT
13333 #define ARM_VARIANT &fpu_fpa_ext_v2
13334  cCE(lfm,       c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
13335  cCL(lfmfd,     c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
13336  cCL(lfmea,     d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
13337  cCE(sfm,       c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
13338  cCL(sfmfd,     d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
13339  cCL(sfmea,     c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
13340
13341 #undef ARM_VARIANT
13342 #define ARM_VARIANT &fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
13343   /* Moves and type conversions.  */
13344  cCE(fcpys,     eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
13345  cCE(fmrs,      e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
13346  cCE(fmsr,      e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
13347  cCE(fmstat,    ef1fa10, 0, (),               noargs),
13348  cCE(fsitos,    eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
13349  cCE(fuitos,    eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
13350  cCE(ftosis,    ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
13351  cCE(ftosizs,   ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
13352  cCE(ftouis,    ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
13353  cCE(ftouizs,   ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
13354  cCE(fmrx,      ef00a10, 2, (RR, RVC),        rd_rn),
13355  cCE(fmxr,      ee00a10, 2, (RVC, RR),        rn_rd),
13356
13357   /* Memory operations.  */
13358  cCE(flds,      d100a00, 2, (RVS, ADDR),      vfp_sp_ldst),
13359  cCE(fsts,      d000a00, 2, (RVS, ADDR),      vfp_sp_ldst),
13360  cCE(fldmias,   c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
13361  cCE(fldmfds,   c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
13362  cCE(fldmdbs,   d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
13363  cCE(fldmeas,   d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
13364  cCE(fldmiax,   c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
13365  cCE(fldmfdx,   c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
13366  cCE(fldmdbx,   d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
13367  cCE(fldmeax,   d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
13368  cCE(fstmias,   c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
13369  cCE(fstmeas,   c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
13370  cCE(fstmdbs,   d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
13371  cCE(fstmfds,   d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
13372  cCE(fstmiax,   c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
13373  cCE(fstmeax,   c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
13374  cCE(fstmdbx,   d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
13375  cCE(fstmfdx,   d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
13376
13377   /* Monadic operations.  */
13378  cCE(fabss,     eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
13379  cCE(fnegs,     eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
13380  cCE(fsqrts,    eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
13381
13382   /* Dyadic operations.  */
13383  cCE(fadds,     e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
13384  cCE(fsubs,     e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
13385  cCE(fmuls,     e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
13386  cCE(fdivs,     e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
13387  cCE(fmacs,     e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
13388  cCE(fmscs,     e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
13389  cCE(fnmuls,    e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
13390  cCE(fnmacs,    e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
13391  cCE(fnmscs,    e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
13392
13393   /* Comparisons.  */
13394  cCE(fcmps,     eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
13395  cCE(fcmpzs,    eb50a40, 1, (RVS),            vfp_sp_compare_z),
13396  cCE(fcmpes,    eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
13397  cCE(fcmpezs,   eb50ac0, 1, (RVS),            vfp_sp_compare_z),
13398
13399 #undef ARM_VARIANT
13400 #define ARM_VARIANT &fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
13401   /* Moves and type conversions.  */
13402  cCE(fcpyd,     eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
13403  cCE(fcvtds,    eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
13404  cCE(fcvtsd,    eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
13405  cCE(fmdhr,     e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
13406  cCE(fmdlr,     e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
13407  cCE(fmrdh,     e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
13408  cCE(fmrdl,     e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
13409  cCE(fsitod,    eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
13410  cCE(fuitod,    eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
13411  cCE(ftosid,    ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
13412  cCE(ftosizd,   ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
13413  cCE(ftouid,    ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
13414  cCE(ftouizd,   ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
13415
13416   /* Memory operations.  */
13417  cCE(fldd,      d100b00, 2, (RVD, ADDR),      vfp_dp_ldst),
13418  cCE(fstd,      d000b00, 2, (RVD, ADDR),      vfp_dp_ldst),
13419  cCE(fldmiad,   c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
13420  cCE(fldmfdd,   c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
13421  cCE(fldmdbd,   d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
13422  cCE(fldmead,   d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
13423  cCE(fstmiad,   c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
13424  cCE(fstmead,   c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
13425  cCE(fstmdbd,   d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
13426  cCE(fstmfdd,   d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
13427
13428   /* Monadic operations.  */
13429  cCE(fabsd,     eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
13430  cCE(fnegd,     eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
13431  cCE(fsqrtd,    eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
13432
13433   /* Dyadic operations.  */
13434  cCE(faddd,     e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
13435  cCE(fsubd,     e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
13436  cCE(fmuld,     e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
13437  cCE(fdivd,     e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
13438  cCE(fmacd,     e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
13439  cCE(fmscd,     e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
13440  cCE(fnmuld,    e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
13441  cCE(fnmacd,    e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
13442  cCE(fnmscd,    e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
13443
13444   /* Comparisons.  */
13445  cCE(fcmpd,     eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
13446  cCE(fcmpzd,    eb50b40, 1, (RVD),            vfp_dp_rd),
13447  cCE(fcmped,    eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
13448  cCE(fcmpezd,   eb50bc0, 1, (RVD),            vfp_dp_rd),
13449
13450 #undef ARM_VARIANT
13451 #define ARM_VARIANT &fpu_vfp_ext_v2
13452  cCE(fmsrr,     c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
13453  cCE(fmrrs,     c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
13454  cCE(fmdrr,     c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
13455  cCE(fmrrd,     c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
13456
13457 #undef THUMB_VARIANT
13458 #define THUMB_VARIANT &fpu_neon_ext_v1
13459 #undef ARM_VARIANT
13460 #define ARM_VARIANT &fpu_neon_ext_v1
13461   /* Data processing with three registers of the same length.  */
13462   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
13463  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
13464  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
13465  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
13466  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
13467  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
13468  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
13469  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
13470  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
13471   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
13472  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
13473  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
13474  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
13475  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
13476  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
13477  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
13478  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
13479  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
13480   /* If not immediate, fall back to neon_dyadic_i64_su.
13481      shl_imm should accept I8 I16 I32 I64,
13482      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
13483  nUF(vshl,      vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
13484  nUF(vshlq,     vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
13485  nUF(vqshl,     vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
13486  nUF(vqshlq,    vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
13487   /* Logic ops, types optional & ignored.  */
13488  nUF(vand,      vand,    2, (RNDQ, NILO),        neon_logic),
13489  nUF(vandq,     vand,    2, (RNQ,  NILO),        neon_logic),
13490  nUF(vbic,      vbic,    2, (RNDQ, NILO),        neon_logic),
13491  nUF(vbicq,     vbic,    2, (RNQ,  NILO),        neon_logic),
13492  nUF(vorr,      vorr,    2, (RNDQ, NILO),        neon_logic),
13493  nUF(vorrq,     vorr,    2, (RNQ,  NILO),        neon_logic),
13494  nUF(vorn,      vorn,    2, (RNDQ, NILO),        neon_logic),
13495  nUF(vornq,     vorn,    2, (RNQ,  NILO),        neon_logic),
13496  nUF(veor,      veor,    3, (RNDQ, oRNDQ, RNDQ), neon_logic),
13497  nUF(veorq,     veor,    3, (RNQ,  oRNQ,  RNQ),  neon_logic),
13498   /* Bitfield ops, untyped.  */
13499  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
13500  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
13501  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
13502  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
13503  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
13504  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
13505   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F32.  */
13506  nUF(vabd,      vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
13507  nUF(vabdq,     vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
13508  nUF(vmax,      vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
13509  nUF(vmaxq,     vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
13510  nUF(vmin,      vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
13511  nUF(vminq,     vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
13512   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
13513      back to neon_dyadic_if_su.  */
13514  nUF(vcge,      vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
13515  nUF(vcgeq,     vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
13516  nUF(vcgt,      vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
13517  nUF(vcgtq,     vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
13518  nUF(vclt,      vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
13519  nUF(vcltq,     vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
13520  nUF(vcle,      vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
13521  nUF(vcleq,     vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
13522   /* Comparison. Type I8 I16 I32 F32. Non-immediate -> neon_dyadic_if_i.  */
13523  nUF(vceq,      vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
13524  nUF(vceqq,     vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
13525   /* As above, D registers only.  */
13526  nUF(vpmax,     vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
13527  nUF(vpmin,     vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
13528   /* Int and float variants, signedness unimportant.  */
13529   /* If not scalar, fall back to neon_dyadic_if_i.  */
13530  nUF(vmla,      vmla,    3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_mac_maybe_scalar),
13531  nUF(vmlaq,     vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
13532  nUF(vmls,      vmls,    3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_mac_maybe_scalar),
13533  nUF(vmlsq,     vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
13534  nUF(vpadd,     vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
13535   /* Add/sub take types I8 I16 I32 I64 F32.  */
13536  nUF(vadd,      vadd,    3, (RNDQ, oRNDQ, RNDQ), neon_addsub_if_i),
13537  nUF(vaddq,     vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
13538  nUF(vsub,      vsub,    3, (RNDQ, oRNDQ, RNDQ), neon_addsub_if_i),
13539  nUF(vsubq,     vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
13540   /* vtst takes sizes 8, 16, 32.  */
13541  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
13542  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
13543   /* VMUL takes I8 I16 I32 F32 P8.  */
13544  nUF(vmul,      vmul,    3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_mul),
13545  nUF(vmulq,     vmul,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
13546   /* VQD{R}MULH takes S16 S32.  */
13547  nUF(vqdmulh,   vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
13548  nUF(vqdmulhq,  vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
13549  nUF(vqrdmulh,  vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
13550  nUF(vqrdmulhq, vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
13551  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
13552  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
13553  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
13554  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
13555  NUF(vaclt,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
13556  NUF(vacltq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
13557  NUF(vacle,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
13558  NUF(vacleq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
13559  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
13560  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
13561  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
13562  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
13563
13564   /* Two address, int/float. Types S8 S16 S32 F32.  */
13565  NUF(vabs,      1b10300, 2, (RNDQ, RNDQ),     neon_abs_neg),
13566  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
13567  NUF(vneg,      1b10380, 2, (RNDQ, RNDQ),     neon_abs_neg),
13568  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
13569
13570   /* Data processing with two registers and a shift amount.  */
13571   /* Right shifts, and variants with rounding.
13572      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
13573  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
13574  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
13575  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
13576  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
13577  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
13578  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
13579  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
13580  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
13581   /* Shift and insert. Sizes accepted 8 16 32 64.  */
13582  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
13583  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
13584  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
13585  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
13586   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
13587  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
13588  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
13589   /* Right shift immediate, saturating & narrowing, with rounding variants.
13590      Types accepted S16 S32 S64 U16 U32 U64.  */
13591  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
13592  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
13593   /* As above, unsigned. Types accepted S16 S32 S64.  */
13594  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
13595  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
13596   /* Right shift narrowing. Types accepted I16 I32 I64.  */
13597  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
13598  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
13599   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
13600  nUF(vshll,     vshll,   3, (RNQ, RND, I32),  neon_shll),
13601   /* CVT with optional immediate for fixed-point variant.  */
13602  nUF(vcvt,      vcvt,    3, (RNDQ, RNDQ, oI32b), neon_cvt),
13603  nUF(vcvtq,     vcvt,    3, (RNQ,  RNQ,  oI32b), neon_cvt),
13604
13605   /* One register and an immediate value. All encoding special-cased!  */
13606  NCE(vmov,      0,       1, (VMOV),             neon_mov),
13607  NCE(vmovq,     0,       1, (VMOV),             neon_mov),
13608  nUF(vmvn,      vmvn,    2, (RNDQ, RNDQ_IMVNb), neon_mvn),
13609  nUF(vmvnq,     vmvn,    2, (RNQ,  RNDQ_IMVNb), neon_mvn),
13610
13611   /* Data processing, three registers of different lengths.  */
13612   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
13613  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
13614  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
13615  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
13616  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
13617   /* If not scalar, fall back to neon_dyadic_long.
13618      Vector types as above, scalar types S16 S32 U16 U32.  */
13619  nUF(vmlal,     vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
13620  nUF(vmlsl,     vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
13621   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
13622  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
13623  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
13624   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
13625  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
13626  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
13627  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
13628  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
13629   /* Saturating doubling multiplies. Types S16 S32.  */
13630  nUF(vqdmlal,   vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
13631  nUF(vqdmlsl,   vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
13632  nUF(vqdmull,   vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
13633   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
13634      S16 S32 U16 U32.  */
13635  nUF(vmull,     vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
13636
13637   /* Extract. Size 8.  */
13638  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I7), neon_ext),
13639  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I7), neon_ext),
13640
13641   /* Two registers, miscellaneous.  */
13642   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
13643  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
13644  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
13645  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
13646  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
13647  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
13648  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
13649   /* Vector replicate. Sizes 8 16 32.  */
13650  nCE(vdup,      vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
13651  nCE(vdupq,     vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
13652   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
13653  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
13654   /* VMOVN. Types I16 I32 I64.  */
13655  nUF(vmovn,     vmovn,   2, (RND, RNQ),       neon_movn),
13656   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
13657  nUF(vqmovn,    vqmovn,  2, (RND, RNQ),       neon_qmovn),
13658   /* VQMOVUN. Types S16 S32 S64.  */
13659  nUF(vqmovun,   vqmovun, 2, (RND, RNQ),       neon_qmovun),
13660   /* VZIP / VUZP. Sizes 8 16 32.  */
13661  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
13662  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
13663  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
13664  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
13665   /* VQABS / VQNEG. Types S8 S16 S32.  */
13666  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
13667  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
13668  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
13669  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
13670   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
13671  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
13672  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
13673  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
13674  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
13675   /* Reciprocal estimates. Types U32 F32.  */
13676  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
13677  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
13678  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
13679  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
13680   /* VCLS. Types S8 S16 S32.  */
13681  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
13682  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
13683   /* VCLZ. Types I8 I16 I32.  */
13684  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
13685  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
13686   /* VCNT. Size 8.  */
13687  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
13688  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
13689   /* Two address, untyped.  */
13690  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
13691  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
13692   /* VTRN. Sizes 8 16 32.  */
13693  nUF(vtrn,      vtrn,    2, (RNDQ, RNDQ),     neon_trn),
13694  nUF(vtrnq,     vtrn,    2, (RNQ,  RNQ),      neon_trn),
13695
13696   /* Table lookup. Size 8.  */
13697  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
13698  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
13699
13700 #undef THUMB_VARIANT
13701 #define THUMB_VARIANT &fpu_vfp_v3_or_neon_ext
13702 #undef ARM_VARIANT
13703 #define ARM_VARIANT &fpu_vfp_v3_or_neon_ext
13704
13705   /* Load/store instructions. Available in Neon or VFPv3.  */
13706  NCE(vldm,      c900b00, 2, (RRw, NRDLST),    neon_ldm_stm),
13707  NCE(vldmia,    c900b00, 2, (RRw, NRDLST),    neon_ldm_stm),
13708  NCE(vldmdb,    d100b00, 2, (RRw, NRDLST),    neon_ldm_stm),
13709  NCE(vstm,      c800b00, 2, (RRw, NRDLST),    neon_ldm_stm),
13710  NCE(vstmia,    c800b00, 2, (RRw, NRDLST),    neon_ldm_stm),
13711  NCE(vstmdb,    d000b00, 2, (RRw, NRDLST),    neon_ldm_stm),
13712  NCE(vldr,      d100b00, 2, (RND, ADDR),      neon_ldr_str),
13713  NCE(vstr,      d000b00, 2, (RND, ADDR),      neon_ldr_str),
13714
13715   /* Neon element/structure load/store.  */
13716  nUF(vld1,      vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
13717  nUF(vst1,      vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
13718  nUF(vld2,      vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
13719  nUF(vst2,      vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
13720  nUF(vld3,      vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
13721  nUF(vst3,      vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
13722  nUF(vld4,      vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
13723  nUF(vst4,      vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
13724
13725 #undef THUMB_VARIANT
13726 #define THUMB_VARIANT &fpu_vfp_ext_v3
13727 #undef ARM_VARIANT
13728 #define ARM_VARIANT &fpu_vfp_ext_v3
13729
13730  cCE(fconsts,   eb00a00, 2, (RVS, I255),      vfp_sp_const),
13731  cCE(fconstd,   eb00b00, 2, (RVD, I255),      vfp_dp_const),
13732  cCE(fshtos,    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
13733  cCE(fshtod,    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
13734  cCE(fsltos,    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
13735  cCE(fsltod,    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
13736  cCE(fuhtos,    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
13737  cCE(fuhtod,    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
13738  cCE(fultos,    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
13739  cCE(fultod,    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
13740  cCE(ftoshs,    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
13741  cCE(ftoshd,    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
13742  cCE(ftosls,    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
13743  cCE(ftosld,    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
13744  cCE(ftouhs,    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
13745  cCE(ftouhd,    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
13746  cCE(ftouls,    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
13747  cCE(ftould,    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
13748
13749 #undef THUMB_VARIANT
13750 #undef ARM_VARIANT
13751 #define ARM_VARIANT &arm_cext_xscale /* Intel XScale extensions.         */
13752  cCE(mia,       e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
13753  cCE(miaph,     e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
13754  cCE(miabb,     e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
13755  cCE(miabt,     e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
13756  cCE(miatb,     e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
13757  cCE(miatt,     e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
13758  cCE(mar,       c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
13759  cCE(mra,       c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
13760
13761 #undef ARM_VARIANT
13762 #define ARM_VARIANT &arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
13763  cCE(tandcb,    e13f130, 1, (RR),                   iwmmxt_tandorc),
13764  cCE(tandch,    e53f130, 1, (RR),                   iwmmxt_tandorc),
13765  cCE(tandcw,    e93f130, 1, (RR),                   iwmmxt_tandorc),
13766  cCE(tbcstb,    e400010, 2, (RIWR, RR),             rn_rd),
13767  cCE(tbcsth,    e400050, 2, (RIWR, RR),             rn_rd),
13768  cCE(tbcstw,    e400090, 2, (RIWR, RR),             rn_rd),
13769  cCE(textrcb,   e130170, 2, (RR, I7),               iwmmxt_textrc),
13770  cCE(textrch,   e530170, 2, (RR, I7),               iwmmxt_textrc),
13771  cCE(textrcw,   e930170, 2, (RR, I7),               iwmmxt_textrc),
13772  cCE(textrmub,  e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
13773  cCE(textrmuh,  e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
13774  cCE(textrmuw,  e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
13775  cCE(textrmsb,  e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
13776  cCE(textrmsh,  e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
13777  cCE(textrmsw,  e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
13778  cCE(tinsrb,    e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
13779  cCE(tinsrh,    e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
13780  cCE(tinsrw,    e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
13781  cCE(tmcr,      e000110, 2, (RIWC, RR),             rn_rd),
13782  cCE(tmcrr,     c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
13783  cCE(tmia,      e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
13784  cCE(tmiaph,    e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
13785  cCE(tmiabb,    e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
13786  cCE(tmiabt,    e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
13787  cCE(tmiatb,    e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
13788  cCE(tmiatt,    e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
13789  cCE(tmovmskb,  e100030, 2, (RR, RIWR),             rd_rn),
13790  cCE(tmovmskh,  e500030, 2, (RR, RIWR),             rd_rn),
13791  cCE(tmovmskw,  e900030, 2, (RR, RIWR),             rd_rn),
13792  cCE(tmrc,      e100110, 2, (RR, RIWC),             rd_rn),
13793  cCE(tmrrc,     c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
13794  cCE(torcb,     e13f150, 1, (RR),                   iwmmxt_tandorc),
13795  cCE(torch,     e53f150, 1, (RR),                   iwmmxt_tandorc),
13796  cCE(torcw,     e93f150, 1, (RR),                   iwmmxt_tandorc),
13797  cCE(waccb,     e0001c0, 2, (RIWR, RIWR),           rd_rn),
13798  cCE(wacch,     e4001c0, 2, (RIWR, RIWR),           rd_rn),
13799  cCE(waccw,     e8001c0, 2, (RIWR, RIWR),           rd_rn),
13800  cCE(waddbss,   e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13801  cCE(waddb,     e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13802  cCE(waddbus,   e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13803  cCE(waddhss,   e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13804  cCE(waddh,     e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13805  cCE(waddhus,   e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13806  cCE(waddwss,   eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13807  cCE(waddw,     e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13808  cCE(waddwus,   e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13809  cCE(waligni,   e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
13810  cCE(walignr0,  e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13811  cCE(walignr1,  e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13812  cCE(walignr2,  ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13813  cCE(walignr3,  eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13814  cCE(wand,      e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13815  cCE(wandn,     e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13816  cCE(wavg2b,    e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13817  cCE(wavg2br,   e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13818  cCE(wavg2h,    ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13819  cCE(wavg2hr,   ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13820  cCE(wcmpeqb,   e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13821  cCE(wcmpeqh,   e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13822  cCE(wcmpeqw,   e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13823  cCE(wcmpgtub,  e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13824  cCE(wcmpgtuh,  e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13825  cCE(wcmpgtuw,  e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13826  cCE(wcmpgtsb,  e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13827  cCE(wcmpgtsh,  e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13828  cCE(wcmpgtsw,  eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13829  cCE(wldrb,     c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
13830  cCE(wldrh,     c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
13831  cCE(wldrw,     c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
13832  cCE(wldrd,     c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
13833  cCE(wmacs,     e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13834  cCE(wmacsz,    e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13835  cCE(wmacu,     e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13836  cCE(wmacuz,    e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13837  cCE(wmadds,    ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13838  cCE(wmaddu,    e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13839  cCE(wmaxsb,    e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13840  cCE(wmaxsh,    e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13841  cCE(wmaxsw,    ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13842  cCE(wmaxub,    e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13843  cCE(wmaxuh,    e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13844  cCE(wmaxuw,    e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13845  cCE(wminsb,    e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13846  cCE(wminsh,    e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13847  cCE(wminsw,    eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13848  cCE(wminub,    e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13849  cCE(wminuh,    e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13850  cCE(wminuw,    e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13851  cCE(wmov,      e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
13852  cCE(wmulsm,    e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13853  cCE(wmulsl,    e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13854  cCE(wmulum,    e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13855  cCE(wmulul,    e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13856  cCE(wor,       e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13857  cCE(wpackhss,  e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13858  cCE(wpackhus,  e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13859  cCE(wpackwss,  eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13860  cCE(wpackwus,  e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13861  cCE(wpackdss,  ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13862  cCE(wpackdus,  ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13863  cCE(wrorh,     e700040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13864  cCE(wrorhg,    e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13865  cCE(wrorw,     eb00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13866  cCE(wrorwg,    eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13867  cCE(wrord,     ef00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13868  cCE(wrordg,    ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13869  cCE(wsadb,     e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13870  cCE(wsadbz,    e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13871  cCE(wsadh,     e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13872  cCE(wsadhz,    e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13873  cCE(wshufh,    e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
13874  cCE(wsllh,     e500040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13875  cCE(wsllhg,    e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13876  cCE(wsllw,     e900040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13877  cCE(wsllwg,    e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13878  cCE(wslld,     ed00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13879  cCE(wslldg,    ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13880  cCE(wsrah,     e400040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13881  cCE(wsrahg,    e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13882  cCE(wsraw,     e800040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13883  cCE(wsrawg,    e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13884  cCE(wsrad,     ec00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13885  cCE(wsradg,    ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13886  cCE(wsrlh,     e600040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13887  cCE(wsrlhg,    e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13888  cCE(wsrlw,     ea00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13889  cCE(wsrlwg,    ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13890  cCE(wsrld,     ee00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13891  cCE(wsrldg,    ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
13892  cCE(wstrb,     c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
13893  cCE(wstrh,     c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
13894  cCE(wstrw,     c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
13895  cCE(wstrd,     c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
13896  cCE(wsubbss,   e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13897  cCE(wsubb,     e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13898  cCE(wsubbus,   e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13899  cCE(wsubhss,   e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13900  cCE(wsubh,     e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13901  cCE(wsubhus,   e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13902  cCE(wsubwss,   eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13903  cCE(wsubw,     e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13904  cCE(wsubwus,   e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13905  cCE(wunpckehub,e0000c0, 2, (RIWR, RIWR),           rd_rn),
13906  cCE(wunpckehuh,e4000c0, 2, (RIWR, RIWR),           rd_rn),
13907  cCE(wunpckehuw,e8000c0, 2, (RIWR, RIWR),           rd_rn),
13908  cCE(wunpckehsb,e2000c0, 2, (RIWR, RIWR),           rd_rn),
13909  cCE(wunpckehsh,e6000c0, 2, (RIWR, RIWR),           rd_rn),
13910  cCE(wunpckehsw,ea000c0, 2, (RIWR, RIWR),           rd_rn),
13911  cCE(wunpckihb, e1000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13912  cCE(wunpckihh, e5000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13913  cCE(wunpckihw, e9000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13914  cCE(wunpckelub,e0000e0, 2, (RIWR, RIWR),           rd_rn),
13915  cCE(wunpckeluh,e4000e0, 2, (RIWR, RIWR),           rd_rn),
13916  cCE(wunpckeluw,e8000e0, 2, (RIWR, RIWR),           rd_rn),
13917  cCE(wunpckelsb,e2000e0, 2, (RIWR, RIWR),           rd_rn),
13918  cCE(wunpckelsh,e6000e0, 2, (RIWR, RIWR),           rd_rn),
13919  cCE(wunpckelsw,ea000e0, 2, (RIWR, RIWR),           rd_rn),
13920  cCE(wunpckilb, e1000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13921  cCE(wunpckilh, e5000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13922  cCE(wunpckilw, e9000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13923  cCE(wxor,      e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
13924  cCE(wzero,     e300000, 1, (RIWR),                 iwmmxt_wzero),
13925
13926 #undef ARM_VARIANT
13927 #define ARM_VARIANT &arm_cext_maverick /* Cirrus Maverick instructions. */
13928  cCE(cfldrs,    c100400, 2, (RMF, ADDR),              rd_cpaddr),
13929  cCE(cfldrd,    c500400, 2, (RMD, ADDR),              rd_cpaddr),
13930  cCE(cfldr32,   c100500, 2, (RMFX, ADDR),             rd_cpaddr),
13931  cCE(cfldr64,   c500500, 2, (RMDX, ADDR),             rd_cpaddr),
13932  cCE(cfstrs,    c000400, 2, (RMF, ADDR),              rd_cpaddr),
13933  cCE(cfstrd,    c400400, 2, (RMD, ADDR),              rd_cpaddr),
13934  cCE(cfstr32,   c000500, 2, (RMFX, ADDR),             rd_cpaddr),
13935  cCE(cfstr64,   c400500, 2, (RMDX, ADDR),             rd_cpaddr),
13936  cCE(cfmvsr,    e000450, 2, (RMF, RR),                rn_rd),
13937  cCE(cfmvrs,    e100450, 2, (RR, RMF),                rd_rn),
13938  cCE(cfmvdlr,   e000410, 2, (RMD, RR),                rn_rd),
13939  cCE(cfmvrdl,   e100410, 2, (RR, RMD),                rd_rn),
13940  cCE(cfmvdhr,   e000430, 2, (RMD, RR),                rn_rd),
13941  cCE(cfmvrdh,   e100430, 2, (RR, RMD),                rd_rn),
13942  cCE(cfmv64lr,  e000510, 2, (RMDX, RR),               rn_rd),
13943  cCE(cfmvr64l,  e100510, 2, (RR, RMDX),               rd_rn),
13944  cCE(cfmv64hr,  e000530, 2, (RMDX, RR),               rn_rd),
13945  cCE(cfmvr64h,  e100530, 2, (RR, RMDX),               rd_rn),
13946  cCE(cfmval32,  e200440, 2, (RMAX, RMFX),             rd_rn),
13947  cCE(cfmv32al,  e100440, 2, (RMFX, RMAX),             rd_rn),
13948  cCE(cfmvam32,  e200460, 2, (RMAX, RMFX),             rd_rn),
13949  cCE(cfmv32am,  e100460, 2, (RMFX, RMAX),             rd_rn),
13950  cCE(cfmvah32,  e200480, 2, (RMAX, RMFX),             rd_rn),
13951  cCE(cfmv32ah,  e100480, 2, (RMFX, RMAX),             rd_rn),
13952  cCE(cfmva32,   e2004a0, 2, (RMAX, RMFX),             rd_rn),
13953  cCE(cfmv32a,   e1004a0, 2, (RMFX, RMAX),             rd_rn),
13954  cCE(cfmva64,   e2004c0, 2, (RMAX, RMDX),             rd_rn),
13955  cCE(cfmv64a,   e1004c0, 2, (RMDX, RMAX),             rd_rn),
13956  cCE(cfmvsc32,  e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
13957  cCE(cfmv32sc,  e1004e0, 2, (RMDX, RMDS),             rd),
13958  cCE(cfcpys,    e000400, 2, (RMF, RMF),               rd_rn),
13959  cCE(cfcpyd,    e000420, 2, (RMD, RMD),               rd_rn),
13960  cCE(cfcvtsd,   e000460, 2, (RMD, RMF),               rd_rn),
13961  cCE(cfcvtds,   e000440, 2, (RMF, RMD),               rd_rn),
13962  cCE(cfcvt32s,  e000480, 2, (RMF, RMFX),              rd_rn),
13963  cCE(cfcvt32d,  e0004a0, 2, (RMD, RMFX),              rd_rn),
13964  cCE(cfcvt64s,  e0004c0, 2, (RMF, RMDX),              rd_rn),
13965  cCE(cfcvt64d,  e0004e0, 2, (RMD, RMDX),              rd_rn),
13966  cCE(cfcvts32,  e100580, 2, (RMFX, RMF),              rd_rn),
13967  cCE(cfcvtd32,  e1005a0, 2, (RMFX, RMD),              rd_rn),
13968  cCE(cftruncs32,e1005c0, 2, (RMFX, RMF),              rd_rn),
13969  cCE(cftruncd32,e1005e0, 2, (RMFX, RMD),              rd_rn),
13970  cCE(cfrshl32,  e000550, 3, (RMFX, RMFX, RR),         mav_triple),
13971  cCE(cfrshl64,  e000570, 3, (RMDX, RMDX, RR),         mav_triple),
13972  cCE(cfsh32,    e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
13973  cCE(cfsh64,    e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
13974  cCE(cfcmps,    e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
13975  cCE(cfcmpd,    e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
13976  cCE(cfcmp32,   e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
13977  cCE(cfcmp64,   e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
13978  cCE(cfabss,    e300400, 2, (RMF, RMF),               rd_rn),
13979  cCE(cfabsd,    e300420, 2, (RMD, RMD),               rd_rn),
13980  cCE(cfnegs,    e300440, 2, (RMF, RMF),               rd_rn),
13981  cCE(cfnegd,    e300460, 2, (RMD, RMD),               rd_rn),
13982  cCE(cfadds,    e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
13983  cCE(cfaddd,    e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
13984  cCE(cfsubs,    e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
13985  cCE(cfsubd,    e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
13986  cCE(cfmuls,    e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
13987  cCE(cfmuld,    e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
13988  cCE(cfabs32,   e300500, 2, (RMFX, RMFX),             rd_rn),
13989  cCE(cfabs64,   e300520, 2, (RMDX, RMDX),             rd_rn),
13990  cCE(cfneg32,   e300540, 2, (RMFX, RMFX),             rd_rn),
13991  cCE(cfneg64,   e300560, 2, (RMDX, RMDX),             rd_rn),
13992  cCE(cfadd32,   e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
13993  cCE(cfadd64,   e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
13994  cCE(cfsub32,   e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
13995  cCE(cfsub64,   e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
13996  cCE(cfmul32,   e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
13997  cCE(cfmul64,   e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
13998  cCE(cfmac32,   e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
13999  cCE(cfmsc32,   e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
14000  cCE(cfmadd32,  e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
14001  cCE(cfmsub32,  e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
14002  cCE(cfmadda32, e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
14003  cCE(cfmsuba32, e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
14004 };
14005 #undef ARM_VARIANT
14006 #undef THUMB_VARIANT
14007 #undef TCE
14008 #undef TCM
14009 #undef TUE
14010 #undef TUF
14011 #undef TCC
14012 #undef cCE
14013 #undef cCL
14014 #undef C3E
14015 #undef CE
14016 #undef CM
14017 #undef UE
14018 #undef UF
14019 #undef UT
14020 #undef NUF
14021 #undef nUF
14022 #undef NCE
14023 #undef nCE
14024 #undef OPS0
14025 #undef OPS1
14026 #undef OPS2
14027 #undef OPS3
14028 #undef OPS4
14029 #undef OPS5
14030 #undef OPS6
14031 #undef do_0
14032 \f
14033 /* MD interface: bits in the object file.  */
14034
14035 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
14036    for use in the a.out file, and stores them in the array pointed to by buf.
14037    This knows about the endian-ness of the target machine and does
14038    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
14039    2 (short) and 4 (long)  Floating numbers are put out as a series of
14040    LITTLENUMS (shorts, here at least).  */
14041
14042 void
14043 md_number_to_chars (char * buf, valueT val, int n)
14044 {
14045   if (target_big_endian)
14046     number_to_chars_bigendian (buf, val, n);
14047   else
14048     number_to_chars_littleendian (buf, val, n);
14049 }
14050
14051 static valueT
14052 md_chars_to_number (char * buf, int n)
14053 {
14054   valueT result = 0;
14055   unsigned char * where = (unsigned char *) buf;
14056
14057   if (target_big_endian)
14058     {
14059       while (n--)
14060         {
14061           result <<= 8;
14062           result |= (*where++ & 255);
14063         }
14064     }
14065   else
14066     {
14067       while (n--)
14068         {
14069           result <<= 8;
14070           result |= (where[n] & 255);
14071         }
14072     }
14073
14074   return result;
14075 }
14076
14077 /* MD interface: Sections.  */
14078
14079 /* Estimate the size of a frag before relaxing.  Assume everything fits in
14080    2 bytes.  */
14081
14082 int
14083 md_estimate_size_before_relax (fragS * fragp,
14084                                segT    segtype ATTRIBUTE_UNUSED)
14085 {
14086   fragp->fr_var = 2;
14087   return 2;
14088 }
14089
14090 /* Convert a machine dependent frag.  */
14091
14092 void
14093 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
14094 {
14095   unsigned long insn;
14096   unsigned long old_op;
14097   char *buf;
14098   expressionS exp;
14099   fixS *fixp;
14100   int reloc_type;
14101   int pc_rel;
14102   int opcode;
14103
14104   buf = fragp->fr_literal + fragp->fr_fix;
14105
14106   old_op = bfd_get_16(abfd, buf);
14107   if (fragp->fr_symbol) {
14108       exp.X_op = O_symbol;
14109       exp.X_add_symbol = fragp->fr_symbol;
14110   } else {
14111       exp.X_op = O_constant;
14112   }
14113   exp.X_add_number = fragp->fr_offset;
14114   opcode = fragp->fr_subtype;
14115   switch (opcode)
14116     {
14117     case T_MNEM_ldr_pc:
14118     case T_MNEM_ldr_pc2:
14119     case T_MNEM_ldr_sp:
14120     case T_MNEM_str_sp:
14121     case T_MNEM_ldr:
14122     case T_MNEM_ldrb:
14123     case T_MNEM_ldrh:
14124     case T_MNEM_str:
14125     case T_MNEM_strb:
14126     case T_MNEM_strh:
14127       if (fragp->fr_var == 4)
14128         {
14129           insn = THUMB_OP32(opcode);
14130           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
14131             {
14132               insn |= (old_op & 0x700) << 4;
14133             }
14134           else
14135             {
14136               insn |= (old_op & 7) << 12;
14137               insn |= (old_op & 0x38) << 13;
14138             }
14139           insn |= 0x00000c00;
14140           put_thumb32_insn (buf, insn);
14141           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
14142         }
14143       else
14144         {
14145           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
14146         }
14147       pc_rel = (opcode == T_MNEM_ldr_pc2);
14148       break;
14149     case T_MNEM_adr:
14150       if (fragp->fr_var == 4)
14151         {
14152           insn = THUMB_OP32 (opcode);
14153           insn |= (old_op & 0xf0) << 4;
14154           put_thumb32_insn (buf, insn);
14155           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
14156         }
14157       else
14158         {
14159           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
14160           exp.X_add_number -= 4;
14161         }
14162       pc_rel = 1;
14163       break;
14164     case T_MNEM_mov:
14165     case T_MNEM_movs:
14166     case T_MNEM_cmp:
14167     case T_MNEM_cmn:
14168       if (fragp->fr_var == 4)
14169         {
14170           int r0off = (opcode == T_MNEM_mov
14171                        || opcode == T_MNEM_movs) ? 0 : 8;
14172           insn = THUMB_OP32 (opcode);
14173           insn = (insn & 0xe1ffffff) | 0x10000000;
14174           insn |= (old_op & 0x700) << r0off;
14175           put_thumb32_insn (buf, insn);
14176           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
14177         }
14178       else
14179         {
14180           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
14181         }
14182       pc_rel = 0;
14183       break;
14184     case T_MNEM_b:
14185       if (fragp->fr_var == 4)
14186         {
14187           insn = THUMB_OP32(opcode);
14188           put_thumb32_insn (buf, insn);
14189           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
14190         }
14191       else
14192         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
14193       pc_rel = 1;
14194       break;
14195     case T_MNEM_bcond:
14196       if (fragp->fr_var == 4)
14197         {
14198           insn = THUMB_OP32(opcode);
14199           insn |= (old_op & 0xf00) << 14;
14200           put_thumb32_insn (buf, insn);
14201           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
14202         }
14203       else
14204         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
14205       pc_rel = 1;
14206       break;
14207     case T_MNEM_add_sp:
14208     case T_MNEM_add_pc:
14209     case T_MNEM_inc_sp:
14210     case T_MNEM_dec_sp:
14211       if (fragp->fr_var == 4)
14212         {
14213           /* ??? Choose between add and addw.  */
14214           insn = THUMB_OP32 (opcode);
14215           insn |= (old_op & 0xf0) << 4;
14216           put_thumb32_insn (buf, insn);
14217           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
14218         }
14219       else
14220         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
14221       pc_rel = 0;
14222       break;
14223
14224     case T_MNEM_addi:
14225     case T_MNEM_addis:
14226     case T_MNEM_subi:
14227     case T_MNEM_subis:
14228       if (fragp->fr_var == 4)
14229         {
14230           insn = THUMB_OP32 (opcode);
14231           insn |= (old_op & 0xf0) << 4;
14232           insn |= (old_op & 0xf) << 16;
14233           put_thumb32_insn (buf, insn);
14234           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
14235         }
14236       else
14237         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
14238       pc_rel = 0;
14239       break;
14240     default:
14241       abort();
14242     }
14243   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
14244                       reloc_type);
14245   fixp->fx_file = fragp->fr_file;
14246   fixp->fx_line = fragp->fr_line;
14247   fragp->fr_fix += fragp->fr_var;
14248 }
14249
14250 /* Return the size of a relaxable immediate operand instruction.
14251    SHIFT and SIZE specify the form of the allowable immediate.  */
14252 static int
14253 relax_immediate (fragS *fragp, int size, int shift)
14254 {
14255   offsetT offset;
14256   offsetT mask;
14257   offsetT low;
14258
14259   /* ??? Should be able to do better than this.  */
14260   if (fragp->fr_symbol)
14261     return 4;
14262
14263   low = (1 << shift) - 1;
14264   mask = (1 << (shift + size)) - (1 << shift);
14265   offset = fragp->fr_offset;
14266   /* Force misaligned offsets to 32-bit variant.  */
14267   if (offset & low)
14268     return -4;
14269   if (offset & ~mask)
14270     return 4;
14271   return 2;
14272 }
14273
14274 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
14275    load.  */
14276 static int
14277 relax_adr (fragS *fragp, asection *sec)
14278 {
14279   addressT addr;
14280   offsetT val;
14281
14282   /* Assume worst case for symbols not known to be in the same section.  */
14283   if (!S_IS_DEFINED(fragp->fr_symbol)
14284       || sec != S_GET_SEGMENT (fragp->fr_symbol))
14285     return 4;
14286
14287   val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
14288   addr = fragp->fr_address + fragp->fr_fix;
14289   addr = (addr + 4) & ~3;
14290   /* Fix the insn as the 4-byte version if the target address is not
14291      sufficiently aligned.  This is prevents an infinite loop when two
14292      instructions have contradictory range/alignment requirements.  */
14293   if (val & 3)
14294     return -4;
14295   val -= addr;
14296   if (val < 0 || val > 1020)
14297     return 4;
14298   return 2;
14299 }
14300
14301 /* Return the size of a relaxable add/sub immediate instruction.  */
14302 static int
14303 relax_addsub (fragS *fragp, asection *sec)
14304 {
14305   char *buf;
14306   int op;
14307
14308   buf = fragp->fr_literal + fragp->fr_fix;
14309   op = bfd_get_16(sec->owner, buf);
14310   if ((op & 0xf) == ((op >> 4) & 0xf))
14311     return relax_immediate (fragp, 8, 0);
14312   else
14313     return relax_immediate (fragp, 3, 0);
14314 }
14315
14316
14317 /* Return the size of a relaxable branch instruction.  BITS is the
14318    size of the offset field in the narrow instruction.  */
14319
14320 static int
14321 relax_branch (fragS *fragp, asection *sec, int bits)
14322 {
14323   addressT addr;
14324   offsetT val;
14325   offsetT limit;
14326
14327   /* Assume worst case for symbols not known to be in the same section.  */
14328   if (!S_IS_DEFINED(fragp->fr_symbol)
14329       || sec != S_GET_SEGMENT (fragp->fr_symbol))
14330     return 4;
14331
14332   val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
14333   addr = fragp->fr_address + fragp->fr_fix + 4;
14334   val -= addr;
14335
14336   /* Offset is a signed value *2 */
14337   limit = 1 << bits;
14338   if (val >= limit || val < -limit)
14339     return 4;
14340   return 2;
14341 }
14342
14343
14344 /* Relax a machine dependent frag.  This returns the amount by which
14345    the current size of the frag should change.  */
14346
14347 int
14348 arm_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
14349 {
14350   int oldsize;
14351   int newsize;
14352
14353   oldsize = fragp->fr_var;
14354   switch (fragp->fr_subtype)
14355     {
14356     case T_MNEM_ldr_pc2:
14357       newsize = relax_adr(fragp, sec);
14358       break;
14359     case T_MNEM_ldr_pc:
14360     case T_MNEM_ldr_sp:
14361     case T_MNEM_str_sp:
14362       newsize = relax_immediate(fragp, 8, 2);
14363       break;
14364     case T_MNEM_ldr:
14365     case T_MNEM_str:
14366       newsize = relax_immediate(fragp, 5, 2);
14367       break;
14368     case T_MNEM_ldrh:
14369     case T_MNEM_strh:
14370       newsize = relax_immediate(fragp, 5, 1);
14371       break;
14372     case T_MNEM_ldrb:
14373     case T_MNEM_strb:
14374       newsize = relax_immediate(fragp, 5, 0);
14375       break;
14376     case T_MNEM_adr:
14377       newsize = relax_adr(fragp, sec);
14378       break;
14379     case T_MNEM_mov:
14380     case T_MNEM_movs:
14381     case T_MNEM_cmp:
14382     case T_MNEM_cmn:
14383       newsize = relax_immediate(fragp, 8, 0);
14384       break;
14385     case T_MNEM_b:
14386       newsize = relax_branch(fragp, sec, 11);
14387       break;
14388     case T_MNEM_bcond:
14389       newsize = relax_branch(fragp, sec, 8);
14390       break;
14391     case T_MNEM_add_sp:
14392     case T_MNEM_add_pc:
14393       newsize = relax_immediate (fragp, 8, 2);
14394       break;
14395     case T_MNEM_inc_sp:
14396     case T_MNEM_dec_sp:
14397       newsize = relax_immediate (fragp, 7, 2);
14398       break;
14399     case T_MNEM_addi:
14400     case T_MNEM_addis:
14401     case T_MNEM_subi:
14402     case T_MNEM_subis:
14403       newsize = relax_addsub (fragp, sec);
14404       break;
14405     default:
14406       abort();
14407     }
14408   if (newsize < 0)
14409     {
14410       fragp->fr_var = -newsize;
14411       md_convert_frag (sec->owner, sec, fragp);
14412       frag_wane(fragp);
14413       return -(newsize + oldsize);
14414     }
14415   fragp->fr_var = newsize;
14416   return newsize - oldsize;
14417 }
14418
14419 /* Round up a section size to the appropriate boundary.  */
14420
14421 valueT
14422 md_section_align (segT   segment ATTRIBUTE_UNUSED,
14423                   valueT size)
14424 {
14425 #ifdef OBJ_ELF
14426   return size;
14427 #else
14428   /* Round all sects to multiple of 4.  */
14429   return (size + 3) & ~3;
14430 #endif
14431 }
14432
14433 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
14434    of an rs_align_code fragment.  */
14435
14436 void
14437 arm_handle_align (fragS * fragP)
14438 {
14439   static char const arm_noop[4] = { 0x00, 0x00, 0xa0, 0xe1 };
14440   static char const thumb_noop[2] = { 0xc0, 0x46 };
14441   static char const arm_bigend_noop[4] = { 0xe1, 0xa0, 0x00, 0x00 };
14442   static char const thumb_bigend_noop[2] = { 0x46, 0xc0 };
14443
14444   int bytes, fix, noop_size;
14445   char * p;
14446   const char * noop;
14447
14448   if (fragP->fr_type != rs_align_code)
14449     return;
14450
14451   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
14452   p = fragP->fr_literal + fragP->fr_fix;
14453   fix = 0;
14454
14455   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
14456     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
14457
14458   if (fragP->tc_frag_data)
14459     {
14460       if (target_big_endian)
14461         noop = thumb_bigend_noop;
14462       else
14463         noop = thumb_noop;
14464       noop_size = sizeof (thumb_noop);
14465     }
14466   else
14467     {
14468       if (target_big_endian)
14469         noop = arm_bigend_noop;
14470       else
14471         noop = arm_noop;
14472       noop_size = sizeof (arm_noop);
14473     }
14474
14475   if (bytes & (noop_size - 1))
14476     {
14477       fix = bytes & (noop_size - 1);
14478       memset (p, 0, fix);
14479       p += fix;
14480       bytes -= fix;
14481     }
14482
14483   while (bytes >= noop_size)
14484     {
14485       memcpy (p, noop, noop_size);
14486       p += noop_size;
14487       bytes -= noop_size;
14488       fix += noop_size;
14489     }
14490
14491   fragP->fr_fix += fix;
14492   fragP->fr_var = noop_size;
14493 }
14494
14495 /* Called from md_do_align.  Used to create an alignment
14496    frag in a code section.  */
14497
14498 void
14499 arm_frag_align_code (int n, int max)
14500 {
14501   char * p;
14502
14503   /* We assume that there will never be a requirement
14504      to support alignments greater than 32 bytes.  */
14505   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
14506     as_fatal (_("alignments greater than 32 bytes not supported in .text sections."));
14507
14508   p = frag_var (rs_align_code,
14509                 MAX_MEM_FOR_RS_ALIGN_CODE,
14510                 1,
14511                 (relax_substateT) max,
14512                 (symbolS *) NULL,
14513                 (offsetT) n,
14514                 (char *) NULL);
14515   *p = 0;
14516 }
14517
14518 /* Perform target specific initialisation of a frag.  */
14519
14520 void
14521 arm_init_frag (fragS * fragP)
14522 {
14523   /* Record whether this frag is in an ARM or a THUMB area.  */
14524   fragP->tc_frag_data = thumb_mode;
14525 }
14526
14527 #ifdef OBJ_ELF
14528 /* When we change sections we need to issue a new mapping symbol.  */
14529
14530 void
14531 arm_elf_change_section (void)
14532 {
14533   flagword flags;
14534   segment_info_type *seginfo;
14535
14536   /* Link an unlinked unwind index table section to the .text section.  */
14537   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
14538       && elf_linked_to_section (now_seg) == NULL)
14539     elf_linked_to_section (now_seg) = text_section;
14540
14541   if (!SEG_NORMAL (now_seg))
14542     return;
14543
14544   flags = bfd_get_section_flags (stdoutput, now_seg);
14545
14546   /* We can ignore sections that only contain debug info.  */
14547   if ((flags & SEC_ALLOC) == 0)
14548     return;
14549
14550   seginfo = seg_info (now_seg);
14551   mapstate = seginfo->tc_segment_info_data.mapstate;
14552   marked_pr_dependency = seginfo->tc_segment_info_data.marked_pr_dependency;
14553 }
14554
14555 int
14556 arm_elf_section_type (const char * str, size_t len)
14557 {
14558   if (len == 5 && strncmp (str, "exidx", 5) == 0)
14559     return SHT_ARM_EXIDX;
14560
14561   return -1;
14562 }
14563 \f
14564 /* Code to deal with unwinding tables.  */
14565
14566 static void add_unwind_adjustsp (offsetT);
14567
14568 /* Cenerate and deferred unwind frame offset.  */
14569
14570 static void
14571 flush_pending_unwind (void)
14572 {
14573   offsetT offset;
14574
14575   offset = unwind.pending_offset;
14576   unwind.pending_offset = 0;
14577   if (offset != 0)
14578     add_unwind_adjustsp (offset);
14579 }
14580
14581 /* Add an opcode to this list for this function.  Two-byte opcodes should
14582    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
14583    order.  */
14584
14585 static void
14586 add_unwind_opcode (valueT op, int length)
14587 {
14588   /* Add any deferred stack adjustment.  */
14589   if (unwind.pending_offset)
14590     flush_pending_unwind ();
14591
14592   unwind.sp_restored = 0;
14593
14594   if (unwind.opcode_count + length > unwind.opcode_alloc)
14595     {
14596       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
14597       if (unwind.opcodes)
14598         unwind.opcodes = xrealloc (unwind.opcodes,
14599                                    unwind.opcode_alloc);
14600       else
14601         unwind.opcodes = xmalloc (unwind.opcode_alloc);
14602     }
14603   while (length > 0)
14604     {
14605       length--;
14606       unwind.opcodes[unwind.opcode_count] = op & 0xff;
14607       op >>= 8;
14608       unwind.opcode_count++;
14609     }
14610 }
14611
14612 /* Add unwind opcodes to adjust the stack pointer.  */
14613
14614 static void
14615 add_unwind_adjustsp (offsetT offset)
14616 {
14617   valueT op;
14618
14619   if (offset > 0x200)
14620     {
14621       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
14622       char bytes[5];
14623       int n;
14624       valueT o;
14625
14626       /* Long form: 0xb2, uleb128.  */
14627       /* This might not fit in a word so add the individual bytes,
14628          remembering the list is built in reverse order.  */
14629       o = (valueT) ((offset - 0x204) >> 2);
14630       if (o == 0)
14631         add_unwind_opcode (0, 1);
14632
14633       /* Calculate the uleb128 encoding of the offset.  */
14634       n = 0;
14635       while (o)
14636         {
14637           bytes[n] = o & 0x7f;
14638           o >>= 7;
14639           if (o)
14640             bytes[n] |= 0x80;
14641           n++;
14642         }
14643       /* Add the insn.  */
14644       for (; n; n--)
14645         add_unwind_opcode (bytes[n - 1], 1);
14646       add_unwind_opcode (0xb2, 1);
14647     }
14648   else if (offset > 0x100)
14649     {
14650       /* Two short opcodes.  */
14651       add_unwind_opcode (0x3f, 1);
14652       op = (offset - 0x104) >> 2;
14653       add_unwind_opcode (op, 1);
14654     }
14655   else if (offset > 0)
14656     {
14657       /* Short opcode.  */
14658       op = (offset - 4) >> 2;
14659       add_unwind_opcode (op, 1);
14660     }
14661   else if (offset < 0)
14662     {
14663       offset = -offset;
14664       while (offset > 0x100)
14665         {
14666           add_unwind_opcode (0x7f, 1);
14667           offset -= 0x100;
14668         }
14669       op = ((offset - 4) >> 2) | 0x40;
14670       add_unwind_opcode (op, 1);
14671     }
14672 }
14673
14674 /* Finish the list of unwind opcodes for this function.  */
14675 static void
14676 finish_unwind_opcodes (void)
14677 {
14678   valueT op;
14679
14680   if (unwind.fp_used)
14681     {
14682       /* Adjust sp as neccessary.  */
14683       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
14684       flush_pending_unwind ();
14685
14686       /* After restoring sp from the frame pointer.  */
14687       op = 0x90 | unwind.fp_reg;
14688       add_unwind_opcode (op, 1);
14689     }
14690   else
14691     flush_pending_unwind ();
14692 }
14693
14694
14695 /* Start an exception table entry.  If idx is nonzero this is an index table
14696    entry.  */
14697
14698 static void
14699 start_unwind_section (const segT text_seg, int idx)
14700 {
14701   const char * text_name;
14702   const char * prefix;
14703   const char * prefix_once;
14704   const char * group_name;
14705   size_t prefix_len;
14706   size_t text_len;
14707   char * sec_name;
14708   size_t sec_name_len;
14709   int type;
14710   int flags;
14711   int linkonce;
14712
14713   if (idx)
14714     {
14715       prefix = ELF_STRING_ARM_unwind;
14716       prefix_once = ELF_STRING_ARM_unwind_once;
14717       type = SHT_ARM_EXIDX;
14718     }
14719   else
14720     {
14721       prefix = ELF_STRING_ARM_unwind_info;
14722       prefix_once = ELF_STRING_ARM_unwind_info_once;
14723       type = SHT_PROGBITS;
14724     }
14725
14726   text_name = segment_name (text_seg);
14727   if (streq (text_name, ".text"))
14728     text_name = "";
14729
14730   if (strncmp (text_name, ".gnu.linkonce.t.",
14731                strlen (".gnu.linkonce.t.")) == 0)
14732     {
14733       prefix = prefix_once;
14734       text_name += strlen (".gnu.linkonce.t.");
14735     }
14736
14737   prefix_len = strlen (prefix);
14738   text_len = strlen (text_name);
14739   sec_name_len = prefix_len + text_len;
14740   sec_name = xmalloc (sec_name_len + 1);
14741   memcpy (sec_name, prefix, prefix_len);
14742   memcpy (sec_name + prefix_len, text_name, text_len);
14743   sec_name[prefix_len + text_len] = '\0';
14744
14745   flags = SHF_ALLOC;
14746   linkonce = 0;
14747   group_name = 0;
14748
14749   /* Handle COMDAT group.  */
14750   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
14751     {
14752       group_name = elf_group_name (text_seg);
14753       if (group_name == NULL)
14754         {
14755           as_bad ("Group section `%s' has no group signature",
14756                   segment_name (text_seg));
14757           ignore_rest_of_line ();
14758           return;
14759         }
14760       flags |= SHF_GROUP;
14761       linkonce = 1;
14762     }
14763
14764   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
14765
14766   /* Set the setion link for index tables.  */
14767   if (idx)
14768     elf_linked_to_section (now_seg) = text_seg;
14769 }
14770
14771
14772 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
14773    personality routine data.  Returns zero, or the index table value for
14774    and inline entry.  */
14775
14776 static valueT
14777 create_unwind_entry (int have_data)
14778 {
14779   int size;
14780   addressT where;
14781   char *ptr;
14782   /* The current word of data.  */
14783   valueT data;
14784   /* The number of bytes left in this word.  */
14785   int n;
14786
14787   finish_unwind_opcodes ();
14788
14789   /* Remember the current text section.  */
14790   unwind.saved_seg = now_seg;
14791   unwind.saved_subseg = now_subseg;
14792
14793   start_unwind_section (now_seg, 0);
14794
14795   if (unwind.personality_routine == NULL)
14796     {
14797       if (unwind.personality_index == -2)
14798         {
14799           if (have_data)
14800             as_bad (_("handerdata in cantunwind frame"));
14801           return 1; /* EXIDX_CANTUNWIND.  */
14802         }
14803
14804       /* Use a default personality routine if none is specified.  */
14805       if (unwind.personality_index == -1)
14806         {
14807           if (unwind.opcode_count > 3)
14808             unwind.personality_index = 1;
14809           else
14810             unwind.personality_index = 0;
14811         }
14812
14813       /* Space for the personality routine entry.  */
14814       if (unwind.personality_index == 0)
14815         {
14816           if (unwind.opcode_count > 3)
14817             as_bad (_("too many unwind opcodes for personality routine 0"));
14818
14819           if (!have_data)
14820             {
14821               /* All the data is inline in the index table.  */
14822               data = 0x80;
14823               n = 3;
14824               while (unwind.opcode_count > 0)
14825                 {
14826                   unwind.opcode_count--;
14827                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
14828                   n--;
14829                 }
14830
14831               /* Pad with "finish" opcodes.  */
14832               while (n--)
14833                 data = (data << 8) | 0xb0;
14834
14835               return data;
14836             }
14837           size = 0;
14838         }
14839       else
14840         /* We get two opcodes "free" in the first word.  */
14841         size = unwind.opcode_count - 2;
14842     }
14843   else
14844     /* An extra byte is required for the opcode count.  */
14845     size = unwind.opcode_count + 1;
14846
14847   size = (size + 3) >> 2;
14848   if (size > 0xff)
14849     as_bad (_("too many unwind opcodes"));
14850
14851   frag_align (2, 0, 0);
14852   record_alignment (now_seg, 2);
14853   unwind.table_entry = expr_build_dot ();
14854
14855   /* Allocate the table entry.  */
14856   ptr = frag_more ((size << 2) + 4);
14857   where = frag_now_fix () - ((size << 2) + 4);
14858
14859   switch (unwind.personality_index)
14860     {
14861     case -1:
14862       /* ??? Should this be a PLT generating relocation?  */
14863       /* Custom personality routine.  */
14864       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
14865                BFD_RELOC_ARM_PREL31);
14866
14867       where += 4;
14868       ptr += 4;
14869
14870       /* Set the first byte to the number of additional words.  */
14871       data = size - 1;
14872       n = 3;
14873       break;
14874
14875     /* ABI defined personality routines.  */
14876     case 0:
14877       /* Three opcodes bytes are packed into the first word.  */
14878       data = 0x80;
14879       n = 3;
14880       break;
14881
14882     case 1:
14883     case 2:
14884       /* The size and first two opcode bytes go in the first word.  */
14885       data = ((0x80 + unwind.personality_index) << 8) | size;
14886       n = 2;
14887       break;
14888
14889     default:
14890       /* Should never happen.  */
14891       abort ();
14892     }
14893
14894   /* Pack the opcodes into words (MSB first), reversing the list at the same
14895      time.  */
14896   while (unwind.opcode_count > 0)
14897     {
14898       if (n == 0)
14899         {
14900           md_number_to_chars (ptr, data, 4);
14901           ptr += 4;
14902           n = 4;
14903           data = 0;
14904         }
14905       unwind.opcode_count--;
14906       n--;
14907       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
14908     }
14909
14910   /* Finish off the last word.  */
14911   if (n < 4)
14912     {
14913       /* Pad with "finish" opcodes.  */
14914       while (n--)
14915         data = (data << 8) | 0xb0;
14916
14917       md_number_to_chars (ptr, data, 4);
14918     }
14919
14920   if (!have_data)
14921     {
14922       /* Add an empty descriptor if there is no user-specified data.   */
14923       ptr = frag_more (4);
14924       md_number_to_chars (ptr, 0, 4);
14925     }
14926
14927   return 0;
14928 }
14929
14930 /* Convert REGNAME to a DWARF-2 register number.  */
14931
14932 int
14933 tc_arm_regname_to_dw2regnum (const char *regname)
14934 {
14935   int reg = arm_reg_parse ((char **) &regname, REG_TYPE_RN, NULL);
14936
14937   if (reg == FAIL)
14938     return -1;
14939
14940   return reg;
14941 }
14942
14943 /* Initialize the DWARF-2 unwind information for this procedure.  */
14944
14945 void
14946 tc_arm_frame_initial_instructions (void)
14947 {
14948   cfi_add_CFA_def_cfa (REG_SP, 0);
14949 }
14950 #endif /* OBJ_ELF */
14951
14952
14953 /* MD interface: Symbol and relocation handling.  */
14954
14955 /* Return the address within the segment that a PC-relative fixup is
14956    relative to.  For ARM, PC-relative fixups applied to instructions
14957    are generally relative to the location of the fixup plus 8 bytes.
14958    Thumb branches are offset by 4, and Thumb loads relative to PC
14959    require special handling.  */
14960
14961 long
14962 md_pcrel_from_section (fixS * fixP, segT seg)
14963 {
14964   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
14965
14966   /* If this is pc-relative and we are going to emit a relocation
14967      then we just want to put out any pipeline compensation that the linker
14968      will need.  Otherwise we want to use the calculated base.  */
14969   if (fixP->fx_pcrel 
14970       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
14971           || arm_force_relocation (fixP)))
14972     base = 0;
14973
14974   switch (fixP->fx_r_type)
14975     {
14976       /* PC relative addressing on the Thumb is slightly odd as the
14977          bottom two bits of the PC are forced to zero for the
14978          calculation.  This happens *after* application of the
14979          pipeline offset.  However, Thumb adrl already adjusts for
14980          this, so we need not do it again.  */
14981     case BFD_RELOC_ARM_THUMB_ADD:
14982       return base & ~3;
14983
14984     case BFD_RELOC_ARM_THUMB_OFFSET:
14985     case BFD_RELOC_ARM_T32_OFFSET_IMM:
14986     case BFD_RELOC_ARM_T32_ADD_PC12:
14987     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
14988       return (base + 4) & ~3;
14989
14990       /* Thumb branches are simply offset by +4.  */
14991     case BFD_RELOC_THUMB_PCREL_BRANCH7:
14992     case BFD_RELOC_THUMB_PCREL_BRANCH9:
14993     case BFD_RELOC_THUMB_PCREL_BRANCH12:
14994     case BFD_RELOC_THUMB_PCREL_BRANCH20:
14995     case BFD_RELOC_THUMB_PCREL_BRANCH23:
14996     case BFD_RELOC_THUMB_PCREL_BRANCH25:
14997     case BFD_RELOC_THUMB_PCREL_BLX:
14998       return base + 4;
14999
15000       /* ARM mode branches are offset by +8.  However, the Windows CE
15001          loader expects the relocation not to take this into account.  */
15002     case BFD_RELOC_ARM_PCREL_BRANCH:
15003     case BFD_RELOC_ARM_PCREL_CALL:
15004     case BFD_RELOC_ARM_PCREL_JUMP:
15005     case BFD_RELOC_ARM_PCREL_BLX:
15006     case BFD_RELOC_ARM_PLT32:
15007 #ifdef TE_WINCE
15008       return base;
15009 #else
15010       return base + 8;
15011 #endif
15012
15013       /* ARM mode loads relative to PC are also offset by +8.  Unlike
15014          branches, the Windows CE loader *does* expect the relocation
15015          to take this into account.  */
15016     case BFD_RELOC_ARM_OFFSET_IMM:
15017     case BFD_RELOC_ARM_OFFSET_IMM8:
15018     case BFD_RELOC_ARM_HWLITERAL:
15019     case BFD_RELOC_ARM_LITERAL:
15020     case BFD_RELOC_ARM_CP_OFF_IMM:
15021       return base + 8;
15022
15023
15024       /* Other PC-relative relocations are un-offset.  */
15025     default:
15026       return base;
15027     }
15028 }
15029
15030 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
15031    Otherwise we have no need to default values of symbols.  */
15032
15033 symbolS *
15034 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
15035 {
15036 #ifdef OBJ_ELF
15037   if (name[0] == '_' && name[1] == 'G'
15038       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
15039     {
15040       if (!GOT_symbol)
15041         {
15042           if (symbol_find (name))
15043             as_bad ("GOT already in the symbol table");
15044
15045           GOT_symbol = symbol_new (name, undefined_section,
15046                                    (valueT) 0, & zero_address_frag);
15047         }
15048
15049       return GOT_symbol;
15050     }
15051 #endif
15052
15053   return 0;
15054 }
15055
15056 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
15057    computed as two separate immediate values, added together.  We
15058    already know that this value cannot be computed by just one ARM
15059    instruction.  */
15060
15061 static unsigned int
15062 validate_immediate_twopart (unsigned int   val,
15063                             unsigned int * highpart)
15064 {
15065   unsigned int a;
15066   unsigned int i;
15067
15068   for (i = 0; i < 32; i += 2)
15069     if (((a = rotate_left (val, i)) & 0xff) != 0)
15070       {
15071         if (a & 0xff00)
15072           {
15073             if (a & ~ 0xffff)
15074               continue;
15075             * highpart = (a  >> 8) | ((i + 24) << 7);
15076           }
15077         else if (a & 0xff0000)
15078           {
15079             if (a & 0xff000000)
15080               continue;
15081             * highpart = (a >> 16) | ((i + 16) << 7);
15082           }
15083         else
15084           {
15085             assert (a & 0xff000000);
15086             * highpart = (a >> 24) | ((i + 8) << 7);
15087           }
15088
15089         return (a & 0xff) | (i << 7);
15090       }
15091
15092   return FAIL;
15093 }
15094
15095 static int
15096 validate_offset_imm (unsigned int val, int hwse)
15097 {
15098   if ((hwse && val > 255) || val > 4095)
15099     return FAIL;
15100   return val;
15101 }
15102
15103 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
15104    negative immediate constant by altering the instruction.  A bit of
15105    a hack really.
15106         MOV <-> MVN
15107         AND <-> BIC
15108         ADC <-> SBC
15109         by inverting the second operand, and
15110         ADD <-> SUB
15111         CMP <-> CMN
15112         by negating the second operand.  */
15113
15114 static int
15115 negate_data_op (unsigned long * instruction,
15116                 unsigned long   value)
15117 {
15118   int op, new_inst;
15119   unsigned long negated, inverted;
15120
15121   negated = encode_arm_immediate (-value);
15122   inverted = encode_arm_immediate (~value);
15123
15124   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
15125   switch (op)
15126     {
15127       /* First negates.  */
15128     case OPCODE_SUB:             /* ADD <-> SUB  */
15129       new_inst = OPCODE_ADD;
15130       value = negated;
15131       break;
15132
15133     case OPCODE_ADD:
15134       new_inst = OPCODE_SUB;
15135       value = negated;
15136       break;
15137
15138     case OPCODE_CMP:             /* CMP <-> CMN  */
15139       new_inst = OPCODE_CMN;
15140       value = negated;
15141       break;
15142
15143     case OPCODE_CMN:
15144       new_inst = OPCODE_CMP;
15145       value = negated;
15146       break;
15147
15148       /* Now Inverted ops.  */
15149     case OPCODE_MOV:             /* MOV <-> MVN  */
15150       new_inst = OPCODE_MVN;
15151       value = inverted;
15152       break;
15153
15154     case OPCODE_MVN:
15155       new_inst = OPCODE_MOV;
15156       value = inverted;
15157       break;
15158
15159     case OPCODE_AND:             /* AND <-> BIC  */
15160       new_inst = OPCODE_BIC;
15161       value = inverted;
15162       break;
15163
15164     case OPCODE_BIC:
15165       new_inst = OPCODE_AND;
15166       value = inverted;
15167       break;
15168
15169     case OPCODE_ADC:              /* ADC <-> SBC  */
15170       new_inst = OPCODE_SBC;
15171       value = inverted;
15172       break;
15173
15174     case OPCODE_SBC:
15175       new_inst = OPCODE_ADC;
15176       value = inverted;
15177       break;
15178
15179       /* We cannot do anything.  */
15180     default:
15181       return FAIL;
15182     }
15183
15184   if (value == (unsigned) FAIL)
15185     return FAIL;
15186
15187   *instruction &= OPCODE_MASK;
15188   *instruction |= new_inst << DATA_OP_SHIFT;
15189   return value;
15190 }
15191
15192 /* Like negate_data_op, but for Thumb-2.   */
15193
15194 static unsigned int
15195 thumb32_negate_data_op (offsetT *instruction, offsetT value)
15196 {
15197   int op, new_inst;
15198   int rd;
15199   offsetT negated, inverted;
15200
15201   negated = encode_thumb32_immediate (-value);
15202   inverted = encode_thumb32_immediate (~value);
15203
15204   rd = (*instruction >> 8) & 0xf;
15205   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
15206   switch (op)
15207     {
15208       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
15209     case T2_OPCODE_SUB:
15210       new_inst = T2_OPCODE_ADD;
15211       value = negated;
15212       break;
15213
15214     case T2_OPCODE_ADD:
15215       new_inst = T2_OPCODE_SUB;
15216       value = negated;
15217       break;
15218
15219       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
15220     case T2_OPCODE_ORR:
15221       new_inst = T2_OPCODE_ORN;
15222       value = inverted;
15223       break;
15224
15225     case T2_OPCODE_ORN:
15226       new_inst = T2_OPCODE_ORR;
15227       value = inverted;
15228       break;
15229
15230       /* AND <-> BIC.  TST has no inverted equivalent.  */
15231     case T2_OPCODE_AND:
15232       new_inst = T2_OPCODE_BIC;
15233       if (rd == 15)
15234         value = FAIL;
15235       else
15236         value = inverted;
15237       break;
15238
15239     case T2_OPCODE_BIC:
15240       new_inst = T2_OPCODE_AND;
15241       value = inverted;
15242       break;
15243
15244       /* ADC <-> SBC  */
15245     case T2_OPCODE_ADC:
15246       new_inst = T2_OPCODE_SBC;
15247       value = inverted;
15248       break;
15249
15250     case T2_OPCODE_SBC:
15251       new_inst = T2_OPCODE_ADC;
15252       value = inverted;
15253       break;
15254
15255       /* We cannot do anything.  */
15256     default:
15257       return FAIL;
15258     }
15259
15260   if (value == FAIL)
15261     return FAIL;
15262
15263   *instruction &= T2_OPCODE_MASK;
15264   *instruction |= new_inst << T2_DATA_OP_SHIFT;
15265   return value;
15266 }
15267
15268 /* Read a 32-bit thumb instruction from buf.  */
15269 static unsigned long
15270 get_thumb32_insn (char * buf)
15271 {
15272   unsigned long insn;
15273   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
15274   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
15275
15276   return insn;
15277 }
15278
15279 void
15280 md_apply_fix (fixS *    fixP,
15281                valueT * valP,
15282                segT     seg)
15283 {
15284   offsetT        value = * valP;
15285   offsetT        newval;
15286   unsigned int   newimm;
15287   unsigned long  temp;
15288   int            sign;
15289   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
15290
15291   assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
15292
15293   /* Note whether this will delete the relocation.  */
15294   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
15295     fixP->fx_done = 1;
15296
15297   /* On a 64-bit host, silently truncate 'value' to 32 bits for
15298      consistency with the behavior on 32-bit hosts.  Remember value
15299      for emit_reloc.  */
15300   value &= 0xffffffff;
15301   value ^= 0x80000000;
15302   value -= 0x80000000; 
15303
15304   *valP = value;
15305   fixP->fx_addnumber = value;
15306
15307   /* Same treatment for fixP->fx_offset.  */
15308   fixP->fx_offset &= 0xffffffff;
15309   fixP->fx_offset ^= 0x80000000;
15310   fixP->fx_offset -= 0x80000000;
15311
15312   switch (fixP->fx_r_type)
15313     {
15314     case BFD_RELOC_NONE:
15315       /* This will need to go in the object file.  */
15316       fixP->fx_done = 0;
15317       break;
15318
15319     case BFD_RELOC_ARM_IMMEDIATE:
15320       /* We claim that this fixup has been processed here,
15321          even if in fact we generate an error because we do
15322          not have a reloc for it, so tc_gen_reloc will reject it.  */
15323       fixP->fx_done = 1;
15324
15325       if (fixP->fx_addsy
15326           && ! S_IS_DEFINED (fixP->fx_addsy))
15327         {
15328           as_bad_where (fixP->fx_file, fixP->fx_line,
15329                         _("undefined symbol %s used as an immediate value"),
15330                         S_GET_NAME (fixP->fx_addsy));
15331           break;
15332         }
15333
15334       newimm = encode_arm_immediate (value);
15335       temp = md_chars_to_number (buf, INSN_SIZE);
15336
15337       /* If the instruction will fail, see if we can fix things up by
15338          changing the opcode.  */
15339       if (newimm == (unsigned int) FAIL
15340           && (newimm = negate_data_op (&temp, value)) == (unsigned int) FAIL)
15341         {
15342           as_bad_where (fixP->fx_file, fixP->fx_line,
15343                         _("invalid constant (%lx) after fixup"),
15344                         (unsigned long) value);
15345           break;
15346         }
15347
15348       newimm |= (temp & 0xfffff000);
15349       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
15350       break;
15351
15352     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
15353       {
15354         unsigned int highpart = 0;
15355         unsigned int newinsn  = 0xe1a00000; /* nop.  */
15356
15357         newimm = encode_arm_immediate (value);
15358         temp = md_chars_to_number (buf, INSN_SIZE);
15359
15360         /* If the instruction will fail, see if we can fix things up by
15361            changing the opcode.  */
15362         if (newimm == (unsigned int) FAIL
15363             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
15364           {
15365             /* No ?  OK - try using two ADD instructions to generate
15366                the value.  */
15367             newimm = validate_immediate_twopart (value, & highpart);
15368
15369             /* Yes - then make sure that the second instruction is
15370                also an add.  */
15371             if (newimm != (unsigned int) FAIL)
15372               newinsn = temp;
15373             /* Still No ?  Try using a negated value.  */
15374             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
15375               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
15376             /* Otherwise - give up.  */
15377             else
15378               {
15379                 as_bad_where (fixP->fx_file, fixP->fx_line,
15380                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
15381                               (long) value);
15382                 break;
15383               }
15384
15385             /* Replace the first operand in the 2nd instruction (which
15386                is the PC) with the destination register.  We have
15387                already added in the PC in the first instruction and we
15388                do not want to do it again.  */
15389             newinsn &= ~ 0xf0000;
15390             newinsn |= ((newinsn & 0x0f000) << 4);
15391           }
15392
15393         newimm |= (temp & 0xfffff000);
15394         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
15395
15396         highpart |= (newinsn & 0xfffff000);
15397         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
15398       }
15399       break;
15400
15401     case BFD_RELOC_ARM_OFFSET_IMM:
15402       if (!fixP->fx_done && seg->use_rela_p)
15403         value = 0;
15404
15405     case BFD_RELOC_ARM_LITERAL:
15406       sign = value >= 0;
15407
15408       if (value < 0)
15409         value = - value;
15410
15411       if (validate_offset_imm (value, 0) == FAIL)
15412         {
15413           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
15414             as_bad_where (fixP->fx_file, fixP->fx_line,
15415                           _("invalid literal constant: pool needs to be closer"));
15416           else
15417             as_bad_where (fixP->fx_file, fixP->fx_line,
15418                           _("bad immediate value for offset (%ld)"),
15419                           (long) value);
15420           break;
15421         }
15422
15423       newval = md_chars_to_number (buf, INSN_SIZE);
15424       newval &= 0xff7ff000;
15425       newval |= value | (sign ? INDEX_UP : 0);
15426       md_number_to_chars (buf, newval, INSN_SIZE);
15427       break;
15428
15429     case BFD_RELOC_ARM_OFFSET_IMM8:
15430     case BFD_RELOC_ARM_HWLITERAL:
15431       sign = value >= 0;
15432
15433       if (value < 0)
15434         value = - value;
15435
15436       if (validate_offset_imm (value, 1) == FAIL)
15437         {
15438           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
15439             as_bad_where (fixP->fx_file, fixP->fx_line,
15440                           _("invalid literal constant: pool needs to be closer"));
15441           else
15442             as_bad (_("bad immediate value for half-word offset (%ld)"),
15443                     (long) value);
15444           break;
15445         }
15446
15447       newval = md_chars_to_number (buf, INSN_SIZE);
15448       newval &= 0xff7ff0f0;
15449       newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
15450       md_number_to_chars (buf, newval, INSN_SIZE);
15451       break;
15452
15453     case BFD_RELOC_ARM_T32_OFFSET_U8:
15454       if (value < 0 || value > 1020 || value % 4 != 0)
15455         as_bad_where (fixP->fx_file, fixP->fx_line,
15456                       _("bad immediate value for offset (%ld)"), (long) value);
15457       value /= 4;
15458
15459       newval = md_chars_to_number (buf+2, THUMB_SIZE);
15460       newval |= value;
15461       md_number_to_chars (buf+2, newval, THUMB_SIZE);
15462       break;
15463
15464     case BFD_RELOC_ARM_T32_OFFSET_IMM:
15465       /* This is a complicated relocation used for all varieties of Thumb32
15466          load/store instruction with immediate offset:
15467
15468          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
15469                                                    *4, optional writeback(W)
15470                                                    (doubleword load/store)
15471
15472          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
15473          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
15474          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
15475          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
15476          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
15477
15478          Uppercase letters indicate bits that are already encoded at
15479          this point.  Lowercase letters are our problem.  For the
15480          second block of instructions, the secondary opcode nybble
15481          (bits 8..11) is present, and bit 23 is zero, even if this is
15482          a PC-relative operation.  */
15483       newval = md_chars_to_number (buf, THUMB_SIZE);
15484       newval <<= 16;
15485       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
15486
15487       if ((newval & 0xf0000000) == 0xe0000000)
15488         {
15489           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
15490           if (value >= 0)
15491             newval |= (1 << 23);
15492           else
15493             value = -value;
15494           if (value % 4 != 0)
15495             {
15496               as_bad_where (fixP->fx_file, fixP->fx_line,
15497                             _("offset not a multiple of 4"));
15498               break;
15499             }
15500           value /= 4;
15501           if (value > 0xff)
15502             {
15503               as_bad_where (fixP->fx_file, fixP->fx_line,
15504                             _("offset out of range"));
15505               break;
15506             }
15507           newval &= ~0xff;
15508         }
15509       else if ((newval & 0x000f0000) == 0x000f0000)
15510         {
15511           /* PC-relative, 12-bit offset.  */
15512           if (value >= 0)
15513             newval |= (1 << 23);
15514           else
15515             value = -value;
15516           if (value > 0xfff)
15517             {
15518               as_bad_where (fixP->fx_file, fixP->fx_line,
15519                             _("offset out of range"));
15520               break;
15521             }
15522           newval &= ~0xfff;
15523         }
15524       else if ((newval & 0x00000100) == 0x00000100)
15525         {
15526           /* Writeback: 8-bit, +/- offset.  */
15527           if (value >= 0)
15528             newval |= (1 << 9);
15529           else
15530             value = -value;
15531           if (value > 0xff)
15532             {
15533               as_bad_where (fixP->fx_file, fixP->fx_line,
15534                             _("offset out of range"));
15535               break;
15536             }
15537           newval &= ~0xff;
15538         }
15539       else if ((newval & 0x00000f00) == 0x00000e00)
15540         {
15541           /* T-instruction: positive 8-bit offset.  */
15542           if (value < 0 || value > 0xff)
15543             {
15544               as_bad_where (fixP->fx_file, fixP->fx_line,
15545                             _("offset out of range"));
15546               break;
15547             }
15548           newval &= ~0xff;
15549           newval |= value;
15550         }
15551       else
15552         {
15553           /* Positive 12-bit or negative 8-bit offset.  */
15554           int limit;
15555           if (value >= 0)
15556             {
15557               newval |= (1 << 23);
15558               limit = 0xfff;
15559             }
15560           else
15561             {
15562               value = -value;
15563               limit = 0xff;
15564             }
15565           if (value > limit)
15566             {
15567               as_bad_where (fixP->fx_file, fixP->fx_line,
15568                             _("offset out of range"));
15569               break;
15570             }
15571           newval &= ~limit;
15572         }
15573
15574       newval |= value;
15575       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
15576       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
15577       break;
15578
15579     case BFD_RELOC_ARM_SHIFT_IMM:
15580       newval = md_chars_to_number (buf, INSN_SIZE);
15581       if (((unsigned long) value) > 32
15582           || (value == 32
15583               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
15584         {
15585           as_bad_where (fixP->fx_file, fixP->fx_line,
15586                         _("shift expression is too large"));
15587           break;
15588         }
15589
15590       if (value == 0)
15591         /* Shifts of zero must be done as lsl.  */
15592         newval &= ~0x60;
15593       else if (value == 32)
15594         value = 0;
15595       newval &= 0xfffff07f;
15596       newval |= (value & 0x1f) << 7;
15597       md_number_to_chars (buf, newval, INSN_SIZE);
15598       break;
15599
15600     case BFD_RELOC_ARM_T32_IMMEDIATE:
15601     case BFD_RELOC_ARM_T32_IMM12:
15602     case BFD_RELOC_ARM_T32_ADD_PC12:
15603       /* We claim that this fixup has been processed here,
15604          even if in fact we generate an error because we do
15605          not have a reloc for it, so tc_gen_reloc will reject it.  */
15606       fixP->fx_done = 1;
15607
15608       if (fixP->fx_addsy
15609           && ! S_IS_DEFINED (fixP->fx_addsy))
15610         {
15611           as_bad_where (fixP->fx_file, fixP->fx_line,
15612                         _("undefined symbol %s used as an immediate value"),
15613                         S_GET_NAME (fixP->fx_addsy));
15614           break;
15615         }
15616
15617       newval = md_chars_to_number (buf, THUMB_SIZE);
15618       newval <<= 16;
15619       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
15620
15621       /* FUTURE: Implement analogue of negate_data_op for T32.  */
15622       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE)
15623         {
15624           newimm = encode_thumb32_immediate (value);
15625           if (newimm == (unsigned int) FAIL)
15626             newimm = thumb32_negate_data_op (&newval, value);
15627         }
15628       else
15629         {
15630           /* 12 bit immediate for addw/subw.  */
15631           if (value < 0)
15632             {
15633               value = -value;
15634               newval ^= 0x00a00000;
15635             }
15636           if (value > 0xfff)
15637             newimm = (unsigned int) FAIL;
15638           else
15639             newimm = value;
15640         }
15641
15642       if (newimm == (unsigned int)FAIL)
15643         {
15644           as_bad_where (fixP->fx_file, fixP->fx_line,
15645                         _("invalid constant (%lx) after fixup"),
15646                         (unsigned long) value);
15647           break;
15648         }
15649
15650       newval |= (newimm & 0x800) << 15;
15651       newval |= (newimm & 0x700) << 4;
15652       newval |= (newimm & 0x0ff);
15653
15654       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
15655       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
15656       break;
15657
15658     case BFD_RELOC_ARM_SMC:
15659       if (((unsigned long) value) > 0xffff)
15660         as_bad_where (fixP->fx_file, fixP->fx_line,
15661                       _("invalid smc expression"));
15662       newval = md_chars_to_number (buf, INSN_SIZE);
15663       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
15664       md_number_to_chars (buf, newval, INSN_SIZE);
15665       break;
15666
15667     case BFD_RELOC_ARM_SWI:
15668       if (fixP->tc_fix_data != 0)
15669         {
15670           if (((unsigned long) value) > 0xff)
15671             as_bad_where (fixP->fx_file, fixP->fx_line,
15672                           _("invalid swi expression"));
15673           newval = md_chars_to_number (buf, THUMB_SIZE);
15674           newval |= value;
15675           md_number_to_chars (buf, newval, THUMB_SIZE);
15676         }
15677       else
15678         {
15679           if (((unsigned long) value) > 0x00ffffff)
15680             as_bad_where (fixP->fx_file, fixP->fx_line,
15681                           _("invalid swi expression"));
15682           newval = md_chars_to_number (buf, INSN_SIZE);
15683           newval |= value;
15684           md_number_to_chars (buf, newval, INSN_SIZE);
15685         }
15686       break;
15687
15688     case BFD_RELOC_ARM_MULTI:
15689       if (((unsigned long) value) > 0xffff)
15690         as_bad_where (fixP->fx_file, fixP->fx_line,
15691                       _("invalid expression in load/store multiple"));
15692       newval = value | md_chars_to_number (buf, INSN_SIZE);
15693       md_number_to_chars (buf, newval, INSN_SIZE);
15694       break;
15695
15696 #ifdef OBJ_ELF
15697     case BFD_RELOC_ARM_PCREL_CALL:
15698       newval = md_chars_to_number (buf, INSN_SIZE);
15699       if ((newval & 0xf0000000) == 0xf0000000)
15700         temp = 1;
15701       else
15702         temp = 3;
15703       goto arm_branch_common;
15704
15705     case BFD_RELOC_ARM_PCREL_JUMP:
15706     case BFD_RELOC_ARM_PLT32:
15707 #endif
15708     case BFD_RELOC_ARM_PCREL_BRANCH:
15709       temp = 3;
15710       goto arm_branch_common;
15711
15712     case BFD_RELOC_ARM_PCREL_BLX:
15713       temp = 1;
15714     arm_branch_common:
15715       /* We are going to store value (shifted right by two) in the
15716          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
15717          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
15718          also be be clear.  */
15719       if (value & temp)
15720         as_bad_where (fixP->fx_file, fixP->fx_line,
15721                       _("misaligned branch destination"));
15722       if ((value & (offsetT)0xfe000000) != (offsetT)0
15723           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
15724         as_bad_where (fixP->fx_file, fixP->fx_line,
15725                       _("branch out of range"));
15726
15727       if (fixP->fx_done || !seg->use_rela_p)
15728         {
15729           newval = md_chars_to_number (buf, INSN_SIZE);
15730           newval |= (value >> 2) & 0x00ffffff;
15731           /* Set the H bit on BLX instructions.  */
15732           if (temp == 1)
15733             {
15734               if (value & 2)
15735                 newval |= 0x01000000;
15736               else
15737                 newval &= ~0x01000000;
15738             }
15739           md_number_to_chars (buf, newval, INSN_SIZE);
15740         }
15741       break;
15742
15743     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CZB */
15744       /* CZB can only branch forward.  */
15745       if (value & ~0x7e)
15746         as_bad_where (fixP->fx_file, fixP->fx_line,
15747                       _("branch out of range"));
15748
15749       if (fixP->fx_done || !seg->use_rela_p)
15750         {
15751           newval = md_chars_to_number (buf, THUMB_SIZE);
15752           newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
15753           md_number_to_chars (buf, newval, THUMB_SIZE);
15754         }
15755       break;
15756
15757     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
15758       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
15759         as_bad_where (fixP->fx_file, fixP->fx_line,
15760                       _("branch out of range"));
15761
15762       if (fixP->fx_done || !seg->use_rela_p)
15763         {
15764           newval = md_chars_to_number (buf, THUMB_SIZE);
15765           newval |= (value & 0x1ff) >> 1;
15766           md_number_to_chars (buf, newval, THUMB_SIZE);
15767         }
15768       break;
15769
15770     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
15771       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
15772         as_bad_where (fixP->fx_file, fixP->fx_line,
15773                       _("branch out of range"));
15774
15775       if (fixP->fx_done || !seg->use_rela_p)
15776         {
15777           newval = md_chars_to_number (buf, THUMB_SIZE);
15778           newval |= (value & 0xfff) >> 1;
15779           md_number_to_chars (buf, newval, THUMB_SIZE);
15780         }
15781       break;
15782
15783     case BFD_RELOC_THUMB_PCREL_BRANCH20:
15784       if ((value & ~0x1fffff) && ((value & ~0x1fffff) != ~0x1fffff))
15785         as_bad_where (fixP->fx_file, fixP->fx_line,
15786                       _("conditional branch out of range"));
15787
15788       if (fixP->fx_done || !seg->use_rela_p)
15789         {
15790           offsetT newval2;
15791           addressT S, J1, J2, lo, hi;
15792
15793           S  = (value & 0x00100000) >> 20;
15794           J2 = (value & 0x00080000) >> 19;
15795           J1 = (value & 0x00040000) >> 18;
15796           hi = (value & 0x0003f000) >> 12;
15797           lo = (value & 0x00000ffe) >> 1;
15798
15799           newval   = md_chars_to_number (buf, THUMB_SIZE);
15800           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
15801           newval  |= (S << 10) | hi;
15802           newval2 |= (J1 << 13) | (J2 << 11) | lo;
15803           md_number_to_chars (buf, newval, THUMB_SIZE);
15804           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
15805         }
15806       break;
15807
15808     case BFD_RELOC_THUMB_PCREL_BLX:
15809     case BFD_RELOC_THUMB_PCREL_BRANCH23:
15810       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
15811         as_bad_where (fixP->fx_file, fixP->fx_line,
15812                       _("branch out of range"));
15813
15814       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
15815         /* For a BLX instruction, make sure that the relocation is rounded up
15816            to a word boundary.  This follows the semantics of the instruction
15817            which specifies that bit 1 of the target address will come from bit
15818            1 of the base address.  */
15819         value = (value + 1) & ~ 1;
15820
15821       if (fixP->fx_done || !seg->use_rela_p)
15822         {
15823           offsetT newval2;
15824
15825           newval   = md_chars_to_number (buf, THUMB_SIZE);
15826           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
15827           newval  |= (value & 0x7fffff) >> 12;
15828           newval2 |= (value & 0xfff) >> 1;
15829           md_number_to_chars (buf, newval, THUMB_SIZE);
15830           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
15831         }
15832       break;
15833
15834     case BFD_RELOC_THUMB_PCREL_BRANCH25:
15835       if ((value & ~0x1ffffff) && ((value & ~0x1ffffff) != ~0x1ffffff))
15836         as_bad_where (fixP->fx_file, fixP->fx_line,
15837                       _("branch out of range"));
15838
15839       if (fixP->fx_done || !seg->use_rela_p)
15840         {
15841           offsetT newval2;
15842           addressT S, I1, I2, lo, hi;
15843
15844           S  = (value & 0x01000000) >> 24;
15845           I1 = (value & 0x00800000) >> 23;
15846           I2 = (value & 0x00400000) >> 22;
15847           hi = (value & 0x003ff000) >> 12;
15848           lo = (value & 0x00000ffe) >> 1;
15849
15850           I1 = !(I1 ^ S);
15851           I2 = !(I2 ^ S);
15852
15853           newval   = md_chars_to_number (buf, THUMB_SIZE);
15854           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
15855           newval  |= (S << 10) | hi;
15856           newval2 |= (I1 << 13) | (I2 << 11) | lo;
15857           md_number_to_chars (buf, newval, THUMB_SIZE);
15858           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
15859         }
15860       break;
15861
15862     case BFD_RELOC_8:
15863       if (fixP->fx_done || !seg->use_rela_p)
15864         md_number_to_chars (buf, value, 1);
15865       break;
15866
15867     case BFD_RELOC_16:
15868       if (fixP->fx_done || !seg->use_rela_p)
15869         md_number_to_chars (buf, value, 2);
15870       break;
15871
15872 #ifdef OBJ_ELF
15873     case BFD_RELOC_ARM_TLS_GD32:
15874     case BFD_RELOC_ARM_TLS_LE32:
15875     case BFD_RELOC_ARM_TLS_IE32:
15876     case BFD_RELOC_ARM_TLS_LDM32:
15877     case BFD_RELOC_ARM_TLS_LDO32:
15878       S_SET_THREAD_LOCAL (fixP->fx_addsy);
15879       /* fall through */
15880
15881     case BFD_RELOC_ARM_GOT32:
15882     case BFD_RELOC_ARM_GOTOFF:
15883     case BFD_RELOC_ARM_TARGET2:
15884       if (fixP->fx_done || !seg->use_rela_p)
15885         md_number_to_chars (buf, 0, 4);
15886       break;
15887 #endif
15888
15889     case BFD_RELOC_RVA:
15890     case BFD_RELOC_32:
15891     case BFD_RELOC_ARM_TARGET1:
15892     case BFD_RELOC_ARM_ROSEGREL32:
15893     case BFD_RELOC_ARM_SBREL32:
15894     case BFD_RELOC_32_PCREL:
15895       if (fixP->fx_done || !seg->use_rela_p)
15896         md_number_to_chars (buf, value, 4);
15897       break;
15898
15899 #ifdef OBJ_ELF
15900     case BFD_RELOC_ARM_PREL31:
15901       if (fixP->fx_done || !seg->use_rela_p)
15902         {
15903           newval = md_chars_to_number (buf, 4) & 0x80000000;
15904           if ((value ^ (value >> 1)) & 0x40000000)
15905             {
15906               as_bad_where (fixP->fx_file, fixP->fx_line,
15907                             _("rel31 relocation overflow"));
15908             }
15909           newval |= value & 0x7fffffff;
15910           md_number_to_chars (buf, newval, 4);
15911         }
15912       break;
15913 #endif
15914
15915     case BFD_RELOC_ARM_CP_OFF_IMM:
15916     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
15917       if (value < -1023 || value > 1023 || (value & 3))
15918         as_bad_where (fixP->fx_file, fixP->fx_line,
15919                       _("co-processor offset out of range"));
15920     cp_off_common:
15921       sign = value >= 0;
15922       if (value < 0)
15923         value = -value;
15924       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
15925           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
15926         newval = md_chars_to_number (buf, INSN_SIZE);
15927       else
15928         newval = get_thumb32_insn (buf);
15929       newval &= 0xff7fff00;
15930       newval |= (value >> 2) | (sign ? INDEX_UP : 0);
15931       if (value == 0)
15932         newval &= ~WRITE_BACK;
15933       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
15934           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
15935         md_number_to_chars (buf, newval, INSN_SIZE);
15936       else
15937         put_thumb32_insn (buf, newval);
15938       break;
15939
15940     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
15941     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
15942       if (value < -255 || value > 255)
15943         as_bad_where (fixP->fx_file, fixP->fx_line,
15944                       _("co-processor offset out of range"));
15945       goto cp_off_common;
15946
15947     case BFD_RELOC_ARM_THUMB_OFFSET:
15948       newval = md_chars_to_number (buf, THUMB_SIZE);
15949       /* Exactly what ranges, and where the offset is inserted depends
15950          on the type of instruction, we can establish this from the
15951          top 4 bits.  */
15952       switch (newval >> 12)
15953         {
15954         case 4: /* PC load.  */
15955           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
15956              forced to zero for these loads; md_pcrel_from has already
15957              compensated for this.  */
15958           if (value & 3)
15959             as_bad_where (fixP->fx_file, fixP->fx_line,
15960                           _("invalid offset, target not word aligned (0x%08lX)"),
15961                           (((unsigned long) fixP->fx_frag->fr_address
15962                             + (unsigned long) fixP->fx_where) & ~3)
15963                           + (unsigned long) value);
15964
15965           if (value & ~0x3fc)
15966             as_bad_where (fixP->fx_file, fixP->fx_line,
15967                           _("invalid offset, value too big (0x%08lX)"),
15968                           (long) value);
15969
15970           newval |= value >> 2;
15971           break;
15972
15973         case 9: /* SP load/store.  */
15974           if (value & ~0x3fc)
15975             as_bad_where (fixP->fx_file, fixP->fx_line,
15976                           _("invalid offset, value too big (0x%08lX)"),
15977                           (long) value);
15978           newval |= value >> 2;
15979           break;
15980
15981         case 6: /* Word load/store.  */
15982           if (value & ~0x7c)
15983             as_bad_where (fixP->fx_file, fixP->fx_line,
15984                           _("invalid offset, value too big (0x%08lX)"),
15985                           (long) value);
15986           newval |= value << 4; /* 6 - 2.  */
15987           break;
15988
15989         case 7: /* Byte load/store.  */
15990           if (value & ~0x1f)
15991             as_bad_where (fixP->fx_file, fixP->fx_line,
15992                           _("invalid offset, value too big (0x%08lX)"),
15993                           (long) value);
15994           newval |= value << 6;
15995           break;
15996
15997         case 8: /* Halfword load/store.  */
15998           if (value & ~0x3e)
15999             as_bad_where (fixP->fx_file, fixP->fx_line,
16000                           _("invalid offset, value too big (0x%08lX)"),
16001                           (long) value);
16002           newval |= value << 5; /* 6 - 1.  */
16003           break;
16004
16005         default:
16006           as_bad_where (fixP->fx_file, fixP->fx_line,
16007                         "Unable to process relocation for thumb opcode: %lx",
16008                         (unsigned long) newval);
16009           break;
16010         }
16011       md_number_to_chars (buf, newval, THUMB_SIZE);
16012       break;
16013
16014     case BFD_RELOC_ARM_THUMB_ADD:
16015       /* This is a complicated relocation, since we use it for all of
16016          the following immediate relocations:
16017
16018             3bit ADD/SUB
16019             8bit ADD/SUB
16020             9bit ADD/SUB SP word-aligned
16021            10bit ADD PC/SP word-aligned
16022
16023          The type of instruction being processed is encoded in the
16024          instruction field:
16025
16026            0x8000  SUB
16027            0x00F0  Rd
16028            0x000F  Rs
16029       */
16030       newval = md_chars_to_number (buf, THUMB_SIZE);
16031       {
16032         int rd = (newval >> 4) & 0xf;
16033         int rs = newval & 0xf;
16034         int subtract = !!(newval & 0x8000);
16035
16036         /* Check for HI regs, only very restricted cases allowed:
16037            Adjusting SP, and using PC or SP to get an address.  */
16038         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
16039             || (rs > 7 && rs != REG_SP && rs != REG_PC))
16040           as_bad_where (fixP->fx_file, fixP->fx_line,
16041                         _("invalid Hi register with immediate"));
16042
16043         /* If value is negative, choose the opposite instruction.  */
16044         if (value < 0)
16045           {
16046             value = -value;
16047             subtract = !subtract;
16048             if (value < 0)
16049               as_bad_where (fixP->fx_file, fixP->fx_line,
16050                             _("immediate value out of range"));
16051           }
16052
16053         if (rd == REG_SP)
16054           {
16055             if (value & ~0x1fc)
16056               as_bad_where (fixP->fx_file, fixP->fx_line,
16057                             _("invalid immediate for stack address calculation"));
16058             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
16059             newval |= value >> 2;
16060           }
16061         else if (rs == REG_PC || rs == REG_SP)
16062           {
16063             if (subtract || value & ~0x3fc)
16064               as_bad_where (fixP->fx_file, fixP->fx_line,
16065                             _("invalid immediate for address calculation (value = 0x%08lX)"),
16066                             (unsigned long) value);
16067             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
16068             newval |= rd << 8;
16069             newval |= value >> 2;
16070           }
16071         else if (rs == rd)
16072           {
16073             if (value & ~0xff)
16074               as_bad_where (fixP->fx_file, fixP->fx_line,
16075                             _("immediate value out of range"));
16076             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
16077             newval |= (rd << 8) | value;
16078           }
16079         else
16080           {
16081             if (value & ~0x7)
16082               as_bad_where (fixP->fx_file, fixP->fx_line,
16083                             _("immediate value out of range"));
16084             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
16085             newval |= rd | (rs << 3) | (value << 6);
16086           }
16087       }
16088       md_number_to_chars (buf, newval, THUMB_SIZE);
16089       break;
16090
16091     case BFD_RELOC_ARM_THUMB_IMM:
16092       newval = md_chars_to_number (buf, THUMB_SIZE);
16093       if (value < 0 || value > 255)
16094         as_bad_where (fixP->fx_file, fixP->fx_line,
16095                       _("invalid immediate: %ld is too large"),
16096                       (long) value);
16097       newval |= value;
16098       md_number_to_chars (buf, newval, THUMB_SIZE);
16099       break;
16100
16101     case BFD_RELOC_ARM_THUMB_SHIFT:
16102       /* 5bit shift value (0..32).  LSL cannot take 32.  */
16103       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
16104       temp = newval & 0xf800;
16105       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
16106         as_bad_where (fixP->fx_file, fixP->fx_line,
16107                       _("invalid shift value: %ld"), (long) value);
16108       /* Shifts of zero must be encoded as LSL.  */
16109       if (value == 0)
16110         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
16111       /* Shifts of 32 are encoded as zero.  */
16112       else if (value == 32)
16113         value = 0;
16114       newval |= value << 6;
16115       md_number_to_chars (buf, newval, THUMB_SIZE);
16116       break;
16117
16118     case BFD_RELOC_VTABLE_INHERIT:
16119     case BFD_RELOC_VTABLE_ENTRY:
16120       fixP->fx_done = 0;
16121       return;
16122
16123     case BFD_RELOC_UNUSED:
16124     default:
16125       as_bad_where (fixP->fx_file, fixP->fx_line,
16126                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
16127     }
16128 }
16129
16130 /* Translate internal representation of relocation info to BFD target
16131    format.  */
16132
16133 arelent *
16134 tc_gen_reloc (asection *section, fixS *fixp)
16135 {
16136   arelent * reloc;
16137   bfd_reloc_code_real_type code;
16138
16139   reloc = xmalloc (sizeof (arelent));
16140
16141   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
16142   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
16143   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
16144
16145   if (fixp->fx_pcrel)
16146     {
16147       if (section->use_rela_p)
16148         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
16149       else
16150         fixp->fx_offset = reloc->address;
16151     }
16152   reloc->addend = fixp->fx_offset;
16153
16154   switch (fixp->fx_r_type)
16155     {
16156     case BFD_RELOC_8:
16157       if (fixp->fx_pcrel)
16158         {
16159           code = BFD_RELOC_8_PCREL;
16160           break;
16161         }
16162
16163     case BFD_RELOC_16:
16164       if (fixp->fx_pcrel)
16165         {
16166           code = BFD_RELOC_16_PCREL;
16167           break;
16168         }
16169
16170     case BFD_RELOC_32:
16171       if (fixp->fx_pcrel)
16172         {
16173           code = BFD_RELOC_32_PCREL;
16174           break;
16175         }
16176
16177     case BFD_RELOC_NONE:
16178     case BFD_RELOC_ARM_PCREL_BRANCH:
16179     case BFD_RELOC_ARM_PCREL_BLX:
16180     case BFD_RELOC_RVA:
16181     case BFD_RELOC_THUMB_PCREL_BRANCH7:
16182     case BFD_RELOC_THUMB_PCREL_BRANCH9:
16183     case BFD_RELOC_THUMB_PCREL_BRANCH12:
16184     case BFD_RELOC_THUMB_PCREL_BRANCH20:
16185     case BFD_RELOC_THUMB_PCREL_BRANCH23:
16186     case BFD_RELOC_THUMB_PCREL_BRANCH25:
16187     case BFD_RELOC_THUMB_PCREL_BLX:
16188     case BFD_RELOC_VTABLE_ENTRY:
16189     case BFD_RELOC_VTABLE_INHERIT:
16190       code = fixp->fx_r_type;
16191       break;
16192
16193     case BFD_RELOC_ARM_LITERAL:
16194     case BFD_RELOC_ARM_HWLITERAL:
16195       /* If this is called then the a literal has
16196          been referenced across a section boundary.  */
16197       as_bad_where (fixp->fx_file, fixp->fx_line,
16198                     _("literal referenced across section boundary"));
16199       return NULL;
16200
16201 #ifdef OBJ_ELF
16202     case BFD_RELOC_ARM_GOT32:
16203     case BFD_RELOC_ARM_GOTOFF:
16204     case BFD_RELOC_ARM_PLT32:
16205     case BFD_RELOC_ARM_TARGET1:
16206     case BFD_RELOC_ARM_ROSEGREL32:
16207     case BFD_RELOC_ARM_SBREL32:
16208     case BFD_RELOC_ARM_PREL31:
16209     case BFD_RELOC_ARM_TARGET2:
16210     case BFD_RELOC_ARM_TLS_LE32:
16211     case BFD_RELOC_ARM_TLS_LDO32:
16212     case BFD_RELOC_ARM_PCREL_CALL:
16213     case BFD_RELOC_ARM_PCREL_JUMP:
16214       code = fixp->fx_r_type;
16215       break;
16216
16217     case BFD_RELOC_ARM_TLS_GD32:
16218     case BFD_RELOC_ARM_TLS_IE32:
16219     case BFD_RELOC_ARM_TLS_LDM32:
16220       /* BFD will include the symbol's address in the addend.
16221          But we don't want that, so subtract it out again here.  */
16222       if (!S_IS_COMMON (fixp->fx_addsy))
16223         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
16224       code = fixp->fx_r_type;
16225       break;
16226 #endif
16227
16228     case BFD_RELOC_ARM_IMMEDIATE:
16229       as_bad_where (fixp->fx_file, fixp->fx_line,
16230                     _("internal relocation (type: IMMEDIATE) not fixed up"));
16231       return NULL;
16232
16233     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
16234       as_bad_where (fixp->fx_file, fixp->fx_line,
16235                     _("ADRL used for a symbol not defined in the same file"));
16236       return NULL;
16237
16238     case BFD_RELOC_ARM_OFFSET_IMM:
16239       if (section->use_rela_p)
16240         {
16241           code = fixp->fx_r_type;
16242           break;
16243         }
16244
16245       if (fixp->fx_addsy != NULL
16246           && !S_IS_DEFINED (fixp->fx_addsy)
16247           && S_IS_LOCAL (fixp->fx_addsy))
16248         {
16249           as_bad_where (fixp->fx_file, fixp->fx_line,
16250                         _("undefined local label `%s'"),
16251                         S_GET_NAME (fixp->fx_addsy));
16252           return NULL;
16253         }
16254
16255       as_bad_where (fixp->fx_file, fixp->fx_line,
16256                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
16257       return NULL;
16258
16259     default:
16260       {
16261         char * type;
16262
16263         switch (fixp->fx_r_type)
16264           {
16265           case BFD_RELOC_NONE:             type = "NONE";         break;
16266           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
16267           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
16268           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
16269           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
16270           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
16271           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
16272           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
16273           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
16274           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
16275           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
16276           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
16277           default:                         type = _("<unknown>"); break;
16278           }
16279         as_bad_where (fixp->fx_file, fixp->fx_line,
16280                       _("cannot represent %s relocation in this object file format"),
16281                       type);
16282         return NULL;
16283       }
16284     }
16285
16286 #ifdef OBJ_ELF
16287   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
16288       && GOT_symbol
16289       && fixp->fx_addsy == GOT_symbol)
16290     {
16291       code = BFD_RELOC_ARM_GOTPC;
16292       reloc->addend = fixp->fx_offset = reloc->address;
16293     }
16294 #endif
16295
16296   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
16297
16298   if (reloc->howto == NULL)
16299     {
16300       as_bad_where (fixp->fx_file, fixp->fx_line,
16301                     _("cannot represent %s relocation in this object file format"),
16302                     bfd_get_reloc_code_name (code));
16303       return NULL;
16304     }
16305
16306   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
16307      vtable entry to be used in the relocation's section offset.  */
16308   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16309     reloc->address = fixp->fx_offset;
16310
16311   return reloc;
16312 }
16313
16314 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
16315
16316 void
16317 cons_fix_new_arm (fragS *       frag,
16318                   int           where,
16319                   int           size,
16320                   expressionS * exp)
16321 {
16322   bfd_reloc_code_real_type type;
16323   int pcrel = 0;
16324
16325   /* Pick a reloc.
16326      FIXME: @@ Should look at CPU word size.  */
16327   switch (size)
16328     {
16329     case 1:
16330       type = BFD_RELOC_8;
16331       break;
16332     case 2:
16333       type = BFD_RELOC_16;
16334       break;
16335     case 4:
16336     default:
16337       type = BFD_RELOC_32;
16338       break;
16339     case 8:
16340       type = BFD_RELOC_64;
16341       break;
16342     }
16343
16344   fix_new_exp (frag, where, (int) size, exp, pcrel, type);
16345 }
16346
16347 #if defined OBJ_COFF || defined OBJ_ELF
16348 void
16349 arm_validate_fix (fixS * fixP)
16350 {
16351   /* If the destination of the branch is a defined symbol which does not have
16352      the THUMB_FUNC attribute, then we must be calling a function which has
16353      the (interfacearm) attribute.  We look for the Thumb entry point to that
16354      function and change the branch to refer to that function instead.  */
16355   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
16356       && fixP->fx_addsy != NULL
16357       && S_IS_DEFINED (fixP->fx_addsy)
16358       && ! THUMB_IS_FUNC (fixP->fx_addsy))
16359     {
16360       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
16361     }
16362 }
16363 #endif
16364
16365 int
16366 arm_force_relocation (struct fix * fixp)
16367 {
16368 #if defined (OBJ_COFF) && defined (TE_PE)
16369   if (fixp->fx_r_type == BFD_RELOC_RVA)
16370     return 1;
16371 #endif
16372
16373   /* Resolve these relocations even if the symbol is extern or weak.  */
16374   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
16375       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
16376       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
16377       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
16378       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
16379       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12)
16380     return 0;
16381
16382   return generic_force_reloc (fixp);
16383 }
16384
16385 #ifdef OBJ_COFF
16386 /* This is a little hack to help the gas/arm/adrl.s test.  It prevents
16387    local labels from being added to the output symbol table when they
16388    are used with the ADRL pseudo op.  The ADRL relocation should always
16389    be resolved before the binbary is emitted, so it is safe to say that
16390    it is adjustable.  */
16391
16392 bfd_boolean
16393 arm_fix_adjustable (fixS * fixP)
16394 {
16395   if (fixP->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE)
16396     return 1;
16397   return 0;
16398 }
16399 #endif
16400
16401 #ifdef OBJ_ELF
16402 /* Relocations against function names must be left unadjusted,
16403    so that the linker can use this information to generate interworking
16404    stubs.  The MIPS version of this function
16405    also prevents relocations that are mips-16 specific, but I do not
16406    know why it does this.
16407
16408    FIXME:
16409    There is one other problem that ought to be addressed here, but
16410    which currently is not:  Taking the address of a label (rather
16411    than a function) and then later jumping to that address.  Such
16412    addresses also ought to have their bottom bit set (assuming that
16413    they reside in Thumb code), but at the moment they will not.  */
16414
16415 bfd_boolean
16416 arm_fix_adjustable (fixS * fixP)
16417 {
16418   if (fixP->fx_addsy == NULL)
16419     return 1;
16420
16421   /* Preserve relocations against symbols with function type.  */
16422   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
16423     return 0;
16424
16425   if (THUMB_IS_FUNC (fixP->fx_addsy)
16426       && fixP->fx_subsy == NULL)
16427     return 0;
16428
16429   /* We need the symbol name for the VTABLE entries.  */
16430   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
16431       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16432     return 0;
16433
16434   /* Don't allow symbols to be discarded on GOT related relocs.  */
16435   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
16436       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
16437       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
16438       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
16439       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
16440       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
16441       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
16442       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
16443       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
16444     return 0;
16445
16446   return 1;
16447 }
16448
16449 const char *
16450 elf32_arm_target_format (void)
16451 {
16452 #ifdef TE_SYMBIAN
16453   return (target_big_endian
16454           ? "elf32-bigarm-symbian"
16455           : "elf32-littlearm-symbian");
16456 #elif defined (TE_VXWORKS)
16457   return (target_big_endian
16458           ? "elf32-bigarm-vxworks"
16459           : "elf32-littlearm-vxworks");
16460 #else
16461   if (target_big_endian)
16462     return "elf32-bigarm";
16463   else
16464     return "elf32-littlearm";
16465 #endif
16466 }
16467
16468 void
16469 armelf_frob_symbol (symbolS * symp,
16470                     int *     puntp)
16471 {
16472   elf_frob_symbol (symp, puntp);
16473 }
16474 #endif
16475
16476 /* MD interface: Finalization.  */
16477
16478 /* A good place to do this, although this was probably not intended
16479    for this kind of use.  We need to dump the literal pool before
16480    references are made to a null symbol pointer.  */
16481
16482 void
16483 arm_cleanup (void)
16484 {
16485   literal_pool * pool;
16486
16487   for (pool = list_of_pools; pool; pool = pool->next)
16488     {
16489       /* Put it at the end of the relevent section.  */
16490       subseg_set (pool->section, pool->sub_section);
16491 #ifdef OBJ_ELF
16492       arm_elf_change_section ();
16493 #endif
16494       s_ltorg (0);
16495     }
16496 }
16497
16498 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
16499    ARM ones.  */
16500
16501 void
16502 arm_adjust_symtab (void)
16503 {
16504 #ifdef OBJ_COFF
16505   symbolS * sym;
16506
16507   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
16508     {
16509       if (ARM_IS_THUMB (sym))
16510         {
16511           if (THUMB_IS_FUNC (sym))
16512             {
16513               /* Mark the symbol as a Thumb function.  */
16514               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
16515                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
16516                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
16517
16518               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
16519                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
16520               else
16521                 as_bad (_("%s: unexpected function type: %d"),
16522                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
16523             }
16524           else switch (S_GET_STORAGE_CLASS (sym))
16525             {
16526             case C_EXT:
16527               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
16528               break;
16529             case C_STAT:
16530               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
16531               break;
16532             case C_LABEL:
16533               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
16534               break;
16535             default:
16536               /* Do nothing.  */
16537               break;
16538             }
16539         }
16540
16541       if (ARM_IS_INTERWORK (sym))
16542         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
16543     }
16544 #endif
16545 #ifdef OBJ_ELF
16546   symbolS * sym;
16547   char      bind;
16548
16549   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
16550     {
16551       if (ARM_IS_THUMB (sym))
16552         {
16553           elf_symbol_type * elf_sym;
16554
16555           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
16556           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
16557
16558           if (! bfd_is_arm_mapping_symbol_name (elf_sym->symbol.name))
16559             {
16560               /* If it's a .thumb_func, declare it as so,
16561                  otherwise tag label as .code 16.  */
16562               if (THUMB_IS_FUNC (sym))
16563                 elf_sym->internal_elf_sym.st_info =
16564                   ELF_ST_INFO (bind, STT_ARM_TFUNC);
16565               else
16566                 elf_sym->internal_elf_sym.st_info =
16567                   ELF_ST_INFO (bind, STT_ARM_16BIT);
16568             }
16569         }
16570     }
16571 #endif
16572 }
16573
16574 /* MD interface: Initialization.  */
16575
16576 static void
16577 set_constant_flonums (void)
16578 {
16579   int i;
16580
16581   for (i = 0; i < NUM_FLOAT_VALS; i++)
16582     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
16583       abort ();
16584 }
16585
16586 void
16587 md_begin (void)
16588 {
16589   unsigned mach;
16590   unsigned int i;
16591
16592   if (   (arm_ops_hsh = hash_new ()) == NULL
16593       || (arm_cond_hsh = hash_new ()) == NULL
16594       || (arm_shift_hsh = hash_new ()) == NULL
16595       || (arm_psr_hsh = hash_new ()) == NULL
16596       || (arm_v7m_psr_hsh = hash_new ()) == NULL
16597       || (arm_reg_hsh = hash_new ()) == NULL
16598       || (arm_reloc_hsh = hash_new ()) == NULL
16599       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
16600     as_fatal (_("virtual memory exhausted"));
16601
16602   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
16603     hash_insert (arm_ops_hsh, insns[i].template, (PTR) (insns + i));
16604   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
16605     hash_insert (arm_cond_hsh, conds[i].template, (PTR) (conds + i));
16606   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
16607     hash_insert (arm_shift_hsh, shift_names[i].name, (PTR) (shift_names + i));
16608   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
16609     hash_insert (arm_psr_hsh, psrs[i].template, (PTR) (psrs + i));
16610   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
16611     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template, (PTR) (v7m_psrs + i));
16612   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
16613     hash_insert (arm_reg_hsh, reg_names[i].name, (PTR) (reg_names + i));
16614   for (i = 0;
16615        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
16616        i++)
16617     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template,
16618                  (PTR) (barrier_opt_names + i));
16619 #ifdef OBJ_ELF
16620   for (i = 0; i < sizeof (reloc_names) / sizeof (struct reloc_entry); i++)
16621     hash_insert (arm_reloc_hsh, reloc_names[i].name, (PTR) (reloc_names + i));
16622 #endif
16623
16624   set_constant_flonums ();
16625
16626   /* Set the cpu variant based on the command-line options.  We prefer
16627      -mcpu= over -march= if both are set (as for GCC); and we prefer
16628      -mfpu= over any other way of setting the floating point unit.
16629      Use of legacy options with new options are faulted.  */
16630   if (legacy_cpu)
16631     {
16632       if (mcpu_cpu_opt || march_cpu_opt)
16633         as_bad (_("use of old and new-style options to set CPU type"));
16634
16635       mcpu_cpu_opt = legacy_cpu;
16636     }
16637   else if (!mcpu_cpu_opt)
16638     mcpu_cpu_opt = march_cpu_opt;
16639
16640   if (legacy_fpu)
16641     {
16642       if (mfpu_opt)
16643         as_bad (_("use of old and new-style options to set FPU type"));
16644
16645       mfpu_opt = legacy_fpu;
16646     }
16647   else if (!mfpu_opt)
16648     {
16649 #if !(defined (TE_LINUX) || defined (TE_NetBSD) || defined (TE_VXWORKS))
16650       /* Some environments specify a default FPU.  If they don't, infer it
16651          from the processor.  */
16652       if (mcpu_fpu_opt)
16653         mfpu_opt = mcpu_fpu_opt;
16654       else
16655         mfpu_opt = march_fpu_opt;
16656 #else
16657       mfpu_opt = &fpu_default;
16658 #endif
16659     }
16660
16661   if (!mfpu_opt)
16662     {
16663       if (!mcpu_cpu_opt)
16664         mfpu_opt = &fpu_default;
16665       else if (ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
16666         mfpu_opt = &fpu_arch_vfp_v2;
16667       else
16668         mfpu_opt = &fpu_arch_fpa;
16669     }
16670
16671 #ifdef CPU_DEFAULT
16672   if (!mcpu_cpu_opt)
16673     {
16674       mcpu_cpu_opt = &cpu_default;
16675       selected_cpu = cpu_default;
16676     }
16677 #else
16678   if (mcpu_cpu_opt)
16679     selected_cpu = *mcpu_cpu_opt;
16680   else
16681     mcpu_cpu_opt = &arm_arch_any;
16682 #endif
16683
16684   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
16685
16686   arm_arch_used = thumb_arch_used = arm_arch_none;
16687
16688 #if defined OBJ_COFF || defined OBJ_ELF
16689   {
16690     unsigned int flags = 0;
16691
16692 #if defined OBJ_ELF
16693     flags = meabi_flags;
16694
16695     switch (meabi_flags)
16696       {
16697       case EF_ARM_EABI_UNKNOWN:
16698 #endif
16699         /* Set the flags in the private structure.  */
16700         if (uses_apcs_26)      flags |= F_APCS26;
16701         if (support_interwork) flags |= F_INTERWORK;
16702         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
16703         if (pic_code)          flags |= F_PIC;
16704         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
16705           flags |= F_SOFT_FLOAT;
16706
16707         switch (mfloat_abi_opt)
16708           {
16709           case ARM_FLOAT_ABI_SOFT:
16710           case ARM_FLOAT_ABI_SOFTFP:
16711             flags |= F_SOFT_FLOAT;
16712             break;
16713
16714           case ARM_FLOAT_ABI_HARD:
16715             if (flags & F_SOFT_FLOAT)
16716               as_bad (_("hard-float conflicts with specified fpu"));
16717             break;
16718           }
16719
16720         /* Using pure-endian doubles (even if soft-float).      */
16721         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
16722           flags |= F_VFP_FLOAT;
16723
16724 #if defined OBJ_ELF
16725         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
16726             flags |= EF_ARM_MAVERICK_FLOAT;
16727         break;
16728
16729       case EF_ARM_EABI_VER4:
16730       case EF_ARM_EABI_VER5:
16731         /* No additional flags to set.  */
16732         break;
16733
16734       default:
16735         abort ();
16736       }
16737 #endif
16738     bfd_set_private_flags (stdoutput, flags);
16739
16740     /* We have run out flags in the COFF header to encode the
16741        status of ATPCS support, so instead we create a dummy,
16742        empty, debug section called .arm.atpcs.  */
16743     if (atpcs)
16744       {
16745         asection * sec;
16746
16747         sec = bfd_make_section (stdoutput, ".arm.atpcs");
16748
16749         if (sec != NULL)
16750           {
16751             bfd_set_section_flags
16752               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
16753             bfd_set_section_size (stdoutput, sec, 0);
16754             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
16755           }
16756       }
16757   }
16758 #endif
16759
16760   /* Record the CPU type as well.  */
16761   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
16762     mach = bfd_mach_arm_iWMMXt;
16763   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
16764     mach = bfd_mach_arm_XScale;
16765   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
16766     mach = bfd_mach_arm_ep9312;
16767   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
16768     mach = bfd_mach_arm_5TE;
16769   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
16770     {
16771       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
16772         mach = bfd_mach_arm_5T;
16773       else
16774         mach = bfd_mach_arm_5;
16775     }
16776   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
16777     {
16778       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
16779         mach = bfd_mach_arm_4T;
16780       else
16781         mach = bfd_mach_arm_4;
16782     }
16783   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
16784     mach = bfd_mach_arm_3M;
16785   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
16786     mach = bfd_mach_arm_3;
16787   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
16788     mach = bfd_mach_arm_2a;
16789   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
16790     mach = bfd_mach_arm_2;
16791   else
16792     mach = bfd_mach_arm_unknown;
16793
16794   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
16795 }
16796
16797 /* Command line processing.  */
16798
16799 /* md_parse_option
16800       Invocation line includes a switch not recognized by the base assembler.
16801       See if it's a processor-specific option.
16802
16803       This routine is somewhat complicated by the need for backwards
16804       compatibility (since older releases of gcc can't be changed).
16805       The new options try to make the interface as compatible as
16806       possible with GCC.
16807
16808       New options (supported) are:
16809
16810               -mcpu=<cpu name>           Assemble for selected processor
16811               -march=<architecture name> Assemble for selected architecture
16812               -mfpu=<fpu architecture>   Assemble for selected FPU.
16813               -EB/-mbig-endian           Big-endian
16814               -EL/-mlittle-endian        Little-endian
16815               -k                         Generate PIC code
16816               -mthumb                    Start in Thumb mode
16817               -mthumb-interwork          Code supports ARM/Thumb interworking
16818
16819       For now we will also provide support for:
16820
16821               -mapcs-32                  32-bit Program counter
16822               -mapcs-26                  26-bit Program counter
16823               -macps-float               Floats passed in FP registers
16824               -mapcs-reentrant           Reentrant code
16825               -matpcs
16826       (sometime these will probably be replaced with -mapcs=<list of options>
16827       and -matpcs=<list of options>)
16828
16829       The remaining options are only supported for back-wards compatibility.
16830       Cpu variants, the arm part is optional:
16831               -m[arm]1                Currently not supported.
16832               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
16833               -m[arm]3                Arm 3 processor
16834               -m[arm]6[xx],           Arm 6 processors
16835               -m[arm]7[xx][t][[d]m]   Arm 7 processors
16836               -m[arm]8[10]            Arm 8 processors
16837               -m[arm]9[20][tdmi]      Arm 9 processors
16838               -mstrongarm[110[0]]     StrongARM processors
16839               -mxscale                XScale processors
16840               -m[arm]v[2345[t[e]]]    Arm architectures
16841               -mall                   All (except the ARM1)
16842       FP variants:
16843               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
16844               -mfpe-old               (No float load/store multiples)
16845               -mvfpxd                 VFP Single precision
16846               -mvfp                   All VFP
16847               -mno-fpu                Disable all floating point instructions
16848
16849       The following CPU names are recognized:
16850               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
16851               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
16852               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
16853               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
16854               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
16855               arm10t arm10e, arm1020t, arm1020e, arm10200e,
16856               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
16857
16858       */
16859
16860 const char * md_shortopts = "m:k";
16861
16862 #ifdef ARM_BI_ENDIAN
16863 #define OPTION_EB (OPTION_MD_BASE + 0)
16864 #define OPTION_EL (OPTION_MD_BASE + 1)
16865 #else
16866 #if TARGET_BYTES_BIG_ENDIAN
16867 #define OPTION_EB (OPTION_MD_BASE + 0)
16868 #else
16869 #define OPTION_EL (OPTION_MD_BASE + 1)
16870 #endif
16871 #endif
16872
16873 struct option md_longopts[] =
16874 {
16875 #ifdef OPTION_EB
16876   {"EB", no_argument, NULL, OPTION_EB},
16877 #endif
16878 #ifdef OPTION_EL
16879   {"EL", no_argument, NULL, OPTION_EL},
16880 #endif
16881   {NULL, no_argument, NULL, 0}
16882 };
16883
16884 size_t md_longopts_size = sizeof (md_longopts);
16885
16886 struct arm_option_table
16887 {
16888   char *option;         /* Option name to match.  */
16889   char *help;           /* Help information.  */
16890   int  *var;            /* Variable to change.  */
16891   int   value;          /* What to change it to.  */
16892   char *deprecated;     /* If non-null, print this message.  */
16893 };
16894
16895 struct arm_option_table arm_opts[] =
16896 {
16897   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
16898   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
16899   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
16900    &support_interwork, 1, NULL},
16901   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
16902   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
16903   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
16904    1, NULL},
16905   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
16906   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
16907   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
16908   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
16909    NULL},
16910
16911   /* These are recognized by the assembler, but have no affect on code.  */
16912   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
16913   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
16914   {NULL, NULL, NULL, 0, NULL}
16915 };
16916
16917 struct arm_legacy_option_table
16918 {
16919   char *option;                         /* Option name to match.  */
16920   const arm_feature_set **var;          /* Variable to change.  */
16921   const arm_feature_set value;          /* What to change it to.  */
16922   char *deprecated;                     /* If non-null, print this message.  */
16923 };
16924
16925 const struct arm_legacy_option_table arm_legacy_opts[] =
16926 {
16927   /* DON'T add any new processors to this list -- we want the whole list
16928      to go away...  Add them to the processors table instead.  */
16929   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
16930   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
16931   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
16932   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
16933   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
16934   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
16935   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
16936   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
16937   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
16938   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
16939   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
16940   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
16941   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
16942   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
16943   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
16944   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
16945   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
16946   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
16947   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
16948   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
16949   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
16950   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
16951   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
16952   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
16953   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
16954   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
16955   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
16956   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
16957   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
16958   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
16959   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
16960   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
16961   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
16962   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
16963   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
16964   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
16965   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
16966   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
16967   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
16968   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
16969   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
16970   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
16971   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
16972   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
16973   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
16974   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
16975   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
16976   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
16977   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
16978   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
16979   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
16980   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
16981   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
16982   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
16983   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
16984   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
16985   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
16986   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
16987   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
16988   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
16989   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
16990   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
16991   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
16992   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
16993   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
16994   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
16995   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
16996   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
16997   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
16998   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
16999    N_("use -mcpu=strongarm110")},
17000   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
17001    N_("use -mcpu=strongarm1100")},
17002   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
17003    N_("use -mcpu=strongarm1110")},
17004   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
17005   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
17006   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
17007
17008   /* Architecture variants -- don't add any more to this list either.  */
17009   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
17010   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
17011   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
17012   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
17013   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
17014   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
17015   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
17016   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
17017   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
17018   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
17019   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
17020   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
17021   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
17022   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
17023   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
17024   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
17025   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
17026   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
17027
17028   /* Floating point variants -- don't add any more to this list either.  */
17029   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
17030   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
17031   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
17032   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
17033    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
17034
17035   {NULL, NULL, ARM_ARCH_NONE, NULL}
17036 };
17037
17038 struct arm_cpu_option_table
17039 {
17040   char *name;
17041   const arm_feature_set value;
17042   /* For some CPUs we assume an FPU unless the user explicitly sets
17043      -mfpu=...  */
17044   const arm_feature_set default_fpu;
17045   /* The canonical name of the CPU, or NULL to use NAME converted to upper
17046      case.  */
17047   const char *canonical_name;
17048 };
17049
17050 /* This list should, at a minimum, contain all the cpu names
17051    recognized by GCC.  */
17052 static const struct arm_cpu_option_table arm_cpus[] =
17053 {
17054   {"all",               ARM_ANY,         FPU_ARCH_FPA,    NULL},
17055   {"arm1",              ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL},
17056   {"arm2",              ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL},
17057   {"arm250",            ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
17058   {"arm3",              ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
17059   {"arm6",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17060   {"arm60",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17061   {"arm600",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17062   {"arm610",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17063   {"arm620",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17064   {"arm7",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17065   {"arm7m",             ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
17066   {"arm7d",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17067   {"arm7dm",            ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
17068   {"arm7di",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17069   {"arm7dmi",           ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
17070   {"arm70",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17071   {"arm700",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17072   {"arm700i",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17073   {"arm710",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17074   {"arm710t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17075   {"arm720",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17076   {"arm720t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17077   {"arm740t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17078   {"arm710c",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17079   {"arm7100",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17080   {"arm7500",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17081   {"arm7500fe",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
17082   {"arm7t",             ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17083   {"arm7tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17084   {"arm7tdmi-s",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17085   {"arm8",              ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
17086   {"arm810",            ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
17087   {"strongarm",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
17088   {"strongarm1",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
17089   {"strongarm110",      ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
17090   {"strongarm1100",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
17091   {"strongarm1110",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
17092   {"arm9",              ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17093   {"arm920",            ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"},
17094   {"arm920t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17095   {"arm922t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17096   {"arm940t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17097   {"arm9tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
17098   /* For V5 or later processors we default to using VFP; but the user
17099      should really set the FPU type explicitly.  */
17100   {"arm9e-r0",          ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
17101   {"arm9e",             ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
17102   {"arm926ej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
17103   {"arm926ejs",         ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
17104   {"arm926ej-s",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
17105   {"arm946e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
17106   {"arm946e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"},
17107   {"arm946e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
17108   {"arm966e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
17109   {"arm966e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"},
17110   {"arm966e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
17111   {"arm968e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
17112   {"arm10t",            ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
17113   {"arm10tdmi",         ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
17114   {"arm10e",            ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
17115   {"arm1020",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"},
17116   {"arm1020t",          ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
17117   {"arm1020e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
17118   {"arm1022e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
17119   {"arm1026ejs",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM1026EJ-S"},
17120   {"arm1026ej-s",       ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
17121   {"arm1136js",         ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"},
17122   {"arm1136j-s",        ARM_ARCH_V6,     FPU_NONE,        NULL},
17123   {"arm1136jfs",        ARM_ARCH_V6,     FPU_ARCH_VFP_V2, "ARM1136JF-S"},
17124   {"arm1136jf-s",       ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL},
17125   {"mpcore",            ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, NULL},
17126   {"mpcorenovfp",       ARM_ARCH_V6K,    FPU_NONE,        NULL},
17127   {"arm1156t2-s",       ARM_ARCH_V6T2,   FPU_NONE,        NULL},
17128   {"arm1156t2f-s",      ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL},
17129   {"arm1176jz-s",       ARM_ARCH_V6ZK,   FPU_NONE,        NULL},
17130   {"arm1176jzf-s",      ARM_ARCH_V6ZK,   FPU_ARCH_VFP_V2, NULL},
17131   {"cortex-a8",         ARM_ARCH_V7A,    ARM_FEATURE(0, FPU_VFP_V3
17132                                                         | FPU_NEON_EXT_V1),
17133                                                           NULL},
17134   {"cortex-r4",         ARM_ARCH_V7R,    FPU_NONE,        NULL},
17135   {"cortex-m3",         ARM_ARCH_V7M,    FPU_NONE,        NULL},
17136   /* ??? XSCALE is really an architecture.  */
17137   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
17138   /* ??? iwmmxt is not a processor.  */
17139   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL},
17140   {"i80200",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
17141   /* Maverick */
17142   {"ep9312",    ARM_FEATURE(ARM_AEXT_V4T, ARM_CEXT_MAVERICK), FPU_ARCH_MAVERICK, "ARM920T"},
17143   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE, NULL}
17144 };
17145
17146 struct arm_arch_option_table
17147 {
17148   char *name;
17149   const arm_feature_set value;
17150   const arm_feature_set default_fpu;
17151 };
17152
17153 /* This list should, at a minimum, contain all the architecture names
17154    recognized by GCC.  */
17155 static const struct arm_arch_option_table arm_archs[] =
17156 {
17157   {"all",               ARM_ANY,         FPU_ARCH_FPA},
17158   {"armv1",             ARM_ARCH_V1,     FPU_ARCH_FPA},
17159   {"armv2",             ARM_ARCH_V2,     FPU_ARCH_FPA},
17160   {"armv2a",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
17161   {"armv2s",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
17162   {"armv3",             ARM_ARCH_V3,     FPU_ARCH_FPA},
17163   {"armv3m",            ARM_ARCH_V3M,    FPU_ARCH_FPA},
17164   {"armv4",             ARM_ARCH_V4,     FPU_ARCH_FPA},
17165   {"armv4xm",           ARM_ARCH_V4xM,   FPU_ARCH_FPA},
17166   {"armv4t",            ARM_ARCH_V4T,    FPU_ARCH_FPA},
17167   {"armv4txm",          ARM_ARCH_V4TxM,  FPU_ARCH_FPA},
17168   {"armv5",             ARM_ARCH_V5,     FPU_ARCH_VFP},
17169   {"armv5t",            ARM_ARCH_V5T,    FPU_ARCH_VFP},
17170   {"armv5txm",          ARM_ARCH_V5TxM,  FPU_ARCH_VFP},
17171   {"armv5te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP},
17172   {"armv5texp",         ARM_ARCH_V5TExP, FPU_ARCH_VFP},
17173   {"armv5tej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP},
17174   {"armv6",             ARM_ARCH_V6,     FPU_ARCH_VFP},
17175   {"armv6j",            ARM_ARCH_V6,     FPU_ARCH_VFP},
17176   {"armv6k",            ARM_ARCH_V6K,    FPU_ARCH_VFP},
17177   {"armv6z",            ARM_ARCH_V6Z,    FPU_ARCH_VFP},
17178   {"armv6zk",           ARM_ARCH_V6ZK,   FPU_ARCH_VFP},
17179   {"armv6t2",           ARM_ARCH_V6T2,   FPU_ARCH_VFP},
17180   {"armv6kt2",          ARM_ARCH_V6KT2,  FPU_ARCH_VFP},
17181   {"armv6zt2",          ARM_ARCH_V6ZT2,  FPU_ARCH_VFP},
17182   {"armv6zkt2",         ARM_ARCH_V6ZKT2, FPU_ARCH_VFP},
17183   {"armv7",             ARM_ARCH_V7,     FPU_ARCH_VFP},
17184   {"armv7a",            ARM_ARCH_V7A,    FPU_ARCH_VFP},
17185   {"armv7r",            ARM_ARCH_V7R,    FPU_ARCH_VFP},
17186   {"armv7m",            ARM_ARCH_V7M,    FPU_ARCH_VFP},
17187   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP},
17188   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP},
17189   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE}
17190 };
17191
17192 /* ISA extensions in the co-processor space.  */
17193 struct arm_option_cpu_value_table
17194 {
17195   char *name;
17196   const arm_feature_set value;
17197 };
17198
17199 static const struct arm_option_cpu_value_table arm_extensions[] =
17200 {
17201   {"maverick",          ARM_FEATURE (0, ARM_CEXT_MAVERICK)},
17202   {"xscale",            ARM_FEATURE (0, ARM_CEXT_XSCALE)},
17203   {"iwmmxt",            ARM_FEATURE (0, ARM_CEXT_IWMMXT)},
17204   {NULL,                ARM_ARCH_NONE}
17205 };
17206
17207 /* This list should, at a minimum, contain all the fpu names
17208    recognized by GCC.  */
17209 static const struct arm_option_cpu_value_table arm_fpus[] =
17210 {
17211   {"softfpa",           FPU_NONE},
17212   {"fpe",               FPU_ARCH_FPE},
17213   {"fpe2",              FPU_ARCH_FPE},
17214   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
17215   {"fpa",               FPU_ARCH_FPA},
17216   {"fpa10",             FPU_ARCH_FPA},
17217   {"fpa11",             FPU_ARCH_FPA},
17218   {"arm7500fe",         FPU_ARCH_FPA},
17219   {"softvfp",           FPU_ARCH_VFP},
17220   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
17221   {"vfp",               FPU_ARCH_VFP_V2},
17222   {"vfp9",              FPU_ARCH_VFP_V2},
17223   {"vfp3",              FPU_ARCH_VFP_V3},
17224   {"vfp10",             FPU_ARCH_VFP_V2},
17225   {"vfp10-r0",          FPU_ARCH_VFP_V1},
17226   {"vfpxd",             FPU_ARCH_VFP_V1xD},
17227   {"arm1020t",          FPU_ARCH_VFP_V1},
17228   {"arm1020e",          FPU_ARCH_VFP_V2},
17229   {"arm1136jfs",        FPU_ARCH_VFP_V2},
17230   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
17231   {"maverick",          FPU_ARCH_MAVERICK},
17232   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
17233   {NULL,                ARM_ARCH_NONE}
17234 };
17235
17236 struct arm_option_value_table
17237 {
17238   char *name;
17239   long value;
17240 };
17241
17242 static const struct arm_option_value_table arm_float_abis[] =
17243 {
17244   {"hard",      ARM_FLOAT_ABI_HARD},
17245   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
17246   {"soft",      ARM_FLOAT_ABI_SOFT},
17247   {NULL,        0}
17248 };
17249
17250 #ifdef OBJ_ELF
17251 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
17252 static const struct arm_option_value_table arm_eabis[] =
17253 {
17254   {"gnu",       EF_ARM_EABI_UNKNOWN},
17255   {"4",         EF_ARM_EABI_VER4},
17256   {"5",         EF_ARM_EABI_VER5},
17257   {NULL,        0}
17258 };
17259 #endif
17260
17261 struct arm_long_option_table
17262 {
17263   char * option;                /* Substring to match.  */
17264   char * help;                  /* Help information.  */
17265   int (* func) (char * subopt); /* Function to decode sub-option.  */
17266   char * deprecated;            /* If non-null, print this message.  */
17267 };
17268
17269 static int
17270 arm_parse_extension (char * str, const arm_feature_set **opt_p)
17271 {
17272   arm_feature_set *ext_set = xmalloc (sizeof (arm_feature_set));
17273
17274   /* Copy the feature set, so that we can modify it.  */
17275   *ext_set = **opt_p;
17276   *opt_p = ext_set;
17277
17278   while (str != NULL && *str != 0)
17279     {
17280       const struct arm_option_cpu_value_table * opt;
17281       char * ext;
17282       int optlen;
17283
17284       if (*str != '+')
17285         {
17286           as_bad (_("invalid architectural extension"));
17287           return 0;
17288         }
17289
17290       str++;
17291       ext = strchr (str, '+');
17292
17293       if (ext != NULL)
17294         optlen = ext - str;
17295       else
17296         optlen = strlen (str);
17297
17298       if (optlen == 0)
17299         {
17300           as_bad (_("missing architectural extension"));
17301           return 0;
17302         }
17303
17304       for (opt = arm_extensions; opt->name != NULL; opt++)
17305         if (strncmp (opt->name, str, optlen) == 0)
17306           {
17307             ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->value);
17308             break;
17309           }
17310
17311       if (opt->name == NULL)
17312         {
17313           as_bad (_("unknown architectural extnsion `%s'"), str);
17314           return 0;
17315         }
17316
17317       str = ext;
17318     };
17319
17320   return 1;
17321 }
17322
17323 static int
17324 arm_parse_cpu (char * str)
17325 {
17326   const struct arm_cpu_option_table * opt;
17327   char * ext = strchr (str, '+');
17328   int optlen;
17329
17330   if (ext != NULL)
17331     optlen = ext - str;
17332   else
17333     optlen = strlen (str);
17334
17335   if (optlen == 0)
17336     {
17337       as_bad (_("missing cpu name `%s'"), str);
17338       return 0;
17339     }
17340
17341   for (opt = arm_cpus; opt->name != NULL; opt++)
17342     if (strncmp (opt->name, str, optlen) == 0)
17343       {
17344         mcpu_cpu_opt = &opt->value;
17345         mcpu_fpu_opt = &opt->default_fpu;
17346         if (opt->canonical_name)
17347           strcpy(selected_cpu_name, opt->canonical_name);
17348         else
17349           {
17350             int i;
17351             for (i = 0; i < optlen; i++)
17352               selected_cpu_name[i] = TOUPPER (opt->name[i]);
17353             selected_cpu_name[i] = 0;
17354           }
17355
17356         if (ext != NULL)
17357           return arm_parse_extension (ext, &mcpu_cpu_opt);
17358
17359         return 1;
17360       }
17361
17362   as_bad (_("unknown cpu `%s'"), str);
17363   return 0;
17364 }
17365
17366 static int
17367 arm_parse_arch (char * str)
17368 {
17369   const struct arm_arch_option_table *opt;
17370   char *ext = strchr (str, '+');
17371   int optlen;
17372
17373   if (ext != NULL)
17374     optlen = ext - str;
17375   else
17376     optlen = strlen (str);
17377
17378   if (optlen == 0)
17379     {
17380       as_bad (_("missing architecture name `%s'"), str);
17381       return 0;
17382     }
17383
17384   for (opt = arm_archs; opt->name != NULL; opt++)
17385     if (streq (opt->name, str))
17386       {
17387         march_cpu_opt = &opt->value;
17388         march_fpu_opt = &opt->default_fpu;
17389         strcpy(selected_cpu_name, opt->name);
17390
17391         if (ext != NULL)
17392           return arm_parse_extension (ext, &march_cpu_opt);
17393
17394         return 1;
17395       }
17396
17397   as_bad (_("unknown architecture `%s'\n"), str);
17398   return 0;
17399 }
17400
17401 static int
17402 arm_parse_fpu (char * str)
17403 {
17404   const struct arm_option_cpu_value_table * opt;
17405
17406   for (opt = arm_fpus; opt->name != NULL; opt++)
17407     if (streq (opt->name, str))
17408       {
17409         mfpu_opt = &opt->value;
17410         return 1;
17411       }
17412
17413   as_bad (_("unknown floating point format `%s'\n"), str);
17414   return 0;
17415 }
17416
17417 static int
17418 arm_parse_float_abi (char * str)
17419 {
17420   const struct arm_option_value_table * opt;
17421
17422   for (opt = arm_float_abis; opt->name != NULL; opt++)
17423     if (streq (opt->name, str))
17424       {
17425         mfloat_abi_opt = opt->value;
17426         return 1;
17427       }
17428
17429   as_bad (_("unknown floating point abi `%s'\n"), str);
17430   return 0;
17431 }
17432
17433 #ifdef OBJ_ELF
17434 static int
17435 arm_parse_eabi (char * str)
17436 {
17437   const struct arm_option_value_table *opt;
17438
17439   for (opt = arm_eabis; opt->name != NULL; opt++)
17440     if (streq (opt->name, str))
17441       {
17442         meabi_flags = opt->value;
17443         return 1;
17444       }
17445   as_bad (_("unknown EABI `%s'\n"), str);
17446   return 0;
17447 }
17448 #endif
17449
17450 struct arm_long_option_table arm_long_opts[] =
17451 {
17452   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
17453    arm_parse_cpu, NULL},
17454   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
17455    arm_parse_arch, NULL},
17456   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
17457    arm_parse_fpu, NULL},
17458   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
17459    arm_parse_float_abi, NULL},
17460 #ifdef OBJ_ELF
17461   {"meabi=", N_("<ver>\t  assemble for eabi version <ver>"),
17462    arm_parse_eabi, NULL},
17463 #endif
17464   {NULL, NULL, 0, NULL}
17465 };
17466
17467 int
17468 md_parse_option (int c, char * arg)
17469 {
17470   struct arm_option_table *opt;
17471   const struct arm_legacy_option_table *fopt;
17472   struct arm_long_option_table *lopt;
17473
17474   switch (c)
17475     {
17476 #ifdef OPTION_EB
17477     case OPTION_EB:
17478       target_big_endian = 1;
17479       break;
17480 #endif
17481
17482 #ifdef OPTION_EL
17483     case OPTION_EL:
17484       target_big_endian = 0;
17485       break;
17486 #endif
17487
17488     case 'a':
17489       /* Listing option.  Just ignore these, we don't support additional
17490          ones.  */
17491       return 0;
17492
17493     default:
17494       for (opt = arm_opts; opt->option != NULL; opt++)
17495         {
17496           if (c == opt->option[0]
17497               && ((arg == NULL && opt->option[1] == 0)
17498                   || streq (arg, opt->option + 1)))
17499             {
17500 #if WARN_DEPRECATED
17501               /* If the option is deprecated, tell the user.  */
17502               if (opt->deprecated != NULL)
17503                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
17504                            arg ? arg : "", _(opt->deprecated));
17505 #endif
17506
17507               if (opt->var != NULL)
17508                 *opt->var = opt->value;
17509
17510               return 1;
17511             }
17512         }
17513
17514       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
17515         {
17516           if (c == fopt->option[0]
17517               && ((arg == NULL && fopt->option[1] == 0)
17518                   || streq (arg, fopt->option + 1)))
17519             {
17520 #if WARN_DEPRECATED
17521               /* If the option is deprecated, tell the user.  */
17522               if (fopt->deprecated != NULL)
17523                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
17524                            arg ? arg : "", _(fopt->deprecated));
17525 #endif
17526
17527               if (fopt->var != NULL)
17528                 *fopt->var = &fopt->value;
17529
17530               return 1;
17531             }
17532         }
17533
17534       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
17535         {
17536           /* These options are expected to have an argument.  */
17537           if (c == lopt->option[0]
17538               && arg != NULL
17539               && strncmp (arg, lopt->option + 1,
17540                           strlen (lopt->option + 1)) == 0)
17541             {
17542 #if WARN_DEPRECATED
17543               /* If the option is deprecated, tell the user.  */
17544               if (lopt->deprecated != NULL)
17545                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
17546                            _(lopt->deprecated));
17547 #endif
17548
17549               /* Call the sup-option parser.  */
17550               return lopt->func (arg + strlen (lopt->option) - 1);
17551             }
17552         }
17553
17554       return 0;
17555     }
17556
17557   return 1;
17558 }
17559
17560 void
17561 md_show_usage (FILE * fp)
17562 {
17563   struct arm_option_table *opt;
17564   struct arm_long_option_table *lopt;
17565
17566   fprintf (fp, _(" ARM-specific assembler options:\n"));
17567
17568   for (opt = arm_opts; opt->option != NULL; opt++)
17569     if (opt->help != NULL)
17570       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
17571
17572   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
17573     if (lopt->help != NULL)
17574       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
17575
17576 #ifdef OPTION_EB
17577   fprintf (fp, _("\
17578   -EB                     assemble code for a big-endian cpu\n"));
17579 #endif
17580
17581 #ifdef OPTION_EL
17582   fprintf (fp, _("\
17583   -EL                     assemble code for a little-endian cpu\n"));
17584 #endif
17585 }
17586
17587
17588 #ifdef OBJ_ELF
17589 typedef struct
17590 {
17591   int val;
17592   arm_feature_set flags;
17593 } cpu_arch_ver_table;
17594
17595 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
17596    least features first.  */
17597 static const cpu_arch_ver_table cpu_arch_ver[] =
17598 {
17599     {1, ARM_ARCH_V4},
17600     {2, ARM_ARCH_V4T},
17601     {3, ARM_ARCH_V5},
17602     {4, ARM_ARCH_V5TE},
17603     {5, ARM_ARCH_V5TEJ},
17604     {6, ARM_ARCH_V6},
17605     {7, ARM_ARCH_V6Z},
17606     {8, ARM_ARCH_V6K},
17607     {9, ARM_ARCH_V6T2},
17608     {10, ARM_ARCH_V7A},
17609     {10, ARM_ARCH_V7R},
17610     {10, ARM_ARCH_V7M},
17611     {0, ARM_ARCH_NONE}
17612 };
17613
17614 /* Set the public EABI object attributes.  */
17615 static void
17616 aeabi_set_public_attributes (void)
17617 {
17618   int arch;
17619   arm_feature_set flags;
17620   arm_feature_set tmp;
17621   const cpu_arch_ver_table *p;
17622
17623   /* Choose the architecture based on the capabilities of the requested cpu
17624      (if any) and/or the instructions actually used.  */
17625   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
17626   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
17627   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
17628     
17629   tmp = flags;
17630   arch = 0;
17631   for (p = cpu_arch_ver; p->val; p++)
17632     {
17633       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
17634         {
17635           arch = p->val;
17636           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
17637         }
17638     }
17639
17640   /* Tag_CPU_name.  */
17641   if (selected_cpu_name[0])
17642     {
17643       char *p;
17644
17645       p = selected_cpu_name;
17646       if (strncmp(p, "armv", 4) == 0)
17647         {
17648           int i;
17649           
17650           p += 4;
17651           for (i = 0; p[i]; i++)
17652             p[i] = TOUPPER (p[i]);
17653         }
17654       elf32_arm_add_eabi_attr_string (stdoutput, 5, p);
17655     }
17656   /* Tag_CPU_arch.  */
17657   elf32_arm_add_eabi_attr_int (stdoutput, 6, arch);
17658   /* Tag_CPU_arch_profile.  */
17659   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a))
17660     elf32_arm_add_eabi_attr_int (stdoutput, 7, 'A');
17661   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
17662     elf32_arm_add_eabi_attr_int (stdoutput, 7, 'R');
17663   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m))
17664     elf32_arm_add_eabi_attr_int (stdoutput, 7, 'M');
17665   /* Tag_ARM_ISA_use.  */
17666   if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_full))
17667     elf32_arm_add_eabi_attr_int (stdoutput, 8, 1);
17668   /* Tag_THUMB_ISA_use.  */
17669   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_full))
17670     elf32_arm_add_eabi_attr_int (stdoutput, 9,
17671         ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2) ? 2 : 1);
17672   /* Tag_VFP_arch.  */
17673   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_vfp_ext_v3)
17674       || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_vfp_ext_v3))
17675     elf32_arm_add_eabi_attr_int (stdoutput, 10, 3);
17676   else if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_vfp_ext_v2)
17677            || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_vfp_ext_v2))
17678     elf32_arm_add_eabi_attr_int (stdoutput, 10, 2);
17679   else if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_vfp_ext_v1)
17680            || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_vfp_ext_v1)
17681            || ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_vfp_ext_v1xd)
17682            || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_vfp_ext_v1xd))
17683     elf32_arm_add_eabi_attr_int (stdoutput, 10, 1);
17684   /* Tag_WMMX_arch.  */
17685   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_cext_iwmmxt)
17686       || ARM_CPU_HAS_FEATURE (arm_arch_used, arm_cext_iwmmxt))
17687     elf32_arm_add_eabi_attr_int (stdoutput, 11, 1);
17688   /* Tag_NEON_arch.  */
17689   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_neon_ext_v1)
17690       || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_neon_ext_v1))
17691     elf32_arm_add_eabi_attr_int (stdoutput, 12, 1);
17692 }
17693
17694 /* Add the .ARM.attributes section.  */
17695 void
17696 arm_md_end (void)
17697 {
17698   segT s;
17699   char *p;
17700   addressT addr;
17701   offsetT size;
17702   
17703   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
17704     return;
17705
17706   aeabi_set_public_attributes ();
17707   size = elf32_arm_eabi_attr_size (stdoutput);
17708   s = subseg_new (".ARM.attributes", 0);
17709   bfd_set_section_flags (stdoutput, s, SEC_READONLY | SEC_DATA);
17710   addr = frag_now_fix ();
17711   p = frag_more (size);
17712   elf32_arm_set_eabi_attr_contents (stdoutput, (bfd_byte *)p, size);
17713 }
17714
17715
17716 /* Parse a .cpu directive.  */
17717
17718 static void
17719 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
17720 {
17721   const struct arm_cpu_option_table *opt;
17722   char *name;
17723   char saved_char;
17724
17725   name = input_line_pointer;
17726   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
17727     input_line_pointer++;
17728   saved_char = *input_line_pointer;
17729   *input_line_pointer = 0;
17730
17731   /* Skip the first "all" entry.  */
17732   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
17733     if (streq (opt->name, name))
17734       {
17735         mcpu_cpu_opt = &opt->value;
17736         selected_cpu = opt->value;
17737         if (opt->canonical_name)
17738           strcpy(selected_cpu_name, opt->canonical_name);
17739         else
17740           {
17741             int i;
17742             for (i = 0; opt->name[i]; i++)
17743               selected_cpu_name[i] = TOUPPER (opt->name[i]);
17744             selected_cpu_name[i] = 0;
17745           }
17746         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
17747         *input_line_pointer = saved_char;
17748         demand_empty_rest_of_line ();
17749         return;
17750       }
17751   as_bad (_("unknown cpu `%s'"), name);
17752   *input_line_pointer = saved_char;
17753   ignore_rest_of_line ();
17754 }
17755
17756
17757 /* Parse a .arch directive.  */
17758
17759 static void
17760 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
17761 {
17762   const struct arm_arch_option_table *opt;
17763   char saved_char;
17764   char *name;
17765
17766   name = input_line_pointer;
17767   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
17768     input_line_pointer++;
17769   saved_char = *input_line_pointer;
17770   *input_line_pointer = 0;
17771
17772   /* Skip the first "all" entry.  */
17773   for (opt = arm_archs + 1; opt->name != NULL; opt++)
17774     if (streq (opt->name, name))
17775       {
17776         mcpu_cpu_opt = &opt->value;
17777         selected_cpu = opt->value;
17778         strcpy(selected_cpu_name, opt->name);
17779         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
17780         *input_line_pointer = saved_char;
17781         demand_empty_rest_of_line ();
17782         return;
17783       }
17784
17785   as_bad (_("unknown architecture `%s'\n"), name);
17786   *input_line_pointer = saved_char;
17787   ignore_rest_of_line ();
17788 }
17789
17790
17791 /* Parse a .fpu directive.  */
17792
17793 static void
17794 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
17795 {
17796   const struct arm_option_cpu_value_table *opt;
17797   char saved_char;
17798   char *name;
17799
17800   name = input_line_pointer;
17801   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
17802     input_line_pointer++;
17803   saved_char = *input_line_pointer;
17804   *input_line_pointer = 0;
17805   
17806   for (opt = arm_fpus; opt->name != NULL; opt++)
17807     if (streq (opt->name, name))
17808       {
17809         mfpu_opt = &opt->value;
17810         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
17811         *input_line_pointer = saved_char;
17812         demand_empty_rest_of_line ();
17813         return;
17814       }
17815
17816   as_bad (_("unknown floating point format `%s'\n"), name);
17817   *input_line_pointer = saved_char;
17818   ignore_rest_of_line ();
17819 }
17820 #endif /* OBJ_ELF */
17821