PR 1300
[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 #define ARM_1           ARM_ARCH_V1
102 #define ARM_2           ARM_ARCH_V2
103 #define ARM_3           ARM_ARCH_V2S
104 #define ARM_250         ARM_ARCH_V2S
105 #define ARM_6           ARM_ARCH_V3
106 #define ARM_7           ARM_ARCH_V3
107 #define ARM_8           ARM_ARCH_V4
108 #define ARM_9           ARM_ARCH_V4T
109 #define ARM_STRONG      ARM_ARCH_V4
110 #define ARM_CPU_MASK    0x0000000f              /* XXX? */
111
112 #ifndef CPU_DEFAULT
113 #if defined __XSCALE__
114 #define CPU_DEFAULT     (ARM_ARCH_XSCALE)
115 #else
116 #if defined __thumb__
117 #define CPU_DEFAULT     (ARM_ARCH_V5T)
118 #endif
119 #endif
120 #endif
121
122 #ifndef FPU_DEFAULT
123 # ifdef TE_LINUX
124 #  define FPU_DEFAULT FPU_ARCH_FPA
125 # elif defined (TE_NetBSD)
126 #  ifdef OBJ_ELF
127 #   define FPU_DEFAULT FPU_ARCH_VFP     /* Soft-float, but VFP order.  */
128 #  else
129     /* Legacy a.out format.  */
130 #   define FPU_DEFAULT FPU_ARCH_FPA     /* Soft-float, but FPA order.  */
131 #  endif
132 # elif defined (TE_VXWORKS)
133 #  define FPU_DEFAULT FPU_ARCH_VFP      /* Soft-float, VFP order.  */
134 # else
135    /* For backwards compatibility, default to FPA.  */
136 #  define FPU_DEFAULT FPU_ARCH_FPA
137 # endif
138 #endif /* ifndef FPU_DEFAULT */
139
140 #define streq(a, b)           (strcmp (a, b) == 0)
141
142 static unsigned long cpu_variant;
143 static unsigned long arm_arch_used;
144 static unsigned long thumb_arch_used;
145
146 /* Flags stored in private area of BFD structure.  */
147 static int uses_apcs_26      = FALSE;
148 static int atpcs             = FALSE;
149 static int support_interwork = FALSE;
150 static int uses_apcs_float   = FALSE;
151 static int pic_code          = FALSE;
152
153 /* Variables that we set while parsing command-line options.  Once all
154    options have been read we re-process these values to set the real
155    assembly flags.  */
156 static int legacy_cpu = -1;
157 static int legacy_fpu = -1;
158
159 static int mcpu_cpu_opt = -1;
160 static int mcpu_fpu_opt = -1;
161 static int march_cpu_opt = -1;
162 static int march_fpu_opt = -1;
163 static int mfpu_opt = -1;
164 static int mfloat_abi_opt = -1;
165 /* Record user cpu selection for object attributes.
166    Zero if no default or user specified CPU.  */
167 static int selected_cpu = -1;
168 /* Must be long enough to hold any of the names in arm_cpus.  */
169 static char selected_cpu_name[16];
170 #ifdef OBJ_ELF
171 # ifdef EABI_DEFAULT
172 static int meabi_flags = EABI_DEFAULT;
173 # else
174 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
175 # endif
176 #endif
177
178 #ifdef OBJ_ELF
179 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
180 symbolS * GOT_symbol;
181 #endif
182
183 /* 0: assemble for ARM,
184    1: assemble for Thumb,
185    2: assemble for Thumb even though target CPU does not support thumb
186       instructions.  */
187 static int thumb_mode = 0;
188
189 /* If unified_syntax is true, we are processing the new unified
190    ARM/Thumb syntax.  Important differences from the old ARM mode:
191
192      - Immediate operands do not require a # prefix.
193      - Conditional affixes always appear at the end of the
194        instruction.  (For backward compatibility, those instructions
195        that formerly had them in the middle, continue to accept them
196        there.)
197      - The IT instruction may appear, and if it does is validated
198        against subsequent conditional affixes.  It does not generate
199        machine code.
200
201    Important differences from the old Thumb mode:
202
203      - Immediate operands do not require a # prefix.
204      - Most of the V6T2 instructions are only available in unified mode.
205      - The .N and .W suffixes are recognized and honored (it is an error
206        if they cannot be honored).
207      - All instructions set the flags if and only if they have an 's' affix.
208      - Conditional affixes may be used.  They are validated against
209        preceding IT instructions.  Unlike ARM mode, you cannot use a
210        conditional affix except in the scope of an IT instruction.  */
211
212 static bfd_boolean unified_syntax = FALSE;
213
214 struct arm_it
215 {
216   const char *  error;
217   unsigned long instruction;
218   int           size;
219   int           size_req;
220   int           cond;
221   /* Set to the opcode if the instruction needs relaxation.
222      Zero if the instruction is not relaxed.  */
223   unsigned long relax;
224   struct
225   {
226     bfd_reloc_code_real_type type;
227     expressionS              exp;
228     int                      pc_rel;
229   } reloc;
230
231   struct
232   {
233     unsigned reg;
234     signed int imm;
235     unsigned present    : 1;  /* Operand present.  */
236     unsigned isreg      : 1;  /* Operand was a register.  */
237     unsigned immisreg   : 1;  /* .imm field is a second register.  */
238     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
239     unsigned writeback  : 1;  /* Operand has trailing !  */
240     unsigned preind     : 1;  /* Preindexed address.  */
241     unsigned postind    : 1;  /* Postindexed address.  */
242     unsigned negative   : 1;  /* Index register was negated.  */
243     unsigned shifted    : 1;  /* Shift applied to operation.  */
244     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
245   } operands[6];
246 };
247
248 static struct arm_it inst;
249
250 #define NUM_FLOAT_VALS 8
251
252 const char * fp_const[] =
253 {
254   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
255 };
256
257 /* Number of littlenums required to hold an extended precision number.  */
258 #define MAX_LITTLENUMS 6
259
260 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
261
262 #define FAIL    (-1)
263 #define SUCCESS (0)
264
265 #define SUFF_S 1
266 #define SUFF_D 2
267 #define SUFF_E 3
268 #define SUFF_P 4
269
270 #define CP_T_X   0x00008000
271 #define CP_T_Y   0x00400000
272
273 #define CONDS_BIT        0x00100000
274 #define LOAD_BIT         0x00100000
275
276 #define DOUBLE_LOAD_FLAG 0x00000001
277
278 struct asm_cond
279 {
280   const char *  template;
281   unsigned long value;
282 };
283
284 #define COND_ALWAYS 0xE
285
286 struct asm_psr
287 {
288   const char *template;
289   unsigned long field;
290 };
291
292 /* The bit that distinguishes CPSR and SPSR.  */
293 #define SPSR_BIT   (1 << 22)
294
295 /* The individual PSR flag bits.  */
296 #define PSR_c   (1 << 16)
297 #define PSR_x   (1 << 17)
298 #define PSR_s   (1 << 18)
299 #define PSR_f   (1 << 19)
300
301 struct reloc_entry
302 {
303   char *name;
304   bfd_reloc_code_real_type reloc;
305 };
306
307 enum vfp_sp_reg_pos
308 {
309   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn
310 };
311
312 enum vfp_ldstm_type
313 {
314   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
315 };
316
317 /* ARM register categories.  This includes coprocessor numbers and various
318    architecture extensions' registers.  */
319 enum arm_reg_type
320 {
321   REG_TYPE_RN,
322   REG_TYPE_CP,
323   REG_TYPE_CN,
324   REG_TYPE_FN,
325   REG_TYPE_VFS,
326   REG_TYPE_VFD,
327   REG_TYPE_VFC,
328   REG_TYPE_MVF,
329   REG_TYPE_MVD,
330   REG_TYPE_MVFX,
331   REG_TYPE_MVDX,
332   REG_TYPE_MVAX,
333   REG_TYPE_DSPSC,
334   REG_TYPE_MMXWR,
335   REG_TYPE_MMXWC,
336   REG_TYPE_MMXWCG,
337   REG_TYPE_XSCALE,
338 };
339
340 /* Structure for a hash table entry for a register.  */
341 struct reg_entry
342 {
343   const char   *name;
344   unsigned char number;
345   unsigned char type;
346   unsigned char builtin;
347 };
348
349 /* Diagnostics used when we don't get a register of the expected type.  */
350 const char *const reg_expected_msgs[] =
351 {
352   N_("ARM register expected"),
353   N_("bad or missing co-processor number"),
354   N_("co-processor register expected"),
355   N_("FPA register expected"),
356   N_("VFP single precision register expected"),
357   N_("VFP double precision register expected"),
358   N_("VFP system register expected"),
359   N_("Maverick MVF register expected"),
360   N_("Maverick MVD register expected"),
361   N_("Maverick MVFX register expected"),
362   N_("Maverick MVDX register expected"),
363   N_("Maverick MVAX register expected"),
364   N_("Maverick DSPSC register expected"),
365   N_("iWMMXt data register expected"),
366   N_("iWMMXt control register expected"),
367   N_("iWMMXt scalar register expected"),
368   N_("XScale accumulator register expected"),
369 };
370
371 /* Some well known registers that we refer to directly elsewhere.  */
372 #define REG_SP  13
373 #define REG_LR  14
374 #define REG_PC  15
375
376 /* ARM instructions take 4bytes in the object file, Thumb instructions
377    take 2:  */
378 #define INSN_SIZE       4
379
380 struct asm_opcode
381 {
382   /* Basic string to match.  */
383   const char *template;
384
385   /* Parameters to instruction.  */
386   unsigned char operands[8];
387
388   /* Conditional tag - see opcode_lookup.  */
389   unsigned int tag : 4;
390
391   /* Basic instruction code.  */
392   unsigned int avalue : 28;
393
394   /* Thumb-format instruction code.  */
395   unsigned int tvalue;
396
397   /* Which architecture variant provides this instruction.  */
398   unsigned long avariant;
399   unsigned long tvariant;
400
401   /* Function to call to encode instruction in ARM format.  */
402   void (* aencode) (void);
403
404   /* Function to call to encode instruction in Thumb format.  */
405   void (* tencode) (void);
406 };
407
408 /* Defines for various bits that we will want to toggle.  */
409 #define INST_IMMEDIATE  0x02000000
410 #define OFFSET_REG      0x02000000
411 #define HWOFFSET_IMM    0x00400000
412 #define SHIFT_BY_REG    0x00000010
413 #define PRE_INDEX       0x01000000
414 #define INDEX_UP        0x00800000
415 #define WRITE_BACK      0x00200000
416 #define LDM_TYPE_2_OR_3 0x00400000
417
418 #define LITERAL_MASK    0xf000f000
419 #define OPCODE_MASK     0xfe1fffff
420 #define V4_STR_BIT      0x00000020
421
422 #define DATA_OP_SHIFT   21
423
424 /* Codes to distinguish the arithmetic instructions.  */
425 #define OPCODE_AND      0
426 #define OPCODE_EOR      1
427 #define OPCODE_SUB      2
428 #define OPCODE_RSB      3
429 #define OPCODE_ADD      4
430 #define OPCODE_ADC      5
431 #define OPCODE_SBC      6
432 #define OPCODE_RSC      7
433 #define OPCODE_TST      8
434 #define OPCODE_TEQ      9
435 #define OPCODE_CMP      10
436 #define OPCODE_CMN      11
437 #define OPCODE_ORR      12
438 #define OPCODE_MOV      13
439 #define OPCODE_BIC      14
440 #define OPCODE_MVN      15
441
442 #define T_OPCODE_MUL 0x4340
443 #define T_OPCODE_TST 0x4200
444 #define T_OPCODE_CMN 0x42c0
445 #define T_OPCODE_NEG 0x4240
446 #define T_OPCODE_MVN 0x43c0
447
448 #define T_OPCODE_ADD_R3 0x1800
449 #define T_OPCODE_SUB_R3 0x1a00
450 #define T_OPCODE_ADD_HI 0x4400
451 #define T_OPCODE_ADD_ST 0xb000
452 #define T_OPCODE_SUB_ST 0xb080
453 #define T_OPCODE_ADD_SP 0xa800
454 #define T_OPCODE_ADD_PC 0xa000
455 #define T_OPCODE_ADD_I8 0x3000
456 #define T_OPCODE_SUB_I8 0x3800
457 #define T_OPCODE_ADD_I3 0x1c00
458 #define T_OPCODE_SUB_I3 0x1e00
459
460 #define T_OPCODE_ASR_R  0x4100
461 #define T_OPCODE_LSL_R  0x4080
462 #define T_OPCODE_LSR_R  0x40c0
463 #define T_OPCODE_ROR_R  0x41c0
464 #define T_OPCODE_ASR_I  0x1000
465 #define T_OPCODE_LSL_I  0x0000
466 #define T_OPCODE_LSR_I  0x0800
467
468 #define T_OPCODE_MOV_I8 0x2000
469 #define T_OPCODE_CMP_I8 0x2800
470 #define T_OPCODE_CMP_LR 0x4280
471 #define T_OPCODE_MOV_HR 0x4600
472 #define T_OPCODE_CMP_HR 0x4500
473
474 #define T_OPCODE_LDR_PC 0x4800
475 #define T_OPCODE_LDR_SP 0x9800
476 #define T_OPCODE_STR_SP 0x9000
477 #define T_OPCODE_LDR_IW 0x6800
478 #define T_OPCODE_STR_IW 0x6000
479 #define T_OPCODE_LDR_IH 0x8800
480 #define T_OPCODE_STR_IH 0x8000
481 #define T_OPCODE_LDR_IB 0x7800
482 #define T_OPCODE_STR_IB 0x7000
483 #define T_OPCODE_LDR_RW 0x5800
484 #define T_OPCODE_STR_RW 0x5000
485 #define T_OPCODE_LDR_RH 0x5a00
486 #define T_OPCODE_STR_RH 0x5200
487 #define T_OPCODE_LDR_RB 0x5c00
488 #define T_OPCODE_STR_RB 0x5400
489
490 #define T_OPCODE_PUSH   0xb400
491 #define T_OPCODE_POP    0xbc00
492
493 #define T_OPCODE_BRANCH 0xe000
494
495 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
496 #define THUMB_PP_PC_LR 0x0100
497 #define THUMB_LOAD_BIT 0x0800
498
499 #define BAD_ARGS        _("bad arguments to instruction")
500 #define BAD_PC          _("r15 not allowed here")
501 #define BAD_COND        _("instruction cannot be conditional")
502 #define BAD_OVERLAP     _("registers may not be the same")
503 #define BAD_HIREG       _("lo register required")
504 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
505 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
506
507 static struct hash_control *arm_ops_hsh;
508 static struct hash_control *arm_cond_hsh;
509 static struct hash_control *arm_shift_hsh;
510 static struct hash_control *arm_psr_hsh;
511 static struct hash_control *arm_reg_hsh;
512 static struct hash_control *arm_reloc_hsh;
513
514 /* Stuff needed to resolve the label ambiguity
515    As:
516      ...
517      label:   <insn>
518    may differ from:
519      ...
520      label:
521               <insn>
522 */
523
524 symbolS *  last_label_seen;
525 static int label_is_thumb_function_name = FALSE;
526 \f
527 /* Literal pool structure.  Held on a per-section
528    and per-sub-section basis.  */
529
530 #define MAX_LITERAL_POOL_SIZE 1024
531 typedef struct literal_pool
532 {
533   expressionS    literals [MAX_LITERAL_POOL_SIZE];
534   unsigned int   next_free_entry;
535   unsigned int   id;
536   symbolS *      symbol;
537   segT           section;
538   subsegT        sub_section;
539   struct literal_pool * next;
540 } literal_pool;
541
542 /* Pointer to a linked list of literal pools.  */
543 literal_pool * list_of_pools = NULL;
544
545 /* State variables for IT block handling.  */
546 static bfd_boolean current_it_mask = 0;
547 static int current_cc;
548
549 \f
550 /* Pure syntax.  */
551
552 /* This array holds the chars that always start a comment.  If the
553    pre-processor is disabled, these aren't very useful.  */
554 const char comment_chars[] = "@";
555
556 /* This array holds the chars that only start a comment at the beginning of
557    a line.  If the line seems to have the form '# 123 filename'
558    .line and .file directives will appear in the pre-processed output.  */
559 /* Note that input_file.c hand checks for '#' at the beginning of the
560    first line of the input file.  This is because the compiler outputs
561    #NO_APP at the beginning of its output.  */
562 /* Also note that comments like this one will always work.  */
563 const char line_comment_chars[] = "#";
564
565 const char line_separator_chars[] = ";";
566
567 /* Chars that can be used to separate mant
568    from exp in floating point numbers.  */
569 const char EXP_CHARS[] = "eE";
570
571 /* Chars that mean this number is a floating point constant.  */
572 /* As in 0f12.456  */
573 /* or    0d1.2345e12  */
574
575 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
576
577 /* Prefix characters that indicate the start of an immediate
578    value.  */
579 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
580
581 /* Separator character handling.  */
582
583 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
584
585 static inline int
586 skip_past_char (char ** str, char c)
587 {
588   if (**str == c)
589     {
590       (*str)++;
591       return SUCCESS;
592     }
593   else
594     return FAIL;
595 }
596 #define skip_past_comma(str) skip_past_char (str, ',')
597
598 /* Arithmetic expressions (possibly involving symbols).  */
599
600 /* Return TRUE if anything in the expression is a bignum.  */
601
602 static int
603 walk_no_bignums (symbolS * sp)
604 {
605   if (symbol_get_value_expression (sp)->X_op == O_big)
606     return 1;
607
608   if (symbol_get_value_expression (sp)->X_add_symbol)
609     {
610       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
611               || (symbol_get_value_expression (sp)->X_op_symbol
612                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
613     }
614
615   return 0;
616 }
617
618 static int in_my_get_expression = 0;
619
620 /* Third argument to my_get_expression.  */
621 #define GE_NO_PREFIX 0
622 #define GE_IMM_PREFIX 1
623 #define GE_OPT_PREFIX 2
624
625 static int
626 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
627 {
628   char * save_in;
629   segT   seg;
630
631   /* In unified syntax, all prefixes are optional.  */
632   if (unified_syntax)
633     prefix_mode = GE_OPT_PREFIX;
634
635   switch (prefix_mode)
636     {
637     case GE_NO_PREFIX: break;
638     case GE_IMM_PREFIX:
639       if (!is_immediate_prefix (**str))
640         {
641           inst.error = _("immediate expression requires a # prefix");
642           return FAIL;
643         }
644       (*str)++;
645       break;
646     case GE_OPT_PREFIX:
647       if (is_immediate_prefix (**str))
648         (*str)++;
649       break;
650     default: abort ();
651     }
652
653   memset (ep, 0, sizeof (expressionS));
654
655   save_in = input_line_pointer;
656   input_line_pointer = *str;
657   in_my_get_expression = 1;
658   seg = expression (ep);
659   in_my_get_expression = 0;
660
661   if (ep->X_op == O_illegal)
662     {
663       /* We found a bad expression in md_operand().  */
664       *str = input_line_pointer;
665       input_line_pointer = save_in;
666       if (inst.error == NULL)
667         inst.error = _("bad expression");
668       return 1;
669     }
670
671 #ifdef OBJ_AOUT
672   if (seg != absolute_section
673       && seg != text_section
674       && seg != data_section
675       && seg != bss_section
676       && seg != undefined_section)
677     {
678       inst.error = _("bad segment");
679       *str = input_line_pointer;
680       input_line_pointer = save_in;
681       return 1;
682     }
683 #endif
684
685   /* Get rid of any bignums now, so that we don't generate an error for which
686      we can't establish a line number later on.  Big numbers are never valid
687      in instructions, which is where this routine is always called.  */
688   if (ep->X_op == O_big
689       || (ep->X_add_symbol
690           && (walk_no_bignums (ep->X_add_symbol)
691               || (ep->X_op_symbol
692                   && walk_no_bignums (ep->X_op_symbol)))))
693     {
694       inst.error = _("invalid constant");
695       *str = input_line_pointer;
696       input_line_pointer = save_in;
697       return 1;
698     }
699
700   *str = input_line_pointer;
701   input_line_pointer = save_in;
702   return 0;
703 }
704
705 /* Turn a string in input_line_pointer into a floating point constant
706    of type TYPE, and store the appropriate bytes in *LITP.  The number
707    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
708    returned, or NULL on OK.
709
710    Note that fp constants aren't represent in the normal way on the ARM.
711    In big endian mode, things are as expected.  However, in little endian
712    mode fp constants are big-endian word-wise, and little-endian byte-wise
713    within the words.  For example, (double) 1.1 in big endian mode is
714    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
715    the byte sequence 99 99 f1 3f 9a 99 99 99.
716
717    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
718
719 char *
720 md_atof (int type, char * litP, int * sizeP)
721 {
722   int prec;
723   LITTLENUM_TYPE words[MAX_LITTLENUMS];
724   char *t;
725   int i;
726
727   switch (type)
728     {
729     case 'f':
730     case 'F':
731     case 's':
732     case 'S':
733       prec = 2;
734       break;
735
736     case 'd':
737     case 'D':
738     case 'r':
739     case 'R':
740       prec = 4;
741       break;
742
743     case 'x':
744     case 'X':
745       prec = 6;
746       break;
747
748     case 'p':
749     case 'P':
750       prec = 6;
751       break;
752
753     default:
754       *sizeP = 0;
755       return _("bad call to MD_ATOF()");
756     }
757
758   t = atof_ieee (input_line_pointer, type, words);
759   if (t)
760     input_line_pointer = t;
761   *sizeP = prec * 2;
762
763   if (target_big_endian)
764     {
765       for (i = 0; i < prec; i++)
766         {
767           md_number_to_chars (litP, (valueT) words[i], 2);
768           litP += 2;
769         }
770     }
771   else
772     {
773       if (cpu_variant & FPU_ARCH_VFP)
774         for (i = prec - 1; i >= 0; i--)
775           {
776             md_number_to_chars (litP, (valueT) words[i], 2);
777             litP += 2;
778           }
779       else
780         /* For a 4 byte float the order of elements in `words' is 1 0.
781            For an 8 byte float the order is 1 0 3 2.  */
782         for (i = 0; i < prec; i += 2)
783           {
784             md_number_to_chars (litP, (valueT) words[i + 1], 2);
785             md_number_to_chars (litP + 2, (valueT) words[i], 2);
786             litP += 4;
787           }
788     }
789
790   return 0;
791 }
792
793 /* We handle all bad expressions here, so that we can report the faulty
794    instruction in the error message.  */
795 void
796 md_operand (expressionS * expr)
797 {
798   if (in_my_get_expression)
799     expr->X_op = O_illegal;
800 }
801
802 /* Immediate values.  */
803
804 /* Generic immediate-value read function for use in directives.
805    Accepts anything that 'expression' can fold to a constant.
806    *val receives the number.  */
807 #ifdef OBJ_ELF
808 static int
809 immediate_for_directive (int *val)
810 {
811   expressionS exp;
812   exp.X_op = O_illegal;
813
814   if (is_immediate_prefix (*input_line_pointer))
815     {
816       input_line_pointer++;
817       expression (&exp);
818     }
819
820   if (exp.X_op != O_constant)
821     {
822       as_bad (_("expected #constant"));
823       ignore_rest_of_line ();
824       return FAIL;
825     }
826   *val = exp.X_add_number;
827   return SUCCESS;
828 }
829 #endif
830
831 /* Register parsing.  */
832
833 /* Generic register parser.  CCP points to what should be the
834    beginning of a register name.  If it is indeed a valid register
835    name, advance CCP over it and return the reg_entry structure;
836    otherwise return NULL.  Does not issue diagnostics.  */
837
838 static struct reg_entry *
839 arm_reg_parse_multi (char **ccp)
840 {
841   char *start = *ccp;
842   char *p;
843   struct reg_entry *reg;
844
845 #ifdef REGISTER_PREFIX
846   if (*start != REGISTER_PREFIX)
847     return NULL;
848   start++;
849 #endif
850 #ifdef OPTIONAL_REGISTER_PREFIX
851   if (*start == OPTIONAL_REGISTER_PREFIX)
852     start++;
853 #endif
854
855   p = start;
856   if (!ISALPHA (*p) || !is_name_beginner (*p))
857     return NULL;
858
859   do
860     p++;
861   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
862
863   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
864
865   if (!reg)
866     return NULL;
867
868   *ccp = p;
869   return reg;
870 }
871
872 /* As above, but the register must be of type TYPE, and the return
873    value is the register number or FAIL.  */
874
875 static int
876 arm_reg_parse (char **ccp, enum arm_reg_type type)
877 {
878   char *start = *ccp;
879   struct reg_entry *reg = arm_reg_parse_multi (ccp);
880
881   if (reg && reg->type == type)
882     return reg->number;
883
884   /* Alternative syntaxes are accepted for a few register classes.  */
885   switch (type)
886     {
887     case REG_TYPE_MVF:
888     case REG_TYPE_MVD:
889     case REG_TYPE_MVFX:
890     case REG_TYPE_MVDX:
891       /* Generic coprocessor register names are allowed for these.  */
892       if (reg->type == REG_TYPE_CN)
893         return reg->number;
894       break;
895
896     case REG_TYPE_CP:
897       /* For backward compatibility, a bare number is valid here.  */
898       {
899         unsigned long processor = strtoul (start, ccp, 10);
900         if (*ccp != start && processor <= 15)
901           return processor;
902       }
903
904     case REG_TYPE_MMXWC:
905       /* WC includes WCG.  ??? I'm not sure this is true for all
906          instructions that take WC registers.  */
907       if (reg->type == REG_TYPE_MMXWCG)
908         return reg->number;
909       break;
910
911     default:
912       break;
913     }
914
915   *ccp = start;
916   return FAIL;
917 }
918
919 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
920 static long
921 parse_reg_list (char ** strp)
922 {
923   char * str = * strp;
924   long   range = 0;
925   int    another_range;
926
927   /* We come back here if we get ranges concatenated by '+' or '|'.  */
928   do
929     {
930       another_range = 0;
931
932       if (*str == '{')
933         {
934           int in_range = 0;
935           int cur_reg = -1;
936
937           str++;
938           do
939             {
940               int reg;
941
942               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
943                 {
944                   inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
945                   return FAIL;
946                 }
947
948               if (in_range)
949                 {
950                   int i;
951
952                   if (reg <= cur_reg)
953                     {
954                       inst.error = _("bad range in register list");
955                       return FAIL;
956                     }
957
958                   for (i = cur_reg + 1; i < reg; i++)
959                     {
960                       if (range & (1 << i))
961                         as_tsktsk
962                           (_("Warning: duplicated register (r%d) in register list"),
963                            i);
964                       else
965                         range |= 1 << i;
966                     }
967                   in_range = 0;
968                 }
969
970               if (range & (1 << reg))
971                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
972                            reg);
973               else if (reg <= cur_reg)
974                 as_tsktsk (_("Warning: register range not in ascending order"));
975
976               range |= 1 << reg;
977               cur_reg = reg;
978             }
979           while (skip_past_comma (&str) != FAIL
980                  || (in_range = 1, *str++ == '-'));
981           str--;
982
983           if (*str++ != '}')
984             {
985               inst.error = _("missing `}'");
986               return FAIL;
987             }
988         }
989       else
990         {
991           expressionS expr;
992
993           if (my_get_expression (&expr, &str, GE_NO_PREFIX))
994             return FAIL;
995
996           if (expr.X_op == O_constant)
997             {
998               if (expr.X_add_number
999                   != (expr.X_add_number & 0x0000ffff))
1000                 {
1001                   inst.error = _("invalid register mask");
1002                   return FAIL;
1003                 }
1004
1005               if ((range & expr.X_add_number) != 0)
1006                 {
1007                   int regno = range & expr.X_add_number;
1008
1009                   regno &= -regno;
1010                   regno = (1 << regno) - 1;
1011                   as_tsktsk
1012                     (_("Warning: duplicated register (r%d) in register list"),
1013                      regno);
1014                 }
1015
1016               range |= expr.X_add_number;
1017             }
1018           else
1019             {
1020               if (inst.reloc.type != 0)
1021                 {
1022                   inst.error = _("expression too complex");
1023                   return FAIL;
1024                 }
1025
1026               memcpy (&inst.reloc.exp, &expr, sizeof (expressionS));
1027               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1028               inst.reloc.pc_rel = 0;
1029             }
1030         }
1031
1032       if (*str == '|' || *str == '+')
1033         {
1034           str++;
1035           another_range = 1;
1036         }
1037     }
1038   while (another_range);
1039
1040   *strp = str;
1041   return range;
1042 }
1043
1044 /* Parse a VFP register list.  If the string is invalid return FAIL.
1045    Otherwise return the number of registers, and set PBASE to the first
1046    register.  Double precision registers are matched if DP is nonzero.  */
1047
1048 static int
1049 parse_vfp_reg_list (char **str, unsigned int *pbase, int dp)
1050 {
1051   int base_reg;
1052   int new_base;
1053   int regtype;
1054   int max_regs;
1055   int count = 0;
1056   int warned = 0;
1057   unsigned long mask = 0;
1058   int i;
1059
1060   if (**str != '{')
1061     return FAIL;
1062
1063   (*str)++;
1064
1065   if (dp)
1066     {
1067       regtype = REG_TYPE_VFD;
1068       max_regs = 16;
1069     }
1070   else
1071     {
1072       regtype = REG_TYPE_VFS;
1073       max_regs = 32;
1074     }
1075
1076   base_reg = max_regs;
1077
1078   do
1079     {
1080       new_base = arm_reg_parse (str, regtype);
1081       if (new_base == FAIL)
1082         {
1083           inst.error = gettext (reg_expected_msgs[regtype]);
1084           return FAIL;
1085         }
1086
1087       if (new_base < base_reg)
1088         base_reg = new_base;
1089
1090       if (mask & (1 << new_base))
1091         {
1092           inst.error = _("invalid register list");
1093           return FAIL;
1094         }
1095
1096       if ((mask >> new_base) != 0 && ! warned)
1097         {
1098           as_tsktsk (_("register list not in ascending order"));
1099           warned = 1;
1100         }
1101
1102       mask |= 1 << new_base;
1103       count++;
1104
1105       if (**str == '-') /* We have the start of a range expression */
1106         {
1107           int high_range;
1108
1109           (*str)++;
1110
1111           if ((high_range = arm_reg_parse (str, regtype)) == FAIL)
1112             {
1113               inst.error = gettext (reg_expected_msgs[regtype]);
1114               return FAIL;
1115             }
1116
1117           if (high_range <= new_base)
1118             {
1119               inst.error = _("register range not in ascending order");
1120               return FAIL;
1121             }
1122
1123           for (new_base++; new_base <= high_range; new_base++)
1124             {
1125               if (mask & (1 << new_base))
1126                 {
1127                   inst.error = _("invalid register list");
1128                   return FAIL;
1129                 }
1130
1131               mask |= 1 << new_base;
1132               count++;
1133             }
1134         }
1135     }
1136   while (skip_past_comma (str) != FAIL);
1137
1138   (*str)++;
1139
1140   /* Sanity check -- should have raised a parse error above.  */
1141   if (count == 0 || count > max_regs)
1142     abort ();
1143
1144   *pbase = base_reg;
1145
1146   /* Final test -- the registers must be consecutive.  */
1147   mask >>= base_reg;
1148   for (i = 0; i < count; i++)
1149     {
1150       if ((mask & (1u << i)) == 0)
1151         {
1152           inst.error = _("non-contiguous register range");
1153           return FAIL;
1154         }
1155     }
1156
1157   return count;
1158 }
1159
1160 /* Parse an explicit relocation suffix on an expression.  This is
1161    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
1162    arm_reloc_hsh contains no entries, so this function can only
1163    succeed if there is no () after the word.  Returns -1 on error,
1164    BFD_RELOC_UNUSED if there wasn't any suffix.  */
1165 static int
1166 parse_reloc (char **str)
1167 {
1168   struct reloc_entry *r;
1169   char *p, *q;
1170
1171   if (**str != '(')
1172     return BFD_RELOC_UNUSED;
1173
1174   p = *str + 1;
1175   q = p;
1176
1177   while (*q && *q != ')' && *q != ',')
1178     q++;
1179   if (*q != ')')
1180     return -1;
1181
1182   if ((r = hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
1183     return -1;
1184
1185   *str = q + 1;
1186   return r->reloc;
1187 }
1188
1189 /* Directives: register aliases.  */
1190
1191 static void
1192 insert_reg_alias (char *str, int number, int type)
1193 {
1194   struct reg_entry *new;
1195   const char *name;
1196
1197   if ((new = hash_find (arm_reg_hsh, str)) != 0)
1198     {
1199       if (new->builtin)
1200         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
1201
1202       /* Only warn about a redefinition if it's not defined as the
1203          same register.  */
1204       else if (new->number != number || new->type != type)
1205         as_warn (_("ignoring redefinition of register alias '%s'"), str);
1206
1207       return;
1208     }
1209
1210   name = xstrdup (str);
1211   new = xmalloc (sizeof (struct reg_entry));
1212
1213   new->name = name;
1214   new->number = number;
1215   new->type = type;
1216   new->builtin = FALSE;
1217
1218   if (hash_insert (arm_reg_hsh, name, (PTR) new))
1219     abort ();
1220 }
1221
1222 /* Look for the .req directive.  This is of the form:
1223
1224         new_register_name .req existing_register_name
1225
1226    If we find one, or if it looks sufficiently like one that we want to
1227    handle any error here, return non-zero.  Otherwise return zero.  */
1228
1229 static int
1230 create_register_alias (char * newname, char *p)
1231 {
1232   struct reg_entry *old;
1233   char *oldname, *nbuf;
1234   size_t nlen;
1235
1236   /* The input scrubber ensures that whitespace after the mnemonic is
1237      collapsed to single spaces.  */
1238   oldname = p;
1239   if (strncmp (oldname, " .req ", 6) != 0)
1240     return 0;
1241
1242   oldname += 6;
1243   if (*oldname == '\0')
1244     return 0;
1245
1246   old = hash_find (arm_reg_hsh, oldname);
1247   if (!old)
1248     {
1249       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
1250       return 1;
1251     }
1252
1253   /* If TC_CASE_SENSITIVE is defined, then newname already points to
1254      the desired alias name, and p points to its end.  If not, then
1255      the desired alias name is in the global original_case_string.  */
1256 #ifdef TC_CASE_SENSITIVE
1257   nlen = p - newname;
1258 #else
1259   newname = original_case_string;
1260   nlen = strlen (newname);
1261 #endif
1262
1263   nbuf = alloca (nlen + 1);
1264   memcpy (nbuf, newname, nlen);
1265   nbuf[nlen] = '\0';
1266
1267   /* Create aliases under the new name as stated; an all-lowercase
1268      version of the new name; and an all-uppercase version of the new
1269      name.  */
1270   insert_reg_alias (nbuf, old->number, old->type);
1271
1272   for (p = nbuf; *p; p++)
1273     *p = TOUPPER (*p);
1274
1275   if (strncmp (nbuf, newname, nlen))
1276     insert_reg_alias (nbuf, old->number, old->type);
1277
1278   for (p = nbuf; *p; p++)
1279     *p = TOLOWER (*p);
1280
1281   if (strncmp (nbuf, newname, nlen))
1282     insert_reg_alias (nbuf, old->number, old->type);
1283
1284   return 1;
1285 }
1286
1287 /* Should never be called, as .req goes between the alias and the
1288    register name, not at the beginning of the line.  */
1289 static void
1290 s_req (int a ATTRIBUTE_UNUSED)
1291 {
1292   as_bad (_("invalid syntax for .req directive"));
1293 }
1294
1295 /* The .unreq directive deletes an alias which was previously defined
1296    by .req.  For example:
1297
1298        my_alias .req r11
1299        .unreq my_alias    */
1300
1301 static void
1302 s_unreq (int a ATTRIBUTE_UNUSED)
1303 {
1304   char * name;
1305   char saved_char;
1306
1307   name = input_line_pointer;
1308
1309   while (*input_line_pointer != 0
1310          && *input_line_pointer != ' '
1311          && *input_line_pointer != '\n')
1312     ++input_line_pointer;
1313
1314   saved_char = *input_line_pointer;
1315   *input_line_pointer = 0;
1316
1317   if (!*name)
1318     as_bad (_("invalid syntax for .unreq directive"));
1319   else
1320     {
1321       struct reg_entry *reg = hash_find (arm_reg_hsh, name);
1322
1323       if (!reg)
1324         as_bad (_("unknown register alias '%s'"), name);
1325       else if (reg->builtin)
1326         as_warn (_("ignoring attempt to undefine built-in register '%s'"),
1327                  name);
1328       else
1329         {
1330           hash_delete (arm_reg_hsh, name);
1331           free ((char *) reg->name);
1332           free (reg);
1333         }
1334     }
1335
1336   *input_line_pointer = saved_char;
1337   demand_empty_rest_of_line ();
1338 }
1339
1340 /* Directives: Instruction set selection.  */
1341
1342 #ifdef OBJ_ELF
1343 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
1344    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
1345    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
1346    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
1347
1348 static enum mstate mapstate = MAP_UNDEFINED;
1349
1350 static void
1351 mapping_state (enum mstate state)
1352 {
1353   symbolS * symbolP;
1354   const char * symname;
1355   int type;
1356
1357   if (mapstate == state)
1358     /* The mapping symbol has already been emitted.
1359        There is nothing else to do.  */
1360     return;
1361
1362   mapstate = state;
1363
1364   switch (state)
1365     {
1366     case MAP_DATA:
1367       symname = "$d";
1368       type = BSF_NO_FLAGS;
1369       break;
1370     case MAP_ARM:
1371       symname = "$a";
1372       type = BSF_NO_FLAGS;
1373       break;
1374     case MAP_THUMB:
1375       symname = "$t";
1376       type = BSF_NO_FLAGS;
1377       break;
1378     case MAP_UNDEFINED:
1379       return;
1380     default:
1381       abort ();
1382     }
1383
1384   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
1385
1386   symbolP = symbol_new (symname, now_seg, (valueT) frag_now_fix (), frag_now);
1387   symbol_table_insert (symbolP);
1388   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
1389
1390   switch (state)
1391     {
1392     case MAP_ARM:
1393       THUMB_SET_FUNC (symbolP, 0);
1394       ARM_SET_THUMB (symbolP, 0);
1395       ARM_SET_INTERWORK (symbolP, support_interwork);
1396       break;
1397
1398     case MAP_THUMB:
1399       THUMB_SET_FUNC (symbolP, 1);
1400       ARM_SET_THUMB (symbolP, 1);
1401       ARM_SET_INTERWORK (symbolP, support_interwork);
1402       break;
1403
1404     case MAP_DATA:
1405     default:
1406       return;
1407     }
1408 }
1409 #else
1410 #define mapping_state(x) /* nothing */
1411 #endif
1412
1413 /* Find the real, Thumb encoded start of a Thumb function.  */
1414
1415 static symbolS *
1416 find_real_start (symbolS * symbolP)
1417 {
1418   char *       real_start;
1419   const char * name = S_GET_NAME (symbolP);
1420   symbolS *    new_target;
1421
1422   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
1423 #define STUB_NAME ".real_start_of"
1424
1425   if (name == NULL)
1426     abort ();
1427
1428   /* The compiler may generate BL instructions to local labels because
1429      it needs to perform a branch to a far away location. These labels
1430      do not have a corresponding ".real_start_of" label.  We check
1431      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
1432      the ".real_start_of" convention for nonlocal branches.  */
1433   if (S_IS_LOCAL (symbolP) || name[0] == '.')
1434     return symbolP;
1435
1436   real_start = ACONCAT ((STUB_NAME, name, NULL));
1437   new_target = symbol_find (real_start);
1438
1439   if (new_target == NULL)
1440     {
1441       as_warn ("Failed to find real start of function: %s\n", name);
1442       new_target = symbolP;
1443     }
1444
1445   return new_target;
1446 }
1447
1448 static void
1449 opcode_select (int width)
1450 {
1451   switch (width)
1452     {
1453     case 16:
1454       if (! thumb_mode)
1455         {
1456           if (! (cpu_variant & ARM_EXT_V4T))
1457             as_bad (_("selected processor does not support THUMB opcodes"));
1458
1459           thumb_mode = 1;
1460           /* No need to force the alignment, since we will have been
1461              coming from ARM mode, which is word-aligned.  */
1462           record_alignment (now_seg, 1);
1463         }
1464       mapping_state (MAP_THUMB);
1465       break;
1466
1467     case 32:
1468       if (thumb_mode)
1469         {
1470           if ((cpu_variant & ARM_ALL) == ARM_EXT_V4T)
1471             as_bad (_("selected processor does not support ARM opcodes"));
1472
1473           thumb_mode = 0;
1474
1475           if (!need_pass_2)
1476             frag_align (2, 0, 0);
1477
1478           record_alignment (now_seg, 1);
1479         }
1480       mapping_state (MAP_ARM);
1481       break;
1482
1483     default:
1484       as_bad (_("invalid instruction size selected (%d)"), width);
1485     }
1486 }
1487
1488 static void
1489 s_arm (int ignore ATTRIBUTE_UNUSED)
1490 {
1491   opcode_select (32);
1492   demand_empty_rest_of_line ();
1493 }
1494
1495 static void
1496 s_thumb (int ignore ATTRIBUTE_UNUSED)
1497 {
1498   opcode_select (16);
1499   demand_empty_rest_of_line ();
1500 }
1501
1502 static void
1503 s_code (int unused ATTRIBUTE_UNUSED)
1504 {
1505   int temp;
1506
1507   temp = get_absolute_expression ();
1508   switch (temp)
1509     {
1510     case 16:
1511     case 32:
1512       opcode_select (temp);
1513       break;
1514
1515     default:
1516       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
1517     }
1518 }
1519
1520 static void
1521 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
1522 {
1523   /* If we are not already in thumb mode go into it, EVEN if
1524      the target processor does not support thumb instructions.
1525      This is used by gcc/config/arm/lib1funcs.asm for example
1526      to compile interworking support functions even if the
1527      target processor should not support interworking.  */
1528   if (! thumb_mode)
1529     {
1530       thumb_mode = 2;
1531       record_alignment (now_seg, 1);
1532     }
1533
1534   demand_empty_rest_of_line ();
1535 }
1536
1537 static void
1538 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
1539 {
1540   s_thumb (0);
1541
1542   /* The following label is the name/address of the start of a Thumb function.
1543      We need to know this for the interworking support.  */
1544   label_is_thumb_function_name = TRUE;
1545 }
1546
1547 /* Perform a .set directive, but also mark the alias as
1548    being a thumb function.  */
1549
1550 static void
1551 s_thumb_set (int equiv)
1552 {
1553   /* XXX the following is a duplicate of the code for s_set() in read.c
1554      We cannot just call that code as we need to get at the symbol that
1555      is created.  */
1556   char *    name;
1557   char      delim;
1558   char *    end_name;
1559   symbolS * symbolP;
1560
1561   /* Especial apologies for the random logic:
1562      This just grew, and could be parsed much more simply!
1563      Dean - in haste.  */
1564   name      = input_line_pointer;
1565   delim     = get_symbol_end ();
1566   end_name  = input_line_pointer;
1567   *end_name = delim;
1568
1569   if (*input_line_pointer != ',')
1570     {
1571       *end_name = 0;
1572       as_bad (_("expected comma after name \"%s\""), name);
1573       *end_name = delim;
1574       ignore_rest_of_line ();
1575       return;
1576     }
1577
1578   input_line_pointer++;
1579   *end_name = 0;
1580
1581   if (name[0] == '.' && name[1] == '\0')
1582     {
1583       /* XXX - this should not happen to .thumb_set.  */
1584       abort ();
1585     }
1586
1587   if ((symbolP = symbol_find (name)) == NULL
1588       && (symbolP = md_undefined_symbol (name)) == NULL)
1589     {
1590 #ifndef NO_LISTING
1591       /* When doing symbol listings, play games with dummy fragments living
1592          outside the normal fragment chain to record the file and line info
1593          for this symbol.  */
1594       if (listing & LISTING_SYMBOLS)
1595         {
1596           extern struct list_info_struct * listing_tail;
1597           fragS * dummy_frag = xmalloc (sizeof (fragS));
1598
1599           memset (dummy_frag, 0, sizeof (fragS));
1600           dummy_frag->fr_type = rs_fill;
1601           dummy_frag->line = listing_tail;
1602           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
1603           dummy_frag->fr_symbol = symbolP;
1604         }
1605       else
1606 #endif
1607         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
1608
1609 #ifdef OBJ_COFF
1610       /* "set" symbols are local unless otherwise specified.  */
1611       SF_SET_LOCAL (symbolP);
1612 #endif /* OBJ_COFF  */
1613     }                           /* Make a new symbol.  */
1614
1615   symbol_table_insert (symbolP);
1616
1617   * end_name = delim;
1618
1619   if (equiv
1620       && S_IS_DEFINED (symbolP)
1621       && S_GET_SEGMENT (symbolP) != reg_section)
1622     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
1623
1624   pseudo_set (symbolP);
1625
1626   demand_empty_rest_of_line ();
1627
1628   /* XXX Now we come to the Thumb specific bit of code.  */
1629
1630   THUMB_SET_FUNC (symbolP, 1);
1631   ARM_SET_THUMB (symbolP, 1);
1632 #if defined OBJ_ELF || defined OBJ_COFF
1633   ARM_SET_INTERWORK (symbolP, support_interwork);
1634 #endif
1635 }
1636
1637 /* Directives: Mode selection.  */
1638
1639 /* .syntax [unified|divided] - choose the new unified syntax
1640    (same for Arm and Thumb encoding, modulo slight differences in what
1641    can be represented) or the old divergent syntax for each mode.  */
1642 static void
1643 s_syntax (int unused ATTRIBUTE_UNUSED)
1644 {
1645   char *name, delim;
1646
1647   name = input_line_pointer;
1648   delim = get_symbol_end ();
1649
1650   if (!strcasecmp (name, "unified"))
1651     unified_syntax = TRUE;
1652   else if (!strcasecmp (name, "divided"))
1653     unified_syntax = FALSE;
1654   else
1655     {
1656       as_bad (_("unrecognized syntax mode \"%s\""), name);
1657       return;
1658     }
1659   *input_line_pointer = delim;
1660   demand_empty_rest_of_line ();
1661 }
1662
1663 /* Directives: sectioning and alignment.  */
1664
1665 /* Same as s_align_ptwo but align 0 => align 2.  */
1666
1667 static void
1668 s_align (int unused ATTRIBUTE_UNUSED)
1669 {
1670   int temp;
1671   long temp_fill;
1672   long max_alignment = 15;
1673
1674   temp = get_absolute_expression ();
1675   if (temp > max_alignment)
1676     as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
1677   else if (temp < 0)
1678     {
1679       as_bad (_("alignment negative. 0 assumed."));
1680       temp = 0;
1681     }
1682
1683   if (*input_line_pointer == ',')
1684     {
1685       input_line_pointer++;
1686       temp_fill = get_absolute_expression ();
1687     }
1688   else
1689     temp_fill = 0;
1690
1691   if (!temp)
1692     temp = 2;
1693
1694   /* Only make a frag if we HAVE to.  */
1695   if (temp && !need_pass_2)
1696     frag_align (temp, (int) temp_fill, 0);
1697   demand_empty_rest_of_line ();
1698
1699   record_alignment (now_seg, temp);
1700 }
1701
1702 static void
1703 s_bss (int ignore ATTRIBUTE_UNUSED)
1704 {
1705   /* We don't support putting frags in the BSS segment, we fake it by
1706      marking in_bss, then looking at s_skip for clues.  */
1707   subseg_set (bss_section, 0);
1708   demand_empty_rest_of_line ();
1709   mapping_state (MAP_DATA);
1710 }
1711
1712 static void
1713 s_even (int ignore ATTRIBUTE_UNUSED)
1714 {
1715   /* Never make frag if expect extra pass.  */
1716   if (!need_pass_2)
1717     frag_align (1, 0, 0);
1718
1719   record_alignment (now_seg, 1);
1720
1721   demand_empty_rest_of_line ();
1722 }
1723
1724 /* Directives: Literal pools.  */
1725
1726 static literal_pool *
1727 find_literal_pool (void)
1728 {
1729   literal_pool * pool;
1730
1731   for (pool = list_of_pools; pool != NULL; pool = pool->next)
1732     {
1733       if (pool->section == now_seg
1734           && pool->sub_section == now_subseg)
1735         break;
1736     }
1737
1738   return pool;
1739 }
1740
1741 static literal_pool *
1742 find_or_make_literal_pool (void)
1743 {
1744   /* Next literal pool ID number.  */
1745   static unsigned int latest_pool_num = 1;
1746   literal_pool *      pool;
1747
1748   pool = find_literal_pool ();
1749
1750   if (pool == NULL)
1751     {
1752       /* Create a new pool.  */
1753       pool = xmalloc (sizeof (* pool));
1754       if (! pool)
1755         return NULL;
1756
1757       pool->next_free_entry = 0;
1758       pool->section         = now_seg;
1759       pool->sub_section     = now_subseg;
1760       pool->next            = list_of_pools;
1761       pool->symbol          = NULL;
1762
1763       /* Add it to the list.  */
1764       list_of_pools = pool;
1765     }
1766
1767   /* New pools, and emptied pools, will have a NULL symbol.  */
1768   if (pool->symbol == NULL)
1769     {
1770       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
1771                                     (valueT) 0, &zero_address_frag);
1772       pool->id = latest_pool_num ++;
1773     }
1774
1775   /* Done.  */
1776   return pool;
1777 }
1778
1779 /* Add the literal in the global 'inst'
1780    structure to the relevent literal pool.  */
1781
1782 static int
1783 add_to_lit_pool (void)
1784 {
1785   literal_pool * pool;
1786   unsigned int entry;
1787
1788   pool = find_or_make_literal_pool ();
1789
1790   /* Check if this literal value is already in the pool.  */
1791   for (entry = 0; entry < pool->next_free_entry; entry ++)
1792     {
1793       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
1794           && (inst.reloc.exp.X_op == O_constant)
1795           && (pool->literals[entry].X_add_number
1796               == inst.reloc.exp.X_add_number)
1797           && (pool->literals[entry].X_unsigned
1798               == inst.reloc.exp.X_unsigned))
1799         break;
1800
1801       if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
1802           && (inst.reloc.exp.X_op == O_symbol)
1803           && (pool->literals[entry].X_add_number
1804               == inst.reloc.exp.X_add_number)
1805           && (pool->literals[entry].X_add_symbol
1806               == inst.reloc.exp.X_add_symbol)
1807           && (pool->literals[entry].X_op_symbol
1808               == inst.reloc.exp.X_op_symbol))
1809         break;
1810     }
1811
1812   /* Do we need to create a new entry?  */
1813   if (entry == pool->next_free_entry)
1814     {
1815       if (entry >= MAX_LITERAL_POOL_SIZE)
1816         {
1817           inst.error = _("literal pool overflow");
1818           return FAIL;
1819         }
1820
1821       pool->literals[entry] = inst.reloc.exp;
1822       pool->next_free_entry += 1;
1823     }
1824
1825   inst.reloc.exp.X_op         = O_symbol;
1826   inst.reloc.exp.X_add_number = ((int) entry) * 4;
1827   inst.reloc.exp.X_add_symbol = pool->symbol;
1828
1829   return SUCCESS;
1830 }
1831
1832 /* Can't use symbol_new here, so have to create a symbol and then at
1833    a later date assign it a value. Thats what these functions do.  */
1834
1835 static void
1836 symbol_locate (symbolS *    symbolP,
1837                const char * name,       /* It is copied, the caller can modify.  */
1838                segT         segment,    /* Segment identifier (SEG_<something>).  */
1839                valueT       valu,       /* Symbol value.  */
1840                fragS *      frag)       /* Associated fragment.  */
1841 {
1842   unsigned int name_length;
1843   char * preserved_copy_of_name;
1844
1845   name_length = strlen (name) + 1;   /* +1 for \0.  */
1846   obstack_grow (&notes, name, name_length);
1847   preserved_copy_of_name = obstack_finish (&notes);
1848
1849 #ifdef tc_canonicalize_symbol_name
1850   preserved_copy_of_name =
1851     tc_canonicalize_symbol_name (preserved_copy_of_name);
1852 #endif
1853
1854   S_SET_NAME (symbolP, preserved_copy_of_name);
1855
1856   S_SET_SEGMENT (symbolP, segment);
1857   S_SET_VALUE (symbolP, valu);
1858   symbol_clear_list_pointers (symbolP);
1859
1860   symbol_set_frag (symbolP, frag);
1861
1862   /* Link to end of symbol chain.  */
1863   {
1864     extern int symbol_table_frozen;
1865
1866     if (symbol_table_frozen)
1867       abort ();
1868   }
1869
1870   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
1871
1872   obj_symbol_new_hook (symbolP);
1873
1874 #ifdef tc_symbol_new_hook
1875   tc_symbol_new_hook (symbolP);
1876 #endif
1877
1878 #ifdef DEBUG_SYMS
1879   verify_symbol_chain (symbol_rootP, symbol_lastP);
1880 #endif /* DEBUG_SYMS  */
1881 }
1882
1883
1884 static void
1885 s_ltorg (int ignored ATTRIBUTE_UNUSED)
1886 {
1887   unsigned int entry;
1888   literal_pool * pool;
1889   char sym_name[20];
1890
1891   pool = find_literal_pool ();
1892   if (pool == NULL
1893       || pool->symbol == NULL
1894       || pool->next_free_entry == 0)
1895     return;
1896
1897   mapping_state (MAP_DATA);
1898
1899   /* Align pool as you have word accesses.
1900      Only make a frag if we have to.  */
1901   if (!need_pass_2)
1902     frag_align (2, 0, 0);
1903
1904   record_alignment (now_seg, 2);
1905
1906   sprintf (sym_name, "$$lit_\002%x", pool->id);
1907
1908   symbol_locate (pool->symbol, sym_name, now_seg,
1909                  (valueT) frag_now_fix (), frag_now);
1910   symbol_table_insert (pool->symbol);
1911
1912   ARM_SET_THUMB (pool->symbol, thumb_mode);
1913
1914 #if defined OBJ_COFF || defined OBJ_ELF
1915   ARM_SET_INTERWORK (pool->symbol, support_interwork);
1916 #endif
1917
1918   for (entry = 0; entry < pool->next_free_entry; entry ++)
1919     /* First output the expression in the instruction to the pool.  */
1920     emit_expr (&(pool->literals[entry]), 4); /* .word  */
1921
1922   /* Mark the pool as empty.  */
1923   pool->next_free_entry = 0;
1924   pool->symbol = NULL;
1925 }
1926
1927 #ifdef OBJ_ELF
1928 /* Forward declarations for functions below, in the MD interface
1929    section.  */
1930 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
1931 static valueT create_unwind_entry (int);
1932 static void start_unwind_section (const segT, int);
1933 static void add_unwind_opcode (valueT, int);
1934 static void flush_pending_unwind (void);
1935
1936 /* Directives: Data.  */
1937
1938 static void
1939 s_arm_elf_cons (int nbytes)
1940 {
1941   expressionS exp;
1942
1943 #ifdef md_flush_pending_output
1944   md_flush_pending_output ();
1945 #endif
1946
1947   if (is_it_end_of_statement ())
1948     {
1949       demand_empty_rest_of_line ();
1950       return;
1951     }
1952
1953 #ifdef md_cons_align
1954   md_cons_align (nbytes);
1955 #endif
1956
1957   mapping_state (MAP_DATA);
1958   do
1959     {
1960       int reloc;
1961       char *base = input_line_pointer;
1962
1963       expression (& exp);
1964
1965       if (exp.X_op != O_symbol)
1966         emit_expr (&exp, (unsigned int) nbytes);
1967       else
1968         {
1969           char *before_reloc = input_line_pointer;
1970           reloc = parse_reloc (&input_line_pointer);
1971           if (reloc == -1)
1972             {
1973               as_bad (_("unrecognized relocation suffix"));
1974               ignore_rest_of_line ();
1975               return;
1976             }
1977           else if (reloc == BFD_RELOC_UNUSED)
1978             emit_expr (&exp, (unsigned int) nbytes);
1979           else
1980             {
1981               reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, reloc);
1982               int size = bfd_get_reloc_size (howto);
1983
1984               if (reloc == BFD_RELOC_ARM_PLT32)
1985                 {
1986                   as_bad (_("(plt) is only valid on branch targets"));
1987                   reloc = BFD_RELOC_UNUSED;
1988                   size = 0;
1989                 }
1990
1991               if (size > nbytes)
1992                 as_bad (_("%s relocations do not fit in %d bytes"),
1993                         howto->name, nbytes);
1994               else
1995                 {
1996                   /* We've parsed an expression stopping at O_symbol.
1997                      But there may be more expression left now that we
1998                      have parsed the relocation marker.  Parse it again.
1999                      XXX Surely there is a cleaner way to do this.  */
2000                   char *p = input_line_pointer;
2001                   int offset;
2002                   char *save_buf = alloca (input_line_pointer - base);
2003                   memcpy (save_buf, base, input_line_pointer - base);
2004                   memmove (base + (input_line_pointer - before_reloc),
2005                            base, before_reloc - base);
2006
2007                   input_line_pointer = base + (input_line_pointer-before_reloc);
2008                   expression (&exp);
2009                   memcpy (base, save_buf, p - base);
2010
2011                   offset = nbytes - size;
2012                   p = frag_more ((int) nbytes);
2013                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
2014                                size, &exp, 0, reloc);
2015                 }
2016             }
2017         }
2018     }
2019   while (*input_line_pointer++ == ',');
2020
2021   /* Put terminator back into stream.  */
2022   input_line_pointer --;
2023   demand_empty_rest_of_line ();
2024 }
2025
2026
2027 /* Parse a .rel31 directive.  */
2028
2029 static void
2030 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
2031 {
2032   expressionS exp;
2033   char *p;
2034   valueT highbit;
2035
2036   highbit = 0;
2037   if (*input_line_pointer == '1')
2038     highbit = 0x80000000;
2039   else if (*input_line_pointer != '0')
2040     as_bad (_("expected 0 or 1"));
2041
2042   input_line_pointer++;
2043   if (*input_line_pointer != ',')
2044     as_bad (_("missing comma"));
2045   input_line_pointer++;
2046
2047 #ifdef md_flush_pending_output
2048   md_flush_pending_output ();
2049 #endif
2050
2051 #ifdef md_cons_align
2052   md_cons_align (4);
2053 #endif
2054
2055   mapping_state (MAP_DATA);
2056
2057   expression (&exp);
2058
2059   p = frag_more (4);
2060   md_number_to_chars (p, highbit, 4);
2061   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
2062                BFD_RELOC_ARM_PREL31);
2063
2064   demand_empty_rest_of_line ();
2065 }
2066
2067 /* Directives: AEABI stack-unwind tables.  */
2068
2069 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
2070
2071 static void
2072 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
2073 {
2074   demand_empty_rest_of_line ();
2075   /* Mark the start of the function.  */
2076   unwind.proc_start = expr_build_dot ();
2077
2078   /* Reset the rest of the unwind info.  */
2079   unwind.opcode_count = 0;
2080   unwind.table_entry = NULL;
2081   unwind.personality_routine = NULL;
2082   unwind.personality_index = -1;
2083   unwind.frame_size = 0;
2084   unwind.fp_offset = 0;
2085   unwind.fp_reg = 13;
2086   unwind.fp_used = 0;
2087   unwind.sp_restored = 0;
2088 }
2089
2090
2091 /* Parse a handlerdata directive.  Creates the exception handling table entry
2092    for the function.  */
2093
2094 static void
2095 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
2096 {
2097   demand_empty_rest_of_line ();
2098   if (unwind.table_entry)
2099     as_bad (_("dupicate .handlerdata directive"));
2100
2101   create_unwind_entry (1);
2102 }
2103
2104 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
2105
2106 static void
2107 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
2108 {
2109   long where;
2110   char *ptr;
2111   valueT val;
2112
2113   demand_empty_rest_of_line ();
2114
2115   /* Add eh table entry.  */
2116   if (unwind.table_entry == NULL)
2117     val = create_unwind_entry (0);
2118   else
2119     val = 0;
2120
2121   /* Add index table entry.  This is two words.  */
2122   start_unwind_section (unwind.saved_seg, 1);
2123   frag_align (2, 0, 0);
2124   record_alignment (now_seg, 2);
2125
2126   ptr = frag_more (8);
2127   where = frag_now_fix () - 8;
2128
2129   /* Self relative offset of the function start.  */
2130   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
2131            BFD_RELOC_ARM_PREL31);
2132
2133   /* Indicate dependency on EHABI-defined personality routines to the
2134      linker, if it hasn't been done already.  */
2135   if (unwind.personality_index >= 0 && unwind.personality_index < 3
2136       && !(marked_pr_dependency & (1 << unwind.personality_index)))
2137     {
2138       static const char *const name[] = {
2139         "__aeabi_unwind_cpp_pr0",
2140         "__aeabi_unwind_cpp_pr1",
2141         "__aeabi_unwind_cpp_pr2"
2142       };
2143       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
2144       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
2145       marked_pr_dependency |= 1 << unwind.personality_index;
2146       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
2147         = marked_pr_dependency;
2148     }
2149
2150   if (val)
2151     /* Inline exception table entry.  */
2152     md_number_to_chars (ptr + 4, val, 4);
2153   else
2154     /* Self relative offset of the table entry.  */
2155     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
2156              BFD_RELOC_ARM_PREL31);
2157
2158   /* Restore the original section.  */
2159   subseg_set (unwind.saved_seg, unwind.saved_subseg);
2160 }
2161
2162
2163 /* Parse an unwind_cantunwind directive.  */
2164
2165 static void
2166 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
2167 {
2168   demand_empty_rest_of_line ();
2169   if (unwind.personality_routine || unwind.personality_index != -1)
2170     as_bad (_("personality routine specified for cantunwind frame"));
2171
2172   unwind.personality_index = -2;
2173 }
2174
2175
2176 /* Parse a personalityindex directive.  */
2177
2178 static void
2179 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
2180 {
2181   expressionS exp;
2182
2183   if (unwind.personality_routine || unwind.personality_index != -1)
2184     as_bad (_("duplicate .personalityindex directive"));
2185
2186   expression (&exp);
2187
2188   if (exp.X_op != O_constant
2189       || exp.X_add_number < 0 || exp.X_add_number > 15)
2190     {
2191       as_bad (_("bad personality routine number"));
2192       ignore_rest_of_line ();
2193       return;
2194     }
2195
2196   unwind.personality_index = exp.X_add_number;
2197
2198   demand_empty_rest_of_line ();
2199 }
2200
2201
2202 /* Parse a personality directive.  */
2203
2204 static void
2205 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
2206 {
2207   char *name, *p, c;
2208
2209   if (unwind.personality_routine || unwind.personality_index != -1)
2210     as_bad (_("duplicate .personality directive"));
2211
2212   name = input_line_pointer;
2213   c = get_symbol_end ();
2214   p = input_line_pointer;
2215   unwind.personality_routine = symbol_find_or_make (name);
2216   *p = c;
2217   demand_empty_rest_of_line ();
2218 }
2219
2220
2221 /* Parse a directive saving core registers.  */
2222
2223 static void
2224 s_arm_unwind_save_core (void)
2225 {
2226   valueT op;
2227   long range;
2228   int n;
2229
2230   range = parse_reg_list (&input_line_pointer);
2231   if (range == FAIL)
2232     {
2233       as_bad (_("expected register list"));
2234       ignore_rest_of_line ();
2235       return;
2236     }
2237
2238   demand_empty_rest_of_line ();
2239
2240   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
2241      into .unwind_save {..., sp...}.  We aren't bothered about the value of
2242      ip because it is clobbered by calls.  */
2243   if (unwind.sp_restored && unwind.fp_reg == 12
2244       && (range & 0x3000) == 0x1000)
2245     {
2246       unwind.opcode_count--;
2247       unwind.sp_restored = 0;
2248       range = (range | 0x2000) & ~0x1000;
2249       unwind.pending_offset = 0;
2250     }
2251
2252   /* Pop r4-r15.  */
2253   if (range & 0xfff0)
2254     {
2255       /* See if we can use the short opcodes.  These pop a block of up to 8
2256          registers starting with r4, plus maybe r14.  */
2257       for (n = 0; n < 8; n++)
2258         {
2259           /* Break at the first non-saved register.      */
2260           if ((range & (1 << (n + 4))) == 0)
2261             break;
2262         }
2263       /* See if there are any other bits set.  */
2264       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
2265         {
2266           /* Use the long form.  */
2267           op = 0x8000 | ((range >> 4) & 0xfff);
2268           add_unwind_opcode (op, 2);
2269         }
2270       else
2271         {
2272           /* Use the short form.  */
2273           if (range & 0x4000)
2274             op = 0xa8; /* Pop r14.      */
2275           else
2276             op = 0xa0; /* Do not pop r14.  */
2277           op |= (n - 1);
2278           add_unwind_opcode (op, 1);
2279         }
2280     }
2281
2282   /* Pop r0-r3.  */
2283   if (range & 0xf)
2284     {
2285       op = 0xb100 | (range & 0xf);
2286       add_unwind_opcode (op, 2);
2287     }
2288
2289   /* Record the number of bytes pushed.  */
2290   for (n = 0; n < 16; n++)
2291     {
2292       if (range & (1 << n))
2293         unwind.frame_size += 4;
2294     }
2295 }
2296
2297
2298 /* Parse a directive saving FPA registers.  */
2299
2300 static void
2301 s_arm_unwind_save_fpa (int reg)
2302 {
2303   expressionS exp;
2304   int num_regs;
2305   valueT op;
2306
2307   /* Get Number of registers to transfer.  */
2308   if (skip_past_comma (&input_line_pointer) != FAIL)
2309     expression (&exp);
2310   else
2311     exp.X_op = O_illegal;
2312
2313   if (exp.X_op != O_constant)
2314     {
2315       as_bad (_("expected , <constant>"));
2316       ignore_rest_of_line ();
2317       return;
2318     }
2319
2320   num_regs = exp.X_add_number;
2321
2322   if (num_regs < 1 || num_regs > 4)
2323     {
2324       as_bad (_("number of registers must be in the range [1:4]"));
2325       ignore_rest_of_line ();
2326       return;
2327     }
2328
2329   demand_empty_rest_of_line ();
2330
2331   if (reg == 4)
2332     {
2333       /* Short form.  */
2334       op = 0xb4 | (num_regs - 1);
2335       add_unwind_opcode (op, 1);
2336     }
2337   else
2338     {
2339       /* Long form.  */
2340       op = 0xc800 | (reg << 4) | (num_regs - 1);
2341       add_unwind_opcode (op, 2);
2342     }
2343   unwind.frame_size += num_regs * 12;
2344 }
2345
2346
2347 /* Parse a directive saving VFP registers.  */
2348
2349 static void
2350 s_arm_unwind_save_vfp (void)
2351 {
2352   int count;
2353   unsigned int reg;
2354   valueT op;
2355
2356   count = parse_vfp_reg_list (&input_line_pointer, &reg, 1);
2357   if (count == FAIL)
2358     {
2359       as_bad (_("expected register list"));
2360       ignore_rest_of_line ();
2361       return;
2362     }
2363
2364   demand_empty_rest_of_line ();
2365
2366   if (reg == 8)
2367     {
2368       /* Short form.  */
2369       op = 0xb8 | (count - 1);
2370       add_unwind_opcode (op, 1);
2371     }
2372   else
2373     {
2374       /* Long form.  */
2375       op = 0xb300 | (reg << 4) | (count - 1);
2376       add_unwind_opcode (op, 2);
2377     }
2378   unwind.frame_size += count * 8 + 4;
2379 }
2380
2381
2382 /* Parse a directive saving iWMMXt data registers.  */
2383
2384 static void
2385 s_arm_unwind_save_mmxwr (void)
2386 {
2387   int reg;
2388   int hi_reg;
2389   int i;
2390   unsigned mask = 0;
2391   valueT op;
2392
2393   if (*input_line_pointer == '{')
2394     input_line_pointer++;
2395
2396   do
2397     {
2398       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
2399
2400       if (reg == FAIL)
2401         {
2402           as_bad (_(reg_expected_msgs[REG_TYPE_MMXWR]));
2403           goto error;
2404         }
2405
2406       if (mask >> reg)
2407         as_tsktsk (_("register list not in ascending order"));
2408       mask |= 1 << reg;
2409
2410       if (*input_line_pointer == '-')
2411         {
2412           input_line_pointer++;
2413           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
2414           if (hi_reg == FAIL)
2415             {
2416               as_bad (_(reg_expected_msgs[REG_TYPE_MMXWR]));
2417               goto error;
2418             }
2419           else if (reg >= hi_reg)
2420             {
2421               as_bad (_("bad register range"));
2422               goto error;
2423             }
2424           for (; reg < hi_reg; reg++)
2425             mask |= 1 << reg;
2426         }
2427     }
2428   while (skip_past_comma (&input_line_pointer) != FAIL);
2429
2430   if (*input_line_pointer == '}')
2431     input_line_pointer++;
2432
2433   demand_empty_rest_of_line ();
2434
2435   /* Generate any deferred opcodes becuuse we're going to be looking at
2436      the list.  */
2437   flush_pending_unwind ();
2438
2439   for (i = 0; i < 16; i++)
2440     {
2441       if (mask & (1 << i))
2442         unwind.frame_size += 8;
2443     }
2444
2445   /* Attempt to combine with a previous opcode.  We do this because gcc
2446      likes to output separate unwind directives for a single block of
2447      registers.  */
2448   if (unwind.opcode_count > 0)
2449     {
2450       i = unwind.opcodes[unwind.opcode_count - 1];
2451       if ((i & 0xf8) == 0xc0)
2452         {
2453           i &= 7;
2454           /* Only merge if the blocks are contiguous.  */
2455           if (i < 6)
2456             {
2457               if ((mask & 0xfe00) == (1 << 9))
2458                 {
2459                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
2460                   unwind.opcode_count--;
2461                 }
2462             }
2463           else if (i == 6 && unwind.opcode_count >= 2)
2464             {
2465               i = unwind.opcodes[unwind.opcode_count - 2];
2466               reg = i >> 4;
2467               i &= 0xf;
2468
2469               op = 0xffff << (reg - 1);
2470               if (reg > 0
2471                   || ((mask & op) == (1u << (reg - 1))))
2472                 {
2473                   op = (1 << (reg + i + 1)) - 1;
2474                   op &= ~((1 << reg) - 1);
2475                   mask |= op;
2476                   unwind.opcode_count -= 2;
2477                 }
2478             }
2479         }
2480     }
2481
2482   hi_reg = 15;
2483   /* We want to generate opcodes in the order the registers have been
2484      saved, ie. descending order.  */
2485   for (reg = 15; reg >= -1; reg--)
2486     {
2487       /* Save registers in blocks.  */
2488       if (reg < 0
2489           || !(mask & (1 << reg)))
2490         {
2491           /* We found an unsaved reg.  Generate opcodes to save the
2492              preceeding block.  */
2493           if (reg != hi_reg)
2494             {
2495               if (reg == 9)
2496                 {
2497                   /* Short form.  */
2498                   op = 0xc0 | (hi_reg - 10);
2499                   add_unwind_opcode (op, 1);
2500                 }
2501               else
2502                 {
2503                   /* Long form.  */
2504                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
2505                   add_unwind_opcode (op, 2);
2506                 }
2507             }
2508           hi_reg = reg - 1;
2509         }
2510     }
2511
2512   return;
2513 error:
2514   ignore_rest_of_line ();
2515 }
2516
2517 static void
2518 s_arm_unwind_save_mmxwcg (void)
2519 {
2520   int reg;
2521   int hi_reg;
2522   unsigned mask = 0;
2523   valueT op;
2524
2525   if (*input_line_pointer == '{')
2526     input_line_pointer++;
2527
2528   do
2529     {
2530       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
2531
2532       if (reg == FAIL)
2533         {
2534           as_bad (_(reg_expected_msgs[REG_TYPE_MMXWCG]));
2535           goto error;
2536         }
2537
2538       reg -= 8;
2539       if (mask >> reg)
2540         as_tsktsk (_("register list not in ascending order"));
2541       mask |= 1 << reg;
2542
2543       if (*input_line_pointer == '-')
2544         {
2545           input_line_pointer++;
2546           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
2547           if (hi_reg == FAIL)
2548             {
2549               as_bad (_(reg_expected_msgs[REG_TYPE_MMXWCG]));
2550               goto error;
2551             }
2552           else if (reg >= hi_reg)
2553             {
2554               as_bad (_("bad register range"));
2555               goto error;
2556             }
2557           for (; reg < hi_reg; reg++)
2558             mask |= 1 << reg;
2559         }
2560     }
2561   while (skip_past_comma (&input_line_pointer) != FAIL);
2562
2563   if (*input_line_pointer == '}')
2564     input_line_pointer++;
2565
2566   demand_empty_rest_of_line ();
2567
2568   /* Generate any deferred opcodes becuuse we're going to be looking at
2569      the list.  */
2570   flush_pending_unwind ();
2571
2572   for (reg = 0; reg < 16; reg++)
2573     {
2574       if (mask & (1 << reg))
2575         unwind.frame_size += 4;
2576     }
2577   op = 0xc700 | mask;
2578   add_unwind_opcode (op, 2);
2579   return;
2580 error:
2581   ignore_rest_of_line ();
2582 }
2583
2584
2585 /* Parse an unwind_save directive.  */
2586
2587 static void
2588 s_arm_unwind_save (int ignored ATTRIBUTE_UNUSED)
2589 {
2590   char *peek;
2591   struct reg_entry *reg;
2592   bfd_boolean had_brace = FALSE;
2593
2594   /* Figure out what sort of save we have.  */
2595   peek = input_line_pointer;
2596
2597   if (*peek == '{')
2598     {
2599       had_brace = TRUE;
2600       peek++;
2601     }
2602
2603   reg = arm_reg_parse_multi (&peek);
2604
2605   if (!reg)
2606     {
2607       as_bad (_("register expected"));
2608       ignore_rest_of_line ();
2609       return;
2610     }
2611
2612   switch (reg->type)
2613     {
2614     case REG_TYPE_FN:
2615       if (had_brace)
2616         {
2617           as_bad (_("FPA .unwind_save does not take a register list"));
2618           ignore_rest_of_line ();
2619           return;
2620         }
2621       s_arm_unwind_save_fpa (reg->number);
2622       return;
2623
2624     case REG_TYPE_RN:     s_arm_unwind_save_core ();   return;
2625     case REG_TYPE_VFD:     s_arm_unwind_save_vfp ();    return;
2626     case REG_TYPE_MMXWR:  s_arm_unwind_save_mmxwr ();  return;
2627     case REG_TYPE_MMXWCG: s_arm_unwind_save_mmxwcg (); return;
2628
2629     default:
2630       as_bad (_(".unwind_save does not support this kind of register"));
2631       ignore_rest_of_line ();
2632     }
2633 }
2634
2635
2636 /* Parse an unwind_movsp directive.  */
2637
2638 static void
2639 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
2640 {
2641   int reg;
2642   valueT op;
2643
2644   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
2645   if (reg == FAIL)
2646     {
2647       as_bad (_(reg_expected_msgs[REG_TYPE_RN]));
2648       ignore_rest_of_line ();
2649       return;
2650     }
2651   demand_empty_rest_of_line ();
2652
2653   if (reg == REG_SP || reg == REG_PC)
2654     {
2655       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
2656       return;
2657     }
2658
2659   if (unwind.fp_reg != REG_SP)
2660     as_bad (_("unexpected .unwind_movsp directive"));
2661
2662   /* Generate opcode to restore the value.  */
2663   op = 0x90 | reg;
2664   add_unwind_opcode (op, 1);
2665
2666   /* Record the information for later.  */
2667   unwind.fp_reg = reg;
2668   unwind.fp_offset = unwind.frame_size;
2669   unwind.sp_restored = 1;
2670 }
2671
2672 /* Parse an unwind_pad directive.  */
2673
2674 static void
2675 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
2676 {
2677   int offset;
2678
2679   if (immediate_for_directive (&offset) == FAIL)
2680     return;
2681
2682   if (offset & 3)
2683     {
2684       as_bad (_("stack increment must be multiple of 4"));
2685       ignore_rest_of_line ();
2686       return;
2687     }
2688
2689   /* Don't generate any opcodes, just record the details for later.  */
2690   unwind.frame_size += offset;
2691   unwind.pending_offset += offset;
2692
2693   demand_empty_rest_of_line ();
2694 }
2695
2696 /* Parse an unwind_setfp directive.  */
2697
2698 static void
2699 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
2700 {
2701   int sp_reg;
2702   int fp_reg;
2703   int offset;
2704
2705   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
2706   if (skip_past_comma (&input_line_pointer) == FAIL)
2707     sp_reg = FAIL;
2708   else
2709     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
2710
2711   if (fp_reg == FAIL || sp_reg == FAIL)
2712     {
2713       as_bad (_("expected <reg>, <reg>"));
2714       ignore_rest_of_line ();
2715       return;
2716     }
2717
2718   /* Optional constant.  */
2719   if (skip_past_comma (&input_line_pointer) != FAIL)
2720     {
2721       if (immediate_for_directive (&offset) == FAIL)
2722         return;
2723     }
2724   else
2725     offset = 0;
2726
2727   demand_empty_rest_of_line ();
2728
2729   if (sp_reg != 13 && sp_reg != unwind.fp_reg)
2730     {
2731       as_bad (_("register must be either sp or set by a previous"
2732                 "unwind_movsp directive"));
2733       return;
2734     }
2735
2736   /* Don't generate any opcodes, just record the information for later.  */
2737   unwind.fp_reg = fp_reg;
2738   unwind.fp_used = 1;
2739   if (sp_reg == 13)
2740     unwind.fp_offset = unwind.frame_size - offset;
2741   else
2742     unwind.fp_offset -= offset;
2743 }
2744
2745 /* Parse an unwind_raw directive.  */
2746
2747 static void
2748 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
2749 {
2750   expressionS exp;
2751   /* This is an arbitary limit.  */
2752   unsigned char op[16];
2753   int count;
2754
2755   expression (&exp);
2756   if (exp.X_op == O_constant
2757       && skip_past_comma (&input_line_pointer) != FAIL)
2758     {
2759       unwind.frame_size += exp.X_add_number;
2760       expression (&exp);
2761     }
2762   else
2763     exp.X_op = O_illegal;
2764
2765   if (exp.X_op != O_constant)
2766     {
2767       as_bad (_("expected <offset>, <opcode>"));
2768       ignore_rest_of_line ();
2769       return;
2770     }
2771
2772   count = 0;
2773
2774   /* Parse the opcode.  */
2775   for (;;)
2776     {
2777       if (count >= 16)
2778         {
2779           as_bad (_("unwind opcode too long"));
2780           ignore_rest_of_line ();
2781         }
2782       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
2783         {
2784           as_bad (_("invalid unwind opcode"));
2785           ignore_rest_of_line ();
2786           return;
2787         }
2788       op[count++] = exp.X_add_number;
2789
2790       /* Parse the next byte.  */
2791       if (skip_past_comma (&input_line_pointer) == FAIL)
2792         break;
2793
2794       expression (&exp);
2795     }
2796
2797   /* Add the opcode bytes in reverse order.  */
2798   while (count--)
2799     add_unwind_opcode (op[count], 1);
2800
2801   demand_empty_rest_of_line ();
2802 }
2803
2804
2805 /* Parse a .eabi_attribute directive.  */
2806
2807 static void
2808 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
2809 {
2810   expressionS exp;
2811   bfd_boolean is_string;
2812   int tag;
2813   unsigned int i = 0;
2814   char *s = NULL;
2815   char saved_char;
2816
2817   expression (& exp);
2818   if (exp.X_op != O_constant)
2819     goto bad;
2820
2821   tag = exp.X_add_number;
2822   if (tag == 4 || tag == 5 || tag == 32 || (tag > 32 && (tag & 1) != 0))
2823     is_string = 1;
2824   else
2825     is_string = 0;
2826
2827   if (skip_past_comma (&input_line_pointer) == FAIL)
2828     goto bad;
2829   if (tag == 32 || !is_string)
2830     {
2831       expression (& exp);
2832       if (exp.X_op != O_constant)
2833         {
2834           as_bad (_("expected numeric constant"));
2835           ignore_rest_of_line ();
2836           return;
2837         }
2838       i = exp.X_add_number;
2839     }
2840   if (tag == Tag_compatibility
2841       && skip_past_comma (&input_line_pointer) == FAIL)
2842     {
2843       as_bad (_("expected comma"));
2844       ignore_rest_of_line ();
2845       return;
2846     }
2847   if (is_string)
2848     {
2849       skip_whitespace(input_line_pointer);
2850       if (*input_line_pointer != '"')
2851         goto bad_string;
2852       input_line_pointer++;
2853       s = input_line_pointer;
2854       while (*input_line_pointer && *input_line_pointer != '"')
2855         input_line_pointer++;
2856       if (*input_line_pointer != '"')
2857         goto bad_string;
2858       saved_char = *input_line_pointer;
2859       *input_line_pointer = 0;
2860     }
2861   else
2862     {
2863       s = NULL;
2864       saved_char = 0;
2865     }
2866   
2867   if (tag == Tag_compatibility)
2868     elf32_arm_add_eabi_attr_compat (stdoutput, i, s);
2869   else if (is_string)
2870     elf32_arm_add_eabi_attr_string (stdoutput, tag, s);
2871   else
2872     elf32_arm_add_eabi_attr_int (stdoutput, tag, i);
2873
2874   if (s)
2875     {
2876       *input_line_pointer = saved_char;
2877       input_line_pointer++;
2878     }
2879   demand_empty_rest_of_line ();
2880   return;
2881 bad_string:
2882   as_bad (_("bad string constant"));
2883   ignore_rest_of_line ();
2884   return;
2885 bad:
2886   as_bad (_("expected <tag> , <value>"));
2887   ignore_rest_of_line ();
2888 }
2889
2890 static void s_arm_arch (int);
2891 static void s_arm_cpu (int);
2892 static void s_arm_fpu (int);
2893 #endif /* OBJ_ELF */
2894
2895 /* This table describes all the machine specific pseudo-ops the assembler
2896    has to support.  The fields are:
2897      pseudo-op name without dot
2898      function to call to execute this pseudo-op
2899      Integer arg to pass to the function.  */
2900
2901 const pseudo_typeS md_pseudo_table[] =
2902 {
2903   /* Never called because '.req' does not start a line.  */
2904   { "req",         s_req,         0 },
2905   { "unreq",       s_unreq,       0 },
2906   { "bss",         s_bss,         0 },
2907   { "align",       s_align,       0 },
2908   { "arm",         s_arm,         0 },
2909   { "thumb",       s_thumb,       0 },
2910   { "code",        s_code,        0 },
2911   { "force_thumb", s_force_thumb, 0 },
2912   { "thumb_func",  s_thumb_func,  0 },
2913   { "thumb_set",   s_thumb_set,   0 },
2914   { "even",        s_even,        0 },
2915   { "ltorg",       s_ltorg,       0 },
2916   { "pool",        s_ltorg,       0 },
2917   { "syntax",      s_syntax,      0 },
2918 #ifdef OBJ_ELF
2919   { "word",        s_arm_elf_cons, 4 },
2920   { "long",        s_arm_elf_cons, 4 },
2921   { "rel31",       s_arm_rel31,   0 },
2922   { "fnstart",          s_arm_unwind_fnstart,   0 },
2923   { "fnend",            s_arm_unwind_fnend,     0 },
2924   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
2925   { "personality",      s_arm_unwind_personality, 0 },
2926   { "personalityindex", s_arm_unwind_personalityindex, 0 },
2927   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
2928   { "save",             s_arm_unwind_save,      0 },
2929   { "movsp",            s_arm_unwind_movsp,     0 },
2930   { "pad",              s_arm_unwind_pad,       0 },
2931   { "setfp",            s_arm_unwind_setfp,     0 },
2932   { "unwind_raw",       s_arm_unwind_raw,       0 },
2933   { "cpu",              s_arm_cpu,              0 },
2934   { "arch",             s_arm_arch,             0 },
2935   { "fpu",              s_arm_fpu,              0 },
2936   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
2937 #else
2938   { "word",        cons, 4},
2939 #endif
2940   { "extend",      float_cons, 'x' },
2941   { "ldouble",     float_cons, 'x' },
2942   { "packed",      float_cons, 'p' },
2943   { 0, 0, 0 }
2944 };
2945 \f
2946 /* Parser functions used exclusively in instruction operands.  */
2947
2948 /* Generic immediate-value read function for use in insn parsing.
2949    STR points to the beginning of the immediate (the leading #);
2950    VAL receives the value; if the value is outside [MIN, MAX]
2951    issue an error.  PREFIX_OPT is true if the immediate prefix is
2952    optional.  */
2953
2954 static int
2955 parse_immediate (char **str, int *val, int min, int max,
2956                  bfd_boolean prefix_opt)
2957 {
2958   expressionS exp;
2959   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
2960   if (exp.X_op != O_constant)
2961     {
2962       inst.error = _("constant expression required");
2963       return FAIL;
2964     }
2965
2966   if (exp.X_add_number < min || exp.X_add_number > max)
2967     {
2968       inst.error = _("immediate value out of range");
2969       return FAIL;
2970     }
2971
2972   *val = exp.X_add_number;
2973   return SUCCESS;
2974 }
2975
2976 /* Returns the pseudo-register number of an FPA immediate constant,
2977    or FAIL if there isn't a valid constant here.  */
2978
2979 static int
2980 parse_fpa_immediate (char ** str)
2981 {
2982   LITTLENUM_TYPE words[MAX_LITTLENUMS];
2983   char *         save_in;
2984   expressionS    exp;
2985   int            i;
2986   int            j;
2987
2988   /* First try and match exact strings, this is to guarantee
2989      that some formats will work even for cross assembly.  */
2990
2991   for (i = 0; fp_const[i]; i++)
2992     {
2993       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
2994         {
2995           char *start = *str;
2996
2997           *str += strlen (fp_const[i]);
2998           if (is_end_of_line[(unsigned char) **str])
2999             return i + 8;
3000           *str = start;
3001         }
3002     }
3003
3004   /* Just because we didn't get a match doesn't mean that the constant
3005      isn't valid, just that it is in a format that we don't
3006      automatically recognize.  Try parsing it with the standard
3007      expression routines.  */
3008
3009   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
3010
3011   /* Look for a raw floating point number.  */
3012   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
3013       && is_end_of_line[(unsigned char) *save_in])
3014     {
3015       for (i = 0; i < NUM_FLOAT_VALS; i++)
3016         {
3017           for (j = 0; j < MAX_LITTLENUMS; j++)
3018             {
3019               if (words[j] != fp_values[i][j])
3020                 break;
3021             }
3022
3023           if (j == MAX_LITTLENUMS)
3024             {
3025               *str = save_in;
3026               return i + 8;
3027             }
3028         }
3029     }
3030
3031   /* Try and parse a more complex expression, this will probably fail
3032      unless the code uses a floating point prefix (eg "0f").  */
3033   save_in = input_line_pointer;
3034   input_line_pointer = *str;
3035   if (expression (&exp) == absolute_section
3036       && exp.X_op == O_big
3037       && exp.X_add_number < 0)
3038     {
3039       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
3040          Ditto for 15.  */
3041       if (gen_to_words (words, 5, (long) 15) == 0)
3042         {
3043           for (i = 0; i < NUM_FLOAT_VALS; i++)
3044             {
3045               for (j = 0; j < MAX_LITTLENUMS; j++)
3046                 {
3047                   if (words[j] != fp_values[i][j])
3048                     break;
3049                 }
3050
3051               if (j == MAX_LITTLENUMS)
3052                 {
3053                   *str = input_line_pointer;
3054                   input_line_pointer = save_in;
3055                   return i + 8;
3056                 }
3057             }
3058         }
3059     }
3060
3061   *str = input_line_pointer;
3062   input_line_pointer = save_in;
3063   inst.error = _("invalid FPA immediate expression");
3064   return FAIL;
3065 }
3066
3067 /* Shift operands.  */
3068 enum shift_kind
3069 {
3070   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
3071 };
3072
3073 struct asm_shift_name
3074 {
3075   const char      *name;
3076   enum shift_kind  kind;
3077 };
3078
3079 /* Third argument to parse_shift.  */
3080 enum parse_shift_mode
3081 {
3082   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
3083   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
3084   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
3085   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
3086   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
3087 };
3088
3089 /* Parse a <shift> specifier on an ARM data processing instruction.
3090    This has three forms:
3091
3092      (LSL|LSR|ASL|ASR|ROR) Rs
3093      (LSL|LSR|ASL|ASR|ROR) #imm
3094      RRX
3095
3096    Note that ASL is assimilated to LSL in the instruction encoding, and
3097    RRX to ROR #0 (which cannot be written as such).  */
3098
3099 static int
3100 parse_shift (char **str, int i, enum parse_shift_mode mode)
3101 {
3102   const struct asm_shift_name *shift_name;
3103   enum shift_kind shift;
3104   char *s = *str;
3105   char *p = s;
3106   int reg;
3107
3108   for (p = *str; ISALPHA (*p); p++)
3109     ;
3110
3111   if (p == *str)
3112     {
3113       inst.error = _("shift expression expected");
3114       return FAIL;
3115     }
3116
3117   shift_name = hash_find_n (arm_shift_hsh, *str, p - *str);
3118
3119   if (shift_name == NULL)
3120     {
3121       inst.error = _("shift expression expected");
3122       return FAIL;
3123     }
3124
3125   shift = shift_name->kind;
3126
3127   switch (mode)
3128     {
3129     case NO_SHIFT_RESTRICT:
3130     case SHIFT_IMMEDIATE:   break;
3131
3132     case SHIFT_LSL_OR_ASR_IMMEDIATE:
3133       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
3134         {
3135           inst.error = _("'LSL' or 'ASR' required");
3136           return FAIL;
3137         }
3138       break;
3139
3140     case SHIFT_LSL_IMMEDIATE:
3141       if (shift != SHIFT_LSL)
3142         {
3143           inst.error = _("'LSL' required");
3144           return FAIL;
3145         }
3146       break;
3147
3148     case SHIFT_ASR_IMMEDIATE:
3149       if (shift != SHIFT_ASR)
3150         {
3151           inst.error = _("'ASR' required");
3152           return FAIL;
3153         }
3154       break;
3155
3156     default: abort ();
3157     }
3158
3159   if (shift != SHIFT_RRX)
3160     {
3161       /* Whitespace can appear here if the next thing is a bare digit.  */
3162       skip_whitespace (p);
3163
3164       if (mode == NO_SHIFT_RESTRICT
3165           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
3166         {
3167           inst.operands[i].imm = reg;
3168           inst.operands[i].immisreg = 1;
3169         }
3170       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3171         return FAIL;
3172     }
3173   inst.operands[i].shift_kind = shift;
3174   inst.operands[i].shifted = 1;
3175   *str = p;
3176   return SUCCESS;
3177 }
3178
3179 /* Parse a <shifter_operand> for an ARM data processing instruction:
3180
3181       #<immediate>
3182       #<immediate>, <rotate>
3183       <Rm>
3184       <Rm>, <shift>
3185
3186    where <shift> is defined by parse_shift above, and <rotate> is a
3187    multiple of 2 between 0 and 30.  Validation of immediate operands
3188    is deferred to md_apply_fix.  */
3189
3190 static int
3191 parse_shifter_operand (char **str, int i)
3192 {
3193   int value;
3194   expressionS expr;
3195
3196   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
3197     {
3198       inst.operands[i].reg = value;
3199       inst.operands[i].isreg = 1;
3200
3201       /* parse_shift will override this if appropriate */
3202       inst.reloc.exp.X_op = O_constant;
3203       inst.reloc.exp.X_add_number = 0;
3204
3205       if (skip_past_comma (str) == FAIL)
3206         return SUCCESS;
3207
3208       /* Shift operation on register.  */
3209       return parse_shift (str, i, NO_SHIFT_RESTRICT);
3210     }
3211
3212   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
3213     return FAIL;
3214
3215   if (skip_past_comma (str) == SUCCESS)
3216     {
3217       /* #x, y -- ie explicit rotation by Y.  */
3218       if (my_get_expression (&expr, str, GE_NO_PREFIX))
3219         return FAIL;
3220
3221       if (expr.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
3222         {
3223           inst.error = _("constant expression expected");
3224           return FAIL;
3225         }
3226
3227       value = expr.X_add_number;
3228       if (value < 0 || value > 30 || value % 2 != 0)
3229         {
3230           inst.error = _("invalid rotation");
3231           return FAIL;
3232         }
3233       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
3234         {
3235           inst.error = _("invalid constant");
3236           return FAIL;
3237         }
3238
3239       /* Convert to decoded value.  md_apply_fix will put it back.  */
3240       inst.reloc.exp.X_add_number
3241         = (((inst.reloc.exp.X_add_number << (32 - value))
3242             | (inst.reloc.exp.X_add_number >> value)) & 0xffffffff);
3243     }
3244
3245   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
3246   inst.reloc.pc_rel = 0;
3247   return SUCCESS;
3248 }
3249
3250 /* Parse all forms of an ARM address expression.  Information is written
3251    to inst.operands[i] and/or inst.reloc.
3252
3253    Preindexed addressing (.preind=1):
3254
3255    [Rn, #offset]       .reg=Rn .reloc.exp=offset
3256    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3257    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3258                        .shift_kind=shift .reloc.exp=shift_imm
3259
3260    These three may have a trailing ! which causes .writeback to be set also.
3261
3262    Postindexed addressing (.postind=1, .writeback=1):
3263
3264    [Rn], #offset       .reg=Rn .reloc.exp=offset
3265    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3266    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3267                        .shift_kind=shift .reloc.exp=shift_imm
3268
3269    Unindexed addressing (.preind=0, .postind=0):
3270
3271    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
3272
3273    Other:
3274
3275    [Rn]{!}             shorthand for [Rn,#0]{!}
3276    =immediate          .isreg=0 .reloc.exp=immediate
3277    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
3278
3279   It is the caller's responsibility to check for addressing modes not
3280   supported by the instruction, and to set inst.reloc.type.  */
3281
3282 static int
3283 parse_address (char **str, int i)
3284 {
3285   char *p = *str;
3286   int reg;
3287
3288   if (skip_past_char (&p, '[') == FAIL)
3289     {
3290       if (skip_past_char (&p, '=') == FAIL)
3291         {
3292           /* bare address - translate to PC-relative offset */
3293           inst.reloc.pc_rel = 1;
3294           inst.operands[i].reg = REG_PC;
3295           inst.operands[i].isreg = 1;
3296           inst.operands[i].preind = 1;
3297         }
3298       /* else a load-constant pseudo op, no special treatment needed here */
3299
3300       if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
3301         return FAIL;
3302
3303       *str = p;
3304       return SUCCESS;
3305     }
3306
3307   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
3308     {
3309       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3310       return FAIL;
3311     }
3312   inst.operands[i].reg = reg;
3313   inst.operands[i].isreg = 1;
3314
3315   if (skip_past_comma (&p) == SUCCESS)
3316     {
3317       inst.operands[i].preind = 1;
3318
3319       if (*p == '+') p++;
3320       else if (*p == '-') p++, inst.operands[i].negative = 1;
3321
3322       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
3323         {
3324           inst.operands[i].imm = reg;
3325           inst.operands[i].immisreg = 1;
3326
3327           if (skip_past_comma (&p) == SUCCESS)
3328             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
3329               return FAIL;
3330         }
3331       else
3332         {
3333           if (inst.operands[i].negative)
3334             {
3335               inst.operands[i].negative = 0;
3336               p--;
3337             }
3338           if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3339             return FAIL;
3340         }
3341     }
3342
3343   if (skip_past_char (&p, ']') == FAIL)
3344     {
3345       inst.error = _("']' expected");
3346       return FAIL;
3347     }
3348
3349   if (skip_past_char (&p, '!') == SUCCESS)
3350     inst.operands[i].writeback = 1;
3351
3352   else if (skip_past_comma (&p) == SUCCESS)
3353     {
3354       if (skip_past_char (&p, '{') == SUCCESS)
3355         {
3356           /* [Rn], {expr} - unindexed, with option */
3357           if (parse_immediate (&p, &inst.operands[i].imm,
3358                                0, 255, TRUE) == FAIL)
3359             return FAIL;
3360
3361           if (skip_past_char (&p, '}') == FAIL)
3362             {
3363               inst.error = _("'}' expected at end of 'option' field");
3364               return FAIL;
3365             }
3366           if (inst.operands[i].preind)
3367             {
3368               inst.error = _("cannot combine index with option");
3369               return FAIL;
3370             }
3371           *str = p;
3372           return SUCCESS;
3373         }
3374       else
3375         {
3376           inst.operands[i].postind = 1;
3377           inst.operands[i].writeback = 1;
3378
3379           if (inst.operands[i].preind)
3380             {
3381               inst.error = _("cannot combine pre- and post-indexing");
3382               return FAIL;
3383             }
3384
3385           if (*p == '+') p++;
3386           else if (*p == '-') p++, inst.operands[i].negative = 1;
3387
3388           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
3389             {
3390               inst.operands[i].imm = reg;
3391               inst.operands[i].immisreg = 1;
3392
3393               if (skip_past_comma (&p) == SUCCESS)
3394                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
3395                   return FAIL;
3396             }
3397           else
3398             {
3399               if (inst.operands[i].negative)
3400                 {
3401                   inst.operands[i].negative = 0;
3402                   p--;
3403                 }
3404               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3405                 return FAIL;
3406             }
3407         }
3408     }
3409
3410   /* If at this point neither .preind nor .postind is set, we have a
3411      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
3412   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
3413     {
3414       inst.operands[i].preind = 1;
3415       inst.reloc.exp.X_op = O_constant;
3416       inst.reloc.exp.X_add_number = 0;
3417     }
3418   *str = p;
3419   return SUCCESS;
3420 }
3421
3422 /* Miscellaneous. */
3423
3424 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
3425    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
3426 static int
3427 parse_psr (char **str)
3428 {
3429   char *p;
3430   unsigned long psr_field;
3431
3432   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
3433      feature for ease of use and backwards compatibility.  */
3434   p = *str;
3435   if (*p == 's' || *p == 'S')
3436     psr_field = SPSR_BIT;
3437   else if (*p == 'c' || *p == 'C')
3438     psr_field = 0;
3439   else
3440     goto error;
3441
3442   p++;
3443   if (strncasecmp (p, "PSR", 3) != 0)
3444     goto error;
3445   p += 3;
3446
3447   if (*p == '_')
3448     {
3449       /* A suffix follows.  */
3450       const struct asm_psr *psr;
3451       char *start;
3452
3453       p++;
3454       start = p;
3455
3456       do
3457         p++;
3458       while (ISALNUM (*p) || *p == '_');
3459
3460       psr = hash_find_n (arm_psr_hsh, start, p - start);
3461       if (!psr)
3462         goto error;
3463
3464       psr_field |= psr->field;
3465     }
3466   else
3467     {
3468       if (ISALNUM (*p))
3469         goto error;    /* Garbage after "[CS]PSR".  */
3470
3471       psr_field |= (PSR_c | PSR_f);
3472     }
3473   *str = p;
3474   return psr_field;
3475
3476  error:
3477   inst.error = _("flag for {c}psr instruction expected");
3478   return FAIL;
3479 }
3480
3481 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
3482    value suitable for splatting into the AIF field of the instruction.  */
3483
3484 static int
3485 parse_cps_flags (char **str)
3486 {
3487   int val = 0;
3488   int saw_a_flag = 0;
3489   char *s = *str;
3490
3491   for (;;)
3492     switch (*s++)
3493       {
3494       case '\0': case ',':
3495         goto done;
3496
3497       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
3498       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
3499       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
3500
3501       default:
3502         inst.error = _("unrecognized CPS flag");
3503         return FAIL;
3504       }
3505
3506  done:
3507   if (saw_a_flag == 0)
3508     {
3509       inst.error = _("missing CPS flags");
3510       return FAIL;
3511     }
3512
3513   *str = s - 1;
3514   return val;
3515 }
3516
3517 /* Parse an endian specifier ("BE" or "LE", case insensitive);
3518    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
3519
3520 static int
3521 parse_endian_specifier (char **str)
3522 {
3523   int little_endian;
3524   char *s = *str;
3525
3526   if (strncasecmp (s, "BE", 2))
3527     little_endian = 0;
3528   else if (strncasecmp (s, "LE", 2))
3529     little_endian = 1;
3530   else
3531     {
3532       inst.error = _("valid endian specifiers are be or le");
3533       return FAIL;
3534     }
3535
3536   if (ISALNUM (s[2]) || s[2] == '_')
3537     {
3538       inst.error = _("valid endian specifiers are be or le");
3539       return FAIL;
3540     }
3541
3542   *str = s + 2;
3543   return little_endian;
3544 }
3545
3546 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
3547    value suitable for poking into the rotate field of an sxt or sxta
3548    instruction, or FAIL on error.  */
3549
3550 static int
3551 parse_ror (char **str)
3552 {
3553   int rot;
3554   char *s = *str;
3555
3556   if (strncasecmp (s, "ROR", 3) == 0)
3557     s += 3;
3558   else
3559     {
3560       inst.error = _("missing rotation field after comma");
3561       return FAIL;
3562     }
3563
3564   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
3565     return FAIL;
3566
3567   switch (rot)
3568     {
3569     case  0: *str = s; return 0x0;
3570     case  8: *str = s; return 0x1;
3571     case 16: *str = s; return 0x2;
3572     case 24: *str = s; return 0x3;
3573
3574     default:
3575       inst.error = _("rotation can only be 0, 8, 16, or 24");
3576       return FAIL;
3577     }
3578 }
3579
3580 /* Parse a conditional code (from conds[] below).  The value returned is in the
3581    range 0 .. 14, or FAIL.  */
3582 static int
3583 parse_cond (char **str)
3584 {
3585   char *p, *q;
3586   const struct asm_cond *c;
3587
3588   p = q = *str;
3589   while (ISALPHA (*q))
3590     q++;
3591
3592   c = hash_find_n (arm_cond_hsh, p, q - p);
3593   if (!c)
3594     {
3595       inst.error = _("condition required");
3596       return FAIL;
3597     }
3598
3599   *str = q;
3600   return c->value;
3601 }
3602
3603 /* Parse the operands of a table branch instruction.  Similar to a memory
3604    operand.  */
3605 static int
3606 parse_tb (char **str)
3607 {
3608   char * p = *str;
3609   int reg;
3610
3611   if (skip_past_char (&p, '[') == FAIL)
3612     return FAIL;
3613
3614   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
3615     {
3616       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3617       return FAIL;
3618     }
3619   inst.operands[0].reg = reg;
3620
3621   if (skip_past_comma (&p) == FAIL)
3622     return FAIL;
3623   
3624   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
3625     {
3626       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3627       return FAIL;
3628     }
3629   inst.operands[0].imm = reg;
3630
3631   if (skip_past_comma (&p) == SUCCESS)
3632     {
3633       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
3634         return FAIL;
3635       if (inst.reloc.exp.X_add_number != 1)
3636         {
3637           inst.error = _("invalid shift");
3638           return FAIL;
3639         }
3640       inst.operands[0].shifted = 1;
3641     }
3642
3643   if (skip_past_char (&p, ']') == FAIL)
3644     {
3645       inst.error = _("']' expected");
3646       return FAIL;
3647     }
3648   *str = p;
3649   return SUCCESS;
3650 }
3651
3652 /* Matcher codes for parse_operands.  */
3653 enum operand_parse_code
3654 {
3655   OP_stop,      /* end of line */
3656
3657   OP_RR,        /* ARM register */
3658   OP_RRnpc,     /* ARM register, not r15 */
3659   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
3660   OP_RRw,       /* ARM register, not r15, optional trailing ! */
3661   OP_RCP,       /* Coprocessor number */
3662   OP_RCN,       /* Coprocessor register */
3663   OP_RF,        /* FPA register */
3664   OP_RVS,       /* VFP single precision register */
3665   OP_RVD,       /* VFP double precision register */
3666   OP_RVC,       /* VFP control register */
3667   OP_RMF,       /* Maverick F register */
3668   OP_RMD,       /* Maverick D register */
3669   OP_RMFX,      /* Maverick FX register */
3670   OP_RMDX,      /* Maverick DX register */
3671   OP_RMAX,      /* Maverick AX register */
3672   OP_RMDS,      /* Maverick DSPSC register */
3673   OP_RIWR,      /* iWMMXt wR register */
3674   OP_RIWC,      /* iWMMXt wC register */
3675   OP_RIWG,      /* iWMMXt wCG register */
3676   OP_RXA,       /* XScale accumulator register */
3677
3678   OP_REGLST,    /* ARM register list */
3679   OP_VRSLST,    /* VFP single-precision register list */
3680   OP_VRDLST,    /* VFP double-precision register list */
3681
3682   OP_I7,        /* immediate value 0 .. 7 */
3683   OP_I15,       /*                 0 .. 15 */
3684   OP_I16,       /*                 1 .. 16 */
3685   OP_I31,       /*                 0 .. 31 */
3686   OP_I31w,      /*                 0 .. 31, optional trailing ! */
3687   OP_I32,       /*                 1 .. 32 */
3688   OP_I63s,      /*               -64 .. 63 */
3689   OP_I255,      /*                 0 .. 255 */
3690   OP_Iffff,     /*                 0 .. 65535 */
3691
3692   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
3693   OP_I7b,       /*                             0 .. 7 */
3694   OP_I15b,      /*                             0 .. 15 */
3695   OP_I31b,      /*                             0 .. 31 */
3696
3697   OP_SH,        /* shifter operand */
3698   OP_ADDR,      /* Memory address expression (any mode) */
3699   OP_EXP,       /* arbitrary expression */
3700   OP_EXPi,      /* same, with optional immediate prefix */
3701   OP_EXPr,      /* same, with optional relocation suffix */
3702
3703   OP_CPSF,      /* CPS flags */
3704   OP_ENDI,      /* Endianness specifier */
3705   OP_PSR,       /* CPSR/SPSR mask for msr */
3706   OP_COND,      /* conditional code */
3707   OP_TB,        /* Table branch.  */
3708
3709   OP_RRnpc_I0,  /* ARM register or literal 0 */
3710   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
3711   OP_RR_EXi,    /* ARM register or expression with imm prefix */
3712   OP_RF_IF,     /* FPA register or immediate */
3713   OP_RIWR_RIWC, /* iWMMXt R or C reg */
3714
3715   /* Optional operands.  */
3716   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
3717   OP_oI31b,      /*                             0 .. 31 */
3718   OP_oIffffb,    /*                             0 .. 65535 */
3719   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
3720
3721   OP_oRR,        /* ARM register */
3722   OP_oRRnpc,     /* ARM register, not the PC */
3723   OP_oSHll,      /* LSL immediate */
3724   OP_oSHar,      /* ASR immediate */
3725   OP_oSHllar,    /* LSL or ASR immediate */
3726   OP_oROR,       /* ROR 0/8/16/24 */
3727
3728   OP_FIRST_OPTIONAL = OP_oI7b
3729 };
3730
3731 /* Generic instruction operand parser.  This does no encoding and no
3732    semantic validation; it merely squirrels values away in the inst
3733    structure.  Returns SUCCESS or FAIL depending on whether the
3734    specified grammar matched.  */
3735 static int
3736 parse_operands (char *str, const unsigned char *pattern)
3737 {
3738   unsigned const char *upat = pattern;
3739   char *backtrack_pos = 0;
3740   const char *backtrack_error = 0;
3741   int i, val, backtrack_index = 0;
3742
3743 #define po_char_or_fail(chr) do {               \
3744   if (skip_past_char (&str, chr) == FAIL)       \
3745     goto bad_args;                              \
3746 } while (0)
3747
3748 #define po_reg_or_fail(regtype) do {                    \
3749   val = arm_reg_parse (&str, regtype);                  \
3750   if (val == FAIL)                                      \
3751     {                                                   \
3752       inst.error = _(reg_expected_msgs[regtype]);       \
3753       goto failure;                                     \
3754     }                                                   \
3755   inst.operands[i].reg = val;                           \
3756   inst.operands[i].isreg = 1;                           \
3757 } while (0)
3758
3759 #define po_reg_or_goto(regtype, label) do {     \
3760   val = arm_reg_parse (&str, regtype);          \
3761   if (val == FAIL)                              \
3762     goto label;                                 \
3763                                                 \
3764   inst.operands[i].reg = val;                   \
3765   inst.operands[i].isreg = 1;                   \
3766 } while (0)
3767
3768 #define po_imm_or_fail(min, max, popt) do {                     \
3769   if (parse_immediate (&str, &val, min, max, popt) == FAIL)     \
3770     goto failure;                                               \
3771   inst.operands[i].imm = val;                                   \
3772 } while (0)
3773
3774 #define po_misc_or_fail(expr) do {              \
3775   if (expr)                                     \
3776     goto failure;                               \
3777 } while (0)
3778
3779   skip_whitespace (str);
3780
3781   for (i = 0; upat[i] != OP_stop; i++)
3782     {
3783       if (upat[i] >= OP_FIRST_OPTIONAL)
3784         {
3785           /* Remember where we are in case we need to backtrack.  */
3786           assert (!backtrack_pos);
3787           backtrack_pos = str;
3788           backtrack_error = inst.error;
3789           backtrack_index = i;
3790         }
3791
3792       if (i > 0)
3793         po_char_or_fail (',');
3794
3795       switch (upat[i])
3796         {
3797           /* Registers */
3798         case OP_oRRnpc:
3799         case OP_RRnpc:
3800         case OP_oRR:
3801         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
3802         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
3803         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
3804         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
3805         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
3806         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
3807         case OP_RVC:   po_reg_or_fail (REG_TYPE_VFC);     break;
3808         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
3809         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
3810         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
3811         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
3812         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
3813         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
3814         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
3815         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
3816         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
3817         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
3818
3819         case OP_RRnpcb:
3820           po_char_or_fail ('[');
3821           po_reg_or_fail  (REG_TYPE_RN);
3822           po_char_or_fail (']');
3823           break;
3824
3825         case OP_RRw:
3826           po_reg_or_fail (REG_TYPE_RN);
3827           if (skip_past_char (&str, '!') == SUCCESS)
3828             inst.operands[i].writeback = 1;
3829           break;
3830
3831           /* Immediates */
3832         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
3833         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
3834         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
3835         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
3836         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
3837         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
3838         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
3839         case OP_Iffff:   po_imm_or_fail (  0, 0xffff, FALSE);   break;
3840
3841         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
3842         case OP_oI7b:
3843         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
3844         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
3845         case OP_oI31b:
3846         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
3847         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
3848
3849           /* Immediate variants */
3850         case OP_oI255c:
3851           po_char_or_fail ('{');
3852           po_imm_or_fail (0, 255, TRUE);
3853           po_char_or_fail ('}');
3854           break;
3855
3856         case OP_I31w:
3857           /* The expression parser chokes on a trailing !, so we have
3858              to find it first and zap it.  */
3859           {
3860             char *s = str;
3861             while (*s && *s != ',')
3862               s++;
3863             if (s[-1] == '!')
3864               {
3865                 s[-1] = '\0';
3866                 inst.operands[i].writeback = 1;
3867               }
3868             po_imm_or_fail (0, 31, TRUE);
3869             if (str == s - 1)
3870               str = s;
3871           }
3872           break;
3873
3874           /* Expressions */
3875         case OP_EXPi:   EXPi:
3876           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
3877                                               GE_OPT_PREFIX));
3878           break;
3879
3880         case OP_EXP:
3881           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
3882                                               GE_NO_PREFIX));
3883           break;
3884
3885         case OP_EXPr:   EXPr:
3886           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
3887                                               GE_NO_PREFIX));
3888           if (inst.reloc.exp.X_op == O_symbol)
3889             {
3890               val = parse_reloc (&str);
3891               if (val == -1)
3892                 {
3893                   inst.error = _("unrecognized relocation suffix");
3894                   goto failure;
3895                 }
3896               else if (val != BFD_RELOC_UNUSED)
3897                 {
3898                   inst.operands[i].imm = val;
3899                   inst.operands[i].hasreloc = 1;
3900                 }
3901             }
3902           break;
3903
3904           /* Register or expression */
3905         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
3906         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
3907
3908           /* Register or immediate */
3909         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
3910         I0:               po_imm_or_fail (0, 0, FALSE);       break;
3911
3912         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
3913         IF:
3914           if (!is_immediate_prefix (*str))
3915             goto bad_args;
3916           str++;
3917           val = parse_fpa_immediate (&str);
3918           if (val == FAIL)
3919             goto failure;
3920           /* FPA immediates are encoded as registers 8-15.
3921              parse_fpa_immediate has already applied the offset.  */
3922           inst.operands[i].reg = val;
3923           inst.operands[i].isreg = 1;
3924           break;
3925
3926           /* Two kinds of register */
3927         case OP_RIWR_RIWC:
3928           {
3929             struct reg_entry *rege = arm_reg_parse_multi (&str);
3930             if (rege->type != REG_TYPE_MMXWR
3931                 && rege->type != REG_TYPE_MMXWC
3932                 && rege->type != REG_TYPE_MMXWCG)
3933               {
3934                 inst.error = _("iWMMXt data or control register expected");
3935                 goto failure;
3936               }
3937             inst.operands[i].reg = rege->number;
3938             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
3939           }
3940           break;
3941
3942           /* Misc */
3943         case OP_CPSF:    val = parse_cps_flags (&str);          break;
3944         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
3945         case OP_oROR:    val = parse_ror (&str);                break;
3946         case OP_PSR:     val = parse_psr (&str);                break;
3947         case OP_COND:    val = parse_cond (&str);               break;
3948
3949         case OP_TB:
3950           po_misc_or_fail (parse_tb (&str));
3951           break;
3952
3953           /* Register lists */
3954         case OP_REGLST:
3955           val = parse_reg_list (&str);
3956           if (*str == '^')
3957             {
3958               inst.operands[1].writeback = 1;
3959               str++;
3960             }
3961           break;
3962
3963         case OP_VRSLST:
3964           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, 0);
3965           break;
3966
3967         case OP_VRDLST:
3968           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, 1);
3969           break;
3970
3971           /* Addressing modes */
3972         case OP_ADDR:
3973           po_misc_or_fail (parse_address (&str, i));
3974           break;
3975
3976         case OP_SH:
3977           po_misc_or_fail (parse_shifter_operand (&str, i));
3978           break;
3979
3980         case OP_oSHll:
3981           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
3982           break;
3983
3984         case OP_oSHar:
3985           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
3986           break;
3987
3988         case OP_oSHllar:
3989           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
3990           break;
3991
3992         default:
3993           as_fatal ("unhandled operand code %d", upat[i]);
3994         }
3995
3996       /* Various value-based sanity checks and shared operations.  We
3997          do not signal immediate failures for the register constraints;
3998          this allows a syntax error to take precedence.  */
3999       switch (upat[i])
4000         {
4001         case OP_oRRnpc:
4002         case OP_RRnpc:
4003         case OP_RRnpcb:
4004         case OP_RRw:
4005         case OP_RRnpc_I0:
4006           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
4007             inst.error = BAD_PC;
4008           break;
4009
4010         case OP_CPSF:
4011         case OP_ENDI:
4012         case OP_oROR:
4013         case OP_PSR:
4014         case OP_COND:
4015         case OP_REGLST:
4016         case OP_VRSLST:
4017         case OP_VRDLST:
4018           if (val == FAIL)
4019             goto failure;
4020           inst.operands[i].imm = val;
4021           break;
4022
4023         default:
4024           break;
4025         }
4026
4027       /* If we get here, this operand was successfully parsed.  */
4028       inst.operands[i].present = 1;
4029       continue;
4030
4031     bad_args:
4032       inst.error = BAD_ARGS;
4033
4034     failure:
4035       if (!backtrack_pos)
4036         return FAIL;
4037
4038       /* Do not backtrack over a trailing optional argument that
4039          absorbed some text.  We will only fail again, with the
4040          'garbage following instruction' error message, which is
4041          probably less helpful than the current one.  */
4042       if (backtrack_index == i && backtrack_pos != str
4043           && upat[i+1] == OP_stop)
4044         return FAIL;
4045
4046       /* Try again, skipping the optional argument at backtrack_pos.  */
4047       str = backtrack_pos;
4048       inst.error = backtrack_error;
4049       inst.operands[backtrack_index].present = 0;
4050       i = backtrack_index;
4051       backtrack_pos = 0;
4052     }
4053
4054   /* Check that we have parsed all the arguments.  */
4055   if (*str != '\0' && !inst.error)
4056     inst.error = _("garbage following instruction");
4057
4058   return inst.error ? FAIL : SUCCESS;
4059 }
4060
4061 #undef po_char_or_fail
4062 #undef po_reg_or_fail
4063 #undef po_reg_or_goto
4064 #undef po_imm_or_fail
4065 \f
4066 /* Shorthand macro for instruction encoding functions issuing errors.  */
4067 #define constraint(expr, err) do {              \
4068   if (expr)                                     \
4069     {                                           \
4070       inst.error = err;                         \
4071       return;                                   \
4072     }                                           \
4073 } while (0)
4074
4075 /* Functions for operand encoding.  ARM, then Thumb.  */
4076
4077 #define rotate_left(v, n) (v << n | v >> (32 - n))
4078
4079 /* If VAL can be encoded in the immediate field of an ARM instruction,
4080    return the encoded form.  Otherwise, return FAIL.  */
4081
4082 static unsigned int
4083 encode_arm_immediate (unsigned int val)
4084 {
4085   unsigned int a, i;
4086
4087   for (i = 0; i < 32; i += 2)
4088     if ((a = rotate_left (val, i)) <= 0xff)
4089       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
4090
4091   return FAIL;
4092 }
4093
4094 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
4095    return the encoded form.  Otherwise, return FAIL.  */
4096 static unsigned int
4097 encode_thumb32_immediate (unsigned int val)
4098 {
4099   unsigned int a, i;
4100
4101   if (val <= 0xff)
4102     return val;
4103
4104   for (i = 1; i <= 24; i++)
4105     {
4106       a = val >> i;
4107       if ((val & ~(0xff << i)) == 0)
4108         return ((val >> i) & 0x7f) | ((32 - i) << 7);
4109     }
4110
4111   a = val & 0xff;
4112   if (val == ((a << 16) | a))
4113     return 0x100 | a;
4114   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
4115     return 0x300 | a;
4116
4117   a = val & 0xff00;
4118   if (val == ((a << 16) | a))
4119     return 0x200 | (a >> 8);
4120
4121   return FAIL;
4122 }
4123 /* Encode a VFP SP register number into inst.instruction.  */
4124
4125 static void
4126 encode_arm_vfp_sp_reg (int reg, enum vfp_sp_reg_pos pos)
4127 {
4128   switch (pos)
4129     {
4130     case VFP_REG_Sd:
4131       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
4132       break;
4133
4134     case VFP_REG_Sn:
4135       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
4136       break;
4137
4138     case VFP_REG_Sm:
4139       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
4140       break;
4141
4142     default:
4143       abort ();
4144     }
4145 }
4146
4147 /* Encode a <shift> in an ARM-format instruction.  The immediate,
4148    if any, is handled by md_apply_fix.   */
4149 static void
4150 encode_arm_shift (int i)
4151 {
4152   if (inst.operands[i].shift_kind == SHIFT_RRX)
4153     inst.instruction |= SHIFT_ROR << 5;
4154   else
4155     {
4156       inst.instruction |= inst.operands[i].shift_kind << 5;
4157       if (inst.operands[i].immisreg)
4158         {
4159           inst.instruction |= SHIFT_BY_REG;
4160           inst.instruction |= inst.operands[i].imm << 8;
4161         }
4162       else
4163         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
4164     }
4165 }
4166
4167 static void
4168 encode_arm_shifter_operand (int i)
4169 {
4170   if (inst.operands[i].isreg)
4171     {
4172       inst.instruction |= inst.operands[i].reg;
4173       encode_arm_shift (i);
4174     }
4175   else
4176     inst.instruction |= INST_IMMEDIATE;
4177 }
4178
4179 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
4180 static void
4181 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
4182 {
4183   assert (inst.operands[i].isreg);
4184   inst.instruction |= inst.operands[i].reg << 16;
4185
4186   if (inst.operands[i].preind)
4187     {
4188       if (is_t)
4189         {
4190           inst.error = _("instruction does not accept preindexed addressing");
4191           return;
4192         }
4193       inst.instruction |= PRE_INDEX;
4194       if (inst.operands[i].writeback)
4195         inst.instruction |= WRITE_BACK;
4196
4197     }
4198   else if (inst.operands[i].postind)
4199     {
4200       assert (inst.operands[i].writeback);
4201       if (is_t)
4202         inst.instruction |= WRITE_BACK;
4203     }
4204   else /* unindexed - only for coprocessor */
4205     {
4206       inst.error = _("instruction does not accept unindexed addressing");
4207       return;
4208     }
4209
4210   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
4211       && (((inst.instruction & 0x000f0000) >> 16)
4212           == ((inst.instruction & 0x0000f000) >> 12)))
4213     as_warn ((inst.instruction & LOAD_BIT)
4214              ? _("destination register same as write-back base")
4215              : _("source register same as write-back base"));
4216 }
4217
4218 /* inst.operands[i] was set up by parse_address.  Encode it into an
4219    ARM-format mode 2 load or store instruction.  If is_t is true,
4220    reject forms that cannot be used with a T instruction (i.e. not
4221    post-indexed).  */
4222 static void
4223 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
4224 {
4225   encode_arm_addr_mode_common (i, is_t);
4226
4227   if (inst.operands[i].immisreg)
4228     {
4229       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
4230       inst.instruction |= inst.operands[i].imm;
4231       if (!inst.operands[i].negative)
4232         inst.instruction |= INDEX_UP;
4233       if (inst.operands[i].shifted)
4234         {
4235           if (inst.operands[i].shift_kind == SHIFT_RRX)
4236             inst.instruction |= SHIFT_ROR << 5;
4237           else
4238             {
4239               inst.instruction |= inst.operands[i].shift_kind << 5;
4240               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
4241             }
4242         }
4243     }
4244   else /* immediate offset in inst.reloc */
4245     {
4246       if (inst.reloc.type == BFD_RELOC_UNUSED)
4247         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
4248     }
4249 }
4250
4251 /* inst.operands[i] was set up by parse_address.  Encode it into an
4252    ARM-format mode 3 load or store instruction.  Reject forms that
4253    cannot be used with such instructions.  If is_t is true, reject
4254    forms that cannot be used with a T instruction (i.e. not
4255    post-indexed).  */
4256 static void
4257 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
4258 {
4259   if (inst.operands[i].immisreg && inst.operands[i].shifted)
4260     {
4261       inst.error = _("instruction does not accept scaled register index");
4262       return;
4263     }
4264
4265   encode_arm_addr_mode_common (i, is_t);
4266
4267   if (inst.operands[i].immisreg)
4268     {
4269       inst.instruction |= inst.operands[i].imm;
4270       if (!inst.operands[i].negative)
4271         inst.instruction |= INDEX_UP;
4272     }
4273   else /* immediate offset in inst.reloc */
4274     {
4275       inst.instruction |= HWOFFSET_IMM;
4276       if (inst.reloc.type == BFD_RELOC_UNUSED)
4277         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
4278     }
4279 }
4280
4281 /* inst.operands[i] was set up by parse_address.  Encode it into an
4282    ARM-format instruction.  Reject all forms which cannot be encoded
4283    into a coprocessor load/store instruction.  If wb_ok is false,
4284    reject use of writeback; if unind_ok is false, reject use of
4285    unindexed addressing.  If reloc_override is not 0, use it instead
4286    of BFD_ARM_CP_OFF_IMM.  */
4287
4288 static int
4289 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
4290 {
4291   inst.instruction |= inst.operands[i].reg << 16;
4292
4293   assert (!(inst.operands[i].preind && inst.operands[i].postind));
4294
4295   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
4296     {
4297       assert (!inst.operands[i].writeback);
4298       if (!unind_ok)
4299         {
4300           inst.error = _("instruction does not support unindexed addressing");
4301           return FAIL;
4302         }
4303       inst.instruction |= inst.operands[i].imm;
4304       inst.instruction |= INDEX_UP;
4305       return SUCCESS;
4306     }
4307
4308   if (inst.operands[i].preind)
4309     inst.instruction |= PRE_INDEX;
4310
4311   if (inst.operands[i].writeback)
4312     {
4313       if (inst.operands[i].reg == REG_PC)
4314         {
4315           inst.error = _("pc may not be used with write-back");
4316           return FAIL;
4317         }
4318       if (!wb_ok)
4319         {
4320           inst.error = _("instruction does not support writeback");
4321           return FAIL;
4322         }
4323       inst.instruction |= WRITE_BACK;
4324     }
4325
4326   if (reloc_override)
4327     inst.reloc.type = reloc_override;
4328   else if (thumb_mode)
4329     inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
4330   else
4331     inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
4332   return SUCCESS;
4333 }
4334
4335 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
4336    Determine whether it can be performed with a move instruction; if
4337    it can, convert inst.instruction to that move instruction and
4338    return 1; if it can't, convert inst.instruction to a literal-pool
4339    load and return 0.  If this is not a valid thing to do in the
4340    current context, set inst.error and return 1.
4341
4342    inst.operands[i] describes the destination register.  */
4343
4344 static int
4345 move_or_literal_pool (int i, bfd_boolean thumb_p, bfd_boolean mode_3)
4346 {
4347   if ((inst.instruction & (thumb_p ? THUMB_LOAD_BIT : LOAD_BIT)) == 0)
4348     {
4349       inst.error = _("invalid pseudo operation");
4350       return 1;
4351     }
4352   if (inst.reloc.exp.X_op != O_constant && inst.reloc.exp.X_op != O_symbol)
4353     {
4354       inst.error = _("constant expression expected");
4355       return 1;
4356     }
4357   if (inst.reloc.exp.X_op == O_constant)
4358     {
4359       if (thumb_p)
4360         {
4361           if ((inst.reloc.exp.X_add_number & ~0xFF) == 0)
4362             {
4363               /* This can be done with a mov(1) instruction.  */
4364               inst.instruction  = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
4365               inst.instruction |= inst.reloc.exp.X_add_number;
4366               return 1;
4367             }
4368         }
4369       else
4370         {
4371           int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
4372           if (value != FAIL)
4373             {
4374               /* This can be done with a mov instruction.  */
4375               inst.instruction &= LITERAL_MASK;
4376               inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
4377               inst.instruction |= value & 0xfff;
4378               return 1;
4379             }
4380
4381           value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
4382           if (value != FAIL)
4383             {
4384               /* This can be done with a mvn instruction.  */
4385               inst.instruction &= LITERAL_MASK;
4386               inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
4387               inst.instruction |= value & 0xfff;
4388               return 1;
4389             }
4390         }
4391     }
4392
4393   if (add_to_lit_pool () == FAIL)
4394     {
4395       inst.error = _("literal pool insertion failed");
4396       return 1;
4397     }
4398   inst.operands[1].reg = REG_PC;
4399   inst.operands[1].isreg = 1;
4400   inst.operands[1].preind = 1;
4401   inst.reloc.pc_rel = 1;
4402   inst.reloc.type = (thumb_p
4403                      ? BFD_RELOC_ARM_THUMB_OFFSET
4404                      : (mode_3
4405                         ? BFD_RELOC_ARM_HWLITERAL
4406                         : BFD_RELOC_ARM_LITERAL));
4407   return 0;
4408 }
4409
4410 /* Functions for instruction encoding, sorted by subarchitecture.
4411    First some generics; their names are taken from the conventional
4412    bit positions for register arguments in ARM format instructions.  */
4413
4414 static void
4415 do_noargs (void)
4416 {
4417 }
4418
4419 static void
4420 do_rd (void)
4421 {
4422   inst.instruction |= inst.operands[0].reg << 12;
4423 }
4424
4425 static void
4426 do_rd_rm (void)
4427 {
4428   inst.instruction |= inst.operands[0].reg << 12;
4429   inst.instruction |= inst.operands[1].reg;
4430 }
4431
4432 static void
4433 do_rd_rn (void)
4434 {
4435   inst.instruction |= inst.operands[0].reg << 12;
4436   inst.instruction |= inst.operands[1].reg << 16;
4437 }
4438
4439 static void
4440 do_rn_rd (void)
4441 {
4442   inst.instruction |= inst.operands[0].reg << 16;
4443   inst.instruction |= inst.operands[1].reg << 12;
4444 }
4445
4446 static void
4447 do_rd_rm_rn (void)
4448 {
4449   unsigned Rn = inst.operands[2].reg;
4450   /* Enforce resutrictions on SWP instruction.  */
4451   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
4452     constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
4453                 _("Rn must not overlap other operands"));
4454   inst.instruction |= inst.operands[0].reg << 12;
4455   inst.instruction |= inst.operands[1].reg;
4456   inst.instruction |= Rn << 16;
4457 }
4458
4459 static void
4460 do_rd_rn_rm (void)
4461 {
4462   inst.instruction |= inst.operands[0].reg << 12;
4463   inst.instruction |= inst.operands[1].reg << 16;
4464   inst.instruction |= inst.operands[2].reg;
4465 }
4466
4467 static void
4468 do_rm_rd_rn (void)
4469 {
4470   inst.instruction |= inst.operands[0].reg;
4471   inst.instruction |= inst.operands[1].reg << 12;
4472   inst.instruction |= inst.operands[2].reg << 16;
4473 }
4474
4475 static void
4476 do_imm0 (void)
4477 {
4478   inst.instruction |= inst.operands[0].imm;
4479 }
4480
4481 static void
4482 do_rd_cpaddr (void)
4483 {
4484   inst.instruction |= inst.operands[0].reg << 12;
4485   encode_arm_cp_address (1, TRUE, TRUE, 0);
4486 }
4487
4488 /* ARM instructions, in alphabetical order by function name (except
4489    that wrapper functions appear immediately after the function they
4490    wrap).  */
4491
4492 /* This is a pseudo-op of the form "adr rd, label" to be converted
4493    into a relative address of the form "add rd, pc, #label-.-8".  */
4494
4495 static void
4496 do_adr (void)
4497 {
4498   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
4499
4500   /* Frag hacking will turn this into a sub instruction if the offset turns
4501      out to be negative.  */
4502   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
4503   inst.reloc.pc_rel = 1;
4504   inst.reloc.exp.X_add_number -= 8;
4505 }
4506
4507 /* This is a pseudo-op of the form "adrl rd, label" to be converted
4508    into a relative address of the form:
4509    add rd, pc, #low(label-.-8)"
4510    add rd, rd, #high(label-.-8)"  */
4511
4512 static void
4513 do_adrl (void)
4514 {
4515   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
4516
4517   /* Frag hacking will turn this into a sub instruction if the offset turns
4518      out to be negative.  */
4519   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
4520   inst.reloc.pc_rel            = 1;
4521   inst.size                    = INSN_SIZE * 2;
4522   inst.reloc.exp.X_add_number -= 8;
4523 }
4524
4525 static void
4526 do_arit (void)
4527 {
4528   if (!inst.operands[1].present)
4529     inst.operands[1].reg = inst.operands[0].reg;
4530   inst.instruction |= inst.operands[0].reg << 12;
4531   inst.instruction |= inst.operands[1].reg << 16;
4532   encode_arm_shifter_operand (2);
4533 }
4534
4535 static void
4536 do_bfc (void)
4537 {
4538   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
4539   constraint (msb > 32, _("bit-field extends past end of register"));
4540   /* The instruction encoding stores the LSB and MSB,
4541      not the LSB and width.  */
4542   inst.instruction |= inst.operands[0].reg << 12;
4543   inst.instruction |= inst.operands[1].imm << 7;
4544   inst.instruction |= (msb - 1) << 16;
4545 }
4546
4547 static void
4548 do_bfi (void)
4549 {
4550   unsigned int msb;
4551
4552   /* #0 in second position is alternative syntax for bfc, which is
4553      the same instruction but with REG_PC in the Rm field.  */
4554   if (!inst.operands[1].isreg)
4555     inst.operands[1].reg = REG_PC;
4556
4557   msb = inst.operands[2].imm + inst.operands[3].imm;
4558   constraint (msb > 32, _("bit-field extends past end of register"));
4559   /* The instruction encoding stores the LSB and MSB,
4560      not the LSB and width.  */
4561   inst.instruction |= inst.operands[0].reg << 12;
4562   inst.instruction |= inst.operands[1].reg;
4563   inst.instruction |= inst.operands[2].imm << 7;
4564   inst.instruction |= (msb - 1) << 16;
4565 }
4566
4567 static void
4568 do_bfx (void)
4569 {
4570   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
4571               _("bit-field extends past end of register"));
4572   inst.instruction |= inst.operands[0].reg << 12;
4573   inst.instruction |= inst.operands[1].reg;
4574   inst.instruction |= inst.operands[2].imm << 7;
4575   inst.instruction |= (inst.operands[3].imm - 1) << 16;
4576 }
4577
4578 /* ARM V5 breakpoint instruction (argument parse)
4579      BKPT <16 bit unsigned immediate>
4580      Instruction is not conditional.
4581         The bit pattern given in insns[] has the COND_ALWAYS condition,
4582         and it is an error if the caller tried to override that.  */
4583
4584 static void
4585 do_bkpt (void)
4586 {
4587   /* Top 12 of 16 bits to bits 19:8.  */
4588   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
4589
4590   /* Bottom 4 of 16 bits to bits 3:0.  */
4591   inst.instruction |= inst.operands[0].imm & 0xf;
4592 }
4593
4594 static void
4595 encode_branch (int default_reloc)
4596 {
4597   if (inst.operands[0].hasreloc)
4598     {
4599       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32,
4600                   _("the only suffix valid here is '(plt)'"));
4601       inst.reloc.type   = BFD_RELOC_ARM_PLT32;
4602     }
4603   else
4604     {
4605       inst.reloc.type = default_reloc;
4606     }
4607   inst.reloc.pc_rel = 1;
4608 }
4609
4610 static void
4611 do_branch (void)
4612 {
4613 #ifdef OBJ_ELF
4614   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
4615     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
4616   else
4617 #endif
4618     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
4619 }
4620
4621 static void
4622 do_bl (void)
4623 {
4624 #ifdef OBJ_ELF
4625   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
4626     {
4627       if (inst.cond == COND_ALWAYS)
4628         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
4629       else
4630         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
4631     }
4632   else
4633 #endif
4634     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
4635 }
4636
4637 /* ARM V5 branch-link-exchange instruction (argument parse)
4638      BLX <target_addr>          ie BLX(1)
4639      BLX{<condition>} <Rm>      ie BLX(2)
4640    Unfortunately, there are two different opcodes for this mnemonic.
4641    So, the insns[].value is not used, and the code here zaps values
4642         into inst.instruction.
4643    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
4644
4645 static void
4646 do_blx (void)
4647 {
4648   if (inst.operands[0].isreg)
4649     {
4650       /* Arg is a register; the opcode provided by insns[] is correct.
4651          It is not illegal to do "blx pc", just useless.  */
4652       if (inst.operands[0].reg == REG_PC)
4653         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
4654
4655       inst.instruction |= inst.operands[0].reg;
4656     }
4657   else
4658     {
4659       /* Arg is an address; this instruction cannot be executed
4660          conditionally, and the opcode must be adjusted.  */
4661       constraint (inst.cond != COND_ALWAYS, BAD_COND);
4662       inst.instruction = 0xfa000000;
4663 #ifdef OBJ_ELF
4664       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
4665         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
4666       else
4667 #endif
4668         encode_branch (BFD_RELOC_ARM_PCREL_BLX);
4669     }
4670 }
4671
4672 static void
4673 do_bx (void)
4674 {
4675   if (inst.operands[0].reg == REG_PC)
4676     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
4677
4678   inst.instruction |= inst.operands[0].reg;
4679 }
4680
4681
4682 /* ARM v5TEJ.  Jump to Jazelle code.  */
4683
4684 static void
4685 do_bxj (void)
4686 {
4687   if (inst.operands[0].reg == REG_PC)
4688     as_tsktsk (_("use of r15 in bxj is not really useful"));
4689
4690   inst.instruction |= inst.operands[0].reg;
4691 }
4692
4693 /* Co-processor data operation:
4694       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
4695       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
4696 static void
4697 do_cdp (void)
4698 {
4699   inst.instruction |= inst.operands[0].reg << 8;
4700   inst.instruction |= inst.operands[1].imm << 20;
4701   inst.instruction |= inst.operands[2].reg << 12;
4702   inst.instruction |= inst.operands[3].reg << 16;
4703   inst.instruction |= inst.operands[4].reg;
4704   inst.instruction |= inst.operands[5].imm << 5;
4705 }
4706
4707 static void
4708 do_cmp (void)
4709 {
4710   inst.instruction |= inst.operands[0].reg << 16;
4711   encode_arm_shifter_operand (1);
4712 }
4713
4714 /* Transfer between coprocessor and ARM registers.
4715    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
4716    MRC2
4717    MCR{cond}
4718    MCR2
4719
4720    No special properties.  */
4721
4722 static void
4723 do_co_reg (void)
4724 {
4725   inst.instruction |= inst.operands[0].reg << 8;
4726   inst.instruction |= inst.operands[1].imm << 21;
4727   inst.instruction |= inst.operands[2].reg << 12;
4728   inst.instruction |= inst.operands[3].reg << 16;
4729   inst.instruction |= inst.operands[4].reg;
4730   inst.instruction |= inst.operands[5].imm << 5;
4731 }
4732
4733 /* Transfer between coprocessor register and pair of ARM registers.
4734    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
4735    MCRR2
4736    MRRC{cond}
4737    MRRC2
4738
4739    Two XScale instructions are special cases of these:
4740
4741      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
4742      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
4743
4744    Result unpredicatable if Rd or Rn is R15.  */
4745
4746 static void
4747 do_co_reg2c (void)
4748 {
4749   inst.instruction |= inst.operands[0].reg << 8;
4750   inst.instruction |= inst.operands[1].imm << 4;
4751   inst.instruction |= inst.operands[2].reg << 12;
4752   inst.instruction |= inst.operands[3].reg << 16;
4753   inst.instruction |= inst.operands[4].reg;
4754 }
4755
4756 static void
4757 do_cpsi (void)
4758 {
4759   inst.instruction |= inst.operands[0].imm << 6;
4760   inst.instruction |= inst.operands[1].imm;
4761 }
4762
4763 static void
4764 do_it (void)
4765 {
4766   /* There is no IT instruction in ARM mode.  We
4767      process it but do not generate code for it.  */
4768   inst.size = 0;
4769 }
4770
4771 static void
4772 do_ldmstm (void)
4773 {
4774   int base_reg = inst.operands[0].reg;
4775   int range = inst.operands[1].imm;
4776
4777   inst.instruction |= base_reg << 16;
4778   inst.instruction |= range;
4779
4780   if (inst.operands[1].writeback)
4781     inst.instruction |= LDM_TYPE_2_OR_3;
4782
4783   if (inst.operands[0].writeback)
4784     {
4785       inst.instruction |= WRITE_BACK;
4786       /* Check for unpredictable uses of writeback.  */
4787       if (inst.instruction & LOAD_BIT)
4788         {
4789           /* Not allowed in LDM type 2.  */
4790           if ((inst.instruction & LDM_TYPE_2_OR_3)
4791               && ((range & (1 << REG_PC)) == 0))
4792             as_warn (_("writeback of base register is UNPREDICTABLE"));
4793           /* Only allowed if base reg not in list for other types.  */
4794           else if (range & (1 << base_reg))
4795             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
4796         }
4797       else /* STM.  */
4798         {
4799           /* Not allowed for type 2.  */
4800           if (inst.instruction & LDM_TYPE_2_OR_3)
4801             as_warn (_("writeback of base register is UNPREDICTABLE"));
4802           /* Only allowed if base reg not in list, or first in list.  */
4803           else if ((range & (1 << base_reg))
4804                    && (range & ((1 << base_reg) - 1)))
4805             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
4806         }
4807     }
4808 }
4809
4810 /* ARMv5TE load-consecutive (argument parse)
4811    Mode is like LDRH.
4812
4813      LDRccD R, mode
4814      STRccD R, mode.  */
4815
4816 static void
4817 do_ldrd (void)
4818 {
4819   constraint (inst.operands[0].reg % 2 != 0,
4820               _("first destination register must be even"));
4821   constraint (inst.operands[1].present
4822               && inst.operands[1].reg != inst.operands[0].reg + 1,
4823               _("can only load two consecutive registers"));
4824   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
4825   constraint (!inst.operands[2].isreg, _("'[' expected"));
4826
4827   if (!inst.operands[1].present)
4828     inst.operands[1].reg = inst.operands[0].reg + 1;
4829   
4830   if (inst.instruction & LOAD_BIT)
4831     {
4832       /* encode_arm_addr_mode_3 will diagnose overlap between the base
4833          register and the first register written; we have to diagnose
4834          overlap between the base and the second register written here.  */
4835
4836       if (inst.operands[2].reg == inst.operands[1].reg
4837           && (inst.operands[2].writeback || inst.operands[2].postind))
4838         as_warn (_("base register written back, and overlaps "
4839                    "second destination register"));
4840
4841       /* For an index-register load, the index register must not overlap the
4842          destination (even if not write-back).  */
4843       else if (inst.operands[2].immisreg
4844                && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
4845                    || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
4846         as_warn (_("index register overlaps destination register"));
4847     }
4848
4849   inst.instruction |= inst.operands[0].reg << 12;
4850   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
4851 }
4852
4853 static void
4854 do_ldrex (void)
4855 {
4856   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
4857               || inst.operands[1].postind || inst.operands[1].writeback
4858               || inst.operands[1].immisreg || inst.operands[1].shifted
4859               || inst.operands[1].negative
4860               /* This can arise if the programmer has written
4861                    strex rN, rM, foo
4862                  or if they have mistakenly used a register name as the last
4863                  operand,  eg:
4864                    strex rN, rM, rX
4865                  It is very difficult to distinguish between these two cases
4866                  because "rX" might actually be a label. ie the register
4867                  name has been occluded by a symbol of the same name. So we
4868                  just generate a general 'bad addressing mode' type error
4869                  message and leave it up to the programmer to discover the
4870                  true cause and fix their mistake.  */
4871               || (inst.operands[1].reg == REG_PC),
4872               BAD_ADDR_MODE);
4873
4874   constraint (inst.reloc.exp.X_op != O_constant
4875               || inst.reloc.exp.X_add_number != 0,
4876               _("offset must be zero in ARM encoding"));
4877
4878   inst.instruction |= inst.operands[0].reg << 12;
4879   inst.instruction |= inst.operands[1].reg << 16;
4880   inst.reloc.type = BFD_RELOC_UNUSED;
4881 }
4882
4883 static void
4884 do_ldrexd (void)
4885 {
4886   constraint (inst.operands[0].reg % 2 != 0,
4887               _("even register required"));
4888   constraint (inst.operands[1].present
4889               && inst.operands[1].reg != inst.operands[0].reg + 1,
4890               _("can only load two consecutive registers"));
4891   /* If op 1 were present and equal to PC, this function wouldn't
4892      have been called in the first place.  */
4893   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
4894
4895   inst.instruction |= inst.operands[0].reg << 12;
4896   inst.instruction |= inst.operands[2].reg << 16;
4897 }
4898
4899 static void
4900 do_ldst (void)
4901 {
4902   inst.instruction |= inst.operands[0].reg << 12;
4903   if (!inst.operands[1].isreg)
4904     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/FALSE))
4905       return;
4906   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
4907 }
4908
4909 static void
4910 do_ldstt (void)
4911 {
4912   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
4913      reject [Rn,...].  */
4914   if (inst.operands[1].preind)
4915     {
4916       constraint (inst.reloc.exp.X_op != O_constant ||
4917                   inst.reloc.exp.X_add_number != 0,
4918                   _("this instruction requires a post-indexed address"));
4919
4920       inst.operands[1].preind = 0;
4921       inst.operands[1].postind = 1;
4922       inst.operands[1].writeback = 1;
4923     }
4924   inst.instruction |= inst.operands[0].reg << 12;
4925   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
4926 }
4927
4928 /* Halfword and signed-byte load/store operations.  */
4929
4930 static void
4931 do_ldstv4 (void)
4932 {
4933   inst.instruction |= inst.operands[0].reg << 12;
4934   if (!inst.operands[1].isreg)
4935     if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/TRUE))
4936       return;
4937   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
4938 }
4939
4940 static void
4941 do_ldsttv4 (void)
4942 {
4943   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
4944      reject [Rn,...].  */
4945   if (inst.operands[1].preind)
4946     {
4947       constraint (inst.reloc.exp.X_op != O_constant ||
4948                   inst.reloc.exp.X_add_number != 0,
4949                   _("this instruction requires a post-indexed address"));
4950
4951       inst.operands[1].preind = 0;
4952       inst.operands[1].postind = 1;
4953       inst.operands[1].writeback = 1;
4954     }
4955   inst.instruction |= inst.operands[0].reg << 12;
4956   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
4957 }
4958
4959 /* Co-processor register load/store.
4960    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
4961 static void
4962 do_lstc (void)
4963 {
4964   inst.instruction |= inst.operands[0].reg << 8;
4965   inst.instruction |= inst.operands[1].reg << 12;
4966   encode_arm_cp_address (2, TRUE, TRUE, 0);
4967 }
4968
4969 static void
4970 do_mlas (void)
4971 {
4972   /* This restriction does not apply to mls (nor to mla in v6, but
4973      that's hard to detect at present).  */
4974   if (inst.operands[0].reg == inst.operands[1].reg
4975       && !(inst.instruction & 0x00400000))
4976     as_tsktsk (_("rd and rm should be different in mla"));
4977
4978   inst.instruction |= inst.operands[0].reg << 16;
4979   inst.instruction |= inst.operands[1].reg;
4980   inst.instruction |= inst.operands[2].reg << 8;
4981   inst.instruction |= inst.operands[3].reg << 12;
4982
4983 }
4984
4985 static void
4986 do_mov (void)
4987 {
4988   inst.instruction |= inst.operands[0].reg << 12;
4989   encode_arm_shifter_operand (1);
4990 }
4991
4992 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
4993 static void
4994 do_mov16 (void)
4995 {
4996   inst.instruction |= inst.operands[0].reg << 12;
4997   /* The value is in two pieces: 0:11, 16:19.  */
4998   inst.instruction |= (inst.operands[1].imm & 0x00000fff);
4999   inst.instruction |= (inst.operands[1].imm & 0x0000f000) << 4;
5000 }
5001
5002 static void
5003 do_mrs (void)
5004 {
5005   /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
5006   constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
5007               != (PSR_c|PSR_f),
5008               _("'CPSR' or 'SPSR' expected"));
5009   inst.instruction |= inst.operands[0].reg << 12;
5010   inst.instruction |= (inst.operands[1].imm & SPSR_BIT);
5011 }
5012
5013 /* Two possible forms:
5014       "{C|S}PSR_<field>, Rm",
5015       "{C|S}PSR_f, #expression".  */
5016
5017 static void
5018 do_msr (void)
5019 {
5020   inst.instruction |= inst.operands[0].imm;
5021   if (inst.operands[1].isreg)
5022     inst.instruction |= inst.operands[1].reg;
5023   else
5024     {
5025       inst.instruction |= INST_IMMEDIATE;
5026       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5027       inst.reloc.pc_rel = 0;
5028     }
5029 }
5030
5031 static void
5032 do_mul (void)
5033 {
5034   if (!inst.operands[2].present)
5035     inst.operands[2].reg = inst.operands[0].reg;
5036   inst.instruction |= inst.operands[0].reg << 16;
5037   inst.instruction |= inst.operands[1].reg;
5038   inst.instruction |= inst.operands[2].reg << 8;
5039
5040   if (inst.operands[0].reg == inst.operands[1].reg)
5041     as_tsktsk (_("rd and rm should be different in mul"));
5042 }
5043
5044 /* Long Multiply Parser
5045    UMULL RdLo, RdHi, Rm, Rs
5046    SMULL RdLo, RdHi, Rm, Rs
5047    UMLAL RdLo, RdHi, Rm, Rs
5048    SMLAL RdLo, RdHi, Rm, Rs.  */
5049
5050 static void
5051 do_mull (void)
5052 {
5053   inst.instruction |= inst.operands[0].reg << 12;
5054   inst.instruction |= inst.operands[1].reg << 16;
5055   inst.instruction |= inst.operands[2].reg;
5056   inst.instruction |= inst.operands[3].reg << 8;
5057
5058   /* rdhi, rdlo and rm must all be different.  */
5059   if (inst.operands[0].reg == inst.operands[1].reg
5060       || inst.operands[0].reg == inst.operands[2].reg
5061       || inst.operands[1].reg == inst.operands[2].reg)
5062     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
5063 }
5064
5065 static void
5066 do_nop (void)
5067 {
5068   if (inst.operands[0].present)
5069     {
5070       /* Architectural NOP hints are CPSR sets with no bits selected.  */
5071       inst.instruction &= 0xf0000000;
5072       inst.instruction |= 0x0320f000 + inst.operands[0].imm;
5073     }
5074 }
5075
5076 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
5077    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
5078    Condition defaults to COND_ALWAYS.
5079    Error if Rd, Rn or Rm are R15.  */
5080
5081 static void
5082 do_pkhbt (void)
5083 {
5084   inst.instruction |= inst.operands[0].reg << 12;
5085   inst.instruction |= inst.operands[1].reg << 16;
5086   inst.instruction |= inst.operands[2].reg;
5087   if (inst.operands[3].present)
5088     encode_arm_shift (3);
5089 }
5090
5091 /* ARM V6 PKHTB (Argument Parse).  */
5092
5093 static void
5094 do_pkhtb (void)
5095 {
5096   if (!inst.operands[3].present)
5097     {
5098       /* If the shift specifier is omitted, turn the instruction
5099          into pkhbt rd, rm, rn. */
5100       inst.instruction &= 0xfff00010;
5101       inst.instruction |= inst.operands[0].reg << 12;
5102       inst.instruction |= inst.operands[1].reg;
5103       inst.instruction |= inst.operands[2].reg << 16;
5104     }
5105   else
5106     {
5107       inst.instruction |= inst.operands[0].reg << 12;
5108       inst.instruction |= inst.operands[1].reg << 16;
5109       inst.instruction |= inst.operands[2].reg;
5110       encode_arm_shift (3);
5111     }
5112 }
5113
5114 /* ARMv5TE: Preload-Cache
5115
5116     PLD <addr_mode>
5117
5118   Syntactically, like LDR with B=1, W=0, L=1.  */
5119
5120 static void
5121 do_pld (void)
5122 {
5123   constraint (!inst.operands[0].isreg,
5124               _("'[' expected after PLD mnemonic"));
5125   constraint (inst.operands[0].postind,
5126               _("post-indexed expression used in preload instruction"));
5127   constraint (inst.operands[0].writeback,
5128               _("writeback used in preload instruction"));
5129   constraint (!inst.operands[0].preind,
5130               _("unindexed addressing used in preload instruction"));
5131   inst.instruction |= inst.operands[0].reg;
5132   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
5133 }
5134
5135 static void
5136 do_push_pop (void)
5137 {
5138   inst.operands[1] = inst.operands[0];
5139   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
5140   inst.operands[0].isreg = 1;
5141   inst.operands[0].writeback = 1;
5142   inst.operands[0].reg = REG_SP;
5143   do_ldmstm ();
5144 }
5145
5146 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
5147    word at the specified address and the following word
5148    respectively.
5149    Unconditionally executed.
5150    Error if Rn is R15.  */
5151
5152 static void
5153 do_rfe (void)
5154 {
5155   inst.instruction |= inst.operands[0].reg << 16;
5156   if (inst.operands[0].writeback)
5157     inst.instruction |= WRITE_BACK;
5158 }
5159
5160 /* ARM V6 ssat (argument parse).  */
5161
5162 static void
5163 do_ssat (void)
5164 {
5165   inst.instruction |= inst.operands[0].reg << 12;
5166   inst.instruction |= (inst.operands[1].imm - 1) << 16;
5167   inst.instruction |= inst.operands[2].reg;
5168
5169   if (inst.operands[3].present)
5170     encode_arm_shift (3);
5171 }
5172
5173 /* ARM V6 usat (argument parse).  */
5174
5175 static void
5176 do_usat (void)
5177 {
5178   inst.instruction |= inst.operands[0].reg << 12;
5179   inst.instruction |= inst.operands[1].imm << 16;
5180   inst.instruction |= inst.operands[2].reg;
5181
5182   if (inst.operands[3].present)
5183     encode_arm_shift (3);
5184 }
5185
5186 /* ARM V6 ssat16 (argument parse).  */
5187
5188 static void
5189 do_ssat16 (void)
5190 {
5191   inst.instruction |= inst.operands[0].reg << 12;
5192   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
5193   inst.instruction |= inst.operands[2].reg;
5194 }
5195
5196 static void
5197 do_usat16 (void)
5198 {
5199   inst.instruction |= inst.operands[0].reg << 12;
5200   inst.instruction |= inst.operands[1].imm << 16;
5201   inst.instruction |= inst.operands[2].reg;
5202 }
5203
5204 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
5205    preserving the other bits.
5206
5207    setend <endian_specifier>, where <endian_specifier> is either
5208    BE or LE.  */
5209
5210 static void
5211 do_setend (void)
5212 {
5213   if (inst.operands[0].imm)
5214     inst.instruction |= 0x200;
5215 }
5216
5217 static void
5218 do_shift (void)
5219 {
5220   unsigned int Rm = (inst.operands[1].present
5221                      ? inst.operands[1].reg
5222                      : inst.operands[0].reg);
5223
5224   inst.instruction |= inst.operands[0].reg << 12;
5225   inst.instruction |= Rm;
5226   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
5227     {
5228       constraint (inst.operands[0].reg != Rm,
5229                   _("source1 and dest must be same register"));
5230       inst.instruction |= inst.operands[2].reg << 8;
5231       inst.instruction |= SHIFT_BY_REG;
5232     }
5233   else
5234     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
5235 }
5236
5237 static void
5238 do_smc (void)
5239 {
5240   inst.reloc.type = BFD_RELOC_ARM_SMC;
5241   inst.reloc.pc_rel = 0;
5242 }
5243
5244 static void
5245 do_swi (void)
5246 {
5247   inst.reloc.type = BFD_RELOC_ARM_SWI;
5248   inst.reloc.pc_rel = 0;
5249 }
5250
5251 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
5252    SMLAxy{cond} Rd,Rm,Rs,Rn
5253    SMLAWy{cond} Rd,Rm,Rs,Rn
5254    Error if any register is R15.  */
5255
5256 static void
5257 do_smla (void)
5258 {
5259   inst.instruction |= inst.operands[0].reg << 16;
5260   inst.instruction |= inst.operands[1].reg;
5261   inst.instruction |= inst.operands[2].reg << 8;
5262   inst.instruction |= inst.operands[3].reg << 12;
5263 }
5264
5265 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
5266    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
5267    Error if any register is R15.
5268    Warning if Rdlo == Rdhi.  */
5269
5270 static void
5271 do_smlal (void)
5272 {
5273   inst.instruction |= inst.operands[0].reg << 12;
5274   inst.instruction |= inst.operands[1].reg << 16;
5275   inst.instruction |= inst.operands[2].reg;
5276   inst.instruction |= inst.operands[3].reg << 8;
5277
5278   if (inst.operands[0].reg == inst.operands[1].reg)
5279     as_tsktsk (_("rdhi and rdlo must be different"));
5280 }
5281
5282 /* ARM V5E (El Segundo) signed-multiply (argument parse)
5283    SMULxy{cond} Rd,Rm,Rs
5284    Error if any register is R15.  */
5285
5286 static void
5287 do_smul (void)
5288 {
5289   inst.instruction |= inst.operands[0].reg << 16;
5290   inst.instruction |= inst.operands[1].reg;
5291   inst.instruction |= inst.operands[2].reg << 8;
5292 }
5293
5294 /* ARM V6 srs (argument parse).  */
5295
5296 static void
5297 do_srs (void)
5298 {
5299   inst.instruction |= inst.operands[0].imm;
5300   if (inst.operands[0].writeback)
5301     inst.instruction |= WRITE_BACK;
5302 }
5303
5304 /* ARM V6 strex (argument parse).  */
5305
5306 static void
5307 do_strex (void)
5308 {
5309   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
5310               || inst.operands[2].postind || inst.operands[2].writeback
5311               || inst.operands[2].immisreg || inst.operands[2].shifted
5312               || inst.operands[2].negative
5313               /* See comment in do_ldrex().  */
5314               || (inst.operands[2].reg == REG_PC),
5315               BAD_ADDR_MODE);
5316
5317   constraint (inst.operands[0].reg == inst.operands[1].reg
5318               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
5319
5320   constraint (inst.reloc.exp.X_op != O_constant
5321               || inst.reloc.exp.X_add_number != 0,
5322               _("offset must be zero in ARM encoding"));
5323
5324   inst.instruction |= inst.operands[0].reg << 12;
5325   inst.instruction |= inst.operands[1].reg;
5326   inst.instruction |= inst.operands[2].reg << 16;
5327   inst.reloc.type = BFD_RELOC_UNUSED;
5328 }
5329
5330 static void
5331 do_strexd (void)
5332 {
5333   constraint (inst.operands[1].reg % 2 != 0,
5334               _("even register required"));
5335   constraint (inst.operands[2].present
5336               && inst.operands[2].reg != inst.operands[1].reg + 1,
5337               _("can only store two consecutive registers"));
5338   /* If op 2 were present and equal to PC, this function wouldn't
5339      have been called in the first place.  */
5340   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
5341
5342   constraint (inst.operands[0].reg == inst.operands[1].reg
5343               || inst.operands[0].reg == inst.operands[1].reg + 1
5344               || inst.operands[0].reg == inst.operands[3].reg,
5345               BAD_OVERLAP);
5346
5347   inst.instruction |= inst.operands[0].reg << 12;
5348   inst.instruction |= inst.operands[1].reg;
5349   inst.instruction |= inst.operands[3].reg << 16;
5350 }
5351
5352 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
5353    extends it to 32-bits, and adds the result to a value in another
5354    register.  You can specify a rotation by 0, 8, 16, or 24 bits
5355    before extracting the 16-bit value.
5356    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
5357    Condition defaults to COND_ALWAYS.
5358    Error if any register uses R15.  */
5359
5360 static void
5361 do_sxtah (void)
5362 {
5363   inst.instruction |= inst.operands[0].reg << 12;
5364   inst.instruction |= inst.operands[1].reg << 16;
5365   inst.instruction |= inst.operands[2].reg;
5366   inst.instruction |= inst.operands[3].imm << 10;
5367 }
5368
5369 /* ARM V6 SXTH.
5370
5371    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
5372    Condition defaults to COND_ALWAYS.
5373    Error if any register uses R15.  */
5374
5375 static void
5376 do_sxth (void)
5377 {
5378   inst.instruction |= inst.operands[0].reg << 12;
5379   inst.instruction |= inst.operands[1].reg;
5380   inst.instruction |= inst.operands[2].imm << 10;
5381 }
5382 \f
5383 /* VFP instructions.  In a logical order: SP variant first, monad
5384    before dyad, arithmetic then move then load/store.  */
5385
5386 static void
5387 do_vfp_sp_monadic (void)
5388 {
5389   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5390   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sm);
5391 }
5392
5393 static void
5394 do_vfp_sp_dyadic (void)
5395 {
5396   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5397   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sn);
5398   encode_arm_vfp_sp_reg (inst.operands[2].reg, VFP_REG_Sm);
5399 }
5400
5401 static void
5402 do_vfp_sp_compare_z (void)
5403 {
5404   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5405 }
5406
5407 static void
5408 do_vfp_dp_sp_cvt (void)
5409 {
5410   inst.instruction |= inst.operands[0].reg << 12;
5411   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sm);
5412 }
5413
5414 static void
5415 do_vfp_sp_dp_cvt (void)
5416 {
5417   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5418   inst.instruction |= inst.operands[1].reg;
5419 }
5420
5421 static void
5422 do_vfp_reg_from_sp (void)
5423 {
5424   inst.instruction |= inst.operands[0].reg << 12;
5425   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sn);
5426 }
5427
5428 static void
5429 do_vfp_reg2_from_sp2 (void)
5430 {
5431   constraint (inst.operands[2].imm != 2,
5432               _("only two consecutive VFP SP registers allowed here"));
5433   inst.instruction |= inst.operands[0].reg << 12;
5434   inst.instruction |= inst.operands[1].reg << 16;
5435   encode_arm_vfp_sp_reg (inst.operands[2].reg, VFP_REG_Sm);
5436 }
5437
5438 static void
5439 do_vfp_sp_from_reg (void)
5440 {
5441   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sn);
5442   inst.instruction |= inst.operands[1].reg << 12;
5443 }
5444
5445 static void
5446 do_vfp_sp2_from_reg2 (void)
5447 {
5448   constraint (inst.operands[0].imm != 2,
5449               _("only two consecutive VFP SP registers allowed here"));
5450   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sm);
5451   inst.instruction |= inst.operands[1].reg << 12;
5452   inst.instruction |= inst.operands[2].reg << 16;
5453 }
5454
5455 static void
5456 do_vfp_sp_ldst (void)
5457 {
5458   encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5459   encode_arm_cp_address (1, FALSE, TRUE, 0);
5460 }
5461
5462 static void
5463 do_vfp_dp_ldst (void)
5464 {
5465   inst.instruction |= inst.operands[0].reg << 12;
5466   encode_arm_cp_address (1, FALSE, TRUE, 0);
5467 }
5468
5469
5470 static void
5471 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
5472 {
5473   if (inst.operands[0].writeback)
5474     inst.instruction |= WRITE_BACK;
5475   else
5476     constraint (ldstm_type != VFP_LDSTMIA,
5477                 _("this addressing mode requires base-register writeback"));
5478   inst.instruction |= inst.operands[0].reg << 16;
5479   encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sd);
5480   inst.instruction |= inst.operands[1].imm;
5481 }
5482
5483 static void
5484 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
5485 {
5486   int count;
5487
5488   if (inst.operands[0].writeback)
5489     inst.instruction |= WRITE_BACK;
5490   else
5491     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
5492                 _("this addressing mode requires base-register writeback"));
5493
5494   inst.instruction |= inst.operands[0].reg << 16;
5495   inst.instruction |= inst.operands[1].reg << 12;
5496
5497   count = inst.operands[1].imm << 1;
5498   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
5499     count += 1;
5500
5501   inst.instruction |= count;
5502 }
5503
5504 static void
5505 do_vfp_sp_ldstmia (void)
5506 {
5507   vfp_sp_ldstm (VFP_LDSTMIA);
5508 }
5509
5510 static void
5511 do_vfp_sp_ldstmdb (void)
5512 {
5513   vfp_sp_ldstm (VFP_LDSTMDB);
5514 }
5515
5516 static void
5517 do_vfp_dp_ldstmia (void)
5518 {
5519   vfp_dp_ldstm (VFP_LDSTMIA);
5520 }
5521
5522 static void
5523 do_vfp_dp_ldstmdb (void)
5524 {
5525   vfp_dp_ldstm (VFP_LDSTMDB);
5526 }
5527
5528 static void
5529 do_vfp_xp_ldstmia (void)
5530 {
5531   vfp_dp_ldstm (VFP_LDSTMIAX);
5532 }
5533
5534 static void
5535 do_vfp_xp_ldstmdb (void)
5536 {
5537   vfp_dp_ldstm (VFP_LDSTMDBX);
5538 }
5539 \f
5540 /* FPA instructions.  Also in a logical order.  */
5541
5542 static void
5543 do_fpa_cmp (void)
5544 {
5545   inst.instruction |= inst.operands[0].reg << 16;
5546   inst.instruction |= inst.operands[1].reg;
5547 }
5548
5549 static void
5550 do_fpa_ldmstm (void)
5551 {
5552   inst.instruction |= inst.operands[0].reg << 12;
5553   switch (inst.operands[1].imm)
5554     {
5555     case 1: inst.instruction |= CP_T_X;          break;
5556     case 2: inst.instruction |= CP_T_Y;          break;
5557     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
5558     case 4:                                      break;
5559     default: abort ();
5560     }
5561
5562   if (inst.instruction & (PRE_INDEX | INDEX_UP))
5563     {
5564       /* The instruction specified "ea" or "fd", so we can only accept
5565          [Rn]{!}.  The instruction does not really support stacking or
5566          unstacking, so we have to emulate these by setting appropriate
5567          bits and offsets.  */
5568       constraint (inst.reloc.exp.X_op != O_constant
5569                   || inst.reloc.exp.X_add_number != 0,
5570                   _("this instruction does not support indexing"));
5571
5572       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
5573         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
5574
5575       if (!(inst.instruction & INDEX_UP))
5576         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
5577
5578       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
5579         {
5580           inst.operands[2].preind = 0;
5581           inst.operands[2].postind = 1;
5582         }
5583     }
5584
5585   encode_arm_cp_address (2, TRUE, TRUE, 0);
5586 }
5587 \f
5588 /* iWMMXt instructions: strictly in alphabetical order.  */
5589
5590 static void
5591 do_iwmmxt_tandorc (void)
5592 {
5593   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
5594 }
5595
5596 static void
5597 do_iwmmxt_textrc (void)
5598 {
5599   inst.instruction |= inst.operands[0].reg << 12;
5600   inst.instruction |= inst.operands[1].imm;
5601 }
5602
5603 static void
5604 do_iwmmxt_textrm (void)
5605 {
5606   inst.instruction |= inst.operands[0].reg << 12;
5607   inst.instruction |= inst.operands[1].reg << 16;
5608   inst.instruction |= inst.operands[2].imm;
5609 }
5610
5611 static void
5612 do_iwmmxt_tinsr (void)
5613 {
5614   inst.instruction |= inst.operands[0].reg << 16;
5615   inst.instruction |= inst.operands[1].reg << 12;
5616   inst.instruction |= inst.operands[2].imm;
5617 }
5618
5619 static void
5620 do_iwmmxt_tmia (void)
5621 {
5622   inst.instruction |= inst.operands[0].reg << 5;
5623   inst.instruction |= inst.operands[1].reg;
5624   inst.instruction |= inst.operands[2].reg << 12;
5625 }
5626
5627 static void
5628 do_iwmmxt_waligni (void)
5629 {
5630   inst.instruction |= inst.operands[0].reg << 12;
5631   inst.instruction |= inst.operands[1].reg << 16;
5632   inst.instruction |= inst.operands[2].reg;
5633   inst.instruction |= inst.operands[3].imm << 20;
5634 }
5635
5636 static void
5637 do_iwmmxt_wmov (void)
5638 {
5639   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
5640   inst.instruction |= inst.operands[0].reg << 12;
5641   inst.instruction |= inst.operands[1].reg << 16;
5642   inst.instruction |= inst.operands[1].reg;
5643 }
5644
5645 static void
5646 do_iwmmxt_wldstbh (void)
5647 {
5648   int reloc;
5649   inst.instruction |= inst.operands[0].reg << 12;
5650   inst.reloc.exp.X_add_number *= 4;
5651   if (thumb_mode)
5652     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
5653   else
5654     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
5655   encode_arm_cp_address (1, TRUE, FALSE, reloc);
5656 }
5657
5658 static void
5659 do_iwmmxt_wldstw (void)
5660 {
5661   /* RIWR_RIWC clears .isreg for a control register.  */
5662   if (!inst.operands[0].isreg)
5663     {
5664       constraint (inst.cond != COND_ALWAYS, BAD_COND);
5665       inst.instruction |= 0xf0000000;
5666     }
5667
5668   inst.instruction |= inst.operands[0].reg << 12;
5669   encode_arm_cp_address (1, TRUE, TRUE, 0);
5670 }
5671
5672 static void
5673 do_iwmmxt_wldstd (void)
5674 {
5675   inst.instruction |= inst.operands[0].reg << 12;
5676   encode_arm_cp_address (1, TRUE, FALSE, 0);
5677 }
5678
5679 static void
5680 do_iwmmxt_wshufh (void)
5681 {
5682   inst.instruction |= inst.operands[0].reg << 12;
5683   inst.instruction |= inst.operands[1].reg << 16;
5684   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
5685   inst.instruction |= (inst.operands[2].imm & 0x0f);
5686 }
5687
5688 static void
5689 do_iwmmxt_wzero (void)
5690 {
5691   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
5692   inst.instruction |= inst.operands[0].reg;
5693   inst.instruction |= inst.operands[0].reg << 12;
5694   inst.instruction |= inst.operands[0].reg << 16;
5695 }
5696 \f
5697 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
5698    operations first, then control, shift, and load/store.  */
5699
5700 /* Insns like "foo X,Y,Z".  */
5701
5702 static void
5703 do_mav_triple (void)
5704 {
5705   inst.instruction |= inst.operands[0].reg << 16;
5706   inst.instruction |= inst.operands[1].reg;
5707   inst.instruction |= inst.operands[2].reg << 12;
5708 }
5709
5710 /* Insns like "foo W,X,Y,Z".
5711     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
5712
5713 static void
5714 do_mav_quad (void)
5715 {
5716   inst.instruction |= inst.operands[0].reg << 5;
5717   inst.instruction |= inst.operands[1].reg << 12;
5718   inst.instruction |= inst.operands[2].reg << 16;
5719   inst.instruction |= inst.operands[3].reg;
5720 }
5721
5722 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
5723 static void
5724 do_mav_dspsc (void)
5725 {
5726   inst.instruction |= inst.operands[1].reg << 12;
5727 }
5728
5729 /* Maverick shift immediate instructions.
5730    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
5731    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
5732
5733 static void
5734 do_mav_shift (void)
5735 {
5736   int imm = inst.operands[2].imm;
5737
5738   inst.instruction |= inst.operands[0].reg << 12;
5739   inst.instruction |= inst.operands[1].reg << 16;
5740
5741   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
5742      Bits 5-7 of the insn should have bits 4-6 of the immediate.
5743      Bit 4 should be 0.  */
5744   imm = (imm & 0xf) | ((imm & 0x70) << 1);
5745
5746   inst.instruction |= imm;
5747 }
5748 \f
5749 /* XScale instructions.  Also sorted arithmetic before move.  */
5750
5751 /* Xscale multiply-accumulate (argument parse)
5752      MIAcc   acc0,Rm,Rs
5753      MIAPHcc acc0,Rm,Rs
5754      MIAxycc acc0,Rm,Rs.  */
5755
5756 static void
5757 do_xsc_mia (void)
5758 {
5759   inst.instruction |= inst.operands[1].reg;
5760   inst.instruction |= inst.operands[2].reg << 12;
5761 }
5762
5763 /* Xscale move-accumulator-register (argument parse)
5764
5765      MARcc   acc0,RdLo,RdHi.  */
5766
5767 static void
5768 do_xsc_mar (void)
5769 {
5770   inst.instruction |= inst.operands[1].reg << 12;
5771   inst.instruction |= inst.operands[2].reg << 16;
5772 }
5773
5774 /* Xscale move-register-accumulator (argument parse)
5775
5776      MRAcc   RdLo,RdHi,acc0.  */
5777
5778 static void
5779 do_xsc_mra (void)
5780 {
5781   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
5782   inst.instruction |= inst.operands[0].reg << 12;
5783   inst.instruction |= inst.operands[1].reg << 16;
5784 }
5785 \f
5786 /* Encoding functions relevant only to Thumb.  */
5787
5788 /* inst.operands[i] is a shifted-register operand; encode
5789    it into inst.instruction in the format used by Thumb32.  */
5790
5791 static void
5792 encode_thumb32_shifted_operand (int i)
5793 {
5794   unsigned int value = inst.reloc.exp.X_add_number;
5795   unsigned int shift = inst.operands[i].shift_kind;
5796
5797   constraint (inst.operands[i].immisreg,
5798               _("shift by register not allowed in thumb mode"));
5799   inst.instruction |= inst.operands[i].reg;
5800   if (shift == SHIFT_RRX)
5801     inst.instruction |= SHIFT_ROR << 4;
5802   else
5803     {
5804       constraint (inst.reloc.exp.X_op != O_constant,
5805                   _("expression too complex"));
5806
5807       constraint (value > 32
5808                   || (value == 32 && (shift == SHIFT_LSL
5809                                       || shift == SHIFT_ROR)),
5810                   _("shift expression is too large"));
5811
5812       if (value == 0)
5813         shift = SHIFT_LSL;
5814       else if (value == 32)
5815         value = 0;
5816
5817       inst.instruction |= shift << 4;
5818       inst.instruction |= (value & 0x1c) << 10;
5819       inst.instruction |= (value & 0x03) << 6;
5820     }
5821 }
5822
5823
5824 /* inst.operands[i] was set up by parse_address.  Encode it into a
5825    Thumb32 format load or store instruction.  Reject forms that cannot
5826    be used with such instructions.  If is_t is true, reject forms that
5827    cannot be used with a T instruction; if is_d is true, reject forms
5828    that cannot be used with a D instruction.  */
5829
5830 static void
5831 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
5832 {
5833   bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
5834
5835   constraint (!inst.operands[i].isreg,
5836               _("Thumb does not support the ldr =N pseudo-operation"));
5837
5838   inst.instruction |= inst.operands[i].reg << 16;
5839   if (inst.operands[i].immisreg)
5840     {
5841       constraint (is_pc, _("cannot use register index with PC-relative addressing"));
5842       constraint (is_t || is_d, _("cannot use register index with this instruction"));
5843       constraint (inst.operands[i].negative,
5844                   _("Thumb does not support negative register indexing"));
5845       constraint (inst.operands[i].postind,
5846                   _("Thumb does not support register post-indexing"));
5847       constraint (inst.operands[i].writeback,
5848                   _("Thumb does not support register indexing with writeback"));
5849       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
5850                   _("Thumb supports only LSL in shifted register indexing"));
5851
5852       inst.instruction |= inst.operands[1].imm;
5853       if (inst.operands[i].shifted)
5854         {
5855           constraint (inst.reloc.exp.X_op != O_constant,
5856                       _("expression too complex"));
5857           constraint (inst.reloc.exp.X_add_number < 0
5858                       || inst.reloc.exp.X_add_number > 3,
5859                       _("shift out of range"));
5860           inst.instruction |= inst.reloc.exp.X_add_number << 4;
5861         }
5862       inst.reloc.type = BFD_RELOC_UNUSED;
5863     }
5864   else if (inst.operands[i].preind)
5865     {
5866       constraint (is_pc && inst.operands[i].writeback,
5867                   _("cannot use writeback with PC-relative addressing"));
5868       constraint (is_t && inst.operands[1].writeback,
5869                   _("cannot use writeback with this instruction"));
5870
5871       if (is_d)
5872         {
5873           inst.instruction |= 0x01000000;
5874           if (inst.operands[i].writeback)
5875             inst.instruction |= 0x00200000;
5876         }
5877       else
5878         {
5879           inst.instruction |= 0x00000c00;
5880           if (inst.operands[i].writeback)
5881             inst.instruction |= 0x00000100;
5882         }
5883       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
5884     }
5885   else if (inst.operands[i].postind)
5886     {
5887       assert (inst.operands[i].writeback);
5888       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
5889       constraint (is_t, _("cannot use post-indexing with this instruction"));
5890
5891       if (is_d)
5892         inst.instruction |= 0x00200000;
5893       else
5894         inst.instruction |= 0x00000900;
5895       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
5896     }
5897   else /* unindexed - only for coprocessor */
5898     inst.error = _("instruction does not accept unindexed addressing");
5899 }
5900
5901 /* Table of Thumb instructions which exist in both 16- and 32-bit
5902    encodings (the latter only in post-V6T2 cores).  The index is the
5903    value used in the insns table below.  When there is more than one
5904    possible 16-bit encoding for the instruction, this table always
5905    holds variant (1).
5906    Also contains several pseudo-instructions used during relaxation.  */
5907 #define T16_32_TAB                              \
5908   X(adc,   4140, eb400000),                     \
5909   X(adcs,  4140, eb500000),                     \
5910   X(add,   1c00, eb000000),                     \
5911   X(adds,  1c00, eb100000),                     \
5912   X(addi,  0000, f1000000),                     \
5913   X(addis, 0000, f1100000),                     \
5914   X(add_pc,000f, f20f0000),                     \
5915   X(add_sp,000d, f10d0000),                     \
5916   X(adr,   000f, f20f0000),                     \
5917   X(and,   4000, ea000000),                     \
5918   X(ands,  4000, ea100000),                     \
5919   X(asr,   1000, fa40f000),                     \
5920   X(asrs,  1000, fa50f000),                     \
5921   X(b,     e000, f000b000),                     \
5922   X(bcond, d000, f0008000),                     \
5923   X(bic,   4380, ea200000),                     \
5924   X(bics,  4380, ea300000),                     \
5925   X(cmn,   42c0, eb100f00),                     \
5926   X(cmp,   2800, ebb00f00),                     \
5927   X(cpsie, b660, f3af8400),                     \
5928   X(cpsid, b670, f3af8600),                     \
5929   X(cpy,   4600, ea4f0000),                     \
5930   X(dec_sp,80dd, f1bd0d00),                     \
5931   X(eor,   4040, ea800000),                     \
5932   X(eors,  4040, ea900000),                     \
5933   X(inc_sp,00dd, f10d0d00),                     \
5934   X(ldmia, c800, e8900000),                     \
5935   X(ldr,   6800, f8500000),                     \
5936   X(ldrb,  7800, f8100000),                     \
5937   X(ldrh,  8800, f8300000),                     \
5938   X(ldrsb, 5600, f9100000),                     \
5939   X(ldrsh, 5e00, f9300000),                     \
5940   X(ldr_pc,4800, f85f0000),                     \
5941   X(ldr_pc2,4800, f85f0000),                    \
5942   X(ldr_sp,9800, f85d0000),                     \
5943   X(lsl,   0000, fa00f000),                     \
5944   X(lsls,  0000, fa10f000),                     \
5945   X(lsr,   0800, fa20f000),                     \
5946   X(lsrs,  0800, fa30f000),                     \
5947   X(mov,   2000, ea4f0000),                     \
5948   X(movs,  2000, ea5f0000),                     \
5949   X(mul,   4340, fb00f000),                     \
5950   X(muls,  4340, ffffffff), /* no 32b muls */   \
5951   X(mvn,   43c0, ea6f0000),                     \
5952   X(mvns,  43c0, ea7f0000),                     \
5953   X(neg,   4240, f1c00000), /* rsb #0 */        \
5954   X(negs,  4240, f1d00000), /* rsbs #0 */       \
5955   X(orr,   4300, ea400000),                     \
5956   X(orrs,  4300, ea500000),                     \
5957   X(pop,   bc00, e8bd0000), /* ldmia sp!,... */ \
5958   X(push,  b400, e92d0000), /* stmdb sp!,... */ \
5959   X(rev,   ba00, fa90f080),                     \
5960   X(rev16, ba40, fa90f090),                     \
5961   X(revsh, bac0, fa90f0b0),                     \
5962   X(ror,   41c0, fa60f000),                     \
5963   X(rors,  41c0, fa70f000),                     \
5964   X(sbc,   4180, eb600000),                     \
5965   X(sbcs,  4180, eb700000),                     \
5966   X(stmia, c000, e8800000),                     \
5967   X(str,   6000, f8400000),                     \
5968   X(strb,  7000, f8000000),                     \
5969   X(strh,  8000, f8200000),                     \
5970   X(str_sp,9000, f84d0000),                     \
5971   X(sub,   1e00, eba00000),                     \
5972   X(subs,  1e00, ebb00000),                     \
5973   X(subi,  8000, f1a00000),                     \
5974   X(subis, 8000, f1b00000),                     \
5975   X(sxtb,  b240, fa4ff080),                     \
5976   X(sxth,  b200, fa0ff080),                     \
5977   X(tst,   4200, ea100f00),                     \
5978   X(uxtb,  b2c0, fa5ff080),                     \
5979   X(uxth,  b280, fa1ff080),                     \
5980   X(nop,   bf00, f3af8000),                     \
5981   X(yield, bf10, f3af8001),                     \
5982   X(wfe,   bf20, f3af8002),                     \
5983   X(wfi,   bf30, f3af8003),                     \
5984   X(sev,   bf40, f3af9004), /* typo, 8004? */
5985
5986 /* To catch errors in encoding functions, the codes are all offset by
5987    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
5988    as 16-bit instructions.  */
5989 #define X(a,b,c) T_MNEM_##a
5990 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
5991 #undef X
5992
5993 #define X(a,b,c) 0x##b
5994 static const unsigned short thumb_op16[] = { T16_32_TAB };
5995 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
5996 #undef X
5997
5998 #define X(a,b,c) 0x##c
5999 static const unsigned int thumb_op32[] = { T16_32_TAB };
6000 #define THUMB_OP32(n) (thumb_op32[(n) - (T16_32_OFFSET + 1)])
6001 #define THUMB_SETS_FLAGS(n) (THUMB_OP32 (n) & 0x00100000)
6002 #undef X
6003 #undef T16_32_TAB
6004
6005 /* Thumb instruction encoders, in alphabetical order.  */
6006
6007 /* ADDW or SUBW.  */
6008 static void
6009 do_t_add_sub_w (void)
6010 {
6011   int Rd, Rn;
6012
6013   Rd = inst.operands[0].reg;
6014   Rn = inst.operands[1].reg;
6015
6016   constraint (Rd == 15, _("PC not allowed as destination"));
6017   inst.instruction |= (Rn << 16) | (Rd << 8);
6018   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
6019 }
6020
6021 /* Parse an add or subtract instruction.  We get here with inst.instruction
6022    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
6023
6024 static void
6025 do_t_add_sub (void)
6026 {
6027   int Rd, Rs, Rn;
6028
6029   Rd = inst.operands[0].reg;
6030   Rs = (inst.operands[1].present
6031         ? inst.operands[1].reg    /* Rd, Rs, foo */
6032         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
6033
6034   if (unified_syntax)
6035     {
6036       bfd_boolean flags;
6037       bfd_boolean narrow;
6038       int opcode;
6039
6040       flags = (inst.instruction == T_MNEM_adds
6041                || inst.instruction == T_MNEM_subs);
6042       if (flags)
6043         narrow = (current_it_mask == 0);
6044       else
6045         narrow = (current_it_mask != 0);
6046       if (!inst.operands[2].isreg)
6047         {
6048           opcode = 0;
6049           if (inst.size_req != 4)
6050             {
6051               int add;
6052
6053               add = (inst.instruction == T_MNEM_add
6054                      || inst.instruction == T_MNEM_adds);
6055               /* Attempt to use a narrow opcode, with relaxation if
6056                  appropriate.  */
6057               if (Rd == REG_SP && Rs == REG_SP && !flags)
6058                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
6059               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
6060                 opcode = T_MNEM_add_sp;
6061               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
6062                 opcode = T_MNEM_add_pc;
6063               else if (Rd <= 7 && Rs <= 7 && narrow)
6064                 {
6065                   if (flags)
6066                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
6067                   else
6068                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
6069                 }
6070               if (opcode)
6071                 {
6072                   inst.instruction = THUMB_OP16(opcode);
6073                   inst.instruction |= (Rd << 4) | Rs;
6074                   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
6075                   if (inst.size_req != 2)
6076                     inst.relax = opcode;
6077                 }
6078               else
6079                 constraint (inst.size_req == 2, BAD_HIREG);
6080             }
6081           if (inst.size_req == 4
6082               || (inst.size_req != 2 && !opcode))
6083             {
6084               /* ??? Convert large immediates to addw/subw.  */
6085               inst.instruction = THUMB_OP32 (inst.instruction);
6086               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
6087               inst.instruction |= inst.operands[0].reg << 8;
6088               inst.instruction |= inst.operands[1].reg << 16;
6089               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
6090             }
6091         }
6092       else
6093         {
6094           Rn = inst.operands[2].reg;
6095           /* See if we can do this with a 16-bit instruction.  */
6096           if (!inst.operands[2].shifted && inst.size_req != 4)
6097             {
6098               if (Rd > 7 || Rs > 7 || Rn > 7)
6099                 narrow = FALSE;
6100
6101               if (narrow)
6102                 {
6103                   inst.instruction = ((inst.instruction == T_MNEM_adds
6104                                        || inst.instruction == T_MNEM_add)
6105                                       ? T_OPCODE_ADD_R3
6106                                       : T_OPCODE_SUB_R3);
6107                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
6108                   return;
6109                 }
6110
6111               if (inst.instruction == T_MNEM_add)
6112                 {
6113                   if (Rd == Rs)
6114                     {
6115                       inst.instruction = T_OPCODE_ADD_HI;
6116                       inst.instruction |= (Rd & 8) << 4;
6117                       inst.instruction |= (Rd & 7);
6118                       inst.instruction |= Rn << 3;
6119                       return;
6120                     }
6121                   /* ... because addition is commutative! */
6122                   else if (Rd == Rn)
6123                     {
6124                       inst.instruction = T_OPCODE_ADD_HI;
6125                       inst.instruction |= (Rd & 8) << 4;
6126                       inst.instruction |= (Rd & 7);
6127                       inst.instruction |= Rs << 3;
6128                       return;
6129                     }
6130                 }
6131             }
6132           /* If we get here, it can't be done in 16 bits.  */
6133           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
6134                       _("shift must be constant"));
6135           inst.instruction = THUMB_OP32 (inst.instruction);
6136           inst.instruction |= Rd << 8;
6137           inst.instruction |= Rs << 16;
6138           encode_thumb32_shifted_operand (2);
6139         }
6140     }
6141   else
6142     {
6143       constraint (inst.instruction == T_MNEM_adds
6144                   || inst.instruction == T_MNEM_subs,
6145                   BAD_THUMB32);
6146
6147       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
6148         {
6149           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
6150                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
6151                       BAD_HIREG);
6152
6153           inst.instruction = (inst.instruction == T_MNEM_add
6154                               ? 0x0000 : 0x8000);
6155           inst.instruction |= (Rd << 4) | Rs;
6156           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
6157           return;
6158         }
6159
6160       Rn = inst.operands[2].reg;
6161       constraint (inst.operands[2].shifted, _("unshifted register required"));
6162
6163       /* We now have Rd, Rs, and Rn set to registers.  */
6164       if (Rd > 7 || Rs > 7 || Rn > 7)
6165         {
6166           /* Can't do this for SUB.      */
6167           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
6168           inst.instruction = T_OPCODE_ADD_HI;
6169           inst.instruction |= (Rd & 8) << 4;
6170           inst.instruction |= (Rd & 7);
6171           if (Rs == Rd)
6172             inst.instruction |= Rn << 3;
6173           else if (Rn == Rd)
6174             inst.instruction |= Rs << 3;
6175           else
6176             constraint (1, _("dest must overlap one source register"));
6177         }
6178       else
6179         {
6180           inst.instruction = (inst.instruction == T_MNEM_add
6181                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
6182           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
6183         }
6184     }
6185 }
6186
6187 static void
6188 do_t_adr (void)
6189 {
6190   if (unified_syntax && inst.size_req == 0 && inst.operands[0].reg <= 7)
6191     {
6192       /* Defer to section relaxation.  */
6193       inst.relax = inst.instruction;
6194       inst.instruction = THUMB_OP16 (inst.instruction);
6195       inst.instruction |= inst.operands[0].reg << 4;
6196     }
6197   else if (unified_syntax && inst.size_req != 2)
6198     {
6199       /* Generate a 32-bit opcode.  */
6200       inst.instruction = THUMB_OP32 (inst.instruction);
6201       inst.instruction |= inst.operands[0].reg << 8;
6202       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
6203       inst.reloc.pc_rel = 1;
6204     }
6205   else
6206     {
6207       /* Generate a 16-bit opcode.  */
6208       inst.instruction = THUMB_OP16 (inst.instruction);
6209       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
6210       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
6211       inst.reloc.pc_rel = 1;
6212
6213       inst.instruction |= inst.operands[0].reg << 4;
6214     }
6215 }
6216
6217 /* Arithmetic instructions for which there is just one 16-bit
6218    instruction encoding, and it allows only two low registers.
6219    For maximal compatibility with ARM syntax, we allow three register
6220    operands even when Thumb-32 instructions are not available, as long
6221    as the first two are identical.  For instance, both "sbc r0,r1" and
6222    "sbc r0,r0,r1" are allowed.  */
6223 static void
6224 do_t_arit3 (void)
6225 {
6226   int Rd, Rs, Rn;
6227
6228   Rd = inst.operands[0].reg;
6229   Rs = (inst.operands[1].present
6230         ? inst.operands[1].reg    /* Rd, Rs, foo */
6231         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
6232   Rn = inst.operands[2].reg;
6233
6234   if (unified_syntax)
6235     {
6236       if (!inst.operands[2].isreg)
6237         {
6238           /* For an immediate, we always generate a 32-bit opcode;
6239              section relaxation will shrink it later if possible.  */
6240           inst.instruction = THUMB_OP32 (inst.instruction);
6241           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
6242           inst.instruction |= Rd << 8;
6243           inst.instruction |= Rs << 16;
6244           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
6245         }
6246       else
6247         {
6248           bfd_boolean narrow;
6249
6250           /* See if we can do this with a 16-bit instruction.  */
6251           if (THUMB_SETS_FLAGS (inst.instruction))
6252             narrow = current_it_mask == 0;
6253           else
6254             narrow = current_it_mask != 0;
6255
6256           if (Rd > 7 || Rn > 7 || Rs > 7)
6257             narrow = FALSE;
6258           if (inst.operands[2].shifted)
6259             narrow = FALSE;
6260           if (inst.size_req == 4)
6261             narrow = FALSE;
6262
6263           if (narrow
6264               && Rd == Rs)
6265             {
6266               inst.instruction = THUMB_OP16 (inst.instruction);
6267               inst.instruction |= Rd;
6268               inst.instruction |= Rn << 3;
6269               return;
6270             }
6271
6272           /* If we get here, it can't be done in 16 bits.  */
6273           constraint (inst.operands[2].shifted
6274                       && inst.operands[2].immisreg,
6275                       _("shift must be constant"));
6276           inst.instruction = THUMB_OP32 (inst.instruction);
6277           inst.instruction |= Rd << 8;
6278           inst.instruction |= Rs << 16;
6279           encode_thumb32_shifted_operand (2);
6280         }
6281     }
6282   else
6283     {
6284       /* On its face this is a lie - the instruction does set the
6285          flags.  However, the only supported mnemonic in this mode
6286          says it doesn't.  */
6287       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
6288
6289       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
6290                   _("unshifted register required"));
6291       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
6292       constraint (Rd != Rs,
6293                   _("dest and source1 must be the same register"));
6294
6295       inst.instruction = THUMB_OP16 (inst.instruction);
6296       inst.instruction |= Rd;
6297       inst.instruction |= Rn << 3;
6298     }
6299 }
6300
6301 /* Similarly, but for instructions where the arithmetic operation is
6302    commutative, so we can allow either of them to be different from
6303    the destination operand in a 16-bit instruction.  For instance, all
6304    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
6305    accepted.  */
6306 static void
6307 do_t_arit3c (void)
6308 {
6309   int Rd, Rs, Rn;
6310
6311   Rd = inst.operands[0].reg;
6312   Rs = (inst.operands[1].present
6313         ? inst.operands[1].reg    /* Rd, Rs, foo */
6314         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
6315   Rn = inst.operands[2].reg;
6316
6317   if (unified_syntax)
6318     {
6319       if (!inst.operands[2].isreg)
6320         {
6321           /* For an immediate, we always generate a 32-bit opcode;
6322              section relaxation will shrink it later if possible.  */
6323           inst.instruction = THUMB_OP32 (inst.instruction);
6324           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
6325           inst.instruction |= Rd << 8;
6326           inst.instruction |= Rs << 16;
6327           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
6328         }
6329       else
6330         {
6331           bfd_boolean narrow;
6332
6333           /* See if we can do this with a 16-bit instruction.  */
6334           if (THUMB_SETS_FLAGS (inst.instruction))
6335             narrow = current_it_mask == 0;
6336           else
6337             narrow = current_it_mask != 0;
6338
6339           if (Rd > 7 || Rn > 7 || Rs > 7)
6340             narrow = FALSE;
6341           if (inst.operands[2].shifted)
6342             narrow = FALSE;
6343           if (inst.size_req == 4)
6344             narrow = FALSE;
6345
6346           if (narrow)
6347             {
6348               if (Rd == Rs)
6349                 {
6350                   inst.instruction = THUMB_OP16 (inst.instruction);
6351                   inst.instruction |= Rd;
6352                   inst.instruction |= Rn << 3;
6353                   return;
6354                 }
6355               if (Rd == Rn)
6356                 {
6357                   inst.instruction = THUMB_OP16 (inst.instruction);
6358                   inst.instruction |= Rd;
6359                   inst.instruction |= Rs << 3;
6360                   return;
6361                 }
6362             }
6363
6364           /* If we get here, it can't be done in 16 bits.  */
6365           constraint (inst.operands[2].shifted
6366                       && inst.operands[2].immisreg,
6367                       _("shift must be constant"));
6368           inst.instruction = THUMB_OP32 (inst.instruction);
6369           inst.instruction |= Rd << 8;
6370           inst.instruction |= Rs << 16;
6371           encode_thumb32_shifted_operand (2);
6372         }
6373     }
6374   else
6375     {
6376       /* On its face this is a lie - the instruction does set the
6377          flags.  However, the only supported mnemonic in this mode
6378          says it doesn't.  */
6379       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
6380
6381       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
6382                   _("unshifted register required"));
6383       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
6384
6385       inst.instruction = THUMB_OP16 (inst.instruction);
6386       inst.instruction |= Rd;
6387
6388       if (Rd == Rs)
6389         inst.instruction |= Rn << 3;
6390       else if (Rd == Rn)
6391         inst.instruction |= Rs << 3;
6392       else
6393         constraint (1, _("dest must overlap one source register"));
6394     }
6395 }
6396
6397 static void
6398 do_t_bfc (void)
6399 {
6400   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
6401   constraint (msb > 32, _("bit-field extends past end of register"));
6402   /* The instruction encoding stores the LSB and MSB,
6403      not the LSB and width.  */
6404   inst.instruction |= inst.operands[0].reg << 8;
6405   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
6406   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
6407   inst.instruction |= msb - 1;
6408 }
6409
6410 static void
6411 do_t_bfi (void)
6412 {
6413   unsigned int msb;
6414
6415   /* #0 in second position is alternative syntax for bfc, which is
6416      the same instruction but with REG_PC in the Rm field.  */
6417   if (!inst.operands[1].isreg)
6418     inst.operands[1].reg = REG_PC;
6419
6420   msb = inst.operands[2].imm + inst.operands[3].imm;
6421   constraint (msb > 32, _("bit-field extends past end of register"));
6422   /* The instruction encoding stores the LSB and MSB,
6423      not the LSB and width.  */
6424   inst.instruction |= inst.operands[0].reg << 8;
6425   inst.instruction |= inst.operands[1].reg << 16;
6426   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
6427   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
6428   inst.instruction |= msb - 1;
6429 }
6430
6431 static void
6432 do_t_bfx (void)
6433 {
6434   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
6435               _("bit-field extends past end of register"));
6436   inst.instruction |= inst.operands[0].reg << 8;
6437   inst.instruction |= inst.operands[1].reg << 16;
6438   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
6439   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
6440   inst.instruction |= inst.operands[3].imm - 1;
6441 }
6442
6443 /* ARM V5 Thumb BLX (argument parse)
6444         BLX <target_addr>       which is BLX(1)
6445         BLX <Rm>                which is BLX(2)
6446    Unfortunately, there are two different opcodes for this mnemonic.
6447    So, the insns[].value is not used, and the code here zaps values
6448         into inst.instruction.
6449
6450    ??? How to take advantage of the additional two bits of displacement
6451    available in Thumb32 mode?  Need new relocation?  */
6452
6453 static void
6454 do_t_blx (void)
6455 {
6456   if (inst.operands[0].isreg)
6457     /* We have a register, so this is BLX(2).  */
6458     inst.instruction |= inst.operands[0].reg << 3;
6459   else
6460     {
6461       /* No register.  This must be BLX(1).  */
6462       inst.instruction = 0xf000e800;
6463 #ifdef OBJ_ELF
6464       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
6465         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
6466       else
6467 #endif
6468         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BLX;
6469       inst.reloc.pc_rel = 1;
6470     }
6471 }
6472
6473 static void
6474 do_t_branch (void)
6475 {
6476   int opcode;
6477   if (inst.cond != COND_ALWAYS)
6478     opcode = T_MNEM_bcond;
6479   else
6480     opcode = inst.instruction;
6481
6482   if (unified_syntax && inst.size_req == 4)
6483     {
6484       inst.instruction = THUMB_OP32(opcode);
6485       if (inst.cond == COND_ALWAYS)
6486         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH25;
6487       else
6488         {
6489           assert (inst.cond != 0xF);
6490           inst.instruction |= inst.cond << 22;
6491           inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH20;
6492         }
6493     }
6494   else
6495     {
6496       inst.instruction = THUMB_OP16(opcode);
6497       if (inst.cond == COND_ALWAYS)
6498         inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH12;
6499       else
6500         {
6501           inst.instruction |= inst.cond << 8;
6502           inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH9;
6503         }
6504       /* Allow section relaxation.  */
6505       if (unified_syntax && inst.size_req != 2)
6506         inst.relax = opcode;
6507     }
6508
6509   inst.reloc.pc_rel = 1;
6510 }
6511
6512 static void
6513 do_t_bkpt (void)
6514 {
6515   if (inst.operands[0].present)
6516     {
6517       constraint (inst.operands[0].imm > 255,
6518                   _("immediate value out of range"));
6519       inst.instruction |= inst.operands[0].imm;
6520     }
6521 }
6522
6523 static void
6524 do_t_branch23 (void)
6525 {
6526   inst.reloc.type   = BFD_RELOC_THUMB_PCREL_BRANCH23;
6527   inst.reloc.pc_rel = 1;
6528
6529   /* If the destination of the branch is a defined symbol which does not have
6530      the THUMB_FUNC attribute, then we must be calling a function which has
6531      the (interfacearm) attribute.  We look for the Thumb entry point to that
6532      function and change the branch to refer to that function instead.  */
6533   if (   inst.reloc.exp.X_op == O_symbol
6534       && inst.reloc.exp.X_add_symbol != NULL
6535       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
6536       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
6537     inst.reloc.exp.X_add_symbol =
6538       find_real_start (inst.reloc.exp.X_add_symbol);
6539 }
6540
6541 static void
6542 do_t_bx (void)
6543 {
6544   inst.instruction |= inst.operands[0].reg << 3;
6545   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
6546      should cause the alignment to be checked once it is known.  This is
6547      because BX PC only works if the instruction is word aligned.  */
6548 }
6549
6550 static void
6551 do_t_bxj (void)
6552 {
6553   if (inst.operands[0].reg == REG_PC)
6554     as_tsktsk (_("use of r15 in bxj is not really useful"));
6555
6556   inst.instruction |= inst.operands[0].reg << 16;
6557 }
6558
6559 static void
6560 do_t_clz (void)
6561 {
6562   inst.instruction |= inst.operands[0].reg << 8;
6563   inst.instruction |= inst.operands[1].reg << 16;
6564   inst.instruction |= inst.operands[1].reg;
6565 }
6566
6567 static void
6568 do_t_cpsi (void)
6569 {
6570   if (unified_syntax
6571       && (inst.operands[1].present || inst.size_req == 4))
6572     {
6573       unsigned int imod = (inst.instruction & 0x0030) >> 4;
6574       inst.instruction = 0xf3af8000;
6575       inst.instruction |= imod << 9;
6576       inst.instruction |= inst.operands[0].imm << 5;
6577       if (inst.operands[1].present)
6578         inst.instruction |= 0x100 | inst.operands[1].imm;
6579     }
6580   else
6581     {
6582       constraint (inst.operands[1].present,
6583                   _("Thumb does not support the 2-argument "
6584                     "form of this instruction"));
6585       inst.instruction |= inst.operands[0].imm;
6586     }
6587 }
6588
6589 /* THUMB CPY instruction (argument parse).  */
6590
6591 static void
6592 do_t_cpy (void)
6593 {
6594   if (inst.size_req == 4)
6595     {
6596       inst.instruction = THUMB_OP32 (T_MNEM_mov);
6597       inst.instruction |= inst.operands[0].reg << 8;
6598       inst.instruction |= inst.operands[1].reg;
6599     }
6600   else
6601     {
6602       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
6603       inst.instruction |= (inst.operands[0].reg & 0x7);
6604       inst.instruction |= inst.operands[1].reg << 3;
6605     }
6606 }
6607
6608 static void
6609 do_t_czb (void)
6610 {
6611   constraint (inst.operands[0].reg > 7, BAD_HIREG);
6612   inst.instruction |= inst.operands[0].reg;
6613   inst.reloc.pc_rel = 1;
6614   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
6615 }
6616
6617 static void
6618 do_t_hint (void)
6619 {
6620   if (unified_syntax && inst.size_req == 4)
6621     inst.instruction = THUMB_OP32 (inst.instruction);
6622   else
6623     inst.instruction = THUMB_OP16 (inst.instruction);
6624 }
6625
6626 static void
6627 do_t_it (void)
6628 {
6629   unsigned int cond = inst.operands[0].imm;
6630
6631   current_it_mask = (inst.instruction & 0xf) | 0x10;
6632   current_cc = cond;
6633
6634   /* If the condition is a negative condition, invert the mask.  */
6635   if ((cond & 0x1) == 0x0)
6636     {
6637       unsigned int mask = inst.instruction & 0x000f;
6638
6639       if ((mask & 0x7) == 0)
6640         /* no conversion needed */;
6641       else if ((mask & 0x3) == 0)
6642         mask ^= 0x8;
6643       else if ((mask & 0x1) == 0)
6644         mask ^= 0xC;
6645       else
6646         mask ^= 0xE;
6647
6648       inst.instruction &= 0xfff0;
6649       inst.instruction |= mask;
6650     }
6651
6652   inst.instruction |= cond << 4;
6653 }
6654
6655 static void
6656 do_t_ldmstm (void)
6657 {
6658   /* This really doesn't seem worth it.  */
6659   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
6660               _("expression too complex"));
6661   constraint (inst.operands[1].writeback,
6662               _("Thumb load/store multiple does not support {reglist}^"));
6663
6664   if (unified_syntax)
6665     {
6666       /* See if we can use a 16-bit instruction.  */
6667       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
6668           && inst.size_req != 4
6669           && inst.operands[0].reg <= 7
6670           && !(inst.operands[1].imm & ~0xff)
6671           && (inst.instruction == T_MNEM_stmia
6672               ? inst.operands[0].writeback
6673               : (inst.operands[0].writeback
6674                  == !(inst.operands[1].imm & (1 << inst.operands[0].reg)))))
6675         {
6676           if (inst.instruction == T_MNEM_stmia
6677               && (inst.operands[1].imm & (1 << inst.operands[0].reg))
6678               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
6679             as_warn (_("value stored for r%d is UNPREDICTABLE"),
6680                      inst.operands[0].reg);
6681
6682           inst.instruction = THUMB_OP16 (inst.instruction);
6683           inst.instruction |= inst.operands[0].reg << 8;
6684           inst.instruction |= inst.operands[1].imm;
6685         }
6686       else
6687         {
6688           if (inst.operands[1].imm & (1 << 13))
6689             as_warn (_("SP should not be in register list"));
6690           if (inst.instruction == T_MNEM_stmia)
6691             {
6692               if (inst.operands[1].imm & (1 << 15))
6693                 as_warn (_("PC should not be in register list"));
6694               if (inst.operands[1].imm & (1 << inst.operands[0].reg))
6695                 as_warn (_("value stored for r%d is UNPREDICTABLE"),
6696                          inst.operands[0].reg);
6697             }
6698           else
6699             {
6700               if (inst.operands[1].imm & (1 << 14)
6701                   && inst.operands[1].imm & (1 << 15))
6702                 as_warn (_("LR and PC should not both be in register list"));
6703               if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
6704                   && inst.operands[0].writeback)
6705                 as_warn (_("base register should not be in register list "
6706                            "when written back"));
6707             }
6708           if (inst.instruction < 0xffff)
6709             inst.instruction = THUMB_OP32 (inst.instruction);
6710           inst.instruction |= inst.operands[0].reg << 16;
6711           inst.instruction |= inst.operands[1].imm;
6712           if (inst.operands[0].writeback)
6713             inst.instruction |= WRITE_BACK;
6714         }
6715     }
6716   else
6717     {
6718       constraint (inst.operands[0].reg > 7
6719                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
6720       if (inst.instruction == T_MNEM_stmia)
6721         {
6722           if (!inst.operands[0].writeback)
6723             as_warn (_("this instruction will write back the base register"));
6724           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
6725               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
6726             as_warn (_("value stored for r%d is UNPREDICTABLE"),
6727                      inst.operands[0].reg);
6728         }
6729       else
6730         {
6731           if (!inst.operands[0].writeback
6732               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
6733             as_warn (_("this instruction will write back the base register"));
6734           else if (inst.operands[0].writeback
6735                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
6736             as_warn (_("this instruction will not write back the base register"));
6737         }
6738
6739       inst.instruction = THUMB_OP16 (inst.instruction);
6740       inst.instruction |= inst.operands[0].reg << 8;
6741       inst.instruction |= inst.operands[1].imm;
6742     }
6743 }
6744
6745 static void
6746 do_t_ldrex (void)
6747 {
6748   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
6749               || inst.operands[1].postind || inst.operands[1].writeback
6750               || inst.operands[1].immisreg || inst.operands[1].shifted
6751               || inst.operands[1].negative,
6752               BAD_ADDR_MODE);
6753
6754   inst.instruction |= inst.operands[0].reg << 12;
6755   inst.instruction |= inst.operands[1].reg << 16;
6756   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
6757 }
6758
6759 static void
6760 do_t_ldrexd (void)
6761 {
6762   if (!inst.operands[1].present)
6763     {
6764       constraint (inst.operands[0].reg == REG_LR,
6765                   _("r14 not allowed as first register "
6766                     "when second register is omitted"));
6767       inst.operands[1].reg = inst.operands[0].reg + 1;
6768     }
6769   constraint (inst.operands[0].reg == inst.operands[1].reg,
6770               BAD_OVERLAP);
6771
6772   inst.instruction |= inst.operands[0].reg << 12;
6773   inst.instruction |= inst.operands[1].reg << 8;
6774   inst.instruction |= inst.operands[2].reg << 16;
6775 }
6776
6777 static void
6778 do_t_ldst (void)
6779 {
6780   unsigned long opcode;
6781   int Rn;
6782
6783   opcode = inst.instruction;
6784   if (unified_syntax)
6785     {
6786       if (inst.operands[1].isreg
6787           && !inst.operands[1].writeback
6788           && !inst.operands[1].shifted && !inst.operands[1].postind
6789           && !inst.operands[1].negative && inst.operands[0].reg <= 7
6790           && opcode <= 0xffff
6791           && inst.size_req != 4)
6792         {
6793           /* Insn may have a 16-bit form.  */
6794           Rn = inst.operands[1].reg;
6795           if (inst.operands[1].immisreg)
6796             {
6797               inst.instruction = THUMB_OP16 (opcode);
6798               /* [Rn, Ri] */
6799               if (Rn <= 7 && inst.operands[1].imm <= 7)
6800                 goto op16;
6801             }
6802           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
6803                     && opcode != T_MNEM_ldrsb)
6804                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
6805                    || (Rn == REG_SP && opcode == T_MNEM_str))
6806             {
6807               /* [Rn, #const] */
6808               if (Rn > 7)
6809                 {
6810                   if (Rn == REG_PC)
6811                     {
6812                       if (inst.reloc.pc_rel)
6813                         opcode = T_MNEM_ldr_pc2;
6814                       else
6815                         opcode = T_MNEM_ldr_pc;
6816                     }
6817                   else
6818                     {
6819                       if (opcode == T_MNEM_ldr)
6820                         opcode = T_MNEM_ldr_sp;
6821                       else
6822                         opcode = T_MNEM_str_sp;
6823                     }
6824                   inst.instruction = inst.operands[0].reg << 8;
6825                 }
6826               else
6827                 {
6828                   inst.instruction = inst.operands[0].reg;
6829                   inst.instruction |= inst.operands[1].reg << 3;
6830                 }
6831               inst.instruction |= THUMB_OP16 (opcode);
6832               if (inst.size_req == 2)
6833                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
6834               else
6835                 inst.relax = opcode;
6836               return;
6837             }
6838         }
6839       /* Definitely a 32-bit variant.  */
6840       inst.instruction = THUMB_OP32 (opcode);
6841       inst.instruction |= inst.operands[0].reg << 12;
6842       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
6843       return;
6844     }
6845
6846   constraint (inst.operands[0].reg > 7, BAD_HIREG);
6847
6848   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
6849     {
6850       /* Only [Rn,Rm] is acceptable.  */
6851       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
6852       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
6853                   || inst.operands[1].postind || inst.operands[1].shifted
6854                   || inst.operands[1].negative,
6855                   _("Thumb does not support this addressing mode"));
6856       inst.instruction = THUMB_OP16 (inst.instruction);
6857       goto op16;
6858     }
6859      
6860   inst.instruction = THUMB_OP16 (inst.instruction);
6861   if (!inst.operands[1].isreg)
6862     if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
6863       return;
6864
6865   constraint (!inst.operands[1].preind
6866               || inst.operands[1].shifted
6867               || inst.operands[1].writeback,
6868               _("Thumb does not support this addressing mode"));
6869   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
6870     {
6871       constraint (inst.instruction & 0x0600,
6872                   _("byte or halfword not valid for base register"));
6873       constraint (inst.operands[1].reg == REG_PC
6874                   && !(inst.instruction & THUMB_LOAD_BIT),
6875                   _("r15 based store not allowed"));
6876       constraint (inst.operands[1].immisreg,
6877                   _("invalid base register for register offset"));
6878
6879       if (inst.operands[1].reg == REG_PC)
6880         inst.instruction = T_OPCODE_LDR_PC;
6881       else if (inst.instruction & THUMB_LOAD_BIT)
6882         inst.instruction = T_OPCODE_LDR_SP;
6883       else
6884         inst.instruction = T_OPCODE_STR_SP;
6885
6886       inst.instruction |= inst.operands[0].reg << 8;
6887       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
6888       return;
6889     }
6890
6891   constraint (inst.operands[1].reg > 7, BAD_HIREG);
6892   if (!inst.operands[1].immisreg)
6893     {
6894       /* Immediate offset.  */
6895       inst.instruction |= inst.operands[0].reg;
6896       inst.instruction |= inst.operands[1].reg << 3;
6897       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
6898       return;
6899     }
6900
6901   /* Register offset.  */
6902   constraint (inst.operands[1].imm > 7, BAD_HIREG);
6903   constraint (inst.operands[1].negative,
6904               _("Thumb does not support this addressing mode"));
6905
6906  op16:
6907   switch (inst.instruction)
6908     {
6909     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
6910     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
6911     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
6912     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
6913     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
6914     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
6915     case 0x5600 /* ldrsb */:
6916     case 0x5e00 /* ldrsh */: break;
6917     default: abort ();
6918     }
6919
6920   inst.instruction |= inst.operands[0].reg;
6921   inst.instruction |= inst.operands[1].reg << 3;
6922   inst.instruction |= inst.operands[1].imm << 6;
6923 }
6924
6925 static void
6926 do_t_ldstd (void)
6927 {
6928   if (!inst.operands[1].present)
6929     {
6930       inst.operands[1].reg = inst.operands[0].reg + 1;
6931       constraint (inst.operands[0].reg == REG_LR,
6932                   _("r14 not allowed here"));
6933     }
6934   inst.instruction |= inst.operands[0].reg << 12;
6935   inst.instruction |= inst.operands[1].reg << 8;
6936   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
6937                             
6938 }
6939
6940 static void
6941 do_t_ldstt (void)
6942 {
6943   inst.instruction |= inst.operands[0].reg << 12;
6944   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
6945 }
6946
6947 static void
6948 do_t_mla (void)
6949 {
6950   inst.instruction |= inst.operands[0].reg << 8;
6951   inst.instruction |= inst.operands[1].reg << 16;
6952   inst.instruction |= inst.operands[2].reg;
6953   inst.instruction |= inst.operands[3].reg << 12;
6954 }
6955
6956 static void
6957 do_t_mlal (void)
6958 {
6959   inst.instruction |= inst.operands[0].reg << 12;
6960   inst.instruction |= inst.operands[1].reg << 8;
6961   inst.instruction |= inst.operands[2].reg << 16;
6962   inst.instruction |= inst.operands[3].reg;
6963 }
6964
6965 static void
6966 do_t_mov_cmp (void)
6967 {
6968   if (unified_syntax)
6969     {
6970       int r0off = (inst.instruction == T_MNEM_mov
6971                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
6972       unsigned long opcode;
6973       bfd_boolean narrow;
6974       bfd_boolean low_regs;
6975
6976       low_regs = (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7);
6977       opcode = inst.instruction;
6978       if (current_it_mask)
6979         narrow = opcode != T_MNEM_movs;
6980       else
6981         narrow = opcode != T_MNEM_movs || low_regs;
6982       if (inst.size_req == 4
6983           || inst.operands[1].shifted)
6984         narrow = FALSE;
6985
6986       if (!inst.operands[1].isreg)
6987         {
6988           /* Immediate operand.  */
6989           if (current_it_mask == 0 && opcode == T_MNEM_mov)
6990             narrow = 0;
6991           if (low_regs && narrow)
6992             {
6993               inst.instruction = THUMB_OP16 (opcode);
6994               inst.instruction |= inst.operands[0].reg << 8;
6995               if (inst.size_req == 2)
6996                 inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
6997               else
6998                 inst.relax = opcode;
6999             }
7000           else
7001             {
7002               inst.instruction = THUMB_OP32 (inst.instruction);
7003               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7004               inst.instruction |= inst.operands[0].reg << r0off;
7005               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7006             }
7007         }
7008       else if (!narrow)
7009         {
7010           inst.instruction = THUMB_OP32 (inst.instruction);
7011           inst.instruction |= inst.operands[0].reg << r0off;
7012           encode_thumb32_shifted_operand (1);
7013         }
7014       else
7015         switch (inst.instruction)
7016           {
7017           case T_MNEM_mov:
7018             inst.instruction = T_OPCODE_MOV_HR;
7019             inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
7020             inst.instruction |= (inst.operands[0].reg & 0x7);
7021             inst.instruction |= inst.operands[1].reg << 3;
7022             break;
7023
7024           case T_MNEM_movs:
7025             /* We know we have low registers at this point.
7026                Generate ADD Rd, Rs, #0.  */
7027             inst.instruction = T_OPCODE_ADD_I3;
7028             inst.instruction |= inst.operands[0].reg;
7029             inst.instruction |= inst.operands[1].reg << 3;
7030             break;
7031
7032           case T_MNEM_cmp:
7033             if (low_regs)
7034               {
7035                 inst.instruction = T_OPCODE_CMP_LR;
7036                 inst.instruction |= inst.operands[0].reg;
7037                 inst.instruction |= inst.operands[1].reg << 3;
7038               }
7039             else
7040               {
7041                 inst.instruction = T_OPCODE_CMP_HR;
7042                 inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
7043                 inst.instruction |= (inst.operands[0].reg & 0x7);
7044                 inst.instruction |= inst.operands[1].reg << 3;
7045               }
7046             break;
7047           }
7048       return;
7049     }
7050
7051   inst.instruction = THUMB_OP16 (inst.instruction);
7052   if (inst.operands[1].isreg)
7053     {
7054       if (inst.operands[0].reg < 8 && inst.operands[1].reg < 8)
7055         {
7056           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
7057              since a MOV instruction produces unpredictable results.  */
7058           if (inst.instruction == T_OPCODE_MOV_I8)
7059             inst.instruction = T_OPCODE_ADD_I3;
7060           else
7061             inst.instruction = T_OPCODE_CMP_LR;
7062
7063           inst.instruction |= inst.operands[0].reg;
7064           inst.instruction |= inst.operands[1].reg << 3;
7065         }
7066       else
7067         {
7068           if (inst.instruction == T_OPCODE_MOV_I8)
7069             inst.instruction = T_OPCODE_MOV_HR;
7070           else
7071             inst.instruction = T_OPCODE_CMP_HR;
7072           do_t_cpy ();
7073         }
7074     }
7075   else
7076     {
7077       constraint (inst.operands[0].reg > 7,
7078                   _("only lo regs allowed with immediate"));
7079       inst.instruction |= inst.operands[0].reg << 8;
7080       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
7081     }
7082 }
7083
7084 static void
7085 do_t_mov16 (void)
7086 {
7087   inst.instruction |= inst.operands[0].reg << 8;
7088   inst.instruction |= (inst.operands[1].imm & 0xf000) << 4;
7089   inst.instruction |= (inst.operands[1].imm & 0x0800) << 15;
7090   inst.instruction |= (inst.operands[1].imm & 0x0700) << 4;
7091   inst.instruction |= (inst.operands[1].imm & 0x00ff);
7092 }
7093
7094 static void
7095 do_t_mvn_tst (void)
7096 {
7097   if (unified_syntax)
7098     {
7099       int r0off = (inst.instruction == T_MNEM_mvn
7100                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
7101       bfd_boolean narrow;
7102
7103       if (inst.size_req == 4
7104           || inst.instruction > 0xffff
7105           || inst.operands[1].shifted
7106           || inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
7107         narrow = FALSE;
7108       else if (inst.instruction == T_MNEM_cmn)
7109         narrow = TRUE;
7110       else if (THUMB_SETS_FLAGS (inst.instruction))
7111         narrow = (current_it_mask == 0);
7112       else
7113         narrow = (current_it_mask != 0);
7114
7115       if (!inst.operands[1].isreg)
7116         {
7117           /* For an immediate, we always generate a 32-bit opcode;
7118              section relaxation will shrink it later if possible.  */
7119           if (inst.instruction < 0xffff)
7120             inst.instruction = THUMB_OP32 (inst.instruction);
7121           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7122           inst.instruction |= inst.operands[0].reg << r0off;
7123           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7124         }
7125       else
7126         {
7127           /* See if we can do this with a 16-bit instruction.  */
7128           if (narrow)
7129             {
7130               inst.instruction = THUMB_OP16 (inst.instruction);
7131               inst.instruction |= inst.operands[0].reg;
7132               inst.instruction |= inst.operands[1].reg << 3;
7133             }
7134           else
7135             {
7136               constraint (inst.operands[1].shifted
7137                           && inst.operands[1].immisreg,
7138                           _("shift must be constant"));
7139               if (inst.instruction < 0xffff)
7140                 inst.instruction = THUMB_OP32 (inst.instruction);
7141               inst.instruction |= inst.operands[0].reg << r0off;
7142               encode_thumb32_shifted_operand (1);
7143             }
7144         }
7145     }
7146   else
7147     {
7148       constraint (inst.instruction > 0xffff
7149                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
7150       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
7151                   _("unshifted register required"));
7152       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
7153                   BAD_HIREG);
7154
7155       inst.instruction = THUMB_OP16 (inst.instruction);
7156       inst.instruction |= inst.operands[0].reg;
7157       inst.instruction |= inst.operands[1].reg << 3;
7158     }
7159 }
7160
7161 static void
7162 do_t_mrs (void)
7163 {
7164   /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
7165   constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
7166               != (PSR_c|PSR_f),
7167               _("'CPSR' or 'SPSR' expected"));
7168   inst.instruction |= inst.operands[0].reg << 8;
7169   inst.instruction |= (inst.operands[1].imm & SPSR_BIT) >> 2;
7170 }
7171
7172 static void
7173 do_t_msr (void)
7174 {
7175   constraint (!inst.operands[1].isreg,
7176               _("Thumb encoding does not support an immediate here"));
7177   inst.instruction |= (inst.operands[0].imm & SPSR_BIT) >> 2;
7178   inst.instruction |= (inst.operands[0].imm & ~SPSR_BIT) >> 8;
7179   inst.instruction |= inst.operands[1].reg << 16;
7180 }
7181
7182 static void
7183 do_t_mul (void)
7184 {
7185   if (!inst.operands[2].present)
7186     inst.operands[2].reg = inst.operands[0].reg;
7187
7188   /* There is no 32-bit MULS and no 16-bit MUL. */
7189   if (unified_syntax && inst.instruction == T_MNEM_mul)
7190     {
7191       inst.instruction = THUMB_OP32 (inst.instruction);
7192       inst.instruction |= inst.operands[0].reg << 8;
7193       inst.instruction |= inst.operands[1].reg << 16;
7194       inst.instruction |= inst.operands[2].reg << 0;
7195     }
7196   else
7197     {
7198       constraint (!unified_syntax
7199                   && inst.instruction == T_MNEM_muls, BAD_THUMB32);
7200       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
7201                   BAD_HIREG);
7202
7203       inst.instruction = THUMB_OP16 (inst.instruction);
7204       inst.instruction |= inst.operands[0].reg;
7205
7206       if (inst.operands[0].reg == inst.operands[1].reg)
7207         inst.instruction |= inst.operands[2].reg << 3;
7208       else if (inst.operands[0].reg == inst.operands[2].reg)
7209         inst.instruction |= inst.operands[1].reg << 3;
7210       else
7211         constraint (1, _("dest must overlap one source register"));
7212     }
7213 }
7214
7215 static void
7216 do_t_mull (void)
7217 {
7218   inst.instruction |= inst.operands[0].reg << 12;
7219   inst.instruction |= inst.operands[1].reg << 8;
7220   inst.instruction |= inst.operands[2].reg << 16;
7221   inst.instruction |= inst.operands[3].reg;
7222
7223   if (inst.operands[0].reg == inst.operands[1].reg)
7224     as_tsktsk (_("rdhi and rdlo must be different"));
7225 }
7226
7227 static void
7228 do_t_nop (void)
7229 {
7230   if (unified_syntax)
7231     {
7232       if (inst.size_req == 4 || inst.operands[0].imm > 15)
7233         {
7234           inst.instruction = THUMB_OP32 (inst.instruction);
7235           inst.instruction |= inst.operands[0].imm;
7236         }
7237       else
7238         {
7239           inst.instruction = THUMB_OP16 (inst.instruction);
7240           inst.instruction |= inst.operands[0].imm << 4;
7241         }
7242     }
7243   else
7244     {
7245       constraint (inst.operands[0].present,
7246                   _("Thumb does not support NOP with hints"));
7247       inst.instruction = 0x46c0;
7248     }
7249 }
7250
7251 static void
7252 do_t_neg (void)
7253 {
7254   if (unified_syntax)
7255     {
7256       bfd_boolean narrow;
7257
7258       if (THUMB_SETS_FLAGS (inst.instruction))
7259         narrow = (current_it_mask == 0);
7260       else
7261         narrow = (current_it_mask != 0);
7262       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
7263         narrow = FALSE;
7264       if (inst.size_req == 4)
7265         narrow = FALSE;
7266
7267       if (!narrow)
7268         {
7269           inst.instruction = THUMB_OP32 (inst.instruction);
7270           inst.instruction |= inst.operands[0].reg << 8;
7271           inst.instruction |= inst.operands[1].reg << 16;
7272         }
7273       else
7274         {
7275           inst.instruction = THUMB_OP16 (inst.instruction);
7276           inst.instruction |= inst.operands[0].reg;
7277           inst.instruction |= inst.operands[1].reg << 3;
7278         }
7279     }
7280   else
7281     {
7282       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
7283                   BAD_HIREG);
7284       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
7285
7286       inst.instruction = THUMB_OP16 (inst.instruction);
7287       inst.instruction |= inst.operands[0].reg;
7288       inst.instruction |= inst.operands[1].reg << 3;
7289     }
7290 }
7291
7292 static void
7293 do_t_pkhbt (void)
7294 {
7295   inst.instruction |= inst.operands[0].reg << 8;
7296   inst.instruction |= inst.operands[1].reg << 16;
7297   inst.instruction |= inst.operands[2].reg;
7298   if (inst.operands[3].present)
7299     {
7300       unsigned int val = inst.reloc.exp.X_add_number;
7301       constraint (inst.reloc.exp.X_op != O_constant,
7302                   _("expression too complex"));
7303       inst.instruction |= (val & 0x1c) << 10;
7304       inst.instruction |= (val & 0x03) << 6;
7305     }
7306 }
7307
7308 static void
7309 do_t_pkhtb (void)
7310 {
7311   if (!inst.operands[3].present)
7312     inst.instruction &= ~0x00000020;
7313   do_t_pkhbt ();
7314 }
7315
7316 static void
7317 do_t_pld (void)
7318 {
7319   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
7320 }
7321
7322 static void
7323 do_t_push_pop (void)
7324 {
7325   unsigned mask;
7326   
7327   constraint (inst.operands[0].writeback,
7328               _("push/pop do not support {reglist}^"));
7329   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
7330               _("expression too complex"));
7331
7332   mask = inst.operands[0].imm;
7333   if ((mask & ~0xff) == 0)
7334     inst.instruction = THUMB_OP16 (inst.instruction);
7335   else if ((inst.instruction == T_MNEM_push
7336             && (mask & ~0xff) == 1 << REG_LR)
7337            || (inst.instruction == T_MNEM_pop
7338                && (mask & ~0xff) == 1 << REG_PC))
7339     {
7340       inst.instruction = THUMB_OP16 (inst.instruction);
7341       inst.instruction |= THUMB_PP_PC_LR;
7342       mask &= 0xff;
7343     }
7344   else if (unified_syntax)
7345     {
7346       if (mask & (1 << 13))
7347         inst.error =  _("SP not allowed in register list");
7348       if (inst.instruction == T_MNEM_push)
7349         {
7350           if (mask & (1 << 15))
7351             inst.error = _("PC not allowed in register list");
7352         }
7353       else
7354         {
7355           if (mask & (1 << 14)
7356               && mask & (1 << 15))
7357             inst.error = _("LR and PC should not both be in register list");
7358         }
7359       if ((mask & (mask - 1)) == 0)
7360         {
7361           /* Single register push/pop implemented as str/ldr.  */
7362           if (inst.instruction == T_MNEM_push)
7363             inst.instruction = 0xf84d0d04; /* str reg, [sp, #-4]! */
7364           else
7365             inst.instruction = 0xf85d0b04; /* ldr reg, [sp], #4 */
7366           mask = ffs(mask) - 1;
7367           mask <<= 12;
7368         }
7369       else
7370         inst.instruction = THUMB_OP32 (inst.instruction);
7371     }
7372   else
7373     {
7374       inst.error = _("invalid register list to push/pop instruction");
7375       return;
7376     }
7377
7378   inst.instruction |= mask;
7379 }
7380
7381 static void
7382 do_t_rbit (void)
7383 {
7384   inst.instruction |= inst.operands[0].reg << 8;
7385   inst.instruction |= inst.operands[1].reg << 16;
7386 }
7387
7388 static void
7389 do_t_rev (void)
7390 {
7391   if (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
7392       && inst.size_req != 4)
7393     {
7394       inst.instruction = THUMB_OP16 (inst.instruction);
7395       inst.instruction |= inst.operands[0].reg;
7396       inst.instruction |= inst.operands[1].reg << 3;
7397     }
7398   else if (unified_syntax)
7399     {
7400       inst.instruction = THUMB_OP32 (inst.instruction);
7401       inst.instruction |= inst.operands[0].reg << 8;
7402       inst.instruction |= inst.operands[1].reg << 16;
7403       inst.instruction |= inst.operands[1].reg;
7404     }
7405   else
7406     inst.error = BAD_HIREG;
7407 }
7408
7409 static void
7410 do_t_rsb (void)
7411 {
7412   int Rd, Rs;
7413
7414   Rd = inst.operands[0].reg;
7415   Rs = (inst.operands[1].present
7416         ? inst.operands[1].reg    /* Rd, Rs, foo */
7417         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
7418
7419   inst.instruction |= Rd << 8;
7420   inst.instruction |= Rs << 16;
7421   if (!inst.operands[2].isreg)
7422     {
7423       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7424       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7425     }
7426   else
7427     encode_thumb32_shifted_operand (2);
7428 }
7429
7430 static void
7431 do_t_setend (void)
7432 {
7433   if (inst.operands[0].imm)
7434     inst.instruction |= 0x8;
7435 }
7436
7437 static void
7438 do_t_shift (void)
7439 {
7440   if (!inst.operands[1].present)
7441     inst.operands[1].reg = inst.operands[0].reg;
7442
7443   if (unified_syntax)
7444     {
7445       bfd_boolean narrow;
7446       int shift_kind;
7447
7448       switch (inst.instruction)
7449         {
7450         case T_MNEM_asr:
7451         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
7452         case T_MNEM_lsl:
7453         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
7454         case T_MNEM_lsr:
7455         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
7456         case T_MNEM_ror:
7457         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
7458         default: abort ();
7459         }
7460
7461       if (THUMB_SETS_FLAGS (inst.instruction))
7462         narrow = (current_it_mask == 0);
7463       else
7464         narrow = (current_it_mask != 0);
7465       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
7466         narrow = FALSE;
7467       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
7468         narrow = FALSE;
7469       if (inst.operands[2].isreg
7470           && (inst.operands[1].reg != inst.operands[0].reg
7471               || inst.operands[2].reg > 7))
7472         narrow = FALSE;
7473       if (inst.size_req == 4)
7474         narrow = FALSE;
7475
7476       if (!narrow)
7477         {
7478           if (inst.operands[2].isreg)
7479             {
7480               inst.instruction = THUMB_OP32 (inst.instruction);
7481               inst.instruction |= inst.operands[0].reg << 8;
7482               inst.instruction |= inst.operands[1].reg << 16;
7483               inst.instruction |= inst.operands[2].reg;
7484             }
7485           else
7486             {
7487               inst.operands[1].shifted = 1;
7488               inst.operands[1].shift_kind = shift_kind;
7489               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
7490                                              ? T_MNEM_movs : T_MNEM_mov);
7491               inst.instruction |= inst.operands[0].reg << 8;
7492               encode_thumb32_shifted_operand (1);
7493               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
7494               inst.reloc.type = BFD_RELOC_UNUSED;
7495             }
7496         }
7497       else
7498         {
7499           if (inst.operands[2].isreg)
7500             {
7501               switch (shift_kind)
7502                 {
7503                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
7504                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
7505                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
7506                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
7507                 default: abort ();
7508                 }
7509           
7510               inst.instruction |= inst.operands[0].reg;
7511               inst.instruction |= inst.operands[2].reg << 3;
7512             }
7513           else
7514             {
7515               switch (shift_kind)
7516                 {
7517                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
7518                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
7519                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
7520                 default: abort ();
7521                 }
7522               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
7523               inst.instruction |= inst.operands[0].reg;
7524               inst.instruction |= inst.operands[1].reg << 3;
7525             }
7526         }
7527     }
7528   else
7529     {
7530       constraint (inst.operands[0].reg > 7
7531                   || inst.operands[1].reg > 7, BAD_HIREG);
7532       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
7533
7534       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
7535         {
7536           constraint (inst.operands[2].reg > 7, BAD_HIREG);
7537           constraint (inst.operands[0].reg != inst.operands[1].reg,
7538                       _("source1 and dest must be same register"));
7539
7540           switch (inst.instruction)
7541             {
7542             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
7543             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
7544             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
7545             case T_MNEM_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 (inst.instruction)
7555             {
7556             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
7557             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
7558             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
7559             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
7560             default: abort ();
7561             }
7562           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
7563           inst.instruction |= inst.operands[0].reg;
7564           inst.instruction |= inst.operands[1].reg << 3;
7565         }
7566     }
7567 }
7568
7569 static void
7570 do_t_simd (void)
7571 {
7572   inst.instruction |= inst.operands[0].reg << 8;
7573   inst.instruction |= inst.operands[1].reg << 16;
7574   inst.instruction |= inst.operands[2].reg;
7575 }
7576
7577 static void
7578 do_t_smc (void)
7579 {
7580   unsigned int value = inst.reloc.exp.X_add_number;
7581   constraint (inst.reloc.exp.X_op != O_constant,
7582               _("expression too complex"));
7583   inst.reloc.type = BFD_RELOC_UNUSED;
7584   inst.instruction |= (value & 0xf000) >> 12;
7585   inst.instruction |= (value & 0x0ff0);
7586   inst.instruction |= (value & 0x000f) << 16;
7587 }
7588
7589 static void
7590 do_t_ssat (void)
7591 {
7592   inst.instruction |= inst.operands[0].reg << 8;
7593   inst.instruction |= inst.operands[1].imm - 1;
7594   inst.instruction |= inst.operands[2].reg << 16;
7595
7596   if (inst.operands[3].present)
7597     {
7598       constraint (inst.reloc.exp.X_op != O_constant,
7599                   _("expression too complex"));
7600
7601       if (inst.reloc.exp.X_add_number != 0)
7602         {
7603           if (inst.operands[3].shift_kind == SHIFT_ASR)
7604             inst.instruction |= 0x00200000;  /* sh bit */
7605           inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
7606           inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
7607         }
7608       inst.reloc.type = BFD_RELOC_UNUSED;
7609     }
7610 }
7611
7612 static void
7613 do_t_ssat16 (void)
7614 {
7615   inst.instruction |= inst.operands[0].reg << 8;
7616   inst.instruction |= inst.operands[1].imm - 1;
7617   inst.instruction |= inst.operands[2].reg << 16;
7618 }
7619
7620 static void
7621 do_t_strex (void)
7622 {
7623   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
7624               || inst.operands[2].postind || inst.operands[2].writeback
7625               || inst.operands[2].immisreg || inst.operands[2].shifted
7626               || inst.operands[2].negative,
7627               BAD_ADDR_MODE);
7628
7629   inst.instruction |= inst.operands[0].reg << 8;
7630   inst.instruction |= inst.operands[1].reg << 12;
7631   inst.instruction |= inst.operands[2].reg << 16;
7632   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
7633 }
7634
7635 static void
7636 do_t_strexd (void)
7637 {
7638   if (!inst.operands[2].present)
7639     inst.operands[2].reg = inst.operands[1].reg + 1;
7640
7641   constraint (inst.operands[0].reg == inst.operands[1].reg
7642               || inst.operands[0].reg == inst.operands[2].reg
7643               || inst.operands[0].reg == inst.operands[3].reg
7644               || inst.operands[1].reg == inst.operands[2].reg,
7645               BAD_OVERLAP);
7646
7647   inst.instruction |= inst.operands[0].reg;
7648   inst.instruction |= inst.operands[1].reg << 12;
7649   inst.instruction |= inst.operands[2].reg << 8;
7650   inst.instruction |= inst.operands[3].reg << 16;
7651 }
7652
7653 static void
7654 do_t_sxtah (void)
7655 {
7656   inst.instruction |= inst.operands[0].reg << 8;
7657   inst.instruction |= inst.operands[1].reg << 16;
7658   inst.instruction |= inst.operands[2].reg;
7659   inst.instruction |= inst.operands[3].imm << 4;
7660 }
7661
7662 static void
7663 do_t_sxth (void)
7664 {
7665   if (inst.instruction <= 0xffff && inst.size_req != 4
7666       && inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
7667       && (!inst.operands[2].present || inst.operands[2].imm == 0))
7668     {
7669       inst.instruction = THUMB_OP16 (inst.instruction);
7670       inst.instruction |= inst.operands[0].reg;
7671       inst.instruction |= inst.operands[1].reg << 3;
7672     }
7673   else if (unified_syntax)
7674     {
7675       if (inst.instruction <= 0xffff)
7676         inst.instruction = THUMB_OP32 (inst.instruction);
7677       inst.instruction |= inst.operands[0].reg << 8;
7678       inst.instruction |= inst.operands[1].reg;
7679       inst.instruction |= inst.operands[2].imm << 4;
7680     }
7681   else
7682     {
7683       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
7684                   _("Thumb encoding does not support rotation"));
7685       constraint (1, BAD_HIREG);
7686     }
7687 }
7688
7689 static void
7690 do_t_swi (void)
7691 {
7692   inst.reloc.type = BFD_RELOC_ARM_SWI;
7693 }
7694
7695 static void
7696 do_t_tb (void)
7697 {
7698   int half;
7699
7700   half = (inst.instruction & 0x10) != 0;
7701   constraint (inst.operands[0].imm == 15,
7702               _("PC is not a valid index register"));
7703   constraint (!half && inst.operands[0].shifted,
7704               _("instruction does not allow shifted index"));
7705   constraint (half && !inst.operands[0].shifted,
7706               _("instruction requires shifted index"));
7707   inst.instruction |= (inst.operands[0].reg << 16) | inst.operands[0].imm;
7708 }
7709
7710 static void
7711 do_t_usat (void)
7712 {
7713   inst.instruction |= inst.operands[0].reg << 8;
7714   inst.instruction |= inst.operands[1].imm;
7715   inst.instruction |= inst.operands[2].reg << 16;
7716
7717   if (inst.operands[3].present)
7718     {
7719       constraint (inst.reloc.exp.X_op != O_constant,
7720                   _("expression too complex"));
7721       if (inst.reloc.exp.X_add_number != 0)
7722         {
7723           if (inst.operands[3].shift_kind == SHIFT_ASR)
7724             inst.instruction |= 0x00200000;  /* sh bit */
7725
7726           inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
7727           inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
7728         }
7729       inst.reloc.type = BFD_RELOC_UNUSED;
7730     }
7731 }
7732
7733 static void
7734 do_t_usat16 (void)
7735 {
7736   inst.instruction |= inst.operands[0].reg << 8;
7737   inst.instruction |= inst.operands[1].imm;
7738   inst.instruction |= inst.operands[2].reg << 16;
7739 }
7740 \f
7741 /* Overall per-instruction processing.  */
7742
7743 /* We need to be able to fix up arbitrary expressions in some statements.
7744    This is so that we can handle symbols that are an arbitrary distance from
7745    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
7746    which returns part of an address in a form which will be valid for
7747    a data instruction.  We do this by pushing the expression into a symbol
7748    in the expr_section, and creating a fix for that.  */
7749
7750 static void
7751 fix_new_arm (fragS *       frag,
7752              int           where,
7753              short int     size,
7754              expressionS * exp,
7755              int           pc_rel,
7756              int           reloc)
7757 {
7758   fixS *           new_fix;
7759
7760   switch (exp->X_op)
7761     {
7762     case O_constant:
7763     case O_symbol:
7764     case O_add:
7765     case O_subtract:
7766       new_fix = fix_new_exp (frag, where, size, exp, pc_rel, reloc);
7767       break;
7768
7769     default:
7770       new_fix = fix_new (frag, where, size, make_expr_symbol (exp), 0,
7771                          pc_rel, reloc);
7772       break;
7773     }
7774
7775   /* Mark whether the fix is to a THUMB instruction, or an ARM
7776      instruction.  */
7777   new_fix->tc_fix_data = thumb_mode;
7778 }
7779
7780 /* Create a frg for an instruction requiring relaxation.  */
7781 static void
7782 output_relax_insn (void)
7783 {
7784   char * to;
7785   symbolS *sym;
7786   int offset;
7787
7788   switch (inst.reloc.exp.X_op)
7789     {
7790     case O_symbol:
7791       sym = inst.reloc.exp.X_add_symbol;
7792       offset = inst.reloc.exp.X_add_number;
7793       break;
7794     case O_constant:
7795       sym = NULL;
7796       offset = inst.reloc.exp.X_add_number;
7797       break;
7798     default:
7799       sym = make_expr_symbol (&inst.reloc.exp);
7800       offset = 0;
7801       break;
7802   }
7803   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
7804                  inst.relax, sym, offset, NULL/*offset, opcode*/);
7805   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
7806
7807 #ifdef OBJ_ELF
7808   dwarf2_emit_insn (INSN_SIZE);
7809 #endif
7810 }
7811
7812 /* Write a 32-bit thumb instruction to buf.  */
7813 static void
7814 put_thumb32_insn (char * buf, unsigned long insn)
7815 {
7816   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
7817   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
7818 }
7819
7820 static void
7821 output_inst (const char * str)
7822 {
7823   char * to = NULL;
7824
7825   if (inst.error)
7826     {
7827       as_bad ("%s -- `%s'", inst.error, str);
7828       return;
7829     }
7830   if (inst.relax) {
7831       output_relax_insn();
7832       return;
7833   }
7834   if (inst.size == 0)
7835     return;
7836
7837   to = frag_more (inst.size);
7838
7839   if (thumb_mode && (inst.size > THUMB_SIZE))
7840     {
7841       assert (inst.size == (2 * THUMB_SIZE));
7842       put_thumb32_insn (to, inst.instruction);
7843     }
7844   else if (inst.size > INSN_SIZE)
7845     {
7846       assert (inst.size == (2 * INSN_SIZE));
7847       md_number_to_chars (to, inst.instruction, INSN_SIZE);
7848       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
7849     }
7850   else
7851     md_number_to_chars (to, inst.instruction, inst.size);
7852
7853   if (inst.reloc.type != BFD_RELOC_UNUSED)
7854     fix_new_arm (frag_now, to - frag_now->fr_literal,
7855                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
7856                  inst.reloc.type);
7857
7858 #ifdef OBJ_ELF
7859   dwarf2_emit_insn (inst.size);
7860 #endif
7861 }
7862
7863 /* Tag values used in struct asm_opcode's tag field.  */
7864 enum opcode_tag
7865 {
7866   OT_unconditional,     /* Instruction cannot be conditionalized.
7867                            The ARM condition field is still 0xE.  */
7868   OT_unconditionalF,    /* Instruction cannot be conditionalized
7869                            and carries 0xF in its ARM condition field.  */
7870   OT_csuffix,           /* Instruction takes a conditional suffix.  */
7871   OT_cinfix3,           /* Instruction takes a conditional infix,
7872                            beginning at character index 3.  (In
7873                            unified mode, it becomes a suffix.)  */
7874   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
7875                            character index 3, even in unified mode.  Used for
7876                            legacy instructions where suffix and infix forms
7877                            may be ambiguous.  */
7878   OT_csuf_or_in3,       /* Instruction takes either a conditional
7879                            suffix or an infix at character index 3.  */
7880   OT_odd_infix_unc,     /* This is the unconditional variant of an
7881                            instruction that takes a conditional infix
7882                            at an unusual position.  In unified mode,
7883                            this variant will accept a suffix.  */
7884   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
7885                            are the conditional variants of instructions that
7886                            take conditional infixes in unusual positions.
7887                            The infix appears at character index
7888                            (tag - OT_odd_infix_0).  These are not accepted
7889                            in unified mode.  */
7890 };
7891
7892 /* Subroutine of md_assemble, responsible for looking up the primary
7893    opcode from the mnemonic the user wrote.  STR points to the
7894    beginning of the mnemonic.
7895
7896    This is not simply a hash table lookup, because of conditional
7897    variants.  Most instructions have conditional variants, which are
7898    expressed with a _conditional affix_ to the mnemonic.  If we were
7899    to encode each conditional variant as a literal string in the opcode
7900    table, it would have approximately 20,000 entries.
7901
7902    Most mnemonics take this affix as a suffix, and in unified syntax,
7903    'most' is upgraded to 'all'.  However, in the divided syntax, some
7904    instructions take the affix as an infix, notably the s-variants of
7905    the arithmetic instructions.  Of those instructions, all but six
7906    have the infix appear after the third character of the mnemonic.
7907
7908    Accordingly, the algorithm for looking up primary opcodes given
7909    an identifier is:
7910
7911    1. Look up the identifier in the opcode table.
7912       If we find a match, go to step U.
7913
7914    2. Look up the last two characters of the identifier in the
7915       conditions table.  If we find a match, look up the first N-2
7916       characters of the identifier in the opcode table.  If we
7917       find a match, go to step CE.
7918
7919    3. Look up the fourth and fifth characters of the identifier in
7920       the conditions table.  If we find a match, extract those
7921       characters from the identifier, and look up the remaining
7922       characters in the opcode table.  If we find a match, go
7923       to step CM.
7924
7925    4. Fail.
7926
7927    U. Examine the tag field of the opcode structure, in case this is
7928       one of the six instructions with its conditional infix in an
7929       unusual place.  If it is, the tag tells us where to find the
7930       infix; look it up in the conditions table and set inst.cond
7931       accordingly.  Otherwise, this is an unconditional instruction.
7932       Again set inst.cond accordingly.  Return the opcode structure.
7933
7934   CE. Examine the tag field to make sure this is an instruction that
7935       should receive a conditional suffix.  If it is not, fail.
7936       Otherwise, set inst.cond from the suffix we already looked up,
7937       and return the opcode structure.
7938
7939   CM. Examine the tag field to make sure this is an instruction that
7940       should receive a conditional infix after the third character.
7941       If it is not, fail.  Otherwise, undo the edits to the current
7942       line of input and proceed as for case CE.  */
7943
7944 static const struct asm_opcode *
7945 opcode_lookup (char **str)
7946 {
7947   char *end, *base;
7948   char *affix;
7949   const struct asm_opcode *opcode;
7950   const struct asm_cond *cond;
7951   char save[2];
7952
7953   /* Scan up to the end of the mnemonic, which must end in white space,
7954      '.' (in unified mode only), or end of string.  */
7955   for (base = end = *str; *end != '\0'; end++)
7956     if (*end == ' ' || (unified_syntax && *end == '.'))
7957       break;
7958
7959   if (end == base)
7960     return 0;
7961
7962   /* Handle a possible width suffix.  */
7963   if (end[0] == '.')
7964     {
7965       if (end[1] == 'w' && (end[2] == ' ' || end[2] == '\0'))
7966         inst.size_req = 4;
7967       else if (end[1] == 'n' && (end[2] == ' ' || end[2] == '\0'))
7968         inst.size_req = 2;
7969       else
7970         return 0;
7971
7972       *str = end + 2;
7973     }
7974   else
7975     *str = end;
7976
7977   /* Look for unaffixed or special-case affixed mnemonic.  */
7978   opcode = hash_find_n (arm_ops_hsh, base, end - base);
7979   if (opcode)
7980     {
7981       /* step U */
7982       if (opcode->tag < OT_odd_infix_0)
7983         {
7984           inst.cond = COND_ALWAYS;
7985           return opcode;
7986         }
7987
7988       if (unified_syntax)
7989         as_warn (_("conditional infixes are deprecated in unified syntax"));
7990       affix = base + (opcode->tag - OT_odd_infix_0);
7991       cond = hash_find_n (arm_cond_hsh, affix, 2);
7992       assert (cond);
7993
7994       inst.cond = cond->value;
7995       return opcode;
7996     }
7997
7998   /* Cannot have a conditional suffix on a mnemonic of less than two
7999      characters.  */
8000   if (end - base < 3)
8001     return 0;
8002
8003   /* Look for suffixed mnemonic.  */
8004   affix = end - 2;
8005   cond = hash_find_n (arm_cond_hsh, affix, 2);
8006   opcode = hash_find_n (arm_ops_hsh, base, affix - base);
8007   if (opcode && cond)
8008     {
8009       /* step CE */
8010       switch (opcode->tag)
8011         {
8012         case OT_cinfix3_legacy:
8013           /* Ignore conditional suffixes matched on infix only mnemonics.  */
8014           break;
8015
8016         case OT_cinfix3:
8017         case OT_odd_infix_unc:
8018           if (!unified_syntax)
8019             return 0;
8020           /* else fall through */
8021
8022         case OT_csuffix:
8023         case OT_csuf_or_in3:
8024           inst.cond = cond->value;
8025           return opcode;
8026
8027         case OT_unconditional:
8028         case OT_unconditionalF:
8029           /* delayed diagnostic */
8030           inst.error = BAD_COND;
8031           inst.cond = COND_ALWAYS;
8032           return opcode;
8033
8034         default:
8035           return 0;
8036         }
8037     }
8038
8039   /* Cannot have a usual-position infix on a mnemonic of less than
8040      six characters (five would be a suffix).  */
8041   if (end - base < 6)
8042     return 0;
8043
8044   /* Look for infixed mnemonic in the usual position.  */
8045   affix = base + 3;
8046   cond = hash_find_n (arm_cond_hsh, affix, 2);
8047   if (!cond)
8048     return 0;
8049
8050   memcpy (save, affix, 2);
8051   memmove (affix, affix + 2, (end - affix) - 2);
8052   opcode = hash_find_n (arm_ops_hsh, base, (end - base) - 2);
8053   memmove (affix + 2, affix, (end - affix) - 2);
8054   memcpy (affix, save, 2);
8055
8056   if (opcode && (opcode->tag == OT_cinfix3 || opcode->tag == OT_csuf_or_in3
8057                  || opcode->tag == OT_cinfix3_legacy))
8058     {
8059       /* step CM */
8060       if (unified_syntax && opcode->tag == OT_cinfix3)
8061         as_warn (_("conditional infixes are deprecated in unified syntax"));
8062
8063       inst.cond = cond->value;
8064       return opcode;
8065     }
8066
8067   return 0;
8068 }
8069
8070 void
8071 md_assemble (char *str)
8072 {
8073   char *p = str;
8074   const struct asm_opcode * opcode;
8075
8076   /* Align the previous label if needed.  */
8077   if (last_label_seen != NULL)
8078     {
8079       symbol_set_frag (last_label_seen, frag_now);
8080       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
8081       S_SET_SEGMENT (last_label_seen, now_seg);
8082     }
8083
8084   memset (&inst, '\0', sizeof (inst));
8085   inst.reloc.type = BFD_RELOC_UNUSED;
8086
8087   opcode = opcode_lookup (&p);
8088   if (!opcode)
8089     {
8090       /* It wasn't an instruction, but it might be a register alias of
8091          the form alias .req reg.  */
8092       if (!create_register_alias (str, p))
8093         as_bad (_("bad instruction `%s'"), str);
8094
8095       return;
8096     }
8097
8098   if (thumb_mode)
8099     {
8100       unsigned long variant;
8101
8102       variant = cpu_variant;
8103       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
8104       if ((variant & ARM_EXT_V6T2) == 0)
8105         variant &= ARM_ANY;
8106       /* Check that this instruction is supported for this CPU.  */
8107       if (thumb_mode == 1 && (opcode->tvariant & variant) == 0)
8108         {
8109           as_bad (_("selected processor does not support `%s'"), str);
8110           return;
8111         }
8112       if (inst.cond != COND_ALWAYS && !unified_syntax
8113           && opcode->tencode != do_t_branch)
8114         {
8115           as_bad (_("Thumb does not support conditional execution"));
8116           return;
8117         }
8118
8119       /* Check conditional suffixes.  */
8120       if (current_it_mask)
8121         {
8122           int cond;
8123           cond = current_cc ^ ((current_it_mask >> 4) & 1) ^ 1;
8124           if (cond != inst.cond)
8125             {
8126               as_bad (_("incorrect condition in IT block"));
8127               return;
8128             }
8129           current_it_mask <<= 1;
8130           current_it_mask &= 0x1f;
8131         }
8132       else if (inst.cond != COND_ALWAYS && opcode->tencode != do_t_branch)
8133         {
8134           as_bad (_("thumb conditional instrunction not in IT block"));
8135           return;
8136         }
8137
8138       mapping_state (MAP_THUMB);
8139       inst.instruction = opcode->tvalue;
8140
8141       if (!parse_operands (p, opcode->operands))
8142         opcode->tencode ();
8143
8144       /* Clear current_it_mask at the end of an IT block.  */
8145       if (current_it_mask == 0x10)
8146         current_it_mask = 0;
8147
8148       if (!(inst.error || inst.relax))
8149         {
8150           assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
8151           inst.size = (inst.instruction > 0xffff ? 4 : 2);
8152           if (inst.size_req && inst.size_req != inst.size)
8153             {
8154               as_bad (_("cannot honor width suffix -- `%s'"), str);
8155               return;
8156             }
8157         }
8158       thumb_arch_used |= opcode->tvariant;
8159       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
8160          set those bits when Thumb-2 32-bit instuctions are seen.  ie.
8161          anything other than bl/blx.
8162          This is overly pessimistic for relaxable instructions.  */
8163       if ((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
8164           || inst.relax)
8165         thumb_arch_used |= ARM_EXT_V6T2;
8166     }
8167   else
8168     {
8169       /* Check that this instruction is supported for this CPU.  */
8170       if ((opcode->avariant & cpu_variant) == 0)
8171         {
8172           as_bad (_("selected processor does not support `%s'"), str);
8173           return;
8174         }
8175       if (inst.size_req)
8176         {
8177           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
8178           return;
8179         }
8180
8181       mapping_state (MAP_ARM);
8182       inst.instruction = opcode->avalue;
8183       if (opcode->tag == OT_unconditionalF)
8184         inst.instruction |= 0xF << 28;
8185       else
8186         inst.instruction |= inst.cond << 28;
8187       inst.size = INSN_SIZE;
8188       if (!parse_operands (p, opcode->operands))
8189         opcode->aencode ();
8190       /* Arm mode bx is marked as both v4T and v5 because it's still required
8191          on a hypothetical non-thumb v5 core.  */
8192       if (opcode->avariant == (ARM_EXT_V4T | ARM_EXT_V5))
8193         arm_arch_used |= ARM_EXT_V4T;
8194       else
8195         arm_arch_used |= opcode->avariant;
8196     }
8197   output_inst (str);
8198 }
8199
8200 /* Various frobbings of labels and their addresses.  */
8201
8202 void
8203 arm_start_line_hook (void)
8204 {
8205   last_label_seen = NULL;
8206 }
8207
8208 void
8209 arm_frob_label (symbolS * sym)
8210 {
8211   last_label_seen = sym;
8212
8213   ARM_SET_THUMB (sym, thumb_mode);
8214
8215 #if defined OBJ_COFF || defined OBJ_ELF
8216   ARM_SET_INTERWORK (sym, support_interwork);
8217 #endif
8218
8219   /* Note - do not allow local symbols (.Lxxx) to be labeled
8220      as Thumb functions.  This is because these labels, whilst
8221      they exist inside Thumb code, are not the entry points for
8222      possible ARM->Thumb calls.  Also, these labels can be used
8223      as part of a computed goto or switch statement.  eg gcc
8224      can generate code that looks like this:
8225
8226                 ldr  r2, [pc, .Laaa]
8227                 lsl  r3, r3, #2
8228                 ldr  r2, [r3, r2]
8229                 mov  pc, r2
8230
8231        .Lbbb:  .word .Lxxx
8232        .Lccc:  .word .Lyyy
8233        ..etc...
8234        .Laaa:   .word Lbbb
8235
8236      The first instruction loads the address of the jump table.
8237      The second instruction converts a table index into a byte offset.
8238      The third instruction gets the jump address out of the table.
8239      The fourth instruction performs the jump.
8240
8241      If the address stored at .Laaa is that of a symbol which has the
8242      Thumb_Func bit set, then the linker will arrange for this address
8243      to have the bottom bit set, which in turn would mean that the
8244      address computation performed by the third instruction would end
8245      up with the bottom bit set.  Since the ARM is capable of unaligned
8246      word loads, the instruction would then load the incorrect address
8247      out of the jump table, and chaos would ensue.  */
8248   if (label_is_thumb_function_name
8249       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
8250       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
8251     {
8252       /* When the address of a Thumb function is taken the bottom
8253          bit of that address should be set.  This will allow
8254          interworking between Arm and Thumb functions to work
8255          correctly.  */
8256
8257       THUMB_SET_FUNC (sym, 1);
8258
8259       label_is_thumb_function_name = FALSE;
8260     }
8261
8262 #ifdef OBJ_ELF
8263   dwarf2_emit_label (sym);
8264 #endif
8265 }
8266
8267 int
8268 arm_data_in_code (void)
8269 {
8270   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
8271     {
8272       *input_line_pointer = '/';
8273       input_line_pointer += 5;
8274       *input_line_pointer = 0;
8275       return 1;
8276     }
8277
8278   return 0;
8279 }
8280
8281 char *
8282 arm_canonicalize_symbol_name (char * name)
8283 {
8284   int len;
8285
8286   if (thumb_mode && (len = strlen (name)) > 5
8287       && streq (name + len - 5, "/data"))
8288     *(name + len - 5) = 0;
8289
8290   return name;
8291 }
8292 \f
8293 /* Table of all register names defined by default.  The user can
8294    define additional names with .req.  Note that all register names
8295    should appear in both upper and lowercase variants.  Some registers
8296    also have mixed-case names.  */
8297
8298 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE }
8299 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
8300 #define REGSET(p,t) \
8301   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
8302   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
8303   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
8304   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
8305
8306 static const struct reg_entry reg_names[] =
8307 {
8308   /* ARM integer registers.  */
8309   REGSET(r, RN), REGSET(R, RN),
8310
8311   /* ATPCS synonyms.  */
8312   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
8313   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
8314   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
8315
8316   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
8317   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
8318   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
8319
8320   /* Well-known aliases.  */
8321   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
8322   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
8323
8324   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
8325   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
8326
8327   /* Coprocessor numbers.  */
8328   REGSET(p, CP), REGSET(P, CP),
8329
8330   /* Coprocessor register numbers.  The "cr" variants are for backward
8331      compatibility.  */
8332   REGSET(c,  CN), REGSET(C, CN),
8333   REGSET(cr, CN), REGSET(CR, CN),
8334
8335   /* FPA registers.  */
8336   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
8337   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
8338
8339   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
8340   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
8341
8342   /* VFP SP registers.  */
8343   REGSET(s,VFS),
8344   REGNUM(s,16,VFS), REGNUM(s,17,VFS), REGNUM(s,18,VFS), REGNUM(s,19,VFS),
8345   REGNUM(s,20,VFS), REGNUM(s,21,VFS), REGNUM(s,22,VFS), REGNUM(s,23,VFS),
8346   REGNUM(s,24,VFS), REGNUM(s,25,VFS), REGNUM(s,26,VFS), REGNUM(s,27,VFS),
8347   REGNUM(s,28,VFS), REGNUM(s,29,VFS), REGNUM(s,30,VFS), REGNUM(s,31,VFS),
8348
8349   REGSET(S,VFS),
8350   REGNUM(S,16,VFS), REGNUM(S,17,VFS), REGNUM(S,18,VFS), REGNUM(S,19,VFS),
8351   REGNUM(S,20,VFS), REGNUM(S,21,VFS), REGNUM(S,22,VFS), REGNUM(S,23,VFS),
8352   REGNUM(S,24,VFS), REGNUM(S,25,VFS), REGNUM(S,26,VFS), REGNUM(S,27,VFS),
8353   REGNUM(S,28,VFS), REGNUM(S,29,VFS), REGNUM(S,30,VFS), REGNUM(S,31,VFS),
8354
8355   /* VFP DP Registers.  */
8356   REGSET(d,VFD), REGSET(D,VFS),
8357
8358   /* VFP control registers.  */
8359   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
8360   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
8361
8362   /* Maverick DSP coprocessor registers.  */
8363   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
8364   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
8365
8366   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
8367   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
8368   REGDEF(dspsc,0,DSPSC),
8369
8370   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
8371   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
8372   REGDEF(DSPSC,0,DSPSC),
8373
8374   /* iWMMXt data registers - p0, c0-15.  */
8375   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
8376
8377   /* iWMMXt control registers - p1, c0-3.  */
8378   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
8379   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
8380   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
8381   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
8382
8383   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
8384   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
8385   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
8386   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
8387   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
8388
8389   /* XScale accumulator registers.  */
8390   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
8391 };
8392 #undef REGDEF
8393 #undef REGNUM
8394 #undef REGSET
8395
8396 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
8397    within psr_required_here.  */
8398 static const struct asm_psr psrs[] =
8399 {
8400   /* Backward compatibility notation.  Note that "all" is no longer
8401      truly all possible PSR bits.  */
8402   {"all",  PSR_c | PSR_f},
8403   {"flg",  PSR_f},
8404   {"ctl",  PSR_c},
8405
8406   /* Individual flags.  */
8407   {"f",    PSR_f},
8408   {"c",    PSR_c},
8409   {"x",    PSR_x},
8410   {"s",    PSR_s},
8411   /* Combinations of flags.  */
8412   {"fs",   PSR_f | PSR_s},
8413   {"fx",   PSR_f | PSR_x},
8414   {"fc",   PSR_f | PSR_c},
8415   {"sf",   PSR_s | PSR_f},
8416   {"sx",   PSR_s | PSR_x},
8417   {"sc",   PSR_s | PSR_c},
8418   {"xf",   PSR_x | PSR_f},
8419   {"xs",   PSR_x | PSR_s},
8420   {"xc",   PSR_x | PSR_c},
8421   {"cf",   PSR_c | PSR_f},
8422   {"cs",   PSR_c | PSR_s},
8423   {"cx",   PSR_c | PSR_x},
8424   {"fsx",  PSR_f | PSR_s | PSR_x},
8425   {"fsc",  PSR_f | PSR_s | PSR_c},
8426   {"fxs",  PSR_f | PSR_x | PSR_s},
8427   {"fxc",  PSR_f | PSR_x | PSR_c},
8428   {"fcs",  PSR_f | PSR_c | PSR_s},
8429   {"fcx",  PSR_f | PSR_c | PSR_x},
8430   {"sfx",  PSR_s | PSR_f | PSR_x},
8431   {"sfc",  PSR_s | PSR_f | PSR_c},
8432   {"sxf",  PSR_s | PSR_x | PSR_f},
8433   {"sxc",  PSR_s | PSR_x | PSR_c},
8434   {"scf",  PSR_s | PSR_c | PSR_f},
8435   {"scx",  PSR_s | PSR_c | PSR_x},
8436   {"xfs",  PSR_x | PSR_f | PSR_s},
8437   {"xfc",  PSR_x | PSR_f | PSR_c},
8438   {"xsf",  PSR_x | PSR_s | PSR_f},
8439   {"xsc",  PSR_x | PSR_s | PSR_c},
8440   {"xcf",  PSR_x | PSR_c | PSR_f},
8441   {"xcs",  PSR_x | PSR_c | PSR_s},
8442   {"cfs",  PSR_c | PSR_f | PSR_s},
8443   {"cfx",  PSR_c | PSR_f | PSR_x},
8444   {"csf",  PSR_c | PSR_s | PSR_f},
8445   {"csx",  PSR_c | PSR_s | PSR_x},
8446   {"cxf",  PSR_c | PSR_x | PSR_f},
8447   {"cxs",  PSR_c | PSR_x | PSR_s},
8448   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
8449   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
8450   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
8451   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
8452   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
8453   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
8454   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
8455   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
8456   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
8457   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
8458   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
8459   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
8460   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
8461   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
8462   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
8463   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
8464   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
8465   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
8466   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
8467   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
8468   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
8469   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
8470   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
8471   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
8472 };
8473
8474 /* Table of all shift-in-operand names.  */
8475 static const struct asm_shift_name shift_names [] =
8476 {
8477   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
8478   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
8479   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
8480   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
8481   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
8482   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
8483 };
8484
8485 /* Table of all explicit relocation names.  */
8486 #ifdef OBJ_ELF
8487 static struct reloc_entry reloc_names[] =
8488 {
8489   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
8490   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
8491   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
8492   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
8493   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
8494   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
8495   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
8496   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
8497   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
8498   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
8499   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32}
8500 };
8501 #endif
8502
8503 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
8504 static const struct asm_cond conds[] =
8505 {
8506   {"eq", 0x0},
8507   {"ne", 0x1},
8508   {"cs", 0x2}, {"hs", 0x2},
8509   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
8510   {"mi", 0x4},
8511   {"pl", 0x5},
8512   {"vs", 0x6},
8513   {"vc", 0x7},
8514   {"hi", 0x8},
8515   {"ls", 0x9},
8516   {"ge", 0xa},
8517   {"lt", 0xb},
8518   {"gt", 0xc},
8519   {"le", 0xd},
8520   {"al", 0xe}
8521 };
8522
8523 /* Table of ARM-format instructions.    */
8524
8525 /* Macros for gluing together operand strings.  N.B. In all cases
8526    other than OPS0, the trailing OP_stop comes from default
8527    zero-initialization of the unspecified elements of the array.  */
8528 #define OPS0()            { OP_stop, }
8529 #define OPS1(a)           { OP_##a, }
8530 #define OPS2(a,b)         { OP_##a,OP_##b, }
8531 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
8532 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
8533 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
8534 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
8535
8536 /* These macros abstract out the exact format of the mnemonic table and
8537    save some repeated characters.  */
8538
8539 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
8540 #define TxCE(mnem, op, top, nops, ops, ae, te) \
8541   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
8542     THUMB_VARIANT, do_##ae, do_##te }
8543
8544 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
8545    a T_MNEM_xyz enumerator.  */
8546 #define TCE(mnem, aop, top, nops, ops, ae, te) \
8547        TxCE(mnem, aop, 0x##top, nops, ops, ae, te)
8548 #define tCE(mnem, aop, top, nops, ops, ae, te) \
8549        TxCE(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
8550
8551 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
8552    infix after the third character.  */
8553 #define TxC3(mnem, op, top, nops, ops, ae, te) \
8554   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
8555     THUMB_VARIANT, do_##ae, do_##te }
8556 #define TC3(mnem, aop, top, nops, ops, ae, te) \
8557        TxC3(mnem, aop, 0x##top, nops, ops, ae, te)
8558 #define tC3(mnem, aop, top, nops, ops, ae, te) \
8559        TxC3(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
8560
8561 /* Mnemonic with a conditional infix in an unusual place.  Each and every variant has to
8562    appear in the condition table.  */
8563 #define TxCM_(m1, m2, m3, op, top, nops, ops, ae, te)   \
8564   { #m1 #m2 #m3, OPS##nops ops, sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
8565     0x##op, top, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##te }
8566
8567 #define TxCM(m1, m2, op, top, nops, ops, ae, te)        \
8568   TxCM_(m1,   , m2, op, top, nops, ops, ae, te),        \
8569   TxCM_(m1, eq, m2, op, top, nops, ops, ae, te),        \
8570   TxCM_(m1, ne, m2, op, top, nops, ops, ae, te),        \
8571   TxCM_(m1, cs, m2, op, top, nops, ops, ae, te),        \
8572   TxCM_(m1, hs, m2, op, top, nops, ops, ae, te),        \
8573   TxCM_(m1, cc, m2, op, top, nops, ops, ae, te),        \
8574   TxCM_(m1, ul, m2, op, top, nops, ops, ae, te),        \
8575   TxCM_(m1, lo, m2, op, top, nops, ops, ae, te),        \
8576   TxCM_(m1, mi, m2, op, top, nops, ops, ae, te),        \
8577   TxCM_(m1, pl, m2, op, top, nops, ops, ae, te),        \
8578   TxCM_(m1, vs, m2, op, top, nops, ops, ae, te),        \
8579   TxCM_(m1, vc, m2, op, top, nops, ops, ae, te),        \
8580   TxCM_(m1, hi, m2, op, top, nops, ops, ae, te),        \
8581   TxCM_(m1, ls, m2, op, top, nops, ops, ae, te),        \
8582   TxCM_(m1, ge, m2, op, top, nops, ops, ae, te),        \
8583   TxCM_(m1, lt, m2, op, top, nops, ops, ae, te),        \
8584   TxCM_(m1, gt, m2, op, top, nops, ops, ae, te),        \
8585   TxCM_(m1, le, m2, op, top, nops, ops, ae, te),        \
8586   TxCM_(m1, al, m2, op, top, nops, ops, ae, te)
8587
8588 #define TCM(m1,m2, aop, top, nops, ops, ae, te)         \
8589        TxCM(m1,m2, aop, 0x##top, nops, ops, ae, te)
8590 #define tCM(m1,m2, aop, top, nops, ops, ae, te)                 \
8591        TxCM(m1,m2, aop, T_MNEM_##top, nops, ops, ae, te)
8592
8593 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
8594    field is still 0xE.  */
8595 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
8596   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
8597     THUMB_VARIANT, do_##ae, do_##te }
8598
8599 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
8600    condition code field.  */
8601 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
8602   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
8603     THUMB_VARIANT, do_##ae, do_##te }
8604
8605 /* ARM-only variants of all the above.  */
8606 #define CE(mnem,  op, nops, ops, ae)    \
8607   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
8608
8609 #define C3(mnem, op, nops, ops, ae)     \
8610   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
8611
8612 /* Legacy mnemonics that always have conditional infix after the third
8613    character.  */
8614 #define CL(mnem, op, nops, ops, ae)     \
8615   { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
8616     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
8617
8618 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
8619 #define cCE(mnem,  op, nops, ops, ae)   \
8620   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
8621
8622 /* Legacy coprocessor instructions where conditional infix and conditional
8623    suffix are ambiguous.  For consistency this includes all FPA instructions,
8624    not just the potentially ambiguous ones.  */
8625 #define cCL(mnem, op, nops, ops, ae)    \
8626   { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
8627     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
8628
8629 /* Coprocessor, takes either a suffix or a position-3 infix
8630    (for an FPA corner case). */
8631 #define C3E(mnem, op, nops, ops, ae) \
8632   { #mnem, OPS##nops ops, OT_csuf_or_in3, \
8633     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
8634
8635 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
8636   { #m1 #m2 #m3, OPS##nops ops, \
8637     sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
8638     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
8639
8640 #define CM(m1, m2, op, nops, ops, ae)   \
8641   xCM_(m1,   , m2, op, nops, ops, ae),  \
8642   xCM_(m1, eq, m2, op, nops, ops, ae),  \
8643   xCM_(m1, ne, m2, op, nops, ops, ae),  \
8644   xCM_(m1, cs, m2, op, nops, ops, ae),  \
8645   xCM_(m1, hs, m2, op, nops, ops, ae),  \
8646   xCM_(m1, cc, m2, op, nops, ops, ae),  \
8647   xCM_(m1, ul, m2, op, nops, ops, ae),  \
8648   xCM_(m1, lo, m2, op, nops, ops, ae),  \
8649   xCM_(m1, mi, m2, op, nops, ops, ae),  \
8650   xCM_(m1, pl, m2, op, nops, ops, ae),  \
8651   xCM_(m1, vs, m2, op, nops, ops, ae),  \
8652   xCM_(m1, vc, m2, op, nops, ops, ae),  \
8653   xCM_(m1, hi, m2, op, nops, ops, ae),  \
8654   xCM_(m1, ls, m2, op, nops, ops, ae),  \
8655   xCM_(m1, ge, m2, op, nops, ops, ae),  \
8656   xCM_(m1, lt, m2, op, nops, ops, ae),  \
8657   xCM_(m1, gt, m2, op, nops, ops, ae),  \
8658   xCM_(m1, le, m2, op, nops, ops, ae),  \
8659   xCM_(m1, al, m2, op, nops, ops, ae)
8660
8661 #define UE(mnem, op, nops, ops, ae)     \
8662   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
8663
8664 #define UF(mnem, op, nops, ops, ae)     \
8665   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
8666
8667 #define do_0 0
8668
8669 /* Thumb-only, unconditional.  */
8670 #define UT(mnem,  op, nops, ops, te) TUE(mnem,  0, op, nops, ops, 0, te)
8671
8672 static const struct asm_opcode insns[] =
8673 {
8674 #define ARM_VARIANT ARM_EXT_V1 /* Core ARM Instructions.  */
8675 #define THUMB_VARIANT ARM_EXT_V4T
8676  tCE(and,       0000000, and,      3, (RR, oRR, SH), arit, t_arit3c),
8677  tC3(ands,      0100000, ands,     3, (RR, oRR, SH), arit, t_arit3c),
8678  tCE(eor,       0200000, eor,      3, (RR, oRR, SH), arit, t_arit3c),
8679  tC3(eors,      0300000, eors,     3, (RR, oRR, SH), arit, t_arit3c),
8680  tCE(sub,       0400000, sub,      3, (RR, oRR, SH), arit, t_add_sub),
8681  tC3(subs,      0500000, subs,     3, (RR, oRR, SH), arit, t_add_sub),
8682  tCE(add,       0800000, add,      3, (RR, oRR, SH), arit, t_add_sub),
8683  tC3(adds,      0900000, adds,     3, (RR, oRR, SH), arit, t_add_sub),
8684  tCE(adc,       0a00000, adc,      3, (RR, oRR, SH), arit, t_arit3c),
8685  tC3(adcs,      0b00000, adcs,     3, (RR, oRR, SH), arit, t_arit3c),
8686  tCE(sbc,       0c00000, sbc,      3, (RR, oRR, SH), arit, t_arit3),
8687  tC3(sbcs,      0d00000, sbcs,     3, (RR, oRR, SH), arit, t_arit3),
8688  tCE(orr,       1800000, orr,      3, (RR, oRR, SH), arit, t_arit3c),
8689  tC3(orrs,      1900000, orrs,     3, (RR, oRR, SH), arit, t_arit3c),
8690  tCE(bic,       1c00000, bic,      3, (RR, oRR, SH), arit, t_arit3),
8691  tC3(bics,      1d00000, bics,     3, (RR, oRR, SH), arit, t_arit3),
8692
8693  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
8694     for setting PSR flag bits.  They are obsolete in V6 and do not
8695     have Thumb equivalents. */
8696  tCE(tst,       1100000, tst,      2, (RR, SH),      cmp,  t_mvn_tst),
8697  tC3(tsts,      1100000, tst,      2, (RR, SH),      cmp,  t_mvn_tst),
8698   CL(tstp,      110f000,           2, (RR, SH),      cmp),
8699  tCE(cmp,       1500000, cmp,      2, (RR, SH),      cmp,  t_mov_cmp),
8700  tC3(cmps,      1500000, cmp,      2, (RR, SH),      cmp,  t_mov_cmp),
8701   CL(cmpp,      150f000,           2, (RR, SH),      cmp),
8702  tCE(cmn,       1700000, cmn,      2, (RR, SH),      cmp,  t_mvn_tst),
8703  tC3(cmns,      1700000, cmn,      2, (RR, SH),      cmp,  t_mvn_tst),
8704   CL(cmnp,      170f000,           2, (RR, SH),      cmp),
8705
8706  tCE(mov,       1a00000, mov,      2, (RR, SH),      mov,  t_mov_cmp),
8707  tC3(movs,      1b00000, movs,     2, (RR, SH),      mov,  t_mov_cmp),
8708  tCE(mvn,       1e00000, mvn,      2, (RR, SH),      mov,  t_mvn_tst),
8709  tC3(mvns,      1f00000, mvns,     2, (RR, SH),      mov,  t_mvn_tst),
8710
8711  tCE(ldr,       4100000, ldr,      2, (RR, ADDR),    ldst, t_ldst),
8712  tC3(ldrb,      4500000, ldrb,     2, (RR, ADDR),    ldst, t_ldst),
8713  tCE(str,       4000000, str,      2, (RR, ADDR),    ldst, t_ldst),
8714  tC3(strb,      4400000, strb,     2, (RR, ADDR),    ldst, t_ldst),
8715
8716  tC3(stmia,     8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
8717  tC3(stmea,     8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
8718  tC3(ldmia,     8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
8719  tC3(ldmfd,     8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
8720
8721  TCE(swi,       f000000, df00,     1, (EXPi),        swi, t_swi),
8722  tCE(b,         a000000, b,        1, (EXPr),        branch, t_branch),
8723  TCE(bl,        b000000, f000f800, 1, (EXPr),        bl, t_branch23),
8724
8725   /* Pseudo ops.  */
8726  tCE(adr,       28f0000, adr,      2, (RR, EXP),     adr,  t_adr),
8727   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
8728  tCE(nop,       1a00000, nop,      1, (oI255c),      nop,  t_nop),
8729
8730   /* Thumb-compatibility pseudo ops.  */
8731  tCE(lsl,       1a00000, lsl,      3, (RR, oRR, SH), shift, t_shift),
8732  tC3(lsls,      1b00000, lsls,     3, (RR, oRR, SH), shift, t_shift),
8733  tCE(lsr,       1a00020, lsr,      3, (RR, oRR, SH), shift, t_shift),
8734  tC3(lsrs,      1b00020, lsrs,     3, (RR, oRR, SH), shift, t_shift),
8735  tCE(asr,       1a00040, asr,      3, (RR, oRR, SH), shift, t_shift),
8736  tC3(asrs,      1b00040, asrs,     3, (RR, oRR, SH), shift, t_shift),
8737  tCE(ror,       1a00060, ror,      3, (RR, oRR, SH), shift, t_shift),
8738  tC3(rors,      1b00060, rors,     3, (RR, oRR, SH), shift, t_shift),
8739  tCE(neg,       2600000, neg,      2, (RR, RR),      rd_rn, t_neg),
8740  tC3(negs,      2700000, negs,     2, (RR, RR),      rd_rn, t_neg),
8741  tCE(push,      92d0000, push,     1, (REGLST),      push_pop, t_push_pop),
8742  tCE(pop,       8bd0000, pop,      1, (REGLST),      push_pop, t_push_pop),
8743
8744 #undef THUMB_VARIANT
8745 #define THUMB_VARIANT ARM_EXT_V6
8746  TCE(cpy,       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
8747
8748  /* V1 instructions with no Thumb analogue prior to V6T2.  */
8749 #undef THUMB_VARIANT
8750 #define THUMB_VARIANT ARM_EXT_V6T2
8751  TCE(rsb,       0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
8752  TC3(rsbs,      0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
8753  TCE(teq,       1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
8754  TC3(teqs,      1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
8755   CL(teqp,      130f000,           2, (RR, SH),      cmp),
8756
8757  TC3(ldrt,      4300000, f8500e00, 2, (RR, ADDR),    ldstt, t_ldstt),
8758  TC3(ldrbt,     4700000, f8300e00, 2, (RR, ADDR),    ldstt, t_ldstt),
8759  TC3(strt,      4200000, f8400e00, 2, (RR, ADDR),    ldstt, t_ldstt),
8760  TC3(strbt,     4600000, f8200e00, 2, (RR, ADDR),    ldstt, t_ldstt),
8761
8762  TC3(stmdb,     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
8763  TC3(stmfd,     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
8764
8765  TC3(ldmdb,     9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
8766  TC3(ldmea,     9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
8767
8768  /* V1 instructions with no Thumb analogue at all.  */
8769   CE(rsc,       0e00000,           3, (RR, oRR, SH), arit),
8770   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
8771
8772   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
8773   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
8774   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
8775   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
8776   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
8777   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
8778   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
8779   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
8780
8781 #undef ARM_VARIANT
8782 #define ARM_VARIANT ARM_EXT_V2  /* ARM 2 - multiplies.  */
8783 #undef THUMB_VARIANT
8784 #define THUMB_VARIANT ARM_EXT_V4T
8785  tCE(mul,       0000090, mul,      3, (RRnpc, RRnpc, oRR), mul, t_mul),
8786  tC3(muls,      0100090, muls,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
8787
8788 #undef THUMB_VARIANT
8789 #define THUMB_VARIANT ARM_EXT_V6T2
8790  TCE(mla,       0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
8791   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
8792
8793   /* Generic coprocessor instructions.  */
8794  TCE(cdp,       e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
8795  TCE(ldc,       c100000, ec100000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8796  TC3(ldcl,      c500000, ec500000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8797  TCE(stc,       c000000, ec000000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8798  TC3(stcl,      c400000, ec400000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8799  TCE(mcr,       e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
8800  TCE(mrc,       e100010, ee100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
8801
8802 #undef ARM_VARIANT
8803 #define ARM_VARIANT ARM_EXT_V2S /* ARM 3 - swp instructions.  */
8804   CE(swp,       1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
8805   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
8806
8807 #undef ARM_VARIANT
8808 #define ARM_VARIANT ARM_EXT_V3  /* ARM 6 Status register instructions.  */
8809  TCE(mrs,       10f0000, f3ef8000, 2, (RR, PSR),     mrs, t_mrs),
8810  TCE(msr,       120f000, f3808000, 2, (PSR, RR_EXi), msr, t_msr),
8811
8812 #undef ARM_VARIANT
8813 #define ARM_VARIANT ARM_EXT_V3M  /* ARM 7M long multiplies.  */
8814  TCE(smull,     0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
8815   CM(smull,s,   0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
8816  TCE(umull,     0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
8817   CM(umull,s,   0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
8818  TCE(smlal,     0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
8819   CM(smlal,s,   0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
8820  TCE(umlal,     0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
8821   CM(umlal,s,   0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
8822
8823 #undef ARM_VARIANT
8824 #define ARM_VARIANT ARM_EXT_V4  /* ARM Architecture 4.  */
8825 #undef THUMB_VARIANT
8826 #define THUMB_VARIANT ARM_EXT_V4T
8827  tC3(ldrh,      01000b0, ldrh,     2, (RR, ADDR), ldstv4, t_ldst),
8828  tC3(strh,      00000b0, strh,     2, (RR, ADDR), ldstv4, t_ldst),
8829  tC3(ldrsh,     01000f0, ldrsh,    2, (RR, ADDR), ldstv4, t_ldst),
8830  tC3(ldrsb,     01000d0, ldrsb,    2, (RR, ADDR), ldstv4, t_ldst),
8831  tCM(ld,sh,     01000f0, ldrsh,    2, (RR, ADDR), ldstv4, t_ldst),
8832  tCM(ld,sb,     01000d0, ldrsb,    2, (RR, ADDR), ldstv4, t_ldst),
8833
8834 #undef ARM_VARIANT
8835 #define ARM_VARIANT ARM_EXT_V4T|ARM_EXT_V5
8836   /* ARM Architecture 4T.  */
8837   /* Note: bx (and blx) are required on V5, even if the processor does
8838      not support Thumb.  */
8839  TCE(bx,        12fff10, 4700, 1, (RR), bx, t_bx),
8840
8841 #undef ARM_VARIANT
8842 #define ARM_VARIANT ARM_EXT_V5 /*  ARM Architecture 5T.  */
8843 #undef THUMB_VARIANT
8844 #define THUMB_VARIANT ARM_EXT_V5T
8845   /* Note: blx has 2 variants; the .value coded here is for
8846      BLX(2).  Only this variant has conditional execution.  */
8847  TCE(blx,       12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
8848  TUE(bkpt,      1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
8849
8850 #undef THUMB_VARIANT
8851 #define THUMB_VARIANT ARM_EXT_V6T2
8852  TCE(clz,       16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
8853  TUF(ldc2,      c100000, fc100000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8854  TUF(ldc2l,     c500000, fc500000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8855  TUF(stc2,      c000000, fc000000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8856  TUF(stc2l,     c400000, fc400000, 3, (RCP, RCN, ADDR),                 lstc,   lstc),
8857  TUF(cdp2,      e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
8858  TUF(mcr2,      e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
8859  TUF(mrc2,      e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
8860
8861 #undef ARM_VARIANT
8862 #define ARM_VARIANT ARM_EXT_V5ExP /*  ARM Architecture 5TExP.  */
8863  TCE(smlabb,    1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8864  TCE(smlatb,    10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8865  TCE(smlabt,    10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8866  TCE(smlatt,    10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8867
8868  TCE(smlawb,    1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8869  TCE(smlawt,    12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
8870
8871  TCE(smlalbb,   1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
8872  TCE(smlaltb,   14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
8873  TCE(smlalbt,   14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
8874  TCE(smlaltt,   14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
8875
8876  TCE(smulbb,    1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8877  TCE(smultb,    16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8878  TCE(smulbt,    16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8879  TCE(smultt,    16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8880
8881  TCE(smulwb,    12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8882  TCE(smulwt,    12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
8883
8884  TCE(qadd,      1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
8885  TCE(qdadd,     1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
8886  TCE(qsub,      1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
8887  TCE(qdsub,     1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, rd_rm_rn),
8888
8889 #undef ARM_VARIANT
8890 #define ARM_VARIANT ARM_EXT_V5E /*  ARM Architecture 5TE.  */
8891  TUF(pld,       450f000, f810f000, 1, (ADDR),                pld,  t_pld),
8892  TC3(ldrd,      00000d0, e9500000, 3, (RRnpc, oRRnpc, ADDR), ldrd, t_ldstd),
8893  TC3(strd,      00000f0, e9400000, 3, (RRnpc, oRRnpc, ADDR), ldrd, t_ldstd),
8894
8895  TCE(mcrr,      c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
8896  TCE(mrrc,      c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
8897
8898 #undef ARM_VARIANT
8899 #define ARM_VARIANT ARM_EXT_V5J /*  ARM Architecture 5TEJ.  */
8900  TCE(bxj,       12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
8901
8902 #undef ARM_VARIANT
8903 #define ARM_VARIANT ARM_EXT_V6 /*  ARM V6.  */
8904 #undef THUMB_VARIANT
8905 #define THUMB_VARIANT ARM_EXT_V6
8906  TUF(cpsie,     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
8907  TUF(cpsid,     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
8908  tCE(rev,       6bf0f30, rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
8909  tCE(rev16,     6bf0fb0, rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
8910  tCE(revsh,     6ff0fb0, revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
8911  tCE(sxth,      6bf0070, sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
8912  tCE(uxth,      6ff0070, uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
8913  tCE(sxtb,      6af0070, sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
8914  tCE(uxtb,      6ef0070, uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
8915  TUF(setend,    1010000, b650,     1, (ENDI),                     setend, t_setend),
8916
8917 #undef THUMB_VARIANT
8918 #define THUMB_VARIANT ARM_EXT_V6T2
8919  TUF(cps,       1020000, f3af8100, 1, (I31b),                     imm0, imm0),
8920  TCE(ldrex,     1900f9f, e8500f00, 2, (RRnpc, ADDR),              ldrex, t_ldrex),
8921  TUF(mcrr2,     c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
8922  TUF(mrrc2,     c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
8923  TCE(pkhbt,     6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
8924  TCE(pkhtb,     6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
8925  TCE(qadd16,    6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8926  TCE(qadd8,     6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8927  TCE(qaddsubx,  6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8928  TCE(qsub16,    6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8929  TCE(qsub8,     6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8930  TCE(qsubaddx,  6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8931  TCE(sadd16,    6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8932  TCE(sadd8,     6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8933  TCE(saddsubx,  6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8934  TCE(shadd16,   6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8935  TCE(shadd8,    6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8936  TCE(shaddsubx, 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8937  TCE(shsub16,   6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8938  TCE(shsub8,    6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8939  TCE(shsubaddx, 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8940  TCE(ssub16,    6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8941  TCE(ssub8,     6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8942  TCE(ssubaddx,  6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8943  TCE(uadd16,    6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8944  TCE(uadd8,     6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8945  TCE(uaddsubx,  6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8946  TCE(uhadd16,   6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8947  TCE(uhadd8,    6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8948  TCE(uhaddsubx, 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8949  TCE(uhsub16,   6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8950  TCE(uhsub8,    6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8951  TCE(uhsubaddx, 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8952  TCE(uqadd16,   6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8953  TCE(uqadd8,    6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8954  TCE(uqaddsubx, 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8955  TCE(uqsub16,   6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8956  TCE(uqsub8,    6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8957  TCE(uqsubaddx, 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8958  TCE(usub16,    6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8959  TCE(usub8,     6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8960  TCE(usubaddx,  6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8961  TUF(rfeia,     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
8962   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
8963   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
8964  TUF(rfedb,     9100a00, e810c000, 1, (RRw),                       rfe, rfe),
8965  TUF(rfefd,     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
8966   UF(rfefa,     9900a00,           1, (RRw),                       rfe),
8967   UF(rfeea,     8100a00,           1, (RRw),                       rfe),
8968  TUF(rfeed,     9100a00, e810c000, 1, (RRw),                       rfe, rfe),
8969  TCE(sxtah,     6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
8970  TCE(sxtab16,   6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
8971  TCE(sxtab,     6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
8972  TCE(sxtb16,    68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
8973  TCE(uxtah,     6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
8974  TCE(uxtab16,   6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
8975  TCE(uxtab,     6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
8976  TCE(uxtb16,    6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
8977  TCE(sel,       6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
8978  TCE(smlad,     7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
8979  TCE(smladx,    7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
8980  TCE(smlald,    7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
8981  TCE(smlaldx,   7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
8982  TCE(smlsd,     7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
8983  TCE(smlsdx,    7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
8984  TCE(smlsld,    7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
8985  TCE(smlsldx,   7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
8986  TCE(smmla,     7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
8987  TCE(smmlar,    7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
8988  TCE(smmls,     75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
8989  TCE(smmlsr,    75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
8990  TCE(smmul,     750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
8991  TCE(smmulr,    750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
8992  TCE(smuad,     700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
8993  TCE(smuadx,    700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
8994  TCE(smusd,     700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
8995  TCE(smusdx,    700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
8996  TUF(srsia,     8cd0500, e980c000, 1, (I31w),                      srs,  srs),
8997   UF(srsib,     9cd0500,           1, (I31w),                      srs),
8998   UF(srsda,     84d0500,           1, (I31w),                      srs),
8999  TUF(srsdb,     94d0500, e800c000, 1, (I31w),                      srs,  srs),
9000  TCE(ssat,      6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
9001  TCE(ssat16,    6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
9002  TCE(strex,     1800f90, e8400000, 3, (RRnpc, RRnpc, ADDR),        strex,  t_strex),
9003  TCE(umaal,     0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
9004  TCE(usad8,     780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
9005  TCE(usada8,    7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
9006  TCE(usat,      6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
9007  TCE(usat16,    6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
9008
9009 #undef ARM_VARIANT
9010 #define ARM_VARIANT ARM_EXT_V6K
9011 #undef THUMB_VARIANT
9012 #define THUMB_VARIANT ARM_EXT_V6K
9013  tCE(yield,     320f001, yield,    0, (), noargs, t_hint),
9014  tCE(wfe,       320f002, wfe,      0, (), noargs, t_hint),
9015  tCE(wfi,       320f003, wfi,      0, (), noargs, t_hint),
9016  tCE(sev,       320f004, sev,      0, (), noargs, t_hint),
9017
9018 #undef THUMB_VARIANT
9019 #define THUMB_VARIANT ARM_EXT_V6T2
9020  TCE(ldrexb,    1d00f9f, e8d00f4f, 2, (RRnpc, RRnpcb),                rd_rn,  rd_rn),
9021  TCE(ldrexh,    1f00f9f, e8d00f5f, 2, (RRnpc, RRnpcb),                rd_rn,  rd_rn),
9022  TCE(ldrexd,    1b00f9f, e8d0007f, 3, (RRnpc, oRRnpc, RRnpcb),        ldrexd, t_ldrexd),
9023  TCE(strexb,    1c00f90, e8c00f40, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
9024  TCE(strexh,    1e00f90, e8c00f50, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
9025  TCE(strexd,    1a00f90, e8c00070, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb), strexd, t_strexd),
9026  TUF(clrex,     57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
9027
9028 #undef ARM_VARIANT
9029 #define ARM_VARIANT ARM_EXT_V6Z
9030  TCE(smc,       1600070, f7f08000, 1, (EXPi), smc, t_smc),
9031
9032 #undef ARM_VARIANT
9033 #define ARM_VARIANT ARM_EXT_V6T2
9034  TCE(bfc,       7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
9035  TCE(bfi,       7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
9036  TCE(sbfx,      7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
9037  TCE(ubfx,      7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
9038
9039  TCE(mls,       0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
9040  TCE(movw,      3000000, f2400000, 2, (RRnpc, Iffff),               mov16, t_mov16),
9041  TCE(movt,      3400000, f2c00000, 2, (RRnpc, Iffff),               mov16, t_mov16),
9042  TCE(rbit,      3ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
9043
9044  TC3(ldrht,     03000b0, f8300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9045  TC3(ldrsht,    03000f0, f9300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9046  TC3(ldrsbt,    03000d0, f9100e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9047  TC3(strht,     02000b0, f8200e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9048
9049   UT(cbnz,      b900,    2, (RR, EXP), t_czb),
9050   UT(cbz,       b100,    2, (RR, EXP), t_czb),
9051  /* ARM does not really have an IT instruction.  */
9052  TUE(it,        0, bf08, 1, (COND),    it, t_it),
9053  TUE(itt,       0, bf0c, 1, (COND),    it, t_it),
9054  TUE(ite,       0, bf04, 1, (COND),    it, t_it),
9055  TUE(ittt,      0, bf0e, 1, (COND),    it, t_it),
9056  TUE(itet,      0, bf06, 1, (COND),    it, t_it),
9057  TUE(itte,      0, bf0a, 1, (COND),    it, t_it),
9058  TUE(itee,      0, bf02, 1, (COND),    it, t_it),
9059  TUE(itttt,     0, bf0f, 1, (COND),    it, t_it),
9060  TUE(itett,     0, bf07, 1, (COND),    it, t_it),
9061  TUE(ittet,     0, bf0b, 1, (COND),    it, t_it),
9062  TUE(iteet,     0, bf03, 1, (COND),    it, t_it),
9063  TUE(ittte,     0, bf0d, 1, (COND),    it, t_it),
9064  TUE(itete,     0, bf05, 1, (COND),    it, t_it),
9065  TUE(ittee,     0, bf09, 1, (COND),    it, t_it),
9066  TUE(iteee,     0, bf01, 1, (COND),    it, t_it),
9067
9068  /* Thumb2 only instructions.  */
9069 #undef ARM_VARIANT
9070 #define ARM_VARIANT 0
9071
9072  TCE(addw,      0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
9073  TCE(subw,      0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
9074  TCE(tbb,       0, e8d0f000, 1, (TB), 0, t_tb),
9075  TCE(tbh,       0, e8d0f010, 1, (TB), 0, t_tb),
9076
9077 #undef ARM_VARIANT
9078 #define ARM_VARIANT FPU_FPA_EXT_V1  /* Core FPA instruction set (V1).  */
9079  cCE(wfs,       e200110, 1, (RR),            rd),
9080  cCE(rfs,       e300110, 1, (RR),            rd),
9081  cCE(wfc,       e400110, 1, (RR),            rd),
9082  cCE(rfc,       e500110, 1, (RR),            rd),
9083
9084  cCL(ldfs,      c100100, 2, (RF, ADDR),      rd_cpaddr),
9085  cCL(ldfd,      c108100, 2, (RF, ADDR),      rd_cpaddr),
9086  cCL(ldfe,      c500100, 2, (RF, ADDR),      rd_cpaddr),
9087  cCL(ldfp,      c508100, 2, (RF, ADDR),      rd_cpaddr),
9088
9089  cCL(stfs,      c000100, 2, (RF, ADDR),      rd_cpaddr),
9090  cCL(stfd,      c008100, 2, (RF, ADDR),      rd_cpaddr),
9091  cCL(stfe,      c400100, 2, (RF, ADDR),      rd_cpaddr),
9092  cCL(stfp,      c408100, 2, (RF, ADDR),      rd_cpaddr),
9093
9094  cCL(mvfs,      e008100, 2, (RF, RF_IF),     rd_rm),
9095  cCL(mvfsp,     e008120, 2, (RF, RF_IF),     rd_rm),
9096  cCL(mvfsm,     e008140, 2, (RF, RF_IF),     rd_rm),
9097  cCL(mvfsz,     e008160, 2, (RF, RF_IF),     rd_rm),
9098  cCL(mvfd,      e008180, 2, (RF, RF_IF),     rd_rm),
9099  cCL(mvfdp,     e0081a0, 2, (RF, RF_IF),     rd_rm),
9100  cCL(mvfdm,     e0081c0, 2, (RF, RF_IF),     rd_rm),
9101  cCL(mvfdz,     e0081e0, 2, (RF, RF_IF),     rd_rm),
9102  cCL(mvfe,      e088100, 2, (RF, RF_IF),     rd_rm),
9103  cCL(mvfep,     e088120, 2, (RF, RF_IF),     rd_rm),
9104  cCL(mvfem,     e088140, 2, (RF, RF_IF),     rd_rm),
9105  cCL(mvfez,     e088160, 2, (RF, RF_IF),     rd_rm),
9106
9107  cCL(mnfs,      e108100, 2, (RF, RF_IF),     rd_rm),
9108  cCL(mnfsp,     e108120, 2, (RF, RF_IF),     rd_rm),
9109  cCL(mnfsm,     e108140, 2, (RF, RF_IF),     rd_rm),
9110  cCL(mnfsz,     e108160, 2, (RF, RF_IF),     rd_rm),
9111  cCL(mnfd,      e108180, 2, (RF, RF_IF),     rd_rm),
9112  cCL(mnfdp,     e1081a0, 2, (RF, RF_IF),     rd_rm),
9113  cCL(mnfdm,     e1081c0, 2, (RF, RF_IF),     rd_rm),
9114  cCL(mnfdz,     e1081e0, 2, (RF, RF_IF),     rd_rm),
9115  cCL(mnfe,      e188100, 2, (RF, RF_IF),     rd_rm),
9116  cCL(mnfep,     e188120, 2, (RF, RF_IF),     rd_rm),
9117  cCL(mnfem,     e188140, 2, (RF, RF_IF),     rd_rm),
9118  cCL(mnfez,     e188160, 2, (RF, RF_IF),     rd_rm),
9119
9120  cCL(abss,      e208100, 2, (RF, RF_IF),     rd_rm),
9121  cCL(abssp,     e208120, 2, (RF, RF_IF),     rd_rm),
9122  cCL(abssm,     e208140, 2, (RF, RF_IF),     rd_rm),
9123  cCL(abssz,     e208160, 2, (RF, RF_IF),     rd_rm),
9124  cCL(absd,      e208180, 2, (RF, RF_IF),     rd_rm),
9125  cCL(absdp,     e2081a0, 2, (RF, RF_IF),     rd_rm),
9126  cCL(absdm,     e2081c0, 2, (RF, RF_IF),     rd_rm),
9127  cCL(absdz,     e2081e0, 2, (RF, RF_IF),     rd_rm),
9128  cCL(abse,      e288100, 2, (RF, RF_IF),     rd_rm),
9129  cCL(absep,     e288120, 2, (RF, RF_IF),     rd_rm),
9130  cCL(absem,     e288140, 2, (RF, RF_IF),     rd_rm),
9131  cCL(absez,     e288160, 2, (RF, RF_IF),     rd_rm),
9132
9133  cCL(rnds,      e308100, 2, (RF, RF_IF),     rd_rm),
9134  cCL(rndsp,     e308120, 2, (RF, RF_IF),     rd_rm),
9135  cCL(rndsm,     e308140, 2, (RF, RF_IF),     rd_rm),
9136  cCL(rndsz,     e308160, 2, (RF, RF_IF),     rd_rm),
9137  cCL(rndd,      e308180, 2, (RF, RF_IF),     rd_rm),
9138  cCL(rnddp,     e3081a0, 2, (RF, RF_IF),     rd_rm),
9139  cCL(rnddm,     e3081c0, 2, (RF, RF_IF),     rd_rm),
9140  cCL(rnddz,     e3081e0, 2, (RF, RF_IF),     rd_rm),
9141  cCL(rnde,      e388100, 2, (RF, RF_IF),     rd_rm),
9142  cCL(rndep,     e388120, 2, (RF, RF_IF),     rd_rm),
9143  cCL(rndem,     e388140, 2, (RF, RF_IF),     rd_rm),
9144  cCL(rndez,     e388160, 2, (RF, RF_IF),     rd_rm),
9145
9146  cCL(sqts,      e408100, 2, (RF, RF_IF),     rd_rm),
9147  cCL(sqtsp,     e408120, 2, (RF, RF_IF),     rd_rm),
9148  cCL(sqtsm,     e408140, 2, (RF, RF_IF),     rd_rm),
9149  cCL(sqtsz,     e408160, 2, (RF, RF_IF),     rd_rm),
9150  cCL(sqtd,      e408180, 2, (RF, RF_IF),     rd_rm),
9151  cCL(sqtdp,     e4081a0, 2, (RF, RF_IF),     rd_rm),
9152  cCL(sqtdm,     e4081c0, 2, (RF, RF_IF),     rd_rm),
9153  cCL(sqtdz,     e4081e0, 2, (RF, RF_IF),     rd_rm),
9154  cCL(sqte,      e488100, 2, (RF, RF_IF),     rd_rm),
9155  cCL(sqtep,     e488120, 2, (RF, RF_IF),     rd_rm),
9156  cCL(sqtem,     e488140, 2, (RF, RF_IF),     rd_rm),
9157  cCL(sqtez,     e488160, 2, (RF, RF_IF),     rd_rm),
9158
9159  cCL(logs,      e508100, 2, (RF, RF_IF),     rd_rm),
9160  cCL(logsp,     e508120, 2, (RF, RF_IF),     rd_rm),
9161  cCL(logsm,     e508140, 2, (RF, RF_IF),     rd_rm),
9162  cCL(logsz,     e508160, 2, (RF, RF_IF),     rd_rm),
9163  cCL(logd,      e508180, 2, (RF, RF_IF),     rd_rm),
9164  cCL(logdp,     e5081a0, 2, (RF, RF_IF),     rd_rm),
9165  cCL(logdm,     e5081c0, 2, (RF, RF_IF),     rd_rm),
9166  cCL(logdz,     e5081e0, 2, (RF, RF_IF),     rd_rm),
9167  cCL(loge,      e588100, 2, (RF, RF_IF),     rd_rm),
9168  cCL(logep,     e588120, 2, (RF, RF_IF),     rd_rm),
9169  cCL(logem,     e588140, 2, (RF, RF_IF),     rd_rm),
9170  cCL(logez,     e588160, 2, (RF, RF_IF),     rd_rm),
9171
9172  cCL(lgns,      e608100, 2, (RF, RF_IF),     rd_rm),
9173  cCL(lgnsp,     e608120, 2, (RF, RF_IF),     rd_rm),
9174  cCL(lgnsm,     e608140, 2, (RF, RF_IF),     rd_rm),
9175  cCL(lgnsz,     e608160, 2, (RF, RF_IF),     rd_rm),
9176  cCL(lgnd,      e608180, 2, (RF, RF_IF),     rd_rm),
9177  cCL(lgndp,     e6081a0, 2, (RF, RF_IF),     rd_rm),
9178  cCL(lgndm,     e6081c0, 2, (RF, RF_IF),     rd_rm),
9179  cCL(lgndz,     e6081e0, 2, (RF, RF_IF),     rd_rm),
9180  cCL(lgne,      e688100, 2, (RF, RF_IF),     rd_rm),
9181  cCL(lgnep,     e688120, 2, (RF, RF_IF),     rd_rm),
9182  cCL(lgnem,     e688140, 2, (RF, RF_IF),     rd_rm),
9183  cCL(lgnez,     e688160, 2, (RF, RF_IF),     rd_rm),
9184
9185  cCL(exps,      e708100, 2, (RF, RF_IF),     rd_rm),
9186  cCL(expsp,     e708120, 2, (RF, RF_IF),     rd_rm),
9187  cCL(expsm,     e708140, 2, (RF, RF_IF),     rd_rm),
9188  cCL(expsz,     e708160, 2, (RF, RF_IF),     rd_rm),
9189  cCL(expd,      e708180, 2, (RF, RF_IF),     rd_rm),
9190  cCL(expdp,     e7081a0, 2, (RF, RF_IF),     rd_rm),
9191  cCL(expdm,     e7081c0, 2, (RF, RF_IF),     rd_rm),
9192  cCL(expdz,     e7081e0, 2, (RF, RF_IF),     rd_rm),
9193  cCL(expe,      e788100, 2, (RF, RF_IF),     rd_rm),
9194  cCL(expep,     e788120, 2, (RF, RF_IF),     rd_rm),
9195  cCL(expem,     e788140, 2, (RF, RF_IF),     rd_rm),
9196  cCL(expdz,     e788160, 2, (RF, RF_IF),     rd_rm),
9197
9198  cCL(sins,      e808100, 2, (RF, RF_IF),     rd_rm),
9199  cCL(sinsp,     e808120, 2, (RF, RF_IF),     rd_rm),
9200  cCL(sinsm,     e808140, 2, (RF, RF_IF),     rd_rm),
9201  cCL(sinsz,     e808160, 2, (RF, RF_IF),     rd_rm),
9202  cCL(sind,      e808180, 2, (RF, RF_IF),     rd_rm),
9203  cCL(sindp,     e8081a0, 2, (RF, RF_IF),     rd_rm),
9204  cCL(sindm,     e8081c0, 2, (RF, RF_IF),     rd_rm),
9205  cCL(sindz,     e8081e0, 2, (RF, RF_IF),     rd_rm),
9206  cCL(sine,      e888100, 2, (RF, RF_IF),     rd_rm),
9207  cCL(sinep,     e888120, 2, (RF, RF_IF),     rd_rm),
9208  cCL(sinem,     e888140, 2, (RF, RF_IF),     rd_rm),
9209  cCL(sinez,     e888160, 2, (RF, RF_IF),     rd_rm),
9210
9211  cCL(coss,      e908100, 2, (RF, RF_IF),     rd_rm),
9212  cCL(cossp,     e908120, 2, (RF, RF_IF),     rd_rm),
9213  cCL(cossm,     e908140, 2, (RF, RF_IF),     rd_rm),
9214  cCL(cossz,     e908160, 2, (RF, RF_IF),     rd_rm),
9215  cCL(cosd,      e908180, 2, (RF, RF_IF),     rd_rm),
9216  cCL(cosdp,     e9081a0, 2, (RF, RF_IF),     rd_rm),
9217  cCL(cosdm,     e9081c0, 2, (RF, RF_IF),     rd_rm),
9218  cCL(cosdz,     e9081e0, 2, (RF, RF_IF),     rd_rm),
9219  cCL(cose,      e988100, 2, (RF, RF_IF),     rd_rm),
9220  cCL(cosep,     e988120, 2, (RF, RF_IF),     rd_rm),
9221  cCL(cosem,     e988140, 2, (RF, RF_IF),     rd_rm),
9222  cCL(cosez,     e988160, 2, (RF, RF_IF),     rd_rm),
9223
9224  cCL(tans,      ea08100, 2, (RF, RF_IF),     rd_rm),
9225  cCL(tansp,     ea08120, 2, (RF, RF_IF),     rd_rm),
9226  cCL(tansm,     ea08140, 2, (RF, RF_IF),     rd_rm),
9227  cCL(tansz,     ea08160, 2, (RF, RF_IF),     rd_rm),
9228  cCL(tand,      ea08180, 2, (RF, RF_IF),     rd_rm),
9229  cCL(tandp,     ea081a0, 2, (RF, RF_IF),     rd_rm),
9230  cCL(tandm,     ea081c0, 2, (RF, RF_IF),     rd_rm),
9231  cCL(tandz,     ea081e0, 2, (RF, RF_IF),     rd_rm),
9232  cCL(tane,      ea88100, 2, (RF, RF_IF),     rd_rm),
9233  cCL(tanep,     ea88120, 2, (RF, RF_IF),     rd_rm),
9234  cCL(tanem,     ea88140, 2, (RF, RF_IF),     rd_rm),
9235  cCL(tanez,     ea88160, 2, (RF, RF_IF),     rd_rm),
9236
9237  cCL(asns,      eb08100, 2, (RF, RF_IF),     rd_rm),
9238  cCL(asnsp,     eb08120, 2, (RF, RF_IF),     rd_rm),
9239  cCL(asnsm,     eb08140, 2, (RF, RF_IF),     rd_rm),
9240  cCL(asnsz,     eb08160, 2, (RF, RF_IF),     rd_rm),
9241  cCL(asnd,      eb08180, 2, (RF, RF_IF),     rd_rm),
9242  cCL(asndp,     eb081a0, 2, (RF, RF_IF),     rd_rm),
9243  cCL(asndm,     eb081c0, 2, (RF, RF_IF),     rd_rm),
9244  cCL(asndz,     eb081e0, 2, (RF, RF_IF),     rd_rm),
9245  cCL(asne,      eb88100, 2, (RF, RF_IF),     rd_rm),
9246  cCL(asnep,     eb88120, 2, (RF, RF_IF),     rd_rm),
9247  cCL(asnem,     eb88140, 2, (RF, RF_IF),     rd_rm),
9248  cCL(asnez,     eb88160, 2, (RF, RF_IF),     rd_rm),
9249
9250  cCL(acss,      ec08100, 2, (RF, RF_IF),     rd_rm),
9251  cCL(acssp,     ec08120, 2, (RF, RF_IF),     rd_rm),
9252  cCL(acssm,     ec08140, 2, (RF, RF_IF),     rd_rm),
9253  cCL(acssz,     ec08160, 2, (RF, RF_IF),     rd_rm),
9254  cCL(acsd,      ec08180, 2, (RF, RF_IF),     rd_rm),
9255  cCL(acsdp,     ec081a0, 2, (RF, RF_IF),     rd_rm),
9256  cCL(acsdm,     ec081c0, 2, (RF, RF_IF),     rd_rm),
9257  cCL(acsdz,     ec081e0, 2, (RF, RF_IF),     rd_rm),
9258  cCL(acse,      ec88100, 2, (RF, RF_IF),     rd_rm),
9259  cCL(acsep,     ec88120, 2, (RF, RF_IF),     rd_rm),
9260  cCL(acsem,     ec88140, 2, (RF, RF_IF),     rd_rm),
9261  cCL(acsez,     ec88160, 2, (RF, RF_IF),     rd_rm),
9262
9263  cCL(atns,      ed08100, 2, (RF, RF_IF),     rd_rm),
9264  cCL(atnsp,     ed08120, 2, (RF, RF_IF),     rd_rm),
9265  cCL(atnsm,     ed08140, 2, (RF, RF_IF),     rd_rm),
9266  cCL(atnsz,     ed08160, 2, (RF, RF_IF),     rd_rm),
9267  cCL(atnd,      ed08180, 2, (RF, RF_IF),     rd_rm),
9268  cCL(atndp,     ed081a0, 2, (RF, RF_IF),     rd_rm),
9269  cCL(atndm,     ed081c0, 2, (RF, RF_IF),     rd_rm),
9270  cCL(atndz,     ed081e0, 2, (RF, RF_IF),     rd_rm),
9271  cCL(atne,      ed88100, 2, (RF, RF_IF),     rd_rm),
9272  cCL(atnep,     ed88120, 2, (RF, RF_IF),     rd_rm),
9273  cCL(atnem,     ed88140, 2, (RF, RF_IF),     rd_rm),
9274  cCL(atnez,     ed88160, 2, (RF, RF_IF),     rd_rm),
9275
9276  cCL(urds,      ee08100, 2, (RF, RF_IF),     rd_rm),
9277  cCL(urdsp,     ee08120, 2, (RF, RF_IF),     rd_rm),
9278  cCL(urdsm,     ee08140, 2, (RF, RF_IF),     rd_rm),
9279  cCL(urdsz,     ee08160, 2, (RF, RF_IF),     rd_rm),
9280  cCL(urdd,      ee08180, 2, (RF, RF_IF),     rd_rm),
9281  cCL(urddp,     ee081a0, 2, (RF, RF_IF),     rd_rm),
9282  cCL(urddm,     ee081c0, 2, (RF, RF_IF),     rd_rm),
9283  cCL(urddz,     ee081e0, 2, (RF, RF_IF),     rd_rm),
9284  cCL(urde,      ee88100, 2, (RF, RF_IF),     rd_rm),
9285  cCL(urdep,     ee88120, 2, (RF, RF_IF),     rd_rm),
9286  cCL(urdem,     ee88140, 2, (RF, RF_IF),     rd_rm),
9287  cCL(urdez,     ee88160, 2, (RF, RF_IF),     rd_rm),
9288
9289  cCL(nrms,      ef08100, 2, (RF, RF_IF),     rd_rm),
9290  cCL(nrmsp,     ef08120, 2, (RF, RF_IF),     rd_rm),
9291  cCL(nrmsm,     ef08140, 2, (RF, RF_IF),     rd_rm),
9292  cCL(nrmsz,     ef08160, 2, (RF, RF_IF),     rd_rm),
9293  cCL(nrmd,      ef08180, 2, (RF, RF_IF),     rd_rm),
9294  cCL(nrmdp,     ef081a0, 2, (RF, RF_IF),     rd_rm),
9295  cCL(nrmdm,     ef081c0, 2, (RF, RF_IF),     rd_rm),
9296  cCL(nrmdz,     ef081e0, 2, (RF, RF_IF),     rd_rm),
9297  cCL(nrme,      ef88100, 2, (RF, RF_IF),     rd_rm),
9298  cCL(nrmep,     ef88120, 2, (RF, RF_IF),     rd_rm),
9299  cCL(nrmem,     ef88140, 2, (RF, RF_IF),     rd_rm),
9300  cCL(nrmez,     ef88160, 2, (RF, RF_IF),     rd_rm),
9301
9302  cCL(adfs,      e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
9303  cCL(adfsp,     e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
9304  cCL(adfsm,     e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
9305  cCL(adfsz,     e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
9306  cCL(adfd,      e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
9307  cCL(adfdp,     e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9308  cCL(adfdm,     e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9309  cCL(adfdz,     e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9310  cCL(adfe,      e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
9311  cCL(adfep,     e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
9312  cCL(adfem,     e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
9313  cCL(adfez,     e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
9314
9315  cCL(sufs,      e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
9316  cCL(sufsp,     e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
9317  cCL(sufsm,     e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
9318  cCL(sufsz,     e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
9319  cCL(sufd,      e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
9320  cCL(sufdp,     e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9321  cCL(sufdm,     e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9322  cCL(sufdz,     e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9323  cCL(sufe,      e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
9324  cCL(sufep,     e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
9325  cCL(sufem,     e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
9326  cCL(sufez,     e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
9327
9328  cCL(rsfs,      e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
9329  cCL(rsfsp,     e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
9330  cCL(rsfsm,     e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
9331  cCL(rsfsz,     e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
9332  cCL(rsfd,      e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
9333  cCL(rsfdp,     e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9334  cCL(rsfdm,     e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9335  cCL(rsfdz,     e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9336  cCL(rsfe,      e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
9337  cCL(rsfep,     e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
9338  cCL(rsfem,     e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
9339  cCL(rsfez,     e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
9340
9341  cCL(mufs,      e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
9342  cCL(mufsp,     e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
9343  cCL(mufsm,     e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
9344  cCL(mufsz,     e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
9345  cCL(mufd,      e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
9346  cCL(mufdp,     e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9347  cCL(mufdm,     e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9348  cCL(mufdz,     e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9349  cCL(mufe,      e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
9350  cCL(mufep,     e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
9351  cCL(mufem,     e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
9352  cCL(mufez,     e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
9353
9354  cCL(dvfs,      e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
9355  cCL(dvfsp,     e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
9356  cCL(dvfsm,     e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
9357  cCL(dvfsz,     e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
9358  cCL(dvfd,      e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
9359  cCL(dvfdp,     e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9360  cCL(dvfdm,     e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9361  cCL(dvfdz,     e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9362  cCL(dvfe,      e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
9363  cCL(dvfep,     e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
9364  cCL(dvfem,     e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
9365  cCL(dvfez,     e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
9366
9367  cCL(rdfs,      e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
9368  cCL(rdfsp,     e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
9369  cCL(rdfsm,     e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
9370  cCL(rdfsz,     e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
9371  cCL(rdfd,      e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
9372  cCL(rdfdp,     e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9373  cCL(rdfdm,     e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9374  cCL(rdfdz,     e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9375  cCL(rdfe,      e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
9376  cCL(rdfep,     e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
9377  cCL(rdfem,     e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
9378  cCL(rdfez,     e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
9379
9380  cCL(pows,      e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
9381  cCL(powsp,     e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
9382  cCL(powsm,     e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
9383  cCL(powsz,     e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
9384  cCL(powd,      e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
9385  cCL(powdp,     e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9386  cCL(powdm,     e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9387  cCL(powdz,     e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9388  cCL(powe,      e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
9389  cCL(powep,     e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
9390  cCL(powem,     e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
9391  cCL(powez,     e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
9392
9393  cCL(rpws,      e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
9394  cCL(rpwsp,     e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
9395  cCL(rpwsm,     e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
9396  cCL(rpwsz,     e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
9397  cCL(rpwd,      e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
9398  cCL(rpwdp,     e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9399  cCL(rpwdm,     e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9400  cCL(rpwdz,     e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9401  cCL(rpwe,      e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
9402  cCL(rpwep,     e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
9403  cCL(rpwem,     e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
9404  cCL(rpwez,     e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
9405
9406  cCL(rmfs,      e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
9407  cCL(rmfsp,     e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
9408  cCL(rmfsm,     e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
9409  cCL(rmfsz,     e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
9410  cCL(rmfd,      e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
9411  cCL(rmfdp,     e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9412  cCL(rmfdm,     e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9413  cCL(rmfdz,     e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9414  cCL(rmfe,      e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
9415  cCL(rmfep,     e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
9416  cCL(rmfem,     e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
9417  cCL(rmfez,     e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
9418
9419  cCL(fmls,      e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
9420  cCL(fmlsp,     e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
9421  cCL(fmlsm,     e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
9422  cCL(fmlsz,     e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
9423  cCL(fmld,      e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
9424  cCL(fmldp,     e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9425  cCL(fmldm,     e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9426  cCL(fmldz,     e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9427  cCL(fmle,      e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
9428  cCL(fmlep,     e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
9429  cCL(fmlem,     e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
9430  cCL(fmlez,     e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
9431
9432  cCL(fdvs,      ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
9433  cCL(fdvsp,     ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
9434  cCL(fdvsm,     ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
9435  cCL(fdvsz,     ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
9436  cCL(fdvd,      ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
9437  cCL(fdvdp,     ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9438  cCL(fdvdm,     ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9439  cCL(fdvdz,     ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9440  cCL(fdve,      ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
9441  cCL(fdvep,     ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
9442  cCL(fdvem,     ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
9443  cCL(fdvez,     ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
9444
9445  cCL(frds,      eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
9446  cCL(frdsp,     eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
9447  cCL(frdsm,     eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
9448  cCL(frdsz,     eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
9449  cCL(frdd,      eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
9450  cCL(frddp,     eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9451  cCL(frddm,     eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9452  cCL(frddz,     eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9453  cCL(frde,      eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
9454  cCL(frdep,     eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
9455  cCL(frdem,     eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
9456  cCL(frdez,     eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
9457
9458  cCL(pols,      ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
9459  cCL(polsp,     ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
9460  cCL(polsm,     ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
9461  cCL(polsz,     ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
9462  cCL(pold,      ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
9463  cCL(poldp,     ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9464  cCL(poldm,     ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9465  cCL(poldz,     ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9466  cCL(pole,      ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
9467  cCL(polep,     ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
9468  cCL(polem,     ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
9469  cCL(polez,     ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
9470
9471  cCE(cmf,       e90f110, 2, (RF, RF_IF),     fpa_cmp),
9472  C3E(cmfe,      ed0f110, 2, (RF, RF_IF),     fpa_cmp),
9473  cCE(cnf,       eb0f110, 2, (RF, RF_IF),     fpa_cmp),
9474  C3E(cnfe,      ef0f110, 2, (RF, RF_IF),     fpa_cmp),
9475
9476  cCL(flts,      e000110, 2, (RF, RR),        rn_rd),
9477  cCL(fltsp,     e000130, 2, (RF, RR),        rn_rd),
9478  cCL(fltsm,     e000150, 2, (RF, RR),        rn_rd),
9479  cCL(fltsz,     e000170, 2, (RF, RR),        rn_rd),
9480  cCL(fltd,      e000190, 2, (RF, RR),        rn_rd),
9481  cCL(fltdp,     e0001b0, 2, (RF, RR),        rn_rd),
9482  cCL(fltdm,     e0001d0, 2, (RF, RR),        rn_rd),
9483  cCL(fltdz,     e0001f0, 2, (RF, RR),        rn_rd),
9484  cCL(flte,      e080110, 2, (RF, RR),        rn_rd),
9485  cCL(fltep,     e080130, 2, (RF, RR),        rn_rd),
9486  cCL(fltem,     e080150, 2, (RF, RR),        rn_rd),
9487  cCL(fltez,     e080170, 2, (RF, RR),        rn_rd),
9488
9489   /* The implementation of the FIX instruction is broken on some
9490      assemblers, in that it accepts a precision specifier as well as a
9491      rounding specifier, despite the fact that this is meaningless.
9492      To be more compatible, we accept it as well, though of course it
9493      does not set any bits.  */
9494  cCE(fix,       e100110, 2, (RR, RF),        rd_rm),
9495  cCL(fixp,      e100130, 2, (RR, RF),        rd_rm),
9496  cCL(fixm,      e100150, 2, (RR, RF),        rd_rm),
9497  cCL(fixz,      e100170, 2, (RR, RF),        rd_rm),
9498  cCL(fixsp,     e100130, 2, (RR, RF),        rd_rm),
9499  cCL(fixsm,     e100150, 2, (RR, RF),        rd_rm),
9500  cCL(fixsz,     e100170, 2, (RR, RF),        rd_rm),
9501  cCL(fixdp,     e100130, 2, (RR, RF),        rd_rm),
9502  cCL(fixdm,     e100150, 2, (RR, RF),        rd_rm),
9503  cCL(fixdz,     e100170, 2, (RR, RF),        rd_rm),
9504  cCL(fixep,     e100130, 2, (RR, RF),        rd_rm),
9505  cCL(fixem,     e100150, 2, (RR, RF),        rd_rm),
9506  cCL(fixez,     e100170, 2, (RR, RF),        rd_rm),
9507
9508   /* Instructions that were new with the real FPA, call them V2.  */
9509 #undef ARM_VARIANT
9510 #define ARM_VARIANT FPU_FPA_EXT_V2
9511  cCE(lfm,       c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9512  cCL(lfmfd,     c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9513  cCL(lfmea,     d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9514  cCE(sfm,       c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9515  cCL(sfmfd,     d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9516  cCL(sfmea,     c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9517
9518 #undef ARM_VARIANT
9519 #define ARM_VARIANT FPU_VFP_EXT_V1xD  /* VFP V1xD (single precision).  */
9520   /* Moves and type conversions.  */
9521  cCE(fcpys,     eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
9522  cCE(fmrs,      e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
9523  cCE(fmsr,      e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
9524  cCE(fmstat,    ef1fa10, 0, (),               noargs),
9525  cCE(fsitos,    eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9526  cCE(fuitos,    eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
9527  cCE(ftosis,    ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
9528  cCE(ftosizs,   ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9529  cCE(ftouis,    ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
9530  cCE(ftouizs,   ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9531  cCE(fmrx,      ef00a10, 2, (RR, RVC),        rd_rn),
9532  cCE(fmxr,      ee00a10, 2, (RVC, RR),        rn_rd),
9533
9534   /* Memory operations.  */
9535  cCE(flds,      d100a00, 2, (RVS, ADDR),      vfp_sp_ldst),
9536  cCE(fsts,      d000a00, 2, (RVS, ADDR),      vfp_sp_ldst),
9537  cCE(fldmias,   c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9538  cCE(fldmfds,   c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9539  cCE(fldmdbs,   d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
9540  cCE(fldmeas,   d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
9541  cCE(fldmiax,   c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
9542  cCE(fldmfdx,   c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
9543  cCE(fldmdbx,   d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
9544  cCE(fldmeax,   d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
9545  cCE(fstmias,   c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9546  cCE(fstmeas,   c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9547  cCE(fstmdbs,   d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
9548  cCE(fstmfds,   d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
9549  cCE(fstmiax,   c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
9550  cCE(fstmeax,   c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
9551  cCE(fstmdbx,   d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
9552  cCE(fstmfdx,   d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
9553
9554   /* Monadic operations.  */
9555  cCE(fabss,     eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9556  cCE(fnegs,     eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
9557  cCE(fsqrts,    eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9558
9559   /* Dyadic operations.  */
9560  cCE(fadds,     e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9561  cCE(fsubs,     e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9562  cCE(fmuls,     e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9563  cCE(fdivs,     e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9564  cCE(fmacs,     e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9565  cCE(fmscs,     e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9566  cCE(fnmuls,    e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9567  cCE(fnmacs,    e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9568  cCE(fnmscs,    e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
9569
9570   /* Comparisons.  */
9571  cCE(fcmps,     eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
9572  cCE(fcmpzs,    eb50a40, 1, (RVS),            vfp_sp_compare_z),
9573  cCE(fcmpes,    eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
9574  cCE(fcmpezs,   eb50ac0, 1, (RVS),            vfp_sp_compare_z),
9575
9576 #undef ARM_VARIANT
9577 #define ARM_VARIANT FPU_VFP_EXT_V1 /* VFP V1 (Double precision).  */
9578   /* Moves and type conversions.  */
9579  cCE(fcpyd,     eb00b40, 2, (RVD, RVD),       rd_rm),
9580  cCE(fcvtds,    eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
9581  cCE(fcvtsd,    eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9582  cCE(fmdhr,     e200b10, 2, (RVD, RR),        rn_rd),
9583  cCE(fmdlr,     e000b10, 2, (RVD, RR),        rn_rd),
9584  cCE(fmrdh,     e300b10, 2, (RR, RVD),        rd_rn),
9585  cCE(fmrdl,     e100b10, 2, (RR, RVD),        rd_rn),
9586  cCE(fsitod,    eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
9587  cCE(fuitod,    eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
9588  cCE(ftosid,    ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9589  cCE(ftosizd,   ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9590  cCE(ftouid,    ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9591  cCE(ftouizd,   ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
9592
9593   /* Memory operations.  */
9594  cCE(fldd,      d100b00, 2, (RVD, ADDR),      vfp_dp_ldst),
9595  cCE(fstd,      d000b00, 2, (RVD, ADDR),      vfp_dp_ldst),
9596  cCE(fldmiad,   c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
9597  cCE(fldmfdd,   c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
9598  cCE(fldmdbd,   d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
9599  cCE(fldmead,   d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
9600  cCE(fstmiad,   c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
9601  cCE(fstmead,   c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
9602  cCE(fstmdbd,   d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
9603  cCE(fstmfdd,   d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
9604
9605   /* Monadic operations.  */
9606  cCE(fabsd,     eb00bc0, 2, (RVD, RVD),       rd_rm),
9607  cCE(fnegd,     eb10b40, 2, (RVD, RVD),       rd_rm),
9608  cCE(fsqrtd,    eb10bc0, 2, (RVD, RVD),       rd_rm),
9609
9610   /* Dyadic operations.  */
9611  cCE(faddd,     e300b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9612  cCE(fsubd,     e300b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
9613  cCE(fmuld,     e200b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9614  cCE(fdivd,     e800b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9615  cCE(fmacd,     e000b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9616  cCE(fmscd,     e100b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
9617  cCE(fnmuld,    e200b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
9618  cCE(fnmacd,    e000b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
9619  cCE(fnmscd,    e100b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
9620
9621   /* Comparisons.  */
9622  cCE(fcmpd,     eb40b40, 2, (RVD, RVD),       rd_rm),
9623  cCE(fcmpzd,    eb50b40, 1, (RVD),            rd),
9624  cCE(fcmped,    eb40bc0, 2, (RVD, RVD),       rd_rm),
9625  cCE(fcmpezd,   eb50bc0, 1, (RVD),            rd),
9626
9627 #undef ARM_VARIANT
9628 #define ARM_VARIANT FPU_VFP_EXT_V2
9629  cCE(fmsrr,     c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
9630  cCE(fmrrs,     c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
9631  cCE(fmdrr,     c400b10, 3, (RVD, RR, RR),    rm_rd_rn),
9632  cCE(fmrrd,     c500b10, 3, (RR, RR, RVD),    rd_rn_rm),
9633
9634 #undef ARM_VARIANT
9635 #define ARM_VARIANT ARM_CEXT_XSCALE /* Intel XScale extensions.  */
9636  cCE(mia,       e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9637  cCE(miaph,     e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9638  cCE(miabb,     e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9639  cCE(miabt,     e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9640  cCE(miatb,     e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9641  cCE(miatt,     e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
9642  cCE(mar,       c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
9643  cCE(mra,       c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
9644
9645 #undef ARM_VARIANT
9646 #define ARM_VARIANT ARM_CEXT_IWMMXT /* Intel Wireless MMX technology.  */
9647  cCE(tandcb,    e13f130, 1, (RR),                   iwmmxt_tandorc),
9648  cCE(tandch,    e53f130, 1, (RR),                   iwmmxt_tandorc),
9649  cCE(tandcw,    e93f130, 1, (RR),                   iwmmxt_tandorc),
9650  cCE(tbcstb,    e400010, 2, (RIWR, RR),             rn_rd),
9651  cCE(tbcsth,    e400050, 2, (RIWR, RR),             rn_rd),
9652  cCE(tbcstw,    e400090, 2, (RIWR, RR),             rn_rd),
9653  cCE(textrcb,   e130170, 2, (RR, I7),               iwmmxt_textrc),
9654  cCE(textrch,   e530170, 2, (RR, I7),               iwmmxt_textrc),
9655  cCE(textrcw,   e930170, 2, (RR, I7),               iwmmxt_textrc),
9656  cCE(textrmub,  e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9657  cCE(textrmuh,  e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9658  cCE(textrmuw,  e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9659  cCE(textrmsb,  e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9660  cCE(textrmsh,  e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9661  cCE(textrmsw,  e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
9662  cCE(tinsrb,    e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
9663  cCE(tinsrh,    e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
9664  cCE(tinsrw,    e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
9665  cCE(tmcr,      e000110, 2, (RIWC, RR),             rn_rd),
9666  cCE(tmcrr,     c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
9667  cCE(tmia,      e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9668  cCE(tmiaph,    e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9669  cCE(tmiabb,    e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9670  cCE(tmiabt,    e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9671  cCE(tmiatb,    e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9672  cCE(tmiatt,    e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
9673  cCE(tmovmskb,  e100030, 2, (RR, RIWR),             rd_rn),
9674  cCE(tmovmskh,  e500030, 2, (RR, RIWR),             rd_rn),
9675  cCE(tmovmskw,  e900030, 2, (RR, RIWR),             rd_rn),
9676  cCE(tmrc,      e100110, 2, (RR, RIWC),             rd_rn),
9677  cCE(tmrrc,     c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
9678  cCE(torcb,     e13f150, 1, (RR),                   iwmmxt_tandorc),
9679  cCE(torch,     e53f150, 1, (RR),                   iwmmxt_tandorc),
9680  cCE(torcw,     e93f150, 1, (RR),                   iwmmxt_tandorc),
9681  cCE(waccb,     e0001c0, 2, (RIWR, RIWR),           rd_rn),
9682  cCE(wacch,     e4001c0, 2, (RIWR, RIWR),           rd_rn),
9683  cCE(waccw,     e8001c0, 2, (RIWR, RIWR),           rd_rn),
9684  cCE(waddbss,   e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9685  cCE(waddb,     e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9686  cCE(waddbus,   e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9687  cCE(waddhss,   e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9688  cCE(waddh,     e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9689  cCE(waddhus,   e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9690  cCE(waddwss,   eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9691  cCE(waddw,     e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9692  cCE(waddwus,   e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9693  cCE(waligni,   e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
9694  cCE(walignr0,  e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9695  cCE(walignr1,  e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9696  cCE(walignr2,  ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9697  cCE(walignr3,  eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9698  cCE(wand,      e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9699  cCE(wandn,     e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9700  cCE(wavg2b,    e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9701  cCE(wavg2br,   e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9702  cCE(wavg2h,    ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9703  cCE(wavg2hr,   ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9704  cCE(wcmpeqb,   e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9705  cCE(wcmpeqh,   e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9706  cCE(wcmpeqw,   e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9707  cCE(wcmpgtub,  e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9708  cCE(wcmpgtuh,  e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9709  cCE(wcmpgtuw,  e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9710  cCE(wcmpgtsb,  e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9711  cCE(wcmpgtsh,  e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9712  cCE(wcmpgtsw,  eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9713  cCE(wldrb,     c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
9714  cCE(wldrh,     c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
9715  cCE(wldrw,     c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
9716  cCE(wldrd,     c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
9717  cCE(wmacs,     e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9718  cCE(wmacsz,    e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9719  cCE(wmacu,     e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9720  cCE(wmacuz,    e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9721  cCE(wmadds,    ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9722  cCE(wmaddu,    e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9723  cCE(wmaxsb,    e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9724  cCE(wmaxsh,    e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9725  cCE(wmaxsw,    ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9726  cCE(wmaxub,    e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9727  cCE(wmaxuh,    e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9728  cCE(wmaxuw,    e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9729  cCE(wminsb,    e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9730  cCE(wminsh,    e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9731  cCE(wminsw,    eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9732  cCE(wminub,    e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9733  cCE(wminuh,    e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9734  cCE(wminuw,    e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9735  cCE(wmov,      e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
9736  cCE(wmulsm,    e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9737  cCE(wmulsl,    e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9738  cCE(wmulum,    e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9739  cCE(wmulul,    e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9740  cCE(wor,       e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9741  cCE(wpackhss,  e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9742  cCE(wpackhus,  e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9743  cCE(wpackwss,  eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9744  cCE(wpackwus,  e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9745  cCE(wpackdss,  ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9746  cCE(wpackdus,  ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9747  cCE(wrorh,     e700040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9748  cCE(wrorhg,    e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9749  cCE(wrorw,     eb00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9750  cCE(wrorwg,    eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9751  cCE(wrord,     ef00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9752  cCE(wrordg,    ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9753  cCE(wsadb,     e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9754  cCE(wsadbz,    e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9755  cCE(wsadh,     e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9756  cCE(wsadhz,    e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9757  cCE(wshufh,    e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
9758  cCE(wsllh,     e500040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9759  cCE(wsllhg,    e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9760  cCE(wsllw,     e900040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9761  cCE(wsllwg,    e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9762  cCE(wslld,     ed00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9763  cCE(wslldg,    ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9764  cCE(wsrah,     e400040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9765  cCE(wsrahg,    e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9766  cCE(wsraw,     e800040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9767  cCE(wsrawg,    e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9768  cCE(wsrad,     ec00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9769  cCE(wsradg,    ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9770  cCE(wsrlh,     e600040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9771  cCE(wsrlhg,    e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9772  cCE(wsrlw,     ea00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9773  cCE(wsrlwg,    ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9774  cCE(wsrld,     ee00040, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9775  cCE(wsrldg,    ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
9776  cCE(wstrb,     c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
9777  cCE(wstrh,     c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
9778  cCE(wstrw,     c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
9779  cCE(wstrd,     c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
9780  cCE(wsubbss,   e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9781  cCE(wsubb,     e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9782  cCE(wsubbus,   e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9783  cCE(wsubhss,   e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9784  cCE(wsubh,     e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9785  cCE(wsubhus,   e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9786  cCE(wsubwss,   eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9787  cCE(wsubw,     e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9788  cCE(wsubwus,   e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9789  cCE(wunpckehub,e0000c0, 2, (RIWR, RIWR),           rd_rn),
9790  cCE(wunpckehuh,e4000c0, 2, (RIWR, RIWR),           rd_rn),
9791  cCE(wunpckehuw,e8000c0, 2, (RIWR, RIWR),           rd_rn),
9792  cCE(wunpckehsb,e2000c0, 2, (RIWR, RIWR),           rd_rn),
9793  cCE(wunpckehsh,e6000c0, 2, (RIWR, RIWR),           rd_rn),
9794  cCE(wunpckehsw,ea000c0, 2, (RIWR, RIWR),           rd_rn),
9795  cCE(wunpckihb, e1000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9796  cCE(wunpckihh, e5000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9797  cCE(wunpckihw, e9000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9798  cCE(wunpckelub,e0000e0, 2, (RIWR, RIWR),           rd_rn),
9799  cCE(wunpckeluh,e4000e0, 2, (RIWR, RIWR),           rd_rn),
9800  cCE(wunpckeluw,e8000e0, 2, (RIWR, RIWR),           rd_rn),
9801  cCE(wunpckelsb,e2000e0, 2, (RIWR, RIWR),           rd_rn),
9802  cCE(wunpckelsh,e6000e0, 2, (RIWR, RIWR),           rd_rn),
9803  cCE(wunpckelsw,ea000e0, 2, (RIWR, RIWR),           rd_rn),
9804  cCE(wunpckilb, e1000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9805  cCE(wunpckilh, e5000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9806  cCE(wunpckilw, e9000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9807  cCE(wxor,      e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
9808  cCE(wzero,     e300000, 1, (RIWR),                 iwmmxt_wzero),
9809
9810 #undef ARM_VARIANT
9811 #define ARM_VARIANT ARM_CEXT_MAVERICK /* Cirrus Maverick instructions.  */
9812  cCE(cfldrs,    c100400, 2, (RMF, ADDR),              rd_cpaddr),
9813  cCE(cfldrd,    c500400, 2, (RMD, ADDR),              rd_cpaddr),
9814  cCE(cfldr32,   c100500, 2, (RMFX, ADDR),             rd_cpaddr),
9815  cCE(cfldr64,   c500500, 2, (RMDX, ADDR),             rd_cpaddr),
9816  cCE(cfstrs,    c000400, 2, (RMF, ADDR),              rd_cpaddr),
9817  cCE(cfstrd,    c400400, 2, (RMD, ADDR),              rd_cpaddr),
9818  cCE(cfstr32,   c000500, 2, (RMFX, ADDR),             rd_cpaddr),
9819  cCE(cfstr64,   c400500, 2, (RMDX, ADDR),             rd_cpaddr),
9820  cCE(cfmvsr,    e000450, 2, (RMF, RR),                rn_rd),
9821  cCE(cfmvrs,    e100450, 2, (RR, RMF),                rd_rn),
9822  cCE(cfmvdlr,   e000410, 2, (RMD, RR),                rn_rd),
9823  cCE(cfmvrdl,   e100410, 2, (RR, RMD),                rd_rn),
9824  cCE(cfmvdhr,   e000430, 2, (RMD, RR),                rn_rd),
9825  cCE(cfmvrdh,   e100430, 2, (RR, RMD),                rd_rn),
9826  cCE(cfmv64lr,  e000510, 2, (RMDX, RR),               rn_rd),
9827  cCE(cfmvr64l,  e100510, 2, (RR, RMDX),               rd_rn),
9828  cCE(cfmv64hr,  e000530, 2, (RMDX, RR),               rn_rd),
9829  cCE(cfmvr64h,  e100530, 2, (RR, RMDX),               rd_rn),
9830  cCE(cfmval32,  e200440, 2, (RMAX, RMFX),             rd_rn),
9831  cCE(cfmv32al,  e100440, 2, (RMFX, RMAX),             rd_rn),
9832  cCE(cfmvam32,  e200460, 2, (RMAX, RMFX),             rd_rn),
9833  cCE(cfmv32am,  e100460, 2, (RMFX, RMAX),             rd_rn),
9834  cCE(cfmvah32,  e200480, 2, (RMAX, RMFX),             rd_rn),
9835  cCE(cfmv32ah,  e100480, 2, (RMFX, RMAX),             rd_rn),
9836  cCE(cfmva32,   e2004a0, 2, (RMAX, RMFX),             rd_rn),
9837  cCE(cfmv32a,   e1004a0, 2, (RMFX, RMAX),             rd_rn),
9838  cCE(cfmva64,   e2004c0, 2, (RMAX, RMDX),             rd_rn),
9839  cCE(cfmv64a,   e1004c0, 2, (RMDX, RMAX),             rd_rn),
9840  cCE(cfmvsc32,  e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
9841  cCE(cfmv32sc,  e1004e0, 2, (RMDX, RMDS),             rd),
9842  cCE(cfcpys,    e000400, 2, (RMF, RMF),               rd_rn),
9843  cCE(cfcpyd,    e000420, 2, (RMD, RMD),               rd_rn),
9844  cCE(cfcvtsd,   e000460, 2, (RMD, RMF),               rd_rn),
9845  cCE(cfcvtds,   e000440, 2, (RMF, RMD),               rd_rn),
9846  cCE(cfcvt32s,  e000480, 2, (RMF, RMFX),              rd_rn),
9847  cCE(cfcvt32d,  e0004a0, 2, (RMD, RMFX),              rd_rn),
9848  cCE(cfcvt64s,  e0004c0, 2, (RMF, RMDX),              rd_rn),
9849  cCE(cfcvt64d,  e0004e0, 2, (RMD, RMDX),              rd_rn),
9850  cCE(cfcvts32,  e100580, 2, (RMFX, RMF),              rd_rn),
9851  cCE(cfcvtd32,  e1005a0, 2, (RMFX, RMD),              rd_rn),
9852  cCE(cftruncs32,e1005c0, 2, (RMFX, RMF),              rd_rn),
9853  cCE(cftruncd32,e1005e0, 2, (RMFX, RMD),              rd_rn),
9854  cCE(cfrshl32,  e000550, 3, (RMFX, RMFX, RR),         mav_triple),
9855  cCE(cfrshl64,  e000570, 3, (RMDX, RMDX, RR),         mav_triple),
9856  cCE(cfsh32,    e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
9857  cCE(cfsh64,    e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
9858  cCE(cfcmps,    e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
9859  cCE(cfcmpd,    e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
9860  cCE(cfcmp32,   e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
9861  cCE(cfcmp64,   e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
9862  cCE(cfabss,    e300400, 2, (RMF, RMF),               rd_rn),
9863  cCE(cfabsd,    e300420, 2, (RMD, RMD),               rd_rn),
9864  cCE(cfnegs,    e300440, 2, (RMF, RMF),               rd_rn),
9865  cCE(cfnegd,    e300460, 2, (RMD, RMD),               rd_rn),
9866  cCE(cfadds,    e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
9867  cCE(cfaddd,    e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
9868  cCE(cfsubs,    e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
9869  cCE(cfsubd,    e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
9870  cCE(cfmuls,    e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
9871  cCE(cfmuld,    e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
9872  cCE(cfabs32,   e300500, 2, (RMFX, RMFX),             rd_rn),
9873  cCE(cfabs64,   e300520, 2, (RMDX, RMDX),             rd_rn),
9874  cCE(cfneg32,   e300540, 2, (RMFX, RMFX),             rd_rn),
9875  cCE(cfneg64,   e300560, 2, (RMDX, RMDX),             rd_rn),
9876  cCE(cfadd32,   e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9877  cCE(cfadd64,   e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
9878  cCE(cfsub32,   e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9879  cCE(cfsub64,   e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
9880  cCE(cfmul32,   e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9881  cCE(cfmul64,   e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
9882  cCE(cfmac32,   e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9883  cCE(cfmsc32,   e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
9884  cCE(cfmadd32,  e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
9885  cCE(cfmsub32,  e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
9886  cCE(cfmadda32, e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
9887  cCE(cfmsuba32, e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
9888 };
9889 #undef ARM_VARIANT
9890 #undef THUMB_VARIANT
9891 #undef TCE
9892 #undef TCM
9893 #undef TUE
9894 #undef TUF
9895 #undef TCC
9896 #undef cCE
9897 #undef cCL
9898 #undef C3E
9899 #undef CE
9900 #undef CM
9901 #undef UE
9902 #undef UF
9903 #undef UT
9904 #undef OPS0
9905 #undef OPS1
9906 #undef OPS2
9907 #undef OPS3
9908 #undef OPS4
9909 #undef OPS5
9910 #undef OPS6
9911 #undef do_0
9912 \f
9913 /* MD interface: bits in the object file.  */
9914
9915 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
9916    for use in the a.out file, and stores them in the array pointed to by buf.
9917    This knows about the endian-ness of the target machine and does
9918    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
9919    2 (short) and 4 (long)  Floating numbers are put out as a series of
9920    LITTLENUMS (shorts, here at least).  */
9921
9922 void
9923 md_number_to_chars (char * buf, valueT val, int n)
9924 {
9925   if (target_big_endian)
9926     number_to_chars_bigendian (buf, val, n);
9927   else
9928     number_to_chars_littleendian (buf, val, n);
9929 }
9930
9931 static valueT
9932 md_chars_to_number (char * buf, int n)
9933 {
9934   valueT result = 0;
9935   unsigned char * where = (unsigned char *) buf;
9936
9937   if (target_big_endian)
9938     {
9939       while (n--)
9940         {
9941           result <<= 8;
9942           result |= (*where++ & 255);
9943         }
9944     }
9945   else
9946     {
9947       while (n--)
9948         {
9949           result <<= 8;
9950           result |= (where[n] & 255);
9951         }
9952     }
9953
9954   return result;
9955 }
9956
9957 /* MD interface: Sections.  */
9958
9959 /* Estimate the size of a frag before relaxing.  Assume everything fits in
9960    2 bytes.  */
9961
9962 int
9963 md_estimate_size_before_relax (fragS * fragp,
9964                                segT    segtype ATTRIBUTE_UNUSED)
9965 {
9966   fragp->fr_var = 2;
9967   return 2;
9968 }
9969
9970 /* Convert a machine dependent frag.  */
9971
9972 void
9973 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
9974 {
9975   unsigned long insn;
9976   unsigned long old_op;
9977   char *buf;
9978   expressionS exp;
9979   fixS *fixp;
9980   int reloc_type;
9981   int pc_rel;
9982   int opcode;
9983
9984   buf = fragp->fr_literal + fragp->fr_fix;
9985
9986   old_op = bfd_get_16(abfd, buf);
9987   if (fragp->fr_symbol) {
9988       exp.X_op = O_symbol;
9989       exp.X_add_symbol = fragp->fr_symbol;
9990   } else {
9991       exp.X_op = O_constant;
9992   }
9993   exp.X_add_number = fragp->fr_offset;
9994   opcode = fragp->fr_subtype;
9995   switch (opcode)
9996     {
9997     case T_MNEM_ldr_pc:
9998     case T_MNEM_ldr_pc2:
9999     case T_MNEM_ldr_sp:
10000     case T_MNEM_str_sp:
10001     case T_MNEM_ldr:
10002     case T_MNEM_ldrb:
10003     case T_MNEM_ldrh:
10004     case T_MNEM_str:
10005     case T_MNEM_strb:
10006     case T_MNEM_strh:
10007       if (fragp->fr_var == 4)
10008         {
10009           insn = THUMB_OP32(opcode);
10010           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
10011             {
10012               insn |= (old_op & 0x700) << 4;
10013             }
10014           else
10015             {
10016               insn |= (old_op & 7) << 12;
10017               insn |= (old_op & 0x38) << 13;
10018             }
10019           insn |= 0x00000c00;
10020           put_thumb32_insn (buf, insn);
10021           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10022         }
10023       else
10024         {
10025           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
10026         }
10027       pc_rel = (opcode == T_MNEM_ldr_pc2);
10028       break;
10029     case T_MNEM_adr:
10030       if (fragp->fr_var == 4)
10031         {
10032           insn = THUMB_OP32 (opcode);
10033           insn |= (old_op & 0xf0) << 4;
10034           put_thumb32_insn (buf, insn);
10035           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
10036         }
10037       else
10038         {
10039           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
10040           exp.X_add_number -= 4;
10041         }
10042       pc_rel = 1;
10043       break;
10044     case T_MNEM_mov:
10045     case T_MNEM_movs:
10046     case T_MNEM_cmp:
10047     case T_MNEM_cmn:
10048       if (fragp->fr_var == 4)
10049         {
10050           int r0off = (opcode == T_MNEM_mov
10051                        || opcode == T_MNEM_movs) ? 0 : 8;
10052           insn = THUMB_OP32 (opcode);
10053           insn = (insn & 0xe1ffffff) | 0x10000000;
10054           insn |= (old_op & 0x700) << r0off;
10055           put_thumb32_insn (buf, insn);
10056           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
10057         }
10058       else
10059         {
10060           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
10061         }
10062       pc_rel = 0;
10063       break;
10064     case T_MNEM_b:
10065       if (fragp->fr_var == 4)
10066         {
10067           insn = THUMB_OP32(opcode);
10068           put_thumb32_insn (buf, insn);
10069           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
10070         }
10071       else
10072         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
10073       pc_rel = 1;
10074       break;
10075     case T_MNEM_bcond:
10076       if (fragp->fr_var == 4)
10077         {
10078           insn = THUMB_OP32(opcode);
10079           insn |= (old_op & 0xf00) << 14;
10080           put_thumb32_insn (buf, insn);
10081           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
10082         }
10083       else
10084         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
10085       pc_rel = 1;
10086       break;
10087     case T_MNEM_add_sp:
10088     case T_MNEM_add_pc:
10089     case T_MNEM_inc_sp:
10090     case T_MNEM_dec_sp:
10091       if (fragp->fr_var == 4)
10092         {
10093           /* ??? Choose between add and addw.  */
10094           insn = THUMB_OP32 (opcode);
10095           insn |= (old_op & 0xf0) << 4;
10096           put_thumb32_insn (buf, insn);
10097           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
10098         }
10099       else
10100         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
10101       pc_rel = 0;
10102       break;
10103
10104     case T_MNEM_addi:
10105     case T_MNEM_addis:
10106     case T_MNEM_subi:
10107     case T_MNEM_subis:
10108       if (fragp->fr_var == 4)
10109         {
10110           insn = THUMB_OP32 (opcode);
10111           insn |= (old_op & 0xf0) << 4;
10112           insn |= (old_op & 0xf) << 16;
10113           put_thumb32_insn (buf, insn);
10114           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
10115         }
10116       else
10117         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
10118       pc_rel = 0;
10119       break;
10120     default:
10121       abort();
10122     }
10123   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
10124                       reloc_type);
10125   fixp->fx_file = fragp->fr_file;
10126   fixp->fx_line = fragp->fr_line;
10127   fragp->fr_fix += fragp->fr_var;
10128 }
10129
10130 /* Return the size of a relaxable immediate operand instruction.
10131    SHIFT and SIZE specify the form of the allowable immediate.  */
10132 static int
10133 relax_immediate (fragS *fragp, int size, int shift)
10134 {
10135   offsetT offset;
10136   offsetT mask;
10137   offsetT low;
10138
10139   /* ??? Should be able to do better than this.  */
10140   if (fragp->fr_symbol)
10141     return 4;
10142
10143   low = (1 << shift) - 1;
10144   mask = (1 << (shift + size)) - (1 << shift);
10145   offset = fragp->fr_offset;
10146   /* Force misaligned offsets to 32-bit variant.  */
10147   if (offset & low)
10148     return -4;
10149   if (offset & ~mask)
10150     return 4;
10151   return 2;
10152 }
10153
10154 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
10155    load.  */
10156 static int
10157 relax_adr (fragS *fragp, asection *sec)
10158 {
10159   addressT addr;
10160   offsetT val;
10161
10162   /* Assume worst case for symbols not known to be in the same section.  */
10163   if (!S_IS_DEFINED(fragp->fr_symbol)
10164       || sec != S_GET_SEGMENT (fragp->fr_symbol))
10165     return 4;
10166
10167   val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
10168   addr = fragp->fr_address + fragp->fr_fix;
10169   addr = (addr + 4) & ~3;
10170   /* Fix the insn as the 4-byte version if the target address is not
10171      sufficiently aligned.  This is prevents an infinite loop when two
10172      instructions have contradictory range/alignment requirements.  */
10173   if (val & 3)
10174     return -4;
10175   val -= addr;
10176   if (val < 0 || val > 1020)
10177     return 4;
10178   return 2;
10179 }
10180
10181 /* Return the size of a relaxable add/sub immediate instruction.  */
10182 static int
10183 relax_addsub (fragS *fragp, asection *sec)
10184 {
10185   char *buf;
10186   int op;
10187
10188   buf = fragp->fr_literal + fragp->fr_fix;
10189   op = bfd_get_16(sec->owner, buf);
10190   if ((op & 0xf) == ((op >> 4) & 0xf))
10191     return relax_immediate (fragp, 8, 0);
10192   else
10193     return relax_immediate (fragp, 3, 0);
10194 }
10195
10196
10197 /* Return the size of a relaxable branch instruction.  BITS is the
10198    size of the offset field in the narrow instruction.  */
10199
10200 static int
10201 relax_branch (fragS *fragp, asection *sec, int bits)
10202 {
10203   addressT addr;
10204   offsetT val;
10205   offsetT limit;
10206
10207   /* Assume worst case for symbols not known to be in the same section.  */
10208   if (!S_IS_DEFINED(fragp->fr_symbol)
10209       || sec != S_GET_SEGMENT (fragp->fr_symbol))
10210     return 4;
10211
10212   val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
10213   addr = fragp->fr_address + fragp->fr_fix + 4;
10214   val -= addr;
10215
10216   /* Offset is a signed value *2 */
10217   limit = 1 << bits;
10218   if (val >= limit || val < -limit)
10219     return 4;
10220   return 2;
10221 }
10222
10223
10224 /* Relax a machine dependent frag.  This returns the amount by which
10225    the current size of the frag should change.  */
10226
10227 int
10228 arm_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
10229 {
10230   int oldsize;
10231   int newsize;
10232
10233   oldsize = fragp->fr_var;
10234   switch (fragp->fr_subtype)
10235     {
10236     case T_MNEM_ldr_pc2:
10237       newsize = relax_adr(fragp, sec);
10238       break;
10239     case T_MNEM_ldr_pc:
10240     case T_MNEM_ldr_sp:
10241     case T_MNEM_str_sp:
10242       newsize = relax_immediate(fragp, 8, 2);
10243       break;
10244     case T_MNEM_ldr:
10245     case T_MNEM_str:
10246       newsize = relax_immediate(fragp, 5, 2);
10247       break;
10248     case T_MNEM_ldrh:
10249     case T_MNEM_strh:
10250       newsize = relax_immediate(fragp, 5, 1);
10251       break;
10252     case T_MNEM_ldrb:
10253     case T_MNEM_strb:
10254       newsize = relax_immediate(fragp, 5, 0);
10255       break;
10256     case T_MNEM_adr:
10257       newsize = relax_adr(fragp, sec);
10258       break;
10259     case T_MNEM_mov:
10260     case T_MNEM_movs:
10261     case T_MNEM_cmp:
10262     case T_MNEM_cmn:
10263       newsize = relax_immediate(fragp, 8, 0);
10264       break;
10265     case T_MNEM_b:
10266       newsize = relax_branch(fragp, sec, 11);
10267       break;
10268     case T_MNEM_bcond:
10269       newsize = relax_branch(fragp, sec, 8);
10270       break;
10271     case T_MNEM_add_sp:
10272     case T_MNEM_add_pc:
10273       newsize = relax_immediate (fragp, 8, 2);
10274       break;
10275     case T_MNEM_inc_sp:
10276     case T_MNEM_dec_sp:
10277       newsize = relax_immediate (fragp, 7, 2);
10278       break;
10279     case T_MNEM_addi:
10280     case T_MNEM_addis:
10281     case T_MNEM_subi:
10282     case T_MNEM_subis:
10283       newsize = relax_addsub (fragp, sec);
10284       break;
10285     default:
10286       abort();
10287     }
10288   if (newsize < 0)
10289     {
10290       fragp->fr_var = -newsize;
10291       md_convert_frag (sec->owner, sec, fragp);
10292       frag_wane(fragp);
10293       return -(newsize + oldsize);
10294     }
10295   fragp->fr_var = newsize;
10296   return newsize - oldsize;
10297 }
10298
10299 /* Round up a section size to the appropriate boundary.  */
10300
10301 valueT
10302 md_section_align (segT   segment ATTRIBUTE_UNUSED,
10303                   valueT size)
10304 {
10305 #ifdef OBJ_ELF
10306   return size;
10307 #else
10308   /* Round all sects to multiple of 4.  */
10309   return (size + 3) & ~3;
10310 #endif
10311 }
10312
10313 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
10314    of an rs_align_code fragment.  */
10315
10316 void
10317 arm_handle_align (fragS * fragP)
10318 {
10319   static char const arm_noop[4] = { 0x00, 0x00, 0xa0, 0xe1 };
10320   static char const thumb_noop[2] = { 0xc0, 0x46 };
10321   static char const arm_bigend_noop[4] = { 0xe1, 0xa0, 0x00, 0x00 };
10322   static char const thumb_bigend_noop[2] = { 0x46, 0xc0 };
10323
10324   int bytes, fix, noop_size;
10325   char * p;
10326   const char * noop;
10327
10328   if (fragP->fr_type != rs_align_code)
10329     return;
10330
10331   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
10332   p = fragP->fr_literal + fragP->fr_fix;
10333   fix = 0;
10334
10335   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
10336     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
10337
10338   if (fragP->tc_frag_data)
10339     {
10340       if (target_big_endian)
10341         noop = thumb_bigend_noop;
10342       else
10343         noop = thumb_noop;
10344       noop_size = sizeof (thumb_noop);
10345     }
10346   else
10347     {
10348       if (target_big_endian)
10349         noop = arm_bigend_noop;
10350       else
10351         noop = arm_noop;
10352       noop_size = sizeof (arm_noop);
10353     }
10354
10355   if (bytes & (noop_size - 1))
10356     {
10357       fix = bytes & (noop_size - 1);
10358       memset (p, 0, fix);
10359       p += fix;
10360       bytes -= fix;
10361     }
10362
10363   while (bytes >= noop_size)
10364     {
10365       memcpy (p, noop, noop_size);
10366       p += noop_size;
10367       bytes -= noop_size;
10368       fix += noop_size;
10369     }
10370
10371   fragP->fr_fix += fix;
10372   fragP->fr_var = noop_size;
10373 }
10374
10375 /* Called from md_do_align.  Used to create an alignment
10376    frag in a code section.  */
10377
10378 void
10379 arm_frag_align_code (int n, int max)
10380 {
10381   char * p;
10382
10383   /* We assume that there will never be a requirement
10384      to support alignments greater than 32 bytes.  */
10385   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
10386     as_fatal (_("alignments greater than 32 bytes not supported in .text sections."));
10387
10388   p = frag_var (rs_align_code,
10389                 MAX_MEM_FOR_RS_ALIGN_CODE,
10390                 1,
10391                 (relax_substateT) max,
10392                 (symbolS *) NULL,
10393                 (offsetT) n,
10394                 (char *) NULL);
10395   *p = 0;
10396 }
10397
10398 /* Perform target specific initialisation of a frag.  */
10399
10400 void
10401 arm_init_frag (fragS * fragP)
10402 {
10403   /* Record whether this frag is in an ARM or a THUMB area.  */
10404   fragP->tc_frag_data = thumb_mode;
10405 }
10406
10407 #ifdef OBJ_ELF
10408 /* When we change sections we need to issue a new mapping symbol.  */
10409
10410 void
10411 arm_elf_change_section (void)
10412 {
10413   flagword flags;
10414   segment_info_type *seginfo;
10415
10416   /* Link an unlinked unwind index table section to the .text section.  */
10417   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
10418       && elf_linked_to_section (now_seg) == NULL)
10419     elf_linked_to_section (now_seg) = text_section;
10420
10421   if (!SEG_NORMAL (now_seg))
10422     return;
10423
10424   flags = bfd_get_section_flags (stdoutput, now_seg);
10425
10426   /* We can ignore sections that only contain debug info.  */
10427   if ((flags & SEC_ALLOC) == 0)
10428     return;
10429
10430   seginfo = seg_info (now_seg);
10431   mapstate = seginfo->tc_segment_info_data.mapstate;
10432   marked_pr_dependency = seginfo->tc_segment_info_data.marked_pr_dependency;
10433 }
10434
10435 int
10436 arm_elf_section_type (const char * str, size_t len)
10437 {
10438   if (len == 5 && strncmp (str, "exidx", 5) == 0)
10439     return SHT_ARM_EXIDX;
10440
10441   return -1;
10442 }
10443 \f
10444 /* Code to deal with unwinding tables.  */
10445
10446 static void add_unwind_adjustsp (offsetT);
10447
10448 /* Cenerate and deferred unwind frame offset.  */
10449
10450 static void
10451 flush_pending_unwind (void)
10452 {
10453   offsetT offset;
10454
10455   offset = unwind.pending_offset;
10456   unwind.pending_offset = 0;
10457   if (offset != 0)
10458     add_unwind_adjustsp (offset);
10459 }
10460
10461 /* Add an opcode to this list for this function.  Two-byte opcodes should
10462    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
10463    order.  */
10464
10465 static void
10466 add_unwind_opcode (valueT op, int length)
10467 {
10468   /* Add any deferred stack adjustment.  */
10469   if (unwind.pending_offset)
10470     flush_pending_unwind ();
10471
10472   unwind.sp_restored = 0;
10473
10474   if (unwind.opcode_count + length > unwind.opcode_alloc)
10475     {
10476       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
10477       if (unwind.opcodes)
10478         unwind.opcodes = xrealloc (unwind.opcodes,
10479                                    unwind.opcode_alloc);
10480       else
10481         unwind.opcodes = xmalloc (unwind.opcode_alloc);
10482     }
10483   while (length > 0)
10484     {
10485       length--;
10486       unwind.opcodes[unwind.opcode_count] = op & 0xff;
10487       op >>= 8;
10488       unwind.opcode_count++;
10489     }
10490 }
10491
10492 /* Add unwind opcodes to adjust the stack pointer.  */
10493
10494 static void
10495 add_unwind_adjustsp (offsetT offset)
10496 {
10497   valueT op;
10498
10499   if (offset > 0x200)
10500     {
10501       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
10502       char bytes[5];
10503       int n;
10504       valueT o;
10505
10506       /* Long form: 0xb2, uleb128.  */
10507       /* This might not fit in a word so add the individual bytes,
10508          remembering the list is built in reverse order.  */
10509       o = (valueT) ((offset - 0x204) >> 2);
10510       if (o == 0)
10511         add_unwind_opcode (0, 1);
10512
10513       /* Calculate the uleb128 encoding of the offset.  */
10514       n = 0;
10515       while (o)
10516         {
10517           bytes[n] = o & 0x7f;
10518           o >>= 7;
10519           if (o)
10520             bytes[n] |= 0x80;
10521           n++;
10522         }
10523       /* Add the insn.  */
10524       for (; n; n--)
10525         add_unwind_opcode (bytes[n - 1], 1);
10526       add_unwind_opcode (0xb2, 1);
10527     }
10528   else if (offset > 0x100)
10529     {
10530       /* Two short opcodes.  */
10531       add_unwind_opcode (0x3f, 1);
10532       op = (offset - 0x104) >> 2;
10533       add_unwind_opcode (op, 1);
10534     }
10535   else if (offset > 0)
10536     {
10537       /* Short opcode.  */
10538       op = (offset - 4) >> 2;
10539       add_unwind_opcode (op, 1);
10540     }
10541   else if (offset < 0)
10542     {
10543       offset = -offset;
10544       while (offset > 0x100)
10545         {
10546           add_unwind_opcode (0x7f, 1);
10547           offset -= 0x100;
10548         }
10549       op = ((offset - 4) >> 2) | 0x40;
10550       add_unwind_opcode (op, 1);
10551     }
10552 }
10553
10554 /* Finish the list of unwind opcodes for this function.  */
10555 static void
10556 finish_unwind_opcodes (void)
10557 {
10558   valueT op;
10559
10560   if (unwind.fp_used)
10561     {
10562       /* Adjust sp as neccessary.  */
10563       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
10564       flush_pending_unwind ();
10565
10566       /* After restoring sp from the frame pointer.  */
10567       op = 0x90 | unwind.fp_reg;
10568       add_unwind_opcode (op, 1);
10569     }
10570   else
10571     flush_pending_unwind ();
10572 }
10573
10574
10575 /* Start an exception table entry.  If idx is nonzero this is an index table
10576    entry.  */
10577
10578 static void
10579 start_unwind_section (const segT text_seg, int idx)
10580 {
10581   const char * text_name;
10582   const char * prefix;
10583   const char * prefix_once;
10584   const char * group_name;
10585   size_t prefix_len;
10586   size_t text_len;
10587   char * sec_name;
10588   size_t sec_name_len;
10589   int type;
10590   int flags;
10591   int linkonce;
10592
10593   if (idx)
10594     {
10595       prefix = ELF_STRING_ARM_unwind;
10596       prefix_once = ELF_STRING_ARM_unwind_once;
10597       type = SHT_ARM_EXIDX;
10598     }
10599   else
10600     {
10601       prefix = ELF_STRING_ARM_unwind_info;
10602       prefix_once = ELF_STRING_ARM_unwind_info_once;
10603       type = SHT_PROGBITS;
10604     }
10605
10606   text_name = segment_name (text_seg);
10607   if (streq (text_name, ".text"))
10608     text_name = "";
10609
10610   if (strncmp (text_name, ".gnu.linkonce.t.",
10611                strlen (".gnu.linkonce.t.")) == 0)
10612     {
10613       prefix = prefix_once;
10614       text_name += strlen (".gnu.linkonce.t.");
10615     }
10616
10617   prefix_len = strlen (prefix);
10618   text_len = strlen (text_name);
10619   sec_name_len = prefix_len + text_len;
10620   sec_name = xmalloc (sec_name_len + 1);
10621   memcpy (sec_name, prefix, prefix_len);
10622   memcpy (sec_name + prefix_len, text_name, text_len);
10623   sec_name[prefix_len + text_len] = '\0';
10624
10625   flags = SHF_ALLOC;
10626   linkonce = 0;
10627   group_name = 0;
10628
10629   /* Handle COMDAT group.  */
10630   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
10631     {
10632       group_name = elf_group_name (text_seg);
10633       if (group_name == NULL)
10634         {
10635           as_bad ("Group section `%s' has no group signature",
10636                   segment_name (text_seg));
10637           ignore_rest_of_line ();
10638           return;
10639         }
10640       flags |= SHF_GROUP;
10641       linkonce = 1;
10642     }
10643
10644   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
10645
10646   /* Set the setion link for index tables.  */
10647   if (idx)
10648     elf_linked_to_section (now_seg) = text_seg;
10649 }
10650
10651
10652 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
10653    personality routine data.  Returns zero, or the index table value for
10654    and inline entry.  */
10655
10656 static valueT
10657 create_unwind_entry (int have_data)
10658 {
10659   int size;
10660   addressT where;
10661   char *ptr;
10662   /* The current word of data.  */
10663   valueT data;
10664   /* The number of bytes left in this word.  */
10665   int n;
10666
10667   finish_unwind_opcodes ();
10668
10669   /* Remember the current text section.  */
10670   unwind.saved_seg = now_seg;
10671   unwind.saved_subseg = now_subseg;
10672
10673   start_unwind_section (now_seg, 0);
10674
10675   if (unwind.personality_routine == NULL)
10676     {
10677       if (unwind.personality_index == -2)
10678         {
10679           if (have_data)
10680             as_bad (_("handerdata in cantunwind frame"));
10681           return 1; /* EXIDX_CANTUNWIND.  */
10682         }
10683
10684       /* Use a default personality routine if none is specified.  */
10685       if (unwind.personality_index == -1)
10686         {
10687           if (unwind.opcode_count > 3)
10688             unwind.personality_index = 1;
10689           else
10690             unwind.personality_index = 0;
10691         }
10692
10693       /* Space for the personality routine entry.  */
10694       if (unwind.personality_index == 0)
10695         {
10696           if (unwind.opcode_count > 3)
10697             as_bad (_("too many unwind opcodes for personality routine 0"));
10698
10699           if (!have_data)
10700             {
10701               /* All the data is inline in the index table.  */
10702               data = 0x80;
10703               n = 3;
10704               while (unwind.opcode_count > 0)
10705                 {
10706                   unwind.opcode_count--;
10707                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
10708                   n--;
10709                 }
10710
10711               /* Pad with "finish" opcodes.  */
10712               while (n--)
10713                 data = (data << 8) | 0xb0;
10714
10715               return data;
10716             }
10717           size = 0;
10718         }
10719       else
10720         /* We get two opcodes "free" in the first word.  */
10721         size = unwind.opcode_count - 2;
10722     }
10723   else
10724     /* An extra byte is required for the opcode count.  */
10725     size = unwind.opcode_count + 1;
10726
10727   size = (size + 3) >> 2;
10728   if (size > 0xff)
10729     as_bad (_("too many unwind opcodes"));
10730
10731   frag_align (2, 0, 0);
10732   record_alignment (now_seg, 2);
10733   unwind.table_entry = expr_build_dot ();
10734
10735   /* Allocate the table entry.  */
10736   ptr = frag_more ((size << 2) + 4);
10737   where = frag_now_fix () - ((size << 2) + 4);
10738
10739   switch (unwind.personality_index)
10740     {
10741     case -1:
10742       /* ??? Should this be a PLT generating relocation?  */
10743       /* Custom personality routine.  */
10744       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
10745                BFD_RELOC_ARM_PREL31);
10746
10747       where += 4;
10748       ptr += 4;
10749
10750       /* Set the first byte to the number of additional words.  */
10751       data = size - 1;
10752       n = 3;
10753       break;
10754
10755     /* ABI defined personality routines.  */
10756     case 0:
10757       /* Three opcodes bytes are packed into the first word.  */
10758       data = 0x80;
10759       n = 3;
10760       break;
10761
10762     case 1:
10763     case 2:
10764       /* The size and first two opcode bytes go in the first word.  */
10765       data = ((0x80 + unwind.personality_index) << 8) | size;
10766       n = 2;
10767       break;
10768
10769     default:
10770       /* Should never happen.  */
10771       abort ();
10772     }
10773
10774   /* Pack the opcodes into words (MSB first), reversing the list at the same
10775      time.  */
10776   while (unwind.opcode_count > 0)
10777     {
10778       if (n == 0)
10779         {
10780           md_number_to_chars (ptr, data, 4);
10781           ptr += 4;
10782           n = 4;
10783           data = 0;
10784         }
10785       unwind.opcode_count--;
10786       n--;
10787       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
10788     }
10789
10790   /* Finish off the last word.  */
10791   if (n < 4)
10792     {
10793       /* Pad with "finish" opcodes.  */
10794       while (n--)
10795         data = (data << 8) | 0xb0;
10796
10797       md_number_to_chars (ptr, data, 4);
10798     }
10799
10800   if (!have_data)
10801     {
10802       /* Add an empty descriptor if there is no user-specified data.   */
10803       ptr = frag_more (4);
10804       md_number_to_chars (ptr, 0, 4);
10805     }
10806
10807   return 0;
10808 }
10809
10810 /* Convert REGNAME to a DWARF-2 register number.  */
10811
10812 int
10813 tc_arm_regname_to_dw2regnum (const char *regname)
10814 {
10815   int reg = arm_reg_parse ((char **) &regname, REG_TYPE_RN);
10816
10817   if (reg == FAIL)
10818     return -1;
10819
10820   return reg;
10821 }
10822
10823 /* Initialize the DWARF-2 unwind information for this procedure.  */
10824
10825 void
10826 tc_arm_frame_initial_instructions (void)
10827 {
10828   cfi_add_CFA_def_cfa (REG_SP, 0);
10829 }
10830 #endif /* OBJ_ELF */
10831
10832
10833 /* MD interface: Symbol and relocation handling.  */
10834
10835 /* Return the address within the segment that a PC-relative fixup is
10836    relative to.  For ARM, PC-relative fixups applied to instructions
10837    are generally relative to the location of the fixup plus 8 bytes.
10838    Thumb branches are offset by 4, and Thumb loads relative to PC
10839    require special handling.  */
10840
10841 long
10842 md_pcrel_from_section (fixS * fixP, segT seg)
10843 {
10844   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
10845
10846   /* If this is pc-relative and we are going to emit a relocation
10847      then we just want to put out any pipeline compensation that the linker
10848      will need.  Otherwise we want to use the calculated base.  */
10849   if (fixP->fx_pcrel 
10850       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
10851           || arm_force_relocation (fixP)))
10852     base = 0;
10853
10854   switch (fixP->fx_r_type)
10855     {
10856       /* PC relative addressing on the Thumb is slightly odd as the
10857          bottom two bits of the PC are forced to zero for the
10858          calculation.  This happens *after* application of the
10859          pipeline offset.  However, Thumb adrl already adjusts for
10860          this, so we need not do it again.  */
10861     case BFD_RELOC_ARM_THUMB_ADD:
10862       return base & ~3;
10863
10864     case BFD_RELOC_ARM_THUMB_OFFSET:
10865     case BFD_RELOC_ARM_T32_OFFSET_IMM:
10866     case BFD_RELOC_ARM_T32_ADD_PC12:
10867     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
10868       return (base + 4) & ~3;
10869
10870       /* Thumb branches are simply offset by +4.  */
10871     case BFD_RELOC_THUMB_PCREL_BRANCH7:
10872     case BFD_RELOC_THUMB_PCREL_BRANCH9:
10873     case BFD_RELOC_THUMB_PCREL_BRANCH12:
10874     case BFD_RELOC_THUMB_PCREL_BRANCH20:
10875     case BFD_RELOC_THUMB_PCREL_BRANCH23:
10876     case BFD_RELOC_THUMB_PCREL_BRANCH25:
10877     case BFD_RELOC_THUMB_PCREL_BLX:
10878       return base + 4;
10879
10880       /* ARM mode branches are offset by +8.  However, the Windows CE
10881          loader expects the relocation not to take this into account.  */
10882     case BFD_RELOC_ARM_PCREL_BRANCH:
10883     case BFD_RELOC_ARM_PCREL_CALL:
10884     case BFD_RELOC_ARM_PCREL_JUMP:
10885     case BFD_RELOC_ARM_PCREL_BLX:
10886     case BFD_RELOC_ARM_PLT32:
10887 #ifdef TE_WINCE
10888       return base;
10889 #else
10890       return base + 8;
10891 #endif
10892
10893       /* ARM mode loads relative to PC are also offset by +8.  Unlike
10894          branches, the Windows CE loader *does* expect the relocation
10895          to take this into account.  */
10896     case BFD_RELOC_ARM_OFFSET_IMM:
10897     case BFD_RELOC_ARM_OFFSET_IMM8:
10898     case BFD_RELOC_ARM_HWLITERAL:
10899     case BFD_RELOC_ARM_LITERAL:
10900     case BFD_RELOC_ARM_CP_OFF_IMM:
10901       return base + 8;
10902
10903
10904       /* Other PC-relative relocations are un-offset.  */
10905     default:
10906       return base;
10907     }
10908 }
10909
10910 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
10911    Otherwise we have no need to default values of symbols.  */
10912
10913 symbolS *
10914 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
10915 {
10916 #ifdef OBJ_ELF
10917   if (name[0] == '_' && name[1] == 'G'
10918       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
10919     {
10920       if (!GOT_symbol)
10921         {
10922           if (symbol_find (name))
10923             as_bad ("GOT already in the symbol table");
10924
10925           GOT_symbol = symbol_new (name, undefined_section,
10926                                    (valueT) 0, & zero_address_frag);
10927         }
10928
10929       return GOT_symbol;
10930     }
10931 #endif
10932
10933   return 0;
10934 }
10935
10936 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
10937    computed as two separate immediate values, added together.  We
10938    already know that this value cannot be computed by just one ARM
10939    instruction.  */
10940
10941 static unsigned int
10942 validate_immediate_twopart (unsigned int   val,
10943                             unsigned int * highpart)
10944 {
10945   unsigned int a;
10946   unsigned int i;
10947
10948   for (i = 0; i < 32; i += 2)
10949     if (((a = rotate_left (val, i)) & 0xff) != 0)
10950       {
10951         if (a & 0xff00)
10952           {
10953             if (a & ~ 0xffff)
10954               continue;
10955             * highpart = (a  >> 8) | ((i + 24) << 7);
10956           }
10957         else if (a & 0xff0000)
10958           {
10959             if (a & 0xff000000)
10960               continue;
10961             * highpart = (a >> 16) | ((i + 16) << 7);
10962           }
10963         else
10964           {
10965             assert (a & 0xff000000);
10966             * highpart = (a >> 24) | ((i + 8) << 7);
10967           }
10968
10969         return (a & 0xff) | (i << 7);
10970       }
10971
10972   return FAIL;
10973 }
10974
10975 static int
10976 validate_offset_imm (unsigned int val, int hwse)
10977 {
10978   if ((hwse && val > 255) || val > 4095)
10979     return FAIL;
10980   return val;
10981 }
10982
10983 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
10984    negative immediate constant by altering the instruction.  A bit of
10985    a hack really.
10986         MOV <-> MVN
10987         AND <-> BIC
10988         ADC <-> SBC
10989         by inverting the second operand, and
10990         ADD <-> SUB
10991         CMP <-> CMN
10992         by negating the second operand.  */
10993
10994 static int
10995 negate_data_op (unsigned long * instruction,
10996                 unsigned long   value)
10997 {
10998   int op, new_inst;
10999   unsigned long negated, inverted;
11000
11001   negated = encode_arm_immediate (-value);
11002   inverted = encode_arm_immediate (~value);
11003
11004   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
11005   switch (op)
11006     {
11007       /* First negates.  */
11008     case OPCODE_SUB:             /* ADD <-> SUB  */
11009       new_inst = OPCODE_ADD;
11010       value = negated;
11011       break;
11012
11013     case OPCODE_ADD:
11014       new_inst = OPCODE_SUB;
11015       value = negated;
11016       break;
11017
11018     case OPCODE_CMP:             /* CMP <-> CMN  */
11019       new_inst = OPCODE_CMN;
11020       value = negated;
11021       break;
11022
11023     case OPCODE_CMN:
11024       new_inst = OPCODE_CMP;
11025       value = negated;
11026       break;
11027
11028       /* Now Inverted ops.  */
11029     case OPCODE_MOV:             /* MOV <-> MVN  */
11030       new_inst = OPCODE_MVN;
11031       value = inverted;
11032       break;
11033
11034     case OPCODE_MVN:
11035       new_inst = OPCODE_MOV;
11036       value = inverted;
11037       break;
11038
11039     case OPCODE_AND:             /* AND <-> BIC  */
11040       new_inst = OPCODE_BIC;
11041       value = inverted;
11042       break;
11043
11044     case OPCODE_BIC:
11045       new_inst = OPCODE_AND;
11046       value = inverted;
11047       break;
11048
11049     case OPCODE_ADC:              /* ADC <-> SBC  */
11050       new_inst = OPCODE_SBC;
11051       value = inverted;
11052       break;
11053
11054     case OPCODE_SBC:
11055       new_inst = OPCODE_ADC;
11056       value = inverted;
11057       break;
11058
11059       /* We cannot do anything.  */
11060     default:
11061       return FAIL;
11062     }
11063
11064   if (value == (unsigned) FAIL)
11065     return FAIL;
11066
11067   *instruction &= OPCODE_MASK;
11068   *instruction |= new_inst << DATA_OP_SHIFT;
11069   return value;
11070 }
11071
11072 /* Read a 32-bit thumb instruction from buf.  */
11073 static unsigned long
11074 get_thumb32_insn (char * buf)
11075 {
11076   unsigned long insn;
11077   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
11078   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
11079
11080   return insn;
11081 }
11082
11083 void
11084 md_apply_fix (fixS *    fixP,
11085                valueT * valP,
11086                segT     seg)
11087 {
11088   offsetT        value = * valP;
11089   offsetT        newval;
11090   unsigned int   newimm;
11091   unsigned long  temp;
11092   int            sign;
11093   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
11094
11095   assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
11096
11097   /* Note whether this will delete the relocation.  */
11098   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
11099     fixP->fx_done = 1;
11100
11101   /* On a 64-bit host, silently truncate 'value' to 32 bits for
11102      consistency with the behavior on 32-bit hosts.  Remember value
11103      for emit_reloc.  */
11104   value &= 0xffffffff;
11105   value ^= 0x80000000;
11106   value -= 0x80000000; 
11107
11108   *valP = value;
11109   fixP->fx_addnumber = value;
11110
11111   /* Same treatment for fixP->fx_offset.  */
11112   fixP->fx_offset &= 0xffffffff;
11113   fixP->fx_offset ^= 0x80000000;
11114   fixP->fx_offset -= 0x80000000;
11115
11116   switch (fixP->fx_r_type)
11117     {
11118     case BFD_RELOC_NONE:
11119       /* This will need to go in the object file.  */
11120       fixP->fx_done = 0;
11121       break;
11122
11123     case BFD_RELOC_ARM_IMMEDIATE:
11124       /* We claim that this fixup has been processed here,
11125          even if in fact we generate an error because we do
11126          not have a reloc for it, so tc_gen_reloc will reject it.  */
11127       fixP->fx_done = 1;
11128
11129       if (fixP->fx_addsy
11130           && ! S_IS_DEFINED (fixP->fx_addsy))
11131         {
11132           as_bad_where (fixP->fx_file, fixP->fx_line,
11133                         _("undefined symbol %s used as an immediate value"),
11134                         S_GET_NAME (fixP->fx_addsy));
11135           break;
11136         }
11137
11138       newimm = encode_arm_immediate (value);
11139       temp = md_chars_to_number (buf, INSN_SIZE);
11140
11141       /* If the instruction will fail, see if we can fix things up by
11142          changing the opcode.  */
11143       if (newimm == (unsigned int) FAIL
11144           && (newimm = negate_data_op (&temp, value)) == (unsigned int) FAIL)
11145         {
11146           as_bad_where (fixP->fx_file, fixP->fx_line,
11147                         _("invalid constant (%lx) after fixup"),
11148                         (unsigned long) value);
11149           break;
11150         }
11151
11152       newimm |= (temp & 0xfffff000);
11153       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
11154       break;
11155
11156     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
11157       {
11158         unsigned int highpart = 0;
11159         unsigned int newinsn  = 0xe1a00000; /* nop.  */
11160
11161         newimm = encode_arm_immediate (value);
11162         temp = md_chars_to_number (buf, INSN_SIZE);
11163
11164         /* If the instruction will fail, see if we can fix things up by
11165            changing the opcode.  */
11166         if (newimm == (unsigned int) FAIL
11167             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
11168           {
11169             /* No ?  OK - try using two ADD instructions to generate
11170                the value.  */
11171             newimm = validate_immediate_twopart (value, & highpart);
11172
11173             /* Yes - then make sure that the second instruction is
11174                also an add.  */
11175             if (newimm != (unsigned int) FAIL)
11176               newinsn = temp;
11177             /* Still No ?  Try using a negated value.  */
11178             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
11179               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
11180             /* Otherwise - give up.  */
11181             else
11182               {
11183                 as_bad_where (fixP->fx_file, fixP->fx_line,
11184                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
11185                               (long) value);
11186                 break;
11187               }
11188
11189             /* Replace the first operand in the 2nd instruction (which
11190                is the PC) with the destination register.  We have
11191                already added in the PC in the first instruction and we
11192                do not want to do it again.  */
11193             newinsn &= ~ 0xf0000;
11194             newinsn |= ((newinsn & 0x0f000) << 4);
11195           }
11196
11197         newimm |= (temp & 0xfffff000);
11198         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
11199
11200         highpart |= (newinsn & 0xfffff000);
11201         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
11202       }
11203       break;
11204
11205     case BFD_RELOC_ARM_OFFSET_IMM:
11206     case BFD_RELOC_ARM_LITERAL:
11207       sign = value >= 0;
11208
11209       if (value < 0)
11210         value = - value;
11211
11212       if (validate_offset_imm (value, 0) == FAIL)
11213         {
11214           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
11215             as_bad_where (fixP->fx_file, fixP->fx_line,
11216                           _("invalid literal constant: pool needs to be closer"));
11217           else
11218             as_bad_where (fixP->fx_file, fixP->fx_line,
11219                           _("bad immediate value for offset (%ld)"),
11220                           (long) value);
11221           break;
11222         }
11223
11224       newval = md_chars_to_number (buf, INSN_SIZE);
11225       newval &= 0xff7ff000;
11226       newval |= value | (sign ? INDEX_UP : 0);
11227       md_number_to_chars (buf, newval, INSN_SIZE);
11228       break;
11229
11230     case BFD_RELOC_ARM_OFFSET_IMM8:
11231     case BFD_RELOC_ARM_HWLITERAL:
11232       sign = value >= 0;
11233
11234       if (value < 0)
11235         value = - value;
11236
11237       if (validate_offset_imm (value, 1) == FAIL)
11238         {
11239           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
11240             as_bad_where (fixP->fx_file, fixP->fx_line,
11241                           _("invalid literal constant: pool needs to be closer"));
11242           else
11243             as_bad (_("bad immediate value for half-word offset (%ld)"),
11244                     (long) value);
11245           break;
11246         }
11247
11248       newval = md_chars_to_number (buf, INSN_SIZE);
11249       newval &= 0xff7ff0f0;
11250       newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
11251       md_number_to_chars (buf, newval, INSN_SIZE);
11252       break;
11253
11254     case BFD_RELOC_ARM_T32_OFFSET_U8:
11255       if (value < 0 || value > 1020 || value % 4 != 0)
11256         as_bad_where (fixP->fx_file, fixP->fx_line,
11257                       _("bad immediate value for offset (%ld)"), (long) value);
11258       value /= 4;
11259
11260       newval = md_chars_to_number (buf+2, THUMB_SIZE);
11261       newval |= value;
11262       md_number_to_chars (buf+2, newval, THUMB_SIZE);
11263       break;
11264
11265     case BFD_RELOC_ARM_T32_OFFSET_IMM:
11266       /* This is a complicated relocation used for all varieties of Thumb32
11267          load/store instruction with immediate offset:
11268
11269          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
11270                                                    *4, optional writeback(W)
11271                                                    (doubleword load/store)
11272
11273          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
11274          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
11275          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
11276          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
11277          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
11278
11279          Uppercase letters indicate bits that are already encoded at
11280          this point.  Lowercase letters are our problem.  For the
11281          second block of instructions, the secondary opcode nybble
11282          (bits 8..11) is present, and bit 23 is zero, even if this is
11283          a PC-relative operation.  */
11284       newval = md_chars_to_number (buf, THUMB_SIZE);
11285       newval <<= 16;
11286       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
11287
11288       if ((newval & 0xf0000000) == 0xe0000000)
11289         {
11290           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
11291           if (value >= 0)
11292             newval |= (1 << 23);
11293           else
11294             value = -value;
11295           if (value % 4 != 0)
11296             {
11297               as_bad_where (fixP->fx_file, fixP->fx_line,
11298                             _("offset not a multiple of 4"));
11299               break;
11300             }
11301           value /= 4;
11302           if (value >= 0xff)
11303             {
11304               as_bad_where (fixP->fx_file, fixP->fx_line,
11305                             _("offset out of range"));
11306               break;
11307             }
11308           newval &= ~0xff;
11309         }
11310       else if ((newval & 0x000f0000) == 0x000f0000)
11311         {
11312           /* PC-relative, 12-bit offset.  */
11313           if (value >= 0)
11314             newval |= (1 << 23);
11315           else
11316             value = -value;
11317           if (value >= 0xfff)
11318             {
11319               as_bad_where (fixP->fx_file, fixP->fx_line,
11320                             _("offset out of range"));
11321               break;
11322             }
11323           newval &= ~0xfff;
11324         }
11325       else if ((newval & 0x00000100) == 0x00000100)
11326         {
11327           /* Writeback: 8-bit, +/- offset.  */
11328           if (value >= 0)
11329             newval |= (1 << 9);
11330           else
11331             value = -value;
11332           if (value >= 0xff)
11333             {
11334               as_bad_where (fixP->fx_file, fixP->fx_line,
11335                             _("offset out of range"));
11336               break;
11337             }
11338           newval &= ~0xff;
11339         }
11340       else if ((newval & 0x00000f00) == 0x00000e00)
11341         {
11342           /* T-instruction: positive 8-bit offset.  */
11343           if (value < 0 || value >= 0xff)
11344             {
11345               as_bad_where (fixP->fx_file, fixP->fx_line,
11346                             _("offset out of range"));
11347               break;
11348             }
11349           newval &= ~0xff;
11350           newval |= value;
11351         }
11352       else
11353         {
11354           /* Positive 12-bit or negative 8-bit offset.  */
11355           int limit;
11356           if (value >= 0)
11357             {
11358               newval |= (1 << 23);
11359               limit = 0xfff;
11360             }
11361           else
11362             {
11363               value = -value;
11364               limit = 0xff;
11365             }
11366           if (value > limit)
11367             {
11368               as_bad_where (fixP->fx_file, fixP->fx_line,
11369                             _("offset out of range"));
11370               break;
11371             }
11372           newval &= ~limit;
11373         }
11374
11375       newval |= value;
11376       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
11377       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
11378       break;
11379
11380     case BFD_RELOC_ARM_SHIFT_IMM:
11381       newval = md_chars_to_number (buf, INSN_SIZE);
11382       if (((unsigned long) value) > 32
11383           || (value == 32
11384               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
11385         {
11386           as_bad_where (fixP->fx_file, fixP->fx_line,
11387                         _("shift expression is too large"));
11388           break;
11389         }
11390
11391       if (value == 0)
11392         /* Shifts of zero must be done as lsl.  */
11393         newval &= ~0x60;
11394       else if (value == 32)
11395         value = 0;
11396       newval &= 0xfffff07f;
11397       newval |= (value & 0x1f) << 7;
11398       md_number_to_chars (buf, newval, INSN_SIZE);
11399       break;
11400
11401     case BFD_RELOC_ARM_T32_IMMEDIATE:
11402     case BFD_RELOC_ARM_T32_IMM12:
11403     case BFD_RELOC_ARM_T32_ADD_PC12:
11404       /* We claim that this fixup has been processed here,
11405          even if in fact we generate an error because we do
11406          not have a reloc for it, so tc_gen_reloc will reject it.  */
11407       fixP->fx_done = 1;
11408
11409       if (fixP->fx_addsy
11410           && ! S_IS_DEFINED (fixP->fx_addsy))
11411         {
11412           as_bad_where (fixP->fx_file, fixP->fx_line,
11413                         _("undefined symbol %s used as an immediate value"),
11414                         S_GET_NAME (fixP->fx_addsy));
11415           break;
11416         }
11417
11418       newval = md_chars_to_number (buf, THUMB_SIZE);
11419       newval <<= 16;
11420       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
11421
11422       /* FUTURE: Implement analogue of negate_data_op for T32.  */
11423       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE)
11424         newimm = encode_thumb32_immediate (value);
11425       else
11426         {
11427           /* 12 bit immediate for addw/subw.  */
11428           if (value < 0)
11429             {
11430               value = -value;
11431               newval ^= 0x00a00000;
11432             }
11433           if (value > 0xfff)
11434             newimm = (unsigned int) FAIL;
11435           else
11436             newimm = value;
11437         }
11438
11439       if (newimm == (unsigned int)FAIL)
11440         {
11441           as_bad_where (fixP->fx_file, fixP->fx_line,
11442                         _("invalid constant (%lx) after fixup"),
11443                         (unsigned long) value);
11444           break;
11445         }
11446
11447       newval |= (newimm & 0x800) << 15;
11448       newval |= (newimm & 0x700) << 4;
11449       newval |= (newimm & 0x0ff);
11450
11451       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
11452       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
11453       break;
11454
11455     case BFD_RELOC_ARM_SMC:
11456       if (((unsigned long) value) > 0xffff)
11457         as_bad_where (fixP->fx_file, fixP->fx_line,
11458                       _("invalid smc expression"));
11459       newval = md_chars_to_number (buf, INSN_SIZE);
11460       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
11461       md_number_to_chars (buf, newval, INSN_SIZE);
11462       break;
11463
11464     case BFD_RELOC_ARM_SWI:
11465       if (fixP->tc_fix_data != 0)
11466         {
11467           if (((unsigned long) value) > 0xff)
11468             as_bad_where (fixP->fx_file, fixP->fx_line,
11469                           _("invalid swi expression"));
11470           newval = md_chars_to_number (buf, THUMB_SIZE);
11471           newval |= value;
11472           md_number_to_chars (buf, newval, THUMB_SIZE);
11473         }
11474       else
11475         {
11476           if (((unsigned long) value) > 0x00ffffff)
11477             as_bad_where (fixP->fx_file, fixP->fx_line,
11478                           _("invalid swi expression"));
11479           newval = md_chars_to_number (buf, INSN_SIZE);
11480           newval |= value;
11481           md_number_to_chars (buf, newval, INSN_SIZE);
11482         }
11483       break;
11484
11485     case BFD_RELOC_ARM_MULTI:
11486       if (((unsigned long) value) > 0xffff)
11487         as_bad_where (fixP->fx_file, fixP->fx_line,
11488                       _("invalid expression in load/store multiple"));
11489       newval = value | md_chars_to_number (buf, INSN_SIZE);
11490       md_number_to_chars (buf, newval, INSN_SIZE);
11491       break;
11492
11493 #ifdef OBJ_ELF
11494     case BFD_RELOC_ARM_PCREL_CALL:
11495       newval = md_chars_to_number (buf, INSN_SIZE);
11496       if ((newval & 0xf0000000) == 0xf0000000)
11497         temp = 1;
11498       else
11499         temp = 3;
11500       goto arm_branch_common;
11501
11502     case BFD_RELOC_ARM_PCREL_JUMP:
11503     case BFD_RELOC_ARM_PLT32:
11504 #endif
11505     case BFD_RELOC_ARM_PCREL_BRANCH:
11506       temp = 3;
11507       goto arm_branch_common;
11508
11509     case BFD_RELOC_ARM_PCREL_BLX:
11510       temp = 1;
11511     arm_branch_common:
11512       /* We are going to store value (shifted right by two) in the
11513          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
11514          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
11515          also be be clear.  */
11516       if (value & temp)
11517         as_bad_where (fixP->fx_file, fixP->fx_line,
11518                       _("misaligned branch destination"));
11519       if ((value & (offsetT)0xfe000000) != (offsetT)0
11520           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
11521         as_bad_where (fixP->fx_file, fixP->fx_line,
11522                       _("branch out of range"));
11523
11524       if (fixP->fx_done || !seg->use_rela_p)
11525         {
11526           newval = md_chars_to_number (buf, INSN_SIZE);
11527           newval |= (value >> 2) & 0x00ffffff;
11528           md_number_to_chars (buf, newval, INSN_SIZE);
11529         }
11530       break;
11531
11532     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CZB */
11533       /* CZB can only branch forward.  */
11534       if (value & ~0x7e)
11535         as_bad_where (fixP->fx_file, fixP->fx_line,
11536                       _("branch out of range"));
11537
11538       if (fixP->fx_done || !seg->use_rela_p)
11539         {
11540           newval = md_chars_to_number (buf, THUMB_SIZE);
11541           newval |= ((value & 0x2e) << 2) | ((value & 0x40) << 3);
11542           md_number_to_chars (buf, newval, THUMB_SIZE);
11543         }
11544       break;
11545
11546     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
11547       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
11548         as_bad_where (fixP->fx_file, fixP->fx_line,
11549                       _("branch out of range"));
11550
11551       if (fixP->fx_done || !seg->use_rela_p)
11552         {
11553           newval = md_chars_to_number (buf, THUMB_SIZE);
11554           newval |= (value & 0x1ff) >> 1;
11555           md_number_to_chars (buf, newval, THUMB_SIZE);
11556         }
11557       break;
11558
11559     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
11560       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
11561         as_bad_where (fixP->fx_file, fixP->fx_line,
11562                       _("branch out of range"));
11563
11564       if (fixP->fx_done || !seg->use_rela_p)
11565         {
11566           newval = md_chars_to_number (buf, THUMB_SIZE);
11567           newval |= (value & 0xfff) >> 1;
11568           md_number_to_chars (buf, newval, THUMB_SIZE);
11569         }
11570       break;
11571
11572     case BFD_RELOC_THUMB_PCREL_BRANCH20:
11573       if ((value & ~0x1fffff) && ((value & ~0x1fffff) != ~0x1fffff))
11574         as_bad_where (fixP->fx_file, fixP->fx_line,
11575                       _("conditional branch out of range"));
11576
11577       if (fixP->fx_done || !seg->use_rela_p)
11578         {
11579           offsetT newval2;
11580           addressT S, J1, J2, lo, hi;
11581
11582           S  = (value & 0x00100000) >> 20;
11583           J2 = (value & 0x00080000) >> 19;
11584           J1 = (value & 0x00040000) >> 18;
11585           hi = (value & 0x0003f000) >> 12;
11586           lo = (value & 0x00000ffe) >> 1;
11587
11588           newval   = md_chars_to_number (buf, THUMB_SIZE);
11589           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
11590           newval  |= (S << 10) | hi;
11591           newval2 |= (J1 << 13) | (J2 << 11) | lo;
11592           md_number_to_chars (buf, newval, THUMB_SIZE);
11593           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
11594         }
11595       break;
11596
11597     case BFD_RELOC_THUMB_PCREL_BLX:
11598     case BFD_RELOC_THUMB_PCREL_BRANCH23:
11599       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
11600         as_bad_where (fixP->fx_file, fixP->fx_line,
11601                       _("branch out of range"));
11602
11603       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
11604         /* For a BLX instruction, make sure that the relocation is rounded up
11605            to a word boundary.  This follows the semantics of the instruction
11606            which specifies that bit 1 of the target address will come from bit
11607            1 of the base address.  */
11608         value = (value + 1) & ~ 1;
11609
11610       if (fixP->fx_done || !seg->use_rela_p)
11611         {
11612           offsetT newval2;
11613
11614           newval   = md_chars_to_number (buf, THUMB_SIZE);
11615           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
11616           newval  |= (value & 0x7fffff) >> 12;
11617           newval2 |= (value & 0xfff) >> 1;
11618           md_number_to_chars (buf, newval, THUMB_SIZE);
11619           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
11620         }
11621       break;
11622
11623     case BFD_RELOC_THUMB_PCREL_BRANCH25:
11624       if ((value & ~0x1ffffff) && ((value & ~0x1ffffff) != ~0x1ffffff))
11625         as_bad_where (fixP->fx_file, fixP->fx_line,
11626                       _("branch out of range"));
11627
11628       if (fixP->fx_done || !seg->use_rela_p)
11629         {
11630           offsetT newval2;
11631           addressT S, I1, I2, lo, hi;
11632
11633           S  = (value & 0x01000000) >> 24;
11634           I1 = (value & 0x00800000) >> 23;
11635           I2 = (value & 0x00400000) >> 22;
11636           hi = (value & 0x003ff000) >> 12;
11637           lo = (value & 0x00000ffe) >> 1;
11638
11639           I1 = !(I1 ^ S);
11640           I2 = !(I2 ^ S);
11641
11642           newval   = md_chars_to_number (buf, THUMB_SIZE);
11643           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
11644           newval  |= (S << 10) | hi;
11645           newval2 |= (I1 << 13) | (I2 << 11) | lo;
11646           md_number_to_chars (buf, newval, THUMB_SIZE);
11647           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
11648         }
11649       break;
11650
11651     case BFD_RELOC_8:
11652       if (fixP->fx_done || !seg->use_rela_p)
11653         md_number_to_chars (buf, value, 1);
11654       break;
11655
11656     case BFD_RELOC_16:
11657       if (fixP->fx_done || !seg->use_rela_p)
11658         md_number_to_chars (buf, value, 2);
11659       break;
11660
11661 #ifdef OBJ_ELF
11662     case BFD_RELOC_ARM_TLS_GD32:
11663     case BFD_RELOC_ARM_TLS_LE32:
11664     case BFD_RELOC_ARM_TLS_IE32:
11665     case BFD_RELOC_ARM_TLS_LDM32:
11666     case BFD_RELOC_ARM_TLS_LDO32:
11667       S_SET_THREAD_LOCAL (fixP->fx_addsy);
11668       /* fall through */
11669
11670     case BFD_RELOC_ARM_GOT32:
11671     case BFD_RELOC_ARM_GOTOFF:
11672     case BFD_RELOC_ARM_TARGET2:
11673       if (fixP->fx_done || !seg->use_rela_p)
11674         md_number_to_chars (buf, 0, 4);
11675       break;
11676 #endif
11677
11678     case BFD_RELOC_RVA:
11679     case BFD_RELOC_32:
11680     case BFD_RELOC_ARM_TARGET1:
11681     case BFD_RELOC_ARM_ROSEGREL32:
11682     case BFD_RELOC_ARM_SBREL32:
11683     case BFD_RELOC_32_PCREL:
11684       if (fixP->fx_done || !seg->use_rela_p)
11685         md_number_to_chars (buf, value, 4);
11686       break;
11687
11688 #ifdef OBJ_ELF
11689     case BFD_RELOC_ARM_PREL31:
11690       if (fixP->fx_done || !seg->use_rela_p)
11691         {
11692           newval = md_chars_to_number (buf, 4) & 0x80000000;
11693           if ((value ^ (value >> 1)) & 0x40000000)
11694             {
11695               as_bad_where (fixP->fx_file, fixP->fx_line,
11696                             _("rel31 relocation overflow"));
11697             }
11698           newval |= value & 0x7fffffff;
11699           md_number_to_chars (buf, newval, 4);
11700         }
11701       break;
11702 #endif
11703
11704     case BFD_RELOC_ARM_CP_OFF_IMM:
11705     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
11706       if (value < -1023 || value > 1023 || (value & 3))
11707         as_bad_where (fixP->fx_file, fixP->fx_line,
11708                       _("co-processor offset out of range"));
11709     cp_off_common:
11710       sign = value >= 0;
11711       if (value < 0)
11712         value = -value;
11713       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
11714           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
11715         newval = md_chars_to_number (buf, INSN_SIZE);
11716       else
11717         newval = get_thumb32_insn (buf);
11718       newval &= 0xff7fff00;
11719       newval |= (value >> 2) | (sign ? INDEX_UP : 0);
11720       if (value == 0)
11721         newval &= ~WRITE_BACK;
11722       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
11723           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
11724         md_number_to_chars (buf, newval, INSN_SIZE);
11725       else
11726         put_thumb32_insn (buf, newval);
11727       break;
11728
11729     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
11730     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
11731       if (value < -255 || value > 255)
11732         as_bad_where (fixP->fx_file, fixP->fx_line,
11733                       _("co-processor offset out of range"));
11734       goto cp_off_common;
11735
11736     case BFD_RELOC_ARM_THUMB_OFFSET:
11737       newval = md_chars_to_number (buf, THUMB_SIZE);
11738       /* Exactly what ranges, and where the offset is inserted depends
11739          on the type of instruction, we can establish this from the
11740          top 4 bits.  */
11741       switch (newval >> 12)
11742         {
11743         case 4: /* PC load.  */
11744           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
11745              forced to zero for these loads; md_pcrel_from has already
11746              compensated for this.  */
11747           if (value & 3)
11748             as_bad_where (fixP->fx_file, fixP->fx_line,
11749                           _("invalid offset, target not word aligned (0x%08lX)"),
11750                           (((unsigned long) fixP->fx_frag->fr_address
11751                             + (unsigned long) fixP->fx_where) & ~3)
11752                           + (unsigned long) value);
11753
11754           if (value & ~0x3fc)
11755             as_bad_where (fixP->fx_file, fixP->fx_line,
11756                           _("invalid offset, value too big (0x%08lX)"),
11757                           (long) value);
11758
11759           newval |= value >> 2;
11760           break;
11761
11762         case 9: /* SP load/store.  */
11763           if (value & ~0x3fc)
11764             as_bad_where (fixP->fx_file, fixP->fx_line,
11765                           _("invalid offset, value too big (0x%08lX)"),
11766                           (long) value);
11767           newval |= value >> 2;
11768           break;
11769
11770         case 6: /* Word load/store.  */
11771           if (value & ~0x7c)
11772             as_bad_where (fixP->fx_file, fixP->fx_line,
11773                           _("invalid offset, value too big (0x%08lX)"),
11774                           (long) value);
11775           newval |= value << 4; /* 6 - 2.  */
11776           break;
11777
11778         case 7: /* Byte load/store.  */
11779           if (value & ~0x1f)
11780             as_bad_where (fixP->fx_file, fixP->fx_line,
11781                           _("invalid offset, value too big (0x%08lX)"),
11782                           (long) value);
11783           newval |= value << 6;
11784           break;
11785
11786         case 8: /* Halfword load/store.  */
11787           if (value & ~0x3e)
11788             as_bad_where (fixP->fx_file, fixP->fx_line,
11789                           _("invalid offset, value too big (0x%08lX)"),
11790                           (long) value);
11791           newval |= value << 5; /* 6 - 1.  */
11792           break;
11793
11794         default:
11795           as_bad_where (fixP->fx_file, fixP->fx_line,
11796                         "Unable to process relocation for thumb opcode: %lx",
11797                         (unsigned long) newval);
11798           break;
11799         }
11800       md_number_to_chars (buf, newval, THUMB_SIZE);
11801       break;
11802
11803     case BFD_RELOC_ARM_THUMB_ADD:
11804       /* This is a complicated relocation, since we use it for all of
11805          the following immediate relocations:
11806
11807             3bit ADD/SUB
11808             8bit ADD/SUB
11809             9bit ADD/SUB SP word-aligned
11810            10bit ADD PC/SP word-aligned
11811
11812          The type of instruction being processed is encoded in the
11813          instruction field:
11814
11815            0x8000  SUB
11816            0x00F0  Rd
11817            0x000F  Rs
11818       */
11819       newval = md_chars_to_number (buf, THUMB_SIZE);
11820       {
11821         int rd = (newval >> 4) & 0xf;
11822         int rs = newval & 0xf;
11823         int subtract = !!(newval & 0x8000);
11824
11825         /* Check for HI regs, only very restricted cases allowed:
11826            Adjusting SP, and using PC or SP to get an address.  */
11827         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
11828             || (rs > 7 && rs != REG_SP && rs != REG_PC))
11829           as_bad_where (fixP->fx_file, fixP->fx_line,
11830                         _("invalid Hi register with immediate"));
11831
11832         /* If value is negative, choose the opposite instruction.  */
11833         if (value < 0)
11834           {
11835             value = -value;
11836             subtract = !subtract;
11837             if (value < 0)
11838               as_bad_where (fixP->fx_file, fixP->fx_line,
11839                             _("immediate value out of range"));
11840           }
11841
11842         if (rd == REG_SP)
11843           {
11844             if (value & ~0x1fc)
11845               as_bad_where (fixP->fx_file, fixP->fx_line,
11846                             _("invalid immediate for stack address calculation"));
11847             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
11848             newval |= value >> 2;
11849           }
11850         else if (rs == REG_PC || rs == REG_SP)
11851           {
11852             if (subtract || value & ~0x3fc)
11853               as_bad_where (fixP->fx_file, fixP->fx_line,
11854                             _("invalid immediate for address calculation (value = 0x%08lX)"),
11855                             (unsigned long) value);
11856             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
11857             newval |= rd << 8;
11858             newval |= value >> 2;
11859           }
11860         else if (rs == rd)
11861           {
11862             if (value & ~0xff)
11863               as_bad_where (fixP->fx_file, fixP->fx_line,
11864                             _("immediate value out of range"));
11865             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
11866             newval |= (rd << 8) | value;
11867           }
11868         else
11869           {
11870             if (value & ~0x7)
11871               as_bad_where (fixP->fx_file, fixP->fx_line,
11872                             _("immediate value out of range"));
11873             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
11874             newval |= rd | (rs << 3) | (value << 6);
11875           }
11876       }
11877       md_number_to_chars (buf, newval, THUMB_SIZE);
11878       break;
11879
11880     case BFD_RELOC_ARM_THUMB_IMM:
11881       newval = md_chars_to_number (buf, THUMB_SIZE);
11882       if (value < 0 || value > 255)
11883         as_bad_where (fixP->fx_file, fixP->fx_line,
11884                       _("invalid immediate: %ld is too large"),
11885                       (long) value);
11886       newval |= value;
11887       md_number_to_chars (buf, newval, THUMB_SIZE);
11888       break;
11889
11890     case BFD_RELOC_ARM_THUMB_SHIFT:
11891       /* 5bit shift value (0..32).  LSL cannot take 32.  */
11892       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
11893       temp = newval & 0xf800;
11894       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
11895         as_bad_where (fixP->fx_file, fixP->fx_line,
11896                       _("invalid shift value: %ld"), (long) value);
11897       /* Shifts of zero must be encoded as LSL.  */
11898       if (value == 0)
11899         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
11900       /* Shifts of 32 are encoded as zero.  */
11901       else if (value == 32)
11902         value = 0;
11903       newval |= value << 6;
11904       md_number_to_chars (buf, newval, THUMB_SIZE);
11905       break;
11906
11907     case BFD_RELOC_VTABLE_INHERIT:
11908     case BFD_RELOC_VTABLE_ENTRY:
11909       fixP->fx_done = 0;
11910       return;
11911
11912     case BFD_RELOC_UNUSED:
11913     default:
11914       as_bad_where (fixP->fx_file, fixP->fx_line,
11915                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
11916     }
11917 }
11918
11919 /* Translate internal representation of relocation info to BFD target
11920    format.  */
11921
11922 arelent *
11923 tc_gen_reloc (asection * section ATTRIBUTE_UNUSED,
11924               fixS *     fixp)
11925 {
11926   arelent * reloc;
11927   bfd_reloc_code_real_type code;
11928
11929   reloc = xmalloc (sizeof (arelent));
11930
11931   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
11932   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
11933   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11934
11935   if (fixp->fx_pcrel)
11936     fixp->fx_offset = reloc->address;
11937   reloc->addend = fixp->fx_offset;
11938
11939   switch (fixp->fx_r_type)
11940     {
11941     case BFD_RELOC_8:
11942       if (fixp->fx_pcrel)
11943         {
11944           code = BFD_RELOC_8_PCREL;
11945           break;
11946         }
11947
11948     case BFD_RELOC_16:
11949       if (fixp->fx_pcrel)
11950         {
11951           code = BFD_RELOC_16_PCREL;
11952           break;
11953         }
11954
11955     case BFD_RELOC_32:
11956       if (fixp->fx_pcrel)
11957         {
11958           code = BFD_RELOC_32_PCREL;
11959           break;
11960         }
11961
11962     case BFD_RELOC_NONE:
11963     case BFD_RELOC_ARM_PCREL_BRANCH:
11964     case BFD_RELOC_ARM_PCREL_BLX:
11965     case BFD_RELOC_RVA:
11966     case BFD_RELOC_THUMB_PCREL_BRANCH7:
11967     case BFD_RELOC_THUMB_PCREL_BRANCH9:
11968     case BFD_RELOC_THUMB_PCREL_BRANCH12:
11969     case BFD_RELOC_THUMB_PCREL_BRANCH20:
11970     case BFD_RELOC_THUMB_PCREL_BRANCH23:
11971     case BFD_RELOC_THUMB_PCREL_BRANCH25:
11972     case BFD_RELOC_THUMB_PCREL_BLX:
11973     case BFD_RELOC_VTABLE_ENTRY:
11974     case BFD_RELOC_VTABLE_INHERIT:
11975       code = fixp->fx_r_type;
11976       break;
11977
11978     case BFD_RELOC_ARM_LITERAL:
11979     case BFD_RELOC_ARM_HWLITERAL:
11980       /* If this is called then the a literal has
11981          been referenced across a section boundary.  */
11982       as_bad_where (fixp->fx_file, fixp->fx_line,
11983                     _("literal referenced across section boundary"));
11984       return NULL;
11985
11986 #ifdef OBJ_ELF
11987     case BFD_RELOC_ARM_GOT32:
11988     case BFD_RELOC_ARM_GOTOFF:
11989     case BFD_RELOC_ARM_PLT32:
11990     case BFD_RELOC_ARM_TARGET1:
11991     case BFD_RELOC_ARM_ROSEGREL32:
11992     case BFD_RELOC_ARM_SBREL32:
11993     case BFD_RELOC_ARM_PREL31:
11994     case BFD_RELOC_ARM_TARGET2:
11995     case BFD_RELOC_ARM_TLS_LE32:
11996     case BFD_RELOC_ARM_TLS_LDO32:
11997     case BFD_RELOC_ARM_PCREL_CALL:
11998     case BFD_RELOC_ARM_PCREL_JUMP:
11999       code = fixp->fx_r_type;
12000       break;
12001
12002     case BFD_RELOC_ARM_TLS_GD32:
12003     case BFD_RELOC_ARM_TLS_IE32:
12004     case BFD_RELOC_ARM_TLS_LDM32:
12005       /* BFD will include the symbol's address in the addend.
12006          But we don't want that, so subtract it out again here.  */
12007       if (!S_IS_COMMON (fixp->fx_addsy))
12008         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
12009       code = fixp->fx_r_type;
12010       break;
12011 #endif
12012
12013     case BFD_RELOC_ARM_IMMEDIATE:
12014       as_bad_where (fixp->fx_file, fixp->fx_line,
12015                     _("internal relocation (type: IMMEDIATE) not fixed up"));
12016       return NULL;
12017
12018     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
12019       as_bad_where (fixp->fx_file, fixp->fx_line,
12020                     _("ADRL used for a symbol not defined in the same file"));
12021       return NULL;
12022
12023     case BFD_RELOC_ARM_OFFSET_IMM:
12024       if (fixp->fx_addsy != NULL
12025           && !S_IS_DEFINED (fixp->fx_addsy)
12026           && S_IS_LOCAL (fixp->fx_addsy))
12027         {
12028           as_bad_where (fixp->fx_file, fixp->fx_line,
12029                         _("undefined local label `%s'"),
12030                         S_GET_NAME (fixp->fx_addsy));
12031           return NULL;
12032         }
12033
12034       as_bad_where (fixp->fx_file, fixp->fx_line,
12035                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
12036       return NULL;
12037
12038     default:
12039       {
12040         char * type;
12041
12042         switch (fixp->fx_r_type)
12043           {
12044           case BFD_RELOC_NONE:             type = "NONE";         break;
12045           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
12046           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
12047           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
12048           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
12049           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
12050           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
12051           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
12052           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
12053           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
12054           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
12055           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
12056           default:                         type = _("<unknown>"); break;
12057           }
12058         as_bad_where (fixp->fx_file, fixp->fx_line,
12059                       _("cannot represent %s relocation in this object file format"),
12060                       type);
12061         return NULL;
12062       }
12063     }
12064
12065 #ifdef OBJ_ELF
12066   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
12067       && GOT_symbol
12068       && fixp->fx_addsy == GOT_symbol)
12069     {
12070       code = BFD_RELOC_ARM_GOTPC;
12071       reloc->addend = fixp->fx_offset = reloc->address;
12072     }
12073 #endif
12074
12075   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12076
12077   if (reloc->howto == NULL)
12078     {
12079       as_bad_where (fixp->fx_file, fixp->fx_line,
12080                     _("cannot represent %s relocation in this object file format"),
12081                     bfd_get_reloc_code_name (code));
12082       return NULL;
12083     }
12084
12085   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
12086      vtable entry to be used in the relocation's section offset.  */
12087   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12088     reloc->address = fixp->fx_offset;
12089
12090   return reloc;
12091 }
12092
12093 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
12094
12095 void
12096 cons_fix_new_arm (fragS *       frag,
12097                   int           where,
12098                   int           size,
12099                   expressionS * exp)
12100 {
12101   bfd_reloc_code_real_type type;
12102   int pcrel = 0;
12103
12104   /* Pick a reloc.
12105      FIXME: @@ Should look at CPU word size.  */
12106   switch (size)
12107     {
12108     case 1:
12109       type = BFD_RELOC_8;
12110       break;
12111     case 2:
12112       type = BFD_RELOC_16;
12113       break;
12114     case 4:
12115     default:
12116       type = BFD_RELOC_32;
12117       break;
12118     case 8:
12119       type = BFD_RELOC_64;
12120       break;
12121     }
12122
12123   fix_new_exp (frag, where, (int) size, exp, pcrel, type);
12124 }
12125
12126 #if defined OBJ_COFF || defined OBJ_ELF
12127 void
12128 arm_validate_fix (fixS * fixP)
12129 {
12130   /* If the destination of the branch is a defined symbol which does not have
12131      the THUMB_FUNC attribute, then we must be calling a function which has
12132      the (interfacearm) attribute.  We look for the Thumb entry point to that
12133      function and change the branch to refer to that function instead.  */
12134   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
12135       && fixP->fx_addsy != NULL
12136       && S_IS_DEFINED (fixP->fx_addsy)
12137       && ! THUMB_IS_FUNC (fixP->fx_addsy))
12138     {
12139       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
12140     }
12141 }
12142 #endif
12143
12144 int
12145 arm_force_relocation (struct fix * fixp)
12146 {
12147 #if defined (OBJ_COFF) && defined (TE_PE)
12148   if (fixp->fx_r_type == BFD_RELOC_RVA)
12149     return 1;
12150 #endif
12151
12152   /* Resolve these relocations even if the symbol is extern or weak.  */
12153   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
12154       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
12155       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
12156       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
12157       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
12158       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12)
12159     return 0;
12160
12161   return generic_force_reloc (fixp);
12162 }
12163
12164 #ifdef OBJ_COFF
12165 /* This is a little hack to help the gas/arm/adrl.s test.  It prevents
12166    local labels from being added to the output symbol table when they
12167    are used with the ADRL pseudo op.  The ADRL relocation should always
12168    be resolved before the binbary is emitted, so it is safe to say that
12169    it is adjustable.  */
12170
12171 bfd_boolean
12172 arm_fix_adjustable (fixS * fixP)
12173 {
12174   if (fixP->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE)
12175     return 1;
12176   return 0;
12177 }
12178 #endif
12179
12180 #ifdef OBJ_ELF
12181 /* Relocations against Thumb function names must be left unadjusted,
12182    so that the linker can use this information to correctly set the
12183    bottom bit of their addresses.  The MIPS version of this function
12184    also prevents relocations that are mips-16 specific, but I do not
12185    know why it does this.
12186
12187    FIXME:
12188    There is one other problem that ought to be addressed here, but
12189    which currently is not:  Taking the address of a label (rather
12190    than a function) and then later jumping to that address.  Such
12191    addresses also ought to have their bottom bit set (assuming that
12192    they reside in Thumb code), but at the moment they will not.  */
12193
12194 bfd_boolean
12195 arm_fix_adjustable (fixS * fixP)
12196 {
12197   if (fixP->fx_addsy == NULL)
12198     return 1;
12199
12200   if (THUMB_IS_FUNC (fixP->fx_addsy)
12201       && fixP->fx_subsy == NULL)
12202     return 0;
12203
12204   /* We need the symbol name for the VTABLE entries.  */
12205   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12206       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12207     return 0;
12208
12209   /* Don't allow symbols to be discarded on GOT related relocs.  */
12210   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
12211       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
12212       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
12213       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
12214       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
12215       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
12216       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
12217       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
12218       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
12219     return 0;
12220
12221   return 1;
12222 }
12223
12224 const char *
12225 elf32_arm_target_format (void)
12226 {
12227 #ifdef TE_SYMBIAN
12228   return (target_big_endian
12229           ? "elf32-bigarm-symbian"
12230           : "elf32-littlearm-symbian");
12231 #elif defined (TE_VXWORKS)
12232   return (target_big_endian
12233           ? "elf32-bigarm-vxworks"
12234           : "elf32-littlearm-vxworks");
12235 #else
12236   if (target_big_endian)
12237     return "elf32-bigarm";
12238   else
12239     return "elf32-littlearm";
12240 #endif
12241 }
12242
12243 void
12244 armelf_frob_symbol (symbolS * symp,
12245                     int *     puntp)
12246 {
12247   elf_frob_symbol (symp, puntp);
12248 }
12249 #endif
12250
12251 /* MD interface: Finalization.  */
12252
12253 /* A good place to do this, although this was probably not intended
12254    for this kind of use.  We need to dump the literal pool before
12255    references are made to a null symbol pointer.  */
12256
12257 void
12258 arm_cleanup (void)
12259 {
12260   literal_pool * pool;
12261
12262   for (pool = list_of_pools; pool; pool = pool->next)
12263     {
12264       /* Put it at the end of the relevent section.  */
12265       subseg_set (pool->section, pool->sub_section);
12266 #ifdef OBJ_ELF
12267       arm_elf_change_section ();
12268 #endif
12269       s_ltorg (0);
12270     }
12271 }
12272
12273 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
12274    ARM ones.  */
12275
12276 void
12277 arm_adjust_symtab (void)
12278 {
12279 #ifdef OBJ_COFF
12280   symbolS * sym;
12281
12282   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
12283     {
12284       if (ARM_IS_THUMB (sym))
12285         {
12286           if (THUMB_IS_FUNC (sym))
12287             {
12288               /* Mark the symbol as a Thumb function.  */
12289               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
12290                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
12291                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
12292
12293               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
12294                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
12295               else
12296                 as_bad (_("%s: unexpected function type: %d"),
12297                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
12298             }
12299           else switch (S_GET_STORAGE_CLASS (sym))
12300             {
12301             case C_EXT:
12302               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
12303               break;
12304             case C_STAT:
12305               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
12306               break;
12307             case C_LABEL:
12308               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
12309               break;
12310             default:
12311               /* Do nothing.  */
12312               break;
12313             }
12314         }
12315
12316       if (ARM_IS_INTERWORK (sym))
12317         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
12318     }
12319 #endif
12320 #ifdef OBJ_ELF
12321   symbolS * sym;
12322   char      bind;
12323
12324   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
12325     {
12326       if (ARM_IS_THUMB (sym))
12327         {
12328           elf_symbol_type * elf_sym;
12329
12330           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
12331           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
12332
12333           if (! bfd_is_arm_mapping_symbol_name (elf_sym->symbol.name))
12334             {
12335               /* If it's a .thumb_func, declare it as so,
12336                  otherwise tag label as .code 16.  */
12337               if (THUMB_IS_FUNC (sym))
12338                 elf_sym->internal_elf_sym.st_info =
12339                   ELF_ST_INFO (bind, STT_ARM_TFUNC);
12340               else
12341                 elf_sym->internal_elf_sym.st_info =
12342                   ELF_ST_INFO (bind, STT_ARM_16BIT);
12343             }
12344         }
12345     }
12346 #endif
12347 }
12348
12349 /* MD interface: Initialization.  */
12350
12351 static void
12352 set_constant_flonums (void)
12353 {
12354   int i;
12355
12356   for (i = 0; i < NUM_FLOAT_VALS; i++)
12357     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
12358       abort ();
12359 }
12360
12361 void
12362 md_begin (void)
12363 {
12364   unsigned mach;
12365   unsigned int i;
12366
12367   if (   (arm_ops_hsh = hash_new ()) == NULL
12368       || (arm_cond_hsh = hash_new ()) == NULL
12369       || (arm_shift_hsh = hash_new ()) == NULL
12370       || (arm_psr_hsh = hash_new ()) == NULL
12371       || (arm_reg_hsh = hash_new ()) == NULL
12372       || (arm_reloc_hsh = hash_new ()) == NULL)
12373     as_fatal (_("virtual memory exhausted"));
12374
12375   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
12376     hash_insert (arm_ops_hsh, insns[i].template, (PTR) (insns + i));
12377   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
12378     hash_insert (arm_cond_hsh, conds[i].template, (PTR) (conds + i));
12379   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
12380     hash_insert (arm_shift_hsh, shift_names[i].name, (PTR) (shift_names + i));
12381   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
12382     hash_insert (arm_psr_hsh, psrs[i].template, (PTR) (psrs + i));
12383   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
12384     hash_insert (arm_reg_hsh, reg_names[i].name, (PTR) (reg_names + i));
12385 #ifdef OBJ_ELF
12386   for (i = 0; i < sizeof (reloc_names) / sizeof (struct reloc_entry); i++)
12387     hash_insert (arm_reloc_hsh, reloc_names[i].name, (PTR) (reloc_names + i));
12388 #endif
12389
12390   set_constant_flonums ();
12391
12392   /* Set the cpu variant based on the command-line options.  We prefer
12393      -mcpu= over -march= if both are set (as for GCC); and we prefer
12394      -mfpu= over any other way of setting the floating point unit.
12395      Use of legacy options with new options are faulted.  */
12396   if (legacy_cpu != -1)
12397     {
12398       if (mcpu_cpu_opt != -1 || march_cpu_opt != -1)
12399         as_bad (_("use of old and new-style options to set CPU type"));
12400
12401       mcpu_cpu_opt = legacy_cpu;
12402     }
12403   else if (mcpu_cpu_opt == -1)
12404     mcpu_cpu_opt = march_cpu_opt;
12405
12406   if (legacy_fpu != -1)
12407     {
12408       if (mfpu_opt != -1)
12409         as_bad (_("use of old and new-style options to set FPU type"));
12410
12411       mfpu_opt = legacy_fpu;
12412     }
12413   else if (mfpu_opt == -1)
12414     {
12415 #if !(defined (TE_LINUX) || defined (TE_NetBSD) || defined (TE_VXWORKS))
12416       /* Some environments specify a default FPU.  If they don't, infer it
12417          from the processor.  */
12418       if (mcpu_fpu_opt != -1)
12419         mfpu_opt = mcpu_fpu_opt;
12420       else
12421         mfpu_opt = march_fpu_opt;
12422 #else
12423       mfpu_opt = FPU_DEFAULT;
12424 #endif
12425     }
12426
12427   if (mfpu_opt == -1)
12428     {
12429       if (mcpu_cpu_opt == -1)
12430         mfpu_opt = FPU_DEFAULT;
12431       else if (mcpu_cpu_opt & ARM_EXT_V5)
12432         mfpu_opt = FPU_ARCH_VFP_V2;
12433       else
12434         mfpu_opt = FPU_ARCH_FPA;
12435     }
12436
12437 #ifdef CPU_DEFAULT
12438   if (mcpu_cpu_opt == -1)
12439     selected_cpu = mcpu_cpu_opt = CPU_DEFAULT;
12440 #else
12441   if (mcpu_cpu_opt == -1)
12442     {
12443       mcpu_cpu_opt = ARM_ANY;
12444       selected_cpu = 0;
12445     }
12446   else
12447     selected_cpu = mcpu_cpu_opt;
12448 #endif
12449
12450   cpu_variant = mcpu_cpu_opt | mfpu_opt;
12451
12452   arm_arch_used = thumb_arch_used = 0;
12453
12454 #if defined OBJ_COFF || defined OBJ_ELF
12455   {
12456     unsigned int flags = 0;
12457
12458 #if defined OBJ_ELF
12459     flags = meabi_flags;
12460
12461     switch (meabi_flags)
12462       {
12463       case EF_ARM_EABI_UNKNOWN:
12464 #endif
12465         /* Set the flags in the private structure.  */
12466         if (uses_apcs_26)      flags |= F_APCS26;
12467         if (support_interwork) flags |= F_INTERWORK;
12468         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
12469         if (pic_code)          flags |= F_PIC;
12470         if ((cpu_variant & FPU_ANY) == FPU_NONE
12471              || (cpu_variant & FPU_ANY) == FPU_ARCH_VFP) /* VFP layout only.  */
12472           flags |= F_SOFT_FLOAT;
12473
12474         switch (mfloat_abi_opt)
12475           {
12476           case ARM_FLOAT_ABI_SOFT:
12477           case ARM_FLOAT_ABI_SOFTFP:
12478             flags |= F_SOFT_FLOAT;
12479             break;
12480
12481           case ARM_FLOAT_ABI_HARD:
12482             if (flags & F_SOFT_FLOAT)
12483               as_bad (_("hard-float conflicts with specified fpu"));
12484             break;
12485           }
12486
12487         /* Using VFP conventions (even if soft-float).  */
12488         if (cpu_variant & FPU_VFP_EXT_NONE)
12489           flags |= F_VFP_FLOAT;
12490
12491 #if defined OBJ_ELF
12492         if (cpu_variant & FPU_ARCH_MAVERICK)
12493             flags |= EF_ARM_MAVERICK_FLOAT;
12494         break;
12495
12496       case EF_ARM_EABI_VER4:
12497         /* No additional flags to set.  */
12498         break;
12499
12500       default:
12501         abort ();
12502       }
12503 #endif
12504     bfd_set_private_flags (stdoutput, flags);
12505
12506     /* We have run out flags in the COFF header to encode the
12507        status of ATPCS support, so instead we create a dummy,
12508        empty, debug section called .arm.atpcs.  */
12509     if (atpcs)
12510       {
12511         asection * sec;
12512
12513         sec = bfd_make_section (stdoutput, ".arm.atpcs");
12514
12515         if (sec != NULL)
12516           {
12517             bfd_set_section_flags
12518               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
12519             bfd_set_section_size (stdoutput, sec, 0);
12520             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
12521           }
12522       }
12523   }
12524 #endif
12525
12526   /* Record the CPU type as well.  */
12527   switch (cpu_variant & ARM_CPU_MASK)
12528     {
12529     case ARM_2:
12530       mach = bfd_mach_arm_2;
12531       break;
12532
12533     case ARM_3:                 /* Also ARM_250.  */
12534       mach = bfd_mach_arm_2a;
12535       break;
12536
12537     case ARM_6:                 /* Also ARM_7.  */
12538       mach = bfd_mach_arm_3;
12539       break;
12540
12541     default:
12542       mach = bfd_mach_arm_unknown;
12543       break;
12544     }
12545
12546   /* Catch special cases.  */
12547   if (cpu_variant & ARM_CEXT_IWMMXT)
12548     mach = bfd_mach_arm_iWMMXt;
12549   else if (cpu_variant & ARM_CEXT_XSCALE)
12550     mach = bfd_mach_arm_XScale;
12551   else if (cpu_variant & ARM_CEXT_MAVERICK)
12552     mach = bfd_mach_arm_ep9312;
12553   else if (cpu_variant & ARM_EXT_V5E)
12554     mach = bfd_mach_arm_5TE;
12555   else if (cpu_variant & ARM_EXT_V5)
12556     {
12557       if (cpu_variant & ARM_EXT_V4T)
12558         mach = bfd_mach_arm_5T;
12559       else
12560         mach = bfd_mach_arm_5;
12561     }
12562   else if (cpu_variant & ARM_EXT_V4)
12563     {
12564       if (cpu_variant & ARM_EXT_V4T)
12565         mach = bfd_mach_arm_4T;
12566       else
12567         mach = bfd_mach_arm_4;
12568     }
12569   else if (cpu_variant & ARM_EXT_V3M)
12570     mach = bfd_mach_arm_3M;
12571
12572   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
12573 }
12574
12575 /* Command line processing.  */
12576
12577 /* md_parse_option
12578       Invocation line includes a switch not recognized by the base assembler.
12579       See if it's a processor-specific option.
12580
12581       This routine is somewhat complicated by the need for backwards
12582       compatibility (since older releases of gcc can't be changed).
12583       The new options try to make the interface as compatible as
12584       possible with GCC.
12585
12586       New options (supported) are:
12587
12588               -mcpu=<cpu name>           Assemble for selected processor
12589               -march=<architecture name> Assemble for selected architecture
12590               -mfpu=<fpu architecture>   Assemble for selected FPU.
12591               -EB/-mbig-endian           Big-endian
12592               -EL/-mlittle-endian        Little-endian
12593               -k                         Generate PIC code
12594               -mthumb                    Start in Thumb mode
12595               -mthumb-interwork          Code supports ARM/Thumb interworking
12596
12597       For now we will also provide support for:
12598
12599               -mapcs-32                  32-bit Program counter
12600               -mapcs-26                  26-bit Program counter
12601               -macps-float               Floats passed in FP registers
12602               -mapcs-reentrant           Reentrant code
12603               -matpcs
12604       (sometime these will probably be replaced with -mapcs=<list of options>
12605       and -matpcs=<list of options>)
12606
12607       The remaining options are only supported for back-wards compatibility.
12608       Cpu variants, the arm part is optional:
12609               -m[arm]1                Currently not supported.
12610               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
12611               -m[arm]3                Arm 3 processor
12612               -m[arm]6[xx],           Arm 6 processors
12613               -m[arm]7[xx][t][[d]m]   Arm 7 processors
12614               -m[arm]8[10]            Arm 8 processors
12615               -m[arm]9[20][tdmi]      Arm 9 processors
12616               -mstrongarm[110[0]]     StrongARM processors
12617               -mxscale                XScale processors
12618               -m[arm]v[2345[t[e]]]    Arm architectures
12619               -mall                   All (except the ARM1)
12620       FP variants:
12621               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
12622               -mfpe-old               (No float load/store multiples)
12623               -mvfpxd                 VFP Single precision
12624               -mvfp                   All VFP
12625               -mno-fpu                Disable all floating point instructions
12626
12627       The following CPU names are recognized:
12628               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
12629               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
12630               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
12631               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
12632               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
12633               arm10t arm10e, arm1020t, arm1020e, arm10200e,
12634               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
12635
12636       */
12637
12638 const char * md_shortopts = "m:k";
12639
12640 #ifdef ARM_BI_ENDIAN
12641 #define OPTION_EB (OPTION_MD_BASE + 0)
12642 #define OPTION_EL (OPTION_MD_BASE + 1)
12643 #else
12644 #if TARGET_BYTES_BIG_ENDIAN
12645 #define OPTION_EB (OPTION_MD_BASE + 0)
12646 #else
12647 #define OPTION_EL (OPTION_MD_BASE + 1)
12648 #endif
12649 #endif
12650
12651 struct option md_longopts[] =
12652 {
12653 #ifdef OPTION_EB
12654   {"EB", no_argument, NULL, OPTION_EB},
12655 #endif
12656 #ifdef OPTION_EL
12657   {"EL", no_argument, NULL, OPTION_EL},
12658 #endif
12659   {NULL, no_argument, NULL, 0}
12660 };
12661
12662 size_t md_longopts_size = sizeof (md_longopts);
12663
12664 struct arm_option_table
12665 {
12666   char *option;         /* Option name to match.  */
12667   char *help;           /* Help information.  */
12668   int  *var;            /* Variable to change.  */
12669   int   value;          /* What to change it to.  */
12670   char *deprecated;     /* If non-null, print this message.  */
12671 };
12672
12673 struct arm_option_table arm_opts[] =
12674 {
12675   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
12676   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
12677   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
12678    &support_interwork, 1, NULL},
12679   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
12680   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
12681   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
12682    1, NULL},
12683   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
12684   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
12685   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
12686   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
12687    NULL},
12688
12689   /* These are recognized by the assembler, but have no affect on code.  */
12690   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
12691   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
12692
12693   /* DON'T add any new processors to this list -- we want the whole list
12694      to go away...  Add them to the processors table instead.  */
12695   {"marm1",      NULL, &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
12696   {"m1",         NULL, &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
12697   {"marm2",      NULL, &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
12698   {"m2",         NULL, &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
12699   {"marm250",    NULL, &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
12700   {"m250",       NULL, &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
12701   {"marm3",      NULL, &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
12702   {"m3",         NULL, &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
12703   {"marm6",      NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
12704   {"m6",         NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
12705   {"marm600",    NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
12706   {"m600",       NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
12707   {"marm610",    NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
12708   {"m610",       NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
12709   {"marm620",    NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
12710   {"m620",       NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
12711   {"marm7",      NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
12712   {"m7",         NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
12713   {"marm70",     NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
12714   {"m70",        NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
12715   {"marm700",    NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
12716   {"m700",       NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
12717   {"marm700i",   NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
12718   {"m700i",      NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
12719   {"marm710",    NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
12720   {"m710",       NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
12721   {"marm710c",   NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
12722   {"m710c",      NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
12723   {"marm720",    NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
12724   {"m720",       NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
12725   {"marm7d",     NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
12726   {"m7d",        NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
12727   {"marm7di",    NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
12728   {"m7di",       NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
12729   {"marm7m",     NULL, &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
12730   {"m7m",        NULL, &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
12731   {"marm7dm",    NULL, &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
12732   {"m7dm",       NULL, &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
12733   {"marm7dmi",   NULL, &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
12734   {"m7dmi",      NULL, &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
12735   {"marm7100",   NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
12736   {"m7100",      NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
12737   {"marm7500",   NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
12738   {"m7500",      NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
12739   {"marm7500fe", NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
12740   {"m7500fe",    NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
12741   {"marm7t",     NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
12742   {"m7t",        NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
12743   {"marm7tdmi",  NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
12744   {"m7tdmi",     NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
12745   {"marm710t",   NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
12746   {"m710t",      NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
12747   {"marm720t",   NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
12748   {"m720t",      NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
12749   {"marm740t",   NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
12750   {"m740t",      NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
12751   {"marm8",      NULL, &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
12752   {"m8",         NULL, &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
12753   {"marm810",    NULL, &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
12754   {"m810",       NULL, &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
12755   {"marm9",      NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
12756   {"m9",         NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
12757   {"marm9tdmi",  NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
12758   {"m9tdmi",     NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
12759   {"marm920",    NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
12760   {"m920",       NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
12761   {"marm940",    NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
12762   {"m940",       NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
12763   {"mstrongarm", NULL, &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
12764   {"mstrongarm110", NULL, &legacy_cpu, ARM_ARCH_V4,
12765    N_("use -mcpu=strongarm110")},
12766   {"mstrongarm1100", NULL, &legacy_cpu, ARM_ARCH_V4,
12767    N_("use -mcpu=strongarm1100")},
12768   {"mstrongarm1110", NULL, &legacy_cpu, ARM_ARCH_V4,
12769    N_("use -mcpu=strongarm1110")},
12770   {"mxscale",    NULL, &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
12771   {"miwmmxt",    NULL, &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
12772   {"mall",       NULL, &legacy_cpu, ARM_ANY,      N_("use -mcpu=all")},
12773
12774   /* Architecture variants -- don't add any more to this list either.  */
12775   {"mv2",        NULL, &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
12776   {"marmv2",     NULL, &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
12777   {"mv2a",       NULL, &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
12778   {"marmv2a",    NULL, &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
12779   {"mv3",        NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
12780   {"marmv3",     NULL, &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
12781   {"mv3m",       NULL, &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
12782   {"marmv3m",    NULL, &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
12783   {"mv4",        NULL, &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
12784   {"marmv4",     NULL, &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
12785   {"mv4t",       NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
12786   {"marmv4t",    NULL, &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
12787   {"mv5",        NULL, &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
12788   {"marmv5",     NULL, &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
12789   {"mv5t",       NULL, &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
12790   {"marmv5t",    NULL, &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
12791   {"mv5e",       NULL, &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
12792   {"marmv5e",    NULL, &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
12793
12794   /* Floating point variants -- don't add any more to this list either.  */
12795   {"mfpe-old", NULL, &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
12796   {"mfpa10",   NULL, &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
12797   {"mfpa11",   NULL, &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
12798   {"mno-fpu",  NULL, &legacy_fpu, 0,
12799    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
12800
12801   {NULL, NULL, NULL, 0, NULL}
12802 };
12803
12804 struct arm_cpu_option_table
12805 {
12806   char *name;
12807   int   value;
12808   /* For some CPUs we assume an FPU unless the user explicitly sets
12809      -mfpu=...  */
12810   int   default_fpu;
12811   /* The canonical name of the CPU, or NULL to use NAME converted to upper
12812      case.  */
12813   const char *canonical_name;
12814 };
12815
12816 /* This list should, at a minimum, contain all the cpu names
12817    recognized by GCC.  */
12818 static struct arm_cpu_option_table arm_cpus[] =
12819 {
12820   {"all",               ARM_ANY,         FPU_ARCH_FPA,    NULL},
12821   {"arm1",              ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL},
12822   {"arm2",              ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL},
12823   {"arm250",            ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
12824   {"arm3",              ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL},
12825   {"arm6",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12826   {"arm60",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12827   {"arm600",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12828   {"arm610",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12829   {"arm620",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12830   {"arm7",              ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12831   {"arm7m",             ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
12832   {"arm7d",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12833   {"arm7dm",            ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
12834   {"arm7di",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12835   {"arm7dmi",           ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL},
12836   {"arm70",             ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12837   {"arm700",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12838   {"arm700i",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12839   {"arm710",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12840   {"arm710t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12841   {"arm720",            ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12842   {"arm720t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12843   {"arm740t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12844   {"arm710c",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12845   {"arm7100",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12846   {"arm7500",           ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12847   {"arm7500fe",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL},
12848   {"arm7t",             ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12849   {"arm7tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12850   {"arm7tdmi-s",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12851   {"arm8",              ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12852   {"arm810",            ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12853   {"strongarm",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12854   {"strongarm1",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12855   {"strongarm110",      ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12856   {"strongarm1100",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12857   {"strongarm1110",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL},
12858   {"arm9",              ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12859   {"arm920",            ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"},
12860   {"arm920t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12861   {"arm922t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12862   {"arm940t",           ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12863   {"arm9tdmi",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL},
12864   /* For V5 or later processors we default to using VFP; but the user
12865      should really set the FPU type explicitly.  */
12866   {"arm9e-r0",          ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
12867   {"arm9e",             ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12868   {"arm926ej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
12869   {"arm926ejs",         ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"},
12870   {"arm926ej-s",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
12871   {"arm946e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
12872   {"arm946e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"},
12873   {"arm946e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12874   {"arm966e-r0",        ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
12875   {"arm966e",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"},
12876   {"arm966e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12877   {"arm968e-s",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12878   {"arm10t",            ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
12879   {"arm10tdmi",         ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
12880   {"arm10e",            ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12881   {"arm1020",           ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"},
12882   {"arm1020t",          ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL},
12883   {"arm1020e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12884   {"arm1022e",          ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL},
12885   {"arm1026ejs",        ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM1026EJ-S"},
12886   {"arm1026ej-s",       ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL},
12887   {"arm1136js",         ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"},
12888   {"arm1136j-s",        ARM_ARCH_V6,     FPU_NONE,        NULL},
12889   {"arm1136jfs",        ARM_ARCH_V6,     FPU_ARCH_VFP_V2, "ARM1136JF-S"},
12890   {"arm1136jf-s",       ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL},
12891   {"mpcore",            ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, NULL},
12892   {"mpcorenovfp",       ARM_ARCH_V6K,    FPU_NONE,        NULL},
12893   {"arm1156t2-s",       ARM_ARCH_V6T2,   FPU_NONE,        NULL},
12894   {"arm1156t2f-s",      ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL},
12895   {"arm1176jz-s",       ARM_ARCH_V6ZK,   FPU_NONE,        NULL},
12896   {"arm1176jzf-s",      ARM_ARCH_V6ZK,   FPU_ARCH_VFP_V2, NULL},
12897   /* ??? XSCALE is really an architecture.  */
12898   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
12899   /* ??? iwmmxt is not a processor.  */
12900   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL},
12901   {"i80200",            ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
12902   /* Maverick */
12903   {"ep9312",    ARM_ARCH_V4T | ARM_CEXT_MAVERICK, FPU_ARCH_MAVERICK, "ARM920T"},
12904   {NULL, 0, 0, NULL}
12905 };
12906
12907 struct arm_arch_option_table
12908 {
12909   char *name;
12910   int   value;
12911   int   default_fpu;
12912 };
12913
12914 /* This list should, at a minimum, contain all the architecture names
12915    recognized by GCC.  */
12916 static struct arm_arch_option_table arm_archs[] =
12917 {
12918   {"all",               ARM_ANY,         FPU_ARCH_FPA},
12919   {"armv1",             ARM_ARCH_V1,     FPU_ARCH_FPA},
12920   {"armv2",             ARM_ARCH_V2,     FPU_ARCH_FPA},
12921   {"armv2a",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
12922   {"armv2s",            ARM_ARCH_V2S,    FPU_ARCH_FPA},
12923   {"armv3",             ARM_ARCH_V3,     FPU_ARCH_FPA},
12924   {"armv3m",            ARM_ARCH_V3M,    FPU_ARCH_FPA},
12925   {"armv4",             ARM_ARCH_V4,     FPU_ARCH_FPA},
12926   {"armv4xm",           ARM_ARCH_V4xM,   FPU_ARCH_FPA},
12927   {"armv4t",            ARM_ARCH_V4T,    FPU_ARCH_FPA},
12928   {"armv4txm",          ARM_ARCH_V4TxM,  FPU_ARCH_FPA},
12929   {"armv5",             ARM_ARCH_V5,     FPU_ARCH_VFP},
12930   {"armv5t",            ARM_ARCH_V5T,    FPU_ARCH_VFP},
12931   {"armv5txm",          ARM_ARCH_V5TxM,  FPU_ARCH_VFP},
12932   {"armv5te",           ARM_ARCH_V5TE,   FPU_ARCH_VFP},
12933   {"armv5texp",         ARM_ARCH_V5TExP, FPU_ARCH_VFP},
12934   {"armv5tej",          ARM_ARCH_V5TEJ,  FPU_ARCH_VFP},
12935   {"armv6",             ARM_ARCH_V6,     FPU_ARCH_VFP},
12936   {"armv6j",            ARM_ARCH_V6,     FPU_ARCH_VFP},
12937   {"armv6k",            ARM_ARCH_V6K,    FPU_ARCH_VFP},
12938   {"armv6z",            ARM_ARCH_V6Z,    FPU_ARCH_VFP},
12939   {"armv6zk",           ARM_ARCH_V6ZK,   FPU_ARCH_VFP},
12940   {"armv6t2",           ARM_ARCH_V6T2,   FPU_ARCH_VFP},
12941   {"armv6kt2",          ARM_ARCH_V6KT2,  FPU_ARCH_VFP},
12942   {"armv6zt2",          ARM_ARCH_V6ZT2,  FPU_ARCH_VFP},
12943   {"armv6zkt2",         ARM_ARCH_V6ZKT2, FPU_ARCH_VFP},
12944   {"xscale",            ARM_ARCH_XSCALE, FPU_ARCH_VFP},
12945   {"iwmmxt",            ARM_ARCH_IWMMXT, FPU_ARCH_VFP},
12946   {NULL, 0, 0}
12947 };
12948
12949 /* ISA extensions in the co-processor space.  */
12950 struct arm_option_value_table
12951 {
12952   char *name;
12953   int value;
12954 };
12955
12956 static struct arm_option_value_table arm_extensions[] =
12957 {
12958   {"maverick",          ARM_CEXT_MAVERICK},
12959   {"xscale",            ARM_CEXT_XSCALE},
12960   {"iwmmxt",            ARM_CEXT_IWMMXT},
12961   {NULL,                0}
12962 };
12963
12964 /* This list should, at a minimum, contain all the fpu names
12965    recognized by GCC.  */
12966 static struct arm_option_value_table arm_fpus[] =
12967 {
12968   {"softfpa",           FPU_NONE},
12969   {"fpe",               FPU_ARCH_FPE},
12970   {"fpe2",              FPU_ARCH_FPE},
12971   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
12972   {"fpa",               FPU_ARCH_FPA},
12973   {"fpa10",             FPU_ARCH_FPA},
12974   {"fpa11",             FPU_ARCH_FPA},
12975   {"arm7500fe",         FPU_ARCH_FPA},
12976   {"softvfp",           FPU_ARCH_VFP},
12977   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
12978   {"vfp",               FPU_ARCH_VFP_V2},
12979   {"vfp9",              FPU_ARCH_VFP_V2},
12980   {"vfp10",             FPU_ARCH_VFP_V2},
12981   {"vfp10-r0",          FPU_ARCH_VFP_V1},
12982   {"vfpxd",             FPU_ARCH_VFP_V1xD},
12983   {"arm1020t",          FPU_ARCH_VFP_V1},
12984   {"arm1020e",          FPU_ARCH_VFP_V2},
12985   {"arm1136jfs",        FPU_ARCH_VFP_V2},
12986   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
12987   {"maverick",          FPU_ARCH_MAVERICK},
12988   {NULL, 0}
12989 };
12990
12991 static struct arm_option_value_table arm_float_abis[] =
12992 {
12993   {"hard",      ARM_FLOAT_ABI_HARD},
12994   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
12995   {"soft",      ARM_FLOAT_ABI_SOFT},
12996   {NULL, 0}
12997 };
12998
12999 #ifdef OBJ_ELF
13000 /* We only know how to output GNU and ver 4 (AAELF) formats.  */
13001 static struct arm_option_value_table arm_eabis[] =
13002 {
13003   {"gnu",       EF_ARM_EABI_UNKNOWN},
13004   {"4",         EF_ARM_EABI_VER4},
13005   {NULL, 0}
13006 };
13007 #endif
13008
13009 struct arm_long_option_table
13010 {
13011   char * option;                /* Substring to match.  */
13012   char * help;                  /* Help information.  */
13013   int (* func) (char * subopt); /* Function to decode sub-option.  */
13014   char * deprecated;            /* If non-null, print this message.  */
13015 };
13016
13017 static int
13018 arm_parse_extension (char * str, int * opt_p)
13019 {
13020   while (str != NULL && *str != 0)
13021     {
13022       struct arm_option_value_table * opt;
13023       char * ext;
13024       int optlen;
13025
13026       if (*str != '+')
13027         {
13028           as_bad (_("invalid architectural extension"));
13029           return 0;
13030         }
13031
13032       str++;
13033       ext = strchr (str, '+');
13034
13035       if (ext != NULL)
13036         optlen = ext - str;
13037       else
13038         optlen = strlen (str);
13039
13040       if (optlen == 0)
13041         {
13042           as_bad (_("missing architectural extension"));
13043           return 0;
13044         }
13045
13046       for (opt = arm_extensions; opt->name != NULL; opt++)
13047         if (strncmp (opt->name, str, optlen) == 0)
13048           {
13049             *opt_p |= opt->value;
13050             break;
13051           }
13052
13053       if (opt->name == NULL)
13054         {
13055           as_bad (_("unknown architectural extnsion `%s'"), str);
13056           return 0;
13057         }
13058
13059       str = ext;
13060     };
13061
13062   return 1;
13063 }
13064
13065 static int
13066 arm_parse_cpu (char * str)
13067 {
13068   struct arm_cpu_option_table * opt;
13069   char * ext = strchr (str, '+');
13070   int optlen;
13071
13072   if (ext != NULL)
13073     optlen = ext - str;
13074   else
13075     optlen = strlen (str);
13076
13077   if (optlen == 0)
13078     {
13079       as_bad (_("missing cpu name `%s'"), str);
13080       return 0;
13081     }
13082
13083   for (opt = arm_cpus; opt->name != NULL; opt++)
13084     if (strncmp (opt->name, str, optlen) == 0)
13085       {
13086         mcpu_cpu_opt = opt->value;
13087         mcpu_fpu_opt = opt->default_fpu;
13088         if (opt->canonical_name)
13089           strcpy(selected_cpu_name, opt->canonical_name);
13090         else
13091           {
13092             int i;
13093             for (i = 0; i < optlen; i++)
13094               selected_cpu_name[i] = TOUPPER (opt->name[i]);
13095             selected_cpu_name[i] = 0;
13096           }
13097
13098         if (ext != NULL)
13099           return arm_parse_extension (ext, &mcpu_cpu_opt);
13100
13101         return 1;
13102       }
13103
13104   as_bad (_("unknown cpu `%s'"), str);
13105   return 0;
13106 }
13107
13108 static int
13109 arm_parse_arch (char * str)
13110 {
13111   struct arm_arch_option_table *opt;
13112   char *ext = strchr (str, '+');
13113   int optlen;
13114
13115   if (ext != NULL)
13116     optlen = ext - str;
13117   else
13118     optlen = strlen (str);
13119
13120   if (optlen == 0)
13121     {
13122       as_bad (_("missing architecture name `%s'"), str);
13123       return 0;
13124     }
13125
13126   for (opt = arm_archs; opt->name != NULL; opt++)
13127     if (streq (opt->name, str))
13128       {
13129         march_cpu_opt = opt->value;
13130         march_fpu_opt = opt->default_fpu;
13131         strcpy(selected_cpu_name, opt->name);
13132
13133         if (ext != NULL)
13134           return arm_parse_extension (ext, &march_cpu_opt);
13135
13136         return 1;
13137       }
13138
13139   as_bad (_("unknown architecture `%s'\n"), str);
13140   return 0;
13141 }
13142
13143 static int
13144 arm_parse_fpu (char * str)
13145 {
13146   struct arm_option_value_table * opt;
13147
13148   for (opt = arm_fpus; opt->name != NULL; opt++)
13149     if (streq (opt->name, str))
13150       {
13151         mfpu_opt = opt->value;
13152         return 1;
13153       }
13154
13155   as_bad (_("unknown floating point format `%s'\n"), str);
13156   return 0;
13157 }
13158
13159 static int
13160 arm_parse_float_abi (char * str)
13161 {
13162   struct arm_option_value_table * opt;
13163
13164   for (opt = arm_float_abis; opt->name != NULL; opt++)
13165     if (streq (opt->name, str))
13166       {
13167         mfloat_abi_opt = opt->value;
13168         return 1;
13169       }
13170
13171   as_bad (_("unknown floating point abi `%s'\n"), str);
13172   return 0;
13173 }
13174
13175 #ifdef OBJ_ELF
13176 static int
13177 arm_parse_eabi (char * str)
13178 {
13179   struct arm_option_value_table *opt;
13180
13181   for (opt = arm_eabis; opt->name != NULL; opt++)
13182     if (streq (opt->name, str))
13183       {
13184         meabi_flags = opt->value;
13185         return 1;
13186       }
13187   as_bad (_("unknown EABI `%s'\n"), str);
13188   return 0;
13189 }
13190 #endif
13191
13192 struct arm_long_option_table arm_long_opts[] =
13193 {
13194   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
13195    arm_parse_cpu, NULL},
13196   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
13197    arm_parse_arch, NULL},
13198   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
13199    arm_parse_fpu, NULL},
13200   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
13201    arm_parse_float_abi, NULL},
13202 #ifdef OBJ_ELF
13203   {"meabi=", N_("<ver>\t  assemble for eabi version <ver>"),
13204    arm_parse_eabi, NULL},
13205 #endif
13206   {NULL, NULL, 0, NULL}
13207 };
13208
13209 int
13210 md_parse_option (int c, char * arg)
13211 {
13212   struct arm_option_table *opt;
13213   struct arm_long_option_table *lopt;
13214
13215   switch (c)
13216     {
13217 #ifdef OPTION_EB
13218     case OPTION_EB:
13219       target_big_endian = 1;
13220       break;
13221 #endif
13222
13223 #ifdef OPTION_EL
13224     case OPTION_EL:
13225       target_big_endian = 0;
13226       break;
13227 #endif
13228
13229     case 'a':
13230       /* Listing option.  Just ignore these, we don't support additional
13231          ones.  */
13232       return 0;
13233
13234     default:
13235       for (opt = arm_opts; opt->option != NULL; opt++)
13236         {
13237           if (c == opt->option[0]
13238               && ((arg == NULL && opt->option[1] == 0)
13239                   || streq (arg, opt->option + 1)))
13240             {
13241 #if WARN_DEPRECATED
13242               /* If the option is deprecated, tell the user.  */
13243               if (opt->deprecated != NULL)
13244                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
13245                            arg ? arg : "", _(opt->deprecated));
13246 #endif
13247
13248               if (opt->var != NULL)
13249                 *opt->var = opt->value;
13250
13251               return 1;
13252             }
13253         }
13254
13255       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
13256         {
13257           /* These options are expected to have an argument.  */
13258           if (c == lopt->option[0]
13259               && arg != NULL
13260               && strncmp (arg, lopt->option + 1,
13261                           strlen (lopt->option + 1)) == 0)
13262             {
13263 #if WARN_DEPRECATED
13264               /* If the option is deprecated, tell the user.  */
13265               if (lopt->deprecated != NULL)
13266                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
13267                            _(lopt->deprecated));
13268 #endif
13269
13270               /* Call the sup-option parser.  */
13271               return lopt->func (arg + strlen (lopt->option) - 1);
13272             }
13273         }
13274
13275       return 0;
13276     }
13277
13278   return 1;
13279 }
13280
13281 void
13282 md_show_usage (FILE * fp)
13283 {
13284   struct arm_option_table *opt;
13285   struct arm_long_option_table *lopt;
13286
13287   fprintf (fp, _(" ARM-specific assembler options:\n"));
13288
13289   for (opt = arm_opts; opt->option != NULL; opt++)
13290     if (opt->help != NULL)
13291       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
13292
13293   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
13294     if (lopt->help != NULL)
13295       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
13296
13297 #ifdef OPTION_EB
13298   fprintf (fp, _("\
13299   -EB                     assemble code for a big-endian cpu\n"));
13300 #endif
13301
13302 #ifdef OPTION_EL
13303   fprintf (fp, _("\
13304   -EL                     assemble code for a little-endian cpu\n"));
13305 #endif
13306 }
13307
13308
13309 #ifdef OBJ_ELF
13310 /* Set the public EABI object attributes.  */
13311 static void
13312 aeabi_set_public_attributes (void)
13313 {
13314   int arch;
13315   int flags;
13316
13317   /* Choose the architecture based on the capabilities of the requested cpu
13318      (if any) and/or the instructions actually used.  */
13319   flags = selected_cpu | mfpu_opt | arm_arch_used | thumb_arch_used;
13320   if (flags & ARM_EXT_V6T2)
13321     arch = 8;
13322   else if (flags & ARM_EXT_V6Z)
13323     arch = 7;
13324   else if (flags & ARM_EXT_V6K)
13325     arch = 9;
13326   else if (flags & ARM_EXT_V6)
13327     arch = 6;
13328   else if (flags & ARM_EXT_V5E)
13329     arch = 4;
13330   else if (flags & (ARM_EXT_V5 | ARM_EXT_V5T))
13331     arch = 3;
13332   else if (flags & ARM_EXT_V4T)
13333     arch = 2;
13334   else if (flags & ARM_EXT_V4)
13335     arch = 1;
13336   else
13337     arch = 0;
13338
13339   /* Tag_CPU_name.  */
13340   if (selected_cpu_name[0])
13341     {
13342       char *p;
13343
13344       p = selected_cpu_name;
13345       if (strncmp(p, "armv", 4) == 0)
13346         {
13347           int i;
13348           
13349           p += 4;
13350           for (i = 0; p[i]; i++)
13351             p[i] = TOUPPER (p[i]);
13352         }
13353       elf32_arm_add_eabi_attr_string (stdoutput, 5, p);
13354     }
13355   /* Tag_CPU_arch.  */
13356   elf32_arm_add_eabi_attr_int (stdoutput, 6, arch);
13357   /* Tag_ARM_ISA_use.  */
13358   if (arm_arch_used)
13359     elf32_arm_add_eabi_attr_int (stdoutput, 8, 1);
13360   /* Tag_THUMB_ISA_use.  */
13361   if (thumb_arch_used)
13362     elf32_arm_add_eabi_attr_int (stdoutput, 9,
13363                                  (thumb_arch_used & ARM_EXT_V6T2) ? 2 : 1);
13364   /* Tag_VFP_arch.  */
13365   if ((arm_arch_used | thumb_arch_used) & FPU_ARCH_VFP_V2)
13366     elf32_arm_add_eabi_attr_int (stdoutput, 10, 2);
13367   else if ((arm_arch_used | thumb_arch_used) & FPU_ARCH_VFP_V1)
13368     elf32_arm_add_eabi_attr_int (stdoutput, 10, 1);
13369   /* Tag_WMMX_arch.  */
13370   if ((arm_arch_used | thumb_arch_used) & ARM_CEXT_IWMMXT)
13371     elf32_arm_add_eabi_attr_int (stdoutput, 11, 1);
13372 }
13373
13374 /* Add the .ARM.attributes section.  */
13375 void
13376 arm_md_end (void)
13377 {
13378   segT s;
13379   char *p;
13380   addressT addr;
13381   offsetT size;
13382   
13383   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
13384     return;
13385
13386   aeabi_set_public_attributes ();
13387   size = elf32_arm_eabi_attr_size (stdoutput);
13388   s = subseg_new (".ARM.attributes", 0);
13389   bfd_set_section_flags (stdoutput, s, SEC_READONLY | SEC_DATA);
13390   addr = frag_now_fix ();
13391   p = frag_more (size);
13392   elf32_arm_set_eabi_attr_contents (stdoutput, (bfd_byte *)p, size);
13393 }
13394
13395
13396 /* Parse a .cpu directive.  */
13397
13398 static void
13399 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
13400 {
13401   struct arm_cpu_option_table *opt;
13402   char *name;
13403   char saved_char;
13404
13405   name = input_line_pointer;
13406   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
13407     input_line_pointer++;
13408   saved_char = *input_line_pointer;
13409   *input_line_pointer = 0;
13410
13411   /* Skip the first "all" entry.  */
13412   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
13413     if (streq (opt->name, name))
13414       {
13415         mcpu_cpu_opt = opt->value;
13416         selected_cpu = mcpu_cpu_opt;
13417         if (opt->canonical_name)
13418           strcpy(selected_cpu_name, opt->canonical_name);
13419         else
13420           {
13421             int i;
13422             for (i = 0; opt->name[i]; i++)
13423               selected_cpu_name[i] = TOUPPER (opt->name[i]);
13424             selected_cpu_name[i] = 0;
13425           }
13426         cpu_variant = mcpu_cpu_opt | mfpu_opt;
13427         *input_line_pointer = saved_char;
13428         demand_empty_rest_of_line ();
13429         return;
13430       }
13431   as_bad (_("unknown cpu `%s'"), name);
13432   *input_line_pointer = saved_char;
13433   ignore_rest_of_line ();
13434 }
13435
13436
13437 /* Parse a .arch directive.  */
13438
13439 static void
13440 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
13441 {
13442   struct arm_arch_option_table *opt;
13443   char saved_char;
13444   char *name;
13445
13446   name = input_line_pointer;
13447   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
13448     input_line_pointer++;
13449   saved_char = *input_line_pointer;
13450   *input_line_pointer = 0;
13451
13452   /* Skip the first "all" entry.  */
13453   for (opt = arm_archs + 1; opt->name != NULL; opt++)
13454     if (streq (opt->name, name))
13455       {
13456         mcpu_cpu_opt = opt->value;
13457         selected_cpu = mcpu_cpu_opt;
13458         strcpy(selected_cpu_name, opt->name);
13459         cpu_variant = mcpu_cpu_opt | mfpu_opt;
13460         *input_line_pointer = saved_char;
13461         demand_empty_rest_of_line ();
13462         return;
13463       }
13464
13465   as_bad (_("unknown architecture `%s'\n"), name);
13466   *input_line_pointer = saved_char;
13467   ignore_rest_of_line ();
13468 }
13469
13470
13471 /* Parse a .fpu directive.  */
13472
13473 static void
13474 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
13475 {
13476   struct arm_option_value_table *opt;
13477   char saved_char;
13478   char *name;
13479
13480   name = input_line_pointer;
13481   while (*input_line_pointer && !ISSPACE(*input_line_pointer))
13482     input_line_pointer++;
13483   saved_char = *input_line_pointer;
13484   *input_line_pointer = 0;
13485   
13486   for (opt = arm_fpus; opt->name != NULL; opt++)
13487     if (streq (opt->name, name))
13488       {
13489         mfpu_opt = opt->value;
13490         cpu_variant = mcpu_cpu_opt | mfpu_opt;
13491         *input_line_pointer = saved_char;
13492         demand_empty_rest_of_line ();
13493         return;
13494       }
13495
13496   as_bad (_("unknown floating point format `%s'\n"), name);
13497   *input_line_pointer = saved_char;
13498   ignore_rest_of_line ();
13499 }
13500 #endif /* OBJ_ELF */
13501