2006-01-31 Paul Brook <paul@codesourcery.com>
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005
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 #define  NO_RELOC 0
30 #include "as.h"
31 #include "safe-ctype.h"
32
33 /* Need TARGET_CPU.  */
34 #include "config.h"
35 #include "subsegs.h"
36 #include "obstack.h"
37 #include "symbols.h"
38 #include "listing.h"
39
40 #include "opcode/arm.h"
41
42 #ifdef OBJ_ELF
43 #include "elf/arm.h"
44 #include "dwarf2dbg.h"
45 #include "dw2gencfi.h"
46 #endif
47
48 /* XXX Set this to 1 after the next binutils release.  */
49 #define WARN_DEPRECATED 0
50
51 #ifdef OBJ_ELF
52 /* Must be at least the size of the largest unwind opcode (currently two).  */
53 #define ARM_OPCODE_CHUNK_SIZE 8
54
55 /* This structure holds the unwinding state.  */
56
57 static struct
58 {
59   symbolS *       proc_start;
60   symbolS *       table_entry;
61   symbolS *       personality_routine;
62   int             personality_index;
63   /* The segment containing the function.  */
64   segT            saved_seg;
65   subsegT         saved_subseg;
66   /* Opcodes generated from this function.  */
67   unsigned char * opcodes;
68   int             opcode_count;
69   int             opcode_alloc;
70   /* The number of bytes pushed to the stack.  */
71   offsetT         frame_size;
72   /* We don't add stack adjustment opcodes immediately so that we can merge
73      multiple adjustments.  We can also omit the final adjustment
74      when using a frame pointer.  */
75   offsetT         pending_offset;
76   /* These two fields are set by both unwind_movsp and unwind_setfp.  They
77      hold the reg+offset to use when restoring sp from a frame pointer.  */
78   offsetT         fp_offset;
79   int             fp_reg;
80   /* Nonzero if an unwind_setfp directive has been seen.  */
81   unsigned        fp_used:1;
82   /* Nonzero if the last opcode restores sp from fp_reg.  */
83   unsigned        sp_restored:1;
84 } unwind;
85
86 /* Bit N indicates that an R_ARM_NONE relocation has been output for
87    __aeabi_unwind_cpp_prN already if set. This enables dependencies to be
88    emitted only once per section, to save unnecessary bloat.  */
89 static unsigned int marked_pr_dependency = 0;
90
91 #endif /* OBJ_ELF */
92
93 enum arm_float_abi
94 {
95   ARM_FLOAT_ABI_HARD,
96   ARM_FLOAT_ABI_SOFTFP,
97   ARM_FLOAT_ABI_SOFT
98 };
99
100 /* Types of processor to assemble for.  */
101 #ifndef CPU_DEFAULT
102 #if defined __XSCALE__
103 #define CPU_DEFAULT     ARM_ARCH_XSCALE
104 #else
105 #if defined __thumb__
106 #define CPU_DEFAULT     ARM_ARCH_V5T
107 #endif
108 #endif
109 #endif
110
111 #ifndef FPU_DEFAULT
112 # ifdef TE_LINUX
113 #  define FPU_DEFAULT FPU_ARCH_FPA
114 # elif defined (TE_NetBSD)
115 #  ifdef OBJ_ELF
116 #   define FPU_DEFAULT FPU_ARCH_VFP     /* Soft-float, but VFP order.  */
117 #  else
118     /* Legacy a.out format.  */
119 #   define FPU_DEFAULT FPU_ARCH_FPA     /* Soft-float, but FPA order.  */
120 #  endif
121 # elif defined (TE_VXWORKS)
122 #  define FPU_DEFAULT FPU_ARCH_VFP      /* Soft-float, VFP order.  */
123 # else
124    /* For backwards compatibility, default to FPA.  */
125 #  define FPU_DEFAULT FPU_ARCH_FPA
126 # endif
127 #endif /* ifndef FPU_DEFAULT */
128
129 #define streq(a, b)           (strcmp (a, b) == 0)
130
131 static arm_feature_set cpu_variant;
132 static arm_feature_set arm_arch_used;
133 static arm_feature_set thumb_arch_used;
134
135 /* Flags stored in private area of BFD structure.  */
136 static int uses_apcs_26      = FALSE;
137 static int atpcs             = FALSE;
138 static int support_interwork = FALSE;
139 static int uses_apcs_float   = FALSE;
140 static int pic_code          = FALSE;
141
142 /* Variables that we set while parsing command-line options.  Once all
143    options have been read we re-process these values to set the real
144    assembly flags.  */
145 static const arm_feature_set *legacy_cpu = NULL;
146 static const arm_feature_set *legacy_fpu = NULL;
147
148 static const arm_feature_set *mcpu_cpu_opt = NULL;
149 static const arm_feature_set *mcpu_fpu_opt = NULL;
150 static const arm_feature_set *march_cpu_opt = NULL;
151 static const arm_feature_set *march_fpu_opt = NULL;
152 static const arm_feature_set *mfpu_opt = NULL;
153
154 /* Constants for known architecture features.  */
155 static const arm_feature_set fpu_default = FPU_DEFAULT;
156 static const arm_feature_set fpu_arch_vfp_v1 = FPU_ARCH_VFP_V1;
157 static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
158 static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
159 static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
160 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
161 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
162
163 #ifdef CPU_DEFAULT
164 static const arm_feature_set cpu_default = CPU_DEFAULT;
165 #endif
166
167 static const arm_feature_set arm_ext_v1 = ARM_FEATURE (ARM_EXT_V1, 0);
168 static const arm_feature_set arm_ext_v2 = ARM_FEATURE (ARM_EXT_V1, 0);
169 static const arm_feature_set arm_ext_v2s = ARM_FEATURE (ARM_EXT_V2S, 0);
170 static const arm_feature_set arm_ext_v3 = ARM_FEATURE (ARM_EXT_V3, 0);
171 static const arm_feature_set arm_ext_v3m = ARM_FEATURE (ARM_EXT_V3M, 0);
172 static const arm_feature_set arm_ext_v4 = ARM_FEATURE (ARM_EXT_V4, 0);
173 static const arm_feature_set arm_ext_v4t = ARM_FEATURE (ARM_EXT_V4T, 0);
174 static const arm_feature_set arm_ext_v5 = ARM_FEATURE (ARM_EXT_V5, 0);
175 static const arm_feature_set arm_ext_v4t_5 =
176   ARM_FEATURE (ARM_EXT_V4T | ARM_EXT_V5, 0);
177 static const arm_feature_set arm_ext_v5t = ARM_FEATURE (ARM_EXT_V5T, 0);
178 static const arm_feature_set arm_ext_v5e = ARM_FEATURE (ARM_EXT_V5E, 0);
179 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE (ARM_EXT_V5ExP, 0);
180 static const arm_feature_set arm_ext_v5j = ARM_FEATURE (ARM_EXT_V5J, 0);
181 static const arm_feature_set arm_ext_v6 = ARM_FEATURE (ARM_EXT_V6, 0);
182 static const arm_feature_set arm_ext_v6k = ARM_FEATURE (ARM_EXT_V6K, 0);
183 static const arm_feature_set arm_ext_v6z = ARM_FEATURE (ARM_EXT_V6Z, 0);
184 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE (ARM_EXT_V6T2, 0);
185
186 static const arm_feature_set arm_arch_any = ARM_ANY;
187 static const arm_feature_set arm_arch_full = ARM_FEATURE (-1, -1);
188 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
189 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
190
191 static const arm_feature_set arm_cext_iwmmxt =
192   ARM_FEATURE (0, ARM_CEXT_IWMMXT);
193 static const arm_feature_set arm_cext_xscale =
194   ARM_FEATURE (0, ARM_CEXT_XSCALE);
195 static const arm_feature_set arm_cext_maverick =
196   ARM_FEATURE (0, ARM_CEXT_MAVERICK);
197 static const arm_feature_set fpu_fpa_ext_v1 = ARM_FEATURE (0, FPU_FPA_EXT_V1);
198 static const arm_feature_set fpu_fpa_ext_v2 = ARM_FEATURE (0, FPU_FPA_EXT_V2);
199 static const arm_feature_set fpu_vfp_ext_v1xd =
200   ARM_FEATURE (0, FPU_VFP_EXT_V1xD);
201 static const arm_feature_set fpu_vfp_ext_v1 = ARM_FEATURE (0, FPU_VFP_EXT_V1);
202 static const arm_feature_set fpu_vfp_ext_v2 = ARM_FEATURE (0, FPU_VFP_EXT_V2);
203
204 static int mfloat_abi_opt = -1;
205 /* Record user cpu selection for object attributes.  */
206 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
207 /* Must be long enough to hold any of the names in arm_cpus.  */
208 static char selected_cpu_name[16];
209 #ifdef OBJ_ELF
210 # ifdef EABI_DEFAULT
211 static int meabi_flags = EABI_DEFAULT;
212 # else
213 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
214 # endif
215 #endif
216
217 #ifdef OBJ_ELF
218 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
219 symbolS * GOT_symbol;
220 #endif
221
222 /* 0: assemble for ARM,
223    1: assemble for Thumb,
224    2: assemble for Thumb even though target CPU does not support thumb
225       instructions.  */
226 static int thumb_mode = 0;
227
228 /* If unified_syntax is true, we are processing the new unified
229    ARM/Thumb syntax.  Important differences from the old ARM mode:
230
231      - Immediate operands do not require a # prefix.
232      - Conditional affixes always appear at the end of the
233        instruction.  (For backward compatibility, those instructions
234        that formerly had them in the middle, continue to accept them
235        there.)
236      - The IT instruction may appear, and if it does is validated
237        against subsequent conditional affixes.  It does not generate
238        machine code.
239
240    Important differences from the old Thumb mode:
241
242      - Immediate operands do not require a # prefix.
243      - Most of the V6T2 instructions are only available in unified mode.
244      - The .N and .W suffixes are recognized and honored (it is an error
245        if they cannot be honored).
246      - All instructions set the flags if and only if they have an 's' affix.
247      - Conditional affixes may be used.  They are validated against
248        preceding IT instructions.  Unlike ARM mode, you cannot use a
249        conditional affix except in the scope of an IT instruction.  */
250
251 static bfd_boolean unified_syntax = FALSE;
252
253 struct arm_it
254 {
255   const char *  error;
256   unsigned long instruction;
257   int           size;
258   int           size_req;
259   int           cond;
260   /* Set to the opcode if the instruction needs relaxation.
261      Zero if the instruction is not relaxed.  */
262   unsigned long relax;
263   struct
264   {
265     bfd_reloc_code_real_type type;
266     expressionS              exp;
267     int                      pc_rel;
268   } reloc;
269
270   struct
271   {
272     unsigned reg;
273     signed int imm;
274     unsigned present    : 1;  /* Operand present.  */
275     unsigned isreg      : 1;  /* Operand was a register.  */
276     unsigned immisreg   : 1;  /* .imm field is a second register.  */
277     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
278     unsigned writeback  : 1;  /* Operand has trailing !  */
279     unsigned preind     : 1;  /* Preindexed address.  */
280     unsigned postind    : 1;  /* Postindexed address.  */
281     unsigned negative   : 1;  /* Index register was negated.  */
282     unsigned shifted    : 1;  /* Shift applied to operation.  */
283     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
284   } operands[6];
285 };
286
287 static struct arm_it inst;
288
289 #define NUM_FLOAT_VALS 8
290
291 const char * fp_const[] =
292 {
293   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
294 };
295
296 /* Number of littlenums required to hold an extended precision number.  */
297 #define MAX_LITTLENUMS 6
298
299 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
300
301 #define FAIL    (-1)
302 #define SUCCESS (0)
303
304 #define SUFF_S 1
305 #define SUFF_D 2
306 #define SUFF_E 3
307 #define SUFF_P 4
308
309 #define CP_T_X   0x00008000
310 #define CP_T_Y   0x00400000
311
312 #define CONDS_BIT        0x00100000
313 #define LOAD_BIT         0x00100000
314
315 #define DOUBLE_LOAD_FLAG 0x00000001
316
317 struct asm_cond
318 {
319   const char *  template;
320   unsigned long value;
321 };
322
323 #define COND_ALWAYS 0xE
324
325 struct asm_psr
326 {
327   const char *template;
328   unsigned long field;
329 };
330
331 /* The bit that distinguishes CPSR and SPSR.  */
332 #define SPSR_BIT   (1 << 22)
333
334 /* The individual PSR flag bits.  */
335 #define PSR_c   (1 << 16)
336 #define PSR_x   (1 << 17)
337 #define PSR_s   (1 << 18)
338 #define PSR_f   (1 << 19)
339
340 struct reloc_entry
341 {
342   char *name;
343   bfd_reloc_code_real_type reloc;
344 };
345
346 enum vfp_sp_reg_pos
347 {
348   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn
349 };
350
351 enum vfp_ldstm_type
352 {
353   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
354 };
355
356 /* ARM register categories.  This includes coprocessor numbers and various
357    architecture extensions' registers.  */
358 enum arm_reg_type
359 {
360   REG_TYPE_RN,
361   REG_TYPE_CP,
362   REG_TYPE_CN,
363   REG_TYPE_FN,
364   REG_TYPE_VFS,
365   REG_TYPE_VFD,
366   REG_TYPE_VFC,
367   REG_TYPE_MVF,
368   REG_TYPE_MVD,
369   REG_TYPE_MVFX,
370   REG_TYPE_MVDX,
371   REG_TYPE_MVAX,
372   REG_TYPE_DSPSC,
373   REG_TYPE_MMXWR,
374   REG_TYPE_MMXWC,
375   REG_TYPE_MMXWCG,
376   REG_TYPE_XSCALE,
377 };
378
379 /* Structure for a hash table entry for a register.  */
380 struct reg_entry
381 {
382   const char   *name;
383   unsigned char number;
384   unsigned char type;
385   unsigned char builtin;
386 };
387
388 /* Diagnostics used when we don't get a register of the expected type.  */
389 const char *const reg_expected_msgs[] =
390 {
391   N_("ARM register expected"),
392   N_("bad or missing co-processor number"),
393   N_("co-processor register expected"),
394   N_("FPA register expected"),
395   N_("VFP single precision register expected"),
396   N_("VFP double precision register expected"),
397   N_("VFP system register expected"),
398   N_("Maverick MVF register expected"),
399   N_("Maverick MVD register expected"),
400   N_("Maverick MVFX register expected"),
401   N_("Maverick MVDX register expected"),
402   N_("Maverick MVAX register expected"),
403   N_("Maverick DSPSC register expected"),
404   N_("iWMMXt data register expected"),
405   N_("iWMMXt control register expected"),
406   N_("iWMMXt scalar register expected"),
407   N_("XScale accumulator register expected"),
408 };
409
410 /* Some well known registers that we refer to directly elsewhere.  */
411 #define REG_SP  13
412 #define REG_LR  14
413 #define REG_PC  15
414
415 /* ARM instructions take 4bytes in the object file, Thumb instructions
416    take 2:  */
417 #define INSN_SIZE       4
418
419 struct asm_opcode
420 {
421   /* Basic string to match.  */
422   const char *template;
423
424   /* Parameters to instruction.  */
425   unsigned char operands[8];
426
427   /* Conditional tag - see opcode_lookup.  */
428   unsigned int tag : 4;
429
430   /* Basic instruction code.  */
431   unsigned int avalue : 28;
432
433   /* Thumb-format instruction code.  */
434   unsigned int tvalue;
435
436   /* Which architecture variant provides this instruction.  */
437   const arm_feature_set *avariant;
438   const arm_feature_set *tvariant;
439
440   /* Function to call to encode instruction in ARM format.  */
441   void (* aencode) (void);
442
443   /* Function to call to encode instruction in Thumb format.  */
444   void (* tencode) (void);
445 };
446
447 /* Defines for various bits that we will want to toggle.  */
448 #define INST_IMMEDIATE  0x02000000
449 #define OFFSET_REG      0x02000000
450 #define HWOFFSET_IMM    0x00400000
451 #define SHIFT_BY_REG    0x00000010
452 #define PRE_INDEX       0x01000000
453 #define INDEX_UP        0x00800000
454 #define WRITE_BACK      0x00200000
455 #define LDM_TYPE_2_OR_3 0x00400000
456
457 #define LITERAL_MASK    0xf000f000
458 #define OPCODE_MASK     0xfe1fffff
459 #define V4_STR_BIT      0x00000020
460
461 #define DATA_OP_SHIFT   21
462
463 /* Codes to distinguish the arithmetic instructions.  */
464 #define OPCODE_AND      0
465 #define OPCODE_EOR      1
466 #define OPCODE_SUB      2
467 #define OPCODE_RSB      3
468 #define OPCODE_ADD      4
469 #define OPCODE_ADC      5
470 #define OPCODE_SBC      6
471 #define OPCODE_RSC      7
472 #define OPCODE_TST      8
473 #define OPCODE_TEQ      9
474 #define OPCODE_CMP      10
475 #define OPCODE_CMN      11
476 #define OPCODE_ORR      12
477 #define OPCODE_MOV      13
478 #define OPCODE_BIC      14
479 #define OPCODE_MVN      15
480
481 #define T_OPCODE_MUL 0x4340
482 #define T_OPCODE_TST 0x4200
483 #define T_OPCODE_CMN 0x42c0
484 #define T_OPCODE_NEG 0x4240
485 #define T_OPCODE_MVN 0x43c0
486
487 #define T_OPCODE_ADD_R3 0x1800
488 #define T_OPCODE_SUB_R3 0x1a00
489 #define T_OPCODE_ADD_HI 0x4400
490 #define T_OPCODE_ADD_ST 0xb000
491 #define T_OPCODE_SUB_ST 0xb080
492 #define T_OPCODE_ADD_SP 0xa800
493 #define T_OPCODE_ADD_PC 0xa000
494 #define T_OPCODE_ADD_I8 0x3000
495 #define T_OPCODE_SUB_I8 0x3800
496 #define T_OPCODE_ADD_I3 0x1c00
497 #define T_OPCODE_SUB_I3 0x1e00
498
499 #define T_OPCODE_ASR_R  0x4100
500 #define T_OPCODE_LSL_R  0x4080
501 #define T_OPCODE_LSR_R  0x40c0
502 #define T_OPCODE_ROR_R  0x41c0
503 #define T_OPCODE_ASR_I  0x1000
504 #define T_OPCODE_LSL_I  0x0000
505 #define T_OPCODE_LSR_I  0x0800
506
507 #define T_OPCODE_MOV_I8 0x2000
508 #define T_OPCODE_CMP_I8 0x2800
509 #define T_OPCODE_CMP_LR 0x4280
510 #define T_OPCODE_MOV_HR 0x4600
511 #define T_OPCODE_CMP_HR 0x4500
512
513 #define T_OPCODE_LDR_PC 0x4800
514 #define T_OPCODE_LDR_SP 0x9800
515 #define T_OPCODE_STR_SP 0x9000
516 #define T_OPCODE_LDR_IW 0x6800
517 #define T_OPCODE_STR_IW 0x6000
518 #define T_OPCODE_LDR_IH 0x8800
519 #define T_OPCODE_STR_IH 0x8000
520 #define T_OPCODE_LDR_IB 0x7800
521 #define T_OPCODE_STR_IB 0x7000
522 #define T_OPCODE_LDR_RW 0x5800
523 #define T_OPCODE_STR_RW 0x5000
524 #define T_OPCODE_LDR_RH 0x5a00
525 #define T_OPCODE_STR_RH 0x5200
526 #define T_OPCODE_LDR_RB 0x5c00
527 #define T_OPCODE_STR_RB 0x5400
528
529 #define T_OPCODE_PUSH   0xb400
530 #define T_OPCODE_POP    0xbc00
531
532 #define T_OPCODE_BRANCH 0xe000
533
534 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
535 #define THUMB_PP_PC_LR 0x0100
536 #define THUMB_LOAD_BIT 0x0800
537
538 #define BAD_ARGS        _("bad arguments to instruction")
539 #define BAD_PC          _("r15 not allowed here")
540 #define BAD_COND        _("instruction cannot be conditional")
541 #define BAD_OVERLAP     _("registers may not be the same")
542 #define BAD_HIREG       _("lo register required")
543 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
544 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
545
546 static struct hash_control *arm_ops_hsh;
547 static struct hash_control *arm_cond_hsh;
548 static struct hash_control *arm_shift_hsh;
549 static struct hash_control *arm_psr_hsh;
550 static struct hash_control *arm_reg_hsh;
551 static struct hash_control *arm_reloc_hsh;
552
553 /* Stuff needed to resolve the label ambiguity
554    As:
555      ...
556      label:   <insn>
557    may differ from:
558      ...
559      label:
560               <insn>
561 */
562
563 symbolS *  last_label_seen;
564 static int label_is_thumb_function_name = FALSE;
565 \f
566 /* Literal pool structure.  Held on a per-section
567    and per-sub-section basis.  */
568
569 #define MAX_LITERAL_POOL_SIZE 1024
570 typedef struct literal_pool
571 {
572   expressionS    literals [MAX_LITERAL_POOL_SIZE];
573   unsigned int   next_free_entry;
574   unsigned int   id;
575   symbolS *      symbol;
576   segT           section;
577   subsegT        sub_section;
578   struct literal_pool * next;
579 } literal_pool;
580
581 /* Pointer to a linked list of literal pools.  */
582 literal_pool * list_of_pools = NULL;
583
584 /* State variables for IT block handling.  */
585 static bfd_boolean current_it_mask = 0;
586 static int current_cc;
587
588 \f
589 /* Pure syntax.  */
590
591 /* This array holds the chars that always start a comment.  If the
592    pre-processor is disabled, these aren't very useful.  */
593 const char comment_chars[] = "@";
594
595 /* This array holds the chars that only start a comment at the beginning of
596    a line.  If the line seems to have the form '# 123 filename'
597    .line and .file directives will appear in the pre-processed output.  */
598 /* Note that input_file.c hand checks for '#' at the beginning of the
599    first line of the input file.  This is because the compiler outputs
600    #NO_APP at the beginning of its output.  */
601 /* Also note that comments like this one will always work.  */
602 const char line_comment_chars[] = "#";
603
604 const char line_separator_chars[] = ";";
605
606 /* Chars that can be used to separate mant
607    from exp in floating point numbers.  */
608 const char EXP_CHARS[] = "eE";
609
610 /* Chars that mean this number is a floating point constant.  */
611 /* As in 0f12.456  */
612 /* or    0d1.2345e12  */
613
614 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
615
616 /* Prefix characters that indicate the start of an immediate
617    value.  */
618 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
619
620 /* Separator character handling.  */
621
622 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
623
624 static inline int
625 skip_past_char (char ** str, char c)
626 {
627   if (**str == c)
628     {
629       (*str)++;
630       return SUCCESS;
631     }
632   else
633     return FAIL;
634 }
635 #define skip_past_comma(str) skip_past_char (str, ',')
636
637 /* Arithmetic expressions (possibly involving symbols).  */
638
639 /* Return TRUE if anything in the expression is a bignum.  */
640
641 static int
642 walk_no_bignums (symbolS * sp)
643 {
644   if (symbol_get_value_expression (sp)->X_op == O_big)
645     return 1;
646
647   if (symbol_get_value_expression (sp)->X_add_symbol)
648     {
649       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
650               || (symbol_get_value_expression (sp)->X_op_symbol
651                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
652     }
653
654   return 0;
655 }
656
657 static int in_my_get_expression = 0;
658
659 /* Third argument to my_get_expression.  */
660 #define GE_NO_PREFIX 0
661 #define GE_IMM_PREFIX 1
662 #define GE_OPT_PREFIX 2
663
664 static int
665 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
666 {
667   char * save_in;
668   segT   seg;
669
670   /* In unified syntax, all prefixes are optional.  */
671   if (unified_syntax)
672     prefix_mode = GE_OPT_PREFIX;
673
674   switch (prefix_mode)
675     {
676     case GE_NO_PREFIX: break;
677     case GE_IMM_PREFIX:
678       if (!is_immediate_prefix (**str))
679         {
680           inst.error = _("immediate expression requires a # prefix");
681           return FAIL;
682         }
683       (*str)++;
684       break;
685     case GE_OPT_PREFIX:
686       if (is_immediate_prefix (**str))
687         (*str)++;
688       break;
689     default: abort ();
690     }
691
692   memset (ep, 0, sizeof (expressionS));
693
694   save_in = input_line_pointer;
695   input_line_pointer = *str;
696   in_my_get_expression = 1;
697   seg = expression (ep);
698   in_my_get_expression = 0;
699
700   if (ep->X_op == O_illegal)
701     {
702       /* We found a bad expression in md_operand().  */
703       *str = input_line_pointer;
704       input_line_pointer = save_in;
705       if (inst.error == NULL)
706         inst.error = _("bad expression");
707       return 1;
708     }
709
710 #ifdef OBJ_AOUT
711   if (seg != absolute_section
712       && seg != text_section
713       && seg != data_section
714       && seg != bss_section
715       && seg != undefined_section)
716     {
717       inst.error = _("bad segment");
718       *str = input_line_pointer;
719       input_line_pointer = save_in;
720       return 1;
721     }
722 #endif
723
724   /* Get rid of any bignums now, so that we don't generate an error for which
725      we can't establish a line number later on.  Big numbers are never valid
726      in instructions, which is where this routine is always called.  */
727   if (ep->X_op == O_big
728       || (ep->X_add_symbol
729           && (walk_no_bignums (ep->X_add_symbol)
730               || (ep->X_op_symbol
731                   && walk_no_bignums (ep->X_op_symbol)))))
732     {
733       inst.error = _("invalid constant");
734       *str = input_line_pointer;
735       input_line_pointer = save_in;
736       return 1;
737     }
738
739   *str = input_line_pointer;
740   input_line_pointer = save_in;
741   return 0;
742 }
743
744 /* Turn a string in input_line_pointer into a floating point constant
745    of type TYPE, and store the appropriate bytes in *LITP.  The number
746    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
747    returned, or NULL on OK.
748
749    Note that fp constants aren't represent in the normal way on the ARM.
750    In big endian mode, things are as expected.  However, in little endian
751    mode fp constants are big-endian word-wise, and little-endian byte-wise
752    within the words.  For example, (double) 1.1 in big endian mode is
753    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
754    the byte sequence 99 99 f1 3f 9a 99 99 99.
755
756    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
757
758 char *
759 md_atof (int type, char * litP, int * sizeP)
760 {
761   int prec;
762   LITTLENUM_TYPE words[MAX_LITTLENUMS];
763   char *t;
764   int i;
765
766   switch (type)
767     {
768     case 'f':
769     case 'F':
770     case 's':
771     case 'S':
772       prec = 2;
773       break;
774
775     case 'd':
776     case 'D':
777     case 'r':
778     case 'R':
779       prec = 4;
780       break;
781
782     case 'x':
783     case 'X':
784       prec = 6;
785       break;
786
787     case 'p':
788     case 'P':
789       prec = 6;
790       break;
791
792     default:
793       *sizeP = 0;
794       return _("bad call to MD_ATOF()");
795     }
796
797   t = atof_ieee (input_line_pointer, type, words);
798   if (t)
799     input_line_pointer = t;
800   *sizeP = prec * 2;
801
802   if (target_big_endian)
803     {
804       for (i = 0; i < prec; i++)
805         {
806           md_number_to_chars (litP, (valueT) words[i], 2);
807           litP += 2;
808         }
809     }
810   else
811     {
812       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
813         for (i = prec - 1; i >= 0; i--)
814           {
815             md_number_to_chars (litP, (valueT) words[i], 2);
816             litP += 2;
817           }
818       else
819         /* For a 4 byte float the order of elements in `words' is 1 0.
820            For an 8 byte float the order is 1 0 3 2.  */
821         for (i = 0; i < prec; i += 2)
822           {
823             md_number_to_chars (litP, (valueT) words[i + 1], 2);
824             md_number_to_chars (litP + 2, (valueT) words[i], 2);
825             litP += 4;
826           }
827     }
828
829   return 0;
830 }
831
832 /* We handle all bad expressions here, so that we can report the faulty
833    instruction in the error message.  */
834 void
835 md_operand (expressionS * expr)
836 {
837   if (in_my_get_expression)
838     expr->X_op = O_illegal;
839 }
840
841 /* Immediate values.  */
842
843 /* Generic immediate-value read function for use in directives.
844    Accepts anything that 'expression' can fold to a constant.
845    *val receives the number.  */
846 #ifdef OBJ_ELF
847 static int
848 immediate_for_directive (int *val)
849 {
850   expressionS exp;
851   exp.X_op = O_illegal;
852
853   if (is_immediate_prefix (*input_line_pointer))
854     {
855       input_line_pointer++;
856       expression (&exp);
857     }
858
859   if (exp.X_op != O_constant)
860     {
861       as_bad (_("expected #constant"));
862       ignore_rest_of_line ();
863       return FAIL;
864     }
865   *val = exp.X_add_number;
866   return SUCCESS;
867 }
868 #endif
869
870 /* Register parsing.  */
871
872 /* Generic register parser.  CCP points to what should be the
873    beginning of a register name.  If it is indeed a valid register
874    name, advance CCP over it and return the reg_entry structure;
875    otherwise return NULL.  Does not issue diagnostics.  */
876
877 static struct reg_entry *
878 arm_reg_parse_multi (char **ccp)
879 {
880   char *start = *ccp;
881   char *p;
882   struct reg_entry *reg;
883
884 #ifdef REGISTER_PREFIX
885   if (*start != REGISTER_PREFIX)
886     return NULL;
887   start++;
888 #endif
889 #ifdef OPTIONAL_REGISTER_PREFIX
890   if (*start == OPTIONAL_REGISTER_PREFIX)
891     start++;
892 #endif
893
894   p = start;
895   if (!ISALPHA (*p) || !is_name_beginner (*p))
896     return NULL;
897
898   do
899     p++;
900   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
901
902   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
903
904   if (!reg)
905     return NULL;
906
907   *ccp = p;
908   return reg;
909 }
910
911 /* As above, but the register must be of type TYPE, and the return
912    value is the register number or FAIL.  */
913
914 static int
915 arm_reg_parse (char **ccp, enum arm_reg_type type)
916 {
917   char *start = *ccp;
918   struct reg_entry *reg = arm_reg_parse_multi (ccp);
919
920   if (reg && reg->type == type)
921     return reg->number;
922
923   /* Alternative syntaxes are accepted for a few register classes.  */
924   switch (type)
925     {
926     case REG_TYPE_MVF:
927     case REG_TYPE_MVD:
928     case REG_TYPE_MVFX:
929     case REG_TYPE_MVDX:
930       /* Generic coprocessor register names are allowed for these.  */
931       if (reg->type == REG_TYPE_CN)
932         return reg->number;
933       break;
934
935     case REG_TYPE_CP:
936       /* For backward compatibility, a bare number is valid here.  */
937       {
938         unsigned long processor = strtoul (start, ccp, 10);
939         if (*ccp != start && processor <= 15)
940           return processor;
941       }
942
943     case REG_TYPE_MMXWC:
944       /* WC includes WCG.  ??? I'm not sure this is true for all
945          instructions that take WC registers.  */
946       if (reg->type == REG_TYPE_MMXWCG)
947         return reg->number;
948       break;
949
950     default:
951       break;
952     }
953
954   *ccp = start;
955   return FAIL;
956 }
957
958 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
959 static long
960 parse_reg_list (char ** strp)
961 {
962   char * str = * strp;
963   long   range = 0;
964   int    another_range;
965
966   /* We come back here if we get ranges concatenated by '+' or '|'.  */
967   do
968     {
969       another_range = 0;
970
971       if (*str == '{')
972         {
973           int in_range = 0;
974           int cur_reg = -1;
975
976           str++;
977           do
978             {
979               int reg;
980
981               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
982                 {
983                   inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
984                   return FAIL;
985                 }
986
987               if (in_range)
988                 {
989                   int i;
990
991                   if (reg <= cur_reg)
992                     {
993                       inst.error = _("bad range in register list");
994                       return FAIL;
995                     }
996
997                   for (i = cur_reg + 1; i < reg; i++)
998                     {
999                       if (range & (1 << i))
1000                         as_tsktsk
1001                           (_("Warning: duplicated register (r%d) in register list"),
1002                            i);
1003                       else
1004                         range |= 1 << i;
1005                     }
1006                   in_range = 0;
1007                 }
1008
1009               if (range & (1 << reg))
1010                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1011                            reg);
1012               else if (reg <= cur_reg)
1013                 as_tsktsk (_("Warning: register range not in ascending order"));
1014
1015               range |= 1 << reg;
1016               cur_reg = reg;
1017             }
1018           while (skip_past_comma (&str) != FAIL
1019                  || (in_range = 1, *str++ == '-'));
1020           str--;
1021
1022           if (*str++ != '}')
1023             {
1024               inst.error = _("missing `}'");
1025               return FAIL;
1026             }
1027         }
1028       else
1029         {
1030           expressionS expr;
1031
1032           if (my_get_expression (&expr, &str, GE_NO_PREFIX))
1033             return FAIL;
1034
1035           if (expr.X_op == O_constant)
1036             {
1037               if (expr.X_add_number
1038                   != (expr.X_add_number & 0x0000ffff))
1039                 {
1040                   inst.error = _("invalid register mask");
1041                   return FAIL;
1042                 }
1043
1044               if ((range & expr.X_add_number) != 0)
1045                 {
1046                   int regno = range & expr.X_add_number;
1047
1048                   regno &= -regno;
1049                   regno = (1 << regno) - 1;
1050                   as_tsktsk
1051                     (_("Warning: duplicated register (r%d) in register list"),
1052                      regno);
1053                 }
1054
1055               range |= expr.X_add_number;
1056             }
1057           else
1058             {
1059               if (inst.reloc.type != 0)
1060                 {
1061                   inst.error = _("expression too complex");
1062                   return FAIL;
1063                 }
1064
1065               memcpy (&inst.reloc.exp, &expr, sizeof (expressionS));
1066               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1067               inst.reloc.pc_rel = 0;
1068             }
1069         }
1070
1071       if (*str == '|' || *str == '+')
1072         {
1073           str++;
1074           another_range = 1;
1075         }
1076     }
1077   while (another_range);
1078
1079   *strp = str;
1080   return range;
1081 }
1082
1083 /* Parse a VFP register list.  If the string is invalid return FAIL.
1084    Otherwise return the number of registers, and set PBASE to the first
1085    register.  Double precision registers are matched if DP is nonzero.  */
1086
1087 static int
1088 parse_vfp_reg_list (char **str, unsigned int *pbase, int dp)
1089 {
1090   int base_reg;
1091   int new_base;
1092   int regtype;
1093   int max_regs;
1094   int count = 0;
1095   int warned = 0;
1096   unsigned long mask = 0;
1097   int i;
1098
1099   if (**str != '{')
1100     return FAIL;
1101
1102   (*str)++;
1103
1104   if (dp)
1105     {
1106       regtype = REG_TYPE_VFD;
1107       max_regs = 16;
1108     }
1109   else
1110     {
1111       regtype = REG_TYPE_VFS;
1112       max_regs = 32;
1113     }
1114
1115   base_reg = max_regs;
1116
1117   do
1118     {
1119       new_base = arm_reg_parse (str, regtype);
1120       if (new_base == FAIL)
1121         {
1122           inst.error = gettext (reg_expected_msgs[regtype]);
1123           return FAIL;
1124         }
1125
1126       if (new_base < base_reg)
1127         base_reg = new_base;
1128
1129       if (mask & (1 << new_base))
1130         {
1131           inst.error = _("invalid register list");
1132           return FAIL;
1133         }
1134
1135       if ((mask >> new_base) != 0 && ! warned)
1136         {
1137           as_tsktsk (_("register list not in ascending order"));
1138           warned = 1;
1139         }
1140
1141       mask |= 1 << new_base;
1142       count++;
1143
1144       if (**str == '-') /* We have the start of a range expression */
1145         {
1146           int high_range;
1147
1148           (*str)++;
1149
1150           if ((high_range = arm_reg_parse (str, regtype)) == FAIL)
1151             {
1152               inst.error = gettext (reg_expected_msgs[regtype]);
1153               return FAIL;
1154             }
1155
1156           if (high_range <= new_base)
1157             {
1158               inst.error = _("register range not in ascending order");
1159               return FAIL;
1160             }
1161
1162           for (new_base++; new_base <= high_range; new_base++)
1163             {
1164               if (mask & (1 << new_base))
1165                 {
1166                   inst.error = _("invalid register list");
1167                   return FAIL;
1168                 }
1169
1170               mask |= 1 << new_base;
1171               count++;
1172             }
1173         }
1174     }
1175   while (skip_past_comma (str) != FAIL);
1176
1177   (*str)++;
1178
1179   /* Sanity check -- should have raised a parse error above.  */
1180   if (count == 0 || count > max_regs)
1181     abort ();
1182
1183   *pbase = base_reg;
1184
1185   /* Final test -- the registers must be consecutive.  */
1186   mask >>= base_reg;
1187   for (i = 0; i < count; i++)
1188     {
1189       if ((mask & (1u << i)) == 0)
1190         {
1191           inst.error = _("non-contiguous register range");
1192           return FAIL;
1193         }
1194     }
1195
1196   return count;
1197 }
1198
1199 /* Parse an explicit relocation suffix on an expression.  This is
1200    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
1201    arm_reloc_hsh contains no entries, so this function can only
1202    succeed if there is no () after the word.  Returns -1 on error,
1203    BFD_RELOC_UNUSED if there wasn't any suffix.  */
1204 static int
1205 parse_reloc (char **str)
1206 {
1207   struct reloc_entry *r;
1208   char *p, *q;
1209
1210   if (**str != '(')
1211     return BFD_RELOC_UNUSED;
1212
1213   p = *str + 1;
1214   q = p;
1215
1216   while (*q && *q != ')' && *q != ',')
1217     q++;
1218   if (*q != ')')
1219     return -1;
1220
1221   if ((r = hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
1222     return -1;
1223
1224   *str = q + 1;
1225   return r->reloc;
1226 }
1227
1228 /* Directives: register aliases.  */
1229
1230 static void
1231 insert_reg_alias (char *str, int number, int type)
1232 {
1233   struct reg_entry *new;
1234   const char *name;
1235
1236   if ((new = hash_find (arm_reg_hsh, str)) != 0)
1237     {
1238       if (new->builtin)
1239         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
1240
1241       /* Only warn about a redefinition if it's not defined as the
1242          same register.  */
1243       else if (new->number != number || new->type != type)
1244         as_warn (_("ignoring redefinition of register alias '%s'"), str);
1245
1246       return;
1247     }
1248
1249   name = xstrdup (str);
1250   new = xmalloc (sizeof (struct reg_entry));
1251
1252   new->name = name;
1253   new->number = number;
1254   new->type = type;
1255   new->builtin = FALSE;
1256
1257   if (hash_insert (arm_reg_hsh, name, (PTR) new))
1258     abort ();
1259 }
1260
1261 /* Look for the .req directive.  This is of the form:
1262
1263         new_register_name .req existing_register_name
1264
1265    If we find one, or if it looks sufficiently like one that we want to
1266    handle any error here, return non-zero.  Otherwise return zero.  */
1267
1268 static int
1269 create_register_alias (char * newname, char *p)
1270 {
1271   struct reg_entry *old;
1272   char *oldname, *nbuf;
1273   size_t nlen;
1274
1275   /* The input scrubber ensures that whitespace after the mnemonic is
1276      collapsed to single spaces.  */
1277   oldname = p;
1278   if (strncmp (oldname, " .req ", 6) != 0)
1279     return 0;
1280
1281   oldname += 6;
1282   if (*oldname == '\0')
1283     return 0;
1284
1285   old = hash_find (arm_reg_hsh, oldname);
1286   if (!old)
1287     {
1288       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
1289       return 1;
1290     }
1291
1292   /* If TC_CASE_SENSITIVE is defined, then newname already points to
1293      the desired alias name, and p points to its end.  If not, then
1294      the desired alias name is in the global original_case_string.  */
1295 #ifdef TC_CASE_SENSITIVE
1296   nlen = p - newname;
1297 #else
1298   newname = original_case_string;
1299   nlen = strlen (newname);
1300 #endif
1301
1302   nbuf = alloca (nlen + 1);
1303   memcpy (nbuf, newname, nlen);
1304   nbuf[nlen] = '\0';
1305
1306   /* Create aliases under the new name as stated; an all-lowercase
1307      version of the new name; and an all-uppercase version of the new
1308      name.  */
1309   insert_reg_alias (nbuf, old->number, old->type);
1310
1311   for (p = nbuf; *p; p++)
1312     *p = TOUPPER (*p);
1313
1314   if (strncmp (nbuf, newname, nlen))
1315     insert_reg_alias (nbuf, old->number, old->type);
1316
1317   for (p = nbuf; *p; p++)
1318     *p = TOLOWER (*p);
1319
1320   if (strncmp (nbuf, newname, nlen))
1321     insert_reg_alias (nbuf, old->number, old->type);
1322
1323   return 1;
1324 }
1325
1326 /* Should never be called, as .req goes between the alias and the
1327    register name, not at the beginning of the line.  */
1328 static void
1329 s_req (int a ATTRIBUTE_UNUSED)
1330 {
1331   as_bad (_("invalid syntax for .req directive"));
1332 }
1333
1334 /* The .unreq directive deletes an alias which was previously defined
1335    by .req.  For example:
1336
1337        my_alias .req r11
1338        .unreq my_alias    */
1339
1340 static void
1341 s_unreq (int a ATTRIBUTE_UNUSED)
1342 {
1343   char * name;
1344   char saved_char;
1345
1346   name = input_line_pointer;
1347
1348   while (*input_line_pointer != 0
1349          && *input_line_pointer != ' '
1350          && *input_line_pointer != '\n')
1351     ++input_line_pointer;
1352
1353   saved_char = *input_line_pointer;
1354   *input_line_pointer = 0;
1355
1356   if (!*name)
1357     as_bad (_("invalid syntax for .unreq directive"));
1358   else
1359     {
1360       struct reg_entry *reg = hash_find (arm_reg_hsh, name);
1361
1362       if (!reg)
1363         as_bad (_("unknown register alias '%s'"), name);
1364       else if (reg->builtin)
1365         as_warn (_("ignoring attempt to undefine built-in register '%s'"),
1366                  name);
1367       else
1368         {
1369           hash_delete (arm_reg_hsh, name);
1370           free ((char *) reg->name);
1371           free (reg);
1372         }
1373     }
1374
1375   *input_line_pointer = saved_char;
1376   demand_empty_rest_of_line ();
1377 }
1378
1379 /* Directives: Instruction set selection.  */
1380
1381 #ifdef OBJ_ELF
1382 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
1383    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
1384    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
1385    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
1386
1387 static enum mstate mapstate = MAP_UNDEFINED;
1388
1389 static void
1390 mapping_state (enum mstate state)
1391 {
1392   symbolS * symbolP;
1393   const char * symname;
1394   int type;
1395
1396   if (mapstate == state)
1397     /* The mapping symbol has already been emitted.
1398        There is nothing else to do.  */
1399     return;
1400
1401   mapstate = state;
1402
1403   switch (state)
1404     {
1405     case MAP_DATA:
1406       symname = "$d";
1407       type = BSF_NO_FLAGS;
1408       break;
1409     case MAP_ARM:
1410       symname = "$a";
1411       type = BSF_NO_FLAGS;
1412       break;
1413     case MAP_THUMB:
1414       symname = "$t";
1415       type = BSF_NO_FLAGS;
1416       break;
1417     case MAP_UNDEFINED:
1418       return;
1419     default:
1420       abort ();
1421     }
1422
1423   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
1424
1425   symbolP = symbol_new (symname, now_seg, (valueT) frag_now_fix (), frag_now);
1426   symbol_table_insert (symbolP);
1427   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
1428
1429   switch (state)
1430     {
1431     case MAP_ARM:
1432       THUMB_SET_FUNC (symbolP, 0);
1433       ARM_SET_THUMB (symbolP, 0);
1434       ARM_SET_INTERWORK (symbolP, support_interwork);
1435       break;
1436
1437     case MAP_THUMB:
1438       THUMB_SET_FUNC (symbolP, 1);
1439       ARM_SET_THUMB (symbolP, 1);
1440       ARM_SET_INTERWORK (symbolP, support_interwork);
1441       break;
1442
1443     case MAP_DATA:
1444     default:
1445       return;
1446     }
1447 }
1448 #else
1449 #define mapping_state(x) /* nothing */
1450 #endif
1451
1452 /* Find the real, Thumb encoded start of a Thumb function.  */
1453
1454 static symbolS *
1455 find_real_start (symbolS * symbolP)
1456 {
1457   char *       real_start;
1458   const char * name = S_GET_NAME (symbolP);
1459   symbolS *    new_target;
1460
1461   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
1462 #define STUB_NAME ".real_start_of"
1463
1464   if (name == NULL)
1465     abort ();
1466
1467   /* The compiler may generate BL instructions to local labels because
1468      it needs to perform a branch to a far away location. These labels
1469      do not have a corresponding ".real_start_of" label.  We check
1470      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
1471      the ".real_start_of" convention for nonlocal branches.  */
1472   if (S_IS_LOCAL (symbolP) || name[0] == '.')
1473     return symbolP;
1474
1475   real_start = ACONCAT ((STUB_NAME, name, NULL));
1476   new_target = symbol_find (real_start);
1477
1478   if (new_target == NULL)
1479     {
1480       as_warn ("Failed to find real start of function: %s\n", name);
1481       new_target = symbolP;
1482     }
1483
1484   return new_target;
1485 }
1486
1487 static void
1488 opcode_select (int width)
1489 {
1490   switch (width)
1491     {
1492     case 16:
1493       if (! thumb_mode)
1494         {
1495           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
1496             as_bad (_("selected processor does not support THUMB opcodes"));
1497
1498           thumb_mode = 1;
1499           /* No need to force the alignment, since we will have been
1500              coming from ARM mode, which is word-aligned.  */
1501           record_alignment (now_seg, 1);
1502         }
1503       mapping_state (MAP_THUMB);
1504       break;
1505
1506     case 32:
1507       if (thumb_mode)
1508         {
1509           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
1510             as_bad (_("selected processor does not support ARM opcodes"));
1511
1512           thumb_mode = 0;
1513
1514           if (!need_pass_2)
1515             frag_align (2, 0, 0);
1516
1517           record_alignment (now_seg, 1);
1518         }
1519       mapping_state (MAP_ARM);
1520       break;
1521
1522     default:
1523       as_bad (_("invalid instruction size selected (%d)"), width);
1524     }
1525 }
1526
1527 static void
1528 s_arm (int ignore ATTRIBUTE_UNUSED)
1529 {
1530   opcode_select (32);
1531   demand_empty_rest_of_line ();
1532 }
1533
1534 static void
1535 s_thumb (int ignore ATTRIBUTE_UNUSED)
1536 {
1537   opcode_select (16);
1538   demand_empty_rest_of_line ();
1539 }
1540
1541 static void
1542 s_code (int unused ATTRIBUTE_UNUSED)
1543 {
1544   int temp;
1545
1546   temp = get_absolute_expression ();
1547   switch (temp)
1548     {
1549     case 16:
1550     case 32:
1551       opcode_select (temp);
1552       break;
1553
1554     default:
1555       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
1556     }
1557 }
1558
1559 static void
1560 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
1561 {
1562   /* If we are not already in thumb mode go into it, EVEN if
1563      the target processor does not support thumb instructions.
1564      This is used by gcc/config/arm/lib1funcs.asm for example
1565      to compile interworking support functions even if the
1566      target processor should not support interworking.  */
1567   if (! thumb_mode)
1568     {
1569       thumb_mode = 2;
1570       record_alignment (now_seg, 1);
1571     }
1572
1573   demand_empty_rest_of_line ();
1574 }
1575
1576 static void
1577 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
1578 {
1579   s_thumb (0);
1580
1581   /* The following label is the name/address of the start of a Thumb function.
1582      We need to know this for the interworking support.  */
1583   label_is_thumb_function_name = TRUE;
1584 }
1585
1586 /* Perform a .set directive, but also mark the alias as
1587    being a thumb function.  */
1588
1589 static void
1590 s_thumb_set (int equiv)
1591 {
1592   /* XXX the following is a duplicate of the code for s_set() in read.c
1593      We cannot just call that code as we need to get at the symbol that
1594      is created.  */
1595   char *    name;
1596   char      delim;
1597   char *    end_name;
1598   symbolS * symbolP;
1599
1600   /* Especial apologies for the random logic:
1601      This just grew, and could be parsed much more simply!
1602      Dean - in haste.  */
1603   name      = input_line_pointer;
1604   delim     = get_symbol_end ();
1605   end_name  = input_line_pointer;
1606   *end_name = delim;
1607
1608   if (*input_line_pointer != ',')
1609     {
1610       *end_name = 0;
1611       as_bad (_("expected comma after name \"%s\""), name);
1612       *end_name = delim;
1613       ignore_rest_of_line ();
1614       return;
1615     }
1616
1617   input_line_pointer++;
1618   *end_name = 0;
1619
1620   if (name[0] == '.' && name[1] == '\0')
1621     {
1622       /* XXX - this should not happen to .thumb_set.  */
1623       abort ();
1624     }
1625
1626   if ((symbolP = symbol_find (name)) == NULL
1627       && (symbolP = md_undefined_symbol (name)) == NULL)
1628     {
1629 #ifndef NO_LISTING
1630       /* When doing symbol listings, play games with dummy fragments living
1631          outside the normal fragment chain to record the file and line info
1632          for this symbol.  */
1633       if (listing & LISTING_SYMBOLS)
1634         {
1635           extern struct list_info_struct * listing_tail;
1636           fragS * dummy_frag = xmalloc (sizeof (fragS));
1637
1638           memset (dummy_frag, 0, sizeof (fragS));
1639           dummy_frag->fr_type = rs_fill;
1640           dummy_frag->line = listing_tail;
1641           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
1642           dummy_frag->fr_symbol = symbolP;
1643         }
1644       else
1645 #endif
1646         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
1647
1648 #ifdef OBJ_COFF
1649       /* "set" symbols are local unless otherwise specified.  */
1650       SF_SET_LOCAL (symbolP);
1651 #endif /* OBJ_COFF  */
1652     }                           /* Make a new symbol.  */
1653
1654   symbol_table_insert (symbolP);
1655
1656   * end_name = delim;
1657
1658   if (equiv
1659       && S_IS_DEFINED (symbolP)
1660       && S_GET_SEGMENT (symbolP) != reg_section)
1661     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
1662
1663   pseudo_set (symbolP);
1664
1665   demand_empty_rest_of_line ();
1666
1667   /* XXX Now we come to the Thumb specific bit of code.  */
1668
1669   THUMB_SET_FUNC (symbolP, 1);
1670   ARM_SET_THUMB (symbolP, 1);
1671 #if defined OBJ_ELF || defined OBJ_COFF
1672   ARM_SET_INTERWORK (symbolP, support_interwork);
1673 #endif
1674 }
1675
1676 /* Directives: Mode selection.  */
1677
1678 /* .syntax [unified|divided] - choose the new unified syntax
1679    (same for Arm and Thumb encoding, modulo slight differences in what
1680    can be represented) or the old divergent syntax for each mode.  */
1681 static void
1682 s_syntax (int unused ATTRIBUTE_UNUSED)
1683 {
1684   char *name, delim;
1685
1686   name = input_line_pointer;
1687   delim = get_symbol_end ();
1688
1689   if (!strcasecmp (name, "unified"))
1690     unified_syntax = TRUE;
1691   else if (!strcasecmp (name, "divided"))
1692     unified_syntax = FALSE;
1693   else
1694     {
1695       as_bad (_("unrecognized syntax mode \"%s\""), name);
1696       return;
1697     }
1698   *input_line_pointer = delim;
1699   demand_empty_rest_of_line ();
1700 }
1701
1702 /* Directives: sectioning and alignment.  */
1703
1704 /* Same as s_align_ptwo but align 0 => align 2.  */
1705
1706 static void
1707 s_align (int unused ATTRIBUTE_UNUSED)
1708 {
1709   int temp;
1710   long temp_fill;
1711   long max_alignment = 15;
1712
1713   temp = get_absolute_expression ();
1714   if (temp > max_alignment)
1715     as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
1716   else if (temp < 0)
1717     {
1718       as_bad (_("alignment negative. 0 assumed."));
1719       temp = 0;
1720     }
1721
1722   if (*input_line_pointer == ',')
1723     {
1724       input_line_pointer++;
1725       temp_fill = get_absolute_expression ();
1726     }
1727   else
1728     temp_fill = 0;
1729
1730   if (!temp)
1731     temp = 2;
1732
1733   /* Only make a frag if we HAVE to.  */
1734   if (temp && !need_pass_2)
1735     frag_align (temp, (int) temp_fill, 0);
1736   demand_empty_rest_of_line ();
1737
1738   record_alignment (now_seg, temp);
1739 }
1740
1741 static void
1742 s_bss (int ignore ATTRIBUTE_UNUSED)
1743 {
1744   /* We don't support putting frags in the BSS segment, we fake it by
1745      marking in_bss, then looking at s_skip for clues.  */
1746   subseg_set (bss_section, 0);
1747   demand_empty_rest_of_line ();
1748   mapping_state (MAP_DATA);
1749 }
1750
1751 static void
1752 s_even (int ignore ATTRIBUTE_UNUSED)
1753 {
1754   /* Never make frag if expect extra pass.  */
1755   if (!need_pass_2)
1756     frag_align (1, 0, 0);
1757
1758   record_alignment (now_seg, 1);
1759
1760   demand_empty_rest_of_line ();
1761 }
1762
1763 /* Directives: Literal pools.  */
1764
1765 static literal_pool *
1766 find_literal_pool (void)
1767 {
1768   literal_pool * pool;
1769
1770   for (pool = list_of_pools; pool != NULL; pool = pool->next)
1771     {
1772       if (pool->section == now_seg
1773           && pool->sub_section == now_subseg)
1774         break;
1775     }
1776
1777   return pool;
1778 }
1779
1780 static literal_pool *
1781 find_or_make_literal_pool (void)
1782 {
1783   /* Next literal pool ID number.  */
1784   static unsigned int latest_pool_num = 1;
1785   literal_pool *      pool;
1786
1787   pool = find_literal_pool ();
1788
1789   if (pool == NULL)
1790     {
1791       /* Create a new pool.  */
1792       pool = xmalloc (sizeof (* pool));
1793       if (! pool)
1794         return NULL;
1795
1796       pool->next_free_entry = 0;
1797       pool->section         = now_seg;
1798       pool->sub_section     = now_subseg;
1799       pool->next            = list_of_pools;
1800       pool->symbol          = NULL;
1801
1802       /* Add it to the list.  */
1803       list_of_pools = pool;
1804     }
1805
1806   /* New pools, and emptied pools, will have a NULL symbol.  */
1807   if (pool->symbol == NULL)
1808     {
1809       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
1810                                     (valueT) 0, &zero_address_frag);
1811       pool->id = latest_pool_num ++;
1812     }
1813
1814   /* Done.  */
1815   return pool;
1816 }
1817
1818 /* Add the literal in the global 'inst'
1819    structure to the relevent literal pool.  */
1820
1821 static int
1822 add_to_lit_pool (void)
1823 {
1824   literal_pool * pool;
1825   unsigned int entry;
1826
1827   pool = find_or_make_literal_pool ();
1828
1829   /* Check if this literal value is already in the pool.  */
1830   for (entry = 0; entry < pool->next_free_entry; entry ++)
1831     {
1832       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
1833           && (inst.reloc.exp.X_op == O_constant)
1834           && (pool->literals[entry].X_add_number
1835               == inst.reloc.exp.X_add_number)
1836           && (pool->literals[entry].X_unsigned
1837               == inst.reloc.exp.X_unsigned))
1838         break;
1839
1840       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
1841           && (inst.reloc.exp.X_op == O_symbol)
1842           && (pool->literals[entry].X_add_number
1843               == inst.reloc.exp.X_add_number)
1844           && (pool->literals[entry].X_add_symbol
1845               == inst.reloc.exp.X_add_symbol)
1846           && (pool->literals[entry].X_op_symbol
1847               == inst.reloc.exp.X_op_symbol))
1848         break;
1849     }
1850
1851   /* Do we need to create a new entry?  */
1852   if (entry == pool->next_free_entry)
1853     {
1854       if (entry >= MAX_LITERAL_POOL_SIZE)
1855         {
1856           inst.error = _("literal pool overflow");
1857           return FAIL;
1858         }
1859
1860       pool->literals[entry] = inst.reloc.exp;
1861       pool->next_free_entry += 1;
1862     }
1863
1864   inst.reloc.exp.X_op         = O_symbol;
1865   inst.reloc.exp.X_add_number = ((int) entry) * 4;
1866   inst.reloc.exp.X_add_symbol = pool->symbol;
1867
1868   return SUCCESS;
1869 }
1870
1871 /* Can't use symbol_new here, so have to create a symbol and then at
1872    a later date assign it a value. Thats what these functions do.  */
1873
1874 static void
1875 symbol_locate (symbolS *    symbolP,
1876                const char * name,       /* It is copied, the caller can modify.  */
1877                segT         segment,    /* Segment identifier (SEG_<something>).  */
1878                valueT       valu,       /* Symbol value.  */
1879                fragS *      frag)       /* Associated fragment.  */
1880 {
1881   unsigned int name_length;
1882   char * preserved_copy_of_name;
1883
1884   name_length = strlen (name) + 1;   /* +1 for \0.  */
1885   obstack_grow (&notes, name, name_length);
1886   preserved_copy_of_name = obstack_finish (&notes);
1887
1888 #ifdef tc_canonicalize_symbol_name
1889   preserved_copy_of_name =
1890     tc_canonicalize_symbol_name (preserved_copy_of_name);
1891 #endif
1892
1893   S_SET_NAME (symbolP, preserved_copy_of_name);
1894
1895   S_SET_SEGMENT (symbolP, segment);
1896   S_SET_VALUE (symbolP, valu);
1897   symbol_clear_list_pointers (symbolP);
1898
1899   symbol_set_frag (symbolP, frag);
1900
1901   /* Link to end of symbol chain.  */
1902   {
1903     extern int symbol_table_frozen;
1904
1905     if (symbol_table_frozen)
1906       abort ();
1907   }
1908
1909   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
1910
1911   obj_symbol_new_hook (symbolP);
1912
1913 #ifdef tc_symbol_new_hook
1914   tc_symbol_new_hook (symbolP);
1915 #endif
1916
1917 #ifdef DEBUG_SYMS
1918   verify_symbol_chain (symbol_rootP, symbol_lastP);
1919 #endif /* DEBUG_SYMS  */
1920 }
1921
1922
1923 static void
1924 s_ltorg (int ignored ATTRIBUTE_UNUSED)
1925 {
1926   unsigned int entry;
1927   literal_pool * pool;
1928   char sym_name[20];
1929
1930   pool = find_literal_pool ();
1931   if (pool == NULL
1932       || pool->symbol == NULL
1933       || pool->next_free_entry == 0)
1934     return;
1935
1936   mapping_state (MAP_DATA);
1937
1938   /* Align pool as you have word accesses.
1939      Only make a frag if we have to.  */
1940   if (!need_pass_2)
1941     frag_align (2, 0, 0);
1942
1943   record_alignment (now_seg, 2);
1944
1945   sprintf (sym_name, "$$lit_\002%x", pool->id);
1946
1947   symbol_locate (pool->symbol, sym_name, now_seg,
1948                  (valueT) frag_now_fix (), frag_now);
1949   symbol_table_insert (pool->symbol);
1950
1951   ARM_SET_THUMB (pool->symbol, thumb_mode);
1952
1953 #if defined OBJ_COFF || defined OBJ_ELF
1954   ARM_SET_INTERWORK (pool->symbol, support_interwork);
1955 #endif
1956
1957   for (entry = 0; entry < pool->next_free_entry; entry ++)
1958     /* First output the expression in the instruction to the pool.  */
1959     emit_expr (&(pool->literals[entry]), 4); /* .word  */
1960
1961   /* Mark the pool as empty.  */
1962   pool->next_free_entry = 0;
1963   pool->symbol = NULL;
1964 }
1965
1966 #ifdef OBJ_ELF
1967 /* Forward declarations for functions below, in the MD interface
1968    section.  */
1969 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
1970 static valueT create_unwind_entry (int);
1971 static void start_unwind_section (const segT, int);
1972 static void add_unwind_opcode (valueT, int);
1973 static void flush_pending_unwind (void);
1974
1975 /* Directives: Data.  */
1976
1977 static void
1978 s_arm_elf_cons (int nbytes)
1979 {
1980   expressionS exp;
1981
1982 #ifdef md_flush_pending_output
1983   md_flush_pending_output ();
1984 #endif
1985
1986   if (is_it_end_of_statement ())
1987     {
1988       demand_empty_rest_of_line ();
1989       return;
1990     }
1991
1992 #ifdef md_cons_align
1993   md_cons_align (nbytes);
1994 #endif
1995
1996   mapping_state (MAP_DATA);
1997   do
1998     {
1999       int reloc;
2000       char *base = input_line_pointer;
2001
2002       expression (& exp);
2003
2004       if (exp.X_op != O_symbol)
2005         emit_expr (&exp, (unsigned int) nbytes);
2006       else
2007         {
2008           char *before_reloc = input_line_pointer;
2009           reloc = parse_reloc (&input_line_pointer);
2010           if (reloc == -1)
2011             {
2012               as_bad (_("unrecognized relocation suffix"));
2013               ignore_rest_of_line ();
2014               return;
2015             }
2016           else if (reloc == BFD_RELOC_UNUSED)
2017             emit_expr (&exp, (unsigned int) nbytes);
2018           else
2019             {
2020               reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, reloc);
2021               int size = bfd_get_reloc_size (howto);
2022
2023               if (reloc == BFD_RELOC_ARM_PLT32)
2024                 {
2025                   as_bad (_("(plt) is only valid on branch targets"));
2026                   reloc = BFD_RELOC_UNUSED;
2027                   size = 0;
2028                 }
2029
2030               if (size > nbytes)
2031                 as_bad (_("%s relocations do not fit in %d bytes"),
2032                         howto->name, nbytes);
2033               else
2034                 {
2035                   /* We've parsed an expression stopping at O_symbol.
2036                      But there may be more expression left now that we
2037                      have parsed the relocation marker.  Parse it again.
2038                      XXX Surely there is a cleaner way to do this.  */
2039                   char *p = input_line_pointer;
2040                   int offset;
2041                   char *save_buf = alloca (input_line_pointer - base);
2042                   memcpy (save_buf, base, input_line_pointer - base);
2043                   memmove (base + (input_line_pointer - before_reloc),
2044                            base, before_reloc - base);
2045
2046                   input_line_pointer = base + (input_line_pointer-before_reloc);
2047                   expression (&exp);
2048                   memcpy (base, save_buf, p - base);
2049
2050                   offset = nbytes - size;
2051                   p = frag_more ((int) nbytes);
2052                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
2053                                size, &exp, 0, reloc);
2054                 }
2055             }
2056         }
2057     }
2058   while (*input_line_pointer++ == ',');
2059
2060   /* Put terminator back into stream.  */
2061   input_line_pointer --;
2062   demand_empty_rest_of_line ();
2063 }
2064
2065
2066 /* Parse a .rel31 directive.  */
2067
2068 static void
2069 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
2070 {
2071   expressionS exp;
2072   char *p;
2073   valueT highbit;
2074
2075   highbit = 0;
2076   if (*input_line_pointer == '1')
2077     highbit = 0x80000000;
2078   else if (*input_line_pointer != '0')
2079     as_bad (_("expected 0 or 1"));
2080
2081   input_line_pointer++;
2082   if (*input_line_pointer != ',')
2083     as_bad (_("missing comma"));
2084   input_line_pointer++;
2085
2086 #ifdef md_flush_pending_output
2087   md_flush_pending_output ();
2088 #endif
2089
2090 #ifdef md_cons_align
2091   md_cons_align (4);
2092 #endif
2093
2094   mapping_state (MAP_DATA);
2095
2096   expression (&exp);
2097
2098   p = frag_more (4);
2099   md_number_to_chars (p, highbit, 4);
2100   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
2101                BFD_RELOC_ARM_PREL31);
2102
2103   demand_empty_rest_of_line ();
2104 }
2105
2106 /* Directives: AEABI stack-unwind tables.  */
2107
2108 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
2109
2110 static void
2111 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
2112 {
2113   demand_empty_rest_of_line ();
2114   /* Mark the start of the function.  */
2115   unwind.proc_start = expr_build_dot ();
2116
2117   /* Reset the rest of the unwind info.  */
2118   unwind.opcode_count = 0;
2119   unwind.table_entry = NULL;
2120   unwind.personality_routine = NULL;
2121   unwind.personality_index = -1;
2122   unwind.frame_size = 0;
2123   unwind.fp_offset = 0;
2124   unwind.fp_reg = 13;
2125   unwind.fp_used = 0;
2126   unwind.sp_restored = 0;
2127 }
2128
2129
2130 /* Parse a handlerdata directive.  Creates the exception handling table entry
2131    for the function.  */
2132
2133 static void
2134 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
2135 {
2136   demand_empty_rest_of_line ();
2137   if (unwind.table_entry)
2138     as_bad (_("dupicate .handlerdata directive"));
2139
2140   create_unwind_entry (1);
2141 }
2142
2143 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
2144
2145 static void
2146 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
2147 {
2148   long where;
2149   char *ptr;
2150   valueT val;
2151
2152   demand_empty_rest_of_line ();
2153
2154   /* Add eh table entry.  */
2155   if (unwind.table_entry == NULL)
2156     val = create_unwind_entry (0);
2157   else
2158     val = 0;
2159
2160   /* Add index table entry.  This is two words.  */
2161   start_unwind_section (unwind.saved_seg, 1);
2162   frag_align (2, 0, 0);
2163   record_alignment (now_seg, 2);
2164
2165   ptr = frag_more (8);
2166   where = frag_now_fix () - 8;
2167
2168   /* Self relative offset of the function start.  */
2169   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
2170            BFD_RELOC_ARM_PREL31);
2171
2172   /* Indicate dependency on EHABI-defined personality routines to the
2173      linker, if it hasn't been done already.  */
2174   if (unwind.personality_index >= 0 && unwind.personality_index < 3
2175       && !(marked_pr_dependency & (1 << unwind.personality_index)))
2176     {
2177       static const char *const name[] = {
2178         "__aeabi_unwind_cpp_pr0",
2179         "__aeabi_unwind_cpp_pr1",
2180         "__aeabi_unwind_cpp_pr2"
2181       };
2182       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
2183       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
2184       marked_pr_dependency |= 1 << unwind.personality_index;
2185       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
2186         = marked_pr_dependency;
2187     }
2188
2189   if (val)
2190     /* Inline exception table entry.  */
2191     md_number_to_chars (ptr + 4, val, 4);
2192   else
2193     /* Self relative offset of the table entry.  */
2194     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
2195              BFD_RELOC_ARM_PREL31);
2196
2197   /* Restore the original section.  */
2198   subseg_set (unwind.saved_seg, unwind.saved_subseg);
2199 }
2200
2201
2202 /* Parse an unwind_cantunwind directive.  */
2203
2204 static void
2205 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
2206 {
2207   demand_empty_rest_of_line ();
2208   if (unwind.personality_routine || unwind.personality_index != -1)
2209     as_bad (_("personality routine specified for cantunwind frame"));
2210
2211   unwind.personality_index = -2;
2212 }
2213
2214
2215 /* Parse a personalityindex directive.  */
2216
2217 static void
2218 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
2219 {
2220   expressionS exp;
2221
2222   if (unwind.personality_routine || unwind.personality_index != -1)
2223     as_bad (_("duplicate .personalityindex directive"));
2224
2225   expression (&exp);
2226
2227   if (exp.X_op != O_constant
2228       || exp.X_add_number < 0 || exp.X_add_number > 15)
2229     {
2230       as_bad (_("bad personality routine number"));
2231       ignore_rest_of_line ();
2232       return;
2233     }
2234
2235   unwind.personality_index = exp.X_add_number;
2236
2237   demand_empty_rest_of_line ();
2238 }
2239
2240
2241 /* Parse a personality directive.  */
2242
2243 static void
2244 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
2245 {
2246   char *name, *p, c;
2247
2248   if (unwind.personality_routine || unwind.personality_index != -1)
2249     as_bad (_("duplicate .personality directive"));
2250
2251   name = input_line_pointer;
2252   c = get_symbol_end ();
2253   p = input_line_pointer;
2254   unwind.personality_routine = symbol_find_or_make (name);
2255   *p = c;
2256   demand_empty_rest_of_line ();
2257 }
2258
2259
2260 /* Parse a directive saving core registers.  */
2261
2262 static void
2263 s_arm_unwind_save_core (void)
2264 {
2265   valueT op;
2266   long range;
2267   int n;
2268
2269   range = parse_reg_list (&input_line_pointer);
2270   if (range == FAIL)
2271     {
2272       as_bad (_("expected register list"));
2273       ignore_rest_of_line ();
2274       return;
2275     }
2276
2277   demand_empty_rest_of_line ();
2278
2279   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
2280      into .unwind_save {..., sp...}.  We aren't bothered about the value of
2281      ip because it is clobbered by calls.  */
2282   if (unwind.sp_restored && unwind.fp_reg == 12
2283       && (range & 0x3000) == 0x1000)
2284     {
2285       unwind.opcode_count--;
2286       unwind.sp_restored = 0;
2287       range = (range | 0x2000) & ~0x1000;
2288       unwind.pending_offset = 0;
2289     }
2290
2291   /* Pop r4-r15.  */
2292   if (range & 0xfff0)
2293     {
2294       /* See if we can use the short opcodes.  These pop a block of up to 8
2295          registers starting with r4, plus maybe r14.  */
2296       for (n = 0; n < 8; n++)
2297         {
2298           /* Break at the first non-saved register.      */
2299           if ((range & (1 << (n + 4))) == 0)
2300             break;
2301         }
2302       /* See if there are any other bits set.  */
2303       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
2304         {
2305           /* Use the long form.  */
2306           op = 0x8000 | ((range >> 4) & 0xfff);
2307           add_unwind_opcode (op, 2);
2308         }
2309       else
2310         {
2311           /* Use the short form.  */
2312           if (range & 0x4000)
2313             op = 0xa8; /* Pop r14.      */
2314           else
2315             op = 0xa0; /* Do not pop r14.  */
2316           op |= (n - 1);
2317           add_unwind_opcode (op, 1);
2318         }
2319     }
2320
2321   /* Pop r0-r3.  */
2322   if (range & 0xf)
2323     {
2324       op = 0xb100 | (range & 0xf);
2325       add_unwind_opcode (op, 2);
2326     }
2327
2328   /* Record the number of bytes pushed.  */
2329   for (n = 0; n < 16; n++)
2330     {
2331       if (range & (1 << n))
2332         unwind.frame_size += 4;
2333     }
2334 }
2335
2336
2337 /* Parse a directive saving FPA registers.  */
2338
2339 static void
2340 s_arm_unwind_save_fpa (int reg)
2341 {
2342   expressionS exp;
2343   int num_regs;
2344   valueT op;
2345
2346   /* Get Number of registers to transfer.  */
2347   if (skip_past_comma (&input_line_pointer) != FAIL)
2348     expression (&exp);
2349   else
2350     exp.X_op = O_illegal;
2351
2352   if (exp.X_op != O_constant)
2353     {
2354       as_bad (_("expected , <constant>"));
2355       ignore_rest_of_line ();
2356       return;
2357     }
2358
2359   num_regs = exp.X_add_number;
2360
2361   if (num_regs < 1 || num_regs > 4)
2362     {
2363       as_bad (_("number of registers must be in the range [1:4]"));
2364       ignore_rest_of_line ();
2365       return;
2366     }
2367
2368   demand_empty_rest_of_line ();
2369
2370   if (reg == 4)
2371     {
2372       /* Short form.  */
2373       op = 0xb4 | (num_regs - 1);
2374       add_unwind_opcode (op, 1);
2375     }
2376   else
2377     {
2378       /* Long form.  */
2379       op = 0xc800 | (reg << 4) | (num_regs - 1);
2380       add_unwind_opcode (op, 2);
2381     }
2382   unwind.frame_size += num_regs * 12;
2383 }
2384
2385
2386 /* Parse a directive saving VFP registers.  */
2387
2388 static void
2389 s_arm_unwind_save_vfp (void)
2390 {
2391   int count;
2392   unsigned int reg;
2393   valueT op;
2394
2395   count = parse_vfp_reg_list (&input_line_pointer, &reg, 1);
2396   if (count == FAIL)
2397     {
2398       as_bad (_("expected register list"));
2399       ignore_rest_of_line ();
2400       return;
2401     }
2402
2403   demand_empty_rest_of_line ();
2404
2405   if (reg == 8)
2406     {
2407       /* Short form.  */
2408       op = 0xb8 | (count - 1);
2409       add_unwind_opcode (op, 1);
2410     }
2411   else
2412     {
2413       /* Long form.  */
2414       op = 0xb300 | (reg << 4) | (count - 1);
2415       add_unwind_opcode (op, 2);
2416     }
2417   unwind.frame_size += count * 8 + 4;
2418 }
2419
2420
2421 /* Parse a directive saving iWMMXt data registers.  */
2422
2423 static void
2424 s_arm_unwind_save_mmxwr (void)
2425 {
2426   int reg;
2427   int hi_reg;
2428   int i;
2429   unsigned mask = 0;
2430   valueT op;
2431
2432   if (*input_line_pointer == '{')
2433     input_line_pointer++;
2434
2435   do
2436     {
2437       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
2438
2439       if (reg == FAIL)
2440         {
2441           as_bad (_(reg_expected_msgs[REG_TYPE_MMXWR]));
2442           goto error;
2443         }
2444
2445       if (mask >> reg)
2446         as_tsktsk (_("register list not in ascending order"));
2447       mask |= 1 << reg;
2448
2449       if (*input_line_pointer == '-')
2450         {
2451           input_line_pointer++;
2452           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
2453           if (hi_reg == FAIL)
2454             {
2455               as_bad (_(reg_expected_msgs[REG_TYPE_MMXWR]));
2456               goto error;
2457             }
2458           else if (reg >= hi_reg)
2459             {
2460               as_bad (_("bad register range"));
2461               goto error;
2462             }
2463           for (; reg < hi_reg; reg++)
2464             mask |= 1 << reg;
2465         }
2466     }
2467   while (skip_past_comma (&input_line_pointer) != FAIL);
2468
2469   if (*input_line_pointer == '}')
2470     input_line_pointer++;
2471
2472   demand_empty_rest_of_line ();
2473
2474   /* Generate any deferred opcodes becuuse we're going to be looking at
2475      the list.  */
2476   flush_pending_unwind ();
2477
2478   for (i = 0; i < 16; i++)
2479     {
2480       if (mask & (1 << i))
2481         unwind.frame_size += 8;
2482     }
2483
2484   /* Attempt to combine with a previous opcode.  We do this because gcc
2485      likes to output separate unwind directives for a single block of
2486      registers.  */
2487   if (unwind.opcode_count > 0)
2488     {
2489       i = unwind.opcodes[unwind.opcode_count - 1];
2490       if ((i & 0xf8) == 0xc0)
2491         {
2492           i &= 7;
2493           /* Only merge if the blocks are contiguous.  */
2494           if (i < 6)
2495             {
2496               if ((mask & 0xfe00) == (1 << 9))
2497                 {
2498                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
2499                   unwind.opcode_count--;
2500                 }
2501             }
2502           else if (i == 6 && unwind.opcode_count >= 2)
2503             {
2504               i = unwind.opcodes[unwind.opcode_count - 2];
2505               reg = i >> 4;
2506               i &= 0xf;
2507
2508               op = 0xffff << (reg - 1);
2509               if (reg > 0
2510                   || ((mask & op) == (1u << (reg - 1))))
2511                 {
2512                   op = (1 << (reg + i + 1)) - 1;
2513                   op &= ~((1 << reg) - 1);
2514                   mask |= op;
2515                   unwind.opcode_count -= 2;
2516                 }
2517             }
2518         }
2519     }
2520
2521   hi_reg = 15;
2522   /* We want to generate opcodes in the order the registers have been
2523      saved, ie. descending order.  */
2524   for (reg = 15; reg >= -1; reg--)
2525     {
2526       /* Save registers in blocks.  */
2527       if (reg < 0
2528           || !(mask & (1 << reg)))
2529         {
2530           /* We found an unsaved reg.  Generate opcodes to save the
2531              preceeding block.  */
2532           if (reg != hi_reg)
2533             {
2534               if (reg == 9)
2535                 {
2536                   /* Short form.  */
2537                   op = 0xc0 | (hi_reg - 10);
2538                   add_unwind_opcode (op, 1);
2539                 }
2540               else
2541                 {
2542                   /* Long form.  */
2543                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
2544                   add_unwind_opcode (op, 2);
2545                 }
2546             }
2547           hi_reg = reg - 1;
2548         }
2549     }
2550
2551   return;
2552 error:
2553   ignore_rest_of_line ();
2554 }
2555
2556 static void
2557 s_arm_unwind_save_mmxwcg (void)
2558 {
2559   int reg;
2560   int hi_reg;
2561   unsigned mask = 0;
2562   valueT op;
2563
2564   if (*input_line_pointer == '{')
2565     input_line_pointer++;
2566
2567   do
2568     {
2569       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
2570
2571       if (reg == FAIL)
2572         {
2573           as_bad (_(reg_expected_msgs[REG_TYPE_MMXWCG]));
2574           goto error;
2575         }
2576
2577       reg -= 8;
2578       if (mask >> reg)
2579         as_tsktsk (_("register list not in ascending order"));
2580       mask |= 1 << reg;
2581
2582       if (*input_line_pointer == '-')
2583         {
2584           input_line_pointer++;
2585           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
2586           if (hi_reg == FAIL)
2587             {
2588               as_bad (_(reg_expected_msgs[REG_TYPE_MMXWCG]));
2589               goto error;
2590             }
2591           else if (reg >= hi_reg)
2592             {
2593               as_bad (_("bad register range"));
2594               goto error;
2595             }
2596           for (; reg < hi_reg; reg++)
2597             mask |= 1 << reg;
2598         }
2599     }
2600   while (skip_past_comma (&input_line_pointer) != FAIL);
2601
2602   if (*input_line_pointer == '}')
2603     input_line_pointer++;
2604
2605   demand_empty_rest_of_line ();
2606
2607   /* Generate any deferred opcodes becuuse we're going to be looking at
2608      the list.  */
2609   flush_pending_unwind ();
2610
2611   for (reg = 0; reg < 16; reg++)
2612     {
2613       if (mask & (1 << reg))
2614         unwind.frame_size += 4;
2615     }
2616   op = 0xc700 | mask;
2617   add_unwind_opcode (op, 2);
2618   return;
2619 error:
2620   ignore_rest_of_line ();
2621 }
2622
2623
2624 /* Parse an unwind_save directive.  */
2625
2626 static void
2627 s_arm_unwind_save (int ignored ATTRIBUTE_UNUSED)
2628 {
2629   char *peek;
2630   struct reg_entry *reg;
2631   bfd_boolean had_brace = FALSE;
2632
2633   /* Figure out what sort of save we have.  */
2634   peek = input_line_pointer;
2635
2636   if (*peek == '{')
2637     {
2638       had_brace = TRUE;
2639       peek++;
2640     }
2641
2642   reg = arm_reg_parse_multi (&peek);
2643
2644   if (!reg)
2645     {
2646       as_bad (_("register expected"));
2647       ignore_rest_of_line ();
2648       return;
2649     }
2650
2651   switch (reg->type)
2652     {
2653     case REG_TYPE_FN:
2654       if (had_brace)
2655         {
2656           as_bad (_("FPA .unwind_save does not take a register list"));
2657           ignore_rest_of_line ();
2658           return;
2659         }
2660       s_arm_unwind_save_fpa (reg->number);
2661       return;
2662
2663     case REG_TYPE_RN:     s_arm_unwind_save_core ();   return;
2664     case REG_TYPE_VFD:     s_arm_unwind_save_vfp ();    return;
2665     case REG_TYPE_MMXWR:  s_arm_unwind_save_mmxwr ();  return;
2666     case REG_TYPE_MMXWCG: s_arm_unwind_save_mmxwcg (); return;
2667
2668     default:
2669       as_bad (_(".unwind_save does not support this kind of register"));
2670       ignore_rest_of_line ();
2671     }
2672 }
2673
2674
2675 /* Parse an unwind_movsp directive.  */
2676
2677 static void
2678 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
2679 {
2680   int reg;
2681   valueT op;
2682
2683   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
2684   if (reg == FAIL)
2685     {
2686       as_bad (_(reg_expected_msgs[REG_TYPE_RN]));
2687       ignore_rest_of_line ();
2688       return;
2689     }
2690   demand_empty_rest_of_line ();
2691
2692   if (reg == REG_SP || reg == REG_PC)
2693     {
2694       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
2695       return;
2696     }
2697
2698   if (unwind.fp_reg != REG_SP)
2699     as_bad (_("unexpected .unwind_movsp directive"));
2700
2701   /* Generate opcode to restore the value.  */
2702   op = 0x90 | reg;
2703   add_unwind_opcode (op, 1);
2704
2705   /* Record the information for later.  */
2706   unwind.fp_reg = reg;
2707   unwind.fp_offset = unwind.frame_size;
2708   unwind.sp_restored = 1;
2709 }
2710
2711 /* Parse an unwind_pad directive.  */
2712
2713 static void
2714 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
2715 {
2716   int offset;
2717
2718   if (immediate_for_directive (&offset) == FAIL)
2719     return;
2720
2721   if (offset & 3)
2722     {
2723       as_bad (_("stack increment must be multiple of 4"));
2724       ignore_rest_of_line ();
2725       return;
2726     }
2727
2728   /* Don't generate any opcodes, just record the details for later.  */
2729   unwind.frame_size += offset;
2730   unwind.pending_offset += offset;
2731
2732   demand_empty_rest_of_line ();
2733 }
2734
2735 /* Parse an unwind_setfp directive.  */
2736
2737 static void
2738 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
2739 {
2740   int sp_reg;
2741   int fp_reg;
2742   int offset;
2743
2744   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
2745   if (skip_past_comma (&input_line_pointer) == FAIL)
2746     sp_reg = FAIL;
2747   else
2748     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
2749
2750   if (fp_reg == FAIL || sp_reg == FAIL)
2751     {
2752       as_bad (_("expected <reg>, <reg>"));
2753       ignore_rest_of_line ();
2754       return;
2755     }
2756
2757   /* Optional constant.  */
2758   if (skip_past_comma (&input_line_pointer) != FAIL)
2759     {
2760       if (immediate_for_directive (&offset) == FAIL)
2761         return;
2762     }
2763   else
2764     offset = 0;
2765
2766   demand_empty_rest_of_line ();
2767
2768   if (sp_reg != 13 && sp_reg != unwind.fp_reg)
2769     {
2770       as_bad (_("register must be either sp or set by a previous"
2771                 "unwind_movsp directive"));
2772       return;
2773     }
2774
2775   /* Don't generate any opcodes, just record the information for later.  */
2776   unwind.fp_reg = fp_reg;
2777   unwind.fp_used = 1;
2778   if (sp_reg == 13)
2779     unwind.fp_offset = unwind.frame_size - offset;
2780   else
2781     unwind.fp_offset -= offset;
2782 }
2783
2784 /* Parse an unwind_raw directive.  */
2785
2786 static void
2787 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
2788 {
2789   expressionS exp;
2790   /* This is an arbitary limit.  */
2791   unsigned char op[16];
2792   int count;
2793
2794   expression (&exp);
2795   if (exp.X_op == O_constant
2796       && skip_past_comma (&input_line_pointer) != FAIL)
2797     {
2798       unwind.frame_size += exp.X_add_number;
2799       expression (&exp);
2800     }
2801   else
2802     exp.X_op = O_illegal;
2803
2804   if (exp.X_op != O_constant)
2805     {
2806       as_bad (_("expected <offset>, <opcode>"));
2807       ignore_rest_of_line ();
2808       return;
2809     }
2810
2811   count = 0;
2812
2813   /* Parse the opcode.  */
2814   for (;;)
2815     {
2816       if (count >= 16)
2817         {
2818           as_bad (_("unwind opcode too long"));
2819           ignore_rest_of_line ();
2820         }
2821       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
2822         {
2823           as_bad (_("invalid unwind opcode"));
2824           ignore_rest_of_line ();
2825           return;
2826         }
2827       op[count++] = exp.X_add_number;
2828
2829       /* Parse the next byte.  */
2830       if (skip_past_comma (&input_line_pointer) == FAIL)
2831         break;
2832
2833       expression (&exp);
2834     }
2835
2836   /* Add the opcode bytes in reverse order.  */
2837   while (count--)
2838     add_unwind_opcode (op[count], 1);
2839
2840   demand_empty_rest_of_line ();
2841 }
2842
2843
2844 /* Parse a .eabi_attribute directive.  */
2845
2846 static void
2847 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
2848 {
2849   expressionS exp;
2850   bfd_boolean is_string;
2851   int tag;
2852   unsigned int i = 0;
2853   char *s = NULL;
2854   char saved_char;
2855
2856   expression (& exp);
2857   if (exp.X_op != O_constant)
2858     goto bad;
2859
2860   tag = exp.X_add_number;
2861   if (tag == 4 || tag == 5 || tag == 32 || (tag > 32 && (tag & 1) != 0))
2862     is_string = 1;
2863   else
2864     is_string = 0;
2865
2866   if (skip_past_comma (&input_line_pointer) == FAIL)
2867     goto bad;
2868   if (tag == 32 || !is_string)
2869     {
2870       expression (& exp);
2871       if (exp.X_op != O_constant)
2872         {
2873           as_bad (_("expected numeric constant"));
2874           ignore_rest_of_line ();
2875           return;
2876         }
2877       i = exp.X_add_number;
2878     }
2879   if (tag == Tag_compatibility
2880       && skip_past_comma (&input_line_pointer) == FAIL)
2881     {
2882       as_bad (_("expected comma"));
2883       ignore_rest_of_line ();
2884       return;
2885     }
2886   if (is_string)
2887     {
2888       skip_whitespace(input_line_pointer);
2889       if (*input_line_pointer != '"')
2890         goto bad_string;
2891       input_line_pointer++;
2892       s = input_line_pointer;
2893       while (*input_line_pointer && *input_line_pointer != '"')
2894         input_line_pointer++;
2895       if (*input_line_pointer != '"')
2896         goto bad_string;
2897       saved_char = *input_line_pointer;
2898       *input_line_pointer = 0;
2899     }
2900   else
2901     {
2902       s = NULL;
2903       saved_char = 0;
2904     }
2905   
2906   if (tag == Tag_compatibility)
2907     elf32_arm_add_eabi_attr_compat (stdoutput, i, s);
2908   else if (is_string)
2909     elf32_arm_add_eabi_attr_string (stdoutput, tag, s);
2910   else
2911     elf32_arm_add_eabi_attr_int (stdoutput, tag, i);
2912
2913   if (s)
2914     {
2915       *input_line_pointer = saved_char;
2916       input_line_pointer++;
2917     }
2918   demand_empty_rest_of_line ();
2919   return;
2920 bad_string:
2921   as_bad (_("bad string constant"));
2922   ignore_rest_of_line ();
2923   return;
2924 bad:
2925   as_bad (_("expected <tag> , <value>"));
2926   ignore_rest_of_line ();
2927 }
2928
2929 static void s_arm_arch (int);
2930 static void s_arm_cpu (int);
2931 static void s_arm_fpu (int);
2932 #endif /* OBJ_ELF */
2933
2934 /* This table describes all the machine specific pseudo-ops the assembler
2935    has to support.  The fields are:
2936      pseudo-op name without dot
2937      function to call to execute this pseudo-op
2938      Integer arg to pass to the function.  */
2939
2940 const pseudo_typeS md_pseudo_table[] =
2941 {
2942   /* Never called because '.req' does not start a line.  */
2943   { "req",         s_req,         0 },
2944   { "unreq",       s_unreq,       0 },
2945   { "bss",         s_bss,         0 },
2946   { "align",       s_align,       0 },
2947   { "arm",         s_arm,         0 },
2948   { "thumb",       s_thumb,       0 },
2949   { "code",        s_code,        0 },
2950   { "force_thumb", s_force_thumb, 0 },
2951   { "thumb_func",  s_thumb_func,  0 },
2952   { "thumb_set",   s_thumb_set,   0 },
2953   { "even",        s_even,        0 },
2954   { "ltorg",       s_ltorg,       0 },
2955   { "pool",        s_ltorg,       0 },
2956   { "syntax",      s_syntax,      0 },
2957 #ifdef OBJ_ELF
2958   { "word",        s_arm_elf_cons, 4 },
2959   { "long",        s_arm_elf_cons, 4 },
2960   { "rel31",       s_arm_rel31,   0 },
2961   { "fnstart",          s_arm_unwind_fnstart,   0 },
2962   { "fnend",            s_arm_unwind_fnend,     0 },
2963   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
2964   { "personality",      s_arm_unwind_personality, 0 },
2965   { "personalityindex", s_arm_unwind_personalityindex, 0 },
2966   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
2967   { "save",             s_arm_unwind_save,      0 },
2968   { "movsp",            s_arm_unwind_movsp,     0 },
2969   { "pad",              s_arm_unwind_pad,       0 },
2970   { "setfp",            s_arm_unwind_setfp,     0 },
2971   { "unwind_raw",       s_arm_unwind_raw,       0 },
2972   { "cpu",              s_arm_cpu,              0 },
2973   { "arch",             s_arm_arch,             0 },
2974   { "fpu",              s_arm_fpu,              0 },
2975   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
2976 #else
2977   { "word",        cons, 4},
2978 #endif
2979   { "extend",      float_cons, 'x' },
2980   { "ldouble",     float_cons, 'x' },
2981   { "packed",      float_cons, 'p' },
2982   { 0, 0, 0 }
2983 };
2984 \f
2985 /* Parser functions used exclusively in instruction operands.  */
2986
2987 /* Generic immediate-value read function for use in insn parsing.
2988    STR points to the beginning of the immediate (the leading #);
2989    VAL receives the value; if the value is outside [MIN, MAX]
2990    issue an error.  PREFIX_OPT is true if the immediate prefix is
2991    optional.  */
2992
2993 static int
2994 parse_immediate (char **str, int *val, int min, int max,
2995                  bfd_boolean prefix_opt)
2996 {
2997   expressionS exp;
2998   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
2999   if (exp.X_op != O_constant)
3000     {
3001       inst.error = _("constant expression required");
3002       return FAIL;
3003     }
3004
3005   if (exp.X_add_number < min || exp.X_add_number > max)
3006     {
3007       inst.error = _("immediate value out of range");
3008       return FAIL;
3009     }
3010
3011   *val = exp.X_add_number;
3012   return SUCCESS;
3013 }
3014
3015 /* Returns the pseudo-register number of an FPA immediate constant,
3016    or FAIL if there isn't a valid constant here.  */
3017
3018 static int
3019 parse_fpa_immediate (char ** str)
3020 {
3021   LITTLENUM_TYPE words[MAX_LITTLENUMS];
3022   char *         save_in;
3023   expressionS    exp;
3024   int            i;
3025   int            j;
3026
3027   /* First try and match exact strings, this is to guarantee
3028      that some formats will work even for cross assembly.  */
3029
3030   for (i = 0; fp_const[i]; i++)
3031     {
3032       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
3033         {
3034           char *start = *str;
3035
3036           *str += strlen (fp_const[i]);
3037           if (is_end_of_line[(unsigned char) **str])
3038             return i + 8;
3039           *str = start;
3040         }
3041     }
3042
3043   /* Just because we didn't get a match doesn't mean that the constant
3044      isn't valid, just that it is in a format that we don't
3045      automatically recognize.  Try parsing it with the standard
3046      expression routines.  */
3047
3048   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
3049
3050   /* Look for a raw floating point number.  */
3051   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
3052       && is_end_of_line[(unsigned char) *save_in])
3053     {
3054       for (i = 0; i < NUM_FLOAT_VALS; i++)
3055         {
3056           for (j = 0; j < MAX_LITTLENUMS; j++)
3057             {
3058               if (words[j] != fp_values[i][j])
3059                 break;
3060             }
3061
3062           if (j == MAX_LITTLENUMS)
3063             {
3064               *str = save_in;
3065               return i + 8;
3066             }
3067         }
3068     }
3069
3070   /* Try and parse a more complex expression, this will probably fail
3071      unless the code uses a floating point prefix (eg "0f").  */
3072   save_in = input_line_pointer;
3073   input_line_pointer = *str;
3074   if (expression (&exp) == absolute_section
3075       && exp.X_op == O_big
3076       && exp.X_add_number < 0)
3077     {
3078       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
3079          Ditto for 15.  */
3080       if (gen_to_words (words, 5, (long) 15) == 0)
3081         {
3082           for (i = 0; i < NUM_FLOAT_VALS; i++)
3083             {
3084               for (j = 0; j < MAX_LITTLENUMS; j++)
3085                 {
3086                   if (words[j] != fp_values[i][j])
3087                     break;
3088                 }
3089
3090               if (j == MAX_LITTLENUMS)
3091                 {
3092                   *str = input_line_pointer;
3093                   input_line_pointer = save_in;
3094                   return i + 8;
3095                 }
3096             }
3097         }
3098     }
3099
3100   *str = input_line_pointer;
3101   input_line_pointer = save_in;
3102   inst.error = _("invalid FPA immediate expression");
3103   return FAIL;
3104 }
3105
3106 /* Shift operands.  */
3107 enum shift_kind
3108 {
3109   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
3110 };
3111
3112 struct asm_shift_name
3113 {
3114   const char      *name;
3115   enum shift_kind  kind;
3116 };
3117
3118 /* Third argument to parse_shift.  */
3119 enum parse_shift_mode
3120 {
3121   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
3122   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
3123   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
3124   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
3125   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
3126 };
3127
3128 /* Parse a <shift> specifier on an ARM data processing instruction.
3129    This has three forms:
3130
3131      (LSL|LSR|ASL|ASR|ROR) Rs
3132      (LSL|LSR|ASL|ASR|ROR) #imm
3133      RRX
3134
3135    Note that ASL is assimilated to LSL in the instruction encoding, and
3136    RRX to ROR #0 (which cannot be written as such).  */
3137
3138 static int
3139 parse_shift (char **str, int i, enum parse_shift_mode mode)
3140 {
3141   const struct asm_shift_name *shift_name;
3142   enum shift_kind shift;
3143   char *s = *str;
3144   char *p = s;
3145   int reg;
3146
3147   for (p = *str; ISALPHA (*p); p++)
3148     ;
3149
3150   if (p == *str)
3151     {
3152       inst.error = _("shift expression expected");
3153       return FAIL;
3154     }
3155
3156   shift_name = hash_find_n (arm_shift_hsh, *str, p - *str);
3157
3158   if (shift_name == NULL)
3159     {
3160       inst.error = _("shift expression expected");
3161       return FAIL;
3162     }
3163
3164   shift = shift_name->kind;
3165
3166   switch (mode)
3167     {
3168     case NO_SHIFT_RESTRICT:
3169     case SHIFT_IMMEDIATE:   break;
3170
3171     case SHIFT_LSL_OR_ASR_IMMEDIATE:
3172       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
3173         {
3174           inst.error = _("'LSL' or 'ASR' required");
3175           return FAIL;
3176         }
3177       break;
3178
3179     case SHIFT_LSL_IMMEDIATE:
3180       if (shift != SHIFT_LSL)
3181         {
3182           inst.error = _("'LSL' required");
3183           return FAIL;
3184         }
3185       break;
3186
3187     case SHIFT_ASR_IMMEDIATE:
3188       if (shift != SHIFT_ASR)
3189         {
3190           inst.error = _("'ASR' required");
3191           return FAIL;
3192         }
3193       break;
3194
3195     default: abort ();
3196     }
3197
3198   if (shift != SHIFT_RRX)
3199     {
3200       /* Whitespace can appear here if the next thing is a bare digit.  */
3201       skip_whitespace (p);
3202
3203       if (mode == NO_SHIFT_RESTRICT
3204           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
3205         {
3206           inst.operands[i].imm = reg;
3207           inst.operands[i].immisreg = 1;
3208         }
3209       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3210         return FAIL;
3211     }
3212   inst.operands[i].shift_kind = shift;
3213   inst.operands[i].shifted = 1;
3214   *str = p;
3215   return SUCCESS;
3216 }
3217
3218 /* Parse a <shifter_operand> for an ARM data processing instruction:
3219
3220       #<immediate>
3221       #<immediate>, <rotate>
3222       <Rm>
3223       <Rm>, <shift>
3224
3225    where <shift> is defined by parse_shift above, and <rotate> is a
3226    multiple of 2 between 0 and 30.  Validation of immediate operands
3227    is deferred to md_apply_fix.  */
3228
3229 static int
3230 parse_shifter_operand (char **str, int i)
3231 {
3232   int value;
3233   expressionS expr;
3234
3235   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
3236     {
3237       inst.operands[i].reg = value;
3238       inst.operands[i].isreg = 1;
3239
3240       /* parse_shift will override this if appropriate */
3241       inst.reloc.exp.X_op = O_constant;
3242       inst.reloc.exp.X_add_number = 0;
3243
3244       if (skip_past_comma (str) == FAIL)
3245         return SUCCESS;
3246
3247       /* Shift operation on register.  */
3248       return parse_shift (str, i, NO_SHIFT_RESTRICT);
3249     }
3250
3251   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
3252     return FAIL;
3253
3254   if (skip_past_comma (str) == SUCCESS)
3255     {
3256       /* #x, y -- ie explicit rotation by Y.  */
3257       if (my_get_expression (&expr, str, GE_NO_PREFIX))
3258         return FAIL;
3259
3260       if (expr.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
3261         {
3262           inst.error = _("constant expression expected");
3263           return FAIL;
3264         }
3265
3266       value = expr.X_add_number;
3267       if (value < 0 || value > 30 || value % 2 != 0)
3268         {
3269           inst.error = _("invalid rotation");
3270           return FAIL;
3271         }
3272       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
3273         {
3274           inst.error = _("invalid constant");
3275           return FAIL;
3276         }
3277
3278       /* Convert to decoded value.  md_apply_fix will put it back.  */
3279       inst.reloc.exp.X_add_number
3280         = (((inst.reloc.exp.X_add_number << (32 - value))
3281             | (inst.reloc.exp.X_add_number >> value)) & 0xffffffff);
3282     }
3283
3284   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
3285   inst.reloc.pc_rel = 0;
3286   return SUCCESS;
3287 }
3288
3289 /* Parse all forms of an ARM address expression.  Information is written
3290    to inst.operands[i] and/or inst.reloc.
3291
3292    Preindexed addressing (.preind=1):
3293
3294    [Rn, #offset]       .reg=Rn .reloc.exp=offset
3295    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3296    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3297                        .shift_kind=shift .reloc.exp=shift_imm
3298
3299    These three may have a trailing ! which causes .writeback to be set also.
3300
3301    Postindexed addressing (.postind=1, .writeback=1):
3302
3303    [Rn], #offset       .reg=Rn .reloc.exp=offset
3304    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3305    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3306                        .shift_kind=shift .reloc.exp=shift_imm
3307
3308    Unindexed addressing (.preind=0, .postind=0):
3309
3310    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
3311
3312    Other:
3313
3314    [Rn]{!}             shorthand for [Rn,#0]{!}
3315    =immediate          .isreg=0 .reloc.exp=immediate
3316    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
3317
3318   It is the caller's responsibility to check for addressing modes not
3319   supported by the instruction, and to set inst.reloc.type.  */
3320
3321 static int
3322 parse_address (char **str, int i)
3323 {
3324   char *p = *str;
3325   int reg;
3326
3327   if (skip_past_char (&p, '[') == FAIL)
3328     {
3329       if (skip_past_char (&p, '=') == FAIL)
3330         {
3331           /* bare address - translate to PC-relative offset */
3332           inst.reloc.pc_rel = 1;
3333           inst.operands[i].reg = REG_PC;
3334           inst.operands[i].isreg = 1;
3335           inst.operands[i].preind = 1;
3336         }
3337       /* else a load-constant pseudo op, no special treatment needed here */
3338
3339       if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
3340         return FAIL;
3341
3342       *str = p;
3343       return SUCCESS;
3344     }
3345
3346   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
3347     {
3348       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3349       return FAIL;
3350     }
3351   inst.operands[i].reg = reg;
3352   inst.operands[i].isreg = 1;
3353
3354   if (skip_past_comma (&p) == SUCCESS)
3355     {
3356       inst.operands[i].preind = 1;
3357
3358       if (*p == '+') p++;
3359       else if (*p == '-') p++, inst.operands[i].negative = 1;
3360
3361       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
3362         {
3363           inst.operands[i].imm = reg;
3364           inst.operands[i].immisreg = 1;
3365
3366           if (skip_past_comma (&p) == SUCCESS)
3367             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
3368               return FAIL;
3369         }
3370       else
3371         {
3372           if (inst.operands[i].negative)
3373             {
3374               inst.operands[i].negative = 0;
3375               p--;
3376             }
3377           if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3378             return FAIL;
3379         }
3380     }
3381
3382   if (skip_past_char (&p, ']') == FAIL)
3383     {
3384       inst.error = _("']' expected");
3385       return FAIL;
3386     }
3387
3388   if (skip_past_char (&p, '!') == SUCCESS)
3389     inst.operands[i].writeback = 1;
3390
3391   else if (skip_past_comma (&p) == SUCCESS)
3392     {
3393       if (skip_past_char (&p, '{') == SUCCESS)
3394         {
3395           /* [Rn], {expr} - unindexed, with option */
3396           if (parse_immediate (&p, &inst.operands[i].imm,
3397                                0, 255, TRUE) == FAIL)
3398             return FAIL;
3399
3400           if (skip_past_char (&p, '}') == FAIL)
3401             {
3402               inst.error = _("'}' expected at end of 'option' field");
3403               return FAIL;
3404             }
3405           if (inst.operands[i].preind)
3406             {
3407               inst.error = _("cannot combine index with option");
3408               return FAIL;
3409             }
3410           *str = p;
3411           return SUCCESS;
3412         }
3413       else
3414         {
3415           inst.operands[i].postind = 1;
3416           inst.operands[i].writeback = 1;
3417
3418           if (inst.operands[i].preind)
3419             {
3420               inst.error = _("cannot combine pre- and post-indexing");
3421               return FAIL;
3422             }
3423
3424           if (*p == '+') p++;
3425           else if (*p == '-') p++, inst.operands[i].negative = 1;
3426
3427           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
3428             {
3429               inst.operands[i].imm = reg;
3430               inst.operands[i].immisreg = 1;
3431
3432               if (skip_past_comma (&p) == SUCCESS)
3433                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
3434                   return FAIL;
3435             }
3436           else
3437             {
3438               if (inst.operands[i].negative)
3439                 {
3440                   inst.operands[i].negative = 0;
3441                   p--;
3442                 }
3443               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3444                 return FAIL;
3445             }
3446         }
3447     }
3448
3449   /* If at this point neither .preind nor .postind is set, we have a
3450      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
3451   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
3452     {
3453       inst.operands[i].preind = 1;
3454       inst.reloc.exp.X_op = O_constant;
3455       inst.reloc.exp.X_add_number = 0;
3456     }
3457   *str = p;
3458   return SUCCESS;
3459 }
3460
3461 /* Miscellaneous. */
3462
3463 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
3464    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
3465 static int
3466 parse_psr (char **str)
3467 {
3468   char *p;
3469   unsigned long psr_field;
3470
3471   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
3472      feature for ease of use and backwards compatibility.  */
3473   p = *str;
3474   if (*p == 's' || *p == 'S')
3475     psr_field = SPSR_BIT;
3476   else if (*p == 'c' || *p == 'C')
3477     psr_field = 0;
3478   else
3479     goto error;
3480
3481   p++;
3482   if (strncasecmp (p, "PSR", 3) != 0)
3483     goto error;
3484   p += 3;
3485
3486   if (*p == '_')
3487     {
3488       /* A suffix follows.  */
3489       const struct asm_psr *psr;
3490       char *start;
3491
3492       p++;
3493       start = p;
3494
3495       do
3496         p++;
3497       while (ISALNUM (*p) || *p == '_');
3498
3499       psr = hash_find_n (arm_psr_hsh, start, p - start);
3500       if (!psr)
3501         goto error;
3502
3503       psr_field |= psr->field;
3504     }
3505   else
3506     {
3507       if (ISALNUM (*p))
3508         goto error;    /* Garbage after "[CS]PSR".  */
3509
3510       psr_field |= (PSR_c | PSR_f);
3511     }
3512   *str = p;
3513   return psr_field;
3514
3515  error:
3516   inst.error = _("flag for {c}psr instruction expected");
3517   return FAIL;
3518 }
3519
3520 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
3521    value suitable for splatting into the AIF field of the instruction.  */
3522
3523 static int
3524 parse_cps_flags (char **str)
3525 {
3526   int val = 0;
3527   int saw_a_flag = 0;
3528   char *s = *str;
3529
3530   for (;;)
3531     switch (*s++)
3532       {
3533       case '\0': case ',':
3534         goto done;
3535
3536       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
3537       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
3538       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
3539
3540       default:
3541         inst.error = _("unrecognized CPS flag");
3542         return FAIL;
3543       }
3544
3545  done:
3546   if (saw_a_flag == 0)
3547     {
3548       inst.error = _("missing CPS flags");
3549       return FAIL;
3550     }
3551
3552   *str = s - 1;
3553   return val;
3554 }
3555
3556 /* Parse an endian specifier ("BE" or "LE", case insensitive);
3557    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
3558
3559 static int
3560 parse_endian_specifier (char **str)
3561 {
3562   int little_endian;
3563   char *s = *str;
3564
3565   if (strncasecmp (s, "BE", 2))
3566     little_endian = 0;
3567   else if (strncasecmp (s, "LE", 2))
3568     little_endian = 1;
3569   else
3570     {
3571       inst.error = _("valid endian specifiers are be or le");
3572       return FAIL;
3573     }
3574
3575   if (ISALNUM (s[2]) || s[2] == '_')
3576     {
3577       inst.error = _("valid endian specifiers are be or le");
3578       return FAIL;
3579     }
3580
3581   *str = s + 2;
3582   return little_endian;
3583 }
3584
3585 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
3586    value suitable for poking into the rotate field of an sxt or sxta
3587    instruction, or FAIL on error.  */
3588
3589 static int
3590 parse_ror (char **str)
3591 {
3592   int rot;
3593   char *s = *str;
3594
3595   if (strncasecmp (s, "ROR", 3) == 0)
3596     s += 3;
3597   else
3598     {
3599       inst.error = _("missing rotation field after comma");
3600       return FAIL;
3601     }
3602
3603   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
3604     return FAIL;
3605
3606   switch (rot)
3607     {
3608     case  0: *str = s; return 0x0;
3609     case  8: *str = s; return 0x1;
3610     case 16: *str = s; return 0x2;
3611     case 24: *str = s; return 0x3;
3612
3613     default:
3614       inst.error = _("rotation can only be 0, 8, 16, or 24");
3615       return FAIL;
3616     }
3617 }
3618
3619 /* Parse a conditional code (from conds[] below).  The value returned is in the
3620    range 0 .. 14, or FAIL.  */
3621 static int
3622 parse_cond (char **str)
3623 {
3624   char *p, *q;
3625   const struct asm_cond *c;
3626
3627   p = q = *str;
3628   while (ISALPHA (*q))
3629     q++;
3630
3631   c = hash_find_n (arm_cond_hsh, p, q - p);
3632   if (!c)
3633     {
3634       inst.error = _("condition required");
3635       return FAIL;
3636     }
3637
3638   *str = q;
3639   return c->value;
3640 }
3641
3642 /* Parse the operands of a table branch instruction.  Similar to a memory
3643    operand.  */
3644 static int
3645 parse_tb (char **str)
3646 {
3647   char * p = *str;
3648   int reg;
3649
3650   if (skip_past_char (&p, '[') == FAIL)
3651     return FAIL;
3652
3653   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
3654     {
3655       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3656       return FAIL;
3657     }
3658   inst.operands[0].reg = reg;
3659
3660   if (skip_past_comma (&p) == FAIL)
3661     return FAIL;
3662   
3663   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
3664     {
3665       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3666       return FAIL;
3667     }
3668   inst.operands[0].imm = reg;
3669
3670   if (skip_past_comma (&p) == SUCCESS)
3671     {
3672       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
3673         return FAIL;
3674       if (inst.reloc.exp.X_add_number != 1)
3675         {
3676           inst.error = _("invalid shift");
3677           return FAIL;
3678         }
3679       inst.operands[0].shifted = 1;
3680     }
3681
3682   if (skip_past_char (&p, ']') == FAIL)
3683     {
3684       inst.error = _("']' expected");
3685       return FAIL;
3686     }
3687   *str = p;
3688   return SUCCESS;
3689 }
3690
3691 /* Matcher codes for parse_operands.  */
3692 enum operand_parse_code
3693 {
3694   OP_stop,      /* end of line */
3695
3696   OP_RR,        /* ARM register */
3697   OP_RRnpc,     /* ARM register, not r15 */
3698   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
3699   OP_RRw,       /* ARM register, not r15, optional trailing ! */
3700   OP_RCP,       /* Coprocessor number */
3701   OP_RCN,       /* Coprocessor register */
3702   OP_RF,        /* FPA register */
3703   OP_RVS,       /* VFP single precision register */
3704   OP_RVD,       /* VFP double precision register */
3705   OP_RVC,       /* VFP control register */
3706   OP_RMF,       /* Maverick F register */
3707   OP_RMD,       /* Maverick D register */
3708   OP_RMFX,      /* Maverick FX register */
3709   OP_RMDX,      /* Maverick DX register */
3710   OP_RMAX,      /* Maverick AX register */
3711   OP_RMDS,      /* Maverick DSPSC register */
3712   OP_RIWR,      /* iWMMXt wR register */
3713   OP_RIWC,      /* iWMMXt wC register */
3714   OP_RIWG,      /* iWMMXt wCG register */
3715   OP_RXA,       /* XScale accumulator register */
3716
3717   OP_REGLST,    /* ARM register list */
3718   OP_VRSLST,    /* VFP single-precision register list */
3719   OP_VRDLST,    /* VFP double-precision register list */
3720
3721   OP_I7,        /* immediate value 0 .. 7 */
3722   OP_I15,       /*                 0 .. 15 */
3723   OP_I16,       /*                 1 .. 16 */
3724   OP_I31,       /*                 0 .. 31 */
3725   OP_I31w,      /*                 0 .. 31, optional trailing ! */
3726   OP_I32,       /*                 1 .. 32 */
3727   OP_I63s,      /*               -64 .. 63 */
3728   OP_I255,      /*                 0 .. 255 */
3729   OP_Iffff,     /*                 0 .. 65535 */
3730
3731   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
3732   OP_I7b,       /*                             0 .. 7 */
3733   OP_I15b,      /*                             0 .. 15 */
3734   OP_I31b,      /*                             0 .. 31 */
3735
3736   OP_SH,        /* shifter operand */
3737   OP_ADDR,      /* Memory address expression (any mode) */
3738   OP_EXP,       /* arbitrary expression */
3739   OP_EXPi,      /* same, with optional immediate prefix */
3740   OP_EXPr,      /* same, with optional relocation suffix */
3741
3742   OP_CPSF,      /* CPS flags */
3743   OP_ENDI,      /* Endianness specifier */
3744   OP_PSR,       /* CPSR/SPSR mask for msr */
3745   OP_COND,      /* conditional code */
3746   OP_TB,        /* Table branch.  */
3747
3748   OP_RRnpc_I0,  /* ARM register or literal 0 */
3749   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
3750   OP_RR_EXi,    /* ARM register or expression with imm prefix */
3751   OP_RF_IF,     /* FPA register or immediate */
3752   OP_RIWR_RIWC, /* iWMMXt R or C reg */
3753
3754   /* Optional operands.  */
3755   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
3756   OP_oI31b,      /*                             0 .. 31 */
3757   OP_oIffffb,    /*                             0 .. 65535 */
3758   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
3759
3760   OP_oRR,        /* ARM register */
3761   OP_oRRnpc,     /* ARM register, not the PC */
3762   OP_oSHll,      /* LSL immediate */
3763   OP_oSHar,      /* ASR immediate */
3764   OP_oSHllar,    /* LSL or ASR immediate */
3765   OP_oROR,       /* ROR 0/8/16/24 */
3766
3767   OP_FIRST_OPTIONAL = OP_oI7b
3768 };
3769
3770 /* Generic instruction operand parser.  This does no encoding and no
3771    semantic validation; it merely squirrels values away in the inst
3772    structure.  Returns SUCCESS or FAIL depending on whether the
3773    specified grammar matched.  */
3774 static int
3775 parse_operands (char *str, const unsigned char *pattern)
3776 {
3777   unsigned const char *upat = pattern;
3778   char *backtrack_pos = 0;
3779   const char *backtrack_error = 0;
3780   int i, val, backtrack_index = 0;
3781
3782 #define po_char_or_fail(chr) do {               \
3783   if (skip_past_char (&str, chr) == FAIL)       \
3784     goto bad_args;                              \
3785 } while (0)
3786
3787 #define po_reg_or_fail(regtype) do {                    \
3788   val = arm_reg_parse (&str, regtype);                  \
3789   if (val == FAIL)                                      \
3790     {                                                   \
3791       inst.error = _(reg_expected_msgs[regtype]);       \
3792       goto failure;                                     \
3793     }                                                   \
3794   inst.operands[i].reg = val;                           \
3795   inst.operands[i].isreg = 1;                           \
3796 } while (0)
3797
3798 #define po_reg_or_goto(regtype, label) do {     \
3799   val = arm_reg_parse (&str, regtype);          \
3800   if (val == FAIL)                              \
3801     goto label;                                 \
3802                                                 \
3803   inst.operands[i].reg = val;                   \
3804   inst.operands[i].isreg = 1;                   \
3805 } while (0)
3806
3807 #define po_imm_or_fail(min, max, popt) do {                     \
3808   if (parse_immediate (&str, &val, min, max, popt) == FAIL)     \
3809     goto failure;                                               \
3810   inst.operands[i].imm = val;                                   \
3811 } while (0)
3812
3813 #define po_misc_or_fail(expr) do {              \
3814   if (expr)                                     \
3815     goto failure;                               \
3816 } while (0)
3817
3818   skip_whitespace (str);
3819
3820   for (i = 0; upat[i] != OP_stop; i++)
3821     {
3822       if (upat[i] >= OP_FIRST_OPTIONAL)
3823         {
3824           /* Remember where we are in case we need to backtrack.  */
3825           assert (!backtrack_pos);
3826           backtrack_pos = str;
3827           backtrack_error = inst.error;
3828           backtrack_index = i;
3829         }
3830
3831       if (i > 0)
3832         po_char_or_fail (',');
3833
3834       switch (upat[i])
3835         {
3836           /* Registers */
3837         case OP_oRRnpc:
3838         case OP_RRnpc:
3839         case OP_oRR:
3840         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
3841         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
3842         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
3843         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
3844         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
3845         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
3846         case OP_RVC:   po_reg_or_fail (REG_TYPE_VFC);     break;
3847         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
3848         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
3849         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
3850         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
3851         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
3852         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
3853         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
3854         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
3855         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
3856         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
3857
3858         case OP_RRnpcb:
3859           po_char_or_fail ('[');
3860           po_reg_or_fail  (REG_TYPE_RN);
3861           po_char_or_fail (']');
3862           break;
3863
3864         case OP_RRw:
3865           po_reg_or_fail (REG_TYPE_RN);
3866           if (skip_past_char (&str, '!') == SUCCESS)
3867             inst.operands[i].writeback = 1;
3868           break;
3869
3870           /* Immediates */
3871         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
3872         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
3873         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
3874         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
3875         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
3876         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
3877         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
3878         case OP_Iffff:   po_imm_or_fail (  0, 0xffff, FALSE);   break;
3879
3880         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
3881         case OP_oI7b:
3882         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
3883         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
3884         case OP_oI31b:
3885         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
3886         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
3887
3888           /* Immediate variants */
3889         case OP_oI255c:
3890           po_char_or_fail ('{');
3891           po_imm_or_fail (0, 255, TRUE);
3892           po_char_or_fail ('}');
3893           break;
3894
3895         case OP_I31w:
3896           /* The expression parser chokes on a trailing !, so we have
3897              to find it first and zap it.  */
3898           {
3899             char *s = str;
3900             while (*s && *s != ',')
3901               s++;
3902             if (s[-1] == '!')
3903               {
3904                 s[-1] = '\0';
3905                 inst.operands[i].writeback = 1;
3906               }
3907             po_imm_or_fail (0, 31, TRUE);
3908             if (str == s - 1)
3909               str = s;
3910           }
3911           break;
3912
3913           /* Expressions */
3914         case OP_EXPi:   EXPi:
3915           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
3916                                               GE_OPT_PREFIX));
3917           break;
3918
3919         case OP_EXP:
3920           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
3921                                               GE_NO_PREFIX));
3922           break;
3923
3924         case OP_EXPr:   EXPr:
3925           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
3926                                               GE_NO_PREFIX));
3927           if (inst.reloc.exp.X_op == O_symbol)
3928             {
3929               val = parse_reloc (&str);
3930               if (val == -1)
3931                 {
3932                   inst.error = _("unrecognized relocation suffix");
3933                   goto failure;
3934                 }
3935               else if (val != BFD_RELOC_UNUSED)
3936                 {
3937                   inst.operands[i].imm = val;
3938                   inst.operands[i].hasreloc = 1;
3939                 }
3940             }
3941           break;
3942
3943           /* Register or expression */
3944         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
3945         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
3946
3947           /* Register or immediate */
3948         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
3949         I0:               po_imm_or_fail (0, 0, FALSE);       break;
3950
3951         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
3952         IF:
3953           if (!is_immediate_prefix (*str))
3954             goto bad_args;
3955           str++;
3956           val = parse_fpa_immediate (&str);
3957           if (val == FAIL)
3958             goto failure;
3959           /* FPA immediates are encoded as registers 8-15.
3960              parse_fpa_immediate has already applied the offset.  */
3961           inst.operands[i].reg = val;
3962           inst.operands[i].isreg = 1;
3963           break;
3964
3965           /* Two kinds of register */
3966         case OP_RIWR_RIWC:
3967           {
3968             struct reg_entry *rege = arm_reg_parse_multi (&str);
3969             if (rege->type != REG_TYPE_MMXWR
3970                 && rege->type != REG_TYPE_MMXWC
3971                 && rege->type != REG_TYPE_MMXWCG)
3972               {
3973                 inst.error = _("iWMMXt data or control register expected");
3974                 goto failure;
3975               }
3976             inst.operands[i].reg = rege->number;
3977             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
3978           }
3979           break;
3980
3981           /* Misc */
3982         case OP_CPSF:    val = parse_cps_flags (&str);          break;
3983         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
3984         case OP_oROR:    val = parse_ror (&str);                break;
3985         case OP_PSR:     val = parse_psr (&str);                break;
3986         case OP_COND:    val = parse_cond (&str);               break;
3987
3988         case OP_TB:
3989           po_misc_or_fail (parse_tb (&str));
3990           break;
3991
3992           /* Register lists */
3993         case OP_REGLST:
3994           val = parse_reg_list (&str);
3995           if (*str == '^')
3996             {
3997               inst.operands[1].writeback = 1;
3998               str++;
3999             }
4000           break;
4001
4002         case OP_VRSLST:
4003           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, 0);
4004           break;
4005
4006         case OP_VRDLST:
4007           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, 1);
4008           break;
4009
4010           /* Addressing modes */
4011         case OP_ADDR:
4012           po_misc_or_fail (parse_address (&str, i));
4013           break;
4014
4015         case OP_SH:
4016           po_misc_or_fail (parse_shifter_operand (&str, i));
4017           break;
4018
4019         case OP_oSHll:
4020           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
4021           break;
4022
4023         case OP_oSHar:
4024           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
4025           break;
4026
4027         case OP_oSHllar:
4028           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
4029           break;
4030
4031         default:
4032           as_fatal ("unhandled operand code %d", upat[i]);
4033         }
4034
4035       /* Various value-based sanity checks and shared operations.  We
4036          do not signal immediate failures for the register constraints;
4037          this allows a syntax error to take precedence.  */
4038       switch (upat[i])
4039         {
4040         case OP_oRRnpc:
4041         case OP_RRnpc:
4042         case OP_RRnpcb:
4043         case OP_RRw:
4044         case OP_RRnpc_I0:
4045           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
4046             inst.error = BAD_PC;
4047           break;
4048
4049         case OP_CPSF:
4050         case OP_ENDI:
4051         case OP_oROR:
4052         case OP_PSR:
4053         case OP_COND:
4054         case OP_REGLST:
4055         case OP_VRSLST:
4056         case OP_VRDLST:
4057           if (val == FAIL)
4058             goto failure;
4059           inst.operands[i].imm = val;
4060           break;
4061
4062         default:
4063           break;
4064         }
4065
4066       /* If we get here, this operand was successfully parsed.  */
4067       inst.operands[i].present = 1;
4068       continue;
4069
4070     bad_args:
4071       inst.error = BAD_ARGS;
4072
4073     failure:
4074       if (!backtrack_pos)
4075         return FAIL;
4076
4077       /* Do not backtrack over a trailing optional argument that
4078          absorbed some text.  We will only fail again, with the
4079          'garbage following instruction' error message, which is
4080          probably less helpful than the current one.  */
4081       if (backtrack_index == i && backtrack_pos != str
4082           && upat[i+1] == OP_stop)
4083         return FAIL;
4084
4085       /* Try again, skipping the optional argument at backtrack_pos.  */
4086       str = backtrack_pos;
4087       inst.error = backtrack_error;
4088       inst.operands[backtrack_index].present = 0;
4089       i = backtrack_index;
4090       backtrack_pos = 0;
4091     }
4092
4093   /* Check that we have parsed all the arguments.  */
4094   if (*str != '\0' && !inst.error)
4095     inst.error = _("garbage following instruction");
4096
4097   return inst.error ? FAIL : SUCCESS;
4098 }
4099
4100 #undef po_char_or_fail
4101 #undef po_reg_or_fail
4102 #undef po_reg_or_goto
4103 #undef po_imm_or_fail
4104 \f
4105 /* Shorthand macro for instruction encoding functions issuing errors.  */
4106 #define constraint(expr, err) do {              \
4107   if (expr)                                     \
4108     {                                           \
4109       inst.error = err;                         \
4110       return;                                   \
4111     }                                           \
4112 } while (0)
4113
4114 /* Functions for operand encoding.  ARM, then Thumb.  */
4115
4116 #define rotate_left(v, n) (v << n | v >> (32 - n))
4117
4118 /* If VAL can be encoded in the immediate field of an ARM instruction,
4119    return the encoded form.  Otherwise, return FAIL.  */
4120
4121 static unsigned int
4122 encode_arm_immediate (unsigned int val)
4123 {
4124   unsigned int a, i;
4125
4126   for (i = 0; i < 32; i += 2)
4127     if ((a = rotate_left (val, i)) <= 0xff)
4128       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
4129
4130   return FAIL;
4131 }
4132
4133 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
4134    return the encoded form.  Otherwise, return FAIL.  */
4135 static unsigned int
4136 encode_thumb32_immediate (unsigned int val)
4137 {
4138   unsigned int a, i;
4139
4140   if (val <= 0xff)
4141     return val;
4142
4143   for (i = 1; i <= 24; i++)
4144     {
4145       a = val >> i;
4146       if ((val & ~(0xff << i)) == 0)
4147         return ((val >> i) & 0x7f) | ((32 - i) << 7);
4148     }
4149
4150   a = val & 0xff;
4151   if (val == ((a << 16) | a))
4152     return 0x100 | a;
4153   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
4154     return 0x300 | a;
4155
4156   a = val & 0xff00;
4157   if (val == ((a << 16) | a))
4158     return 0x200 | (a >> 8);
4159
4160   return FAIL;
4161 }
4162 /* Encode a VFP SP register number into inst.instruction.  */
4163
4164 static void
4165 encode_arm_vfp_sp_reg (int reg, enum vfp_sp_reg_pos pos)
4166 {
4167   switch (pos)
4168     {
4169     case VFP_REG_Sd:
4170       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
4171       break;
4172
4173     case VFP_REG_Sn:
4174       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
4175       break;
4176
4177     case VFP_REG_Sm:
4178       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
4179       break;
4180
4181     default:
4182       abort ();
4183     }
4184 }
4185
4186 /* Encode a <shift> in an ARM-format instruction.  The immediate,
4187    if any, is handled by md_apply_fix.   */
4188 static void
4189 encode_arm_shift (int i)
4190 {
4191   if (inst.operands[i].shift_kind == SHIFT_RRX)
4192     inst.instruction |= SHIFT_ROR << 5;
4193   else
4194     {
4195       inst.instruction |= inst.operands[i].shift_kind << 5;
4196       if (inst.operands[i].immisreg)
4197         {
4198           inst.instruction |= SHIFT_BY_REG;
4199           inst.instruction |= inst.operands[i].imm << 8;
4200         }
4201       else
4202         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
4203     }
4204 }
4205
4206 static void
4207 encode_arm_shifter_operand (int i)
4208 {
4209   if (inst.operands[i].isreg)
4210     {
4211       inst.instruction |= inst.operands[i].reg;
4212       encode_arm_shift (i);
4213     }
4214   else
4215     inst.instruction |= INST_IMMEDIATE;
4216 }
4217
4218 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
4219 static void
4220 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
4221 {
4222   assert (inst.operands[i].isreg);
4223   inst.instruction |= inst.operands[i].reg << 16;
4224
4225   if (inst.operands[i].preind)
4226     {
4227       if (is_t)
4228         {
4229           inst.error = _("instruction does not accept preindexed addressing");
4230           return;
4231         }
4232       inst.instruction |= PRE_INDEX;
4233       if (inst.operands[i].writeback)
4234         inst.instruction |= WRITE_BACK;
4235
4236     }
4237   else if (inst.operands[i].postind)
4238     {
4239       assert (inst.operands[i].writeback);
4240       if (is_t)
4241         inst.instruction |= WRITE_BACK;
4242     }
4243   else /* unindexed - only for coprocessor */
4244     {
4245       inst.error = _("instruction does not accept unindexed addressing");
4246       return;
4247     }
4248
4249   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
4250       && (((inst.instruction & 0x000f0000) >> 16)
4251           == ((inst.instruction & 0x0000f000) >> 12)))
4252     as_warn ((inst.instruction & LOAD_BIT)
4253              ? _("destination register same as write-back base")
4254              : _("source register same as write-back base"));
4255 }
4256
4257 /* inst.operands[i] was set up by parse_address.  Encode it into an
4258    ARM-format mode 2 load or store instruction.  If is_t is true,
4259    reject forms that cannot be used with a T instruction (i.e. not
4260    post-indexed).  */
4261 static void
4262 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
4263 {
4264   encode_arm_addr_mode_common (i, is_t);
4265
4266   if (inst.operands[i].immisreg)
4267     {
4268       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
4269       inst.instruction |= inst.operands[i].imm;
4270       if (!inst.operands[i].negative)
4271         inst.instruction |= INDEX_UP;
4272       if (inst.operands[i].shifted)
4273         {
4274           if (inst.operands[i].shift_kind == SHIFT_RRX)
4275             inst.instruction |= SHIFT_ROR << 5;
4276           else
4277             {
4278               inst.instruction |= inst.operands[i].shift_kind << 5;
4279               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
4280             }
4281         }
4282     }
4283   else /* immediate offset in inst.reloc */
4284     {
4285       if (inst.reloc.type == BFD_RELOC_UNUSED)
4286         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
4287     }
4288 }
4289
4290 /* inst.operands[i] was set up by parse_address.  Encode it into an
4291    ARM-format mode 3 load or store instruction.  Reject forms that
4292    cannot be used with such instructions.  If is_t is true, reject
4293    forms that cannot be used with a T instruction (i.e. not
4294    post-indexed).  */
4295 static void
4296 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
4297 {
4298   if (inst.operands[i].immisreg && inst.operands[i].shifted)
4299     {
4300       inst.error = _("instruction does not accept scaled register index");
4301       return;
4302     }
4303
4304   encode_arm_addr_mode_common (i, is_t);
4305
4306   if (inst.operands[i].immisreg)
4307     {
4308       inst.instruction |= inst.operands[i].imm;
4309       if (!inst.operands[i].negative)
4310         inst.instruction |= INDEX_UP;
4311     }
4312   else /* immediate offset in inst.reloc */
4313     {
4314       inst.instruction |= HWOFFSET_IMM;
4315       if (inst.reloc.type == BFD_RELOC_UNUSED)
4316         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
4317     }
4318 }
4319
4320 /* inst.operands[i] was set up by parse_address.  Encode it into an
4321    ARM-format instruction.  Reject all forms which cannot be encoded
4322    into a coprocessor load/store instruction.  If wb_ok is false,
4323    reject use of writeback; if unind_ok is false, reject use of
4324    unindexed addressing.  If reloc_override is not 0, use it instead
4325    of BFD_ARM_CP_OFF_IMM.  */
4326
4327 static int
4328 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
4329 {
4330   inst.instruction |= inst.operands[i].reg << 16;
4331
4332   assert (!(inst.operands[i].preind && inst.operands[i].postind));
4333
4334   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
4335     {
4336       assert (!inst.operands[i].writeback);
4337       if (!unind_ok)
4338         {
4339           inst.error = _("instruction does not support unindexed addressing");
4340           return FAIL;
4341         }
4342       inst.instruction |= inst.operands[i].imm;
4343       inst.instruction |= INDEX_UP;
4344       return SUCCESS;
4345     }
4346
4347   if (inst.operands[i].preind)
4348     inst.instruction |= PRE_INDEX;
4349
4350   if (inst.operands[i].writeback)
4351     {
4352       if (inst.operands[i].reg == REG_PC)
4353         {
4354           inst.error = _("pc may not be used with write-back");
4355           return FAIL;
4356         }
4357       if (!wb_ok)
4358         {
4359           inst.error = _("instruction does not support writeback");
4360           return FAIL;
4361         }
4362       inst.instruction |= WRITE_BACK;
4363     }
4364
4365   if (reloc_override)
4366     inst.reloc.type = reloc_override;
4367   else if (thumb_mode)
4368     inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
4369   else
4370     inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
4371   return SUCCESS;
4372 }
4373
4374 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
4375    Determine whether it can be performed with a move instruction; if
4376    it can, convert inst.instruction to that move instruction and
4377    return 1; if it can't, convert inst.instruction to a literal-pool
4378    load and return 0.  If this is not a valid thing to do in the
4379    current context, set inst.error and return 1.
4380
4381    inst.operands[i] describes the destination register.  */
4382
4383 static int
4384 move_or_literal_pool (int i, bfd_boolean thumb_p, bfd_boolean mode_3)
4385 {
4386   if ((inst.instruction & (thumb_p ? THUMB_LOAD_BIT : LOAD_BIT)) == 0)
4387     {
4388       inst.error = _("invalid pseudo operation");
4389       return 1;
4390     }
4391   if (inst.reloc.exp.X_op != O_constant && inst.reloc.exp.X_op != O_symbol)
4392     {
4393       inst.error = _("constant expression expected");
4394       return 1;
4395     }
4396   if (inst.reloc.exp.X_op == O_constant)
4397     {
4398       if (thumb_p)
4399         {
4400           if ((inst.reloc.exp.X_add_number & ~0xFF) == 0)
4401             {
4402               /* This can be done with a mov(1) instruction.  */
4403               inst.instruction  = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
4404               inst.instruction |= inst.reloc.exp.X_add_number;
4405               return 1;
4406             }
4407         }
4408       else
4409         {
4410           int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
4411           if (value != FAIL)
4412             {
4413               /* This can be done with a mov instruction.  */
4414               inst.instruction &= LITERAL_MASK;
4415               inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
4416               inst.instruction |= value & 0xfff;
4417               return 1;
4418             }
4419
4420           value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
4421           if (value != FAIL)
4422             {
4423               /* This can be done with a mvn instruction.  */
4424               inst.instruction &= LITERAL_MASK;
4425               inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
4426               inst.instruction |= value & 0xfff;
4427               return 1;
4428             }
4429         }
4430     }
4431
4432   if (add_to_lit_pool () == FAIL)
4433     {
4434       inst.error = _("literal pool insertion failed");
4435       return 1;
4436     }
4437   inst.operands[1].reg = REG_PC;
4438   inst.operands[1].isreg = 1;
4439   inst.operands[1].preind = 1;
4440   inst.reloc.pc_rel = 1;
4441   inst.reloc.type = (thumb_p
4442                      ? BFD_RELOC_ARM_THUMB_OFFSET
4443                      : (mode_3
4444                         ? BFD_RELOC_ARM_HWLITERAL
4445                         : BFD_RELOC_ARM_LITERAL));
4446   return 0;
4447 }
4448
4449 /* Functions for instruction encoding, sorted by subarchitecture.
4450    First some generics; their names are taken from the conventional
4451    bit positions for register arguments in ARM format instructions.  */
4452
4453 static void
4454 do_noargs (void)
4455 {
4456 }
4457
4458 static void
4459 do_rd (void)
4460 {
4461   inst.instruction |= inst.operands[0].reg << 12;
4462 }
4463
4464 static void
4465 do_rd_rm (void)
4466 {
4467   inst.instruction |= inst.operands[0].reg << 12;
4468   inst.instruction |= inst.operands[1].reg;
4469 }
4470
4471 static void
4472 do_rd_rn (void)
4473 {
4474   inst.instruction |= inst.operands[0].reg << 12;
4475   inst.instruction |= inst.operands[1].reg << 16;
4476 }
4477
4478 static void
4479 do_rn_rd (void)
4480 {
4481   inst.instruction |= inst.operands[0].reg << 16;
4482   inst.instruction |= inst.operands[1].reg << 12;
4483 }
4484
4485 static void
4486 do_rd_rm_rn (void)
4487 {
4488   unsigned Rn = inst.operands[2].reg;
4489   /* Enforce resutrictions on SWP instruction.  */
4490   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
4491     constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
4492                 _("Rn must not overlap other operands"));
4493   inst.instruction |= inst.operands[0].reg << 12;
4494   inst.instruction |= inst.operands[1].reg;
4495   inst.instruction |= Rn << 16;
4496 }
4497
4498 static void
4499 do_rd_rn_rm (void)
4500 {
4501   inst.instruction |= inst.operands[0].reg << 12;
4502   inst.instruction |= inst.operands[1].reg << 16;
4503   inst.instruction |= inst.operands[2].reg;
4504 }
4505
4506 static void
4507 do_rm_rd_rn (void)
4508 {
4509   inst.instruction |= inst.operands[0].reg;
4510   inst.instruction |= inst.operands[1].reg << 12;
4511   inst.instruction |= inst.operands[2].reg << 16;
4512 }
4513
4514 static void
4515 do_imm0 (void)
4516 {
4517   inst.instruction |= inst.operands[0].imm;
4518 }
4519
4520 static void
4521 do_rd_cpaddr (void)
4522 {
4523   inst.instruction |= inst.operands[0].reg << 12;
4524   encode_arm_cp_address (1, TRUE, TRUE, 0);
4525 }
4526
4527 /* ARM instructions, in alphabetical order by function name (except
4528    that wrapper functions appear immediately after the function they
4529    wrap).  */
4530
4531 /* This is a pseudo-op of the form "adr rd, label" to be converted
4532    into a relative address of the form "add rd, pc, #label-.-8".  */
4533
4534 static void
4535 do_adr (void)
4536 {
4537   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
4538
4539   /* Frag hacking will turn this into a sub instruction if the offset turns
4540      out to be negative.  */
4541   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
4542   inst.reloc.pc_rel = 1;
4543   inst.reloc.exp.X_add_number -= 8;
4544 }
4545
4546 /* This is a pseudo-op of the form "adrl rd, label" to be converted
4547    into a relative address of the form:
4548    add rd, pc, #low(label-.-8)"
4549    add rd, rd, #high(label-.-8)"  */
4550
4551 static void
4552 do_adrl (void)
4553 {
4554   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
4555
4556   /* Frag hacking will turn this into a sub instruction if the offset turns
4557      out to be negative.  */
4558   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
4559   inst.reloc.pc_rel            = 1;
4560   inst.size                    = INSN_SIZE * 2;
4561   inst.reloc.exp.X_add_number -= 8;
4562 }
4563
4564 static void
4565 do_arit (void)
4566 {
4567   if (!inst.operands[1].present)
4568     inst.operands[1].reg = inst.operands[0].reg;
4569   inst.instruction |= inst.operands[0].reg << 12;
4570   inst.instruction |= inst.operands[1].reg << 16;
4571   encode_arm_shifter_operand (2);
4572 }
4573
4574 static void
4575 do_bfc (void)
4576 {
4577   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
4578   constraint (msb > 32, _("bit-field extends past end of register"));
4579   /* The instruction encoding stores the LSB and MSB,
4580      not the LSB and width.  */
4581   inst.instruction |= inst.operands[0].reg << 12;
4582   inst.instruction |= inst.operands[1].imm << 7;
4583   inst.instruction |= (msb - 1) << 16;
4584 }
4585
4586 static void
4587 do_bfi (void)
4588 {
4589   unsigned int msb;
4590
4591   /* #0 in second position is alternative syntax for bfc, which is
4592      the same instruction but with REG_PC in the Rm field.  */
4593   if (!inst.operands[1].isreg)
4594     inst.operands[1].reg = REG_PC;
4595
4596   msb = inst.operands[2].imm + inst.operands[3].imm;
4597   constraint (msb > 32, _("bit-field extends past end of register"));
4598   /* The instruction encoding stores the LSB and MSB,
4599      not the LSB and width.  */
4600   inst.instruction |= inst.operands[0].reg << 12;
4601   inst.instruction |= inst.operands[1].reg;
4602   inst.instruction |= inst.operands[2].imm << 7;
4603   inst.instruction |= (msb - 1) << 16;
4604 }
4605
4606 static void
4607 do_bfx (void)
4608 {
4609   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
4610               _("bit-field extends past end of register"));
4611   inst.instruction |= inst.operands[0].reg << 12;
4612   inst.instruction |= inst.operands[1].reg;
4613   inst.instruction |= inst.operands[2].imm << 7;
4614   inst.instruction |= (inst.operands[3].imm - 1) << 16;
4615 }
4616
4617 /* ARM V5 breakpoint instruction (argument parse)
4618      BKPT <16 bit unsigned immediate>
4619      Instruction is not conditional.
4620         The bit pattern given in insns[] has the COND_ALWAYS condition,
4621         and it is an error if the caller tried to override that.  */
4622
4623 static void
4624 do_bkpt (void)
4625 {
4626   /* Top 12 of 16 bits to bits 19:8.  */
4627   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
4628
4629   /* Bottom 4 of 16 bits to bits 3:0.  */
4630   inst.instruction |= inst.operands[0].imm & 0xf;
4631 }
4632
4633 static void
4634 encode_branch (int default_reloc)
4635 {
4636   if (inst.operands[0].hasreloc)
4637     {
4638       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32,
4639                   _("the only suffix valid here is '(plt)'"));
4640       inst.reloc.type   = BFD_RELOC_ARM_PLT32;
4641     }
4642   else
4643     {
4644       inst.reloc.type = default_reloc;
4645     }
4646   inst.reloc.pc_rel = 1;
4647 }
4648
4649 static void
4650 do_branch (void)
4651 {
4652 #ifdef OBJ_ELF
4653   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
4654     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
4655   else
4656 #endif
4657     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
4658 }
4659
4660 static void
4661 do_bl (void)
4662 {
4663 #ifdef OBJ_ELF
4664   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
4665     {
4666       if (inst.cond == COND_ALWAYS)
4667         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
4668       else
4669         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
4670     }
4671   else
4672 #endif
4673     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
4674 }
4675
4676 /* ARM V5 branch-link-exchange instruction (argument parse)
4677      BLX <target_addr>          ie BLX(1)
4678      BLX{<condition>} <Rm>      ie BLX(2)
4679    Unfortunately, there are two different opcodes for this mnemonic.
4680    So, the insns[].value is not used, and the code here zaps values
4681         into inst.instruction.
4682    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
4683
4684 static void
4685 do_blx (void)
4686 {
4687   if (inst.operands[0].isreg)
4688     {
4689       /* Arg is a register; the opcode provided by insns[] is correct.
4690          It is not illegal to do "blx pc", just useless.  */
4691       if (inst.operands[0].reg == REG_PC)
4692         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
4693
4694       inst.instruction |= inst.operands[0].reg;
4695     }
4696   else
4697     {
4698       /* Arg is an address; this instruction cannot be executed
4699          conditionally, and the opcode must be adjusted.  */
4700       constraint (inst.cond != COND_ALWAYS, BAD_COND);
4701       inst.instruction = 0xfa000000;
4702 #ifdef OBJ_ELF
4703       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
4704         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
4705       else
4706 #endif
4707         encode_branch (BFD_RELOC_ARM_PCREL_BLX);
4708     }
4709 }
4710
4711 static void
4712 do_bx (void)
4713 {
4714   if (inst.operands[0].reg == REG_PC)
4715     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
4716
4717   inst.instruction |= inst.operands[0].reg;
4718 }
4719
4720
4721 /* ARM v5TEJ.  Jump to Jazelle code.  */
4722
4723 static void
4724 do_bxj (void)
4725 {
4726   if (inst.operands[0].reg == REG_PC)
4727     as_tsktsk (_("use of r15 in bxj is not really useful"));
4728
4729   inst.instruction |= inst.operands[0].reg;
4730 }
4731
4732 /* Co-processor data operation:
4733       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
4734       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
4735 static void
4736 do_cdp (void)
4737 {
4738   inst.instruction |= inst.operands[0].reg << 8;
4739   inst.instruction |= inst.operands[1].imm << 20;
4740   inst.instruction |= inst.operands[2].reg << 12;
4741   inst.instruction |= inst.operands[3].reg << 16;
4742   inst.instruction |= inst.operands[4].reg;
4743   inst.instruction |= inst.operands[5].imm << 5;
4744 }
4745
4746 static void
4747 do_cmp (void)
4748 {
4749   inst.instruction |= inst.operands[0].reg << 16;
4750   encode_arm_shifter_operand (1);
4751 }
4752
4753 /* Transfer between coprocessor and ARM registers.
4754    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
4755    MRC2
4756    MCR{cond}
4757    MCR2
4758
4759    No special properties.  */
4760
4761 static void
4762 do_co_reg (void)
4763 {
4764   inst.instruction |= inst.operands[0].reg << 8;
4765   inst.instruction |= inst.operands[1].imm << 21;
4766   inst.instruction |= inst.operands[2].reg << 12;
4767   inst.instruction |= inst.operands[3].reg << 16;
4768   inst.instruction |= inst.operands[4].reg;
4769   inst.instruction |= inst.operands[5].imm << 5;
4770 }
4771
4772 /* Transfer between coprocessor register and pair of ARM registers.
4773    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
4774    MCRR2
4775    MRRC{cond}
4776    MRRC2
4777
4778    Two XScale instructions are special cases of these:
4779
4780      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
4781      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
4782
4783    Result unpredicatable if Rd or Rn is R15.  */
4784
4785 static void
4786 do_co_reg2c (void)
4787 {
4788   inst.instruction |= inst.operands[0].reg << 8;
4789   inst.instruction |= inst.operands[1].imm << 4;
4790   inst.instruction |= inst.operands[2].reg << 12;
4791   inst.instruction |= inst.operands[3].reg << 16;
4792   inst.instruction |= inst.operands[4].reg;
4793 }
4794
4795 static void
4796 do_cpsi (void)
4797 {
4798   inst.instruction |= inst.operands[0].imm << 6;
4799   inst.instruction |= inst.operands[1].imm;
4800 }
4801
4802 static void
4803 do_it (void)
4804 {
4805   /* There is no IT instruction in ARM mode.  We
4806      process it but do not generate code for it.  */
4807   inst.size = 0;
4808 }
4809
4810 static void
4811 do_ldmstm (void)
4812 {
4813   int base_reg = inst.operands[0].reg;
4814   int range = inst.operands[1].imm;
4815
4816   inst.instruction |= base_reg << 16;
4817   inst.instruction |= range;
4818
4819   if (inst.operands[1].writeback)
4820     inst.instruction |= LDM_TYPE_2_OR_3;
4821
4822   if (inst.operands[0].writeback)
4823     {
4824       inst.instruction |= WRITE_BACK;
4825       /* Check for unpredictable uses of writeback.  */
4826       if (inst.instruction & LOAD_BIT)
4827         {
4828           /* Not allowed in LDM type 2.  */
4829           if ((inst.instruction & LDM_TYPE_2_OR_3)
4830               && ((range & (1 << REG_PC)) == 0))
4831             as_warn (_("writeback of base register is UNPREDICTABLE"));
4832           /* Only allowed if base reg not in list for other types.  */
4833           else if (range & (1 << base_reg))
4834             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
4835         }
4836       else /* STM.  */
4837         {
4838           /* Not allowed for type 2.  */
4839           if (inst.instruction & LDM_TYPE_2_OR_3)
4840             as_warn (_("writeback of base register is UNPREDICTABLE"));
4841           /* Only allowed if base reg not in list, or first in list.  */
4842           else if ((range & (1 << base_reg))
4843                    && (range & ((1 << base_reg) - 1)))
4844             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
4845         }
4846     }
4847 }
4848
4849 /* ARMv5TE load-consecutive (argument parse)
4850    Mode is like LDRH.
4851
4852      LDRccD R, mode
4853      STRccD R, mode.  */
4854
4855 static void
4856 do_ldrd (void)
4857 {
4858   constraint (inst.operands[0].reg % 2 != 0,
4859               _("first destination register must be even"));
4860   constraint (inst.operands[1].present
4861               && inst.operands[1].reg != inst.operands[0].reg + 1,
4862               _("can only load two consecutive registers"));
4863   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
4864   constraint (!inst.operands[2].isreg, _("'[' expected"));
4865
4866   if (!inst.operands[1].present)
4867     inst.operands[1].reg = inst.operands[0].reg + 1;
4868   
4869   if (inst.instruction & LOAD_BIT)
4870     {
4871       /* encode_arm_addr_mode_3 will diagnose overlap between the base
4872          register and the first register written; we have to diagnose
4873          overlap between the base and the second register written here.  */
4874
4875       if (inst.operands[2].reg == inst.operands[1].reg
4876           && (inst.operands[2].writeback || inst.operands[2].postind))
4877         as_warn (_("base register written back, and overlaps "
4878                    "second destination register"));
4879
4880       /* For an index-register load, the index register must not overlap the
4881          destination (even if not write-back).  */
4882       else if (inst.operands[2].immisreg
4883                && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
4884                    || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
4885         as_warn (_("index register overlaps destination register"));
4886     }
4887
4888   inst.instruction |= inst.operands[0].reg << 12;
4889   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
4890 }
4891
4892 static void
4893 do_ldrex (void)
4894 {
4895   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
4896               || inst.operands[1].postind || inst.operands[1].writeback
4897               || inst.operands[1].immisreg || inst.operands[1].shifted
4898               || inst.operands[1].negative
4899               /* This can arise if the programmer has written
4900                    strex rN, rM, foo
4901                  or if they have mistakenly used a register name as the last
4902                  operand,  eg:
4903                    strex rN, rM, rX
4904                  It is very difficult to distinguish between these two cases
4905                  because "rX" might actually be a label. ie the register
4906                  name has been occluded by a symbol of the same name. So we
4907                  just generate a general 'bad addressing mode' type error
4908                  message and leave it up to the programmer to discover the
4909                  true cause and fix their mistake.  */
4910               || (inst.operands[1].reg == REG_PC),
4911               BAD_ADDR_MODE);
4912
4913   constraint (inst.reloc.exp.X_op != O_constant
4914               || inst.reloc.exp.X_add_number != 0,
4915               _("offset must be zero in ARM encoding"));
4916
4917   inst.instruction |= inst.operands[0].reg << 12;
4918   inst.instruction |= inst.operands[1].reg << 16;
4919   inst.reloc.type = BFD_RELOC_UNUSED;
4920 }
4921
4922 static void
4923 do_ldrexd (void)
4924 {
4925   constraint (inst.operands[0].reg % 2 != 0,
4926               _("even register required"));
4927   constraint (inst.operands[1].present
4928               && inst.operands[1].reg != inst.operands[0].reg + 1,
4929               _("can only load two consecutive registers"));
4930   /* If op 1 were present and equal to PC, this function wouldn't
4931      have been called in the first place.  */
4932   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
4933
4934   inst.instruction |= inst.operands[0].reg << 12;
4935   inst.instruction |= inst.operands[2].reg << 16;
4936 }
4937
4938 static void
4939 do_ldst (void)
4940 {
4941   inst.instruction |= inst.operands[0].reg << 12;
4942   if (!inst.operands[1].isreg)
4943     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/FALSE))
4944       return;
4945   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
4946 }
4947
4948 static void
4949 do_ldstt (void)
4950 {
4951   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
4952      reject [Rn,...].  */
4953   if (inst.operands[1].preind)
4954     {
4955       constraint (inst.reloc.exp.X_op != O_constant ||
4956                   inst.reloc.exp.X_add_number != 0,
4957                   _("this instruction requires a post-indexed address"));
4958
4959       inst.operands[1].preind = 0;
4960       inst.operands[1].postind = 1;
4961       inst.operands[1].writeback = 1;
4962     }
4963   inst.instruction |= inst.operands[0].reg << 12;
4964   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
4965 }
4966
4967 /* Halfword and signed-byte load/store operations.  */
4968
4969 static void
4970 do_ldstv4 (void)
4971 {
4972   inst.instruction |= inst.operands[0].reg << 12;
4973   if (!inst.operands[1].isreg)
4974     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/TRUE))
4975       return;
4976   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
4977 }
4978
4979 static void
4980 do_ldsttv4 (void)
4981 {
4982   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
4983      reject [Rn,...].  */
4984   if (inst.operands[1].preind)
4985     {
4986       constraint (inst.reloc.exp.X_op != O_constant ||
4987                   inst.reloc.exp.X_add_number != 0,
4988                   _("this instruction requires a post-indexed address"));
4989
4990       inst.operands[1].preind = 0;
4991       inst.operands[1].postind = 1;
4992       inst.operands[1].writeback = 1;
4993     }
4994   inst.instruction |= inst.operands[0].reg << 12;
4995   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
4996 }
4997
4998 /* Co-processor register load/store.
4999    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
5000 static void
5001 do_lstc (void)
5002 {
5003   inst.instruction |= inst.operands[0].reg << 8;
5004   inst.instruction |= inst.operands[1].reg << 12;
5005   encode_arm_cp_address (2, TRUE, TRUE, 0);
5006 }
5007
5008 static void
5009 do_mlas (void)
5010 {
5011   /* This restriction does not apply to mls (nor to mla in v6, but
5012      that's hard to detect at present).  */
5013   if (inst.operands[0].reg == inst.operands[1].reg
5014       && !(inst.instruction & 0x00400000))
5015     as_tsktsk (_("rd and rm should be different in mla"));
5016
5017   inst.instruction |= inst.operands[0].reg << 16;
5018   inst.instruction |= inst.operands[1].reg;
5019   inst.instruction |= inst.operands[2].reg << 8;
5020   inst.instruction |= inst.operands[3].reg << 12;
5021
5022 }
5023
5024 static void
5025 do_mov (void)
5026 {
5027   inst.instruction |= inst.operands[0].reg << 12;
5028   encode_arm_shifter_operand (1);
5029 }
5030
5031 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
5032 static void
5033 do_mov16 (void)
5034 {
5035   inst.instruction |= inst.operands[0].reg << 12;
5036   /* The value is in two pieces: 0:11, 16:19.  */
5037   inst.instruction |= (inst.operands[1].imm & 0x00000fff);
5038   inst.instruction |= (inst.operands[1].imm & 0x0000f000) << 4;
5039 }
5040
5041 static void
5042 do_mrs (void)
5043 {
5044   /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
5045   constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
5046               != (PSR_c|PSR_f),
5047               _("'CPSR' or 'SPSR' expected"));
5048   inst.instruction |= inst.operands[0].reg << 12;
5049   inst.instruction |= (inst.operands[1].imm & SPSR_BIT);
5050 }
5051
5052 /* Two possible forms:
5053       "{C|S}PSR_<field>, Rm",
5054       "{C|S}PSR_f, #expression".  */
5055
5056 static void
5057 do_msr (void)
5058 {
5059   inst.instruction |= inst.operands[0].imm;
5060   if (inst.operands[1].isreg)
5061     inst.instruction |= inst.operands[1].reg;
5062   else
5063     {
5064       inst.instruction |= INST_IMMEDIATE;
5065       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5066       inst.reloc.pc_rel = 0;
5067     }
5068 }
5069
5070 static void
5071 do_mul (void)
5072 {
5073   if (!inst.operands[2].present)
5074     inst.operands[2].reg = inst.operands[0].reg;
5075   inst.instruction |= inst.operands[0].reg << 16;
5076   inst.instruction |= inst.operands[1].reg;
5077   inst.instruction |= inst.operands[2].reg << 8;
5078
5079   if (inst.operands[0].reg == inst.operands[1].reg)
5080     as_tsktsk (_("rd and rm should be different in mul"));
5081 }
5082
5083 /* Long Multiply Parser
5084    UMULL RdLo, RdHi, Rm, Rs
5085    SMULL RdLo, RdHi, Rm, Rs
5086    UMLAL RdLo, RdHi, Rm, Rs
5087    SMLAL RdLo, RdHi, Rm, Rs.  */
5088
5089 static void
5090 do_mull (void)
5091 {
5092   inst.instruction |= inst.operands[0].reg << 12;
5093   inst.instruction |= inst.operands[1].reg << 16;
5094   inst.instruction |= inst.operands[2].reg;
5095   inst.instruction |= inst.operands[3].reg << 8;
5096
5097   /* rdhi, rdlo and rm must all be different.  */
5098   if (inst.operands[0].reg == inst.operands[1].reg
5099       || inst.operands[0].reg == inst.operands[2].reg
5100       || inst.operands[1].reg == inst.operands[2].reg)
5101     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
5102 }
5103
5104 static void
5105 do_nop (void)
5106 {
5107   if (inst.operands[0].present)
5108     {
5109       /* Architectural NOP hints are CPSR sets with no bits selected.  */
5110       inst.instruction &= 0xf0000000;
5111       inst.instruction |= 0x0320f000 + inst.operands[0].imm;
5112     }
5113 }
5114
5115 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
5116    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
5117    Condition defaults to COND_ALWAYS.
5118    Error if Rd, Rn or Rm are R15.  */
5119
5120 static void
5121 do_pkhbt (void)
5122 {
5123   inst.instruction |= inst.operands[0].reg << 12;
5124   inst.instruction |= inst.operands[1].reg << 16;
5125   inst.instruction |= inst.operands[2].reg;
5126   if (inst.operands[3].present)
5127     encode_arm_shift (3);
5128 }
5129
5130 /* ARM V6 PKHTB (Argument Parse).  */
5131
5132 static void
5133 do_pkhtb (void)
5134 {
5135   if (!inst.operands[3].present)
5136     {
5137       /* If the shift specifier is omitted, turn the instruction
5138          into pkhbt rd, rm, rn. */
5139       inst.instruction &= 0xfff00010;
5140       inst.instruction |= inst.operands[0].reg << 12;
5141       inst.instruction |= inst.operands[1].reg;
5142       inst.instruction |= inst.operands[2].reg << 16;
5143     }
5144   else
5145     {
5146       inst.instruction |= inst.operands[0].reg << 12;
5147       inst.instruction |= inst.operands[1].reg << 16;
5148       inst.instruction |= inst.operands[2].reg;
5149       encode_arm_shift (3);
5150     }
5151 }
5152
5153 /* ARMv5TE: Preload-Cache
5154
5155     PLD <addr_mode>
5156
5157   Syntactically, like LDR with B=1, W=0, L=1.  */
5158
5159 static void
5160 do_pld (void)
5161 {
5162   constraint (!inst.operands[0].isreg,
5163               _("'[' expected after PLD mnemonic"));
5164   constraint (inst.operands[0].postind,
5165               _("post-indexed expression used in preload instruction"));
5166   constraint (inst.operands[0].writeback,
5167               _("writeback used in preload instruction"));
5168   constraint (!inst.operands[0].preind,
5169               _("unindexed addressing used in preload instruction"));
5170   inst.instruction |= inst.operands[0].reg;
5171   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
5172 }
5173
5174 static void
5175 do_push_pop (void)
5176 {
5177   inst.operands[1] = inst.operands[0];
5178   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
5179   inst.operands[0].isreg = 1;
5180   inst.operands[0].writeback = 1;
5181   inst.operands[0].reg = REG_SP;
5182   do_ldmstm ();
5183 }
5184
5185 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
5186    word at the specified address and the following word
5187    respectively.
5188    Unconditionally executed.
5189    Error if Rn is R15.  */
5190
5191 static void
5192 do_rfe (void)
5193 {
5194   inst.instruction |= inst.operands[0].reg << 16;
5195   if (inst.operands[0].writeback)
5196     inst.instruction |= WRITE_BACK;
5197 }
5198
5199 /* ARM V6 ssat (argument parse).  */
5200
5201 static void
5202 do_ssat (void)
5203 {
5204   inst.instruction |= inst.operands[0].reg << 12;
5205   inst.instruction |= (inst.operands[1].imm - 1) << 16;
5206   inst.instruction |= inst.operands[2].reg;
5207
5208   if (inst.operands[3].present)
5209     encode_arm_shift (3);
5210 }
5211
5212 /* ARM V6 usat (argument parse).  */
5213
5214 static void
5215 do_usat (void)
5216 {
5217   inst.instruction |= inst.operands[0].reg << 12;
5218   inst.instruction |= inst.operands[1].imm << 16;
5219   inst.instruction |= inst.operands[2].reg;
5220
5221   if (inst.operands[3].present)
5222     encode_arm_shift (3);
5223 }
5224
5225 /* ARM V6 ssat16 (argument parse).  */
5226
5227 static void
5228 do_ssat16 (void)
5229 {
5230   inst.instruction |= inst.operands[0].reg << 12;
5231   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
5232   inst.instruction |= inst.operands[2].reg;
5233 }
5234
5235 static void
5236 do_usat16 (void)
5237 {
5238   inst.instruction |= inst.operands[0].reg << 12;
5239   inst.instruction |= inst.operands[1].imm << 16;
5240   inst.instruction |= inst.operands[2].reg;
5241 }
5242
5243 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
5244    preserving the other bits.
5245
5246    setend <endian_specifier>, where <endian_specifier> is either
5247    BE or LE.  */
5248
5249 static void
5250 do_setend (void)
5251 {
5252   if (inst.operands[0].imm)
5253     inst.instruction |= 0x200;
5254 }
5255
5256 static void
5257 do_shift (void)
5258 {
5259   unsigned int Rm = (inst.operands[1].present
5260                      ? inst.operands[1].reg
5261                      : inst.operands[0].reg);
5262
5263   inst.instruction |= inst.operands[0].reg << 12;
5264   inst.instruction |= Rm;
5265   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
5266     {
5267       constraint (inst.operands[0].reg != Rm,
5268                   _("source1 and dest must be same register"));
5269       inst.instruction |= inst.operands[2].reg << 8;
5270       inst.instruction |= SHIFT_BY_REG;
5271     }
5272   else
5273     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
5274 }
5275
5276 static void
5277 do_smc (void)
5278 {
5279   inst.reloc.type = BFD_RELOC_ARM_SMC;
5280   inst.reloc.pc_rel = 0;
5281 }
5282
5283 static void
5284 do_swi (void)
5285 {
5286   inst.reloc.type = BFD_RELOC_ARM_SWI;
5287   inst.reloc.pc_rel = 0;
5288 }
5289
5290 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
5291    SMLAxy{cond} Rd,Rm,Rs,Rn
5292    SMLAWy{cond} Rd,Rm,Rs,Rn
5293    Error if any register is R15.  */
5294
5295 static void
5296 do_smla (void)
5297 {
5298   inst.instruction |= inst.operands[0].reg << 16;
5299   inst.instruction |= inst.operands[1].reg;
5300   inst.instruction |= inst.operands[2].reg << 8;
5301   inst.instruction |= inst.operands[3].reg << 12;
5302 }
5303
5304 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
5305    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
5306    Error if any register is R15.
5307    Warning if Rdlo == Rdhi.  */
5308
5309 static void
5310 do_smlal (void)
5311 {
5312   inst.instruction |= inst.operands[0].reg << 12;
5313   inst.instruction |= inst.operands[1].reg << 16;
5314   inst.instruction |= inst.operands[2].reg;
5315   inst.instruction |= inst.operands[3].reg << 8;
5316
5317   if (inst.operands[0].reg == inst.operands[1].reg)
5318     as_tsktsk (_("rdhi and rdlo must be different"));
5319 }
5320
5321 /* ARM V5E (El Segundo) signed-multiply (argument parse)
5322    SMULxy{cond} Rd,Rm,Rs
5323    Error if any register is R15.  */
5324
5325 static void
5326 do_smul (void)
5327 {
5328   inst.instruction |= inst.operands[0].reg << 16;
5329   inst.instruction |= inst.operands[1].reg;
5330   inst.instruction |= inst.operands[2].reg << 8;
5331 }
5332
5333 /* ARM V6 srs (argument parse).  */
5334
5335 static void
5336 do_srs (void)
5337 {
5338   inst.instruction |= inst.operands[0].imm;
5339   if (inst.operands[0].writeback)
5340     inst.instruction |= WRITE_BACK;
5341 }
5342
5343 /* ARM V6 strex (argument parse).  */
5344
5345 static void
5346 do_strex (void)
5347 {
5348   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
5349               || inst.operands[2].postind || inst.operands[2].writeback
5350               || inst.operands[2].immisreg || inst.operands[2].shifted
5351               || inst.operands[2].negative
5352               /* See comment in do_ldrex().  */
5353               || (inst.operands[2].reg == REG_PC),
5354               BAD_ADDR_MODE);
5355
5356   constraint (inst.operands[0].reg == inst.operands[1].reg
5357               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
5358
5359   constraint (inst.reloc.exp.X_op != O_constant
5360               || inst.reloc.exp.X_add_number != 0,
5361               _("offset must be zero in ARM encoding"));
5362
5363   inst.instruction |= inst.operands[0].reg << 12;
5364   inst.instruction |= inst.operands[1].reg;
5365   inst.instruction |= inst.operands[2].reg << 16;
5366   inst.reloc.type = BFD_RELOC_UNUSED;
5367 }
5368
5369 static void
5370 do_strexd (void)
5371 {
5372   constraint (inst.operands[1].reg % 2 != 0,
5373               _("even register required"));
5374   constraint (inst.operands[2].present
5375               && inst.operands[2].reg != inst.operands[1].reg + 1,
5376               _("can only store two consecutive registers"));
5377   /* If op 2 were present and equal to PC, this function wouldn't
5378      have been called in the first place.  */
5379   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
5380
5381   constraint (inst.operands[0].reg == inst.operands[1].reg
5382               || inst.operands[0].reg == inst.operands[1].reg + 1
5383               || inst.operands[0].reg == inst.operands[3].reg,
5384               BAD_OVERLAP);
5385
5386   inst.instruction |= inst.operands[0].reg << 12;
5387   inst.instruction |= inst.operands[1].reg;
5388   inst.instruction |= inst.operands[3].reg << 16;
5389 }
5390
5391 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
5392    extends it to 32-bits, and adds the result to a value in another
5393    register.  You can specify a rotation by 0, 8, 16, or 24 bits
5394    before extracting the 16-bit value.
5395    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
5396    Condition defaults to COND_ALWAYS.
5397    Error if any register uses R15.  */
5398
5399 static void
5400 do_sxtah (void)
5401 {
5402   inst.instruction |= inst.operands[0].reg << 12;
5403   inst.instruction |= inst.operands[1].reg << 16;
5404   inst.instruction |= inst.operands[2].reg;
5405   inst.instruction |= inst.operands[3].imm << 10;
5406 }
5407
5408 /* ARM V6 SXTH.
5409
5410    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
5411    Condition defaults to COND_ALWAYS.
5412    Error if any register uses R15.  */
5413
5414 static void
5415 do_sxth (void)
5416 {
5417   inst.instruction |= inst.operands[0].reg << 12;
5418   inst.instruction |= inst.operands[1].reg;
5419   inst.instruction |= inst.operands[2].imm << 10;
5420 }
5421 \f
5422 /* VFP instructions.  In a logical order: SP variant first, monad
5423    before dyad, arithmetic then move then load/store.  */
5424
5425 static void
5426 do_vfp_sp_monadic (void)
5427 {
5428   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5429   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sm);
5430 }
5431
5432 static void
5433 do_vfp_sp_dyadic (void)
5434 {
5435   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5436   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sn);
5437   encode_arm_vfp_sp_reg (inst.operands[2].reg, VFP_REG_Sm);
5438 }
5439
5440 static void
5441 do_vfp_sp_compare_z (void)
5442 {
5443   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5444 }
5445
5446 static void
5447 do_vfp_dp_sp_cvt (void)
5448 {
5449   inst.instruction |= inst.operands[0].reg << 12;
5450   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sm);
5451 }
5452
5453 static void
5454 do_vfp_sp_dp_cvt (void)
5455 {
5456   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5457   inst.instruction |= inst.operands[1].reg;
5458 }
5459
5460 static void
5461 do_vfp_reg_from_sp (void)
5462 {
5463   inst.instruction |= inst.operands[0].reg << 12;
5464   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sn);
5465 }
5466
5467 static void
5468 do_vfp_reg2_from_sp2 (void)
5469 {
5470   constraint (inst.operands[2].imm != 2,
5471               _("only two consecutive VFP SP registers allowed here"));
5472   inst.instruction |= inst.operands[0].reg << 12;
5473   inst.instruction |= inst.operands[1].reg << 16;
5474   encode_arm_vfp_sp_reg (inst.operands[2].reg, VFP_REG_Sm);
5475 }
5476
5477 static void
5478 do_vfp_sp_from_reg (void)
5479 {
5480   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sn);
5481   inst.instruction |= inst.operands[1].reg << 12;
5482 }
5483
5484 static void
5485 do_vfp_sp2_from_reg2 (void)
5486 {
5487   constraint (inst.operands[0].imm != 2,
5488               _("only two consecutive VFP SP registers allowed here"));
5489   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sm);
5490   inst.instruction |= inst.operands[1].reg << 12;
5491   inst.instruction |= inst.operands[2].reg << 16;
5492 }
5493
5494 static void
5495 do_vfp_sp_ldst (void)
5496 {
5497   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5498   encode_arm_cp_address (1, FALSE, TRUE, 0);
5499 }
5500
5501 static void
5502 do_vfp_dp_ldst (void)
5503 {
5504   inst.instruction |= inst.operands[0].reg << 12;
5505   encode_arm_cp_address (1, FALSE, TRUE, 0);
5506 }
5507
5508
5509 static void
5510 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
5511 {
5512   if (inst.operands[0].writeback)
5513     inst.instruction |= WRITE_BACK;
5514   else
5515     constraint (ldstm_type != VFP_LDSTMIA,
5516                 _("this addressing mode requires base-register writeback"));
5517   inst.instruction |= inst.operands[0].reg << 16;
5518   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sd);
5519   inst.instruction |= inst.operands[1].imm;
5520 }
5521
5522 static void
5523 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
5524 {
5525   int count;
5526
5527   if (inst.operands[0].writeback)
5528     inst.instruction |= WRITE_BACK;
5529   else
5530     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
5531                 _("this addressing mode requires base-register writeback"));
5532
5533   inst.instruction |= inst.operands[0].reg << 16;
5534   inst.instruction |= inst.operands[1].reg << 12;
5535
5536   count = inst.operands[1].imm << 1;
5537   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
5538     count += 1;
5539
5540   inst.instruction |= count;
5541 }
5542
5543 static void
5544 do_vfp_sp_ldstmia (void)
5545 {
5546   vfp_sp_ldstm (VFP_LDSTMIA);
5547 }
5548
5549 static void
5550 do_vfp_sp_ldstmdb (void)
5551 {
5552   vfp_sp_ldstm (VFP_LDSTMDB);
5553 }
5554
5555 static void
5556 do_vfp_dp_ldstmia (void)
5557 {
5558   vfp_dp_ldstm (VFP_LDSTMIA);
5559 }
5560
5561 static void
5562 do_vfp_dp_ldstmdb (void)
5563 {
5564   vfp_dp_ldstm (VFP_LDSTMDB);
5565 }
5566
5567 static void
5568 do_vfp_xp_ldstmia (void)
5569 {
5570   vfp_dp_ldstm (VFP_LDSTMIAX);
5571 }
5572
5573 static void
5574 do_vfp_xp_ldstmdb (void)
5575 {
5576   vfp_dp_ldstm (VFP_LDSTMDBX);
5577 }
5578 \f
5579 /* FPA instructions.  Also in a logical order.  */
5580
5581 static void
5582 do_fpa_cmp (void)
5583 {
5584   inst.instruction |= inst.operands[0].reg << 16;
5585   inst.instruction |= inst.operands[1].reg;
5586 }
5587
5588 static void
5589 do_fpa_ldmstm (void)
5590 {
5591   inst.instruction |= inst.operands[0].reg << 12;
5592   switch (inst.operands[1].imm)
5593     {
5594     case 1: inst.instruction |= CP_T_X;          break;
5595     case 2: inst.instruction |= CP_T_Y;          break;
5596     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
5597     case 4:                                      break;
5598     default: abort ();
5599     }
5600
5601   if (inst.instruction & (PRE_INDEX | INDEX_UP))
5602     {
5603       /* The instruction specified "ea" or "fd", so we can only accept
5604          [Rn]{!}.  The instruction does not really support stacking or
5605          unstacking, so we have to emulate these by setting appropriate
5606          bits and offsets.  */
5607       constraint (inst.reloc.exp.X_op != O_constant
5608                   || inst.reloc.exp.X_add_number != 0,
5609                   _("this instruction does not support indexing"));
5610
5611       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
5612         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
5613
5614       if (!(inst.instruction & INDEX_UP))
5615         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
5616
5617       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
5618         {
5619           inst.operands[2].preind = 0;
5620           inst.operands[2].postind = 1;
5621         }
5622     }
5623
5624   encode_arm_cp_address (2, TRUE, TRUE, 0);
5625 }
5626 \f
5627 /* iWMMXt instructions: strictly in alphabetical order.  */
5628
5629 static void
5630 do_iwmmxt_tandorc (void)
5631 {
5632   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
5633 }
5634
5635 static void
5636 do_iwmmxt_textrc (void)
5637 {
5638   inst.instruction |= inst.operands[0].reg << 12;
5639   inst.instruction |= inst.operands[1].imm;
5640 }
5641
5642 static void
5643 do_iwmmxt_textrm (void)
5644 {
5645   inst.instruction |= inst.operands[0].reg << 12;
5646   inst.instruction |= inst.operands[1].reg << 16;
5647   inst.instruction |= inst.operands[2].imm;
5648 }
5649
5650 static void
5651 do_iwmmxt_tinsr (void)
5652 {
5653   inst.instruction |= inst.operands[0].reg << 16;
5654   inst.instruction |= inst.operands[1].reg << 12;
5655   inst.instruction |= inst.operands[2].imm;
5656 }
5657
5658 static void
5659 do_iwmmxt_tmia (void)
5660 {
5661   inst.instruction |= inst.operands[0].reg << 5;
5662   inst.instruction |= inst.operands[1].reg;
5663   inst.instruction |= inst.operands[2].reg << 12;
5664 }
5665
5666 static void
5667 do_iwmmxt_waligni (void)
5668 {
5669   inst.instruction |= inst.operands[0].reg << 12;
5670   inst.instruction |= inst.operands[1].reg << 16;
5671   inst.instruction |= inst.operands[2].reg;
5672   inst.instruction |= inst.operands[3].imm << 20;
5673 }
5674
5675 static void
5676 do_iwmmxt_wmov (void)
5677 {
5678   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
5679   inst.instruction |= inst.operands[0].reg << 12;
5680   inst.instruction |= inst.operands[1].reg << 16;
5681   inst.instruction |= inst.operands[1].reg;
5682 }
5683
5684 static void
5685 do_iwmmxt_wldstbh (void)
5686 {
5687   int reloc;
5688   inst.instruction |= inst.operands[0].reg << 12;
5689   inst.reloc.exp.X_add_number *= 4;
5690   if (thumb_mode)
5691     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
5692   else
5693     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
5694   encode_arm_cp_address (1, TRUE, FALSE, reloc);
5695 }
5696
5697 static void
5698 do_iwmmxt_wldstw (void)
5699 {
5700   /* RIWR_RIWC clears .isreg for a control register.  */
5701   if (!inst.operands[0].isreg)
5702     {
5703       constraint (inst.cond != COND_ALWAYS, BAD_COND);
5704       inst.instruction |= 0xf0000000;
5705     }
5706
5707   inst.instruction |= inst.operands[0].reg << 12;
5708   encode_arm_cp_address (1, TRUE, TRUE, 0);
5709 }
5710
5711 static void
5712 do_iwmmxt_wldstd (void)
5713 {
5714   inst.instruction |= inst.operands[0].reg << 12;
5715   encode_arm_cp_address (1, TRUE, FALSE, 0);
5716 }
5717
5718 static void
5719 do_iwmmxt_wshufh (void)
5720 {
5721   inst.instruction |= inst.operands[0].reg << 12;
5722   inst.instruction |= inst.operands[1].reg << 16;
5723   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
5724   inst.instruction |= (inst.operands[2].imm & 0x0f);
5725 }
5726
5727 static void
5728 do_iwmmxt_wzero (void)
5729 {
5730   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
5731   inst.instruction |= inst.operands[0].reg;
5732   inst.instruction |= inst.operands[0].reg << 12;
5733   inst.instruction |= inst.operands[0].reg << 16;
5734 }
5735 \f
5736 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
5737    operations first, then control, shift, and load/store.  */
5738
5739 /* Insns like "foo X,Y,Z".  */
5740
5741 static void
5742 do_mav_triple (void)
5743 {
5744   inst.instruction |= inst.operands[0].reg << 16;
5745   inst.instruction |= inst.operands[1].reg;
5746   inst.instruction |= inst.operands[2].reg << 12;
5747 }
5748
5749 /* Insns like "foo W,X,Y,Z".
5750     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
5751
5752 static void
5753 do_mav_quad (void)
5754 {
5755   inst.instruction |= inst.operands[0].reg << 5;
5756   inst.instruction |= inst.operands[1].reg << 12;
5757   inst.instruction |= inst.operands[2].reg << 16;
5758   inst.instruction |= inst.operands[3].reg;
5759 }
5760
5761 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
5762 static void
5763 do_mav_dspsc (void)
5764 {
5765   inst.instruction |= inst.operands[1].reg << 12;
5766 }
5767
5768 /* Maverick shift immediate instructions.
5769    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
5770    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
5771
5772 static void
5773 do_mav_shift (void)
5774 {
5775   int imm = inst.operands[2].imm;
5776
5777   inst.instruction |= inst.operands[0].reg << 12;
5778   inst.instruction |= inst.operands[1].reg << 16;
5779
5780   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
5781      Bits 5-7 of the insn should have bits 4-6 of the immediate.
5782      Bit 4 should be 0.  */
5783   imm = (imm & 0xf) | ((imm & 0x70) << 1);
5784
5785   inst.instruction |= imm;
5786 }
5787 \f
5788 /* XScale instructions.  Also sorted arithmetic before move.  */
5789
5790 /* Xscale multiply-accumulate (argument parse)
5791      MIAcc   acc0,Rm,Rs
5792      MIAPHcc acc0,Rm,Rs
5793      MIAxycc acc0,Rm,Rs.  */
5794
5795 static void
5796 do_xsc_mia (void)
5797 {
5798   inst.instruction |= inst.operands[1].reg;
5799   inst.instruction |= inst.operands[2].reg << 12;
5800 }
5801
5802 /* Xscale move-accumulator-register (argument parse)
5803
5804      MARcc   acc0,RdLo,RdHi.  */
5805
5806 static void
5807 do_xsc_mar (void)
5808 {
5809   inst.instruction |= inst.operands[1].reg << 12;
5810   inst.instruction |= inst.operands[2].reg << 16;
5811 }
5812
5813 /* Xscale move-register-accumulator (argument parse)
5814
5815      MRAcc   RdLo,RdHi,acc0.  */
5816
5817 static void
5818 do_xsc_mra (void)
5819 {
5820   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
5821   inst.instruction |= inst.operands[0].reg << 12;
5822   inst.instruction |= inst.operands[1].reg << 16;
5823 }
5824 \f
5825 /* Encoding functions relevant only to Thumb.  */
5826
5827 /* inst.operands[i] is a shifted-register operand; encode
5828    it into inst.instruction in the format used by Thumb32.  */
5829
5830 static void
5831 encode_thumb32_shifted_operand (int i)
5832 {
5833   unsigned int value = inst.reloc.exp.X_add_number;
5834   unsigned int shift = inst.operands[i].shift_kind;
5835
5836   constraint (inst.operands[i].immisreg,
5837               _("shift by register not allowed in thumb mode"));
5838   inst.instruction |= inst.operands[i].reg;
5839   if (shift == SHIFT_RRX)
5840     inst.instruction |= SHIFT_ROR << 4;
5841   else
5842     {
5843       constraint (inst.reloc.exp.X_op != O_constant,
5844                   _("expression too complex"));
5845
5846       constraint (value > 32
5847                   || (value == 32 && (shift == SHIFT_LSL
5848                                       || shift == SHIFT_ROR)),
5849                   _("shift expression is too large"));
5850
5851       if (value == 0)
5852         shift = SHIFT_LSL;
5853       else if (value == 32)
5854         value = 0;
5855
5856       inst.instruction |= shift << 4;
5857       inst.instruction |= (value & 0x1c) << 10;
5858       inst.instruction |= (value & 0x03) << 6;
5859     }
5860 }
5861
5862
5863 /* inst.operands[i] was set up by parse_address.  Encode it into a
5864    Thumb32 format load or store instruction.  Reject forms that cannot
5865    be used with such instructions.  If is_t is true, reject forms that
5866    cannot be used with a T instruction; if is_d is true, reject forms
5867    that cannot be used with a D instruction.  */
5868
5869 static void
5870 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
5871 {
5872   bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
5873
5874   constraint (!inst.operands[i].isreg,
5875               _("Thumb does not support the ldr =N pseudo-operation"));
5876
5877   inst.instruction |= inst.operands[i].reg << 16;
5878   if (inst.operands[i].immisreg)
5879     {
5880       constraint (is_pc, _("cannot use register index with PC-relative addressing"));
5881       constraint (is_t || is_d, _("cannot use register index with this instruction"));
5882       constraint (inst.operands[i].negative,
5883                   _("Thumb does not support negative register indexing"));
5884       constraint (inst.operands[i].postind,
5885                   _("Thumb does not support register post-indexing"));
5886       constraint (inst.operands[i].writeback,
5887                   _("Thumb does not support register indexing with writeback"));
5888       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
5889                   _("Thumb supports only LSL in shifted register indexing"));
5890
5891       inst.instruction |= inst.operands[1].imm;
5892       if (inst.operands[i].shifted)
5893         {
5894           constraint (inst.reloc.exp.X_op != O_constant,
5895                       _("expression too complex"));
5896           constraint (inst.reloc.exp.X_add_number < 0
5897                       || inst.reloc.exp.X_add_number > 3,
5898                       _("shift out of range"));
5899           inst.instruction |= inst.reloc.exp.X_add_number << 4;
5900         }
5901       inst.reloc.type = BFD_RELOC_UNUSED;
5902     }
5903   else if (inst.operands[i].preind)
5904     {
5905       constraint (is_pc && inst.operands[i].writeback,
5906                   _("cannot use writeback with PC-relative addressing"));
5907       constraint (is_t && inst.operands[1].writeback,
5908                   _("cannot use writeback with this instruction"));
5909
5910       if (is_d)
5911         {
5912           inst.instruction |= 0x01000000;
5913           if (inst.operands[i].writeback)
5914             inst.instruction |= 0x00200000;
5915         }
5916       else
5917         {
5918           inst.instruction |= 0x00000c00;
5919           if (inst.operands[i].writeback)
5920             inst.instruction |= 0x00000100;
5921         }
5922       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
5923     }
5924   else if (inst.operands[i].postind)
5925     {
5926       assert (inst.operands[i].writeback);
5927       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
5928       constraint (is_t, _("cannot use post-indexing with this instruction"));
5929
5930       if (is_d)
5931         inst.instruction |= 0x00200000;
5932       else
5933         inst.instruction |= 0x00000900;
5934       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
5935     }
5936   else /* unindexed - only for coprocessor */
5937     inst.error = _("instruction does not accept unindexed addressing");
5938 }
5939
5940 /* Table of Thumb instructions which exist in both 16- and 32-bit
5941    encodings (the latter only in post-V6T2 cores).  The index is the
5942    value used in the insns table below.  When there is more than one
5943    possible 16-bit encoding for the instruction, this table always
5944    holds variant (1).
5945    Also contains several pseudo-instructions used during relaxation.  */
5946 #define T16_32_TAB                              \
5947   X(adc,   4140, eb400000),                     \
5948   X(adcs,  4140, eb500000),                     \
5949   X(add,   1c00, eb000000),                     \
5950   X(adds,  1c00, eb100000),                     \
5951   X(addi,  0000, f1000000),                     \
5952   X(addis, 0000, f1100000),                     \
5953   X(add_pc,000f, f20f0000),                     \
5954   X(add_sp,000d, f10d0000),                     \
5955   X(adr,   000f, f20f0000),                     \
5956   X(and,   4000, ea000000),                     \
5957   X(ands,  4000, ea100000),                     \
5958   X(asr,   1000, fa40f000),                     \
5959   X(asrs,  1000, fa50f000),                     \
5960   X(b,     e000, f000b000),                     \
5961   X(bcond, d000, f0008000),                     \
5962   X(bic,   4380, ea200000),                     \
5963   X(bics,  4380, ea300000),                     \
5964   X(cmn,   42c0, eb100f00),                     \
5965   X(cmp,   2800, ebb00f00),                     \
5966   X(cpsie, b660, f3af8400),                     \
5967   X(cpsid, b670, f3af8600),                     \
5968   X(cpy,   4600, ea4f0000),                     \
5969   X(dec_sp,80dd, f1bd0d00),                     \
5970   X(eor,   4040, ea800000),                     \
5971   X(eors,  4040, ea900000),                     \
5972   X(inc_sp,00dd, f10d0d00),                     \
5973   X(ldmia, c800, e8900000),                     \
5974   X(ldr,   6800, f8500000),                     \
5975   X(ldrb,  7800, f8100000),                     \
5976   X(ldrh,  8800, f8300000),                     \
5977   X(ldrsb, 5600, f9100000),                     \
5978   X(ldrsh, 5e00, f9300000),                     \
5979   X(ldr_pc,4800, f85f0000),                     \
5980   X(ldr_pc2,4800, f85f0000),                    \
5981   X(ldr_sp,9800, f85d0000),                     \
5982   X(lsl,   0000, fa00f000),                     \
5983   X(lsls,  0000, fa10f000),                     \
5984   X(lsr,   0800, fa20f000),                     \
5985   X(lsrs,  0800, fa30f000),                     \
5986   X(mov,   2000, ea4f0000),                     \
5987   X(movs,  2000, ea5f0000),                     \
5988   X(mul,   4340, fb00f000),                     \
5989   X(muls,  4340, ffffffff), /* no 32b muls */   \
5990   X(mvn,   43c0, ea6f0000),                     \
5991   X(mvns,  43c0, ea7f0000),                     \
5992   X(neg,   4240, f1c00000), /* rsb #0 */        \
5993   X(negs,  4240, f1d00000), /* rsbs #0 */       \
5994   X(orr,   4300, ea400000),                     \
5995   X(orrs,  4300, ea500000),                     \
5996   X(pop,   bc00, e8bd0000), /* ldmia sp!,... */ \
5997   X(push,  b400, e92d0000), /* stmdb sp!,... */ \
5998   X(rev,   ba00, fa90f080),                     \
5999   X(rev16, ba40, fa90f090),                     \
6000   X(revsh, bac0, fa90f0b0),                     \
6001   X(ror,   41c0, fa60f000),                     \
6002   X(rors,  41c0, fa70f000),                     \
6003   X(sbc,   4180, eb600000),                     \
6004   X(sbcs,  4180, eb700000),                     \
6005   X(stmia, c000, e8800000),                     \
6006   X(str,   6000, f8400000),                     \
6007   X(strb,  7000, f8000000),                     \
6008   X(strh,  8000, f8200000),                     \
6009   X(str_sp,9000, f84d0000),                     \
6010   X(sub,   1e00, eba00000),                     \
6011   X(subs,  1e00, ebb00000),                     \
6012   X(subi,  8000, f1a00000),                     \
6013   X(subis, 8000, f1b00000),                     \
6014   X(sxtb,  b240, fa4ff080),                     \
6015   X(sxth,  b200, fa0ff080),                     \
6016   X(tst,   4200, ea100f00),                     \
6017   X(uxtb,  b2c0, fa5ff080),                     \
6018   X(uxth,  b280, fa1ff080),                     \
6019   X(nop,   bf00, f3af8000),                     \
6020   X(yield, bf10, f3af8001),                     \
6021   X(wfe,   bf20, f3af8002),                     \
6022   X(wfi,   bf30, f3af8003),                     \
6023   X(sev,   bf40, f3af9004), /* typo, 8004? */
6024
6025 /* To catch errors in encoding functions, the codes are all offset by
6026    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
6027    as 16-bit instructions.  */
6028 #define X(a,b,c) T_MNEM_##a
6029 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
6030 #undef X
6031
6032 #define X(a,b,c) 0x##b
6033 static const unsigned short thumb_op16[] = { T16_32_TAB };
6034 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
6035 #undef X
6036
6037 #define X(a,b,c) 0x##c
6038 static const unsigned int thumb_op32[] = { T16_32_TAB };
6039 #define THUMB_OP32(n) (thumb_op32[(n) - (T16_32_OFFSET + 1)])
6040 #define THUMB_SETS_FLAGS(n) (THUMB_OP32 (n) & 0x00100000)
6041 #undef X
6042 #undef T16_32_TAB
6043
6044 /* Thumb instruction encoders, in alphabetical order.  */
6045
6046 /* ADDW or SUBW.  */
6047 static void
6048 do_t_add_sub_w (void)
6049 {
6050   int Rd, Rn;
6051
6052   Rd = inst.operands[0].reg;
6053   Rn = inst.operands[1].reg;
6054
6055   constraint (Rd == 15, _("PC not allowed as destination"));
6056   inst.instruction |= (Rn << 16) | (Rd << 8);
6057   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
6058 }
6059
6060 /* Parse an add or subtract instruction.  We get here with inst.instruction
6061    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
6062
6063 static void
6064 do_t_add_sub (void)
6065 {
6066   int Rd, Rs, Rn;
6067
6068   Rd = inst.operands[0].reg;
6069   Rs = (inst.operands[1].present
6070         ? inst.operands[1].reg    /* Rd, Rs, foo */
6071         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
6072
6073   if (unified_syntax)
6074     {
6075       bfd_boolean flags;
6076       bfd_boolean narrow;
6077       int opcode;
6078
6079       flags = (inst.instruction == T_MNEM_adds
6080                || inst.instruction == T_MNEM_subs);
6081       if (flags)
6082         narrow = (current_it_mask == 0);
6083       else
6084         narrow = (current_it_mask != 0);
6085       if (!inst.operands[2].isreg)
6086         {
6087           opcode = 0;
6088           if (inst.size_req != 4)
6089             {
6090               int add;
6091
6092               add = (inst.instruction == T_MNEM_add
6093                      || inst.instruction == T_MNEM_adds);
6094               /* Attempt to use a narrow opcode, with relaxation if
6095                  appropriate.  */
6096               if (Rd == REG_SP && Rs == REG_SP && !flags)
6097                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
6098               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
6099                 opcode = T_MNEM_add_sp;
6100               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
6101                 opcode = T_MNEM_add_pc;
6102               else if (Rd <= 7 && Rs <= 7 && narrow)
6103                 {
6104                   if (flags)
6105                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
6106                   else
6107                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
6108                 }
6109               if (opcode)
6110                 {
6111                   inst.instruction = THUMB_OP16(opcode);
6112                   inst.instruction |= (Rd << 4) | Rs;
6113                   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
6114                   if (inst.size_req != 2)
6115                     inst.relax = opcode;
6116                 }
6117               else
6118                 constraint (inst.size_req == 2, BAD_HIREG);
6119             }
6120           if (inst.size_req == 4
6121               || (inst.size_req != 2 && !opcode))
6122             {
6123               /* ??? Convert large immediates to addw/subw.  */
6124               inst.instruction = THUMB_OP32 (inst.instruction);
6125               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
6126               inst.instruction |= inst.operands[0].reg << 8;
6127               inst.instruction |= inst.operands[1].reg << 16;
6128               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
6129             }
6130         }
6131       else
6132         {
6133           Rn = inst.operands[2].reg;
6134           /* See if we can do this with a 16-bit instruction.  */
6135           if (!inst.operands[2].shifted && inst.size_req != 4)
6136             {
6137               if (Rd > 7 || Rs > 7 || Rn > 7)
6138                 narrow = FALSE;
6139
6140               if (narrow)
6141                 {
6142                   inst.instruction = ((inst.instruction == T_MNEM_adds
6143                                        || inst.instruction == T_MNEM_add)
6144                                       ? T_OPCODE_ADD_R3
6145                                       : T_OPCODE_SUB_R3);
6146                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
6147                   return;
6148                 }
6149
6150               if (inst.instruction == T_MNEM_add)
6151                 {
6152                   if (Rd == Rs)
6153                     {
6154                       inst.instruction = T_OPCODE_ADD_HI;
6155                       inst.instruction |= (Rd & 8) << 4;
6156                       inst.instruction |= (Rd & 7);
6157                       inst.instruction |= Rn << 3;
6158                       return;
6159                     }
6160                   /* ... because addition is commutative! */
6161                   else if (Rd == Rn)
6162                     {
6163                       inst.instruction = T_OPCODE_ADD_HI;
6164                       inst.instruction |= (Rd & 8) << 4;
6165                       inst.instruction |= (Rd & 7);
6166                       inst.instruction |= Rs << 3;
6167                       return;
6168                     }
6169                 }
6170             }
6171           /* If we get here, it can't be done in 16 bits.  */
6172           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
6173                       _("shift must be constant"));
6174           inst.instruction = THUMB_OP32 (inst.instruction);
6175           inst.instruction |= Rd << 8;
6176           inst.instruction |= Rs << 16;
6177           encode_thumb32_shifted_operand (2);
6178         }
6179     }
6180   else
6181     {
6182       constraint (inst.instruction == T_MNEM_adds
6183                   || inst.instruction == T_MNEM_subs,
6184                   BAD_THUMB32);
6185
6186       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
6187         {
6188           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
6189                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
6190                       BAD_HIREG);
6191
6192           inst.instruction = (inst.instruction == T_MNEM_add
6193                               ? 0x0000 : 0x8000);
6194           inst.instruction |= (Rd << 4) | Rs;
6195           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
6196           return;
6197         }
6198
6199       Rn = inst.operands[2].reg;
6200       constraint (inst.operands[2].shifted, _("unshifted register required"));
6201
6202       /* We now have Rd, Rs, and Rn set to registers.  */
6203       if (Rd > 7 || Rs > 7 || Rn > 7)
6204         {
6205           /* Can't do this for SUB.      */
6206           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
6207           inst.instruction = T_OPCODE_ADD_HI;
6208           inst.instruction |= (Rd & 8) << 4;
6209           inst.instruction |= (Rd & 7);
6210           if (Rs == Rd)
6211             inst.instruction |= Rn << 3;
6212           else if (Rn == Rd)
6213             inst.instruction |= Rs << 3;
6214           else
6215             constraint (1, _("dest must overlap one source register"));
6216         }
6217       else
6218         {
6219           inst.instruction = (inst.instruction == T_MNEM_add
6220                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
6221           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
6222         }
6223     }
6224 }
6225
6226 static void
6227 do_t_adr (void)
6228 {
6229   if (unified_syntax && inst.size_req == 0 && inst.operands[0].reg <= 7)
6230     {
6231       /* Defer to section relaxation.  */
6232       inst.relax = inst.instruction;
6233       inst.instruction = THUMB_OP16 (inst.instruction);
6234       inst.instruction |= inst.operands[0].reg << 4;
6235     }
6236   else if (unified_syntax && inst.size_req != 2)
6237     {
6238       /* Generate a 32-bit opcode.  */
6239       inst.instruction = THUMB_OP32 (inst.instruction);
6240       inst.instruction |= inst.operands[0].reg << 8;
6241       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
6242       inst.reloc.pc_rel = 1;
6243     }
6244   else
6245     {
6246       /* Generate a 16-bit opcode.  */
6247       inst.instruction = THUMB_OP16 (inst.instruction);
6248       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
6249       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
6250       inst.reloc.pc_rel = 1;
6251
6252       inst.instruction |= inst.operands[0].reg << 4;
6253     }
6254 }
6255
6256 /* Arithmetic instructions for which there is just one 16-bit
6257    instruction encoding, and it allows only two low registers.
6258    For maximal compatibility with ARM syntax, we allow three register
6259    operands even when Thumb-32 instructions are not available, as long
6260    as the first two are identical.  For instance, both "sbc r0,r1" and
6261    "sbc r0,r0,r1" are allowed.  */
6262 static void
6263 do_t_arit3 (void)
6264 {
6265   int Rd, Rs, Rn;
6266
6267   Rd = inst.operands[0].reg;
6268   Rs = (inst.operands[1].present
6269         ? inst.operands[1].reg    /* Rd, Rs, foo */
6270         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
6271   Rn = inst.operands[2].reg;
6272
6273   if (unified_syntax)
6274     {
6275       if (!inst.operands[2].isreg)
6276         {
6277           /* For an immediate, we always generate a 32-bit opcode;
6278              section relaxation will shrink it later if possible.  */
6279           inst.instruction = THUMB_OP32 (inst.instruction);
6280           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
6281           inst.instruction |= Rd << 8;
6282           inst.instruction |= Rs << 16;
6283           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
6284         }
6285       else
6286         {
6287           bfd_boolean narrow;
6288
6289           /* See if we can do this with a 16-bit instruction.  */
6290           if (THUMB_SETS_FLAGS (inst.instruction))
6291             narrow = current_it_mask == 0;
6292           else
6293             narrow = current_it_mask != 0;
6294
6295           if (Rd > 7 || Rn > 7 || Rs > 7)
6296             narrow = FALSE;
6297           if (inst.operands[2].shifted)
6298             narrow = FALSE;
6299           if (inst.size_req == 4)
6300             narrow = FALSE;
6301
6302           if (narrow
6303               && Rd == Rs)
6304             {
6305               inst.instruction = THUMB_OP16 (inst.instruction);
6306               inst.instruction |= Rd;
6307               inst.instruction |= Rn << 3;
6308               return;
6309             }
6310
6311           /* If we get here, it can't be done in 16 bits.  */
6312           constraint (inst.operands[2].shifted
6313                       && inst.operands[2].immisreg,
6314                       _("shift must be constant"));
6315           inst.instruction = THUMB_OP32 (inst.instruction);
6316           inst.instruction |= Rd << 8;
6317           inst.instruction |= Rs << 16;
6318           encode_thumb32_shifted_operand (2);
6319         }
6320     }
6321   else
6322     {
6323       /* On its face this is a lie - the instruction does set the
6324          flags.  However, the only supported mnemonic in this mode
6325          says it doesn't.  */
6326       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
6327
6328       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
6329                   _("unshifted register required"));
6330       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
6331       constraint (Rd != Rs,
6332                   _("dest and source1 must be the same register"));
6333
6334       inst.instruction = THUMB_OP16 (inst.instruction);
6335       inst.instruction |= Rd;
6336       inst.instruction |= Rn << 3;
6337     }
6338 }
6339
6340 /* Similarly, but for instructions where the arithmetic operation is
6341    commutative, so we can allow either of them to be different from
6342    the destination operand in a 16-bit instruction.  For instance, all
6343    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
6344    accepted.  */
6345 static void
6346 do_t_arit3c (void)
6347 {
6348   int Rd, Rs, Rn;
6349
6350   Rd = inst.operands[0].reg;
6351   Rs = (inst.operands[1].present
6352         ? inst.operands[1].reg    /* Rd, Rs, foo */
6353         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
6354   Rn = inst.operands[2].reg;
6355
6356   if (unified_syntax)
6357     {
6358       if (!inst.operands[2].isreg)
6359         {
6360           /* For an immediate, we always generate a 32-bit opcode;
6361              section relaxation will shrink it later if possible.  */
6362           inst.instruction = THUMB_OP32 (inst.instruction);
6363           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
6364           inst.instruction |= Rd << 8;
6365           inst.instruction |= Rs << 16;
6366           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
6367         }
6368       else
6369         {
6370           bfd_boolean narrow;
6371
6372           /* See if we can do this with a 16-bit instruction.  */
6373           if (THUMB_SETS_FLAGS (inst.instruction))
6374             narrow = current_it_mask == 0;
6375           else
6376             narrow = current_it_mask != 0;
6377
6378           if (Rd > 7 || Rn > 7 || Rs > 7)
6379             narrow = FALSE;
6380           if (inst.operands[2].shifted)
6381             narrow = FALSE;
6382           if (inst.size_req == 4)
6383             narrow = FALSE;
6384
6385           if (narrow)
6386             {
6387               if (Rd == Rs)
6388                 {
6389                   inst.instruction = THUMB_OP16 (inst.instruction);
6390                   inst.instruction |= Rd;
6391                   inst.instruction |= Rn << 3;
6392                   return;
6393                 }
6394               if (Rd == Rn)
6395                 {
6396                   inst.instruction = THUMB_OP16 (inst.instruction);
6397                   inst.instruction |= Rd;
6398                   inst.instruction |= Rs << 3;
6399                   return;
6400                 }
6401             }
6402
6403           /* If we get here, it can't be done in 16 bits.  */
6404           constraint (inst.operands[2].shifted
6405                       && inst.operands[2].immisreg,
6406                       _("shift must be constant"));
6407           inst.instruction = THUMB_OP32 (inst.instruction);
6408           inst.instruction |= Rd << 8;
6409           inst.instruction |= Rs << 16;
6410           encode_thumb32_shifted_operand (2);
6411         }
6412     }
6413   else
6414     {
6415       /* On its face this is a lie - the instruction does set the
6416          flags.  However, the only supported mnemonic in this mode
6417          says it doesn't.  */
6418       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
6419
6420       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
6421                   _("unshifted register required"));
6422       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
6423
6424       inst.instruction = THUMB_OP16 (inst.instruction);
6425       inst.instruction |= Rd;
6426
6427       if (Rd == Rs)
6428         inst.instruction |= Rn << 3;
6429       else if (Rd == Rn)
6430         inst.instruction |= Rs << 3;
6431       else
6432         constraint (1, _("dest must overlap one source register"));
6433     }
6434 }
6435
6436 static void
6437 do_t_bfc (void)
6438 {
6439   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
6440   constraint (msb > 32, _("bit-field extends past end of register"));
6441   /* The instruction encoding stores the LSB and MSB,
6442      not the LSB and width.  */
6443   inst.instruction |= inst.operands[0].reg << 8;
6444   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
6445   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
6446   inst.instruction |= msb - 1;
6447 }
6448
6449 static void
6450 do_t_bfi (void)
6451 {
6452   unsigned int msb;
6453
6454   /* #0 in second position is alternative syntax for bfc, which is
6455      the same instruction but with REG_PC in the Rm field.  */
6456   if (!inst.operands[1].isreg)
6457     inst.operands[1].reg = REG_PC;
6458
6459   msb = inst.operands[2].imm + inst.operands[3].imm;
6460   constraint (msb > 32, _("bit-field extends past end of register"));
6461   /* The instruction encoding stores the LSB and MSB,
6462      not the LSB and width.  */
6463   inst.instruction |= inst.operands[0].reg << 8;
6464   inst.instruction |= inst.operands[1].reg << 16;
6465   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
6466   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
6467   inst.instruction |= msb - 1;
6468 }
6469
6470 static void
6471 do_t_bfx (void)
6472 {
6473   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
6474               _("bit-field extends past end of register"));
6475   inst.instruction |= inst.operands[0].reg << 8;
6476   inst.instruction |= inst.operands[1].reg << 16;
6477   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
6478   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
6479   inst.instruction |= inst.operands[3].imm - 1;
6480 }
6481
6482 /* ARM V5 Thumb BLX (argument parse)
6483         BLX <target_addr>       which is BLX(1)
6484         BLX <Rm>                which is BLX(2)
6485    Unfortunately, there are two different opcodes for this mnemonic.
6486    So, the insns[].value is not used, and the code here zaps values
6487         into inst.instruction.
6488
6489    ??? How to take advantage of the additional two bits of displacement
6490    available in Thumb32 mode?  Need new relocation?  */
6491
6492 static void
6493 do_t_blx (void)
6494 {
6495   if (inst.operands[0].isreg)
6496     /* We have a register, so this is BLX(2).  */
6497     inst.instruction |= inst.operands[0].reg << 3;
6498   else
6499     {
6500       /* No register.  This must be BLX(1).  */
6501       inst.instruction = 0xf000e800;
6502 #ifdef OBJ_ELF
6503       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
6504         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
6505       else
6506 #endif
6507         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BLX;
6508       inst.reloc.pc_rel = 1;
6509     }
6510 }
6511
6512 static void
6513 do_t_branch (void)
6514 {
6515   int opcode;
6516   if (inst.cond != COND_ALWAYS)
6517     opcode = T_MNEM_bcond;
6518   else
6519     opcode = inst.instruction;
6520
6521   if (unified_syntax && inst.size_req == 4)
6522     {
6523       inst.instruction = THUMB_OP32(opcode);
6524       if (inst.cond == COND_ALWAYS)
6525         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH25;
6526       else
6527         {
6528           assert (inst.cond != 0xF);
6529           inst.instruction |= inst.cond << 22;
6530           inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH20;
6531         }
6532     }
6533   else
6534     {
6535       inst.instruction = THUMB_OP16(opcode);
6536       if (inst.cond == COND_ALWAYS)
6537         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH12;
6538       else
6539         {
6540           inst.instruction |= inst.cond << 8;
6541           inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH9;
6542         }
6543       /* Allow section relaxation.  */
6544       if (unified_syntax && inst.size_req != 2)
6545         inst.relax = opcode;
6546     }
6547
6548   inst.reloc.pc_rel = 1;
6549 }
6550
6551 static void
6552 do_t_bkpt (void)
6553 {
6554   if (inst.operands[0].present)
6555     {
6556       constraint (inst.operands[0].imm > 255,
6557                   _("immediate value out of range"));
6558       inst.instruction |= inst.operands[0].imm;
6559     }
6560 }
6561
6562 static void
6563 do_t_branch23 (void)
6564 {
6565   inst.reloc.type   = BFD_RELOC_THUMB_PCREL_BRANCH23;
6566   inst.reloc.pc_rel = 1;
6567
6568   /* If the destination of the branch is a defined symbol which does not have
6569      the THUMB_FUNC attribute, then we must be calling a function which has
6570      the (interfacearm) attribute.  We look for the Thumb entry point to that
6571      function and change the branch to refer to that function instead.  */
6572   if (   inst.reloc.exp.X_op == O_symbol
6573       && inst.reloc.exp.X_add_symbol != NULL
6574       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
6575       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
6576     inst.reloc.exp.X_add_symbol =
6577       find_real_start (inst.reloc.exp.X_add_symbol);
6578 }
6579
6580 static void
6581 do_t_bx (void)
6582 {
6583   inst.instruction |= inst.operands[0].reg << 3;
6584   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
6585      should cause the alignment to be checked once it is known.  This is
6586      because BX PC only works if the instruction is word aligned.  */
6587 }
6588
6589 static void
6590 do_t_bxj (void)
6591 {
6592   if (inst.operands[0].reg == REG_PC)
6593     as_tsktsk (_("use of r15 in bxj is not really useful"));
6594
6595   inst.instruction |= inst.operands[0].reg << 16;
6596 }
6597
6598 static void
6599 do_t_clz (void)
6600 {
6601   inst.instruction |= inst.operands[0].reg << 8;
6602   inst.instruction |= inst.operands[1].reg << 16;
6603   inst.instruction |= inst.operands[1].reg;
6604 }
6605
6606 static void
6607 do_t_cpsi (void)
6608 {
6609   if (unified_syntax
6610       && (inst.operands[1].present || inst.size_req == 4))
6611     {
6612       unsigned int imod = (inst.instruction & 0x0030) >> 4;
6613       inst.instruction = 0xf3af8000;
6614       inst.instruction |= imod << 9;
6615       inst.instruction |= inst.operands[0].imm << 5;
6616       if (inst.operands[1].present)
6617         inst.instruction |= 0x100 | inst.operands[1].imm;
6618     }
6619   else
6620     {
6621       constraint (inst.operands[1].present,
6622                   _("Thumb does not support the 2-argument "
6623                     "form of this instruction"));
6624       inst.instruction |= inst.operands[0].imm;
6625     }
6626 }
6627
6628 /* THUMB CPY instruction (argument parse).  */
6629
6630 static void
6631 do_t_cpy (void)
6632 {
6633   if (inst.size_req == 4)
6634     {
6635       inst.instruction = THUMB_OP32 (T_MNEM_mov);
6636       inst.instruction |= inst.operands[0].reg << 8;
6637       inst.instruction |= inst.operands[1].reg;
6638     }
6639   else
6640     {
6641       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
6642       inst.instruction |= (inst.operands[0].reg & 0x7);
6643       inst.instruction |= inst.operands[1].reg << 3;
6644     }
6645 }
6646
6647 static void
6648 do_t_czb (void)
6649 {
6650   constraint (inst.operands[0].reg > 7, BAD_HIREG);
6651   inst.instruction |= inst.operands[0].reg;
6652   inst.reloc.pc_rel = 1;
6653   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
6654 }
6655
6656 static void
6657 do_t_hint (void)
6658 {
6659   if (unified_syntax && inst.size_req == 4)
6660     inst.instruction = THUMB_OP32 (inst.instruction);
6661   else
6662     inst.instruction = THUMB_OP16 (inst.instruction);
6663 }
6664
6665 static void
6666 do_t_it (void)
6667 {
6668   unsigned int cond = inst.operands[0].imm;
6669
6670   current_it_mask = (inst.instruction & 0xf) | 0x10;
6671   current_cc = cond;
6672
6673   /* If the condition is a negative condition, invert the mask.  */
6674   if ((cond & 0x1) == 0x0)
6675     {
6676       unsigned int mask = inst.instruction & 0x000f;
6677
6678       if ((mask & 0x7) == 0)
6679         /* no conversion needed */;
6680       else if ((mask & 0x3) == 0)
6681         mask ^= 0x8;
6682       else if ((mask & 0x1) == 0)
6683         mask ^= 0xC;
6684       else
6685         mask ^= 0xE;
6686
6687       inst.instruction &= 0xfff0;
6688       inst.instruction |= mask;
6689     }
6690
6691   inst.instruction |= cond << 4;
6692 }
6693
6694 static void
6695 do_t_ldmstm (void)
6696 {
6697   /* This really doesn't seem worth it.  */
6698   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
6699               _("expression too complex"));
6700   constraint (inst.operands[1].writeback,
6701               _("Thumb load/store multiple does not support {reglist}^"));
6702
6703   if (unified_syntax)
6704     {
6705       /* See if we can use a 16-bit instruction.  */
6706       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
6707           && inst.size_req != 4
6708           && inst.operands[0].reg <= 7
6709           && !(inst.operands[1].imm & ~0xff)
6710           && (inst.instruction == T_MNEM_stmia
6711               ? inst.operands[0].writeback
6712               : (inst.operands[0].writeback
6713                  == !(inst.operands[1].imm & (1 << inst.operands[0].reg)))))
6714         {
6715           if (inst.instruction == T_MNEM_stmia
6716               && (inst.operands[1].imm & (1 << inst.operands[0].reg))
6717               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
6718             as_warn (_("value stored for r%d is UNPREDICTABLE"),
6719                      inst.operands[0].reg);
6720
6721           inst.instruction = THUMB_OP16 (inst.instruction);
6722           inst.instruction |= inst.operands[0].reg << 8;
6723           inst.instruction |= inst.operands[1].imm;
6724         }
6725       else
6726         {
6727           if (inst.operands[1].imm & (1 << 13))
6728             as_warn (_("SP should not be in register list"));
6729           if (inst.instruction == T_MNEM_stmia)
6730             {
6731               if (inst.operands[1].imm & (1 << 15))
6732                 as_warn (_("PC should not be in register list"));
6733               if (inst.operands[1].imm & (1 << inst.operands[0].reg))
6734                 as_warn (_("value stored for r%d is UNPREDICTABLE"),
6735                          inst.operands[0].reg);
6736             }
6737           else
6738             {
6739               if (inst.operands[1].imm & (1 << 14)
6740                   && inst.operands[1].imm & (1 << 15))
6741                 as_warn (_("LR and PC should not both be in register list"));
6742               if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
6743                   && inst.operands[0].writeback)
6744                 as_warn (_("base register should not be in register list "
6745                            "when written back"));
6746             }
6747           if (inst.instruction < 0xffff)
6748             inst.instruction = THUMB_OP32 (inst.instruction);
6749           inst.instruction |= inst.operands[0].reg << 16;
6750           inst.instruction |= inst.operands[1].imm;
6751           if (inst.operands[0].writeback)
6752             inst.instruction |= WRITE_BACK;
6753         }
6754     }
6755   else
6756     {
6757       constraint (inst.operands[0].reg > 7
6758                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
6759       if (inst.instruction == T_MNEM_stmia)
6760         {
6761           if (!inst.operands[0].writeback)
6762             as_warn (_("this instruction will write back the base register"));
6763           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
6764               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
6765             as_warn (_("value stored for r%d is UNPREDICTABLE"),
6766                      inst.operands[0].reg);
6767         }
6768       else
6769         {
6770           if (!inst.operands[0].writeback
6771               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
6772             as_warn (_("this instruction will write back the base register"));
6773           else if (inst.operands[0].writeback
6774                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
6775             as_warn (_("this instruction will not write back the base register"));
6776         }
6777
6778       inst.instruction = THUMB_OP16 (inst.instruction);
6779       inst.instruction |= inst.operands[0].reg << 8;
6780       inst.instruction |= inst.operands[1].imm;
6781     }
6782 }
6783
6784 static void
6785 do_t_ldrex (void)
6786 {
6787   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
6788               || inst.operands[1].postind || inst.operands[1].writeback
6789               || inst.operands[1].immisreg || inst.operands[1].shifted
6790               || inst.operands[1].negative,
6791               BAD_ADDR_MODE);
6792
6793   inst.instruction |= inst.operands[0].reg << 12;
6794   inst.instruction |= inst.operands[1].reg << 16;
6795   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
6796 }
6797
6798 static void
6799 do_t_ldrexd (void)
6800 {
6801   if (!inst.operands[1].present)
6802     {
6803       constraint (inst.operands[0].reg == REG_LR,
6804                   _("r14 not allowed as first register "
6805                     "when second register is omitted"));
6806       inst.operands[1].reg = inst.operands[0].reg + 1;
6807     }
6808   constraint (inst.operands[0].reg == inst.operands[1].reg,
6809               BAD_OVERLAP);
6810
6811   inst.instruction |= inst.operands[0].reg << 12;
6812   inst.instruction |= inst.operands[1].reg << 8;
6813   inst.instruction |= inst.operands[2].reg << 16;
6814 }
6815
6816 static void
6817 do_t_ldst (void)
6818 {
6819   unsigned long opcode;
6820   int Rn;
6821
6822   opcode = inst.instruction;
6823   if (unified_syntax)
6824     {
6825       if (inst.operands[1].isreg
6826           && !inst.operands[1].writeback
6827           && !inst.operands[1].shifted && !inst.operands[1].postind
6828           && !inst.operands[1].negative && inst.operands[0].reg <= 7
6829           && opcode <= 0xffff
6830           && inst.size_req != 4)
6831         {
6832           /* Insn may have a 16-bit form.  */
6833           Rn = inst.operands[1].reg;
6834           if (inst.operands[1].immisreg)
6835             {
6836               inst.instruction = THUMB_OP16 (opcode);
6837               /* [Rn, Ri] */
6838               if (Rn <= 7 && inst.operands[1].imm <= 7)
6839                 goto op16;
6840             }
6841           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
6842                     && opcode != T_MNEM_ldrsb)
6843                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
6844                    || (Rn == REG_SP && opcode == T_MNEM_str))
6845             {
6846               /* [Rn, #const] */
6847               if (Rn > 7)
6848                 {
6849                   if (Rn == REG_PC)
6850                     {
6851                       if (inst.reloc.pc_rel)
6852                         opcode = T_MNEM_ldr_pc2;
6853                       else
6854                         opcode = T_MNEM_ldr_pc;
6855                     }
6856                   else
6857                     {
6858                       if (opcode == T_MNEM_ldr)
6859                         opcode = T_MNEM_ldr_sp;
6860                       else
6861                         opcode = T_MNEM_str_sp;
6862                     }
6863                   inst.instruction = inst.operands[0].reg << 8;
6864                 }
6865               else
6866                 {
6867                   inst.instruction = inst.operands[0].reg;
6868                   inst.instruction |= inst.operands[1].reg << 3;
6869                 }
6870               inst.instruction |= THUMB_OP16 (opcode);
6871               if (inst.size_req == 2)
6872                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
6873               else
6874                 inst.relax = opcode;
6875               return;
6876             }
6877         }
6878       /* Definitely a 32-bit variant.  */
6879       inst.instruction = THUMB_OP32 (opcode);
6880       inst.instruction |= inst.operands[0].reg << 12;
6881       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
6882       return;
6883     }
6884
6885   constraint (inst.operands[0].reg > 7, BAD_HIREG);
6886
6887   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
6888     {
6889       /* Only [Rn,Rm] is acceptable.  */
6890       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
6891       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
6892                   || inst.operands[1].postind || inst.operands[1].shifted
6893                   || inst.operands[1].negative,
6894                   _("Thumb does not support this addressing mode"));
6895       inst.instruction = THUMB_OP16 (inst.instruction);
6896       goto op16;
6897     }
6898      
6899   inst.instruction = THUMB_OP16 (inst.instruction);
6900   if (!inst.operands[1].isreg)
6901     if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
6902       return;
6903
6904   constraint (!inst.operands[1].preind
6905               || inst.operands[1].shifted
6906               || inst.operands[1].writeback,
6907               _("Thumb does not support this addressing mode"));
6908   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
6909     {
6910       constraint (inst.instruction & 0x0600,
6911                   _("byte or halfword not valid for base register"));
6912       constraint (inst.operands[1].reg == REG_PC
6913                   && !(inst.instruction & THUMB_LOAD_BIT),
6914                   _("r15 based store not allowed"));
6915       constraint (inst.operands[1].immisreg,
6916                   _("invalid base register for register offset"));
6917
6918       if (inst.operands[1].reg == REG_PC)
6919         inst.instruction = T_OPCODE_LDR_PC;
6920       else if (inst.instruction & THUMB_LOAD_BIT)
6921         inst.instruction = T_OPCODE_LDR_SP;
6922       else
6923         inst.instruction = T_OPCODE_STR_SP;
6924
6925       inst.instruction |= inst.operands[0].reg << 8;
6926       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
6927       return;
6928     }
6929
6930   constraint (inst.operands[1].reg > 7, BAD_HIREG);
6931   if (!inst.operands[1].immisreg)
6932     {
6933       /* Immediate offset.  */
6934       inst.instruction |= inst.operands[0].reg;
6935       inst.instruction |= inst.operands[1].reg << 3;
6936       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
6937       return;
6938     }
6939
6940   /* Register offset.  */
6941   constraint (inst.operands[1].imm > 7, BAD_HIREG);
6942   constraint (inst.operands[1].negative,
6943               _("Thumb does not support this addressing mode"));
6944
6945  op16:
6946   switch (inst.instruction)
6947     {
6948     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
6949     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
6950     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
6951     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
6952     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
6953     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
6954     case 0x5600 /* ldrsb */:
6955     case 0x5e00 /* ldrsh */: break;
6956     default: abort ();
6957     }
6958
6959   inst.instruction |= inst.operands[0].reg;
6960   inst.instruction |= inst.operands[1].reg << 3;
6961   inst.instruction |= inst.operands[1].imm << 6;
6962 }
6963
6964 static void
6965 do_t_ldstd (void)
6966 {
6967   if (!inst.operands[1].present)
6968     {
6969       inst.operands[1].reg = inst.operands[0].reg + 1;
6970       constraint (inst.operands[0].reg == REG_LR,
6971                   _("r14 not allowed here"));
6972     }
6973   inst.instruction |= inst.operands[0].reg << 12;
6974   inst.instruction |= inst.operands[1].reg << 8;
6975   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
6976                             
6977 }
6978
6979 static void
6980 do_t_ldstt (void)
6981 {
6982   inst.instruction |= inst.operands[0].reg << 12;
6983   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
6984 }
6985
6986 static void
6987 do_t_mla (void)
6988 {
6989   inst.instruction |= inst.operands[0].reg << 8;
6990   inst.instruction |= inst.operands[1].reg << 16;
6991   inst.instruction |= inst.operands[2].reg;
6992   inst.instruction |= inst.operands[3].reg << 12;
6993 }
6994
6995 static void
6996 do_t_mlal (void)
6997 {
6998   inst.instruction |= inst.operands[0].reg << 12;
6999   inst.instruction |= inst.operands[1].reg << 8;
7000   inst.instruction |= inst.operands[2].reg << 16;
7001   inst.instruction |= inst.operands[3].reg;
7002 }
7003
7004 static void
7005 do_t_mov_cmp (void)
7006 {
7007   if (unified_syntax)
7008     {
7009       int r0off = (inst.instruction == T_MNEM_mov
7010                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
7011       unsigned long opcode;
7012       bfd_boolean narrow;
7013       bfd_boolean low_regs;
7014
7015       low_regs = (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7);
7016       opcode = inst.instruction;
7017       if (current_it_mask)
7018         narrow = opcode != T_MNEM_movs;
7019       else
7020         narrow = opcode != T_MNEM_movs || low_regs;
7021       if (inst.size_req == 4
7022           || inst.operands[1].shifted)
7023         narrow = FALSE;
7024
7025       if (!inst.operands[1].isreg)
7026         {
7027           /* Immediate operand.  */
7028           if (current_it_mask == 0 && opcode == T_MNEM_mov)
7029             narrow = 0;
7030           if (low_regs && narrow)
7031             {
7032               inst.instruction = THUMB_OP16 (opcode);
7033               inst.instruction |= inst.operands[0].reg << 8;
7034               if (inst.size_req == 2)
7035                 inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
7036               else
7037                 inst.relax = opcode;
7038             }
7039           else
7040             {
7041               inst.instruction = THUMB_OP32 (inst.instruction);
7042               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7043               inst.instruction |= inst.operands[0].reg << r0off;
7044               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7045             }
7046         }
7047       else if (!narrow)
7048         {
7049           inst.instruction = THUMB_OP32 (inst.instruction);
7050           inst.instruction |= inst.operands[0].reg << r0off;
7051           encode_thumb32_shifted_operand (1);
7052         }
7053       else
7054         switch (inst.instruction)
7055           {
7056           case T_MNEM_mov:
7057             inst.instruction = T_OPCODE_MOV_HR;
7058             inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
7059             inst.instruction |= (inst.operands[0].reg & 0x7);
7060             inst.instruction |= inst.operands[1].reg << 3;
7061             break;
7062
7063           case T_MNEM_movs:
7064             /* We know we have low registers at this point.
7065                Generate ADD Rd, Rs, #0.  */
7066             inst.instruction = T_OPCODE_ADD_I3;
7067             inst.instruction |= inst.operands[0].reg;
7068             inst.instruction |= inst.operands[1].reg << 3;
7069             break;
7070
7071           case T_MNEM_cmp:
7072             if (low_regs)
7073               {
7074                 inst.instruction = T_OPCODE_CMP_LR;
7075                 inst.instruction |= inst.operands[0].reg;
7076                 inst.instruction |= inst.operands[1].reg << 3;
7077               }
7078             else
7079               {
7080                 inst.instruction = T_OPCODE_CMP_HR;
7081                 inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
7082                 inst.instruction |= (inst.operands[0].reg & 0x7);
7083                 inst.instruction |= inst.operands[1].reg << 3;
7084               }
7085             break;
7086           }
7087       return;
7088     }
7089
7090   inst.instruction = THUMB_OP16 (inst.instruction);
7091   if (inst.operands[1].isreg)
7092     {
7093       if (inst.operands[0].reg < 8 && inst.operands[1].reg < 8)
7094         {
7095           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
7096              since a MOV instruction produces unpredictable results.  */
7097           if (inst.instruction == T_OPCODE_MOV_I8)
7098             inst.instruction = T_OPCODE_ADD_I3;
7099           else
7100             inst.instruction = T_OPCODE_CMP_LR;
7101
7102           inst.instruction |= inst.operands[0].reg;
7103           inst.instruction |= inst.operands[1].reg << 3;
7104         }
7105       else
7106         {
7107           if (inst.instruction == T_OPCODE_MOV_I8)
7108             inst.instruction = T_OPCODE_MOV_HR;
7109           else
7110             inst.instruction = T_OPCODE_CMP_HR;
7111           do_t_cpy ();
7112         }
7113     }
7114   else
7115     {
7116       constraint (inst.operands[0].reg > 7,
7117                   _("only lo regs allowed with immediate"));
7118       inst.instruction |= inst.operands[0].reg << 8;
7119       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
7120     }
7121 }
7122
7123 static void
7124 do_t_mov16 (void)
7125 {
7126   inst.instruction |= inst.operands[0].reg << 8;
7127   inst.instruction |= (inst.operands[1].imm & 0xf000) << 4;
7128   inst.instruction |= (inst.operands[1].imm & 0x0800) << 15;
7129   inst.instruction |= (inst.operands[1].imm & 0x0700) << 4;
7130   inst.instruction |= (inst.operands[1].imm & 0x00ff);
7131 }
7132
7133 static void
7134 do_t_mvn_tst (void)
7135 {
7136   if (unified_syntax)
7137     {
7138       int r0off = (inst.instruction == T_MNEM_mvn
7139                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
7140       bfd_boolean narrow;
7141
7142       if (inst.size_req == 4
7143           || inst.instruction > 0xffff
7144           || inst.operands[1].shifted
7145           || inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
7146         narrow = FALSE;
7147       else if (inst.instruction == T_MNEM_cmn)
7148         narrow = TRUE;
7149       else if (THUMB_SETS_FLAGS (inst.instruction))
7150         narrow = (current_it_mask == 0);
7151       else
7152         narrow = (current_it_mask != 0);
7153
7154       if (!inst.operands[1].isreg)
7155         {
7156           /* For an immediate, we always generate a 32-bit opcode;
7157              section relaxation will shrink it later if possible.  */
7158           if (inst.instruction < 0xffff)
7159             inst.instruction = THUMB_OP32 (inst.instruction);
7160           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7161           inst.instruction |= inst.operands[0].reg << r0off;
7162           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7163         }
7164       else
7165         {
7166           /* See if we can do this with a 16-bit instruction.  */
7167           if (narrow)
7168             {
7169               inst.instruction = THUMB_OP16 (inst.instruction);
7170               inst.instruction |= inst.operands[0].reg;
7171               inst.instruction |= inst.operands[1].reg << 3;
7172             }
7173           else
7174             {
7175               constraint (inst.operands[1].shifted
7176                           && inst.operands[1].immisreg,
7177                           _("shift must be constant"));
7178               if (inst.instruction < 0xffff)
7179                 inst.instruction = THUMB_OP32 (inst.instruction);
7180               inst.instruction |= inst.operands[0].reg << r0off;
7181               encode_thumb32_shifted_operand (1);
7182             }
7183         }
7184     }
7185   else
7186     {
7187       constraint (inst.instruction > 0xffff
7188                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
7189       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
7190                   _("unshifted register required"));
7191       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
7192                   BAD_HIREG);
7193
7194       inst.instruction = THUMB_OP16 (inst.instruction);
7195       inst.instruction |= inst.operands[0].reg;
7196       inst.instruction |= inst.operands[1].reg << 3;
7197     }
7198 }
7199
7200 static void
7201 do_t_mrs (void)
7202 {
7203   /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
7204   constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
7205               != (PSR_c|PSR_f),
7206               _("'CPSR' or 'SPSR' expected"));
7207   inst.instruction |= inst.operands[0].reg << 8;
7208   inst.instruction |= (inst.operands[1].imm & SPSR_BIT) >> 2;
7209 }
7210
7211 static void
7212 do_t_msr (void)
7213 {
7214   constraint (!inst.operands[1].isreg,
7215               _("Thumb encoding does not support an immediate here"));
7216   inst.instruction |= (inst.operands[0].imm & SPSR_BIT) >> 2;
7217   inst.instruction |= (inst.operands[0].imm & ~SPSR_BIT) >> 8;
7218   inst.instruction |= inst.operands[1].reg << 16;
7219 }
7220
7221 static void
7222 do_t_mul (void)
7223 {
7224   if (!inst.operands[2].present)
7225     inst.operands[2].reg = inst.operands[0].reg;
7226
7227   /* There is no 32-bit MULS and no 16-bit MUL. */
7228   if (unified_syntax && inst.instruction == T_MNEM_mul)
7229     {
7230       inst.instruction = THUMB_OP32 (inst.instruction);
7231       inst.instruction |= inst.operands[0].reg << 8;
7232       inst.instruction |= inst.operands[1].reg << 16;
7233       inst.instruction |= inst.operands[2].reg << 0;
7234     }
7235   else
7236     {
7237       constraint (!unified_syntax
7238                   && inst.instruction == T_MNEM_muls, BAD_THUMB32);
7239       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
7240                   BAD_HIREG);
7241
7242       inst.instruction = THUMB_OP16 (inst.instruction);
7243       inst.instruction |= inst.operands[0].reg;
7244
7245       if (inst.operands[0].reg == inst.operands[1].reg)
7246         inst.instruction |= inst.operands[2].reg << 3;
7247       else if (inst.operands[0].reg == inst.operands[2].reg)
7248         inst.instruction |= inst.operands[1].reg << 3;
7249       else
7250         constraint (1, _("dest must overlap one source register"));
7251     }
7252 }
7253
7254 static void
7255 do_t_mull (void)
7256 {
7257   inst.instruction |= inst.operands[0].reg << 12;
7258   inst.instruction |= inst.operands[1].reg << 8;
7259   inst.instruction |= inst.operands[2].reg << 16;
7260   inst.instruction |= inst.operands[3].reg;
7261
7262   if (inst.operands[0].reg == inst.operands[1].reg)
7263     as_tsktsk (_("rdhi and rdlo must be different"));
7264 }
7265
7266 static void
7267 do_t_nop (void)
7268 {
7269   if (unified_syntax)
7270     {
7271       if (inst.size_req == 4 || inst.operands[0].imm > 15)
7272         {
7273           inst.instruction = THUMB_OP32 (inst.instruction);
7274           inst.instruction |= inst.operands[0].imm;
7275         }
7276       else
7277         {
7278           inst.instruction = THUMB_OP16 (inst.instruction);
7279           inst.instruction |= inst.operands[0].imm << 4;
7280         }
7281     }
7282   else
7283     {
7284       constraint (inst.operands[0].present,
7285                   _("Thumb does not support NOP with hints"));
7286       inst.instruction = 0x46c0;
7287     }
7288 }
7289
7290 static void
7291 do_t_neg (void)
7292 {
7293   if (unified_syntax)
7294     {
7295       bfd_boolean narrow;
7296
7297       if (THUMB_SETS_FLAGS (inst.instruction))
7298         narrow = (current_it_mask == 0);
7299       else
7300         narrow = (current_it_mask != 0);
7301       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
7302         narrow = FALSE;
7303       if (inst.size_req == 4)
7304         narrow = FALSE;
7305
7306       if (!narrow)
7307         {
7308           inst.instruction = THUMB_OP32 (inst.instruction);
7309           inst.instruction |= inst.operands[0].reg << 8;
7310           inst.instruction |= inst.operands[1].reg << 16;
7311         }
7312       else
7313         {
7314           inst.instruction = THUMB_OP16 (inst.instruction);
7315           inst.instruction |= inst.operands[0].reg;
7316           inst.instruction |= inst.operands[1].reg << 3;
7317         }
7318     }
7319   else
7320     {
7321       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
7322                   BAD_HIREG);
7323       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
7324
7325       inst.instruction = THUMB_OP16 (inst.instruction);
7326       inst.instruction |= inst.operands[0].reg;
7327       inst.instruction |= inst.operands[1].reg << 3;
7328     }
7329 }
7330
7331 static void
7332 do_t_pkhbt (void)
7333 {
7334   inst.instruction |= inst.operands[0].reg << 8;
7335   inst.instruction |= inst.operands[1].reg << 16;
7336   inst.instruction |= inst.operands[2].reg;
7337   if (inst.operands[3].present)
7338     {
7339       unsigned int val = inst.reloc.exp.X_add_number;
7340       constraint (inst.reloc.exp.X_op != O_constant,
7341                   _("expression too complex"));
7342       inst.instruction |= (val & 0x1c) << 10;
7343       inst.instruction |= (val & 0x03) << 6;
7344     }
7345 }
7346
7347 static void
7348 do_t_pkhtb (void)
7349 {
7350   if (!inst.operands[3].present)
7351     inst.instruction &= ~0x00000020;
7352   do_t_pkhbt ();
7353 }
7354
7355 static void
7356 do_t_pld (void)
7357 {
7358   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
7359 }
7360
7361 static void
7362 do_t_push_pop (void)
7363 {
7364   unsigned mask;
7365   
7366   constraint (inst.operands[0].writeback,
7367               _("push/pop do not support {reglist}^"));
7368   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
7369               _("expression too complex"));
7370
7371   mask = inst.operands[0].imm;
7372   if ((mask & ~0xff) == 0)
7373     inst.instruction = THUMB_OP16 (inst.instruction);
7374   else if ((inst.instruction == T_MNEM_push
7375             && (mask & ~0xff) == 1 << REG_LR)
7376            || (inst.instruction == T_MNEM_pop
7377                && (mask & ~0xff) == 1 << REG_PC))
7378     {
7379       inst.instruction = THUMB_OP16 (inst.instruction);
7380       inst.instruction |= THUMB_PP_PC_LR;
7381       mask &= 0xff;
7382     }
7383   else if (unified_syntax)
7384     {
7385       if (mask & (1 << 13))
7386         inst.error =  _("SP not allowed in register list");
7387       if (inst.instruction == T_MNEM_push)
7388         {
7389           if (mask & (1 << 15))
7390             inst.error = _("PC not allowed in register list");
7391         }
7392       else
7393         {
7394           if (mask & (1 << 14)
7395               && mask & (1 << 15))
7396             inst.error = _("LR and PC should not both be in register list");
7397         }
7398       if ((mask & (mask - 1)) == 0)
7399         {
7400           /* Single register push/pop implemented as str/ldr.  */
7401           if (inst.instruction == T_MNEM_push)
7402             inst.instruction = 0xf84d0d04; /* str reg, [sp, #-4]! */
7403           else
7404             inst.instruction = 0xf85d0b04; /* ldr reg, [sp], #4 */
7405           mask = ffs(mask) - 1;
7406           mask <<= 12;
7407         }
7408       else
7409         inst.instruction = THUMB_OP32 (inst.instruction);
7410     }
7411   else
7412     {
7413       inst.error = _("invalid register list to push/pop instruction");
7414       return;
7415     }
7416
7417   inst.instruction |= mask;
7418 }
7419
7420 static void
7421 do_t_rbit (void)
7422 {
7423   inst.instruction |= inst.operands[0].reg << 8;
7424   inst.instruction |= inst.operands[1].reg << 16;
7425 }
7426
7427 static void
7428 do_t_rev (void)
7429 {
7430   if (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
7431       && inst.size_req != 4)
7432     {
7433       inst.instruction = THUMB_OP16 (inst.instruction);
7434       inst.instruction |= inst.operands[0].reg;
7435       inst.instruction |= inst.operands[1].reg << 3;
7436     }
7437   else if (unified_syntax)
7438     {
7439       inst.instruction = THUMB_OP32 (inst.instruction);
7440       inst.instruction |= inst.operands[0].reg << 8;
7441       inst.instruction |= inst.operands[1].reg << 16;
7442       inst.instruction |= inst.operands[1].reg;
7443     }
7444   else
7445     inst.error = BAD_HIREG;
7446 }
7447
7448 static void
7449 do_t_rsb (void)
7450 {
7451   int Rd, Rs;
7452
7453   Rd = inst.operands[0].reg;
7454   Rs = (inst.operands[1].present
7455         ? inst.operands[1].reg    /* Rd, Rs, foo */
7456         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
7457
7458   inst.instruction |= Rd << 8;
7459   inst.instruction |= Rs << 16;
7460   if (!inst.operands[2].isreg)
7461     {
7462       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7463       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7464     }
7465   else
7466     encode_thumb32_shifted_operand (2);
7467 }
7468
7469 static void
7470 do_t_setend (void)
7471 {
7472   if (inst.operands[0].imm)
7473     inst.instruction |= 0x8;
7474 }
7475
7476 static void
7477 do_t_shift (void)
7478 {
7479   if (!inst.operands[1].present)
7480     inst.operands[1].reg = inst.operands[0].reg;
7481
7482   if (unified_syntax)
7483     {
7484       bfd_boolean narrow;
7485       int shift_kind;
7486
7487       switch (inst.instruction)
7488         {
7489         case T_MNEM_asr:
7490         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
7491         case T_MNEM_lsl:
7492         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
7493         case T_MNEM_lsr:
7494         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
7495         case T_MNEM_ror:
7496         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
7497         default: abort ();
7498         }
7499
7500       if (THUMB_SETS_FLAGS (inst.instruction))
7501         narrow = (current_it_mask == 0);
7502       else
7503         narrow = (current_it_mask != 0);
7504       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
7505         narrow = FALSE;
7506       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
7507         narrow = FALSE;
7508       if (inst.operands[2].isreg
7509           && (inst.operands[1].reg != inst.operands[0].reg
7510               || inst.operands[2].reg > 7))
7511         narrow = FALSE;
7512       if (inst.size_req == 4)
7513         narrow = FALSE;
7514
7515       if (!narrow)
7516         {
7517           if (inst.operands[2].isreg)
7518             {
7519               inst.instruction = THUMB_OP32 (inst.instruction);
7520               inst.instruction |= inst.operands[0].reg << 8;
7521               inst.instruction |= inst.operands[1].reg << 16;
7522               inst.instruction |= inst.operands[2].reg;
7523             }
7524           else
7525             {
7526               inst.operands[1].shifted = 1;
7527               inst.operands[1].shift_kind = shift_kind;
7528               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
7529                                              ? T_MNEM_movs : T_MNEM_mov);
7530               inst.instruction |= inst.operands[0].reg << 8;
7531               encode_thumb32_shifted_operand (1);
7532               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
7533               inst.reloc.type = BFD_RELOC_UNUSED;
7534             }
7535         }
7536       else
7537         {
7538           if (inst.operands[2].isreg)
7539             {
7540               switch (shift_kind)
7541                 {
7542                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
7543                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
7544                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
7545                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
7546                 default: abort ();
7547                 }
7548           
7549               inst.instruction |= inst.operands[0].reg;
7550               inst.instruction |= inst.operands[2].reg << 3;
7551             }
7552           else
7553             {
7554               switch (shift_kind)
7555                 {
7556                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
7557                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
7558                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
7559                 default: abort ();
7560                 }
7561               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
7562               inst.instruction |= inst.operands[0].reg;
7563               inst.instruction |= inst.operands[1].reg << 3;
7564             }
7565         }
7566     }
7567   else
7568     {
7569       constraint (inst.operands[0].reg > 7
7570                   || inst.operands[1].reg > 7, BAD_HIREG);
7571       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
7572
7573       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
7574         {
7575           constraint (inst.operands[2].reg > 7, BAD_HIREG);
7576           constraint (inst.operands[0].reg != inst.operands[1].reg,
7577                       _("source1 and dest must be same register"));
7578
7579           switch (inst.instruction)
7580             {
7581             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
7582             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
7583             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
7584             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
7585             default: abort ();
7586             }
7587           
7588           inst.instruction |= inst.operands[0].reg;
7589           inst.instruction |= inst.operands[2].reg << 3;
7590         }
7591       else
7592         {
7593           switch (inst.instruction)
7594             {
7595             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
7596             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
7597             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
7598             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
7599             default: abort ();
7600             }
7601           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
7602           inst.instruction |= inst.operands[0].reg;
7603           inst.instruction |= inst.operands[1].reg << 3;
7604         }
7605     }
7606 }
7607
7608 static void
7609 do_t_simd (void)
7610 {
7611   inst.instruction |= inst.operands[0].reg << 8;
7612   inst.instruction |= inst.operands[1].reg << 16;
7613   inst.instruction |= inst.operands[2].reg;
7614 }
7615
7616 static void
7617 do_t_smc (void)
7618 {
7619   unsigned int value = inst.reloc.exp.X_add_number;
7620   constraint (inst.reloc.exp.X_op != O_constant,
7621               _("expression too complex"));
7622   inst.reloc.type = BFD_RELOC_UNUSED;
7623   inst.instruction |= (value & 0xf000) >> 12;
7624   inst.instruction |= (value & 0x0ff0);
7625   inst.instruction |= (value & 0x000f) << 16;
7626 }
7627
7628 static void
7629 do_t_ssat (void)
7630 {
7631   inst.instruction |= inst.operands[0].reg << 8;
7632   inst.instruction |= inst.operands[1].imm - 1;
7633   inst.instruction |= inst.operands[2].reg << 16;
7634
7635   if (inst.operands[3].present)
7636     {
7637       constraint (inst.reloc.exp.X_op != O_constant,
7638                   _("expression too complex"));
7639
7640       if (inst.reloc.exp.X_add_number != 0)
7641         {
7642           if (inst.operands[3].shift_kind == SHIFT_ASR)
7643             inst.instruction |= 0x00200000;  /* sh bit */
7644           inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
7645           inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
7646         }
7647       inst.reloc.type = BFD_RELOC_UNUSED;
7648     }
7649 }
7650
7651 static void
7652 do_t_ssat16 (void)
7653 {
7654   inst.instruction |= inst.operands[0].reg << 8;
7655   inst.instruction |= inst.operands[1].imm - 1;
7656   inst.instruction |= inst.operands[2].reg << 16;
7657 }
7658
7659 static void
7660 do_t_strex (void)
7661 {
7662   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
7663               || inst.operands[2].postind || inst.operands[2].writeback
7664               || inst.operands[2].immisreg || inst.operands[2].shifted
7665               || inst.operands[2].negative,
7666               BAD_ADDR_MODE);
7667
7668   inst.instruction |= inst.operands[0].reg << 8;
7669   inst.instruction |= inst.operands[1].reg << 12;
7670   inst.instruction |= inst.operands[2].reg << 16;
7671   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
7672 }
7673
7674 static void
7675 do_t_strexd (void)
7676 {
7677   if (!inst.operands[2].present)
7678     inst.operands[2].reg = inst.operands[1].reg + 1;
7679
7680   constraint (inst.operands[0].reg == inst.operands[1].reg
7681               || inst.operands[0].reg == inst.operands[2].reg
7682               || inst.operands[0].reg == inst.operands[3].reg
7683               || inst.operands[1].reg == inst.operands[2].reg,
7684               BAD_OVERLAP);
7685
7686   inst.instruction |= inst.operands[0].reg;
7687   inst.instruction |= inst.operands[1].reg << 12;
7688   inst.instruction |= inst.operands[2].reg << 8;
7689   inst.instruction |= inst.operands[3].reg << 16;
7690 }
7691
7692 static void
7693 do_t_sxtah (void)
7694 {
7695   inst.instruction |= inst.operands[0].reg << 8;
7696   inst.instruction |= inst.operands[1].reg << 16;
7697   inst.instruction |= inst.operands[2].reg;
7698   inst.instruction |= inst.operands[3].imm << 4;
7699 }
7700
7701 static void
7702 do_t_sxth (void)
7703 {
7704   if (inst.instruction <= 0xffff && inst.size_req != 4
7705       && inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
7706       && (!inst.operands[2].present || inst.operands[2].imm == 0))
7707     {
7708       inst.instruction = THUMB_OP16 (inst.instruction);
7709       inst.instruction |= inst.operands[0].reg;
7710       inst.instruction |= inst.operands[1].reg << 3;
7711     }
7712   else if (unified_syntax)
7713     {
7714       if (inst.instruction <= 0xffff)
7715         inst.instruction = THUMB_OP32 (inst.instruction);
7716       inst.instruction |= inst.operands[0].reg << 8;
7717       inst.instruction |= inst.operands[1].reg;
7718       inst.instruction |= inst.operands[2].imm << 4;
7719     }
7720   else
7721     {
7722       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
7723                   _("Thumb encoding does not support rotation"));
7724       constraint (1, BAD_HIREG);
7725     }
7726 }
7727
7728 static void
7729 do_t_swi (void)
7730 {
7731   inst.reloc.type = BFD_RELOC_ARM_SWI;
7732 }
7733
7734 static void
7735 do_t_tb (void)
7736 {
7737   int half;
7738
7739   half = (inst.instruction & 0x10) != 0;
7740   constraint (inst.operands[0].imm == 15,
7741               _("PC is not a valid index register"));
7742   constraint (!half && inst.operands[0].shifted,
7743               _("instruction does not allow shifted index"));
7744   constraint (half && !inst.operands[0].shifted,
7745               _("instruction requires shifted index"));
7746   inst.instruction |= (inst.operands[0].reg << 16) | inst.operands[0].imm;
7747 }
7748
7749 static void
7750 do_t_usat (void)
7751 {
7752   inst.instruction |= inst.operands[0].reg << 8;
7753   inst.instruction |= inst.operands[1].imm;
7754   inst.instruction |= inst.operands[2].reg << 16;
7755
7756   if (inst.operands[3].present)
7757     {
7758       constraint (inst.reloc.exp.X_op != O_constant,
7759                   _("expression too complex"));
7760       if (inst.reloc.exp.X_add_number != 0)
7761         {
7762           if (inst.operands[3].shift_kind == SHIFT_ASR)
7763             inst.instruction |= 0x00200000;  /* sh bit */
7764
7765           inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
7766           inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
7767         }
7768       inst.reloc.type = BFD_RELOC_UNUSED;
7769     }
7770 }
7771
7772 static void
7773 do_t_usat16 (void)
7774 {
7775   inst.instruction |= inst.operands[0].reg << 8;
7776   inst.instruction |= inst.operands[1].imm;
7777   inst.instruction |= inst.operands[2].reg << 16;
7778 }
7779 \f
7780 /* Overall per-instruction processing.  */
7781
7782 /* We need to be able to fix up arbitrary expressions in some statements.
7783    This is so that we can handle symbols that are an arbitrary distance from
7784    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
7785    which returns part of an address in a form which will be valid for
7786    a data instruction.  We do this by pushing the expression into a symbol
7787    in the expr_section, and creating a fix for that.  */
7788
7789 static void
7790 fix_new_arm (fragS *       frag,
7791              int           where,
7792              short int     size,
7793              expressionS * exp,
7794              int           pc_rel,
7795              int           reloc)
7796 {
7797   fixS *           new_fix;
7798
7799   switch (exp->X_op)
7800     {
7801     case O_constant:
7802     case O_symbol:
7803     case O_add:
7804     case O_subtract:
7805       new_fix = fix_new_exp (frag, where, size, exp, pc_rel, reloc);
7806       break;
7807
7808     default:
7809       new_fix = fix_new (frag, where, size, make_expr_symbol (exp), 0,
7810                          pc_rel, reloc);
7811       break;
7812     }
7813
7814   /* Mark whether the fix is to a THUMB instruction, or an ARM
7815      instruction.  */
7816   new_fix->tc_fix_data = thumb_mode;
7817 }
7818
7819 /* Create a frg for an instruction requiring relaxation.  */
7820 static void
7821 output_relax_insn (void)
7822 {
7823   char * to;
7824   symbolS *sym;
7825   int offset;
7826
7827   switch (inst.reloc.exp.X_op)
7828     {
7829     case O_symbol:
7830       sym = inst.reloc.exp.X_add_symbol;
7831       offset = inst.reloc.exp.X_add_number;
7832       break;
7833     case O_constant:
7834       sym = NULL;
7835       offset = inst.reloc.exp.X_add_number;
7836       break;
7837     default:
7838       sym = make_expr_symbol (&inst.reloc.exp);
7839       offset = 0;
7840       break;
7841   }
7842   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
7843                  inst.relax, sym, offset, NULL/*offset, opcode*/);
7844   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
7845
7846 #ifdef OBJ_ELF
7847   dwarf2_emit_insn (INSN_SIZE);
7848 #endif
7849 }
7850
7851 /* Write a 32-bit thumb instruction to buf.  */
7852 static void
7853 put_thumb32_insn (char * buf, unsigned long insn)
7854 {
7855   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
7856   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
7857 }
7858
7859 static void
7860 output_inst (const char * str)
7861 {
7862   char * to = NULL;
7863
7864   if (inst.error)
7865     {
7866       as_bad ("%s -- `%s'", inst.error, str);
7867       return;
7868     }
7869   if (inst.relax) {
7870       output_relax_insn();
7871       return;
7872   }
7873   if (inst.size == 0)
7874     return;
7875
7876   to = frag_more (inst.size);
7877
7878   if (thumb_mode && (inst.size > THUMB_SIZE))
7879     {
7880       assert (inst.size == (2 * THUMB_SIZE));
7881       put_thumb32_insn (to, inst.instruction);
7882     }
7883   else if (inst.size > INSN_SIZE)
7884     {
7885       assert (inst.size == (2 * INSN_SIZE));
7886       md_number_to_chars (to, inst.instruction, INSN_SIZE);
7887       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
7888     }
7889   else
7890     md_number_to_chars (to, inst.instruction, inst.size);
7891
7892   if (inst.reloc.type != BFD_RELOC_UNUSED)
7893     fix_new_arm (frag_now, to - frag_now->fr_literal,
7894                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
7895                  inst.reloc.type);
7896
7897 #ifdef OBJ_ELF
7898   dwarf2_emit_insn (inst.size);
7899 #endif
7900 }
7901
7902 /* Tag values used in struct asm_opcode's tag field.  */
7903 enum opcode_tag
7904 {
7905   OT_unconditional,     /* Instruction cannot be conditionalized.
7906                            The ARM condition field is still 0xE.  */
7907   OT_unconditionalF,    /* Instruction cannot be conditionalized
7908                            and carries 0xF in its ARM condition field.  */
7909   OT_csuffix,           /* Instruction takes a conditional suffix.  */
7910   OT_cinfix3,           /* Instruction takes a conditional infix,
7911                            beginning at character index 3.  (In
7912                            unified mode, it becomes a suffix.)  */
7913   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
7914                            character index 3, even in unified mode.  Used for
7915                            legacy instructions where suffix and infix forms
7916                            may be ambiguous.  */
7917   OT_csuf_or_in3,       /* Instruction takes either a conditional
7918                            suffix or an infix at character index 3.  */
7919   OT_odd_infix_unc,     /* This is the unconditional variant of an
7920                            instruction that takes a conditional infix
7921                            at an unusual position.  In unified mode,
7922                            this variant will accept a suffix.  */
7923   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
7924                            are the conditional variants of instructions that
7925                            take conditional infixes in unusual positions.
7926                            The infix appears at character index
7927                            (tag - OT_odd_infix_0).  These are not accepted
7928                            in unified mode.  */
7929 };
7930
7931 /* Subroutine of md_assemble, responsible for looking up the primary
7932    opcode from the mnemonic the user wrote.  STR points to the
7933    beginning of the mnemonic.
7934
7935    This is not simply a hash table lookup, because of conditional
7936    variants.  Most instructions have conditional variants, which are
7937    expressed with a _conditional affix_ to the mnemonic.  If we were
7938    to encode each conditional variant as a literal string in the opcode
7939    table, it would have approximately 20,000 entries.
7940
7941    Most mnemonics take this affix as a suffix, and in unified syntax,
7942    'most' is upgraded to 'all'.  However, in the divided syntax, some
7943    instructions take the affix as an infix, notably the s-variants of
7944    the arithmetic instructions.  Of those instructions, all but six
7945    have the infix appear after the third character of the mnemonic.
7946
7947    Accordingly, the algorithm for looking up primary opcodes given
7948    an identifier is:
7949
7950    1. Look up the identifier in the opcode table.
7951       If we find a match, go to step U.
7952
7953    2. Look up the last two characters of the identifier in the
7954       conditions table.  If we find a match, look up the first N-2
7955       characters of the identifier in the opcode table.  If we
7956       find a match, go to step CE.
7957
7958    3. Look up the fourth and fifth characters of the identifier in
7959       the conditions table.  If we find a match, extract those
7960       characters from the identifier, and look up the remaining
7961       characters in the opcode table.  If we find a match, go
7962       to step CM.
7963
7964    4. Fail.
7965
7966    U. Examine the tag field of the opcode structure, in case this is
7967       one of the six instructions with its conditional infix in an
7968       unusual place.  If it is, the tag tells us where to find the
7969       infix; look it up in the conditions table and set inst.cond
7970       accordingly.  Otherwise, this is an unconditional instruction.
7971       Again set inst.cond accordingly.  Return the opcode structure.
7972
7973   CE. Examine the tag field to make sure this is an instruction that
7974       should receive a conditional suffix.  If it is not, fail.
7975       Otherwise, set inst.cond from the suffix we already looked up,
7976       and return the opcode structure.
7977
7978   CM. Examine the tag field to make sure this is an instruction that
7979       should receive a conditional infix after the third character.
7980       If it is not, fail.  Otherwise, undo the edits to the current
7981       line of input and proceed as for case CE.  */
7982
7983 static const struct asm_opcode *
7984 opcode_lookup (char **str)
7985 {
7986   char *end, *base;
7987   char *affix;
7988   const struct asm_opcode *opcode;
7989   const struct asm_cond *cond;
7990   char save[2];
7991
7992   /* Scan up to the end of the mnemonic, which must end in white space,
7993      '.' (in unified mode only), or end of string.  */
7994   for (base = end = *str; *end != '\0'; end++)
7995     if (*end == ' ' || (unified_syntax && *end == '.'))
7996       break;
7997
7998   if (end == base)
7999     return 0;
8000
8001   /* Handle a possible width suffix.  */
8002   if (end[0] == '.')
8003     {
8004       if (end[1] == 'w' && (end[2] == ' ' || end[2] == '\0'))
8005         inst.size_req = 4;
8006       else if (end[1] == 'n' && (end[2] == ' ' || end[2] == '\0'))
8007         inst.size_req = 2;
8008       else
8009         return 0;
8010
8011       *str = end + 2;
8012     }
8013   else
8014     *str = end;
8015
8016   /* Look for unaffixed or special-case affixed mnemonic.  */
8017   opcode = hash_find_n (arm_ops_hsh, base, end - base);
8018   if (opcode)
8019     {
8020       /* step U */
8021       if (opcode->tag < OT_odd_infix_0)
8022         {
8023           inst.cond = COND_ALWAYS;
8024           return opcode;
8025         }
8026
8027       if (unified_syntax)
8028         as_warn (_("conditional infixes are deprecated in unified syntax"));
8029       affix = base + (opcode->tag - OT_odd_infix_0);
8030       cond = hash_find_n (arm_cond_hsh, affix, 2);
8031       assert (cond);
8032
8033       inst.cond = cond->value;
8034       return opcode;
8035     }
8036
8037   /* Cannot have a conditional suffix on a mnemonic of less than two
8038      characters.  */
8039   if (end - base < 3)
8040     return 0;
8041
8042   /* Look for suffixed mnemonic.  */
8043   affix = end - 2;
8044   cond = hash_find_n (arm_cond_hsh, affix, 2);
8045   opcode = hash_find_n (arm_ops_hsh, base, affix - base);
8046   if (opcode && cond)
8047     {
8048       /* step CE */
8049       switch (opcode->tag)
8050         {
8051         case OT_cinfix3_legacy:
8052           /* Ignore conditional suffixes matched on infix only mnemonics.  */
8053           break;
8054
8055         case OT_cinfix3:
8056         case OT_odd_infix_unc:
8057           if (!unified_syntax)
8058             return 0;
8059           /* else fall through */
8060
8061         case OT_csuffix:
8062         case OT_csuf_or_in3:
8063           inst.cond = cond->value;
8064           return opcode;
8065
8066         case OT_unconditional:
8067         case OT_unconditionalF:
8068           /* delayed diagnostic */
8069           inst.error = BAD_COND;
8070           inst.cond = COND_ALWAYS;
8071           return opcode;
8072
8073         default:
8074           return 0;
8075         }
8076     }
8077
8078   /* Cannot have a usual-position infix on a mnemonic of less than
8079      six characters (five would be a suffix).  */
8080   if (end - base < 6)
8081     return 0;
8082
8083   /* Look for infixed mnemonic in the usual position.  */
8084   affix = base + 3;
8085   cond = hash_find_n (arm_cond_hsh, affix, 2);
8086   if (!cond)
8087     return 0;
8088
8089   memcpy (save, affix, 2);
8090   memmove (affix, affix + 2, (end - affix) - 2);
8091   opcode = hash_find_n (arm_ops_hsh, base, (end - base) - 2);
8092   memmove (affix + 2, affix, (end - affix) - 2);
8093   memcpy (affix, save, 2);
8094
8095   if (opcode && (opcode->tag == OT_cinfix3 || opcode->tag == OT_csuf_or_in3
8096                  || opcode->tag == OT_cinfix3_legacy))
8097     {
8098       /* step CM */
8099       if (unified_syntax && opcode->tag == OT_cinfix3)
8100         as_warn (_("conditional infixes are deprecated in unified syntax"));
8101
8102       inst.cond = cond->value;
8103       return opcode;
8104     }
8105
8106   return 0;
8107 }
8108
8109 void
8110 md_assemble (char *str)
8111 {
8112   char *p = str;
8113   const struct asm_opcode * opcode;
8114
8115   /* Align the previous label if needed.  */
8116   if (last_label_seen != NULL)
8117     {
8118       symbol_set_frag (last_label_seen, frag_now);
8119       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
8120       S_SET_SEGMENT (last_label_seen, now_seg);
8121     }
8122
8123   memset (&inst, '\0', sizeof (inst));
8124   inst.reloc.type = BFD_RELOC_UNUSED;
8125
8126   opcode = opcode_lookup (&p);
8127   if (!opcode)
8128     {
8129       /* It wasn't an instruction, but it might be a register alias of
8130          the form alias .req reg.  */
8131       if (!create_register_alias (str, p))
8132         as_bad (_("bad instruction `%s'"), str);
8133
8134       return;
8135     }
8136
8137   if (thumb_mode)
8138     {
8139       arm_feature_set variant;
8140
8141       variant = cpu_variant;
8142       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
8143       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
8144         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
8145       /* Check that this instruction is supported for this CPU.  */
8146       if (thumb_mode == 1
8147           && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant))
8148         {
8149           as_bad (_("selected processor does not support `%s'"), str);
8150           return;
8151         }
8152       if (inst.cond != COND_ALWAYS && !unified_syntax
8153           && opcode->tencode != do_t_branch)
8154         {
8155           as_bad (_("Thumb does not support conditional execution"));
8156           return;
8157         }
8158
8159       /* Check conditional suffixes.  */
8160       if (current_it_mask)
8161         {
8162           int cond;
8163           cond = current_cc ^ ((current_it_mask >> 4) & 1) ^ 1;
8164           if (cond != inst.cond)
8165             {
8166               as_bad (_("incorrect condition in IT block"));
8167               return;
8168             }
8169           current_it_mask <<= 1;
8170           current_it_mask &= 0x1f;
8171         }
8172       else if (inst.cond != COND_ALWAYS && opcode->tencode != do_t_branch)
8173         {
8174           as_bad (_("thumb conditional instrunction not in IT block"));
8175           return;
8176         }
8177
8178       mapping_state (MAP_THUMB);
8179       inst.instruction = opcode->tvalue;
8180
8181       if (!parse_operands (p, opcode->operands))
8182         opcode->tencode ();
8183
8184       /* Clear current_it_mask at the end of an IT block.  */
8185       if (current_it_mask == 0x10)
8186         current_it_mask = 0;
8187
8188       if (!(inst.error || inst.relax))
8189         {
8190           assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
8191           inst.size = (inst.instruction > 0xffff ? 4 : 2);
8192           if (inst.size_req && inst.size_req != inst.size)
8193             {
8194               as_bad (_("cannot honor width suffix -- `%s'"), str);
8195               return;
8196             }
8197         }
8198       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
8199                               *opcode->tvariant);
8200       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
8201          set those bits when Thumb-2 32-bit instuctions are seen.  ie.
8202          anything other than bl/blx.
8203          This is overly pessimistic for relaxable instructions.  */
8204       if ((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
8205           || inst.relax)
8206         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
8207                                 arm_ext_v6t2);
8208     }
8209   else
8210     {
8211       /* Check that this instruction is supported for this CPU.  */
8212       if (!ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant))
8213         {
8214           as_bad (_("selected processor does not support `%s'"), str);
8215           return;
8216         }
8217       if (inst.size_req)
8218         {
8219           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
8220           return;
8221         }
8222
8223       mapping_state (MAP_ARM);
8224       inst.instruction = opcode->avalue;
8225       if (opcode->tag == OT_unconditionalF)
8226         inst.instruction |= 0xF << 28;
8227       else
8228         inst.instruction |= inst.cond << 28;
8229       inst.size = INSN_SIZE;
8230       if (!parse_operands (p, opcode->operands))
8231         opcode->aencode ();
8232       /* Arm mode bx is marked as both v4T and v5 because it's still required
8233          on a hypothetical non-thumb v5 core.  */
8234       if (ARM_CPU_HAS_FEATURE (*opcode->avariant, arm_ext_v4t)
8235           || ARM_CPU_HAS_FEATURE (*opcode->avariant, arm_ext_v5))
8236         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
8237       else
8238         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
8239                                 *opcode->avariant);
8240     }
8241   output_inst (str);
8242 }
8243
8244 /* Various frobbings of labels and their addresses.  */
8245
8246 void
8247 arm_start_line_hook (void)
8248 {
8249   last_label_seen = NULL;
8250 }
8251
8252 void
8253 arm_frob_label (symbolS * sym)
8254 {
8255   last_label_seen = sym;
8256
8257   ARM_SET_THUMB (sym, thumb_mode);
8258
8259 #if defined OBJ_COFF || defined OBJ_ELF
8260   ARM_SET_INTERWORK (sym, support_interwork);
8261 #endif
8262
8263   /* Note - do not allow local symbols (.Lxxx) to be labeled
8264      as Thumb functions.  This is because these labels, whilst
8265      they exist inside Thumb code, are not the entry points for
8266      possible ARM->Thumb calls.  Also, these labels can be used
8267      as part of a computed goto or switch statement.  eg gcc
8268      can generate code that looks like this:
8269
8270                 ldr  r2, [pc, .Laaa]
8271                 lsl  r3, r3, #2
8272                 ldr  r2, [r3, r2]
8273                 mov  pc, r2
8274
8275        .Lbbb:  .word .Lxxx
8276        .Lccc:  .word .Lyyy
8277        ..etc...
8278        .Laaa:   .word Lbbb
8279
8280      The first instruction loads the address of the jump table.
8281      The second instruction converts a table index into a byte offset.
8282      The third instruction gets the jump address out of the table.
8283      The fourth instruction performs the jump.
8284
8285      If the address stored at .Laaa is that of a symbol which has the
8286      Thumb_Func bit set, then the linker will arrange for this address
8287      to have the bottom bit set, which in turn would mean that the
8288      address computation performed by the third instruction would end
8289      up with the bottom bit set.  Since the ARM is capable of unaligned
8290      word loads, the instruction would then load the incorrect address
8291      out of the jump table, and chaos would ensue.  */
8292   if (label_is_thumb_function_name
8293       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
8294       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
8295     {
8296       /* When the address of a Thumb function is taken the bottom
8297          bit of that address should be set.  This will allow
8298          interworking between Arm and Thumb functions to work
8299          correctly.  */
8300
8301       THUMB_SET_FUNC (sym, 1);
8302
8303       label_is_thumb_function_name = FALSE;
8304     }
8305
8306 #ifdef OBJ_ELF
8307   dwarf2_emit_label (sym);
8308 #endif
8309 }
8310
8311 int
8312 arm_data_in_code (void)
8313 {
8314   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
8315     {
8316       *input_line_pointer = '/';
8317       input_line_pointer += 5;
8318       *input_line_pointer = 0;
8319       return 1;
8320     }
8321
8322   return 0;
8323 }
8324
8325 char *
8326 arm_canonicalize_symbol_name (char * name)
8327 {
8328   int len;
8329
8330   if (thumb_mode && (len = strlen (name)) > 5
8331       && streq (name + len - 5, "/data"))
8332     *(name + len - 5) = 0;
8333
8334   return name;
8335 }
8336 \f
8337 /* Table of all register names defined by default.  The user can
8338    define additional names with .req.  Note that all register names
8339    should appear in both upper and lowercase variants.  Some registers
8340    also have mixed-case names.  */
8341
8342 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE }
8343 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
8344 #define REGSET(p,t) \
8345   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
8346   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
8347   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
8348   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
8349
8350 static const struct reg_entry reg_names[] =
8351 {
8352   /* ARM integer registers.  */
8353   REGSET(r, RN), REGSET(R, RN),
8354
8355   /* ATPCS synonyms.  */
8356   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
8357   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
8358   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
8359
8360   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
8361   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
8362   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
8363
8364   /* Well-known aliases.  */
8365   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
8366   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
8367
8368   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
8369   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
8370
8371   /* Coprocessor numbers.  */
8372   REGSET(p, CP), REGSET(P, CP),
8373
8374   /* Coprocessor register numbers.  The "cr" variants are for backward
8375      compatibility.  */
8376   REGSET(c,  CN), REGSET(C, CN),
8377   REGSET(cr, CN), REGSET(CR, CN),
8378
8379   /* FPA registers.  */
8380   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
8381   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
8382
8383   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
8384   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
8385
8386   /* VFP SP registers.  */
8387   REGSET(s,VFS),
8388   REGNUM(s,16,VFS), REGNUM(s,17,VFS), REGNUM(s,18,VFS), REGNUM(s,19,VFS),
8389   REGNUM(s,20,VFS), REGNUM(s,21,VFS), REGNUM(s,22,VFS), REGNUM(s,23,VFS),
8390   REGNUM(s,24,VFS), REGNUM(s,25,VFS), REGNUM(s,26,VFS), REGNUM(s,27,VFS),
8391   REGNUM(s,28,VFS), REGNUM(s,29,VFS), REGNUM(s,30,VFS), REGNUM(s,31,VFS),
8392
8393   REGSET(S,VFS),
8394   REGNUM(S,16,VFS), REGNUM(S,17,VFS), REGNUM(S,18,VFS), REGNUM(S,19,VFS),
8395   REGNUM(S,20,VFS), REGNUM(S,21,VFS), REGNUM(S,22,VFS), REGNUM(S,23,VFS),
8396   REGNUM(S,24,VFS), REGNUM(S,25,VFS), REGNUM(S,26,VFS), REGNUM(S,27,VFS),
8397   REGNUM(S,28,VFS), REGNUM(S,29,VFS), REGNUM(S,30,VFS), REGNUM(S,31,VFS),
8398
8399   /* VFP DP Registers.  */
8400   REGSET(d,VFD), REGSET(D,VFS),
8401
8402   /* VFP control registers.  */
8403   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
8404   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
8405
8406   /* Maverick DSP coprocessor registers.  */
8407   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
8408   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
8409
8410   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
8411   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
8412   REGDEF(dspsc,0,DSPSC),
8413
8414   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
8415   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
8416   REGDEF(DSPSC,0,DSPSC),
8417
8418   /* iWMMXt data registers - p0, c0-15.  */
8419   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
8420
8421   /* iWMMXt control registers - p1, c0-3.  */
8422   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
8423   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
8424   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
8425   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
8426
8427   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
8428   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
8429   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
8430   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
8431   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
8432
8433   /* XScale accumulator registers.  */
8434   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
8435 };
8436 #undef REGDEF
8437 #undef REGNUM
8438 #undef REGSET
8439
8440 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
8441    within psr_required_here.  */
8442 static const struct asm_psr psrs[] =
8443 {
8444   /* Backward compatibility notation.  Note that "all" is no longer
8445      truly all possible PSR bits.  */
8446   {"all",  PSR_c | PSR_f},
8447   {"flg",  PSR_f},
8448   {"ctl",  PSR_c},
8449
8450   /* Individual flags.  */
8451   {"f",    PSR_f},
8452   {"c",    PSR_c},
8453   {"x",    PSR_x},
8454   {"s",    PSR_s},
8455   /* Combinations of flags.  */
8456   {"fs",   PSR_f | PSR_s},
8457   {"fx",   PSR_f | PSR_x},
8458   {"fc",   PSR_f | PSR_c},
8459   {"sf",   PSR_s | PSR_f},
8460   {"sx",   PSR_s | PSR_x},
8461   {"sc",   PSR_s | PSR_c},
8462   {"xf",   PSR_x | PSR_f},
8463   {"xs",   PSR_x | PSR_s},
8464   {"xc",   PSR_x | PSR_c},
8465   {"cf",   PSR_c | PSR_f},
8466   {"cs",   PSR_c | PSR_s},
8467   {"cx",   PSR_c | PSR_x},
8468   {"fsx",  PSR_f | PSR_s | PSR_x},
8469   {"fsc",  PSR_f | PSR_s | PSR_c},
8470   {"fxs",  PSR_f | PSR_x | PSR_s},
8471   {"fxc",  PSR_f | PSR_x | PSR_c},
8472   {"fcs",  PSR_f | PSR_c | PSR_s},
8473   {"fcx",  PSR_f | PSR_c | PSR_x},
8474   {"sfx",  PSR_s | PSR_f | PSR_x},
8475   {"sfc",  PSR_s | PSR_f | PSR_c},
8476   {"sxf",  PSR_s | PSR_x | PSR_f},
8477   {"sxc",  PSR_s | PSR_x | PSR_c},
8478   {"scf",  PSR_s | PSR_c | PSR_f},
8479   {"scx",  PSR_s | PSR_c | PSR_x},
8480   {"xfs",  PSR_x | PSR_f | PSR_s},
8481   {"xfc",  PSR_x | PSR_f | PSR_c},
8482   {"xsf",  PSR_x | PSR_s | PSR_f},
8483   {"xsc",  PSR_x | PSR_s | PSR_c},
8484   {"xcf",  PSR_x | PSR_c | PSR_f},
8485   {"xcs",  PSR_x | PSR_c | PSR_s},
8486   {"cfs",  PSR_c | PSR_f | PSR_s},
8487   {"cfx",  PSR_c | PSR_f | PSR_x},
8488   {"csf",  PSR_c | PSR_s | PSR_f},
8489   {"csx",  PSR_c | PSR_s | PSR_x},
8490   {"cxf",  PSR_c | PSR_x | PSR_f},
8491   {"cxs",  PSR_c | PSR_x | PSR_s},
8492   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
8493   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
8494   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
8495   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
8496   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
8497   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
8498   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
8499   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
8500   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
8501   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
8502   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
8503   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
8504   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
8505   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
8506   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
8507   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
8508   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
8509   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
8510   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
8511   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
8512   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
8513   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
8514   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
8515   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
8516 };
8517
8518 /* Table of all shift-in-operand names.  */
8519 static const struct asm_shift_name shift_names [] =
8520 {
8521   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
8522   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
8523   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
8524   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
8525   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
8526   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
8527 };
8528
8529 /* Table of all explicit relocation names.  */
8530 #ifdef OBJ_ELF
8531 static struct reloc_entry reloc_names[] =
8532 {
8533   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
8534   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
8535   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
8536   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
8537   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
8538   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
8539   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
8540   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
8541   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
8542   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
8543   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32}
8544 };
8545 #endif
8546
8547 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
8548 static const struct asm_cond conds[] =
8549 {
8550   {"eq", 0x0},
8551   {"ne", 0x1},
8552   {"cs", 0x2}, {"hs", 0x2},
8553   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
8554   {"mi", 0x4},
8555   {"pl", 0x5},
8556   {"vs", 0x6},
8557   {"vc", 0x7},
8558   {"hi", 0x8},
8559   {"ls", 0x9},
8560   {"ge", 0xa},
8561   {"lt", 0xb},
8562   {"gt", 0xc},
8563   {"le", 0xd},
8564   {"al", 0xe}
8565 };
8566
8567 /* Table of ARM-format instructions.    */
8568
8569 /* Macros for gluing together operand strings.  N.B. In all cases
8570    other than OPS0, the trailing OP_stop comes from default
8571    zero-initialization of the unspecified elements of the array.  */
8572 #define OPS0()            { OP_stop, }
8573 #define OPS1(a)           { OP_##a, }
8574 #define OPS2(a,b)         { OP_##a,OP_##b, }
8575 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
8576 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
8577 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
8578 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
8579
8580 /* These macros abstract out the exact format of the mnemonic table and
8581    save some repeated characters.  */
8582
8583 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
8584 #define TxCE(mnem, op, top, nops, ops, ae, te) \
8585   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
8586     THUMB_VARIANT, do_##ae, do_##te }
8587
8588 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
8589    a T_MNEM_xyz enumerator.  */
8590 #define TCE(mnem, aop, top, nops, ops, ae, te) \
8591        TxCE(mnem, aop, 0x##top, nops, ops, ae, te)
8592 #define tCE(mnem, aop, top, nops, ops, ae, te) \
8593        TxCE(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
8594
8595 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
8596    infix after the third character.  */
8597 #define TxC3(mnem, op, top, nops, ops, ae, te) \
8598   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
8599     THUMB_VARIANT, do_##ae, do_##te }
8600 #define TC3(mnem, aop, top, nops, ops, ae, te) \
8601        TxC3(mnem, aop, 0x##top, nops, ops, ae, te)
8602 #define tC3(mnem, aop, top, nops, ops, ae, te) \
8603        TxC3(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
8604
8605 /* Mnemonic with a conditional infix in an unusual place.  Each and every variant has to
8606    appear in the condition table.  */
8607 #define TxCM_(m1, m2, m3, op, top, nops, ops, ae, te)   \
8608   { #m1 #m2 #m3, OPS##nops ops, sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
8609     0x##op, top, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##te }
8610
8611 #define TxCM(m1, m2, op, top, nops, ops, ae, te)        \
8612   TxCM_(m1,   , m2, op, top, nops, ops, ae, te),        \
8613   TxCM_(m1, eq, m2, op, top, nops, ops, ae, te),        \
8614   TxCM_(m1, ne, m2, op, top, nops, ops, ae, te),        \
8615   TxCM_(m1, cs, m2, op, top, nops, ops, ae, te),        \
8616   TxCM_(m1, hs, m2, op, top, nops, ops, ae, te),        \
8617   TxCM_(m1, cc, m2, op, top, nops, ops, ae, te),        \
8618   TxCM_(m1, ul, m2, op, top, nops, ops, ae, te),        \
8619   TxCM_(m1, lo, m2, op, top, nops, ops, ae, te),        \
8620   TxCM_(m1, mi, m2, op, top, nops, ops, ae, te),        \
8621   TxCM_(m1, pl, m2, op, top, nops, ops, ae, te),        \
8622   TxCM_(m1, vs, m2, op, top, nops, ops, ae, te),        \
8623   TxCM_(m1, vc, m2, op, top, nops, ops, ae, te),        \
8624   TxCM_(m1, hi, m2, op, top, nops, ops, ae, te),        \
8625   TxCM_(m1, ls, m2, op, top, nops, ops, ae, te),        \
8626   TxCM_(m1, ge, m2, op, top, nops, ops, ae, te),        \
8627   TxCM_(m1, lt, m2, op, top, nops, ops, ae, te),        \
8628   TxCM_(m1, gt, m2, op, top, nops, ops, ae, te),        \
8629   TxCM_(m1, le, m2, op, top, nops, ops, ae, te),        \
8630   TxCM_(m1, al, m2, op, top, nops, ops, ae, te)
8631
8632 #define TCM(m1,m2, aop, top, nops, ops, ae, te)         \
8633        TxCM(m1,m2, aop, 0x##top, nops, ops, ae, te)
8634 #define tCM(m1,m2, aop, top, nops, ops, ae, te)                 \
8635        TxCM(m1,m2, aop, T_MNEM_##top, nops, ops, ae, te)
8636
8637 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
8638    field is still 0xE.  */
8639 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
8640   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
8641     THUMB_VARIANT, do_##ae, do_##te }
8642
8643 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
8644    condition code field.  */
8645 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
8646   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
8647     THUMB_VARIANT, do_##ae, do_##te }
8648
8649 /* ARM-only variants of all the above.  */
8650 #define CE(mnem,  op, nops, ops, ae)    \
8651   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
8652
8653 #define C3(mnem, op, nops, ops, ae)     \
8654   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
8655
8656 /* Legacy mnemonics that always have conditional infix after the third
8657    character.  */
8658 #define CL(mnem, op, nops, ops, ae)     \
8659   { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
8660     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
8661
8662 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
8663 #define cCE(mnem,  op, nops, ops, ae)   \
8664   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
8665
8666 /* Legacy coprocessor instructions where conditional infix and conditional
8667    suffix are ambiguous.  For consistency this includes all FPA instructions,
8668    not just the potentially ambiguous ones.  */
8669 #define cCL(mnem, op, nops, ops, ae)    \
8670   { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
8671     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
8672
8673 /* Coprocessor, takes either a suffix or a position-3 infix
8674    (for an FPA corner case). */
8675 #define C3E(mnem, op, nops, ops, ae) \
8676   { #mnem, OPS##nops ops, OT_csuf_or_in3, \
8677     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
8678
8679 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
8680   { #m1 #m2 #m3, OPS##nops ops, \
8681     sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
8682     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
8683
8684 #define CM(m1, m2, op, nops, ops, ae)   \
8685   xCM_(m1,   , m2, op, nops, ops, ae),  \
8686   xCM_(m1, eq, m2, op, nops, ops, ae),  \
8687   xCM_(m1, ne, m2, op, nops, ops, ae),  \
8688   xCM_(m1, cs, m2, op, nops, ops, ae),  \
8689   xCM_(m1, hs, m2, op, nops, ops, ae),  \
8690   xCM_(m1, cc, m2, op, nops, ops, ae),  \
8691   xCM_(m1, ul, m2, op, nops, ops, ae),  \
8692   xCM_(m1, lo, m2, op, nops, ops, ae),  \
8693   xCM_(m1, mi, m2, op, nops, ops, ae),  \
8694   xCM_(m1, pl, m2, op, nops, ops, ae),  \
8695   xCM_(m1, vs, m2, op, nops, ops, ae),  \
8696   xCM_(m1, vc, m2, op, nops, ops, ae),  \
8697   xCM_(m1, hi, m2, op, nops, ops, ae),  \
8698   xCM_(m1, ls, m2, op, nops, ops, ae),  \
8699   xCM_(m1, ge, m2, op, nops, ops, ae),  \
8700   xCM_(m1, lt, m2, op, nops, ops, ae),  \
8701   xCM_(m1, gt, m2, op, nops, ops, ae),  \
8702   xCM_(m1, le, m2, op, nops, ops, ae),  \
8703   xCM_(m1, al, m2, op, nops, ops, ae)
8704
8705 #define UE(mnem, op, nops, ops, ae)     \
8706   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
8707
8708 #define UF(mnem, op, nops, ops, ae)     \
8709   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
8710
8711 #define do_0 0
8712
8713 /* Thumb-only, unconditional.  */
8714 #define UT(mnem,  op, nops, ops, te) TUE(mnem,  0, op, nops, ops, 0, te)
8715
8716 static const struct asm_opcode insns[] =
8717 {
8718 #define ARM_VARIANT &arm_ext_v1 /* Core ARM Instructions.  */
8719 #define THUMB_VARIANT &arm_ext_v4t
8720  tCE(and,       0000000, and,      3, (RR, oRR, SH), arit, t_arit3c),
8721  tC3(ands,      0100000, ands,     3, (RR, oRR, SH), arit, t_arit3c),
8722  tCE(eor,       0200000, eor,      3, (RR, oRR, SH), arit, t_arit3c),
8723  tC3(eors,      0300000, eors,     3, (RR, oRR, SH), arit, t_arit3c),
8724  tCE(sub,       0400000, sub,      3, (RR, oRR, SH), arit, t_add_sub),
8725  tC3(subs,      0500000, subs,     3, (RR, oRR, SH), arit, t_add_sub),
8726  tCE(add,       0800000, add,      3, (RR, oRR, SH), arit, t_add_sub),
8727  tC3(adds,      0900000, adds,     3, (RR, oRR, SH), arit, t_add_sub),
8728  tCE(adc,       0a00000, adc,      3, (RR, oRR, SH), arit, t_arit3c),
8729  tC3(adcs,      0b00000, adcs,     3, (RR, oRR, SH), arit, t_arit3c),
8730  tCE(sbc,       0c00000, sbc,      3, (RR, oRR, SH), arit, t_arit3),
8731  tC3(sbcs,      0d00000, sbcs,     3, (RR, oRR, SH), arit, t_arit3),
8732  tCE(orr,       1800000, orr,      3, (RR, oRR, SH), arit, t_arit3c),
8733  tC3(orrs,      1900000, orrs,     3, (RR, oRR, SH), arit, t_arit3c),
8734  tCE(bic,       1c00000, bic,      3, (RR, oRR, SH), arit, t_arit3),
8735  tC3(bics,      1d00000, bics,     3, (RR, oRR, SH), arit, t_arit3),
8736
8737  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
8738     for setting PSR flag bits.  They are obsolete in V6 and do not
8739     have Thumb equivalents. */
8740  tCE(tst,       1100000, tst,      2, (RR, SH),      cmp,  t_mvn_tst),
8741  tC3(tsts,      1100000, tst,      2, (RR, SH),      cmp,  t_mvn_tst),
8742   CL(tstp,      110f000,           2, (RR, SH),      cmp),
8743  tCE(cmp,       1500000, cmp,      2, (RR, SH),      cmp,  t_mov_cmp),
8744  tC3(cmps,      1500000, cmp,      2, (RR, SH),      cmp,  t_mov_cmp),
8745   CL(cmpp,      150f000,           2, (RR, SH),      cmp),
8746  tCE(cmn,       1700000, cmn,      2, (RR, SH),      cmp,  t_mvn_tst),
8747  tC3(cmns,      1700000, cmn,      2, (RR, SH),      cmp,  t_mvn_tst),
8748   CL(cmnp,      170f000,           2, (RR, SH),      cmp),
8749
8750  tCE(mov,       1a00000, mov,      2, (RR, SH),      mov,  t_mov_cmp),
8751  tC3(movs,      1b00000, movs,     2, (RR, SH),      mov,  t_mov_cmp),
8752  tCE(mvn,       1e00000, mvn,      2, (RR, SH),      mov,  t_mvn_tst),
8753  tC3(mvns,      1f00000, mvns,     2, (RR, SH),      mov,  t_mvn_tst),
8754
8755  tCE(ldr,       4100000, ldr,      2, (RR, ADDR),    ldst, t_ldst),
8756  tC3(ldrb,      4500000, ldrb,     2, (RR, ADDR),    ldst, t_ldst),
8757  tCE(str,       4000000, str,      2, (RR, ADDR),    ldst, t_ldst),
8758  tC3(strb,      4400000, strb,     2, (RR, ADDR),    ldst, t_ldst),
8759
8760  tC3(stmia,     8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
8761  tC3(stmea,     8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
8762  tC3(ldmia,     8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
8763  tC3(ldmfd,     8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
8764
8765  TCE(swi,       f000000, df00,     1, (EXPi),        swi, t_swi),
8766  tCE(b,         a000000, b,        1, (EXPr),        branch, t_branch),
8767  TCE(bl,        b000000, f000f800, 1, (EXPr),        bl, t_branch23),
8768
8769   /* Pseudo ops.  */
8770  tCE(adr,       28f0000, adr,      2, (RR, EXP),     adr,  t_adr),
8771   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
8772  tCE(nop,       1a00000, nop,      1, (oI255c),      nop,  t_nop),
8773
8774   /* Thumb-compatibility pseudo ops.  */
8775  tCE(lsl,       1a00000, lsl,      3, (RR, oRR, SH), shift, t_shift),
8776  tC3(lsls,      1b00000, lsls,     3, (RR, oRR, SH), shift, t_shift),
8777  tCE(lsr,       1a00020, lsr,      3, (RR, oRR, SH), shift, t_shift),
8778  tC3(lsrs,      1b00020, lsrs,     3, (RR, oRR, SH), shift, t_shift),
8779  tCE(asr,       1a00040, asr,      3, (RR, oRR, SH), shift, t_shift),
8780  tC3(asrs,      1b00040, asrs,     3, (RR, oRR, SH), shift, t_shift),
8781  tCE(ror,       1a00060, ror,      3, (RR, oRR, SH), shift, t_shift),
8782  tC3(rors,      1b00060, rors,     3, (RR, oRR, SH), shift, t_shift),
8783  tCE(neg,       2600000, neg,      2, (RR, RR),      rd_rn, t_neg),
8784  tC3(negs,      2700000, negs,     2, (RR, RR),      rd_rn, t_neg),
8785  tCE(push,      92d0000, push,     1, (REGLST),      push_pop, t_push_pop),
8786  tCE(pop,       8bd0000, pop,      1, (REGLST),      push_pop, t_push_pop),
8787
8788 #undef THUMB_VARIANT
8789 #define THUMB_VARIANT &arm_ext_v6
8790  TCE(cpy,       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
8791
8792  /* V1 instructions with no Thumb analogue prior to V6T2.  */
8793 #undef THUMB_VARIANT
8794 #define THUMB_VARIANT &arm_ext_v6t2
8795  TCE(rsb,       0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
8796  TC3(rsbs,      0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
8797  TCE(teq,       1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
8798  TC3(teqs,      1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
8799   CL(teqp,      130f000,           2, (RR, SH),      cmp),
8800
8801  TC3(ldrt,      4300000, f8500e00, 2, (RR, ADDR),    ldstt, t_ldstt),
8802  TC3(ldrbt,     4700000, f8300e00, 2, (RR, ADDR),    ldstt, t_ldstt),
8803  TC3(strt,      4200000, f8400e00, 2, (RR, ADDR),    ldstt, t_ldstt),
8804  TC3(strbt,     4600000, f8200e00, 2, (RR, ADDR),    ldstt, t_ldstt),
8805
8806  TC3(stmdb,     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
8807  TC3(stmfd,     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
8808
8809  TC3(ldmdb,     9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
8810  TC3(ldmea,     9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
8811
8812  /* V1 instructions with no Thumb analogue at all.  */
8813   CE(rsc,       0e00000,           3, (RR, oRR, SH), arit),
8814   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
8815
8816   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
8817   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
8818   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
8819   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
8820   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
8821   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
8822   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
8823   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
8824
8825 #undef ARM_VARIANT
8826 #define ARM_VARIANT &arm_ext_v2 /* ARM 2 - multiplies.  */
8827 #undef THUMB_VARIANT
8828 #define THUMB_VARIANT &arm_ext_v4t
8829  tCE(mul,       0000090, mul,      3, (RRnpc, RRnpc, oRR), mul, t_mul),
8830  tC3(muls,      0100090, muls,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
8831
8832 #undef THUMB_VARIANT
8833 #define THUMB_VARIANT &arm_ext_v6t2
8834  TCE(mla,       0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
8835   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
8836
8837   /* Generic coprocessor instructions.  */
8838  TCE(cdp,       e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
8839  TCE(ldc,       c100000, ec100000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8840  TC3(ldcl,      c500000, ec500000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8841  TCE(stc,       c000000, ec000000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8842  TC3(stcl,      c400000, ec400000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8843  TCE(mcr,       e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
8844  TCE(mrc,       e100010, ee100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
8845
8846 #undef ARM_VARIANT
8847 #define ARM_VARIANT &arm_ext_v2s /* ARM 3 - swp instructions.  */
8848   CE(swp,       1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
8849   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
8850
8851 #undef ARM_VARIANT
8852 #define ARM_VARIANT &arm_ext_v3 /* ARM 6 Status register instructions.  */
8853  TCE(mrs,       10f0000, f3ef8000, 2, (RR, PSR),     mrs, t_mrs),
8854  TCE(msr,       120f000, f3808000, 2, (PSR, RR_EXi), msr, t_msr),
8855
8856 #undef ARM_VARIANT
8857 #define ARM_VARIANT &arm_ext_v3m         /* ARM 7M long multiplies.  */
8858  TCE(smull,     0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
8859   CM(smull,s,   0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
8860  TCE(umull,     0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
8861   CM(umull,s,   0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
8862  TCE(smlal,     0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
8863   CM(smlal,s,   0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
8864  TCE(umlal,     0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
8865   CM(umlal,s,   0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
8866
8867 #undef ARM_VARIANT
8868 #define ARM_VARIANT &arm_ext_v4 /* ARM Architecture 4.  */
8869 #undef THUMB_VARIANT
8870 #define THUMB_VARIANT &arm_ext_v4t
8871  tC3(ldrh,      01000b0, ldrh,     2, (RR, ADDR), ldstv4, t_ldst),
8872  tC3(strh,      00000b0, strh,     2, (RR, ADDR), ldstv4, t_ldst),
8873  tC3(ldrsh,     01000f0, ldrsh,    2, (RR, ADDR), ldstv4, t_ldst),
8874  tC3(ldrsb,     01000d0, ldrsb,    2, (RR, ADDR), ldstv4, t_ldst),
8875  tCM(ld,sh,     01000f0, ldrsh,    2, (RR, ADDR), ldstv4, t_ldst),
8876  tCM(ld,sb,     01000d0, ldrsb,    2, (RR, ADDR), ldstv4, t_ldst),
8877
8878 #undef ARM_VARIANT
8879 #define ARM_VARIANT &arm_ext_v4t_5
8880   /* ARM Architecture 4T.  */
8881   /* Note: bx (and blx) are required on V5, even if the processor does
8882      not support Thumb.  */
8883  TCE(bx,        12fff10, 4700, 1, (RR), bx, t_bx),
8884
8885 #undef ARM_VARIANT
8886 #define ARM_VARIANT &arm_ext_v5 /*  ARM Architecture 5T.         */
8887 #undef THUMB_VARIANT
8888 #define THUMB_VARIANT &arm_ext_v5t
8889   /* Note: blx has 2 variants; the .value coded here is for
8890      BLX(2).  Only this variant has conditional execution.  */
8891  TCE(blx,       12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
8892  TUE(bkpt,      1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
8893
8894 #undef THUMB_VARIANT
8895 #define THUMB_VARIANT &arm_ext_v6t2
8896  TCE(clz,       16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
8897  TUF(ldc2,      c100000, fc100000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8898  TUF(ldc2l,     c500000, fc500000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8899  TUF(stc2,      c000000, fc000000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8900  TUF(stc2l,     c400000, fc400000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8901  TUF(cdp2,      e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
8902  TUF(mcr2,      e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
8903  TUF(mrc2,      e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
8904
8905 #undef ARM_VARIANT
8906 #define ARM_VARIANT &arm_ext_v5exp /*  ARM Architecture 5TExP.  */
8907  TCE(smlabb,    1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8908  TCE(smlatb,    10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8909  TCE(smlabt,    10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8910  TCE(smlatt,    10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8911
8912  TCE(smlawb,    1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8913  TCE(smlawt,    12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8914
8915  TCE(smlalbb,   1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
8916  TCE(smlaltb,   14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
8917  TCE(smlalbt,   14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
8918  TCE(smlaltt,   14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
8919
8920  TCE(smulbb,    1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8921  TCE(smultb,    16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8922  TCE(smulbt,    16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8923  TCE(smultt,    16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8924
8925  TCE(smulwb,    12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8926  TCE(smulwt,    12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8927
8928  TCE(qadd,      1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
8929  TCE(qdadd,     1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
8930  TCE(qsub,      1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
8931  TCE(qdsub,     1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
8932
8933 #undef ARM_VARIANT
8934 #define ARM_VARIANT &arm_ext_v5e /*  ARM Architecture 5TE.  */
8935  TUF(pld,       450f000, f810f000, 1, (ADDR),                pld,  t_pld),
8936  TC3(ldrd,      00000d0, e9500000, 3, (RRnpc, oRRnpc, ADDR), ldrd, t_ldstd),
8937  TC3(strd,      00000f0, e9400000, 3, (RRnpc, oRRnpc, ADDR), ldrd, t_ldstd),
8938
8939  TCE(mcrr,      c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
8940  TCE(mrrc,      c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
8941
8942 #undef ARM_VARIANT
8943 #define ARM_VARIANT &arm_ext_v5j /*  ARM Architecture 5TEJ.  */
8944  TCE(bxj,       12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
8945
8946 #undef ARM_VARIANT
8947 #define ARM_VARIANT &arm_ext_v6 /*  ARM V6.  */
8948 #undef THUMB_VARIANT
8949 #define THUMB_VARIANT &arm_ext_v6
8950  TUF(cpsie,     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
8951  TUF(cpsid,     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
8952  tCE(rev,       6bf0f30, rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
8953  tCE(rev16,     6bf0fb0, rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
8954  tCE(revsh,     6ff0fb0, revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
8955  tCE(sxth,      6bf0070, sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
8956  tCE(uxth,      6ff0070, uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
8957  tCE(sxtb,      6af0070, sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
8958  tCE(uxtb,      6ef0070, uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
8959  TUF(setend,    1010000, b650,     1, (ENDI),                     setend, t_setend),
8960
8961 #undef THUMB_VARIANT
8962 #define THUMB_VARIANT &arm_ext_v6t2
8963  TUF(cps,       1020000, f3af8100, 1, (I31b),                     imm0, imm0),
8964  TCE(ldrex,     1900f9f, e8500f00, 2, (RRnpc, ADDR),              ldrex, t_ldrex),
8965  TUF(mcrr2,     c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
8966  TUF(mrrc2,     c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
8967  TCE(pkhbt,     6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
8968  TCE(pkhtb,     6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
8969  TCE(qadd16,    6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8970  TCE(qadd8,     6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8971  TCE(qaddsubx,  6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8972  TCE(qsub16,    6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8973  TCE(qsub8,     6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8974  TCE(qsubaddx,  6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8975  TCE(sadd16,    6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8976  TCE(sadd8,     6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8977  TCE(saddsubx,  6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8978  TCE(shadd16,   6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8979  TCE(shadd8,    6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8980  TCE(shaddsubx, 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8981  TCE(shsub16,   6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8982  TCE(shsub8,    6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8983  TCE(shsubaddx, 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8984  TCE(ssub16,    6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8985  TCE(ssub8,     6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8986  TCE(ssubaddx,  6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8987  TCE(uadd16,    6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8988  TCE(uadd8,     6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8989  TCE(uaddsubx,  6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8990  TCE(uhadd16,   6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8991  TCE(uhadd8,    6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8992  TCE(uhaddsubx, 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8993  TCE(uhsub16,   6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8994  TCE(uhsub8,    6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8995  TCE(uhsubaddx, 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8996  TCE(uqadd16,   6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8997  TCE(uqadd8,    6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8998  TCE(uqaddsubx, 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8999  TCE(uqsub16,   6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
9000  TCE(uqsub8,    6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
9001  TCE(uqsubaddx, 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
9002  TCE(usub16,    6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
9003  TCE(usub8,     6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
9004  TCE(usubaddx,  6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
9005  TUF(rfeia,     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
9006   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
9007   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
9008  TUF(rfedb,     9100a00, e810c000, 1, (RRw),                       rfe, rfe),
9009  TUF(rfefd,     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
9010   UF(rfefa,     9900a00,           1, (RRw),                       rfe),
9011   UF(rfeea,     8100a00,           1, (RRw),                       rfe),
9012  TUF(rfeed,     9100a00, e810c000, 1, (RRw),                       rfe, rfe),
9013  TCE(sxtah,     6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9014  TCE(sxtab16,   6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9015  TCE(sxtab,     6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9016  TCE(sxtb16,    68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
9017  TCE(uxtah,     6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9018  TCE(uxtab16,   6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9019  TCE(uxtab,     6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9020  TCE(uxtb16,    6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
9021  TCE(sel,       6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
9022  TCE(smlad,     7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9023  TCE(smladx,    7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9024  TCE(smlald,    7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
9025  TCE(smlaldx,   7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
9026  TCE(smlsd,     7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9027  TCE(smlsdx,    7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9028  TCE(smlsld,    7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
9029  TCE(smlsldx,   7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
9030  TCE(smmla,     7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9031  TCE(smmlar,    7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9032  TCE(smmls,     75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9033  TCE(smmlsr,    75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9034  TCE(smmul,     750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
9035  TCE(smmulr,    750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
9036  TCE(smuad,     700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
9037  TCE(smuadx,    700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
9038  TCE(smusd,     700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
9039  TCE(smusdx,    700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
9040  TUF(srsia,     8cd0500, e980c000, 1, (I31w),                      srs,  srs),
9041   UF(srsib,     9cd0500,           1, (I31w),                      srs),
9042   UF(srsda,     84d0500,           1, (I31w),                      srs),
9043  TUF(srsdb,     94d0500, e800c000, 1, (I31w),                      srs,  srs),
9044  TCE(ssat,      6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
9045  TCE(ssat16,    6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
9046  TCE(strex,     1800f90, e8400000, 3, (RRnpc, RRnpc, ADDR),        strex,  t_strex),
9047  TCE(umaal,     0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
9048  TCE(usad8,     780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
9049  TCE(usada8,    7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
9050  TCE(usat,      6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
9051  TCE(usat16,    6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
9052
9053 #undef ARM_VARIANT
9054 #define ARM_VARIANT &arm_ext_v6k
9055 #undef THUMB_VARIANT
9056 #define THUMB_VARIANT &arm_ext_v6k
9057  tCE(yield,     320f001, yield,    0, (), noargs, t_hint),
9058  tCE(wfe,       320f002, wfe,      0, (), noargs, t_hint),
9059  tCE(wfi,       320f003, wfi,      0, (), noargs, t_hint),
9060  tCE(sev,       320f004, sev,      0, (), noargs, t_hint),
9061
9062 #undef THUMB_VARIANT
9063 #define THUMB_VARIANT &arm_ext_v6t2
9064  TCE(ldrexb,    1d00f9f, e8d00f4f, 2, (RRnpc, RRnpcb),                rd_rn,  rd_rn),
9065  TCE(ldrexh,    1f00f9f, e8d00f5f, 2, (RRnpc, RRnpcb),                rd_rn,  rd_rn),
9066  TCE(ldrexd,    1b00f9f, e8d0007f, 3, (RRnpc, oRRnpc, RRnpcb),        ldrexd, t_ldrexd),
9067  TCE(strexb,    1c00f90, e8c00f40, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
9068  TCE(strexh,    1e00f90, e8c00f50, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
9069  TCE(strexd,    1a00f90, e8c00070, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb), strexd, t_strexd),
9070  TUF(clrex,     57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
9071
9072 #undef ARM_VARIANT
9073 #define ARM_VARIANT &arm_ext_v6z
9074  TCE(smc,       1600070, f7f08000, 1, (EXPi), smc, t_smc),
9075
9076 #undef ARM_VARIANT
9077 #define ARM_VARIANT &arm_ext_v6t2
9078  TCE(bfc,       7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
9079  TCE(bfi,       7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
9080  TCE(sbfx,      7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
9081  TCE(ubfx,      7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
9082
9083  TCE(mls,       0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
9084  TCE(movw,      3000000, f2400000, 2, (RRnpc, Iffff),               mov16, t_mov16),
9085  TCE(movt,      3400000, f2c00000, 2, (RRnpc, Iffff),               mov16, t_mov16),
9086  TCE(rbit,      3ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
9087
9088  TC3(ldrht,     03000b0, f8300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9089  TC3(ldrsht,    03000f0, f9300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9090  TC3(ldrsbt,    03000d0, f9100e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9091  TC3(strht,     02000b0, f8200e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9092
9093   UT(cbnz,      b900,    2, (RR, EXP), t_czb),
9094   UT(cbz,       b100,    2, (RR, EXP), t_czb),
9095  /* ARM does not really have an IT instruction.  */
9096  TUE(it,        0, bf08, 1, (COND),    it, t_it),
9097  TUE(itt,       0, bf0c, 1, (COND),    it, t_it),
9098  TUE(ite,       0, bf04, 1, (COND),    it, t_it),
9099  TUE(ittt,      0, bf0e, 1, (COND),    it, t_it),
9100  TUE(itet,      0, bf06, 1, (COND),    it, t_it),
9101  TUE(itte,      0, bf0a, 1, (COND),    it, t_it),
9102  TUE(itee,      0, bf02, 1, (COND),    it, t_it),
9103  TUE(itttt,     0, bf0f, 1, (COND),    it, t_it),
9104  TUE(itett,     0, bf07, 1, (COND),    it, t_it),
9105  TUE(ittet,     0, bf0b, 1, (COND),    it, t_it),
9106  TUE(iteet,     0, bf03, 1, (COND),    it, t_it),
9107  TUE(ittte,     0, bf0d, 1, (COND),    it, t_it),
9108  TUE(itete,     0, bf05, 1, (COND),    it, t_it),
9109  TUE(ittee,     0, bf09, 1, (COND),    it, t_it),
9110  TUE(iteee,     0, bf01, 1, (COND),    it, t_it),
9111
9112  /* Thumb2 only instructions.  */
9113 #undef ARM_VARIANT
9114 #define ARM_VARIANT NULL
9115
9116  TCE(addw,      0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
9117  TCE(subw,      0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
9118  TCE(tbb,       0, e8d0f000, 1, (TB), 0, t_tb),
9119  TCE(tbh,       0, e8d0f010, 1, (TB), 0, t_tb),
9120
9121 #undef ARM_VARIANT
9122 #define ARM_VARIANT &fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
9123  cCE(wfs,       e200110, 1, (RR),            rd),
9124  cCE(rfs,       e300110, 1, (RR),            rd),
9125  cCE(wfc,       e400110, 1, (RR),            rd),
9126  cCE(rfc,       e500110, 1, (RR),            rd),
9127
9128  cCL(ldfs,      c100100, 2, (RF, ADDR),      rd_cpaddr),
9129  cCL(ldfd,      c108100, 2, (RF, ADDR),      rd_cpaddr),
9130  cCL(ldfe,      c500100, 2, (RF, ADDR),      rd_cpaddr),
9131  cCL(ldfp,      c508100, 2, (RF, ADDR),      rd_cpaddr),
9132
9133  cCL(stfs,      c000100, 2, (RF, ADDR),      rd_cpaddr),
9134  cCL(stfd,      c008100, 2, (RF, ADDR),      rd_cpaddr),
9135  cCL(stfe,      c400100, 2, (RF, ADDR),      rd_cpaddr),
9136  cCL(stfp,      c408100, 2, (RF, ADDR),      rd_cpaddr),
9137
9138  cCL(mvfs,      e008100, 2, (RF, RF_IF),     rd_rm),
9139  cCL(mvfsp,     e008120, 2, (RF, RF_IF),     rd_rm),
9140  cCL(mvfsm,     e008140, 2, (RF, RF_IF),     rd_rm),
9141  cCL(mvfsz,     e008160, 2, (RF, RF_IF),     rd_rm),
9142  cCL(mvfd,      e008180, 2, (RF, RF_IF),     rd_rm),
9143  cCL(mvfdp,     e0081a0, 2, (RF, RF_IF),     rd_rm),
9144  cCL(mvfdm,     e0081c0, 2, (RF, RF_IF),     rd_rm),
9145  cCL(mvfdz,     e0081e0, 2, (RF, RF_IF),     rd_rm),
9146  cCL(mvfe,      e088100, 2, (RF, RF_IF),     rd_rm),
9147  cCL(mvfep,     e088120, 2, (RF, RF_IF),     rd_rm),
9148  cCL(mvfem,     e088140, 2, (RF, RF_IF),     rd_rm),
9149  cCL(mvfez,     e088160, 2, (RF, RF_IF),     rd_rm),
9150
9151  cCL(mnfs,      e108100, 2, (RF, RF_IF),     rd_rm),
9152  cCL(mnfsp,     e108120, 2, (RF, RF_IF),     rd_rm),
9153  cCL(mnfsm,     e108140, 2, (RF, RF_IF),     rd_rm),
9154  cCL(mnfsz,     e108160, 2, (RF, RF_IF),     rd_rm),
9155  cCL(mnfd,      e108180, 2, (RF, RF_IF),     rd_rm),
9156  cCL(mnfdp,     e1081a0, 2, (RF, RF_IF),     rd_rm),
9157  cCL(mnfdm,     e1081c0, 2, (RF, RF_IF),     rd_rm),
9158  cCL(mnfdz,     e1081e0, 2, (RF, RF_IF),     rd_rm),
9159  cCL(mnfe,      e188100, 2, (RF, RF_IF),     rd_rm),
9160  cCL(mnfep,     e188120, 2, (RF, RF_IF),     rd_rm),
9161  cCL(mnfem,     e188140, 2, (RF, RF_IF),     rd_rm),
9162  cCL(mnfez,     e188160, 2, (RF, RF_IF),     rd_rm),
9163
9164  cCL(abss,      e208100, 2, (RF, RF_IF),     rd_rm),
9165  cCL(abssp,     e208120, 2, (RF, RF_IF),     rd_rm),
9166  cCL(abssm,     e208140, 2, (RF, RF_IF),     rd_rm),
9167  cCL(abssz,     e208160, 2, (RF, RF_IF),     rd_rm),
9168  cCL(absd,      e208180, 2, (RF, RF_IF),     rd_rm),
9169  cCL(absdp,     e2081a0, 2, (RF, RF_IF),     rd_rm),
9170  cCL(absdm,     e2081c0, 2, (RF, RF_IF),     rd_rm),
9171  cCL(absdz,     e2081e0, 2, (RF, RF_IF),     rd_rm),
9172  cCL(abse,      e288100, 2, (RF, RF_IF),     rd_rm),
9173  cCL(absep,     e288120, 2, (RF, RF_IF),     rd_rm),
9174  cCL(absem,     e288140, 2, (RF, RF_IF),     rd_rm),
9175  cCL(absez,     e288160, 2, (RF, RF_IF),     rd_rm),
9176
9177  cCL(rnds,      e308100, 2, (RF, RF_IF),     rd_rm),
9178  cCL(rndsp,     e308120, 2, (RF, RF_IF),     rd_rm),
9179  cCL(rndsm,     e308140, 2, (RF, RF_IF),     rd_rm),
9180  cCL(rndsz,     e308160, 2, (RF, RF_IF),     rd_rm),
9181  cCL(rndd,      e308180, 2, (RF, RF_IF),     rd_rm),
9182  cCL(rnddp,     e3081a0, 2, (RF, RF_IF),     rd_rm),
9183  cCL(rnddm,     e3081c0, 2, (RF, RF_IF),     rd_rm),
9184  cCL(rnddz,     e3081e0, 2, (RF, RF_IF),     rd_rm),
9185  cCL(rnde,      e388100, 2, (RF, RF_IF),     rd_rm),
9186  cCL(rndep,     e388120, 2, (RF, RF_IF),     rd_rm),
9187  cCL(rndem,     e388140, 2, (RF, RF_IF),     rd_rm),
9188  cCL(rndez,     e388160, 2, (RF, RF_IF),     rd_rm),
9189
9190  cCL(sqts,      e408100, 2, (RF, RF_IF),     rd_rm),
9191  cCL(sqtsp,     e408120, 2, (RF, RF_IF),     rd_rm),
9192  cCL(sqtsm,     e408140, 2, (RF, RF_IF),     rd_rm),
9193  cCL(sqtsz,     e408160, 2, (RF, RF_IF),     rd_rm),
9194  cCL(sqtd,      e408180, 2, (RF, RF_IF),     rd_rm),
9195  cCL(sqtdp,     e4081a0, 2, (RF, RF_IF),     rd_rm),
9196  cCL(sqtdm,     e4081c0, 2, (RF, RF_IF),     rd_rm),
9197  cCL(sqtdz,     e4081e0, 2, (RF, RF_IF),     rd_rm),
9198  cCL(sqte,      e488100, 2, (RF, RF_IF),     rd_rm),
9199  cCL(sqtep,     e488120, 2, (RF, RF_IF),     rd_rm),
9200  cCL(sqtem,     e488140, 2, (RF, RF_IF),     rd_rm),
9201  cCL(sqtez,     e488160, 2, (RF, RF_IF),     rd_rm),
9202
9203  cCL(logs,      e508100, 2, (RF, RF_IF),     rd_rm),
9204  cCL(logsp,     e508120, 2, (RF, RF_IF),     rd_rm),
9205  cCL(logsm,     e508140, 2, (RF, RF_IF),     rd_rm),
9206  cCL(logsz,     e508160, 2, (RF, RF_IF),     rd_rm),
9207  cCL(logd,      e508180, 2, (RF, RF_IF),     rd_rm),
9208  cCL(logdp,     e5081a0, 2, (RF, RF_IF),     rd_rm),
9209  cCL(logdm,     e5081c0, 2, (RF, RF_IF),     rd_rm),
9210  cCL(logdz,     e5081e0, 2, (RF, RF_IF),     rd_rm),
9211  cCL(loge,      e588100, 2, (RF, RF_IF),     rd_rm),
9212  cCL(logep,     e588120, 2, (RF, RF_IF),     rd_rm),
9213  cCL(logem,     e588140, 2, (RF, RF_IF),     rd_rm),
9214  cCL(logez,     e588160, 2, (RF, RF_IF),     rd_rm),
9215
9216  cCL(lgns,      e608100, 2, (RF, RF_IF),     rd_rm),
9217  cCL(lgnsp,     e608120, 2, (RF, RF_IF),     rd_rm),
9218  cCL(lgnsm,     e608140, 2, (RF, RF_IF),     rd_rm),
9219  cCL(lgnsz,     e608160, 2, (RF, RF_IF),     rd_rm),
9220  cCL(lgnd,      e608180, 2, (RF, RF_IF),     rd_rm),
9221  cCL(lgndp,     e6081a0, 2, (RF, RF_IF),     rd_rm),
9222  cCL(lgndm,     e6081c0, 2, (RF, RF_IF),     rd_rm),
9223  cCL(lgndz,     e6081e0, 2, (RF, RF_IF),     rd_rm),
9224  cCL(lgne,      e688100, 2, (RF, RF_IF),     rd_rm),
9225  cCL(lgnep,     e688120, 2, (RF, RF_IF),     rd_rm),
9226  cCL(lgnem,     e688140, 2, (RF, RF_IF),     rd_rm),
9227  cCL(lgnez,     e688160, 2, (RF, RF_IF),     rd_rm),
9228
9229  cCL(exps,      e708100, 2, (RF, RF_IF),     rd_rm),
9230  cCL(expsp,     e708120, 2, (RF, RF_IF),     rd_rm),
9231  cCL(expsm,     e708140, 2, (RF, RF_IF),     rd_rm),
9232  cCL(expsz,     e708160, 2, (RF, RF_IF),     rd_rm),
9233  cCL(expd,      e708180, 2, (RF, RF_IF),     rd_rm),
9234  cCL(expdp,     e7081a0, 2, (RF, RF_IF),     rd_rm),
9235  cCL(expdm,     e7081c0, 2, (RF, RF_IF),     rd_rm),
9236  cCL(expdz,     e7081e0, 2, (RF, RF_IF),     rd_rm),
9237  cCL(expe,      e788100, 2, (RF, RF_IF),     rd_rm),
9238  cCL(expep,     e788120, 2, (RF, RF_IF),     rd_rm),
9239  cCL(expem,     e788140, 2, (RF, RF_IF),     rd_rm),
9240  cCL(expdz,     e788160, 2, (RF, RF_IF),     rd_rm),
9241
9242  cCL(sins,      e808100, 2, (RF, RF_IF),     rd_rm),
9243  cCL(sinsp,     e808120, 2, (RF, RF_IF),     rd_rm),
9244  cCL(sinsm,     e808140, 2, (RF, RF_IF),     rd_rm),
9245  cCL(sinsz,     e808160, 2, (RF, RF_IF),     rd_rm),
9246  cCL(sind,      e808180, 2, (RF, RF_IF),     rd_rm),
9247  cCL(sindp,     e8081a0, 2, (RF, RF_IF),     rd_rm),
9248  cCL(sindm,     e8081c0, 2, (RF, RF_IF),     rd_rm),
9249  cCL(sindz,     e8081e0, 2, (RF, RF_IF),     rd_rm),
9250  cCL(sine,      e888100, 2, (RF, RF_IF),     rd_rm),
9251  cCL(sinep,     e888120, 2, (RF, RF_IF),     rd_rm),
9252  cCL(sinem,     e888140, 2, (RF, RF_IF),     rd_rm),
9253  cCL(sinez,     e888160, 2, (RF, RF_IF),     rd_rm),
9254
9255  cCL(coss,      e908100, 2, (RF, RF_IF),     rd_rm),
9256  cCL(cossp,     e908120, 2, (RF, RF_IF),     rd_rm),
9257  cCL(cossm,     e908140, 2, (RF, RF_IF),     rd_rm),
9258  cCL(cossz,     e908160, 2, (RF, RF_IF),     rd_rm),
9259  cCL(cosd,      e908180, 2, (RF, RF_IF),     rd_rm),
9260  cCL(cosdp,     e9081a0, 2, (RF, RF_IF),     rd_rm),
9261  cCL(cosdm,     e9081c0, 2, (RF, RF_IF),     rd_rm),
9262  cCL(cosdz,     e9081e0, 2, (RF, RF_IF),     rd_rm),
9263  cCL(cose,      e988100, 2, (RF, RF_IF),     rd_rm),
9264  cCL(cosep,     e988120, 2, (RF, RF_IF),     rd_rm),
9265  cCL(cosem,     e988140, 2, (RF, RF_IF),     rd_rm),
9266  cCL(cosez,     e988160, 2, (RF, RF_IF),     rd_rm),
9267
9268  cCL(tans,      ea08100, 2, (RF, RF_IF),     rd_rm),
9269  cCL(tansp,     ea08120, 2, (RF, RF_IF),     rd_rm),
9270  cCL(tansm,     ea08140, 2, (RF, RF_IF),     rd_rm),
9271  cCL(tansz,     ea08160, 2, (RF, RF_IF),     rd_rm),
9272  cCL(tand,      ea08180, 2, (RF, RF_IF),     rd_rm),
9273  cCL(tandp,     ea081a0, 2, (RF, RF_IF),     rd_rm),
9274  cCL(tandm,     ea081c0, 2, (RF, RF_IF),     rd_rm),
9275  cCL(tandz,     ea081e0, 2, (RF, RF_IF),     rd_rm),
9276  cCL(tane,      ea88100, 2, (RF, RF_IF),     rd_rm),
9277  cCL(tanep,     ea88120, 2, (RF, RF_IF),     rd_rm),
9278  cCL(tanem,     ea88140, 2, (RF, RF_IF),     rd_rm),
9279  cCL(tanez,     ea88160, 2, (RF, RF_IF),     rd_rm),
9280
9281  cCL(asns,      eb08100, 2, (RF, RF_IF),     rd_rm),
9282  cCL(asnsp,     eb08120, 2, (RF, RF_IF),     rd_rm),
9283  cCL(asnsm,     eb08140, 2, (RF, RF_IF),     rd_rm),
9284  cCL(asnsz,     eb08160, 2, (RF, RF_IF),     rd_rm),
9285  cCL(asnd,      eb08180, 2, (RF, RF_IF),     rd_rm),
9286  cCL(asndp,     eb081a0, 2, (RF, RF_IF),     rd_rm),
9287  cCL(asndm,     eb081c0, 2, (RF, RF_IF),     rd_rm),
9288  cCL(asndz,     eb081e0, 2, (RF, RF_IF),     rd_rm),
9289  cCL(asne,      eb88100, 2, (RF, RF_IF),     rd_rm),
9290  cCL(asnep,     eb88120, 2, (RF, RF_IF),     rd_rm),
9291  cCL(asnem,     eb88140, 2, (RF, RF_IF),     rd_rm),
9292  cCL(asnez,     eb88160, 2, (RF, RF_IF),     rd_rm),
9293
9294  cCL(acss,      ec08100, 2, (RF, RF_IF),     rd_rm),
9295  cCL(acssp,     ec08120, 2, (RF, RF_IF),     rd_rm),
9296  cCL(acssm,     ec08140, 2, (RF, RF_IF),     rd_rm),
9297  cCL(acssz,     ec08160, 2, (RF, RF_IF),     rd_rm),
9298  cCL(acsd,      ec08180, 2, (RF, RF_IF),     rd_rm),
9299  cCL(acsdp,     ec081a0, 2, (RF, RF_IF),     rd_rm),
9300  cCL(acsdm,     ec081c0, 2, (RF, RF_IF),     rd_rm),
9301  cCL(acsdz,     ec081e0, 2, (RF, RF_IF),     rd_rm),
9302  cCL(acse,      ec88100, 2, (RF, RF_IF),     rd_rm),
9303  cCL(acsep,     ec88120, 2, (RF, RF_IF),     rd_rm),
9304  cCL(acsem,     ec88140, 2, (RF, RF_IF),     rd_rm),
9305  cCL(acsez,     ec88160, 2, (RF, RF_IF),     rd_rm),
9306
9307  cCL(atns,      ed08100, 2, (RF, RF_IF),     rd_rm),
9308  cCL(atnsp,     ed08120, 2, (RF, RF_IF),     rd_rm),
9309  cCL(atnsm,     ed08140, 2, (RF, RF_IF),     rd_rm),
9310  cCL(atnsz,     ed08160, 2, (RF, RF_IF),     rd_rm),
9311  cCL(atnd,      ed08180, 2, (RF, RF_IF),     rd_rm),
9312  cCL(atndp,     ed081a0, 2, (RF, RF_IF),     rd_rm),
9313  cCL(atndm,     ed081c0, 2, (RF, RF_IF),     rd_rm),
9314  cCL(atndz,     ed081e0, 2, (RF, RF_IF),     rd_rm),
9315  cCL(atne,      ed88100, 2, (RF, RF_IF),     rd_rm),
9316  cCL(atnep,     ed88120, 2, (RF, RF_IF),     rd_rm),
9317  cCL(atnem,     ed88140, 2, (RF, RF_IF),     rd_rm),
9318  cCL(atnez,     ed88160, 2, (RF, RF_IF),     rd_rm),
9319
9320  cCL(urds,      ee08100, 2, (RF, RF_IF),     rd_rm),
9321  cCL(urdsp,     ee08120, 2, (RF, RF_IF),     rd_rm),
9322  cCL(urdsm,     ee08140, 2, (RF, RF_IF),     rd_rm),
9323  cCL(urdsz,     ee08160, 2, (RF, RF_IF),     rd_rm),
9324  cCL(urdd,      ee08180, 2, (RF, RF_IF),     rd_rm),
9325  cCL(urddp,     ee081a0, 2, (RF, RF_IF),     rd_rm),
9326  cCL(urddm,     ee081c0, 2, (RF, RF_IF),     rd_rm),
9327  cCL(urddz,     ee081e0, 2, (RF, RF_IF),     rd_rm),
9328  cCL(urde,      ee88100, 2, (RF, RF_IF),     rd_rm),
9329  cCL(urdep,     ee88120, 2, (RF, RF_IF),     rd_rm),
9330  cCL(urdem,     ee88140, 2, (RF, RF_IF),     rd_rm),
9331  cCL(urdez,     ee88160, 2, (RF, RF_IF),     rd_rm),
9332
9333  cCL(nrms,      ef08100, 2, (RF, RF_IF),     rd_rm),
9334  cCL(nrmsp,     ef08120, 2, (RF, RF_IF),     rd_rm),
9335  cCL(nrmsm,     ef08140, 2, (RF, RF_IF),     rd_rm),
9336  cCL(nrmsz,     ef08160, 2, (RF, RF_IF),     rd_rm),
9337  cCL(nrmd,      ef08180, 2, (RF, RF_IF),     rd_rm),
9338  cCL(nrmdp,     ef081a0, 2, (RF, RF_IF),     rd_rm),
9339  cCL(nrmdm,     ef081c0, 2, (RF, RF_IF),     rd_rm),
9340  cCL(nrmdz,     ef081e0, 2, (RF, RF_IF),     rd_rm),
9341  cCL(nrme,      ef88100, 2, (RF, RF_IF),     rd_rm),
9342  cCL(nrmep,     ef88120, 2, (RF, RF_IF),     rd_rm),
9343  cCL(nrmem,     ef88140, 2, (RF, RF_IF),     rd_rm),
9344  cCL(nrmez,     ef88160, 2, (RF, RF_IF),     rd_rm),
9345
9346  cCL(adfs,      e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
9347  cCL(adfsp,     e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
9348  cCL(adfsm,     e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
9349  cCL(adfsz,     e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
9350  cCL(adfd,      e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
9351  cCL(adfdp,     e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9352  cCL(adfdm,     e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9353  cCL(adfdz,     e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9354  cCL(adfe,      e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
9355  cCL(adfep,     e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
9356  cCL(adfem,     e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
9357  cCL(adfez,     e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
9358
9359  cCL(sufs,      e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
9360  cCL(sufsp,     e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
9361  cCL(sufsm,     e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
9362  cCL(sufsz,     e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
9363  cCL(sufd,      e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
9364  cCL(sufdp,     e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9365  cCL(sufdm,     e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9366  cCL(sufdz,     e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9367  cCL(sufe,      e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
9368  cCL(sufep,     e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
9369  cCL(sufem,     e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
9370  cCL(sufez,     e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
9371
9372  cCL(rsfs,      e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
9373  cCL(rsfsp,     e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
9374  cCL(rsfsm,     e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
9375  cCL(rsfsz,     e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
9376  cCL(rsfd,      e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
9377  cCL(rsfdp,     e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9378  cCL(rsfdm,     e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9379  cCL(rsfdz,     e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9380  cCL(rsfe,      e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
9381  cCL(rsfep,     e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
9382  cCL(rsfem,     e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
9383  cCL(rsfez,     e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
9384
9385  cCL(mufs,      e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
9386  cCL(mufsp,     e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
9387  cCL(mufsm,     e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
9388  cCL(mufsz,     e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
9389  cCL(mufd,      e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
9390  cCL(mufdp,     e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9391  cCL(mufdm,     e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9392  cCL(mufdz,     e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9393  cCL(mufe,      e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
9394  cCL(mufep,     e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
9395  cCL(mufem,     e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
9396  cCL(mufez,     e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
9397
9398  cCL(dvfs,      e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
9399  cCL(dvfsp,     e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
9400  cCL(dvfsm,     e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
9401  cCL(dvfsz,     e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
9402  cCL(dvfd,      e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
9403  cCL(dvfdp,     e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9404  cCL(dvfdm,     e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9405  cCL(dvfdz,     e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9406  cCL(dvfe,      e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
9407  cCL(dvfep,     e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
9408  cCL(dvfem,     e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
9409  cCL(dvfez,     e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
9410
9411  cCL(rdfs,      e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
9412  cCL(rdfsp,     e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
9413  cCL(rdfsm,     e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
9414  cCL(rdfsz,     e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
9415  cCL(rdfd,      e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
9416  cCL(rdfdp,     e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9417  cCL(rdfdm,     e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9418  cCL(rdfdz,     e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9419  cCL(rdfe,      e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
9420  cCL(rdfep,     e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
9421  cCL(rdfem,     e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
9422  cCL(rdfez,     e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
9423
9424  cCL(pows,      e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
9425  cCL(powsp,     e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
9426  cCL(powsm,     e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
9427  cCL(powsz,     e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
9428  cCL(powd,      e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
9429  cCL(powdp,     e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9430  cCL(powdm,     e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9431  cCL(powdz,     e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9432  cCL(powe,      e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
9433  cCL(powep,     e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
9434  cCL(powem,     e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
9435  cCL(powez,     e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
9436
9437  cCL(rpws,      e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
9438  cCL(rpwsp,     e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
9439  cCL(rpwsm,     e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
9440  cCL(rpwsz,     e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
9441  cCL(rpwd,      e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
9442  cCL(rpwdp,     e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9443  cCL(rpwdm,     e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9444  cCL(rpwdz,     e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9445  cCL(rpwe,      e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
9446  cCL(rpwep,     e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
9447  cCL(rpwem,     e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
9448  cCL(rpwez,     e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
9449
9450  cCL(rmfs,      e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
9451  cCL(rmfsp,     e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
9452  cCL(rmfsm,     e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
9453  cCL(rmfsz,     e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
9454  cCL(rmfd,      e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
9455  cCL(rmfdp,     e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9456  cCL(rmfdm,     e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9457  cCL(rmfdz,     e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9458  cCL(rmfe,      e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
9459  cCL(rmfep,     e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
9460  cCL(rmfem,     e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
9461  cCL(rmfez,     e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
9462
9463  cCL(fmls,      e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
9464  cCL(fmlsp,     e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
9465  cCL(fmlsm,     e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
9466  cCL(fmlsz,     e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
9467  cCL(fmld,      e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
9468  cCL(fmldp,     e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9469  cCL(fmldm,     e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9470  cCL(fmldz,     e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9471  cCL(fmle,      e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
9472  cCL(fmlep,     e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
9473  cCL(fmlem,     e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
9474  cCL(fmlez,     e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
9475
9476  cCL(fdvs,      ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
9477  cCL(fdvsp,     ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
9478  cCL(fdvsm,     ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
9479  cCL(fdvsz,     ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
9480  cCL(fdvd,      ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
9481  cCL(fdvdp,     ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9482  cCL(fdvdm,     ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9483  cCL(fdvdz,     ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9484  cCL(fdve,      ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
9485  cCL(fdvep,     ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
9486  cCL(fdvem,     ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
9487  cCL(fdvez,     ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
9488
9489  cCL(frds,      eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
9490  cCL(frdsp,     eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
9491  cCL(frdsm,     eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
9492  cCL(frdsz,     eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
9493  cCL(frdd,      eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
9494  cCL(frddp,     eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9495  cCL(frddm,     eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9496  cCL(frddz,     eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9497  cCL(frde,      eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
9498  cCL(frdep,     eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
9499  cCL(frdem,     eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
9500  cCL(frdez,     eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
9501
9502  cCL(pols,      ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
9503  cCL(polsp,     ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
9504  cCL(polsm,     ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
9505  cCL(polsz,     ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
9506  cCL(pold,      ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
9507  cCL(poldp,     ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9508  cCL(poldm,     ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9509  cCL(poldz,     ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9510  cCL(pole,      ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
9511  cCL(polep,     ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
9512  cCL(polem,     ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
9513  cCL(polez,     ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
9514
9515  cCE(cmf,       e90f110, 2, (RF, RF_IF),     fpa_cmp),
9516  C3E(cmfe,      ed0f110, 2, (RF, RF_IF),     fpa_cmp),
9517  cCE(cnf,       eb0f110, 2, (RF, RF_IF),     fpa_cmp),
9518  C3E(cnfe,      ef0f110, 2, (RF, RF_IF),     fpa_cmp),
9519
9520  cCL(flts,      e000110, 2, (RF, RR),        rn_rd),
9521  cCL(fltsp,     e000130, 2, (RF, RR),        rn_rd),
9522  cCL(fltsm,     e000150, 2, (RF, RR),        rn_rd),
9523  cCL(fltsz,     e000170, 2, (RF, RR),        rn_rd),
9524  cCL(fltd,      e000190, 2, (RF, RR),        rn_rd),
9525  cCL(fltdp,     e0001b0, 2, (RF, RR),        rn_rd),
9526  cCL(fltdm,     e0001d0, 2, (RF, RR),        rn_rd),
9527  cCL(fltdz,     e0001f0, 2, (RF, RR),        rn_rd),
9528  cCL(flte,      e080110, 2, (RF, RR),        rn_rd),
9529  cCL(fltep,     e080130, 2, (RF, RR),        rn_rd),
9530  cCL(fltem,     e080150, 2, (RF, RR),        rn_rd),
9531  cCL(fltez,     e080170, 2, (RF, RR),        rn_rd),
9532
9533   /* The implementation of the FIX instruction is broken on some
9534      assemblers, in that it accepts a precision specifier as well as a
9535      rounding specifier, despite the fact that this is meaningless.
9536      To be more compatible, we accept it as well, though of course it
9537      does not set any bits.  */
9538  cCE(fix,       e100110, 2, (RR, RF),        rd_rm),
9539  cCL(fixp,      e100130, 2, (RR, RF),        rd_rm),
9540  cCL(fixm,      e100150, 2, (RR, RF),        rd_rm),
9541  cCL(fixz,      e100170, 2, (RR, RF),        rd_rm),
9542  cCL(fixsp,     e100130, 2, (RR, RF),        rd_rm),
9543  cCL(fixsm,     e100150, 2, (RR, RF),        rd_rm),
9544  cCL(fixsz,     e100170, 2, (RR, RF),        rd_rm),
9545  cCL(fixdp,     e100130, 2, (RR, RF),        rd_rm),
9546  cCL(fixdm,     e100150, 2, (RR, RF),        rd_rm),
9547  cCL(fixdz,     e100170, 2, (RR, RF),        rd_rm),
9548  cCL(fixep,     e100130, 2, (RR, RF),        rd_rm),
9549  cCL(fixem,     e100150, 2, (RR, RF),        rd_rm),
9550  cCL(fixez,     e100170, 2, (RR, RF),        rd_rm),
9551
9552   /* Instructions that were new with the real FPA, call them V2.  */
9553 #undef ARM_VARIANT
9554 #define ARM_VARIANT &fpu_fpa_ext_v2
9555  cCE(lfm,       c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9556  cCL(lfmfd,     c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9557  cCL(lfmea,     d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9558  cCE(sfm,       c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9559  cCL(sfmfd,     d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9560  cCL(sfmea,     c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9561
9562 #undef ARM_VARIANT
9563 #define ARM_VARIANT &fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
9564   /* Moves and type conversions.  */
9565  cCE(fcpys,     eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
9566  cCE(fmrs,      e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
9567  cCE(fmsr,      e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
9568  cCE(fmstat,    ef1fa10, 0, (),               noargs),
9569  cCE(fsitos,    eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9570  cCE(fuitos,    eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
9571  cCE(ftosis,    ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
9572  cCE(ftosizs,   ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9573  cCE(ftouis,    ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
9574  cCE(ftouizs,   ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9575  cCE(fmrx,      ef00a10, 2, (RR, RVC),        rd_rn),
9576  cCE(fmxr,      ee00a10, 2, (RVC, RR),        rn_rd),
9577
9578   /* Memory operations.  */
9579  cCE(flds,      d100a00, 2, (RVS, ADDR),      vfp_sp_ldst),
9580  cCE(fsts,      d000a00, 2, (RVS, ADDR),      vfp_sp_ldst),
9581  cCE(fldmias,   c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9582  cCE(fldmfds,   c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9583  cCE(fldmdbs,   d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
9584  cCE(fldmeas,   d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
9585  cCE(fldmiax,   c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
9586  cCE(fldmfdx,   c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
9587  cCE(fldmdbx,   d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
9588  cCE(fldmeax,   d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
9589  cCE(fstmias,   c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9590  cCE(fstmeas,   c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9591  cCE(fstmdbs,   d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
9592  cCE(fstmfds,   d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
9593  cCE(fstmiax,   c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
9594  cCE(fstmeax,   c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
9595  cCE(fstmdbx,   d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
9596  cCE(fstmfdx,   d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
9597
9598   /* Monadic operations.  */
9599  cCE(fabss,     eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9600  cCE(fnegs,     eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
9601  cCE(fsqrts,    eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9602
9603   /* Dyadic operations.  */
9604  cCE(fadds,     e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9605  cCE(fsubs,     e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9606  cCE(fmuls,     e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9607  cCE(fdivs,     e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9608  cCE(fmacs,     e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9609  cCE(fmscs,     e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9610  cCE(fnmuls,    e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9611  cCE(fnmacs,    e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9612  cCE(fnmscs,    e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9613
9614   /* Comparisons.  */
9615  cCE(fcmps,     eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
9616  cCE(fcmpzs,    eb50a40, 1, (RVS),            vfp_sp_compare_z),
9617  cCE(fcmpes,    eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9618  cCE(fcmpezs,   eb50ac0, 1, (RVS),            vfp_sp_compare_z),
9619
9620 #undef ARM_VARIANT
9621 #define ARM_VARIANT &fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
9622   /* Moves and type conversions.  */
9623  cCE(fcpyd,     eb00b40, 2, (RVD, RVD),       rd_rm),
9624  cCE(fcvtds,    eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
9625  cCE(fcvtsd,    eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9626  cCE(fmdhr,     e200b10, 2, (RVD, RR),        rn_rd),
9627  cCE(fmdlr,     e000b10, 2, (RVD, RR),        rn_rd),
9628  cCE(fmrdh,     e300b10, 2, (RR, RVD),        rd_rn),
9629  cCE(fmrdl,     e100b10, 2, (RR, RVD),        rd_rn),
9630  cCE(fsitod,    eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
9631  cCE(fuitod,    eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
9632  cCE(ftosid,    ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9633  cCE(ftosizd,   ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9634  cCE(ftouid,    ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9635  cCE(ftouizd,   ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9636
9637   /* Memory operations.  */
9638  cCE(fldd,      d100b00, 2, (RVD, ADDR),      vfp_dp_ldst),
9639  cCE(fstd,      d000b00, 2, (RVD, ADDR),      vfp_dp_ldst),
9640  cCE(fldmiad,   c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
9641  cCE(fldmfdd,   c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
9642  cCE(fldmdbd,   d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
9643  cCE(fldmead,   d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
9644  cCE(fstmiad,   c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
9645  cCE(fstmead,   c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
9646  cCE(fstmdbd,   d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
9647  cCE(fstmfdd,   d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
9648
9649   /* Monadic operations.  */
9650  cCE(fabsd,     eb00bc0, 2, (RVD, RVD),       rd_rm),
9651  cCE(fnegd,     eb10b40, 2, (RVD, RVD),       rd_rm),
9652  cCE(fsqrtd,    eb10bc0, 2, (RVD, RVD),       rd_rm),
9653
9654   /* Dyadic operations.  */
9655  cCE(faddd,     e300b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9656  cCE(fsubd,     e300b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
9657  cCE(fmuld,     e200b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9658  cCE(fdivd,     e800b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9659  cCE(fmacd,     e000b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9660  cCE(fmscd,     e100b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9661  cCE(fnmuld,    e200b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
9662  cCE(fnmacd,    e000b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
9663  cCE(fnmscd,    e100b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
9664
9665   /* Comparisons.  */
9666  cCE(fcmpd,     eb40b40, 2, (RVD, RVD),       rd_rm),
9667  cCE(fcmpzd,    eb50b40, 1, (RVD),            rd),
9668  cCE(fcmped,    eb40bc0, 2, (RVD, RVD),       rd_rm),
9669  cCE(fcmpezd,   eb50bc0, 1, (RVD),            rd),
9670
9671 #undef ARM_VARIANT
9672 #define ARM_VARIANT &fpu_vfp_ext_v2
9673  cCE(fmsrr,     c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
9674  cCE(fmrrs,     c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
9675  cCE(fmdrr,     c400b10, 3, (RVD, RR, RR),    rm_rd_rn),
9676  cCE(fmrrd,     c500b10, 3, (RR, RR, RVD),    rd_rn_rm),
9677
9678 #undef ARM_VARIANT
9679 #define ARM_VARIANT &arm_cext_xscale /* Intel XScale extensions.         */
9680  cCE(mia,       e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9681  cCE(miaph,     e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9682  cCE(miabb,     e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9683  cCE(miabt,     e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9684  cCE(miatb,     e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9685  cCE(miatt,     e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9686  cCE(mar,       c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
9687  cCE(mra,       c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
9688
9689 #undef ARM_VARIANT
9690 #define ARM_VARIANT &arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
9691  cCE(tandcb,    e13f130, 1, (RR),                   iwmmxt_tandorc),
9692  cCE(tandch,    e53f130, 1, (RR),                   iwmmxt_tandorc),
9693  cCE(tandcw,    e93f130, 1, (RR),                   iwmmxt_tandorc),
9694  cCE(tbcstb,    e400010, 2, (RIWR, RR),             rn_rd),
9695  cCE(tbcsth,    e400050, 2, (RIWR, RR),             rn_rd),
9696  cCE(tbcstw,    e400090, 2, (RIWR, RR),             rn_rd),
9697  cCE(textrcb,   e130170, 2, (RR, I7),               iwmmxt_textrc),
9698  cCE(textrch,   e530170, 2, (RR, I7),               iwmmxt_textrc),
9699  cCE(textrcw,   e930170, 2, (RR, I7),               iwmmxt_textrc),
9700  cCE(textrmub,  e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9701  cCE(textrmuh,  e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9702  cCE(textrmuw,  e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9703  cCE(textrmsb,  e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9704  cCE(textrmsh,  e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9705  cCE(textrmsw,  e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9706  cCE(tinsrb,    e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
9707  cCE(tinsrh,    e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
9708  cCE(tinsrw,    e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
9709  cCE(tmcr,      e000110, 2, (RIWC, RR),             rn_rd),
9710  cCE(tmcrr,     c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
9711  cCE(tmia,      e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9712  cCE(tmiaph,    e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9713  cCE(tmiabb,    e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9714  cCE(tmiabt,    e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9715  cCE(tmiatb,    e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9716  cCE(tmiatt,    e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9717  cCE(tmovmskb,  e100030, 2, (RR, RIWR),             rd_rn),
9718  cCE(tmovmskh,  e500030, 2, (RR, RIWR),             rd_rn),
9719  cCE(tmovmskw,  e900030, 2, (RR, RIWR),             rd_rn),
9720  cCE(tmrc,      e100110, 2, (RR, RIWC),             rd_rn),
9721  cCE(tmrrc,     c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
9722  cCE(torcb,     e13f150, 1, (RR),                   iwmmxt_tandorc),
9723  cCE(torch,     e53f150, 1, (RR),                   iwmmxt_tandorc),
9724  cCE(torcw,     e93f150, 1, (RR),                   iwmmxt_tandorc),
9725  cCE(waccb,     e0001c0, 2, (RIWR, RIWR),           rd_rn),
9726  cCE(wacch,     e4001c0, 2, (RIWR, RIWR),           rd_rn),
9727  cCE(waccw,     e8001c0, 2, (RIWR, RIWR),           rd_rn),
9728  cCE(waddbss,   e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9729  cCE(waddb,     e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9730  cCE(waddbus,   e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9731  cCE(waddhss,   e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9732  cCE(waddh,     e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9733  cCE(waddhus,   e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9734  cCE(waddwss,   eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9735  cCE(waddw,     e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9736  cCE(waddwus,   e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9737  cCE(waligni,   e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
9738  cCE(walignr0,  e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9739  cCE(walignr1,  e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9740  cCE(walignr2,  ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9741  cCE(walignr3,  eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9742  cCE(wand,      e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9743  cCE(wandn,     e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9744  cCE(wavg2b,    e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9745  cCE(wavg2br,   e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9746  cCE(wavg2h,    ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9747  cCE(wavg2hr,   ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9748  cCE(wcmpeqb,   e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9749  cCE(wcmpeqh,   e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9750  cCE(wcmpeqw,   e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9751  cCE(wcmpgtub,  e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9752  cCE(wcmpgtuh,  e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9753  cCE(wcmpgtuw,  e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9754  cCE(wcmpgtsb,  e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9755  cCE(wcmpgtsh,  e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9756  cCE(wcmpgtsw,  eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9757  cCE(wldrb,     c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
9758  cCE(wldrh,     c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
9759  cCE(wldrw,     c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
9760  cCE(wldrd,     c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
9761  cCE(wmacs,     e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9762  cCE(wmacsz,    e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9763  cCE(wmacu,     e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9764  cCE(wmacuz,    e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9765  cCE(wmadds,    ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9766  cCE(wmaddu,    e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9767  cCE(wmaxsb,    e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9768  cCE(wmaxsh,    e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9769  cCE(wmaxsw,    ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9770  cCE(wmaxub,    e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9771  cCE(wmaxuh,    e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9772  cCE(wmaxuw,    e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9773  cCE(wminsb,    e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9774  cCE(wminsh,    e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9775  cCE(wminsw,    eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9776  cCE(wminub,    e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9777  cCE(wminuh,    e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9778  cCE(wminuw,    e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9779  cCE(wmov,      e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
9780  cCE(wmulsm,    e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9781  cCE(wmulsl,    e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9782  cCE(wmulum,    e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9783  cCE(wmulul,    e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9784  cCE(wor,       e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9785  cCE(wpackhss,  e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9786  cCE(wpackhus,  e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9787  cCE(wpackwss,  eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9788  cCE(wpackwus,  e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9789  cCE(wpackdss,  ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9790  cCE(wpackdus,  ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9791  cCE(wrorh,     e700040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9792  cCE(wrorhg,    e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9793  cCE(wrorw,     eb00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9794  cCE(wrorwg,    eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9795  cCE(wrord,     ef00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9796  cCE(wrordg,    ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9797  cCE(wsadb,     e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9798  cCE(wsadbz,    e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9799  cCE(wsadh,     e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9800  cCE(wsadhz,    e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9801  cCE(wshufh,    e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
9802  cCE(wsllh,     e500040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9803  cCE(wsllhg,    e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9804  cCE(wsllw,     e900040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9805  cCE(wsllwg,    e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9806  cCE(wslld,     ed00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9807  cCE(wslldg,    ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9808  cCE(wsrah,     e400040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9809  cCE(wsrahg,    e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9810  cCE(wsraw,     e800040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9811  cCE(wsrawg,    e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9812  cCE(wsrad,     ec00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9813  cCE(wsradg,    ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9814  cCE(wsrlh,     e600040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9815  cCE(wsrlhg,    e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9816  cCE(wsrlw,     ea00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9817  cCE(wsrlwg,    ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9818  cCE(wsrld,     ee00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9819  cCE(wsrldg,    ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9820  cCE(wstrb,     c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
9821  cCE(wstrh,     c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
9822  cCE(wstrw,     c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
9823  cCE(wstrd,     c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
9824  cCE(wsubbss,   e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9825  cCE(wsubb,     e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9826  cCE(wsubbus,   e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9827  cCE(wsubhss,   e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9828  cCE(wsubh,     e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9829  cCE(wsubhus,   e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9830  cCE(wsubwss,   eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9831  cCE(wsubw,     e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9832  cCE(wsubwus,   e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9833  cCE(wunpckehub,e0000c0, 2, (RIWR, RIWR),           rd_rn),
9834  cCE(wunpckehuh,e4000c0, 2, (RIWR, RIWR),           rd_rn),
9835  cCE(wunpckehuw,e8000c0, 2, (RIWR, RIWR),           rd_rn),
9836  cCE(wunpckehsb,e2000c0, 2, (RIWR, RIWR),           rd_rn),
9837  cCE(wunpckehsh,e6000c0, 2, (RIWR, RIWR),           rd_rn),
9838  cCE(wunpckehsw,ea000c0, 2, (RIWR, RIWR),           rd_rn),
9839  cCE(wunpckihb, e1000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9840  cCE(wunpckihh, e5000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9841  cCE(wunpckihw, e9000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9842  cCE(wunpckelub,e0000e0, 2, (RIWR, RIWR),           rd_rn),
9843  cCE(wunpckeluh,e4000e0, 2, (RIWR, RIWR),           rd_rn),
9844  cCE(wunpckeluw,e8000e0, 2, (RIWR, RIWR),           rd_rn),
9845  cCE(wunpckelsb,e2000e0, 2, (RIWR, RIWR),           rd_rn),
9846  cCE(wunpckelsh,e6000e0, 2, (RIWR, RIWR),           rd_rn),
9847  cCE(wunpckelsw,ea000e0, 2, (RIWR, RIWR),           rd_rn),
9848  cCE(wunpckilb, e1000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9849  cCE(wunpckilh, e5000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9850  cCE(wunpckilw, e9000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9851  cCE(wxor,      e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9852  cCE(wzero,     e300000, 1, (RIWR),                 iwmmxt_wzero),
9853
9854 #undef ARM_VARIANT
9855 #define ARM_VARIANT &arm_cext_maverick /* Cirrus Maverick instructions. */
9856  cCE(cfldrs,    c100400, 2, (RMF, ADDR),              rd_cpaddr),
9857  cCE(cfldrd,    c500400, 2, (RMD, ADDR),              rd_cpaddr),
9858  cCE(cfldr32,   c100500, 2, (RMFX, ADDR),             rd_cpaddr),
9859  cCE(cfldr64,   c500500, 2, (RMDX, ADDR),             rd_cpaddr),
9860  cCE(cfstrs,    c000400, 2, (RMF, ADDR),              rd_cpaddr),
9861  cCE(cfstrd,    c400400, 2, (RMD, ADDR),              rd_cpaddr),
9862  cCE(cfstr32,   c000500, 2, (RMFX, ADDR),             rd_cpaddr),
9863  cCE(cfstr64,   c400500, 2, (RMDX, ADDR),             rd_cpaddr),
9864  cCE(cfmvsr,    e000450, 2, (RMF, RR),                rn_rd),
9865  cCE(cfmvrs,    e100450, 2, (RR, RMF),                rd_rn),
9866  cCE(cfmvdlr,   e000410, 2, (RMD, RR),                rn_rd),
9867  cCE(cfmvrdl,   e100410, 2, (RR, RMD),                rd_rn),
9868  cCE(cfmvdhr,   e000430, 2, (RMD, RR),                rn_rd),
9869  cCE(cfmvrdh,   e100430, 2, (RR, RMD),                rd_rn),
9870  cCE(cfmv64lr,  e000510, 2, (RMDX, RR),               rn_rd),
9871  cCE(cfmvr64l,  e100510, 2, (RR, RMDX),               rd_rn),
9872  cCE(cfmv64hr,  e000530, 2, (RMDX, RR),               rn_rd),
9873  cCE(cfmvr64h,  e100530, 2, (RR, RMDX),               rd_rn),
9874  cCE(cfmval32,  e200440, 2, (RMAX, RMFX),             rd_rn),
9875  cCE(cfmv32al,  e100440, 2, (RMFX, RMAX),             rd_rn),
9876  cCE(cfmvam32,  e200460, 2, (RMAX, RMFX),             rd_rn),
9877  cCE(cfmv32am,  e100460, 2, (RMFX, RMAX),             rd_rn),
9878  cCE(cfmvah32,  e200480, 2, (RMAX, RMFX),             rd_rn),
9879  cCE(cfmv32ah,  e100480, 2, (RMFX, RMAX),             rd_rn),
9880  cCE(cfmva32,   e2004a0, 2, (RMAX, RMFX),             rd_rn),
9881  cCE(cfmv32a,   e1004a0, 2, (RMFX, RMAX),             rd_rn),
9882  cCE(cfmva64,   e2004c0, 2, (RMAX, RMDX),             rd_rn),
9883  cCE(cfmv64a,   e1004c0, 2, (RMDX, RMAX),             rd_rn),
9884  cCE(cfmvsc32,  e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
9885  cCE(cfmv32sc,  e1004e0, 2, (RMDX, RMDS),             rd),
9886  cCE(cfcpys,    e000400, 2, (RMF, RMF),               rd_rn),
9887  cCE(cfcpyd,    e000420, 2, (RMD, RMD),               rd_rn),
9888  cCE(cfcvtsd,   e000460, 2, (RMD, RMF),               rd_rn),
9889  cCE(cfcvtds,   e000440, 2, (RMF, RMD),               rd_rn),
9890  cCE(cfcvt32s,  e000480, 2, (RMF, RMFX),              rd_rn),
9891  cCE(cfcvt32d,  e0004a0, 2, (RMD, RMFX),              rd_rn),
9892  cCE(cfcvt64s,  e0004c0, 2, (RMF, RMDX),              rd_rn),
9893  cCE(cfcvt64d,  e0004e0, 2, (RMD, RMDX),              rd_rn),
9894  cCE(cfcvts32,  e100580, 2, (RMFX, RMF),              rd_rn),
9895  cCE(cfcvtd32,  e1005a0, 2, (RMFX, RMD),              rd_rn),
9896  cCE(cftruncs32,e1005c0, 2, (RMFX, RMF),              rd_rn),
9897  cCE(cftruncd32,e1005e0, 2, (RMFX, RMD),              rd_rn),
9898  cCE(cfrshl32,  e000550, 3, (RMFX, RMFX, RR),         mav_triple),
9899  cCE(cfrshl64,  e000570, 3, (RMDX, RMDX, RR),         mav_triple),
9900  cCE(cfsh32,    e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
9901  cCE(cfsh64,    e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
9902  cCE(cfcmps,    e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
9903  cCE(cfcmpd,    e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
9904  cCE(cfcmp32,   e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
9905  cCE(cfcmp64,   e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
9906  cCE(cfabss,    e300400, 2, (RMF, RMF),               rd_rn),
9907  cCE(cfabsd,    e300420, 2, (RMD, RMD),               rd_rn),
9908  cCE(cfnegs,    e300440, 2, (RMF, RMF),               rd_rn),
9909  cCE(cfnegd,    e300460, 2, (RMD, RMD),               rd_rn),
9910  cCE(cfadds,    e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
9911  cCE(cfaddd,    e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
9912  cCE(cfsubs,    e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
9913  cCE(cfsubd,    e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
9914  cCE(cfmuls,    e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
9915  cCE(cfmuld,    e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
9916  cCE(cfabs32,   e300500, 2, (RMFX, RMFX),             rd_rn),
9917  cCE(cfabs64,   e300520, 2, (RMDX, RMDX),             rd_rn),
9918  cCE(cfneg32,   e300540, 2, (RMFX, RMFX),             rd_rn),
9919  cCE(cfneg64,   e300560, 2, (RMDX, RMDX),             rd_rn),
9920  cCE(cfadd32,   e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9921  cCE(cfadd64,   e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
9922  cCE(cfsub32,   e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9923  cCE(cfsub64,   e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
9924  cCE(cfmul32,   e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9925  cCE(cfmul64,   e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
9926  cCE(cfmac32,   e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9927  cCE(cfmsc32,   e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9928  cCE(cfmadd32,  e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
9929  cCE(cfmsub32,  e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
9930  cCE(cfmadda32, e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
9931  cCE(cfmsuba32, e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
9932 };
9933 #undef ARM_VARIANT
9934 #undef THUMB_VARIANT
9935 #undef TCE
9936 #undef TCM
9937 #undef TUE
9938 #undef TUF
9939 #undef TCC
9940 #undef cCE
9941 #undef cCL
9942 #undef C3E
9943 #undef CE
9944 #undef CM
9945 #undef UE
9946 #undef UF
9947 #undef UT
9948 #undef OPS0
9949 #undef OPS1
9950 #undef OPS2
9951 #undef OPS3
9952 #undef OPS4
9953 #undef OPS5
9954 #undef OPS6
9955 #undef do_0
9956 \f
9957 /* MD interface: bits in the object file.  */
9958
9959 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
9960    for use in the a.out file, and stores them in the array pointed to by buf.
9961    This knows about the endian-ness of the target machine and does
9962    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
9963    2 (short) and 4 (long)  Floating numbers are put out as a series of
9964    LITTLENUMS (shorts, here at least).  */
9965
9966 void
9967 md_number_to_chars (char * buf, valueT val, int n)
9968 {
9969   if (target_big_endian)
9970     number_to_chars_bigendian (buf, val, n);
9971   else
9972     number_to_chars_littleendian (buf, val, n);
9973 }
9974
9975 static valueT
9976 md_chars_to_number (char * buf, int n)
9977 {
9978   valueT result = 0;
9979   unsigned char * where = (unsigned char *) buf;
9980
9981   if (target_big_endian)
9982     {
9983       while (n--)
9984         {
9985           result <<= 8;
9986           result |= (*where++ & 255);
9987         }
9988     }
9989   else
9990     {
9991       while (n--)
9992         {
9993           result <<= 8;
9994           result |= (where[n] & 255);
9995         }
9996     }
9997
9998   return result;
9999 }
10000
10001 /* MD interface: Sections.  */
10002
10003 /* Estimate the size of a frag before relaxing.  Assume everything fits in
10004    2 bytes.  */
10005
10006 int
10007 md_estimate_size_before_relax (fragS * fragp,
10008                                segT    segtype ATTRIBUTE_UNUSED)
10009 {
10010   fragp->fr_var = 2;
10011   return 2;
10012 }
10013
10014 /* Convert a machine dependent frag.  */
10015
10016 void
10017 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
10018 {
10019   unsigned long insn;
10020   unsigned long old_op;
10021   char *buf;
10022   expressionS exp;
10023   fixS *fixp;
10024   int reloc_type;
10025   int pc_rel;
10026   int opcode;
10027
10028   buf = fragp->fr_literal + fragp->fr_fix;
10029
10030   old_op = bfd_get_16(abfd, buf);
10031   if (fragp->fr_symbol) {
10032       exp.X_op = O_symbol;
10033       exp.X_add_symbol = fragp->fr_symbol;
10034   } else {
10035       exp.X_op = O_constant;
10036   }
10037   exp.X_add_number = fragp->fr_offset;
10038   opcode = fragp->fr_subtype;
10039   switch (opcode)
10040     {
10041     case T_MNEM_ldr_pc:
10042     case T_MNEM_ldr_pc2:
10043     case T_MNEM_ldr_sp:
10044     case T_MNEM_str_sp:
10045     case T_MNEM_ldr:
10046     case T_MNEM_ldrb:
10047     case T_MNEM_ldrh:
10048     case T_MNEM_str:
10049     case T_MNEM_strb:
10050     case T_MNEM_strh:
10051       if (fragp->fr_var == 4)
10052         {
10053           insn = THUMB_OP32(opcode);
10054           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
10055             {
10056               insn |= (old_op & 0x700) << 4;
10057             }
10058           else
10059             {
10060               insn |= (old_op & 7) << 12;
10061               insn |= (old_op & 0x38) << 13;
10062             }
10063           insn |= 0x00000c00;
10064           put_thumb32_insn (buf, insn);
10065           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10066         }
10067       else
10068         {
10069           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
10070         }
10071       pc_rel = (opcode == T_MNEM_ldr_pc2);
10072       break;
10073     case T_MNEM_adr:
10074       if (fragp->fr_var == 4)
10075         {
10076           insn = THUMB_OP32 (opcode);
10077           insn |= (old_op & 0xf0) << 4;
10078           put_thumb32_insn (buf, insn);
10079           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
10080         }
10081       else
10082         {
10083           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
10084           exp.X_add_number -= 4;
10085         }
10086       pc_rel = 1;
10087       break;
10088     case T_MNEM_mov:
10089     case T_MNEM_movs:
10090     case T_MNEM_cmp:
10091     case T_MNEM_cmn:
10092       if (fragp->fr_var == 4)
10093         {
10094           int r0off = (opcode == T_MNEM_mov
10095                        || opcode == T_MNEM_movs) ? 0 : 8;
10096           insn = THUMB_OP32 (opcode);
10097           insn = (insn & 0xe1ffffff) | 0x10000000;
10098           insn |= (old_op & 0x700) << r0off;
10099           put_thumb32_insn (buf, insn);
10100           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
10101         }
10102       else
10103         {
10104           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
10105         }
10106       pc_rel = 0;
10107       break;
10108     case T_MNEM_b:
10109       if (fragp->fr_var == 4)
10110         {
10111           insn = THUMB_OP32(opcode);
10112           put_thumb32_insn (buf, insn);
10113           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
10114         }
10115       else
10116         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
10117       pc_rel = 1;
10118       break;
10119     case T_MNEM_bcond:
10120       if (fragp->fr_var == 4)
10121         {
10122           insn = THUMB_OP32(opcode);
10123           insn |= (old_op & 0xf00) << 14;
10124           put_thumb32_insn (buf, insn);
10125           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
10126         }
10127       else
10128         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
10129       pc_rel = 1;
10130       break;
10131     case T_MNEM_add_sp:
10132     case T_MNEM_add_pc:
10133     case T_MNEM_inc_sp:
10134     case T_MNEM_dec_sp:
10135       if (fragp->fr_var == 4)
10136         {
10137           /* ??? Choose between add and addw.  */
10138           insn = THUMB_OP32 (opcode);
10139           insn |= (old_op & 0xf0) << 4;
10140           put_thumb32_insn (buf, insn);
10141           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
10142         }
10143       else
10144         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
10145       pc_rel = 0;
10146       break;
10147
10148     case T_MNEM_addi:
10149     case T_MNEM_addis:
10150     case T_MNEM_subi:
10151     case T_MNEM_subis:
10152       if (fragp->fr_var == 4)
10153         {
10154           insn = THUMB_OP32 (opcode);
10155           insn |= (old_op & 0xf0) << 4;
10156           insn |= (old_op & 0xf) << 16;
10157           put_thumb32_insn (buf, insn);
10158           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
10159         }
10160       else
10161         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
10162       pc_rel = 0;
10163       break;
10164     default:
10165       abort();
10166     }
10167   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
10168                       reloc_type);
10169   fixp->fx_file = fragp->fr_file;
10170   fixp->fx_line = fragp->fr_line;
10171   fragp->fr_fix += fragp->fr_var;
10172 }
10173
10174 /* Return the size of a relaxable immediate operand instruction.
10175    SHIFT and SIZE specify the form of the allowable immediate.  */
10176 static int
10177 relax_immediate (fragS *fragp, int size, int shift)
10178 {
10179   offsetT offset;
10180   offsetT mask;
10181   offsetT low;
10182
10183   /* ??? Should be able to do better than this.  */
10184   if (fragp->fr_symbol)
10185     return 4;
10186
10187   low = (1 << shift) - 1;
10188   mask = (1 << (shift + size)) - (1 << shift);
10189   offset = fragp->fr_offset;
10190   /* Force misaligned offsets to 32-bit variant.  */
10191   if (offset & low)
10192     return -4;
10193   if (offset & ~mask)
10194     return 4;
10195   return 2;
10196 }
10197
10198 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
10199    load.  */
10200 static int
10201 relax_adr (fragS *fragp, asection *sec)
10202 {
10203   addressT addr;
10204   offsetT val;
10205
10206   /* Assume worst case for symbols not known to be in the same section.  */
10207   if (!S_IS_DEFINED(fragp->fr_symbol)
10208       || sec != S_GET_SEGMENT (fragp->fr_symbol))
10209     return 4;
10210
10211   val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
10212   addr = fragp->fr_address + fragp->fr_fix;
10213   addr = (addr + 4) & ~3;
10214   /* Fix the insn as the 4-byte version if the target address is not
10215      sufficiently aligned.  This is prevents an infinite loop when two
10216      instructions have contradictory range/alignment requirements.  */
10217   if (val & 3)
10218     return -4;
10219   val -= addr;
10220   if (val < 0 || val > 1020)
10221     return 4;
10222   return 2;
10223 }
10224
10225 /* Return the size of a relaxable add/sub immediate instruction.  */
10226 static int
10227 relax_addsub (fragS *fragp, asection *sec)
10228 {
10229   char *buf;
10230   int op;
10231
10232   buf = fragp->fr_literal + fragp->fr_fix;
10233   op = bfd_get_16(sec->owner, buf);
10234   if ((op & 0xf) == ((op >> 4) & 0xf))
10235     return relax_immediate (fragp, 8, 0);
10236   else
10237     return relax_immediate (fragp, 3, 0);
10238 }
10239
10240
10241 /* Return the size of a relaxable branch instruction.  BITS is the
10242    size of the offset field in the narrow instruction.  */
10243
10244 static int
10245 relax_branch (fragS *fragp, asection *sec, int bits)
10246 {
10247   addressT addr;
10248   offsetT val;
10249   offsetT limit;
10250
10251   /* Assume worst case for symbols not known to be in the same section.  */
10252   if (!S_IS_DEFINED(fragp->fr_symbol)
10253       || sec != S_GET_SEGMENT (fragp->fr_symbol))
10254     return 4;
10255
10256   val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
10257   addr = fragp->fr_address + fragp->fr_fix + 4;
10258   val -= addr;
10259
10260   /* Offset is a signed value *2 */
10261   limit = 1 << bits;
10262   if (val >= limit || val < -limit)
10263     return 4;
10264   return 2;
10265 }
10266
10267
10268 /* Relax a machine dependent frag.  This returns the amount by which
10269    the current size of the frag should change.  */
10270
10271 int
10272 arm_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
10273 {
10274   int oldsize;
10275   int newsize;
10276
10277   oldsize = fragp->fr_var;
10278   switch (fragp->fr_subtype)
10279     {
10280     case T_MNEM_ldr_pc2:
10281       newsize = relax_adr(fragp, sec);
10282       break;
10283     case T_MNEM_ldr_pc:
10284     case T_MNEM_ldr_sp:
10285     case T_MNEM_str_sp:
10286       newsize = relax_immediate(fragp, 8, 2);
10287       break;
10288     case T_MNEM_ldr:
10289     case T_MNEM_str:
10290       newsize = relax_immediate(fragp, 5, 2);
10291       break;
10292     case T_MNEM_ldrh:
10293     case T_MNEM_strh:
10294       newsize = relax_immediate(fragp, 5, 1);
10295       break;
10296     case T_MNEM_ldrb:
10297     case T_MNEM_strb:
10298       newsize = relax_immediate(fragp, 5, 0);
10299       break;
10300     case T_MNEM_adr:
10301       newsize = relax_adr(fragp, sec);
10302       break;
10303     case T_MNEM_mov:
10304     case T_MNEM_movs:
10305     case T_MNEM_cmp:
10306     case T_MNEM_cmn:
10307       newsize = relax_immediate(fragp, 8, 0);
10308       break;
10309     case T_MNEM_b:
10310       newsize = relax_branch(fragp, sec, 11);
10311       break;
10312     case T_MNEM_bcond:
10313       newsize = relax_branch(fragp, sec, 8);
10314       break;
10315     case T_MNEM_add_sp:
10316     case T_MNEM_add_pc:
10317       newsize = relax_immediate (fragp, 8, 2);
10318       break;
10319     case T_MNEM_inc_sp:
10320     case T_MNEM_dec_sp:
10321       newsize = relax_immediate (fragp, 7, 2);
10322       break;
10323     case T_MNEM_addi:
10324     case T_MNEM_addis:
10325     case T_MNEM_subi:
10326     case T_MNEM_subis:
10327       newsize = relax_addsub (fragp, sec);
10328       break;
10329     default:
10330       abort();
10331     }
10332   if (newsize < 0)
10333     {
10334       fragp->fr_var = -newsize;
10335       md_convert_frag (sec->owner, sec, fragp);
10336       frag_wane(fragp);
10337       return -(newsize + oldsize);
10338     }
10339   fragp->fr_var = newsize;
10340   return newsize - oldsize;
10341 }
10342
10343 /* Round up a section size to the appropriate boundary.  */
10344
10345 valueT
10346 md_section_align (segT   segment ATTRIBUTE_UNUSED,
10347                   valueT size)
10348 {
10349 #ifdef OBJ_ELF
10350   return size;
10351 #else
10352   /* Round all sects to multiple of 4.  */
10353   return (size + 3) & ~3;
10354 #endif
10355 }
10356
10357 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
10358    of an rs_align_code fragment.  */
10359
10360 void
10361 arm_handle_align (fragS * fragP)
10362 {
10363   static char const arm_noop[4] = { 0x00, 0x00, 0xa0, 0xe1 };
10364   static char const thumb_noop[2] = { 0xc0, 0x46 };
10365   static char const arm_bigend_noop[4] = { 0xe1, 0xa0, 0x00, 0x00 };
10366   static char const thumb_bigend_noop[2] = { 0x46, 0xc0 };
10367
10368   int bytes, fix, noop_size;
10369   char * p;
10370   const char * noop;
10371
10372   if (fragP->fr_type != rs_align_code)
10373     return;
10374
10375   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
10376   p = fragP->fr_literal + fragP->fr_fix;
10377   fix = 0;
10378
10379   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
10380     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
10381
10382   if (fragP->tc_frag_data)
10383     {
10384       if (target_big_endian)
10385         noop = thumb_bigend_noop;
10386       else
10387         noop = thumb_noop;
10388       noop_size = sizeof (thumb_noop);
10389     }
10390   else
10391     {
10392       if (target_big_endian)
10393         noop = arm_bigend_noop;
10394       else
10395         noop = arm_noop;
10396       noop_size = sizeof (arm_noop);
10397     }
10398
10399   if (bytes & (noop_size - 1))
10400     {
10401       fix = bytes & (noop_size - 1);
10402       memset (p, 0, fix);
10403       p += fix;
10404       bytes -= fix;
10405     }
10406
10407   while (bytes >= noop_size)
10408     {
10409       memcpy (p, noop, noop_size);
10410       p += noop_size;
10411       bytes -= noop_size;
10412       fix += noop_size;
10413     }
10414
10415   fragP->fr_fix += fix;
10416   fragP->fr_var = noop_size;
10417 }
10418
10419 /* Called from md_do_align.  Used to create an alignment
10420    frag in a code section.  */
10421
10422 void
10423 arm_frag_align_code (int n, int max)
10424 {
10425   char * p;
10426
10427   /* We assume that there will never be a requirement
10428      to support alignments greater than 32 bytes.  */
10429   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
10430     as_fatal (_("alignments greater than 32 bytes not supported in .text sections."));
10431
10432   p = frag_var (rs_align_code,
10433                 MAX_MEM_FOR_RS_ALIGN_CODE,
10434                 1,
10435                 (relax_substateT) max,
10436                 (symbolS *) NULL,
10437                 (offsetT) n,
10438                 (char *) NULL);
10439   *p = 0;
10440 }
10441
10442 /* Perform target specific initialisation of a frag.  */
10443
10444 void
10445 arm_init_frag (fragS * fragP)
10446 {
10447   /* Record whether this frag is in an ARM or a THUMB area.  */
10448   fragP->tc_frag_data = thumb_mode;
10449 }
10450
10451 #ifdef OBJ_ELF
10452 /* When we change sections we need to issue a new mapping symbol.  */
10453
10454 void
10455 arm_elf_change_section (void)
10456 {
10457   flagword flags;
10458   segment_info_type *seginfo;
10459
10460   /* Link an unlinked unwind index table section to the .text section.  */
10461   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
10462       && elf_linked_to_section (now_seg) == NULL)
10463     elf_linked_to_section (now_seg) = text_section;
10464
10465   if (!SEG_NORMAL (now_seg))
10466     return;
10467
10468   flags = bfd_get_section_flags (stdoutput, now_seg);
10469
10470   /* We can ignore sections that only contain debug info.  */
10471   if ((flags & SEC_ALLOC) == 0)
10472     return;
10473
10474   seginfo = seg_info (now_seg);
10475   mapstate = seginfo->tc_segment_info_data.mapstate;
10476   marked_pr_dependency = seginfo->tc_segment_info_data.marked_pr_dependency;
10477 }
10478
10479 int
10480 arm_elf_section_type (const char * str, size_t len)
10481 {
10482   if (len == 5 && strncmp (str, "exidx", 5) == 0)
10483     return SHT_ARM_EXIDX;
10484
10485   return -1;
10486 }
10487 \f
10488 /* Code to deal with unwinding tables.  */
10489
10490 static void add_unwind_adjustsp (offsetT);
10491
10492 /* Cenerate and deferred unwind frame offset.  */
10493
10494 static void
10495 flush_pending_unwind (void)
10496 {
10497   offsetT offset;
10498
10499   offset = unwind.pending_offset;
10500   unwind.pending_offset = 0;
10501   if (offset != 0)
10502     add_unwind_adjustsp (offset);
10503 }
10504
10505 /* Add an opcode to this list for this function.  Two-byte opcodes should
10506    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
10507    order.  */
10508
10509 static void
10510 add_unwind_opcode (valueT op, int length)
10511 {
10512   /* Add any deferred stack adjustment.  */
10513   if (unwind.pending_offset)
10514     flush_pending_unwind ();
10515
10516   unwind.sp_restored = 0;
10517
10518   if (unwind.opcode_count + length > unwind.opcode_alloc)
10519     {
10520       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
10521       if (unwind.opcodes)
10522         unwind.opcodes = xrealloc (unwind.opcodes,
10523                                    unwind.opcode_alloc);
10524       else
10525         unwind.opcodes = xmalloc (unwind.opcode_alloc);
10526     }
10527   while (length > 0)
10528     {
10529       length--;
10530       unwind.opcodes[unwind.opcode_count] = op & 0xff;
10531       op >>= 8;
10532       unwind.opcode_count++;
10533     }
10534 }
10535
10536 /* Add unwind opcodes to adjust the stack pointer.  */
10537
10538 static void
10539 add_unwind_adjustsp (offsetT offset)
10540 {
10541   valueT op;
10542
10543   if (offset > 0x200)
10544     {
10545       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
10546       char bytes[5];
10547       int n;
10548       valueT o;
10549
10550       /* Long form: 0xb2, uleb128.  */
10551       /* This might not fit in a word so add the individual bytes,
10552          remembering the list is built in reverse order.  */
10553       o = (valueT) ((offset - 0x204) >> 2);
10554       if (o == 0)
10555         add_unwind_opcode (0, 1);
10556
10557       /* Calculate the uleb128 encoding of the offset.  */
10558       n = 0;
10559       while (o)
10560         {
10561           bytes[n] = o & 0x7f;
10562           o >>= 7;
10563           if (o)
10564             bytes[n] |= 0x80;
10565           n++;
10566         }
10567       /* Add the insn.  */
10568       for (; n; n--)
10569         add_unwind_opcode (bytes[n - 1], 1);
10570       add_unwind_opcode (0xb2, 1);
10571     }
10572   else if (offset > 0x100)
10573     {
10574       /* Two short opcodes.  */
10575       add_unwind_opcode (0x3f, 1);
10576       op = (offset - 0x104) >> 2;
10577       add_unwind_opcode (op, 1);
10578     }
10579   else if (offset > 0)
10580     {
10581       /* Short opcode.  */
10582       op = (offset - 4) >> 2;
10583       add_unwind_opcode (op, 1);
10584     }
10585   else if (offset < 0)
10586     {
10587       offset = -offset;
10588       while (offset > 0x100)
10589         {
10590           add_unwind_opcode (0x7f, 1);
10591           offset -= 0x100;
10592         }
10593       op = ((offset - 4) >> 2) | 0x40;
10594       add_unwind_opcode (op, 1);
10595     }
10596 }
10597
10598 /* Finish the list of unwind opcodes for this function.  */
10599 static void
10600 finish_unwind_opcodes (void)
10601 {
10602   valueT op;
10603
10604   if (unwind.fp_used)
10605     {
10606       /* Adjust sp as neccessary.  */
10607       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
10608       flush_pending_unwind ();
10609
10610       /* After restoring sp from the frame pointer.  */
10611       op = 0x90 | unwind.fp_reg;
10612       add_unwind_opcode (op, 1);
10613     }
10614   else
10615     flush_pending_unwind ();
10616 }
10617
10618
10619 /* Start an exception table entry.  If idx is nonzero this is an index table
10620    entry.  */
10621
10622 static void
10623 start_unwind_section (const segT text_seg, int idx)
10624 {
10625   const char * text_name;
10626   const char * prefix;
10627   const char * prefix_once;
10628   const char * group_name;
10629   size_t prefix_len;
10630   size_t text_len;
10631   char * sec_name;
10632   size_t sec_name_len;
10633   int type;
10634   int flags;
10635   int linkonce;
10636
10637   if (idx)
10638     {
10639       prefix = ELF_STRING_ARM_unwind;
10640       prefix_once = ELF_STRING_ARM_unwind_once;
10641       type = SHT_ARM_EXIDX;
10642     }
10643   else
10644     {
10645       prefix = ELF_STRING_ARM_unwind_info;
10646       prefix_once = ELF_STRING_ARM_unwind_info_once;
10647       type = SHT_PROGBITS;
10648     }
10649
10650   text_name = segment_name (text_seg);
10651   if (streq (text_name, ".text"))
10652     text_name = "";
10653
10654   if (strncmp (text_name, ".gnu.linkonce.t.",
10655                strlen (".gnu.linkonce.t.")) == 0)
10656     {
10657       prefix = prefix_once;
10658       text_name += strlen (".gnu.linkonce.t.");
10659     }
10660
10661   prefix_len = strlen (prefix);
10662   text_len = strlen (text_name);
10663   sec_name_len = prefix_len + text_len;
10664   sec_name = xmalloc (sec_name_len + 1);
10665   memcpy (sec_name, prefix, prefix_len);
10666   memcpy (sec_name + prefix_len, text_name, text_len);
10667   sec_name[prefix_len + text_len] = '\0';
10668
10669   flags = SHF_ALLOC;
10670   linkonce = 0;
10671   group_name = 0;
10672
10673   /* Handle COMDAT group.  */
10674   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
10675     {
10676       group_name = elf_group_name (text_seg);
10677       if (group_name == NULL)
10678         {
10679           as_bad ("Group section `%s' has no group signature",
10680                   segment_name (text_seg));
10681           ignore_rest_of_line ();
10682           return;
10683         }
10684       flags |= SHF_GROUP;
10685       linkonce = 1;
10686     }
10687
10688   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
10689
10690   /* Set the setion link for index tables.  */
10691   if (idx)
10692     elf_linked_to_section (now_seg) = text_seg;
10693 }
10694
10695
10696 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
10697    personality routine data.  Returns zero, or the index table value for
10698    and inline entry.  */
10699
10700 static valueT
10701 create_unwind_entry (int have_data)
10702 {
10703   int size;
10704   addressT where;
10705   char *ptr;
10706   /* The current word of data.  */
10707   valueT data;
10708   /* The number of bytes left in this word.  */
10709   int n;
10710
10711   finish_unwind_opcodes ();
10712
10713   /* Remember the current text section.  */
10714   unwind.saved_seg = now_seg;
10715   unwind.saved_subseg = now_subseg;
10716
10717   start_unwind_section (now_seg, 0);
10718
10719   if (unwind.personality_routine == NULL)
10720     {
10721       if (unwind.personality_index == -2)
10722         {
10723           if (have_data)
10724             as_bad (_("handerdata in cantunwind frame"));
10725           return 1; /* EXIDX_CANTUNWIND.  */
10726         }
10727
10728       /* Use a default personality routine if none is specified.  */
10729       if (unwind.personality_index == -1)
10730         {
10731           if (unwind.opcode_count > 3)
10732             unwind.personality_index = 1;
10733           else
10734             unwind.personality_index = 0;
10735         }
10736
10737       /* Space for the personality routine entry.  */
10738       if (unwind.personality_index == 0)
10739         {
10740           if (unwind.opcode_count > 3)
10741             as_bad (_("too many unwind opcodes for personality routine 0"));
10742
10743           if (!have_data)
10744             {
10745               /* All the data is inline in the index table.  */
10746               data = 0x80;
10747               n = 3;
10748               while (unwind.opcode_count > 0)
10749                 {
10750                   unwind.opcode_count--;
10751                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
10752                   n--;
10753                 }
10754
10755               /* Pad with "finish" opcodes.  */
10756               while (n--)
10757                 data = (data << 8) | 0xb0;
10758
10759               return data;
10760             }
10761           size = 0;
10762         }
10763       else
10764         /* We get two opcodes "free" in the first word.  */
10765         size = unwind.opcode_count - 2;
10766     }
10767   else
10768     /* An extra byte is required for the opcode count.  */
10769     size = unwind.opcode_count + 1;
10770
10771   size = (size + 3) >> 2;
10772   if (size > 0xff)
10773     as_bad (_("too many unwind opcodes"));
10774
10775   frag_align (2, 0, 0);
10776   record_alignment (now_seg, 2);
10777   unwind.table_entry = expr_build_dot ();
10778
10779   /* Allocate the table entry.  */
10780   ptr = frag_more ((size << 2) + 4);
10781   where = frag_now_fix () - ((size << 2) + 4);
10782
10783   switch (unwind.personality_index)
10784     {
10785     case -1:
10786       /* ??? Should this be a PLT generating relocation?  */
10787       /* Custom personality routine.  */
10788       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
10789                BFD_RELOC_ARM_PREL31);
10790
10791       where += 4;
10792       ptr += 4;
10793
10794       /* Set the first byte to the number of additional words.  */
10795       data = size - 1;
10796       n = 3;
10797       break;
10798
10799     /* ABI defined personality routines.  */
10800     case 0:
10801       /* Three opcodes bytes are packed into the first word.  */
10802       data = 0x80;
10803       n = 3;
10804       break;
10805
10806     case 1:
10807     case 2:
10808       /* The size and first two opcode bytes go in the first word.  */
10809       data = ((0x80 + unwind.personality_index) << 8) | size;
10810       n = 2;
10811       break;
10812
10813     default:
10814       /* Should never happen.  */
10815       abort ();
10816     }
10817
10818   /* Pack the opcodes into words (MSB first), reversing the list at the same
10819      time.  */
10820   while (unwind.opcode_count > 0)
10821     {
10822       if (n == 0)
10823         {
10824           md_number_to_chars (ptr, data, 4);
10825           ptr += 4;
10826           n = 4;
10827           data = 0;
10828         }
10829       unwind.opcode_count--;
10830       n--;
10831       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
10832     }
10833
10834   /* Finish off the last word.  */
10835   if (n < 4)
10836     {
10837       /* Pad with "finish" opcodes.  */
10838       while (n--)
10839         data = (data << 8) | 0xb0;
10840
10841       md_number_to_chars (ptr, data, 4);
10842     }
10843
10844   if (!have_data)
10845     {
10846       /* Add an empty descriptor if there is no user-specified data.   */
10847       ptr = frag_more (4);
10848       md_number_to_chars (ptr, 0, 4);
10849     }
10850
10851   return 0;
10852 }
10853
10854 /* Convert REGNAME to a DWARF-2 register number.  */
10855
10856 int
10857 tc_arm_regname_to_dw2regnum (const char *regname)
10858 {
10859   int reg = arm_reg_parse ((char **) &regname, REG_TYPE_RN);
10860
10861   if (reg == FAIL)
10862     return -1;
10863
10864   return reg;
10865 }
10866
10867 /* Initialize the DWARF-2 unwind information for this procedure.  */
10868
10869 void
10870 tc_arm_frame_initial_instructions (void)
10871 {
10872   cfi_add_CFA_def_cfa (REG_SP, 0);
10873 }
10874 #endif /* OBJ_ELF */
10875
10876
10877 /* MD interface: Symbol and relocation handling.  */
10878
10879 /* Return the address within the segment that a PC-relative fixup is
10880    relative to.  For ARM, PC-relative fixups applied to instructions
10881    are generally relative to the location of the fixup plus 8 bytes.
10882    Thumb branches are offset by 4, and Thumb loads relative to PC
10883    require special handling.  */
10884
10885 long
10886 md_pcrel_from_section (fixS * fixP, segT seg)
10887 {
10888   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
10889
10890   /* If this is pc-relative and we are going to emit a relocation
10891      then we just want to put out any pipeline compensation that the linker
10892      will need.  Otherwise we want to use the calculated base.  */
10893   if (fixP->fx_pcrel 
10894       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
10895           || arm_force_relocation (fixP)))
10896     base = 0;
10897
10898   switch (fixP->fx_r_type)
10899     {
10900       /* PC relative addressing on the Thumb is slightly odd as the
10901          bottom two bits of the PC are forced to zero for the
10902          calculation.  This happens *after* application of the
10903          pipeline offset.  However, Thumb adrl already adjusts for
10904          this, so we need not do it again.  */
10905     case BFD_RELOC_ARM_THUMB_ADD:
10906       return base & ~3;
10907
10908     case BFD_RELOC_ARM_THUMB_OFFSET:
10909     case BFD_RELOC_ARM_T32_OFFSET_IMM:
10910     case BFD_RELOC_ARM_T32_ADD_PC12:
10911     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
10912       return (base + 4) & ~3;
10913
10914       /* Thumb branches are simply offset by +4.  */
10915     case BFD_RELOC_THUMB_PCREL_BRANCH7:
10916     case BFD_RELOC_THUMB_PCREL_BRANCH9:
10917     case BFD_RELOC_THUMB_PCREL_BRANCH12:
10918     case BFD_RELOC_THUMB_PCREL_BRANCH20:
10919     case BFD_RELOC_THUMB_PCREL_BRANCH23:
10920     case BFD_RELOC_THUMB_PCREL_BRANCH25:
10921     case BFD_RELOC_THUMB_PCREL_BLX:
10922       return base + 4;
10923
10924       /* ARM mode branches are offset by +8.  However, the Windows CE
10925          loader expects the relocation not to take this into account.  */
10926     case BFD_RELOC_ARM_PCREL_BRANCH:
10927     case BFD_RELOC_ARM_PCREL_CALL:
10928     case BFD_RELOC_ARM_PCREL_JUMP:
10929     case BFD_RELOC_ARM_PCREL_BLX:
10930     case BFD_RELOC_ARM_PLT32:
10931 #ifdef TE_WINCE
10932       return base;
10933 #else
10934       return base + 8;
10935 #endif
10936
10937       /* ARM mode loads relative to PC are also offset by +8.  Unlike
10938          branches, the Windows CE loader *does* expect the relocation
10939          to take this into account.  */
10940     case BFD_RELOC_ARM_OFFSET_IMM:
10941     case BFD_RELOC_ARM_OFFSET_IMM8:
10942     case BFD_RELOC_ARM_HWLITERAL:
10943     case BFD_RELOC_ARM_LITERAL:
10944     case BFD_RELOC_ARM_CP_OFF_IMM:
10945       return base + 8;
10946
10947
10948       /* Other PC-relative relocations are un-offset.  */
10949     default:
10950       return base;
10951     }
10952 }
10953
10954 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
10955    Otherwise we have no need to default values of symbols.  */
10956
10957 symbolS *
10958 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
10959 {
10960 #ifdef OBJ_ELF
10961   if (name[0] == '_' && name[1] == 'G'
10962       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
10963     {
10964       if (!GOT_symbol)
10965         {
10966           if (symbol_find (name))
10967             as_bad ("GOT already in the symbol table");
10968
10969           GOT_symbol = symbol_new (name, undefined_section,
10970                                    (valueT) 0, & zero_address_frag);
10971         }
10972
10973       return GOT_symbol;
10974     }
10975 #endif
10976
10977   return 0;
10978 }
10979
10980 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
10981    computed as two separate immediate values, added together.  We
10982    already know that this value cannot be computed by just one ARM
10983    instruction.  */
10984
10985 static unsigned int
10986 validate_immediate_twopart (unsigned int   val,
10987                             unsigned int * highpart)
10988 {
10989   unsigned int a;
10990   unsigned int i;
10991
10992   for (i = 0; i < 32; i += 2)
10993     if (((a = rotate_left (val, i)) & 0xff) != 0)
10994       {
10995         if (a & 0xff00)
10996           {
10997             if (a & ~ 0xffff)
10998               continue;
10999             * highpart = (a  >> 8) | ((i + 24) << 7);
11000           }
11001         else if (a & 0xff0000)
11002           {
11003             if (a & 0xff000000)
11004               continue;
11005             * highpart = (a >> 16) | ((i + 16) << 7);
11006           }
11007         else
11008           {
11009             assert (a & 0xff000000);
11010             * highpart = (a >> 24) | ((i + 8) << 7);
11011           }
11012
11013         return (a & 0xff) | (i << 7);
11014       }
11015
11016   return FAIL;
11017 }
11018
11019 static int
11020 validate_offset_imm (unsigned int val, int hwse)
11021 {
11022   if ((hwse && val > 255) || val > 4095)
11023     return FAIL;
11024   return val;
11025 }
11026
11027 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
11028    negative immediate constant by altering the instruction.  A bit of
11029    a hack really.
11030         MOV <-> MVN
11031         AND <-> BIC
11032         ADC <-> SBC
11033         by inverting the second operand, and
11034         ADD <-> SUB
11035         CMP <-> CMN
11036         by negating the second operand.  */
11037
11038 static int
11039 negate_data_op (unsigned long * instruction,
11040                 unsigned long   value)
11041 {
11042   int op, new_inst;
11043   unsigned long negated, inverted;
11044
11045   negated = encode_arm_immediate (-value);
11046   inverted = encode_arm_immediate (~value);
11047
11048   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
11049   switch (op)
11050     {
11051       /* First negates.  */
11052     case OPCODE_SUB:             /* ADD <-> SUB  */
11053       new_inst = OPCODE_ADD;
11054       value = negated;
11055       break;
11056
11057     case OPCODE_ADD:
11058       new_inst = OPCODE_SUB;
11059       value = negated;
11060       break;
11061
11062     case OPCODE_CMP:             /* CMP <-> CMN  */
11063       new_inst = OPCODE_CMN;
11064       value = negated;
11065       break;
11066
11067     case OPCODE_CMN:
11068       new_inst = OPCODE_CMP;
11069       value = negated;
11070       break;
11071
11072       /* Now Inverted ops.  */
11073     case OPCODE_MOV:             /* MOV <-> MVN  */
11074       new_inst = OPCODE_MVN;
11075       value = inverted;
11076       break;
11077
11078     case OPCODE_MVN:
11079       new_inst = OPCODE_MOV;
11080       value = inverted;
11081       break;
11082
11083     case OPCODE_AND:             /* AND <-> BIC  */
11084       new_inst = OPCODE_BIC;
11085       value = inverted;
11086       break;
11087
11088     case OPCODE_BIC:
11089       new_inst = OPCODE_AND;
11090       value = inverted;
11091       break;
11092
11093     case OPCODE_ADC:              /* ADC <-> SBC  */
11094       new_inst = OPCODE_SBC;
11095       value = inverted;
11096       break;
11097
11098     case OPCODE_SBC:
11099       new_inst = OPCODE_ADC;
11100       value = inverted;
11101       break;
11102
11103       /* We cannot do anything.  */
11104     default:
11105       return FAIL;
11106     }
11107
11108   if (value == (unsigned) FAIL)
11109     return FAIL;
11110
11111   *instruction &= OPCODE_MASK;
11112   *instruction |= new_inst << DATA_OP_SHIFT;
11113   return value;
11114 }
11115
11116 /* Read a 32-bit thumb instruction from buf.  */
11117 static unsigned long
11118 get_thumb32_insn (char * buf)
11119 {
11120   unsigned long insn;
11121   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
11122   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
11123
11124   return insn;
11125 }
11126
11127 void
11128 md_apply_fix (fixS *    fixP,
11129                valueT * valP,
11130                segT     seg)
11131 {
11132   offsetT        value = * valP;
11133   offsetT        newval;
11134   unsigned int   newimm;
11135   unsigned long  temp;
11136   int            sign;
11137   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
11138
11139   assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
11140
11141   /* Note whether this will delete the relocation.  */
11142   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
11143     fixP->fx_done = 1;
11144
11145   /* On a 64-bit host, silently truncate 'value' to 32 bits for
11146      consistency with the behavior on 32-bit hosts.  Remember value
11147      for emit_reloc.  */
11148   value &= 0xffffffff;
11149   value ^= 0x80000000;
11150   value -= 0x80000000; 
11151
11152   *valP = value;
11153   fixP->fx_addnumber = value;
11154
11155   /* Same treatment for fixP->fx_offset.  */
11156   fixP->fx_offset &= 0xffffffff;
11157   fixP->fx_offset ^= 0x80000000;
11158   fixP->fx_offset -= 0x80000000;
11159
11160   switch (fixP->fx_r_type)
11161     {
11162     case BFD_RELOC_NONE:
11163       /* This will need to go in the object file.  */
11164       fixP->fx_done = 0;
11165       break;
11166
11167     case BFD_RELOC_ARM_IMMEDIATE:
11168       /* We claim that this fixup has been processed here,
11169          even if in fact we generate an error because we do
11170          not have a reloc for it, so tc_gen_reloc will reject it.  */
11171       fixP->fx_done = 1;
11172
11173       if (fixP->fx_addsy
11174           && ! S_IS_DEFINED (fixP->fx_addsy))
11175         {
11176           as_bad_where (fixP->fx_file, fixP->fx_line,
11177                         _("undefined symbol %s used as an immediate value"),
11178                         S_GET_NAME (fixP->fx_addsy));
11179           break;
11180         }
11181
11182       newimm = encode_arm_immediate (value);
11183       temp = md_chars_to_number (buf, INSN_SIZE);
11184
11185       /* If the instruction will fail, see if we can fix things up by
11186          changing the opcode.  */
11187       if (newimm == (unsigned int) FAIL
11188           && (newimm = negate_data_op (&temp, value)) == (unsigned int) FAIL)
11189         {
11190           as_bad_where (fixP->fx_file, fixP->fx_line,
11191                         _("invalid constant (%lx) after fixup"),
11192                         (unsigned long) value);
11193           break;
11194         }
11195
11196       newimm |= (temp & 0xfffff000);
11197       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
11198       break;
11199
11200     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
11201       {
11202         unsigned int highpart = 0;
11203         unsigned int newinsn  = 0xe1a00000; /* nop.  */
11204
11205         newimm = encode_arm_immediate (value);
11206         temp = md_chars_to_number (buf, INSN_SIZE);
11207
11208         /* If the instruction will fail, see if we can fix things up by
11209            changing the opcode.  */
11210         if (newimm == (unsigned int) FAIL
11211             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
11212           {
11213             /* No ?  OK - try using two ADD instructions to generate
11214                the value.  */
11215             newimm = validate_immediate_twopart (value, & highpart);
11216
11217             /* Yes - then make sure that the second instruction is
11218                also an add.  */
11219             if (newimm != (unsigned int) FAIL)
11220               newinsn = temp;
11221             /* Still No ?  Try using a negated value.  */
11222             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
11223               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
11224             /* Otherwise - give up.  */
11225             else
11226               {
11227                 as_bad_where (fixP->fx_file, fixP->fx_line,
11228                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
11229                               (long) value);
11230                 break;
11231               }
11232
11233             /* Replace the first operand in the 2nd instruction (which
11234                is the PC) with the destination register.  We have
11235                already added in the PC in the first instruction and we
11236                do not want to do it again.  */
11237             newinsn &= ~ 0xf0000;
11238             newinsn |= ((newinsn & 0x0f000) << 4);
11239           }
11240
11241         newimm |= (temp & 0xfffff000);
11242         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
11243
11244         highpart |= (newinsn & 0xfffff000);
11245         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
11246       }
11247       break;
11248
11249     case BFD_RELOC_ARM_OFFSET_IMM:
11250     case BFD_RELOC_ARM_LITERAL:
11251       sign = value >= 0;
11252
11253       if (value < 0)
11254         value = - value;
11255
11256       if (validate_offset_imm (value, 0) == FAIL)
11257         {
11258           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
11259             as_bad_where (fixP->fx_file, fixP->fx_line,
11260                           _("invalid literal constant: pool needs to be closer"));
11261           else
11262             as_bad_where (fixP->fx_file, fixP->fx_line,
11263                           _("bad immediate value for offset (%ld)"),
11264                           (long) value);
11265           break;
11266         }
11267
11268       newval = md_chars_to_number (buf, INSN_SIZE);
11269       newval &= 0xff7ff000;
11270       newval |= value | (sign ? INDEX_UP : 0);
11271       md_number_to_chars (buf, newval, INSN_SIZE);
11272       break;
11273
11274     case BFD_RELOC_ARM_OFFSET_IMM8:
11275     case BFD_RELOC_ARM_HWLITERAL:
11276       sign = value >= 0;
11277
11278       if (value < 0)
11279         value = - value;
11280
11281       if (validate_offset_imm (value, 1) == FAIL)
11282         {
11283           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
11284             as_bad_where (fixP->fx_file, fixP->fx_line,
11285                           _("invalid literal constant: pool needs to be closer"));
11286           else
11287             as_bad (_("bad immediate value for half-word offset (%ld)"),
11288                     (long) value);
11289           break;
11290         }
11291
11292       newval = md_chars_to_number (buf, INSN_SIZE);
11293       newval &= 0xff7ff0f0;
11294       newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
11295       md_number_to_chars (buf, newval, INSN_SIZE);
11296       break;
11297
11298     case BFD_RELOC_ARM_T32_OFFSET_U8:
11299       if (value < 0 || value > 1020 || value % 4 != 0)
11300         as_bad_where (fixP->fx_file, fixP->fx_line,
11301                       _("bad immediate value for offset (%ld)"), (long) value);
11302       value /= 4;
11303
11304       newval = md_chars_to_number (buf+2, THUMB_SIZE);
11305       newval |= value;
11306       md_number_to_chars (buf+2, newval, THUMB_SIZE);
11307       break;
11308
11309     case BFD_RELOC_ARM_T32_OFFSET_IMM:
11310       /* This is a complicated relocation used for all varieties of Thumb32
11311          load/store instruction with immediate offset:
11312
11313          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
11314                                                    *4, optional writeback(W)
11315                                                    (doubleword load/store)
11316
11317          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
11318          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
11319          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
11320          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
11321          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
11322
11323          Uppercase letters indicate bits that are already encoded at
11324          this point.  Lowercase letters are our problem.  For the
11325          second block of instructions, the secondary opcode nybble
11326          (bits 8..11) is present, and bit 23 is zero, even if this is
11327          a PC-relative operation.  */
11328       newval = md_chars_to_number (buf, THUMB_SIZE);
11329       newval <<= 16;
11330       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
11331
11332       if ((newval & 0xf0000000) == 0xe0000000)
11333         {
11334           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
11335           if (value >= 0)
11336             newval |= (1 << 23);
11337           else
11338             value = -value;
11339           if (value % 4 != 0)
11340             {
11341               as_bad_where (fixP->fx_file, fixP->fx_line,
11342                             _("offset not a multiple of 4"));
11343               break;
11344             }
11345           value /= 4;
11346           if (value >= 0xff)
11347             {
11348               as_bad_where (fixP->fx_file, fixP->fx_line,
11349                             _("offset out of range"));
11350               break;
11351             }
11352           newval &= ~0xff;
11353         }
11354       else if ((newval & 0x000f0000) == 0x000f0000)
11355         {
11356           /* PC-relative, 12-bit offset.  */
11357           if (value >= 0)
11358             newval |= (1 << 23);
11359           else
11360             value = -value;
11361           if (value >= 0xfff)
11362             {
11363               as_bad_where (fixP->fx_file, fixP->fx_line,
11364                             _("offset out of range"));
11365               break;
11366             }
11367           newval &= ~0xfff;
11368         }
11369       else if ((newval & 0x00000100) == 0x00000100)
11370         {
11371           /* Writeback: 8-bit, +/- offset.  */
11372           if (value >= 0)
11373             newval |= (1 << 9);
11374           else
11375             value = -value;
11376           if (value >= 0xff)
11377             {
11378               as_bad_where (fixP->fx_file, fixP->fx_line,
11379                             _("offset out of range"));
11380               break;
11381             }
11382           newval &= ~0xff;
11383         }
11384       else if ((newval & 0x00000f00) == 0x00000e00)
11385         {
11386           /* T-instruction: positive 8-bit offset.  */
11387           if (value < 0 || value >= 0xff)
11388             {
11389               as_bad_where (fixP->fx_file, fixP->fx_line,
11390                             _("offset out of range"));
11391               break;
11392             }
11393           newval &= ~0xff;
11394           newval |= value;
11395         }
11396       else
11397         {
11398           /* Positive 12-bit or negative 8-bit offset.  */
11399           int limit;
11400           if (value >= 0)
11401             {
11402               newval |= (1 << 23);
11403               limit = 0xfff;
11404             }
11405           else
11406             {
11407               value = -value;
11408               limit = 0xff;
11409             }
11410           if (value > limit)
11411             {
11412               as_bad_where (fixP->fx_file, fixP->fx_line,
11413                             _("offset out of range"));
11414               break;
11415             }
11416           newval &= ~limit;
11417         }
11418
11419       newval |= value;
11420       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
11421       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
11422       break;
11423
11424     case BFD_RELOC_ARM_SHIFT_IMM:
11425       newval = md_chars_to_number (buf, INSN_SIZE);
11426       if (((unsigned long) value) > 32
11427           || (value == 32
11428               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
11429         {
11430           as_bad_where (fixP->fx_file, fixP->fx_line,
11431                         _("shift expression is too large"));
11432           break;
11433         }
11434
11435       if (value == 0)
11436         /* Shifts of zero must be done as lsl.  */
11437         newval &= ~0x60;
11438       else if (value == 32)
11439         value = 0;
11440       newval &= 0xfffff07f;
11441       newval |= (value & 0x1f) << 7;
11442       md_number_to_chars (buf, newval, INSN_SIZE);
11443       break;
11444
11445     case BFD_RELOC_ARM_T32_IMMEDIATE:
11446     case BFD_RELOC_ARM_T32_IMM12:
11447     case BFD_RELOC_ARM_T32_ADD_PC12:
11448       /* We claim that this fixup has been processed here,
11449          even if in fact we generate an error because we do
11450          not have a reloc for it, so tc_gen_reloc will reject it.  */
11451       fixP->fx_done = 1;
11452
11453       if (fixP->fx_addsy
11454           && ! S_IS_DEFINED (fixP->fx_addsy))
11455         {
11456           as_bad_where (fixP->fx_file, fixP->fx_line,
11457                         _("undefined symbol %s used as an immediate value"),
11458                         S_GET_NAME (fixP->fx_addsy));
11459           break;
11460         }
11461
11462       newval = md_chars_to_number (buf, THUMB_SIZE);
11463       newval <<= 16;
11464       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
11465
11466       /* FUTURE: Implement analogue of negate_data_op for T32.  */
11467       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE)
11468         newimm = encode_thumb32_immediate (value);
11469       else
11470         {
11471           /* 12 bit immediate for addw/subw.  */
11472           if (value < 0)
11473             {
11474               value = -value;
11475               newval ^= 0x00a00000;
11476             }
11477           if (value > 0xfff)
11478             newimm = (unsigned int) FAIL;
11479           else
11480             newimm = value;
11481         }
11482
11483       if (newimm == (unsigned int)FAIL)
11484         {
11485           as_bad_where (fixP->fx_file, fixP->fx_line,
11486                         _("invalid constant (%lx) after fixup"),
11487                         (unsigned long) value);
11488           break;
11489         }
11490
11491       newval |= (newimm & 0x800) << 15;
11492       newval |= (newimm & 0x700) << 4;
11493       newval |= (newimm & 0x0ff);
11494
11495       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
11496       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
11497       break;
11498
11499     case BFD_RELOC_ARM_SMC:
11500       if (((unsigned long) value) > 0xffff)
11501         as_bad_where (fixP->fx_file, fixP->fx_line,
11502                       _("invalid smc expression"));
11503       newval = md_chars_to_number (buf, INSN_SIZE);
11504       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
11505       md_number_to_chars (buf, newval, INSN_SIZE);
11506       break;
11507
11508     case BFD_RELOC_ARM_SWI:
11509       if (fixP->tc_fix_data != 0)
11510         {
11511           if (((unsigned long) value) > 0xff)
11512             as_bad_where (fixP->fx_file, fixP->fx_line,
11513                           _("invalid swi expression"));
11514           newval = md_chars_to_number (buf, THUMB_SIZE);
11515           newval |= value;
11516           md_number_to_chars (buf, newval, THUMB_SIZE);
11517         }
11518       else
11519         {
11520           if (((unsigned long) value) > 0x00ffffff)
11521             as_bad_where (fixP->fx_file, fixP->fx_line,
11522                           _("invalid swi expression"));
11523           newval = md_chars_to_number (buf, INSN_SIZE);
11524           newval |= value;
11525           md_number_to_chars (buf, newval, INSN_SIZE);
11526         }
11527       break;
11528
11529     case BFD_RELOC_ARM_MULTI:
11530       if (((unsigned long) value) > 0xffff)
11531         as_bad_where (fixP->fx_file, fixP->fx_line,
11532                       _("invalid expression in load/store multiple"));
11533       newval = value | md_chars_to_number (buf, INSN_SIZE);
11534       md_number_to_chars (buf, newval, INSN_SIZE);
11535       break;
11536
11537 #ifdef OBJ_ELF
11538     case BFD_RELOC_ARM_PCREL_CALL:
11539       newval = md_chars_to_number (buf, INSN_SIZE);
11540       if ((newval & 0xf0000000) == 0xf0000000)
11541         temp = 1;
11542       else
11543         temp = 3;
11544       goto arm_branch_common;
11545
11546     case BFD_RELOC_ARM_PCREL_JUMP:
11547     case BFD_RELOC_ARM_PLT32:
11548 #endif
11549     case BFD_RELOC_ARM_PCREL_BRANCH:
11550       temp = 3;
11551       goto arm_branch_common;
11552
11553     case BFD_RELOC_ARM_PCREL_BLX:
11554       temp = 1;
11555     arm_branch_common:
11556       /* We are going to store value (shifted right by two) in the
11557          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
11558          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
11559          also be be clear.  */
11560       if (value & temp)
11561         as_bad_where (fixP->fx_file, fixP->fx_line,
11562                       _("misaligned branch destination"));
11563       if ((value & (offsetT)0xfe000000) != (offsetT)0
11564           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
11565         as_bad_where (fixP->fx_file, fixP->fx_line,
11566                       _("branch out of range"));
11567
11568       if (fixP->fx_done || !seg->use_rela_p)
11569         {
11570           newval = md_chars_to_number (buf, INSN_SIZE);
11571           newval |= (value >> 2) & 0x00ffffff;
11572           md_number_to_chars (buf, newval, INSN_SIZE);
11573         }
11574       break;
11575
11576     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CZB */
11577       /* CZB can only branch forward.  */
11578       if (value & ~0x7e)
11579         as_bad_where (fixP->fx_file, fixP->fx_line,
11580                       _("branch out of range"));
11581
11582       if (fixP->fx_done || !seg->use_rela_p)
11583         {
11584           newval = md_chars_to_number (buf, THUMB_SIZE);
11585           newval |= ((value & 0x2e) << 2) | ((value & 0x40) << 3);
11586           md_number_to_chars (buf, newval, THUMB_SIZE);
11587         }
11588       break;
11589
11590     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
11591       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
11592         as_bad_where (fixP->fx_file, fixP->fx_line,
11593                       _("branch out of range"));
11594
11595       if (fixP->fx_done || !seg->use_rela_p)
11596         {
11597           newval = md_chars_to_number (buf, THUMB_SIZE);
11598           newval |= (value & 0x1ff) >> 1;
11599           md_number_to_chars (buf, newval, THUMB_SIZE);
11600         }
11601       break;
11602
11603     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
11604       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
11605         as_bad_where (fixP->fx_file, fixP->fx_line,
11606                       _("branch out of range"));
11607
11608       if (fixP->fx_done || !seg->use_rela_p)
11609         {
11610           newval = md_chars_to_number (buf, THUMB_SIZE);
11611           newval |= (value & 0xfff) >> 1;
11612           md_number_to_chars (buf, newval, THUMB_SIZE);
11613         }
11614       break;
11615
11616     case BFD_RELOC_THUMB_PCREL_BRANCH20:
11617       if ((value & ~0x1fffff) && ((value & ~0x1fffff) != ~0x1fffff))
11618         as_bad_where (fixP->fx_file, fixP->fx_line,
11619                       _("conditional branch out of range"));
11620
11621       if (fixP->fx_done || !seg->use_rela_p)
11622         {
11623           offsetT newval2;
11624           addressT S, J1, J2, lo, hi;
11625
11626           S  = (value & 0x00100000) >> 20;
11627           J2 = (value & 0x00080000) >> 19;
11628           J1 = (value & 0x00040000) >> 18;
11629           hi = (value & 0x0003f000) >> 12;
11630           lo = (value & 0x00000ffe) >> 1;
11631
11632           newval   = md_chars_to_number (buf, THUMB_SIZE);
11633           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
11634           newval  |= (S << 10) | hi;
11635           newval2 |= (J1 << 13) | (J2 << 11) | lo;
11636           md_number_to_chars (buf, newval, THUMB_SIZE);
11637           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
11638         }
11639       break;
11640
11641     case BFD_RELOC_THUMB_PCREL_BLX:
11642     case BFD_RELOC_THUMB_PCREL_BRANCH23:
11643       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
11644         as_bad_where (fixP->fx_file, fixP->fx_line,
11645                       _("branch out of range"));
11646
11647       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
11648         /* For a BLX instruction, make sure that the relocation is rounded up
11649            to a word boundary.  This follows the semantics of the instruction
11650            which specifies that bit 1 of the target address will come from bit
11651            1 of the base address.  */
11652         value = (value + 1) & ~ 1;
11653
11654       if (fixP->fx_done || !seg->use_rela_p)
11655         {
11656           offsetT newval2;
11657
11658           newval   = md_chars_to_number (buf, THUMB_SIZE);
11659           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
11660           newval  |= (value & 0x7fffff) >> 12;
11661           newval2 |= (value & 0xfff) >> 1;
11662           md_number_to_chars (buf, newval, THUMB_SIZE);
11663           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
11664         }
11665       break;
11666
11667     case BFD_RELOC_THUMB_PCREL_BRANCH25:
11668       if ((value & ~0x1ffffff) && ((value & ~0x1ffffff) != ~0x1ffffff))
11669         as_bad_where (fixP->fx_file, fixP->fx_line,
11670                       _("branch out of range"));
11671
11672       if (fixP->fx_done || !seg->use_rela_p)
11673         {
11674           offsetT newval2;
11675           addressT S, I1, I2, lo, hi;
11676
11677           S  = (value & 0x01000000) >> 24;
11678           I1 = (value & 0x00800000) >> 23;
11679           I2 = (value & 0x00400000) >> 22;
11680           hi = (value & 0x003ff000) >> 12;
11681           lo = (value & 0x00000ffe) >> 1;
11682
11683           I1 = !(I1 ^ S);
11684           I2 = !(I2 ^ S);
11685
11686           newval   = md_chars_to_number (buf, THUMB_SIZE);
11687           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
11688           newval  |= (S << 10) | hi;
11689           newval2 |= (I1 << 13) | (I2 << 11) | lo;
11690           md_number_to_chars (buf, newval, THUMB_SIZE);
11691           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
11692         }
11693       break;
11694
11695     case BFD_RELOC_8:
11696       if (fixP->fx_done || !seg->use_rela_p)
11697         md_number_to_chars (buf, value, 1);
11698       break;
11699
11700     case BFD_RELOC_16:
11701       if (fixP->fx_done || !seg->use_rela_p)
11702         md_number_to_chars (buf, value, 2);
11703       break;
11704
11705 #ifdef OBJ_ELF
11706     case BFD_RELOC_ARM_TLS_GD32:
11707     case BFD_RELOC_ARM_TLS_LE32:
11708     case BFD_RELOC_ARM_TLS_IE32:
11709     case BFD_RELOC_ARM_TLS_LDM32:
11710     case BFD_RELOC_ARM_TLS_LDO32:
11711       S_SET_THREAD_LOCAL (fixP->fx_addsy);
11712       /* fall through */
11713
11714     case BFD_RELOC_ARM_GOT32:
11715     case BFD_RELOC_ARM_GOTOFF:
11716     case BFD_RELOC_ARM_TARGET2:
11717       if (fixP->fx_done || !seg->use_rela_p)
11718         md_number_to_chars (buf, 0, 4);
11719       break;
11720 #endif
11721
11722     case BFD_RELOC_RVA:
11723     case BFD_RELOC_32:
11724     case BFD_RELOC_ARM_TARGET1:
11725     case BFD_RELOC_ARM_ROSEGREL32:
11726     case BFD_RELOC_ARM_SBREL32:
11727     case BFD_RELOC_32_PCREL:
11728       if (fixP->fx_done || !seg->use_rela_p)
11729         md_number_to_chars (buf, value, 4);
11730       break;
11731
11732 #ifdef OBJ_ELF
11733     case BFD_RELOC_ARM_PREL31:
11734       if (fixP->fx_done || !seg->use_rela_p)
11735         {
11736           newval = md_chars_to_number (buf, 4) & 0x80000000;
11737           if ((value ^ (value >> 1)) & 0x40000000)
11738             {
11739               as_bad_where (fixP->fx_file, fixP->fx_line,
11740                             _("rel31 relocation overflow"));
11741             }
11742           newval |= value & 0x7fffffff;
11743           md_number_to_chars (buf, newval, 4);
11744         }
11745       break;
11746 #endif
11747
11748     case BFD_RELOC_ARM_CP_OFF_IMM:
11749     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
11750       if (value < -1023 || value > 1023 || (value & 3))
11751         as_bad_where (fixP->fx_file, fixP->fx_line,
11752                       _("co-processor offset out of range"));
11753     cp_off_common:
11754       sign = value >= 0;
11755       if (value < 0)
11756         value = -value;
11757       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
11758           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
11759         newval = md_chars_to_number (buf, INSN_SIZE);
11760       else
11761         newval = get_thumb32_insn (buf);
11762       newval &= 0xff7fff00;
11763       newval |= (value >> 2) | (sign ? INDEX_UP : 0);
11764       if (value == 0)
11765         newval &= ~WRITE_BACK;
11766       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
11767           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
11768         md_number_to_chars (buf, newval, INSN_SIZE);
11769       else
11770         put_thumb32_insn (buf, newval);
11771       break;
11772
11773     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
11774     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
11775       if (value < -255 || value > 255)
11776         as_bad_where (fixP->fx_file, fixP->fx_line,
11777                       _("co-processor offset out of range"));
11778       goto cp_off_common;
11779
11780     case BFD_RELOC_ARM_THUMB_OFFSET:
11781       newval = md_chars_to_number (buf, THUMB_SIZE);
11782       /* Exactly what ranges, and where the offset is inserted depends
11783          on the type of instruction, we can establish this from the
11784          top 4 bits.  */
11785       switch (newval >> 12)
11786         {
11787         case 4: /* PC load.  */
11788           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
11789              forced to zero for these loads; md_pcrel_from has already
11790              compensated for this.  */
11791           if (value & 3)
11792             as_bad_where (fixP->fx_file, fixP->fx_line,
11793                           _("invalid offset, target not word aligned (0x%08lX)"),
11794                           (((unsigned long) fixP->fx_frag->fr_address
11795                             + (unsigned long) fixP->fx_where) & ~3)
11796                           + (unsigned long) value);
11797
11798           if (value & ~0x3fc)
11799             as_bad_where (fixP->fx_file, fixP->fx_line,
11800                           _("invalid offset, value too big (0x%08lX)"),
11801                           (long) value);
11802
11803           newval |= value >> 2;
11804           break;
11805
11806         case 9: /* SP load/store.  */
11807           if (value & ~0x3fc)
11808             as_bad_where (fixP->fx_file, fixP->fx_line,
11809                           _("invalid offset, value too big (0x%08lX)"),
11810                           (long) value);
11811           newval |= value >> 2;
11812           break;
11813
11814         case 6: /* Word load/store.  */
11815           if (value & ~0x7c)
11816             as_bad_where (fixP->fx_file, fixP->fx_line,
11817                           _("invalid offset, value too big (0x%08lX)"),
11818                           (long) value);
11819           newval |= value << 4; /* 6 - 2.  */
11820           break;
11821
11822         case 7: /* Byte load/store.  */
11823           if (value & ~0x1f)
11824             as_bad_where (fixP->fx_file, fixP->fx_line,
11825                           _("invalid offset, value too big (0x%08lX)"),
11826                           (long) value);
11827           newval |= value << 6;
11828           break;
11829
11830         case 8: /* Halfword load/store.  */
11831           if (value & ~0x3e)
11832             as_bad_where (fixP->fx_file, fixP->fx_line,
11833                           _("invalid offset, value too big (0x%08lX)"),
11834                           (long) value);
11835           newval |= value << 5; /* 6 - 1.  */
11836           break;
11837
11838         default:
11839           as_bad_where (fixP->fx_file, fixP->fx_line,
11840                         "Unable to process relocation for thumb opcode: %lx",
11841                         (unsigned long) newval);
11842           break;
11843         }
11844       md_number_to_chars (buf, newval, THUMB_SIZE);
11845       break;
11846
11847     case BFD_RELOC_ARM_THUMB_ADD:
11848       /* This is a complicated relocation, since we use it for all of
11849          the following immediate relocations:
11850
11851             3bit ADD/SUB
11852             8bit ADD/SUB
11853             9bit ADD/SUB SP word-aligned
11854            10bit ADD PC/SP word-aligned
11855
11856          The type of instruction being processed is encoded in the
11857          instruction field:
11858
11859            0x8000  SUB
11860            0x00F0  Rd
11861            0x000F  Rs
11862       */
11863       newval = md_chars_to_number (buf, THUMB_SIZE);
11864       {
11865         int rd = (newval >> 4) & 0xf;
11866         int rs = newval & 0xf;
11867         int subtract = !!(newval & 0x8000);
11868
11869         /* Check for HI regs, only very restricted cases allowed:
11870            Adjusting SP, and using PC or SP to get an address.  */
11871         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
11872             || (rs > 7 && rs != REG_SP && rs != REG_PC))
11873           as_bad_where (fixP->fx_file, fixP->fx_line,
11874                         _("invalid Hi register with immediate"));
11875
11876         /* If value is negative, choose the opposite instruction.  */
11877         if (value < 0)
11878           {
11879             value = -value;
11880             subtract = !subtract;
11881             if (value < 0)
11882               as_bad_where (fixP->fx_file, fixP->fx_line,
11883                             _("immediate value out of range"));
11884           }
11885
11886         if (rd == REG_SP)
11887           {
11888             if (value & ~0x1fc)
11889               as_bad_where (fixP->fx_file, fixP->fx_line,
11890                             _("invalid immediate for stack address calculation"));
11891             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
11892             newval |= value >> 2;
11893           }
11894         else if (rs == REG_PC || rs == REG_SP)
11895           {
11896             if (subtract || value & ~0x3fc)
11897               as_bad_where (fixP->fx_file, fixP->fx_line,
11898                             _("invalid immediate for address calculation (value = 0x%08lX)"),
11899                             (unsigned long) value);
11900             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
11901             newval |= rd << 8;
11902             newval |= value >> 2;
11903           }
11904         else if (rs == rd)
11905           {
11906             if (value & ~0xff)
11907               as_bad_where (fixP->fx_file, fixP->fx_line,
11908                             _("immediate value out of range"));
11909             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
11910             newval |= (rd << 8) | value;
11911           }
11912         else
11913           {
11914             if (value & ~0x7)
11915               as_bad_where (fixP->fx_file, fixP->fx_line,
11916                             _("immediate value out of range"));
11917             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
11918             newval |= rd | (rs << 3) | (value << 6);
11919           }
11920       }
11921       md_number_to_chars (buf, newval, THUMB_SIZE);
11922       break;
11923
11924     case BFD_RELOC_ARM_THUMB_IMM:
11925       newval = md_chars_to_number (buf, THUMB_SIZE);
11926       if (value < 0 || value > 255)
11927         as_bad_where (fixP->fx_file, fixP->fx_line,
11928                       _("invalid immediate: %ld is too large"),
11929                       (long) value);
11930       newval |= value;
11931       md_number_to_chars (buf, newval, THUMB_SIZE);
11932       break;
11933
11934     case BFD_RELOC_ARM_THUMB_SHIFT:
11935       /* 5bit shift value (0..32).  LSL cannot take 32.  */
11936       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
11937       temp = newval & 0xf800;
11938       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
11939         as_bad_where (fixP->fx_file, fixP->fx_line,
11940                       _("invalid shift value: %ld"), (long) value);
11941       /* Shifts of zero must be encoded as LSL.  */
11942       if (value == 0)
11943         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
11944       /* Shifts of 32 are encoded as zero.  */
11945       else if (value == 32)
11946         value = 0;
11947       newval |= value << 6;
11948       md_number_to_chars (buf, newval, THUMB_SIZE);
11949       break;
11950
11951     case BFD_RELOC_VTABLE_INHERIT:
11952     case BFD_RELOC_VTABLE_ENTRY:
11953       fixP->fx_done = 0;
11954       return;
11955
11956     case BFD_RELOC_UNUSED:
11957     default:
11958       as_bad_where (fixP->fx_file, fixP->fx_line,
11959                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
11960     }
11961 }
11962
11963 /* Translate internal representation of relocation info to BFD target
11964    format.  */
11965
11966 arelent *
11967 tc_gen_reloc (asection * section ATTRIBUTE_UNUSED,
11968               fixS *     fixp)
11969 {
11970   arelent * reloc;
11971   bfd_reloc_code_real_type code;
11972
11973   reloc = xmalloc (sizeof (arelent));
11974
11975   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
11976   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
11977   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11978
11979   if (fixp->fx_pcrel)
11980     fixp->fx_offset = reloc->address;
11981   reloc->addend = fixp->fx_offset;
11982
11983   switch (fixp->fx_r_type)
11984     {
11985     case BFD_RELOC_8:
11986       if (fixp->fx_pcrel)
11987         {
11988           code = BFD_RELOC_8_PCREL;
11989           break;
11990         }
11991
11992     case BFD_RELOC_16:
11993       if (fixp->fx_pcrel)
11994         {
11995           code = BFD_RELOC_16_PCREL;
11996           break;
11997         }
11998
11999     case BFD_RELOC_32:
12000       if (fixp->fx_pcrel)
12001         {
12002           code = BFD_RELOC_32_PCREL;
12003           break;
12004         }
12005
12006     case BFD_RELOC_NONE:
12007     case BFD_RELOC_ARM_PCREL_BRANCH:
12008     case BFD_RELOC_ARM_PCREL_BLX:
12009     case BFD_RELOC_RVA:
12010     case BFD_RELOC_THUMB_PCREL_BRANCH7:
12011     case BFD_RELOC_THUMB_PCREL_BRANCH9:
12012     case BFD_RELOC_THUMB_PCREL_BRANCH12:
12013     case BFD_RELOC_THUMB_PCREL_BRANCH20:
12014     case BFD_RELOC_THUMB_PCREL_BRANCH23:
12015     case BFD_RELOC_THUMB_PCREL_BRANCH25:
12016     case BFD_RELOC_THUMB_PCREL_BLX:
12017     case BFD_RELOC_VTABLE_ENTRY:
12018     case BFD_RELOC_VTABLE_INHERIT:
12019       code = fixp->fx_r_type;
12020       break;
12021
12022     case BFD_RELOC_ARM_LITERAL:
12023     case BFD_RELOC_ARM_HWLITERAL:
12024       /* If this is called then the a literal has
12025          been referenced across a section boundary.  */
12026       as_bad_where (fixp->fx_file, fixp->fx_line,
12027                     _("literal referenced across section boundary"));
12028       return NULL;
12029
12030 #ifdef OBJ_ELF
12031     case BFD_RELOC_ARM_GOT32:
12032     case BFD_RELOC_ARM_GOTOFF:
12033     case BFD_RELOC_ARM_PLT32:
12034     case BFD_RELOC_ARM_TARGET1:
12035     case BFD_RELOC_ARM_ROSEGREL32:
12036     case BFD_RELOC_ARM_SBREL32:
12037     case BFD_RELOC_ARM_PREL31:
12038     case BFD_RELOC_ARM_TARGET2:
12039     case BFD_RELOC_ARM_TLS_LE32:
12040     case BFD_RELOC_ARM_TLS_LDO32:
12041     case BFD_RELOC_ARM_PCREL_CALL:
12042     case BFD_RELOC_ARM_PCREL_JUMP:
12043       code = fixp->fx_r_type;
12044       break;
12045
12046     case BFD_RELOC_ARM_TLS_GD32:
12047     case BFD_RELOC_ARM_TLS_IE32:
12048     case BFD_RELOC_ARM_TLS_LDM32:
12049       /* BFD will include the symbol's address in the addend.
12050          But we don't want that, so subtract it out again here.  */
12051       if (!S_IS_COMMON (fixp->fx_addsy))
12052         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
12053       code = fixp->fx_r_type;
12054       break;
12055 #endif
12056
12057     case BFD_RELOC_ARM_IMMEDIATE:
12058       as_bad_where (fixp->fx_file, fixp->fx_line,
12059                     _("internal relocation (type: IMMEDIATE) not fixed up"));
12060       return NULL;
12061
12062     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
12063       as_bad_where (fixp->fx_file, fixp->fx_line,
12064                     _("ADRL used for a symbol not defined in the same file"));
12065       return NULL;
12066
12067     case BFD_RELOC_ARM_OFFSET_IMM:
12068       if (fixp->fx_addsy != NULL
12069           && !S_IS_DEFINED (fixp->fx_addsy)
12070           && S_IS_LOCAL (fixp->fx_addsy))
12071         {
12072           as_bad_where (fixp->fx_file, fixp->fx_line,
12073                         _("undefined local label `%s'"),
12074                         S_GET_NAME (fixp->fx_addsy));
12075           return NULL;
12076         }
12077
12078       as_bad_where (fixp->fx_file, fixp->fx_line,
12079                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
12080       return NULL;
12081
12082     default:
12083       {
12084         char * type;
12085
12086         switch (fixp->fx_r_type)
12087           {
12088           case BFD_RELOC_NONE:             type = "NONE";         break;
12089           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
12090           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
12091           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
12092           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
12093           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
12094           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
12095           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
12096           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
12097           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
12098           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
12099           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
12100           default:                         type = _("<unknown>"); break;
12101           }
12102         as_bad_where (fixp->fx_file, fixp->fx_line,
12103                       _("cannot represent %s relocation in this object file format"),
12104                       type);
12105         return NULL;
12106       }
12107     }
12108
12109 #ifdef OBJ_ELF
12110   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
12111       && GOT_symbol
12112       && fixp->fx_addsy == GOT_symbol)
12113     {
12114       code = BFD_RELOC_ARM_GOTPC;
12115       reloc->addend = fixp->fx_offset = reloc->address;
12116     }
12117 #endif
12118
12119   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12120
12121   if (reloc->howto == NULL)
12122     {
12123       as_bad_where (fixp->fx_file, fixp->fx_line,
12124                     _("cannot represent %s relocation in this object file format"),
12125                     bfd_get_reloc_code_name (code));
12126       return NULL;
12127     }
12128
12129   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
12130      vtable entry to be used in the relocation's section offset.  */
12131   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12132     reloc->address = fixp->fx_offset;
12133
12134   return reloc;
12135 }
12136
12137 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
12138
12139 void
12140 cons_fix_new_arm (fragS *       frag,
12141                   int           where,
12142                   int           size,
12143                   expressionS * exp)
12144 {
12145   bfd_reloc_code_real_type type;
12146   int pcrel = 0;
12147
12148   /* Pick a reloc.
12149      FIXME: @@ Should look at CPU word size.  */
12150   switch (size)
12151     {
12152     case 1:
12153       type = BFD_RELOC_8;
12154       break;
12155     case 2:
12156       type = BFD_RELOC_16;
12157       break;
12158     case 4:
12159     default:
12160       type = BFD_RELOC_32;
12161       break;
12162     case 8:
12163       type = BFD_RELOC_64;
12164       break;
12165     }
12166
12167   fix_new_exp (frag, where, (int) size, exp, pcrel, type);
12168 }
12169
12170 #if defined OBJ_COFF || defined OBJ_ELF
12171 void
12172 arm_validate_fix (fixS * fixP)
12173 {
12174   /* If the destination of the branch is a defined symbol which does not have
12175      the THUMB_FUNC attribute, then we must be calling a function which has
12176      the (interfacearm) attribute.  We look for the Thumb entry point to that
12177      function and change the branch to refer to that function instead.  */
12178   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
12179       && fixP->fx_addsy != NULL
12180       && S_IS_DEFINED (fixP->fx_addsy)
12181       && ! THUMB_IS_FUNC (fixP->fx_addsy))
12182     {
12183       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
12184     }
12185 }
12186 #endif
12187
12188 int
12189 arm_force_relocation (struct fix * fixp)
12190 {
12191 #if defined (OBJ_COFF) && defined (TE_PE)
12192   if (fixp->fx_r_type == BFD_RELOC_RVA)
12193     return 1;
12194 #endif
12195
12196   /* Resolve these relocations even if the symbol is extern or weak.  */
12197   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
12198       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
12199       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
12200       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
12201       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
12202       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12)
12203     return 0;
12204
12205   return generic_force_reloc (fixp);
12206 }
12207
12208 #ifdef OBJ_COFF
12209 /* This is a little hack to help the gas/arm/adrl.s test.  It prevents
12210    local labels from being added to the output symbol table when they
12211    are used with the ADRL pseudo op.  The ADRL relocation should always
12212    be resolved before the binbary is emitted, so it is safe to say that
12213    it is adjustable.  */
12214
12215 bfd_boolean
12216 arm_fix_adjustable (fixS * fixP)
12217 {
12218   if (fixP->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE)
12219     return 1;
12220   return 0;
12221 }
12222 #endif
12223
12224 #ifdef OBJ_ELF
12225 /* Relocations against Thumb function names must be left unadjusted,
12226    so that the linker can use this information to correctly set the
12227    bottom bit of their addresses.  The MIPS version of this function
12228    also prevents relocations that are mips-16 specific, but I do not
12229    know why it does this.
12230
12231    FIXME:
12232    There is one other problem that ought to be addressed here, but
12233    which currently is not:  Taking the address of a label (rather
12234    than a function) and then later jumping to that address.  Such
12235    addresses also ought to have their bottom bit set (assuming that
12236    they reside in Thumb code), but at the moment they will not.  */
12237
12238 bfd_boolean
12239 arm_fix_adjustable (fixS * fixP)
12240 {
12241   if (fixP->fx_addsy == NULL)
12242     return 1;
12243
12244   if (THUMB_IS_FUNC (fixP->fx_addsy)
12245       && fixP->fx_subsy == NULL)
12246     return 0;
12247
12248   /* We need the symbol name for the VTABLE entries.  */
12249   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12250       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12251     return 0;
12252
12253   /* Don't allow symbols to be discarded on GOT related relocs.  */
12254   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
12255       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
12256       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
12257       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
12258       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
12259       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
12260       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
12261       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
12262       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
12263     return 0;
12264
12265   return 1;
12266 }
12267
12268 const char *
12269 elf32_arm_target_format (void)
12270 {
12271 #ifdef TE_SYMBIAN
12272   return (target_big_endian
12273           ? "elf32-bigarm-symbian"
12274           : "elf32-littlearm-symbian");
12275 #elif defined (TE_VXWORKS)
12276   return (target_big_endian
12277           ? "elf32-bigarm-vxworks"
12278           : "elf32-littlearm-vxworks");
12279 #else
12280   if (target_big_endian)
12281     return "elf32-bigarm";
12282   else
12283     return "elf32-littlearm";
12284 #endif
12285 }
12286
12287 void
12288 armelf_frob_symbol (symbolS * symp,
12289                     int *     puntp)
12290 {
12291   elf_frob_symbol (symp, puntp);
12292 }
12293 #endif
12294
12295 /* MD interface: Finalization.  */
12296
12297 /* A good place to do this, although this was probably not intended
12298    for this kind of use.  We need to dump the literal pool before
12299    references are made to a null symbol pointer.  */
12300
12301 void
12302 arm_cleanup (void)
12303 {
12304   literal_pool * pool;
12305
12306   for (pool = list_of_pools; pool; pool = pool->next)
12307     {
12308       /* Put it at the end of the relevent section.  */
12309       subseg_set (pool->section, pool->sub_section);
12310 #ifdef OBJ_ELF
12311       arm_elf_change_section ();
12312 #endif
12313       s_ltorg (0);
12314     }
12315 }
12316
12317 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
12318    ARM ones.  */
12319
12320 void
12321 arm_adjust_symtab (void)
12322 {
12323 #ifdef OBJ_COFF
12324   symbolS * sym;
12325
12326   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
12327     {
12328       if (ARM_IS_THUMB (sym))
12329         {
12330           if (THUMB_IS_FUNC (sym))
12331             {
12332               /* Mark the symbol as a Thumb function.  */
12333               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
12334                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
12335                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
12336
12337               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
12338                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
12339               else
12340                 as_bad (_("%s: unexpected function type: %d"),
12341                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
12342             }
12343           else switch (S_GET_STORAGE_CLASS (sym))
12344             {
12345             case C_EXT:
12346               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
12347               break;
12348             case C_STAT:
12349               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
12350               break;
12351             case C_LABEL:
12352               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
12353               break;
12354             default:
12355               /* Do nothing.  */
12356               break;
12357             }
12358         }
12359
12360       if (ARM_IS_INTERWORK (sym))
12361         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
12362     }
12363 #endif
12364 #ifdef OBJ_ELF
12365   symbolS * sym;
12366   char      bind;
12367
12368   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
12369     {
12370       if (ARM_IS_THUMB (sym))
12371         {
12372           elf_symbol_type * elf_sym;
12373
12374           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
12375           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
12376
12377           if (! bfd_is_arm_mapping_symbol_name (elf_sym->symbol.name))
12378             {
12379               /* If it's a .thumb_func, declare it as so,
12380                  otherwise tag label as .code 16.  */
12381               if (THUMB_IS_FUNC (sym))
12382                 elf_sym->internal_elf_sym.st_info =
12383                   ELF_ST_INFO (bind, STT_ARM_TFUNC);
12384               else
12385                 elf_sym->internal_elf_sym.st_info =
12386                   ELF_ST_INFO (bind, STT_ARM_16BIT);
12387             }
12388         }
12389     }
12390 #endif
12391 }
12392
12393 /* MD interface: Initialization.  */
12394
12395 static void
12396 set_constant_flonums (void)
12397 {
12398   int i;
12399
12400   for (i = 0; i < NUM_FLOAT_VALS; i++)
12401     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
12402       abort ();
12403 }
12404
12405 void
12406 md_begin (void)
12407 {
12408   unsigned mach;
12409   unsigned int i;
12410
12411   if (   (arm_ops_hsh = hash_new ()) == NULL
12412       || (arm_cond_hsh = hash_new ()) == NULL
12413       || (arm_shift_hsh = hash_new ()) == NULL
12414       || (arm_psr_hsh = hash_new ()) == NULL
12415       || (arm_reg_hsh = hash_new ()) == NULL
12416       || (arm_reloc_hsh = hash_new ()) == NULL)
12417     as_fatal (_("virtual memory exhausted"));
12418
12419   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
12420     hash_insert (arm_ops_hsh, insns[i].template, (PTR) (insns + i));
12421   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
12422     hash_insert (arm_cond_hsh, conds[i].template, (PTR) (conds + i));
12423   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
12424     hash_insert (arm_shift_hsh, shift_names[i].name, (PTR) (shift_names + i));
12425   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
12426     hash_insert (arm_psr_hsh, psrs[i].template, (PTR) (psrs + i));
12427   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
12428     hash_insert (arm_reg_hsh, reg_names[i].name, (PTR) (reg_names + i));
12429 #ifdef OBJ_ELF
12430   for (i = 0; i < sizeof (reloc_names) / sizeof (struct reloc_entry); i++)
12431     hash_insert (arm_reloc_hsh, reloc_names[i].name, (PTR) (reloc_names + i));
12432 #endif
12433
12434   set_constant_flonums ();
12435
12436   /* Set the cpu variant based on the command-line options.  We prefer
12437      -mcpu= over -march= if both are set (as for GCC); and we prefer
12438      -mfpu= over any other way of setting the floating point unit.
12439      Use of legacy options with new options are faulted.  */
12440   if (legacy_cpu)
12441     {
12442       if (mcpu_cpu_opt || march_cpu_opt)
12443         as_bad (_("use of old and new-style options to set CPU type"));
12444
12445       mcpu_cpu_opt = legacy_cpu;
12446     }
12447   else if (!mcpu_cpu_opt)
12448     mcpu_cpu_opt = march_cpu_opt;
12449
12450   if (legacy_fpu)
12451     {
12452       if (mfpu_opt)
12453         as_bad (_("use of old and new-style options to set FPU type"));
12454
12455       mfpu_opt = legacy_fpu;
12456     }
12457   else if (!mfpu_opt)
12458     {
12459 #if !(defined (TE_LINUX) || defined (TE_NetBSD) || defined (TE_VXWORKS))
12460       /* Some environments specify a default FPU.  If they don't, infer it
12461          from the processor.  */
12462       if (mcpu_fpu_opt)
12463         mfpu_opt = mcpu_fpu_opt;
12464       else
12465         mfpu_opt = march_fpu_opt;
12466 #else
12467       mfpu_opt = &fpu_default;
12468 #endif
12469     }
12470
12471   if (!mfpu_opt)
12472     {
12473       if (!mcpu_cpu_opt)
12474         mfpu_opt = &fpu_default;
12475       else if (ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
12476         mfpu_opt = &fpu_arch_vfp_v2;
12477       else
12478         mfpu_opt = &fpu_arch_fpa;
12479     }
12480
12481 #ifdef CPU_DEFAULT
12482   if (!mcpu_cpu_opt)
12483     {
12484       mcpu_cpu_opt = &cpu_default;
12485       selected_cpu = cpu_default;
12486     }
12487 #else
12488   if (mcpu_cpu_opt)
12489     selected_cpu = *mcpu_cpu_opt;
12490   else
12491     mcpu_cpu_opt = &arm_arch_any;
12492 #endif
12493
12494   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
12495
12496   arm_arch_used = thumb_arch_used = arm_arch_none;
12497
12498 #if defined OBJ_COFF || defined OBJ_ELF
12499   {
12500     unsigned int flags = 0;
12501
12502 #if defined OBJ_ELF
12503     flags = meabi_flags;
12504
12505     switch (meabi_flags)
12506       {
12507       case EF_ARM_EABI_UNKNOWN:
12508 #endif
12509         /* Set the flags in the private structure.  */
12510         if (uses_apcs_26)      flags |= F_APCS26;
12511         if (support_interwork) flags |= F_INTERWORK;
12512         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
12513         if (pic_code)          flags |= F_PIC;
12514         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
12515           flags |= F_SOFT_FLOAT;
12516
12517         switch (mfloat_abi_opt)
12518           {
12519           case ARM_FLOAT_ABI_SOFT:
12520           case ARM_FLOAT_ABI_SOFTFP:
12521             flags |= F_SOFT_FLOAT;
12522             break;
12523
12524           case ARM_FLOAT_ABI_HARD:
12525             if (flags & F_SOFT_FLOAT)
12526               as_bad (_("hard-float conflicts with specified fpu"));
12527             break;
12528           }
12529
12530         /* Using pure-endian doubles (even if soft-float).      */
12531         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
12532           flags |= F_VFP_FLOAT;
12533
12534 #if defined OBJ_ELF
12535         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
12536             flags |= EF_ARM_MAVERICK_FLOAT;
12537         break;
12538
12539       case EF_ARM_EABI_VER4:
12540         /* No additional flags to set.  */
12541         break;
12542
12543       default:
12544         abort ();
12545       }
12546 #endif
12547     bfd_set_private_flags (stdoutput, flags);
12548
12549     /* We have run out flags in the COFF header to encode the
12550        status of ATPCS support, so instead we create a dummy,
12551        empty, debug section called .arm.atpcs.  */
12552     if (atpcs)
12553       {
12554         asection * sec;
12555
12556         sec = bfd_make_section (stdoutput, ".arm.atpcs");
12557
12558         if (sec != NULL)
12559           {
12560             bfd_set_section_flags
12561               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
12562             bfd_set_section_size (stdoutput, sec, 0);
12563             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
12564           }
12565       }
12566   }
12567 #endif
12568
12569   /* Record the CPU type as well.  */
12570   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
12571     mach = bfd_mach_arm_iWMMXt;
12572   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
12573     mach = bfd_mach_arm_XScale;
12574   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
12575     mach = bfd_mach_arm_ep9312;
12576   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
12577     mach = bfd_mach_arm_5TE;
12578   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
12579     {
12580       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
12581         mach = bfd_mach_arm_5T;
12582       else
12583         mach = bfd_mach_arm_5;
12584     }
12585   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
12586     {
12587       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
12588         mach = bfd_mach_arm_4T;
12589       else
12590         mach = bfd_mach_arm_4;
12591     }
12592   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
12593     mach = bfd_mach_arm_3M;
12594   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
12595     mach = bfd_mach_arm_3;
12596   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
12597     mach = bfd_mach_arm_2a;
12598   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
12599     mach = bfd_mach_arm_2;
12600   else
12601     mach = bfd_mach_arm_unknown;
12602
12603   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
12604 }
12605
12606 /* Command line processing.  */
12607
12608 /* md_parse_option
12609       Invocation line includes a switch not recognized by the base assembler.
12610       See if it's a processor-specific option.
12611
12612       This routine is somewhat complicated by the need for backwards
12613       compatibility (since older releases of gcc can't be changed).
12614       The new options try to make the interface as compatible as
12615       possible with GCC.
12616
12617       New options (supported) are:
12618
12619               -mcpu=<cpu name>           Assemble for selected processor
12620               -march=<architecture name> Assemble for selected architecture
12621               -mfpu=<fpu architecture>   Assemble for selected FPU.
12622               -EB/-mbig-endian           Big-endian
12623               -EL/-mlittle-endian        Little-endian
12624               -k                         Generate PIC code
12625               -mthumb                    Start in Thumb mode
12626               -mthumb-interwork          Code supports ARM/Thumb interworking
12627
12628       For now we will also provide support for:
12629
12630               -mapcs-32                  32-bit Program counter
12631               -mapcs-26                  26-bit Program counter
12632               -macps-float               Floats passed in FP registers
12633               -mapcs-reentrant           Reentrant code
12634               -matpcs
12635       (sometime these will probably be replaced with -mapcs=<list of options>
12636       and -matpcs=<list of options>)
12637
12638       The remaining options are only supported for back-wards compatibility.
12639       Cpu variants, the arm part is optional:
12640               -m[arm]1                Currently not supported.
12641               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
12642               -m[arm]3                Arm 3 processor
12643               -m[arm]6[xx],           Arm 6 processors
12644               -m[arm]7[xx][t][[d]m]   Arm 7 processors
12645               -m[arm]8[10]            Arm 8 processors
12646               -m[arm]9[20][tdmi]      Arm 9 processors
12647               -mstrongarm[110[0]]     StrongARM processors
12648               -mxscale                XScale processors
12649               -m[arm]v[2345[t[e]]]    Arm architectures
12650               -mall                   All (except the ARM1)
12651       FP variants:
12652               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
12653               -mfpe-old               (No float load/store multiples)
12654               -mvfpxd                 VFP Single precision
12655               -mvfp                   All VFP
12656               -mno-fpu                Disable all floating point instructions
12657
12658       The following CPU names are recognized:
12659               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
12660               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
12661               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
12662               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
12663               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
12664               arm10t arm10e, arm1020t, arm1020e, arm10200e,
12665               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
12666
12667       */
12668
12669 const char * md_shortopts = "m:k";
12670
12671 #ifdef ARM_BI_ENDIAN
12672 #define OPTION_EB (OPTION_MD_BASE + 0)
12673 #define OPTION_EL (OPTION_MD_BASE + 1)
12674 #else
12675 #if TARGET_BYTES_BIG_ENDIAN
12676 #define OPTION_EB (OPTION_MD_BASE + 0)
12677 #else
12678 #define OPTION_EL (OPTION_MD_BASE + 1)
12679 #endif
12680 #endif
12681
12682 struct option md_longopts[] =
12683 {
12684 #ifdef OPTION_EB
12685   {"EB", no_argument, NULL, OPTION_EB},
12686 #endif
12687 #ifdef OPTION_EL
12688   {"EL", no_argument, NULL, OPTION_EL},
12689 #endif
12690   {NULL, no_argument, NULL, 0}
12691 };
12692
12693 size_t md_longopts_size = sizeof (md_longopts);
12694
12695 struct arm_option_table
12696 {
12697   char *option;         /* Option name to match.  */
12698   char *help;           /* Help information.  */
12699   int  *var;            /* Variable to change.  */
12700   int   value;          /* What to change it to.  */
12701   char *deprecated;     /* If non-null, print this message.  */
12702 };
12703
12704 struct arm_option_table arm_opts[] =
12705 {
12706   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
12707   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
12708   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
12709    &support_interwork, 1, NULL},
12710   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
12711   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
12712   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
12713    1, NULL},
12714   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
12715   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
12716   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
12717   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
12718    NULL},
12719
12720   /* These are recognized by the assembler, but have no affect on code.  */
12721   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
12722   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
12723   {NULL, NULL, NULL, 0, NULL}
12724 };
12725
12726 struct arm_legacy_option_table
12727 {
12728   char *option;                         /* Option name to match.  */
12729   const arm_feature_set **var;          /* Variable to change.  */
12730   const arm_feature_set value;          /* What to change it to.  */
12731   char *deprecated;                     /* If non-null, print this message.  */
12732 };
12733
12734 const struct arm_legacy_option_table arm_legacy_opts[] =
12735 {
12736   /* DON'T add any new processors to this list -- we want the whole list
12737      to go away...  Add them to the processors table instead.  */
12738   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
12739   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
12740   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
12741   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
12742   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
12743   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
12744   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
12745   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
12746   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
12747   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
12748   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
12749   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
12750   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
12751   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
12752   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
12753   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
12754   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
12755   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
12756   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
12757   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
12758   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
12759   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
12760   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
12761   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
12762   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
12763   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
12764   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
12765   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
12766   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
12767   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
12768   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
12769   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
12770   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
12771   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
12772   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
12773   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
12774   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
12775   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
12776   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
12777   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
12778   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
12779   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
12780   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
12781   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
12782   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
12783   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
12784   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
12785   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
12786   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
12787   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
12788   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
12789   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
12790   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
12791   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
12792   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
12793   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
12794   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
12795   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
12796   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
12797   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
12798   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
12799   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
12800   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
12801   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
12802   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
12803   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
12804   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
12805   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
12806   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
12807   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
12808    N_("use -mcpu=strongarm110")},
12809   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
12810    N_("use -mcpu=strongarm1100")},
12811   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
12812    N_("use -mcpu=strongarm1110")},
12813   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
12814   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
12815   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
12816
12817   /* Architecture variants -- don't add any more to this list either.  */
12818   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
12819   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
12820   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
12821   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
12822   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
12823   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
12824   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
12825   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
12826   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
12827   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
12828   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
12829   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
12830   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
12831   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
12832   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
12833   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
12834   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
12835   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
12836
12837   /* Floating point variants -- don't add any more to this list either.  */
12838   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
12839   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
12840   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
12841   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
12842    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
12843
12844   {NULL, NULL, ARM_ARCH_NONE, NULL}
12845 };
12846
12847 struct arm_cpu_option_table
12848 {
12849   char *name;
12850   const arm_feature_set value;
12851   /* For some CPUs we assume an FPU unless the user explicitly sets
12852      -mfpu=...  */
12853   const arm_feature_set default_fpu;
12854   /* The canonical name of the CPU, or NULL to use NAME converted to upper
12855      case.  */
12856   const char *canonical_name;
12857 };
12858
12859 /* This list should, at a minimum, contain all the cpu names
12860    recognized by GCC.  */
12861 static const struct arm_cpu_option_table arm_cpus[] =
12862 {
12863   {"all",               ARM_ANY,         FPU_ARCH_FPA,    NULL},
12864   {"arm1",              ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL},
12865   {"arm2",              ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL},
12866   {"arm250",            ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
12867   {"arm3",              ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
12868   {"arm6",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12869   {"arm60",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12870   {"arm600",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12871   {"arm610",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12872   {"arm620",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12873   {"arm7",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12874   {"arm7m",             ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
12875   {"arm7d",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12876   {"arm7dm",            ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
12877   {"arm7di",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12878   {"arm7dmi",           ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
12879   {"arm70",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12880   {"arm700",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12881   {"arm700i",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12882   {"arm710",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12883   {"arm710t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12884   {"arm720",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12885   {"arm720t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12886   {"arm740t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12887   {"arm710c",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12888   {"arm7100",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12889   {"arm7500",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12890   {"arm7500fe",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12891   {"arm7t",             ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12892   {"arm7tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12893   {"arm7tdmi-s",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12894   {"arm8",              ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12895   {"arm810",            ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12896   {"strongarm",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12897   {"strongarm1",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12898   {"strongarm110",      ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12899   {"strongarm1100",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12900   {"strongarm1110",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12901   {"arm9",              ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12902   {"arm920",            ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"},
12903   {"arm920t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12904   {"arm922t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12905   {"arm940t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12906   {"arm9tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12907   /* For V5 or later processors we default to using VFP; but the user
12908      should really set the FPU type explicitly.  */
12909   {"arm9e-r0",          ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
12910   {"arm9e",             ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12911   {"arm926ej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
12912   {"arm926ejs",         ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
12913   {"arm926ej-s",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
12914   {"arm946e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
12915   {"arm946e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"},
12916   {"arm946e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12917   {"arm966e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
12918   {"arm966e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"},
12919   {"arm966e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12920   {"arm968e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12921   {"arm10t",            ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
12922   {"arm10tdmi",         ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
12923   {"arm10e",            ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12924   {"arm1020",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"},
12925   {"arm1020t",          ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
12926   {"arm1020e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12927   {"arm1022e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12928   {"arm1026ejs",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM1026EJ-S"},
12929   {"arm1026ej-s",       ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
12930   {"arm1136js",         ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"},
12931   {"arm1136j-s",        ARM_ARCH_V6,     FPU_NONE,        NULL},
12932   {"arm1136jfs",        ARM_ARCH_V6,     FPU_ARCH_VFP_V2, "ARM1136JF-S"},
12933   {"arm1136jf-s",       ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL},
12934   {"mpcore",            ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, NULL},
12935   {"mpcorenovfp",       ARM_ARCH_V6K,    FPU_NONE,        NULL},
12936   {"arm1156t2-s",       ARM_ARCH_V6T2,   FPU_NONE,        NULL},
12937   {"arm1156t2f-s",      ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL},
12938   {"arm1176jz-s",       ARM_ARCH_V6ZK,   FPU_NONE,        NULL},
12939   {"arm1176jzf-s",      ARM_ARCH_V6ZK,   FPU_ARCH_VFP_V2, NULL},
12940   /* ??? XSCALE is really an architecture.  */
12941   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
12942   /* ??? iwmmxt is not a processor.  */
12943   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL},
12944   {"i80200",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
12945   /* Maverick */
12946   {"ep9312",    ARM_FEATURE(ARM_AEXT_V4T, ARM_CEXT_MAVERICK), FPU_ARCH_MAVERICK, "ARM920T"},
12947   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE, NULL}
12948 };
12949
12950 struct arm_arch_option_table
12951 {
12952   char *name;
12953   const arm_feature_set value;
12954   const arm_feature_set default_fpu;
12955 };
12956
12957 /* This list should, at a minimum, contain all the architecture names
12958    recognized by GCC.  */
12959 static const struct arm_arch_option_table arm_archs[] =
12960 {
12961   {"all",               ARM_ANY,         FPU_ARCH_FPA},
12962   {"armv1",             ARM_ARCH_V1,     FPU_ARCH_FPA},
12963   {"armv2",             ARM_ARCH_V2,     FPU_ARCH_FPA},
12964   {"armv2a",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
12965   {"armv2s",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
12966   {"armv3",             ARM_ARCH_V3,     FPU_ARCH_FPA},
12967   {"armv3m",            ARM_ARCH_V3M,    FPU_ARCH_FPA},
12968   {"armv4",             ARM_ARCH_V4,     FPU_ARCH_FPA},
12969   {"armv4xm",           ARM_ARCH_V4xM,   FPU_ARCH_FPA},
12970   {"armv4t",            ARM_ARCH_V4T,    FPU_ARCH_FPA},
12971   {"armv4txm",          ARM_ARCH_V4TxM,  FPU_ARCH_FPA},
12972   {"armv5",             ARM_ARCH_V5,     FPU_ARCH_VFP},
12973   {"armv5t",            ARM_ARCH_V5T,    FPU_ARCH_VFP},
12974   {"armv5txm",          ARM_ARCH_V5TxM,  FPU_ARCH_VFP},
12975   {"armv5te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP},
12976   {"armv5texp",         ARM_ARCH_V5TExP, FPU_ARCH_VFP},
12977   {"armv5tej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP},
12978   {"armv6",             ARM_ARCH_V6,     FPU_ARCH_VFP},
12979   {"armv6j",            ARM_ARCH_V6,     FPU_ARCH_VFP},
12980   {"armv6k",            ARM_ARCH_V6K,    FPU_ARCH_VFP},
12981   {"armv6z",            ARM_ARCH_V6Z,    FPU_ARCH_VFP},
12982   {"armv6zk",           ARM_ARCH_V6ZK,   FPU_ARCH_VFP},
12983   {"armv6t2",           ARM_ARCH_V6T2,   FPU_ARCH_VFP},
12984   {"armv6kt2",          ARM_ARCH_V6KT2,  FPU_ARCH_VFP},
12985   {"armv6zt2",          ARM_ARCH_V6ZT2,  FPU_ARCH_VFP},
12986   {"armv6zkt2",         ARM_ARCH_V6ZKT2, FPU_ARCH_VFP},
12987   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP},
12988   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP},
12989   {NULL,                ARM_ARCH_NONE,   ARM_ARCH_NONE}
12990 };
12991
12992 /* ISA extensions in the co-processor space.  */
12993 struct arm_option_cpu_value_table
12994 {
12995   char *name;
12996   const arm_feature_set value;
12997 };
12998
12999 static const struct arm_option_cpu_value_table arm_extensions[] =
13000 {
13001   {"maverick",          ARM_FEATURE (0, ARM_CEXT_MAVERICK)},
13002   {"xscale",            ARM_FEATURE (0, ARM_CEXT_XSCALE)},
13003   {"iwmmxt",            ARM_FEATURE (0, ARM_CEXT_IWMMXT)},
13004   {NULL,                ARM_ARCH_NONE}
13005 };
13006
13007 /* This list should, at a minimum, contain all the fpu names
13008    recognized by GCC.  */
13009 static const struct arm_option_cpu_value_table arm_fpus[] =
13010 {
13011   {"softfpa",           FPU_NONE},
13012   {"fpe",               FPU_ARCH_FPE},
13013   {"fpe2",              FPU_ARCH_FPE},
13014   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
13015   {"fpa",               FPU_ARCH_FPA},
13016   {"fpa10",             FPU_ARCH_FPA},
13017   {"fpa11",             FPU_ARCH_FPA},
13018   {"arm7500fe",         FPU_ARCH_FPA},
13019   {"softvfp",           FPU_ARCH_VFP},
13020   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
13021   {"vfp",               FPU_ARCH_VFP_V2},
13022   {"vfp9",              FPU_ARCH_VFP_V2},
13023   {"vfp10",             FPU_ARCH_VFP_V2},
13024   {"vfp10-r0",          FPU_ARCH_VFP_V1},
13025   {"vfpxd",             FPU_ARCH_VFP_V1xD},
13026   {"arm1020t",          FPU_ARCH_VFP_V1},
13027   {"arm1020e",          FPU_ARCH_VFP_V2},
13028   {"arm1136jfs",        FPU_ARCH_VFP_V2},
13029   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
13030   {"maverick",          FPU_ARCH_MAVERICK},
13031   {NULL,                ARM_ARCH_NONE}
13032 };
13033
13034 struct arm_option_value_table
13035 {
13036   char *name;
13037   long value;
13038 };
13039
13040 static const struct arm_option_value_table arm_float_abis[] =
13041 {
13042   {"hard",      ARM_FLOAT_ABI_HARD},
13043   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
13044   {"soft",      ARM_FLOAT_ABI_SOFT},
13045   {NULL,        0}
13046 };
13047
13048 #ifdef OBJ_ELF
13049 /* We only know how to output GNU and ver 4 (AAELF) formats.  */
13050 static const struct arm_option_value_table arm_eabis[] =
13051 {
13052   {"gnu",       EF_ARM_EABI_UNKNOWN},
13053   {"4",         EF_ARM_EABI_VER4},
13054   {NULL,        0}
13055 };
13056 #endif
13057
13058 struct arm_long_option_table
13059 {
13060   char * option;                /* Substring to match.  */
13061   char * help;                  /* Help information.  */
13062   int (* func) (char * subopt); /* Function to decode sub-option.  */
13063   char * deprecated;            /* If non-null, print this message.  */
13064 };
13065
13066 static int
13067 arm_parse_extension (char * str, const arm_feature_set **opt_p)
13068 {
13069   arm_feature_set *ext_set = xmalloc (sizeof (arm_feature_set));
13070
13071   /* Copy the feature set, so that we can modify it.  */
13072   *ext_set = **opt_p;
13073   *opt_p = ext_set;
13074
13075   while (str != NULL && *str != 0)
13076     {
13077       const struct arm_option_cpu_value_table * opt;
13078       char * ext;
13079       int optlen;
13080
13081       if (*str != '+')
13082         {
13083           as_bad (_("invalid architectural extension"));
13084           return 0;
13085         }
13086
13087       str++;
13088       ext = strchr (str, '+');
13089
13090       if (ext != NULL)
13091         optlen = ext - str;
13092       else
13093         optlen = strlen (str);
13094
13095       if (optlen == 0)
13096         {
13097           as_bad (_("missing architectural extension"));
13098           return 0;
13099         }
13100
13101       for (opt = arm_extensions; opt->name != NULL; opt++)
13102         if (strncmp (opt->name, str, optlen) == 0)
13103           {
13104             ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->value);
13105             break;
13106           }
13107
13108       if (opt->name == NULL)
13109         {
13110           as_bad (_("unknown architectural extnsion `%s'"), str);
13111           return 0;
13112         }
13113
13114       str = ext;
13115     };
13116
13117   return 1;
13118 }
13119
13120 static int
13121 arm_parse_cpu (char * str)
13122 {
13123   const struct arm_cpu_option_table * opt;
13124   char * ext = strchr (str, '+');
13125   int optlen;
13126
13127   if (ext != NULL)
13128     optlen = ext - str;
13129   else
13130     optlen = strlen (str);
13131
13132   if (optlen == 0)
13133     {
13134       as_bad (_("missing cpu name `%s'"), str);
13135       return 0;
13136     }
13137
13138   for (opt = arm_cpus; opt->name != NULL; opt++)
13139     if (strncmp (opt->name, str, optlen) == 0)
13140       {
13141         mcpu_cpu_opt = &opt->value;
13142         mcpu_fpu_opt = &opt->default_fpu;
13143         if (opt->canonical_name)
13144           strcpy(selected_cpu_name, opt->canonical_name);
13145         else
13146           {
13147             int i;
13148             for (i = 0; i < optlen; i++)
13149               selected_cpu_name[i] = TOUPPER (opt->name[i]);
13150             selected_cpu_name[i] = 0;
13151           }
13152
13153         if (ext != NULL)
13154           return arm_parse_extension (ext, &mcpu_cpu_opt);
13155
13156         return 1;
13157       }
13158
13159   as_bad (_("unknown cpu `%s'"), str);
13160   return 0;
13161 }
13162
13163 static int
13164 arm_parse_arch (char * str)
13165 {
13166   const struct arm_arch_option_table *opt;
13167   char *ext = strchr (str, '+');
13168   int optlen;
13169
13170   if (ext != NULL)
13171     optlen = ext - str;
13172   else
13173     optlen = strlen (str);
13174
13175   if (optlen == 0)
13176     {
13177       as_bad (_("missing architecture name `%s'"), str);
13178       return 0;
13179     }
13180
13181   for (opt = arm_archs; opt->name != NULL; opt++)
13182     if (streq (opt->name, str))
13183       {
13184         march_cpu_opt = &opt->value;
13185         march_fpu_opt = &opt->default_fpu;
13186         strcpy(selected_cpu_name, opt->name);
13187
13188         if (ext != NULL)
13189           return arm_parse_extension (ext, &march_cpu_opt);
13190
13191         return 1;
13192       }
13193
13194   as_bad (_("unknown architecture `%s'\n"), str);
13195   return 0;
13196 }
13197
13198 static int
13199 arm_parse_fpu (char * str)
13200 {
13201   const struct arm_option_cpu_value_table * opt;
13202
13203   for (opt = arm_fpus; opt->name != NULL; opt++)
13204     if (streq (opt->name, str))
13205       {
13206         mfpu_opt = &opt->value;
13207         return 1;
13208       }
13209
13210   as_bad (_("unknown floating point format `%s'\n"), str);
13211   return 0;
13212 }
13213
13214 static int
13215 arm_parse_float_abi (char * str)
13216 {
13217   const struct arm_option_value_table * opt;
13218
13219   for (opt = arm_float_abis; opt->name != NULL; opt++)
13220     if (streq (opt->name, str))
13221       {
13222         mfloat_abi_opt = opt->value;
13223         return 1;
13224       }
13225
13226   as_bad (_("unknown floating point abi `%s'\n"), str);
13227   return 0;
13228 }
13229
13230 #ifdef OBJ_ELF
13231 static int
13232 arm_parse_eabi (char * str)
13233 {
13234   const struct arm_option_value_table *opt;
13235
13236   for (opt = arm_eabis; opt->name != NULL; opt++)
13237     if (streq (opt->name, str))
13238       {
13239         meabi_flags = opt->value;
13240         return 1;
13241       }
13242   as_bad (_("unknown EABI `%s'\n"), str);
13243   return 0;
13244 }
13245 #endif
13246
13247 struct arm_long_option_table arm_long_opts[] =
13248 {
13249   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
13250    arm_parse_cpu, NULL},
13251   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
13252    arm_parse_arch, NULL},
13253   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
13254    arm_parse_fpu, NULL},
13255   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
13256    arm_parse_float_abi, NULL},
13257 #ifdef OBJ_ELF
13258   {"meabi=", N_("<ver>\t  assemble for eabi version <ver>"),
13259    arm_parse_eabi, NULL},
13260 #endif
13261   {NULL, NULL, 0, NULL}
13262 };
13263
13264 int
13265 md_parse_option (int c, char * arg)
13266 {
13267   struct arm_option_table *opt;
13268   const struct arm_legacy_option_table *fopt;
13269   struct arm_long_option_table *lopt;
13270
13271   switch (c)
13272     {
13273 #ifdef OPTION_EB
13274     case OPTION_EB:
13275       target_big_endian = 1;
13276       break;
13277 #endif
13278
13279 #ifdef OPTION_EL
13280     case OPTION_EL:
13281       target_big_endian = 0;
13282       break;
13283 #endif
13284
13285     case 'a':
13286       /* Listing option.  Just ignore these, we don't support additional
13287          ones.  */
13288       return 0;
13289
13290     default:
13291       for (opt = arm_opts; opt->option != NULL; opt++)
13292         {
13293           if (c == opt->option[0]
13294               && ((arg == NULL && opt->option[1] == 0)
13295                   || streq (arg, opt->option + 1)))
13296             {
13297 #if WARN_DEPRECATED
13298               /* If the option is deprecated, tell the user.  */
13299               if (opt->deprecated != NULL)
13300                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
13301                            arg ? arg : "", _(opt->deprecated));
13302 #endif
13303
13304               if (opt->var != NULL)
13305                 *opt->var = opt->value;
13306
13307               return 1;
13308             }
13309         }
13310
13311       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
13312         {
13313           if (c == fopt->option[0]
13314               && ((arg == NULL && fopt->option[1] == 0)
13315                   || streq (arg, fopt->option + 1)))
13316             {
13317 #if WARN_DEPRECATED
13318               /* If the option is deprecated, tell the user.  */
13319               if (fopt->deprecated != NULL)
13320                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
13321                            arg ? arg : "", _(fopt->deprecated));
13322 #endif
13323
13324               if (fopt->var != NULL)
13325                 *fopt->var = &fopt->value;
13326
13327               return 1;
13328             }
13329         }
13330
13331       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
13332         {
13333           /* These options are expected to have an argument.  */
13334           if (c == lopt->option[0]
13335               && arg != NULL
13336               && strncmp (arg, lopt->option + 1,
13337                           strlen (lopt->option + 1)) == 0)
13338             {
13339 #if WARN_DEPRECATED
13340               /* If the option is deprecated, tell the user.  */
13341               if (lopt->deprecated != NULL)
13342                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
13343                            _(lopt->deprecated));
13344 #endif
13345
13346               /* Call the sup-option parser.  */
13347               return lopt->func (arg + strlen (lopt->option) - 1);
13348             }
13349         }
13350
13351       return 0;
13352     }
13353
13354   return 1;
13355 }
13356
13357 void
13358 md_show_usage (FILE * fp)
13359 {
13360   struct arm_option_table *opt;
13361   struct arm_long_option_table *lopt;
13362
13363   fprintf (fp, _(" ARM-specific assembler options:\n"));
13364
13365   for (opt = arm_opts; opt->option != NULL; opt++)
13366     if (opt->help != NULL)
13367       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
13368
13369   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
13370     if (lopt->help != NULL)
13371       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
13372
13373 #ifdef OPTION_EB
13374   fprintf (fp, _("\
13375   -EB                     assemble code for a big-endian cpu\n"));
13376 #endif
13377
13378 #ifdef OPTION_EL
13379   fprintf (fp, _("\
13380   -EL                     assemble code for a little-endian cpu\n"));
13381 #endif
13382 }
13383
13384
13385 #ifdef OBJ_ELF
13386 /* Set the public EABI object attributes.  */
13387 static void
13388 aeabi_set_public_attributes (void)
13389 {
13390   int arch;
13391   arm_feature_set flags;
13392
13393   /* Choose the architecture based on the capabilities of the requested cpu
13394      (if any) and/or the instructions actually used.  */
13395   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
13396   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
13397   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
13398   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v6t2))
13399     arch = 8;
13400   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v6z))
13401     arch = 7;
13402   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v6k))
13403     arch = 9;
13404   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v6))
13405     arch = 6;
13406   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v5e))
13407     arch = 4;
13408   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v5)
13409            || ARM_CPU_HAS_FEATURE (flags, arm_ext_v5t))
13410     arch = 3;
13411   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t))
13412     arch = 2;
13413   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4))
13414     arch = 1;
13415   else
13416     arch = 0;
13417
13418   /* Tag_CPU_name.  */
13419   if (selected_cpu_name[0])
13420     {
13421       char *p;
13422
13423       p = selected_cpu_name;
13424       if (strncmp(p, "armv", 4) == 0)
13425         {
13426           int i;
13427           
13428           p += 4;
13429           for (i = 0; p[i]; i++)
13430             p[i] = TOUPPER (p[i]);
13431         }
13432       elf32_arm_add_eabi_attr_string (stdoutput, 5, p);
13433     }
13434   /* Tag_CPU_arch.  */
13435   elf32_arm_add_eabi_attr_int (stdoutput, 6, arch);
13436   /* Tag_ARM_ISA_use.  */
13437   if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_full))
13438     elf32_arm_add_eabi_attr_int (stdoutput, 8, 1);
13439   /* Tag_THUMB_ISA_use.  */
13440   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_full))
13441     elf32_arm_add_eabi_attr_int (stdoutput, 9,
13442         ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2) ? 2 : 1);
13443   /* Tag_VFP_arch.  */
13444   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_arch_vfp_v2)
13445       || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_arch_vfp_v2))
13446     elf32_arm_add_eabi_attr_int (stdoutput, 10, 2);
13447   else if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_arch_vfp_v1)
13448            || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_arch_vfp_v1))
13449     elf32_arm_add_eabi_attr_int (stdoutput, 10, 1);
13450   /* Tag_WMMX_arch.  */
13451   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_cext_iwmmxt)
13452       || ARM_CPU_HAS_FEATURE (arm_arch_used, arm_cext_iwmmxt))
13453     elf32_arm_add_eabi_attr_int (stdoutput, 11, 1);
13454 }
13455
13456 /* Add the .ARM.attributes section.  */
13457 void
13458 arm_md_end (void)
13459 {
13460   segT s;
13461   char *p;
13462   addressT addr;
13463   offsetT size;
13464   
13465   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
13466     return;
13467
13468   aeabi_set_public_attributes ();
13469   size = elf32_arm_eabi_attr_size (stdoutput);
13470   s = subseg_new (".ARM.attributes", 0);
13471   bfd_set_section_flags (stdoutput, s, SEC_READONLY | SEC_DATA);
13472   addr = frag_now_fix ();
13473   p = frag_more (size);
13474   elf32_arm_set_eabi_attr_contents (stdoutput, (bfd_byte *)p, size);
13475 }
13476
13477
13478 /* Parse a .cpu directive.  */
13479
13480 static void
13481 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
13482 {
13483   const struct arm_cpu_option_table *opt;
13484   char *name;
13485   char saved_char;
13486
13487   name = input_line_pointer;
13488   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
13489     input_line_pointer++;
13490   saved_char = *input_line_pointer;
13491   *input_line_pointer = 0;
13492
13493   /* Skip the first "all" entry.  */
13494   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
13495     if (streq (opt->name, name))
13496       {
13497         mcpu_cpu_opt = &opt->value;
13498         selected_cpu = opt->value;
13499         if (opt->canonical_name)
13500           strcpy(selected_cpu_name, opt->canonical_name);
13501         else
13502           {
13503             int i;
13504             for (i = 0; opt->name[i]; i++)
13505               selected_cpu_name[i] = TOUPPER (opt->name[i]);
13506             selected_cpu_name[i] = 0;
13507           }
13508         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
13509         *input_line_pointer = saved_char;
13510         demand_empty_rest_of_line ();
13511         return;
13512       }
13513   as_bad (_("unknown cpu `%s'"), name);
13514   *input_line_pointer = saved_char;
13515   ignore_rest_of_line ();
13516 }
13517
13518
13519 /* Parse a .arch directive.  */
13520
13521 static void
13522 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
13523 {
13524   const struct arm_arch_option_table *opt;
13525   char saved_char;
13526   char *name;
13527
13528   name = input_line_pointer;
13529   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
13530     input_line_pointer++;
13531   saved_char = *input_line_pointer;
13532   *input_line_pointer = 0;
13533
13534   /* Skip the first "all" entry.  */
13535   for (opt = arm_archs + 1; opt->name != NULL; opt++)
13536     if (streq (opt->name, name))
13537       {
13538         mcpu_cpu_opt = &opt->value;
13539         selected_cpu = opt->value;
13540         strcpy(selected_cpu_name, opt->name);
13541         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
13542         *input_line_pointer = saved_char;
13543         demand_empty_rest_of_line ();
13544         return;
13545       }
13546
13547   as_bad (_("unknown architecture `%s'\n"), name);
13548   *input_line_pointer = saved_char;
13549   ignore_rest_of_line ();
13550 }
13551
13552
13553 /* Parse a .fpu directive.  */
13554
13555 static void
13556 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
13557 {
13558   const struct arm_option_cpu_value_table *opt;
13559   char saved_char;
13560   char *name;
13561
13562   name = input_line_pointer;
13563   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
13564     input_line_pointer++;
13565   saved_char = *input_line_pointer;
13566   *input_line_pointer = 0;
13567   
13568   for (opt = arm_fpus; opt->name != NULL; opt++)
13569     if (streq (opt->name, name))
13570       {
13571         mfpu_opt = &opt->value;
13572         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
13573         *input_line_pointer = saved_char;
13574         demand_empty_rest_of_line ();
13575         return;
13576       }
13577
13578   as_bad (_("unknown floating point format `%s'\n"), name);
13579   *input_line_pointer = saved_char;
13580   ignore_rest_of_line ();
13581 }
13582 #endif /* OBJ_ELF */
13583