2006-09-19 Mark Shinwell <shinwell@codesourcery.com>
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005
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 #include <stdarg.h>
31 #define  NO_RELOC 0
32 #include "as.h"
33 #include "safe-ctype.h"
34
35 /* Need TARGET_CPU.  */
36 #include "config.h"
37 #include "subsegs.h"
38 #include "obstack.h"
39 #include "symbols.h"
40 #include "listing.h"
41
42 #include "opcode/arm.h"
43
44 #ifdef OBJ_ELF
45 #include "elf/arm.h"
46 #include "dwarf2dbg.h"
47 #include "dw2gencfi.h"
48 #endif
49
50 /* XXX Set this to 1 after the next binutils release.  */
51 #define WARN_DEPRECATED 0
52
53 #ifdef OBJ_ELF
54 /* Must be at least the size of the largest unwind opcode (currently two).  */
55 #define ARM_OPCODE_CHUNK_SIZE 8
56
57 /* This structure holds the unwinding state.  */
58
59 static struct
60 {
61   symbolS *       proc_start;
62   symbolS *       table_entry;
63   symbolS *       personality_routine;
64   int             personality_index;
65   /* The segment containing the function.  */
66   segT            saved_seg;
67   subsegT         saved_subseg;
68   /* Opcodes generated from this function.  */
69   unsigned char * opcodes;
70   int             opcode_count;
71   int             opcode_alloc;
72   /* The number of bytes pushed to the stack.  */
73   offsetT         frame_size;
74   /* We don't add stack adjustment opcodes immediately so that we can merge
75      multiple adjustments.  We can also omit the final adjustment
76      when using a frame pointer.  */
77   offsetT         pending_offset;
78   /* These two fields are set by both unwind_movsp and unwind_setfp.  They
79      hold the reg+offset to use when restoring sp from a frame pointer.  */
80   offsetT         fp_offset;
81   int             fp_reg;
82   /* Nonzero if an unwind_setfp directive has been seen.  */
83   unsigned        fp_used:1;
84   /* Nonzero if the last opcode restores sp from fp_reg.  */
85   unsigned        sp_restored:1;
86 } unwind;
87
88 /* Results from operand parsing worker functions.  */
89
90 typedef enum
91 {
92   PARSE_OPERAND_SUCCESS,
93   PARSE_OPERAND_FAIL,
94   PARSE_OPERAND_FAIL_NO_BACKTRACK
95 } parse_operand_result;
96
97 /* Bit N indicates that an R_ARM_NONE relocation has been output for
98    __aeabi_unwind_cpp_prN already if set. This enables dependencies to be
99    emitted only once per section, to save unnecessary bloat.  */
100 static unsigned int marked_pr_dependency = 0;
101
102 #endif /* OBJ_ELF */
103
104 enum arm_float_abi
105 {
106   ARM_FLOAT_ABI_HARD,
107   ARM_FLOAT_ABI_SOFTFP,
108   ARM_FLOAT_ABI_SOFT
109 };
110
111 /* Types of processor to assemble for.  */
112 #ifndef CPU_DEFAULT
113 #if defined __XSCALE__
114 #define CPU_DEFAULT     ARM_ARCH_XSCALE
115 #else
116 #if defined __thumb__
117 #define CPU_DEFAULT     ARM_ARCH_V5T
118 #endif
119 #endif
120 #endif
121
122 #ifndef FPU_DEFAULT
123 # ifdef TE_LINUX
124 #  define FPU_DEFAULT FPU_ARCH_FPA
125 # elif defined (TE_NetBSD)
126 #  ifdef OBJ_ELF
127 #   define FPU_DEFAULT FPU_ARCH_VFP     /* Soft-float, but VFP order.  */
128 #  else
129     /* Legacy a.out format.  */
130 #   define FPU_DEFAULT FPU_ARCH_FPA     /* Soft-float, but FPA order.  */
131 #  endif
132 # elif defined (TE_VXWORKS)
133 #  define FPU_DEFAULT FPU_ARCH_VFP      /* Soft-float, VFP order.  */
134 # else
135    /* For backwards compatibility, default to FPA.  */
136 #  define FPU_DEFAULT FPU_ARCH_FPA
137 # endif
138 #endif /* ifndef FPU_DEFAULT */
139
140 #define streq(a, b)           (strcmp (a, b) == 0)
141
142 static arm_feature_set cpu_variant;
143 static arm_feature_set arm_arch_used;
144 static arm_feature_set thumb_arch_used;
145
146 /* Flags stored in private area of BFD structure.  */
147 static int uses_apcs_26      = FALSE;
148 static int atpcs             = FALSE;
149 static int support_interwork = FALSE;
150 static int uses_apcs_float   = FALSE;
151 static int pic_code          = FALSE;
152
153 /* Variables that we set while parsing command-line options.  Once all
154    options have been read we re-process these values to set the real
155    assembly flags.  */
156 static const arm_feature_set *legacy_cpu = NULL;
157 static const arm_feature_set *legacy_fpu = NULL;
158
159 static const arm_feature_set *mcpu_cpu_opt = NULL;
160 static const arm_feature_set *mcpu_fpu_opt = NULL;
161 static const arm_feature_set *march_cpu_opt = NULL;
162 static const arm_feature_set *march_fpu_opt = NULL;
163 static const arm_feature_set *mfpu_opt = NULL;
164 static const arm_feature_set *object_arch = NULL;
165
166 /* Constants for known architecture features.  */
167 static const arm_feature_set fpu_default = FPU_DEFAULT;
168 static const arm_feature_set fpu_arch_vfp_v1 = FPU_ARCH_VFP_V1;
169 static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
170 static const arm_feature_set fpu_arch_vfp_v3 = FPU_ARCH_VFP_V3;
171 static const arm_feature_set fpu_arch_neon_v1 = FPU_ARCH_NEON_V1;
172 static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
173 static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
174 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
175 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
176
177 #ifdef CPU_DEFAULT
178 static const arm_feature_set cpu_default = CPU_DEFAULT;
179 #endif
180
181 static const arm_feature_set arm_ext_v1 = ARM_FEATURE (ARM_EXT_V1, 0);
182 static const arm_feature_set arm_ext_v2 = ARM_FEATURE (ARM_EXT_V1, 0);
183 static const arm_feature_set arm_ext_v2s = ARM_FEATURE (ARM_EXT_V2S, 0);
184 static const arm_feature_set arm_ext_v3 = ARM_FEATURE (ARM_EXT_V3, 0);
185 static const arm_feature_set arm_ext_v3m = ARM_FEATURE (ARM_EXT_V3M, 0);
186 static const arm_feature_set arm_ext_v4 = ARM_FEATURE (ARM_EXT_V4, 0);
187 static const arm_feature_set arm_ext_v4t = ARM_FEATURE (ARM_EXT_V4T, 0);
188 static const arm_feature_set arm_ext_v5 = ARM_FEATURE (ARM_EXT_V5, 0);
189 static const arm_feature_set arm_ext_v4t_5 =
190   ARM_FEATURE (ARM_EXT_V4T | ARM_EXT_V5, 0);
191 static const arm_feature_set arm_ext_v5t = ARM_FEATURE (ARM_EXT_V5T, 0);
192 static const arm_feature_set arm_ext_v5e = ARM_FEATURE (ARM_EXT_V5E, 0);
193 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE (ARM_EXT_V5ExP, 0);
194 static const arm_feature_set arm_ext_v5j = ARM_FEATURE (ARM_EXT_V5J, 0);
195 static const arm_feature_set arm_ext_v6 = ARM_FEATURE (ARM_EXT_V6, 0);
196 static const arm_feature_set arm_ext_v6k = ARM_FEATURE (ARM_EXT_V6K, 0);
197 static const arm_feature_set arm_ext_v6z = ARM_FEATURE (ARM_EXT_V6Z, 0);
198 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE (ARM_EXT_V6T2, 0);
199 static const arm_feature_set arm_ext_v6_notm = ARM_FEATURE (ARM_EXT_V6_NOTM, 0);
200 static const arm_feature_set arm_ext_div = ARM_FEATURE (ARM_EXT_DIV, 0);
201 static const arm_feature_set arm_ext_v7 = ARM_FEATURE (ARM_EXT_V7, 0);
202 static const arm_feature_set arm_ext_v7a = ARM_FEATURE (ARM_EXT_V7A, 0);
203 static const arm_feature_set arm_ext_v7r = ARM_FEATURE (ARM_EXT_V7R, 0);
204 static const arm_feature_set arm_ext_v7m = ARM_FEATURE (ARM_EXT_V7M, 0);
205
206 static const arm_feature_set arm_arch_any = ARM_ANY;
207 static const arm_feature_set arm_arch_full = ARM_FEATURE (-1, -1);
208 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
209 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
210
211 static const arm_feature_set arm_cext_iwmmxt2 =
212   ARM_FEATURE (0, ARM_CEXT_IWMMXT2);
213 static const arm_feature_set arm_cext_iwmmxt =
214   ARM_FEATURE (0, ARM_CEXT_IWMMXT);
215 static const arm_feature_set arm_cext_xscale =
216   ARM_FEATURE (0, ARM_CEXT_XSCALE);
217 static const arm_feature_set arm_cext_maverick =
218   ARM_FEATURE (0, ARM_CEXT_MAVERICK);
219 static const arm_feature_set fpu_fpa_ext_v1 = ARM_FEATURE (0, FPU_FPA_EXT_V1);
220 static const arm_feature_set fpu_fpa_ext_v2 = ARM_FEATURE (0, FPU_FPA_EXT_V2);
221 static const arm_feature_set fpu_vfp_ext_v1xd =
222   ARM_FEATURE (0, FPU_VFP_EXT_V1xD);
223 static const arm_feature_set fpu_vfp_ext_v1 = ARM_FEATURE (0, FPU_VFP_EXT_V1);
224 static const arm_feature_set fpu_vfp_ext_v2 = ARM_FEATURE (0, FPU_VFP_EXT_V2);
225 static const arm_feature_set fpu_vfp_ext_v3 = ARM_FEATURE (0, FPU_VFP_EXT_V3);
226 static const arm_feature_set fpu_neon_ext_v1 = ARM_FEATURE (0, FPU_NEON_EXT_V1);
227 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
228   ARM_FEATURE (0, FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
229
230 static int mfloat_abi_opt = -1;
231 /* Record user cpu selection for object attributes.  */
232 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
233 /* Must be long enough to hold any of the names in arm_cpus.  */
234 static char selected_cpu_name[16];
235 #ifdef OBJ_ELF
236 # ifdef EABI_DEFAULT
237 static int meabi_flags = EABI_DEFAULT;
238 # else
239 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
240 # endif
241 #endif
242
243 #ifdef OBJ_ELF
244 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
245 symbolS * GOT_symbol;
246 #endif
247
248 /* 0: assemble for ARM,
249    1: assemble for Thumb,
250    2: assemble for Thumb even though target CPU does not support thumb
251       instructions.  */
252 static int thumb_mode = 0;
253
254 /* If unified_syntax is true, we are processing the new unified
255    ARM/Thumb syntax.  Important differences from the old ARM mode:
256
257      - Immediate operands do not require a # prefix.
258      - Conditional affixes always appear at the end of the
259        instruction.  (For backward compatibility, those instructions
260        that formerly had them in the middle, continue to accept them
261        there.)
262      - The IT instruction may appear, and if it does is validated
263        against subsequent conditional affixes.  It does not generate
264        machine code.
265
266    Important differences from the old Thumb mode:
267
268      - Immediate operands do not require a # prefix.
269      - Most of the V6T2 instructions are only available in unified mode.
270      - The .N and .W suffixes are recognized and honored (it is an error
271        if they cannot be honored).
272      - All instructions set the flags if and only if they have an 's' affix.
273      - Conditional affixes may be used.  They are validated against
274        preceding IT instructions.  Unlike ARM mode, you cannot use a
275        conditional affix except in the scope of an IT instruction.  */
276
277 static bfd_boolean unified_syntax = FALSE;
278
279 enum neon_el_type
280 {
281   NT_invtype,
282   NT_untyped,
283   NT_integer,
284   NT_float,
285   NT_poly,
286   NT_signed,
287   NT_unsigned
288 };
289
290 struct neon_type_el
291 {
292   enum neon_el_type type;
293   unsigned size;
294 };
295
296 #define NEON_MAX_TYPE_ELS 4
297
298 struct neon_type
299 {
300   struct neon_type_el el[NEON_MAX_TYPE_ELS];
301   unsigned elems;
302 };
303
304 struct arm_it
305 {
306   const char *  error;
307   unsigned long instruction;
308   int           size;
309   int           size_req;
310   int           cond;
311   /* "uncond_value" is set to the value in place of the conditional field in
312      unconditional versions of the instruction, or -1 if nothing is
313      appropriate.  */
314   int           uncond_value;
315   struct neon_type vectype;
316   /* Set to the opcode if the instruction needs relaxation.
317      Zero if the instruction is not relaxed.  */
318   unsigned long relax;
319   struct
320   {
321     bfd_reloc_code_real_type type;
322     expressionS              exp;
323     int                      pc_rel;
324   } reloc;
325
326   struct
327   {
328     unsigned reg;
329     signed int imm;
330     struct neon_type_el vectype;
331     unsigned present    : 1;  /* Operand present.  */
332     unsigned isreg      : 1;  /* Operand was a register.  */
333     unsigned immisreg   : 1;  /* .imm field is a second register.  */
334     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
335     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
336     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
337        instructions. This allows us to disambiguate ARM <-> vector insns.  */
338     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
339     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
340     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
341     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
342     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
343     unsigned writeback  : 1;  /* Operand has trailing !  */
344     unsigned preind     : 1;  /* Preindexed address.  */
345     unsigned postind    : 1;  /* Postindexed address.  */
346     unsigned negative   : 1;  /* Index register was negated.  */
347     unsigned shifted    : 1;  /* Shift applied to operation.  */
348     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
349   } operands[6];
350 };
351
352 static struct arm_it inst;
353
354 #define NUM_FLOAT_VALS 8
355
356 const char * fp_const[] =
357 {
358   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
359 };
360
361 /* Number of littlenums required to hold an extended precision number.  */
362 #define MAX_LITTLENUMS 6
363
364 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
365
366 #define FAIL    (-1)
367 #define SUCCESS (0)
368
369 #define SUFF_S 1
370 #define SUFF_D 2
371 #define SUFF_E 3
372 #define SUFF_P 4
373
374 #define CP_T_X   0x00008000
375 #define CP_T_Y   0x00400000
376
377 #define CONDS_BIT        0x00100000
378 #define LOAD_BIT         0x00100000
379
380 #define DOUBLE_LOAD_FLAG 0x00000001
381
382 struct asm_cond
383 {
384   const char *  template;
385   unsigned long value;
386 };
387
388 #define COND_ALWAYS 0xE
389
390 struct asm_psr
391 {
392   const char *template;
393   unsigned long field;
394 };
395
396 struct asm_barrier_opt
397 {
398   const char *template;
399   unsigned long value;
400 };
401
402 /* The bit that distinguishes CPSR and SPSR.  */
403 #define SPSR_BIT   (1 << 22)
404
405 /* The individual PSR flag bits.  */
406 #define PSR_c   (1 << 16)
407 #define PSR_x   (1 << 17)
408 #define PSR_s   (1 << 18)
409 #define PSR_f   (1 << 19)
410
411 struct reloc_entry
412 {
413   char *name;
414   bfd_reloc_code_real_type reloc;
415 };
416
417 enum vfp_reg_pos
418 {
419   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
420   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
421 };
422
423 enum vfp_ldstm_type
424 {
425   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
426 };
427
428 /* Bits for DEFINED field in neon_typed_alias.  */
429 #define NTA_HASTYPE  1
430 #define NTA_HASINDEX 2
431
432 struct neon_typed_alias
433 {
434   unsigned char defined;
435   unsigned char index;
436   struct neon_type_el eltype;
437 };
438
439 /* ARM register categories.  This includes coprocessor numbers and various
440    architecture extensions' registers.  */
441 enum arm_reg_type
442 {
443   REG_TYPE_RN,
444   REG_TYPE_CP,
445   REG_TYPE_CN,
446   REG_TYPE_FN,
447   REG_TYPE_VFS,
448   REG_TYPE_VFD,
449   REG_TYPE_NQ,
450   REG_TYPE_VFSD,
451   REG_TYPE_NDQ,
452   REG_TYPE_NSDQ,
453   REG_TYPE_VFC,
454   REG_TYPE_MVF,
455   REG_TYPE_MVD,
456   REG_TYPE_MVFX,
457   REG_TYPE_MVDX,
458   REG_TYPE_MVAX,
459   REG_TYPE_DSPSC,
460   REG_TYPE_MMXWR,
461   REG_TYPE_MMXWC,
462   REG_TYPE_MMXWCG,
463   REG_TYPE_XSCALE,
464 };
465
466 /* Structure for a hash table entry for a register.
467    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
468    information which states whether a vector type or index is specified (for a
469    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
470 struct reg_entry
471 {
472   const char        *name;
473   unsigned char      number;
474   unsigned char      type;
475   unsigned char      builtin;
476   struct neon_typed_alias *neon;
477 };
478
479 /* Diagnostics used when we don't get a register of the expected type.  */
480 const char *const reg_expected_msgs[] =
481 {
482   N_("ARM register expected"),
483   N_("bad or missing co-processor number"),
484   N_("co-processor register expected"),
485   N_("FPA register expected"),
486   N_("VFP single precision register expected"),
487   N_("VFP/Neon double precision register expected"),
488   N_("Neon quad precision register expected"),
489   N_("VFP single or double precision register expected"),
490   N_("Neon double or quad precision register expected"),
491   N_("VFP single, double or Neon quad precision register expected"),
492   N_("VFP system register expected"),
493   N_("Maverick MVF register expected"),
494   N_("Maverick MVD register expected"),
495   N_("Maverick MVFX register expected"),
496   N_("Maverick MVDX register expected"),
497   N_("Maverick MVAX register expected"),
498   N_("Maverick DSPSC register expected"),
499   N_("iWMMXt data register expected"),
500   N_("iWMMXt control register expected"),
501   N_("iWMMXt scalar register expected"),
502   N_("XScale accumulator register expected"),
503 };
504
505 /* Some well known registers that we refer to directly elsewhere.  */
506 #define REG_SP  13
507 #define REG_LR  14
508 #define REG_PC  15
509
510 /* ARM instructions take 4bytes in the object file, Thumb instructions
511    take 2:  */
512 #define INSN_SIZE       4
513
514 struct asm_opcode
515 {
516   /* Basic string to match.  */
517   const char *template;
518
519   /* Parameters to instruction.  */
520   unsigned char operands[8];
521
522   /* Conditional tag - see opcode_lookup.  */
523   unsigned int tag : 4;
524
525   /* Basic instruction code.  */
526   unsigned int avalue : 28;
527
528   /* Thumb-format instruction code.  */
529   unsigned int tvalue;
530
531   /* Which architecture variant provides this instruction.  */
532   const arm_feature_set *avariant;
533   const arm_feature_set *tvariant;
534
535   /* Function to call to encode instruction in ARM format.  */
536   void (* aencode) (void);
537
538   /* Function to call to encode instruction in Thumb format.  */
539   void (* tencode) (void);
540 };
541
542 /* Defines for various bits that we will want to toggle.  */
543 #define INST_IMMEDIATE  0x02000000
544 #define OFFSET_REG      0x02000000
545 #define HWOFFSET_IMM    0x00400000
546 #define SHIFT_BY_REG    0x00000010
547 #define PRE_INDEX       0x01000000
548 #define INDEX_UP        0x00800000
549 #define WRITE_BACK      0x00200000
550 #define LDM_TYPE_2_OR_3 0x00400000
551
552 #define LITERAL_MASK    0xf000f000
553 #define OPCODE_MASK     0xfe1fffff
554 #define V4_STR_BIT      0x00000020
555
556 #define DATA_OP_SHIFT   21
557
558 #define T2_OPCODE_MASK  0xfe1fffff
559 #define T2_DATA_OP_SHIFT 21
560
561 /* Codes to distinguish the arithmetic instructions.  */
562 #define OPCODE_AND      0
563 #define OPCODE_EOR      1
564 #define OPCODE_SUB      2
565 #define OPCODE_RSB      3
566 #define OPCODE_ADD      4
567 #define OPCODE_ADC      5
568 #define OPCODE_SBC      6
569 #define OPCODE_RSC      7
570 #define OPCODE_TST      8
571 #define OPCODE_TEQ      9
572 #define OPCODE_CMP      10
573 #define OPCODE_CMN      11
574 #define OPCODE_ORR      12
575 #define OPCODE_MOV      13
576 #define OPCODE_BIC      14
577 #define OPCODE_MVN      15
578
579 #define T2_OPCODE_AND   0
580 #define T2_OPCODE_BIC   1
581 #define T2_OPCODE_ORR   2
582 #define T2_OPCODE_ORN   3
583 #define T2_OPCODE_EOR   4
584 #define T2_OPCODE_ADD   8
585 #define T2_OPCODE_ADC   10
586 #define T2_OPCODE_SBC   11
587 #define T2_OPCODE_SUB   13
588 #define T2_OPCODE_RSB   14
589
590 #define T_OPCODE_MUL 0x4340
591 #define T_OPCODE_TST 0x4200
592 #define T_OPCODE_CMN 0x42c0
593 #define T_OPCODE_NEG 0x4240
594 #define T_OPCODE_MVN 0x43c0
595
596 #define T_OPCODE_ADD_R3 0x1800
597 #define T_OPCODE_SUB_R3 0x1a00
598 #define T_OPCODE_ADD_HI 0x4400
599 #define T_OPCODE_ADD_ST 0xb000
600 #define T_OPCODE_SUB_ST 0xb080
601 #define T_OPCODE_ADD_SP 0xa800
602 #define T_OPCODE_ADD_PC 0xa000
603 #define T_OPCODE_ADD_I8 0x3000
604 #define T_OPCODE_SUB_I8 0x3800
605 #define T_OPCODE_ADD_I3 0x1c00
606 #define T_OPCODE_SUB_I3 0x1e00
607
608 #define T_OPCODE_ASR_R  0x4100
609 #define T_OPCODE_LSL_R  0x4080
610 #define T_OPCODE_LSR_R  0x40c0
611 #define T_OPCODE_ROR_R  0x41c0
612 #define T_OPCODE_ASR_I  0x1000
613 #define T_OPCODE_LSL_I  0x0000
614 #define T_OPCODE_LSR_I  0x0800
615
616 #define T_OPCODE_MOV_I8 0x2000
617 #define T_OPCODE_CMP_I8 0x2800
618 #define T_OPCODE_CMP_LR 0x4280
619 #define T_OPCODE_MOV_HR 0x4600
620 #define T_OPCODE_CMP_HR 0x4500
621
622 #define T_OPCODE_LDR_PC 0x4800
623 #define T_OPCODE_LDR_SP 0x9800
624 #define T_OPCODE_STR_SP 0x9000
625 #define T_OPCODE_LDR_IW 0x6800
626 #define T_OPCODE_STR_IW 0x6000
627 #define T_OPCODE_LDR_IH 0x8800
628 #define T_OPCODE_STR_IH 0x8000
629 #define T_OPCODE_LDR_IB 0x7800
630 #define T_OPCODE_STR_IB 0x7000
631 #define T_OPCODE_LDR_RW 0x5800
632 #define T_OPCODE_STR_RW 0x5000
633 #define T_OPCODE_LDR_RH 0x5a00
634 #define T_OPCODE_STR_RH 0x5200
635 #define T_OPCODE_LDR_RB 0x5c00
636 #define T_OPCODE_STR_RB 0x5400
637
638 #define T_OPCODE_PUSH   0xb400
639 #define T_OPCODE_POP    0xbc00
640
641 #define T_OPCODE_BRANCH 0xe000
642
643 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
644 #define THUMB_PP_PC_LR 0x0100
645 #define THUMB_LOAD_BIT 0x0800
646 #define THUMB2_LOAD_BIT 0x00100000
647
648 #define BAD_ARGS        _("bad arguments to instruction")
649 #define BAD_PC          _("r15 not allowed here")
650 #define BAD_COND        _("instruction cannot be conditional")
651 #define BAD_OVERLAP     _("registers may not be the same")
652 #define BAD_HIREG       _("lo register required")
653 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
654 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
655 #define BAD_BRANCH      _("branch must be last instruction in IT block")
656 #define BAD_NOT_IT      _("instruction not allowed in IT block")
657 #define BAD_FPU         _("selected FPU does not support instruction")
658
659 static struct hash_control *arm_ops_hsh;
660 static struct hash_control *arm_cond_hsh;
661 static struct hash_control *arm_shift_hsh;
662 static struct hash_control *arm_psr_hsh;
663 static struct hash_control *arm_v7m_psr_hsh;
664 static struct hash_control *arm_reg_hsh;
665 static struct hash_control *arm_reloc_hsh;
666 static struct hash_control *arm_barrier_opt_hsh;
667
668 /* Stuff needed to resolve the label ambiguity
669    As:
670      ...
671      label:   <insn>
672    may differ from:
673      ...
674      label:
675               <insn>
676 */
677
678 symbolS *  last_label_seen;
679 static int label_is_thumb_function_name = FALSE;
680 \f
681 /* Literal pool structure.  Held on a per-section
682    and per-sub-section basis.  */
683
684 #define MAX_LITERAL_POOL_SIZE 1024
685 typedef struct literal_pool
686 {
687   expressionS    literals [MAX_LITERAL_POOL_SIZE];
688   unsigned int   next_free_entry;
689   unsigned int   id;
690   symbolS *      symbol;
691   segT           section;
692   subsegT        sub_section;
693   struct literal_pool * next;
694 } literal_pool;
695
696 /* Pointer to a linked list of literal pools.  */
697 literal_pool * list_of_pools = NULL;
698
699 /* State variables for IT block handling.  */
700 static bfd_boolean current_it_mask = 0;
701 static int current_cc;
702
703 \f
704 /* Pure syntax.  */
705
706 /* This array holds the chars that always start a comment.  If the
707    pre-processor is disabled, these aren't very useful.  */
708 const char comment_chars[] = "@";
709
710 /* This array holds the chars that only start a comment at the beginning of
711    a line.  If the line seems to have the form '# 123 filename'
712    .line and .file directives will appear in the pre-processed output.  */
713 /* Note that input_file.c hand checks for '#' at the beginning of the
714    first line of the input file.  This is because the compiler outputs
715    #NO_APP at the beginning of its output.  */
716 /* Also note that comments like this one will always work.  */
717 const char line_comment_chars[] = "#";
718
719 const char line_separator_chars[] = ";";
720
721 /* Chars that can be used to separate mant
722    from exp in floating point numbers.  */
723 const char EXP_CHARS[] = "eE";
724
725 /* Chars that mean this number is a floating point constant.  */
726 /* As in 0f12.456  */
727 /* or    0d1.2345e12  */
728
729 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
730
731 /* Prefix characters that indicate the start of an immediate
732    value.  */
733 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
734
735 /* Separator character handling.  */
736
737 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
738
739 static inline int
740 skip_past_char (char ** str, char c)
741 {
742   if (**str == c)
743     {
744       (*str)++;
745       return SUCCESS;
746     }
747   else
748     return FAIL;
749 }
750 #define skip_past_comma(str) skip_past_char (str, ',')
751
752 /* Arithmetic expressions (possibly involving symbols).  */
753
754 /* Return TRUE if anything in the expression is a bignum.  */
755
756 static int
757 walk_no_bignums (symbolS * sp)
758 {
759   if (symbol_get_value_expression (sp)->X_op == O_big)
760     return 1;
761
762   if (symbol_get_value_expression (sp)->X_add_symbol)
763     {
764       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
765               || (symbol_get_value_expression (sp)->X_op_symbol
766                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
767     }
768
769   return 0;
770 }
771
772 static int in_my_get_expression = 0;
773
774 /* Third argument to my_get_expression.  */
775 #define GE_NO_PREFIX 0
776 #define GE_IMM_PREFIX 1
777 #define GE_OPT_PREFIX 2
778 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
779    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
780 #define GE_OPT_PREFIX_BIG 3
781
782 static int
783 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
784 {
785   char * save_in;
786   segT   seg;
787
788   /* In unified syntax, all prefixes are optional.  */
789   if (unified_syntax)
790     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
791                   : GE_OPT_PREFIX;
792
793   switch (prefix_mode)
794     {
795     case GE_NO_PREFIX: break;
796     case GE_IMM_PREFIX:
797       if (!is_immediate_prefix (**str))
798         {
799           inst.error = _("immediate expression requires a # prefix");
800           return FAIL;
801         }
802       (*str)++;
803       break;
804     case GE_OPT_PREFIX:
805     case GE_OPT_PREFIX_BIG:
806       if (is_immediate_prefix (**str))
807         (*str)++;
808       break;
809     default: abort ();
810     }
811
812   memset (ep, 0, sizeof (expressionS));
813
814   save_in = input_line_pointer;
815   input_line_pointer = *str;
816   in_my_get_expression = 1;
817   seg = expression (ep);
818   in_my_get_expression = 0;
819
820   if (ep->X_op == O_illegal)
821     {
822       /* We found a bad expression in md_operand().  */
823       *str = input_line_pointer;
824       input_line_pointer = save_in;
825       if (inst.error == NULL)
826         inst.error = _("bad expression");
827       return 1;
828     }
829
830 #ifdef OBJ_AOUT
831   if (seg != absolute_section
832       && seg != text_section
833       && seg != data_section
834       && seg != bss_section
835       && seg != undefined_section)
836     {
837       inst.error = _("bad segment");
838       *str = input_line_pointer;
839       input_line_pointer = save_in;
840       return 1;
841     }
842 #endif
843
844   /* Get rid of any bignums now, so that we don't generate an error for which
845      we can't establish a line number later on.  Big numbers are never valid
846      in instructions, which is where this routine is always called.  */
847   if (prefix_mode != GE_OPT_PREFIX_BIG
848       && (ep->X_op == O_big
849           || (ep->X_add_symbol
850               && (walk_no_bignums (ep->X_add_symbol)
851                   || (ep->X_op_symbol
852                       && walk_no_bignums (ep->X_op_symbol))))))
853     {
854       inst.error = _("invalid constant");
855       *str = input_line_pointer;
856       input_line_pointer = save_in;
857       return 1;
858     }
859
860   *str = input_line_pointer;
861   input_line_pointer = save_in;
862   return 0;
863 }
864
865 /* Turn a string in input_line_pointer into a floating point constant
866    of type TYPE, and store the appropriate bytes in *LITP.  The number
867    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
868    returned, or NULL on OK.
869
870    Note that fp constants aren't represent in the normal way on the ARM.
871    In big endian mode, things are as expected.  However, in little endian
872    mode fp constants are big-endian word-wise, and little-endian byte-wise
873    within the words.  For example, (double) 1.1 in big endian mode is
874    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
875    the byte sequence 99 99 f1 3f 9a 99 99 99.
876
877    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
878
879 char *
880 md_atof (int type, char * litP, int * sizeP)
881 {
882   int prec;
883   LITTLENUM_TYPE words[MAX_LITTLENUMS];
884   char *t;
885   int i;
886
887   switch (type)
888     {
889     case 'f':
890     case 'F':
891     case 's':
892     case 'S':
893       prec = 2;
894       break;
895
896     case 'd':
897     case 'D':
898     case 'r':
899     case 'R':
900       prec = 4;
901       break;
902
903     case 'x':
904     case 'X':
905       prec = 6;
906       break;
907
908     case 'p':
909     case 'P':
910       prec = 6;
911       break;
912
913     default:
914       *sizeP = 0;
915       return _("bad call to MD_ATOF()");
916     }
917
918   t = atof_ieee (input_line_pointer, type, words);
919   if (t)
920     input_line_pointer = t;
921   *sizeP = prec * 2;
922
923   if (target_big_endian)
924     {
925       for (i = 0; i < prec; i++)
926         {
927           md_number_to_chars (litP, (valueT) words[i], 2);
928           litP += 2;
929         }
930     }
931   else
932     {
933       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
934         for (i = prec - 1; i >= 0; i--)
935           {
936             md_number_to_chars (litP, (valueT) words[i], 2);
937             litP += 2;
938           }
939       else
940         /* For a 4 byte float the order of elements in `words' is 1 0.
941            For an 8 byte float the order is 1 0 3 2.  */
942         for (i = 0; i < prec; i += 2)
943           {
944             md_number_to_chars (litP, (valueT) words[i + 1], 2);
945             md_number_to_chars (litP + 2, (valueT) words[i], 2);
946             litP += 4;
947           }
948     }
949
950   return 0;
951 }
952
953 /* We handle all bad expressions here, so that we can report the faulty
954    instruction in the error message.  */
955 void
956 md_operand (expressionS * expr)
957 {
958   if (in_my_get_expression)
959     expr->X_op = O_illegal;
960 }
961
962 /* Immediate values.  */
963
964 /* Generic immediate-value read function for use in directives.
965    Accepts anything that 'expression' can fold to a constant.
966    *val receives the number.  */
967 #ifdef OBJ_ELF
968 static int
969 immediate_for_directive (int *val)
970 {
971   expressionS exp;
972   exp.X_op = O_illegal;
973
974   if (is_immediate_prefix (*input_line_pointer))
975     {
976       input_line_pointer++;
977       expression (&exp);
978     }
979
980   if (exp.X_op != O_constant)
981     {
982       as_bad (_("expected #constant"));
983       ignore_rest_of_line ();
984       return FAIL;
985     }
986   *val = exp.X_add_number;
987   return SUCCESS;
988 }
989 #endif
990
991 /* Register parsing.  */
992
993 /* Generic register parser.  CCP points to what should be the
994    beginning of a register name.  If it is indeed a valid register
995    name, advance CCP over it and return the reg_entry structure;
996    otherwise return NULL.  Does not issue diagnostics.  */
997
998 static struct reg_entry *
999 arm_reg_parse_multi (char **ccp)
1000 {
1001   char *start = *ccp;
1002   char *p;
1003   struct reg_entry *reg;
1004
1005 #ifdef REGISTER_PREFIX
1006   if (*start != REGISTER_PREFIX)
1007     return NULL;
1008   start++;
1009 #endif
1010 #ifdef OPTIONAL_REGISTER_PREFIX
1011   if (*start == OPTIONAL_REGISTER_PREFIX)
1012     start++;
1013 #endif
1014
1015   p = start;
1016   if (!ISALPHA (*p) || !is_name_beginner (*p))
1017     return NULL;
1018
1019   do
1020     p++;
1021   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1022
1023   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1024
1025   if (!reg)
1026     return NULL;
1027
1028   *ccp = p;
1029   return reg;
1030 }
1031
1032 static int
1033 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1034                     enum arm_reg_type type)
1035 {
1036   /* Alternative syntaxes are accepted for a few register classes.  */
1037   switch (type)
1038     {
1039     case REG_TYPE_MVF:
1040     case REG_TYPE_MVD:
1041     case REG_TYPE_MVFX:
1042     case REG_TYPE_MVDX:
1043       /* Generic coprocessor register names are allowed for these.  */
1044       if (reg && reg->type == REG_TYPE_CN)
1045         return reg->number;
1046       break;
1047
1048     case REG_TYPE_CP:
1049       /* For backward compatibility, a bare number is valid here.  */
1050       {
1051         unsigned long processor = strtoul (start, ccp, 10);
1052         if (*ccp != start && processor <= 15)
1053           return processor;
1054       }
1055
1056     case REG_TYPE_MMXWC:
1057       /* WC includes WCG.  ??? I'm not sure this is true for all
1058          instructions that take WC registers.  */
1059       if (reg && reg->type == REG_TYPE_MMXWCG)
1060         return reg->number;
1061       break;
1062
1063     default:
1064       break;
1065     }
1066
1067   return FAIL;
1068 }
1069
1070 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1071    return value is the register number or FAIL.  */
1072
1073 static int
1074 arm_reg_parse (char **ccp, enum arm_reg_type type)
1075 {
1076   char *start = *ccp;
1077   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1078   int ret;
1079
1080   /* Do not allow a scalar (reg+index) to parse as a register.  */
1081   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1082     return FAIL;
1083
1084   if (reg && reg->type == type)
1085     return reg->number;
1086
1087   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1088     return ret;
1089
1090   *ccp = start;
1091   return FAIL;
1092 }
1093
1094 /* Parse a Neon type specifier. *STR should point at the leading '.'
1095    character. Does no verification at this stage that the type fits the opcode
1096    properly. E.g.,
1097
1098      .i32.i32.s16
1099      .s32.f32
1100      .u16
1101
1102    Can all be legally parsed by this function.
1103
1104    Fills in neon_type struct pointer with parsed information, and updates STR
1105    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1106    type, FAIL if not.  */
1107
1108 static int
1109 parse_neon_type (struct neon_type *type, char **str)
1110 {
1111   char *ptr = *str;
1112
1113   if (type)
1114     type->elems = 0;
1115
1116   while (type->elems < NEON_MAX_TYPE_ELS)
1117     {
1118       enum neon_el_type thistype = NT_untyped;
1119       unsigned thissize = -1u;
1120
1121       if (*ptr != '.')
1122         break;
1123
1124       ptr++;
1125
1126       /* Just a size without an explicit type.  */
1127       if (ISDIGIT (*ptr))
1128         goto parsesize;
1129
1130       switch (TOLOWER (*ptr))
1131         {
1132         case 'i': thistype = NT_integer; break;
1133         case 'f': thistype = NT_float; break;
1134         case 'p': thistype = NT_poly; break;
1135         case 's': thistype = NT_signed; break;
1136         case 'u': thistype = NT_unsigned; break;
1137         case 'd':
1138           thistype = NT_float;
1139           thissize = 64;
1140           ptr++;
1141           goto done;
1142         default:
1143           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1144           return FAIL;
1145         }
1146
1147       ptr++;
1148
1149       /* .f is an abbreviation for .f32.  */
1150       if (thistype == NT_float && !ISDIGIT (*ptr))
1151         thissize = 32;
1152       else
1153         {
1154         parsesize:
1155           thissize = strtoul (ptr, &ptr, 10);
1156
1157           if (thissize != 8 && thissize != 16 && thissize != 32
1158               && thissize != 64)
1159             {
1160               as_bad (_("bad size %d in type specifier"), thissize);
1161               return FAIL;
1162             }
1163         }
1164
1165       done:
1166       if (type)
1167         {
1168           type->el[type->elems].type = thistype;
1169           type->el[type->elems].size = thissize;
1170           type->elems++;
1171         }
1172     }
1173
1174   /* Empty/missing type is not a successful parse.  */
1175   if (type->elems == 0)
1176     return FAIL;
1177
1178   *str = ptr;
1179
1180   return SUCCESS;
1181 }
1182
1183 /* Errors may be set multiple times during parsing or bit encoding
1184    (particularly in the Neon bits), but usually the earliest error which is set
1185    will be the most meaningful. Avoid overwriting it with later (cascading)
1186    errors by calling this function.  */
1187
1188 static void
1189 first_error (const char *err)
1190 {
1191   if (!inst.error)
1192     inst.error = err;
1193 }
1194
1195 /* Parse a single type, e.g. ".s32", leading period included.  */
1196 static int
1197 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1198 {
1199   char *str = *ccp;
1200   struct neon_type optype;
1201
1202   if (*str == '.')
1203     {
1204       if (parse_neon_type (&optype, &str) == SUCCESS)
1205         {
1206           if (optype.elems == 1)
1207             *vectype = optype.el[0];
1208           else
1209             {
1210               first_error (_("only one type should be specified for operand"));
1211               return FAIL;
1212             }
1213         }
1214       else
1215         {
1216           first_error (_("vector type expected"));
1217           return FAIL;
1218         }
1219     }
1220   else
1221     return FAIL;
1222   
1223   *ccp = str;
1224   
1225   return SUCCESS;
1226 }
1227
1228 /* Special meanings for indices (which have a range of 0-7), which will fit into
1229    a 4-bit integer.  */
1230
1231 #define NEON_ALL_LANES          15
1232 #define NEON_INTERLEAVE_LANES   14
1233
1234 /* Parse either a register or a scalar, with an optional type. Return the
1235    register number, and optionally fill in the actual type of the register
1236    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1237    type/index information in *TYPEINFO.  */
1238
1239 static int
1240 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1241                            enum arm_reg_type *rtype,
1242                            struct neon_typed_alias *typeinfo)
1243 {
1244   char *str = *ccp;
1245   struct reg_entry *reg = arm_reg_parse_multi (&str);
1246   struct neon_typed_alias atype;
1247   struct neon_type_el parsetype;
1248
1249   atype.defined = 0;
1250   atype.index = -1;
1251   atype.eltype.type = NT_invtype;
1252   atype.eltype.size = -1;
1253
1254   /* Try alternate syntax for some types of register. Note these are mutually
1255      exclusive with the Neon syntax extensions.  */
1256   if (reg == NULL)
1257     {
1258       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1259       if (altreg != FAIL)
1260         *ccp = str;
1261       if (typeinfo)
1262         *typeinfo = atype;
1263       return altreg;
1264     }
1265
1266   /* Undo polymorphism when a set of register types may be accepted.  */
1267   if ((type == REG_TYPE_NDQ
1268        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1269       || (type == REG_TYPE_VFSD
1270           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1271       || (type == REG_TYPE_NSDQ
1272           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1273               || reg->type == REG_TYPE_NQ)))
1274     type = reg->type;
1275
1276   if (type != reg->type)
1277     return FAIL;
1278
1279   if (reg->neon)
1280     atype = *reg->neon;
1281   
1282   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1283     {
1284       if ((atype.defined & NTA_HASTYPE) != 0)
1285         {
1286           first_error (_("can't redefine type for operand"));
1287           return FAIL;
1288         }
1289       atype.defined |= NTA_HASTYPE;
1290       atype.eltype = parsetype;
1291     }
1292     
1293   if (skip_past_char (&str, '[') == SUCCESS)
1294     {
1295       if (type != REG_TYPE_VFD)
1296         {
1297           first_error (_("only D registers may be indexed"));
1298           return FAIL;
1299         }
1300     
1301       if ((atype.defined & NTA_HASINDEX) != 0)
1302         {
1303           first_error (_("can't change index for operand"));
1304           return FAIL;
1305         }
1306
1307       atype.defined |= NTA_HASINDEX;
1308
1309       if (skip_past_char (&str, ']') == SUCCESS)
1310         atype.index = NEON_ALL_LANES;
1311       else
1312         {
1313           expressionS exp;
1314
1315           my_get_expression (&exp, &str, GE_NO_PREFIX);
1316
1317           if (exp.X_op != O_constant)
1318             {
1319               first_error (_("constant expression required"));
1320               return FAIL;
1321             }
1322
1323           if (skip_past_char (&str, ']') == FAIL)
1324             return FAIL;
1325
1326           atype.index = exp.X_add_number;
1327         }
1328     }
1329   
1330   if (typeinfo)
1331     *typeinfo = atype;
1332   
1333   if (rtype)
1334     *rtype = type;
1335   
1336   *ccp = str;
1337   
1338   return reg->number;
1339 }
1340
1341 /* Like arm_reg_parse, but allow allow the following extra features:
1342     - If RTYPE is non-zero, return the (possibly restricted) type of the
1343       register (e.g. Neon double or quad reg when either has been requested).
1344     - If this is a Neon vector type with additional type information, fill
1345       in the struct pointed to by VECTYPE (if non-NULL).
1346    This function will fault on encountering a scalar.
1347 */
1348
1349 static int
1350 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1351                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1352 {
1353   struct neon_typed_alias atype;
1354   char *str = *ccp;
1355   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1356
1357   if (reg == FAIL)
1358     return FAIL;
1359
1360   /* Do not allow a scalar (reg+index) to parse as a register.  */
1361   if ((atype.defined & NTA_HASINDEX) != 0)
1362     {
1363       first_error (_("register operand expected, but got scalar"));
1364       return FAIL;
1365     }
1366
1367   if (vectype)
1368     *vectype = atype.eltype;
1369
1370   *ccp = str;
1371
1372   return reg;
1373 }
1374
1375 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1376 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1377
1378 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1379    have enough information to be able to do a good job bounds-checking. So, we
1380    just do easy checks here, and do further checks later.  */
1381
1382 static int
1383 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1384 {
1385   int reg;
1386   char *str = *ccp;
1387   struct neon_typed_alias atype;
1388   
1389   reg = parse_typed_reg_or_scalar (&str, REG_TYPE_VFD, NULL, &atype);
1390   
1391   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1392     return FAIL;
1393   
1394   if (atype.index == NEON_ALL_LANES)
1395     {
1396       first_error (_("scalar must have an index"));
1397       return FAIL;
1398     }
1399   else if (atype.index >= 64 / elsize)
1400     {
1401       first_error (_("scalar index out of range"));
1402       return FAIL;
1403     }
1404   
1405   if (type)
1406     *type = atype.eltype;
1407   
1408   *ccp = str;
1409   
1410   return reg * 16 + atype.index;
1411 }
1412
1413 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1414 static long
1415 parse_reg_list (char ** strp)
1416 {
1417   char * str = * strp;
1418   long   range = 0;
1419   int    another_range;
1420
1421   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1422   do
1423     {
1424       another_range = 0;
1425
1426       if (*str == '{')
1427         {
1428           int in_range = 0;
1429           int cur_reg = -1;
1430
1431           str++;
1432           do
1433             {
1434               int reg;
1435
1436               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1437                 {
1438                   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
1439                   return FAIL;
1440                 }
1441
1442               if (in_range)
1443                 {
1444                   int i;
1445
1446                   if (reg <= cur_reg)
1447                     {
1448                       first_error (_("bad range in register list"));
1449                       return FAIL;
1450                     }
1451
1452                   for (i = cur_reg + 1; i < reg; i++)
1453                     {
1454                       if (range & (1 << i))
1455                         as_tsktsk
1456                           (_("Warning: duplicated register (r%d) in register list"),
1457                            i);
1458                       else
1459                         range |= 1 << i;
1460                     }
1461                   in_range = 0;
1462                 }
1463
1464               if (range & (1 << reg))
1465                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1466                            reg);
1467               else if (reg <= cur_reg)
1468                 as_tsktsk (_("Warning: register range not in ascending order"));
1469
1470               range |= 1 << reg;
1471               cur_reg = reg;
1472             }
1473           while (skip_past_comma (&str) != FAIL
1474                  || (in_range = 1, *str++ == '-'));
1475           str--;
1476
1477           if (*str++ != '}')
1478             {
1479               first_error (_("missing `}'"));
1480               return FAIL;
1481             }
1482         }
1483       else
1484         {
1485           expressionS expr;
1486
1487           if (my_get_expression (&expr, &str, GE_NO_PREFIX))
1488             return FAIL;
1489
1490           if (expr.X_op == O_constant)
1491             {
1492               if (expr.X_add_number
1493                   != (expr.X_add_number & 0x0000ffff))
1494                 {
1495                   inst.error = _("invalid register mask");
1496                   return FAIL;
1497                 }
1498
1499               if ((range & expr.X_add_number) != 0)
1500                 {
1501                   int regno = range & expr.X_add_number;
1502
1503                   regno &= -regno;
1504                   regno = (1 << regno) - 1;
1505                   as_tsktsk
1506                     (_("Warning: duplicated register (r%d) in register list"),
1507                      regno);
1508                 }
1509
1510               range |= expr.X_add_number;
1511             }
1512           else
1513             {
1514               if (inst.reloc.type != 0)
1515                 {
1516                   inst.error = _("expression too complex");
1517                   return FAIL;
1518                 }
1519
1520               memcpy (&inst.reloc.exp, &expr, sizeof (expressionS));
1521               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1522               inst.reloc.pc_rel = 0;
1523             }
1524         }
1525
1526       if (*str == '|' || *str == '+')
1527         {
1528           str++;
1529           another_range = 1;
1530         }
1531     }
1532   while (another_range);
1533
1534   *strp = str;
1535   return range;
1536 }
1537
1538 /* Types of registers in a list.  */
1539
1540 enum reg_list_els
1541 {
1542   REGLIST_VFP_S,
1543   REGLIST_VFP_D,
1544   REGLIST_NEON_D
1545 };
1546
1547 /* Parse a VFP register list.  If the string is invalid return FAIL.
1548    Otherwise return the number of registers, and set PBASE to the first
1549    register.  Parses registers of type ETYPE.
1550    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1551      - Q registers can be used to specify pairs of D registers
1552      - { } can be omitted from around a singleton register list
1553          FIXME: This is not implemented, as it would require backtracking in
1554          some cases, e.g.:
1555            vtbl.8 d3,d4,d5
1556          This could be done (the meaning isn't really ambiguous), but doesn't
1557          fit in well with the current parsing framework.
1558      - 32 D registers may be used (also true for VFPv3).
1559    FIXME: Types are ignored in these register lists, which is probably a
1560    bug.  */
1561
1562 static int
1563 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype)
1564 {
1565   char *str = *ccp;
1566   int base_reg;
1567   int new_base;
1568   enum arm_reg_type regtype = 0;
1569   int max_regs = 0;
1570   int count = 0;
1571   int warned = 0;
1572   unsigned long mask = 0;
1573   int i;
1574
1575   if (*str != '{')
1576     {
1577       inst.error = _("expecting {");
1578       return FAIL;
1579     }
1580
1581   str++;
1582
1583   switch (etype)
1584     {
1585     case REGLIST_VFP_S:
1586       regtype = REG_TYPE_VFS;
1587       max_regs = 32;
1588       break;
1589     
1590     case REGLIST_VFP_D:
1591       regtype = REG_TYPE_VFD;
1592       break;
1593     
1594     case REGLIST_NEON_D:
1595       regtype = REG_TYPE_NDQ;
1596       break;
1597     }
1598
1599   if (etype != REGLIST_VFP_S)
1600     {
1601       /* VFPv3 allows 32 D registers.  */
1602       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
1603         {
1604           max_regs = 32;
1605           if (thumb_mode)
1606             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1607                                     fpu_vfp_ext_v3);
1608           else
1609             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1610                                     fpu_vfp_ext_v3);
1611         }
1612       else
1613         max_regs = 16;
1614     }
1615
1616   base_reg = max_regs;
1617
1618   do
1619     {
1620       int setmask = 1, addregs = 1;
1621
1622       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
1623
1624       if (new_base == FAIL)
1625         {
1626           first_error (_(reg_expected_msgs[regtype]));
1627           return FAIL;
1628         }
1629  
1630       if (new_base >= max_regs)
1631         {
1632           first_error (_("register out of range in list"));
1633           return FAIL;
1634         }
1635  
1636       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1637       if (regtype == REG_TYPE_NQ)
1638         {
1639           setmask = 3;
1640           addregs = 2;
1641         }
1642
1643       if (new_base < base_reg)
1644         base_reg = new_base;
1645
1646       if (mask & (setmask << new_base))
1647         {
1648           first_error (_("invalid register list"));
1649           return FAIL;
1650         }
1651
1652       if ((mask >> new_base) != 0 && ! warned)
1653         {
1654           as_tsktsk (_("register list not in ascending order"));
1655           warned = 1;
1656         }
1657
1658       mask |= setmask << new_base;
1659       count += addregs;
1660
1661       if (*str == '-') /* We have the start of a range expression */
1662         {
1663           int high_range;
1664
1665           str++;
1666
1667           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
1668               == FAIL)
1669             {
1670               inst.error = gettext (reg_expected_msgs[regtype]);
1671               return FAIL;
1672             }
1673
1674           if (high_range >= max_regs)
1675             {
1676               first_error (_("register out of range in list"));
1677               return FAIL;
1678             }
1679
1680           if (regtype == REG_TYPE_NQ)
1681             high_range = high_range + 1;
1682
1683           if (high_range <= new_base)
1684             {
1685               inst.error = _("register range not in ascending order");
1686               return FAIL;
1687             }
1688
1689           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1690             {
1691               if (mask & (setmask << new_base))
1692                 {
1693                   inst.error = _("invalid register list");
1694                   return FAIL;
1695                 }
1696
1697               mask |= setmask << new_base;
1698               count += addregs;
1699             }
1700         }
1701     }
1702   while (skip_past_comma (&str) != FAIL);
1703
1704   str++;
1705
1706   /* Sanity check -- should have raised a parse error above.  */
1707   if (count == 0 || count > max_regs)
1708     abort ();
1709
1710   *pbase = base_reg;
1711
1712   /* Final test -- the registers must be consecutive.  */
1713   mask >>= base_reg;
1714   for (i = 0; i < count; i++)
1715     {
1716       if ((mask & (1u << i)) == 0)
1717         {
1718           inst.error = _("non-contiguous register range");
1719           return FAIL;
1720         }
1721     }
1722
1723   *ccp = str;
1724
1725   return count;
1726 }
1727
1728 /* True if two alias types are the same.  */
1729
1730 static int
1731 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
1732 {
1733   if (!a && !b)
1734     return 1;
1735     
1736   if (!a || !b)
1737     return 0;
1738
1739   if (a->defined != b->defined)
1740     return 0;
1741   
1742   if ((a->defined & NTA_HASTYPE) != 0
1743       && (a->eltype.type != b->eltype.type
1744           || a->eltype.size != b->eltype.size))
1745     return 0;
1746
1747   if ((a->defined & NTA_HASINDEX) != 0
1748       && (a->index != b->index))
1749     return 0;
1750   
1751   return 1;
1752 }
1753
1754 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
1755    The base register is put in *PBASE.
1756    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
1757    the return value.
1758    The register stride (minus one) is put in bit 4 of the return value.
1759    Bits [6:5] encode the list length (minus one).
1760    The type of the list elements is put in *ELTYPE, if non-NULL.  */
1761
1762 #define NEON_LANE(X)            ((X) & 0xf)
1763 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
1764 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
1765
1766 static int
1767 parse_neon_el_struct_list (char **str, unsigned *pbase,
1768                            struct neon_type_el *eltype)
1769 {
1770   char *ptr = *str;
1771   int base_reg = -1;
1772   int reg_incr = -1;
1773   int count = 0;
1774   int lane = -1;
1775   int leading_brace = 0;
1776   enum arm_reg_type rtype = REG_TYPE_NDQ;
1777   int addregs = 1;
1778   const char *const incr_error = "register stride must be 1 or 2";
1779   const char *const type_error = "mismatched element/structure types in list";
1780   struct neon_typed_alias firsttype;
1781   
1782   if (skip_past_char (&ptr, '{') == SUCCESS)
1783     leading_brace = 1;
1784   
1785   do
1786     {
1787       struct neon_typed_alias atype;
1788       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
1789
1790       if (getreg == FAIL)
1791         {
1792           first_error (_(reg_expected_msgs[rtype]));
1793           return FAIL;
1794         }
1795       
1796       if (base_reg == -1)
1797         {
1798           base_reg = getreg;
1799           if (rtype == REG_TYPE_NQ)
1800             {
1801               reg_incr = 1;
1802               addregs = 2;
1803             }
1804           firsttype = atype;
1805         }
1806       else if (reg_incr == -1)
1807         {
1808           reg_incr = getreg - base_reg;
1809           if (reg_incr < 1 || reg_incr > 2)
1810             {
1811               first_error (_(incr_error));
1812               return FAIL;
1813             }
1814         }
1815       else if (getreg != base_reg + reg_incr * count)
1816         {
1817           first_error (_(incr_error));
1818           return FAIL;
1819         }
1820
1821       if (!neon_alias_types_same (&atype, &firsttype))
1822         {
1823           first_error (_(type_error));
1824           return FAIL;
1825         }
1826       
1827       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
1828          modes.  */
1829       if (ptr[0] == '-')
1830         {
1831           struct neon_typed_alias htype;
1832           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
1833           if (lane == -1)
1834             lane = NEON_INTERLEAVE_LANES;
1835           else if (lane != NEON_INTERLEAVE_LANES)
1836             {
1837               first_error (_(type_error));
1838               return FAIL;
1839             }
1840           if (reg_incr == -1)
1841             reg_incr = 1;
1842           else if (reg_incr != 1)
1843             {
1844               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
1845               return FAIL;
1846             }
1847           ptr++;
1848           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
1849           if (hireg == FAIL)
1850             {
1851               first_error (_(reg_expected_msgs[rtype]));
1852               return FAIL;
1853             }
1854           if (!neon_alias_types_same (&htype, &firsttype))
1855             {
1856               first_error (_(type_error));
1857               return FAIL;
1858             }
1859           count += hireg + dregs - getreg;
1860           continue;
1861         }
1862       
1863       /* If we're using Q registers, we can't use [] or [n] syntax.  */
1864       if (rtype == REG_TYPE_NQ)
1865         {
1866           count += 2;
1867           continue;
1868         }
1869       
1870       if ((atype.defined & NTA_HASINDEX) != 0)
1871         {
1872           if (lane == -1)
1873             lane = atype.index;
1874           else if (lane != atype.index)
1875             {
1876               first_error (_(type_error));
1877               return FAIL;
1878             }
1879         }
1880       else if (lane == -1)
1881         lane = NEON_INTERLEAVE_LANES;
1882       else if (lane != NEON_INTERLEAVE_LANES)
1883         {
1884           first_error (_(type_error));
1885           return FAIL;
1886         }
1887       count++;
1888     }
1889   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
1890   
1891   /* No lane set by [x]. We must be interleaving structures.  */
1892   if (lane == -1)
1893     lane = NEON_INTERLEAVE_LANES;
1894   
1895   /* Sanity check.  */
1896   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
1897       || (count > 1 && reg_incr == -1))
1898     {
1899       first_error (_("error parsing element/structure list"));
1900       return FAIL;
1901     }
1902
1903   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
1904     {
1905       first_error (_("expected }"));
1906       return FAIL;
1907     }
1908   
1909   if (reg_incr == -1)
1910     reg_incr = 1;
1911
1912   if (eltype)
1913     *eltype = firsttype.eltype;
1914
1915   *pbase = base_reg;
1916   *str = ptr;
1917   
1918   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
1919 }
1920
1921 /* Parse an explicit relocation suffix on an expression.  This is
1922    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
1923    arm_reloc_hsh contains no entries, so this function can only
1924    succeed if there is no () after the word.  Returns -1 on error,
1925    BFD_RELOC_UNUSED if there wasn't any suffix.  */
1926 static int
1927 parse_reloc (char **str)
1928 {
1929   struct reloc_entry *r;
1930   char *p, *q;
1931
1932   if (**str != '(')
1933     return BFD_RELOC_UNUSED;
1934
1935   p = *str + 1;
1936   q = p;
1937
1938   while (*q && *q != ')' && *q != ',')
1939     q++;
1940   if (*q != ')')
1941     return -1;
1942
1943   if ((r = hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
1944     return -1;
1945
1946   *str = q + 1;
1947   return r->reloc;
1948 }
1949
1950 /* Directives: register aliases.  */
1951
1952 static struct reg_entry *
1953 insert_reg_alias (char *str, int number, int type)
1954 {
1955   struct reg_entry *new;
1956   const char *name;
1957
1958   if ((new = hash_find (arm_reg_hsh, str)) != 0)
1959     {
1960       if (new->builtin)
1961         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
1962
1963       /* Only warn about a redefinition if it's not defined as the
1964          same register.  */
1965       else if (new->number != number || new->type != type)
1966         as_warn (_("ignoring redefinition of register alias '%s'"), str);
1967
1968       return 0;
1969     }
1970
1971   name = xstrdup (str);
1972   new = xmalloc (sizeof (struct reg_entry));
1973
1974   new->name = name;
1975   new->number = number;
1976   new->type = type;
1977   new->builtin = FALSE;
1978   new->neon = NULL;
1979
1980   if (hash_insert (arm_reg_hsh, name, (PTR) new))
1981     abort ();
1982   
1983   return new;
1984 }
1985
1986 static void
1987 insert_neon_reg_alias (char *str, int number, int type,
1988                        struct neon_typed_alias *atype)
1989 {
1990   struct reg_entry *reg = insert_reg_alias (str, number, type);
1991   
1992   if (!reg)
1993     {
1994       first_error (_("attempt to redefine typed alias"));
1995       return;
1996     }
1997   
1998   if (atype)
1999     {
2000       reg->neon = xmalloc (sizeof (struct neon_typed_alias));
2001       *reg->neon = *atype;
2002     }
2003 }
2004
2005 /* Look for the .req directive.  This is of the form:
2006
2007         new_register_name .req existing_register_name
2008
2009    If we find one, or if it looks sufficiently like one that we want to
2010    handle any error here, return non-zero.  Otherwise return zero.  */
2011
2012 static int
2013 create_register_alias (char * newname, char *p)
2014 {
2015   struct reg_entry *old;
2016   char *oldname, *nbuf;
2017   size_t nlen;
2018
2019   /* The input scrubber ensures that whitespace after the mnemonic is
2020      collapsed to single spaces.  */
2021   oldname = p;
2022   if (strncmp (oldname, " .req ", 6) != 0)
2023     return 0;
2024
2025   oldname += 6;
2026   if (*oldname == '\0')
2027     return 0;
2028
2029   old = hash_find (arm_reg_hsh, oldname);
2030   if (!old)
2031     {
2032       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2033       return 1;
2034     }
2035
2036   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2037      the desired alias name, and p points to its end.  If not, then
2038      the desired alias name is in the global original_case_string.  */
2039 #ifdef TC_CASE_SENSITIVE
2040   nlen = p - newname;
2041 #else
2042   newname = original_case_string;
2043   nlen = strlen (newname);
2044 #endif
2045
2046   nbuf = alloca (nlen + 1);
2047   memcpy (nbuf, newname, nlen);
2048   nbuf[nlen] = '\0';
2049
2050   /* Create aliases under the new name as stated; an all-lowercase
2051      version of the new name; and an all-uppercase version of the new
2052      name.  */
2053   insert_reg_alias (nbuf, old->number, old->type);
2054
2055   for (p = nbuf; *p; p++)
2056     *p = TOUPPER (*p);
2057
2058   if (strncmp (nbuf, newname, nlen))
2059     insert_reg_alias (nbuf, old->number, old->type);
2060
2061   for (p = nbuf; *p; p++)
2062     *p = TOLOWER (*p);
2063
2064   if (strncmp (nbuf, newname, nlen))
2065     insert_reg_alias (nbuf, old->number, old->type);
2066
2067   return 1;
2068 }
2069
2070 /* Create a Neon typed/indexed register alias using directives, e.g.:
2071      X .dn d5.s32[1]
2072      Y .qn 6.s16
2073      Z .dn d7
2074      T .dn Z[0]
2075    These typed registers can be used instead of the types specified after the
2076    Neon mnemonic, so long as all operands given have types. Types can also be
2077    specified directly, e.g.:
2078      vadd d0.s32, d1.s32, d2.s32
2079 */
2080
2081 static int
2082 create_neon_reg_alias (char *newname, char *p)
2083 {
2084   enum arm_reg_type basetype;
2085   struct reg_entry *basereg;
2086   struct reg_entry mybasereg;
2087   struct neon_type ntype;
2088   struct neon_typed_alias typeinfo;
2089   char *namebuf, *nameend;
2090   int namelen;
2091   
2092   typeinfo.defined = 0;
2093   typeinfo.eltype.type = NT_invtype;
2094   typeinfo.eltype.size = -1;
2095   typeinfo.index = -1;
2096   
2097   nameend = p;
2098   
2099   if (strncmp (p, " .dn ", 5) == 0)
2100     basetype = REG_TYPE_VFD;
2101   else if (strncmp (p, " .qn ", 5) == 0)
2102     basetype = REG_TYPE_NQ;
2103   else
2104     return 0;
2105   
2106   p += 5;
2107   
2108   if (*p == '\0')
2109     return 0;
2110   
2111   basereg = arm_reg_parse_multi (&p);
2112
2113   if (basereg && basereg->type != basetype)
2114     {
2115       as_bad (_("bad type for register"));
2116       return 0;
2117     }
2118
2119   if (basereg == NULL)
2120     {
2121       expressionS exp;
2122       /* Try parsing as an integer.  */
2123       my_get_expression (&exp, &p, GE_NO_PREFIX);
2124       if (exp.X_op != O_constant)
2125         {
2126           as_bad (_("expression must be constant"));
2127           return 0;
2128         }
2129       basereg = &mybasereg;
2130       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2131                                                   : exp.X_add_number;
2132       basereg->neon = 0;
2133     }
2134
2135   if (basereg->neon)
2136     typeinfo = *basereg->neon;
2137
2138   if (parse_neon_type (&ntype, &p) == SUCCESS)
2139     {
2140       /* We got a type.  */
2141       if (typeinfo.defined & NTA_HASTYPE)
2142         {
2143           as_bad (_("can't redefine the type of a register alias"));
2144           return 0;
2145         }
2146       
2147       typeinfo.defined |= NTA_HASTYPE;
2148       if (ntype.elems != 1)
2149         {
2150           as_bad (_("you must specify a single type only"));
2151           return 0;
2152         }
2153       typeinfo.eltype = ntype.el[0];
2154     }
2155   
2156   if (skip_past_char (&p, '[') == SUCCESS)
2157     {
2158       expressionS exp;
2159       /* We got a scalar index.  */
2160     
2161       if (typeinfo.defined & NTA_HASINDEX)
2162         {
2163           as_bad (_("can't redefine the index of a scalar alias"));
2164           return 0;
2165         }
2166     
2167       my_get_expression (&exp, &p, GE_NO_PREFIX);
2168     
2169       if (exp.X_op != O_constant)
2170         {
2171           as_bad (_("scalar index must be constant"));
2172           return 0;
2173         }
2174       
2175       typeinfo.defined |= NTA_HASINDEX;
2176       typeinfo.index = exp.X_add_number;
2177     
2178       if (skip_past_char (&p, ']') == FAIL)
2179         {
2180           as_bad (_("expecting ]"));
2181           return 0;
2182         }
2183     }
2184
2185   namelen = nameend - newname;
2186   namebuf = alloca (namelen + 1);
2187   strncpy (namebuf, newname, namelen);
2188   namebuf[namelen] = '\0';
2189   
2190   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2191                          typeinfo.defined != 0 ? &typeinfo : NULL);
2192     
2193   /* Insert name in all uppercase.  */
2194   for (p = namebuf; *p; p++)
2195     *p = TOUPPER (*p);
2196   
2197   if (strncmp (namebuf, newname, namelen))
2198     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2199                            typeinfo.defined != 0 ? &typeinfo : NULL);
2200   
2201   /* Insert name in all lowercase.  */
2202   for (p = namebuf; *p; p++)
2203     *p = TOLOWER (*p);
2204   
2205   if (strncmp (namebuf, newname, namelen))
2206     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2207                            typeinfo.defined != 0 ? &typeinfo : NULL);
2208   
2209   return 1;
2210 }
2211
2212 /* Should never be called, as .req goes between the alias and the
2213    register name, not at the beginning of the line.  */
2214 static void
2215 s_req (int a ATTRIBUTE_UNUSED)
2216 {
2217   as_bad (_("invalid syntax for .req directive"));
2218 }
2219
2220 static void
2221 s_dn (int a ATTRIBUTE_UNUSED)
2222 {
2223   as_bad (_("invalid syntax for .dn directive"));
2224 }
2225
2226 static void
2227 s_qn (int a ATTRIBUTE_UNUSED)
2228 {
2229   as_bad (_("invalid syntax for .qn directive"));
2230 }
2231
2232 /* The .unreq directive deletes an alias which was previously defined
2233    by .req.  For example:
2234
2235        my_alias .req r11
2236        .unreq my_alias    */
2237
2238 static void
2239 s_unreq (int a ATTRIBUTE_UNUSED)
2240 {
2241   char * name;
2242   char saved_char;
2243
2244   name = input_line_pointer;
2245
2246   while (*input_line_pointer != 0
2247          && *input_line_pointer != ' '
2248          && *input_line_pointer != '\n')
2249     ++input_line_pointer;
2250
2251   saved_char = *input_line_pointer;
2252   *input_line_pointer = 0;
2253
2254   if (!*name)
2255     as_bad (_("invalid syntax for .unreq directive"));
2256   else
2257     {
2258       struct reg_entry *reg = hash_find (arm_reg_hsh, name);
2259
2260       if (!reg)
2261         as_bad (_("unknown register alias '%s'"), name);
2262       else if (reg->builtin)
2263         as_warn (_("ignoring attempt to undefine built-in register '%s'"),
2264                  name);
2265       else
2266         {
2267           hash_delete (arm_reg_hsh, name);
2268           free ((char *) reg->name);
2269           if (reg->neon)
2270             free (reg->neon);
2271           free (reg);
2272         }
2273     }
2274
2275   *input_line_pointer = saved_char;
2276   demand_empty_rest_of_line ();
2277 }
2278
2279 /* Directives: Instruction set selection.  */
2280
2281 #ifdef OBJ_ELF
2282 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2283    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2284    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2285    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2286
2287 static enum mstate mapstate = MAP_UNDEFINED;
2288
2289 static void
2290 mapping_state (enum mstate state)
2291 {
2292   symbolS * symbolP;
2293   const char * symname;
2294   int type;
2295
2296   if (mapstate == state)
2297     /* The mapping symbol has already been emitted.
2298        There is nothing else to do.  */
2299     return;
2300
2301   mapstate = state;
2302
2303   switch (state)
2304     {
2305     case MAP_DATA:
2306       symname = "$d";
2307       type = BSF_NO_FLAGS;
2308       break;
2309     case MAP_ARM:
2310       symname = "$a";
2311       type = BSF_NO_FLAGS;
2312       break;
2313     case MAP_THUMB:
2314       symname = "$t";
2315       type = BSF_NO_FLAGS;
2316       break;
2317     case MAP_UNDEFINED:
2318       return;
2319     default:
2320       abort ();
2321     }
2322
2323   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2324
2325   symbolP = symbol_new (symname, now_seg, (valueT) frag_now_fix (), frag_now);
2326   symbol_table_insert (symbolP);
2327   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2328
2329   switch (state)
2330     {
2331     case MAP_ARM:
2332       THUMB_SET_FUNC (symbolP, 0);
2333       ARM_SET_THUMB (symbolP, 0);
2334       ARM_SET_INTERWORK (symbolP, support_interwork);
2335       break;
2336
2337     case MAP_THUMB:
2338       THUMB_SET_FUNC (symbolP, 1);
2339       ARM_SET_THUMB (symbolP, 1);
2340       ARM_SET_INTERWORK (symbolP, support_interwork);
2341       break;
2342
2343     case MAP_DATA:
2344     default:
2345       return;
2346     }
2347 }
2348 #else
2349 #define mapping_state(x) /* nothing */
2350 #endif
2351
2352 /* Find the real, Thumb encoded start of a Thumb function.  */
2353
2354 static symbolS *
2355 find_real_start (symbolS * symbolP)
2356 {
2357   char *       real_start;
2358   const char * name = S_GET_NAME (symbolP);
2359   symbolS *    new_target;
2360
2361   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2362 #define STUB_NAME ".real_start_of"
2363
2364   if (name == NULL)
2365     abort ();
2366
2367   /* The compiler may generate BL instructions to local labels because
2368      it needs to perform a branch to a far away location. These labels
2369      do not have a corresponding ".real_start_of" label.  We check
2370      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2371      the ".real_start_of" convention for nonlocal branches.  */
2372   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2373     return symbolP;
2374
2375   real_start = ACONCAT ((STUB_NAME, name, NULL));
2376   new_target = symbol_find (real_start);
2377
2378   if (new_target == NULL)
2379     {
2380       as_warn ("Failed to find real start of function: %s\n", name);
2381       new_target = symbolP;
2382     }
2383
2384   return new_target;
2385 }
2386
2387 static void
2388 opcode_select (int width)
2389 {
2390   switch (width)
2391     {
2392     case 16:
2393       if (! thumb_mode)
2394         {
2395           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2396             as_bad (_("selected processor does not support THUMB opcodes"));
2397
2398           thumb_mode = 1;
2399           /* No need to force the alignment, since we will have been
2400              coming from ARM mode, which is word-aligned.  */
2401           record_alignment (now_seg, 1);
2402         }
2403       mapping_state (MAP_THUMB);
2404       break;
2405
2406     case 32:
2407       if (thumb_mode)
2408         {
2409           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2410             as_bad (_("selected processor does not support ARM opcodes"));
2411
2412           thumb_mode = 0;
2413
2414           if (!need_pass_2)
2415             frag_align (2, 0, 0);
2416
2417           record_alignment (now_seg, 1);
2418         }
2419       mapping_state (MAP_ARM);
2420       break;
2421
2422     default:
2423       as_bad (_("invalid instruction size selected (%d)"), width);
2424     }
2425 }
2426
2427 static void
2428 s_arm (int ignore ATTRIBUTE_UNUSED)
2429 {
2430   opcode_select (32);
2431   demand_empty_rest_of_line ();
2432 }
2433
2434 static void
2435 s_thumb (int ignore ATTRIBUTE_UNUSED)
2436 {
2437   opcode_select (16);
2438   demand_empty_rest_of_line ();
2439 }
2440
2441 static void
2442 s_code (int unused ATTRIBUTE_UNUSED)
2443 {
2444   int temp;
2445
2446   temp = get_absolute_expression ();
2447   switch (temp)
2448     {
2449     case 16:
2450     case 32:
2451       opcode_select (temp);
2452       break;
2453
2454     default:
2455       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2456     }
2457 }
2458
2459 static void
2460 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2461 {
2462   /* If we are not already in thumb mode go into it, EVEN if
2463      the target processor does not support thumb instructions.
2464      This is used by gcc/config/arm/lib1funcs.asm for example
2465      to compile interworking support functions even if the
2466      target processor should not support interworking.  */
2467   if (! thumb_mode)
2468     {
2469       thumb_mode = 2;
2470       record_alignment (now_seg, 1);
2471     }
2472
2473   demand_empty_rest_of_line ();
2474 }
2475
2476 static void
2477 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2478 {
2479   s_thumb (0);
2480
2481   /* The following label is the name/address of the start of a Thumb function.
2482      We need to know this for the interworking support.  */
2483   label_is_thumb_function_name = TRUE;
2484 }
2485
2486 /* Perform a .set directive, but also mark the alias as
2487    being a thumb function.  */
2488
2489 static void
2490 s_thumb_set (int equiv)
2491 {
2492   /* XXX the following is a duplicate of the code for s_set() in read.c
2493      We cannot just call that code as we need to get at the symbol that
2494      is created.  */
2495   char *    name;
2496   char      delim;
2497   char *    end_name;
2498   symbolS * symbolP;
2499
2500   /* Especial apologies for the random logic:
2501      This just grew, and could be parsed much more simply!
2502      Dean - in haste.  */
2503   name      = input_line_pointer;
2504   delim     = get_symbol_end ();
2505   end_name  = input_line_pointer;
2506   *end_name = delim;
2507
2508   if (*input_line_pointer != ',')
2509     {
2510       *end_name = 0;
2511       as_bad (_("expected comma after name \"%s\""), name);
2512       *end_name = delim;
2513       ignore_rest_of_line ();
2514       return;
2515     }
2516
2517   input_line_pointer++;
2518   *end_name = 0;
2519
2520   if (name[0] == '.' && name[1] == '\0')
2521     {
2522       /* XXX - this should not happen to .thumb_set.  */
2523       abort ();
2524     }
2525
2526   if ((symbolP = symbol_find (name)) == NULL
2527       && (symbolP = md_undefined_symbol (name)) == NULL)
2528     {
2529 #ifndef NO_LISTING
2530       /* When doing symbol listings, play games with dummy fragments living
2531          outside the normal fragment chain to record the file and line info
2532          for this symbol.  */
2533       if (listing & LISTING_SYMBOLS)
2534         {
2535           extern struct list_info_struct * listing_tail;
2536           fragS * dummy_frag = xmalloc (sizeof (fragS));
2537
2538           memset (dummy_frag, 0, sizeof (fragS));
2539           dummy_frag->fr_type = rs_fill;
2540           dummy_frag->line = listing_tail;
2541           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2542           dummy_frag->fr_symbol = symbolP;
2543         }
2544       else
2545 #endif
2546         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2547
2548 #ifdef OBJ_COFF
2549       /* "set" symbols are local unless otherwise specified.  */
2550       SF_SET_LOCAL (symbolP);
2551 #endif /* OBJ_COFF  */
2552     }                           /* Make a new symbol.  */
2553
2554   symbol_table_insert (symbolP);
2555
2556   * end_name = delim;
2557
2558   if (equiv
2559       && S_IS_DEFINED (symbolP)
2560       && S_GET_SEGMENT (symbolP) != reg_section)
2561     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2562
2563   pseudo_set (symbolP);
2564
2565   demand_empty_rest_of_line ();
2566
2567   /* XXX Now we come to the Thumb specific bit of code.  */
2568
2569   THUMB_SET_FUNC (symbolP, 1);
2570   ARM_SET_THUMB (symbolP, 1);
2571 #if defined OBJ_ELF || defined OBJ_COFF
2572   ARM_SET_INTERWORK (symbolP, support_interwork);
2573 #endif
2574 }
2575
2576 /* Directives: Mode selection.  */
2577
2578 /* .syntax [unified|divided] - choose the new unified syntax
2579    (same for Arm and Thumb encoding, modulo slight differences in what
2580    can be represented) or the old divergent syntax for each mode.  */
2581 static void
2582 s_syntax (int unused ATTRIBUTE_UNUSED)
2583 {
2584   char *name, delim;
2585
2586   name = input_line_pointer;
2587   delim = get_symbol_end ();
2588
2589   if (!strcasecmp (name, "unified"))
2590     unified_syntax = TRUE;
2591   else if (!strcasecmp (name, "divided"))
2592     unified_syntax = FALSE;
2593   else
2594     {
2595       as_bad (_("unrecognized syntax mode \"%s\""), name);
2596       return;
2597     }
2598   *input_line_pointer = delim;
2599   demand_empty_rest_of_line ();
2600 }
2601
2602 /* Directives: sectioning and alignment.  */
2603
2604 /* Same as s_align_ptwo but align 0 => align 2.  */
2605
2606 static void
2607 s_align (int unused ATTRIBUTE_UNUSED)
2608 {
2609   int temp;
2610   long temp_fill;
2611   long max_alignment = 15;
2612
2613   temp = get_absolute_expression ();
2614   if (temp > max_alignment)
2615     as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
2616   else if (temp < 0)
2617     {
2618       as_bad (_("alignment negative. 0 assumed."));
2619       temp = 0;
2620     }
2621
2622   if (*input_line_pointer == ',')
2623     {
2624       input_line_pointer++;
2625       temp_fill = get_absolute_expression ();
2626     }
2627   else
2628     temp_fill = 0;
2629
2630   if (!temp)
2631     temp = 2;
2632
2633   /* Only make a frag if we HAVE to.  */
2634   if (temp && !need_pass_2)
2635     frag_align (temp, (int) temp_fill, 0);
2636   demand_empty_rest_of_line ();
2637
2638   record_alignment (now_seg, temp);
2639 }
2640
2641 static void
2642 s_bss (int ignore ATTRIBUTE_UNUSED)
2643 {
2644   /* We don't support putting frags in the BSS segment, we fake it by
2645      marking in_bss, then looking at s_skip for clues.  */
2646   subseg_set (bss_section, 0);
2647   demand_empty_rest_of_line ();
2648   mapping_state (MAP_DATA);
2649 }
2650
2651 static void
2652 s_even (int ignore ATTRIBUTE_UNUSED)
2653 {
2654   /* Never make frag if expect extra pass.  */
2655   if (!need_pass_2)
2656     frag_align (1, 0, 0);
2657
2658   record_alignment (now_seg, 1);
2659
2660   demand_empty_rest_of_line ();
2661 }
2662
2663 /* Directives: Literal pools.  */
2664
2665 static literal_pool *
2666 find_literal_pool (void)
2667 {
2668   literal_pool * pool;
2669
2670   for (pool = list_of_pools; pool != NULL; pool = pool->next)
2671     {
2672       if (pool->section == now_seg
2673           && pool->sub_section == now_subseg)
2674         break;
2675     }
2676
2677   return pool;
2678 }
2679
2680 static literal_pool *
2681 find_or_make_literal_pool (void)
2682 {
2683   /* Next literal pool ID number.  */
2684   static unsigned int latest_pool_num = 1;
2685   literal_pool *      pool;
2686
2687   pool = find_literal_pool ();
2688
2689   if (pool == NULL)
2690     {
2691       /* Create a new pool.  */
2692       pool = xmalloc (sizeof (* pool));
2693       if (! pool)
2694         return NULL;
2695
2696       pool->next_free_entry = 0;
2697       pool->section         = now_seg;
2698       pool->sub_section     = now_subseg;
2699       pool->next            = list_of_pools;
2700       pool->symbol          = NULL;
2701
2702       /* Add it to the list.  */
2703       list_of_pools = pool;
2704     }
2705
2706   /* New pools, and emptied pools, will have a NULL symbol.  */
2707   if (pool->symbol == NULL)
2708     {
2709       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
2710                                     (valueT) 0, &zero_address_frag);
2711       pool->id = latest_pool_num ++;
2712     }
2713
2714   /* Done.  */
2715   return pool;
2716 }
2717
2718 /* Add the literal in the global 'inst'
2719    structure to the relevent literal pool.  */
2720
2721 static int
2722 add_to_lit_pool (void)
2723 {
2724   literal_pool * pool;
2725   unsigned int entry;
2726
2727   pool = find_or_make_literal_pool ();
2728
2729   /* Check if this literal value is already in the pool.  */
2730   for (entry = 0; entry < pool->next_free_entry; entry ++)
2731     {
2732       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
2733           && (inst.reloc.exp.X_op == O_constant)
2734           && (pool->literals[entry].X_add_number
2735               == inst.reloc.exp.X_add_number)
2736           && (pool->literals[entry].X_unsigned
2737               == inst.reloc.exp.X_unsigned))
2738         break;
2739
2740       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
2741           && (inst.reloc.exp.X_op == O_symbol)
2742           && (pool->literals[entry].X_add_number
2743               == inst.reloc.exp.X_add_number)
2744           && (pool->literals[entry].X_add_symbol
2745               == inst.reloc.exp.X_add_symbol)
2746           && (pool->literals[entry].X_op_symbol
2747               == inst.reloc.exp.X_op_symbol))
2748         break;
2749     }
2750
2751   /* Do we need to create a new entry?  */
2752   if (entry == pool->next_free_entry)
2753     {
2754       if (entry >= MAX_LITERAL_POOL_SIZE)
2755         {
2756           inst.error = _("literal pool overflow");
2757           return FAIL;
2758         }
2759
2760       pool->literals[entry] = inst.reloc.exp;
2761       pool->next_free_entry += 1;
2762     }
2763
2764   inst.reloc.exp.X_op         = O_symbol;
2765   inst.reloc.exp.X_add_number = ((int) entry) * 4;
2766   inst.reloc.exp.X_add_symbol = pool->symbol;
2767
2768   return SUCCESS;
2769 }
2770
2771 /* Can't use symbol_new here, so have to create a symbol and then at
2772    a later date assign it a value. Thats what these functions do.  */
2773
2774 static void
2775 symbol_locate (symbolS *    symbolP,
2776                const char * name,       /* It is copied, the caller can modify.  */
2777                segT         segment,    /* Segment identifier (SEG_<something>).  */
2778                valueT       valu,       /* Symbol value.  */
2779                fragS *      frag)       /* Associated fragment.  */
2780 {
2781   unsigned int name_length;
2782   char * preserved_copy_of_name;
2783
2784   name_length = strlen (name) + 1;   /* +1 for \0.  */
2785   obstack_grow (&notes, name, name_length);
2786   preserved_copy_of_name = obstack_finish (&notes);
2787
2788 #ifdef tc_canonicalize_symbol_name
2789   preserved_copy_of_name =
2790     tc_canonicalize_symbol_name (preserved_copy_of_name);
2791 #endif
2792
2793   S_SET_NAME (symbolP, preserved_copy_of_name);
2794
2795   S_SET_SEGMENT (symbolP, segment);
2796   S_SET_VALUE (symbolP, valu);
2797   symbol_clear_list_pointers (symbolP);
2798
2799   symbol_set_frag (symbolP, frag);
2800
2801   /* Link to end of symbol chain.  */
2802   {
2803     extern int symbol_table_frozen;
2804
2805     if (symbol_table_frozen)
2806       abort ();
2807   }
2808
2809   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
2810
2811   obj_symbol_new_hook (symbolP);
2812
2813 #ifdef tc_symbol_new_hook
2814   tc_symbol_new_hook (symbolP);
2815 #endif
2816
2817 #ifdef DEBUG_SYMS
2818   verify_symbol_chain (symbol_rootP, symbol_lastP);
2819 #endif /* DEBUG_SYMS  */
2820 }
2821
2822
2823 static void
2824 s_ltorg (int ignored ATTRIBUTE_UNUSED)
2825 {
2826   unsigned int entry;
2827   literal_pool * pool;
2828   char sym_name[20];
2829
2830   pool = find_literal_pool ();
2831   if (pool == NULL
2832       || pool->symbol == NULL
2833       || pool->next_free_entry == 0)
2834     return;
2835
2836   mapping_state (MAP_DATA);
2837
2838   /* Align pool as you have word accesses.
2839      Only make a frag if we have to.  */
2840   if (!need_pass_2)
2841     frag_align (2, 0, 0);
2842
2843   record_alignment (now_seg, 2);
2844
2845   sprintf (sym_name, "$$lit_\002%x", pool->id);
2846
2847   symbol_locate (pool->symbol, sym_name, now_seg,
2848                  (valueT) frag_now_fix (), frag_now);
2849   symbol_table_insert (pool->symbol);
2850
2851   ARM_SET_THUMB (pool->symbol, thumb_mode);
2852
2853 #if defined OBJ_COFF || defined OBJ_ELF
2854   ARM_SET_INTERWORK (pool->symbol, support_interwork);
2855 #endif
2856
2857   for (entry = 0; entry < pool->next_free_entry; entry ++)
2858     /* First output the expression in the instruction to the pool.  */
2859     emit_expr (&(pool->literals[entry]), 4); /* .word  */
2860
2861   /* Mark the pool as empty.  */
2862   pool->next_free_entry = 0;
2863   pool->symbol = NULL;
2864 }
2865
2866 #ifdef OBJ_ELF
2867 /* Forward declarations for functions below, in the MD interface
2868    section.  */
2869 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
2870 static valueT create_unwind_entry (int);
2871 static void start_unwind_section (const segT, int);
2872 static void add_unwind_opcode (valueT, int);
2873 static void flush_pending_unwind (void);
2874
2875 /* Directives: Data.  */
2876
2877 static void
2878 s_arm_elf_cons (int nbytes)
2879 {
2880   expressionS exp;
2881
2882 #ifdef md_flush_pending_output
2883   md_flush_pending_output ();
2884 #endif
2885
2886   if (is_it_end_of_statement ())
2887     {
2888       demand_empty_rest_of_line ();
2889       return;
2890     }
2891
2892 #ifdef md_cons_align
2893   md_cons_align (nbytes);
2894 #endif
2895
2896   mapping_state (MAP_DATA);
2897   do
2898     {
2899       int reloc;
2900       char *base = input_line_pointer;
2901
2902       expression (& exp);
2903
2904       if (exp.X_op != O_symbol)
2905         emit_expr (&exp, (unsigned int) nbytes);
2906       else
2907         {
2908           char *before_reloc = input_line_pointer;
2909           reloc = parse_reloc (&input_line_pointer);
2910           if (reloc == -1)
2911             {
2912               as_bad (_("unrecognized relocation suffix"));
2913               ignore_rest_of_line ();
2914               return;
2915             }
2916           else if (reloc == BFD_RELOC_UNUSED)
2917             emit_expr (&exp, (unsigned int) nbytes);
2918           else
2919             {
2920               reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, reloc);
2921               int size = bfd_get_reloc_size (howto);
2922
2923               if (reloc == BFD_RELOC_ARM_PLT32)
2924                 {
2925                   as_bad (_("(plt) is only valid on branch targets"));
2926                   reloc = BFD_RELOC_UNUSED;
2927                   size = 0;
2928                 }
2929
2930               if (size > nbytes)
2931                 as_bad (_("%s relocations do not fit in %d bytes"),
2932                         howto->name, nbytes);
2933               else
2934                 {
2935                   /* We've parsed an expression stopping at O_symbol.
2936                      But there may be more expression left now that we
2937                      have parsed the relocation marker.  Parse it again.
2938                      XXX Surely there is a cleaner way to do this.  */
2939                   char *p = input_line_pointer;
2940                   int offset;
2941                   char *save_buf = alloca (input_line_pointer - base);
2942                   memcpy (save_buf, base, input_line_pointer - base);
2943                   memmove (base + (input_line_pointer - before_reloc),
2944                            base, before_reloc - base);
2945
2946                   input_line_pointer = base + (input_line_pointer-before_reloc);
2947                   expression (&exp);
2948                   memcpy (base, save_buf, p - base);
2949
2950                   offset = nbytes - size;
2951                   p = frag_more ((int) nbytes);
2952                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
2953                                size, &exp, 0, reloc);
2954                 }
2955             }
2956         }
2957     }
2958   while (*input_line_pointer++ == ',');
2959
2960   /* Put terminator back into stream.  */
2961   input_line_pointer --;
2962   demand_empty_rest_of_line ();
2963 }
2964
2965
2966 /* Parse a .rel31 directive.  */
2967
2968 static void
2969 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
2970 {
2971   expressionS exp;
2972   char *p;
2973   valueT highbit;
2974
2975   highbit = 0;
2976   if (*input_line_pointer == '1')
2977     highbit = 0x80000000;
2978   else if (*input_line_pointer != '0')
2979     as_bad (_("expected 0 or 1"));
2980
2981   input_line_pointer++;
2982   if (*input_line_pointer != ',')
2983     as_bad (_("missing comma"));
2984   input_line_pointer++;
2985
2986 #ifdef md_flush_pending_output
2987   md_flush_pending_output ();
2988 #endif
2989
2990 #ifdef md_cons_align
2991   md_cons_align (4);
2992 #endif
2993
2994   mapping_state (MAP_DATA);
2995
2996   expression (&exp);
2997
2998   p = frag_more (4);
2999   md_number_to_chars (p, highbit, 4);
3000   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3001                BFD_RELOC_ARM_PREL31);
3002
3003   demand_empty_rest_of_line ();
3004 }
3005
3006 /* Directives: AEABI stack-unwind tables.  */
3007
3008 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3009
3010 static void
3011 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3012 {
3013   demand_empty_rest_of_line ();
3014   /* Mark the start of the function.  */
3015   unwind.proc_start = expr_build_dot ();
3016
3017   /* Reset the rest of the unwind info.  */
3018   unwind.opcode_count = 0;
3019   unwind.table_entry = NULL;
3020   unwind.personality_routine = NULL;
3021   unwind.personality_index = -1;
3022   unwind.frame_size = 0;
3023   unwind.fp_offset = 0;
3024   unwind.fp_reg = 13;
3025   unwind.fp_used = 0;
3026   unwind.sp_restored = 0;
3027 }
3028
3029
3030 /* Parse a handlerdata directive.  Creates the exception handling table entry
3031    for the function.  */
3032
3033 static void
3034 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3035 {
3036   demand_empty_rest_of_line ();
3037   if (unwind.table_entry)
3038     as_bad (_("dupicate .handlerdata directive"));
3039
3040   create_unwind_entry (1);
3041 }
3042
3043 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3044
3045 static void
3046 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3047 {
3048   long where;
3049   char *ptr;
3050   valueT val;
3051
3052   demand_empty_rest_of_line ();
3053
3054   /* Add eh table entry.  */
3055   if (unwind.table_entry == NULL)
3056     val = create_unwind_entry (0);
3057   else
3058     val = 0;
3059
3060   /* Add index table entry.  This is two words.  */
3061   start_unwind_section (unwind.saved_seg, 1);
3062   frag_align (2, 0, 0);
3063   record_alignment (now_seg, 2);
3064
3065   ptr = frag_more (8);
3066   where = frag_now_fix () - 8;
3067
3068   /* Self relative offset of the function start.  */
3069   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3070            BFD_RELOC_ARM_PREL31);
3071
3072   /* Indicate dependency on EHABI-defined personality routines to the
3073      linker, if it hasn't been done already.  */
3074   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3075       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3076     {
3077       static const char *const name[] = {
3078         "__aeabi_unwind_cpp_pr0",
3079         "__aeabi_unwind_cpp_pr1",
3080         "__aeabi_unwind_cpp_pr2"
3081       };
3082       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3083       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3084       marked_pr_dependency |= 1 << unwind.personality_index;
3085       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3086         = marked_pr_dependency;
3087     }
3088
3089   if (val)
3090     /* Inline exception table entry.  */
3091     md_number_to_chars (ptr + 4, val, 4);
3092   else
3093     /* Self relative offset of the table entry.  */
3094     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3095              BFD_RELOC_ARM_PREL31);
3096
3097   /* Restore the original section.  */
3098   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3099 }
3100
3101
3102 /* Parse an unwind_cantunwind directive.  */
3103
3104 static void
3105 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3106 {
3107   demand_empty_rest_of_line ();
3108   if (unwind.personality_routine || unwind.personality_index != -1)
3109     as_bad (_("personality routine specified for cantunwind frame"));
3110
3111   unwind.personality_index = -2;
3112 }
3113
3114
3115 /* Parse a personalityindex directive.  */
3116
3117 static void
3118 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3119 {
3120   expressionS exp;
3121
3122   if (unwind.personality_routine || unwind.personality_index != -1)
3123     as_bad (_("duplicate .personalityindex directive"));
3124
3125   expression (&exp);
3126
3127   if (exp.X_op != O_constant
3128       || exp.X_add_number < 0 || exp.X_add_number > 15)
3129     {
3130       as_bad (_("bad personality routine number"));
3131       ignore_rest_of_line ();
3132       return;
3133     }
3134
3135   unwind.personality_index = exp.X_add_number;
3136
3137   demand_empty_rest_of_line ();
3138 }
3139
3140
3141 /* Parse a personality directive.  */
3142
3143 static void
3144 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3145 {
3146   char *name, *p, c;
3147
3148   if (unwind.personality_routine || unwind.personality_index != -1)
3149     as_bad (_("duplicate .personality directive"));
3150
3151   name = input_line_pointer;
3152   c = get_symbol_end ();
3153   p = input_line_pointer;
3154   unwind.personality_routine = symbol_find_or_make (name);
3155   *p = c;
3156   demand_empty_rest_of_line ();
3157 }
3158
3159
3160 /* Parse a directive saving core registers.  */
3161
3162 static void
3163 s_arm_unwind_save_core (void)
3164 {
3165   valueT op;
3166   long range;
3167   int n;
3168
3169   range = parse_reg_list (&input_line_pointer);
3170   if (range == FAIL)
3171     {
3172       as_bad (_("expected register list"));
3173       ignore_rest_of_line ();
3174       return;
3175     }
3176
3177   demand_empty_rest_of_line ();
3178
3179   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3180      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3181      ip because it is clobbered by calls.  */
3182   if (unwind.sp_restored && unwind.fp_reg == 12
3183       && (range & 0x3000) == 0x1000)
3184     {
3185       unwind.opcode_count--;
3186       unwind.sp_restored = 0;
3187       range = (range | 0x2000) & ~0x1000;
3188       unwind.pending_offset = 0;
3189     }
3190
3191   /* Pop r4-r15.  */
3192   if (range & 0xfff0)
3193     {
3194       /* See if we can use the short opcodes.  These pop a block of up to 8
3195          registers starting with r4, plus maybe r14.  */
3196       for (n = 0; n < 8; n++)
3197         {
3198           /* Break at the first non-saved register.      */
3199           if ((range & (1 << (n + 4))) == 0)
3200             break;
3201         }
3202       /* See if there are any other bits set.  */
3203       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
3204         {
3205           /* Use the long form.  */
3206           op = 0x8000 | ((range >> 4) & 0xfff);
3207           add_unwind_opcode (op, 2);
3208         }
3209       else
3210         {
3211           /* Use the short form.  */
3212           if (range & 0x4000)
3213             op = 0xa8; /* Pop r14.      */
3214           else
3215             op = 0xa0; /* Do not pop r14.  */
3216           op |= (n - 1);
3217           add_unwind_opcode (op, 1);
3218         }
3219     }
3220
3221   /* Pop r0-r3.  */
3222   if (range & 0xf)
3223     {
3224       op = 0xb100 | (range & 0xf);
3225       add_unwind_opcode (op, 2);
3226     }
3227
3228   /* Record the number of bytes pushed.  */
3229   for (n = 0; n < 16; n++)
3230     {
3231       if (range & (1 << n))
3232         unwind.frame_size += 4;
3233     }
3234 }
3235
3236
3237 /* Parse a directive saving FPA registers.  */
3238
3239 static void
3240 s_arm_unwind_save_fpa (int reg)
3241 {
3242   expressionS exp;
3243   int num_regs;
3244   valueT op;
3245
3246   /* Get Number of registers to transfer.  */
3247   if (skip_past_comma (&input_line_pointer) != FAIL)
3248     expression (&exp);
3249   else
3250     exp.X_op = O_illegal;
3251
3252   if (exp.X_op != O_constant)
3253     {
3254       as_bad (_("expected , <constant>"));
3255       ignore_rest_of_line ();
3256       return;
3257     }
3258
3259   num_regs = exp.X_add_number;
3260
3261   if (num_regs < 1 || num_regs > 4)
3262     {
3263       as_bad (_("number of registers must be in the range [1:4]"));
3264       ignore_rest_of_line ();
3265       return;
3266     }
3267
3268   demand_empty_rest_of_line ();
3269
3270   if (reg == 4)
3271     {
3272       /* Short form.  */
3273       op = 0xb4 | (num_regs - 1);
3274       add_unwind_opcode (op, 1);
3275     }
3276   else
3277     {
3278       /* Long form.  */
3279       op = 0xc800 | (reg << 4) | (num_regs - 1);
3280       add_unwind_opcode (op, 2);
3281     }
3282   unwind.frame_size += num_regs * 12;
3283 }
3284
3285
3286 /* Parse a directive saving VFP registers for ARMv6 and above.  */
3287
3288 static void
3289 s_arm_unwind_save_vfp_armv6 (void)
3290 {
3291   int count;
3292   unsigned int start;
3293   valueT op;
3294   int num_vfpv3_regs = 0;
3295   int num_regs_below_16;
3296
3297   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
3298   if (count == FAIL)
3299     {
3300       as_bad (_("expected register list"));
3301       ignore_rest_of_line ();
3302       return;
3303     }
3304
3305   demand_empty_rest_of_line ();
3306
3307   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
3308      than FSTMX/FLDMX-style ones).  */
3309
3310   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
3311   if (start >= 16)
3312     num_vfpv3_regs = count;
3313   else if (start + count > 16)
3314     num_vfpv3_regs = start + count - 16;
3315
3316   if (num_vfpv3_regs > 0)
3317     {
3318       int start_offset = start > 16 ? start - 16 : 0;
3319       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
3320       add_unwind_opcode (op, 2);
3321     }
3322
3323   /* Generate opcode for registers numbered in the range 0 .. 15.  */
3324   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
3325   assert (num_regs_below_16 + num_vfpv3_regs == count);
3326   if (num_regs_below_16 > 0)
3327     {
3328       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
3329       add_unwind_opcode (op, 2);
3330     }
3331
3332   unwind.frame_size += count * 8;
3333 }
3334
3335
3336 /* Parse a directive saving VFP registers for pre-ARMv6.  */
3337
3338 static void
3339 s_arm_unwind_save_vfp (void)
3340 {
3341   int count;
3342   unsigned int reg;
3343   valueT op;
3344
3345   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
3346   if (count == FAIL)
3347     {
3348       as_bad (_("expected register list"));
3349       ignore_rest_of_line ();
3350       return;
3351     }
3352
3353   demand_empty_rest_of_line ();
3354
3355   if (reg == 8)
3356     {
3357       /* Short form.  */
3358       op = 0xb8 | (count - 1);
3359       add_unwind_opcode (op, 1);
3360     }
3361   else
3362     {
3363       /* Long form.  */
3364       op = 0xb300 | (reg << 4) | (count - 1);
3365       add_unwind_opcode (op, 2);
3366     }
3367   unwind.frame_size += count * 8 + 4;
3368 }
3369
3370
3371 /* Parse a directive saving iWMMXt data registers.  */
3372
3373 static void
3374 s_arm_unwind_save_mmxwr (void)
3375 {
3376   int reg;
3377   int hi_reg;
3378   int i;
3379   unsigned mask = 0;
3380   valueT op;
3381
3382   if (*input_line_pointer == '{')
3383     input_line_pointer++;
3384
3385   do
3386     {
3387       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3388
3389       if (reg == FAIL)
3390         {
3391           as_bad (_(reg_expected_msgs[REG_TYPE_MMXWR]));
3392           goto error;
3393         }
3394
3395       if (mask >> reg)
3396         as_tsktsk (_("register list not in ascending order"));
3397       mask |= 1 << reg;
3398
3399       if (*input_line_pointer == '-')
3400         {
3401           input_line_pointer++;
3402           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
3403           if (hi_reg == FAIL)
3404             {
3405               as_bad (_(reg_expected_msgs[REG_TYPE_MMXWR]));
3406               goto error;
3407             }
3408           else if (reg >= hi_reg)
3409             {
3410               as_bad (_("bad register range"));
3411               goto error;
3412             }
3413           for (; reg < hi_reg; reg++)
3414             mask |= 1 << reg;
3415         }
3416     }
3417   while (skip_past_comma (&input_line_pointer) != FAIL);
3418
3419   if (*input_line_pointer == '}')
3420     input_line_pointer++;
3421
3422   demand_empty_rest_of_line ();
3423
3424   /* Generate any deferred opcodes becuuse we're going to be looking at
3425      the list.  */
3426   flush_pending_unwind ();
3427
3428   for (i = 0; i < 16; i++)
3429     {
3430       if (mask & (1 << i))
3431         unwind.frame_size += 8;
3432     }
3433
3434   /* Attempt to combine with a previous opcode.  We do this because gcc
3435      likes to output separate unwind directives for a single block of
3436      registers.  */
3437   if (unwind.opcode_count > 0)
3438     {
3439       i = unwind.opcodes[unwind.opcode_count - 1];
3440       if ((i & 0xf8) == 0xc0)
3441         {
3442           i &= 7;
3443           /* Only merge if the blocks are contiguous.  */
3444           if (i < 6)
3445             {
3446               if ((mask & 0xfe00) == (1 << 9))
3447                 {
3448                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
3449                   unwind.opcode_count--;
3450                 }
3451             }
3452           else if (i == 6 && unwind.opcode_count >= 2)
3453             {
3454               i = unwind.opcodes[unwind.opcode_count - 2];
3455               reg = i >> 4;
3456               i &= 0xf;
3457
3458               op = 0xffff << (reg - 1);
3459               if (reg > 0
3460                   && ((mask & op) == (1u << (reg - 1))))
3461                 {
3462                   op = (1 << (reg + i + 1)) - 1;
3463                   op &= ~((1 << reg) - 1);
3464                   mask |= op;
3465                   unwind.opcode_count -= 2;
3466                 }
3467             }
3468         }
3469     }
3470
3471   hi_reg = 15;
3472   /* We want to generate opcodes in the order the registers have been
3473      saved, ie. descending order.  */
3474   for (reg = 15; reg >= -1; reg--)
3475     {
3476       /* Save registers in blocks.  */
3477       if (reg < 0
3478           || !(mask & (1 << reg)))
3479         {
3480           /* We found an unsaved reg.  Generate opcodes to save the
3481              preceeding block.  */
3482           if (reg != hi_reg)
3483             {
3484               if (reg == 9)
3485                 {
3486                   /* Short form.  */
3487                   op = 0xc0 | (hi_reg - 10);
3488                   add_unwind_opcode (op, 1);
3489                 }
3490               else
3491                 {
3492                   /* Long form.  */
3493                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
3494                   add_unwind_opcode (op, 2);
3495                 }
3496             }
3497           hi_reg = reg - 1;
3498         }
3499     }
3500
3501   return;
3502 error:
3503   ignore_rest_of_line ();
3504 }
3505
3506 static void
3507 s_arm_unwind_save_mmxwcg (void)
3508 {
3509   int reg;
3510   int hi_reg;
3511   unsigned mask = 0;
3512   valueT op;
3513
3514   if (*input_line_pointer == '{')
3515     input_line_pointer++;
3516
3517   do
3518     {
3519       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
3520
3521       if (reg == FAIL)
3522         {
3523           as_bad (_(reg_expected_msgs[REG_TYPE_MMXWCG]));
3524           goto error;
3525         }
3526
3527       reg -= 8;
3528       if (mask >> reg)
3529         as_tsktsk (_("register list not in ascending order"));
3530       mask |= 1 << reg;
3531
3532       if (*input_line_pointer == '-')
3533         {
3534           input_line_pointer++;
3535           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
3536           if (hi_reg == FAIL)
3537             {
3538               as_bad (_(reg_expected_msgs[REG_TYPE_MMXWCG]));
3539               goto error;
3540             }
3541           else if (reg >= hi_reg)
3542             {
3543               as_bad (_("bad register range"));
3544               goto error;
3545             }
3546           for (; reg < hi_reg; reg++)
3547             mask |= 1 << reg;
3548         }
3549     }
3550   while (skip_past_comma (&input_line_pointer) != FAIL);
3551
3552   if (*input_line_pointer == '}')
3553     input_line_pointer++;
3554
3555   demand_empty_rest_of_line ();
3556
3557   /* Generate any deferred opcodes becuuse we're going to be looking at
3558      the list.  */
3559   flush_pending_unwind ();
3560
3561   for (reg = 0; reg < 16; reg++)
3562     {
3563       if (mask & (1 << reg))
3564         unwind.frame_size += 4;
3565     }
3566   op = 0xc700 | mask;
3567   add_unwind_opcode (op, 2);
3568   return;
3569 error:
3570   ignore_rest_of_line ();
3571 }
3572
3573
3574 /* Parse an unwind_save directive.
3575    If the argument is non-zero, this is a .vsave directive.  */
3576
3577 static void
3578 s_arm_unwind_save (int arch_v6)
3579 {
3580   char *peek;
3581   struct reg_entry *reg;
3582   bfd_boolean had_brace = FALSE;
3583
3584   /* Figure out what sort of save we have.  */
3585   peek = input_line_pointer;
3586
3587   if (*peek == '{')
3588     {
3589       had_brace = TRUE;
3590       peek++;
3591     }
3592
3593   reg = arm_reg_parse_multi (&peek);
3594
3595   if (!reg)
3596     {
3597       as_bad (_("register expected"));
3598       ignore_rest_of_line ();
3599       return;
3600     }
3601
3602   switch (reg->type)
3603     {
3604     case REG_TYPE_FN:
3605       if (had_brace)
3606         {
3607           as_bad (_("FPA .unwind_save does not take a register list"));
3608           ignore_rest_of_line ();
3609           return;
3610         }
3611       s_arm_unwind_save_fpa (reg->number);
3612       return;
3613
3614     case REG_TYPE_RN:     s_arm_unwind_save_core ();   return;
3615     case REG_TYPE_VFD:
3616       if (arch_v6)
3617         s_arm_unwind_save_vfp_armv6 ();
3618       else
3619         s_arm_unwind_save_vfp ();
3620       return;
3621     case REG_TYPE_MMXWR:  s_arm_unwind_save_mmxwr ();  return;
3622     case REG_TYPE_MMXWCG: s_arm_unwind_save_mmxwcg (); return;
3623
3624     default:
3625       as_bad (_(".unwind_save does not support this kind of register"));
3626       ignore_rest_of_line ();
3627     }
3628 }
3629
3630
3631 /* Parse an unwind_movsp directive.  */
3632
3633 static void
3634 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
3635 {
3636   int reg;
3637   valueT op;
3638   int offset;
3639
3640   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
3641   if (reg == FAIL)
3642     {
3643       as_bad (_(reg_expected_msgs[REG_TYPE_RN]));
3644       ignore_rest_of_line ();
3645       return;
3646     }
3647
3648   /* Optional constant.  */
3649   if (skip_past_comma (&input_line_pointer) != FAIL)
3650     {
3651       if (immediate_for_directive (&offset) == FAIL)
3652         return;
3653     }
3654   else
3655     offset = 0;
3656
3657   demand_empty_rest_of_line ();
3658
3659   if (reg == REG_SP || reg == REG_PC)
3660     {
3661       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
3662       return;
3663     }
3664
3665   if (unwind.fp_reg != REG_SP)
3666     as_bad (_("unexpected .unwind_movsp directive"));
3667
3668   /* Generate opcode to restore the value.  */
3669   op = 0x90 | reg;
3670   add_unwind_opcode (op, 1);
3671
3672   /* Record the information for later.  */
3673   unwind.fp_reg = reg;
3674   unwind.fp_offset = unwind.frame_size - offset;
3675   unwind.sp_restored = 1;
3676 }
3677
3678 /* Parse an unwind_pad directive.  */
3679
3680 static void
3681 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
3682 {
3683   int offset;
3684
3685   if (immediate_for_directive (&offset) == FAIL)
3686     return;
3687
3688   if (offset & 3)
3689     {
3690       as_bad (_("stack increment must be multiple of 4"));
3691       ignore_rest_of_line ();
3692       return;
3693     }
3694
3695   /* Don't generate any opcodes, just record the details for later.  */
3696   unwind.frame_size += offset;
3697   unwind.pending_offset += offset;
3698
3699   demand_empty_rest_of_line ();
3700 }
3701
3702 /* Parse an unwind_setfp directive.  */
3703
3704 static void
3705 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
3706 {
3707   int sp_reg;
3708   int fp_reg;
3709   int offset;
3710
3711   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
3712   if (skip_past_comma (&input_line_pointer) == FAIL)
3713     sp_reg = FAIL;
3714   else
3715     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
3716
3717   if (fp_reg == FAIL || sp_reg == FAIL)
3718     {
3719       as_bad (_("expected <reg>, <reg>"));
3720       ignore_rest_of_line ();
3721       return;
3722     }
3723
3724   /* Optional constant.  */
3725   if (skip_past_comma (&input_line_pointer) != FAIL)
3726     {
3727       if (immediate_for_directive (&offset) == FAIL)
3728         return;
3729     }
3730   else
3731     offset = 0;
3732
3733   demand_empty_rest_of_line ();
3734
3735   if (sp_reg != 13 && sp_reg != unwind.fp_reg)
3736     {
3737       as_bad (_("register must be either sp or set by a previous"
3738                 "unwind_movsp directive"));
3739       return;
3740     }
3741
3742   /* Don't generate any opcodes, just record the information for later.  */
3743   unwind.fp_reg = fp_reg;
3744   unwind.fp_used = 1;
3745   if (sp_reg == 13)
3746     unwind.fp_offset = unwind.frame_size - offset;
3747   else
3748     unwind.fp_offset -= offset;
3749 }
3750
3751 /* Parse an unwind_raw directive.  */
3752
3753 static void
3754 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
3755 {
3756   expressionS exp;
3757   /* This is an arbitary limit.  */
3758   unsigned char op[16];
3759   int count;
3760
3761   expression (&exp);
3762   if (exp.X_op == O_constant
3763       && skip_past_comma (&input_line_pointer) != FAIL)
3764     {
3765       unwind.frame_size += exp.X_add_number;
3766       expression (&exp);
3767     }
3768   else
3769     exp.X_op = O_illegal;
3770
3771   if (exp.X_op != O_constant)
3772     {
3773       as_bad (_("expected <offset>, <opcode>"));
3774       ignore_rest_of_line ();
3775       return;
3776     }
3777
3778   count = 0;
3779
3780   /* Parse the opcode.  */
3781   for (;;)
3782     {
3783       if (count >= 16)
3784         {
3785           as_bad (_("unwind opcode too long"));
3786           ignore_rest_of_line ();
3787         }
3788       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
3789         {
3790           as_bad (_("invalid unwind opcode"));
3791           ignore_rest_of_line ();
3792           return;
3793         }
3794       op[count++] = exp.X_add_number;
3795
3796       /* Parse the next byte.  */
3797       if (skip_past_comma (&input_line_pointer) == FAIL)
3798         break;
3799
3800       expression (&exp);
3801     }
3802
3803   /* Add the opcode bytes in reverse order.  */
3804   while (count--)
3805     add_unwind_opcode (op[count], 1);
3806
3807   demand_empty_rest_of_line ();
3808 }
3809
3810
3811 /* Parse a .eabi_attribute directive.  */
3812
3813 static void
3814 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
3815 {
3816   expressionS exp;
3817   bfd_boolean is_string;
3818   int tag;
3819   unsigned int i = 0;
3820   char *s = NULL;
3821   char saved_char;
3822
3823   expression (& exp);
3824   if (exp.X_op != O_constant)
3825     goto bad;
3826
3827   tag = exp.X_add_number;
3828   if (tag == 4 || tag == 5 || tag == 32 || (tag > 32 && (tag & 1) != 0))
3829     is_string = 1;
3830   else
3831     is_string = 0;
3832
3833   if (skip_past_comma (&input_line_pointer) == FAIL)
3834     goto bad;
3835   if (tag == 32 || !is_string)
3836     {
3837       expression (& exp);
3838       if (exp.X_op != O_constant)
3839         {
3840           as_bad (_("expected numeric constant"));
3841           ignore_rest_of_line ();
3842           return;
3843         }
3844       i = exp.X_add_number;
3845     }
3846   if (tag == Tag_compatibility
3847       && skip_past_comma (&input_line_pointer) == FAIL)
3848     {
3849       as_bad (_("expected comma"));
3850       ignore_rest_of_line ();
3851       return;
3852     }
3853   if (is_string)
3854     {
3855       skip_whitespace(input_line_pointer);
3856       if (*input_line_pointer != '"')
3857         goto bad_string;
3858       input_line_pointer++;
3859       s = input_line_pointer;
3860       while (*input_line_pointer && *input_line_pointer != '"')
3861         input_line_pointer++;
3862       if (*input_line_pointer != '"')
3863         goto bad_string;
3864       saved_char = *input_line_pointer;
3865       *input_line_pointer = 0;
3866     }
3867   else
3868     {
3869       s = NULL;
3870       saved_char = 0;
3871     }
3872   
3873   if (tag == Tag_compatibility)
3874     elf32_arm_add_eabi_attr_compat (stdoutput, i, s);
3875   else if (is_string)
3876     elf32_arm_add_eabi_attr_string (stdoutput, tag, s);
3877   else
3878     elf32_arm_add_eabi_attr_int (stdoutput, tag, i);
3879
3880   if (s)
3881     {
3882       *input_line_pointer = saved_char;
3883       input_line_pointer++;
3884     }
3885   demand_empty_rest_of_line ();
3886   return;
3887 bad_string:
3888   as_bad (_("bad string constant"));
3889   ignore_rest_of_line ();
3890   return;
3891 bad:
3892   as_bad (_("expected <tag> , <value>"));
3893   ignore_rest_of_line ();
3894 }
3895
3896 static void s_arm_arch (int);
3897 static void s_arm_object_arch (int);
3898 static void s_arm_cpu (int);
3899 static void s_arm_fpu (int);
3900 #endif /* OBJ_ELF */
3901
3902 /* This table describes all the machine specific pseudo-ops the assembler
3903    has to support.  The fields are:
3904      pseudo-op name without dot
3905      function to call to execute this pseudo-op
3906      Integer arg to pass to the function.  */
3907
3908 const pseudo_typeS md_pseudo_table[] =
3909 {
3910   /* Never called because '.req' does not start a line.  */
3911   { "req",         s_req,         0 },
3912   /* Following two are likewise never called.  */
3913   { "dn",          s_dn,          0 },
3914   { "qn",          s_qn,          0 },
3915   { "unreq",       s_unreq,       0 },
3916   { "bss",         s_bss,         0 },
3917   { "align",       s_align,       0 },
3918   { "arm",         s_arm,         0 },
3919   { "thumb",       s_thumb,       0 },
3920   { "code",        s_code,        0 },
3921   { "force_thumb", s_force_thumb, 0 },
3922   { "thumb_func",  s_thumb_func,  0 },
3923   { "thumb_set",   s_thumb_set,   0 },
3924   { "even",        s_even,        0 },
3925   { "ltorg",       s_ltorg,       0 },
3926   { "pool",        s_ltorg,       0 },
3927   { "syntax",      s_syntax,      0 },
3928 #ifdef OBJ_ELF
3929   { "word",        s_arm_elf_cons, 4 },
3930   { "long",        s_arm_elf_cons, 4 },
3931   { "rel31",       s_arm_rel31,   0 },
3932   { "fnstart",          s_arm_unwind_fnstart,   0 },
3933   { "fnend",            s_arm_unwind_fnend,     0 },
3934   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
3935   { "personality",      s_arm_unwind_personality, 0 },
3936   { "personalityindex", s_arm_unwind_personalityindex, 0 },
3937   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
3938   { "save",             s_arm_unwind_save,      0 },
3939   { "vsave",            s_arm_unwind_save,      1 },
3940   { "movsp",            s_arm_unwind_movsp,     0 },
3941   { "pad",              s_arm_unwind_pad,       0 },
3942   { "setfp",            s_arm_unwind_setfp,     0 },
3943   { "unwind_raw",       s_arm_unwind_raw,       0 },
3944   { "cpu",              s_arm_cpu,              0 },
3945   { "arch",             s_arm_arch,             0 },
3946   { "object_arch",      s_arm_object_arch,      0 },
3947   { "fpu",              s_arm_fpu,              0 },
3948   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
3949 #else
3950   { "word",        cons, 4},
3951 #endif
3952   { "extend",      float_cons, 'x' },
3953   { "ldouble",     float_cons, 'x' },
3954   { "packed",      float_cons, 'p' },
3955   { 0, 0, 0 }
3956 };
3957 \f
3958 /* Parser functions used exclusively in instruction operands.  */
3959
3960 /* Generic immediate-value read function for use in insn parsing.
3961    STR points to the beginning of the immediate (the leading #);
3962    VAL receives the value; if the value is outside [MIN, MAX]
3963    issue an error.  PREFIX_OPT is true if the immediate prefix is
3964    optional.  */
3965
3966 static int
3967 parse_immediate (char **str, int *val, int min, int max,
3968                  bfd_boolean prefix_opt)
3969 {
3970   expressionS exp;
3971   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
3972   if (exp.X_op != O_constant)
3973     {
3974       inst.error = _("constant expression required");
3975       return FAIL;
3976     }
3977
3978   if (exp.X_add_number < min || exp.X_add_number > max)
3979     {
3980       inst.error = _("immediate value out of range");
3981       return FAIL;
3982     }
3983
3984   *val = exp.X_add_number;
3985   return SUCCESS;
3986 }
3987
3988 /* Less-generic immediate-value read function with the possibility of loading a
3989    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
3990    instructions. Puts the result directly in inst.operands[i].  */
3991
3992 static int
3993 parse_big_immediate (char **str, int i)
3994 {
3995   expressionS exp;
3996   char *ptr = *str;
3997
3998   my_get_expression (&exp, &ptr, GE_OPT_PREFIX_BIG);
3999
4000   if (exp.X_op == O_constant)
4001     {
4002       inst.operands[i].imm = exp.X_add_number & 0xffffffff;
4003       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4004          O_constant.  We have to be careful not to break compilation for
4005          32-bit X_add_number, though.  */
4006       if ((exp.X_add_number & ~0xffffffffl) != 0)
4007         {
4008           /* X >> 32 is illegal if sizeof (exp.X_add_number) == 4.  */
4009           inst.operands[i].reg = ((exp.X_add_number >> 16) >> 16) & 0xffffffff;
4010           inst.operands[i].regisimm = 1;
4011         }
4012     }
4013   else if (exp.X_op == O_big
4014            && LITTLENUM_NUMBER_OF_BITS * exp.X_add_number > 32
4015            && LITTLENUM_NUMBER_OF_BITS * exp.X_add_number <= 64)
4016     {
4017       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4018       /* Bignums have their least significant bits in
4019          generic_bignum[0]. Make sure we put 32 bits in imm and
4020          32 bits in reg,  in a (hopefully) portable way.  */
4021       assert (parts != 0);
4022       inst.operands[i].imm = 0;
4023       for (j = 0; j < parts; j++, idx++)
4024         inst.operands[i].imm |= generic_bignum[idx]
4025                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4026       inst.operands[i].reg = 0;
4027       for (j = 0; j < parts; j++, idx++)
4028         inst.operands[i].reg |= generic_bignum[idx]
4029                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4030       inst.operands[i].regisimm = 1;
4031     }
4032   else
4033     return FAIL;
4034   
4035   *str = ptr;
4036
4037   return SUCCESS;
4038 }
4039
4040 /* Returns the pseudo-register number of an FPA immediate constant,
4041    or FAIL if there isn't a valid constant here.  */
4042
4043 static int
4044 parse_fpa_immediate (char ** str)
4045 {
4046   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4047   char *         save_in;
4048   expressionS    exp;
4049   int            i;
4050   int            j;
4051
4052   /* First try and match exact strings, this is to guarantee
4053      that some formats will work even for cross assembly.  */
4054
4055   for (i = 0; fp_const[i]; i++)
4056     {
4057       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4058         {
4059           char *start = *str;
4060
4061           *str += strlen (fp_const[i]);
4062           if (is_end_of_line[(unsigned char) **str])
4063             return i + 8;
4064           *str = start;
4065         }
4066     }
4067
4068   /* Just because we didn't get a match doesn't mean that the constant
4069      isn't valid, just that it is in a format that we don't
4070      automatically recognize.  Try parsing it with the standard
4071      expression routines.  */
4072
4073   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4074
4075   /* Look for a raw floating point number.  */
4076   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4077       && is_end_of_line[(unsigned char) *save_in])
4078     {
4079       for (i = 0; i < NUM_FLOAT_VALS; i++)
4080         {
4081           for (j = 0; j < MAX_LITTLENUMS; j++)
4082             {
4083               if (words[j] != fp_values[i][j])
4084                 break;
4085             }
4086
4087           if (j == MAX_LITTLENUMS)
4088             {
4089               *str = save_in;
4090               return i + 8;
4091             }
4092         }
4093     }
4094
4095   /* Try and parse a more complex expression, this will probably fail
4096      unless the code uses a floating point prefix (eg "0f").  */
4097   save_in = input_line_pointer;
4098   input_line_pointer = *str;
4099   if (expression (&exp) == absolute_section
4100       && exp.X_op == O_big
4101       && exp.X_add_number < 0)
4102     {
4103       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4104          Ditto for 15.  */
4105       if (gen_to_words (words, 5, (long) 15) == 0)
4106         {
4107           for (i = 0; i < NUM_FLOAT_VALS; i++)
4108             {
4109               for (j = 0; j < MAX_LITTLENUMS; j++)
4110                 {
4111                   if (words[j] != fp_values[i][j])
4112                     break;
4113                 }
4114
4115               if (j == MAX_LITTLENUMS)
4116                 {
4117                   *str = input_line_pointer;
4118                   input_line_pointer = save_in;
4119                   return i + 8;
4120                 }
4121             }
4122         }
4123     }
4124
4125   *str = input_line_pointer;
4126   input_line_pointer = save_in;
4127   inst.error = _("invalid FPA immediate expression");
4128   return FAIL;
4129 }
4130
4131 /* Returns 1 if a number has "quarter-precision" float format
4132    0baBbbbbbc defgh000 00000000 00000000.  */
4133
4134 static int
4135 is_quarter_float (unsigned imm)
4136 {
4137   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4138   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4139 }
4140
4141 /* Parse an 8-bit "quarter-precision" floating point number of the form:
4142    0baBbbbbbc defgh000 00000000 00000000.
4143    The zero and minus-zero cases need special handling, since they can't be
4144    encoded in the "quarter-precision" float format, but can nonetheless be
4145    loaded as integer constants.  */
4146
4147 static unsigned
4148 parse_qfloat_immediate (char **ccp, int *immed)
4149 {
4150   char *str = *ccp;
4151   char *fpnum;
4152   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4153   int found_fpchar = 0;
4154   
4155   skip_past_char (&str, '#');
4156   
4157   /* We must not accidentally parse an integer as a floating-point number. Make
4158      sure that the value we parse is not an integer by checking for special
4159      characters '.' or 'e'.
4160      FIXME: This is a horrible hack, but doing better is tricky because type
4161      information isn't in a very usable state at parse time.  A better solution
4162      should be implemented as part of the fix for allowing the full range of
4163      pseudo-instructions to be used in VMOV, etc.  */
4164   fpnum = str;
4165   skip_whitespace (fpnum);
4166
4167   if (strncmp (fpnum, "0x", 2) == 0)
4168     return FAIL;
4169   else
4170     {
4171       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
4172         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
4173           {
4174             found_fpchar = 1;
4175             break;
4176           }
4177
4178       if (!found_fpchar)
4179         return FAIL;
4180     }
4181   
4182   if ((str = atof_ieee (str, 's', words)) != NULL)
4183     {
4184       unsigned fpword = 0;
4185       int i;
4186       
4187       /* Our FP word must be 32 bits (single-precision FP).  */
4188       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
4189         {
4190           fpword <<= LITTLENUM_NUMBER_OF_BITS;
4191           fpword |= words[i];
4192         }
4193       
4194       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
4195         *immed = fpword;
4196       else
4197         return FAIL;
4198
4199       *ccp = str;
4200       
4201       return SUCCESS;
4202     }
4203   
4204   return FAIL;
4205 }
4206
4207 /* Shift operands.  */
4208 enum shift_kind
4209 {
4210   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
4211 };
4212
4213 struct asm_shift_name
4214 {
4215   const char      *name;
4216   enum shift_kind  kind;
4217 };
4218
4219 /* Third argument to parse_shift.  */
4220 enum parse_shift_mode
4221 {
4222   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
4223   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
4224   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
4225   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
4226   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
4227 };
4228
4229 /* Parse a <shift> specifier on an ARM data processing instruction.
4230    This has three forms:
4231
4232      (LSL|LSR|ASL|ASR|ROR) Rs
4233      (LSL|LSR|ASL|ASR|ROR) #imm
4234      RRX
4235
4236    Note that ASL is assimilated to LSL in the instruction encoding, and
4237    RRX to ROR #0 (which cannot be written as such).  */
4238
4239 static int
4240 parse_shift (char **str, int i, enum parse_shift_mode mode)
4241 {
4242   const struct asm_shift_name *shift_name;
4243   enum shift_kind shift;
4244   char *s = *str;
4245   char *p = s;
4246   int reg;
4247
4248   for (p = *str; ISALPHA (*p); p++)
4249     ;
4250
4251   if (p == *str)
4252     {
4253       inst.error = _("shift expression expected");
4254       return FAIL;
4255     }
4256
4257   shift_name = hash_find_n (arm_shift_hsh, *str, p - *str);
4258
4259   if (shift_name == NULL)
4260     {
4261       inst.error = _("shift expression expected");
4262       return FAIL;
4263     }
4264
4265   shift = shift_name->kind;
4266
4267   switch (mode)
4268     {
4269     case NO_SHIFT_RESTRICT:
4270     case SHIFT_IMMEDIATE:   break;
4271
4272     case SHIFT_LSL_OR_ASR_IMMEDIATE:
4273       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
4274         {
4275           inst.error = _("'LSL' or 'ASR' required");
4276           return FAIL;
4277         }
4278       break;
4279
4280     case SHIFT_LSL_IMMEDIATE:
4281       if (shift != SHIFT_LSL)
4282         {
4283           inst.error = _("'LSL' required");
4284           return FAIL;
4285         }
4286       break;
4287
4288     case SHIFT_ASR_IMMEDIATE:
4289       if (shift != SHIFT_ASR)
4290         {
4291           inst.error = _("'ASR' required");
4292           return FAIL;
4293         }
4294       break;
4295
4296     default: abort ();
4297     }
4298
4299   if (shift != SHIFT_RRX)
4300     {
4301       /* Whitespace can appear here if the next thing is a bare digit.  */
4302       skip_whitespace (p);
4303
4304       if (mode == NO_SHIFT_RESTRICT
4305           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
4306         {
4307           inst.operands[i].imm = reg;
4308           inst.operands[i].immisreg = 1;
4309         }
4310       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
4311         return FAIL;
4312     }
4313   inst.operands[i].shift_kind = shift;
4314   inst.operands[i].shifted = 1;
4315   *str = p;
4316   return SUCCESS;
4317 }
4318
4319 /* Parse a <shifter_operand> for an ARM data processing instruction:
4320
4321       #<immediate>
4322       #<immediate>, <rotate>
4323       <Rm>
4324       <Rm>, <shift>
4325
4326    where <shift> is defined by parse_shift above, and <rotate> is a
4327    multiple of 2 between 0 and 30.  Validation of immediate operands
4328    is deferred to md_apply_fix.  */
4329
4330 static int
4331 parse_shifter_operand (char **str, int i)
4332 {
4333   int value;
4334   expressionS expr;
4335
4336   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
4337     {
4338       inst.operands[i].reg = value;
4339       inst.operands[i].isreg = 1;
4340
4341       /* parse_shift will override this if appropriate */
4342       inst.reloc.exp.X_op = O_constant;
4343       inst.reloc.exp.X_add_number = 0;
4344
4345       if (skip_past_comma (str) == FAIL)
4346         return SUCCESS;
4347
4348       /* Shift operation on register.  */
4349       return parse_shift (str, i, NO_SHIFT_RESTRICT);
4350     }
4351
4352   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
4353     return FAIL;
4354
4355   if (skip_past_comma (str) == SUCCESS)
4356     {
4357       /* #x, y -- ie explicit rotation by Y.  */
4358       if (my_get_expression (&expr, str, GE_NO_PREFIX))
4359         return FAIL;
4360
4361       if (expr.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
4362         {
4363           inst.error = _("constant expression expected");
4364           return FAIL;
4365         }
4366
4367       value = expr.X_add_number;
4368       if (value < 0 || value > 30 || value % 2 != 0)
4369         {
4370           inst.error = _("invalid rotation");
4371           return FAIL;
4372         }
4373       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
4374         {
4375           inst.error = _("invalid constant");
4376           return FAIL;
4377         }
4378
4379       /* Convert to decoded value.  md_apply_fix will put it back.  */
4380       inst.reloc.exp.X_add_number
4381         = (((inst.reloc.exp.X_add_number << (32 - value))
4382             | (inst.reloc.exp.X_add_number >> value)) & 0xffffffff);
4383     }
4384
4385   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
4386   inst.reloc.pc_rel = 0;
4387   return SUCCESS;
4388 }
4389
4390 /* Group relocation information.  Each entry in the table contains the
4391    textual name of the relocation as may appear in assembler source
4392    and must end with a colon.
4393    Along with this textual name are the relocation codes to be used if
4394    the corresponding instruction is an ALU instruction (ADD or SUB only),
4395    an LDR, an LDRS, or an LDC.  */
4396
4397 struct group_reloc_table_entry
4398 {
4399   const char *name;
4400   int alu_code;
4401   int ldr_code;
4402   int ldrs_code;
4403   int ldc_code;
4404 };
4405
4406 typedef enum
4407 {
4408   /* Varieties of non-ALU group relocation.  */
4409
4410   GROUP_LDR,
4411   GROUP_LDRS,
4412   GROUP_LDC
4413 } group_reloc_type;
4414
4415 static struct group_reloc_table_entry group_reloc_table[] =
4416   { /* Program counter relative: */
4417     { "pc_g0_nc",
4418       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
4419       0,                                /* LDR */
4420       0,                                /* LDRS */
4421       0 },                              /* LDC */
4422     { "pc_g0",
4423       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
4424       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
4425       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
4426       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
4427     { "pc_g1_nc",
4428       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
4429       0,                                /* LDR */
4430       0,                                /* LDRS */
4431       0 },                              /* LDC */
4432     { "pc_g1",
4433       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
4434       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
4435       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
4436       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
4437     { "pc_g2",
4438       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
4439       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
4440       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
4441       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
4442     /* Section base relative */
4443     { "sb_g0_nc",
4444       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
4445       0,                                /* LDR */
4446       0,                                /* LDRS */
4447       0 },                              /* LDC */
4448     { "sb_g0",
4449       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
4450       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
4451       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
4452       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
4453     { "sb_g1_nc",
4454       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
4455       0,                                /* LDR */
4456       0,                                /* LDRS */
4457       0 },                              /* LDC */
4458     { "sb_g1",
4459       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
4460       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
4461       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
4462       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
4463     { "sb_g2",
4464       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
4465       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
4466       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
4467       BFD_RELOC_ARM_LDC_SB_G2 } };      /* LDC */
4468
4469 /* Given the address of a pointer pointing to the textual name of a group
4470    relocation as may appear in assembler source, attempt to find its details
4471    in group_reloc_table.  The pointer will be updated to the character after
4472    the trailing colon.  On failure, FAIL will be returned; SUCCESS
4473    otherwise.  On success, *entry will be updated to point at the relevant
4474    group_reloc_table entry. */
4475
4476 static int
4477 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
4478 {
4479   unsigned int i;
4480   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
4481     {
4482       int length = strlen (group_reloc_table[i].name);
4483
4484       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0 &&
4485           (*str)[length] == ':')
4486         {
4487           *out = &group_reloc_table[i];
4488           *str += (length + 1);
4489           return SUCCESS;
4490         }
4491     }
4492
4493   return FAIL;
4494 }
4495
4496 /* Parse a <shifter_operand> for an ARM data processing instruction
4497    (as for parse_shifter_operand) where group relocations are allowed:
4498
4499       #<immediate>
4500       #<immediate>, <rotate>
4501       #:<group_reloc>:<expression>
4502       <Rm>
4503       <Rm>, <shift>
4504
4505    where <group_reloc> is one of the strings defined in group_reloc_table.
4506    The hashes are optional.
4507
4508    Everything else is as for parse_shifter_operand.  */
4509
4510 static parse_operand_result
4511 parse_shifter_operand_group_reloc (char **str, int i)
4512 {
4513   /* Determine if we have the sequence of characters #: or just :
4514      coming next.  If we do, then we check for a group relocation.
4515      If we don't, punt the whole lot to parse_shifter_operand.  */
4516
4517   if (((*str)[0] == '#' && (*str)[1] == ':')
4518       || (*str)[0] == ':')
4519     {
4520       struct group_reloc_table_entry *entry;
4521
4522       if ((*str)[0] == '#')
4523         (*str) += 2;
4524       else
4525         (*str)++;
4526
4527       /* Try to parse a group relocation.  Anything else is an error.  */
4528       if (find_group_reloc_table_entry (str, &entry) == FAIL)
4529         {
4530           inst.error = _("unknown group relocation");
4531           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
4532         }
4533
4534       /* We now have the group relocation table entry corresponding to
4535          the name in the assembler source.  Next, we parse the expression.  */
4536       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
4537         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
4538
4539       /* Record the relocation type (always the ALU variant here).  */
4540       inst.reloc.type = entry->alu_code;
4541       assert (inst.reloc.type != 0);
4542
4543       return PARSE_OPERAND_SUCCESS;
4544     }
4545   else
4546     return parse_shifter_operand (str, i) == SUCCESS
4547            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
4548
4549   /* Never reached.  */
4550 }
4551
4552 /* Parse all forms of an ARM address expression.  Information is written
4553    to inst.operands[i] and/or inst.reloc.
4554
4555    Preindexed addressing (.preind=1):
4556
4557    [Rn, #offset]       .reg=Rn .reloc.exp=offset
4558    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
4559    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
4560                        .shift_kind=shift .reloc.exp=shift_imm
4561
4562    These three may have a trailing ! which causes .writeback to be set also.
4563
4564    Postindexed addressing (.postind=1, .writeback=1):
4565
4566    [Rn], #offset       .reg=Rn .reloc.exp=offset
4567    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
4568    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
4569                        .shift_kind=shift .reloc.exp=shift_imm
4570
4571    Unindexed addressing (.preind=0, .postind=0):
4572
4573    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
4574
4575    Other:
4576
4577    [Rn]{!}             shorthand for [Rn,#0]{!}
4578    =immediate          .isreg=0 .reloc.exp=immediate
4579    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
4580
4581   It is the caller's responsibility to check for addressing modes not
4582   supported by the instruction, and to set inst.reloc.type.  */
4583
4584 static parse_operand_result
4585 parse_address_main (char **str, int i, int group_relocations,
4586                     group_reloc_type group_type)
4587 {
4588   char *p = *str;
4589   int reg;
4590
4591   if (skip_past_char (&p, '[') == FAIL)
4592     {
4593       if (skip_past_char (&p, '=') == FAIL)
4594         {
4595           /* bare address - translate to PC-relative offset */
4596           inst.reloc.pc_rel = 1;
4597           inst.operands[i].reg = REG_PC;
4598           inst.operands[i].isreg = 1;
4599           inst.operands[i].preind = 1;
4600         }
4601       /* else a load-constant pseudo op, no special treatment needed here */
4602
4603       if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
4604         return PARSE_OPERAND_FAIL;
4605
4606       *str = p;
4607       return PARSE_OPERAND_SUCCESS;
4608     }
4609
4610   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
4611     {
4612       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
4613       return PARSE_OPERAND_FAIL;
4614     }
4615   inst.operands[i].reg = reg;
4616   inst.operands[i].isreg = 1;
4617
4618   if (skip_past_comma (&p) == SUCCESS)
4619     {
4620       inst.operands[i].preind = 1;
4621
4622       if (*p == '+') p++;
4623       else if (*p == '-') p++, inst.operands[i].negative = 1;
4624
4625       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
4626         {
4627           inst.operands[i].imm = reg;
4628           inst.operands[i].immisreg = 1;
4629
4630           if (skip_past_comma (&p) == SUCCESS)
4631             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
4632               return PARSE_OPERAND_FAIL;
4633         }
4634       else if (skip_past_char (&p, ':') == SUCCESS)
4635         {
4636           /* FIXME: '@' should be used here, but it's filtered out by generic
4637              code before we get to see it here. This may be subject to
4638              change.  */
4639           expressionS exp;
4640           my_get_expression (&exp, &p, GE_NO_PREFIX);
4641           if (exp.X_op != O_constant)
4642             {
4643               inst.error = _("alignment must be constant");
4644               return PARSE_OPERAND_FAIL;
4645             }
4646           inst.operands[i].imm = exp.X_add_number << 8;
4647           inst.operands[i].immisalign = 1;
4648           /* Alignments are not pre-indexes.  */
4649           inst.operands[i].preind = 0;
4650         }
4651       else
4652         {
4653           if (inst.operands[i].negative)
4654             {
4655               inst.operands[i].negative = 0;
4656               p--;
4657             }
4658
4659           if (group_relocations &&
4660               ((*p == '#' && *(p + 1) == ':') || *p == ':'))
4661
4662             {
4663               struct group_reloc_table_entry *entry;
4664
4665               /* Skip over the #: or : sequence.  */
4666               if (*p == '#')
4667                 p += 2;
4668               else
4669                 p++;
4670
4671               /* Try to parse a group relocation.  Anything else is an
4672                  error.  */
4673               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
4674                 {
4675                   inst.error = _("unknown group relocation");
4676                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
4677                 }
4678
4679               /* We now have the group relocation table entry corresponding to
4680                  the name in the assembler source.  Next, we parse the
4681                  expression.  */
4682               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
4683                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
4684
4685               /* Record the relocation type.  */
4686               switch (group_type)
4687                 {
4688                   case GROUP_LDR:
4689                     inst.reloc.type = entry->ldr_code;
4690                     break;
4691
4692                   case GROUP_LDRS:
4693                     inst.reloc.type = entry->ldrs_code;
4694                     break;
4695
4696                   case GROUP_LDC:
4697                     inst.reloc.type = entry->ldc_code;
4698                     break;
4699
4700                   default:
4701                     assert (0);
4702                 }
4703
4704               if (inst.reloc.type == 0)
4705                 {
4706                   inst.error = _("this group relocation is not allowed on this instruction");
4707                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
4708                 }
4709             }
4710           else
4711             if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
4712               return PARSE_OPERAND_FAIL;
4713         }
4714     }
4715
4716   if (skip_past_char (&p, ']') == FAIL)
4717     {
4718       inst.error = _("']' expected");
4719       return PARSE_OPERAND_FAIL;
4720     }
4721
4722   if (skip_past_char (&p, '!') == SUCCESS)
4723     inst.operands[i].writeback = 1;
4724
4725   else if (skip_past_comma (&p) == SUCCESS)
4726     {
4727       if (skip_past_char (&p, '{') == SUCCESS)
4728         {
4729           /* [Rn], {expr} - unindexed, with option */
4730           if (parse_immediate (&p, &inst.operands[i].imm,
4731                                0, 255, TRUE) == FAIL)
4732             return PARSE_OPERAND_FAIL;
4733
4734           if (skip_past_char (&p, '}') == FAIL)
4735             {
4736               inst.error = _("'}' expected at end of 'option' field");
4737               return PARSE_OPERAND_FAIL;
4738             }
4739           if (inst.operands[i].preind)
4740             {
4741               inst.error = _("cannot combine index with option");
4742               return PARSE_OPERAND_FAIL;
4743             }
4744           *str = p;
4745           return PARSE_OPERAND_SUCCESS;
4746         }
4747       else
4748         {
4749           inst.operands[i].postind = 1;
4750           inst.operands[i].writeback = 1;
4751
4752           if (inst.operands[i].preind)
4753             {
4754               inst.error = _("cannot combine pre- and post-indexing");
4755               return PARSE_OPERAND_FAIL;
4756             }
4757
4758           if (*p == '+') p++;
4759           else if (*p == '-') p++, inst.operands[i].negative = 1;
4760
4761           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
4762             {
4763               /* We might be using the immediate for alignment already. If we
4764                  are, OR the register number into the low-order bits.  */
4765               if (inst.operands[i].immisalign)
4766                 inst.operands[i].imm |= reg;
4767               else
4768                 inst.operands[i].imm = reg;
4769               inst.operands[i].immisreg = 1;
4770
4771               if (skip_past_comma (&p) == SUCCESS)
4772                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
4773                   return PARSE_OPERAND_FAIL;
4774             }
4775           else
4776             {
4777               if (inst.operands[i].negative)
4778                 {
4779                   inst.operands[i].negative = 0;
4780                   p--;
4781                 }
4782               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
4783                 return PARSE_OPERAND_FAIL;
4784             }
4785         }
4786     }
4787
4788   /* If at this point neither .preind nor .postind is set, we have a
4789      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
4790   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
4791     {
4792       inst.operands[i].preind = 1;
4793       inst.reloc.exp.X_op = O_constant;
4794       inst.reloc.exp.X_add_number = 0;
4795     }
4796   *str = p;
4797   return PARSE_OPERAND_SUCCESS;
4798 }
4799
4800 static int
4801 parse_address (char **str, int i)
4802 {
4803   return parse_address_main (str, i, 0, 0) == PARSE_OPERAND_SUCCESS
4804          ? SUCCESS : FAIL;
4805 }
4806
4807 static parse_operand_result
4808 parse_address_group_reloc (char **str, int i, group_reloc_type type)
4809 {
4810   return parse_address_main (str, i, 1, type);
4811 }
4812
4813 /* Parse an operand for a MOVW or MOVT instruction.  */
4814 static int
4815 parse_half (char **str)
4816 {
4817   char * p;
4818   
4819   p = *str;
4820   skip_past_char (&p, '#');
4821   if (strncasecmp (p, ":lower16:", 9) == 0) 
4822     inst.reloc.type = BFD_RELOC_ARM_MOVW;
4823   else if (strncasecmp (p, ":upper16:", 9) == 0)
4824     inst.reloc.type = BFD_RELOC_ARM_MOVT;
4825
4826   if (inst.reloc.type != BFD_RELOC_UNUSED)
4827     {
4828       p += 9;
4829       skip_whitespace(p);
4830     }
4831
4832   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
4833     return FAIL;
4834
4835   if (inst.reloc.type == BFD_RELOC_UNUSED)
4836     {
4837       if (inst.reloc.exp.X_op != O_constant)
4838         {
4839           inst.error = _("constant expression expected");
4840           return FAIL;
4841         }
4842       if (inst.reloc.exp.X_add_number < 0
4843           || inst.reloc.exp.X_add_number > 0xffff)
4844         {
4845           inst.error = _("immediate value out of range");
4846           return FAIL;
4847         }
4848     }
4849   *str = p;
4850   return SUCCESS;
4851 }
4852
4853 /* Miscellaneous. */
4854
4855 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
4856    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
4857 static int
4858 parse_psr (char **str)
4859 {
4860   char *p;
4861   unsigned long psr_field;
4862   const struct asm_psr *psr;
4863   char *start;
4864
4865   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
4866      feature for ease of use and backwards compatibility.  */
4867   p = *str;
4868   if (strncasecmp (p, "SPSR", 4) == 0)
4869     psr_field = SPSR_BIT;
4870   else if (strncasecmp (p, "CPSR", 4) == 0)
4871     psr_field = 0;
4872   else
4873     {
4874       start = p;
4875       do
4876         p++;
4877       while (ISALNUM (*p) || *p == '_');
4878
4879       psr = hash_find_n (arm_v7m_psr_hsh, start, p - start);
4880       if (!psr)
4881         return FAIL;
4882
4883       *str = p;
4884       return psr->field;
4885     }
4886
4887   p += 4;
4888   if (*p == '_')
4889     {
4890       /* A suffix follows.  */
4891       p++;
4892       start = p;
4893
4894       do
4895         p++;
4896       while (ISALNUM (*p) || *p == '_');
4897
4898       psr = hash_find_n (arm_psr_hsh, start, p - start);
4899       if (!psr)
4900         goto error;
4901
4902       psr_field |= psr->field;
4903     }
4904   else
4905     {
4906       if (ISALNUM (*p))
4907         goto error;    /* Garbage after "[CS]PSR".  */
4908
4909       psr_field |= (PSR_c | PSR_f);
4910     }
4911   *str = p;
4912   return psr_field;
4913
4914  error:
4915   inst.error = _("flag for {c}psr instruction expected");
4916   return FAIL;
4917 }
4918
4919 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
4920    value suitable for splatting into the AIF field of the instruction.  */
4921
4922 static int
4923 parse_cps_flags (char **str)
4924 {
4925   int val = 0;
4926   int saw_a_flag = 0;
4927   char *s = *str;
4928
4929   for (;;)
4930     switch (*s++)
4931       {
4932       case '\0': case ',':
4933         goto done;
4934
4935       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
4936       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
4937       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
4938
4939       default:
4940         inst.error = _("unrecognized CPS flag");
4941         return FAIL;
4942       }
4943
4944  done:
4945   if (saw_a_flag == 0)
4946     {
4947       inst.error = _("missing CPS flags");
4948       return FAIL;
4949     }
4950
4951   *str = s - 1;
4952   return val;
4953 }
4954
4955 /* Parse an endian specifier ("BE" or "LE", case insensitive);
4956    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
4957
4958 static int
4959 parse_endian_specifier (char **str)
4960 {
4961   int little_endian;
4962   char *s = *str;
4963
4964   if (strncasecmp (s, "BE", 2))
4965     little_endian = 0;
4966   else if (strncasecmp (s, "LE", 2))
4967     little_endian = 1;
4968   else
4969     {
4970       inst.error = _("valid endian specifiers are be or le");
4971       return FAIL;
4972     }
4973
4974   if (ISALNUM (s[2]) || s[2] == '_')
4975     {
4976       inst.error = _("valid endian specifiers are be or le");
4977       return FAIL;
4978     }
4979
4980   *str = s + 2;
4981   return little_endian;
4982 }
4983
4984 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
4985    value suitable for poking into the rotate field of an sxt or sxta
4986    instruction, or FAIL on error.  */
4987
4988 static int
4989 parse_ror (char **str)
4990 {
4991   int rot;
4992   char *s = *str;
4993
4994   if (strncasecmp (s, "ROR", 3) == 0)
4995     s += 3;
4996   else
4997     {
4998       inst.error = _("missing rotation field after comma");
4999       return FAIL;
5000     }
5001
5002   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
5003     return FAIL;
5004
5005   switch (rot)
5006     {
5007     case  0: *str = s; return 0x0;
5008     case  8: *str = s; return 0x1;
5009     case 16: *str = s; return 0x2;
5010     case 24: *str = s; return 0x3;
5011
5012     default:
5013       inst.error = _("rotation can only be 0, 8, 16, or 24");
5014       return FAIL;
5015     }
5016 }
5017
5018 /* Parse a conditional code (from conds[] below).  The value returned is in the
5019    range 0 .. 14, or FAIL.  */
5020 static int
5021 parse_cond (char **str)
5022 {
5023   char *p, *q;
5024   const struct asm_cond *c;
5025
5026   p = q = *str;
5027   while (ISALPHA (*q))
5028     q++;
5029
5030   c = hash_find_n (arm_cond_hsh, p, q - p);
5031   if (!c)
5032     {
5033       inst.error = _("condition required");
5034       return FAIL;
5035     }
5036
5037   *str = q;
5038   return c->value;
5039 }
5040
5041 /* Parse an option for a barrier instruction.  Returns the encoding for the
5042    option, or FAIL.  */
5043 static int
5044 parse_barrier (char **str)
5045 {
5046   char *p, *q;
5047   const struct asm_barrier_opt *o;
5048
5049   p = q = *str;
5050   while (ISALPHA (*q))
5051     q++;
5052
5053   o = hash_find_n (arm_barrier_opt_hsh, p, q - p);
5054   if (!o)
5055     return FAIL;
5056
5057   *str = q;
5058   return o->value;
5059 }
5060
5061 /* Parse the operands of a table branch instruction.  Similar to a memory
5062    operand.  */
5063 static int
5064 parse_tb (char **str)
5065 {
5066   char * p = *str;
5067   int reg;
5068
5069   if (skip_past_char (&p, '[') == FAIL)
5070     {
5071       inst.error = _("'[' expected");
5072       return FAIL;
5073     }
5074
5075   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5076     {
5077       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5078       return FAIL;
5079     }
5080   inst.operands[0].reg = reg;
5081
5082   if (skip_past_comma (&p) == FAIL)
5083     {
5084       inst.error = _("',' expected");
5085       return FAIL;
5086     }
5087   
5088   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5089     {
5090       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5091       return FAIL;
5092     }
5093   inst.operands[0].imm = reg;
5094
5095   if (skip_past_comma (&p) == SUCCESS)
5096     {
5097       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
5098         return FAIL;
5099       if (inst.reloc.exp.X_add_number != 1)
5100         {
5101           inst.error = _("invalid shift");
5102           return FAIL;
5103         }
5104       inst.operands[0].shifted = 1;
5105     }
5106
5107   if (skip_past_char (&p, ']') == FAIL)
5108     {
5109       inst.error = _("']' expected");
5110       return FAIL;
5111     }
5112   *str = p;
5113   return SUCCESS;
5114 }
5115
5116 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
5117    information on the types the operands can take and how they are encoded.
5118    Up to four operands may be read; this function handles setting the
5119    ".present" field for each read operand itself.
5120    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
5121    else returns FAIL.  */
5122
5123 static int
5124 parse_neon_mov (char **str, int *which_operand)
5125 {
5126   int i = *which_operand, val;
5127   enum arm_reg_type rtype;
5128   char *ptr = *str;
5129   struct neon_type_el optype;
5130   
5131   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
5132     {
5133       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
5134       inst.operands[i].reg = val;
5135       inst.operands[i].isscalar = 1;
5136       inst.operands[i].vectype = optype;
5137       inst.operands[i++].present = 1;
5138
5139       if (skip_past_comma (&ptr) == FAIL)
5140         goto wanted_comma;
5141       
5142       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5143         goto wanted_arm;
5144       
5145       inst.operands[i].reg = val;
5146       inst.operands[i].isreg = 1;
5147       inst.operands[i].present = 1;
5148     }
5149   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
5150            != FAIL)
5151     {
5152       /* Cases 0, 1, 2, 3, 5 (D only).  */
5153       if (skip_past_comma (&ptr) == FAIL)
5154         goto wanted_comma;
5155       
5156       inst.operands[i].reg = val;
5157       inst.operands[i].isreg = 1;
5158       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5159       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5160       inst.operands[i].isvec = 1;
5161       inst.operands[i].vectype = optype;
5162       inst.operands[i++].present = 1;
5163
5164       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5165         {
5166           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
5167              Case 13: VMOV <Sd>, <Rm>  */
5168           inst.operands[i].reg = val;
5169           inst.operands[i].isreg = 1;
5170           inst.operands[i].present = 1;
5171
5172           if (rtype == REG_TYPE_NQ)
5173             {
5174               first_error (_("can't use Neon quad register here"));
5175               return FAIL;
5176             }
5177           else if (rtype != REG_TYPE_VFS)
5178             {
5179               i++;
5180               if (skip_past_comma (&ptr) == FAIL)
5181                 goto wanted_comma;
5182               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5183                 goto wanted_arm;
5184               inst.operands[i].reg = val;
5185               inst.operands[i].isreg = 1;
5186               inst.operands[i].present = 1;
5187             }
5188         }
5189       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
5190           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
5191              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
5192              Case 10: VMOV.F32 <Sd>, #<imm>
5193              Case 11: VMOV.F64 <Dd>, #<imm>  */
5194         ;
5195       else if (parse_big_immediate (&ptr, i) == SUCCESS)
5196           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
5197              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
5198         ;
5199       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
5200                                            &optype)) != FAIL)
5201         {
5202           /* Case 0: VMOV<c><q> <Qd>, <Qm>
5203              Case 1: VMOV<c><q> <Dd>, <Dm>
5204              Case 8: VMOV.F32 <Sd>, <Sm>
5205              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
5206
5207           inst.operands[i].reg = val;
5208           inst.operands[i].isreg = 1;
5209           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
5210           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5211           inst.operands[i].isvec = 1;
5212           inst.operands[i].vectype = optype;
5213           inst.operands[i].present = 1;
5214           
5215           if (skip_past_comma (&ptr) == SUCCESS)
5216             {
5217               /* Case 15.  */
5218               i++;
5219
5220               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5221                 goto wanted_arm;
5222
5223               inst.operands[i].reg = val;
5224               inst.operands[i].isreg = 1;
5225               inst.operands[i++].present = 1;
5226               
5227               if (skip_past_comma (&ptr) == FAIL)
5228                 goto wanted_comma;
5229               
5230               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
5231                 goto wanted_arm;
5232               
5233               inst.operands[i].reg = val;
5234               inst.operands[i].isreg = 1;
5235               inst.operands[i++].present = 1;
5236             }
5237         }
5238       else
5239         {
5240           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
5241           return FAIL;
5242         }
5243     }
5244   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5245     {
5246       /* Cases 6, 7.  */
5247       inst.operands[i].reg = val;
5248       inst.operands[i].isreg = 1;
5249       inst.operands[i++].present = 1;
5250       
5251       if (skip_past_comma (&ptr) == FAIL)
5252         goto wanted_comma;
5253       
5254       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
5255         {
5256           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
5257           inst.operands[i].reg = val;
5258           inst.operands[i].isscalar = 1;
5259           inst.operands[i].present = 1;
5260           inst.operands[i].vectype = optype;
5261         }
5262       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
5263         {
5264           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
5265           inst.operands[i].reg = val;
5266           inst.operands[i].isreg = 1;
5267           inst.operands[i++].present = 1;
5268           
5269           if (skip_past_comma (&ptr) == FAIL)
5270             goto wanted_comma;
5271           
5272           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
5273               == FAIL)
5274             {
5275               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
5276               return FAIL;
5277             }
5278
5279           inst.operands[i].reg = val;
5280           inst.operands[i].isreg = 1;
5281           inst.operands[i].isvec = 1;
5282           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
5283           inst.operands[i].vectype = optype;
5284           inst.operands[i].present = 1;
5285           
5286           if (rtype == REG_TYPE_VFS)
5287             {
5288               /* Case 14.  */
5289               i++;
5290               if (skip_past_comma (&ptr) == FAIL)
5291                 goto wanted_comma;
5292               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
5293                                               &optype)) == FAIL)
5294                 {
5295                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
5296                   return FAIL;
5297                 }
5298               inst.operands[i].reg = val;
5299               inst.operands[i].isreg = 1;
5300               inst.operands[i].isvec = 1;
5301               inst.operands[i].issingle = 1;
5302               inst.operands[i].vectype = optype;
5303               inst.operands[i].present = 1;
5304             }
5305         }
5306       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
5307                != FAIL)
5308         {
5309           /* Case 13.  */
5310           inst.operands[i].reg = val;
5311           inst.operands[i].isreg = 1;
5312           inst.operands[i].isvec = 1;
5313           inst.operands[i].issingle = 1;
5314           inst.operands[i].vectype = optype;
5315           inst.operands[i++].present = 1;
5316         }
5317     }
5318   else
5319     {
5320       first_error (_("parse error"));
5321       return FAIL;
5322     }
5323
5324   /* Successfully parsed the operands. Update args.  */
5325   *which_operand = i;
5326   *str = ptr;
5327   return SUCCESS;
5328
5329   wanted_comma:
5330   first_error (_("expected comma"));
5331   return FAIL;
5332   
5333   wanted_arm:
5334   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
5335   return FAIL;
5336 }
5337
5338 /* Matcher codes for parse_operands.  */
5339 enum operand_parse_code
5340 {
5341   OP_stop,      /* end of line */
5342
5343   OP_RR,        /* ARM register */
5344   OP_RRnpc,     /* ARM register, not r15 */
5345   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
5346   OP_RRw,       /* ARM register, not r15, optional trailing ! */
5347   OP_RCP,       /* Coprocessor number */
5348   OP_RCN,       /* Coprocessor register */
5349   OP_RF,        /* FPA register */
5350   OP_RVS,       /* VFP single precision register */
5351   OP_RVD,       /* VFP double precision register (0..15) */
5352   OP_RND,       /* Neon double precision register (0..31) */
5353   OP_RNQ,       /* Neon quad precision register */
5354   OP_RVSD,      /* VFP single or double precision register */
5355   OP_RNDQ,      /* Neon double or quad precision register */
5356   OP_RNSDQ,     /* Neon single, double or quad precision register */
5357   OP_RNSC,      /* Neon scalar D[X] */
5358   OP_RVC,       /* VFP control register */
5359   OP_RMF,       /* Maverick F register */
5360   OP_RMD,       /* Maverick D register */
5361   OP_RMFX,      /* Maverick FX register */
5362   OP_RMDX,      /* Maverick DX register */
5363   OP_RMAX,      /* Maverick AX register */
5364   OP_RMDS,      /* Maverick DSPSC register */
5365   OP_RIWR,      /* iWMMXt wR register */
5366   OP_RIWC,      /* iWMMXt wC register */
5367   OP_RIWG,      /* iWMMXt wCG register */
5368   OP_RXA,       /* XScale accumulator register */
5369
5370   OP_REGLST,    /* ARM register list */
5371   OP_VRSLST,    /* VFP single-precision register list */
5372   OP_VRDLST,    /* VFP double-precision register list */
5373   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
5374   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
5375   OP_NSTRLST,   /* Neon element/structure list */
5376
5377   OP_NILO,      /* Neon immediate/logic operands 2 or 2+3. (VBIC, VORR...)  */
5378   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
5379   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
5380   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
5381   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
5382   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
5383   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
5384   OP_VMOV,      /* Neon VMOV operands.  */
5385   OP_RNDQ_IMVNb,/* Neon D or Q reg, or immediate good for VMVN.  */
5386   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
5387   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
5388
5389   OP_I0,        /* immediate zero */
5390   OP_I7,        /* immediate value 0 .. 7 */
5391   OP_I15,       /*                 0 .. 15 */
5392   OP_I16,       /*                 1 .. 16 */
5393   OP_I16z,      /*                 0 .. 16 */
5394   OP_I31,       /*                 0 .. 31 */
5395   OP_I31w,      /*                 0 .. 31, optional trailing ! */
5396   OP_I32,       /*                 1 .. 32 */
5397   OP_I32z,      /*                 0 .. 32 */
5398   OP_I63,       /*                 0 .. 63 */
5399   OP_I63s,      /*               -64 .. 63 */
5400   OP_I64,       /*                 1 .. 64 */
5401   OP_I64z,      /*                 0 .. 64 */
5402   OP_I255,      /*                 0 .. 255 */
5403
5404   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
5405   OP_I7b,       /*                             0 .. 7 */
5406   OP_I15b,      /*                             0 .. 15 */
5407   OP_I31b,      /*                             0 .. 31 */
5408
5409   OP_SH,        /* shifter operand */
5410   OP_SHG,       /* shifter operand with possible group relocation */
5411   OP_ADDR,      /* Memory address expression (any mode) */
5412   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
5413   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
5414   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
5415   OP_EXP,       /* arbitrary expression */
5416   OP_EXPi,      /* same, with optional immediate prefix */
5417   OP_EXPr,      /* same, with optional relocation suffix */
5418   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
5419
5420   OP_CPSF,      /* CPS flags */
5421   OP_ENDI,      /* Endianness specifier */
5422   OP_PSR,       /* CPSR/SPSR mask for msr */
5423   OP_COND,      /* conditional code */
5424   OP_TB,        /* Table branch.  */
5425
5426   OP_RVC_PSR,   /* CPSR/SPSR mask for msr, or VFP control register.  */
5427   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
5428
5429   OP_RRnpc_I0,  /* ARM register or literal 0 */
5430   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
5431   OP_RR_EXi,    /* ARM register or expression with imm prefix */
5432   OP_RF_IF,     /* FPA register or immediate */
5433   OP_RIWR_RIWC, /* iWMMXt R or C reg */
5434   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
5435
5436   /* Optional operands.  */
5437   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
5438   OP_oI31b,      /*                             0 .. 31 */
5439   OP_oI32b,      /*                             1 .. 32 */
5440   OP_oIffffb,    /*                             0 .. 65535 */
5441   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
5442
5443   OP_oRR,        /* ARM register */
5444   OP_oRRnpc,     /* ARM register, not the PC */
5445   OP_oRND,       /* Optional Neon double precision register */
5446   OP_oRNQ,       /* Optional Neon quad precision register */
5447   OP_oRNDQ,      /* Optional Neon double or quad precision register */
5448   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
5449   OP_oSHll,      /* LSL immediate */
5450   OP_oSHar,      /* ASR immediate */
5451   OP_oSHllar,    /* LSL or ASR immediate */
5452   OP_oROR,       /* ROR 0/8/16/24 */
5453   OP_oBARRIER,   /* Option argument for a barrier instruction.  */
5454
5455   OP_FIRST_OPTIONAL = OP_oI7b
5456 };
5457
5458 /* Generic instruction operand parser.  This does no encoding and no
5459    semantic validation; it merely squirrels values away in the inst
5460    structure.  Returns SUCCESS or FAIL depending on whether the
5461    specified grammar matched.  */
5462 static int
5463 parse_operands (char *str, const unsigned char *pattern)
5464 {
5465   unsigned const char *upat = pattern;
5466   char *backtrack_pos = 0;
5467   const char *backtrack_error = 0;
5468   int i, val, backtrack_index = 0;
5469   enum arm_reg_type rtype;
5470   parse_operand_result result;
5471
5472 #define po_char_or_fail(chr) do {               \
5473   if (skip_past_char (&str, chr) == FAIL)       \
5474     goto bad_args;                              \
5475 } while (0)
5476
5477 #define po_reg_or_fail(regtype) do {                            \
5478   val = arm_typed_reg_parse (&str, regtype, &rtype,             \
5479                              &inst.operands[i].vectype);        \
5480   if (val == FAIL)                                              \
5481     {                                                           \
5482       first_error (_(reg_expected_msgs[regtype]));              \
5483       goto failure;                                             \
5484     }                                                           \
5485   inst.operands[i].reg = val;                                   \
5486   inst.operands[i].isreg = 1;                                   \
5487   inst.operands[i].isquad = (rtype == REG_TYPE_NQ);             \
5488   inst.operands[i].issingle = (rtype == REG_TYPE_VFS);          \
5489   inst.operands[i].isvec = (rtype == REG_TYPE_VFS               \
5490                             || rtype == REG_TYPE_VFD            \
5491                             || rtype == REG_TYPE_NQ);           \
5492 } while (0)
5493
5494 #define po_reg_or_goto(regtype, label) do {                     \
5495   val = arm_typed_reg_parse (&str, regtype, &rtype,             \
5496                              &inst.operands[i].vectype);        \
5497   if (val == FAIL)                                              \
5498     goto label;                                                 \
5499                                                                 \
5500   inst.operands[i].reg = val;                                   \
5501   inst.operands[i].isreg = 1;                                   \
5502   inst.operands[i].isquad = (rtype == REG_TYPE_NQ);             \
5503   inst.operands[i].issingle = (rtype == REG_TYPE_VFS);          \
5504   inst.operands[i].isvec = (rtype == REG_TYPE_VFS               \
5505                             || rtype == REG_TYPE_VFD            \
5506                             || rtype == REG_TYPE_NQ);           \
5507 } while (0)
5508
5509 #define po_imm_or_fail(min, max, popt) do {                     \
5510   if (parse_immediate (&str, &val, min, max, popt) == FAIL)     \
5511     goto failure;                                               \
5512   inst.operands[i].imm = val;                                   \
5513 } while (0)
5514
5515 #define po_scalar_or_goto(elsz, label) do {                     \
5516   val = parse_scalar (&str, elsz, &inst.operands[i].vectype);   \
5517   if (val == FAIL)                                              \
5518     goto label;                                                 \
5519   inst.operands[i].reg = val;                                   \
5520   inst.operands[i].isscalar = 1;                                \
5521 } while (0)
5522
5523 #define po_misc_or_fail(expr) do {              \
5524   if (expr)                                     \
5525     goto failure;                               \
5526 } while (0)
5527
5528 #define po_misc_or_fail_no_backtrack(expr) do { \
5529   result = expr;                                \
5530   if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)\
5531     backtrack_pos = 0;                          \
5532   if (result != PARSE_OPERAND_SUCCESS)          \
5533     goto failure;                               \
5534 } while (0)
5535
5536   skip_whitespace (str);
5537
5538   for (i = 0; upat[i] != OP_stop; i++)
5539     {
5540       if (upat[i] >= OP_FIRST_OPTIONAL)
5541         {
5542           /* Remember where we are in case we need to backtrack.  */
5543           assert (!backtrack_pos);
5544           backtrack_pos = str;
5545           backtrack_error = inst.error;
5546           backtrack_index = i;
5547         }
5548
5549       if (i > 0)
5550         po_char_or_fail (',');
5551
5552       switch (upat[i])
5553         {
5554           /* Registers */
5555         case OP_oRRnpc:
5556         case OP_RRnpc:
5557         case OP_oRR:
5558         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
5559         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
5560         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
5561         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
5562         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
5563         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
5564         case OP_oRND:
5565         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
5566         case OP_RVC:   po_reg_or_fail (REG_TYPE_VFC);     break;
5567         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
5568         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
5569         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
5570         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
5571         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
5572         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
5573         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
5574         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
5575         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
5576         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
5577         case OP_oRNQ:
5578         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
5579         case OP_oRNDQ:
5580         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
5581         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
5582         case OP_oRNSDQ:
5583         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
5584
5585         /* Neon scalar. Using an element size of 8 means that some invalid
5586            scalars are accepted here, so deal with those in later code.  */
5587         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
5588
5589         /* WARNING: We can expand to two operands here. This has the potential
5590            to totally confuse the backtracking mechanism! It will be OK at
5591            least as long as we don't try to use optional args as well,
5592            though.  */
5593         case OP_NILO:
5594           {
5595             po_reg_or_goto (REG_TYPE_NDQ, try_imm);
5596             inst.operands[i].present = 1;
5597             i++;
5598             skip_past_comma (&str);
5599             po_reg_or_goto (REG_TYPE_NDQ, one_reg_only);
5600             break;
5601             one_reg_only:
5602             /* Optional register operand was omitted. Unfortunately, it's in
5603                operands[i-1] and we need it to be in inst.operands[i]. Fix that
5604                here (this is a bit grotty).  */
5605             inst.operands[i] = inst.operands[i-1];
5606             inst.operands[i-1].present = 0;
5607             break;
5608             try_imm:
5609             /* There's a possibility of getting a 64-bit immediate here, so
5610                we need special handling.  */
5611             if (parse_big_immediate (&str, i) == FAIL)
5612               {
5613                 inst.error = _("immediate value is out of range");
5614                 goto failure;
5615               }
5616           }
5617           break;
5618
5619         case OP_RNDQ_I0:
5620           {
5621             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
5622             break;
5623             try_imm0:
5624             po_imm_or_fail (0, 0, TRUE);
5625           }
5626           break;
5627
5628         case OP_RVSD_I0:
5629           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
5630           break;
5631
5632         case OP_RR_RNSC:
5633           {
5634             po_scalar_or_goto (8, try_rr);
5635             break;
5636             try_rr:
5637             po_reg_or_fail (REG_TYPE_RN);
5638           }
5639           break;
5640
5641         case OP_RNSDQ_RNSC:
5642           {
5643             po_scalar_or_goto (8, try_nsdq);
5644             break;
5645             try_nsdq:
5646             po_reg_or_fail (REG_TYPE_NSDQ);
5647           }
5648           break;
5649
5650         case OP_RNDQ_RNSC:
5651           {
5652             po_scalar_or_goto (8, try_ndq);
5653             break;
5654             try_ndq:
5655             po_reg_or_fail (REG_TYPE_NDQ);
5656           }
5657           break;
5658
5659         case OP_RND_RNSC:
5660           {
5661             po_scalar_or_goto (8, try_vfd);
5662             break;
5663             try_vfd:
5664             po_reg_or_fail (REG_TYPE_VFD);
5665           }
5666           break;
5667
5668         case OP_VMOV:
5669           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
5670              not careful then bad things might happen.  */
5671           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
5672           break;
5673
5674         case OP_RNDQ_IMVNb:
5675           {
5676             po_reg_or_goto (REG_TYPE_NDQ, try_mvnimm);
5677             break;
5678             try_mvnimm:
5679             /* There's a possibility of getting a 64-bit immediate here, so
5680                we need special handling.  */
5681             if (parse_big_immediate (&str, i) == FAIL)
5682               {
5683                 inst.error = _("immediate value is out of range");
5684                 goto failure;
5685               }
5686           }
5687           break;
5688
5689         case OP_RNDQ_I63b:
5690           {
5691             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
5692             break;
5693             try_shimm:
5694             po_imm_or_fail (0, 63, TRUE);
5695           }
5696           break;
5697
5698         case OP_RRnpcb:
5699           po_char_or_fail ('[');
5700           po_reg_or_fail  (REG_TYPE_RN);
5701           po_char_or_fail (']');
5702           break;
5703
5704         case OP_RRw:
5705           po_reg_or_fail (REG_TYPE_RN);
5706           if (skip_past_char (&str, '!') == SUCCESS)
5707             inst.operands[i].writeback = 1;
5708           break;
5709
5710           /* Immediates */
5711         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
5712         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
5713         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
5714         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
5715         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
5716         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
5717         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
5718         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
5719         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
5720         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
5721         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
5722         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
5723
5724         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
5725         case OP_oI7b:
5726         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
5727         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
5728         case OP_oI31b:
5729         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
5730         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
5731         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
5732
5733           /* Immediate variants */
5734         case OP_oI255c:
5735           po_char_or_fail ('{');
5736           po_imm_or_fail (0, 255, TRUE);
5737           po_char_or_fail ('}');
5738           break;
5739
5740         case OP_I31w:
5741           /* The expression parser chokes on a trailing !, so we have
5742              to find it first and zap it.  */
5743           {
5744             char *s = str;
5745             while (*s && *s != ',')
5746               s++;
5747             if (s[-1] == '!')
5748               {
5749                 s[-1] = '\0';
5750                 inst.operands[i].writeback = 1;
5751               }
5752             po_imm_or_fail (0, 31, TRUE);
5753             if (str == s - 1)
5754               str = s;
5755           }
5756           break;
5757
5758           /* Expressions */
5759         case OP_EXPi:   EXPi:
5760           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
5761                                               GE_OPT_PREFIX));
5762           break;
5763
5764         case OP_EXP:
5765           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
5766                                               GE_NO_PREFIX));
5767           break;
5768
5769         case OP_EXPr:   EXPr:
5770           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
5771                                               GE_NO_PREFIX));
5772           if (inst.reloc.exp.X_op == O_symbol)
5773             {
5774               val = parse_reloc (&str);
5775               if (val == -1)
5776                 {
5777                   inst.error = _("unrecognized relocation suffix");
5778                   goto failure;
5779                 }
5780               else if (val != BFD_RELOC_UNUSED)
5781                 {
5782                   inst.operands[i].imm = val;
5783                   inst.operands[i].hasreloc = 1;
5784                 }
5785             }
5786           break;
5787
5788           /* Operand for MOVW or MOVT.  */
5789         case OP_HALF:
5790           po_misc_or_fail (parse_half (&str));
5791           break;
5792
5793           /* Register or expression */
5794         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
5795         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
5796
5797           /* Register or immediate */
5798         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
5799         I0:               po_imm_or_fail (0, 0, FALSE);       break;
5800
5801         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
5802         IF:
5803           if (!is_immediate_prefix (*str))
5804             goto bad_args;
5805           str++;
5806           val = parse_fpa_immediate (&str);
5807           if (val == FAIL)
5808             goto failure;
5809           /* FPA immediates are encoded as registers 8-15.
5810              parse_fpa_immediate has already applied the offset.  */
5811           inst.operands[i].reg = val;
5812           inst.operands[i].isreg = 1;
5813           break;
5814
5815         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
5816         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
5817
5818           /* Two kinds of register */
5819         case OP_RIWR_RIWC:
5820           {
5821             struct reg_entry *rege = arm_reg_parse_multi (&str);
5822             if (!rege
5823                 || (rege->type != REG_TYPE_MMXWR
5824                     && rege->type != REG_TYPE_MMXWC
5825                     && rege->type != REG_TYPE_MMXWCG))
5826               {
5827                 inst.error = _("iWMMXt data or control register expected");
5828                 goto failure;
5829               }
5830             inst.operands[i].reg = rege->number;
5831             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
5832           }
5833           break;
5834
5835         case OP_RIWC_RIWG:
5836           {
5837             struct reg_entry *rege = arm_reg_parse_multi (&str);
5838             if (!rege
5839                 || (rege->type != REG_TYPE_MMXWC
5840                     && rege->type != REG_TYPE_MMXWCG))
5841               {
5842                 inst.error = _("iWMMXt control register expected");
5843                 goto failure;
5844               }
5845             inst.operands[i].reg = rege->number;
5846             inst.operands[i].isreg = 1;
5847           }
5848           break;
5849
5850           /* Misc */
5851         case OP_CPSF:    val = parse_cps_flags (&str);          break;
5852         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
5853         case OP_oROR:    val = parse_ror (&str);                break;
5854         case OP_PSR:     val = parse_psr (&str);                break;
5855         case OP_COND:    val = parse_cond (&str);               break;
5856         case OP_oBARRIER:val = parse_barrier (&str);            break;
5857
5858         case OP_RVC_PSR:
5859           po_reg_or_goto (REG_TYPE_VFC, try_psr);
5860           inst.operands[i].isvec = 1;  /* Mark VFP control reg as vector.  */
5861           break;
5862           try_psr:
5863           val = parse_psr (&str);
5864           break;
5865
5866         case OP_APSR_RR:
5867           po_reg_or_goto (REG_TYPE_RN, try_apsr);
5868           break;
5869           try_apsr:
5870           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
5871              instruction).  */
5872           if (strncasecmp (str, "APSR_", 5) == 0)
5873             {
5874               unsigned found = 0;
5875               str += 5;
5876               while (found < 15)
5877                 switch (*str++)
5878                   {
5879                   case 'c': found = (found & 1) ? 16 : found | 1; break;
5880                   case 'n': found = (found & 2) ? 16 : found | 2; break;
5881                   case 'z': found = (found & 4) ? 16 : found | 4; break;
5882                   case 'v': found = (found & 8) ? 16 : found | 8; break;
5883                   default: found = 16;
5884                   }
5885               if (found != 15)
5886                 goto failure;
5887               inst.operands[i].isvec = 1;
5888             }
5889           else
5890             goto failure;
5891           break;
5892
5893         case OP_TB:
5894           po_misc_or_fail (parse_tb (&str));
5895           break;
5896
5897           /* Register lists */
5898         case OP_REGLST:
5899           val = parse_reg_list (&str);
5900           if (*str == '^')
5901             {
5902               inst.operands[1].writeback = 1;
5903               str++;
5904             }
5905           break;
5906
5907         case OP_VRSLST:
5908           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
5909           break;
5910
5911         case OP_VRDLST:
5912           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
5913           break;
5914
5915         case OP_VRSDLST:
5916           /* Allow Q registers too.  */
5917           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
5918                                     REGLIST_NEON_D);
5919           if (val == FAIL)
5920             {
5921               inst.error = NULL;
5922               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
5923                                         REGLIST_VFP_S);
5924               inst.operands[i].issingle = 1;
5925             }
5926           break;
5927
5928         case OP_NRDLST:
5929           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
5930                                     REGLIST_NEON_D);
5931           break;
5932
5933         case OP_NSTRLST:
5934           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
5935                                            &inst.operands[i].vectype);
5936           break;
5937
5938           /* Addressing modes */
5939         case OP_ADDR:
5940           po_misc_or_fail (parse_address (&str, i));
5941           break;
5942
5943         case OP_ADDRGLDR:
5944           po_misc_or_fail_no_backtrack (
5945             parse_address_group_reloc (&str, i, GROUP_LDR));
5946           break;
5947
5948         case OP_ADDRGLDRS:
5949           po_misc_or_fail_no_backtrack (
5950             parse_address_group_reloc (&str, i, GROUP_LDRS));
5951           break;
5952
5953         case OP_ADDRGLDC:
5954           po_misc_or_fail_no_backtrack (
5955             parse_address_group_reloc (&str, i, GROUP_LDC));
5956           break;
5957
5958         case OP_SH:
5959           po_misc_or_fail (parse_shifter_operand (&str, i));
5960           break;
5961
5962         case OP_SHG:
5963           po_misc_or_fail_no_backtrack (
5964             parse_shifter_operand_group_reloc (&str, i));
5965           break;
5966
5967         case OP_oSHll:
5968           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
5969           break;
5970
5971         case OP_oSHar:
5972           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
5973           break;
5974
5975         case OP_oSHllar:
5976           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
5977           break;
5978
5979         default:
5980           as_fatal ("unhandled operand code %d", upat[i]);
5981         }
5982
5983       /* Various value-based sanity checks and shared operations.  We
5984          do not signal immediate failures for the register constraints;
5985          this allows a syntax error to take precedence.  */
5986       switch (upat[i])
5987         {
5988         case OP_oRRnpc:
5989         case OP_RRnpc:
5990         case OP_RRnpcb:
5991         case OP_RRw:
5992         case OP_RRnpc_I0:
5993           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
5994             inst.error = BAD_PC;
5995           break;
5996
5997         case OP_CPSF:
5998         case OP_ENDI:
5999         case OP_oROR:
6000         case OP_PSR:
6001         case OP_RVC_PSR:
6002         case OP_COND:
6003         case OP_oBARRIER:
6004         case OP_REGLST:
6005         case OP_VRSLST:
6006         case OP_VRDLST:
6007         case OP_VRSDLST:
6008         case OP_NRDLST:
6009         case OP_NSTRLST:
6010           if (val == FAIL)
6011             goto failure;
6012           inst.operands[i].imm = val;
6013           break;
6014
6015         default:
6016           break;
6017         }
6018
6019       /* If we get here, this operand was successfully parsed.  */
6020       inst.operands[i].present = 1;
6021       continue;
6022
6023     bad_args:
6024       inst.error = BAD_ARGS;
6025
6026     failure:
6027       if (!backtrack_pos)
6028         {
6029           /* The parse routine should already have set inst.error, but set a
6030              defaut here just in case.  */
6031           if (!inst.error)
6032             inst.error = _("syntax error");
6033           return FAIL;
6034         }
6035
6036       /* Do not backtrack over a trailing optional argument that
6037          absorbed some text.  We will only fail again, with the
6038          'garbage following instruction' error message, which is
6039          probably less helpful than the current one.  */
6040       if (backtrack_index == i && backtrack_pos != str
6041           && upat[i+1] == OP_stop)
6042         {
6043           if (!inst.error)
6044             inst.error = _("syntax error");
6045           return FAIL;
6046         }
6047
6048       /* Try again, skipping the optional argument at backtrack_pos.  */
6049       str = backtrack_pos;
6050       inst.error = backtrack_error;
6051       inst.operands[backtrack_index].present = 0;
6052       i = backtrack_index;
6053       backtrack_pos = 0;
6054     }
6055
6056   /* Check that we have parsed all the arguments.  */
6057   if (*str != '\0' && !inst.error)
6058     inst.error = _("garbage following instruction");
6059
6060   return inst.error ? FAIL : SUCCESS;
6061 }
6062
6063 #undef po_char_or_fail
6064 #undef po_reg_or_fail
6065 #undef po_reg_or_goto
6066 #undef po_imm_or_fail
6067 #undef po_scalar_or_fail
6068 \f
6069 /* Shorthand macro for instruction encoding functions issuing errors.  */
6070 #define constraint(expr, err) do {              \
6071   if (expr)                                     \
6072     {                                           \
6073       inst.error = err;                         \
6074       return;                                   \
6075     }                                           \
6076 } while (0)
6077
6078 /* Functions for operand encoding.  ARM, then Thumb.  */
6079
6080 #define rotate_left(v, n) (v << n | v >> (32 - n))
6081
6082 /* If VAL can be encoded in the immediate field of an ARM instruction,
6083    return the encoded form.  Otherwise, return FAIL.  */
6084
6085 static unsigned int
6086 encode_arm_immediate (unsigned int val)
6087 {
6088   unsigned int a, i;
6089
6090   for (i = 0; i < 32; i += 2)
6091     if ((a = rotate_left (val, i)) <= 0xff)
6092       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
6093
6094   return FAIL;
6095 }
6096
6097 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
6098    return the encoded form.  Otherwise, return FAIL.  */
6099 static unsigned int
6100 encode_thumb32_immediate (unsigned int val)
6101 {
6102   unsigned int a, i;
6103
6104   if (val <= 0xff)
6105     return val;
6106
6107   for (i = 1; i <= 24; i++)
6108     {
6109       a = val >> i;
6110       if ((val & ~(0xff << i)) == 0)
6111         return ((val >> i) & 0x7f) | ((32 - i) << 7);
6112     }
6113
6114   a = val & 0xff;
6115   if (val == ((a << 16) | a))
6116     return 0x100 | a;
6117   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
6118     return 0x300 | a;
6119
6120   a = val & 0xff00;
6121   if (val == ((a << 16) | a))
6122     return 0x200 | (a >> 8);
6123
6124   return FAIL;
6125 }
6126 /* Encode a VFP SP or DP register number into inst.instruction.  */
6127
6128 static void
6129 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
6130 {
6131   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
6132       && reg > 15)
6133     {
6134       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
6135         {
6136           if (thumb_mode)
6137             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
6138                                     fpu_vfp_ext_v3);
6139           else
6140             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
6141                                     fpu_vfp_ext_v3);
6142         }
6143       else
6144         {
6145           first_error (_("D register out of range for selected VFP version"));
6146           return;
6147         }
6148     }
6149
6150   switch (pos)
6151     {
6152     case VFP_REG_Sd:
6153       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
6154       break;
6155
6156     case VFP_REG_Sn:
6157       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
6158       break;
6159
6160     case VFP_REG_Sm:
6161       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
6162       break;
6163
6164     case VFP_REG_Dd:
6165       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
6166       break;
6167     
6168     case VFP_REG_Dn:
6169       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
6170       break;
6171     
6172     case VFP_REG_Dm:
6173       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
6174       break;
6175
6176     default:
6177       abort ();
6178     }
6179 }
6180
6181 /* Encode a <shift> in an ARM-format instruction.  The immediate,
6182    if any, is handled by md_apply_fix.   */
6183 static void
6184 encode_arm_shift (int i)
6185 {
6186   if (inst.operands[i].shift_kind == SHIFT_RRX)
6187     inst.instruction |= SHIFT_ROR << 5;
6188   else
6189     {
6190       inst.instruction |= inst.operands[i].shift_kind << 5;
6191       if (inst.operands[i].immisreg)
6192         {
6193           inst.instruction |= SHIFT_BY_REG;
6194           inst.instruction |= inst.operands[i].imm << 8;
6195         }
6196       else
6197         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
6198     }
6199 }
6200
6201 static void
6202 encode_arm_shifter_operand (int i)
6203 {
6204   if (inst.operands[i].isreg)
6205     {
6206       inst.instruction |= inst.operands[i].reg;
6207       encode_arm_shift (i);
6208     }
6209   else
6210     inst.instruction |= INST_IMMEDIATE;
6211 }
6212
6213 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
6214 static void
6215 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
6216 {
6217   assert (inst.operands[i].isreg);
6218   inst.instruction |= inst.operands[i].reg << 16;
6219
6220   if (inst.operands[i].preind)
6221     {
6222       if (is_t)
6223         {
6224           inst.error = _("instruction does not accept preindexed addressing");
6225           return;
6226         }
6227       inst.instruction |= PRE_INDEX;
6228       if (inst.operands[i].writeback)
6229         inst.instruction |= WRITE_BACK;
6230
6231     }
6232   else if (inst.operands[i].postind)
6233     {
6234       assert (inst.operands[i].writeback);
6235       if (is_t)
6236         inst.instruction |= WRITE_BACK;
6237     }
6238   else /* unindexed - only for coprocessor */
6239     {
6240       inst.error = _("instruction does not accept unindexed addressing");
6241       return;
6242     }
6243
6244   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
6245       && (((inst.instruction & 0x000f0000) >> 16)
6246           == ((inst.instruction & 0x0000f000) >> 12)))
6247     as_warn ((inst.instruction & LOAD_BIT)
6248              ? _("destination register same as write-back base")
6249              : _("source register same as write-back base"));
6250 }
6251
6252 /* inst.operands[i] was set up by parse_address.  Encode it into an
6253    ARM-format mode 2 load or store instruction.  If is_t is true,
6254    reject forms that cannot be used with a T instruction (i.e. not
6255    post-indexed).  */
6256 static void
6257 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
6258 {
6259   encode_arm_addr_mode_common (i, is_t);
6260
6261   if (inst.operands[i].immisreg)
6262     {
6263       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
6264       inst.instruction |= inst.operands[i].imm;
6265       if (!inst.operands[i].negative)
6266         inst.instruction |= INDEX_UP;
6267       if (inst.operands[i].shifted)
6268         {
6269           if (inst.operands[i].shift_kind == SHIFT_RRX)
6270             inst.instruction |= SHIFT_ROR << 5;
6271           else
6272             {
6273               inst.instruction |= inst.operands[i].shift_kind << 5;
6274               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
6275             }
6276         }
6277     }
6278   else /* immediate offset in inst.reloc */
6279     {
6280       if (inst.reloc.type == BFD_RELOC_UNUSED)
6281         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
6282     }
6283 }
6284
6285 /* inst.operands[i] was set up by parse_address.  Encode it into an
6286    ARM-format mode 3 load or store instruction.  Reject forms that
6287    cannot be used with such instructions.  If is_t is true, reject
6288    forms that cannot be used with a T instruction (i.e. not
6289    post-indexed).  */
6290 static void
6291 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
6292 {
6293   if (inst.operands[i].immisreg && inst.operands[i].shifted)
6294     {
6295       inst.error = _("instruction does not accept scaled register index");
6296       return;
6297     }
6298
6299   encode_arm_addr_mode_common (i, is_t);
6300
6301   if (inst.operands[i].immisreg)
6302     {
6303       inst.instruction |= inst.operands[i].imm;
6304       if (!inst.operands[i].negative)
6305         inst.instruction |= INDEX_UP;
6306     }
6307   else /* immediate offset in inst.reloc */
6308     {
6309       inst.instruction |= HWOFFSET_IMM;
6310       if (inst.reloc.type == BFD_RELOC_UNUSED)
6311         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
6312     }
6313 }
6314
6315 /* inst.operands[i] was set up by parse_address.  Encode it into an
6316    ARM-format instruction.  Reject all forms which cannot be encoded
6317    into a coprocessor load/store instruction.  If wb_ok is false,
6318    reject use of writeback; if unind_ok is false, reject use of
6319    unindexed addressing.  If reloc_override is not 0, use it instead
6320    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
6321    (in which case it is preserved).  */
6322
6323 static int
6324 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
6325 {
6326   inst.instruction |= inst.operands[i].reg << 16;
6327
6328   assert (!(inst.operands[i].preind && inst.operands[i].postind));
6329
6330   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
6331     {
6332       assert (!inst.operands[i].writeback);
6333       if (!unind_ok)
6334         {
6335           inst.error = _("instruction does not support unindexed addressing");
6336           return FAIL;
6337         }
6338       inst.instruction |= inst.operands[i].imm;
6339       inst.instruction |= INDEX_UP;
6340       return SUCCESS;
6341     }
6342
6343   if (inst.operands[i].preind)
6344     inst.instruction |= PRE_INDEX;
6345
6346   if (inst.operands[i].writeback)
6347     {
6348       if (inst.operands[i].reg == REG_PC)
6349         {
6350           inst.error = _("pc may not be used with write-back");
6351           return FAIL;
6352         }
6353       if (!wb_ok)
6354         {
6355           inst.error = _("instruction does not support writeback");
6356           return FAIL;
6357         }
6358       inst.instruction |= WRITE_BACK;
6359     }
6360
6361   if (reloc_override)
6362     inst.reloc.type = reloc_override;
6363   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
6364             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
6365            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
6366     {
6367       if (thumb_mode)
6368         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
6369       else
6370         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
6371     }
6372
6373   return SUCCESS;
6374 }
6375
6376 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
6377    Determine whether it can be performed with a move instruction; if
6378    it can, convert inst.instruction to that move instruction and
6379    return 1; if it can't, convert inst.instruction to a literal-pool
6380    load and return 0.  If this is not a valid thing to do in the
6381    current context, set inst.error and return 1.
6382
6383    inst.operands[i] describes the destination register.  */
6384
6385 static int
6386 move_or_literal_pool (int i, bfd_boolean thumb_p, bfd_boolean mode_3)
6387 {
6388   unsigned long tbit;
6389
6390   if (thumb_p)
6391     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
6392   else
6393     tbit = LOAD_BIT;
6394
6395   if ((inst.instruction & tbit) == 0)
6396     {
6397       inst.error = _("invalid pseudo operation");
6398       return 1;
6399     }
6400   if (inst.reloc.exp.X_op != O_constant && inst.reloc.exp.X_op != O_symbol)
6401     {
6402       inst.error = _("constant expression expected");
6403       return 1;
6404     }
6405   if (inst.reloc.exp.X_op == O_constant)
6406     {
6407       if (thumb_p)
6408         {
6409           if (!unified_syntax && (inst.reloc.exp.X_add_number & ~0xFF) == 0)
6410             {
6411               /* This can be done with a mov(1) instruction.  */
6412               inst.instruction  = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
6413               inst.instruction |= inst.reloc.exp.X_add_number;
6414               return 1;
6415             }
6416         }
6417       else
6418         {
6419           int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
6420           if (value != FAIL)
6421             {
6422               /* This can be done with a mov instruction.  */
6423               inst.instruction &= LITERAL_MASK;
6424               inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
6425               inst.instruction |= value & 0xfff;
6426               return 1;
6427             }
6428
6429           value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
6430           if (value != FAIL)
6431             {
6432               /* This can be done with a mvn instruction.  */
6433               inst.instruction &= LITERAL_MASK;
6434               inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
6435               inst.instruction |= value & 0xfff;
6436               return 1;
6437             }
6438         }
6439     }
6440
6441   if (add_to_lit_pool () == FAIL)
6442     {
6443       inst.error = _("literal pool insertion failed");
6444       return 1;
6445     }
6446   inst.operands[1].reg = REG_PC;
6447   inst.operands[1].isreg = 1;
6448   inst.operands[1].preind = 1;
6449   inst.reloc.pc_rel = 1;
6450   inst.reloc.type = (thumb_p
6451                      ? BFD_RELOC_ARM_THUMB_OFFSET
6452                      : (mode_3
6453                         ? BFD_RELOC_ARM_HWLITERAL
6454                         : BFD_RELOC_ARM_LITERAL));
6455   return 0;
6456 }
6457
6458 /* Functions for instruction encoding, sorted by subarchitecture.
6459    First some generics; their names are taken from the conventional
6460    bit positions for register arguments in ARM format instructions.  */
6461
6462 static void
6463 do_noargs (void)
6464 {
6465 }
6466
6467 static void
6468 do_rd (void)
6469 {
6470   inst.instruction |= inst.operands[0].reg << 12;
6471 }
6472
6473 static void
6474 do_rd_rm (void)
6475 {
6476   inst.instruction |= inst.operands[0].reg << 12;
6477   inst.instruction |= inst.operands[1].reg;
6478 }
6479
6480 static void
6481 do_rd_rn (void)
6482 {
6483   inst.instruction |= inst.operands[0].reg << 12;
6484   inst.instruction |= inst.operands[1].reg << 16;
6485 }
6486
6487 static void
6488 do_rn_rd (void)
6489 {
6490   inst.instruction |= inst.operands[0].reg << 16;
6491   inst.instruction |= inst.operands[1].reg << 12;
6492 }
6493
6494 static void
6495 do_rd_rm_rn (void)
6496 {
6497   unsigned Rn = inst.operands[2].reg;
6498   /* Enforce resutrictions on SWP instruction.  */
6499   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
6500     constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
6501                 _("Rn must not overlap other operands"));
6502   inst.instruction |= inst.operands[0].reg << 12;
6503   inst.instruction |= inst.operands[1].reg;
6504   inst.instruction |= Rn << 16;
6505 }
6506
6507 static void
6508 do_rd_rn_rm (void)
6509 {
6510   inst.instruction |= inst.operands[0].reg << 12;
6511   inst.instruction |= inst.operands[1].reg << 16;
6512   inst.instruction |= inst.operands[2].reg;
6513 }
6514
6515 static void
6516 do_rm_rd_rn (void)
6517 {
6518   inst.instruction |= inst.operands[0].reg;
6519   inst.instruction |= inst.operands[1].reg << 12;
6520   inst.instruction |= inst.operands[2].reg << 16;
6521 }
6522
6523 static void
6524 do_imm0 (void)
6525 {
6526   inst.instruction |= inst.operands[0].imm;
6527 }
6528
6529 static void
6530 do_rd_cpaddr (void)
6531 {
6532   inst.instruction |= inst.operands[0].reg << 12;
6533   encode_arm_cp_address (1, TRUE, TRUE, 0);
6534 }
6535
6536 /* ARM instructions, in alphabetical order by function name (except
6537    that wrapper functions appear immediately after the function they
6538    wrap).  */
6539
6540 /* This is a pseudo-op of the form "adr rd, label" to be converted
6541    into a relative address of the form "add rd, pc, #label-.-8".  */
6542
6543 static void
6544 do_adr (void)
6545 {
6546   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
6547
6548   /* Frag hacking will turn this into a sub instruction if the offset turns
6549      out to be negative.  */
6550   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
6551   inst.reloc.pc_rel = 1;
6552   inst.reloc.exp.X_add_number -= 8;
6553 }
6554
6555 /* This is a pseudo-op of the form "adrl rd, label" to be converted
6556    into a relative address of the form:
6557    add rd, pc, #low(label-.-8)"
6558    add rd, rd, #high(label-.-8)"  */
6559
6560 static void
6561 do_adrl (void)
6562 {
6563   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
6564
6565   /* Frag hacking will turn this into a sub instruction if the offset turns
6566      out to be negative.  */
6567   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
6568   inst.reloc.pc_rel            = 1;
6569   inst.size                    = INSN_SIZE * 2;
6570   inst.reloc.exp.X_add_number -= 8;
6571 }
6572
6573 static void
6574 do_arit (void)
6575 {
6576   if (!inst.operands[1].present)
6577     inst.operands[1].reg = inst.operands[0].reg;
6578   inst.instruction |= inst.operands[0].reg << 12;
6579   inst.instruction |= inst.operands[1].reg << 16;
6580   encode_arm_shifter_operand (2);
6581 }
6582
6583 static void
6584 do_barrier (void)
6585 {
6586   if (inst.operands[0].present)
6587     {
6588       constraint ((inst.instruction & 0xf0) != 0x40
6589                   && inst.operands[0].imm != 0xf,
6590                   "bad barrier type");
6591       inst.instruction |= inst.operands[0].imm;
6592     }
6593   else
6594     inst.instruction |= 0xf;
6595 }
6596
6597 static void
6598 do_bfc (void)
6599 {
6600   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
6601   constraint (msb > 32, _("bit-field extends past end of register"));
6602   /* The instruction encoding stores the LSB and MSB,
6603      not the LSB and width.  */
6604   inst.instruction |= inst.operands[0].reg << 12;
6605   inst.instruction |= inst.operands[1].imm << 7;
6606   inst.instruction |= (msb - 1) << 16;
6607 }
6608
6609 static void
6610 do_bfi (void)
6611 {
6612   unsigned int msb;
6613
6614   /* #0 in second position is alternative syntax for bfc, which is
6615      the same instruction but with REG_PC in the Rm field.  */
6616   if (!inst.operands[1].isreg)
6617     inst.operands[1].reg = REG_PC;
6618
6619   msb = inst.operands[2].imm + inst.operands[3].imm;
6620   constraint (msb > 32, _("bit-field extends past end of register"));
6621   /* The instruction encoding stores the LSB and MSB,
6622      not the LSB and width.  */
6623   inst.instruction |= inst.operands[0].reg << 12;
6624   inst.instruction |= inst.operands[1].reg;
6625   inst.instruction |= inst.operands[2].imm << 7;
6626   inst.instruction |= (msb - 1) << 16;
6627 }
6628
6629 static void
6630 do_bfx (void)
6631 {
6632   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
6633               _("bit-field extends past end of register"));
6634   inst.instruction |= inst.operands[0].reg << 12;
6635   inst.instruction |= inst.operands[1].reg;
6636   inst.instruction |= inst.operands[2].imm << 7;
6637   inst.instruction |= (inst.operands[3].imm - 1) << 16;
6638 }
6639
6640 /* ARM V5 breakpoint instruction (argument parse)
6641      BKPT <16 bit unsigned immediate>
6642      Instruction is not conditional.
6643         The bit pattern given in insns[] has the COND_ALWAYS condition,
6644         and it is an error if the caller tried to override that.  */
6645
6646 static void
6647 do_bkpt (void)
6648 {
6649   /* Top 12 of 16 bits to bits 19:8.  */
6650   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
6651
6652   /* Bottom 4 of 16 bits to bits 3:0.  */
6653   inst.instruction |= inst.operands[0].imm & 0xf;
6654 }
6655
6656 static void
6657 encode_branch (int default_reloc)
6658 {
6659   if (inst.operands[0].hasreloc)
6660     {
6661       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32,
6662                   _("the only suffix valid here is '(plt)'"));
6663       inst.reloc.type   = BFD_RELOC_ARM_PLT32;
6664     }
6665   else
6666     {
6667       inst.reloc.type = default_reloc;
6668     }
6669   inst.reloc.pc_rel = 1;
6670 }
6671
6672 static void
6673 do_branch (void)
6674 {
6675 #ifdef OBJ_ELF
6676   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
6677     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
6678   else
6679 #endif
6680     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
6681 }
6682
6683 static void
6684 do_bl (void)
6685 {
6686 #ifdef OBJ_ELF
6687   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
6688     {
6689       if (inst.cond == COND_ALWAYS)
6690         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
6691       else
6692         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
6693     }
6694   else
6695 #endif
6696     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
6697 }
6698
6699 /* ARM V5 branch-link-exchange instruction (argument parse)
6700      BLX <target_addr>          ie BLX(1)
6701      BLX{<condition>} <Rm>      ie BLX(2)
6702    Unfortunately, there are two different opcodes for this mnemonic.
6703    So, the insns[].value is not used, and the code here zaps values
6704         into inst.instruction.
6705    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
6706
6707 static void
6708 do_blx (void)
6709 {
6710   if (inst.operands[0].isreg)
6711     {
6712       /* Arg is a register; the opcode provided by insns[] is correct.
6713          It is not illegal to do "blx pc", just useless.  */
6714       if (inst.operands[0].reg == REG_PC)
6715         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
6716
6717       inst.instruction |= inst.operands[0].reg;
6718     }
6719   else
6720     {
6721       /* Arg is an address; this instruction cannot be executed
6722          conditionally, and the opcode must be adjusted.  */
6723       constraint (inst.cond != COND_ALWAYS, BAD_COND);
6724       inst.instruction = 0xfa000000;
6725 #ifdef OBJ_ELF
6726       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
6727         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
6728       else
6729 #endif
6730         encode_branch (BFD_RELOC_ARM_PCREL_BLX);
6731     }
6732 }
6733
6734 static void
6735 do_bx (void)
6736 {
6737   if (inst.operands[0].reg == REG_PC)
6738     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
6739
6740   inst.instruction |= inst.operands[0].reg;
6741 }
6742
6743
6744 /* ARM v5TEJ.  Jump to Jazelle code.  */
6745
6746 static void
6747 do_bxj (void)
6748 {
6749   if (inst.operands[0].reg == REG_PC)
6750     as_tsktsk (_("use of r15 in bxj is not really useful"));
6751
6752   inst.instruction |= inst.operands[0].reg;
6753 }
6754
6755 /* Co-processor data operation:
6756       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
6757       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
6758 static void
6759 do_cdp (void)
6760 {
6761   inst.instruction |= inst.operands[0].reg << 8;
6762   inst.instruction |= inst.operands[1].imm << 20;
6763   inst.instruction |= inst.operands[2].reg << 12;
6764   inst.instruction |= inst.operands[3].reg << 16;
6765   inst.instruction |= inst.operands[4].reg;
6766   inst.instruction |= inst.operands[5].imm << 5;
6767 }
6768
6769 static void
6770 do_cmp (void)
6771 {
6772   inst.instruction |= inst.operands[0].reg << 16;
6773   encode_arm_shifter_operand (1);
6774 }
6775
6776 /* Transfer between coprocessor and ARM registers.
6777    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
6778    MRC2
6779    MCR{cond}
6780    MCR2
6781
6782    No special properties.  */
6783
6784 static void
6785 do_co_reg (void)
6786 {
6787   inst.instruction |= inst.operands[0].reg << 8;
6788   inst.instruction |= inst.operands[1].imm << 21;
6789   inst.instruction |= inst.operands[2].reg << 12;
6790   inst.instruction |= inst.operands[3].reg << 16;
6791   inst.instruction |= inst.operands[4].reg;
6792   inst.instruction |= inst.operands[5].imm << 5;
6793 }
6794
6795 /* Transfer between coprocessor register and pair of ARM registers.
6796    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
6797    MCRR2
6798    MRRC{cond}
6799    MRRC2
6800
6801    Two XScale instructions are special cases of these:
6802
6803      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
6804      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
6805
6806    Result unpredicatable if Rd or Rn is R15.  */
6807
6808 static void
6809 do_co_reg2c (void)
6810 {
6811   inst.instruction |= inst.operands[0].reg << 8;
6812   inst.instruction |= inst.operands[1].imm << 4;
6813   inst.instruction |= inst.operands[2].reg << 12;
6814   inst.instruction |= inst.operands[3].reg << 16;
6815   inst.instruction |= inst.operands[4].reg;
6816 }
6817
6818 static void
6819 do_cpsi (void)
6820 {
6821   inst.instruction |= inst.operands[0].imm << 6;
6822   inst.instruction |= inst.operands[1].imm;
6823 }
6824
6825 static void
6826 do_dbg (void)
6827 {
6828   inst.instruction |= inst.operands[0].imm;
6829 }
6830
6831 static void
6832 do_it (void)
6833 {
6834   /* There is no IT instruction in ARM mode.  We
6835      process it but do not generate code for it.  */
6836   inst.size = 0;
6837 }
6838
6839 static void
6840 do_ldmstm (void)
6841 {
6842   int base_reg = inst.operands[0].reg;
6843   int range = inst.operands[1].imm;
6844
6845   inst.instruction |= base_reg << 16;
6846   inst.instruction |= range;
6847
6848   if (inst.operands[1].writeback)
6849     inst.instruction |= LDM_TYPE_2_OR_3;
6850
6851   if (inst.operands[0].writeback)
6852     {
6853       inst.instruction |= WRITE_BACK;
6854       /* Check for unpredictable uses of writeback.  */
6855       if (inst.instruction & LOAD_BIT)
6856         {
6857           /* Not allowed in LDM type 2.  */
6858           if ((inst.instruction & LDM_TYPE_2_OR_3)
6859               && ((range & (1 << REG_PC)) == 0))
6860             as_warn (_("writeback of base register is UNPREDICTABLE"));
6861           /* Only allowed if base reg not in list for other types.  */
6862           else if (range & (1 << base_reg))
6863             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
6864         }
6865       else /* STM.  */
6866         {
6867           /* Not allowed for type 2.  */
6868           if (inst.instruction & LDM_TYPE_2_OR_3)
6869             as_warn (_("writeback of base register is UNPREDICTABLE"));
6870           /* Only allowed if base reg not in list, or first in list.  */
6871           else if ((range & (1 << base_reg))
6872                    && (range & ((1 << base_reg) - 1)))
6873             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
6874         }
6875     }
6876 }
6877
6878 /* ARMv5TE load-consecutive (argument parse)
6879    Mode is like LDRH.
6880
6881      LDRccD R, mode
6882      STRccD R, mode.  */
6883
6884 static void
6885 do_ldrd (void)
6886 {
6887   constraint (inst.operands[0].reg % 2 != 0,
6888               _("first destination register must be even"));
6889   constraint (inst.operands[1].present
6890               && inst.operands[1].reg != inst.operands[0].reg + 1,
6891               _("can only load two consecutive registers"));
6892   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
6893   constraint (!inst.operands[2].isreg, _("'[' expected"));
6894
6895   if (!inst.operands[1].present)
6896     inst.operands[1].reg = inst.operands[0].reg + 1;
6897   
6898   if (inst.instruction & LOAD_BIT)
6899     {
6900       /* encode_arm_addr_mode_3 will diagnose overlap between the base
6901          register and the first register written; we have to diagnose
6902          overlap between the base and the second register written here.  */
6903
6904       if (inst.operands[2].reg == inst.operands[1].reg
6905           && (inst.operands[2].writeback || inst.operands[2].postind))
6906         as_warn (_("base register written back, and overlaps "
6907                    "second destination register"));
6908
6909       /* For an index-register load, the index register must not overlap the
6910          destination (even if not write-back).  */
6911       else if (inst.operands[2].immisreg
6912                && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
6913                    || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
6914         as_warn (_("index register overlaps destination register"));
6915     }
6916
6917   inst.instruction |= inst.operands[0].reg << 12;
6918   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
6919 }
6920
6921 static void
6922 do_ldrex (void)
6923 {
6924   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
6925               || inst.operands[1].postind || inst.operands[1].writeback
6926               || inst.operands[1].immisreg || inst.operands[1].shifted
6927               || inst.operands[1].negative
6928               /* This can arise if the programmer has written
6929                    strex rN, rM, foo
6930                  or if they have mistakenly used a register name as the last
6931                  operand,  eg:
6932                    strex rN, rM, rX
6933                  It is very difficult to distinguish between these two cases
6934                  because "rX" might actually be a label. ie the register
6935                  name has been occluded by a symbol of the same name. So we
6936                  just generate a general 'bad addressing mode' type error
6937                  message and leave it up to the programmer to discover the
6938                  true cause and fix their mistake.  */
6939               || (inst.operands[1].reg == REG_PC),
6940               BAD_ADDR_MODE);
6941
6942   constraint (inst.reloc.exp.X_op != O_constant
6943               || inst.reloc.exp.X_add_number != 0,
6944               _("offset must be zero in ARM encoding"));
6945
6946   inst.instruction |= inst.operands[0].reg << 12;
6947   inst.instruction |= inst.operands[1].reg << 16;
6948   inst.reloc.type = BFD_RELOC_UNUSED;
6949 }
6950
6951 static void
6952 do_ldrexd (void)
6953 {
6954   constraint (inst.operands[0].reg % 2 != 0,
6955               _("even register required"));
6956   constraint (inst.operands[1].present
6957               && inst.operands[1].reg != inst.operands[0].reg + 1,
6958               _("can only load two consecutive registers"));
6959   /* If op 1 were present and equal to PC, this function wouldn't
6960      have been called in the first place.  */
6961   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
6962
6963   inst.instruction |= inst.operands[0].reg << 12;
6964   inst.instruction |= inst.operands[2].reg << 16;
6965 }
6966
6967 static void
6968 do_ldst (void)
6969 {
6970   inst.instruction |= inst.operands[0].reg << 12;
6971   if (!inst.operands[1].isreg)
6972     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/FALSE))
6973       return;
6974   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
6975 }
6976
6977 static void
6978 do_ldstt (void)
6979 {
6980   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
6981      reject [Rn,...].  */
6982   if (inst.operands[1].preind)
6983     {
6984       constraint (inst.reloc.exp.X_op != O_constant ||
6985                   inst.reloc.exp.X_add_number != 0,
6986                   _("this instruction requires a post-indexed address"));
6987
6988       inst.operands[1].preind = 0;
6989       inst.operands[1].postind = 1;
6990       inst.operands[1].writeback = 1;
6991     }
6992   inst.instruction |= inst.operands[0].reg << 12;
6993   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
6994 }
6995
6996 /* Halfword and signed-byte load/store operations.  */
6997
6998 static void
6999 do_ldstv4 (void)
7000 {
7001   inst.instruction |= inst.operands[0].reg << 12;
7002   if (!inst.operands[1].isreg)
7003     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/TRUE))
7004       return;
7005   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
7006 }
7007
7008 static void
7009 do_ldsttv4 (void)
7010 {
7011   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
7012      reject [Rn,...].  */
7013   if (inst.operands[1].preind)
7014     {
7015       constraint (inst.reloc.exp.X_op != O_constant ||
7016                   inst.reloc.exp.X_add_number != 0,
7017                   _("this instruction requires a post-indexed address"));
7018
7019       inst.operands[1].preind = 0;
7020       inst.operands[1].postind = 1;
7021       inst.operands[1].writeback = 1;
7022     }
7023   inst.instruction |= inst.operands[0].reg << 12;
7024   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
7025 }
7026
7027 /* Co-processor register load/store.
7028    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
7029 static void
7030 do_lstc (void)
7031 {
7032   inst.instruction |= inst.operands[0].reg << 8;
7033   inst.instruction |= inst.operands[1].reg << 12;
7034   encode_arm_cp_address (2, TRUE, TRUE, 0);
7035 }
7036
7037 static void
7038 do_mlas (void)
7039 {
7040   /* This restriction does not apply to mls (nor to mla in v6, but
7041      that's hard to detect at present).  */
7042   if (inst.operands[0].reg == inst.operands[1].reg
7043       && !(inst.instruction & 0x00400000))
7044     as_tsktsk (_("rd and rm should be different in mla"));
7045
7046   inst.instruction |= inst.operands[0].reg << 16;
7047   inst.instruction |= inst.operands[1].reg;
7048   inst.instruction |= inst.operands[2].reg << 8;
7049   inst.instruction |= inst.operands[3].reg << 12;
7050
7051 }
7052
7053 static void
7054 do_mov (void)
7055 {
7056   inst.instruction |= inst.operands[0].reg << 12;
7057   encode_arm_shifter_operand (1);
7058 }
7059
7060 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
7061 static void
7062 do_mov16 (void)
7063 {
7064   bfd_vma imm;
7065   bfd_boolean top;
7066
7067   top = (inst.instruction & 0x00400000) != 0;
7068   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
7069               _(":lower16: not allowed this instruction"));
7070   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
7071               _(":upper16: not allowed instruction"));
7072   inst.instruction |= inst.operands[0].reg << 12;
7073   if (inst.reloc.type == BFD_RELOC_UNUSED)
7074     {
7075       imm = inst.reloc.exp.X_add_number;
7076       /* The value is in two pieces: 0:11, 16:19.  */
7077       inst.instruction |= (imm & 0x00000fff);
7078       inst.instruction |= (imm & 0x0000f000) << 4;
7079     }
7080 }
7081
7082 static void do_vfp_nsyn_opcode (const char *);
7083
7084 static int
7085 do_vfp_nsyn_mrs (void)
7086 {
7087   if (inst.operands[0].isvec)
7088     {
7089       if (inst.operands[1].reg != 1)
7090         first_error (_("operand 1 must be FPSCR"));
7091       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
7092       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
7093       do_vfp_nsyn_opcode ("fmstat");
7094     }
7095   else if (inst.operands[1].isvec)
7096     do_vfp_nsyn_opcode ("fmrx");
7097   else
7098     return FAIL;
7099     
7100   return SUCCESS;
7101 }
7102
7103 static int
7104 do_vfp_nsyn_msr (void)
7105 {
7106   if (inst.operands[0].isvec)
7107     do_vfp_nsyn_opcode ("fmxr");
7108   else
7109     return FAIL;
7110
7111   return SUCCESS;
7112 }
7113
7114 static void
7115 do_mrs (void)
7116 {
7117   if (do_vfp_nsyn_mrs () == SUCCESS)
7118     return;
7119
7120   /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
7121   constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
7122               != (PSR_c|PSR_f),
7123               _("'CPSR' or 'SPSR' expected"));
7124   inst.instruction |= inst.operands[0].reg << 12;
7125   inst.instruction |= (inst.operands[1].imm & SPSR_BIT);
7126 }
7127
7128 /* Two possible forms:
7129       "{C|S}PSR_<field>, Rm",
7130       "{C|S}PSR_f, #expression".  */
7131
7132 static void
7133 do_msr (void)
7134 {
7135   if (do_vfp_nsyn_msr () == SUCCESS)
7136     return;
7137
7138   inst.instruction |= inst.operands[0].imm;
7139   if (inst.operands[1].isreg)
7140     inst.instruction |= inst.operands[1].reg;
7141   else
7142     {
7143       inst.instruction |= INST_IMMEDIATE;
7144       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
7145       inst.reloc.pc_rel = 0;
7146     }
7147 }
7148
7149 static void
7150 do_mul (void)
7151 {
7152   if (!inst.operands[2].present)
7153     inst.operands[2].reg = inst.operands[0].reg;
7154   inst.instruction |= inst.operands[0].reg << 16;
7155   inst.instruction |= inst.operands[1].reg;
7156   inst.instruction |= inst.operands[2].reg << 8;
7157
7158   if (inst.operands[0].reg == inst.operands[1].reg)
7159     as_tsktsk (_("rd and rm should be different in mul"));
7160 }
7161
7162 /* Long Multiply Parser
7163    UMULL RdLo, RdHi, Rm, Rs
7164    SMULL RdLo, RdHi, Rm, Rs
7165    UMLAL RdLo, RdHi, Rm, Rs
7166    SMLAL RdLo, RdHi, Rm, Rs.  */
7167
7168 static void
7169 do_mull (void)
7170 {
7171   inst.instruction |= inst.operands[0].reg << 12;
7172   inst.instruction |= inst.operands[1].reg << 16;
7173   inst.instruction |= inst.operands[2].reg;
7174   inst.instruction |= inst.operands[3].reg << 8;
7175
7176   /* rdhi, rdlo and rm must all be different.  */
7177   if (inst.operands[0].reg == inst.operands[1].reg
7178       || inst.operands[0].reg == inst.operands[2].reg
7179       || inst.operands[1].reg == inst.operands[2].reg)
7180     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
7181 }
7182
7183 static void
7184 do_nop (void)
7185 {
7186   if (inst.operands[0].present)
7187     {
7188       /* Architectural NOP hints are CPSR sets with no bits selected.  */
7189       inst.instruction &= 0xf0000000;
7190       inst.instruction |= 0x0320f000 + inst.operands[0].imm;
7191     }
7192 }
7193
7194 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
7195    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
7196    Condition defaults to COND_ALWAYS.
7197    Error if Rd, Rn or Rm are R15.  */
7198
7199 static void
7200 do_pkhbt (void)
7201 {
7202   inst.instruction |= inst.operands[0].reg << 12;
7203   inst.instruction |= inst.operands[1].reg << 16;
7204   inst.instruction |= inst.operands[2].reg;
7205   if (inst.operands[3].present)
7206     encode_arm_shift (3);
7207 }
7208
7209 /* ARM V6 PKHTB (Argument Parse).  */
7210
7211 static void
7212 do_pkhtb (void)
7213 {
7214   if (!inst.operands[3].present)
7215     {
7216       /* If the shift specifier is omitted, turn the instruction
7217          into pkhbt rd, rm, rn. */
7218       inst.instruction &= 0xfff00010;
7219       inst.instruction |= inst.operands[0].reg << 12;
7220       inst.instruction |= inst.operands[1].reg;
7221       inst.instruction |= inst.operands[2].reg << 16;
7222     }
7223   else
7224     {
7225       inst.instruction |= inst.operands[0].reg << 12;
7226       inst.instruction |= inst.operands[1].reg << 16;
7227       inst.instruction |= inst.operands[2].reg;
7228       encode_arm_shift (3);
7229     }
7230 }
7231
7232 /* ARMv5TE: Preload-Cache
7233
7234     PLD <addr_mode>
7235
7236   Syntactically, like LDR with B=1, W=0, L=1.  */
7237
7238 static void
7239 do_pld (void)
7240 {
7241   constraint (!inst.operands[0].isreg,
7242               _("'[' expected after PLD mnemonic"));
7243   constraint (inst.operands[0].postind,
7244               _("post-indexed expression used in preload instruction"));
7245   constraint (inst.operands[0].writeback,
7246               _("writeback used in preload instruction"));
7247   constraint (!inst.operands[0].preind,
7248               _("unindexed addressing used in preload instruction"));
7249   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
7250 }
7251
7252 /* ARMv7: PLI <addr_mode>  */
7253 static void
7254 do_pli (void)
7255 {
7256   constraint (!inst.operands[0].isreg,
7257               _("'[' expected after PLI mnemonic"));
7258   constraint (inst.operands[0].postind,
7259               _("post-indexed expression used in preload instruction"));
7260   constraint (inst.operands[0].writeback,
7261               _("writeback used in preload instruction"));
7262   constraint (!inst.operands[0].preind,
7263               _("unindexed addressing used in preload instruction"));
7264   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
7265   inst.instruction &= ~PRE_INDEX;
7266 }
7267
7268 static void
7269 do_push_pop (void)
7270 {
7271   inst.operands[1] = inst.operands[0];
7272   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
7273   inst.operands[0].isreg = 1;
7274   inst.operands[0].writeback = 1;
7275   inst.operands[0].reg = REG_SP;
7276   do_ldmstm ();
7277 }
7278
7279 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
7280    word at the specified address and the following word
7281    respectively.
7282    Unconditionally executed.
7283    Error if Rn is R15.  */
7284
7285 static void
7286 do_rfe (void)
7287 {
7288   inst.instruction |= inst.operands[0].reg << 16;
7289   if (inst.operands[0].writeback)
7290     inst.instruction |= WRITE_BACK;
7291 }
7292
7293 /* ARM V6 ssat (argument parse).  */
7294
7295 static void
7296 do_ssat (void)
7297 {
7298   inst.instruction |= inst.operands[0].reg << 12;
7299   inst.instruction |= (inst.operands[1].imm - 1) << 16;
7300   inst.instruction |= inst.operands[2].reg;
7301
7302   if (inst.operands[3].present)
7303     encode_arm_shift (3);
7304 }
7305
7306 /* ARM V6 usat (argument parse).  */
7307
7308 static void
7309 do_usat (void)
7310 {
7311   inst.instruction |= inst.operands[0].reg << 12;
7312   inst.instruction |= inst.operands[1].imm << 16;
7313   inst.instruction |= inst.operands[2].reg;
7314
7315   if (inst.operands[3].present)
7316     encode_arm_shift (3);
7317 }
7318
7319 /* ARM V6 ssat16 (argument parse).  */
7320
7321 static void
7322 do_ssat16 (void)
7323 {
7324   inst.instruction |= inst.operands[0].reg << 12;
7325   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
7326   inst.instruction |= inst.operands[2].reg;
7327 }
7328
7329 static void
7330 do_usat16 (void)
7331 {
7332   inst.instruction |= inst.operands[0].reg << 12;
7333   inst.instruction |= inst.operands[1].imm << 16;
7334   inst.instruction |= inst.operands[2].reg;
7335 }
7336
7337 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
7338    preserving the other bits.
7339
7340    setend <endian_specifier>, where <endian_specifier> is either
7341    BE or LE.  */
7342
7343 static void
7344 do_setend (void)
7345 {
7346   if (inst.operands[0].imm)
7347     inst.instruction |= 0x200;
7348 }
7349
7350 static void
7351 do_shift (void)
7352 {
7353   unsigned int Rm = (inst.operands[1].present
7354                      ? inst.operands[1].reg
7355                      : inst.operands[0].reg);
7356
7357   inst.instruction |= inst.operands[0].reg << 12;
7358   inst.instruction |= Rm;
7359   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
7360     {
7361       inst.instruction |= inst.operands[2].reg << 8;
7362       inst.instruction |= SHIFT_BY_REG;
7363     }
7364   else
7365     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7366 }
7367
7368 static void
7369 do_smc (void)
7370 {
7371   inst.reloc.type = BFD_RELOC_ARM_SMC;
7372   inst.reloc.pc_rel = 0;
7373 }
7374
7375 static void
7376 do_swi (void)
7377 {
7378   inst.reloc.type = BFD_RELOC_ARM_SWI;
7379   inst.reloc.pc_rel = 0;
7380 }
7381
7382 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
7383    SMLAxy{cond} Rd,Rm,Rs,Rn
7384    SMLAWy{cond} Rd,Rm,Rs,Rn
7385    Error if any register is R15.  */
7386
7387 static void
7388 do_smla (void)
7389 {
7390   inst.instruction |= inst.operands[0].reg << 16;
7391   inst.instruction |= inst.operands[1].reg;
7392   inst.instruction |= inst.operands[2].reg << 8;
7393   inst.instruction |= inst.operands[3].reg << 12;
7394 }
7395
7396 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
7397    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
7398    Error if any register is R15.
7399    Warning if Rdlo == Rdhi.  */
7400
7401 static void
7402 do_smlal (void)
7403 {
7404   inst.instruction |= inst.operands[0].reg << 12;
7405   inst.instruction |= inst.operands[1].reg << 16;
7406   inst.instruction |= inst.operands[2].reg;
7407   inst.instruction |= inst.operands[3].reg << 8;
7408
7409   if (inst.operands[0].reg == inst.operands[1].reg)
7410     as_tsktsk (_("rdhi and rdlo must be different"));
7411 }
7412
7413 /* ARM V5E (El Segundo) signed-multiply (argument parse)
7414    SMULxy{cond} Rd,Rm,Rs
7415    Error if any register is R15.  */
7416
7417 static void
7418 do_smul (void)
7419 {
7420   inst.instruction |= inst.operands[0].reg << 16;
7421   inst.instruction |= inst.operands[1].reg;
7422   inst.instruction |= inst.operands[2].reg << 8;
7423 }
7424
7425 /* ARM V6 srs (argument parse).  */
7426
7427 static void
7428 do_srs (void)
7429 {
7430   inst.instruction |= inst.operands[0].imm;
7431   if (inst.operands[0].writeback)
7432     inst.instruction |= WRITE_BACK;
7433 }
7434
7435 /* ARM V6 strex (argument parse).  */
7436
7437 static void
7438 do_strex (void)
7439 {
7440   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
7441               || inst.operands[2].postind || inst.operands[2].writeback
7442               || inst.operands[2].immisreg || inst.operands[2].shifted
7443               || inst.operands[2].negative
7444               /* See comment in do_ldrex().  */
7445               || (inst.operands[2].reg == REG_PC),
7446               BAD_ADDR_MODE);
7447
7448   constraint (inst.operands[0].reg == inst.operands[1].reg
7449               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
7450
7451   constraint (inst.reloc.exp.X_op != O_constant
7452               || inst.reloc.exp.X_add_number != 0,
7453               _("offset must be zero in ARM encoding"));
7454
7455   inst.instruction |= inst.operands[0].reg << 12;
7456   inst.instruction |= inst.operands[1].reg;
7457   inst.instruction |= inst.operands[2].reg << 16;
7458   inst.reloc.type = BFD_RELOC_UNUSED;
7459 }
7460
7461 static void
7462 do_strexd (void)
7463 {
7464   constraint (inst.operands[1].reg % 2 != 0,
7465               _("even register required"));
7466   constraint (inst.operands[2].present
7467               && inst.operands[2].reg != inst.operands[1].reg + 1,
7468               _("can only store two consecutive registers"));
7469   /* If op 2 were present and equal to PC, this function wouldn't
7470      have been called in the first place.  */
7471   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
7472
7473   constraint (inst.operands[0].reg == inst.operands[1].reg
7474               || inst.operands[0].reg == inst.operands[1].reg + 1
7475               || inst.operands[0].reg == inst.operands[3].reg,
7476               BAD_OVERLAP);
7477
7478   inst.instruction |= inst.operands[0].reg << 12;
7479   inst.instruction |= inst.operands[1].reg;
7480   inst.instruction |= inst.operands[3].reg << 16;
7481 }
7482
7483 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
7484    extends it to 32-bits, and adds the result to a value in another
7485    register.  You can specify a rotation by 0, 8, 16, or 24 bits
7486    before extracting the 16-bit value.
7487    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
7488    Condition defaults to COND_ALWAYS.
7489    Error if any register uses R15.  */
7490
7491 static void
7492 do_sxtah (void)
7493 {
7494   inst.instruction |= inst.operands[0].reg << 12;
7495   inst.instruction |= inst.operands[1].reg << 16;
7496   inst.instruction |= inst.operands[2].reg;
7497   inst.instruction |= inst.operands[3].imm << 10;
7498 }
7499
7500 /* ARM V6 SXTH.
7501
7502    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
7503    Condition defaults to COND_ALWAYS.
7504    Error if any register uses R15.  */
7505
7506 static void
7507 do_sxth (void)
7508 {
7509   inst.instruction |= inst.operands[0].reg << 12;
7510   inst.instruction |= inst.operands[1].reg;
7511   inst.instruction |= inst.operands[2].imm << 10;
7512 }
7513 \f
7514 /* VFP instructions.  In a logical order: SP variant first, monad
7515    before dyad, arithmetic then move then load/store.  */
7516
7517 static void
7518 do_vfp_sp_monadic (void)
7519 {
7520   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
7521   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
7522 }
7523
7524 static void
7525 do_vfp_sp_dyadic (void)
7526 {
7527   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
7528   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
7529   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
7530 }
7531
7532 static void
7533 do_vfp_sp_compare_z (void)
7534 {
7535   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
7536 }
7537
7538 static void
7539 do_vfp_dp_sp_cvt (void)
7540 {
7541   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
7542   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
7543 }
7544
7545 static void
7546 do_vfp_sp_dp_cvt (void)
7547 {
7548   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
7549   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
7550 }
7551
7552 static void
7553 do_vfp_reg_from_sp (void)
7554 {
7555   inst.instruction |= inst.operands[0].reg << 12;
7556   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
7557 }
7558
7559 static void
7560 do_vfp_reg2_from_sp2 (void)
7561 {
7562   constraint (inst.operands[2].imm != 2,
7563               _("only two consecutive VFP SP registers allowed here"));
7564   inst.instruction |= inst.operands[0].reg << 12;
7565   inst.instruction |= inst.operands[1].reg << 16;
7566   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
7567 }
7568
7569 static void
7570 do_vfp_sp_from_reg (void)
7571 {
7572   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
7573   inst.instruction |= inst.operands[1].reg << 12;
7574 }
7575
7576 static void
7577 do_vfp_sp2_from_reg2 (void)
7578 {
7579   constraint (inst.operands[0].imm != 2,
7580               _("only two consecutive VFP SP registers allowed here"));
7581   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
7582   inst.instruction |= inst.operands[1].reg << 12;
7583   inst.instruction |= inst.operands[2].reg << 16;
7584 }
7585
7586 static void
7587 do_vfp_sp_ldst (void)
7588 {
7589   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
7590   encode_arm_cp_address (1, FALSE, TRUE, 0);
7591 }
7592
7593 static void
7594 do_vfp_dp_ldst (void)
7595 {
7596   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
7597   encode_arm_cp_address (1, FALSE, TRUE, 0);
7598 }
7599
7600
7601 static void
7602 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
7603 {
7604   if (inst.operands[0].writeback)
7605     inst.instruction |= WRITE_BACK;
7606   else
7607     constraint (ldstm_type != VFP_LDSTMIA,
7608                 _("this addressing mode requires base-register writeback"));
7609   inst.instruction |= inst.operands[0].reg << 16;
7610   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
7611   inst.instruction |= inst.operands[1].imm;
7612 }
7613
7614 static void
7615 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
7616 {
7617   int count;
7618
7619   if (inst.operands[0].writeback)
7620     inst.instruction |= WRITE_BACK;
7621   else
7622     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
7623                 _("this addressing mode requires base-register writeback"));
7624
7625   inst.instruction |= inst.operands[0].reg << 16;
7626   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
7627
7628   count = inst.operands[1].imm << 1;
7629   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
7630     count += 1;
7631
7632   inst.instruction |= count;
7633 }
7634
7635 static void
7636 do_vfp_sp_ldstmia (void)
7637 {
7638   vfp_sp_ldstm (VFP_LDSTMIA);
7639 }
7640
7641 static void
7642 do_vfp_sp_ldstmdb (void)
7643 {
7644   vfp_sp_ldstm (VFP_LDSTMDB);
7645 }
7646
7647 static void
7648 do_vfp_dp_ldstmia (void)
7649 {
7650   vfp_dp_ldstm (VFP_LDSTMIA);
7651 }
7652
7653 static void
7654 do_vfp_dp_ldstmdb (void)
7655 {
7656   vfp_dp_ldstm (VFP_LDSTMDB);
7657 }
7658
7659 static void
7660 do_vfp_xp_ldstmia (void)
7661 {
7662   vfp_dp_ldstm (VFP_LDSTMIAX);
7663 }
7664
7665 static void
7666 do_vfp_xp_ldstmdb (void)
7667 {
7668   vfp_dp_ldstm (VFP_LDSTMDBX);
7669 }
7670
7671 static void
7672 do_vfp_dp_rd_rm (void)
7673 {
7674   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
7675   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
7676 }
7677
7678 static void
7679 do_vfp_dp_rn_rd (void)
7680 {
7681   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
7682   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
7683 }
7684
7685 static void
7686 do_vfp_dp_rd_rn (void)
7687 {
7688   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
7689   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
7690 }
7691
7692 static void
7693 do_vfp_dp_rd_rn_rm (void)
7694 {
7695   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
7696   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
7697   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
7698 }
7699
7700 static void
7701 do_vfp_dp_rd (void)
7702 {
7703   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
7704 }
7705
7706 static void
7707 do_vfp_dp_rm_rd_rn (void)
7708 {
7709   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
7710   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
7711   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
7712 }
7713
7714 /* VFPv3 instructions.  */
7715 static void
7716 do_vfp_sp_const (void)
7717 {
7718   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
7719   inst.instruction |= (inst.operands[1].imm & 15) << 16;
7720   inst.instruction |= (inst.operands[1].imm >> 4);
7721 }
7722
7723 static void
7724 do_vfp_dp_const (void)
7725 {
7726   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
7727   inst.instruction |= (inst.operands[1].imm & 15) << 16;
7728   inst.instruction |= (inst.operands[1].imm >> 4);
7729 }
7730
7731 static void
7732 vfp_conv (int srcsize)
7733 {
7734   unsigned immbits = srcsize - inst.operands[1].imm;
7735   inst.instruction |= (immbits & 1) << 5;
7736   inst.instruction |= (immbits >> 1);
7737 }
7738
7739 static void
7740 do_vfp_sp_conv_16 (void)
7741 {
7742   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
7743   vfp_conv (16);
7744 }
7745
7746 static void
7747 do_vfp_dp_conv_16 (void)
7748 {
7749   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
7750   vfp_conv (16);
7751 }
7752
7753 static void
7754 do_vfp_sp_conv_32 (void)
7755 {
7756   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
7757   vfp_conv (32);
7758 }
7759
7760 static void
7761 do_vfp_dp_conv_32 (void)
7762 {
7763   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
7764   vfp_conv (32);
7765 }
7766
7767 \f
7768 /* FPA instructions.  Also in a logical order.  */
7769
7770 static void
7771 do_fpa_cmp (void)
7772 {
7773   inst.instruction |= inst.operands[0].reg << 16;
7774   inst.instruction |= inst.operands[1].reg;
7775 }
7776
7777 static void
7778 do_fpa_ldmstm (void)
7779 {
7780   inst.instruction |= inst.operands[0].reg << 12;
7781   switch (inst.operands[1].imm)
7782     {
7783     case 1: inst.instruction |= CP_T_X;          break;
7784     case 2: inst.instruction |= CP_T_Y;          break;
7785     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
7786     case 4:                                      break;
7787     default: abort ();
7788     }
7789
7790   if (inst.instruction & (PRE_INDEX | INDEX_UP))
7791     {
7792       /* The instruction specified "ea" or "fd", so we can only accept
7793          [Rn]{!}.  The instruction does not really support stacking or
7794          unstacking, so we have to emulate these by setting appropriate
7795          bits and offsets.  */
7796       constraint (inst.reloc.exp.X_op != O_constant
7797                   || inst.reloc.exp.X_add_number != 0,
7798                   _("this instruction does not support indexing"));
7799
7800       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
7801         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
7802
7803       if (!(inst.instruction & INDEX_UP))
7804         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
7805
7806       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
7807         {
7808           inst.operands[2].preind = 0;
7809           inst.operands[2].postind = 1;
7810         }
7811     }
7812
7813   encode_arm_cp_address (2, TRUE, TRUE, 0);
7814 }
7815
7816 \f
7817 /* iWMMXt instructions: strictly in alphabetical order.  */
7818
7819 static void
7820 do_iwmmxt_tandorc (void)
7821 {
7822   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
7823 }
7824
7825 static void
7826 do_iwmmxt_textrc (void)
7827 {
7828   inst.instruction |= inst.operands[0].reg << 12;
7829   inst.instruction |= inst.operands[1].imm;
7830 }
7831
7832 static void
7833 do_iwmmxt_textrm (void)
7834 {
7835   inst.instruction |= inst.operands[0].reg << 12;
7836   inst.instruction |= inst.operands[1].reg << 16;
7837   inst.instruction |= inst.operands[2].imm;
7838 }
7839
7840 static void
7841 do_iwmmxt_tinsr (void)
7842 {
7843   inst.instruction |= inst.operands[0].reg << 16;
7844   inst.instruction |= inst.operands[1].reg << 12;
7845   inst.instruction |= inst.operands[2].imm;
7846 }
7847
7848 static void
7849 do_iwmmxt_tmia (void)
7850 {
7851   inst.instruction |= inst.operands[0].reg << 5;
7852   inst.instruction |= inst.operands[1].reg;
7853   inst.instruction |= inst.operands[2].reg << 12;
7854 }
7855
7856 static void
7857 do_iwmmxt_waligni (void)
7858 {
7859   inst.instruction |= inst.operands[0].reg << 12;
7860   inst.instruction |= inst.operands[1].reg << 16;
7861   inst.instruction |= inst.operands[2].reg;
7862   inst.instruction |= inst.operands[3].imm << 20;
7863 }
7864
7865 static void
7866 do_iwmmxt_wmerge (void)
7867 {
7868   inst.instruction |= inst.operands[0].reg << 12;
7869   inst.instruction |= inst.operands[1].reg << 16;
7870   inst.instruction |= inst.operands[2].reg;
7871   inst.instruction |= inst.operands[3].imm << 21;
7872 }
7873
7874 static void
7875 do_iwmmxt_wmov (void)
7876 {
7877   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
7878   inst.instruction |= inst.operands[0].reg << 12;
7879   inst.instruction |= inst.operands[1].reg << 16;
7880   inst.instruction |= inst.operands[1].reg;
7881 }
7882
7883 static void
7884 do_iwmmxt_wldstbh (void)
7885 {
7886   int reloc;
7887   inst.instruction |= inst.operands[0].reg << 12;
7888   if (thumb_mode)
7889     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
7890   else
7891     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
7892   encode_arm_cp_address (1, TRUE, FALSE, reloc);
7893 }
7894
7895 static void
7896 do_iwmmxt_wldstw (void)
7897 {
7898   /* RIWR_RIWC clears .isreg for a control register.  */
7899   if (!inst.operands[0].isreg)
7900     {
7901       constraint (inst.cond != COND_ALWAYS, BAD_COND);
7902       inst.instruction |= 0xf0000000;
7903     }
7904
7905   inst.instruction |= inst.operands[0].reg << 12;
7906   encode_arm_cp_address (1, TRUE, TRUE, 0);
7907 }
7908
7909 static void
7910 do_iwmmxt_wldstd (void)
7911 {
7912   inst.instruction |= inst.operands[0].reg << 12;
7913   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
7914       && inst.operands[1].immisreg)
7915     {
7916       inst.instruction &= ~0x1a000ff;
7917       inst.instruction |= (0xf << 28);
7918       if (inst.operands[1].preind)
7919         inst.instruction |= PRE_INDEX;
7920       if (!inst.operands[1].negative)
7921         inst.instruction |= INDEX_UP;
7922       if (inst.operands[1].writeback)
7923         inst.instruction |= WRITE_BACK;
7924       inst.instruction |= inst.operands[1].reg << 16;
7925       inst.instruction |= inst.reloc.exp.X_add_number << 4;
7926       inst.instruction |= inst.operands[1].imm;
7927     }
7928   else
7929     encode_arm_cp_address (1, TRUE, FALSE, 0);
7930 }
7931
7932 static void
7933 do_iwmmxt_wshufh (void)
7934 {
7935   inst.instruction |= inst.operands[0].reg << 12;
7936   inst.instruction |= inst.operands[1].reg << 16;
7937   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
7938   inst.instruction |= (inst.operands[2].imm & 0x0f);
7939 }
7940
7941 static void
7942 do_iwmmxt_wzero (void)
7943 {
7944   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
7945   inst.instruction |= inst.operands[0].reg;
7946   inst.instruction |= inst.operands[0].reg << 12;
7947   inst.instruction |= inst.operands[0].reg << 16;
7948 }
7949
7950 static void
7951 do_iwmmxt_wrwrwr_or_imm5 (void)
7952 {
7953   if (inst.operands[2].isreg)
7954     do_rd_rn_rm ();
7955   else {
7956     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
7957                 _("immediate operand requires iWMMXt2"));
7958     do_rd_rn ();
7959     if (inst.operands[2].imm == 0)
7960       {
7961         switch ((inst.instruction >> 20) & 0xf)
7962           {
7963           case 4:
7964           case 5:
7965           case 6:
7966           case 7: 
7967             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
7968             inst.operands[2].imm = 16;
7969             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
7970             break;
7971           case 8:
7972           case 9:
7973           case 10:
7974           case 11:
7975             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
7976             inst.operands[2].imm = 32;
7977             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
7978             break;
7979           case 12:
7980           case 13:
7981           case 14:
7982           case 15:
7983             {
7984               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
7985               unsigned long wrn;
7986               wrn = (inst.instruction >> 16) & 0xf;
7987               inst.instruction &= 0xff0fff0f;
7988               inst.instruction |= wrn;
7989               /* Bail out here; the instruction is now assembled.  */
7990               return;
7991             }
7992           }
7993       }
7994     /* Map 32 -> 0, etc.  */
7995     inst.operands[2].imm &= 0x1f;
7996     inst.instruction |= (0xf << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
7997   }
7998 }
7999 \f
8000 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
8001    operations first, then control, shift, and load/store.  */
8002
8003 /* Insns like "foo X,Y,Z".  */
8004
8005 static void
8006 do_mav_triple (void)
8007 {
8008   inst.instruction |= inst.operands[0].reg << 16;
8009   inst.instruction |= inst.operands[1].reg;
8010   inst.instruction |= inst.operands[2].reg << 12;
8011 }
8012
8013 /* Insns like "foo W,X,Y,Z".
8014     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
8015
8016 static void
8017 do_mav_quad (void)
8018 {
8019   inst.instruction |= inst.operands[0].reg << 5;
8020   inst.instruction |= inst.operands[1].reg << 12;
8021   inst.instruction |= inst.operands[2].reg << 16;
8022   inst.instruction |= inst.operands[3].reg;
8023 }
8024
8025 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
8026 static void
8027 do_mav_dspsc (void)
8028 {
8029   inst.instruction |= inst.operands[1].reg << 12;
8030 }
8031
8032 /* Maverick shift immediate instructions.
8033    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
8034    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
8035
8036 static void
8037 do_mav_shift (void)
8038 {
8039   int imm = inst.operands[2].imm;
8040
8041   inst.instruction |= inst.operands[0].reg << 12;
8042   inst.instruction |= inst.operands[1].reg << 16;
8043
8044   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
8045      Bits 5-7 of the insn should have bits 4-6 of the immediate.
8046      Bit 4 should be 0.  */
8047   imm = (imm & 0xf) | ((imm & 0x70) << 1);
8048
8049   inst.instruction |= imm;
8050 }
8051 \f
8052 /* XScale instructions.  Also sorted arithmetic before move.  */
8053
8054 /* Xscale multiply-accumulate (argument parse)
8055      MIAcc   acc0,Rm,Rs
8056      MIAPHcc acc0,Rm,Rs
8057      MIAxycc acc0,Rm,Rs.  */
8058
8059 static void
8060 do_xsc_mia (void)
8061 {
8062   inst.instruction |= inst.operands[1].reg;
8063   inst.instruction |= inst.operands[2].reg << 12;
8064 }
8065
8066 /* Xscale move-accumulator-register (argument parse)
8067
8068      MARcc   acc0,RdLo,RdHi.  */
8069
8070 static void
8071 do_xsc_mar (void)
8072 {
8073   inst.instruction |= inst.operands[1].reg << 12;
8074   inst.instruction |= inst.operands[2].reg << 16;
8075 }
8076
8077 /* Xscale move-register-accumulator (argument parse)
8078
8079      MRAcc   RdLo,RdHi,acc0.  */
8080
8081 static void
8082 do_xsc_mra (void)
8083 {
8084   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
8085   inst.instruction |= inst.operands[0].reg << 12;
8086   inst.instruction |= inst.operands[1].reg << 16;
8087 }
8088 \f
8089 /* Encoding functions relevant only to Thumb.  */
8090
8091 /* inst.operands[i] is a shifted-register operand; encode
8092    it into inst.instruction in the format used by Thumb32.  */
8093
8094 static void
8095 encode_thumb32_shifted_operand (int i)
8096 {
8097   unsigned int value = inst.reloc.exp.X_add_number;
8098   unsigned int shift = inst.operands[i].shift_kind;
8099
8100   constraint (inst.operands[i].immisreg,
8101               _("shift by register not allowed in thumb mode"));
8102   inst.instruction |= inst.operands[i].reg;
8103   if (shift == SHIFT_RRX)
8104     inst.instruction |= SHIFT_ROR << 4;
8105   else
8106     {
8107       constraint (inst.reloc.exp.X_op != O_constant,
8108                   _("expression too complex"));
8109
8110       constraint (value > 32
8111                   || (value == 32 && (shift == SHIFT_LSL
8112                                       || shift == SHIFT_ROR)),
8113                   _("shift expression is too large"));
8114
8115       if (value == 0)
8116         shift = SHIFT_LSL;
8117       else if (value == 32)
8118         value = 0;
8119
8120       inst.instruction |= shift << 4;
8121       inst.instruction |= (value & 0x1c) << 10;
8122       inst.instruction |= (value & 0x03) << 6;
8123     }
8124 }
8125
8126
8127 /* inst.operands[i] was set up by parse_address.  Encode it into a
8128    Thumb32 format load or store instruction.  Reject forms that cannot
8129    be used with such instructions.  If is_t is true, reject forms that
8130    cannot be used with a T instruction; if is_d is true, reject forms
8131    that cannot be used with a D instruction.  */
8132
8133 static void
8134 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
8135 {
8136   bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
8137
8138   constraint (!inst.operands[i].isreg,
8139               _("Instruction does not support =N addresses"));
8140
8141   inst.instruction |= inst.operands[i].reg << 16;
8142   if (inst.operands[i].immisreg)
8143     {
8144       constraint (is_pc, _("cannot use register index with PC-relative addressing"));
8145       constraint (is_t || is_d, _("cannot use register index with this instruction"));
8146       constraint (inst.operands[i].negative,
8147                   _("Thumb does not support negative register indexing"));
8148       constraint (inst.operands[i].postind,
8149                   _("Thumb does not support register post-indexing"));
8150       constraint (inst.operands[i].writeback,
8151                   _("Thumb does not support register indexing with writeback"));
8152       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
8153                   _("Thumb supports only LSL in shifted register indexing"));
8154
8155       inst.instruction |= inst.operands[i].imm;
8156       if (inst.operands[i].shifted)
8157         {
8158           constraint (inst.reloc.exp.X_op != O_constant,
8159                       _("expression too complex"));
8160           constraint (inst.reloc.exp.X_add_number < 0
8161                       || inst.reloc.exp.X_add_number > 3,
8162                       _("shift out of range"));
8163           inst.instruction |= inst.reloc.exp.X_add_number << 4;
8164         }
8165       inst.reloc.type = BFD_RELOC_UNUSED;
8166     }
8167   else if (inst.operands[i].preind)
8168     {
8169       constraint (is_pc && inst.operands[i].writeback,
8170                   _("cannot use writeback with PC-relative addressing"));
8171       constraint (is_t && inst.operands[i].writeback,
8172                   _("cannot use writeback with this instruction"));
8173
8174       if (is_d)
8175         {
8176           inst.instruction |= 0x01000000;
8177           if (inst.operands[i].writeback)
8178             inst.instruction |= 0x00200000;
8179         }
8180       else
8181         {
8182           inst.instruction |= 0x00000c00;
8183           if (inst.operands[i].writeback)
8184             inst.instruction |= 0x00000100;
8185         }
8186       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
8187     }
8188   else if (inst.operands[i].postind)
8189     {
8190       assert (inst.operands[i].writeback);
8191       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
8192       constraint (is_t, _("cannot use post-indexing with this instruction"));
8193
8194       if (is_d)
8195         inst.instruction |= 0x00200000;
8196       else
8197         inst.instruction |= 0x00000900;
8198       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
8199     }
8200   else /* unindexed - only for coprocessor */
8201     inst.error = _("instruction does not accept unindexed addressing");
8202 }
8203
8204 /* Table of Thumb instructions which exist in both 16- and 32-bit
8205    encodings (the latter only in post-V6T2 cores).  The index is the
8206    value used in the insns table below.  When there is more than one
8207    possible 16-bit encoding for the instruction, this table always
8208    holds variant (1).
8209    Also contains several pseudo-instructions used during relaxation.  */
8210 #define T16_32_TAB                              \
8211   X(adc,   4140, eb400000),                     \
8212   X(adcs,  4140, eb500000),                     \
8213   X(add,   1c00, eb000000),                     \
8214   X(adds,  1c00, eb100000),                     \
8215   X(addi,  0000, f1000000),                     \
8216   X(addis, 0000, f1100000),                     \
8217   X(add_pc,000f, f20f0000),                     \
8218   X(add_sp,000d, f10d0000),                     \
8219   X(adr,   000f, f20f0000),                     \
8220   X(and,   4000, ea000000),                     \
8221   X(ands,  4000, ea100000),                     \
8222   X(asr,   1000, fa40f000),                     \
8223   X(asrs,  1000, fa50f000),                     \
8224   X(b,     e000, f000b000),                     \
8225   X(bcond, d000, f0008000),                     \
8226   X(bic,   4380, ea200000),                     \
8227   X(bics,  4380, ea300000),                     \
8228   X(cmn,   42c0, eb100f00),                     \
8229   X(cmp,   2800, ebb00f00),                     \
8230   X(cpsie, b660, f3af8400),                     \
8231   X(cpsid, b670, f3af8600),                     \
8232   X(cpy,   4600, ea4f0000),                     \
8233   X(dec_sp,80dd, f1bd0d00),                     \
8234   X(eor,   4040, ea800000),                     \
8235   X(eors,  4040, ea900000),                     \
8236   X(inc_sp,00dd, f10d0d00),                     \
8237   X(ldmia, c800, e8900000),                     \
8238   X(ldr,   6800, f8500000),                     \
8239   X(ldrb,  7800, f8100000),                     \
8240   X(ldrh,  8800, f8300000),                     \
8241   X(ldrsb, 5600, f9100000),                     \
8242   X(ldrsh, 5e00, f9300000),                     \
8243   X(ldr_pc,4800, f85f0000),                     \
8244   X(ldr_pc2,4800, f85f0000),                    \
8245   X(ldr_sp,9800, f85d0000),                     \
8246   X(lsl,   0000, fa00f000),                     \
8247   X(lsls,  0000, fa10f000),                     \
8248   X(lsr,   0800, fa20f000),                     \
8249   X(lsrs,  0800, fa30f000),                     \
8250   X(mov,   2000, ea4f0000),                     \
8251   X(movs,  2000, ea5f0000),                     \
8252   X(mul,   4340, fb00f000),                     \
8253   X(muls,  4340, ffffffff), /* no 32b muls */   \
8254   X(mvn,   43c0, ea6f0000),                     \
8255   X(mvns,  43c0, ea7f0000),                     \
8256   X(neg,   4240, f1c00000), /* rsb #0 */        \
8257   X(negs,  4240, f1d00000), /* rsbs #0 */       \
8258   X(orr,   4300, ea400000),                     \
8259   X(orrs,  4300, ea500000),                     \
8260   X(pop,   bc00, e8bd0000), /* ldmia sp!,... */ \
8261   X(push,  b400, e92d0000), /* stmdb sp!,... */ \
8262   X(rev,   ba00, fa90f080),                     \
8263   X(rev16, ba40, fa90f090),                     \
8264   X(revsh, bac0, fa90f0b0),                     \
8265   X(ror,   41c0, fa60f000),                     \
8266   X(rors,  41c0, fa70f000),                     \
8267   X(sbc,   4180, eb600000),                     \
8268   X(sbcs,  4180, eb700000),                     \
8269   X(stmia, c000, e8800000),                     \
8270   X(str,   6000, f8400000),                     \
8271   X(strb,  7000, f8000000),                     \
8272   X(strh,  8000, f8200000),                     \
8273   X(str_sp,9000, f84d0000),                     \
8274   X(sub,   1e00, eba00000),                     \
8275   X(subs,  1e00, ebb00000),                     \
8276   X(subi,  8000, f1a00000),                     \
8277   X(subis, 8000, f1b00000),                     \
8278   X(sxtb,  b240, fa4ff080),                     \
8279   X(sxth,  b200, fa0ff080),                     \
8280   X(tst,   4200, ea100f00),                     \
8281   X(uxtb,  b2c0, fa5ff080),                     \
8282   X(uxth,  b280, fa1ff080),                     \
8283   X(nop,   bf00, f3af8000),                     \
8284   X(yield, bf10, f3af8001),                     \
8285   X(wfe,   bf20, f3af8002),                     \
8286   X(wfi,   bf30, f3af8003),                     \
8287   X(sev,   bf40, f3af9004), /* typo, 8004? */
8288
8289 /* To catch errors in encoding functions, the codes are all offset by
8290    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
8291    as 16-bit instructions.  */
8292 #define X(a,b,c) T_MNEM_##a
8293 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
8294 #undef X
8295
8296 #define X(a,b,c) 0x##b
8297 static const unsigned short thumb_op16[] = { T16_32_TAB };
8298 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
8299 #undef X
8300
8301 #define X(a,b,c) 0x##c
8302 static const unsigned int thumb_op32[] = { T16_32_TAB };
8303 #define THUMB_OP32(n) (thumb_op32[(n) - (T16_32_OFFSET + 1)])
8304 #define THUMB_SETS_FLAGS(n) (THUMB_OP32 (n) & 0x00100000)
8305 #undef X
8306 #undef T16_32_TAB
8307
8308 /* Thumb instruction encoders, in alphabetical order.  */
8309
8310 /* ADDW or SUBW.  */
8311 static void
8312 do_t_add_sub_w (void)
8313 {
8314   int Rd, Rn;
8315
8316   Rd = inst.operands[0].reg;
8317   Rn = inst.operands[1].reg;
8318
8319   constraint (Rd == 15, _("PC not allowed as destination"));
8320   inst.instruction |= (Rn << 16) | (Rd << 8);
8321   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
8322 }
8323
8324 /* Parse an add or subtract instruction.  We get here with inst.instruction
8325    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
8326
8327 static void
8328 do_t_add_sub (void)
8329 {
8330   int Rd, Rs, Rn;
8331
8332   Rd = inst.operands[0].reg;
8333   Rs = (inst.operands[1].present
8334         ? inst.operands[1].reg    /* Rd, Rs, foo */
8335         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
8336
8337   if (unified_syntax)
8338     {
8339       bfd_boolean flags;
8340       bfd_boolean narrow;
8341       int opcode;
8342
8343       flags = (inst.instruction == T_MNEM_adds
8344                || inst.instruction == T_MNEM_subs);
8345       if (flags)
8346         narrow = (current_it_mask == 0);
8347       else
8348         narrow = (current_it_mask != 0);
8349       if (!inst.operands[2].isreg)
8350         {
8351           int add;
8352
8353           add = (inst.instruction == T_MNEM_add
8354                  || inst.instruction == T_MNEM_adds);
8355           opcode = 0;
8356           if (inst.size_req != 4)
8357             {
8358               /* Attempt to use a narrow opcode, with relaxation if
8359                  appropriate.  */
8360               if (Rd == REG_SP && Rs == REG_SP && !flags)
8361                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
8362               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
8363                 opcode = T_MNEM_add_sp;
8364               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
8365                 opcode = T_MNEM_add_pc;
8366               else if (Rd <= 7 && Rs <= 7 && narrow)
8367                 {
8368                   if (flags)
8369                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
8370                   else
8371                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
8372                 }
8373               if (opcode)
8374                 {
8375                   inst.instruction = THUMB_OP16(opcode);
8376                   inst.instruction |= (Rd << 4) | Rs;
8377                   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
8378                   if (inst.size_req != 2)
8379                     inst.relax = opcode;
8380                 }
8381               else
8382                 constraint (inst.size_req == 2, BAD_HIREG);
8383             }
8384           if (inst.size_req == 4
8385               || (inst.size_req != 2 && !opcode))
8386             {
8387               if (Rs == REG_PC)
8388                 {
8389                   /* Always use addw/subw.  */
8390                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
8391                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
8392                 }
8393               else
8394                 {
8395                   inst.instruction = THUMB_OP32 (inst.instruction);
8396                   inst.instruction = (inst.instruction & 0xe1ffffff)
8397                                      | 0x10000000;
8398                   if (flags)
8399                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
8400                   else
8401                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
8402                 }
8403               inst.instruction |= inst.operands[0].reg << 8;
8404               inst.instruction |= inst.operands[1].reg << 16;
8405             }
8406         }
8407       else
8408         {
8409           Rn = inst.operands[2].reg;
8410           /* See if we can do this with a 16-bit instruction.  */
8411           if (!inst.operands[2].shifted && inst.size_req != 4)
8412             {
8413               if (Rd > 7 || Rs > 7 || Rn > 7)
8414                 narrow = FALSE;
8415
8416               if (narrow)
8417                 {
8418                   inst.instruction = ((inst.instruction == T_MNEM_adds
8419                                        || inst.instruction == T_MNEM_add)
8420                                       ? T_OPCODE_ADD_R3
8421                                       : T_OPCODE_SUB_R3);
8422                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
8423                   return;
8424                 }
8425
8426               if (inst.instruction == T_MNEM_add)
8427                 {
8428                   if (Rd == Rs)
8429                     {
8430                       inst.instruction = T_OPCODE_ADD_HI;
8431                       inst.instruction |= (Rd & 8) << 4;
8432                       inst.instruction |= (Rd & 7);
8433                       inst.instruction |= Rn << 3;
8434                       return;
8435                     }
8436                   /* ... because addition is commutative! */
8437                   else if (Rd == Rn)
8438                     {
8439                       inst.instruction = T_OPCODE_ADD_HI;
8440                       inst.instruction |= (Rd & 8) << 4;
8441                       inst.instruction |= (Rd & 7);
8442                       inst.instruction |= Rs << 3;
8443                       return;
8444                     }
8445                 }
8446             }
8447           /* If we get here, it can't be done in 16 bits.  */
8448           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
8449                       _("shift must be constant"));
8450           inst.instruction = THUMB_OP32 (inst.instruction);
8451           inst.instruction |= Rd << 8;
8452           inst.instruction |= Rs << 16;
8453           encode_thumb32_shifted_operand (2);
8454         }
8455     }
8456   else
8457     {
8458       constraint (inst.instruction == T_MNEM_adds
8459                   || inst.instruction == T_MNEM_subs,
8460                   BAD_THUMB32);
8461
8462       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
8463         {
8464           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
8465                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
8466                       BAD_HIREG);
8467
8468           inst.instruction = (inst.instruction == T_MNEM_add
8469                               ? 0x0000 : 0x8000);
8470           inst.instruction |= (Rd << 4) | Rs;
8471           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
8472           return;
8473         }
8474
8475       Rn = inst.operands[2].reg;
8476       constraint (inst.operands[2].shifted, _("unshifted register required"));
8477
8478       /* We now have Rd, Rs, and Rn set to registers.  */
8479       if (Rd > 7 || Rs > 7 || Rn > 7)
8480         {
8481           /* Can't do this for SUB.      */
8482           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
8483           inst.instruction = T_OPCODE_ADD_HI;
8484           inst.instruction |= (Rd & 8) << 4;
8485           inst.instruction |= (Rd & 7);
8486           if (Rs == Rd)
8487             inst.instruction |= Rn << 3;
8488           else if (Rn == Rd)
8489             inst.instruction |= Rs << 3;
8490           else
8491             constraint (1, _("dest must overlap one source register"));
8492         }
8493       else
8494         {
8495           inst.instruction = (inst.instruction == T_MNEM_add
8496                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
8497           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
8498         }
8499     }
8500 }
8501
8502 static void
8503 do_t_adr (void)
8504 {
8505   if (unified_syntax && inst.size_req == 0 && inst.operands[0].reg <= 7)
8506     {
8507       /* Defer to section relaxation.  */
8508       inst.relax = inst.instruction;
8509       inst.instruction = THUMB_OP16 (inst.instruction);
8510       inst.instruction |= inst.operands[0].reg << 4;
8511     }
8512   else if (unified_syntax && inst.size_req != 2)
8513     {
8514       /* Generate a 32-bit opcode.  */
8515       inst.instruction = THUMB_OP32 (inst.instruction);
8516       inst.instruction |= inst.operands[0].reg << 8;
8517       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
8518       inst.reloc.pc_rel = 1;
8519     }
8520   else
8521     {
8522       /* Generate a 16-bit opcode.  */
8523       inst.instruction = THUMB_OP16 (inst.instruction);
8524       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
8525       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
8526       inst.reloc.pc_rel = 1;
8527
8528       inst.instruction |= inst.operands[0].reg << 4;
8529     }
8530 }
8531
8532 /* Arithmetic instructions for which there is just one 16-bit
8533    instruction encoding, and it allows only two low registers.
8534    For maximal compatibility with ARM syntax, we allow three register
8535    operands even when Thumb-32 instructions are not available, as long
8536    as the first two are identical.  For instance, both "sbc r0,r1" and
8537    "sbc r0,r0,r1" are allowed.  */
8538 static void
8539 do_t_arit3 (void)
8540 {
8541   int Rd, Rs, Rn;
8542
8543   Rd = inst.operands[0].reg;
8544   Rs = (inst.operands[1].present
8545         ? inst.operands[1].reg    /* Rd, Rs, foo */
8546         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
8547   Rn = inst.operands[2].reg;
8548
8549   if (unified_syntax)
8550     {
8551       if (!inst.operands[2].isreg)
8552         {
8553           /* For an immediate, we always generate a 32-bit opcode;
8554              section relaxation will shrink it later if possible.  */
8555           inst.instruction = THUMB_OP32 (inst.instruction);
8556           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
8557           inst.instruction |= Rd << 8;
8558           inst.instruction |= Rs << 16;
8559           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
8560         }
8561       else
8562         {
8563           bfd_boolean narrow;
8564
8565           /* See if we can do this with a 16-bit instruction.  */
8566           if (THUMB_SETS_FLAGS (inst.instruction))
8567             narrow = current_it_mask == 0;
8568           else
8569             narrow = current_it_mask != 0;
8570
8571           if (Rd > 7 || Rn > 7 || Rs > 7)
8572             narrow = FALSE;
8573           if (inst.operands[2].shifted)
8574             narrow = FALSE;
8575           if (inst.size_req == 4)
8576             narrow = FALSE;
8577
8578           if (narrow
8579               && Rd == Rs)
8580             {
8581               inst.instruction = THUMB_OP16 (inst.instruction);
8582               inst.instruction |= Rd;
8583               inst.instruction |= Rn << 3;
8584               return;
8585             }
8586
8587           /* If we get here, it can't be done in 16 bits.  */
8588           constraint (inst.operands[2].shifted
8589                       && inst.operands[2].immisreg,
8590                       _("shift must be constant"));
8591           inst.instruction = THUMB_OP32 (inst.instruction);
8592           inst.instruction |= Rd << 8;
8593           inst.instruction |= Rs << 16;
8594           encode_thumb32_shifted_operand (2);
8595         }
8596     }
8597   else
8598     {
8599       /* On its face this is a lie - the instruction does set the
8600          flags.  However, the only supported mnemonic in this mode
8601          says it doesn't.  */
8602       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
8603
8604       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
8605                   _("unshifted register required"));
8606       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
8607       constraint (Rd != Rs,
8608                   _("dest and source1 must be the same register"));
8609
8610       inst.instruction = THUMB_OP16 (inst.instruction);
8611       inst.instruction |= Rd;
8612       inst.instruction |= Rn << 3;
8613     }
8614 }
8615
8616 /* Similarly, but for instructions where the arithmetic operation is
8617    commutative, so we can allow either of them to be different from
8618    the destination operand in a 16-bit instruction.  For instance, all
8619    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
8620    accepted.  */
8621 static void
8622 do_t_arit3c (void)
8623 {
8624   int Rd, Rs, Rn;
8625
8626   Rd = inst.operands[0].reg;
8627   Rs = (inst.operands[1].present
8628         ? inst.operands[1].reg    /* Rd, Rs, foo */
8629         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
8630   Rn = inst.operands[2].reg;
8631
8632   if (unified_syntax)
8633     {
8634       if (!inst.operands[2].isreg)
8635         {
8636           /* For an immediate, we always generate a 32-bit opcode;
8637              section relaxation will shrink it later if possible.  */
8638           inst.instruction = THUMB_OP32 (inst.instruction);
8639           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
8640           inst.instruction |= Rd << 8;
8641           inst.instruction |= Rs << 16;
8642           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
8643         }
8644       else
8645         {
8646           bfd_boolean narrow;
8647
8648           /* See if we can do this with a 16-bit instruction.  */
8649           if (THUMB_SETS_FLAGS (inst.instruction))
8650             narrow = current_it_mask == 0;
8651           else
8652             narrow = current_it_mask != 0;
8653
8654           if (Rd > 7 || Rn > 7 || Rs > 7)
8655             narrow = FALSE;
8656           if (inst.operands[2].shifted)
8657             narrow = FALSE;
8658           if (inst.size_req == 4)
8659             narrow = FALSE;
8660
8661           if (narrow)
8662             {
8663               if (Rd == Rs)
8664                 {
8665                   inst.instruction = THUMB_OP16 (inst.instruction);
8666                   inst.instruction |= Rd;
8667                   inst.instruction |= Rn << 3;
8668                   return;
8669                 }
8670               if (Rd == Rn)
8671                 {
8672                   inst.instruction = THUMB_OP16 (inst.instruction);
8673                   inst.instruction |= Rd;
8674                   inst.instruction |= Rs << 3;
8675                   return;
8676                 }
8677             }
8678
8679           /* If we get here, it can't be done in 16 bits.  */
8680           constraint (inst.operands[2].shifted
8681                       && inst.operands[2].immisreg,
8682                       _("shift must be constant"));
8683           inst.instruction = THUMB_OP32 (inst.instruction);
8684           inst.instruction |= Rd << 8;
8685           inst.instruction |= Rs << 16;
8686           encode_thumb32_shifted_operand (2);
8687         }
8688     }
8689   else
8690     {
8691       /* On its face this is a lie - the instruction does set the
8692          flags.  However, the only supported mnemonic in this mode
8693          says it doesn't.  */
8694       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
8695
8696       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
8697                   _("unshifted register required"));
8698       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
8699
8700       inst.instruction = THUMB_OP16 (inst.instruction);
8701       inst.instruction |= Rd;
8702
8703       if (Rd == Rs)
8704         inst.instruction |= Rn << 3;
8705       else if (Rd == Rn)
8706         inst.instruction |= Rs << 3;
8707       else
8708         constraint (1, _("dest must overlap one source register"));
8709     }
8710 }
8711
8712 static void
8713 do_t_barrier (void)
8714 {
8715   if (inst.operands[0].present)
8716     {
8717       constraint ((inst.instruction & 0xf0) != 0x40
8718                   && inst.operands[0].imm != 0xf,
8719                   "bad barrier type");
8720       inst.instruction |= inst.operands[0].imm;
8721     }
8722   else
8723     inst.instruction |= 0xf;
8724 }
8725
8726 static void
8727 do_t_bfc (void)
8728 {
8729   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
8730   constraint (msb > 32, _("bit-field extends past end of register"));
8731   /* The instruction encoding stores the LSB and MSB,
8732      not the LSB and width.  */
8733   inst.instruction |= inst.operands[0].reg << 8;
8734   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
8735   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
8736   inst.instruction |= msb - 1;
8737 }
8738
8739 static void
8740 do_t_bfi (void)
8741 {
8742   unsigned int msb;
8743
8744   /* #0 in second position is alternative syntax for bfc, which is
8745      the same instruction but with REG_PC in the Rm field.  */
8746   if (!inst.operands[1].isreg)
8747     inst.operands[1].reg = REG_PC;
8748
8749   msb = inst.operands[2].imm + inst.operands[3].imm;
8750   constraint (msb > 32, _("bit-field extends past end of register"));
8751   /* The instruction encoding stores the LSB and MSB,
8752      not the LSB and width.  */
8753   inst.instruction |= inst.operands[0].reg << 8;
8754   inst.instruction |= inst.operands[1].reg << 16;
8755   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
8756   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
8757   inst.instruction |= msb - 1;
8758 }
8759
8760 static void
8761 do_t_bfx (void)
8762 {
8763   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
8764               _("bit-field extends past end of register"));
8765   inst.instruction |= inst.operands[0].reg << 8;
8766   inst.instruction |= inst.operands[1].reg << 16;
8767   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
8768   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
8769   inst.instruction |= inst.operands[3].imm - 1;
8770 }
8771
8772 /* ARM V5 Thumb BLX (argument parse)
8773         BLX <target_addr>       which is BLX(1)
8774         BLX <Rm>                which is BLX(2)
8775    Unfortunately, there are two different opcodes for this mnemonic.
8776    So, the insns[].value is not used, and the code here zaps values
8777         into inst.instruction.
8778
8779    ??? How to take advantage of the additional two bits of displacement
8780    available in Thumb32 mode?  Need new relocation?  */
8781
8782 static void
8783 do_t_blx (void)
8784 {
8785   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
8786   if (inst.operands[0].isreg)
8787     /* We have a register, so this is BLX(2).  */
8788     inst.instruction |= inst.operands[0].reg << 3;
8789   else
8790     {
8791       /* No register.  This must be BLX(1).  */
8792       inst.instruction = 0xf000e800;
8793 #ifdef OBJ_ELF
8794       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8795         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
8796       else
8797 #endif
8798         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BLX;
8799       inst.reloc.pc_rel = 1;
8800     }
8801 }
8802
8803 static void
8804 do_t_branch (void)
8805 {
8806   int opcode;
8807   int cond;
8808
8809   if (current_it_mask)
8810     {
8811       /* Conditional branches inside IT blocks are encoded as unconditional
8812          branches.  */
8813       cond = COND_ALWAYS;
8814       /* A branch must be the last instruction in an IT block.  */
8815       constraint (current_it_mask != 0x10, BAD_BRANCH);
8816     }
8817   else
8818     cond = inst.cond;
8819
8820   if (cond != COND_ALWAYS)
8821     opcode = T_MNEM_bcond;
8822   else
8823     opcode = inst.instruction;
8824
8825   if (unified_syntax && inst.size_req == 4)
8826     {
8827       inst.instruction = THUMB_OP32(opcode);
8828       if (cond == COND_ALWAYS)
8829         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH25;
8830       else
8831         {
8832           assert (cond != 0xF);
8833           inst.instruction |= cond << 22;
8834           inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH20;
8835         }
8836     }
8837   else
8838     {
8839       inst.instruction = THUMB_OP16(opcode);
8840       if (cond == COND_ALWAYS)
8841         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH12;
8842       else
8843         {
8844           inst.instruction |= cond << 8;
8845           inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH9;
8846         }
8847       /* Allow section relaxation.  */
8848       if (unified_syntax && inst.size_req != 2)
8849         inst.relax = opcode;
8850     }
8851
8852   inst.reloc.pc_rel = 1;
8853 }
8854
8855 static void
8856 do_t_bkpt (void)
8857 {
8858   constraint (inst.cond != COND_ALWAYS,
8859               _("instruction is always unconditional"));
8860   if (inst.operands[0].present)
8861     {
8862       constraint (inst.operands[0].imm > 255,
8863                   _("immediate value out of range"));
8864       inst.instruction |= inst.operands[0].imm;
8865     }
8866 }
8867
8868 static void
8869 do_t_branch23 (void)
8870 {
8871   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
8872   inst.reloc.type   = BFD_RELOC_THUMB_PCREL_BRANCH23;
8873   inst.reloc.pc_rel = 1;
8874
8875   /* If the destination of the branch is a defined symbol which does not have
8876      the THUMB_FUNC attribute, then we must be calling a function which has
8877      the (interfacearm) attribute.  We look for the Thumb entry point to that
8878      function and change the branch to refer to that function instead.  */
8879   if (   inst.reloc.exp.X_op == O_symbol
8880       && inst.reloc.exp.X_add_symbol != NULL
8881       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
8882       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
8883     inst.reloc.exp.X_add_symbol =
8884       find_real_start (inst.reloc.exp.X_add_symbol);
8885 }
8886
8887 static void
8888 do_t_bx (void)
8889 {
8890   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
8891   inst.instruction |= inst.operands[0].reg << 3;
8892   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
8893      should cause the alignment to be checked once it is known.  This is
8894      because BX PC only works if the instruction is word aligned.  */
8895 }
8896
8897 static void
8898 do_t_bxj (void)
8899 {
8900   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
8901   if (inst.operands[0].reg == REG_PC)
8902     as_tsktsk (_("use of r15 in bxj is not really useful"));
8903
8904   inst.instruction |= inst.operands[0].reg << 16;
8905 }
8906
8907 static void
8908 do_t_clz (void)
8909 {
8910   inst.instruction |= inst.operands[0].reg << 8;
8911   inst.instruction |= inst.operands[1].reg << 16;
8912   inst.instruction |= inst.operands[1].reg;
8913 }
8914
8915 static void
8916 do_t_cps (void)
8917 {
8918   constraint (current_it_mask, BAD_NOT_IT);
8919   inst.instruction |= inst.operands[0].imm;
8920 }
8921
8922 static void
8923 do_t_cpsi (void)
8924 {
8925   constraint (current_it_mask, BAD_NOT_IT);
8926   if (unified_syntax
8927       && (inst.operands[1].present || inst.size_req == 4)
8928       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
8929     {
8930       unsigned int imod = (inst.instruction & 0x0030) >> 4;
8931       inst.instruction = 0xf3af8000;
8932       inst.instruction |= imod << 9;
8933       inst.instruction |= inst.operands[0].imm << 5;
8934       if (inst.operands[1].present)
8935         inst.instruction |= 0x100 | inst.operands[1].imm;
8936     }
8937   else
8938     {
8939       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
8940                   && (inst.operands[0].imm & 4),
8941                   _("selected processor does not support 'A' form "
8942                     "of this instruction"));
8943       constraint (inst.operands[1].present || inst.size_req == 4,
8944                   _("Thumb does not support the 2-argument "
8945                     "form of this instruction"));
8946       inst.instruction |= inst.operands[0].imm;
8947     }
8948 }
8949
8950 /* THUMB CPY instruction (argument parse).  */
8951
8952 static void
8953 do_t_cpy (void)
8954 {
8955   if (inst.size_req == 4)
8956     {
8957       inst.instruction = THUMB_OP32 (T_MNEM_mov);
8958       inst.instruction |= inst.operands[0].reg << 8;
8959       inst.instruction |= inst.operands[1].reg;
8960     }
8961   else
8962     {
8963       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
8964       inst.instruction |= (inst.operands[0].reg & 0x7);
8965       inst.instruction |= inst.operands[1].reg << 3;
8966     }
8967 }
8968
8969 static void
8970 do_t_czb (void)
8971 {
8972   constraint (current_it_mask, BAD_NOT_IT);
8973   constraint (inst.operands[0].reg > 7, BAD_HIREG);
8974   inst.instruction |= inst.operands[0].reg;
8975   inst.reloc.pc_rel = 1;
8976   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
8977 }
8978
8979 static void
8980 do_t_dbg (void)
8981 {
8982   inst.instruction |= inst.operands[0].imm;
8983 }
8984
8985 static void
8986 do_t_div (void)
8987 {
8988   if (!inst.operands[1].present)
8989     inst.operands[1].reg = inst.operands[0].reg;
8990   inst.instruction |= inst.operands[0].reg << 8;
8991   inst.instruction |= inst.operands[1].reg << 16;
8992   inst.instruction |= inst.operands[2].reg;
8993 }
8994
8995 static void
8996 do_t_hint (void)
8997 {
8998   if (unified_syntax && inst.size_req == 4)
8999     inst.instruction = THUMB_OP32 (inst.instruction);
9000   else
9001     inst.instruction = THUMB_OP16 (inst.instruction);
9002 }
9003
9004 static void
9005 do_t_it (void)
9006 {
9007   unsigned int cond = inst.operands[0].imm;
9008
9009   constraint (current_it_mask, BAD_NOT_IT);
9010   current_it_mask = (inst.instruction & 0xf) | 0x10;
9011   current_cc = cond;
9012
9013   /* If the condition is a negative condition, invert the mask.  */
9014   if ((cond & 0x1) == 0x0)
9015     {
9016       unsigned int mask = inst.instruction & 0x000f;
9017
9018       if ((mask & 0x7) == 0)
9019         /* no conversion needed */;
9020       else if ((mask & 0x3) == 0)
9021         mask ^= 0x8;
9022       else if ((mask & 0x1) == 0)
9023         mask ^= 0xC;
9024       else
9025         mask ^= 0xE;
9026
9027       inst.instruction &= 0xfff0;
9028       inst.instruction |= mask;
9029     }
9030
9031   inst.instruction |= cond << 4;
9032 }
9033
9034 static void
9035 do_t_ldmstm (void)
9036 {
9037   /* This really doesn't seem worth it.  */
9038   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
9039               _("expression too complex"));
9040   constraint (inst.operands[1].writeback,
9041               _("Thumb load/store multiple does not support {reglist}^"));
9042
9043   if (unified_syntax)
9044     {
9045       /* See if we can use a 16-bit instruction.  */
9046       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
9047           && inst.size_req != 4
9048           && inst.operands[0].reg <= 7
9049           && !(inst.operands[1].imm & ~0xff)
9050           && (inst.instruction == T_MNEM_stmia
9051               ? inst.operands[0].writeback
9052               : (inst.operands[0].writeback
9053                  == !(inst.operands[1].imm & (1 << inst.operands[0].reg)))))
9054         {
9055           if (inst.instruction == T_MNEM_stmia
9056               && (inst.operands[1].imm & (1 << inst.operands[0].reg))
9057               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
9058             as_warn (_("value stored for r%d is UNPREDICTABLE"),
9059                      inst.operands[0].reg);
9060
9061           inst.instruction = THUMB_OP16 (inst.instruction);
9062           inst.instruction |= inst.operands[0].reg << 8;
9063           inst.instruction |= inst.operands[1].imm;
9064         }
9065       else
9066         {
9067           if (inst.operands[1].imm & (1 << 13))
9068             as_warn (_("SP should not be in register list"));
9069           if (inst.instruction == T_MNEM_stmia)
9070             {
9071               if (inst.operands[1].imm & (1 << 15))
9072                 as_warn (_("PC should not be in register list"));
9073               if (inst.operands[1].imm & (1 << inst.operands[0].reg))
9074                 as_warn (_("value stored for r%d is UNPREDICTABLE"),
9075                          inst.operands[0].reg);
9076             }
9077           else
9078             {
9079               if (inst.operands[1].imm & (1 << 14)
9080                   && inst.operands[1].imm & (1 << 15))
9081                 as_warn (_("LR and PC should not both be in register list"));
9082               if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
9083                   && inst.operands[0].writeback)
9084                 as_warn (_("base register should not be in register list "
9085                            "when written back"));
9086             }
9087           if (inst.instruction < 0xffff)
9088             inst.instruction = THUMB_OP32 (inst.instruction);
9089           inst.instruction |= inst.operands[0].reg << 16;
9090           inst.instruction |= inst.operands[1].imm;
9091           if (inst.operands[0].writeback)
9092             inst.instruction |= WRITE_BACK;
9093         }
9094     }
9095   else
9096     {
9097       constraint (inst.operands[0].reg > 7
9098                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
9099       if (inst.instruction == T_MNEM_stmia)
9100         {
9101           if (!inst.operands[0].writeback)
9102             as_warn (_("this instruction will write back the base register"));
9103           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
9104               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
9105             as_warn (_("value stored for r%d is UNPREDICTABLE"),
9106                      inst.operands[0].reg);
9107         }
9108       else
9109         {
9110           if (!inst.operands[0].writeback
9111               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
9112             as_warn (_("this instruction will write back the base register"));
9113           else if (inst.operands[0].writeback
9114                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
9115             as_warn (_("this instruction will not write back the base register"));
9116         }
9117
9118       inst.instruction = THUMB_OP16 (inst.instruction);
9119       inst.instruction |= inst.operands[0].reg << 8;
9120       inst.instruction |= inst.operands[1].imm;
9121     }
9122 }
9123
9124 static void
9125 do_t_ldrex (void)
9126 {
9127   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
9128               || inst.operands[1].postind || inst.operands[1].writeback
9129               || inst.operands[1].immisreg || inst.operands[1].shifted
9130               || inst.operands[1].negative,
9131               BAD_ADDR_MODE);
9132
9133   inst.instruction |= inst.operands[0].reg << 12;
9134   inst.instruction |= inst.operands[1].reg << 16;
9135   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
9136 }
9137
9138 static void
9139 do_t_ldrexd (void)
9140 {
9141   if (!inst.operands[1].present)
9142     {
9143       constraint (inst.operands[0].reg == REG_LR,
9144                   _("r14 not allowed as first register "
9145                     "when second register is omitted"));
9146       inst.operands[1].reg = inst.operands[0].reg + 1;
9147     }
9148   constraint (inst.operands[0].reg == inst.operands[1].reg,
9149               BAD_OVERLAP);
9150
9151   inst.instruction |= inst.operands[0].reg << 12;
9152   inst.instruction |= inst.operands[1].reg << 8;
9153   inst.instruction |= inst.operands[2].reg << 16;
9154 }
9155
9156 static void
9157 do_t_ldst (void)
9158 {
9159   unsigned long opcode;
9160   int Rn;
9161
9162   opcode = inst.instruction;
9163   if (unified_syntax)
9164     {
9165       if (!inst.operands[1].isreg)
9166         {
9167           if (opcode <= 0xffff)
9168             inst.instruction = THUMB_OP32 (opcode);
9169           if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
9170             return;
9171         }
9172       if (inst.operands[1].isreg
9173           && !inst.operands[1].writeback
9174           && !inst.operands[1].shifted && !inst.operands[1].postind
9175           && !inst.operands[1].negative && inst.operands[0].reg <= 7
9176           && opcode <= 0xffff
9177           && inst.size_req != 4)
9178         {
9179           /* Insn may have a 16-bit form.  */
9180           Rn = inst.operands[1].reg;
9181           if (inst.operands[1].immisreg)
9182             {
9183               inst.instruction = THUMB_OP16 (opcode);
9184               /* [Rn, Ri] */
9185               if (Rn <= 7 && inst.operands[1].imm <= 7)
9186                 goto op16;
9187             }
9188           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
9189                     && opcode != T_MNEM_ldrsb)
9190                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
9191                    || (Rn == REG_SP && opcode == T_MNEM_str))
9192             {
9193               /* [Rn, #const] */
9194               if (Rn > 7)
9195                 {
9196                   if (Rn == REG_PC)
9197                     {
9198                       if (inst.reloc.pc_rel)
9199                         opcode = T_MNEM_ldr_pc2;
9200                       else
9201                         opcode = T_MNEM_ldr_pc;
9202                     }
9203                   else
9204                     {
9205                       if (opcode == T_MNEM_ldr)
9206                         opcode = T_MNEM_ldr_sp;
9207                       else
9208                         opcode = T_MNEM_str_sp;
9209                     }
9210                   inst.instruction = inst.operands[0].reg << 8;
9211                 }
9212               else
9213                 {
9214                   inst.instruction = inst.operands[0].reg;
9215                   inst.instruction |= inst.operands[1].reg << 3;
9216                 }
9217               inst.instruction |= THUMB_OP16 (opcode);
9218               if (inst.size_req == 2)
9219                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
9220               else
9221                 inst.relax = opcode;
9222               return;
9223             }
9224         }
9225       /* Definitely a 32-bit variant.  */
9226       inst.instruction = THUMB_OP32 (opcode);
9227       inst.instruction |= inst.operands[0].reg << 12;
9228       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
9229       return;
9230     }
9231
9232   constraint (inst.operands[0].reg > 7, BAD_HIREG);
9233
9234   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
9235     {
9236       /* Only [Rn,Rm] is acceptable.  */
9237       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
9238       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
9239                   || inst.operands[1].postind || inst.operands[1].shifted
9240                   || inst.operands[1].negative,
9241                   _("Thumb does not support this addressing mode"));
9242       inst.instruction = THUMB_OP16 (inst.instruction);
9243       goto op16;
9244     }
9245      
9246   inst.instruction = THUMB_OP16 (inst.instruction);
9247   if (!inst.operands[1].isreg)
9248     if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
9249       return;
9250
9251   constraint (!inst.operands[1].preind
9252               || inst.operands[1].shifted
9253               || inst.operands[1].writeback,
9254               _("Thumb does not support this addressing mode"));
9255   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
9256     {
9257       constraint (inst.instruction & 0x0600,
9258                   _("byte or halfword not valid for base register"));
9259       constraint (inst.operands[1].reg == REG_PC
9260                   && !(inst.instruction & THUMB_LOAD_BIT),
9261                   _("r15 based store not allowed"));
9262       constraint (inst.operands[1].immisreg,
9263                   _("invalid base register for register offset"));
9264
9265       if (inst.operands[1].reg == REG_PC)
9266         inst.instruction = T_OPCODE_LDR_PC;
9267       else if (inst.instruction & THUMB_LOAD_BIT)
9268         inst.instruction = T_OPCODE_LDR_SP;
9269       else
9270         inst.instruction = T_OPCODE_STR_SP;
9271
9272       inst.instruction |= inst.operands[0].reg << 8;
9273       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
9274       return;
9275     }
9276
9277   constraint (inst.operands[1].reg > 7, BAD_HIREG);
9278   if (!inst.operands[1].immisreg)
9279     {
9280       /* Immediate offset.  */
9281       inst.instruction |= inst.operands[0].reg;
9282       inst.instruction |= inst.operands[1].reg << 3;
9283       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
9284       return;
9285     }
9286
9287   /* Register offset.  */
9288   constraint (inst.operands[1].imm > 7, BAD_HIREG);
9289   constraint (inst.operands[1].negative,
9290               _("Thumb does not support this addressing mode"));
9291
9292  op16:
9293   switch (inst.instruction)
9294     {
9295     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
9296     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
9297     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
9298     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
9299     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
9300     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
9301     case 0x5600 /* ldrsb */:
9302     case 0x5e00 /* ldrsh */: break;
9303     default: abort ();
9304     }
9305
9306   inst.instruction |= inst.operands[0].reg;
9307   inst.instruction |= inst.operands[1].reg << 3;
9308   inst.instruction |= inst.operands[1].imm << 6;
9309 }
9310
9311 static void
9312 do_t_ldstd (void)
9313 {
9314   if (!inst.operands[1].present)
9315     {
9316       inst.operands[1].reg = inst.operands[0].reg + 1;
9317       constraint (inst.operands[0].reg == REG_LR,
9318                   _("r14 not allowed here"));
9319     }
9320   inst.instruction |= inst.operands[0].reg << 12;
9321   inst.instruction |= inst.operands[1].reg << 8;
9322   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
9323                             
9324 }
9325
9326 static void
9327 do_t_ldstt (void)
9328 {
9329   inst.instruction |= inst.operands[0].reg << 12;
9330   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
9331 }
9332
9333 static void
9334 do_t_mla (void)
9335 {
9336   inst.instruction |= inst.operands[0].reg << 8;
9337   inst.instruction |= inst.operands[1].reg << 16;
9338   inst.instruction |= inst.operands[2].reg;
9339   inst.instruction |= inst.operands[3].reg << 12;
9340 }
9341
9342 static void
9343 do_t_mlal (void)
9344 {
9345   inst.instruction |= inst.operands[0].reg << 12;
9346   inst.instruction |= inst.operands[1].reg << 8;
9347   inst.instruction |= inst.operands[2].reg << 16;
9348   inst.instruction |= inst.operands[3].reg;
9349 }
9350
9351 static void
9352 do_t_mov_cmp (void)
9353 {
9354   if (unified_syntax)
9355     {
9356       int r0off = (inst.instruction == T_MNEM_mov
9357                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
9358       unsigned long opcode;
9359       bfd_boolean narrow;
9360       bfd_boolean low_regs;
9361
9362       low_regs = (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7);
9363       opcode = inst.instruction;
9364       if (current_it_mask)
9365         narrow = opcode != T_MNEM_movs;
9366       else
9367         narrow = opcode != T_MNEM_movs || low_regs;
9368       if (inst.size_req == 4
9369           || inst.operands[1].shifted)
9370         narrow = FALSE;
9371
9372       if (!inst.operands[1].isreg)
9373         {
9374           /* Immediate operand.  */
9375           if (current_it_mask == 0 && opcode == T_MNEM_mov)
9376             narrow = 0;
9377           if (low_regs && narrow)
9378             {
9379               inst.instruction = THUMB_OP16 (opcode);
9380               inst.instruction |= inst.operands[0].reg << 8;
9381               if (inst.size_req == 2)
9382                 inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
9383               else
9384                 inst.relax = opcode;
9385             }
9386           else
9387             {
9388               inst.instruction = THUMB_OP32 (inst.instruction);
9389               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9390               inst.instruction |= inst.operands[0].reg << r0off;
9391               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9392             }
9393         }
9394       else if (!narrow)
9395         {
9396           inst.instruction = THUMB_OP32 (inst.instruction);
9397           inst.instruction |= inst.operands[0].reg << r0off;
9398           encode_thumb32_shifted_operand (1);
9399         }
9400       else
9401         switch (inst.instruction)
9402           {
9403           case T_MNEM_mov:
9404             inst.instruction = T_OPCODE_MOV_HR;
9405             inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
9406             inst.instruction |= (inst.operands[0].reg & 0x7);
9407             inst.instruction |= inst.operands[1].reg << 3;
9408             break;
9409
9410           case T_MNEM_movs:
9411             /* We know we have low registers at this point.
9412                Generate ADD Rd, Rs, #0.  */
9413             inst.instruction = T_OPCODE_ADD_I3;
9414             inst.instruction |= inst.operands[0].reg;
9415             inst.instruction |= inst.operands[1].reg << 3;
9416             break;
9417
9418           case T_MNEM_cmp:
9419             if (low_regs)
9420               {
9421                 inst.instruction = T_OPCODE_CMP_LR;
9422                 inst.instruction |= inst.operands[0].reg;
9423                 inst.instruction |= inst.operands[1].reg << 3;
9424               }
9425             else
9426               {
9427                 inst.instruction = T_OPCODE_CMP_HR;
9428                 inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
9429                 inst.instruction |= (inst.operands[0].reg & 0x7);
9430                 inst.instruction |= inst.operands[1].reg << 3;
9431               }
9432             break;
9433           }
9434       return;
9435     }
9436
9437   inst.instruction = THUMB_OP16 (inst.instruction);
9438   if (inst.operands[1].isreg)
9439     {
9440       if (inst.operands[0].reg < 8 && inst.operands[1].reg < 8)
9441         {
9442           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
9443              since a MOV instruction produces unpredictable results.  */
9444           if (inst.instruction == T_OPCODE_MOV_I8)
9445             inst.instruction = T_OPCODE_ADD_I3;
9446           else
9447             inst.instruction = T_OPCODE_CMP_LR;
9448
9449           inst.instruction |= inst.operands[0].reg;
9450           inst.instruction |= inst.operands[1].reg << 3;
9451         }
9452       else
9453         {
9454           if (inst.instruction == T_OPCODE_MOV_I8)
9455             inst.instruction = T_OPCODE_MOV_HR;
9456           else
9457             inst.instruction = T_OPCODE_CMP_HR;
9458           do_t_cpy ();
9459         }
9460     }
9461   else
9462     {
9463       constraint (inst.operands[0].reg > 7,
9464                   _("only lo regs allowed with immediate"));
9465       inst.instruction |= inst.operands[0].reg << 8;
9466       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
9467     }
9468 }
9469
9470 static void
9471 do_t_mov16 (void)
9472 {
9473   bfd_vma imm;
9474   bfd_boolean top;
9475
9476   top = (inst.instruction & 0x00800000) != 0;
9477   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
9478     {
9479       constraint (top, _(":lower16: not allowed this instruction"));
9480       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
9481     }
9482   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
9483     {
9484       constraint (!top, _(":upper16: not allowed this instruction"));
9485       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
9486     }
9487
9488   inst.instruction |= inst.operands[0].reg << 8;
9489   if (inst.reloc.type == BFD_RELOC_UNUSED)
9490     {
9491       imm = inst.reloc.exp.X_add_number;
9492       inst.instruction |= (imm & 0xf000) << 4;
9493       inst.instruction |= (imm & 0x0800) << 15;
9494       inst.instruction |= (imm & 0x0700) << 4;
9495       inst.instruction |= (imm & 0x00ff);
9496     }
9497 }
9498
9499 static void
9500 do_t_mvn_tst (void)
9501 {
9502   if (unified_syntax)
9503     {
9504       int r0off = (inst.instruction == T_MNEM_mvn
9505                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
9506       bfd_boolean narrow;
9507
9508       if (inst.size_req == 4
9509           || inst.instruction > 0xffff
9510           || inst.operands[1].shifted
9511           || inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
9512         narrow = FALSE;
9513       else if (inst.instruction == T_MNEM_cmn)
9514         narrow = TRUE;
9515       else if (THUMB_SETS_FLAGS (inst.instruction))
9516         narrow = (current_it_mask == 0);
9517       else
9518         narrow = (current_it_mask != 0);
9519
9520       if (!inst.operands[1].isreg)
9521         {
9522           /* For an immediate, we always generate a 32-bit opcode;
9523              section relaxation will shrink it later if possible.  */
9524           if (inst.instruction < 0xffff)
9525             inst.instruction = THUMB_OP32 (inst.instruction);
9526           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9527           inst.instruction |= inst.operands[0].reg << r0off;
9528           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9529         }
9530       else
9531         {
9532           /* See if we can do this with a 16-bit instruction.  */
9533           if (narrow)
9534             {
9535               inst.instruction = THUMB_OP16 (inst.instruction);
9536               inst.instruction |= inst.operands[0].reg;
9537               inst.instruction |= inst.operands[1].reg << 3;
9538             }
9539           else
9540             {
9541               constraint (inst.operands[1].shifted
9542                           && inst.operands[1].immisreg,
9543                           _("shift must be constant"));
9544               if (inst.instruction < 0xffff)
9545                 inst.instruction = THUMB_OP32 (inst.instruction);
9546               inst.instruction |= inst.operands[0].reg << r0off;
9547               encode_thumb32_shifted_operand (1);
9548             }
9549         }
9550     }
9551   else
9552     {
9553       constraint (inst.instruction > 0xffff
9554                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
9555       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
9556                   _("unshifted register required"));
9557       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
9558                   BAD_HIREG);
9559
9560       inst.instruction = THUMB_OP16 (inst.instruction);
9561       inst.instruction |= inst.operands[0].reg;
9562       inst.instruction |= inst.operands[1].reg << 3;
9563     }
9564 }
9565
9566 static void
9567 do_t_mrs (void)
9568 {
9569   int flags;
9570
9571   if (do_vfp_nsyn_mrs () == SUCCESS)
9572     return;
9573
9574   flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
9575   if (flags == 0)
9576     {
9577       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7m),
9578                   _("selected processor does not support "
9579                     "requested special purpose register"));
9580     }
9581   else
9582     {
9583       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1),
9584                   _("selected processor does not support "
9585                     "requested special purpose register %x"));
9586       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
9587       constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
9588                   _("'CPSR' or 'SPSR' expected"));
9589     }
9590     
9591   inst.instruction |= inst.operands[0].reg << 8;
9592   inst.instruction |= (flags & SPSR_BIT) >> 2;
9593   inst.instruction |= inst.operands[1].imm & 0xff;
9594 }
9595
9596 static void
9597 do_t_msr (void)
9598 {
9599   int flags;
9600
9601   if (do_vfp_nsyn_msr () == SUCCESS)
9602     return;
9603
9604   constraint (!inst.operands[1].isreg,
9605               _("Thumb encoding does not support an immediate here"));
9606   flags = inst.operands[0].imm;
9607   if (flags & ~0xff)
9608     {
9609       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1),
9610                   _("selected processor does not support "
9611                     "requested special purpose register"));
9612     }
9613   else
9614     {
9615       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7m),
9616                   _("selected processor does not support "
9617                     "requested special purpose register"));
9618       flags |= PSR_f;
9619     }
9620   inst.instruction |= (flags & SPSR_BIT) >> 2;
9621   inst.instruction |= (flags & ~SPSR_BIT) >> 8;
9622   inst.instruction |= (flags & 0xff);
9623   inst.instruction |= inst.operands[1].reg << 16;
9624 }
9625
9626 static void
9627 do_t_mul (void)
9628 {
9629   if (!inst.operands[2].present)
9630     inst.operands[2].reg = inst.operands[0].reg;
9631
9632   /* There is no 32-bit MULS and no 16-bit MUL. */
9633   if (unified_syntax && inst.instruction == T_MNEM_mul)
9634     {
9635       inst.instruction = THUMB_OP32 (inst.instruction);
9636       inst.instruction |= inst.operands[0].reg << 8;
9637       inst.instruction |= inst.operands[1].reg << 16;
9638       inst.instruction |= inst.operands[2].reg << 0;
9639     }
9640   else
9641     {
9642       constraint (!unified_syntax
9643                   && inst.instruction == T_MNEM_muls, BAD_THUMB32);
9644       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
9645                   BAD_HIREG);
9646
9647       inst.instruction = THUMB_OP16 (inst.instruction);
9648       inst.instruction |= inst.operands[0].reg;
9649
9650       if (inst.operands[0].reg == inst.operands[1].reg)
9651         inst.instruction |= inst.operands[2].reg << 3;
9652       else if (inst.operands[0].reg == inst.operands[2].reg)
9653         inst.instruction |= inst.operands[1].reg << 3;
9654       else
9655         constraint (1, _("dest must overlap one source register"));
9656     }
9657 }
9658
9659 static void
9660 do_t_mull (void)
9661 {
9662   inst.instruction |= inst.operands[0].reg << 12;
9663   inst.instruction |= inst.operands[1].reg << 8;
9664   inst.instruction |= inst.operands[2].reg << 16;
9665   inst.instruction |= inst.operands[3].reg;
9666
9667   if (inst.operands[0].reg == inst.operands[1].reg)
9668     as_tsktsk (_("rdhi and rdlo must be different"));
9669 }
9670
9671 static void
9672 do_t_nop (void)
9673 {
9674   if (unified_syntax)
9675     {
9676       if (inst.size_req == 4 || inst.operands[0].imm > 15)
9677         {
9678           inst.instruction = THUMB_OP32 (inst.instruction);
9679           inst.instruction |= inst.operands[0].imm;
9680         }
9681       else
9682         {
9683           inst.instruction = THUMB_OP16 (inst.instruction);
9684           inst.instruction |= inst.operands[0].imm << 4;
9685         }
9686     }
9687   else
9688     {
9689       constraint (inst.operands[0].present,
9690                   _("Thumb does not support NOP with hints"));
9691       inst.instruction = 0x46c0;
9692     }
9693 }
9694
9695 static void
9696 do_t_neg (void)
9697 {
9698   if (unified_syntax)
9699     {
9700       bfd_boolean narrow;
9701
9702       if (THUMB_SETS_FLAGS (inst.instruction))
9703         narrow = (current_it_mask == 0);
9704       else
9705         narrow = (current_it_mask != 0);
9706       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
9707         narrow = FALSE;
9708       if (inst.size_req == 4)
9709         narrow = FALSE;
9710
9711       if (!narrow)
9712         {
9713           inst.instruction = THUMB_OP32 (inst.instruction);
9714           inst.instruction |= inst.operands[0].reg << 8;
9715           inst.instruction |= inst.operands[1].reg << 16;
9716         }
9717       else
9718         {
9719           inst.instruction = THUMB_OP16 (inst.instruction);
9720           inst.instruction |= inst.operands[0].reg;
9721           inst.instruction |= inst.operands[1].reg << 3;
9722         }
9723     }
9724   else
9725     {
9726       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
9727                   BAD_HIREG);
9728       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
9729
9730       inst.instruction = THUMB_OP16 (inst.instruction);
9731       inst.instruction |= inst.operands[0].reg;
9732       inst.instruction |= inst.operands[1].reg << 3;
9733     }
9734 }
9735
9736 static void
9737 do_t_pkhbt (void)
9738 {
9739   inst.instruction |= inst.operands[0].reg << 8;
9740   inst.instruction |= inst.operands[1].reg << 16;
9741   inst.instruction |= inst.operands[2].reg;
9742   if (inst.operands[3].present)
9743     {
9744       unsigned int val = inst.reloc.exp.X_add_number;
9745       constraint (inst.reloc.exp.X_op != O_constant,
9746                   _("expression too complex"));
9747       inst.instruction |= (val & 0x1c) << 10;
9748       inst.instruction |= (val & 0x03) << 6;
9749     }
9750 }
9751
9752 static void
9753 do_t_pkhtb (void)
9754 {
9755   if (!inst.operands[3].present)
9756     inst.instruction &= ~0x00000020;
9757   do_t_pkhbt ();
9758 }
9759
9760 static void
9761 do_t_pld (void)
9762 {
9763   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
9764 }
9765
9766 static void
9767 do_t_push_pop (void)
9768 {
9769   unsigned mask;
9770   
9771   constraint (inst.operands[0].writeback,
9772               _("push/pop do not support {reglist}^"));
9773   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
9774               _("expression too complex"));
9775
9776   mask = inst.operands[0].imm;
9777   if ((mask & ~0xff) == 0)
9778     inst.instruction = THUMB_OP16 (inst.instruction);
9779   else if ((inst.instruction == T_MNEM_push
9780             && (mask & ~0xff) == 1 << REG_LR)
9781            || (inst.instruction == T_MNEM_pop
9782                && (mask & ~0xff) == 1 << REG_PC))
9783     {
9784       inst.instruction = THUMB_OP16 (inst.instruction);
9785       inst.instruction |= THUMB_PP_PC_LR;
9786       mask &= 0xff;
9787     }
9788   else if (unified_syntax)
9789     {
9790       if (mask & (1 << 13))
9791         inst.error =  _("SP not allowed in register list");
9792       if (inst.instruction == T_MNEM_push)
9793         {
9794           if (mask & (1 << 15))
9795             inst.error = _("PC not allowed in register list");
9796         }
9797       else
9798         {
9799           if (mask & (1 << 14)
9800               && mask & (1 << 15))
9801             inst.error = _("LR and PC should not both be in register list");
9802         }
9803       if ((mask & (mask - 1)) == 0)
9804         {
9805           /* Single register push/pop implemented as str/ldr.  */
9806           if (inst.instruction == T_MNEM_push)
9807             inst.instruction = 0xf84d0d04; /* str reg, [sp, #-4]! */
9808           else
9809             inst.instruction = 0xf85d0b04; /* ldr reg, [sp], #4 */
9810           mask = ffs(mask) - 1;
9811           mask <<= 12;
9812         }
9813       else
9814         inst.instruction = THUMB_OP32 (inst.instruction);
9815     }
9816   else
9817     {
9818       inst.error = _("invalid register list to push/pop instruction");
9819       return;
9820     }
9821
9822   inst.instruction |= mask;
9823 }
9824
9825 static void
9826 do_t_rbit (void)
9827 {
9828   inst.instruction |= inst.operands[0].reg << 8;
9829   inst.instruction |= inst.operands[1].reg << 16;
9830 }
9831
9832 static void
9833 do_t_rev (void)
9834 {
9835   if (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
9836       && inst.size_req != 4)
9837     {
9838       inst.instruction = THUMB_OP16 (inst.instruction);
9839       inst.instruction |= inst.operands[0].reg;
9840       inst.instruction |= inst.operands[1].reg << 3;
9841     }
9842   else if (unified_syntax)
9843     {
9844       inst.instruction = THUMB_OP32 (inst.instruction);
9845       inst.instruction |= inst.operands[0].reg << 8;
9846       inst.instruction |= inst.operands[1].reg << 16;
9847       inst.instruction |= inst.operands[1].reg;
9848     }
9849   else
9850     inst.error = BAD_HIREG;
9851 }
9852
9853 static void
9854 do_t_rsb (void)
9855 {
9856   int Rd, Rs;
9857
9858   Rd = inst.operands[0].reg;
9859   Rs = (inst.operands[1].present
9860         ? inst.operands[1].reg    /* Rd, Rs, foo */
9861         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
9862
9863   inst.instruction |= Rd << 8;
9864   inst.instruction |= Rs << 16;
9865   if (!inst.operands[2].isreg)
9866     {
9867       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
9868       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
9869     }
9870   else
9871     encode_thumb32_shifted_operand (2);
9872 }
9873
9874 static void
9875 do_t_setend (void)
9876 {
9877   constraint (current_it_mask, BAD_NOT_IT);
9878   if (inst.operands[0].imm)
9879     inst.instruction |= 0x8;
9880 }
9881
9882 static void
9883 do_t_shift (void)
9884 {
9885   if (!inst.operands[1].present)
9886     inst.operands[1].reg = inst.operands[0].reg;
9887
9888   if (unified_syntax)
9889     {
9890       bfd_boolean narrow;
9891       int shift_kind;
9892
9893       switch (inst.instruction)
9894         {
9895         case T_MNEM_asr:
9896         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
9897         case T_MNEM_lsl:
9898         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
9899         case T_MNEM_lsr:
9900         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
9901         case T_MNEM_ror:
9902         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
9903         default: abort ();
9904         }
9905
9906       if (THUMB_SETS_FLAGS (inst.instruction))
9907         narrow = (current_it_mask == 0);
9908       else
9909         narrow = (current_it_mask != 0);
9910       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
9911         narrow = FALSE;
9912       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
9913         narrow = FALSE;
9914       if (inst.operands[2].isreg
9915           && (inst.operands[1].reg != inst.operands[0].reg
9916               || inst.operands[2].reg > 7))
9917         narrow = FALSE;
9918       if (inst.size_req == 4)
9919         narrow = FALSE;
9920
9921       if (!narrow)
9922         {
9923           if (inst.operands[2].isreg)
9924             {
9925               inst.instruction = THUMB_OP32 (inst.instruction);
9926               inst.instruction |= inst.operands[0].reg << 8;
9927               inst.instruction |= inst.operands[1].reg << 16;
9928               inst.instruction |= inst.operands[2].reg;
9929             }
9930           else
9931             {
9932               inst.operands[1].shifted = 1;
9933               inst.operands[1].shift_kind = shift_kind;
9934               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
9935                                              ? T_MNEM_movs : T_MNEM_mov);
9936               inst.instruction |= inst.operands[0].reg << 8;
9937               encode_thumb32_shifted_operand (1);
9938               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
9939               inst.reloc.type = BFD_RELOC_UNUSED;
9940             }
9941         }
9942       else
9943         {
9944           if (inst.operands[2].isreg)
9945             {
9946               switch (shift_kind)
9947                 {
9948                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
9949                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
9950                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
9951                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
9952                 default: abort ();
9953                 }
9954           
9955               inst.instruction |= inst.operands[0].reg;
9956               inst.instruction |= inst.operands[2].reg << 3;
9957             }
9958           else
9959             {
9960               switch (shift_kind)
9961                 {
9962                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
9963                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
9964                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
9965                 default: abort ();
9966                 }
9967               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
9968               inst.instruction |= inst.operands[0].reg;
9969               inst.instruction |= inst.operands[1].reg << 3;
9970             }
9971         }
9972     }
9973   else
9974     {
9975       constraint (inst.operands[0].reg > 7
9976                   || inst.operands[1].reg > 7, BAD_HIREG);
9977       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
9978
9979       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
9980         {
9981           constraint (inst.operands[2].reg > 7, BAD_HIREG);
9982           constraint (inst.operands[0].reg != inst.operands[1].reg,
9983                       _("source1 and dest must be same register"));
9984
9985           switch (inst.instruction)
9986             {
9987             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
9988             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
9989             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
9990             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
9991             default: abort ();
9992             }
9993           
9994           inst.instruction |= inst.operands[0].reg;
9995           inst.instruction |= inst.operands[2].reg << 3;
9996         }
9997       else
9998         {
9999           switch (inst.instruction)
10000             {
10001             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
10002             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
10003             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
10004             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
10005             default: abort ();
10006             }
10007           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
10008           inst.instruction |= inst.operands[0].reg;
10009           inst.instruction |= inst.operands[1].reg << 3;
10010         }
10011     }
10012 }
10013
10014 static void
10015 do_t_simd (void)
10016 {
10017   inst.instruction |= inst.operands[0].reg << 8;
10018   inst.instruction |= inst.operands[1].reg << 16;
10019   inst.instruction |= inst.operands[2].reg;
10020 }
10021
10022 static void
10023 do_t_smc (void)
10024 {
10025   unsigned int value = inst.reloc.exp.X_add_number;
10026   constraint (inst.reloc.exp.X_op != O_constant,
10027               _("expression too complex"));
10028   inst.reloc.type = BFD_RELOC_UNUSED;
10029   inst.instruction |= (value & 0xf000) >> 12;
10030   inst.instruction |= (value & 0x0ff0);
10031   inst.instruction |= (value & 0x000f) << 16;
10032 }
10033
10034 static void
10035 do_t_ssat (void)
10036 {
10037   inst.instruction |= inst.operands[0].reg << 8;
10038   inst.instruction |= inst.operands[1].imm - 1;
10039   inst.instruction |= inst.operands[2].reg << 16;
10040
10041   if (inst.operands[3].present)
10042     {
10043       constraint (inst.reloc.exp.X_op != O_constant,
10044                   _("expression too complex"));
10045
10046       if (inst.reloc.exp.X_add_number != 0)
10047         {
10048           if (inst.operands[3].shift_kind == SHIFT_ASR)
10049             inst.instruction |= 0x00200000;  /* sh bit */
10050           inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
10051           inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
10052         }
10053       inst.reloc.type = BFD_RELOC_UNUSED;
10054     }
10055 }
10056
10057 static void
10058 do_t_ssat16 (void)
10059 {
10060   inst.instruction |= inst.operands[0].reg << 8;
10061   inst.instruction |= inst.operands[1].imm - 1;
10062   inst.instruction |= inst.operands[2].reg << 16;
10063 }
10064
10065 static void
10066 do_t_strex (void)
10067 {
10068   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
10069               || inst.operands[2].postind || inst.operands[2].writeback
10070               || inst.operands[2].immisreg || inst.operands[2].shifted
10071               || inst.operands[2].negative,
10072               BAD_ADDR_MODE);
10073
10074   inst.instruction |= inst.operands[0].reg << 8;
10075   inst.instruction |= inst.operands[1].reg << 12;
10076   inst.instruction |= inst.operands[2].reg << 16;
10077   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
10078 }
10079
10080 static void
10081 do_t_strexd (void)
10082 {
10083   if (!inst.operands[2].present)
10084     inst.operands[2].reg = inst.operands[1].reg + 1;
10085
10086   constraint (inst.operands[0].reg == inst.operands[1].reg
10087               || inst.operands[0].reg == inst.operands[2].reg
10088               || inst.operands[0].reg == inst.operands[3].reg
10089               || inst.operands[1].reg == inst.operands[2].reg,
10090               BAD_OVERLAP);
10091
10092   inst.instruction |= inst.operands[0].reg;
10093   inst.instruction |= inst.operands[1].reg << 12;
10094   inst.instruction |= inst.operands[2].reg << 8;
10095   inst.instruction |= inst.operands[3].reg << 16;
10096 }
10097
10098 static void
10099 do_t_sxtah (void)
10100 {
10101   inst.instruction |= inst.operands[0].reg << 8;
10102   inst.instruction |= inst.operands[1].reg << 16;
10103   inst.instruction |= inst.operands[2].reg;
10104   inst.instruction |= inst.operands[3].imm << 4;
10105 }
10106
10107 static void
10108 do_t_sxth (void)
10109 {
10110   if (inst.instruction <= 0xffff && inst.size_req != 4
10111       && inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
10112       && (!inst.operands[2].present || inst.operands[2].imm == 0))
10113     {
10114       inst.instruction = THUMB_OP16 (inst.instruction);
10115       inst.instruction |= inst.operands[0].reg;
10116       inst.instruction |= inst.operands[1].reg << 3;
10117     }
10118   else if (unified_syntax)
10119     {
10120       if (inst.instruction <= 0xffff)
10121         inst.instruction = THUMB_OP32 (inst.instruction);
10122       inst.instruction |= inst.operands[0].reg << 8;
10123       inst.instruction |= inst.operands[1].reg;
10124       inst.instruction |= inst.operands[2].imm << 4;
10125     }
10126   else
10127     {
10128       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
10129                   _("Thumb encoding does not support rotation"));
10130       constraint (1, BAD_HIREG);
10131     }
10132 }
10133
10134 static void
10135 do_t_swi (void)
10136 {
10137   inst.reloc.type = BFD_RELOC_ARM_SWI;
10138 }
10139
10140 static void
10141 do_t_tb (void)
10142 {
10143   int half;
10144
10145   half = (inst.instruction & 0x10) != 0;
10146   constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
10147   constraint (inst.operands[0].immisreg,
10148               _("instruction requires register index"));
10149   constraint (inst.operands[0].imm == 15,
10150               _("PC is not a valid index register"));
10151   constraint (!half && inst.operands[0].shifted,
10152               _("instruction does not allow shifted index"));
10153   inst.instruction |= (inst.operands[0].reg << 16) | inst.operands[0].imm;
10154 }
10155
10156 static void
10157 do_t_usat (void)
10158 {
10159   inst.instruction |= inst.operands[0].reg << 8;
10160   inst.instruction |= inst.operands[1].imm;
10161   inst.instruction |= inst.operands[2].reg << 16;
10162
10163   if (inst.operands[3].present)
10164     {
10165       constraint (inst.reloc.exp.X_op != O_constant,
10166                   _("expression too complex"));
10167       if (inst.reloc.exp.X_add_number != 0)
10168         {
10169           if (inst.operands[3].shift_kind == SHIFT_ASR)
10170             inst.instruction |= 0x00200000;  /* sh bit */
10171
10172           inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
10173           inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
10174         }
10175       inst.reloc.type = BFD_RELOC_UNUSED;
10176     }
10177 }
10178
10179 static void
10180 do_t_usat16 (void)
10181 {
10182   inst.instruction |= inst.operands[0].reg << 8;
10183   inst.instruction |= inst.operands[1].imm;
10184   inst.instruction |= inst.operands[2].reg << 16;
10185 }
10186
10187 /* Neon instruction encoder helpers.  */
10188   
10189 /* Encodings for the different types for various Neon opcodes.  */
10190
10191 /* An "invalid" code for the following tables.  */
10192 #define N_INV -1u
10193
10194 struct neon_tab_entry
10195 {
10196   unsigned integer;
10197   unsigned float_or_poly;
10198   unsigned scalar_or_imm;
10199 };
10200   
10201 /* Map overloaded Neon opcodes to their respective encodings.  */
10202 #define NEON_ENC_TAB                                    \
10203   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
10204   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
10205   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
10206   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
10207   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
10208   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
10209   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
10210   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
10211   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
10212   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
10213   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
10214   /* Register variants of the following two instructions are encoded as
10215      vcge / vcgt with the operands reversed. */         \
10216   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
10217   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
10218   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
10219   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
10220   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
10221   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
10222   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
10223   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
10224   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
10225   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
10226   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
10227   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
10228   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
10229   X(vshl,       0x0000400, N_INV,     0x0800510),       \
10230   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
10231   X(vand,       0x0000110, N_INV,     0x0800030),       \
10232   X(vbic,       0x0100110, N_INV,     0x0800030),       \
10233   X(veor,       0x1000110, N_INV,     N_INV),           \
10234   X(vorn,       0x0300110, N_INV,     0x0800010),       \
10235   X(vorr,       0x0200110, N_INV,     0x0800010),       \
10236   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
10237   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
10238   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
10239   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
10240   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
10241   X(vst1,       0x0000000, 0x0800000, N_INV),           \
10242   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
10243   X(vst2,       0x0000100, 0x0800100, N_INV),           \
10244   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
10245   X(vst3,       0x0000200, 0x0800200, N_INV),           \
10246   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
10247   X(vst4,       0x0000300, 0x0800300, N_INV),           \
10248   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
10249   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
10250   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
10251   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
10252   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
10253   X(vnmla,      0xe000a40, 0xe000b40, N_INV),           \
10254   X(vnmls,      0xe100a40, 0xe100b40, N_INV),           \
10255   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
10256   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
10257   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
10258   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV)
10259
10260 enum neon_opc
10261 {
10262 #define X(OPC,I,F,S) N_MNEM_##OPC
10263 NEON_ENC_TAB
10264 #undef X
10265 };
10266
10267 static const struct neon_tab_entry neon_enc_tab[] =
10268 {
10269 #define X(OPC,I,F,S) { (I), (F), (S) }
10270 NEON_ENC_TAB
10271 #undef X
10272 };
10273
10274 #define NEON_ENC_INTEGER(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
10275 #define NEON_ENC_ARMREG(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
10276 #define NEON_ENC_POLY(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
10277 #define NEON_ENC_FLOAT(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
10278 #define NEON_ENC_SCALAR(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
10279 #define NEON_ENC_IMMED(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
10280 #define NEON_ENC_INTERLV(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
10281 #define NEON_ENC_LANE(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
10282 #define NEON_ENC_DUP(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
10283 #define NEON_ENC_SINGLE(X) \
10284   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
10285 #define NEON_ENC_DOUBLE(X) \
10286   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
10287
10288 /* Define shapes for instruction operands. The following mnemonic characters
10289    are used in this table:
10290
10291      F - VFP S<n> register
10292      D - Neon D<n> register
10293      Q - Neon Q<n> register
10294      I - Immediate
10295      S - Scalar
10296      R - ARM register
10297      L - D<n> register list
10298    
10299    This table is used to generate various data:
10300      - enumerations of the form NS_DDR to be used as arguments to
10301        neon_select_shape.
10302      - a table classifying shapes into single, double, quad, mixed.
10303      - a table used to drive neon_select_shape.
10304 */
10305
10306 #define NEON_SHAPE_DEF                  \
10307   X(3, (D, D, D), DOUBLE),              \
10308   X(3, (Q, Q, Q), QUAD),                \
10309   X(3, (D, D, I), DOUBLE),              \
10310   X(3, (Q, Q, I), QUAD),                \
10311   X(3, (D, D, S), DOUBLE),              \
10312   X(3, (Q, Q, S), QUAD),                \
10313   X(2, (D, D), DOUBLE),                 \
10314   X(2, (Q, Q), QUAD),                   \
10315   X(2, (D, S), DOUBLE),                 \
10316   X(2, (Q, S), QUAD),                   \
10317   X(2, (D, R), DOUBLE),                 \
10318   X(2, (Q, R), QUAD),                   \
10319   X(2, (D, I), DOUBLE),                 \
10320   X(2, (Q, I), QUAD),                   \
10321   X(3, (D, L, D), DOUBLE),              \
10322   X(2, (D, Q), MIXED),                  \
10323   X(2, (Q, D), MIXED),                  \
10324   X(3, (D, Q, I), MIXED),               \
10325   X(3, (Q, D, I), MIXED),               \
10326   X(3, (Q, D, D), MIXED),               \
10327   X(3, (D, Q, Q), MIXED),               \
10328   X(3, (Q, Q, D), MIXED),               \
10329   X(3, (Q, D, S), MIXED),               \
10330   X(3, (D, Q, S), MIXED),               \
10331   X(4, (D, D, D, I), DOUBLE),           \
10332   X(4, (Q, Q, Q, I), QUAD),             \
10333   X(2, (F, F), SINGLE),                 \
10334   X(3, (F, F, F), SINGLE),              \
10335   X(2, (F, I), SINGLE),                 \
10336   X(2, (F, D), MIXED),                  \
10337   X(2, (D, F), MIXED),                  \
10338   X(3, (F, F, I), MIXED),               \
10339   X(4, (R, R, F, F), SINGLE),           \
10340   X(4, (F, F, R, R), SINGLE),           \
10341   X(3, (D, R, R), DOUBLE),              \
10342   X(3, (R, R, D), DOUBLE),              \
10343   X(2, (S, R), SINGLE),                 \
10344   X(2, (R, S), SINGLE),                 \
10345   X(2, (F, R), SINGLE),                 \
10346   X(2, (R, F), SINGLE)
10347
10348 #define S2(A,B)         NS_##A##B
10349 #define S3(A,B,C)       NS_##A##B##C
10350 #define S4(A,B,C,D)     NS_##A##B##C##D
10351
10352 #define X(N, L, C) S##N L
10353
10354 enum neon_shape
10355 {
10356   NEON_SHAPE_DEF,
10357   NS_NULL
10358 };
10359
10360 #undef X
10361 #undef S2
10362 #undef S3
10363 #undef S4
10364
10365 enum neon_shape_class
10366 {
10367   SC_SINGLE,
10368   SC_DOUBLE,
10369   SC_QUAD,
10370   SC_MIXED
10371 };
10372
10373 #define X(N, L, C) SC_##C
10374
10375 static enum neon_shape_class neon_shape_class[] =
10376 {
10377   NEON_SHAPE_DEF
10378 };
10379
10380 #undef X
10381
10382 enum neon_shape_el
10383 {
10384   SE_F,
10385   SE_D,
10386   SE_Q,
10387   SE_I,
10388   SE_S,
10389   SE_R,
10390   SE_L
10391 };
10392
10393 /* Register widths of above.  */
10394 static unsigned neon_shape_el_size[] =
10395 {
10396   32,
10397   64,
10398   128,
10399   0,
10400   32,
10401   32,
10402   0
10403 };
10404
10405 struct neon_shape_info
10406 {
10407   unsigned els;
10408   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
10409 };
10410
10411 #define S2(A,B)         { SE_##A, SE_##B }
10412 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
10413 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
10414
10415 #define X(N, L, C) { N, S##N L }
10416
10417 static struct neon_shape_info neon_shape_tab[] =
10418 {
10419   NEON_SHAPE_DEF
10420 };
10421
10422 #undef X
10423 #undef S2
10424 #undef S3
10425 #undef S4
10426
10427 /* Bit masks used in type checking given instructions.
10428   'N_EQK' means the type must be the same as (or based on in some way) the key
10429    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
10430    set, various other bits can be set as well in order to modify the meaning of
10431    the type constraint.  */
10432
10433 enum neon_type_mask
10434 {
10435   N_S8   = 0x000001,
10436   N_S16  = 0x000002,
10437   N_S32  = 0x000004,
10438   N_S64  = 0x000008,
10439   N_U8   = 0x000010,
10440   N_U16  = 0x000020,
10441   N_U32  = 0x000040,
10442   N_U64  = 0x000080,
10443   N_I8   = 0x000100,
10444   N_I16  = 0x000200,
10445   N_I32  = 0x000400,
10446   N_I64  = 0x000800,
10447   N_8    = 0x001000,
10448   N_16   = 0x002000,
10449   N_32   = 0x004000,
10450   N_64   = 0x008000,
10451   N_P8   = 0x010000,
10452   N_P16  = 0x020000,
10453   N_F32  = 0x040000,
10454   N_F64  = 0x080000,
10455   N_KEY  = 0x100000, /* key element (main type specifier).  */
10456   N_EQK  = 0x200000, /* given operand has the same type & size as the key.  */
10457   N_VFP  = 0x400000, /* VFP mode: operand size must match register width.  */
10458   N_DBL  = 0x000001, /* if N_EQK, this operand is twice the size.  */
10459   N_HLF  = 0x000002, /* if N_EQK, this operand is half the size.  */
10460   N_SGN  = 0x000004, /* if N_EQK, this operand is forced to be signed.  */
10461   N_UNS  = 0x000008, /* if N_EQK, this operand is forced to be unsigned.  */
10462   N_INT  = 0x000010, /* if N_EQK, this operand is forced to be integer.  */
10463   N_FLT  = 0x000020, /* if N_EQK, this operand is forced to be float.  */
10464   N_SIZ  = 0x000040, /* if N_EQK, this operand is forced to be size-only.  */
10465   N_UTYP = 0,
10466   N_MAX_NONSPECIAL = N_F64
10467 };
10468
10469 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
10470
10471 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
10472 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
10473 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
10474 #define N_SUF_32   (N_SU_32 | N_F32)
10475 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
10476 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F32)
10477
10478 /* Pass this as the first type argument to neon_check_type to ignore types
10479    altogether.  */
10480 #define N_IGNORE_TYPE (N_KEY | N_EQK)
10481
10482 /* Select a "shape" for the current instruction (describing register types or
10483    sizes) from a list of alternatives. Return NS_NULL if the current instruction
10484    doesn't fit. For non-polymorphic shapes, checking is usually done as a
10485    function of operand parsing, so this function doesn't need to be called.
10486    Shapes should be listed in order of decreasing length.  */
10487
10488 static enum neon_shape
10489 neon_select_shape (enum neon_shape shape, ...)
10490 {
10491   va_list ap;
10492   enum neon_shape first_shape = shape;
10493
10494   /* Fix missing optional operands. FIXME: we don't know at this point how
10495      many arguments we should have, so this makes the assumption that we have
10496      > 1. This is true of all current Neon opcodes, I think, but may not be
10497      true in the future.  */
10498   if (!inst.operands[1].present)
10499     inst.operands[1] = inst.operands[0];
10500
10501   va_start (ap, shape);
10502   
10503   for (; shape != NS_NULL; shape = va_arg (ap, int))
10504     {
10505       unsigned j;
10506       int matches = 1;
10507
10508       for (j = 0; j < neon_shape_tab[shape].els; j++)
10509         {
10510           if (!inst.operands[j].present)
10511             {
10512               matches = 0;
10513               break;
10514             }
10515
10516           switch (neon_shape_tab[shape].el[j])
10517             {
10518             case SE_F:
10519               if (!(inst.operands[j].isreg
10520                     && inst.operands[j].isvec
10521                     && inst.operands[j].issingle
10522                     && !inst.operands[j].isquad))
10523                 matches = 0;
10524               break;
10525
10526             case SE_D:
10527               if (!(inst.operands[j].isreg
10528                     && inst.operands[j].isvec
10529                     && !inst.operands[j].isquad
10530                     && !inst.operands[j].issingle))
10531                 matches = 0;
10532               break;
10533
10534             case SE_R:
10535               if (!(inst.operands[j].isreg
10536                     && !inst.operands[j].isvec))
10537                 matches = 0;
10538               break;
10539
10540             case SE_Q:
10541               if (!(inst.operands[j].isreg
10542                     && inst.operands[j].isvec
10543                     && inst.operands[j].isquad
10544                     && !inst.operands[j].issingle))
10545                 matches = 0;
10546               break;
10547
10548             case SE_I:
10549               if (!(!inst.operands[j].isreg
10550                     && !inst.operands[j].isscalar))
10551                 matches = 0;
10552               break;
10553
10554             case SE_S:
10555               if (!(!inst.operands[j].isreg
10556                     && inst.operands[j].isscalar))
10557                 matches = 0;
10558               break;
10559
10560             case SE_L:
10561               break;
10562             }
10563         }
10564       if (matches)
10565         break;
10566     }
10567   
10568   va_end (ap);
10569
10570   if (shape == NS_NULL && first_shape != NS_NULL)
10571     first_error (_("invalid instruction shape"));
10572
10573   return shape;
10574 }
10575
10576 /* True if SHAPE is predominantly a quadword operation (most of the time, this
10577    means the Q bit should be set).  */
10578
10579 static int
10580 neon_quad (enum neon_shape shape)
10581 {
10582   return neon_shape_class[shape] == SC_QUAD;
10583 }
10584
10585 static void
10586 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
10587                        unsigned *g_size)
10588 {
10589   /* Allow modification to be made to types which are constrained to be
10590      based on the key element, based on bits set alongside N_EQK.  */
10591   if ((typebits & N_EQK) != 0)
10592     {
10593       if ((typebits & N_HLF) != 0)
10594         *g_size /= 2;
10595       else if ((typebits & N_DBL) != 0)
10596         *g_size *= 2;
10597       if ((typebits & N_SGN) != 0)
10598         *g_type = NT_signed;
10599       else if ((typebits & N_UNS) != 0)
10600         *g_type = NT_unsigned;
10601       else if ((typebits & N_INT) != 0)
10602         *g_type = NT_integer;
10603       else if ((typebits & N_FLT) != 0)
10604         *g_type = NT_float;
10605       else if ((typebits & N_SIZ) != 0)
10606         *g_type = NT_untyped;
10607     }
10608 }
10609   
10610 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
10611    operand type, i.e. the single type specified in a Neon instruction when it
10612    is the only one given.  */
10613
10614 static struct neon_type_el
10615 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
10616 {
10617   struct neon_type_el dest = *key;
10618   
10619   assert ((thisarg & N_EQK) != 0);
10620   
10621   neon_modify_type_size (thisarg, &dest.type, &dest.size);
10622
10623   return dest;
10624 }
10625
10626 /* Convert Neon type and size into compact bitmask representation.  */
10627
10628 static enum neon_type_mask
10629 type_chk_of_el_type (enum neon_el_type type, unsigned size)
10630 {
10631   switch (type)
10632     {
10633     case NT_untyped:
10634       switch (size)
10635         {
10636         case 8:  return N_8;
10637         case 16: return N_16;
10638         case 32: return N_32;
10639         case 64: return N_64;
10640         default: ;
10641         }
10642       break;
10643
10644     case NT_integer:
10645       switch (size)
10646         {
10647         case 8:  return N_I8;
10648         case 16: return N_I16;
10649         case 32: return N_I32;
10650         case 64: return N_I64;
10651         default: ;
10652         }
10653       break;
10654
10655     case NT_float:
10656       switch (size)
10657         {
10658         case 32: return N_F32;
10659         case 64: return N_F64;
10660         default: ;
10661         }
10662       break;
10663
10664     case NT_poly:
10665       switch (size)
10666         {
10667         case 8:  return N_P8;
10668         case 16: return N_P16;
10669         default: ;
10670         }
10671       break;
10672
10673     case NT_signed:
10674       switch (size)
10675         {
10676         case 8:  return N_S8;
10677         case 16: return N_S16;
10678         case 32: return N_S32;
10679         case 64: return N_S64;
10680         default: ;
10681         }
10682       break;
10683
10684     case NT_unsigned:
10685       switch (size)
10686         {
10687         case 8:  return N_U8;
10688         case 16: return N_U16;
10689         case 32: return N_U32;
10690         case 64: return N_U64;
10691         default: ;
10692         }
10693       break;
10694
10695     default: ;
10696     }
10697   
10698   return N_UTYP;
10699 }
10700
10701 /* Convert compact Neon bitmask type representation to a type and size. Only
10702    handles the case where a single bit is set in the mask.  */
10703
10704 static int
10705 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
10706                      enum neon_type_mask mask)
10707 {
10708   if ((mask & N_EQK) != 0)
10709     return FAIL;
10710
10711   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
10712     *size = 8;
10713   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_P16)) != 0)
10714     *size = 16;
10715   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
10716     *size = 32;
10717   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64)) != 0)
10718     *size = 64;
10719   else
10720     return FAIL;
10721
10722   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
10723     *type = NT_signed;
10724   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
10725     *type = NT_unsigned;
10726   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
10727     *type = NT_integer;
10728   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
10729     *type = NT_untyped;
10730   else if ((mask & (N_P8 | N_P16)) != 0)
10731     *type = NT_poly;
10732   else if ((mask & (N_F32 | N_F64)) != 0)
10733     *type = NT_float;
10734   else
10735     return FAIL;
10736   
10737   return SUCCESS;
10738 }
10739
10740 /* Modify a bitmask of allowed types. This is only needed for type
10741    relaxation.  */
10742
10743 static unsigned
10744 modify_types_allowed (unsigned allowed, unsigned mods)
10745 {
10746   unsigned size;
10747   enum neon_el_type type;
10748   unsigned destmask;
10749   int i;
10750   
10751   destmask = 0;
10752   
10753   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
10754     {
10755       if (el_type_of_type_chk (&type, &size, allowed & i) == SUCCESS)
10756         {
10757           neon_modify_type_size (mods, &type, &size);
10758           destmask |= type_chk_of_el_type (type, size);
10759         }
10760     }
10761   
10762   return destmask;
10763 }
10764
10765 /* Check type and return type classification.
10766    The manual states (paraphrase): If one datatype is given, it indicates the
10767    type given in:
10768     - the second operand, if there is one
10769     - the operand, if there is no second operand
10770     - the result, if there are no operands.
10771    This isn't quite good enough though, so we use a concept of a "key" datatype
10772    which is set on a per-instruction basis, which is the one which matters when
10773    only one data type is written.
10774    Note: this function has side-effects (e.g. filling in missing operands). All
10775    Neon instructions should call it before performing bit encoding.  */
10776
10777 static struct neon_type_el
10778 neon_check_type (unsigned els, enum neon_shape ns, ...)
10779 {
10780   va_list ap;
10781   unsigned i, pass, key_el = 0;
10782   unsigned types[NEON_MAX_TYPE_ELS];
10783   enum neon_el_type k_type = NT_invtype;
10784   unsigned k_size = -1u;
10785   struct neon_type_el badtype = {NT_invtype, -1};
10786   unsigned key_allowed = 0;
10787
10788   /* Optional registers in Neon instructions are always (not) in operand 1.
10789      Fill in the missing operand here, if it was omitted.  */
10790   if (els > 1 && !inst.operands[1].present)
10791     inst.operands[1] = inst.operands[0];
10792
10793   /* Suck up all the varargs.  */
10794   va_start (ap, ns);
10795   for (i = 0; i < els; i++)
10796     {
10797       unsigned thisarg = va_arg (ap, unsigned);
10798       if (thisarg == N_IGNORE_TYPE)
10799         {
10800           va_end (ap);
10801           return badtype;
10802         }
10803       types[i] = thisarg;
10804       if ((thisarg & N_KEY) != 0)
10805         key_el = i;
10806     }
10807   va_end (ap);
10808
10809   if (inst.vectype.elems > 0)
10810     for (i = 0; i < els; i++)
10811       if (inst.operands[i].vectype.type != NT_invtype)
10812         {
10813           first_error (_("types specified in both the mnemonic and operands"));
10814           return badtype;
10815         }
10816
10817   /* Duplicate inst.vectype elements here as necessary.
10818      FIXME: No idea if this is exactly the same as the ARM assembler,
10819      particularly when an insn takes one register and one non-register
10820      operand. */
10821   if (inst.vectype.elems == 1 && els > 1)
10822     {
10823       unsigned j;
10824       inst.vectype.elems = els;
10825       inst.vectype.el[key_el] = inst.vectype.el[0];
10826       for (j = 0; j < els; j++)
10827         if (j != key_el)
10828           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
10829                                                   types[j]);
10830     }
10831   else if (inst.vectype.elems == 0 && els > 0)
10832     {
10833       unsigned j;
10834       /* No types were given after the mnemonic, so look for types specified
10835          after each operand. We allow some flexibility here; as long as the
10836          "key" operand has a type, we can infer the others.  */
10837       for (j = 0; j < els; j++)
10838         if (inst.operands[j].vectype.type != NT_invtype)
10839           inst.vectype.el[j] = inst.operands[j].vectype;
10840
10841       if (inst.operands[key_el].vectype.type != NT_invtype)
10842         {
10843           for (j = 0; j < els; j++)
10844             if (inst.operands[j].vectype.type == NT_invtype)
10845               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
10846                                                       types[j]);
10847         }
10848       else
10849         {
10850           first_error (_("operand types can't be inferred"));
10851           return badtype;
10852         }
10853     }
10854   else if (inst.vectype.elems != els)
10855     {
10856       first_error (_("type specifier has the wrong number of parts"));
10857       return badtype;
10858     }
10859
10860   for (pass = 0; pass < 2; pass++)
10861     {
10862       for (i = 0; i < els; i++)
10863         {
10864           unsigned thisarg = types[i];
10865           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
10866             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
10867           enum neon_el_type g_type = inst.vectype.el[i].type;
10868           unsigned g_size = inst.vectype.el[i].size;
10869
10870           /* Decay more-specific signed & unsigned types to sign-insensitive
10871              integer types if sign-specific variants are unavailable.  */
10872           if ((g_type == NT_signed || g_type == NT_unsigned)
10873               && (types_allowed & N_SU_ALL) == 0)
10874             g_type = NT_integer;
10875
10876           /* If only untyped args are allowed, decay any more specific types to
10877              them. Some instructions only care about signs for some element
10878              sizes, so handle that properly.  */
10879           if ((g_size == 8 && (types_allowed & N_8) != 0)
10880               || (g_size == 16 && (types_allowed & N_16) != 0)
10881               || (g_size == 32 && (types_allowed & N_32) != 0)
10882               || (g_size == 64 && (types_allowed & N_64) != 0))
10883             g_type = NT_untyped;
10884
10885           if (pass == 0)
10886             {
10887               if ((thisarg & N_KEY) != 0)
10888                 {
10889                   k_type = g_type;
10890                   k_size = g_size;
10891                   key_allowed = thisarg & ~N_KEY;
10892                 }
10893             }
10894           else
10895             {
10896               if ((thisarg & N_VFP) != 0)
10897                 {
10898                   enum neon_shape_el regshape = neon_shape_tab[ns].el[i];
10899                   unsigned regwidth = neon_shape_el_size[regshape], match;
10900
10901                   /* In VFP mode, operands must match register widths. If we
10902                      have a key operand, use its width, else use the width of
10903                      the current operand.  */
10904                   if (k_size != -1u)
10905                     match = k_size;
10906                   else
10907                     match = g_size;
10908
10909                   if (regwidth != match)
10910                     {
10911                       first_error (_("operand size must match register width"));
10912                       return badtype;
10913                     }
10914                 }
10915             
10916               if ((thisarg & N_EQK) == 0)
10917                 {
10918                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
10919
10920                   if ((given_type & types_allowed) == 0)
10921                     {
10922                       first_error (_("bad type in Neon instruction"));
10923                       return badtype;
10924                     }
10925                 }
10926               else
10927                 {
10928                   enum neon_el_type mod_k_type = k_type;
10929                   unsigned mod_k_size = k_size;
10930                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
10931                   if (g_type != mod_k_type || g_size != mod_k_size)
10932                     {
10933                       first_error (_("inconsistent types in Neon instruction"));
10934                       return badtype;
10935                     }
10936                 }
10937             }
10938         }
10939     }
10940
10941   return inst.vectype.el[key_el];
10942 }
10943
10944 /* Neon-style VFP instruction forwarding.  */
10945
10946 /* Thumb VFP instructions have 0xE in the condition field.  */
10947
10948 static void
10949 do_vfp_cond_or_thumb (void)
10950 {
10951   if (thumb_mode)
10952     inst.instruction |= 0xe0000000;
10953   else
10954     inst.instruction |= inst.cond << 28;
10955 }
10956
10957 /* Look up and encode a simple mnemonic, for use as a helper function for the
10958    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
10959    etc.  It is assumed that operand parsing has already been done, and that the
10960    operands are in the form expected by the given opcode (this isn't necessarily
10961    the same as the form in which they were parsed, hence some massaging must
10962    take place before this function is called).
10963    Checks current arch version against that in the looked-up opcode.  */
10964
10965 static void
10966 do_vfp_nsyn_opcode (const char *opname)
10967 {
10968   const struct asm_opcode *opcode;
10969   
10970   opcode = hash_find (arm_ops_hsh, opname);
10971
10972   if (!opcode)
10973     abort ();
10974
10975   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
10976                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
10977               _(BAD_FPU));
10978
10979   if (thumb_mode)
10980     {
10981       inst.instruction = opcode->tvalue;
10982       opcode->tencode ();
10983     }
10984   else
10985     {
10986       inst.instruction = (inst.cond << 28) | opcode->avalue;
10987       opcode->aencode ();
10988     }
10989 }
10990
10991 static void
10992 do_vfp_nsyn_add_sub (enum neon_shape rs)
10993 {
10994   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
10995
10996   if (rs == NS_FFF)
10997     {
10998       if (is_add)
10999         do_vfp_nsyn_opcode ("fadds");
11000       else
11001         do_vfp_nsyn_opcode ("fsubs");
11002     }
11003   else
11004     {
11005       if (is_add)
11006         do_vfp_nsyn_opcode ("faddd");
11007       else
11008         do_vfp_nsyn_opcode ("fsubd");
11009     }
11010 }
11011
11012 /* Check operand types to see if this is a VFP instruction, and if so call
11013    PFN ().  */
11014
11015 static int
11016 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
11017 {
11018   enum neon_shape rs;
11019   struct neon_type_el et;
11020
11021   switch (args)
11022     {
11023     case 2:
11024       rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
11025       et = neon_check_type (2, rs,
11026         N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
11027       break;
11028     
11029     case 3:
11030       rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
11031       et = neon_check_type (3, rs,
11032         N_EQK | N_VFP, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
11033       break;
11034
11035     default:
11036       abort ();
11037     }
11038
11039   if (et.type != NT_invtype)
11040     {
11041       pfn (rs);
11042       return SUCCESS;
11043     }
11044   else
11045     inst.error = NULL;
11046
11047   return FAIL;
11048 }
11049
11050 static void
11051 do_vfp_nsyn_mla_mls (enum neon_shape rs)
11052 {
11053   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
11054   
11055   if (rs == NS_FFF)
11056     {
11057       if (is_mla)
11058         do_vfp_nsyn_opcode ("fmacs");
11059       else
11060         do_vfp_nsyn_opcode ("fmscs");
11061     }
11062   else
11063     {
11064       if (is_mla)
11065         do_vfp_nsyn_opcode ("fmacd");
11066       else
11067         do_vfp_nsyn_opcode ("fmscd");
11068     }
11069 }
11070
11071 static void
11072 do_vfp_nsyn_mul (enum neon_shape rs)
11073 {
11074   if (rs == NS_FFF)
11075     do_vfp_nsyn_opcode ("fmuls");
11076   else
11077     do_vfp_nsyn_opcode ("fmuld");
11078 }
11079
11080 static void
11081 do_vfp_nsyn_abs_neg (enum neon_shape rs)
11082 {
11083   int is_neg = (inst.instruction & 0x80) != 0;
11084   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_VFP | N_KEY);
11085
11086   if (rs == NS_FF)
11087     {
11088       if (is_neg)
11089         do_vfp_nsyn_opcode ("fnegs");
11090       else
11091         do_vfp_nsyn_opcode ("fabss");
11092     }
11093   else
11094     {
11095       if (is_neg)
11096         do_vfp_nsyn_opcode ("fnegd");
11097       else
11098         do_vfp_nsyn_opcode ("fabsd");
11099     }
11100 }
11101
11102 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
11103    insns belong to Neon, and are handled elsewhere.  */
11104
11105 static void
11106 do_vfp_nsyn_ldm_stm (int is_dbmode)
11107 {
11108   int is_ldm = (inst.instruction & (1 << 20)) != 0;
11109   if (is_ldm)
11110     {
11111       if (is_dbmode)
11112         do_vfp_nsyn_opcode ("fldmdbs");
11113       else
11114         do_vfp_nsyn_opcode ("fldmias");
11115     }
11116   else
11117     {
11118       if (is_dbmode)
11119         do_vfp_nsyn_opcode ("fstmdbs");
11120       else
11121         do_vfp_nsyn_opcode ("fstmias");
11122     }
11123 }
11124
11125 static void
11126 do_vfp_nsyn_sqrt (void)
11127 {
11128   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
11129   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
11130       
11131   if (rs == NS_FF)
11132     do_vfp_nsyn_opcode ("fsqrts");
11133   else
11134     do_vfp_nsyn_opcode ("fsqrtd");
11135 }
11136
11137 static void
11138 do_vfp_nsyn_div (void)
11139 {
11140   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
11141   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
11142     N_F32 | N_F64 | N_KEY | N_VFP);
11143   
11144   if (rs == NS_FFF)
11145     do_vfp_nsyn_opcode ("fdivs");
11146   else
11147     do_vfp_nsyn_opcode ("fdivd");
11148 }
11149
11150 static void
11151 do_vfp_nsyn_nmul (void)
11152 {
11153   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
11154   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
11155     N_F32 | N_F64 | N_KEY | N_VFP);
11156   
11157   if (rs == NS_FFF)
11158     {
11159       inst.instruction = NEON_ENC_SINGLE (inst.instruction);
11160       do_vfp_sp_dyadic ();
11161     }
11162   else
11163     {
11164       inst.instruction = NEON_ENC_DOUBLE (inst.instruction);
11165       do_vfp_dp_rd_rn_rm ();
11166     }
11167   do_vfp_cond_or_thumb ();
11168 }
11169
11170 static void
11171 do_vfp_nsyn_cmp (void)
11172 {
11173   if (inst.operands[1].isreg)
11174     {
11175       enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
11176       neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
11177       
11178       if (rs == NS_FF)
11179         {
11180           inst.instruction = NEON_ENC_SINGLE (inst.instruction);
11181           do_vfp_sp_monadic ();
11182         }
11183       else
11184         {
11185           inst.instruction = NEON_ENC_DOUBLE (inst.instruction);
11186           do_vfp_dp_rd_rm ();
11187         }
11188     }
11189   else
11190     {
11191       enum neon_shape rs = neon_select_shape (NS_FI, NS_DI, NS_NULL);
11192       neon_check_type (2, rs, N_F32 | N_F64 | N_KEY | N_VFP, N_EQK);
11193
11194       switch (inst.instruction & 0x0fffffff)
11195         {
11196         case N_MNEM_vcmp:
11197           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
11198           break;
11199         case N_MNEM_vcmpe:
11200           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
11201           break;
11202         default:
11203           abort ();
11204         }
11205      
11206       if (rs == NS_FI)
11207         {
11208           inst.instruction = NEON_ENC_SINGLE (inst.instruction);
11209           do_vfp_sp_compare_z ();
11210         }
11211       else
11212         {
11213           inst.instruction = NEON_ENC_DOUBLE (inst.instruction);
11214           do_vfp_dp_rd ();
11215         }
11216     }
11217   do_vfp_cond_or_thumb ();
11218 }
11219
11220 static void
11221 nsyn_insert_sp (void)
11222 {
11223   inst.operands[1] = inst.operands[0];
11224   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
11225   inst.operands[0].reg = 13;
11226   inst.operands[0].isreg = 1;
11227   inst.operands[0].writeback = 1;
11228   inst.operands[0].present = 1;
11229 }
11230
11231 static void
11232 do_vfp_nsyn_push (void)
11233 {
11234   nsyn_insert_sp ();
11235   if (inst.operands[1].issingle)
11236     do_vfp_nsyn_opcode ("fstmdbs");
11237   else
11238     do_vfp_nsyn_opcode ("fstmdbd");
11239 }
11240
11241 static void
11242 do_vfp_nsyn_pop (void)
11243 {
11244   nsyn_insert_sp ();
11245   if (inst.operands[1].issingle)
11246     do_vfp_nsyn_opcode ("fldmdbs");
11247   else
11248     do_vfp_nsyn_opcode ("fldmdbd");
11249 }
11250
11251 /* Fix up Neon data-processing instructions, ORing in the correct bits for
11252    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
11253
11254 static unsigned
11255 neon_dp_fixup (unsigned i)
11256 {
11257   if (thumb_mode)
11258     {
11259       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
11260       if (i & (1 << 24))
11261         i |= 1 << 28;
11262       
11263       i &= ~(1 << 24);
11264       
11265       i |= 0xef000000;
11266     }
11267   else
11268     i |= 0xf2000000;
11269   
11270   return i;
11271 }
11272
11273 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
11274    (0, 1, 2, 3).  */
11275
11276 static unsigned
11277 neon_logbits (unsigned x)
11278 {
11279   return ffs (x) - 4;
11280 }
11281
11282 #define LOW4(R) ((R) & 0xf)
11283 #define HI1(R) (((R) >> 4) & 1)
11284
11285 /* Encode insns with bit pattern:
11286
11287   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
11288   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
11289   
11290   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
11291   different meaning for some instruction.  */
11292
11293 static void
11294 neon_three_same (int isquad, int ubit, int size)
11295 {
11296   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
11297   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
11298   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
11299   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
11300   inst.instruction |= LOW4 (inst.operands[2].reg);
11301   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
11302   inst.instruction |= (isquad != 0) << 6;
11303   inst.instruction |= (ubit != 0) << 24;
11304   if (size != -1)
11305     inst.instruction |= neon_logbits (size) << 20;
11306   
11307   inst.instruction = neon_dp_fixup (inst.instruction);
11308 }
11309
11310 /* Encode instructions of the form:
11311
11312   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
11313   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
11314
11315   Don't write size if SIZE == -1.  */
11316
11317 static void
11318 neon_two_same (int qbit, int ubit, int size)
11319 {
11320   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
11321   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
11322   inst.instruction |= LOW4 (inst.operands[1].reg);
11323   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
11324   inst.instruction |= (qbit != 0) << 6;
11325   inst.instruction |= (ubit != 0) << 24;
11326
11327   if (size != -1)
11328     inst.instruction |= neon_logbits (size) << 18;
11329
11330   inst.instruction = neon_dp_fixup (inst.instruction);
11331 }
11332
11333 /* Neon instruction encoders, in approximate order of appearance.  */
11334
11335 static void
11336 do_neon_dyadic_i_su (void)
11337 {
11338   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
11339   struct neon_type_el et = neon_check_type (3, rs,
11340     N_EQK, N_EQK, N_SU_32 | N_KEY);
11341   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
11342 }
11343
11344 static void
11345 do_neon_dyadic_i64_su (void)
11346 {
11347   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
11348   struct neon_type_el et = neon_check_type (3, rs,
11349     N_EQK, N_EQK, N_SU_ALL | N_KEY);
11350   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
11351 }
11352
11353 static void
11354 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
11355                 unsigned immbits)
11356 {
11357   unsigned size = et.size >> 3;
11358   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
11359   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
11360   inst.instruction |= LOW4 (inst.operands[1].reg);
11361   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
11362   inst.instruction |= (isquad != 0) << 6;
11363   inst.instruction |= immbits << 16;
11364   inst.instruction |= (size >> 3) << 7;
11365   inst.instruction |= (size & 0x7) << 19;
11366   if (write_ubit)
11367     inst.instruction |= (uval != 0) << 24;
11368
11369   inst.instruction = neon_dp_fixup (inst.instruction);
11370 }
11371
11372 static void
11373 do_neon_shl_imm (void)
11374 {
11375   if (!inst.operands[2].isreg)
11376     {
11377       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
11378       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
11379       inst.instruction = NEON_ENC_IMMED (inst.instruction);
11380       neon_imm_shift (FALSE, 0, neon_quad (rs), et, inst.operands[2].imm);
11381     }
11382   else
11383     {
11384       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
11385       struct neon_type_el et = neon_check_type (3, rs,
11386         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
11387       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
11388       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
11389     }
11390 }
11391
11392 static void
11393 do_neon_qshl_imm (void)
11394 {
11395   if (!inst.operands[2].isreg)
11396     {
11397       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
11398       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
11399       inst.instruction = NEON_ENC_IMMED (inst.instruction);
11400       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
11401                       inst.operands[2].imm);
11402     }
11403   else
11404     {
11405       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
11406       struct neon_type_el et = neon_check_type (3, rs,
11407         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
11408       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
11409       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
11410     }
11411 }
11412
11413 static int
11414 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
11415 {
11416   /* Handle .I8 pseudo-instructions.  */
11417   if (size == 8)
11418     {
11419       /* Unfortunately, this will make everything apart from zero out-of-range.
11420          FIXME is this the intended semantics? There doesn't seem much point in
11421          accepting .I8 if so.  */
11422       immediate |= immediate << 8;
11423       size = 16;
11424     }
11425
11426   if (size >= 32)
11427     {
11428       if (immediate == (immediate & 0x000000ff))
11429         {
11430           *immbits = immediate;
11431           return 0x1;
11432         }
11433       else if (immediate == (immediate & 0x0000ff00))
11434         {
11435           *immbits = immediate >> 8;
11436           return 0x3;
11437         }
11438       else if (immediate == (immediate & 0x00ff0000))
11439         {
11440           *immbits = immediate >> 16;
11441           return 0x5;
11442         }
11443       else if (immediate == (immediate & 0xff000000))
11444         {
11445           *immbits = immediate >> 24;
11446           return 0x7;
11447         }
11448       if ((immediate & 0xffff) != (immediate >> 16))
11449         goto bad_immediate;
11450       immediate &= 0xffff;
11451     }
11452
11453   if (immediate == (immediate & 0x000000ff))
11454     {
11455       *immbits = immediate;
11456       return 0x9;
11457     }
11458   else if (immediate == (immediate & 0x0000ff00))
11459     {
11460       *immbits = immediate >> 8;
11461       return 0xb;
11462     }
11463
11464   bad_immediate:
11465   first_error (_("immediate value out of range"));
11466   return FAIL;
11467 }
11468
11469 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
11470    A, B, C, D.  */
11471
11472 static int
11473 neon_bits_same_in_bytes (unsigned imm)
11474 {
11475   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
11476          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
11477          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
11478          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
11479 }
11480
11481 /* For immediate of above form, return 0bABCD.  */
11482
11483 static unsigned
11484 neon_squash_bits (unsigned imm)
11485 {
11486   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
11487          | ((imm & 0x01000000) >> 21);
11488 }
11489
11490 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
11491
11492 static unsigned
11493 neon_qfloat_bits (unsigned imm)
11494 {
11495   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
11496 }
11497
11498 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
11499    the instruction. *OP is passed as the initial value of the op field, and
11500    may be set to a different value depending on the constant (i.e.
11501    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
11502    MVN).  If the immediate looks like a repeated parttern then also
11503    try smaller element sizes.  */
11504
11505 static int
11506 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, unsigned *immbits,
11507                          int *op, int size, enum neon_el_type type)
11508 {
11509   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
11510     {
11511       if (size != 32 || *op == 1)
11512         return FAIL;
11513       *immbits = neon_qfloat_bits (immlo);
11514       return 0xf;
11515     }
11516
11517   if (size == 64)
11518     {
11519       if (neon_bits_same_in_bytes (immhi)
11520           && neon_bits_same_in_bytes (immlo))
11521         {
11522           if (*op == 1)
11523             return FAIL;
11524           *immbits = (neon_squash_bits (immhi) << 4)
11525                      | neon_squash_bits (immlo);
11526           *op = 1;
11527           return 0xe;
11528         }
11529
11530       if (immhi != immlo)
11531         return FAIL;
11532     }
11533
11534   if (size >= 32)
11535     {
11536       if (immlo == (immlo & 0x000000ff))
11537         {
11538           *immbits = immlo;
11539           return 0x0;
11540         }
11541       else if (immlo == (immlo & 0x0000ff00))
11542         {
11543           *immbits = immlo >> 8;
11544           return 0x2;
11545         }
11546       else if (immlo == (immlo & 0x00ff0000))
11547         {
11548           *immbits = immlo >> 16;
11549           return 0x4;
11550         }
11551       else if (immlo == (immlo & 0xff000000))
11552         {
11553           *immbits = immlo >> 24;
11554           return 0x6;
11555         }
11556       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
11557         {
11558           *immbits = (immlo >> 8) & 0xff;
11559           return 0xc;
11560         }
11561       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
11562         {
11563           *immbits = (immlo >> 16) & 0xff;
11564           return 0xd;
11565         }
11566
11567       if ((immlo & 0xffff) != (immlo >> 16))
11568         return FAIL;
11569       immlo &= 0xffff;
11570     }
11571
11572   if (size >= 16)
11573     {
11574       if (immlo == (immlo & 0x000000ff))
11575         {
11576           *immbits = immlo;
11577           return 0x8;
11578         }
11579       else if (immlo == (immlo & 0x0000ff00))
11580         {
11581           *immbits = immlo >> 8;
11582           return 0xa;
11583         }
11584
11585       if ((immlo & 0xff) != (immlo >> 8))
11586         return FAIL;
11587       immlo &= 0xff;
11588     }
11589
11590   if (immlo == (immlo & 0x000000ff))
11591     {
11592       /* Don't allow MVN with 8-bit immediate.  */
11593       if (*op == 1)
11594         return FAIL;
11595       *immbits = immlo;
11596       return 0xe;
11597     }
11598
11599   return FAIL;
11600 }
11601
11602 /* Write immediate bits [7:0] to the following locations:
11603
11604   |28/24|23     19|18 16|15                    4|3     0|
11605   |  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|
11606
11607   This function is used by VMOV/VMVN/VORR/VBIC.  */
11608
11609 static void
11610 neon_write_immbits (unsigned immbits)
11611 {
11612   inst.instruction |= immbits & 0xf;
11613   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
11614   inst.instruction |= ((immbits >> 7) & 0x1) << 24;
11615 }
11616
11617 /* Invert low-order SIZE bits of XHI:XLO.  */
11618
11619 static void
11620 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
11621 {
11622   unsigned immlo = xlo ? *xlo : 0;
11623   unsigned immhi = xhi ? *xhi : 0;
11624
11625   switch (size)
11626     {
11627     case 8:
11628       immlo = (~immlo) & 0xff;
11629       break;
11630
11631     case 16:
11632       immlo = (~immlo) & 0xffff;
11633       break;
11634
11635     case 64:
11636       immhi = (~immhi) & 0xffffffff;
11637       /* fall through.  */
11638
11639     case 32:
11640       immlo = (~immlo) & 0xffffffff;
11641       break;
11642
11643     default:
11644       abort ();
11645     }
11646
11647   if (xlo)
11648     *xlo = immlo;
11649
11650   if (xhi)
11651     *xhi = immhi;
11652 }
11653
11654 static void
11655 do_neon_logic (void)
11656 {
11657   if (inst.operands[2].present && inst.operands[2].isreg)
11658     {
11659       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
11660       neon_check_type (3, rs, N_IGNORE_TYPE);
11661       /* U bit and size field were set as part of the bitmask.  */
11662       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
11663       neon_three_same (neon_quad (rs), 0, -1);
11664     }
11665   else
11666     {
11667       enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
11668       struct neon_type_el et = neon_check_type (2, rs,
11669         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
11670       enum neon_opc opcode = inst.instruction & 0x0fffffff;
11671       unsigned immbits;
11672       int cmode;
11673       
11674       if (et.type == NT_invtype)
11675         return;
11676       
11677       inst.instruction = NEON_ENC_IMMED (inst.instruction);
11678
11679       immbits = inst.operands[1].imm;
11680       if (et.size == 64)
11681         {
11682           /* .i64 is a pseudo-op, so the immediate must be a repeating
11683              pattern.  */
11684           if (immbits != (inst.operands[1].regisimm ?
11685                           inst.operands[1].reg : 0))
11686             {
11687               /* Set immbits to an invalid constant.  */
11688               immbits = 0xdeadbeef;
11689             }
11690         }
11691
11692       switch (opcode)
11693         {
11694         case N_MNEM_vbic:
11695           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
11696           break;
11697         
11698         case N_MNEM_vorr:
11699           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
11700           break;
11701         
11702         case N_MNEM_vand:
11703           /* Pseudo-instruction for VBIC.  */
11704           neon_invert_size (&immbits, 0, et.size);
11705           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
11706           break;
11707         
11708         case N_MNEM_vorn:
11709           /* Pseudo-instruction for VORR.  */
11710           neon_invert_size (&immbits, 0, et.size);
11711           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
11712           break;
11713         
11714         default:
11715           abort ();
11716         }
11717
11718       if (cmode == FAIL)
11719         return;
11720
11721       inst.instruction |= neon_quad (rs) << 6;
11722       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
11723       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
11724       inst.instruction |= cmode << 8;
11725       neon_write_immbits (immbits);
11726       
11727       inst.instruction = neon_dp_fixup (inst.instruction);
11728     }
11729 }
11730
11731 static void
11732 do_neon_bitfield (void)
11733 {
11734   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
11735   neon_check_type (3, rs, N_IGNORE_TYPE);
11736   neon_three_same (neon_quad (rs), 0, -1);
11737 }
11738
11739 static void
11740 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
11741                   unsigned destbits)
11742 {
11743   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
11744   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
11745                                             types | N_KEY);
11746   if (et.type == NT_float)
11747     {
11748       inst.instruction = NEON_ENC_FLOAT (inst.instruction);
11749       neon_three_same (neon_quad (rs), 0, -1);
11750     }
11751   else
11752     {
11753       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
11754       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
11755     }
11756 }
11757
11758 static void
11759 do_neon_dyadic_if_su (void)
11760 {
11761   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
11762 }
11763
11764 static void
11765 do_neon_dyadic_if_su_d (void)
11766 {
11767   /* This version only allow D registers, but that constraint is enforced during
11768      operand parsing so we don't need to do anything extra here.  */
11769   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
11770 }
11771
11772 static void
11773 do_neon_dyadic_if_i_d (void)
11774 {
11775   /* The "untyped" case can't happen. Do this to stop the "U" bit being
11776      affected if we specify unsigned args.  */
11777   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
11778 }
11779
11780 enum vfp_or_neon_is_neon_bits
11781 {
11782   NEON_CHECK_CC = 1,
11783   NEON_CHECK_ARCH = 2
11784 };
11785
11786 /* Call this function if an instruction which may have belonged to the VFP or
11787    Neon instruction sets, but turned out to be a Neon instruction (due to the
11788    operand types involved, etc.). We have to check and/or fix-up a couple of
11789    things:
11790
11791      - Make sure the user hasn't attempted to make a Neon instruction
11792        conditional.
11793      - Alter the value in the condition code field if necessary.
11794      - Make sure that the arch supports Neon instructions.
11795
11796    Which of these operations take place depends on bits from enum
11797    vfp_or_neon_is_neon_bits.
11798
11799    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
11800    current instruction's condition is COND_ALWAYS, the condition field is
11801    changed to inst.uncond_value. This is necessary because instructions shared
11802    between VFP and Neon may be conditional for the VFP variants only, and the
11803    unconditional Neon version must have, e.g., 0xF in the condition field.  */
11804
11805 static int
11806 vfp_or_neon_is_neon (unsigned check)
11807 {
11808   /* Conditions are always legal in Thumb mode (IT blocks).  */
11809   if (!thumb_mode && (check & NEON_CHECK_CC))
11810     {
11811       if (inst.cond != COND_ALWAYS)
11812         {
11813           first_error (_(BAD_COND));
11814           return FAIL;
11815         }
11816       if (inst.uncond_value != -1)
11817         inst.instruction |= inst.uncond_value << 28;
11818     }
11819   
11820   if ((check & NEON_CHECK_ARCH)
11821       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
11822     {
11823       first_error (_(BAD_FPU));
11824       return FAIL;
11825     }
11826   
11827   return SUCCESS;
11828 }
11829
11830 static void
11831 do_neon_addsub_if_i (void)
11832 {
11833   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
11834     return;
11835
11836   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
11837     return;
11838
11839   /* The "untyped" case can't happen. Do this to stop the "U" bit being
11840      affected if we specify unsigned args.  */
11841   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
11842 }
11843
11844 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
11845    result to be:
11846      V<op> A,B     (A is operand 0, B is operand 2)
11847    to mean:
11848      V<op> A,B,A
11849    not:
11850      V<op> A,B,B
11851    so handle that case specially.  */
11852
11853 static void
11854 neon_exchange_operands (void)
11855 {
11856   void *scratch = alloca (sizeof (inst.operands[0]));
11857   if (inst.operands[1].present)
11858     {
11859       /* Swap operands[1] and operands[2].  */
11860       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
11861       inst.operands[1] = inst.operands[2];
11862       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
11863     }
11864   else
11865     {
11866       inst.operands[1] = inst.operands[2];
11867       inst.operands[2] = inst.operands[0];
11868     }
11869 }
11870
11871 static void
11872 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
11873 {
11874   if (inst.operands[2].isreg)
11875     {
11876       if (invert)
11877         neon_exchange_operands ();
11878       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
11879     }
11880   else
11881     {
11882       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
11883       struct neon_type_el et = neon_check_type (2, rs,
11884         N_EQK | N_SIZ, immtypes | N_KEY);
11885
11886       inst.instruction = NEON_ENC_IMMED (inst.instruction);
11887       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
11888       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
11889       inst.instruction |= LOW4 (inst.operands[1].reg);
11890       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
11891       inst.instruction |= neon_quad (rs) << 6;
11892       inst.instruction |= (et.type == NT_float) << 10;
11893       inst.instruction |= neon_logbits (et.size) << 18;
11894       
11895       inst.instruction = neon_dp_fixup (inst.instruction);
11896     }
11897 }
11898
11899 static void
11900 do_neon_cmp (void)
11901 {
11902   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, FALSE);
11903 }
11904
11905 static void
11906 do_neon_cmp_inv (void)
11907 {
11908   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, TRUE);
11909 }
11910
11911 static void
11912 do_neon_ceq (void)
11913 {
11914   neon_compare (N_IF_32, N_IF_32, FALSE);
11915 }
11916
11917 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
11918    scalars, which are encoded in 5 bits, M : Rm.
11919    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
11920    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
11921    index in M.  */
11922
11923 static unsigned
11924 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
11925 {
11926   unsigned regno = NEON_SCALAR_REG (scalar);
11927   unsigned elno = NEON_SCALAR_INDEX (scalar);
11928
11929   switch (elsize)
11930     {
11931     case 16:
11932       if (regno > 7 || elno > 3)
11933         goto bad_scalar;
11934       return regno | (elno << 3);
11935     
11936     case 32:
11937       if (regno > 15 || elno > 1)
11938         goto bad_scalar;
11939       return regno | (elno << 4);
11940
11941     default:
11942     bad_scalar:
11943       first_error (_("scalar out of range for multiply instruction"));
11944     }
11945
11946   return 0;
11947 }
11948
11949 /* Encode multiply / multiply-accumulate scalar instructions.  */
11950
11951 static void
11952 neon_mul_mac (struct neon_type_el et, int ubit)
11953 {
11954   unsigned scalar;
11955
11956   /* Give a more helpful error message if we have an invalid type.  */
11957   if (et.type == NT_invtype)
11958     return;
11959   
11960   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
11961   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
11962   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
11963   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
11964   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
11965   inst.instruction |= LOW4 (scalar);
11966   inst.instruction |= HI1 (scalar) << 5;
11967   inst.instruction |= (et.type == NT_float) << 8;
11968   inst.instruction |= neon_logbits (et.size) << 20;
11969   inst.instruction |= (ubit != 0) << 24;
11970
11971   inst.instruction = neon_dp_fixup (inst.instruction);
11972 }
11973
11974 static void
11975 do_neon_mac_maybe_scalar (void)
11976 {
11977   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
11978     return;
11979
11980   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
11981     return;
11982
11983   if (inst.operands[2].isscalar)
11984     {
11985       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
11986       struct neon_type_el et = neon_check_type (3, rs,
11987         N_EQK, N_EQK, N_I16 | N_I32 | N_F32 | N_KEY);
11988       inst.instruction = NEON_ENC_SCALAR (inst.instruction);
11989       neon_mul_mac (et, neon_quad (rs));
11990     }
11991   else
11992     {
11993       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
11994          affected if we specify unsigned args.  */
11995       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
11996     }
11997 }
11998
11999 static void
12000 do_neon_tst (void)
12001 {
12002   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
12003   struct neon_type_el et = neon_check_type (3, rs,
12004     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
12005   neon_three_same (neon_quad (rs), 0, et.size);
12006 }
12007
12008 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
12009    same types as the MAC equivalents. The polynomial type for this instruction
12010    is encoded the same as the integer type.  */
12011
12012 static void
12013 do_neon_mul (void)
12014 {
12015   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
12016     return;
12017
12018   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
12019     return;
12020
12021   if (inst.operands[2].isscalar)
12022     do_neon_mac_maybe_scalar ();
12023   else
12024     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F32 | N_P8, 0);
12025 }
12026
12027 static void
12028 do_neon_qdmulh (void)
12029 {
12030   if (inst.operands[2].isscalar)
12031     {
12032       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
12033       struct neon_type_el et = neon_check_type (3, rs,
12034         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
12035       inst.instruction = NEON_ENC_SCALAR (inst.instruction);
12036       neon_mul_mac (et, neon_quad (rs));
12037     }
12038   else
12039     {
12040       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
12041       struct neon_type_el et = neon_check_type (3, rs,
12042         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
12043       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
12044       /* The U bit (rounding) comes from bit mask.  */
12045       neon_three_same (neon_quad (rs), 0, et.size);
12046     }
12047 }
12048
12049 static void
12050 do_neon_fcmp_absolute (void)
12051 {
12052   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
12053   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
12054   /* Size field comes from bit mask.  */
12055   neon_three_same (neon_quad (rs), 1, -1);
12056 }
12057
12058 static void
12059 do_neon_fcmp_absolute_inv (void)
12060 {
12061   neon_exchange_operands ();
12062   do_neon_fcmp_absolute ();
12063 }
12064
12065 static void
12066 do_neon_step (void)
12067 {
12068   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
12069   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
12070   neon_three_same (neon_quad (rs), 0, -1);
12071 }
12072
12073 static void
12074 do_neon_abs_neg (void)
12075 {
12076   enum neon_shape rs;
12077   struct neon_type_el et;
12078   
12079   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
12080     return;
12081
12082   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
12083     return;
12084
12085   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
12086   et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_F32 | N_KEY);
12087   
12088   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12089   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12090   inst.instruction |= LOW4 (inst.operands[1].reg);
12091   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
12092   inst.instruction |= neon_quad (rs) << 6;
12093   inst.instruction |= (et.type == NT_float) << 10;
12094   inst.instruction |= neon_logbits (et.size) << 18;
12095   
12096   inst.instruction = neon_dp_fixup (inst.instruction);
12097 }
12098
12099 static void
12100 do_neon_sli (void)
12101 {
12102   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
12103   struct neon_type_el et = neon_check_type (2, rs,
12104     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
12105   int imm = inst.operands[2].imm;
12106   constraint (imm < 0 || (unsigned)imm >= et.size,
12107               _("immediate out of range for insert"));
12108   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
12109 }
12110
12111 static void
12112 do_neon_sri (void)
12113 {
12114   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
12115   struct neon_type_el et = neon_check_type (2, rs,
12116     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
12117   int imm = inst.operands[2].imm;
12118   constraint (imm < 1 || (unsigned)imm > et.size,
12119               _("immediate out of range for insert"));
12120   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
12121 }
12122
12123 static void
12124 do_neon_qshlu_imm (void)
12125 {
12126   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
12127   struct neon_type_el et = neon_check_type (2, rs,
12128     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
12129   int imm = inst.operands[2].imm;
12130   constraint (imm < 0 || (unsigned)imm >= et.size,
12131               _("immediate out of range for shift"));
12132   /* Only encodes the 'U present' variant of the instruction.
12133      In this case, signed types have OP (bit 8) set to 0.
12134      Unsigned types have OP set to 1.  */
12135   inst.instruction |= (et.type == NT_unsigned) << 8;
12136   /* The rest of the bits are the same as other immediate shifts.  */
12137   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
12138 }
12139
12140 static void
12141 do_neon_qmovn (void)
12142 {
12143   struct neon_type_el et = neon_check_type (2, NS_DQ,
12144     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
12145   /* Saturating move where operands can be signed or unsigned, and the
12146      destination has the same signedness.  */
12147   inst.instruction = NEON_ENC_INTEGER (inst.instruction);
12148   if (et.type == NT_unsigned)
12149     inst.instruction |= 0xc0;
12150   else
12151     inst.instruction |= 0x80;
12152   neon_two_same (0, 1, et.size / 2);
12153 }
12154
12155 static void
12156 do_neon_qmovun (void)
12157 {
12158   struct neon_type_el et = neon_check_type (2, NS_DQ,
12159     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
12160   /* Saturating move with unsigned results. Operands must be signed.  */
12161   inst.instruction = NEON_ENC_INTEGER (inst.instruction);
12162   neon_two_same (0, 1, et.size / 2);
12163 }
12164
12165 static void
12166 do_neon_rshift_sat_narrow (void)
12167 {
12168   /* FIXME: Types for narrowing. If operands are signed, results can be signed
12169      or unsigned. If operands are unsigned, results must also be unsigned.  */
12170   struct neon_type_el et = neon_check_type (2, NS_DQI,
12171     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
12172   int imm = inst.operands[2].imm;
12173   /* This gets the bounds check, size encoding and immediate bits calculation
12174      right.  */
12175   et.size /= 2;
12176   
12177   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
12178      VQMOVN.I<size> <Dd>, <Qm>.  */
12179   if (imm == 0)
12180     {
12181       inst.operands[2].present = 0;
12182       inst.instruction = N_MNEM_vqmovn;
12183       do_neon_qmovn ();
12184       return;
12185     }
12186   
12187   constraint (imm < 1 || (unsigned)imm > et.size,
12188               _("immediate out of range"));
12189   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
12190 }
12191
12192 static void
12193 do_neon_rshift_sat_narrow_u (void)
12194 {
12195   /* FIXME: Types for narrowing. If operands are signed, results can be signed
12196      or unsigned. If operands are unsigned, results must also be unsigned.  */
12197   struct neon_type_el et = neon_check_type (2, NS_DQI,
12198     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
12199   int imm = inst.operands[2].imm;
12200   /* This gets the bounds check, size encoding and immediate bits calculation
12201      right.  */
12202   et.size /= 2;
12203
12204   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
12205      VQMOVUN.I<size> <Dd>, <Qm>.  */
12206   if (imm == 0)
12207     {
12208       inst.operands[2].present = 0;
12209       inst.instruction = N_MNEM_vqmovun;
12210       do_neon_qmovun ();
12211       return;
12212     }
12213
12214   constraint (imm < 1 || (unsigned)imm > et.size,
12215               _("immediate out of range"));
12216   /* FIXME: The manual is kind of unclear about what value U should have in
12217      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
12218      must be 1.  */
12219   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
12220 }
12221
12222 static void
12223 do_neon_movn (void)
12224 {
12225   struct neon_type_el et = neon_check_type (2, NS_DQ,
12226     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
12227   inst.instruction = NEON_ENC_INTEGER (inst.instruction);
12228   neon_two_same (0, 1, et.size / 2);
12229 }
12230
12231 static void
12232 do_neon_rshift_narrow (void)
12233 {
12234   struct neon_type_el et = neon_check_type (2, NS_DQI,
12235     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
12236   int imm = inst.operands[2].imm;
12237   /* This gets the bounds check, size encoding and immediate bits calculation
12238      right.  */
12239   et.size /= 2;
12240   
12241   /* If immediate is zero then we are a pseudo-instruction for
12242      VMOVN.I<size> <Dd>, <Qm>  */
12243   if (imm == 0)
12244     {
12245       inst.operands[2].present = 0;
12246       inst.instruction = N_MNEM_vmovn;
12247       do_neon_movn ();
12248       return;
12249     }
12250   
12251   constraint (imm < 1 || (unsigned)imm > et.size,
12252               _("immediate out of range for narrowing operation"));
12253   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
12254 }
12255
12256 static void
12257 do_neon_shll (void)
12258 {
12259   /* FIXME: Type checking when lengthening.  */
12260   struct neon_type_el et = neon_check_type (2, NS_QDI,
12261     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
12262   unsigned imm = inst.operands[2].imm;
12263
12264   if (imm == et.size)
12265     {
12266       /* Maximum shift variant.  */
12267       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
12268       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12269       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12270       inst.instruction |= LOW4 (inst.operands[1].reg);
12271       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
12272       inst.instruction |= neon_logbits (et.size) << 18;
12273       
12274       inst.instruction = neon_dp_fixup (inst.instruction);
12275     }
12276   else
12277     {
12278       /* A more-specific type check for non-max versions.  */
12279       et = neon_check_type (2, NS_QDI,
12280         N_EQK | N_DBL, N_SU_32 | N_KEY);
12281       inst.instruction = NEON_ENC_IMMED (inst.instruction);
12282       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
12283     }
12284 }
12285
12286 /* Check the various types for the VCVT instruction, and return which version
12287    the current instruction is.  */
12288
12289 static int
12290 neon_cvt_flavour (enum neon_shape rs)
12291 {
12292 #define CVT_VAR(C,X,Y)                                                  \
12293   et = neon_check_type (2, rs, whole_reg | (X), whole_reg | (Y));       \
12294   if (et.type != NT_invtype)                                            \
12295     {                                                                   \
12296       inst.error = NULL;                                                \
12297       return (C);                                                       \
12298     }
12299   struct neon_type_el et;
12300   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
12301                         || rs == NS_FF) ? N_VFP : 0;
12302   /* The instruction versions which take an immediate take one register
12303      argument, which is extended to the width of the full register. Thus the
12304      "source" and "destination" registers must have the same width.  Hack that
12305      here by making the size equal to the key (wider, in this case) operand.  */
12306   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
12307   
12308   CVT_VAR (0, N_S32, N_F32);
12309   CVT_VAR (1, N_U32, N_F32);
12310   CVT_VAR (2, N_F32, N_S32);
12311   CVT_VAR (3, N_F32, N_U32);
12312   
12313   whole_reg = N_VFP;
12314   
12315   /* VFP instructions.  */
12316   CVT_VAR (4, N_F32, N_F64);
12317   CVT_VAR (5, N_F64, N_F32);
12318   CVT_VAR (6, N_S32, N_F64 | key);
12319   CVT_VAR (7, N_U32, N_F64 | key);
12320   CVT_VAR (8, N_F64 | key, N_S32);
12321   CVT_VAR (9, N_F64 | key, N_U32);
12322   /* VFP instructions with bitshift.  */
12323   CVT_VAR (10, N_F32 | key, N_S16);
12324   CVT_VAR (11, N_F32 | key, N_U16);
12325   CVT_VAR (12, N_F64 | key, N_S16);
12326   CVT_VAR (13, N_F64 | key, N_U16);
12327   CVT_VAR (14, N_S16, N_F32 | key);
12328   CVT_VAR (15, N_U16, N_F32 | key);
12329   CVT_VAR (16, N_S16, N_F64 | key);
12330   CVT_VAR (17, N_U16, N_F64 | key);
12331   
12332   return -1;
12333 #undef CVT_VAR
12334 }
12335
12336 /* Neon-syntax VFP conversions.  */
12337
12338 static void
12339 do_vfp_nsyn_cvt (enum neon_shape rs, int flavour)
12340 {
12341   const char *opname = 0;
12342   
12343   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI)
12344     {
12345       /* Conversions with immediate bitshift.  */
12346       const char *enc[] =
12347         {
12348           "ftosls",
12349           "ftouls",
12350           "fsltos",
12351           "fultos",
12352           NULL,
12353           NULL,
12354           "ftosld",
12355           "ftould",
12356           "fsltod",
12357           "fultod",
12358           "fshtos",
12359           "fuhtos",
12360           "fshtod",
12361           "fuhtod",
12362           "ftoshs",
12363           "ftouhs",
12364           "ftoshd",
12365           "ftouhd"
12366         };
12367
12368       if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc))
12369         {
12370           opname = enc[flavour];
12371           constraint (inst.operands[0].reg != inst.operands[1].reg,
12372                       _("operands 0 and 1 must be the same register"));
12373           inst.operands[1] = inst.operands[2];
12374           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
12375         }
12376     }
12377   else
12378     {
12379       /* Conversions without bitshift.  */
12380       const char *enc[] =
12381         {
12382           "ftosis",
12383           "ftouis",
12384           "fsitos",
12385           "fuitos",
12386           "fcvtsd",
12387           "fcvtds",
12388           "ftosid",
12389           "ftouid",
12390           "fsitod",
12391           "fuitod"
12392         };
12393
12394       if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc))
12395         opname = enc[flavour];
12396     }
12397
12398   if (opname)
12399     do_vfp_nsyn_opcode (opname);
12400 }
12401
12402 static void
12403 do_vfp_nsyn_cvtz (void)
12404 {
12405   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_NULL);
12406   int flavour = neon_cvt_flavour (rs);
12407   const char *enc[] =
12408     {
12409       "ftosizs",
12410       "ftouizs",
12411       NULL,
12412       NULL,
12413       NULL,
12414       NULL,
12415       "ftosizd",
12416       "ftouizd"
12417     };
12418
12419   if (flavour >= 0 && flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
12420     do_vfp_nsyn_opcode (enc[flavour]);
12421 }
12422
12423 static void
12424 do_neon_cvt (void)
12425 {
12426   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
12427     NS_FD, NS_DF, NS_FF, NS_NULL);
12428   int flavour = neon_cvt_flavour (rs);
12429
12430   /* VFP rather than Neon conversions.  */
12431   if (flavour >= 4)
12432     {
12433       do_vfp_nsyn_cvt (rs, flavour);
12434       return;
12435     }
12436
12437   switch (rs)
12438     {
12439     case NS_DDI:
12440     case NS_QQI:
12441       {
12442         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
12443           return;
12444
12445         /* Fixed-point conversion with #0 immediate is encoded as an
12446            integer conversion.  */
12447         if (inst.operands[2].present && inst.operands[2].imm == 0)
12448           goto int_encode;
12449         unsigned immbits = 32 - inst.operands[2].imm;
12450         unsigned enctab[] = { 0x0000100, 0x1000100, 0x0, 0x1000000 };
12451         inst.instruction = NEON_ENC_IMMED (inst.instruction);
12452         if (flavour != -1)
12453           inst.instruction |= enctab[flavour];
12454         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12455         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12456         inst.instruction |= LOW4 (inst.operands[1].reg);
12457         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
12458         inst.instruction |= neon_quad (rs) << 6;
12459         inst.instruction |= 1 << 21;
12460         inst.instruction |= immbits << 16;
12461
12462         inst.instruction = neon_dp_fixup (inst.instruction);
12463       }
12464       break;
12465
12466     case NS_DD:
12467     case NS_QQ:
12468     int_encode:
12469       {
12470         unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080 };
12471
12472         inst.instruction = NEON_ENC_INTEGER (inst.instruction);
12473
12474         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
12475           return;
12476
12477         if (flavour != -1)
12478           inst.instruction |= enctab[flavour];
12479
12480         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12481         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12482         inst.instruction |= LOW4 (inst.operands[1].reg);
12483         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
12484         inst.instruction |= neon_quad (rs) << 6;
12485         inst.instruction |= 2 << 18;
12486
12487         inst.instruction = neon_dp_fixup (inst.instruction);
12488       }
12489     break;
12490
12491     default:
12492       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
12493       do_vfp_nsyn_cvt (rs, flavour);
12494     }
12495 }
12496
12497 static void
12498 neon_move_immediate (void)
12499 {
12500   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
12501   struct neon_type_el et = neon_check_type (2, rs,
12502     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
12503   unsigned immlo, immhi = 0, immbits;
12504   int op, cmode;
12505
12506   constraint (et.type == NT_invtype,
12507               _("operand size must be specified for immediate VMOV"));
12508
12509   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
12510   op = (inst.instruction & (1 << 5)) != 0;
12511
12512   immlo = inst.operands[1].imm;
12513   if (inst.operands[1].regisimm)
12514     immhi = inst.operands[1].reg;
12515
12516   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
12517               _("immediate has bits set outside the operand size"));
12518
12519   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, &immbits, &op,
12520                                         et.size, et.type)) == FAIL)
12521     {
12522       /* Invert relevant bits only.  */
12523       neon_invert_size (&immlo, &immhi, et.size);
12524       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
12525          with one or the other; those cases are caught by
12526          neon_cmode_for_move_imm.  */
12527       op = !op;
12528       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, &immbits, &op,
12529                                             et.size, et.type)) == FAIL)
12530         {
12531           first_error (_("immediate out of range"));
12532           return;
12533         }
12534     }
12535
12536   inst.instruction &= ~(1 << 5);
12537   inst.instruction |= op << 5;
12538
12539   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12540   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12541   inst.instruction |= neon_quad (rs) << 6;
12542   inst.instruction |= cmode << 8;
12543
12544   neon_write_immbits (immbits);
12545 }
12546
12547 static void
12548 do_neon_mvn (void)
12549 {
12550   if (inst.operands[1].isreg)
12551     {
12552       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
12553       
12554       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
12555       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12556       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12557       inst.instruction |= LOW4 (inst.operands[1].reg);
12558       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
12559       inst.instruction |= neon_quad (rs) << 6;
12560     }
12561   else
12562     {
12563       inst.instruction = NEON_ENC_IMMED (inst.instruction);
12564       neon_move_immediate ();
12565     }
12566
12567   inst.instruction = neon_dp_fixup (inst.instruction);
12568 }
12569
12570 /* Encode instructions of form:
12571
12572   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
12573   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |
12574
12575 */
12576
12577 static void
12578 neon_mixed_length (struct neon_type_el et, unsigned size)
12579 {
12580   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12581   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12582   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
12583   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
12584   inst.instruction |= LOW4 (inst.operands[2].reg);
12585   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
12586   inst.instruction |= (et.type == NT_unsigned) << 24;
12587   inst.instruction |= neon_logbits (size) << 20;
12588   
12589   inst.instruction = neon_dp_fixup (inst.instruction);
12590 }
12591
12592 static void
12593 do_neon_dyadic_long (void)
12594 {
12595   /* FIXME: Type checking for lengthening op.  */
12596   struct neon_type_el et = neon_check_type (3, NS_QDD,
12597     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
12598   neon_mixed_length (et, et.size);
12599 }
12600
12601 static void
12602 do_neon_abal (void)
12603 {
12604   struct neon_type_el et = neon_check_type (3, NS_QDD,
12605     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
12606   neon_mixed_length (et, et.size);
12607 }
12608
12609 static void
12610 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
12611 {
12612   if (inst.operands[2].isscalar)
12613     {
12614       struct neon_type_el et = neon_check_type (3, NS_QDS,
12615         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
12616       inst.instruction = NEON_ENC_SCALAR (inst.instruction);
12617       neon_mul_mac (et, et.type == NT_unsigned);
12618     }
12619   else
12620     {
12621       struct neon_type_el et = neon_check_type (3, NS_QDD,
12622         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
12623       inst.instruction = NEON_ENC_INTEGER (inst.instruction);
12624       neon_mixed_length (et, et.size);
12625     }
12626 }
12627
12628 static void
12629 do_neon_mac_maybe_scalar_long (void)
12630 {
12631   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
12632 }
12633
12634 static void
12635 do_neon_dyadic_wide (void)
12636 {
12637   struct neon_type_el et = neon_check_type (3, NS_QQD,
12638     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
12639   neon_mixed_length (et, et.size);
12640 }
12641
12642 static void
12643 do_neon_dyadic_narrow (void)
12644 {
12645   struct neon_type_el et = neon_check_type (3, NS_QDD,
12646     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
12647   /* Operand sign is unimportant, and the U bit is part of the opcode,
12648      so force the operand type to integer.  */
12649   et.type = NT_integer;
12650   neon_mixed_length (et, et.size / 2);
12651 }
12652
12653 static void
12654 do_neon_mul_sat_scalar_long (void)
12655 {
12656   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
12657 }
12658
12659 static void
12660 do_neon_vmull (void)
12661 {
12662   if (inst.operands[2].isscalar)
12663     do_neon_mac_maybe_scalar_long ();
12664   else
12665     {
12666       struct neon_type_el et = neon_check_type (3, NS_QDD,
12667         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_KEY);
12668       if (et.type == NT_poly)
12669         inst.instruction = NEON_ENC_POLY (inst.instruction);
12670       else
12671         inst.instruction = NEON_ENC_INTEGER (inst.instruction);
12672       /* For polynomial encoding, size field must be 0b00 and the U bit must be
12673          zero. Should be OK as-is.  */
12674       neon_mixed_length (et, et.size);
12675     }
12676 }
12677
12678 static void
12679 do_neon_ext (void)
12680 {
12681   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
12682   struct neon_type_el et = neon_check_type (3, rs,
12683     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
12684   unsigned imm = (inst.operands[3].imm * et.size) / 8;
12685   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12686   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12687   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
12688   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
12689   inst.instruction |= LOW4 (inst.operands[2].reg);
12690   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
12691   inst.instruction |= neon_quad (rs) << 6;
12692   inst.instruction |= imm << 8;
12693   
12694   inst.instruction = neon_dp_fixup (inst.instruction);
12695 }
12696
12697 static void
12698 do_neon_rev (void)
12699 {
12700   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
12701   struct neon_type_el et = neon_check_type (2, rs,
12702     N_EQK, N_8 | N_16 | N_32 | N_KEY);
12703   unsigned op = (inst.instruction >> 7) & 3;
12704   /* N (width of reversed regions) is encoded as part of the bitmask. We
12705      extract it here to check the elements to be reversed are smaller.
12706      Otherwise we'd get a reserved instruction.  */
12707   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
12708   assert (elsize != 0);
12709   constraint (et.size >= elsize,
12710               _("elements must be smaller than reversal region"));
12711   neon_two_same (neon_quad (rs), 1, et.size);
12712 }
12713
12714 static void
12715 do_neon_dup (void)
12716 {
12717   if (inst.operands[1].isscalar)
12718     {
12719       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
12720       struct neon_type_el et = neon_check_type (2, rs,
12721         N_EQK, N_8 | N_16 | N_32 | N_KEY);
12722       unsigned sizebits = et.size >> 3;
12723       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
12724       int logsize = neon_logbits (et.size);
12725       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
12726
12727       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
12728         return;
12729
12730       inst.instruction = NEON_ENC_SCALAR (inst.instruction);
12731       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12732       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12733       inst.instruction |= LOW4 (dm);
12734       inst.instruction |= HI1 (dm) << 5;
12735       inst.instruction |= neon_quad (rs) << 6;
12736       inst.instruction |= x << 17;
12737       inst.instruction |= sizebits << 16;
12738       
12739       inst.instruction = neon_dp_fixup (inst.instruction);
12740     }
12741   else
12742     {
12743       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
12744       struct neon_type_el et = neon_check_type (2, rs,
12745         N_8 | N_16 | N_32 | N_KEY, N_EQK);
12746       /* Duplicate ARM register to lanes of vector.  */
12747       inst.instruction = NEON_ENC_ARMREG (inst.instruction);
12748       switch (et.size)
12749         {
12750         case 8:  inst.instruction |= 0x400000; break;
12751         case 16: inst.instruction |= 0x000020; break;
12752         case 32: inst.instruction |= 0x000000; break;
12753         default: break;
12754         }
12755       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
12756       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
12757       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
12758       inst.instruction |= neon_quad (rs) << 21;
12759       /* The encoding for this instruction is identical for the ARM and Thumb
12760          variants, except for the condition field.  */
12761       do_vfp_cond_or_thumb ();
12762     }
12763 }
12764
12765 /* VMOV has particularly many variations. It can be one of:
12766      0. VMOV<c><q> <Qd>, <Qm>
12767      1. VMOV<c><q> <Dd>, <Dm>
12768    (Register operations, which are VORR with Rm = Rn.)
12769      2. VMOV<c><q>.<dt> <Qd>, #<imm>
12770      3. VMOV<c><q>.<dt> <Dd>, #<imm>
12771    (Immediate loads.)
12772      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
12773    (ARM register to scalar.)
12774      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
12775    (Two ARM registers to vector.)
12776      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
12777    (Scalar to ARM register.)
12778      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
12779    (Vector to two ARM registers.)
12780      8. VMOV.F32 <Sd>, <Sm>
12781      9. VMOV.F64 <Dd>, <Dm>
12782    (VFP register moves.)
12783     10. VMOV.F32 <Sd>, #imm
12784     11. VMOV.F64 <Dd>, #imm
12785    (VFP float immediate load.)
12786     12. VMOV <Rd>, <Sm>
12787    (VFP single to ARM reg.)
12788     13. VMOV <Sd>, <Rm>
12789    (ARM reg to VFP single.)
12790     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
12791    (Two ARM regs to two VFP singles.)
12792     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
12793    (Two VFP singles to two ARM regs.)
12794   
12795    These cases can be disambiguated using neon_select_shape, except cases 1/9
12796    and 3/11 which depend on the operand type too.
12797    
12798    All the encoded bits are hardcoded by this function.
12799    
12800    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
12801    Cases 5, 7 may be used with VFPv2 and above.
12802    
12803    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
12804    can specify a type where it doesn't make sense to, and is ignored).
12805 */
12806
12807 static void
12808 do_neon_mov (void)
12809 {
12810   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
12811     NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR, NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
12812     NS_NULL);
12813   struct neon_type_el et;
12814   const char *ldconst = 0;
12815
12816   switch (rs)
12817     {
12818     case NS_DD:  /* case 1/9.  */
12819       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
12820       /* It is not an error here if no type is given.  */
12821       inst.error = NULL;
12822       if (et.type == NT_float && et.size == 64)
12823         {
12824           do_vfp_nsyn_opcode ("fcpyd");
12825           break;
12826         }
12827       /* fall through.  */
12828
12829     case NS_QQ:  /* case 0/1.  */
12830       {
12831         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
12832           return;
12833         /* The architecture manual I have doesn't explicitly state which
12834            value the U bit should have for register->register moves, but
12835            the equivalent VORR instruction has U = 0, so do that.  */
12836         inst.instruction = 0x0200110;
12837         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
12838         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
12839         inst.instruction |= LOW4 (inst.operands[1].reg);
12840         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
12841         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
12842         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
12843         inst.instruction |= neon_quad (rs) << 6;
12844
12845         inst.instruction = neon_dp_fixup (inst.instruction);
12846       }
12847       break;
12848         
12849     case NS_DI:  /* case 3/11.  */
12850       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
12851       inst.error = NULL;
12852       if (et.type == NT_float && et.size == 64)
12853         {
12854           /* case 11 (fconstd).  */
12855           ldconst = "fconstd";
12856           goto encode_fconstd;
12857         }
12858       /* fall through.  */
12859
12860     case NS_QI:  /* case 2/3.  */
12861       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
12862         return;
12863       inst.instruction = 0x0800010;
12864       neon_move_immediate ();
12865       inst.instruction = neon_dp_fixup (inst.instruction);
12866       break;
12867     
12868     case NS_SR:  /* case 4.  */
12869       {
12870         unsigned bcdebits = 0;
12871         struct neon_type_el et = neon_check_type (2, NS_NULL,
12872           N_8 | N_16 | N_32 | N_KEY, N_EQK);
12873         int logsize = neon_logbits (et.size);
12874         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
12875         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
12876
12877         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
12878                     _(BAD_FPU));
12879         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
12880                     && et.size != 32, _(BAD_FPU));
12881         constraint (et.type == NT_invtype, _("bad type for scalar"));
12882         constraint (x >= 64 / et.size, _("scalar index out of range"));
12883
12884         switch (et.size)
12885           {
12886           case 8:  bcdebits = 0x8; break;
12887           case 16: bcdebits = 0x1; break;
12888           case 32: bcdebits = 0x0; break;
12889           default: ;
12890           }
12891
12892         bcdebits |= x << logsize;
12893
12894         inst.instruction = 0xe000b10;
12895         do_vfp_cond_or_thumb ();
12896         inst.instruction |= LOW4 (dn) << 16;
12897         inst.instruction |= HI1 (dn) << 7;
12898         inst.instruction |= inst.operands[1].reg << 12;
12899         inst.instruction |= (bcdebits & 3) << 5;
12900         inst.instruction |= (bcdebits >> 2) << 21;
12901       }
12902       break;
12903     
12904     case NS_DRR:  /* case 5 (fmdrr).  */
12905       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
12906                   _(BAD_FPU));
12907
12908       inst.instruction = 0xc400b10;
12909       do_vfp_cond_or_thumb ();
12910       inst.instruction |= LOW4 (inst.operands[0].reg);
12911       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
12912       inst.instruction |= inst.operands[1].reg << 12;
12913       inst.instruction |= inst.operands[2].reg << 16;
12914       break;
12915     
12916     case NS_RS:  /* case 6.  */
12917       {
12918         struct neon_type_el et = neon_check_type (2, NS_NULL,
12919           N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
12920         unsigned logsize = neon_logbits (et.size);
12921         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
12922         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
12923         unsigned abcdebits = 0;
12924
12925         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
12926                     _(BAD_FPU));
12927         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
12928                     && et.size != 32, _(BAD_FPU));
12929         constraint (et.type == NT_invtype, _("bad type for scalar"));
12930         constraint (x >= 64 / et.size, _("scalar index out of range"));
12931
12932         switch (et.size)
12933           {
12934           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
12935           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
12936           case 32: abcdebits = 0x00; break;
12937           default: ;
12938           }
12939
12940         abcdebits |= x << logsize;
12941         inst.instruction = 0xe100b10;
12942         do_vfp_cond_or_thumb ();
12943         inst.instruction |= LOW4 (dn) << 16;
12944         inst.instruction |= HI1 (dn) << 7;
12945         inst.instruction |= inst.operands[0].reg << 12;
12946         inst.instruction |= (abcdebits & 3) << 5;
12947         inst.instruction |= (abcdebits >> 2) << 21;
12948       }
12949       break;
12950     
12951     case NS_RRD:  /* case 7 (fmrrd).  */
12952       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
12953                   _(BAD_FPU));
12954
12955       inst.instruction = 0xc500b10;
12956       do_vfp_cond_or_thumb ();
12957       inst.instruction |= inst.operands[0].reg << 12;
12958       inst.instruction |= inst.operands[1].reg << 16;
12959       inst.instruction |= LOW4 (inst.operands[2].reg);
12960       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
12961       break;
12962     
12963     case NS_FF:  /* case 8 (fcpys).  */
12964       do_vfp_nsyn_opcode ("fcpys");
12965       break;
12966     
12967     case NS_FI:  /* case 10 (fconsts).  */
12968       ldconst = "fconsts";
12969       encode_fconstd:
12970       if (is_quarter_float (inst.operands[1].imm))
12971         {
12972           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
12973           do_vfp_nsyn_opcode (ldconst);
12974         }
12975       else
12976         first_error (_("immediate out of range"));
12977       break;
12978     
12979     case NS_RF:  /* case 12 (fmrs).  */
12980       do_vfp_nsyn_opcode ("fmrs");
12981       break;
12982     
12983     case NS_FR:  /* case 13 (fmsr).  */
12984       do_vfp_nsyn_opcode ("fmsr");
12985       break;
12986     
12987     /* The encoders for the fmrrs and fmsrr instructions expect three operands
12988        (one of which is a list), but we have parsed four.  Do some fiddling to
12989        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
12990        expect.  */
12991     case NS_RRFF:  /* case 14 (fmrrs).  */
12992       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
12993                   _("VFP registers must be adjacent"));
12994       inst.operands[2].imm = 2;
12995       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
12996       do_vfp_nsyn_opcode ("fmrrs");
12997       break;
12998     
12999     case NS_FFRR:  /* case 15 (fmsrr).  */
13000       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
13001                   _("VFP registers must be adjacent"));
13002       inst.operands[1] = inst.operands[2];
13003       inst.operands[2] = inst.operands[3];
13004       inst.operands[0].imm = 2;
13005       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
13006       do_vfp_nsyn_opcode ("fmsrr");
13007       break;
13008     
13009     default:
13010       abort ();
13011     }
13012 }
13013
13014 static void
13015 do_neon_rshift_round_imm (void)
13016 {
13017   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
13018   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
13019   int imm = inst.operands[2].imm;
13020
13021   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
13022   if (imm == 0)
13023     {
13024       inst.operands[2].present = 0;
13025       do_neon_mov ();
13026       return;
13027     }
13028
13029   constraint (imm < 1 || (unsigned)imm > et.size,
13030               _("immediate out of range for shift"));
13031   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
13032                   et.size - imm);
13033 }
13034
13035 static void
13036 do_neon_movl (void)
13037 {
13038   struct neon_type_el et = neon_check_type (2, NS_QD,
13039     N_EQK | N_DBL, N_SU_32 | N_KEY);
13040   unsigned sizebits = et.size >> 3;
13041   inst.instruction |= sizebits << 19;
13042   neon_two_same (0, et.type == NT_unsigned, -1);
13043 }
13044
13045 static void
13046 do_neon_trn (void)
13047 {
13048   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13049   struct neon_type_el et = neon_check_type (2, rs,
13050     N_EQK, N_8 | N_16 | N_32 | N_KEY);
13051   inst.instruction = NEON_ENC_INTEGER (inst.instruction);
13052   neon_two_same (neon_quad (rs), 1, et.size);
13053 }
13054
13055 static void
13056 do_neon_zip_uzp (void)
13057 {
13058   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13059   struct neon_type_el et = neon_check_type (2, rs,
13060     N_EQK, N_8 | N_16 | N_32 | N_KEY);
13061   if (rs == NS_DD && et.size == 32)
13062     {
13063       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
13064       inst.instruction = N_MNEM_vtrn;
13065       do_neon_trn ();
13066       return;
13067     }
13068   neon_two_same (neon_quad (rs), 1, et.size);
13069 }
13070
13071 static void
13072 do_neon_sat_abs_neg (void)
13073 {
13074   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13075   struct neon_type_el et = neon_check_type (2, rs,
13076     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
13077   neon_two_same (neon_quad (rs), 1, et.size);
13078 }
13079
13080 static void
13081 do_neon_pair_long (void)
13082 {
13083   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13084   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
13085   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
13086   inst.instruction |= (et.type == NT_unsigned) << 7;
13087   neon_two_same (neon_quad (rs), 1, et.size);
13088 }
13089
13090 static void
13091 do_neon_recip_est (void)
13092 {
13093   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13094   struct neon_type_el et = neon_check_type (2, rs,
13095     N_EQK | N_FLT, N_F32 | N_U32 | N_KEY);
13096   inst.instruction |= (et.type == NT_float) << 8;
13097   neon_two_same (neon_quad (rs), 1, et.size);
13098 }
13099
13100 static void
13101 do_neon_cls (void)
13102 {
13103   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13104   struct neon_type_el et = neon_check_type (2, rs,
13105     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
13106   neon_two_same (neon_quad (rs), 1, et.size);
13107 }
13108
13109 static void
13110 do_neon_clz (void)
13111 {
13112   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13113   struct neon_type_el et = neon_check_type (2, rs,
13114     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
13115   neon_two_same (neon_quad (rs), 1, et.size);
13116 }
13117
13118 static void
13119 do_neon_cnt (void)
13120 {
13121   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13122   struct neon_type_el et = neon_check_type (2, rs,
13123     N_EQK | N_INT, N_8 | N_KEY);
13124   neon_two_same (neon_quad (rs), 1, et.size);
13125 }
13126
13127 static void
13128 do_neon_swp (void)
13129 {
13130   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
13131   neon_two_same (neon_quad (rs), 1, -1);
13132 }
13133
13134 static void
13135 do_neon_tbl_tbx (void)
13136 {
13137   unsigned listlenbits;
13138   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
13139   
13140   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
13141     {
13142       first_error (_("bad list length for table lookup"));
13143       return;
13144     }
13145   
13146   listlenbits = inst.operands[1].imm - 1;
13147   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13148   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13149   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
13150   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
13151   inst.instruction |= LOW4 (inst.operands[2].reg);
13152   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
13153   inst.instruction |= listlenbits << 8;
13154   
13155   inst.instruction = neon_dp_fixup (inst.instruction);
13156 }
13157
13158 static void
13159 do_neon_ldm_stm (void)
13160 {
13161   /* P, U and L bits are part of bitmask.  */
13162   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
13163   unsigned offsetbits = inst.operands[1].imm * 2;
13164
13165   if (inst.operands[1].issingle)
13166     {
13167       do_vfp_nsyn_ldm_stm (is_dbmode);
13168       return;
13169     }
13170
13171   constraint (is_dbmode && !inst.operands[0].writeback,
13172               _("writeback (!) must be used for VLDMDB and VSTMDB"));
13173
13174   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
13175               _("register list must contain at least 1 and at most 16 "
13176                 "registers"));
13177
13178   inst.instruction |= inst.operands[0].reg << 16;
13179   inst.instruction |= inst.operands[0].writeback << 21;
13180   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
13181   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
13182
13183   inst.instruction |= offsetbits;
13184   
13185   do_vfp_cond_or_thumb ();
13186 }
13187
13188 static void
13189 do_neon_ldr_str (void)
13190 {
13191   int is_ldr = (inst.instruction & (1 << 20)) != 0;
13192   
13193   if (inst.operands[0].issingle)
13194     {
13195       if (is_ldr)
13196         do_vfp_nsyn_opcode ("flds");
13197       else
13198         do_vfp_nsyn_opcode ("fsts");
13199     }
13200   else
13201     {
13202       if (is_ldr)
13203         do_vfp_nsyn_opcode ("fldd");
13204       else
13205         do_vfp_nsyn_opcode ("fstd");
13206     }
13207 }
13208
13209 /* "interleave" version also handles non-interleaving register VLD1/VST1
13210    instructions.  */
13211
13212 static void
13213 do_neon_ld_st_interleave (void)
13214 {
13215   struct neon_type_el et = neon_check_type (1, NS_NULL,
13216                                             N_8 | N_16 | N_32 | N_64);
13217   unsigned alignbits = 0;
13218   unsigned idx;
13219   /* The bits in this table go:
13220      0: register stride of one (0) or two (1)
13221      1,2: register list length, minus one (1, 2, 3, 4).
13222      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
13223      We use -1 for invalid entries.  */
13224   const int typetable[] =
13225     {
13226       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
13227        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
13228        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
13229        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
13230     };
13231   int typebits;
13232
13233   if (et.type == NT_invtype)
13234     return;
13235
13236   if (inst.operands[1].immisalign)
13237     switch (inst.operands[1].imm >> 8)
13238       {
13239       case 64: alignbits = 1; break;
13240       case 128:
13241         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) == 3)
13242           goto bad_alignment;
13243         alignbits = 2;
13244         break;
13245       case 256:
13246         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) == 3)
13247           goto bad_alignment;
13248         alignbits = 3;
13249         break;
13250       default:
13251       bad_alignment:
13252         first_error (_("bad alignment"));
13253         return;
13254       }
13255
13256   inst.instruction |= alignbits << 4;
13257   inst.instruction |= neon_logbits (et.size) << 6;
13258
13259   /* Bits [4:6] of the immediate in a list specifier encode register stride
13260      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
13261      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
13262      up the right value for "type" in a table based on this value and the given
13263      list style, then stick it back.  */
13264   idx = ((inst.operands[0].imm >> 4) & 7)
13265         | (((inst.instruction >> 8) & 3) << 3);
13266
13267   typebits = typetable[idx];
13268   
13269   constraint (typebits == -1, _("bad list type for instruction"));
13270
13271   inst.instruction &= ~0xf00;
13272   inst.instruction |= typebits << 8;
13273 }
13274
13275 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
13276    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
13277    otherwise. The variable arguments are a list of pairs of legal (size, align)
13278    values, terminated with -1.  */
13279
13280 static int
13281 neon_alignment_bit (int size, int align, int *do_align, ...)
13282 {
13283   va_list ap;
13284   int result = FAIL, thissize, thisalign;
13285     
13286   if (!inst.operands[1].immisalign)
13287     {
13288       *do_align = 0;
13289       return SUCCESS;
13290     }
13291       
13292   va_start (ap, do_align);
13293
13294   do
13295     {
13296       thissize = va_arg (ap, int);
13297       if (thissize == -1)
13298         break;
13299       thisalign = va_arg (ap, int);
13300
13301       if (size == thissize && align == thisalign)
13302         result = SUCCESS;
13303     }
13304   while (result != SUCCESS);
13305
13306   va_end (ap);
13307
13308   if (result == SUCCESS)
13309     *do_align = 1;
13310   else
13311     first_error (_("unsupported alignment for instruction"));
13312     
13313   return result;
13314 }
13315
13316 static void
13317 do_neon_ld_st_lane (void)
13318 {
13319   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
13320   int align_good, do_align = 0;
13321   int logsize = neon_logbits (et.size);
13322   int align = inst.operands[1].imm >> 8;
13323   int n = (inst.instruction >> 8) & 3;
13324   int max_el = 64 / et.size;
13325   
13326   if (et.type == NT_invtype)
13327     return;
13328   
13329   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
13330               _("bad list length"));
13331   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
13332               _("scalar index out of range"));
13333   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
13334               && et.size == 8,
13335               _("stride of 2 unavailable when element size is 8"));
13336   
13337   switch (n)
13338     {
13339     case 0:  /* VLD1 / VST1.  */
13340       align_good = neon_alignment_bit (et.size, align, &do_align, 16, 16,
13341                                        32, 32, -1);
13342       if (align_good == FAIL)
13343         return;
13344       if (do_align)
13345         {
13346           unsigned alignbits = 0;
13347           switch (et.size)
13348             {
13349             case 16: alignbits = 0x1; break;
13350             case 32: alignbits = 0x3; break;
13351             default: ;
13352             }
13353           inst.instruction |= alignbits << 4;
13354         }
13355       break;
13356
13357     case 1:  /* VLD2 / VST2.  */
13358       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 16, 16, 32,
13359                                        32, 64, -1);
13360       if (align_good == FAIL)
13361         return;
13362       if (do_align)
13363         inst.instruction |= 1 << 4;
13364       break;
13365
13366     case 2:  /* VLD3 / VST3.  */
13367       constraint (inst.operands[1].immisalign,
13368                   _("can't use alignment with this instruction"));
13369       break;
13370
13371     case 3:  /* VLD4 / VST4.  */
13372       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
13373                                        16, 64, 32, 64, 32, 128, -1);
13374       if (align_good == FAIL)
13375         return;
13376       if (do_align)
13377         {
13378           unsigned alignbits = 0;
13379           switch (et.size)
13380             {
13381             case 8:  alignbits = 0x1; break;
13382             case 16: alignbits = 0x1; break;
13383             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
13384             default: ;
13385             }
13386           inst.instruction |= alignbits << 4;
13387         }
13388       break;
13389
13390     default: ;
13391     }
13392
13393   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
13394   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
13395     inst.instruction |= 1 << (4 + logsize);
13396       
13397   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
13398   inst.instruction |= logsize << 10;
13399 }
13400
13401 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
13402
13403 static void
13404 do_neon_ld_dup (void)
13405 {
13406   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
13407   int align_good, do_align = 0;
13408
13409   if (et.type == NT_invtype)
13410     return;
13411
13412   switch ((inst.instruction >> 8) & 3)
13413     {
13414     case 0:  /* VLD1.  */
13415       assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
13416       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
13417                                        &do_align, 16, 16, 32, 32, -1);
13418       if (align_good == FAIL)
13419         return;
13420       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
13421         {
13422         case 1: break;
13423         case 2: inst.instruction |= 1 << 5; break;
13424         default: first_error (_("bad list length")); return;
13425         }
13426       inst.instruction |= neon_logbits (et.size) << 6;
13427       break;
13428
13429     case 1:  /* VLD2.  */
13430       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
13431                                        &do_align, 8, 16, 16, 32, 32, 64, -1);
13432       if (align_good == FAIL)
13433         return;
13434       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
13435                   _("bad list length"));
13436       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
13437         inst.instruction |= 1 << 5;
13438       inst.instruction |= neon_logbits (et.size) << 6;
13439       break;
13440
13441     case 2:  /* VLD3.  */
13442       constraint (inst.operands[1].immisalign,
13443                   _("can't use alignment with this instruction"));
13444       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
13445                   _("bad list length"));
13446       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
13447         inst.instruction |= 1 << 5;
13448       inst.instruction |= neon_logbits (et.size) << 6;
13449       break;
13450
13451     case 3:  /* VLD4.  */
13452       {
13453         int align = inst.operands[1].imm >> 8;
13454         align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
13455                                          16, 64, 32, 64, 32, 128, -1);
13456         if (align_good == FAIL)
13457           return;
13458         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
13459                     _("bad list length"));
13460         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
13461           inst.instruction |= 1 << 5;
13462         if (et.size == 32 && align == 128)
13463           inst.instruction |= 0x3 << 6;
13464         else
13465           inst.instruction |= neon_logbits (et.size) << 6;
13466       }
13467       break;
13468
13469     default: ;
13470     }
13471
13472   inst.instruction |= do_align << 4;
13473 }
13474
13475 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
13476    apart from bits [11:4].  */
13477
13478 static void
13479 do_neon_ldx_stx (void)
13480 {
13481   switch (NEON_LANE (inst.operands[0].imm))
13482     {
13483     case NEON_INTERLEAVE_LANES:
13484       inst.instruction = NEON_ENC_INTERLV (inst.instruction);
13485       do_neon_ld_st_interleave ();
13486       break;
13487     
13488     case NEON_ALL_LANES:
13489       inst.instruction = NEON_ENC_DUP (inst.instruction);
13490       do_neon_ld_dup ();
13491       break;
13492     
13493     default:
13494       inst.instruction = NEON_ENC_LANE (inst.instruction);
13495       do_neon_ld_st_lane ();
13496     }
13497
13498   /* L bit comes from bit mask.  */
13499   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
13500   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
13501   inst.instruction |= inst.operands[1].reg << 16;
13502   
13503   if (inst.operands[1].postind)
13504     {
13505       int postreg = inst.operands[1].imm & 0xf;
13506       constraint (!inst.operands[1].immisreg,
13507                   _("post-index must be a register"));
13508       constraint (postreg == 0xd || postreg == 0xf,
13509                   _("bad register for post-index"));
13510       inst.instruction |= postreg;
13511     }
13512   else if (inst.operands[1].writeback)
13513     {
13514       inst.instruction |= 0xd;
13515     }
13516   else
13517     inst.instruction |= 0xf; 
13518   
13519   if (thumb_mode)
13520     inst.instruction |= 0xf9000000;
13521   else
13522     inst.instruction |= 0xf4000000;
13523 }
13524
13525 \f
13526 /* Overall per-instruction processing.  */
13527
13528 /* We need to be able to fix up arbitrary expressions in some statements.
13529    This is so that we can handle symbols that are an arbitrary distance from
13530    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
13531    which returns part of an address in a form which will be valid for
13532    a data instruction.  We do this by pushing the expression into a symbol
13533    in the expr_section, and creating a fix for that.  */
13534
13535 static void
13536 fix_new_arm (fragS *       frag,
13537              int           where,
13538              short int     size,
13539              expressionS * exp,
13540              int           pc_rel,
13541              int           reloc)
13542 {
13543   fixS *           new_fix;
13544
13545   switch (exp->X_op)
13546     {
13547     case O_constant:
13548     case O_symbol:
13549     case O_add:
13550     case O_subtract:
13551       new_fix = fix_new_exp (frag, where, size, exp, pc_rel, reloc);
13552       break;
13553
13554     default:
13555       new_fix = fix_new (frag, where, size, make_expr_symbol (exp), 0,
13556                          pc_rel, reloc);
13557       break;
13558     }
13559
13560   /* Mark whether the fix is to a THUMB instruction, or an ARM
13561      instruction.  */
13562   new_fix->tc_fix_data = thumb_mode;
13563 }
13564
13565 /* Create a frg for an instruction requiring relaxation.  */
13566 static void
13567 output_relax_insn (void)
13568 {
13569   char * to;
13570   symbolS *sym;
13571   int offset;
13572
13573 #ifdef OBJ_ELF
13574   /* The size of the instruction is unknown, so tie the debug info to the
13575      start of the instruction.  */
13576   dwarf2_emit_insn (0);
13577 #endif
13578
13579   switch (inst.reloc.exp.X_op)
13580     {
13581     case O_symbol:
13582       sym = inst.reloc.exp.X_add_symbol;
13583       offset = inst.reloc.exp.X_add_number;
13584       break;
13585     case O_constant:
13586       sym = NULL;
13587       offset = inst.reloc.exp.X_add_number;
13588       break;
13589     default:
13590       sym = make_expr_symbol (&inst.reloc.exp);
13591       offset = 0;
13592       break;
13593   }
13594   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
13595                  inst.relax, sym, offset, NULL/*offset, opcode*/);
13596   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
13597 }
13598
13599 /* Write a 32-bit thumb instruction to buf.  */
13600 static void
13601 put_thumb32_insn (char * buf, unsigned long insn)
13602 {
13603   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
13604   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
13605 }
13606
13607 static void
13608 output_inst (const char * str)
13609 {
13610   char * to = NULL;
13611
13612   if (inst.error)
13613     {
13614       as_bad ("%s -- `%s'", inst.error, str);
13615       return;
13616     }
13617   if (inst.relax) {
13618       output_relax_insn();
13619       return;
13620   }
13621   if (inst.size == 0)
13622     return;
13623
13624   to = frag_more (inst.size);
13625
13626   if (thumb_mode && (inst.size > THUMB_SIZE))
13627     {
13628       assert (inst.size == (2 * THUMB_SIZE));
13629       put_thumb32_insn (to, inst.instruction);
13630     }
13631   else if (inst.size > INSN_SIZE)
13632     {
13633       assert (inst.size == (2 * INSN_SIZE));
13634       md_number_to_chars (to, inst.instruction, INSN_SIZE);
13635       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
13636     }
13637   else
13638     md_number_to_chars (to, inst.instruction, inst.size);
13639
13640   if (inst.reloc.type != BFD_RELOC_UNUSED)
13641     fix_new_arm (frag_now, to - frag_now->fr_literal,
13642                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
13643                  inst.reloc.type);
13644
13645 #ifdef OBJ_ELF
13646   dwarf2_emit_insn (inst.size);
13647 #endif
13648 }
13649
13650 /* Tag values used in struct asm_opcode's tag field.  */
13651 enum opcode_tag
13652 {
13653   OT_unconditional,     /* Instruction cannot be conditionalized.
13654                            The ARM condition field is still 0xE.  */
13655   OT_unconditionalF,    /* Instruction cannot be conditionalized
13656                            and carries 0xF in its ARM condition field.  */
13657   OT_csuffix,           /* Instruction takes a conditional suffix.  */
13658   OT_csuffixF,          /* Some forms of the instruction take a conditional
13659                            suffix, others place 0xF where the condition field
13660                            would be.  */
13661   OT_cinfix3,           /* Instruction takes a conditional infix,
13662                            beginning at character index 3.  (In
13663                            unified mode, it becomes a suffix.)  */
13664   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
13665                            character index 3, even in unified mode.  Used for
13666                            legacy instructions where suffix and infix forms
13667                            may be ambiguous.  */
13668   OT_csuf_or_in3,       /* Instruction takes either a conditional
13669                            suffix or an infix at character index 3.  */
13670   OT_odd_infix_unc,     /* This is the unconditional variant of an
13671                            instruction that takes a conditional infix
13672                            at an unusual position.  In unified mode,
13673                            this variant will accept a suffix.  */
13674   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
13675                            are the conditional variants of instructions that
13676                            take conditional infixes in unusual positions.
13677                            The infix appears at character index
13678                            (tag - OT_odd_infix_0).  These are not accepted
13679                            in unified mode.  */
13680 };
13681
13682 /* Subroutine of md_assemble, responsible for looking up the primary
13683    opcode from the mnemonic the user wrote.  STR points to the
13684    beginning of the mnemonic.
13685
13686    This is not simply a hash table lookup, because of conditional
13687    variants.  Most instructions have conditional variants, which are
13688    expressed with a _conditional affix_ to the mnemonic.  If we were
13689    to encode each conditional variant as a literal string in the opcode
13690    table, it would have approximately 20,000 entries.
13691
13692    Most mnemonics take this affix as a suffix, and in unified syntax,
13693    'most' is upgraded to 'all'.  However, in the divided syntax, some
13694    instructions take the affix as an infix, notably the s-variants of
13695    the arithmetic instructions.  Of those instructions, all but six
13696    have the infix appear after the third character of the mnemonic.
13697
13698    Accordingly, the algorithm for looking up primary opcodes given
13699    an identifier is:
13700
13701    1. Look up the identifier in the opcode table.
13702       If we find a match, go to step U.
13703
13704    2. Look up the last two characters of the identifier in the
13705       conditions table.  If we find a match, look up the first N-2
13706       characters of the identifier in the opcode table.  If we
13707       find a match, go to step CE.
13708
13709    3. Look up the fourth and fifth characters of the identifier in
13710       the conditions table.  If we find a match, extract those
13711       characters from the identifier, and look up the remaining
13712       characters in the opcode table.  If we find a match, go
13713       to step CM.
13714
13715    4. Fail.
13716
13717    U. Examine the tag field of the opcode structure, in case this is
13718       one of the six instructions with its conditional infix in an
13719       unusual place.  If it is, the tag tells us where to find the
13720       infix; look it up in the conditions table and set inst.cond
13721       accordingly.  Otherwise, this is an unconditional instruction.
13722       Again set inst.cond accordingly.  Return the opcode structure.
13723
13724   CE. Examine the tag field to make sure this is an instruction that
13725       should receive a conditional suffix.  If it is not, fail.
13726       Otherwise, set inst.cond from the suffix we already looked up,
13727       and return the opcode structure.
13728
13729   CM. Examine the tag field to make sure this is an instruction that
13730       should receive a conditional infix after the third character.
13731       If it is not, fail.  Otherwise, undo the edits to the current
13732       line of input and proceed as for case CE.  */
13733
13734 static const struct asm_opcode *
13735 opcode_lookup (char **str)
13736 {
13737   char *end, *base;
13738   char *affix;
13739   const struct asm_opcode *opcode;
13740   const struct asm_cond *cond;
13741   char save[2];
13742   bfd_boolean neon_supported;
13743   
13744   neon_supported = ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1);
13745
13746   /* Scan up to the end of the mnemonic, which must end in white space,
13747      '.' (in unified mode, or for Neon instructions), or end of string.  */
13748   for (base = end = *str; *end != '\0'; end++)
13749     if (*end == ' ' || ((unified_syntax || neon_supported) && *end == '.'))
13750       break;
13751
13752   if (end == base)
13753     return 0;
13754
13755   /* Handle a possible width suffix and/or Neon type suffix.  */
13756   if (end[0] == '.')
13757     {
13758       int offset = 2;
13759       
13760       /* The .w and .n suffixes are only valid if the unified syntax is in
13761          use.  */
13762       if (unified_syntax && end[1] == 'w')
13763         inst.size_req = 4;
13764       else if (unified_syntax && end[1] == 'n')
13765         inst.size_req = 2;
13766       else
13767         offset = 0;
13768
13769       inst.vectype.elems = 0;
13770
13771       *str = end + offset;
13772
13773       if (end[offset] == '.')      
13774         {
13775           /* See if we have a Neon type suffix (possible in either unified or
13776              non-unified ARM syntax mode).  */
13777           if (parse_neon_type (&inst.vectype, str) == FAIL)
13778             return 0;
13779         }
13780       else if (end[offset] != '\0' && end[offset] != ' ')
13781         return 0;
13782     }
13783   else
13784     *str = end;
13785
13786   /* Look for unaffixed or special-case affixed mnemonic.  */
13787   opcode = hash_find_n (arm_ops_hsh, base, end - base);
13788   if (opcode)
13789     {
13790       /* step U */
13791       if (opcode->tag < OT_odd_infix_0)
13792         {
13793           inst.cond = COND_ALWAYS;
13794           return opcode;
13795         }
13796
13797       if (unified_syntax)
13798         as_warn (_("conditional infixes are deprecated in unified syntax"));
13799       affix = base + (opcode->tag - OT_odd_infix_0);
13800       cond = hash_find_n (arm_cond_hsh, affix, 2);
13801       assert (cond);
13802
13803       inst.cond = cond->value;
13804       return opcode;
13805     }
13806
13807   /* Cannot have a conditional suffix on a mnemonic of less than two
13808      characters.  */
13809   if (end - base < 3)
13810     return 0;
13811
13812   /* Look for suffixed mnemonic.  */
13813   affix = end - 2;
13814   cond = hash_find_n (arm_cond_hsh, affix, 2);
13815   opcode = hash_find_n (arm_ops_hsh, base, affix - base);
13816   if (opcode && cond)
13817     {
13818       /* step CE */
13819       switch (opcode->tag)
13820         {
13821         case OT_cinfix3_legacy:
13822           /* Ignore conditional suffixes matched on infix only mnemonics.  */
13823           break;
13824
13825         case OT_cinfix3:
13826         case OT_odd_infix_unc:
13827           if (!unified_syntax)
13828             return 0;
13829           /* else fall through */
13830
13831         case OT_csuffix:
13832         case OT_csuffixF:
13833         case OT_csuf_or_in3:
13834           inst.cond = cond->value;
13835           return opcode;
13836
13837         case OT_unconditional:
13838         case OT_unconditionalF:
13839           if (thumb_mode)
13840             {
13841               inst.cond = cond->value;
13842             }
13843           else
13844             {
13845               /* delayed diagnostic */
13846               inst.error = BAD_COND;
13847               inst.cond = COND_ALWAYS;
13848             }
13849           return opcode;
13850
13851         default:
13852           return 0;
13853         }
13854     }
13855
13856   /* Cannot have a usual-position infix on a mnemonic of less than
13857      six characters (five would be a suffix).  */
13858   if (end - base < 6)
13859     return 0;
13860
13861   /* Look for infixed mnemonic in the usual position.  */
13862   affix = base + 3;
13863   cond = hash_find_n (arm_cond_hsh, affix, 2);
13864   if (!cond)
13865     return 0;
13866
13867   memcpy (save, affix, 2);
13868   memmove (affix, affix + 2, (end - affix) - 2);
13869   opcode = hash_find_n (arm_ops_hsh, base, (end - base) - 2);
13870   memmove (affix + 2, affix, (end - affix) - 2);
13871   memcpy (affix, save, 2);
13872
13873   if (opcode && (opcode->tag == OT_cinfix3 || opcode->tag == OT_csuf_or_in3
13874                  || opcode->tag == OT_cinfix3_legacy))
13875     {
13876       /* step CM */
13877       if (unified_syntax && opcode->tag == OT_cinfix3)
13878         as_warn (_("conditional infixes are deprecated in unified syntax"));
13879
13880       inst.cond = cond->value;
13881       return opcode;
13882     }
13883
13884   return 0;
13885 }
13886
13887 void
13888 md_assemble (char *str)
13889 {
13890   char *p = str;
13891   const struct asm_opcode * opcode;
13892
13893   /* Align the previous label if needed.  */
13894   if (last_label_seen != NULL)
13895     {
13896       symbol_set_frag (last_label_seen, frag_now);
13897       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
13898       S_SET_SEGMENT (last_label_seen, now_seg);
13899     }
13900
13901   memset (&inst, '\0', sizeof (inst));
13902   inst.reloc.type = BFD_RELOC_UNUSED;
13903
13904   opcode = opcode_lookup (&p);
13905   if (!opcode)
13906     {
13907       /* It wasn't an instruction, but it might be a register alias of
13908          the form alias .req reg, or a Neon .dn/.qn directive.  */
13909       if (!create_register_alias (str, p)
13910           && !create_neon_reg_alias (str, p))
13911         as_bad (_("bad instruction `%s'"), str);
13912
13913       return;
13914     }
13915
13916   /* The value which unconditional instructions should have in place of the
13917      condition field.  */
13918   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
13919
13920   if (thumb_mode)
13921     {
13922       arm_feature_set variant;
13923
13924       variant = cpu_variant;
13925       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
13926       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
13927         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
13928       /* Check that this instruction is supported for this CPU.  */
13929       if (!opcode->tvariant
13930           || (thumb_mode == 1
13931               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
13932         {
13933           as_bad (_("selected processor does not support `%s'"), str);
13934           return;
13935         }
13936       if (inst.cond != COND_ALWAYS && !unified_syntax
13937           && opcode->tencode != do_t_branch)
13938         {
13939           as_bad (_("Thumb does not support conditional execution"));
13940           return;
13941         }
13942
13943       /* Check conditional suffixes.  */
13944       if (current_it_mask)
13945         {
13946           int cond;
13947           cond = current_cc ^ ((current_it_mask >> 4) & 1) ^ 1;
13948           current_it_mask <<= 1;
13949           current_it_mask &= 0x1f;
13950           /* The BKPT instruction is unconditional even in an IT block.  */
13951           if (!inst.error
13952               && cond != inst.cond && opcode->tencode != do_t_bkpt)
13953             {
13954               as_bad (_("incorrect condition in IT block"));
13955               return;
13956             }
13957         }
13958       else if (inst.cond != COND_ALWAYS && opcode->tencode != do_t_branch)
13959         {
13960           as_bad (_("thumb conditional instrunction not in IT block"));
13961           return;
13962         }
13963
13964       mapping_state (MAP_THUMB);
13965       inst.instruction = opcode->tvalue;
13966
13967       if (!parse_operands (p, opcode->operands))
13968         opcode->tencode ();
13969
13970       /* Clear current_it_mask at the end of an IT block.  */
13971       if (current_it_mask == 0x10)
13972         current_it_mask = 0;
13973
13974       if (!(inst.error || inst.relax))
13975         {
13976           assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
13977           inst.size = (inst.instruction > 0xffff ? 4 : 2);
13978           if (inst.size_req && inst.size_req != inst.size)
13979             {
13980               as_bad (_("cannot honor width suffix -- `%s'"), str);
13981               return;
13982             }
13983         }
13984       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
13985                               *opcode->tvariant);
13986       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
13987          set those bits when Thumb-2 32-bit instuctions are seen.  ie.
13988          anything other than bl/blx.
13989          This is overly pessimistic for relaxable instructions.  */
13990       if ((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
13991           || inst.relax)
13992         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
13993                                 arm_ext_v6t2);
13994     }
13995   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
13996     {
13997       /* Check that this instruction is supported for this CPU.  */
13998       if (!opcode->avariant ||
13999           !ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant))
14000         {
14001           as_bad (_("selected processor does not support `%s'"), str);
14002           return;
14003         }
14004       if (inst.size_req)
14005         {
14006           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
14007           return;
14008         }
14009
14010       mapping_state (MAP_ARM);
14011       inst.instruction = opcode->avalue;
14012       if (opcode->tag == OT_unconditionalF)
14013         inst.instruction |= 0xF << 28;
14014       else
14015         inst.instruction |= inst.cond << 28;
14016       inst.size = INSN_SIZE;
14017       if (!parse_operands (p, opcode->operands))
14018         opcode->aencode ();
14019       /* Arm mode bx is marked as both v4T and v5 because it's still required
14020          on a hypothetical non-thumb v5 core.  */
14021       if (ARM_CPU_HAS_FEATURE (*opcode->avariant, arm_ext_v4t)
14022           || ARM_CPU_HAS_FEATURE (*opcode->avariant, arm_ext_v5))
14023         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
14024       else
14025         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
14026                                 *opcode->avariant);
14027     }
14028   else
14029     {
14030       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
14031                 "-- `%s'"), str);
14032       return;
14033     }
14034   output_inst (str);
14035 }
14036
14037 /* Various frobbings of labels and their addresses.  */
14038
14039 void
14040 arm_start_line_hook (void)
14041 {
14042   last_label_seen = NULL;
14043 }
14044
14045 void
14046 arm_frob_label (symbolS * sym)
14047 {
14048   last_label_seen = sym;
14049
14050   ARM_SET_THUMB (sym, thumb_mode);
14051
14052 #if defined OBJ_COFF || defined OBJ_ELF
14053   ARM_SET_INTERWORK (sym, support_interwork);
14054 #endif
14055
14056   /* Note - do not allow local symbols (.Lxxx) to be labeled
14057      as Thumb functions.  This is because these labels, whilst
14058      they exist inside Thumb code, are not the entry points for
14059      possible ARM->Thumb calls.  Also, these labels can be used
14060      as part of a computed goto or switch statement.  eg gcc
14061      can generate code that looks like this:
14062
14063                 ldr  r2, [pc, .Laaa]
14064                 lsl  r3, r3, #2
14065                 ldr  r2, [r3, r2]
14066                 mov  pc, r2
14067
14068        .Lbbb:  .word .Lxxx
14069        .Lccc:  .word .Lyyy
14070        ..etc...
14071        .Laaa:   .word Lbbb
14072
14073      The first instruction loads the address of the jump table.
14074      The second instruction converts a table index into a byte offset.
14075      The third instruction gets the jump address out of the table.
14076      The fourth instruction performs the jump.
14077
14078      If the address stored at .Laaa is that of a symbol which has the
14079      Thumb_Func bit set, then the linker will arrange for this address
14080      to have the bottom bit set, which in turn would mean that the
14081      address computation performed by the third instruction would end
14082      up with the bottom bit set.  Since the ARM is capable of unaligned
14083      word loads, the instruction would then load the incorrect address
14084      out of the jump table, and chaos would ensue.  */
14085   if (label_is_thumb_function_name
14086       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
14087       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14088     {
14089       /* When the address of a Thumb function is taken the bottom
14090          bit of that address should be set.  This will allow
14091          interworking between Arm and Thumb functions to work
14092          correctly.  */
14093
14094       THUMB_SET_FUNC (sym, 1);
14095
14096       label_is_thumb_function_name = FALSE;
14097     }
14098
14099 #ifdef OBJ_ELF
14100   dwarf2_emit_label (sym);
14101 #endif
14102 }
14103
14104 int
14105 arm_data_in_code (void)
14106 {
14107   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
14108     {
14109       *input_line_pointer = '/';
14110       input_line_pointer += 5;
14111       *input_line_pointer = 0;
14112       return 1;
14113     }
14114
14115   return 0;
14116 }
14117
14118 char *
14119 arm_canonicalize_symbol_name (char * name)
14120 {
14121   int len;
14122
14123   if (thumb_mode && (len = strlen (name)) > 5
14124       && streq (name + len - 5, "/data"))
14125     *(name + len - 5) = 0;
14126
14127   return name;
14128 }
14129 \f
14130 /* Table of all register names defined by default.  The user can
14131    define additional names with .req.  Note that all register names
14132    should appear in both upper and lowercase variants.  Some registers
14133    also have mixed-case names.  */
14134
14135 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
14136 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
14137 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
14138 #define REGSET(p,t) \
14139   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
14140   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
14141   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
14142   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
14143 #define REGSETH(p,t) \
14144   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
14145   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
14146   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
14147   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
14148 #define REGSET2(p,t) \
14149   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
14150   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
14151   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
14152   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
14153
14154 static const struct reg_entry reg_names[] =
14155 {
14156   /* ARM integer registers.  */
14157   REGSET(r, RN), REGSET(R, RN),
14158
14159   /* ATPCS synonyms.  */
14160   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
14161   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
14162   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
14163
14164   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
14165   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
14166   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
14167
14168   /* Well-known aliases.  */
14169   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
14170   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
14171
14172   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
14173   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
14174
14175   /* Coprocessor numbers.  */
14176   REGSET(p, CP), REGSET(P, CP),
14177
14178   /* Coprocessor register numbers.  The "cr" variants are for backward
14179      compatibility.  */
14180   REGSET(c,  CN), REGSET(C, CN),
14181   REGSET(cr, CN), REGSET(CR, CN),
14182
14183   /* FPA registers.  */
14184   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
14185   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
14186
14187   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
14188   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
14189
14190   /* VFP SP registers.  */
14191   REGSET(s,VFS),  REGSET(S,VFS),
14192   REGSETH(s,VFS), REGSETH(S,VFS),
14193
14194   /* VFP DP Registers.  */
14195   REGSET(d,VFD),  REGSET(D,VFD),
14196   /* Extra Neon DP registers.  */
14197   REGSETH(d,VFD), REGSETH(D,VFD),
14198
14199   /* Neon QP registers.  */
14200   REGSET2(q,NQ),  REGSET2(Q,NQ),
14201
14202   /* VFP control registers.  */
14203   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
14204   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
14205
14206   /* Maverick DSP coprocessor registers.  */
14207   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
14208   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
14209
14210   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
14211   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
14212   REGDEF(dspsc,0,DSPSC),
14213
14214   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
14215   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
14216   REGDEF(DSPSC,0,DSPSC),
14217
14218   /* iWMMXt data registers - p0, c0-15.  */
14219   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
14220
14221   /* iWMMXt control registers - p1, c0-3.  */
14222   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
14223   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
14224   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
14225   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
14226
14227   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
14228   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
14229   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
14230   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
14231   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
14232
14233   /* XScale accumulator registers.  */
14234   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
14235 };
14236 #undef REGDEF
14237 #undef REGNUM
14238 #undef REGSET
14239
14240 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
14241    within psr_required_here.  */
14242 static const struct asm_psr psrs[] =
14243 {
14244   /* Backward compatibility notation.  Note that "all" is no longer
14245      truly all possible PSR bits.  */
14246   {"all",  PSR_c | PSR_f},
14247   {"flg",  PSR_f},
14248   {"ctl",  PSR_c},
14249
14250   /* Individual flags.  */
14251   {"f",    PSR_f},
14252   {"c",    PSR_c},
14253   {"x",    PSR_x},
14254   {"s",    PSR_s},
14255   /* Combinations of flags.  */
14256   {"fs",   PSR_f | PSR_s},
14257   {"fx",   PSR_f | PSR_x},
14258   {"fc",   PSR_f | PSR_c},
14259   {"sf",   PSR_s | PSR_f},
14260   {"sx",   PSR_s | PSR_x},
14261   {"sc",   PSR_s | PSR_c},
14262   {"xf",   PSR_x | PSR_f},
14263   {"xs",   PSR_x | PSR_s},
14264   {"xc",   PSR_x | PSR_c},
14265   {"cf",   PSR_c | PSR_f},
14266   {"cs",   PSR_c | PSR_s},
14267   {"cx",   PSR_c | PSR_x},
14268   {"fsx",  PSR_f | PSR_s | PSR_x},
14269   {"fsc",  PSR_f | PSR_s | PSR_c},
14270   {"fxs",  PSR_f | PSR_x | PSR_s},
14271   {"fxc",  PSR_f | PSR_x | PSR_c},
14272   {"fcs",  PSR_f | PSR_c | PSR_s},
14273   {"fcx",  PSR_f | PSR_c | PSR_x},
14274   {"sfx",  PSR_s | PSR_f | PSR_x},
14275   {"sfc",  PSR_s | PSR_f | PSR_c},
14276   {"sxf",  PSR_s | PSR_x | PSR_f},
14277   {"sxc",  PSR_s | PSR_x | PSR_c},
14278   {"scf",  PSR_s | PSR_c | PSR_f},
14279   {"scx",  PSR_s | PSR_c | PSR_x},
14280   {"xfs",  PSR_x | PSR_f | PSR_s},
14281   {"xfc",  PSR_x | PSR_f | PSR_c},
14282   {"xsf",  PSR_x | PSR_s | PSR_f},
14283   {"xsc",  PSR_x | PSR_s | PSR_c},
14284   {"xcf",  PSR_x | PSR_c | PSR_f},
14285   {"xcs",  PSR_x | PSR_c | PSR_s},
14286   {"cfs",  PSR_c | PSR_f | PSR_s},
14287   {"cfx",  PSR_c | PSR_f | PSR_x},
14288   {"csf",  PSR_c | PSR_s | PSR_f},
14289   {"csx",  PSR_c | PSR_s | PSR_x},
14290   {"cxf",  PSR_c | PSR_x | PSR_f},
14291   {"cxs",  PSR_c | PSR_x | PSR_s},
14292   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
14293   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
14294   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
14295   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
14296   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
14297   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
14298   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
14299   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
14300   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
14301   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
14302   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
14303   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
14304   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
14305   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
14306   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
14307   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
14308   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
14309   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
14310   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
14311   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
14312   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
14313   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
14314   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
14315   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
14316 };
14317
14318 /* Table of V7M psr names.  */
14319 static const struct asm_psr v7m_psrs[] =
14320 {
14321   {"apsr",      0 },
14322   {"iapsr",     1 },
14323   {"eapsr",     2 },
14324   {"psr",       3 },
14325   {"ipsr",      5 },
14326   {"epsr",      6 },
14327   {"iepsr",     7 },
14328   {"msp",       8 },
14329   {"psp",       9 },
14330   {"primask",   16},
14331   {"basepri",   17},
14332   {"basepri_max", 18},
14333   {"faultmask", 19},
14334   {"control",   20}
14335 };
14336
14337 /* Table of all shift-in-operand names.  */
14338 static const struct asm_shift_name shift_names [] =
14339 {
14340   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
14341   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
14342   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
14343   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
14344   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
14345   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
14346 };
14347
14348 /* Table of all explicit relocation names.  */
14349 #ifdef OBJ_ELF
14350 static struct reloc_entry reloc_names[] =
14351 {
14352   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
14353   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
14354   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
14355   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
14356   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
14357   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
14358   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
14359   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
14360   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
14361   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
14362   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32}
14363 };
14364 #endif
14365
14366 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
14367 static const struct asm_cond conds[] =
14368 {
14369   {"eq", 0x0},
14370   {"ne", 0x1},
14371   {"cs", 0x2}, {"hs", 0x2},
14372   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
14373   {"mi", 0x4},
14374   {"pl", 0x5},
14375   {"vs", 0x6},
14376   {"vc", 0x7},
14377   {"hi", 0x8},
14378   {"ls", 0x9},
14379   {"ge", 0xa},
14380   {"lt", 0xb},
14381   {"gt", 0xc},
14382   {"le", 0xd},
14383   {"al", 0xe}
14384 };
14385
14386 static struct asm_barrier_opt barrier_opt_names[] =
14387 {
14388   { "sy",   0xf },
14389   { "un",   0x7 },
14390   { "st",   0xe },
14391   { "unst", 0x6 }
14392 };
14393
14394 /* Table of ARM-format instructions.    */
14395
14396 /* Macros for gluing together operand strings.  N.B. In all cases
14397    other than OPS0, the trailing OP_stop comes from default
14398    zero-initialization of the unspecified elements of the array.  */
14399 #define OPS0()            { OP_stop, }
14400 #define OPS1(a)           { OP_##a, }
14401 #define OPS2(a,b)         { OP_##a,OP_##b, }
14402 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
14403 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
14404 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
14405 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
14406
14407 /* These macros abstract out the exact format of the mnemonic table and
14408    save some repeated characters.  */
14409
14410 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
14411 #define TxCE(mnem, op, top, nops, ops, ae, te) \
14412   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
14413     THUMB_VARIANT, do_##ae, do_##te }
14414
14415 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
14416    a T_MNEM_xyz enumerator.  */
14417 #define TCE(mnem, aop, top, nops, ops, ae, te) \
14418        TxCE(mnem, aop, 0x##top, nops, ops, ae, te)
14419 #define tCE(mnem, aop, top, nops, ops, ae, te) \
14420        TxCE(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
14421
14422 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
14423    infix after the third character.  */
14424 #define TxC3(mnem, op, top, nops, ops, ae, te) \
14425   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
14426     THUMB_VARIANT, do_##ae, do_##te }
14427 #define TC3(mnem, aop, top, nops, ops, ae, te) \
14428        TxC3(mnem, aop, 0x##top, nops, ops, ae, te)
14429 #define tC3(mnem, aop, top, nops, ops, ae, te) \
14430        TxC3(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
14431
14432 /* Mnemonic with a conditional infix in an unusual place.  Each and every variant has to
14433    appear in the condition table.  */
14434 #define TxCM_(m1, m2, m3, op, top, nops, ops, ae, te)   \
14435   { #m1 #m2 #m3, OPS##nops ops, sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
14436     0x##op, top, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##te }
14437
14438 #define TxCM(m1, m2, op, top, nops, ops, ae, te)        \
14439   TxCM_(m1,   , m2, op, top, nops, ops, ae, te),        \
14440   TxCM_(m1, eq, m2, op, top, nops, ops, ae, te),        \
14441   TxCM_(m1, ne, m2, op, top, nops, ops, ae, te),        \
14442   TxCM_(m1, cs, m2, op, top, nops, ops, ae, te),        \
14443   TxCM_(m1, hs, m2, op, top, nops, ops, ae, te),        \
14444   TxCM_(m1, cc, m2, op, top, nops, ops, ae, te),        \
14445   TxCM_(m1, ul, m2, op, top, nops, ops, ae, te),        \
14446   TxCM_(m1, lo, m2, op, top, nops, ops, ae, te),        \
14447   TxCM_(m1, mi, m2, op, top, nops, ops, ae, te),        \
14448   TxCM_(m1, pl, m2, op, top, nops, ops, ae, te),        \
14449   TxCM_(m1, vs, m2, op, top, nops, ops, ae, te),        \
14450   TxCM_(m1, vc, m2, op, top, nops, ops, ae, te),        \
14451   TxCM_(m1, hi, m2, op, top, nops, ops, ae, te),        \
14452   TxCM_(m1, ls, m2, op, top, nops, ops, ae, te),        \
14453   TxCM_(m1, ge, m2, op, top, nops, ops, ae, te),        \
14454   TxCM_(m1, lt, m2, op, top, nops, ops, ae, te),        \
14455   TxCM_(m1, gt, m2, op, top, nops, ops, ae, te),        \
14456   TxCM_(m1, le, m2, op, top, nops, ops, ae, te),        \
14457   TxCM_(m1, al, m2, op, top, nops, ops, ae, te)
14458
14459 #define TCM(m1,m2, aop, top, nops, ops, ae, te)         \
14460        TxCM(m1,m2, aop, 0x##top, nops, ops, ae, te)
14461 #define tCM(m1,m2, aop, top, nops, ops, ae, te)                 \
14462        TxCM(m1,m2, aop, T_MNEM_##top, nops, ops, ae, te)
14463
14464 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
14465    field is still 0xE.  Many of the Thumb variants can be executed
14466    conditionally, so this is checked separately.  */
14467 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
14468   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
14469     THUMB_VARIANT, do_##ae, do_##te }
14470
14471 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
14472    condition code field.  */
14473 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
14474   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
14475     THUMB_VARIANT, do_##ae, do_##te }
14476
14477 /* ARM-only variants of all the above.  */
14478 #define CE(mnem,  op, nops, ops, ae)    \
14479   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
14480
14481 #define C3(mnem, op, nops, ops, ae)     \
14482   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
14483
14484 /* Legacy mnemonics that always have conditional infix after the third
14485    character.  */
14486 #define CL(mnem, op, nops, ops, ae)     \
14487   { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
14488     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
14489
14490 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
14491 #define cCE(mnem,  op, nops, ops, ae)   \
14492   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
14493
14494 /* Legacy coprocessor instructions where conditional infix and conditional
14495    suffix are ambiguous.  For consistency this includes all FPA instructions,
14496    not just the potentially ambiguous ones.  */
14497 #define cCL(mnem, op, nops, ops, ae)    \
14498   { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
14499     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
14500
14501 /* Coprocessor, takes either a suffix or a position-3 infix
14502    (for an FPA corner case). */
14503 #define C3E(mnem, op, nops, ops, ae) \
14504   { #mnem, OPS##nops ops, OT_csuf_or_in3, \
14505     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
14506
14507 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
14508   { #m1 #m2 #m3, OPS##nops ops, \
14509     sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
14510     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
14511
14512 #define CM(m1, m2, op, nops, ops, ae)   \
14513   xCM_(m1,   , m2, op, nops, ops, ae),  \
14514   xCM_(m1, eq, m2, op, nops, ops, ae),  \
14515   xCM_(m1, ne, m2, op, nops, ops, ae),  \
14516   xCM_(m1, cs, m2, op, nops, ops, ae),  \
14517   xCM_(m1, hs, m2, op, nops, ops, ae),  \
14518   xCM_(m1, cc, m2, op, nops, ops, ae),  \
14519   xCM_(m1, ul, m2, op, nops, ops, ae),  \
14520   xCM_(m1, lo, m2, op, nops, ops, ae),  \
14521   xCM_(m1, mi, m2, op, nops, ops, ae),  \
14522   xCM_(m1, pl, m2, op, nops, ops, ae),  \
14523   xCM_(m1, vs, m2, op, nops, ops, ae),  \
14524   xCM_(m1, vc, m2, op, nops, ops, ae),  \
14525   xCM_(m1, hi, m2, op, nops, ops, ae),  \
14526   xCM_(m1, ls, m2, op, nops, ops, ae),  \
14527   xCM_(m1, ge, m2, op, nops, ops, ae),  \
14528   xCM_(m1, lt, m2, op, nops, ops, ae),  \
14529   xCM_(m1, gt, m2, op, nops, ops, ae),  \
14530   xCM_(m1, le, m2, op, nops, ops, ae),  \
14531   xCM_(m1, al, m2, op, nops, ops, ae)
14532
14533 #define UE(mnem, op, nops, ops, ae)     \
14534   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
14535
14536 #define UF(mnem, op, nops, ops, ae)     \
14537   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
14538
14539 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
14540    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
14541    use the same encoding function for each.  */
14542 #define NUF(mnem, op, nops, ops, enc)                                   \
14543   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
14544     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
14545
14546 /* Neon data processing, version which indirects through neon_enc_tab for
14547    the various overloaded versions of opcodes.  */
14548 #define nUF(mnem, op, nops, ops, enc)                                   \
14549   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM_##op, N_MNEM_##op,  \
14550     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
14551
14552 /* Neon insn with conditional suffix for the ARM version, non-overloaded
14553    version.  */
14554 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
14555   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
14556     THUMB_VARIANT, do_##enc, do_##enc }
14557
14558 #define NCE(mnem, op, nops, ops, enc)                                   \
14559   NCE_tag(mnem, op, nops, ops, enc, OT_csuffix)
14560
14561 #define NCEF(mnem, op, nops, ops, enc)                                  \
14562   NCE_tag(mnem, op, nops, ops, enc, OT_csuffixF)
14563
14564 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
14565 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
14566   { #mnem, OPS##nops ops, tag, N_MNEM_##op, N_MNEM_##op,                \
14567     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
14568
14569 #define nCE(mnem, op, nops, ops, enc)                                   \
14570   nCE_tag(mnem, op, nops, ops, enc, OT_csuffix)
14571
14572 #define nCEF(mnem, op, nops, ops, enc)                                  \
14573   nCE_tag(mnem, op, nops, ops, enc, OT_csuffixF)
14574
14575 #define do_0 0
14576
14577 /* Thumb-only, unconditional.  */
14578 #define UT(mnem,  op, nops, ops, te) TUE(mnem,  0, op, nops, ops, 0, te)
14579
14580 static const struct asm_opcode insns[] =
14581 {
14582 #define ARM_VARIANT &arm_ext_v1 /* Core ARM Instructions.  */
14583 #define THUMB_VARIANT &arm_ext_v4t
14584  tCE(and,       0000000, and,      3, (RR, oRR, SH), arit, t_arit3c),
14585  tC3(ands,      0100000, ands,     3, (RR, oRR, SH), arit, t_arit3c),
14586  tCE(eor,       0200000, eor,      3, (RR, oRR, SH), arit, t_arit3c),
14587  tC3(eors,      0300000, eors,     3, (RR, oRR, SH), arit, t_arit3c),
14588  tCE(sub,       0400000, sub,      3, (RR, oRR, SH), arit, t_add_sub),
14589  tC3(subs,      0500000, subs,     3, (RR, oRR, SH), arit, t_add_sub),
14590  tCE(add,       0800000, add,      3, (RR, oRR, SHG), arit, t_add_sub),
14591  tC3(adds,      0900000, adds,     3, (RR, oRR, SHG), arit, t_add_sub),
14592  tCE(adc,       0a00000, adc,      3, (RR, oRR, SH), arit, t_arit3c),
14593  tC3(adcs,      0b00000, adcs,     3, (RR, oRR, SH), arit, t_arit3c),
14594  tCE(sbc,       0c00000, sbc,      3, (RR, oRR, SH), arit, t_arit3),
14595  tC3(sbcs,      0d00000, sbcs,     3, (RR, oRR, SH), arit, t_arit3),
14596  tCE(orr,       1800000, orr,      3, (RR, oRR, SH), arit, t_arit3c),
14597  tC3(orrs,      1900000, orrs,     3, (RR, oRR, SH), arit, t_arit3c),
14598  tCE(bic,       1c00000, bic,      3, (RR, oRR, SH), arit, t_arit3),
14599  tC3(bics,      1d00000, bics,     3, (RR, oRR, SH), arit, t_arit3),
14600
14601  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
14602     for setting PSR flag bits.  They are obsolete in V6 and do not
14603     have Thumb equivalents. */
14604  tCE(tst,       1100000, tst,      2, (RR, SH),      cmp,  t_mvn_tst),
14605  tC3(tsts,      1100000, tst,      2, (RR, SH),      cmp,  t_mvn_tst),
14606   CL(tstp,      110f000,           2, (RR, SH),      cmp),
14607  tCE(cmp,       1500000, cmp,      2, (RR, SH),      cmp,  t_mov_cmp),
14608  tC3(cmps,      1500000, cmp,      2, (RR, SH),      cmp,  t_mov_cmp),
14609   CL(cmpp,      150f000,           2, (RR, SH),      cmp),
14610  tCE(cmn,       1700000, cmn,      2, (RR, SH),      cmp,  t_mvn_tst),
14611  tC3(cmns,      1700000, cmn,      2, (RR, SH),      cmp,  t_mvn_tst),
14612   CL(cmnp,      170f000,           2, (RR, SH),      cmp),
14613
14614  tCE(mov,       1a00000, mov,      2, (RR, SH),      mov,  t_mov_cmp),
14615  tC3(movs,      1b00000, movs,     2, (RR, SH),      mov,  t_mov_cmp),
14616  tCE(mvn,       1e00000, mvn,      2, (RR, SH),      mov,  t_mvn_tst),
14617  tC3(mvns,      1f00000, mvns,     2, (RR, SH),      mov,  t_mvn_tst),
14618
14619  tCE(ldr,       4100000, ldr,      2, (RR, ADDRGLDR),ldst, t_ldst),
14620  tC3(ldrb,      4500000, ldrb,     2, (RR, ADDRGLDR),ldst, t_ldst),
14621  tCE(str,       4000000, str,      2, (RR, ADDRGLDR),ldst, t_ldst),
14622  tC3(strb,      4400000, strb,     2, (RR, ADDRGLDR),ldst, t_ldst),
14623
14624  tCE(stm,       8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
14625  tC3(stmia,     8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
14626  tC3(stmea,     8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
14627  tCE(ldm,       8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
14628  tC3(ldmia,     8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
14629  tC3(ldmfd,     8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
14630
14631  TCE(swi,       f000000, df00,     1, (EXPi),        swi, t_swi),
14632  TCE(svc,       f000000, df00,     1, (EXPi),        swi, t_swi),
14633  tCE(b,         a000000, b,        1, (EXPr),        branch, t_branch),
14634  TCE(bl,        b000000, f000f800, 1, (EXPr),        bl, t_branch23),
14635
14636   /* Pseudo ops.  */
14637  tCE(adr,       28f0000, adr,      2, (RR, EXP),     adr,  t_adr),
14638   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
14639  tCE(nop,       1a00000, nop,      1, (oI255c),      nop,  t_nop),
14640
14641   /* Thumb-compatibility pseudo ops.  */
14642  tCE(lsl,       1a00000, lsl,      3, (RR, oRR, SH), shift, t_shift),
14643  tC3(lsls,      1b00000, lsls,     3, (RR, oRR, SH), shift, t_shift),
14644  tCE(lsr,       1a00020, lsr,      3, (RR, oRR, SH), shift, t_shift),
14645  tC3(lsrs,      1b00020, lsrs,     3, (RR, oRR, SH), shift, t_shift),
14646  tCE(asr,       1a00040, asr,      3, (RR, oRR, SH), shift, t_shift),
14647  tC3(asrs,      1b00040, asrs,     3, (RR, oRR, SH), shift, t_shift),
14648  tCE(ror,       1a00060, ror,      3, (RR, oRR, SH), shift, t_shift),
14649  tC3(rors,      1b00060, rors,     3, (RR, oRR, SH), shift, t_shift),
14650  tCE(neg,       2600000, neg,      2, (RR, RR),      rd_rn, t_neg),
14651  tC3(negs,      2700000, negs,     2, (RR, RR),      rd_rn, t_neg),
14652  tCE(push,      92d0000, push,     1, (REGLST),      push_pop, t_push_pop),
14653  tCE(pop,       8bd0000, pop,      1, (REGLST),      push_pop, t_push_pop),
14654
14655 #undef THUMB_VARIANT
14656 #define THUMB_VARIANT &arm_ext_v6
14657  TCE(cpy,       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
14658
14659  /* V1 instructions with no Thumb analogue prior to V6T2.  */
14660 #undef THUMB_VARIANT
14661 #define THUMB_VARIANT &arm_ext_v6t2
14662  TCE(rsb,       0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
14663  TC3(rsbs,      0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
14664  TCE(teq,       1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
14665  TC3(teqs,      1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
14666   CL(teqp,      130f000,           2, (RR, SH),      cmp),
14667
14668  TC3(ldrt,      4300000, f8500e00, 2, (RR, ADDR),    ldstt, t_ldstt),
14669  TC3(ldrbt,     4700000, f8100e00, 2, (RR, ADDR),    ldstt, t_ldstt),
14670  TC3(strt,      4200000, f8400e00, 2, (RR, ADDR),    ldstt, t_ldstt),
14671  TC3(strbt,     4600000, f8000e00, 2, (RR, ADDR),    ldstt, t_ldstt),
14672
14673  TC3(stmdb,     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
14674  TC3(stmfd,     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
14675
14676  TC3(ldmdb,     9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
14677  TC3(ldmea,     9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
14678
14679  /* V1 instructions with no Thumb analogue at all.  */
14680   CE(rsc,       0e00000,           3, (RR, oRR, SH), arit),
14681   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
14682
14683   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
14684   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
14685   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
14686   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
14687   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
14688   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
14689   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
14690   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
14691
14692 #undef ARM_VARIANT
14693 #define ARM_VARIANT &arm_ext_v2 /* ARM 2 - multiplies.  */
14694 #undef THUMB_VARIANT
14695 #define THUMB_VARIANT &arm_ext_v4t
14696  tCE(mul,       0000090, mul,      3, (RRnpc, RRnpc, oRR), mul, t_mul),
14697  tC3(muls,      0100090, muls,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
14698
14699 #undef THUMB_VARIANT
14700 #define THUMB_VARIANT &arm_ext_v6t2
14701  TCE(mla,       0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
14702   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
14703
14704   /* Generic coprocessor instructions.  */
14705  TCE(cdp,       e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
14706  TCE(ldc,       c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
14707  TC3(ldcl,      c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
14708  TCE(stc,       c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
14709  TC3(stcl,      c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
14710  TCE(mcr,       e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
14711  TCE(mrc,       e100010, ee100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
14712
14713 #undef ARM_VARIANT
14714 #define ARM_VARIANT &arm_ext_v2s /* ARM 3 - swp instructions.  */
14715   CE(swp,       1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
14716   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
14717
14718 #undef ARM_VARIANT
14719 #define ARM_VARIANT &arm_ext_v3 /* ARM 6 Status register instructions.  */
14720  TCE(mrs,       10f0000, f3ef8000, 2, (APSR_RR, RVC_PSR), mrs, t_mrs),
14721  TCE(msr,       120f000, f3808000, 2, (RVC_PSR, RR_EXi), msr, t_msr),
14722
14723 #undef ARM_VARIANT
14724 #define ARM_VARIANT &arm_ext_v3m         /* ARM 7M long multiplies.  */
14725  TCE(smull,     0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
14726   CM(smull,s,   0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
14727  TCE(umull,     0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
14728   CM(umull,s,   0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
14729  TCE(smlal,     0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
14730   CM(smlal,s,   0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
14731  TCE(umlal,     0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
14732   CM(umlal,s,   0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
14733
14734 #undef ARM_VARIANT
14735 #define ARM_VARIANT &arm_ext_v4 /* ARM Architecture 4.  */
14736 #undef THUMB_VARIANT
14737 #define THUMB_VARIANT &arm_ext_v4t
14738  tC3(ldrh,      01000b0, ldrh,     2, (RR, ADDRGLDRS), ldstv4, t_ldst),
14739  tC3(strh,      00000b0, strh,     2, (RR, ADDRGLDRS), ldstv4, t_ldst),
14740  tC3(ldrsh,     01000f0, ldrsh,    2, (RR, ADDRGLDRS), ldstv4, t_ldst),
14741  tC3(ldrsb,     01000d0, ldrsb,    2, (RR, ADDRGLDRS), ldstv4, t_ldst),
14742  tCM(ld,sh,     01000f0, ldrsh,    2, (RR, ADDRGLDRS), ldstv4, t_ldst),
14743  tCM(ld,sb,     01000d0, ldrsb,    2, (RR, ADDRGLDRS), ldstv4, t_ldst),
14744
14745 #undef ARM_VARIANT
14746 #define ARM_VARIANT &arm_ext_v4t_5
14747   /* ARM Architecture 4T.  */
14748   /* Note: bx (and blx) are required on V5, even if the processor does
14749      not support Thumb.  */
14750  TCE(bx,        12fff10, 4700, 1, (RR), bx, t_bx),
14751
14752 #undef ARM_VARIANT
14753 #define ARM_VARIANT &arm_ext_v5 /*  ARM Architecture 5T.         */
14754 #undef THUMB_VARIANT
14755 #define THUMB_VARIANT &arm_ext_v5t
14756   /* Note: blx has 2 variants; the .value coded here is for
14757      BLX(2).  Only this variant has conditional execution.  */
14758  TCE(blx,       12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
14759  TUE(bkpt,      1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
14760
14761 #undef THUMB_VARIANT
14762 #define THUMB_VARIANT &arm_ext_v6t2
14763  TCE(clz,       16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
14764  TUF(ldc2,      c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
14765  TUF(ldc2l,     c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
14766  TUF(stc2,      c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
14767  TUF(stc2l,     c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
14768  TUF(cdp2,      e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
14769  TUF(mcr2,      e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
14770  TUF(mrc2,      e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
14771
14772 #undef ARM_VARIANT
14773 #define ARM_VARIANT &arm_ext_v5exp /*  ARM Architecture 5TExP.  */
14774  TCE(smlabb,    1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
14775  TCE(smlatb,    10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
14776  TCE(smlabt,    10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
14777  TCE(smlatt,    10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
14778
14779  TCE(smlawb,    1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
14780  TCE(smlawt,    12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
14781
14782  TCE(smlalbb,   1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
14783  TCE(smlaltb,   14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
14784  TCE(smlalbt,   14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
14785  TCE(smlaltt,   14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
14786
14787  TCE(smulbb,    1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
14788  TCE(smultb,    16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
14789  TCE(smulbt,    16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
14790  TCE(smultt,    16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
14791
14792  TCE(smulwb,    12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
14793  TCE(smulwt,    12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
14794
14795  TCE(qadd,      1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
14796  TCE(qdadd,     1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
14797  TCE(qsub,      1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
14798  TCE(qdsub,     1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
14799
14800 #undef ARM_VARIANT
14801 #define ARM_VARIANT &arm_ext_v5e /*  ARM Architecture 5TE.  */
14802  TUF(pld,       450f000, f810f000, 1, (ADDR),                pld,  t_pld),
14803  TC3(ldrd,      00000d0, e9500000, 3, (RRnpc, oRRnpc, ADDRGLDRS), ldrd, t_ldstd),
14804  TC3(strd,      00000f0, e9400000, 3, (RRnpc, oRRnpc, ADDRGLDRS), ldrd, t_ldstd),
14805
14806  TCE(mcrr,      c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
14807  TCE(mrrc,      c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
14808
14809 #undef ARM_VARIANT
14810 #define ARM_VARIANT &arm_ext_v5j /*  ARM Architecture 5TEJ.  */
14811  TCE(bxj,       12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
14812
14813 #undef ARM_VARIANT
14814 #define ARM_VARIANT &arm_ext_v6 /*  ARM V6.  */
14815 #undef THUMB_VARIANT
14816 #define THUMB_VARIANT &arm_ext_v6
14817  TUF(cpsie,     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
14818  TUF(cpsid,     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
14819  tCE(rev,       6bf0f30, rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
14820  tCE(rev16,     6bf0fb0, rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
14821  tCE(revsh,     6ff0fb0, revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
14822  tCE(sxth,      6bf0070, sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
14823  tCE(uxth,      6ff0070, uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
14824  tCE(sxtb,      6af0070, sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
14825  tCE(uxtb,      6ef0070, uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
14826  TUF(setend,    1010000, b650,     1, (ENDI),                     setend, t_setend),
14827
14828 #undef THUMB_VARIANT
14829 #define THUMB_VARIANT &arm_ext_v6t2
14830  TCE(ldrex,     1900f9f, e8500f00, 2, (RRnpc, ADDR),              ldrex, t_ldrex),
14831  TUF(mcrr2,     c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
14832  TUF(mrrc2,     c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
14833
14834  TCE(ssat,      6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
14835  TCE(usat,      6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
14836
14837 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
14838 #undef THUMB_VARIANT
14839 #define THUMB_VARIANT &arm_ext_v6_notm
14840  TUF(cps,       1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
14841  TCE(pkhbt,     6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
14842  TCE(pkhtb,     6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
14843  TCE(qadd16,    6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14844  TCE(qadd8,     6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14845  TCE(qaddsubx,  6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14846  TCE(qsub16,    6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14847  TCE(qsub8,     6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14848  TCE(qsubaddx,  6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14849  TCE(sadd16,    6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14850  TCE(sadd8,     6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14851  TCE(saddsubx,  6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14852  TCE(shadd16,   6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14853  TCE(shadd8,    6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14854  TCE(shaddsubx, 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14855  TCE(shsub16,   6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14856  TCE(shsub8,    6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14857  TCE(shsubaddx, 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14858  TCE(ssub16,    6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14859  TCE(ssub8,     6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14860  TCE(ssubaddx,  6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14861  TCE(uadd16,    6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14862  TCE(uadd8,     6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14863  TCE(uaddsubx,  6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14864  TCE(uhadd16,   6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14865  TCE(uhadd8,    6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14866  TCE(uhaddsubx, 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14867  TCE(uhsub16,   6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14868  TCE(uhsub8,    6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14869  TCE(uhsubaddx, 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14870  TCE(uqadd16,   6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14871  TCE(uqadd8,    6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14872  TCE(uqaddsubx, 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14873  TCE(uqsub16,   6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14874  TCE(uqsub8,    6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14875  TCE(uqsubaddx, 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14876  TCE(usub16,    6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14877  TCE(usub8,     6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14878  TCE(usubaddx,  6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14879  TUF(rfeia,     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
14880   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
14881   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
14882  TUF(rfedb,     9100a00, e810c000, 1, (RRw),                       rfe, rfe),
14883  TUF(rfefd,     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
14884   UF(rfefa,     9900a00,           1, (RRw),                       rfe),
14885   UF(rfeea,     8100a00,           1, (RRw),                       rfe),
14886  TUF(rfeed,     9100a00, e810c000, 1, (RRw),                       rfe, rfe),
14887  TCE(sxtah,     6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
14888  TCE(sxtab16,   6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
14889  TCE(sxtab,     6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
14890  TCE(sxtb16,    68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
14891  TCE(uxtah,     6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
14892  TCE(uxtab16,   6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
14893  TCE(uxtab,     6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
14894  TCE(uxtb16,    6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
14895  TCE(sel,       6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
14896  TCE(smlad,     7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
14897  TCE(smladx,    7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
14898  TCE(smlald,    7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
14899  TCE(smlaldx,   7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
14900  TCE(smlsd,     7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
14901  TCE(smlsdx,    7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
14902  TCE(smlsld,    7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
14903  TCE(smlsldx,   7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
14904  TCE(smmla,     7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
14905  TCE(smmlar,    7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
14906  TCE(smmls,     75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
14907  TCE(smmlsr,    75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
14908  TCE(smmul,     750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
14909  TCE(smmulr,    750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
14910  TCE(smuad,     700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
14911  TCE(smuadx,    700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
14912  TCE(smusd,     700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
14913  TCE(smusdx,    700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
14914  TUF(srsia,     8cd0500, e980c000, 1, (I31w),                      srs,  srs),
14915   UF(srsib,     9cd0500,           1, (I31w),                      srs),
14916   UF(srsda,     84d0500,           1, (I31w),                      srs),
14917  TUF(srsdb,     94d0500, e800c000, 1, (I31w),                      srs,  srs),
14918  TCE(ssat16,    6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
14919  TCE(strex,     1800f90, e8400000, 3, (RRnpc, RRnpc, ADDR),        strex,  t_strex),
14920  TCE(umaal,     0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
14921  TCE(usad8,     780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
14922  TCE(usada8,    7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
14923  TCE(usat16,    6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
14924
14925 #undef ARM_VARIANT
14926 #define ARM_VARIANT &arm_ext_v6k
14927 #undef THUMB_VARIANT
14928 #define THUMB_VARIANT &arm_ext_v6k
14929  tCE(yield,     320f001, yield,    0, (), noargs, t_hint),
14930  tCE(wfe,       320f002, wfe,      0, (), noargs, t_hint),
14931  tCE(wfi,       320f003, wfi,      0, (), noargs, t_hint),
14932  tCE(sev,       320f004, sev,      0, (), noargs, t_hint),
14933
14934 #undef THUMB_VARIANT
14935 #define THUMB_VARIANT &arm_ext_v6_notm
14936  TCE(ldrexd,    1b00f9f, e8d0007f, 3, (RRnpc, oRRnpc, RRnpcb),        ldrexd, t_ldrexd),
14937  TCE(strexd,    1a00f90, e8c00070, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb), strexd, t_strexd),
14938
14939 #undef THUMB_VARIANT
14940 #define THUMB_VARIANT &arm_ext_v6t2
14941  TCE(ldrexb,    1d00f9f, e8d00f4f, 2, (RRnpc, RRnpcb),                rd_rn,  rd_rn),
14942  TCE(ldrexh,    1f00f9f, e8d00f5f, 2, (RRnpc, RRnpcb),                rd_rn,  rd_rn),
14943  TCE(strexb,    1c00f90, e8c00f40, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
14944  TCE(strexh,    1e00f90, e8c00f50, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
14945  TUF(clrex,     57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
14946
14947 #undef ARM_VARIANT
14948 #define ARM_VARIANT &arm_ext_v6z
14949  TCE(smc,       1600070, f7f08000, 1, (EXPi), smc, t_smc),
14950
14951 #undef ARM_VARIANT
14952 #define ARM_VARIANT &arm_ext_v6t2
14953  TCE(bfc,       7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
14954  TCE(bfi,       7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
14955  TCE(sbfx,      7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
14956  TCE(ubfx,      7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
14957
14958  TCE(mls,       0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
14959  TCE(movw,      3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
14960  TCE(movt,      3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
14961  TCE(rbit,      6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
14962
14963  TC3(ldrht,     03000b0, f8300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
14964  TC3(ldrsht,    03000f0, f9300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
14965  TC3(ldrsbt,    03000d0, f9100e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
14966  TC3(strht,     02000b0, f8200e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
14967
14968   UT(cbnz,      b900,    2, (RR, EXP), t_czb),
14969   UT(cbz,       b100,    2, (RR, EXP), t_czb),
14970  /* ARM does not really have an IT instruction, so always allow it.  */
14971 #undef ARM_VARIANT
14972 #define ARM_VARIANT &arm_ext_v1
14973  TUE(it,        0, bf08, 1, (COND),    it, t_it),
14974  TUE(itt,       0, bf0c, 1, (COND),    it, t_it),
14975  TUE(ite,       0, bf04, 1, (COND),    it, t_it),
14976  TUE(ittt,      0, bf0e, 1, (COND),    it, t_it),
14977  TUE(itet,      0, bf06, 1, (COND),    it, t_it),
14978  TUE(itte,      0, bf0a, 1, (COND),    it, t_it),
14979  TUE(itee,      0, bf02, 1, (COND),    it, t_it),
14980  TUE(itttt,     0, bf0f, 1, (COND),    it, t_it),
14981  TUE(itett,     0, bf07, 1, (COND),    it, t_it),
14982  TUE(ittet,     0, bf0b, 1, (COND),    it, t_it),
14983  TUE(iteet,     0, bf03, 1, (COND),    it, t_it),
14984  TUE(ittte,     0, bf0d, 1, (COND),    it, t_it),
14985  TUE(itete,     0, bf05, 1, (COND),    it, t_it),
14986  TUE(ittee,     0, bf09, 1, (COND),    it, t_it),
14987  TUE(iteee,     0, bf01, 1, (COND),    it, t_it),
14988
14989  /* Thumb2 only instructions.  */
14990 #undef ARM_VARIANT
14991 #define ARM_VARIANT NULL
14992
14993  TCE(addw,      0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
14994  TCE(subw,      0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
14995  TCE(tbb,       0, e8d0f000, 1, (TB), 0, t_tb),
14996  TCE(tbh,       0, e8d0f010, 1, (TB), 0, t_tb),
14997
14998  /* Thumb-2 hardware division instructions (R and M profiles only).  */
14999 #undef THUMB_VARIANT
15000 #define THUMB_VARIANT &arm_ext_div
15001  TCE(sdiv,      0, fb90f0f0, 3, (RR, oRR, RR), 0, t_div),
15002  TCE(udiv,      0, fbb0f0f0, 3, (RR, oRR, RR), 0, t_div),
15003
15004  /* ARM V7 instructions.  */
15005 #undef ARM_VARIANT
15006 #define ARM_VARIANT &arm_ext_v7
15007 #undef THUMB_VARIANT
15008 #define THUMB_VARIANT &arm_ext_v7
15009  TUF(pli,       450f000, f910f000, 1, (ADDR),     pli,      t_pld),
15010  TCE(dbg,       320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
15011  TUF(dmb,       57ff050, f3bf8f50, 1, (oBARRIER), barrier,  t_barrier),
15012  TUF(dsb,       57ff040, f3bf8f40, 1, (oBARRIER), barrier,  t_barrier),
15013  TUF(isb,       57ff060, f3bf8f60, 1, (oBARRIER), barrier,  t_barrier),
15014
15015 #undef ARM_VARIANT
15016 #define ARM_VARIANT &fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
15017  cCE(wfs,       e200110, 1, (RR),            rd),
15018  cCE(rfs,       e300110, 1, (RR),            rd),
15019  cCE(wfc,       e400110, 1, (RR),            rd),
15020  cCE(rfc,       e500110, 1, (RR),            rd),
15021
15022  cCL(ldfs,      c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
15023  cCL(ldfd,      c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
15024  cCL(ldfe,      c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
15025  cCL(ldfp,      c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
15026
15027  cCL(stfs,      c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
15028  cCL(stfd,      c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
15029  cCL(stfe,      c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
15030  cCL(stfp,      c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
15031
15032  cCL(mvfs,      e008100, 2, (RF, RF_IF),     rd_rm),
15033  cCL(mvfsp,     e008120, 2, (RF, RF_IF),     rd_rm),
15034  cCL(mvfsm,     e008140, 2, (RF, RF_IF),     rd_rm),
15035  cCL(mvfsz,     e008160, 2, (RF, RF_IF),     rd_rm),
15036  cCL(mvfd,      e008180, 2, (RF, RF_IF),     rd_rm),
15037  cCL(mvfdp,     e0081a0, 2, (RF, RF_IF),     rd_rm),
15038  cCL(mvfdm,     e0081c0, 2, (RF, RF_IF),     rd_rm),
15039  cCL(mvfdz,     e0081e0, 2, (RF, RF_IF),     rd_rm),
15040  cCL(mvfe,      e088100, 2, (RF, RF_IF),     rd_rm),
15041  cCL(mvfep,     e088120, 2, (RF, RF_IF),     rd_rm),
15042  cCL(mvfem,     e088140, 2, (RF, RF_IF),     rd_rm),
15043  cCL(mvfez,     e088160, 2, (RF, RF_IF),     rd_rm),
15044
15045  cCL(mnfs,      e108100, 2, (RF, RF_IF),     rd_rm),
15046  cCL(mnfsp,     e108120, 2, (RF, RF_IF),     rd_rm),
15047  cCL(mnfsm,     e108140, 2, (RF, RF_IF),     rd_rm),
15048  cCL(mnfsz,     e108160, 2, (RF, RF_IF),     rd_rm),
15049  cCL(mnfd,      e108180, 2, (RF, RF_IF),     rd_rm),
15050  cCL(mnfdp,     e1081a0, 2, (RF, RF_IF),     rd_rm),
15051  cCL(mnfdm,     e1081c0, 2, (RF, RF_IF),     rd_rm),
15052  cCL(mnfdz,     e1081e0, 2, (RF, RF_IF),     rd_rm),
15053  cCL(mnfe,      e188100, 2, (RF, RF_IF),     rd_rm),
15054  cCL(mnfep,     e188120, 2, (RF, RF_IF),     rd_rm),
15055  cCL(mnfem,     e188140, 2, (RF, RF_IF),     rd_rm),
15056  cCL(mnfez,     e188160, 2, (RF, RF_IF),     rd_rm),
15057
15058  cCL(abss,      e208100, 2, (RF, RF_IF),     rd_rm),
15059  cCL(abssp,     e208120, 2, (RF, RF_IF),     rd_rm),
15060  cCL(abssm,     e208140, 2, (RF, RF_IF),     rd_rm),
15061  cCL(abssz,     e208160, 2, (RF, RF_IF),     rd_rm),
15062  cCL(absd,      e208180, 2, (RF, RF_IF),     rd_rm),
15063  cCL(absdp,     e2081a0, 2, (RF, RF_IF),     rd_rm),
15064  cCL(absdm,     e2081c0, 2, (RF, RF_IF),     rd_rm),
15065  cCL(absdz,     e2081e0, 2, (RF, RF_IF),     rd_rm),
15066  cCL(abse,      e288100, 2, (RF, RF_IF),     rd_rm),
15067  cCL(absep,     e288120, 2, (RF, RF_IF),     rd_rm),
15068  cCL(absem,     e288140, 2, (RF, RF_IF),     rd_rm),
15069  cCL(absez,     e288160, 2, (RF, RF_IF),     rd_rm),
15070
15071  cCL(rnds,      e308100, 2, (RF, RF_IF),     rd_rm),
15072  cCL(rndsp,     e308120, 2, (RF, RF_IF),     rd_rm),
15073  cCL(rndsm,     e308140, 2, (RF, RF_IF),     rd_rm),
15074  cCL(rndsz,     e308160, 2, (RF, RF_IF),     rd_rm),
15075  cCL(rndd,      e308180, 2, (RF, RF_IF),     rd_rm),
15076  cCL(rnddp,     e3081a0, 2, (RF, RF_IF),     rd_rm),
15077  cCL(rnddm,     e3081c0, 2, (RF, RF_IF),     rd_rm),
15078  cCL(rnddz,     e3081e0, 2, (RF, RF_IF),     rd_rm),
15079  cCL(rnde,      e388100, 2, (RF, RF_IF),     rd_rm),
15080  cCL(rndep,     e388120, 2, (RF, RF_IF),     rd_rm),
15081  cCL(rndem,     e388140, 2, (RF, RF_IF),     rd_rm),
15082  cCL(rndez,     e388160, 2, (RF, RF_IF),     rd_rm),
15083
15084  cCL(sqts,      e408100, 2, (RF, RF_IF),     rd_rm),
15085  cCL(sqtsp,     e408120, 2, (RF, RF_IF),     rd_rm),
15086  cCL(sqtsm,     e408140, 2, (RF, RF_IF),     rd_rm),
15087  cCL(sqtsz,     e408160, 2, (RF, RF_IF),     rd_rm),
15088  cCL(sqtd,      e408180, 2, (RF, RF_IF),     rd_rm),
15089  cCL(sqtdp,     e4081a0, 2, (RF, RF_IF),     rd_rm),
15090  cCL(sqtdm,     e4081c0, 2, (RF, RF_IF),     rd_rm),
15091  cCL(sqtdz,     e4081e0, 2, (RF, RF_IF),     rd_rm),
15092  cCL(sqte,      e488100, 2, (RF, RF_IF),     rd_rm),
15093  cCL(sqtep,     e488120, 2, (RF, RF_IF),     rd_rm),
15094  cCL(sqtem,     e488140, 2, (RF, RF_IF),     rd_rm),
15095  cCL(sqtez,     e488160, 2, (RF, RF_IF),     rd_rm),
15096
15097  cCL(logs,      e508100, 2, (RF, RF_IF),     rd_rm),
15098  cCL(logsp,     e508120, 2, (RF, RF_IF),     rd_rm),
15099  cCL(logsm,     e508140, 2, (RF, RF_IF),     rd_rm),
15100  cCL(logsz,     e508160, 2, (RF, RF_IF),     rd_rm),
15101  cCL(logd,      e508180, 2, (RF, RF_IF),     rd_rm),
15102  cCL(logdp,     e5081a0, 2, (RF, RF_IF),     rd_rm),
15103  cCL(logdm,     e5081c0, 2, (RF, RF_IF),     rd_rm),
15104  cCL(logdz,     e5081e0, 2, (RF, RF_IF),     rd_rm),
15105  cCL(loge,      e588100, 2, (RF, RF_IF),     rd_rm),
15106  cCL(logep,     e588120, 2, (RF, RF_IF),     rd_rm),
15107  cCL(logem,     e588140, 2, (RF, RF_IF),     rd_rm),
15108  cCL(logez,     e588160, 2, (RF, RF_IF),     rd_rm),
15109
15110  cCL(lgns,      e608100, 2, (RF, RF_IF),     rd_rm),
15111  cCL(lgnsp,     e608120, 2, (RF, RF_IF),     rd_rm),
15112  cCL(lgnsm,     e608140, 2, (RF, RF_IF),     rd_rm),
15113  cCL(lgnsz,     e608160, 2, (RF, RF_IF),     rd_rm),
15114  cCL(lgnd,      e608180, 2, (RF, RF_IF),     rd_rm),
15115  cCL(lgndp,     e6081a0, 2, (RF, RF_IF),     rd_rm),
15116  cCL(lgndm,     e6081c0, 2, (RF, RF_IF),     rd_rm),
15117  cCL(lgndz,     e6081e0, 2, (RF, RF_IF),     rd_rm),
15118  cCL(lgne,      e688100, 2, (RF, RF_IF),     rd_rm),
15119  cCL(lgnep,     e688120, 2, (RF, RF_IF),     rd_rm),
15120  cCL(lgnem,     e688140, 2, (RF, RF_IF),     rd_rm),
15121  cCL(lgnez,     e688160, 2, (RF, RF_IF),     rd_rm),
15122
15123  cCL(exps,      e708100, 2, (RF, RF_IF),     rd_rm),
15124  cCL(expsp,     e708120, 2, (RF, RF_IF),     rd_rm),
15125  cCL(expsm,     e708140, 2, (RF, RF_IF),     rd_rm),
15126  cCL(expsz,     e708160, 2, (RF, RF_IF),     rd_rm),
15127  cCL(expd,      e708180, 2, (RF, RF_IF),     rd_rm),
15128  cCL(expdp,     e7081a0, 2, (RF, RF_IF),     rd_rm),
15129  cCL(expdm,     e7081c0, 2, (RF, RF_IF),     rd_rm),
15130  cCL(expdz,     e7081e0, 2, (RF, RF_IF),     rd_rm),
15131  cCL(expe,      e788100, 2, (RF, RF_IF),     rd_rm),
15132  cCL(expep,     e788120, 2, (RF, RF_IF),     rd_rm),
15133  cCL(expem,     e788140, 2, (RF, RF_IF),     rd_rm),
15134  cCL(expdz,     e788160, 2, (RF, RF_IF),     rd_rm),
15135
15136  cCL(sins,      e808100, 2, (RF, RF_IF),     rd_rm),
15137  cCL(sinsp,     e808120, 2, (RF, RF_IF),     rd_rm),
15138  cCL(sinsm,     e808140, 2, (RF, RF_IF),     rd_rm),
15139  cCL(sinsz,     e808160, 2, (RF, RF_IF),     rd_rm),
15140  cCL(sind,      e808180, 2, (RF, RF_IF),     rd_rm),
15141  cCL(sindp,     e8081a0, 2, (RF, RF_IF),     rd_rm),
15142  cCL(sindm,     e8081c0, 2, (RF, RF_IF),     rd_rm),
15143  cCL(sindz,     e8081e0, 2, (RF, RF_IF),     rd_rm),
15144  cCL(sine,      e888100, 2, (RF, RF_IF),     rd_rm),
15145  cCL(sinep,     e888120, 2, (RF, RF_IF),     rd_rm),
15146  cCL(sinem,     e888140, 2, (RF, RF_IF),     rd_rm),
15147  cCL(sinez,     e888160, 2, (RF, RF_IF),     rd_rm),
15148
15149  cCL(coss,      e908100, 2, (RF, RF_IF),     rd_rm),
15150  cCL(cossp,     e908120, 2, (RF, RF_IF),     rd_rm),
15151  cCL(cossm,     e908140, 2, (RF, RF_IF),     rd_rm),
15152  cCL(cossz,     e908160, 2, (RF, RF_IF),     rd_rm),
15153  cCL(cosd,      e908180, 2, (RF, RF_IF),     rd_rm),
15154  cCL(cosdp,     e9081a0, 2, (RF, RF_IF),     rd_rm),
15155  cCL(cosdm,     e9081c0, 2, (RF, RF_IF),     rd_rm),
15156  cCL(cosdz,     e9081e0, 2, (RF, RF_IF),     rd_rm),
15157  cCL(cose,      e988100, 2, (RF, RF_IF),     rd_rm),
15158  cCL(cosep,     e988120, 2, (RF, RF_IF),     rd_rm),
15159  cCL(cosem,     e988140, 2, (RF, RF_IF),     rd_rm),
15160  cCL(cosez,     e988160, 2, (RF, RF_IF),     rd_rm),
15161
15162  cCL(tans,      ea08100, 2, (RF, RF_IF),     rd_rm),
15163  cCL(tansp,     ea08120, 2, (RF, RF_IF),     rd_rm),
15164  cCL(tansm,     ea08140, 2, (RF, RF_IF),     rd_rm),
15165  cCL(tansz,     ea08160, 2, (RF, RF_IF),     rd_rm),
15166  cCL(tand,      ea08180, 2, (RF, RF_IF),     rd_rm),
15167  cCL(tandp,     ea081a0, 2, (RF, RF_IF),     rd_rm),
15168  cCL(tandm,     ea081c0, 2, (RF, RF_IF),     rd_rm),
15169  cCL(tandz,     ea081e0, 2, (RF, RF_IF),     rd_rm),
15170  cCL(tane,      ea88100, 2, (RF, RF_IF),     rd_rm),
15171  cCL(tanep,     ea88120, 2, (RF, RF_IF),     rd_rm),
15172  cCL(tanem,     ea88140, 2, (RF, RF_IF),     rd_rm),
15173  cCL(tanez,     ea88160, 2, (RF, RF_IF),     rd_rm),
15174
15175  cCL(asns,      eb08100, 2, (RF, RF_IF),     rd_rm),
15176  cCL(asnsp,     eb08120, 2, (RF, RF_IF),     rd_rm),
15177  cCL(asnsm,     eb08140, 2, (RF, RF_IF),     rd_rm),
15178  cCL(asnsz,     eb08160, 2, (RF, RF_IF),     rd_rm),
15179  cCL(asnd,      eb08180, 2, (RF, RF_IF),     rd_rm),
15180  cCL(asndp,     eb081a0, 2, (RF, RF_IF),     rd_rm),
15181  cCL(asndm,     eb081c0, 2, (RF, RF_IF),     rd_rm),
15182  cCL(asndz,     eb081e0, 2, (RF, RF_IF),     rd_rm),
15183  cCL(asne,      eb88100, 2, (RF, RF_IF),     rd_rm),
15184  cCL(asnep,     eb88120, 2, (RF, RF_IF),     rd_rm),
15185  cCL(asnem,     eb88140, 2, (RF, RF_IF),     rd_rm),
15186  cCL(asnez,     eb88160, 2, (RF, RF_IF),     rd_rm),
15187
15188  cCL(acss,      ec08100, 2, (RF, RF_IF),     rd_rm),
15189  cCL(acssp,     ec08120, 2, (RF, RF_IF),     rd_rm),
15190  cCL(acssm,     ec08140, 2, (RF, RF_IF),     rd_rm),
15191  cCL(acssz,     ec08160, 2, (RF, RF_IF),     rd_rm),
15192  cCL(acsd,      ec08180, 2, (RF, RF_IF),     rd_rm),
15193  cCL(acsdp,     ec081a0, 2, (RF, RF_IF),     rd_rm),
15194  cCL(acsdm,     ec081c0, 2, (RF, RF_IF),     rd_rm),
15195  cCL(acsdz,     ec081e0, 2, (RF, RF_IF),     rd_rm),
15196  cCL(acse,      ec88100, 2, (RF, RF_IF),     rd_rm),
15197  cCL(acsep,     ec88120, 2, (RF, RF_IF),     rd_rm),
15198  cCL(acsem,     ec88140, 2, (RF, RF_IF),     rd_rm),
15199  cCL(acsez,     ec88160, 2, (RF, RF_IF),     rd_rm),
15200
15201  cCL(atns,      ed08100, 2, (RF, RF_IF),     rd_rm),
15202  cCL(atnsp,     ed08120, 2, (RF, RF_IF),     rd_rm),
15203  cCL(atnsm,     ed08140, 2, (RF, RF_IF),     rd_rm),
15204  cCL(atnsz,     ed08160, 2, (RF, RF_IF),     rd_rm),
15205  cCL(atnd,      ed08180, 2, (RF, RF_IF),     rd_rm),
15206  cCL(atndp,     ed081a0, 2, (RF, RF_IF),     rd_rm),
15207  cCL(atndm,     ed081c0, 2, (RF, RF_IF),     rd_rm),
15208  cCL(atndz,     ed081e0, 2, (RF, RF_IF),     rd_rm),
15209  cCL(atne,      ed88100, 2, (RF, RF_IF),     rd_rm),
15210  cCL(atnep,     ed88120, 2, (RF, RF_IF),     rd_rm),
15211  cCL(atnem,     ed88140, 2, (RF, RF_IF),     rd_rm),
15212  cCL(atnez,     ed88160, 2, (RF, RF_IF),     rd_rm),
15213
15214  cCL(urds,      ee08100, 2, (RF, RF_IF),     rd_rm),
15215  cCL(urdsp,     ee08120, 2, (RF, RF_IF),     rd_rm),
15216  cCL(urdsm,     ee08140, 2, (RF, RF_IF),     rd_rm),
15217  cCL(urdsz,     ee08160, 2, (RF, RF_IF),     rd_rm),
15218  cCL(urdd,      ee08180, 2, (RF, RF_IF),     rd_rm),
15219  cCL(urddp,     ee081a0, 2, (RF, RF_IF),     rd_rm),
15220  cCL(urddm,     ee081c0, 2, (RF, RF_IF),     rd_rm),
15221  cCL(urddz,     ee081e0, 2, (RF, RF_IF),     rd_rm),
15222  cCL(urde,      ee88100, 2, (RF, RF_IF),     rd_rm),
15223  cCL(urdep,     ee88120, 2, (RF, RF_IF),     rd_rm),
15224  cCL(urdem,     ee88140, 2, (RF, RF_IF),     rd_rm),
15225  cCL(urdez,     ee88160, 2, (RF, RF_IF),     rd_rm),
15226
15227  cCL(nrms,      ef08100, 2, (RF, RF_IF),     rd_rm),
15228  cCL(nrmsp,     ef08120, 2, (RF, RF_IF),     rd_rm),
15229  cCL(nrmsm,     ef08140, 2, (RF, RF_IF),     rd_rm),
15230  cCL(nrmsz,     ef08160, 2, (RF, RF_IF),     rd_rm),
15231  cCL(nrmd,      ef08180, 2, (RF, RF_IF),     rd_rm),
15232  cCL(nrmdp,     ef081a0, 2, (RF, RF_IF),     rd_rm),
15233  cCL(nrmdm,     ef081c0, 2, (RF, RF_IF),     rd_rm),
15234  cCL(nrmdz,     ef081e0, 2, (RF, RF_IF),     rd_rm),
15235  cCL(nrme,      ef88100, 2, (RF, RF_IF),     rd_rm),
15236  cCL(nrmep,     ef88120, 2, (RF, RF_IF),     rd_rm),
15237  cCL(nrmem,     ef88140, 2, (RF, RF_IF),     rd_rm),
15238  cCL(nrmez,     ef88160, 2, (RF, RF_IF),     rd_rm),
15239
15240  cCL(adfs,      e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
15241  cCL(adfsp,     e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
15242  cCL(adfsm,     e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
15243  cCL(adfsz,     e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
15244  cCL(adfd,      e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
15245  cCL(adfdp,     e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15246  cCL(adfdm,     e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15247  cCL(adfdz,     e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15248  cCL(adfe,      e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
15249  cCL(adfep,     e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
15250  cCL(adfem,     e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
15251  cCL(adfez,     e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
15252
15253  cCL(sufs,      e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
15254  cCL(sufsp,     e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
15255  cCL(sufsm,     e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
15256  cCL(sufsz,     e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
15257  cCL(sufd,      e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
15258  cCL(sufdp,     e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15259  cCL(sufdm,     e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15260  cCL(sufdz,     e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15261  cCL(sufe,      e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
15262  cCL(sufep,     e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
15263  cCL(sufem,     e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
15264  cCL(sufez,     e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
15265
15266  cCL(rsfs,      e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
15267  cCL(rsfsp,     e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
15268  cCL(rsfsm,     e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
15269  cCL(rsfsz,     e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
15270  cCL(rsfd,      e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
15271  cCL(rsfdp,     e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15272  cCL(rsfdm,     e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15273  cCL(rsfdz,     e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15274  cCL(rsfe,      e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
15275  cCL(rsfep,     e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
15276  cCL(rsfem,     e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
15277  cCL(rsfez,     e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
15278
15279  cCL(mufs,      e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
15280  cCL(mufsp,     e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
15281  cCL(mufsm,     e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
15282  cCL(mufsz,     e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
15283  cCL(mufd,      e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
15284  cCL(mufdp,     e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15285  cCL(mufdm,     e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15286  cCL(mufdz,     e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15287  cCL(mufe,      e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
15288  cCL(mufep,     e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
15289  cCL(mufem,     e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
15290  cCL(mufez,     e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
15291
15292  cCL(dvfs,      e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
15293  cCL(dvfsp,     e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
15294  cCL(dvfsm,     e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
15295  cCL(dvfsz,     e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
15296  cCL(dvfd,      e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
15297  cCL(dvfdp,     e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15298  cCL(dvfdm,     e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15299  cCL(dvfdz,     e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15300  cCL(dvfe,      e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
15301  cCL(dvfep,     e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
15302  cCL(dvfem,     e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
15303  cCL(dvfez,     e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
15304
15305  cCL(rdfs,      e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
15306  cCL(rdfsp,     e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
15307  cCL(rdfsm,     e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
15308  cCL(rdfsz,     e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
15309  cCL(rdfd,      e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
15310  cCL(rdfdp,     e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15311  cCL(rdfdm,     e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15312  cCL(rdfdz,     e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15313  cCL(rdfe,      e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
15314  cCL(rdfep,     e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
15315  cCL(rdfem,     e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
15316  cCL(rdfez,     e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
15317
15318  cCL(pows,      e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
15319  cCL(powsp,     e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
15320  cCL(powsm,     e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
15321  cCL(powsz,     e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
15322  cCL(powd,      e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
15323  cCL(powdp,     e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15324  cCL(powdm,     e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15325  cCL(powdz,     e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15326  cCL(powe,      e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
15327  cCL(powep,     e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
15328  cCL(powem,     e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
15329  cCL(powez,     e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
15330
15331  cCL(rpws,      e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
15332  cCL(rpwsp,     e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
15333  cCL(rpwsm,     e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
15334  cCL(rpwsz,     e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
15335  cCL(rpwd,      e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
15336  cCL(rpwdp,     e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15337  cCL(rpwdm,     e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15338  cCL(rpwdz,     e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15339  cCL(rpwe,      e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
15340  cCL(rpwep,     e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
15341  cCL(rpwem,     e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
15342  cCL(rpwez,     e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
15343
15344  cCL(rmfs,      e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
15345  cCL(rmfsp,     e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
15346  cCL(rmfsm,     e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
15347  cCL(rmfsz,     e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
15348  cCL(rmfd,      e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
15349  cCL(rmfdp,     e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15350  cCL(rmfdm,     e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15351  cCL(rmfdz,     e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15352  cCL(rmfe,      e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
15353  cCL(rmfep,     e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
15354  cCL(rmfem,     e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
15355  cCL(rmfez,     e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
15356
15357  cCL(fmls,      e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
15358  cCL(fmlsp,     e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
15359  cCL(fmlsm,     e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
15360  cCL(fmlsz,     e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
15361  cCL(fmld,      e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
15362  cCL(fmldp,     e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15363  cCL(fmldm,     e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15364  cCL(fmldz,     e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15365  cCL(fmle,      e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
15366  cCL(fmlep,     e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
15367  cCL(fmlem,     e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
15368  cCL(fmlez,     e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
15369
15370  cCL(fdvs,      ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
15371  cCL(fdvsp,     ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
15372  cCL(fdvsm,     ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
15373  cCL(fdvsz,     ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
15374  cCL(fdvd,      ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
15375  cCL(fdvdp,     ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15376  cCL(fdvdm,     ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15377  cCL(fdvdz,     ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15378  cCL(fdve,      ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
15379  cCL(fdvep,     ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
15380  cCL(fdvem,     ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
15381  cCL(fdvez,     ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
15382
15383  cCL(frds,      eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
15384  cCL(frdsp,     eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
15385  cCL(frdsm,     eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
15386  cCL(frdsz,     eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
15387  cCL(frdd,      eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
15388  cCL(frddp,     eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15389  cCL(frddm,     eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15390  cCL(frddz,     eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15391  cCL(frde,      eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
15392  cCL(frdep,     eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
15393  cCL(frdem,     eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
15394  cCL(frdez,     eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
15395
15396  cCL(pols,      ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
15397  cCL(polsp,     ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
15398  cCL(polsm,     ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
15399  cCL(polsz,     ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
15400  cCL(pold,      ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
15401  cCL(poldp,     ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
15402  cCL(poldm,     ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
15403  cCL(poldz,     ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
15404  cCL(pole,      ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
15405  cCL(polep,     ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
15406  cCL(polem,     ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
15407  cCL(polez,     ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
15408
15409  cCE(cmf,       e90f110, 2, (RF, RF_IF),     fpa_cmp),
15410  C3E(cmfe,      ed0f110, 2, (RF, RF_IF),     fpa_cmp),
15411  cCE(cnf,       eb0f110, 2, (RF, RF_IF),     fpa_cmp),
15412  C3E(cnfe,      ef0f110, 2, (RF, RF_IF),     fpa_cmp),
15413
15414  cCL(flts,      e000110, 2, (RF, RR),        rn_rd),
15415  cCL(fltsp,     e000130, 2, (RF, RR),        rn_rd),
15416  cCL(fltsm,     e000150, 2, (RF, RR),        rn_rd),
15417  cCL(fltsz,     e000170, 2, (RF, RR),        rn_rd),
15418  cCL(fltd,      e000190, 2, (RF, RR),        rn_rd),
15419  cCL(fltdp,     e0001b0, 2, (RF, RR),        rn_rd),
15420  cCL(fltdm,     e0001d0, 2, (RF, RR),        rn_rd),
15421  cCL(fltdz,     e0001f0, 2, (RF, RR),        rn_rd),
15422  cCL(flte,      e080110, 2, (RF, RR),        rn_rd),
15423  cCL(fltep,     e080130, 2, (RF, RR),        rn_rd),
15424  cCL(fltem,     e080150, 2, (RF, RR),        rn_rd),
15425  cCL(fltez,     e080170, 2, (RF, RR),        rn_rd),
15426
15427   /* The implementation of the FIX instruction is broken on some
15428      assemblers, in that it accepts a precision specifier as well as a
15429      rounding specifier, despite the fact that this is meaningless.
15430      To be more compatible, we accept it as well, though of course it
15431      does not set any bits.  */
15432  cCE(fix,       e100110, 2, (RR, RF),        rd_rm),
15433  cCL(fixp,      e100130, 2, (RR, RF),        rd_rm),
15434  cCL(fixm,      e100150, 2, (RR, RF),        rd_rm),
15435  cCL(fixz,      e100170, 2, (RR, RF),        rd_rm),
15436  cCL(fixsp,     e100130, 2, (RR, RF),        rd_rm),
15437  cCL(fixsm,     e100150, 2, (RR, RF),        rd_rm),
15438  cCL(fixsz,     e100170, 2, (RR, RF),        rd_rm),
15439  cCL(fixdp,     e100130, 2, (RR, RF),        rd_rm),
15440  cCL(fixdm,     e100150, 2, (RR, RF),        rd_rm),
15441  cCL(fixdz,     e100170, 2, (RR, RF),        rd_rm),
15442  cCL(fixep,     e100130, 2, (RR, RF),        rd_rm),
15443  cCL(fixem,     e100150, 2, (RR, RF),        rd_rm),
15444  cCL(fixez,     e100170, 2, (RR, RF),        rd_rm),
15445
15446   /* Instructions that were new with the real FPA, call them V2.  */
15447 #undef ARM_VARIANT
15448 #define ARM_VARIANT &fpu_fpa_ext_v2
15449  cCE(lfm,       c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
15450  cCL(lfmfd,     c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
15451  cCL(lfmea,     d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
15452  cCE(sfm,       c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
15453  cCL(sfmfd,     d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
15454  cCL(sfmea,     c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
15455
15456 #undef ARM_VARIANT
15457 #define ARM_VARIANT &fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
15458   /* Moves and type conversions.  */
15459  cCE(fcpys,     eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
15460  cCE(fmrs,      e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
15461  cCE(fmsr,      e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
15462  cCE(fmstat,    ef1fa10, 0, (),               noargs),
15463  cCE(fsitos,    eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
15464  cCE(fuitos,    eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
15465  cCE(ftosis,    ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
15466  cCE(ftosizs,   ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
15467  cCE(ftouis,    ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
15468  cCE(ftouizs,   ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
15469  cCE(fmrx,      ef00a10, 2, (RR, RVC),        rd_rn),
15470  cCE(fmxr,      ee00a10, 2, (RVC, RR),        rn_rd),
15471
15472   /* Memory operations.  */
15473  cCE(flds,      d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
15474  cCE(fsts,      d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
15475  cCE(fldmias,   c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
15476  cCE(fldmfds,   c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
15477  cCE(fldmdbs,   d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
15478  cCE(fldmeas,   d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
15479  cCE(fldmiax,   c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
15480  cCE(fldmfdx,   c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
15481  cCE(fldmdbx,   d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
15482  cCE(fldmeax,   d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
15483  cCE(fstmias,   c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
15484  cCE(fstmeas,   c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
15485  cCE(fstmdbs,   d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
15486  cCE(fstmfds,   d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
15487  cCE(fstmiax,   c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
15488  cCE(fstmeax,   c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
15489  cCE(fstmdbx,   d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
15490  cCE(fstmfdx,   d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
15491
15492   /* Monadic operations.  */
15493  cCE(fabss,     eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
15494  cCE(fnegs,     eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
15495  cCE(fsqrts,    eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
15496
15497   /* Dyadic operations.  */
15498  cCE(fadds,     e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
15499  cCE(fsubs,     e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
15500  cCE(fmuls,     e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
15501  cCE(fdivs,     e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
15502  cCE(fmacs,     e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
15503  cCE(fmscs,     e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
15504  cCE(fnmuls,    e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
15505  cCE(fnmacs,    e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
15506  cCE(fnmscs,    e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
15507
15508   /* Comparisons.  */
15509  cCE(fcmps,     eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
15510  cCE(fcmpzs,    eb50a40, 1, (RVS),            vfp_sp_compare_z),
15511  cCE(fcmpes,    eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
15512  cCE(fcmpezs,   eb50ac0, 1, (RVS),            vfp_sp_compare_z),
15513
15514 #undef ARM_VARIANT
15515 #define ARM_VARIANT &fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
15516   /* Moves and type conversions.  */
15517  cCE(fcpyd,     eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
15518  cCE(fcvtds,    eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
15519  cCE(fcvtsd,    eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
15520  cCE(fmdhr,     e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
15521  cCE(fmdlr,     e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
15522  cCE(fmrdh,     e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
15523  cCE(fmrdl,     e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
15524  cCE(fsitod,    eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
15525  cCE(fuitod,    eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
15526  cCE(ftosid,    ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
15527  cCE(ftosizd,   ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
15528  cCE(ftouid,    ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
15529  cCE(ftouizd,   ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
15530
15531   /* Memory operations.  */
15532  cCE(fldd,      d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
15533  cCE(fstd,      d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
15534  cCE(fldmiad,   c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
15535  cCE(fldmfdd,   c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
15536  cCE(fldmdbd,   d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
15537  cCE(fldmead,   d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
15538  cCE(fstmiad,   c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
15539  cCE(fstmead,   c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
15540  cCE(fstmdbd,   d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
15541  cCE(fstmfdd,   d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
15542
15543   /* Monadic operations.  */
15544  cCE(fabsd,     eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
15545  cCE(fnegd,     eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
15546  cCE(fsqrtd,    eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
15547
15548   /* Dyadic operations.  */
15549  cCE(faddd,     e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
15550  cCE(fsubd,     e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
15551  cCE(fmuld,     e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
15552  cCE(fdivd,     e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
15553  cCE(fmacd,     e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
15554  cCE(fmscd,     e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
15555  cCE(fnmuld,    e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
15556  cCE(fnmacd,    e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
15557  cCE(fnmscd,    e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
15558
15559   /* Comparisons.  */
15560  cCE(fcmpd,     eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
15561  cCE(fcmpzd,    eb50b40, 1, (RVD),            vfp_dp_rd),
15562  cCE(fcmped,    eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
15563  cCE(fcmpezd,   eb50bc0, 1, (RVD),            vfp_dp_rd),
15564
15565 #undef ARM_VARIANT
15566 #define ARM_VARIANT &fpu_vfp_ext_v2
15567  cCE(fmsrr,     c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
15568  cCE(fmrrs,     c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
15569  cCE(fmdrr,     c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
15570  cCE(fmrrd,     c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
15571
15572 /* Instructions which may belong to either the Neon or VFP instruction sets.
15573    Individual encoder functions perform additional architecture checks.  */
15574 #undef ARM_VARIANT
15575 #define ARM_VARIANT &fpu_vfp_ext_v1xd
15576 #undef THUMB_VARIANT
15577 #define THUMB_VARIANT &fpu_vfp_ext_v1xd
15578   /* These mnemonics are unique to VFP.  */
15579  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
15580  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
15581  nCE(vnmul,     vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
15582  nCE(vnmla,     vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
15583  nCE(vnmls,     vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
15584  nCE(vcmp,      vcmp,    2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
15585  nCE(vcmpe,     vcmpe,   2, (RVSD, RVSD_I0),    vfp_nsyn_cmp),
15586  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
15587  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
15588  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
15589
15590   /* Mnemonics shared by Neon and VFP.  */
15591  nCEF(vmul,     vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
15592  nCEF(vmla,     vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
15593  nCEF(vmls,     vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
15594
15595  nCEF(vadd,     vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
15596  nCEF(vsub,     vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
15597
15598  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
15599  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
15600
15601  NCE(vldm,      c900b00, 2, (RRw, VRSDLST), neon_ldm_stm),
15602  NCE(vldmia,    c900b00, 2, (RRw, VRSDLST), neon_ldm_stm),
15603  NCE(vldmdb,    d100b00, 2, (RRw, VRSDLST), neon_ldm_stm),
15604  NCE(vstm,      c800b00, 2, (RRw, VRSDLST), neon_ldm_stm),
15605  NCE(vstmia,    c800b00, 2, (RRw, VRSDLST), neon_ldm_stm),
15606  NCE(vstmdb,    d000b00, 2, (RRw, VRSDLST), neon_ldm_stm),
15607  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
15608  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
15609
15610  nCEF(vcvt,     vcvt,    3, (RNSDQ, RNSDQ, oI32b), neon_cvt),
15611
15612   /* NOTE: All VMOV encoding is special-cased!  */
15613  NCE(vmov,      0,       1, (VMOV), neon_mov),
15614  NCE(vmovq,     0,       1, (VMOV), neon_mov),
15615
15616 #undef THUMB_VARIANT
15617 #define THUMB_VARIANT &fpu_neon_ext_v1
15618 #undef ARM_VARIANT
15619 #define ARM_VARIANT &fpu_neon_ext_v1
15620   /* Data processing with three registers of the same length.  */
15621   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
15622  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
15623  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
15624  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
15625  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
15626  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
15627  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
15628  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
15629  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
15630   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
15631  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
15632  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
15633  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
15634  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
15635  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
15636  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
15637  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
15638  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
15639   /* If not immediate, fall back to neon_dyadic_i64_su.
15640      shl_imm should accept I8 I16 I32 I64,
15641      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
15642  nUF(vshl,      vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
15643  nUF(vshlq,     vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
15644  nUF(vqshl,     vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
15645  nUF(vqshlq,    vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
15646   /* Logic ops, types optional & ignored.  */
15647  nUF(vand,      vand,    2, (RNDQ, NILO),        neon_logic),
15648  nUF(vandq,     vand,    2, (RNQ,  NILO),        neon_logic),
15649  nUF(vbic,      vbic,    2, (RNDQ, NILO),        neon_logic),
15650  nUF(vbicq,     vbic,    2, (RNQ,  NILO),        neon_logic),
15651  nUF(vorr,      vorr,    2, (RNDQ, NILO),        neon_logic),
15652  nUF(vorrq,     vorr,    2, (RNQ,  NILO),        neon_logic),
15653  nUF(vorn,      vorn,    2, (RNDQ, NILO),        neon_logic),
15654  nUF(vornq,     vorn,    2, (RNQ,  NILO),        neon_logic),
15655  nUF(veor,      veor,    3, (RNDQ, oRNDQ, RNDQ), neon_logic),
15656  nUF(veorq,     veor,    3, (RNQ,  oRNQ,  RNQ),  neon_logic),
15657   /* Bitfield ops, untyped.  */
15658  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
15659  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
15660  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
15661  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
15662  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
15663  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
15664   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F32.  */
15665  nUF(vabd,      vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
15666  nUF(vabdq,     vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
15667  nUF(vmax,      vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
15668  nUF(vmaxq,     vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
15669  nUF(vmin,      vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
15670  nUF(vminq,     vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
15671   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
15672      back to neon_dyadic_if_su.  */
15673  nUF(vcge,      vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
15674  nUF(vcgeq,     vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
15675  nUF(vcgt,      vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
15676  nUF(vcgtq,     vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
15677  nUF(vclt,      vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
15678  nUF(vcltq,     vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
15679  nUF(vcle,      vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
15680  nUF(vcleq,     vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
15681   /* Comparison. Type I8 I16 I32 F32.  */
15682  nUF(vceq,      vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
15683  nUF(vceqq,     vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
15684   /* As above, D registers only.  */
15685  nUF(vpmax,     vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
15686  nUF(vpmin,     vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
15687   /* Int and float variants, signedness unimportant.  */
15688  nUF(vmlaq,     vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
15689  nUF(vmlsq,     vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
15690  nUF(vpadd,     vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
15691   /* Add/sub take types I8 I16 I32 I64 F32.  */
15692  nUF(vaddq,     vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
15693  nUF(vsubq,     vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
15694   /* vtst takes sizes 8, 16, 32.  */
15695  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
15696  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
15697   /* VMUL takes I8 I16 I32 F32 P8.  */
15698  nUF(vmulq,     vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
15699   /* VQD{R}MULH takes S16 S32.  */
15700  nUF(vqdmulh,   vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
15701  nUF(vqdmulhq,  vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
15702  nUF(vqrdmulh,  vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
15703  nUF(vqrdmulhq, vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
15704  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
15705  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
15706  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
15707  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
15708  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
15709  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
15710  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
15711  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
15712  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
15713  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
15714  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
15715  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
15716
15717   /* Two address, int/float. Types S8 S16 S32 F32.  */
15718  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
15719  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
15720
15721   /* Data processing with two registers and a shift amount.  */
15722   /* Right shifts, and variants with rounding.
15723      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
15724  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
15725  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
15726  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
15727  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
15728  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
15729  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
15730  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
15731  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
15732   /* Shift and insert. Sizes accepted 8 16 32 64.  */
15733  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
15734  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
15735  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
15736  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
15737   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
15738  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
15739  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
15740   /* Right shift immediate, saturating & narrowing, with rounding variants.
15741      Types accepted S16 S32 S64 U16 U32 U64.  */
15742  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
15743  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
15744   /* As above, unsigned. Types accepted S16 S32 S64.  */
15745  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
15746  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
15747   /* Right shift narrowing. Types accepted I16 I32 I64.  */
15748  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
15749  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
15750   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
15751  nUF(vshll,     vshll,   3, (RNQ, RND, I32),  neon_shll),
15752   /* CVT with optional immediate for fixed-point variant.  */
15753  nUF(vcvtq,     vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
15754
15755  nUF(vmvn,      vmvn,    2, (RNDQ, RNDQ_IMVNb), neon_mvn),
15756  nUF(vmvnq,     vmvn,    2, (RNQ,  RNDQ_IMVNb), neon_mvn),
15757
15758   /* Data processing, three registers of different lengths.  */
15759   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
15760  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
15761  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
15762  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
15763  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
15764   /* If not scalar, fall back to neon_dyadic_long.
15765      Vector types as above, scalar types S16 S32 U16 U32.  */
15766  nUF(vmlal,     vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
15767  nUF(vmlsl,     vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
15768   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
15769  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
15770  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
15771   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
15772  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
15773  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
15774  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
15775  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
15776   /* Saturating doubling multiplies. Types S16 S32.  */
15777  nUF(vqdmlal,   vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
15778  nUF(vqdmlsl,   vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
15779  nUF(vqdmull,   vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
15780   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
15781      S16 S32 U16 U32.  */
15782  nUF(vmull,     vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
15783
15784   /* Extract. Size 8.  */
15785  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I7), neon_ext),
15786  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I7), neon_ext),
15787
15788   /* Two registers, miscellaneous.  */
15789   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
15790  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
15791  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
15792  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
15793  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
15794  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
15795  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
15796   /* Vector replicate. Sizes 8 16 32.  */
15797  nCE(vdup,      vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
15798  nCE(vdupq,     vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
15799   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
15800  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
15801   /* VMOVN. Types I16 I32 I64.  */
15802  nUF(vmovn,     vmovn,   2, (RND, RNQ),       neon_movn),
15803   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
15804  nUF(vqmovn,    vqmovn,  2, (RND, RNQ),       neon_qmovn),
15805   /* VQMOVUN. Types S16 S32 S64.  */
15806  nUF(vqmovun,   vqmovun, 2, (RND, RNQ),       neon_qmovun),
15807   /* VZIP / VUZP. Sizes 8 16 32.  */
15808  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
15809  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
15810  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
15811  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
15812   /* VQABS / VQNEG. Types S8 S16 S32.  */
15813  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
15814  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
15815  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
15816  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
15817   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
15818  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
15819  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
15820  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
15821  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
15822   /* Reciprocal estimates. Types U32 F32.  */
15823  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
15824  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
15825  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
15826  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
15827   /* VCLS. Types S8 S16 S32.  */
15828  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
15829  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
15830   /* VCLZ. Types I8 I16 I32.  */
15831  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
15832  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
15833   /* VCNT. Size 8.  */
15834  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
15835  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
15836   /* Two address, untyped.  */
15837  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
15838  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
15839   /* VTRN. Sizes 8 16 32.  */
15840  nUF(vtrn,      vtrn,    2, (RNDQ, RNDQ),     neon_trn),
15841  nUF(vtrnq,     vtrn,    2, (RNQ,  RNQ),      neon_trn),
15842
15843   /* Table lookup. Size 8.  */
15844  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
15845  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
15846
15847 #undef THUMB_VARIANT
15848 #define THUMB_VARIANT &fpu_vfp_v3_or_neon_ext
15849 #undef ARM_VARIANT
15850 #define ARM_VARIANT &fpu_vfp_v3_or_neon_ext
15851   /* Neon element/structure load/store.  */
15852  nUF(vld1,      vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
15853  nUF(vst1,      vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
15854  nUF(vld2,      vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
15855  nUF(vst2,      vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
15856  nUF(vld3,      vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
15857  nUF(vst3,      vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
15858  nUF(vld4,      vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
15859  nUF(vst4,      vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
15860
15861 #undef THUMB_VARIANT
15862 #define THUMB_VARIANT &fpu_vfp_ext_v3
15863 #undef ARM_VARIANT
15864 #define ARM_VARIANT &fpu_vfp_ext_v3
15865  cCE(fconsts,   eb00a00, 2, (RVS, I255),      vfp_sp_const),
15866  cCE(fconstd,   eb00b00, 2, (RVD, I255),      vfp_dp_const),
15867  cCE(fshtos,    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
15868  cCE(fshtod,    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
15869  cCE(fsltos,    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
15870  cCE(fsltod,    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
15871  cCE(fuhtos,    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
15872  cCE(fuhtod,    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
15873  cCE(fultos,    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
15874  cCE(fultod,    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
15875  cCE(ftoshs,    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
15876  cCE(ftoshd,    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
15877  cCE(ftosls,    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
15878  cCE(ftosld,    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
15879  cCE(ftouhs,    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
15880  cCE(ftouhd,    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
15881  cCE(ftouls,    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
15882  cCE(ftould,    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
15883
15884 #undef THUMB_VARIANT
15885 #undef ARM_VARIANT
15886 #define ARM_VARIANT &arm_cext_xscale /* Intel XScale extensions.         */
15887  cCE(mia,       e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
15888  cCE(miaph,     e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
15889  cCE(miabb,     e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
15890  cCE(miabt,     e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
15891  cCE(miatb,     e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
15892  cCE(miatt,     e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
15893  cCE(mar,       c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
15894  cCE(mra,       c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
15895
15896 #undef ARM_VARIANT
15897 #define ARM_VARIANT &arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
15898  cCE(tandcb,    e13f130, 1, (RR),                   iwmmxt_tandorc),
15899  cCE(tandch,    e53f130, 1, (RR),                   iwmmxt_tandorc),
15900  cCE(tandcw,    e93f130, 1, (RR),                   iwmmxt_tandorc),
15901  cCE(tbcstb,    e400010, 2, (RIWR, RR),             rn_rd),
15902  cCE(tbcsth,    e400050, 2, (RIWR, RR),             rn_rd),
15903  cCE(tbcstw,    e400090, 2, (RIWR, RR),             rn_rd),
15904  cCE(textrcb,   e130170, 2, (RR, I7),               iwmmxt_textrc),
15905  cCE(textrch,   e530170, 2, (RR, I7),               iwmmxt_textrc),
15906  cCE(textrcw,   e930170, 2, (RR, I7),               iwmmxt_textrc),
15907  cCE(textrmub,  e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
15908  cCE(textrmuh,  e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
15909  cCE(textrmuw,  e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
15910  cCE(textrmsb,  e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
15911  cCE(textrmsh,  e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
15912  cCE(textrmsw,  e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
15913  cCE(tinsrb,    e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
15914  cCE(tinsrh,    e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
15915  cCE(tinsrw,    e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
15916  cCE(tmcr,      e000110, 2, (RIWC_RIWG, RR),        rn_rd),
15917  cCE(tmcrr,     c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
15918  cCE(tmia,      e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
15919  cCE(tmiaph,    e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
15920  cCE(tmiabb,    e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
15921  cCE(tmiabt,    e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
15922  cCE(tmiatb,    e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
15923  cCE(tmiatt,    e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
15924  cCE(tmovmskb,  e100030, 2, (RR, RIWR),             rd_rn),
15925  cCE(tmovmskh,  e500030, 2, (RR, RIWR),             rd_rn),
15926  cCE(tmovmskw,  e900030, 2, (RR, RIWR),             rd_rn),
15927  cCE(tmrc,      e100110, 2, (RR, RIWC_RIWG),        rd_rn),
15928  cCE(tmrrc,     c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
15929  cCE(torcb,     e13f150, 1, (RR),                   iwmmxt_tandorc),
15930  cCE(torch,     e53f150, 1, (RR),                   iwmmxt_tandorc),
15931  cCE(torcw,     e93f150, 1, (RR),                   iwmmxt_tandorc),
15932  cCE(waccb,     e0001c0, 2, (RIWR, RIWR),           rd_rn),
15933  cCE(wacch,     e4001c0, 2, (RIWR, RIWR),           rd_rn),
15934  cCE(waccw,     e8001c0, 2, (RIWR, RIWR),           rd_rn),
15935  cCE(waddbss,   e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15936  cCE(waddb,     e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15937  cCE(waddbus,   e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15938  cCE(waddhss,   e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15939  cCE(waddh,     e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15940  cCE(waddhus,   e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15941  cCE(waddwss,   eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15942  cCE(waddw,     e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15943  cCE(waddwus,   e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15944  cCE(waligni,   e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
15945  cCE(walignr0,  e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15946  cCE(walignr1,  e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15947  cCE(walignr2,  ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15948  cCE(walignr3,  eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15949  cCE(wand,      e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15950  cCE(wandn,     e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15951  cCE(wavg2b,    e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15952  cCE(wavg2br,   e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15953  cCE(wavg2h,    ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15954  cCE(wavg2hr,   ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15955  cCE(wcmpeqb,   e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15956  cCE(wcmpeqh,   e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15957  cCE(wcmpeqw,   e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15958  cCE(wcmpgtub,  e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15959  cCE(wcmpgtuh,  e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15960  cCE(wcmpgtuw,  e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15961  cCE(wcmpgtsb,  e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15962  cCE(wcmpgtsh,  e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15963  cCE(wcmpgtsw,  eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15964  cCE(wldrb,     c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
15965  cCE(wldrh,     c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
15966  cCE(wldrw,     c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
15967  cCE(wldrd,     c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
15968  cCE(wmacs,     e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15969  cCE(wmacsz,    e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15970  cCE(wmacu,     e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15971  cCE(wmacuz,    e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15972  cCE(wmadds,    ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15973  cCE(wmaddu,    e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15974  cCE(wmaxsb,    e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15975  cCE(wmaxsh,    e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15976  cCE(wmaxsw,    ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15977  cCE(wmaxub,    e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15978  cCE(wmaxuh,    e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15979  cCE(wmaxuw,    e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15980  cCE(wminsb,    e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15981  cCE(wminsh,    e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15982  cCE(wminsw,    eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15983  cCE(wminub,    e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15984  cCE(wminuh,    e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15985  cCE(wminuw,    e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15986  cCE(wmov,      e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
15987  cCE(wmulsm,    e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15988  cCE(wmulsl,    e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15989  cCE(wmulum,    e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15990  cCE(wmulul,    e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15991  cCE(wor,       e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15992  cCE(wpackhss,  e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15993  cCE(wpackhus,  e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15994  cCE(wpackwss,  eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15995  cCE(wpackwus,  e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15996  cCE(wpackdss,  ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15997  cCE(wpackdus,  ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
15998  cCE(wrorh,     e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
15999  cCE(wrorhg,    e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16000  cCE(wrorw,     eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16001  cCE(wrorwg,    eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16002  cCE(wrord,     ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16003  cCE(wrordg,    ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16004  cCE(wsadb,     e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16005  cCE(wsadbz,    e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16006  cCE(wsadh,     e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16007  cCE(wsadhz,    e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16008  cCE(wshufh,    e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
16009  cCE(wsllh,     e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16010  cCE(wsllhg,    e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16011  cCE(wsllw,     e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16012  cCE(wsllwg,    e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16013  cCE(wslld,     ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16014  cCE(wslldg,    ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16015  cCE(wsrah,     e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16016  cCE(wsrahg,    e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16017  cCE(wsraw,     e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16018  cCE(wsrawg,    e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16019  cCE(wsrad,     ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16020  cCE(wsradg,    ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16021  cCE(wsrlh,     e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16022  cCE(wsrlhg,    e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16023  cCE(wsrlw,     ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16024  cCE(wsrlwg,    ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16025  cCE(wsrld,     ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
16026  cCE(wsrldg,    ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
16027  cCE(wstrb,     c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
16028  cCE(wstrh,     c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
16029  cCE(wstrw,     c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
16030  cCE(wstrd,     c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
16031  cCE(wsubbss,   e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16032  cCE(wsubb,     e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16033  cCE(wsubbus,   e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16034  cCE(wsubhss,   e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16035  cCE(wsubh,     e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16036  cCE(wsubhus,   e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16037  cCE(wsubwss,   eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16038  cCE(wsubw,     e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16039  cCE(wsubwus,   e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16040  cCE(wunpckehub,e0000c0, 2, (RIWR, RIWR),           rd_rn),
16041  cCE(wunpckehuh,e4000c0, 2, (RIWR, RIWR),           rd_rn),
16042  cCE(wunpckehuw,e8000c0, 2, (RIWR, RIWR),           rd_rn),
16043  cCE(wunpckehsb,e2000c0, 2, (RIWR, RIWR),           rd_rn),
16044  cCE(wunpckehsh,e6000c0, 2, (RIWR, RIWR),           rd_rn),
16045  cCE(wunpckehsw,ea000c0, 2, (RIWR, RIWR),           rd_rn),
16046  cCE(wunpckihb, e1000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16047  cCE(wunpckihh, e5000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16048  cCE(wunpckihw, e9000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16049  cCE(wunpckelub,e0000e0, 2, (RIWR, RIWR),           rd_rn),
16050  cCE(wunpckeluh,e4000e0, 2, (RIWR, RIWR),           rd_rn),
16051  cCE(wunpckeluw,e8000e0, 2, (RIWR, RIWR),           rd_rn),
16052  cCE(wunpckelsb,e2000e0, 2, (RIWR, RIWR),           rd_rn),
16053  cCE(wunpckelsh,e6000e0, 2, (RIWR, RIWR),           rd_rn),
16054  cCE(wunpckelsw,ea000e0, 2, (RIWR, RIWR),           rd_rn),
16055  cCE(wunpckilb, e1000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16056  cCE(wunpckilh, e5000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16057  cCE(wunpckilw, e9000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16058  cCE(wxor,      e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16059  cCE(wzero,     e300000, 1, (RIWR),                 iwmmxt_wzero),
16060
16061 #undef ARM_VARIANT
16062 #define ARM_VARIANT &arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
16063  cCE(torvscb,   e13f190, 1, (RR),                   iwmmxt_tandorc),
16064  cCE(torvsch,   e53f190, 1, (RR),                   iwmmxt_tandorc),
16065  cCE(torvscw,   e93f190, 1, (RR),                   iwmmxt_tandorc),
16066  cCE(wabsb,     e2001c0, 2, (RIWR, RIWR),           rd_rn),
16067  cCE(wabsh,     e6001c0, 2, (RIWR, RIWR),           rd_rn),
16068  cCE(wabsw,     ea001c0, 2, (RIWR, RIWR),           rd_rn),
16069  cCE(wabsdiffb, e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16070  cCE(wabsdiffh, e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16071  cCE(wabsdiffw, e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16072  cCE(waddbhusl, e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16073  cCE(waddbhusm, e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16074  cCE(waddhc,    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16075  cCE(waddwc,    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16076  cCE(waddsubhx, ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16077  cCE(wavg4,     e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16078  cCE(wavg4r,    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16079  cCE(wmaddsn,   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16080  cCE(wmaddsx,   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16081  cCE(wmaddun,   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16082  cCE(wmaddux,   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16083  cCE(wmerge,    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
16084  cCE(wmiabb,    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16085  cCE(wmiabt,    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16086  cCE(wmiatb,    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16087  cCE(wmiatt,    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16088  cCE(wmiabbn,   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16089  cCE(wmiabtn,   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16090  cCE(wmiatbn,   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16091  cCE(wmiattn,   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16092  cCE(wmiawbb,   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16093  cCE(wmiawbt,   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16094  cCE(wmiawtb,   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16095  cCE(wmiawtt,   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16096  cCE(wmiawbbn,  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16097  cCE(wmiawbtn,  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16098  cCE(wmiawtbn,  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16099  cCE(wmiawttn,  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16100  cCE(wmulsmr,   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16101  cCE(wmulumr,   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16102  cCE(wmulwumr,  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16103  cCE(wmulwsmr,  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16104  cCE(wmulwum,   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16105  cCE(wmulwsm,   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16106  cCE(wmulwl,    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16107  cCE(wqmiabb,   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16108  cCE(wqmiabt,   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16109  cCE(wqmiatb,   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16110  cCE(wqmiatt,   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16111  cCE(wqmiabbn,  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16112  cCE(wqmiabtn,  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16113  cCE(wqmiatbn,  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16114  cCE(wqmiattn,  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16115  cCE(wqmulm,    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16116  cCE(wqmulmr,   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16117  cCE(wqmulwm,   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16118  cCE(wqmulwmr,  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16119  cCE(wsubaddhx, ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
16120
16121 #undef ARM_VARIANT
16122 #define ARM_VARIANT &arm_cext_maverick /* Cirrus Maverick instructions. */
16123  cCE(cfldrs,    c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
16124  cCE(cfldrd,    c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
16125  cCE(cfldr32,   c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
16126  cCE(cfldr64,   c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
16127  cCE(cfstrs,    c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
16128  cCE(cfstrd,    c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
16129  cCE(cfstr32,   c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
16130  cCE(cfstr64,   c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
16131  cCE(cfmvsr,    e000450, 2, (RMF, RR),                rn_rd),
16132  cCE(cfmvrs,    e100450, 2, (RR, RMF),                rd_rn),
16133  cCE(cfmvdlr,   e000410, 2, (RMD, RR),                rn_rd),
16134  cCE(cfmvrdl,   e100410, 2, (RR, RMD),                rd_rn),
16135  cCE(cfmvdhr,   e000430, 2, (RMD, RR),                rn_rd),
16136  cCE(cfmvrdh,   e100430, 2, (RR, RMD),                rd_rn),
16137  cCE(cfmv64lr,  e000510, 2, (RMDX, RR),               rn_rd),
16138  cCE(cfmvr64l,  e100510, 2, (RR, RMDX),               rd_rn),
16139  cCE(cfmv64hr,  e000530, 2, (RMDX, RR),               rn_rd),
16140  cCE(cfmvr64h,  e100530, 2, (RR, RMDX),               rd_rn),
16141  cCE(cfmval32,  e200440, 2, (RMAX, RMFX),             rd_rn),
16142  cCE(cfmv32al,  e100440, 2, (RMFX, RMAX),             rd_rn),
16143  cCE(cfmvam32,  e200460, 2, (RMAX, RMFX),             rd_rn),
16144  cCE(cfmv32am,  e100460, 2, (RMFX, RMAX),             rd_rn),
16145  cCE(cfmvah32,  e200480, 2, (RMAX, RMFX),             rd_rn),
16146  cCE(cfmv32ah,  e100480, 2, (RMFX, RMAX),             rd_rn),
16147  cCE(cfmva32,   e2004a0, 2, (RMAX, RMFX),             rd_rn),
16148  cCE(cfmv32a,   e1004a0, 2, (RMFX, RMAX),             rd_rn),
16149  cCE(cfmva64,   e2004c0, 2, (RMAX, RMDX),             rd_rn),
16150  cCE(cfmv64a,   e1004c0, 2, (RMDX, RMAX),             rd_rn),
16151  cCE(cfmvsc32,  e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
16152  cCE(cfmv32sc,  e1004e0, 2, (RMDX, RMDS),             rd),
16153  cCE(cfcpys,    e000400, 2, (RMF, RMF),               rd_rn),
16154  cCE(cfcpyd,    e000420, 2, (RMD, RMD),               rd_rn),
16155  cCE(cfcvtsd,   e000460, 2, (RMD, RMF),               rd_rn),
16156  cCE(cfcvtds,   e000440, 2, (RMF, RMD),               rd_rn),
16157  cCE(cfcvt32s,  e000480, 2, (RMF, RMFX),              rd_rn),
16158  cCE(cfcvt32d,  e0004a0, 2, (RMD, RMFX),              rd_rn),
16159  cCE(cfcvt64s,  e0004c0, 2, (RMF, RMDX),              rd_rn),
16160  cCE(cfcvt64d,  e0004e0, 2, (RMD, RMDX),              rd_rn),
16161  cCE(cfcvts32,  e100580, 2, (RMFX, RMF),              rd_rn),
16162  cCE(cfcvtd32,  e1005a0, 2, (RMFX, RMD),              rd_rn),
16163  cCE(cftruncs32,e1005c0, 2, (RMFX, RMF),              rd_rn),
16164  cCE(cftruncd32,e1005e0, 2, (RMFX, RMD),              rd_rn),
16165  cCE(cfrshl32,  e000550, 3, (RMFX, RMFX, RR),         mav_triple),
16166  cCE(cfrshl64,  e000570, 3, (RMDX, RMDX, RR),         mav_triple),
16167  cCE(cfsh32,    e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
16168  cCE(cfsh64,    e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
16169  cCE(cfcmps,    e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
16170  cCE(cfcmpd,    e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
16171  cCE(cfcmp32,   e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
16172  cCE(cfcmp64,   e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
16173  cCE(cfabss,    e300400, 2, (RMF, RMF),               rd_rn),
16174  cCE(cfabsd,    e300420, 2, (RMD, RMD),               rd_rn),
16175  cCE(cfnegs,    e300440, 2, (RMF, RMF),               rd_rn),
16176  cCE(cfnegd,    e300460, 2, (RMD, RMD),               rd_rn),
16177  cCE(cfadds,    e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
16178  cCE(cfaddd,    e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
16179  cCE(cfsubs,    e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
16180  cCE(cfsubd,    e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
16181  cCE(cfmuls,    e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
16182  cCE(cfmuld,    e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
16183  cCE(cfabs32,   e300500, 2, (RMFX, RMFX),             rd_rn),
16184  cCE(cfabs64,   e300520, 2, (RMDX, RMDX),             rd_rn),
16185  cCE(cfneg32,   e300540, 2, (RMFX, RMFX),             rd_rn),
16186  cCE(cfneg64,   e300560, 2, (RMDX, RMDX),             rd_rn),
16187  cCE(cfadd32,   e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
16188  cCE(cfadd64,   e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
16189  cCE(cfsub32,   e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
16190  cCE(cfsub64,   e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
16191  cCE(cfmul32,   e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
16192  cCE(cfmul64,   e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
16193  cCE(cfmac32,   e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
16194  cCE(cfmsc32,   e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
16195  cCE(cfmadd32,  e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
16196  cCE(cfmsub32,  e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
16197  cCE(cfmadda32, e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
16198  cCE(cfmsuba32, e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
16199 };
16200 #undef ARM_VARIANT
16201 #undef THUMB_VARIANT
16202 #undef TCE
16203 #undef TCM
16204 #undef TUE
16205 #undef TUF
16206 #undef TCC
16207 #undef cCE
16208 #undef cCL
16209 #undef C3E
16210 #undef CE
16211 #undef CM
16212 #undef UE
16213 #undef UF
16214 #undef UT
16215 #undef NUF
16216 #undef nUF
16217 #undef NCE
16218 #undef nCE
16219 #undef OPS0
16220 #undef OPS1
16221 #undef OPS2
16222 #undef OPS3
16223 #undef OPS4
16224 #undef OPS5
16225 #undef OPS6
16226 #undef do_0
16227 \f
16228 /* MD interface: bits in the object file.  */
16229
16230 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
16231    for use in the a.out file, and stores them in the array pointed to by buf.
16232    This knows about the endian-ness of the target machine and does
16233    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
16234    2 (short) and 4 (long)  Floating numbers are put out as a series of
16235    LITTLENUMS (shorts, here at least).  */
16236
16237 void
16238 md_number_to_chars (char * buf, valueT val, int n)
16239 {
16240   if (target_big_endian)
16241     number_to_chars_bigendian (buf, val, n);
16242   else
16243     number_to_chars_littleendian (buf, val, n);
16244 }
16245
16246 static valueT
16247 md_chars_to_number (char * buf, int n)
16248 {
16249   valueT result = 0;
16250   unsigned char * where = (unsigned char *) buf;
16251
16252   if (target_big_endian)
16253     {
16254       while (n--)
16255         {
16256           result <<= 8;
16257           result |= (*where++ & 255);
16258         }
16259     }
16260   else
16261     {
16262       while (n--)
16263         {
16264           result <<= 8;
16265           result |= (where[n] & 255);
16266         }
16267     }
16268
16269   return result;
16270 }
16271
16272 /* MD interface: Sections.  */
16273
16274 /* Estimate the size of a frag before relaxing.  Assume everything fits in
16275    2 bytes.  */
16276
16277 int
16278 md_estimate_size_before_relax (fragS * fragp,
16279                                segT    segtype ATTRIBUTE_UNUSED)
16280 {
16281   fragp->fr_var = 2;
16282   return 2;
16283 }
16284
16285 /* Convert a machine dependent frag.  */
16286
16287 void
16288 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
16289 {
16290   unsigned long insn;
16291   unsigned long old_op;
16292   char *buf;
16293   expressionS exp;
16294   fixS *fixp;
16295   int reloc_type;
16296   int pc_rel;
16297   int opcode;
16298
16299   buf = fragp->fr_literal + fragp->fr_fix;
16300
16301   old_op = bfd_get_16(abfd, buf);
16302   if (fragp->fr_symbol) {
16303       exp.X_op = O_symbol;
16304       exp.X_add_symbol = fragp->fr_symbol;
16305   } else {
16306       exp.X_op = O_constant;
16307   }
16308   exp.X_add_number = fragp->fr_offset;
16309   opcode = fragp->fr_subtype;
16310   switch (opcode)
16311     {
16312     case T_MNEM_ldr_pc:
16313     case T_MNEM_ldr_pc2:
16314     case T_MNEM_ldr_sp:
16315     case T_MNEM_str_sp:
16316     case T_MNEM_ldr:
16317     case T_MNEM_ldrb:
16318     case T_MNEM_ldrh:
16319     case T_MNEM_str:
16320     case T_MNEM_strb:
16321     case T_MNEM_strh:
16322       if (fragp->fr_var == 4)
16323         {
16324           insn = THUMB_OP32(opcode);
16325           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
16326             {
16327               insn |= (old_op & 0x700) << 4;
16328             }
16329           else
16330             {
16331               insn |= (old_op & 7) << 12;
16332               insn |= (old_op & 0x38) << 13;
16333             }
16334           insn |= 0x00000c00;
16335           put_thumb32_insn (buf, insn);
16336           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
16337         }
16338       else
16339         {
16340           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
16341         }
16342       pc_rel = (opcode == T_MNEM_ldr_pc2);
16343       break;
16344     case T_MNEM_adr:
16345       if (fragp->fr_var == 4)
16346         {
16347           insn = THUMB_OP32 (opcode);
16348           insn |= (old_op & 0xf0) << 4;
16349           put_thumb32_insn (buf, insn);
16350           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
16351         }
16352       else
16353         {
16354           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
16355           exp.X_add_number -= 4;
16356         }
16357       pc_rel = 1;
16358       break;
16359     case T_MNEM_mov:
16360     case T_MNEM_movs:
16361     case T_MNEM_cmp:
16362     case T_MNEM_cmn:
16363       if (fragp->fr_var == 4)
16364         {
16365           int r0off = (opcode == T_MNEM_mov
16366                        || opcode == T_MNEM_movs) ? 0 : 8;
16367           insn = THUMB_OP32 (opcode);
16368           insn = (insn & 0xe1ffffff) | 0x10000000;
16369           insn |= (old_op & 0x700) << r0off;
16370           put_thumb32_insn (buf, insn);
16371           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
16372         }
16373       else
16374         {
16375           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
16376         }
16377       pc_rel = 0;
16378       break;
16379     case T_MNEM_b:
16380       if (fragp->fr_var == 4)
16381         {
16382           insn = THUMB_OP32(opcode);
16383           put_thumb32_insn (buf, insn);
16384           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
16385         }
16386       else
16387         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
16388       pc_rel = 1;
16389       break;
16390     case T_MNEM_bcond:
16391       if (fragp->fr_var == 4)
16392         {
16393           insn = THUMB_OP32(opcode);
16394           insn |= (old_op & 0xf00) << 14;
16395           put_thumb32_insn (buf, insn);
16396           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
16397         }
16398       else
16399         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
16400       pc_rel = 1;
16401       break;
16402     case T_MNEM_add_sp:
16403     case T_MNEM_add_pc:
16404     case T_MNEM_inc_sp:
16405     case T_MNEM_dec_sp:
16406       if (fragp->fr_var == 4)
16407         {
16408           /* ??? Choose between add and addw.  */
16409           insn = THUMB_OP32 (opcode);
16410           insn |= (old_op & 0xf0) << 4;
16411           put_thumb32_insn (buf, insn);
16412           if (opcode == T_MNEM_add_pc)
16413             reloc_type = BFD_RELOC_ARM_T32_IMM12;
16414           else
16415             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
16416         }
16417       else
16418         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
16419       pc_rel = 0;
16420       break;
16421
16422     case T_MNEM_addi:
16423     case T_MNEM_addis:
16424     case T_MNEM_subi:
16425     case T_MNEM_subis:
16426       if (fragp->fr_var == 4)
16427         {
16428           insn = THUMB_OP32 (opcode);
16429           insn |= (old_op & 0xf0) << 4;
16430           insn |= (old_op & 0xf) << 16;
16431           put_thumb32_insn (buf, insn);
16432           if (insn & (1 << 20))
16433             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
16434           else
16435             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
16436         }
16437       else
16438         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
16439       pc_rel = 0;
16440       break;
16441     default:
16442       abort();
16443     }
16444   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
16445                       reloc_type);
16446   fixp->fx_file = fragp->fr_file;
16447   fixp->fx_line = fragp->fr_line;
16448   fragp->fr_fix += fragp->fr_var;
16449 }
16450
16451 /* Return the size of a relaxable immediate operand instruction.
16452    SHIFT and SIZE specify the form of the allowable immediate.  */
16453 static int
16454 relax_immediate (fragS *fragp, int size, int shift)
16455 {
16456   offsetT offset;
16457   offsetT mask;
16458   offsetT low;
16459
16460   /* ??? Should be able to do better than this.  */
16461   if (fragp->fr_symbol)
16462     return 4;
16463
16464   low = (1 << shift) - 1;
16465   mask = (1 << (shift + size)) - (1 << shift);
16466   offset = fragp->fr_offset;
16467   /* Force misaligned offsets to 32-bit variant.  */
16468   if (offset & low)
16469     return -4;
16470   if (offset & ~mask)
16471     return 4;
16472   return 2;
16473 }
16474
16475 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
16476    load.  */
16477 static int
16478 relax_adr (fragS *fragp, asection *sec)
16479 {
16480   addressT addr;
16481   offsetT val;
16482
16483   /* Assume worst case for symbols not known to be in the same section.  */
16484   if (!S_IS_DEFINED(fragp->fr_symbol)
16485       || sec != S_GET_SEGMENT (fragp->fr_symbol))
16486     return 4;
16487
16488   val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
16489   addr = fragp->fr_address + fragp->fr_fix;
16490   addr = (addr + 4) & ~3;
16491   /* Fix the insn as the 4-byte version if the target address is not
16492      sufficiently aligned.  This is prevents an infinite loop when two
16493      instructions have contradictory range/alignment requirements.  */
16494   if (val & 3)
16495     return -4;
16496   val -= addr;
16497   if (val < 0 || val > 1020)
16498     return 4;
16499   return 2;
16500 }
16501
16502 /* Return the size of a relaxable add/sub immediate instruction.  */
16503 static int
16504 relax_addsub (fragS *fragp, asection *sec)
16505 {
16506   char *buf;
16507   int op;
16508
16509   buf = fragp->fr_literal + fragp->fr_fix;
16510   op = bfd_get_16(sec->owner, buf);
16511   if ((op & 0xf) == ((op >> 4) & 0xf))
16512     return relax_immediate (fragp, 8, 0);
16513   else
16514     return relax_immediate (fragp, 3, 0);
16515 }
16516
16517
16518 /* Return the size of a relaxable branch instruction.  BITS is the
16519    size of the offset field in the narrow instruction.  */
16520
16521 static int
16522 relax_branch (fragS *fragp, asection *sec, int bits)
16523 {
16524   addressT addr;
16525   offsetT val;
16526   offsetT limit;
16527
16528   /* Assume worst case for symbols not known to be in the same section.  */
16529   if (!S_IS_DEFINED(fragp->fr_symbol)
16530       || sec != S_GET_SEGMENT (fragp->fr_symbol))
16531     return 4;
16532
16533   val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
16534   addr = fragp->fr_address + fragp->fr_fix + 4;
16535   val -= addr;
16536
16537   /* Offset is a signed value *2 */
16538   limit = 1 << bits;
16539   if (val >= limit || val < -limit)
16540     return 4;
16541   return 2;
16542 }
16543
16544
16545 /* Relax a machine dependent frag.  This returns the amount by which
16546    the current size of the frag should change.  */
16547
16548 int
16549 arm_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
16550 {
16551   int oldsize;
16552   int newsize;
16553
16554   oldsize = fragp->fr_var;
16555   switch (fragp->fr_subtype)
16556     {
16557     case T_MNEM_ldr_pc2:
16558       newsize = relax_adr(fragp, sec);
16559       break;
16560     case T_MNEM_ldr_pc:
16561     case T_MNEM_ldr_sp:
16562     case T_MNEM_str_sp:
16563       newsize = relax_immediate(fragp, 8, 2);
16564       break;
16565     case T_MNEM_ldr:
16566     case T_MNEM_str:
16567       newsize = relax_immediate(fragp, 5, 2);
16568       break;
16569     case T_MNEM_ldrh:
16570     case T_MNEM_strh:
16571       newsize = relax_immediate(fragp, 5, 1);
16572       break;
16573     case T_MNEM_ldrb:
16574     case T_MNEM_strb:
16575       newsize = relax_immediate(fragp, 5, 0);
16576       break;
16577     case T_MNEM_adr:
16578       newsize = relax_adr(fragp, sec);
16579       break;
16580     case T_MNEM_mov:
16581     case T_MNEM_movs:
16582     case T_MNEM_cmp:
16583     case T_MNEM_cmn:
16584       newsize = relax_immediate(fragp, 8, 0);
16585       break;
16586     case T_MNEM_b:
16587       newsize = relax_branch(fragp, sec, 11);
16588       break;
16589     case T_MNEM_bcond:
16590       newsize = relax_branch(fragp, sec, 8);
16591       break;
16592     case T_MNEM_add_sp:
16593     case T_MNEM_add_pc:
16594       newsize = relax_immediate (fragp, 8, 2);
16595       break;
16596     case T_MNEM_inc_sp:
16597     case T_MNEM_dec_sp:
16598       newsize = relax_immediate (fragp, 7, 2);
16599       break;
16600     case T_MNEM_addi:
16601     case T_MNEM_addis:
16602     case T_MNEM_subi:
16603     case T_MNEM_subis:
16604       newsize = relax_addsub (fragp, sec);
16605       break;
16606     default:
16607       abort();
16608     }
16609   if (newsize < 0)
16610     {
16611       fragp->fr_var = -newsize;
16612       md_convert_frag (sec->owner, sec, fragp);
16613       frag_wane(fragp);
16614       return -(newsize + oldsize);
16615     }
16616   fragp->fr_var = newsize;
16617   return newsize - oldsize;
16618 }
16619
16620 /* Round up a section size to the appropriate boundary.  */
16621
16622 valueT
16623 md_section_align (segT   segment ATTRIBUTE_UNUSED,
16624                   valueT size)
16625 {
16626 #ifdef OBJ_ELF
16627   return size;
16628 #else
16629   /* Round all sects to multiple of 4.  */
16630   return (size + 3) & ~3;
16631 #endif
16632 }
16633
16634 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
16635    of an rs_align_code fragment.  */
16636
16637 void
16638 arm_handle_align (fragS * fragP)
16639 {
16640   static char const arm_noop[4] = { 0x00, 0x00, 0xa0, 0xe1 };
16641   static char const thumb_noop[2] = { 0xc0, 0x46 };
16642   static char const arm_bigend_noop[4] = { 0xe1, 0xa0, 0x00, 0x00 };
16643   static char const thumb_bigend_noop[2] = { 0x46, 0xc0 };
16644
16645   int bytes, fix, noop_size;
16646   char * p;
16647   const char * noop;
16648
16649   if (fragP->fr_type != rs_align_code)
16650     return;
16651
16652   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
16653   p = fragP->fr_literal + fragP->fr_fix;
16654   fix = 0;
16655
16656   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
16657     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
16658
16659   if (fragP->tc_frag_data)
16660     {
16661       if (target_big_endian)
16662         noop = thumb_bigend_noop;
16663       else
16664         noop = thumb_noop;
16665       noop_size = sizeof (thumb_noop);
16666     }
16667   else
16668     {
16669       if (target_big_endian)
16670         noop = arm_bigend_noop;
16671       else
16672         noop = arm_noop;
16673       noop_size = sizeof (arm_noop);
16674     }
16675
16676   if (bytes & (noop_size - 1))
16677     {
16678       fix = bytes & (noop_size - 1);
16679       memset (p, 0, fix);
16680       p += fix;
16681       bytes -= fix;
16682     }
16683
16684   while (bytes >= noop_size)
16685     {
16686       memcpy (p, noop, noop_size);
16687       p += noop_size;
16688       bytes -= noop_size;
16689       fix += noop_size;
16690     }
16691
16692   fragP->fr_fix += fix;
16693   fragP->fr_var = noop_size;
16694 }
16695
16696 /* Called from md_do_align.  Used to create an alignment
16697    frag in a code section.  */
16698
16699 void
16700 arm_frag_align_code (int n, int max)
16701 {
16702   char * p;
16703
16704   /* We assume that there will never be a requirement
16705      to support alignments greater than 32 bytes.  */
16706   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
16707     as_fatal (_("alignments greater than 32 bytes not supported in .text sections."));
16708
16709   p = frag_var (rs_align_code,
16710                 MAX_MEM_FOR_RS_ALIGN_CODE,
16711                 1,
16712                 (relax_substateT) max,
16713                 (symbolS *) NULL,
16714                 (offsetT) n,
16715                 (char *) NULL);
16716   *p = 0;
16717 }
16718
16719 /* Perform target specific initialisation of a frag.  */
16720
16721 void
16722 arm_init_frag (fragS * fragP)
16723 {
16724   /* Record whether this frag is in an ARM or a THUMB area.  */
16725   fragP->tc_frag_data = thumb_mode;
16726 }
16727
16728 #ifdef OBJ_ELF
16729 /* When we change sections we need to issue a new mapping symbol.  */
16730
16731 void
16732 arm_elf_change_section (void)
16733 {
16734   flagword flags;
16735   segment_info_type *seginfo;
16736
16737   /* Link an unlinked unwind index table section to the .text section.  */
16738   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
16739       && elf_linked_to_section (now_seg) == NULL)
16740     elf_linked_to_section (now_seg) = text_section;
16741
16742   if (!SEG_NORMAL (now_seg))
16743     return;
16744
16745   flags = bfd_get_section_flags (stdoutput, now_seg);
16746
16747   /* We can ignore sections that only contain debug info.  */
16748   if ((flags & SEC_ALLOC) == 0)
16749     return;
16750
16751   seginfo = seg_info (now_seg);
16752   mapstate = seginfo->tc_segment_info_data.mapstate;
16753   marked_pr_dependency = seginfo->tc_segment_info_data.marked_pr_dependency;
16754 }
16755
16756 int
16757 arm_elf_section_type (const char * str, size_t len)
16758 {
16759   if (len == 5 && strncmp (str, "exidx", 5) == 0)
16760     return SHT_ARM_EXIDX;
16761
16762   return -1;
16763 }
16764 \f
16765 /* Code to deal with unwinding tables.  */
16766
16767 static void add_unwind_adjustsp (offsetT);
16768
16769 /* Cenerate and deferred unwind frame offset.  */
16770
16771 static void
16772 flush_pending_unwind (void)
16773 {
16774   offsetT offset;
16775
16776   offset = unwind.pending_offset;
16777   unwind.pending_offset = 0;
16778   if (offset != 0)
16779     add_unwind_adjustsp (offset);
16780 }
16781
16782 /* Add an opcode to this list for this function.  Two-byte opcodes should
16783    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
16784    order.  */
16785
16786 static void
16787 add_unwind_opcode (valueT op, int length)
16788 {
16789   /* Add any deferred stack adjustment.  */
16790   if (unwind.pending_offset)
16791     flush_pending_unwind ();
16792
16793   unwind.sp_restored = 0;
16794
16795   if (unwind.opcode_count + length > unwind.opcode_alloc)
16796     {
16797       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
16798       if (unwind.opcodes)
16799         unwind.opcodes = xrealloc (unwind.opcodes,
16800                                    unwind.opcode_alloc);
16801       else
16802         unwind.opcodes = xmalloc (unwind.opcode_alloc);
16803     }
16804   while (length > 0)
16805     {
16806       length--;
16807       unwind.opcodes[unwind.opcode_count] = op & 0xff;
16808       op >>= 8;
16809       unwind.opcode_count++;
16810     }
16811 }
16812
16813 /* Add unwind opcodes to adjust the stack pointer.  */
16814
16815 static void
16816 add_unwind_adjustsp (offsetT offset)
16817 {
16818   valueT op;
16819
16820   if (offset > 0x200)
16821     {
16822       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
16823       char bytes[5];
16824       int n;
16825       valueT o;
16826
16827       /* Long form: 0xb2, uleb128.  */
16828       /* This might not fit in a word so add the individual bytes,
16829          remembering the list is built in reverse order.  */
16830       o = (valueT) ((offset - 0x204) >> 2);
16831       if (o == 0)
16832         add_unwind_opcode (0, 1);
16833
16834       /* Calculate the uleb128 encoding of the offset.  */
16835       n = 0;
16836       while (o)
16837         {
16838           bytes[n] = o & 0x7f;
16839           o >>= 7;
16840           if (o)
16841             bytes[n] |= 0x80;
16842           n++;
16843         }
16844       /* Add the insn.  */
16845       for (; n; n--)
16846         add_unwind_opcode (bytes[n - 1], 1);
16847       add_unwind_opcode (0xb2, 1);
16848     }
16849   else if (offset > 0x100)
16850     {
16851       /* Two short opcodes.  */
16852       add_unwind_opcode (0x3f, 1);
16853       op = (offset - 0x104) >> 2;
16854       add_unwind_opcode (op, 1);
16855     }
16856   else if (offset > 0)
16857     {
16858       /* Short opcode.  */
16859       op = (offset - 4) >> 2;
16860       add_unwind_opcode (op, 1);
16861     }
16862   else if (offset < 0)
16863     {
16864       offset = -offset;
16865       while (offset > 0x100)
16866         {
16867           add_unwind_opcode (0x7f, 1);
16868           offset -= 0x100;
16869         }
16870       op = ((offset - 4) >> 2) | 0x40;
16871       add_unwind_opcode (op, 1);
16872     }
16873 }
16874
16875 /* Finish the list of unwind opcodes for this function.  */
16876 static void
16877 finish_unwind_opcodes (void)
16878 {
16879   valueT op;
16880
16881   if (unwind.fp_used)
16882     {
16883       /* Adjust sp as neccessary.  */
16884       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
16885       flush_pending_unwind ();
16886
16887       /* After restoring sp from the frame pointer.  */
16888       op = 0x90 | unwind.fp_reg;
16889       add_unwind_opcode (op, 1);
16890     }
16891   else
16892     flush_pending_unwind ();
16893 }
16894
16895
16896 /* Start an exception table entry.  If idx is nonzero this is an index table
16897    entry.  */
16898
16899 static void
16900 start_unwind_section (const segT text_seg, int idx)
16901 {
16902   const char * text_name;
16903   const char * prefix;
16904   const char * prefix_once;
16905   const char * group_name;
16906   size_t prefix_len;
16907   size_t text_len;
16908   char * sec_name;
16909   size_t sec_name_len;
16910   int type;
16911   int flags;
16912   int linkonce;
16913
16914   if (idx)
16915     {
16916       prefix = ELF_STRING_ARM_unwind;
16917       prefix_once = ELF_STRING_ARM_unwind_once;
16918       type = SHT_ARM_EXIDX;
16919     }
16920   else
16921     {
16922       prefix = ELF_STRING_ARM_unwind_info;
16923       prefix_once = ELF_STRING_ARM_unwind_info_once;
16924       type = SHT_PROGBITS;
16925     }
16926
16927   text_name = segment_name (text_seg);
16928   if (streq (text_name, ".text"))
16929     text_name = "";
16930
16931   if (strncmp (text_name, ".gnu.linkonce.t.",
16932                strlen (".gnu.linkonce.t.")) == 0)
16933     {
16934       prefix = prefix_once;
16935       text_name += strlen (".gnu.linkonce.t.");
16936     }
16937
16938   prefix_len = strlen (prefix);
16939   text_len = strlen (text_name);
16940   sec_name_len = prefix_len + text_len;
16941   sec_name = xmalloc (sec_name_len + 1);
16942   memcpy (sec_name, prefix, prefix_len);
16943   memcpy (sec_name + prefix_len, text_name, text_len);
16944   sec_name[prefix_len + text_len] = '\0';
16945
16946   flags = SHF_ALLOC;
16947   linkonce = 0;
16948   group_name = 0;
16949
16950   /* Handle COMDAT group.  */
16951   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
16952     {
16953       group_name = elf_group_name (text_seg);
16954       if (group_name == NULL)
16955         {
16956           as_bad ("Group section `%s' has no group signature",
16957                   segment_name (text_seg));
16958           ignore_rest_of_line ();
16959           return;
16960         }
16961       flags |= SHF_GROUP;
16962       linkonce = 1;
16963     }
16964
16965   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
16966
16967   /* Set the setion link for index tables.  */
16968   if (idx)
16969     elf_linked_to_section (now_seg) = text_seg;
16970 }
16971
16972
16973 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
16974    personality routine data.  Returns zero, or the index table value for
16975    and inline entry.  */
16976
16977 static valueT
16978 create_unwind_entry (int have_data)
16979 {
16980   int size;
16981   addressT where;
16982   char *ptr;
16983   /* The current word of data.  */
16984   valueT data;
16985   /* The number of bytes left in this word.  */
16986   int n;
16987
16988   finish_unwind_opcodes ();
16989
16990   /* Remember the current text section.  */
16991   unwind.saved_seg = now_seg;
16992   unwind.saved_subseg = now_subseg;
16993
16994   start_unwind_section (now_seg, 0);
16995
16996   if (unwind.personality_routine == NULL)
16997     {
16998       if (unwind.personality_index == -2)
16999         {
17000           if (have_data)
17001             as_bad (_("handerdata in cantunwind frame"));
17002           return 1; /* EXIDX_CANTUNWIND.  */
17003         }
17004
17005       /* Use a default personality routine if none is specified.  */
17006       if (unwind.personality_index == -1)
17007         {
17008           if (unwind.opcode_count > 3)
17009             unwind.personality_index = 1;
17010           else
17011             unwind.personality_index = 0;
17012         }
17013
17014       /* Space for the personality routine entry.  */
17015       if (unwind.personality_index == 0)
17016         {
17017           if (unwind.opcode_count > 3)
17018             as_bad (_("too many unwind opcodes for personality routine 0"));
17019
17020           if (!have_data)
17021             {
17022               /* All the data is inline in the index table.  */
17023               data = 0x80;
17024               n = 3;
17025               while (unwind.opcode_count > 0)
17026                 {
17027                   unwind.opcode_count--;
17028                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
17029                   n--;
17030                 }
17031
17032               /* Pad with "finish" opcodes.  */
17033               while (n--)
17034                 data = (data << 8) | 0xb0;
17035
17036               return data;
17037             }
17038           size = 0;
17039         }
17040       else
17041         /* We get two opcodes "free" in the first word.  */
17042         size = unwind.opcode_count - 2;
17043     }
17044   else
17045     /* An extra byte is required for the opcode count.  */
17046     size = unwind.opcode_count + 1;
17047
17048   size = (size + 3) >> 2;
17049   if (size > 0xff)
17050     as_bad (_("too many unwind opcodes"));
17051
17052   frag_align (2, 0, 0);
17053   record_alignment (now_seg, 2);
17054   unwind.table_entry = expr_build_dot ();
17055
17056   /* Allocate the table entry.  */
17057   ptr = frag_more ((size << 2) + 4);
17058   where = frag_now_fix () - ((size << 2) + 4);
17059
17060   switch (unwind.personality_index)
17061     {
17062     case -1:
17063       /* ??? Should this be a PLT generating relocation?  */
17064       /* Custom personality routine.  */
17065       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
17066                BFD_RELOC_ARM_PREL31);
17067
17068       where += 4;
17069       ptr += 4;
17070
17071       /* Set the first byte to the number of additional words.  */
17072       data = size - 1;
17073       n = 3;
17074       break;
17075
17076     /* ABI defined personality routines.  */
17077     case 0:
17078       /* Three opcodes bytes are packed into the first word.  */
17079       data = 0x80;
17080       n = 3;
17081       break;
17082
17083     case 1:
17084     case 2:
17085       /* The size and first two opcode bytes go in the first word.  */
17086       data = ((0x80 + unwind.personality_index) << 8) | size;
17087       n = 2;
17088       break;
17089
17090     default:
17091       /* Should never happen.  */
17092       abort ();
17093     }
17094
17095   /* Pack the opcodes into words (MSB first), reversing the list at the same
17096      time.  */
17097   while (unwind.opcode_count > 0)
17098     {
17099       if (n == 0)
17100         {
17101           md_number_to_chars (ptr, data, 4);
17102           ptr += 4;
17103           n = 4;
17104           data = 0;
17105         }
17106       unwind.opcode_count--;
17107       n--;
17108       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
17109     }
17110
17111   /* Finish off the last word.  */
17112   if (n < 4)
17113     {
17114       /* Pad with "finish" opcodes.  */
17115       while (n--)
17116         data = (data << 8) | 0xb0;
17117
17118       md_number_to_chars (ptr, data, 4);
17119     }
17120
17121   if (!have_data)
17122     {
17123       /* Add an empty descriptor if there is no user-specified data.   */
17124       ptr = frag_more (4);
17125       md_number_to_chars (ptr, 0, 4);
17126     }
17127
17128   return 0;
17129 }
17130
17131 /* Convert REGNAME to a DWARF-2 register number.  */
17132
17133 int
17134 tc_arm_regname_to_dw2regnum (const char *regname)
17135 {
17136   int reg = arm_reg_parse ((char **) &regname, REG_TYPE_RN);
17137
17138   if (reg == FAIL)
17139     return -1;
17140
17141   return reg;
17142 }
17143
17144 /* Initialize the DWARF-2 unwind information for this procedure.  */
17145
17146 void
17147 tc_arm_frame_initial_instructions (void)
17148 {
17149   cfi_add_CFA_def_cfa (REG_SP, 0);
17150 }
17151 #endif /* OBJ_ELF */
17152
17153
17154 /* MD interface: Symbol and relocation handling.  */
17155
17156 /* Return the address within the segment that a PC-relative fixup is
17157    relative to.  For ARM, PC-relative fixups applied to instructions
17158    are generally relative to the location of the fixup plus 8 bytes.
17159    Thumb branches are offset by 4, and Thumb loads relative to PC
17160    require special handling.  */
17161
17162 long
17163 md_pcrel_from_section (fixS * fixP, segT seg)
17164 {
17165   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
17166
17167   /* If this is pc-relative and we are going to emit a relocation
17168      then we just want to put out any pipeline compensation that the linker
17169      will need.  Otherwise we want to use the calculated base.  */
17170   if (fixP->fx_pcrel 
17171       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
17172           || arm_force_relocation (fixP)))
17173     base = 0;
17174
17175   switch (fixP->fx_r_type)
17176     {
17177       /* PC relative addressing on the Thumb is slightly odd as the
17178          bottom two bits of the PC are forced to zero for the
17179          calculation.  This happens *after* application of the
17180          pipeline offset.  However, Thumb adrl already adjusts for
17181          this, so we need not do it again.  */
17182     case BFD_RELOC_ARM_THUMB_ADD:
17183       return base & ~3;
17184
17185     case BFD_RELOC_ARM_THUMB_OFFSET:
17186     case BFD_RELOC_ARM_T32_OFFSET_IMM:
17187     case BFD_RELOC_ARM_T32_ADD_PC12:
17188     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
17189       return (base + 4) & ~3;
17190
17191       /* Thumb branches are simply offset by +4.  */
17192     case BFD_RELOC_THUMB_PCREL_BRANCH7:
17193     case BFD_RELOC_THUMB_PCREL_BRANCH9:
17194     case BFD_RELOC_THUMB_PCREL_BRANCH12:
17195     case BFD_RELOC_THUMB_PCREL_BRANCH20:
17196     case BFD_RELOC_THUMB_PCREL_BRANCH23:
17197     case BFD_RELOC_THUMB_PCREL_BRANCH25:
17198     case BFD_RELOC_THUMB_PCREL_BLX:
17199       return base + 4;
17200
17201       /* ARM mode branches are offset by +8.  However, the Windows CE
17202          loader expects the relocation not to take this into account.  */
17203     case BFD_RELOC_ARM_PCREL_BRANCH:
17204     case BFD_RELOC_ARM_PCREL_CALL:
17205     case BFD_RELOC_ARM_PCREL_JUMP:
17206     case BFD_RELOC_ARM_PCREL_BLX:
17207     case BFD_RELOC_ARM_PLT32:
17208 #ifdef TE_WINCE
17209       return base;
17210 #else
17211       return base + 8;
17212 #endif
17213
17214       /* ARM mode loads relative to PC are also offset by +8.  Unlike
17215          branches, the Windows CE loader *does* expect the relocation
17216          to take this into account.  */
17217     case BFD_RELOC_ARM_OFFSET_IMM:
17218     case BFD_RELOC_ARM_OFFSET_IMM8:
17219     case BFD_RELOC_ARM_HWLITERAL:
17220     case BFD_RELOC_ARM_LITERAL:
17221     case BFD_RELOC_ARM_CP_OFF_IMM:
17222       return base + 8;
17223
17224
17225       /* Other PC-relative relocations are un-offset.  */
17226     default:
17227       return base;
17228     }
17229 }
17230
17231 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
17232    Otherwise we have no need to default values of symbols.  */
17233
17234 symbolS *
17235 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
17236 {
17237 #ifdef OBJ_ELF
17238   if (name[0] == '_' && name[1] == 'G'
17239       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
17240     {
17241       if (!GOT_symbol)
17242         {
17243           if (symbol_find (name))
17244             as_bad ("GOT already in the symbol table");
17245
17246           GOT_symbol = symbol_new (name, undefined_section,
17247                                    (valueT) 0, & zero_address_frag);
17248         }
17249
17250       return GOT_symbol;
17251     }
17252 #endif
17253
17254   return 0;
17255 }
17256
17257 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
17258    computed as two separate immediate values, added together.  We
17259    already know that this value cannot be computed by just one ARM
17260    instruction.  */
17261
17262 static unsigned int
17263 validate_immediate_twopart (unsigned int   val,
17264                             unsigned int * highpart)
17265 {
17266   unsigned int a;
17267   unsigned int i;
17268
17269   for (i = 0; i < 32; i += 2)
17270     if (((a = rotate_left (val, i)) & 0xff) != 0)
17271       {
17272         if (a & 0xff00)
17273           {
17274             if (a & ~ 0xffff)
17275               continue;
17276             * highpart = (a  >> 8) | ((i + 24) << 7);
17277           }
17278         else if (a & 0xff0000)
17279           {
17280             if (a & 0xff000000)
17281               continue;
17282             * highpart = (a >> 16) | ((i + 16) << 7);
17283           }
17284         else
17285           {
17286             assert (a & 0xff000000);
17287             * highpart = (a >> 24) | ((i + 8) << 7);
17288           }
17289
17290         return (a & 0xff) | (i << 7);
17291       }
17292
17293   return FAIL;
17294 }
17295
17296 static int
17297 validate_offset_imm (unsigned int val, int hwse)
17298 {
17299   if ((hwse && val > 255) || val > 4095)
17300     return FAIL;
17301   return val;
17302 }
17303
17304 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
17305    negative immediate constant by altering the instruction.  A bit of
17306    a hack really.
17307         MOV <-> MVN
17308         AND <-> BIC
17309         ADC <-> SBC
17310         by inverting the second operand, and
17311         ADD <-> SUB
17312         CMP <-> CMN
17313         by negating the second operand.  */
17314
17315 static int
17316 negate_data_op (unsigned long * instruction,
17317                 unsigned long   value)
17318 {
17319   int op, new_inst;
17320   unsigned long negated, inverted;
17321
17322   negated = encode_arm_immediate (-value);
17323   inverted = encode_arm_immediate (~value);
17324
17325   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
17326   switch (op)
17327     {
17328       /* First negates.  */
17329     case OPCODE_SUB:             /* ADD <-> SUB  */
17330       new_inst = OPCODE_ADD;
17331       value = negated;
17332       break;
17333
17334     case OPCODE_ADD:
17335       new_inst = OPCODE_SUB;
17336       value = negated;
17337       break;
17338
17339     case OPCODE_CMP:             /* CMP <-> CMN  */
17340       new_inst = OPCODE_CMN;
17341       value = negated;
17342       break;
17343
17344     case OPCODE_CMN:
17345       new_inst = OPCODE_CMP;
17346       value = negated;
17347       break;
17348
17349       /* Now Inverted ops.  */
17350     case OPCODE_MOV:             /* MOV <-> MVN  */
17351       new_inst = OPCODE_MVN;
17352       value = inverted;
17353       break;
17354
17355     case OPCODE_MVN:
17356       new_inst = OPCODE_MOV;
17357       value = inverted;
17358       break;
17359
17360     case OPCODE_AND:             /* AND <-> BIC  */
17361       new_inst = OPCODE_BIC;
17362       value = inverted;
17363       break;
17364
17365     case OPCODE_BIC:
17366       new_inst = OPCODE_AND;
17367       value = inverted;
17368       break;
17369
17370     case OPCODE_ADC:              /* ADC <-> SBC  */
17371       new_inst = OPCODE_SBC;
17372       value = inverted;
17373       break;
17374
17375     case OPCODE_SBC:
17376       new_inst = OPCODE_ADC;
17377       value = inverted;
17378       break;
17379
17380       /* We cannot do anything.  */
17381     default:
17382       return FAIL;
17383     }
17384
17385   if (value == (unsigned) FAIL)
17386     return FAIL;
17387
17388   *instruction &= OPCODE_MASK;
17389   *instruction |= new_inst << DATA_OP_SHIFT;
17390   return value;
17391 }
17392
17393 /* Like negate_data_op, but for Thumb-2.   */
17394
17395 static unsigned int
17396 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
17397 {
17398   int op, new_inst;
17399   int rd;
17400   unsigned int negated, inverted;
17401
17402   negated = encode_thumb32_immediate (-value);
17403   inverted = encode_thumb32_immediate (~value);
17404
17405   rd = (*instruction >> 8) & 0xf;
17406   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
17407   switch (op)
17408     {
17409       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
17410     case T2_OPCODE_SUB:
17411       new_inst = T2_OPCODE_ADD;
17412       value = negated;
17413       break;
17414
17415     case T2_OPCODE_ADD:
17416       new_inst = T2_OPCODE_SUB;
17417       value = negated;
17418       break;
17419
17420       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
17421     case T2_OPCODE_ORR:
17422       new_inst = T2_OPCODE_ORN;
17423       value = inverted;
17424       break;
17425
17426     case T2_OPCODE_ORN:
17427       new_inst = T2_OPCODE_ORR;
17428       value = inverted;
17429       break;
17430
17431       /* AND <-> BIC.  TST has no inverted equivalent.  */
17432     case T2_OPCODE_AND:
17433       new_inst = T2_OPCODE_BIC;
17434       if (rd == 15)
17435         value = FAIL;
17436       else
17437         value = inverted;
17438       break;
17439
17440     case T2_OPCODE_BIC:
17441       new_inst = T2_OPCODE_AND;
17442       value = inverted;
17443       break;
17444
17445       /* ADC <-> SBC  */
17446     case T2_OPCODE_ADC:
17447       new_inst = T2_OPCODE_SBC;
17448       value = inverted;
17449       break;
17450
17451     case T2_OPCODE_SBC:
17452       new_inst = T2_OPCODE_ADC;
17453       value = inverted;
17454       break;
17455
17456       /* We cannot do anything.  */
17457     default:
17458       return FAIL;
17459     }
17460
17461   if (value == (unsigned int)FAIL)
17462     return FAIL;
17463
17464   *instruction &= T2_OPCODE_MASK;
17465   *instruction |= new_inst << T2_DATA_OP_SHIFT;
17466   return value;
17467 }
17468
17469 /* Read a 32-bit thumb instruction from buf.  */
17470 static unsigned long
17471 get_thumb32_insn (char * buf)
17472 {
17473   unsigned long insn;
17474   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
17475   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
17476
17477   return insn;
17478 }
17479
17480
17481 /* We usually want to set the low bit on the address of thumb function
17482    symbols.  In particular .word foo - . should have the low bit set.
17483    Generic code tries to fold the difference of two symbols to
17484    a constant.  Prevent this and force a relocation when the first symbols
17485    is a thumb function.  */
17486 int
17487 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
17488 {
17489   if (op == O_subtract
17490       && l->X_op == O_symbol
17491       && r->X_op == O_symbol
17492       && THUMB_IS_FUNC (l->X_add_symbol))
17493     {
17494       l->X_op = O_subtract;
17495       l->X_op_symbol = r->X_add_symbol;
17496       l->X_add_number -= r->X_add_number;
17497       return 1;
17498     }
17499   /* Process as normal.  */
17500   return 0;
17501 }
17502
17503 void
17504 md_apply_fix (fixS *    fixP,
17505                valueT * valP,
17506                segT     seg)
17507 {
17508   offsetT        value = * valP;
17509   offsetT        newval;
17510   unsigned int   newimm;
17511   unsigned long  temp;
17512   int            sign;
17513   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
17514
17515   assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
17516
17517   /* Note whether this will delete the relocation.  */
17518
17519   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
17520     fixP->fx_done = 1;
17521
17522   /* On a 64-bit host, silently truncate 'value' to 32 bits for
17523      consistency with the behavior on 32-bit hosts.  Remember value
17524      for emit_reloc.  */
17525   value &= 0xffffffff;
17526   value ^= 0x80000000;
17527   value -= 0x80000000; 
17528
17529   *valP = value;
17530   fixP->fx_addnumber = value;
17531
17532   /* Same treatment for fixP->fx_offset.  */
17533   fixP->fx_offset &= 0xffffffff;
17534   fixP->fx_offset ^= 0x80000000;
17535   fixP->fx_offset -= 0x80000000;
17536
17537   switch (fixP->fx_r_type)
17538     {
17539     case BFD_RELOC_NONE:
17540       /* This will need to go in the object file.  */
17541       fixP->fx_done = 0;
17542       break;
17543
17544     case BFD_RELOC_ARM_IMMEDIATE:
17545       /* We claim that this fixup has been processed here,
17546          even if in fact we generate an error because we do
17547          not have a reloc for it, so tc_gen_reloc will reject it.  */
17548       fixP->fx_done = 1;
17549
17550       if (fixP->fx_addsy
17551           && ! S_IS_DEFINED (fixP->fx_addsy))
17552         {
17553           as_bad_where (fixP->fx_file, fixP->fx_line,
17554                         _("undefined symbol %s used as an immediate value"),
17555                         S_GET_NAME (fixP->fx_addsy));
17556           break;
17557         }
17558
17559       newimm = encode_arm_immediate (value);
17560       temp = md_chars_to_number (buf, INSN_SIZE);
17561
17562       /* If the instruction will fail, see if we can fix things up by
17563          changing the opcode.  */
17564       if (newimm == (unsigned int) FAIL
17565           && (newimm = negate_data_op (&temp, value)) == (unsigned int) FAIL)
17566         {
17567           as_bad_where (fixP->fx_file, fixP->fx_line,
17568                         _("invalid constant (%lx) after fixup"),
17569                         (unsigned long) value);
17570           break;
17571         }
17572
17573       newimm |= (temp & 0xfffff000);
17574       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
17575       break;
17576
17577     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
17578       {
17579         unsigned int highpart = 0;
17580         unsigned int newinsn  = 0xe1a00000; /* nop.  */
17581
17582         newimm = encode_arm_immediate (value);
17583         temp = md_chars_to_number (buf, INSN_SIZE);
17584
17585         /* If the instruction will fail, see if we can fix things up by
17586            changing the opcode.  */
17587         if (newimm == (unsigned int) FAIL
17588             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
17589           {
17590             /* No ?  OK - try using two ADD instructions to generate
17591                the value.  */
17592             newimm = validate_immediate_twopart (value, & highpart);
17593
17594             /* Yes - then make sure that the second instruction is
17595                also an add.  */
17596             if (newimm != (unsigned int) FAIL)
17597               newinsn = temp;
17598             /* Still No ?  Try using a negated value.  */
17599             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
17600               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
17601             /* Otherwise - give up.  */
17602             else
17603               {
17604                 as_bad_where (fixP->fx_file, fixP->fx_line,
17605                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
17606                               (long) value);
17607                 break;
17608               }
17609
17610             /* Replace the first operand in the 2nd instruction (which
17611                is the PC) with the destination register.  We have
17612                already added in the PC in the first instruction and we
17613                do not want to do it again.  */
17614             newinsn &= ~ 0xf0000;
17615             newinsn |= ((newinsn & 0x0f000) << 4);
17616           }
17617
17618         newimm |= (temp & 0xfffff000);
17619         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
17620
17621         highpart |= (newinsn & 0xfffff000);
17622         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
17623       }
17624       break;
17625
17626     case BFD_RELOC_ARM_OFFSET_IMM:
17627       if (!fixP->fx_done && seg->use_rela_p)
17628         value = 0;
17629
17630     case BFD_RELOC_ARM_LITERAL:
17631       sign = value >= 0;
17632
17633       if (value < 0)
17634         value = - value;
17635
17636       if (validate_offset_imm (value, 0) == FAIL)
17637         {
17638           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
17639             as_bad_where (fixP->fx_file, fixP->fx_line,
17640                           _("invalid literal constant: pool needs to be closer"));
17641           else
17642             as_bad_where (fixP->fx_file, fixP->fx_line,
17643                           _("bad immediate value for offset (%ld)"),
17644                           (long) value);
17645           break;
17646         }
17647
17648       newval = md_chars_to_number (buf, INSN_SIZE);
17649       newval &= 0xff7ff000;
17650       newval |= value | (sign ? INDEX_UP : 0);
17651       md_number_to_chars (buf, newval, INSN_SIZE);
17652       break;
17653
17654     case BFD_RELOC_ARM_OFFSET_IMM8:
17655     case BFD_RELOC_ARM_HWLITERAL:
17656       sign = value >= 0;
17657
17658       if (value < 0)
17659         value = - value;
17660
17661       if (validate_offset_imm (value, 1) == FAIL)
17662         {
17663           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
17664             as_bad_where (fixP->fx_file, fixP->fx_line,
17665                           _("invalid literal constant: pool needs to be closer"));
17666           else
17667             as_bad (_("bad immediate value for half-word offset (%ld)"),
17668                     (long) value);
17669           break;
17670         }
17671
17672       newval = md_chars_to_number (buf, INSN_SIZE);
17673       newval &= 0xff7ff0f0;
17674       newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
17675       md_number_to_chars (buf, newval, INSN_SIZE);
17676       break;
17677
17678     case BFD_RELOC_ARM_T32_OFFSET_U8:
17679       if (value < 0 || value > 1020 || value % 4 != 0)
17680         as_bad_where (fixP->fx_file, fixP->fx_line,
17681                       _("bad immediate value for offset (%ld)"), (long) value);
17682       value /= 4;
17683
17684       newval = md_chars_to_number (buf+2, THUMB_SIZE);
17685       newval |= value;
17686       md_number_to_chars (buf+2, newval, THUMB_SIZE);
17687       break;
17688
17689     case BFD_RELOC_ARM_T32_OFFSET_IMM:
17690       /* This is a complicated relocation used for all varieties of Thumb32
17691          load/store instruction with immediate offset:
17692
17693          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
17694                                                    *4, optional writeback(W)
17695                                                    (doubleword load/store)
17696
17697          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
17698          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
17699          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
17700          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
17701          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
17702
17703          Uppercase letters indicate bits that are already encoded at
17704          this point.  Lowercase letters are our problem.  For the
17705          second block of instructions, the secondary opcode nybble
17706          (bits 8..11) is present, and bit 23 is zero, even if this is
17707          a PC-relative operation.  */
17708       newval = md_chars_to_number (buf, THUMB_SIZE);
17709       newval <<= 16;
17710       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
17711
17712       if ((newval & 0xf0000000) == 0xe0000000)
17713         {
17714           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
17715           if (value >= 0)
17716             newval |= (1 << 23);
17717           else
17718             value = -value;
17719           if (value % 4 != 0)
17720             {
17721               as_bad_where (fixP->fx_file, fixP->fx_line,
17722                             _("offset not a multiple of 4"));
17723               break;
17724             }
17725           value /= 4;
17726           if (value > 0xff)
17727             {
17728               as_bad_where (fixP->fx_file, fixP->fx_line,
17729                             _("offset out of range"));
17730               break;
17731             }
17732           newval &= ~0xff;
17733         }
17734       else if ((newval & 0x000f0000) == 0x000f0000)
17735         {
17736           /* PC-relative, 12-bit offset.  */
17737           if (value >= 0)
17738             newval |= (1 << 23);
17739           else
17740             value = -value;
17741           if (value > 0xfff)
17742             {
17743               as_bad_where (fixP->fx_file, fixP->fx_line,
17744                             _("offset out of range"));
17745               break;
17746             }
17747           newval &= ~0xfff;
17748         }
17749       else if ((newval & 0x00000100) == 0x00000100)
17750         {
17751           /* Writeback: 8-bit, +/- offset.  */
17752           if (value >= 0)
17753             newval |= (1 << 9);
17754           else
17755             value = -value;
17756           if (value > 0xff)
17757             {
17758               as_bad_where (fixP->fx_file, fixP->fx_line,
17759                             _("offset out of range"));
17760               break;
17761             }
17762           newval &= ~0xff;
17763         }
17764       else if ((newval & 0x00000f00) == 0x00000e00)
17765         {
17766           /* T-instruction: positive 8-bit offset.  */
17767           if (value < 0 || value > 0xff)
17768             {
17769               as_bad_where (fixP->fx_file, fixP->fx_line,
17770                             _("offset out of range"));
17771               break;
17772             }
17773           newval &= ~0xff;
17774           newval |= value;
17775         }
17776       else
17777         {
17778           /* Positive 12-bit or negative 8-bit offset.  */
17779           int limit;
17780           if (value >= 0)
17781             {
17782               newval |= (1 << 23);
17783               limit = 0xfff;
17784             }
17785           else
17786             {
17787               value = -value;
17788               limit = 0xff;
17789             }
17790           if (value > limit)
17791             {
17792               as_bad_where (fixP->fx_file, fixP->fx_line,
17793                             _("offset out of range"));
17794               break;
17795             }
17796           newval &= ~limit;
17797         }
17798
17799       newval |= value;
17800       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
17801       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
17802       break;
17803
17804     case BFD_RELOC_ARM_SHIFT_IMM:
17805       newval = md_chars_to_number (buf, INSN_SIZE);
17806       if (((unsigned long) value) > 32
17807           || (value == 32
17808               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
17809         {
17810           as_bad_where (fixP->fx_file, fixP->fx_line,
17811                         _("shift expression is too large"));
17812           break;
17813         }
17814
17815       if (value == 0)
17816         /* Shifts of zero must be done as lsl.  */
17817         newval &= ~0x60;
17818       else if (value == 32)
17819         value = 0;
17820       newval &= 0xfffff07f;
17821       newval |= (value & 0x1f) << 7;
17822       md_number_to_chars (buf, newval, INSN_SIZE);
17823       break;
17824
17825     case BFD_RELOC_ARM_T32_IMMEDIATE:
17826     case BFD_RELOC_ARM_T32_ADD_IMM:
17827     case BFD_RELOC_ARM_T32_IMM12:
17828     case BFD_RELOC_ARM_T32_ADD_PC12:
17829       /* We claim that this fixup has been processed here,
17830          even if in fact we generate an error because we do
17831          not have a reloc for it, so tc_gen_reloc will reject it.  */
17832       fixP->fx_done = 1;
17833
17834       if (fixP->fx_addsy
17835           && ! S_IS_DEFINED (fixP->fx_addsy))
17836         {
17837           as_bad_where (fixP->fx_file, fixP->fx_line,
17838                         _("undefined symbol %s used as an immediate value"),
17839                         S_GET_NAME (fixP->fx_addsy));
17840           break;
17841         }
17842
17843       newval = md_chars_to_number (buf, THUMB_SIZE);
17844       newval <<= 16;
17845       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
17846
17847       newimm = FAIL;
17848       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
17849           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
17850         {
17851           newimm = encode_thumb32_immediate (value);
17852           if (newimm == (unsigned int) FAIL)
17853             newimm = thumb32_negate_data_op (&newval, value);
17854         }
17855       if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE
17856           && newimm == (unsigned int) FAIL)
17857         {
17858           /* Turn add/sum into addw/subw.  */
17859           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
17860             newval = (newval & 0xfeffffff) | 0x02000000;
17861
17862           /* 12 bit immediate for addw/subw.  */
17863           if (value < 0)
17864             {
17865               value = -value;
17866               newval ^= 0x00a00000;
17867             }
17868           if (value > 0xfff)
17869             newimm = (unsigned int) FAIL;
17870           else
17871             newimm = value;
17872         }
17873
17874       if (newimm == (unsigned int)FAIL)
17875         {
17876           as_bad_where (fixP->fx_file, fixP->fx_line,
17877                         _("invalid constant (%lx) after fixup"),
17878                         (unsigned long) value);
17879           break;
17880         }
17881
17882       newval |= (newimm & 0x800) << 15;
17883       newval |= (newimm & 0x700) << 4;
17884       newval |= (newimm & 0x0ff);
17885
17886       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
17887       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
17888       break;
17889
17890     case BFD_RELOC_ARM_SMC:
17891       if (((unsigned long) value) > 0xffff)
17892         as_bad_where (fixP->fx_file, fixP->fx_line,
17893                       _("invalid smc expression"));
17894       newval = md_chars_to_number (buf, INSN_SIZE);
17895       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
17896       md_number_to_chars (buf, newval, INSN_SIZE);
17897       break;
17898
17899     case BFD_RELOC_ARM_SWI:
17900       if (fixP->tc_fix_data != 0)
17901         {
17902           if (((unsigned long) value) > 0xff)
17903             as_bad_where (fixP->fx_file, fixP->fx_line,
17904                           _("invalid swi expression"));
17905           newval = md_chars_to_number (buf, THUMB_SIZE);
17906           newval |= value;
17907           md_number_to_chars (buf, newval, THUMB_SIZE);
17908         }
17909       else
17910         {
17911           if (((unsigned long) value) > 0x00ffffff)
17912             as_bad_where (fixP->fx_file, fixP->fx_line,
17913                           _("invalid swi expression"));
17914           newval = md_chars_to_number (buf, INSN_SIZE);
17915           newval |= value;
17916           md_number_to_chars (buf, newval, INSN_SIZE);
17917         }
17918       break;
17919
17920     case BFD_RELOC_ARM_MULTI:
17921       if (((unsigned long) value) > 0xffff)
17922         as_bad_where (fixP->fx_file, fixP->fx_line,
17923                       _("invalid expression in load/store multiple"));
17924       newval = value | md_chars_to_number (buf, INSN_SIZE);
17925       md_number_to_chars (buf, newval, INSN_SIZE);
17926       break;
17927
17928 #ifdef OBJ_ELF
17929     case BFD_RELOC_ARM_PCREL_CALL:
17930       newval = md_chars_to_number (buf, INSN_SIZE);
17931       if ((newval & 0xf0000000) == 0xf0000000)
17932         temp = 1;
17933       else
17934         temp = 3;
17935       goto arm_branch_common;
17936
17937     case BFD_RELOC_ARM_PCREL_JUMP:
17938     case BFD_RELOC_ARM_PLT32:
17939 #endif
17940     case BFD_RELOC_ARM_PCREL_BRANCH:
17941       temp = 3;
17942       goto arm_branch_common;
17943
17944     case BFD_RELOC_ARM_PCREL_BLX:
17945       temp = 1;
17946     arm_branch_common:
17947       /* We are going to store value (shifted right by two) in the
17948          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
17949          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
17950          also be be clear.  */
17951       if (value & temp)
17952         as_bad_where (fixP->fx_file, fixP->fx_line,
17953                       _("misaligned branch destination"));
17954       if ((value & (offsetT)0xfe000000) != (offsetT)0
17955           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
17956         as_bad_where (fixP->fx_file, fixP->fx_line,
17957                       _("branch out of range"));
17958
17959       if (fixP->fx_done || !seg->use_rela_p)
17960         {
17961           newval = md_chars_to_number (buf, INSN_SIZE);
17962           newval |= (value >> 2) & 0x00ffffff;
17963           /* Set the H bit on BLX instructions.  */
17964           if (temp == 1)
17965             {
17966               if (value & 2)
17967                 newval |= 0x01000000;
17968               else
17969                 newval &= ~0x01000000;
17970             }
17971           md_number_to_chars (buf, newval, INSN_SIZE);
17972         }
17973       break;
17974
17975     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CZB */
17976       /* CZB can only branch forward.  */
17977       if (value & ~0x7e)
17978         as_bad_where (fixP->fx_file, fixP->fx_line,
17979                       _("branch out of range"));
17980
17981       if (fixP->fx_done || !seg->use_rela_p)
17982         {
17983           newval = md_chars_to_number (buf, THUMB_SIZE);
17984           newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
17985           md_number_to_chars (buf, newval, THUMB_SIZE);
17986         }
17987       break;
17988
17989     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
17990       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
17991         as_bad_where (fixP->fx_file, fixP->fx_line,
17992                       _("branch out of range"));
17993
17994       if (fixP->fx_done || !seg->use_rela_p)
17995         {
17996           newval = md_chars_to_number (buf, THUMB_SIZE);
17997           newval |= (value & 0x1ff) >> 1;
17998           md_number_to_chars (buf, newval, THUMB_SIZE);
17999         }
18000       break;
18001
18002     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
18003       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
18004         as_bad_where (fixP->fx_file, fixP->fx_line,
18005                       _("branch out of range"));
18006
18007       if (fixP->fx_done || !seg->use_rela_p)
18008         {
18009           newval = md_chars_to_number (buf, THUMB_SIZE);
18010           newval |= (value & 0xfff) >> 1;
18011           md_number_to_chars (buf, newval, THUMB_SIZE);
18012         }
18013       break;
18014
18015     case BFD_RELOC_THUMB_PCREL_BRANCH20:
18016       if ((value & ~0x1fffff) && ((value & ~0x1fffff) != ~0x1fffff))
18017         as_bad_where (fixP->fx_file, fixP->fx_line,
18018                       _("conditional branch out of range"));
18019
18020       if (fixP->fx_done || !seg->use_rela_p)
18021         {
18022           offsetT newval2;
18023           addressT S, J1, J2, lo, hi;
18024
18025           S  = (value & 0x00100000) >> 20;
18026           J2 = (value & 0x00080000) >> 19;
18027           J1 = (value & 0x00040000) >> 18;
18028           hi = (value & 0x0003f000) >> 12;
18029           lo = (value & 0x00000ffe) >> 1;
18030
18031           newval   = md_chars_to_number (buf, THUMB_SIZE);
18032           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
18033           newval  |= (S << 10) | hi;
18034           newval2 |= (J1 << 13) | (J2 << 11) | lo;
18035           md_number_to_chars (buf, newval, THUMB_SIZE);
18036           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
18037         }
18038       break;
18039
18040     case BFD_RELOC_THUMB_PCREL_BLX:
18041     case BFD_RELOC_THUMB_PCREL_BRANCH23:
18042       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
18043         as_bad_where (fixP->fx_file, fixP->fx_line,
18044                       _("branch out of range"));
18045
18046       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
18047         /* For a BLX instruction, make sure that the relocation is rounded up
18048            to a word boundary.  This follows the semantics of the instruction
18049            which specifies that bit 1 of the target address will come from bit
18050            1 of the base address.  */
18051         value = (value + 1) & ~ 1;
18052
18053       if (fixP->fx_done || !seg->use_rela_p)
18054         {
18055           offsetT newval2;
18056
18057           newval   = md_chars_to_number (buf, THUMB_SIZE);
18058           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
18059           newval  |= (value & 0x7fffff) >> 12;
18060           newval2 |= (value & 0xfff) >> 1;
18061           md_number_to_chars (buf, newval, THUMB_SIZE);
18062           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
18063         }
18064       break;
18065
18066     case BFD_RELOC_THUMB_PCREL_BRANCH25:
18067       if ((value & ~0x1ffffff) && ((value & ~0x1ffffff) != ~0x1ffffff))
18068         as_bad_where (fixP->fx_file, fixP->fx_line,
18069                       _("branch out of range"));
18070
18071       if (fixP->fx_done || !seg->use_rela_p)
18072         {
18073           offsetT newval2;
18074           addressT S, I1, I2, lo, hi;
18075
18076           S  = (value & 0x01000000) >> 24;
18077           I1 = (value & 0x00800000) >> 23;
18078           I2 = (value & 0x00400000) >> 22;
18079           hi = (value & 0x003ff000) >> 12;
18080           lo = (value & 0x00000ffe) >> 1;
18081
18082           I1 = !(I1 ^ S);
18083           I2 = !(I2 ^ S);
18084
18085           newval   = md_chars_to_number (buf, THUMB_SIZE);
18086           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
18087           newval  |= (S << 10) | hi;
18088           newval2 |= (I1 << 13) | (I2 << 11) | lo;
18089           md_number_to_chars (buf, newval, THUMB_SIZE);
18090           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
18091         }
18092       break;
18093
18094     case BFD_RELOC_8:
18095       if (fixP->fx_done || !seg->use_rela_p)
18096         md_number_to_chars (buf, value, 1);
18097       break;
18098
18099     case BFD_RELOC_16:
18100       if (fixP->fx_done || !seg->use_rela_p)
18101         md_number_to_chars (buf, value, 2);
18102       break;
18103
18104 #ifdef OBJ_ELF
18105     case BFD_RELOC_ARM_TLS_GD32:
18106     case BFD_RELOC_ARM_TLS_LE32:
18107     case BFD_RELOC_ARM_TLS_IE32:
18108     case BFD_RELOC_ARM_TLS_LDM32:
18109     case BFD_RELOC_ARM_TLS_LDO32:
18110       S_SET_THREAD_LOCAL (fixP->fx_addsy);
18111       /* fall through */
18112
18113     case BFD_RELOC_ARM_GOT32:
18114     case BFD_RELOC_ARM_GOTOFF:
18115     case BFD_RELOC_ARM_TARGET2:
18116       if (fixP->fx_done || !seg->use_rela_p)
18117         md_number_to_chars (buf, 0, 4);
18118       break;
18119 #endif
18120
18121     case BFD_RELOC_RVA:
18122     case BFD_RELOC_32:
18123     case BFD_RELOC_ARM_TARGET1:
18124     case BFD_RELOC_ARM_ROSEGREL32:
18125     case BFD_RELOC_ARM_SBREL32:
18126     case BFD_RELOC_32_PCREL:
18127       if (fixP->fx_done || !seg->use_rela_p)
18128         md_number_to_chars (buf, value, 4);
18129       break;
18130
18131 #ifdef OBJ_ELF
18132     case BFD_RELOC_ARM_PREL31:
18133       if (fixP->fx_done || !seg->use_rela_p)
18134         {
18135           newval = md_chars_to_number (buf, 4) & 0x80000000;
18136           if ((value ^ (value >> 1)) & 0x40000000)
18137             {
18138               as_bad_where (fixP->fx_file, fixP->fx_line,
18139                             _("rel31 relocation overflow"));
18140             }
18141           newval |= value & 0x7fffffff;
18142           md_number_to_chars (buf, newval, 4);
18143         }
18144       break;
18145 #endif
18146
18147     case BFD_RELOC_ARM_CP_OFF_IMM:
18148     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
18149       if (value < -1023 || value > 1023 || (value & 3))
18150         as_bad_where (fixP->fx_file, fixP->fx_line,
18151                       _("co-processor offset out of range"));
18152     cp_off_common:
18153       sign = value >= 0;
18154       if (value < 0)
18155         value = -value;
18156       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
18157           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
18158         newval = md_chars_to_number (buf, INSN_SIZE);
18159       else
18160         newval = get_thumb32_insn (buf);
18161       newval &= 0xff7fff00;
18162       newval |= (value >> 2) | (sign ? INDEX_UP : 0);
18163       if (value == 0)
18164         newval &= ~WRITE_BACK;
18165       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
18166           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
18167         md_number_to_chars (buf, newval, INSN_SIZE);
18168       else
18169         put_thumb32_insn (buf, newval);
18170       break;
18171
18172     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
18173     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
18174       if (value < -255 || value > 255)
18175         as_bad_where (fixP->fx_file, fixP->fx_line,
18176                       _("co-processor offset out of range"));
18177       value *= 4;
18178       goto cp_off_common;
18179
18180     case BFD_RELOC_ARM_THUMB_OFFSET:
18181       newval = md_chars_to_number (buf, THUMB_SIZE);
18182       /* Exactly what ranges, and where the offset is inserted depends
18183          on the type of instruction, we can establish this from the
18184          top 4 bits.  */
18185       switch (newval >> 12)
18186         {
18187         case 4: /* PC load.  */
18188           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
18189              forced to zero for these loads; md_pcrel_from has already
18190              compensated for this.  */
18191           if (value & 3)
18192             as_bad_where (fixP->fx_file, fixP->fx_line,
18193                           _("invalid offset, target not word aligned (0x%08lX)"),
18194                           (((unsigned long) fixP->fx_frag->fr_address
18195                             + (unsigned long) fixP->fx_where) & ~3)
18196                           + (unsigned long) value);
18197
18198           if (value & ~0x3fc)
18199             as_bad_where (fixP->fx_file, fixP->fx_line,
18200                           _("invalid offset, value too big (0x%08lX)"),
18201                           (long) value);
18202
18203           newval |= value >> 2;
18204           break;
18205
18206         case 9: /* SP load/store.  */
18207           if (value & ~0x3fc)
18208             as_bad_where (fixP->fx_file, fixP->fx_line,
18209                           _("invalid offset, value too big (0x%08lX)"),
18210                           (long) value);
18211           newval |= value >> 2;
18212           break;
18213
18214         case 6: /* Word load/store.  */
18215           if (value & ~0x7c)
18216             as_bad_where (fixP->fx_file, fixP->fx_line,
18217                           _("invalid offset, value too big (0x%08lX)"),
18218                           (long) value);
18219           newval |= value << 4; /* 6 - 2.  */
18220           break;
18221
18222         case 7: /* Byte load/store.  */
18223           if (value & ~0x1f)
18224             as_bad_where (fixP->fx_file, fixP->fx_line,
18225                           _("invalid offset, value too big (0x%08lX)"),
18226                           (long) value);
18227           newval |= value << 6;
18228           break;
18229
18230         case 8: /* Halfword load/store.  */
18231           if (value & ~0x3e)
18232             as_bad_where (fixP->fx_file, fixP->fx_line,
18233                           _("invalid offset, value too big (0x%08lX)"),
18234                           (long) value);
18235           newval |= value << 5; /* 6 - 1.  */
18236           break;
18237
18238         default:
18239           as_bad_where (fixP->fx_file, fixP->fx_line,
18240                         "Unable to process relocation for thumb opcode: %lx",
18241                         (unsigned long) newval);
18242           break;
18243         }
18244       md_number_to_chars (buf, newval, THUMB_SIZE);
18245       break;
18246
18247     case BFD_RELOC_ARM_THUMB_ADD:
18248       /* This is a complicated relocation, since we use it for all of
18249          the following immediate relocations:
18250
18251             3bit ADD/SUB
18252             8bit ADD/SUB
18253             9bit ADD/SUB SP word-aligned
18254            10bit ADD PC/SP word-aligned
18255
18256          The type of instruction being processed is encoded in the
18257          instruction field:
18258
18259            0x8000  SUB
18260            0x00F0  Rd
18261            0x000F  Rs
18262       */
18263       newval = md_chars_to_number (buf, THUMB_SIZE);
18264       {
18265         int rd = (newval >> 4) & 0xf;
18266         int rs = newval & 0xf;
18267         int subtract = !!(newval & 0x8000);
18268
18269         /* Check for HI regs, only very restricted cases allowed:
18270            Adjusting SP, and using PC or SP to get an address.  */
18271         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
18272             || (rs > 7 && rs != REG_SP && rs != REG_PC))
18273           as_bad_where (fixP->fx_file, fixP->fx_line,
18274                         _("invalid Hi register with immediate"));
18275
18276         /* If value is negative, choose the opposite instruction.  */
18277         if (value < 0)
18278           {
18279             value = -value;
18280             subtract = !subtract;
18281             if (value < 0)
18282               as_bad_where (fixP->fx_file, fixP->fx_line,
18283                             _("immediate value out of range"));
18284           }
18285
18286         if (rd == REG_SP)
18287           {
18288             if (value & ~0x1fc)
18289               as_bad_where (fixP->fx_file, fixP->fx_line,
18290                             _("invalid immediate for stack address calculation"));
18291             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
18292             newval |= value >> 2;
18293           }
18294         else if (rs == REG_PC || rs == REG_SP)
18295           {
18296             if (subtract || value & ~0x3fc)
18297               as_bad_where (fixP->fx_file, fixP->fx_line,
18298                             _("invalid immediate for address calculation (value = 0x%08lX)"),
18299                             (unsigned long) value);
18300             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
18301             newval |= rd << 8;
18302             newval |= value >> 2;
18303           }
18304         else if (rs == rd)
18305           {
18306             if (value & ~0xff)
18307               as_bad_where (fixP->fx_file, fixP->fx_line,
18308                             _("immediate value out of range"));
18309             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
18310             newval |= (rd << 8) | value;
18311           }
18312         else
18313           {
18314             if (value & ~0x7)
18315               as_bad_where (fixP->fx_file, fixP->fx_line,
18316                             _("immediate value out of range"));
18317             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
18318             newval |= rd | (rs << 3) | (value << 6);
18319           }
18320       }
18321       md_number_to_chars (buf, newval, THUMB_SIZE);
18322       break;
18323
18324     case BFD_RELOC_ARM_THUMB_IMM:
18325       newval = md_chars_to_number (buf, THUMB_SIZE);
18326       if (value < 0 || value > 255)
18327         as_bad_where (fixP->fx_file, fixP->fx_line,
18328                       _("invalid immediate: %ld is too large"),
18329                       (long) value);
18330       newval |= value;
18331       md_number_to_chars (buf, newval, THUMB_SIZE);
18332       break;
18333
18334     case BFD_RELOC_ARM_THUMB_SHIFT:
18335       /* 5bit shift value (0..32).  LSL cannot take 32.  */
18336       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
18337       temp = newval & 0xf800;
18338       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
18339         as_bad_where (fixP->fx_file, fixP->fx_line,
18340                       _("invalid shift value: %ld"), (long) value);
18341       /* Shifts of zero must be encoded as LSL.  */
18342       if (value == 0)
18343         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
18344       /* Shifts of 32 are encoded as zero.  */
18345       else if (value == 32)
18346         value = 0;
18347       newval |= value << 6;
18348       md_number_to_chars (buf, newval, THUMB_SIZE);
18349       break;
18350
18351     case BFD_RELOC_VTABLE_INHERIT:
18352     case BFD_RELOC_VTABLE_ENTRY:
18353       fixP->fx_done = 0;
18354       return;
18355
18356     case BFD_RELOC_ARM_MOVW:
18357     case BFD_RELOC_ARM_MOVT:
18358     case BFD_RELOC_ARM_THUMB_MOVW:
18359     case BFD_RELOC_ARM_THUMB_MOVT:
18360       if (fixP->fx_done || !seg->use_rela_p)
18361         {
18362           /* REL format relocations are limited to a 16-bit addend.  */
18363           if (!fixP->fx_done)
18364             {
18365               if (value < -0x1000 || value > 0xffff)
18366                   as_bad_where (fixP->fx_file, fixP->fx_line,
18367                                 _("offset too big"));
18368             }
18369           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
18370                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
18371             {
18372               value >>= 16;
18373             }
18374
18375           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
18376               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
18377             {
18378               newval = get_thumb32_insn (buf);
18379               newval &= 0xfbf08f00;
18380               newval |= (value & 0xf000) << 4;
18381               newval |= (value & 0x0800) << 15;
18382               newval |= (value & 0x0700) << 4;
18383               newval |= (value & 0x00ff);
18384               put_thumb32_insn (buf, newval);
18385             }
18386           else
18387             {
18388               newval = md_chars_to_number (buf, 4);
18389               newval &= 0xfff0f000;
18390               newval |= value & 0x0fff;
18391               newval |= (value & 0xf000) << 4;
18392               md_number_to_chars (buf, newval, 4);
18393             }
18394         }
18395       return;
18396
18397    case BFD_RELOC_ARM_ALU_PC_G0_NC:
18398    case BFD_RELOC_ARM_ALU_PC_G0:
18399    case BFD_RELOC_ARM_ALU_PC_G1_NC:
18400    case BFD_RELOC_ARM_ALU_PC_G1:
18401    case BFD_RELOC_ARM_ALU_PC_G2:
18402    case BFD_RELOC_ARM_ALU_SB_G0_NC:
18403    case BFD_RELOC_ARM_ALU_SB_G0:
18404    case BFD_RELOC_ARM_ALU_SB_G1_NC:
18405    case BFD_RELOC_ARM_ALU_SB_G1:
18406    case BFD_RELOC_ARM_ALU_SB_G2:
18407      assert (!fixP->fx_done);
18408      if (!seg->use_rela_p)
18409        {
18410          bfd_vma insn;
18411          bfd_vma encoded_addend;
18412          bfd_vma addend_abs = abs (value);
18413
18414          /* Check that the absolute value of the addend can be
18415             expressed as an 8-bit constant plus a rotation.  */
18416          encoded_addend = encode_arm_immediate (addend_abs);
18417          if (encoded_addend == (unsigned int) FAIL)
18418            as_bad_where (fixP->fx_file, fixP->fx_line,
18419                          _("the offset 0x%08lX is not representable"),
18420                          addend_abs);
18421
18422          /* Extract the instruction.  */
18423          insn = md_chars_to_number (buf, INSN_SIZE);
18424
18425          /* If the addend is positive, use an ADD instruction.
18426             Otherwise use a SUB.  Take care not to destroy the S bit.  */
18427          insn &= 0xff1fffff;
18428          if (value < 0)
18429            insn |= 1 << 22;
18430          else
18431            insn |= 1 << 23;
18432
18433          /* Place the encoded addend into the first 12 bits of the
18434             instruction.  */
18435          insn &= 0xfffff000;
18436          insn |= encoded_addend;
18437    
18438          /* Update the instruction.  */  
18439          md_number_to_chars (buf, insn, INSN_SIZE);
18440        }
18441      break;
18442
18443     case BFD_RELOC_ARM_LDR_PC_G0:
18444     case BFD_RELOC_ARM_LDR_PC_G1:
18445     case BFD_RELOC_ARM_LDR_PC_G2:
18446     case BFD_RELOC_ARM_LDR_SB_G0:
18447     case BFD_RELOC_ARM_LDR_SB_G1:
18448     case BFD_RELOC_ARM_LDR_SB_G2:
18449       assert (!fixP->fx_done);
18450       if (!seg->use_rela_p)
18451         {
18452           bfd_vma insn;
18453           bfd_vma addend_abs = abs (value);
18454
18455           /* Check that the absolute value of the addend can be
18456              encoded in 12 bits.  */
18457           if (addend_abs >= 0x1000)
18458             as_bad_where (fixP->fx_file, fixP->fx_line,
18459                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
18460                           addend_abs);
18461
18462           /* Extract the instruction.  */
18463           insn = md_chars_to_number (buf, INSN_SIZE);
18464
18465           /* If the addend is negative, clear bit 23 of the instruction.
18466              Otherwise set it.  */
18467           if (value < 0)
18468             insn &= ~(1 << 23);
18469           else
18470             insn |= 1 << 23;
18471
18472           /* Place the absolute value of the addend into the first 12 bits
18473              of the instruction.  */
18474           insn &= 0xfffff000;
18475           insn |= addend_abs;
18476     
18477           /* Update the instruction.  */  
18478           md_number_to_chars (buf, insn, INSN_SIZE);
18479         }
18480       break;
18481
18482     case BFD_RELOC_ARM_LDRS_PC_G0:
18483     case BFD_RELOC_ARM_LDRS_PC_G1:
18484     case BFD_RELOC_ARM_LDRS_PC_G2:
18485     case BFD_RELOC_ARM_LDRS_SB_G0:
18486     case BFD_RELOC_ARM_LDRS_SB_G1:
18487     case BFD_RELOC_ARM_LDRS_SB_G2:
18488       assert (!fixP->fx_done);
18489       if (!seg->use_rela_p)
18490         {
18491           bfd_vma insn;
18492           bfd_vma addend_abs = abs (value);
18493
18494           /* Check that the absolute value of the addend can be
18495              encoded in 8 bits.  */
18496           if (addend_abs >= 0x100)
18497             as_bad_where (fixP->fx_file, fixP->fx_line,
18498                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
18499                           addend_abs);
18500
18501           /* Extract the instruction.  */
18502           insn = md_chars_to_number (buf, INSN_SIZE);
18503
18504           /* If the addend is negative, clear bit 23 of the instruction.
18505              Otherwise set it.  */
18506           if (value < 0)
18507             insn &= ~(1 << 23);
18508           else
18509             insn |= 1 << 23;
18510
18511           /* Place the first four bits of the absolute value of the addend
18512              into the first 4 bits of the instruction, and the remaining
18513              four into bits 8 .. 11.  */
18514           insn &= 0xfffff0f0;
18515           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
18516     
18517           /* Update the instruction.  */  
18518           md_number_to_chars (buf, insn, INSN_SIZE);
18519         }
18520       break;
18521
18522     case BFD_RELOC_ARM_LDC_PC_G0:
18523     case BFD_RELOC_ARM_LDC_PC_G1:
18524     case BFD_RELOC_ARM_LDC_PC_G2:
18525     case BFD_RELOC_ARM_LDC_SB_G0:
18526     case BFD_RELOC_ARM_LDC_SB_G1:
18527     case BFD_RELOC_ARM_LDC_SB_G2:
18528       assert (!fixP->fx_done);
18529       if (!seg->use_rela_p)
18530         {
18531           bfd_vma insn;
18532           bfd_vma addend_abs = abs (value);
18533
18534           /* Check that the absolute value of the addend is a multiple of
18535              four and, when divided by four, fits in 8 bits.  */
18536           if (addend_abs & 0x3)
18537             as_bad_where (fixP->fx_file, fixP->fx_line,
18538                           _("bad offset 0x%08lX (must be word-aligned)"),
18539                           addend_abs);
18540
18541           if ((addend_abs >> 2) > 0xff)
18542             as_bad_where (fixP->fx_file, fixP->fx_line,
18543                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
18544                           addend_abs);
18545
18546           /* Extract the instruction.  */
18547           insn = md_chars_to_number (buf, INSN_SIZE);
18548
18549           /* If the addend is negative, clear bit 23 of the instruction.
18550              Otherwise set it.  */
18551           if (value < 0)
18552             insn &= ~(1 << 23);
18553           else
18554             insn |= 1 << 23;
18555
18556           /* Place the addend (divided by four) into the first eight
18557              bits of the instruction.  */
18558           insn &= 0xfffffff0;
18559           insn |= addend_abs >> 2;
18560     
18561           /* Update the instruction.  */  
18562           md_number_to_chars (buf, insn, INSN_SIZE);
18563         }
18564       break;
18565
18566     case BFD_RELOC_UNUSED:
18567     default:
18568       as_bad_where (fixP->fx_file, fixP->fx_line,
18569                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
18570     }
18571 }
18572
18573 /* Translate internal representation of relocation info to BFD target
18574    format.  */
18575
18576 arelent *
18577 tc_gen_reloc (asection *section, fixS *fixp)
18578 {
18579   arelent * reloc;
18580   bfd_reloc_code_real_type code;
18581
18582   reloc = xmalloc (sizeof (arelent));
18583
18584   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
18585   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18586   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18587
18588   if (fixp->fx_pcrel)
18589     {
18590       if (section->use_rela_p)
18591         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
18592       else
18593         fixp->fx_offset = reloc->address;
18594     }
18595   reloc->addend = fixp->fx_offset;
18596
18597   switch (fixp->fx_r_type)
18598     {
18599     case BFD_RELOC_8:
18600       if (fixp->fx_pcrel)
18601         {
18602           code = BFD_RELOC_8_PCREL;
18603           break;
18604         }
18605
18606     case BFD_RELOC_16:
18607       if (fixp->fx_pcrel)
18608         {
18609           code = BFD_RELOC_16_PCREL;
18610           break;
18611         }
18612
18613     case BFD_RELOC_32:
18614       if (fixp->fx_pcrel)
18615         {
18616           code = BFD_RELOC_32_PCREL;
18617           break;
18618         }
18619
18620     case BFD_RELOC_ARM_MOVW:
18621       if (fixp->fx_pcrel)
18622         {
18623           code = BFD_RELOC_ARM_MOVW_PCREL;
18624           break;
18625         }
18626
18627     case BFD_RELOC_ARM_MOVT:
18628       if (fixp->fx_pcrel)
18629         {
18630           code = BFD_RELOC_ARM_MOVT_PCREL;
18631           break;
18632         }
18633
18634     case BFD_RELOC_ARM_THUMB_MOVW:
18635       if (fixp->fx_pcrel)
18636         {
18637           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
18638           break;
18639         }
18640
18641     case BFD_RELOC_ARM_THUMB_MOVT:
18642       if (fixp->fx_pcrel)
18643         {
18644           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
18645           break;
18646         }
18647
18648     case BFD_RELOC_NONE:
18649     case BFD_RELOC_ARM_PCREL_BRANCH:
18650     case BFD_RELOC_ARM_PCREL_BLX:
18651     case BFD_RELOC_RVA:
18652     case BFD_RELOC_THUMB_PCREL_BRANCH7:
18653     case BFD_RELOC_THUMB_PCREL_BRANCH9:
18654     case BFD_RELOC_THUMB_PCREL_BRANCH12:
18655     case BFD_RELOC_THUMB_PCREL_BRANCH20:
18656     case BFD_RELOC_THUMB_PCREL_BRANCH23:
18657     case BFD_RELOC_THUMB_PCREL_BRANCH25:
18658     case BFD_RELOC_THUMB_PCREL_BLX:
18659     case BFD_RELOC_VTABLE_ENTRY:
18660     case BFD_RELOC_VTABLE_INHERIT:
18661       code = fixp->fx_r_type;
18662       break;
18663
18664     case BFD_RELOC_ARM_LITERAL:
18665     case BFD_RELOC_ARM_HWLITERAL:
18666       /* If this is called then the a literal has
18667          been referenced across a section boundary.  */
18668       as_bad_where (fixp->fx_file, fixp->fx_line,
18669                     _("literal referenced across section boundary"));
18670       return NULL;
18671
18672 #ifdef OBJ_ELF
18673     case BFD_RELOC_ARM_GOT32:
18674     case BFD_RELOC_ARM_GOTOFF:
18675     case BFD_RELOC_ARM_PLT32:
18676     case BFD_RELOC_ARM_TARGET1:
18677     case BFD_RELOC_ARM_ROSEGREL32:
18678     case BFD_RELOC_ARM_SBREL32:
18679     case BFD_RELOC_ARM_PREL31:
18680     case BFD_RELOC_ARM_TARGET2:
18681     case BFD_RELOC_ARM_TLS_LE32:
18682     case BFD_RELOC_ARM_TLS_LDO32:
18683     case BFD_RELOC_ARM_PCREL_CALL:
18684     case BFD_RELOC_ARM_PCREL_JUMP:
18685     case BFD_RELOC_ARM_ALU_PC_G0_NC:
18686     case BFD_RELOC_ARM_ALU_PC_G0:
18687     case BFD_RELOC_ARM_ALU_PC_G1_NC:
18688     case BFD_RELOC_ARM_ALU_PC_G1:
18689     case BFD_RELOC_ARM_ALU_PC_G2:
18690     case BFD_RELOC_ARM_LDR_PC_G0:
18691     case BFD_RELOC_ARM_LDR_PC_G1:
18692     case BFD_RELOC_ARM_LDR_PC_G2:
18693     case BFD_RELOC_ARM_LDRS_PC_G0:
18694     case BFD_RELOC_ARM_LDRS_PC_G1:
18695     case BFD_RELOC_ARM_LDRS_PC_G2:
18696     case BFD_RELOC_ARM_LDC_PC_G0:
18697     case BFD_RELOC_ARM_LDC_PC_G1:
18698     case BFD_RELOC_ARM_LDC_PC_G2:
18699     case BFD_RELOC_ARM_ALU_SB_G0_NC:
18700     case BFD_RELOC_ARM_ALU_SB_G0:
18701     case BFD_RELOC_ARM_ALU_SB_G1_NC:
18702     case BFD_RELOC_ARM_ALU_SB_G1:
18703     case BFD_RELOC_ARM_ALU_SB_G2:
18704     case BFD_RELOC_ARM_LDR_SB_G0:
18705     case BFD_RELOC_ARM_LDR_SB_G1:
18706     case BFD_RELOC_ARM_LDR_SB_G2:
18707     case BFD_RELOC_ARM_LDRS_SB_G0:
18708     case BFD_RELOC_ARM_LDRS_SB_G1:
18709     case BFD_RELOC_ARM_LDRS_SB_G2:
18710     case BFD_RELOC_ARM_LDC_SB_G0:
18711     case BFD_RELOC_ARM_LDC_SB_G1:
18712     case BFD_RELOC_ARM_LDC_SB_G2:
18713       code = fixp->fx_r_type;
18714       break;
18715
18716     case BFD_RELOC_ARM_TLS_GD32:
18717     case BFD_RELOC_ARM_TLS_IE32:
18718     case BFD_RELOC_ARM_TLS_LDM32:
18719       /* BFD will include the symbol's address in the addend.
18720          But we don't want that, so subtract it out again here.  */
18721       if (!S_IS_COMMON (fixp->fx_addsy))
18722         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
18723       code = fixp->fx_r_type;
18724       break;
18725 #endif
18726
18727     case BFD_RELOC_ARM_IMMEDIATE:
18728       as_bad_where (fixp->fx_file, fixp->fx_line,
18729                     _("internal relocation (type: IMMEDIATE) not fixed up"));
18730       return NULL;
18731
18732     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
18733       as_bad_where (fixp->fx_file, fixp->fx_line,
18734                     _("ADRL used for a symbol not defined in the same file"));
18735       return NULL;
18736
18737     case BFD_RELOC_ARM_OFFSET_IMM:
18738       if (section->use_rela_p)
18739         {
18740           code = fixp->fx_r_type;
18741           break;
18742         }
18743
18744       if (fixp->fx_addsy != NULL
18745           && !S_IS_DEFINED (fixp->fx_addsy)
18746           && S_IS_LOCAL (fixp->fx_addsy))
18747         {
18748           as_bad_where (fixp->fx_file, fixp->fx_line,
18749                         _("undefined local label `%s'"),
18750                         S_GET_NAME (fixp->fx_addsy));
18751           return NULL;
18752         }
18753
18754       as_bad_where (fixp->fx_file, fixp->fx_line,
18755                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
18756       return NULL;
18757
18758     default:
18759       {
18760         char * type;
18761
18762         switch (fixp->fx_r_type)
18763           {
18764           case BFD_RELOC_NONE:             type = "NONE";         break;
18765           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
18766           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
18767           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
18768           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
18769           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
18770           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
18771           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
18772           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
18773           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
18774           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
18775           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
18776           default:                         type = _("<unknown>"); break;
18777           }
18778         as_bad_where (fixp->fx_file, fixp->fx_line,
18779                       _("cannot represent %s relocation in this object file format"),
18780                       type);
18781         return NULL;
18782       }
18783     }
18784
18785 #ifdef OBJ_ELF
18786   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
18787       && GOT_symbol
18788       && fixp->fx_addsy == GOT_symbol)
18789     {
18790       code = BFD_RELOC_ARM_GOTPC;
18791       reloc->addend = fixp->fx_offset = reloc->address;
18792     }
18793 #endif
18794
18795   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18796
18797   if (reloc->howto == NULL)
18798     {
18799       as_bad_where (fixp->fx_file, fixp->fx_line,
18800                     _("cannot represent %s relocation in this object file format"),
18801                     bfd_get_reloc_code_name (code));
18802       return NULL;
18803     }
18804
18805   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
18806      vtable entry to be used in the relocation's section offset.  */
18807   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18808     reloc->address = fixp->fx_offset;
18809
18810   return reloc;
18811 }
18812
18813 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
18814
18815 void
18816 cons_fix_new_arm (fragS *       frag,
18817                   int           where,
18818                   int           size,
18819                   expressionS * exp)
18820 {
18821   bfd_reloc_code_real_type type;
18822   int pcrel = 0;
18823
18824   /* Pick a reloc.
18825      FIXME: @@ Should look at CPU word size.  */
18826   switch (size)
18827     {
18828     case 1:
18829       type = BFD_RELOC_8;
18830       break;
18831     case 2:
18832       type = BFD_RELOC_16;
18833       break;
18834     case 4:
18835     default:
18836       type = BFD_RELOC_32;
18837       break;
18838     case 8:
18839       type = BFD_RELOC_64;
18840       break;
18841     }
18842
18843   fix_new_exp (frag, where, (int) size, exp, pcrel, type);
18844 }
18845
18846 #if defined OBJ_COFF || defined OBJ_ELF
18847 void
18848 arm_validate_fix (fixS * fixP)
18849 {
18850   /* If the destination of the branch is a defined symbol which does not have
18851      the THUMB_FUNC attribute, then we must be calling a function which has
18852      the (interfacearm) attribute.  We look for the Thumb entry point to that
18853      function and change the branch to refer to that function instead.  */
18854   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
18855       && fixP->fx_addsy != NULL
18856       && S_IS_DEFINED (fixP->fx_addsy)
18857       && ! THUMB_IS_FUNC (fixP->fx_addsy))
18858     {
18859       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
18860     }
18861 }
18862 #endif
18863
18864 int
18865 arm_force_relocation (struct fix * fixp)
18866 {
18867 #if defined (OBJ_COFF) && defined (TE_PE)
18868   if (fixp->fx_r_type == BFD_RELOC_RVA)
18869     return 1;
18870 #endif
18871
18872   /* Resolve these relocations even if the symbol is extern or weak.  */
18873   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
18874       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
18875       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
18876       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
18877       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
18878       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
18879       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12)
18880     return 0;
18881
18882   /* Always leave these relocations for the linker.  */
18883   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
18884        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
18885       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
18886     return 1;
18887
18888   return generic_force_reloc (fixp);
18889 }
18890
18891 #ifdef OBJ_COFF
18892 /* This is a little hack to help the gas/arm/adrl.s test.  It prevents
18893    local labels from being added to the output symbol table when they
18894    are used with the ADRL pseudo op.  The ADRL relocation should always
18895    be resolved before the binbary is emitted, so it is safe to say that
18896    it is adjustable.  */
18897
18898 bfd_boolean
18899 arm_fix_adjustable (fixS * fixP)
18900 {
18901   if (fixP->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE)
18902     return 1;
18903   return 0;
18904 }
18905 #endif
18906
18907 #ifdef OBJ_ELF
18908 /* Relocations against function names must be left unadjusted,
18909    so that the linker can use this information to generate interworking
18910    stubs.  The MIPS version of this function
18911    also prevents relocations that are mips-16 specific, but I do not
18912    know why it does this.
18913
18914    FIXME:
18915    There is one other problem that ought to be addressed here, but
18916    which currently is not:  Taking the address of a label (rather
18917    than a function) and then later jumping to that address.  Such
18918    addresses also ought to have their bottom bit set (assuming that
18919    they reside in Thumb code), but at the moment they will not.  */
18920
18921 bfd_boolean
18922 arm_fix_adjustable (fixS * fixP)
18923 {
18924   if (fixP->fx_addsy == NULL)
18925     return 1;
18926
18927   /* Preserve relocations against symbols with function type.  */
18928   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
18929     return 0;
18930
18931   if (THUMB_IS_FUNC (fixP->fx_addsy)
18932       && fixP->fx_subsy == NULL)
18933     return 0;
18934
18935   /* We need the symbol name for the VTABLE entries.  */
18936   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18937       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18938     return 0;
18939
18940   /* Don't allow symbols to be discarded on GOT related relocs.  */
18941   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
18942       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
18943       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
18944       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
18945       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
18946       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
18947       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
18948       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
18949       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
18950     return 0;
18951
18952   /* Similarly for group relocations.  */
18953   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
18954        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
18955       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
18956     return 0;
18957
18958   return 1;
18959 }
18960
18961 const char *
18962 elf32_arm_target_format (void)
18963 {
18964 #ifdef TE_SYMBIAN
18965   return (target_big_endian
18966           ? "elf32-bigarm-symbian"
18967           : "elf32-littlearm-symbian");
18968 #elif defined (TE_VXWORKS)
18969   return (target_big_endian
18970           ? "elf32-bigarm-vxworks"
18971           : "elf32-littlearm-vxworks");
18972 #else
18973   if (target_big_endian)
18974     return "elf32-bigarm";
18975   else
18976     return "elf32-littlearm";
18977 #endif
18978 }
18979
18980 void
18981 armelf_frob_symbol (symbolS * symp,
18982                     int *     puntp)
18983 {
18984   elf_frob_symbol (symp, puntp);
18985 }
18986 #endif
18987
18988 /* MD interface: Finalization.  */
18989
18990 /* A good place to do this, although this was probably not intended
18991    for this kind of use.  We need to dump the literal pool before
18992    references are made to a null symbol pointer.  */
18993
18994 void
18995 arm_cleanup (void)
18996 {
18997   literal_pool * pool;
18998
18999   for (pool = list_of_pools; pool; pool = pool->next)
19000     {
19001       /* Put it at the end of the relevent section.  */
19002       subseg_set (pool->section, pool->sub_section);
19003 #ifdef OBJ_ELF
19004       arm_elf_change_section ();
19005 #endif
19006       s_ltorg (0);
19007     }
19008 }
19009
19010 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
19011    ARM ones.  */
19012
19013 void
19014 arm_adjust_symtab (void)
19015 {
19016 #ifdef OBJ_COFF
19017   symbolS * sym;
19018
19019   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
19020     {
19021       if (ARM_IS_THUMB (sym))
19022         {
19023           if (THUMB_IS_FUNC (sym))
19024             {
19025               /* Mark the symbol as a Thumb function.  */
19026               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
19027                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
19028                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
19029
19030               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
19031                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
19032               else
19033                 as_bad (_("%s: unexpected function type: %d"),
19034                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
19035             }
19036           else switch (S_GET_STORAGE_CLASS (sym))
19037             {
19038             case C_EXT:
19039               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
19040               break;
19041             case C_STAT:
19042               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
19043               break;
19044             case C_LABEL:
19045               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
19046               break;
19047             default:
19048               /* Do nothing.  */
19049               break;
19050             }
19051         }
19052
19053       if (ARM_IS_INTERWORK (sym))
19054         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
19055     }
19056 #endif
19057 #ifdef OBJ_ELF
19058   symbolS * sym;
19059   char      bind;
19060
19061   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
19062     {
19063       if (ARM_IS_THUMB (sym))
19064         {
19065           elf_symbol_type * elf_sym;
19066
19067           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
19068           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
19069
19070           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
19071                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
19072             {
19073               /* If it's a .thumb_func, declare it as so,
19074                  otherwise tag label as .code 16.  */
19075               if (THUMB_IS_FUNC (sym))
19076                 elf_sym->internal_elf_sym.st_info =
19077                   ELF_ST_INFO (bind, STT_ARM_TFUNC);
19078               else
19079                 elf_sym->internal_elf_sym.st_info =
19080                   ELF_ST_INFO (bind, STT_ARM_16BIT);
19081             }
19082         }
19083     }
19084 #endif
19085 }
19086
19087 /* MD interface: Initialization.  */
19088
19089 static void
19090 set_constant_flonums (void)
19091 {
19092   int i;
19093
19094   for (i = 0; i < NUM_FLOAT_VALS; i++)
19095     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
19096       abort ();
19097 }
19098
19099 /* Auto-select Thumb mode if it's the only available instruction set for the
19100    given architecture.  */
19101
19102 static void
19103 autoselect_thumb_from_cpu_variant (void)
19104 {
19105   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
19106     opcode_select (16);
19107 }
19108
19109 void
19110 md_begin (void)
19111 {
19112   unsigned mach;
19113   unsigned int i;
19114
19115   if (   (arm_ops_hsh = hash_new ()) == NULL
19116       || (arm_cond_hsh = hash_new ()) == NULL
19117       || (arm_shift_hsh = hash_new ()) == NULL
19118       || (arm_psr_hsh = hash_new ()) == NULL
19119       || (arm_v7m_psr_hsh = hash_new ()) == NULL
19120       || (arm_reg_hsh = hash_new ()) == NULL
19121       || (arm_reloc_hsh = hash_new ()) == NULL
19122       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
19123     as_fatal (_("virtual memory exhausted"));
19124
19125   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
19126     hash_insert (arm_ops_hsh, insns[i].template, (PTR) (insns + i));
19127   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
19128     hash_insert (arm_cond_hsh, conds[i].template, (PTR) (conds + i));
19129   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
19130     hash_insert (arm_shift_hsh, shift_names[i].name, (PTR) (shift_names + i));
19131   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
19132     hash_insert (arm_psr_hsh, psrs[i].template, (PTR) (psrs + i));
19133   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
19134     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template, (PTR) (v7m_psrs + i));
19135   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
19136     hash_insert (arm_reg_hsh, reg_names[i].name, (PTR) (reg_names + i));
19137   for (i = 0;
19138        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
19139        i++)
19140     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template,
19141                  (PTR) (barrier_opt_names + i));
19142 #ifdef OBJ_ELF
19143   for (i = 0; i < sizeof (reloc_names) / sizeof (struct reloc_entry); i++)
19144     hash_insert (arm_reloc_hsh, reloc_names[i].name, (PTR) (reloc_names + i));
19145 #endif
19146
19147   set_constant_flonums ();
19148
19149   /* Set the cpu variant based on the command-line options.  We prefer
19150      -mcpu= over -march= if both are set (as for GCC); and we prefer
19151      -mfpu= over any other way of setting the floating point unit.
19152      Use of legacy options with new options are faulted.  */
19153   if (legacy_cpu)
19154     {
19155       if (mcpu_cpu_opt || march_cpu_opt)
19156         as_bad (_("use of old and new-style options to set CPU type"));
19157
19158       mcpu_cpu_opt = legacy_cpu;
19159     }
19160   else if (!mcpu_cpu_opt)
19161     mcpu_cpu_opt = march_cpu_opt;
19162
19163   if (legacy_fpu)
19164     {
19165       if (mfpu_opt)
19166         as_bad (_("use of old and new-style options to set FPU type"));
19167
19168       mfpu_opt = legacy_fpu;
19169     }
19170   else if (!mfpu_opt)
19171     {
19172 #if !(defined (TE_LINUX) || defined (TE_NetBSD) || defined (TE_VXWORKS))
19173       /* Some environments specify a default FPU.  If they don't, infer it
19174          from the processor.  */
19175       if (mcpu_fpu_opt)
19176         mfpu_opt = mcpu_fpu_opt;
19177       else
19178         mfpu_opt = march_fpu_opt;
19179 #else
19180       mfpu_opt = &fpu_default;
19181 #endif
19182     }
19183
19184   if (!mfpu_opt)
19185     {
19186       if (!mcpu_cpu_opt)
19187         mfpu_opt = &fpu_default;
19188       else if (ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
19189         mfpu_opt = &fpu_arch_vfp_v2;
19190       else
19191         mfpu_opt = &fpu_arch_fpa;
19192     }
19193
19194 #ifdef CPU_DEFAULT
19195   if (!mcpu_cpu_opt)
19196     {
19197       mcpu_cpu_opt = &cpu_default;
19198       selected_cpu = cpu_default;
19199     }
19200 #else
19201   if (mcpu_cpu_opt)
19202     selected_cpu = *mcpu_cpu_opt;
19203   else
19204     mcpu_cpu_opt = &arm_arch_any;
19205 #endif
19206
19207   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
19208
19209   autoselect_thumb_from_cpu_variant ();
19210
19211   arm_arch_used = thumb_arch_used = arm_arch_none;
19212
19213 #if defined OBJ_COFF || defined OBJ_ELF
19214   {
19215     unsigned int flags = 0;
19216
19217 #if defined OBJ_ELF
19218     flags = meabi_flags;
19219
19220     switch (meabi_flags)
19221       {
19222       case EF_ARM_EABI_UNKNOWN:
19223 #endif
19224         /* Set the flags in the private structure.  */
19225         if (uses_apcs_26)      flags |= F_APCS26;
19226         if (support_interwork) flags |= F_INTERWORK;
19227         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
19228         if (pic_code)          flags |= F_PIC;
19229         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
19230           flags |= F_SOFT_FLOAT;
19231
19232         switch (mfloat_abi_opt)
19233           {
19234           case ARM_FLOAT_ABI_SOFT:
19235           case ARM_FLOAT_ABI_SOFTFP:
19236             flags |= F_SOFT_FLOAT;
19237             break;
19238
19239           case ARM_FLOAT_ABI_HARD:
19240             if (flags & F_SOFT_FLOAT)
19241               as_bad (_("hard-float conflicts with specified fpu"));
19242             break;
19243           }
19244
19245         /* Using pure-endian doubles (even if soft-float).      */
19246         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
19247           flags |= F_VFP_FLOAT;
19248
19249 #if defined OBJ_ELF
19250         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
19251             flags |= EF_ARM_MAVERICK_FLOAT;
19252         break;
19253
19254       case EF_ARM_EABI_VER4:
19255       case EF_ARM_EABI_VER5:
19256         /* No additional flags to set.  */
19257         break;
19258
19259       default:
19260         abort ();
19261       }
19262 #endif
19263     bfd_set_private_flags (stdoutput, flags);
19264
19265     /* We have run out flags in the COFF header to encode the
19266        status of ATPCS support, so instead we create a dummy,
19267        empty, debug section called .arm.atpcs.  */
19268     if (atpcs)
19269       {
19270         asection * sec;
19271
19272         sec = bfd_make_section (stdoutput, ".arm.atpcs");
19273
19274         if (sec != NULL)
19275           {
19276             bfd_set_section_flags
19277               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
19278             bfd_set_section_size (stdoutput, sec, 0);
19279             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
19280           }
19281       }
19282   }
19283 #endif
19284
19285   /* Record the CPU type as well.  */
19286   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
19287     mach = bfd_mach_arm_iWMMXt2;
19288   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
19289     mach = bfd_mach_arm_iWMMXt;
19290   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
19291     mach = bfd_mach_arm_XScale;
19292   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
19293     mach = bfd_mach_arm_ep9312;
19294   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
19295     mach = bfd_mach_arm_5TE;
19296   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
19297     {
19298       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
19299         mach = bfd_mach_arm_5T;
19300       else
19301         mach = bfd_mach_arm_5;
19302     }
19303   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
19304     {
19305       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
19306         mach = bfd_mach_arm_4T;
19307       else
19308         mach = bfd_mach_arm_4;
19309     }
19310   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
19311     mach = bfd_mach_arm_3M;
19312   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
19313     mach = bfd_mach_arm_3;
19314   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
19315     mach = bfd_mach_arm_2a;
19316   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
19317     mach = bfd_mach_arm_2;
19318   else
19319     mach = bfd_mach_arm_unknown;
19320
19321   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
19322 }
19323
19324 /* Command line processing.  */
19325
19326 /* md_parse_option
19327       Invocation line includes a switch not recognized by the base assembler.
19328       See if it's a processor-specific option.
19329
19330       This routine is somewhat complicated by the need for backwards
19331       compatibility (since older releases of gcc can't be changed).
19332       The new options try to make the interface as compatible as
19333       possible with GCC.
19334
19335       New options (supported) are:
19336
19337               -mcpu=<cpu name>           Assemble for selected processor
19338               -march=<architecture name> Assemble for selected architecture
19339               -mfpu=<fpu architecture>   Assemble for selected FPU.
19340               -EB/-mbig-endian           Big-endian
19341               -EL/-mlittle-endian        Little-endian
19342               -k                         Generate PIC code
19343               -mthumb                    Start in Thumb mode
19344               -mthumb-interwork          Code supports ARM/Thumb interworking
19345
19346       For now we will also provide support for:
19347
19348               -mapcs-32                  32-bit Program counter
19349               -mapcs-26                  26-bit Program counter
19350               -macps-float               Floats passed in FP registers
19351               -mapcs-reentrant           Reentrant code
19352               -matpcs
19353       (sometime these will probably be replaced with -mapcs=<list of options>
19354       and -matpcs=<list of options>)
19355
19356       The remaining options are only supported for back-wards compatibility.
19357       Cpu variants, the arm part is optional:
19358               -m[arm]1                Currently not supported.
19359               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
19360               -m[arm]3                Arm 3 processor
19361               -m[arm]6[xx],           Arm 6 processors
19362               -m[arm]7[xx][t][[d]m]   Arm 7 processors
19363               -m[arm]8[10]            Arm 8 processors
19364               -m[arm]9[20][tdmi]      Arm 9 processors
19365               -mstrongarm[110[0]]     StrongARM processors
19366               -mxscale                XScale processors
19367               -m[arm]v[2345[t[e]]]    Arm architectures
19368               -mall                   All (except the ARM1)
19369       FP variants:
19370               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
19371               -mfpe-old               (No float load/store multiples)
19372               -mvfpxd                 VFP Single precision
19373               -mvfp                   All VFP
19374               -mno-fpu                Disable all floating point instructions
19375
19376       The following CPU names are recognized:
19377               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
19378               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
19379               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
19380               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
19381               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
19382               arm10t arm10e, arm1020t, arm1020e, arm10200e,
19383               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
19384
19385       */
19386
19387 const char * md_shortopts = "m:k";
19388
19389 #ifdef ARM_BI_ENDIAN
19390 #define OPTION_EB (OPTION_MD_BASE + 0)
19391 #define OPTION_EL (OPTION_MD_BASE + 1)
19392 #else
19393 #if TARGET_BYTES_BIG_ENDIAN
19394 #define OPTION_EB (OPTION_MD_BASE + 0)
19395 #else
19396 #define OPTION_EL (OPTION_MD_BASE + 1)
19397 #endif
19398 #endif
19399
19400 struct option md_longopts[] =
19401 {
19402 #ifdef OPTION_EB
19403   {"EB", no_argument, NULL, OPTION_EB},
19404 #endif
19405 #ifdef OPTION_EL
19406   {"EL", no_argument, NULL, OPTION_EL},
19407 #endif
19408   {NULL, no_argument, NULL, 0}
19409 };
19410
19411 size_t md_longopts_size = sizeof (md_longopts);
19412
19413 struct arm_option_table
19414 {
19415   char *option;         /* Option name to match.  */
19416   char *help;           /* Help information.  */
19417   int  *var;            /* Variable to change.  */
19418   int   value;          /* What to change it to.  */
19419   char *deprecated;     /* If non-null, print this message.  */
19420 };
19421
19422 struct arm_option_table arm_opts[] =
19423 {
19424   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
19425   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
19426   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
19427    &support_interwork, 1, NULL},
19428   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
19429   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
19430   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
19431    1, NULL},
19432   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
19433   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
19434   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
19435   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
19436    NULL},
19437
19438   /* These are recognized by the assembler, but have no affect on code.  */
19439   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
19440   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
19441   {NULL, NULL, NULL, 0, NULL}
19442 };
19443
19444 struct arm_legacy_option_table
19445 {
19446   char *option;                         /* Option name to match.  */
19447   const arm_feature_set **var;          /* Variable to change.  */
19448   const arm_feature_set value;          /* What to change it to.  */
19449   char *deprecated;                     /* If non-null, print this message.  */
19450 };
19451
19452 const struct arm_legacy_option_table arm_legacy_opts[] =
19453 {
19454   /* DON'T add any new processors to this list -- we want the whole list
19455      to go away...  Add them to the processors table instead.  */
19456   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
19457   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
19458   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
19459   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
19460   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
19461   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
19462   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
19463   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
19464   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
19465   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
19466   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
19467   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
19468   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
19469   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
19470   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
19471   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
19472   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
19473   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
19474   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
19475   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
19476   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
19477   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
19478   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
19479   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
19480   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
19481   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
19482   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
19483   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
19484   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
19485   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
19486   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
19487   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
19488   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
19489   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
19490   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
19491   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
19492   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
19493   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
19494   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
19495   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
19496   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
19497   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
19498   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
19499   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
19500   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
19501   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
19502   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
19503   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
19504   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
19505   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
19506   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
19507   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
19508   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
19509   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
19510   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
19511   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
19512   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
19513   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
19514   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
19515   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
19516   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
19517   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
19518   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
19519   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
19520   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
19521   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
19522   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
19523   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
19524   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
19525   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
19526    N_("use -mcpu=strongarm110")},
19527   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
19528    N_("use -mcpu=strongarm1100")},
19529   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
19530    N_("use -mcpu=strongarm1110")},
19531   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
19532   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
19533   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
19534
19535   /* Architecture variants -- don't add any more to this list either.  */
19536   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
19537   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
19538   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
19539   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
19540   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
19541   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
19542   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
19543   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
19544   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
19545   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
19546   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
19547   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
19548   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
19549   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
19550   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
19551   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
19552   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
19553   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
19554
19555   /* Floating point variants -- don't add any more to this list either.  */
19556   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
19557   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
19558   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
19559   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
19560    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
19561
19562   {NULL, NULL, ARM_ARCH_NONE, NULL}
19563 };
19564
19565 struct arm_cpu_option_table
19566 {
19567   char *name;
19568   const arm_feature_set value;
19569   /* For some CPUs we assume an FPU unless the user explicitly sets
19570      -mfpu=...  */
19571   const arm_feature_set default_fpu;
19572   /* The canonical name of the CPU, or NULL to use NAME converted to upper
19573      case.  */
19574   const char *canonical_name;
19575 };
19576
19577 /* This list should, at a minimum, contain all the cpu names
19578    recognized by GCC.  */
19579 static const struct arm_cpu_option_table arm_cpus[] =
19580 {
19581   {"all",               ARM_ANY,         FPU_ARCH_FPA,    NULL},
19582   {"arm1",              ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL},
19583   {"arm2",              ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL},
19584   {"arm250",            ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
19585   {"arm3",              ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
19586   {"arm6",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19587   {"arm60",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19588   {"arm600",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19589   {"arm610",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19590   {"arm620",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19591   {"arm7",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19592   {"arm7m",             ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
19593   {"arm7d",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19594   {"arm7dm",            ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
19595   {"arm7di",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19596   {"arm7dmi",           ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
19597   {"arm70",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19598   {"arm700",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19599   {"arm700i",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19600   {"arm710",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19601   {"arm710t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19602   {"arm720",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19603   {"arm720t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19604   {"arm740t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19605   {"arm710c",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19606   {"arm7100",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19607   {"arm7500",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19608   {"arm7500fe",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
19609   {"arm7t",             ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19610   {"arm7tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19611   {"arm7tdmi-s",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19612   {"arm8",              ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
19613   {"arm810",            ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
19614   {"strongarm",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
19615   {"strongarm1",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
19616   {"strongarm110",      ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
19617   {"strongarm1100",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
19618   {"strongarm1110",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
19619   {"arm9",              ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19620   {"arm920",            ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"},
19621   {"arm920t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19622   {"arm922t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19623   {"arm940t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19624   {"arm9tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
19625   /* For V5 or later processors we default to using VFP; but the user
19626      should really set the FPU type explicitly.  */
19627   {"arm9e-r0",          ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
19628   {"arm9e",             ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
19629   {"arm926ej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
19630   {"arm926ejs",         ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
19631   {"arm926ej-s",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
19632   {"arm946e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
19633   {"arm946e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"},
19634   {"arm946e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
19635   {"arm966e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
19636   {"arm966e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"},
19637   {"arm966e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
19638   {"arm968e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
19639   {"arm10t",            ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
19640   {"arm10tdmi",         ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
19641   {"arm10e",            ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
19642   {"arm1020",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"},
19643   {"arm1020t",          ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
19644   {"arm1020e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
19645   {"arm1022e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
19646   {"arm1026ejs",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM1026EJ-S"},
19647   {"arm1026ej-s",       ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
19648   {"arm1136js",         ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"},
19649   {"arm1136j-s",        ARM_ARCH_V6,     FPU_NONE,        NULL},
19650   {"arm1136jfs",        ARM_ARCH_V6,     FPU_ARCH_VFP_V2, "ARM1136JF-S"},
19651   {"arm1136jf-s",       ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL},
19652   {"mpcore",            ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, NULL},
19653   {"mpcorenovfp",       ARM_ARCH_V6K,    FPU_NONE,        NULL},
19654   {"arm1156t2-s",       ARM_ARCH_V6T2,   FPU_NONE,        NULL},
19655   {"arm1156t2f-s",      ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL},
19656   {"arm1176jz-s",       ARM_ARCH_V6ZK,   FPU_NONE,        NULL},
19657   {"arm1176jzf-s",      ARM_ARCH_V6ZK,   FPU_ARCH_VFP_V2, NULL},
19658   {"cortex-a8",         ARM_ARCH_V7A,    ARM_FEATURE(0, FPU_VFP_V3
19659                                                         | FPU_NEON_EXT_V1),
19660                                                           NULL},
19661   {"cortex-r4",         ARM_ARCH_V7R,    FPU_NONE,        NULL},
19662   {"cortex-m3",         ARM_ARCH_V7M,    FPU_NONE,        NULL},
19663   /* ??? XSCALE is really an architecture.  */
19664   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
19665   /* ??? iwmmxt is not a processor.  */
19666   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL},
19667   {"iwmmxt2",           ARM_ARCH_IWMMXT2,FPU_ARCH_VFP_V2, NULL},
19668   {"i80200",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
19669   /* Maverick */
19670   {"ep9312",    ARM_FEATURE(ARM_AEXT_V4T, ARM_CEXT_MAVERICK), FPU_ARCH_MAVERICK, "ARM920T"},
19671   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE, NULL}
19672 };
19673
19674 struct arm_arch_option_table
19675 {
19676   char *name;
19677   const arm_feature_set value;
19678   const arm_feature_set default_fpu;
19679 };
19680
19681 /* This list should, at a minimum, contain all the architecture names
19682    recognized by GCC.  */
19683 static const struct arm_arch_option_table arm_archs[] =
19684 {
19685   {"all",               ARM_ANY,         FPU_ARCH_FPA},
19686   {"armv1",             ARM_ARCH_V1,     FPU_ARCH_FPA},
19687   {"armv2",             ARM_ARCH_V2,     FPU_ARCH_FPA},
19688   {"armv2a",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
19689   {"armv2s",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
19690   {"armv3",             ARM_ARCH_V3,     FPU_ARCH_FPA},
19691   {"armv3m",            ARM_ARCH_V3M,    FPU_ARCH_FPA},
19692   {"armv4",             ARM_ARCH_V4,     FPU_ARCH_FPA},
19693   {"armv4xm",           ARM_ARCH_V4xM,   FPU_ARCH_FPA},
19694   {"armv4t",            ARM_ARCH_V4T,    FPU_ARCH_FPA},
19695   {"armv4txm",          ARM_ARCH_V4TxM,  FPU_ARCH_FPA},
19696   {"armv5",             ARM_ARCH_V5,     FPU_ARCH_VFP},
19697   {"armv5t",            ARM_ARCH_V5T,    FPU_ARCH_VFP},
19698   {"armv5txm",          ARM_ARCH_V5TxM,  FPU_ARCH_VFP},
19699   {"armv5te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP},
19700   {"armv5texp",         ARM_ARCH_V5TExP, FPU_ARCH_VFP},
19701   {"armv5tej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP},
19702   {"armv6",             ARM_ARCH_V6,     FPU_ARCH_VFP},
19703   {"armv6j",            ARM_ARCH_V6,     FPU_ARCH_VFP},
19704   {"armv6k",            ARM_ARCH_V6K,    FPU_ARCH_VFP},
19705   {"armv6z",            ARM_ARCH_V6Z,    FPU_ARCH_VFP},
19706   {"armv6zk",           ARM_ARCH_V6ZK,   FPU_ARCH_VFP},
19707   {"armv6t2",           ARM_ARCH_V6T2,   FPU_ARCH_VFP},
19708   {"armv6kt2",          ARM_ARCH_V6KT2,  FPU_ARCH_VFP},
19709   {"armv6zt2",          ARM_ARCH_V6ZT2,  FPU_ARCH_VFP},
19710   {"armv6zkt2",         ARM_ARCH_V6ZKT2, FPU_ARCH_VFP},
19711   {"armv7",             ARM_ARCH_V7,     FPU_ARCH_VFP},
19712   {"armv7a",            ARM_ARCH_V7A,    FPU_ARCH_VFP},
19713   {"armv7r",            ARM_ARCH_V7R,    FPU_ARCH_VFP},
19714   {"armv7m",            ARM_ARCH_V7M,    FPU_ARCH_VFP},
19715   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP},
19716   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP},
19717   {"iwmmxt2",           ARM_ARCH_IWMMXT2,FPU_ARCH_VFP},
19718   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE}
19719 };
19720
19721 /* ISA extensions in the co-processor space.  */
19722 struct arm_option_cpu_value_table
19723 {
19724   char *name;
19725   const arm_feature_set value;
19726 };
19727
19728 static const struct arm_option_cpu_value_table arm_extensions[] =
19729 {
19730   {"maverick",          ARM_FEATURE (0, ARM_CEXT_MAVERICK)},
19731   {"xscale",            ARM_FEATURE (0, ARM_CEXT_XSCALE)},
19732   {"iwmmxt",            ARM_FEATURE (0, ARM_CEXT_IWMMXT)},
19733   {"iwmmxt2",           ARM_FEATURE (0, ARM_CEXT_IWMMXT2)},
19734   {NULL,                ARM_ARCH_NONE}
19735 };
19736
19737 /* This list should, at a minimum, contain all the fpu names
19738    recognized by GCC.  */
19739 static const struct arm_option_cpu_value_table arm_fpus[] =
19740 {
19741   {"softfpa",           FPU_NONE},
19742   {"fpe",               FPU_ARCH_FPE},
19743   {"fpe2",              FPU_ARCH_FPE},
19744   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
19745   {"fpa",               FPU_ARCH_FPA},
19746   {"fpa10",             FPU_ARCH_FPA},
19747   {"fpa11",             FPU_ARCH_FPA},
19748   {"arm7500fe",         FPU_ARCH_FPA},
19749   {"softvfp",           FPU_ARCH_VFP},
19750   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
19751   {"vfp",               FPU_ARCH_VFP_V2},
19752   {"vfp9",              FPU_ARCH_VFP_V2},
19753   {"vfp3",              FPU_ARCH_VFP_V3},
19754   {"vfp10",             FPU_ARCH_VFP_V2},
19755   {"vfp10-r0",          FPU_ARCH_VFP_V1},
19756   {"vfpxd",             FPU_ARCH_VFP_V1xD},
19757   {"arm1020t",          FPU_ARCH_VFP_V1},
19758   {"arm1020e",          FPU_ARCH_VFP_V2},
19759   {"arm1136jfs",        FPU_ARCH_VFP_V2},
19760   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
19761   {"maverick",          FPU_ARCH_MAVERICK},
19762   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
19763   {NULL,                ARM_ARCH_NONE}
19764 };
19765
19766 struct arm_option_value_table
19767 {
19768   char *name;
19769   long value;
19770 };
19771
19772 static const struct arm_option_value_table arm_float_abis[] =
19773 {
19774   {"hard",      ARM_FLOAT_ABI_HARD},
19775   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
19776   {"soft",      ARM_FLOAT_ABI_SOFT},
19777   {NULL,        0}
19778 };
19779
19780 #ifdef OBJ_ELF
19781 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
19782 static const struct arm_option_value_table arm_eabis[] =
19783 {
19784   {"gnu",       EF_ARM_EABI_UNKNOWN},
19785   {"4",         EF_ARM_EABI_VER4},
19786   {"5",         EF_ARM_EABI_VER5},
19787   {NULL,        0}
19788 };
19789 #endif
19790
19791 struct arm_long_option_table
19792 {
19793   char * option;                /* Substring to match.  */
19794   char * help;                  /* Help information.  */
19795   int (* func) (char * subopt); /* Function to decode sub-option.  */
19796   char * deprecated;            /* If non-null, print this message.  */
19797 };
19798
19799 static int
19800 arm_parse_extension (char * str, const arm_feature_set **opt_p)
19801 {
19802   arm_feature_set *ext_set = xmalloc (sizeof (arm_feature_set));
19803
19804   /* Copy the feature set, so that we can modify it.  */
19805   *ext_set = **opt_p;
19806   *opt_p = ext_set;
19807
19808   while (str != NULL && *str != 0)
19809     {
19810       const struct arm_option_cpu_value_table * opt;
19811       char * ext;
19812       int optlen;
19813
19814       if (*str != '+')
19815         {
19816           as_bad (_("invalid architectural extension"));
19817           return 0;
19818         }
19819
19820       str++;
19821       ext = strchr (str, '+');
19822
19823       if (ext != NULL)
19824         optlen = ext - str;
19825       else
19826         optlen = strlen (str);
19827
19828       if (optlen == 0)
19829         {
19830           as_bad (_("missing architectural extension"));
19831           return 0;
19832         }
19833
19834       for (opt = arm_extensions; opt->name != NULL; opt++)
19835         if (strncmp (opt->name, str, optlen) == 0)
19836           {
19837             ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->value);
19838             break;
19839           }
19840
19841       if (opt->name == NULL)
19842         {
19843           as_bad (_("unknown architectural extnsion `%s'"), str);
19844           return 0;
19845         }
19846
19847       str = ext;
19848     };
19849
19850   return 1;
19851 }
19852
19853 static int
19854 arm_parse_cpu (char * str)
19855 {
19856   const struct arm_cpu_option_table * opt;
19857   char * ext = strchr (str, '+');
19858   int optlen;
19859
19860   if (ext != NULL)
19861     optlen = ext - str;
19862   else
19863     optlen = strlen (str);
19864
19865   if (optlen == 0)
19866     {
19867       as_bad (_("missing cpu name `%s'"), str);
19868       return 0;
19869     }
19870
19871   for (opt = arm_cpus; opt->name != NULL; opt++)
19872     if (strncmp (opt->name, str, optlen) == 0)
19873       {
19874         mcpu_cpu_opt = &opt->value;
19875         mcpu_fpu_opt = &opt->default_fpu;
19876         if (opt->canonical_name)
19877           strcpy(selected_cpu_name, opt->canonical_name);
19878         else
19879           {
19880             int i;
19881             for (i = 0; i < optlen; i++)
19882               selected_cpu_name[i] = TOUPPER (opt->name[i]);
19883             selected_cpu_name[i] = 0;
19884           }
19885
19886         if (ext != NULL)
19887           return arm_parse_extension (ext, &mcpu_cpu_opt);
19888
19889         return 1;
19890       }
19891
19892   as_bad (_("unknown cpu `%s'"), str);
19893   return 0;
19894 }
19895
19896 static int
19897 arm_parse_arch (char * str)
19898 {
19899   const struct arm_arch_option_table *opt;
19900   char *ext = strchr (str, '+');
19901   int optlen;
19902
19903   if (ext != NULL)
19904     optlen = ext - str;
19905   else
19906     optlen = strlen (str);
19907
19908   if (optlen == 0)
19909     {
19910       as_bad (_("missing architecture name `%s'"), str);
19911       return 0;
19912     }
19913
19914   for (opt = arm_archs; opt->name != NULL; opt++)
19915     if (streq (opt->name, str))
19916       {
19917         march_cpu_opt = &opt->value;
19918         march_fpu_opt = &opt->default_fpu;
19919         strcpy(selected_cpu_name, opt->name);
19920
19921         if (ext != NULL)
19922           return arm_parse_extension (ext, &march_cpu_opt);
19923
19924         return 1;
19925       }
19926
19927   as_bad (_("unknown architecture `%s'\n"), str);
19928   return 0;
19929 }
19930
19931 static int
19932 arm_parse_fpu (char * str)
19933 {
19934   const struct arm_option_cpu_value_table * opt;
19935
19936   for (opt = arm_fpus; opt->name != NULL; opt++)
19937     if (streq (opt->name, str))
19938       {
19939         mfpu_opt = &opt->value;
19940         return 1;
19941       }
19942
19943   as_bad (_("unknown floating point format `%s'\n"), str);
19944   return 0;
19945 }
19946
19947 static int
19948 arm_parse_float_abi (char * str)
19949 {
19950   const struct arm_option_value_table * opt;
19951
19952   for (opt = arm_float_abis; opt->name != NULL; opt++)
19953     if (streq (opt->name, str))
19954       {
19955         mfloat_abi_opt = opt->value;
19956         return 1;
19957       }
19958
19959   as_bad (_("unknown floating point abi `%s'\n"), str);
19960   return 0;
19961 }
19962
19963 #ifdef OBJ_ELF
19964 static int
19965 arm_parse_eabi (char * str)
19966 {
19967   const struct arm_option_value_table *opt;
19968
19969   for (opt = arm_eabis; opt->name != NULL; opt++)
19970     if (streq (opt->name, str))
19971       {
19972         meabi_flags = opt->value;
19973         return 1;
19974       }
19975   as_bad (_("unknown EABI `%s'\n"), str);
19976   return 0;
19977 }
19978 #endif
19979
19980 struct arm_long_option_table arm_long_opts[] =
19981 {
19982   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
19983    arm_parse_cpu, NULL},
19984   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
19985    arm_parse_arch, NULL},
19986   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
19987    arm_parse_fpu, NULL},
19988   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
19989    arm_parse_float_abi, NULL},
19990 #ifdef OBJ_ELF
19991   {"meabi=", N_("<ver>\t  assemble for eabi version <ver>"),
19992    arm_parse_eabi, NULL},
19993 #endif
19994   {NULL, NULL, 0, NULL}
19995 };
19996
19997 int
19998 md_parse_option (int c, char * arg)
19999 {
20000   struct arm_option_table *opt;
20001   const struct arm_legacy_option_table *fopt;
20002   struct arm_long_option_table *lopt;
20003
20004   switch (c)
20005     {
20006 #ifdef OPTION_EB
20007     case OPTION_EB:
20008       target_big_endian = 1;
20009       break;
20010 #endif
20011
20012 #ifdef OPTION_EL
20013     case OPTION_EL:
20014       target_big_endian = 0;
20015       break;
20016 #endif
20017
20018     case 'a':
20019       /* Listing option.  Just ignore these, we don't support additional
20020          ones.  */
20021       return 0;
20022
20023     default:
20024       for (opt = arm_opts; opt->option != NULL; opt++)
20025         {
20026           if (c == opt->option[0]
20027               && ((arg == NULL && opt->option[1] == 0)
20028                   || streq (arg, opt->option + 1)))
20029             {
20030 #if WARN_DEPRECATED
20031               /* If the option is deprecated, tell the user.  */
20032               if (opt->deprecated != NULL)
20033                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
20034                            arg ? arg : "", _(opt->deprecated));
20035 #endif
20036
20037               if (opt->var != NULL)
20038                 *opt->var = opt->value;
20039
20040               return 1;
20041             }
20042         }
20043
20044       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
20045         {
20046           if (c == fopt->option[0]
20047               && ((arg == NULL && fopt->option[1] == 0)
20048                   || streq (arg, fopt->option + 1)))
20049             {
20050 #if WARN_DEPRECATED
20051               /* If the option is deprecated, tell the user.  */
20052               if (fopt->deprecated != NULL)
20053                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
20054                            arg ? arg : "", _(fopt->deprecated));
20055 #endif
20056
20057               if (fopt->var != NULL)
20058                 *fopt->var = &fopt->value;
20059
20060               return 1;
20061             }
20062         }
20063
20064       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
20065         {
20066           /* These options are expected to have an argument.  */
20067           if (c == lopt->option[0]
20068               && arg != NULL
20069               && strncmp (arg, lopt->option + 1,
20070                           strlen (lopt->option + 1)) == 0)
20071             {
20072 #if WARN_DEPRECATED
20073               /* If the option is deprecated, tell the user.  */
20074               if (lopt->deprecated != NULL)
20075                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
20076                            _(lopt->deprecated));
20077 #endif
20078
20079               /* Call the sup-option parser.  */
20080               return lopt->func (arg + strlen (lopt->option) - 1);
20081             }
20082         }
20083
20084       return 0;
20085     }
20086
20087   return 1;
20088 }
20089
20090 void
20091 md_show_usage (FILE * fp)
20092 {
20093   struct arm_option_table *opt;
20094   struct arm_long_option_table *lopt;
20095
20096   fprintf (fp, _(" ARM-specific assembler options:\n"));
20097
20098   for (opt = arm_opts; opt->option != NULL; opt++)
20099     if (opt->help != NULL)
20100       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
20101
20102   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
20103     if (lopt->help != NULL)
20104       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
20105
20106 #ifdef OPTION_EB
20107   fprintf (fp, _("\
20108   -EB                     assemble code for a big-endian cpu\n"));
20109 #endif
20110
20111 #ifdef OPTION_EL
20112   fprintf (fp, _("\
20113   -EL                     assemble code for a little-endian cpu\n"));
20114 #endif
20115 }
20116
20117
20118 #ifdef OBJ_ELF
20119 typedef struct
20120 {
20121   int val;
20122   arm_feature_set flags;
20123 } cpu_arch_ver_table;
20124
20125 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
20126    least features first.  */
20127 static const cpu_arch_ver_table cpu_arch_ver[] =
20128 {
20129     {1, ARM_ARCH_V4},
20130     {2, ARM_ARCH_V4T},
20131     {3, ARM_ARCH_V5},
20132     {4, ARM_ARCH_V5TE},
20133     {5, ARM_ARCH_V5TEJ},
20134     {6, ARM_ARCH_V6},
20135     {7, ARM_ARCH_V6Z},
20136     {8, ARM_ARCH_V6K},
20137     {9, ARM_ARCH_V6T2},
20138     {10, ARM_ARCH_V7A},
20139     {10, ARM_ARCH_V7R},
20140     {10, ARM_ARCH_V7M},
20141     {0, ARM_ARCH_NONE}
20142 };
20143
20144 /* Set the public EABI object attributes.  */
20145 static void
20146 aeabi_set_public_attributes (void)
20147 {
20148   int arch;
20149   arm_feature_set flags;
20150   arm_feature_set tmp;
20151   const cpu_arch_ver_table *p;
20152
20153   /* Choose the architecture based on the capabilities of the requested cpu
20154      (if any) and/or the instructions actually used.  */
20155   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
20156   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
20157   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
20158   /*Allow the user to override the reported architecture.  */
20159   if (object_arch)
20160     {
20161       ARM_CLEAR_FEATURE (flags, flags, arm_arch_any);
20162       ARM_MERGE_FEATURE_SETS (flags, flags, *object_arch);
20163     }
20164
20165   tmp = flags;
20166   arch = 0;
20167   for (p = cpu_arch_ver; p->val; p++)
20168     {
20169       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
20170         {
20171           arch = p->val;
20172           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
20173         }
20174     }
20175
20176   /* Tag_CPU_name.  */
20177   if (selected_cpu_name[0])
20178     {
20179       char *p;
20180
20181       p = selected_cpu_name;
20182       if (strncmp(p, "armv", 4) == 0)
20183         {
20184           int i;
20185           
20186           p += 4;
20187           for (i = 0; p[i]; i++)
20188             p[i] = TOUPPER (p[i]);
20189         }
20190       elf32_arm_add_eabi_attr_string (stdoutput, 5, p);
20191     }
20192   /* Tag_CPU_arch.  */
20193   elf32_arm_add_eabi_attr_int (stdoutput, 6, arch);
20194   /* Tag_CPU_arch_profile.  */
20195   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a))
20196     elf32_arm_add_eabi_attr_int (stdoutput, 7, 'A');
20197   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
20198     elf32_arm_add_eabi_attr_int (stdoutput, 7, 'R');
20199   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m))
20200     elf32_arm_add_eabi_attr_int (stdoutput, 7, 'M');
20201   /* Tag_ARM_ISA_use.  */
20202   if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_full))
20203     elf32_arm_add_eabi_attr_int (stdoutput, 8, 1);
20204   /* Tag_THUMB_ISA_use.  */
20205   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_full))
20206     elf32_arm_add_eabi_attr_int (stdoutput, 9,
20207         ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2) ? 2 : 1);
20208   /* Tag_VFP_arch.  */
20209   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_vfp_ext_v3)
20210       || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_vfp_ext_v3))
20211     elf32_arm_add_eabi_attr_int (stdoutput, 10, 3);
20212   else if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_vfp_ext_v2)
20213            || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_vfp_ext_v2))
20214     elf32_arm_add_eabi_attr_int (stdoutput, 10, 2);
20215   else if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_vfp_ext_v1)
20216            || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_vfp_ext_v1)
20217            || ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_vfp_ext_v1xd)
20218            || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_vfp_ext_v1xd))
20219     elf32_arm_add_eabi_attr_int (stdoutput, 10, 1);
20220   /* Tag_WMMX_arch.  */
20221   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_cext_iwmmxt)
20222       || ARM_CPU_HAS_FEATURE (arm_arch_used, arm_cext_iwmmxt))
20223     elf32_arm_add_eabi_attr_int (stdoutput, 11, 1);
20224   /* Tag_NEON_arch.  */
20225   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_neon_ext_v1)
20226       || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_neon_ext_v1))
20227     elf32_arm_add_eabi_attr_int (stdoutput, 12, 1);
20228 }
20229
20230 /* Add the .ARM.attributes section.  */
20231 void
20232 arm_md_end (void)
20233 {
20234   segT s;
20235   char *p;
20236   addressT addr;
20237   offsetT size;
20238   
20239   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
20240     return;
20241
20242   aeabi_set_public_attributes ();
20243   size = elf32_arm_eabi_attr_size (stdoutput);
20244   s = subseg_new (".ARM.attributes", 0);
20245   bfd_set_section_flags (stdoutput, s, SEC_READONLY | SEC_DATA);
20246   addr = frag_now_fix ();
20247   p = frag_more (size);
20248   elf32_arm_set_eabi_attr_contents (stdoutput, (bfd_byte *)p, size);
20249 }
20250
20251
20252 /* Parse a .cpu directive.  */
20253
20254 static void
20255 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
20256 {
20257   const struct arm_cpu_option_table *opt;
20258   char *name;
20259   char saved_char;
20260
20261   name = input_line_pointer;
20262   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
20263     input_line_pointer++;
20264   saved_char = *input_line_pointer;
20265   *input_line_pointer = 0;
20266
20267   /* Skip the first "all" entry.  */
20268   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
20269     if (streq (opt->name, name))
20270       {
20271         mcpu_cpu_opt = &opt->value;
20272         selected_cpu = opt->value;
20273         if (opt->canonical_name)
20274           strcpy(selected_cpu_name, opt->canonical_name);
20275         else
20276           {
20277             int i;
20278             for (i = 0; opt->name[i]; i++)
20279               selected_cpu_name[i] = TOUPPER (opt->name[i]);
20280             selected_cpu_name[i] = 0;
20281           }
20282         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
20283         *input_line_pointer = saved_char;
20284         demand_empty_rest_of_line ();
20285         return;
20286       }
20287   as_bad (_("unknown cpu `%s'"), name);
20288   *input_line_pointer = saved_char;
20289   ignore_rest_of_line ();
20290 }
20291
20292
20293 /* Parse a .arch directive.  */
20294
20295 static void
20296 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
20297 {
20298   const struct arm_arch_option_table *opt;
20299   char saved_char;
20300   char *name;
20301
20302   name = input_line_pointer;
20303   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
20304     input_line_pointer++;
20305   saved_char = *input_line_pointer;
20306   *input_line_pointer = 0;
20307
20308   /* Skip the first "all" entry.  */
20309   for (opt = arm_archs + 1; opt->name != NULL; opt++)
20310     if (streq (opt->name, name))
20311       {
20312         mcpu_cpu_opt = &opt->value;
20313         selected_cpu = opt->value;
20314         strcpy(selected_cpu_name, opt->name);
20315         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
20316         *input_line_pointer = saved_char;
20317         demand_empty_rest_of_line ();
20318         return;
20319       }
20320
20321   as_bad (_("unknown architecture `%s'\n"), name);
20322   *input_line_pointer = saved_char;
20323   ignore_rest_of_line ();
20324 }
20325
20326
20327 /* Parse a .object_arch directive.  */
20328
20329 static void
20330 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
20331 {
20332   const struct arm_arch_option_table *opt;
20333   char saved_char;
20334   char *name;
20335
20336   name = input_line_pointer;
20337   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
20338     input_line_pointer++;
20339   saved_char = *input_line_pointer;
20340   *input_line_pointer = 0;
20341
20342   /* Skip the first "all" entry.  */
20343   for (opt = arm_archs + 1; opt->name != NULL; opt++)
20344     if (streq (opt->name, name))
20345       {
20346         object_arch = &opt->value;
20347         *input_line_pointer = saved_char;
20348         demand_empty_rest_of_line ();
20349         return;
20350       }
20351
20352   as_bad (_("unknown architecture `%s'\n"), name);
20353   *input_line_pointer = saved_char;
20354   ignore_rest_of_line ();
20355 }
20356
20357
20358 /* Parse a .fpu directive.  */
20359
20360 static void
20361 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
20362 {
20363   const struct arm_option_cpu_value_table *opt;
20364   char saved_char;
20365   char *name;
20366
20367   name = input_line_pointer;
20368   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
20369     input_line_pointer++;
20370   saved_char = *input_line_pointer;
20371   *input_line_pointer = 0;
20372   
20373   for (opt = arm_fpus; opt->name != NULL; opt++)
20374     if (streq (opt->name, name))
20375       {
20376         mfpu_opt = &opt->value;
20377         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
20378         *input_line_pointer = saved_char;
20379         demand_empty_rest_of_line ();
20380         return;
20381       }
20382
20383   as_bad (_("unknown floating point format `%s'\n"), name);
20384   *input_line_pointer = saved_char;
20385   ignore_rest_of_line ();
20386 }
20387 #endif /* OBJ_ELF */
20388